blob: 40f993ff84047e6f48eab6f7255e83ad6e959b7c [file] [log] [blame]
Paul Bakker367dae42009-06-28 21:50:27 +00001#!/usr/bin/perl
2#
3
4use strict;
5
6my $suite_dir = shift or die "Missing suite directory";
7my $suite_name = shift or die "Missing suite name";
Paul Bakker46c17942011-07-13 14:54:54 +00008my $data_name = shift or die "Missing data name";
9my $test_file = $data_name.".c";
Paul Bakker367dae42009-06-28 21:50:27 +000010my $test_helper_file = $suite_dir."/helpers.function";
11my $test_case_file = $suite_dir."/".$suite_name.".function";
Paul Bakker19343182013-08-16 13:31:10 +020012my $test_case_data = $suite_dir."/".$data_name.".data";
13my $test_main_file = $suite_dir."/main_test.function";
Paul Bakker367dae42009-06-28 21:50:27 +000014
15my $line_separator = $/;
16undef $/;
17
18open(TEST_HELPERS, "$test_helper_file") or die "Opening test helpers '$test_helper_file': $!";
19my $test_helpers = <TEST_HELPERS>;
20close(TEST_HELPERS);
21
Paul Bakker19343182013-08-16 13:31:10 +020022open(TEST_MAIN, "$test_main_file") or die "Opening test main '$test_main_file': $!";
23my $test_main = <TEST_MAIN>;
24close(TEST_MAIN);
25
Paul Bakker367dae42009-06-28 21:50:27 +000026open(TEST_CASES, "$test_case_file") or die "Opening test cases '$test_case_file': $!";
27my $test_cases = <TEST_CASES>;
28close(TEST_CASES);
Paul Bakker19343182013-08-16 13:31:10 +020029
30open(TEST_DATA, "$test_case_data") or die "Opening test data '$test_case_data': $!";
31my $test_data = <TEST_DATA>;
32close(TEST_DATA);
33
Paul Bakker33b43f12013-08-20 11:48:36 +020034my ( $suite_header ) = $test_cases =~ /\/\* BEGIN_HEADER \*\/\n(.*?)\n\/\* END_HEADER \*\//s;
35my ( $suite_defines ) = $test_cases =~ /\/\* BEGIN_DEPENDENCIES\n \* (.*?)\n \* END_DEPENDENCIES/s;
Paul Bakker5690efc2011-05-26 13:16:06 +000036
37my $requirements;
38if ($suite_defines =~ /^depends_on:/)
39{
40 ( $requirements ) = $suite_defines =~ /^depends_on:(.*)$/;
41}
Paul Bakker19343182013-08-16 13:31:10 +020042
Paul Bakker5690efc2011-05-26 13:16:06 +000043my @var_req_arr = split(/:/, $requirements);
44my $suite_pre_code;
45my $suite_post_code;
Paul Bakker19343182013-08-16 13:31:10 +020046my $dispatch_code;
47my $mapping_code;
48my %mapping_values;
Paul Bakker5690efc2011-05-26 13:16:06 +000049
50while (@var_req_arr)
51{
52 my $req = shift @var_req_arr;
53
54 $suite_pre_code .= "#ifdef $req\n";
55 $suite_post_code .= "#endif /* $req */\n";
56}
Paul Bakker367dae42009-06-28 21:50:27 +000057
58$/ = $line_separator;
59
60open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!";
61print TEST_FILE << "END";
Paul Bakker28837ff2013-06-24 19:17:50 +020062#include <polarssl/config.h>
Paul Bakker5690efc2011-05-26 13:16:06 +000063
Paul Bakker367dae42009-06-28 21:50:27 +000064$suite_header
65
66$test_helpers
67
Paul Bakker367dae42009-06-28 21:50:27 +000068END
69
Paul Bakkerb34fef22013-08-20 12:06:33 +020070$test_main =~ s/SUITE_PRE_DEP/$suite_pre_code/;
71$test_main =~ s/SUITE_POST_DEP/$suite_post_code/;
72
Paul Bakker33b43f12013-08-20 11:48:36 +020073while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg)
Paul Bakker367dae42009-06-28 21:50:27 +000074{
Paul Bakker19343182013-08-16 13:31:10 +020075 my $function_deps = $1;
Paul Bakker33b43f12013-08-20 11:48:36 +020076 my $function_decl = $2;
77
78 # Sanity checks of function
79 if ($function_decl !~ /^void /)
80 {
81 die "Test function does not have 'void' as return type\n";
82 }
83 if ($function_decl !~ /^void (\w+)\(\s*(.*?)\s*\)\s*{(.*?)}/ms)
84 {
85 die "Function declaration not in expected format\n";
86 }
87 my $function_name = $1;
88 my $function_params = $2;
Paul Bakker19343182013-08-16 13:31:10 +020089 my $function_pre_code;
90 my $function_post_code;
Paul Bakker19343182013-08-16 13:31:10 +020091 my $param_defs;
92 my $param_checks;
93 my @dispatch_params;
Paul Bakker33b43f12013-08-20 11:48:36 +020094 my @var_def_arr = split(/,\s*/, $function_params);
Paul Bakker19343182013-08-16 13:31:10 +020095 my $i = 1;
96 my $mapping_regex = "".$function_name;
97 my $mapping_count = 0;
Paul Bakker367dae42009-06-28 21:50:27 +000098
Paul Bakker33b43f12013-08-20 11:48:36 +020099 $function_decl =~ s/^void /void test_suite_/;
100
Paul Bakker19343182013-08-16 13:31:10 +0200101 if ($function_deps =~ /^depends_on:/)
Paul Bakkerccff1672009-10-03 19:57:10 +0000102 {
Paul Bakker19343182013-08-16 13:31:10 +0200103 ( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/;
Paul Bakkerccff1672009-10-03 19:57:10 +0000104 }
105
Paul Bakker19343182013-08-16 13:31:10 +0200106 foreach my $req (split(/:/, $function_deps))
Paul Bakker367dae42009-06-28 21:50:27 +0000107 {
Paul Bakker19343182013-08-16 13:31:10 +0200108 $function_pre_code .= "#ifdef $req\n";
109 $function_post_code .= "#endif /* $req */\n";
Paul Bakker367dae42009-06-28 21:50:27 +0000110 }
Paul Bakker367dae42009-06-28 21:50:27 +0000111
Paul Bakker19343182013-08-16 13:31:10 +0200112 foreach my $def (@var_def_arr)
113 {
114 # Handle the different parameter types
Paul Bakker33b43f12013-08-20 11:48:36 +0200115 if( substr($def, 0, 4) eq "int " )
Paul Bakker19343182013-08-16 13:31:10 +0200116 {
117 $param_defs .= " int param$i;\n";
118 $param_checks .= " if( verify_int( params[$i], &param$i ) != 0 ) return( 2 );\n";
119 push @dispatch_params, "param$i";
Paul Bakker367dae42009-06-28 21:50:27 +0000120
Paul Bakker19343182013-08-16 13:31:10 +0200121 $mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)";
122 $mapping_count++;
123 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200124 elsif( substr($def, 0, 6) eq "char *" )
Paul Bakker19343182013-08-16 13:31:10 +0200125 {
126 $param_defs .= " char *param$i = params[$i];\n";
127 $param_checks .= " if( verify_string( &param$i ) != 0 ) return( 2 );\n";
128 push @dispatch_params, "param$i";
Paul Bakker19343182013-08-16 13:31:10 +0200129 $mapping_regex .= ":[^:]+";
130 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200131 else
132 {
133 die "Parameter declaration not of supported type (int, char *)\n";
134 }
Paul Bakker19343182013-08-16 13:31:10 +0200135 $i++;
Paul Bakker367dae42009-06-28 21:50:27 +0000136
Paul Bakker19343182013-08-16 13:31:10 +0200137 }
138
139 # Find non-integer values we should map for this function
140 if( $mapping_count)
141 {
142 my @res = $test_data =~ /^$mapping_regex/msg;
143 foreach my $value (@res)
144 {
145 $mapping_values{$value} = 1 if ($value !~ /^\d+$/);
146 }
147 }
148
149 my $call_params = join ", ", @dispatch_params;
150 my $param_count = @var_def_arr + 1;
151 $dispatch_code .= << "END";
152if( strcmp( params[0], "$function_name" ) == 0 )
153{
154$function_pre_code
155$param_defs
156 if( cnt != $param_count )
157 {
158 fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count );
159 return( 2 );
160 }
161
162$param_checks
Paul Bakker33b43f12013-08-20 11:48:36 +0200163 test_suite_$function_name( $call_params );
164 return ( 0 );
Paul Bakker19343182013-08-16 13:31:10 +0200165$function_post_code
166 return ( 3 );
167}
168else
169END
170
Paul Bakker33b43f12013-08-20 11:48:36 +0200171 my $function_code = $function_pre_code . $function_decl . "\n" . $function_post_code;
172 $test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/;
Paul Bakker19343182013-08-16 13:31:10 +0200173}
174
175# Find specific case dependencies that we should be able to check
176# and make check code
177my $dep_check_code;
178
179my @res = $test_data =~ /^depends_on:([\w:]+)/msg;
180my %case_deps;
181foreach my $deps (@res)
182{
183 foreach my $dep (split(/:/, $deps))
184 {
185 $case_deps{$dep} = 1;
186 }
187}
188while( my ($key, $value) = each(%case_deps) )
189{
190 $dep_check_code .= << "END";
191 if( strcmp( str, "$key" ) == 0 )
192 {
193#if defined($key)
194 return( 0 );
195#else
196 return( 1 );
197#endif
198 }
Paul Bakker367dae42009-06-28 21:50:27 +0000199END
200}
201
Paul Bakker19343182013-08-16 13:31:10 +0200202# Make mapping code
203while( my ($key, $value) = each(%mapping_values) )
204{
205 $mapping_code .= << "END";
206 if( strcmp( str, "$key" ) == 0 )
207 {
208 *value = ( $key );
209 return( 0 );
210 }
211END
212}
213
214$dispatch_code =~ s/^(.+)/ $1/mg;
215
216$test_main =~ s/TEST_FILENAME/$test_case_data/;
217$test_main =~ s/FUNCTION_CODE//;
218$test_main =~ s/DEP_CHECK_CODE/$dep_check_code/;
219$test_main =~ s/DISPATCH_FUNCTION/$dispatch_code/;
220$test_main =~ s/MAPPING_CODE/$mapping_code/;
221
Paul Bakker367dae42009-06-28 21:50:27 +0000222print TEST_FILE << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200223$test_main
Paul Bakker367dae42009-06-28 21:50:27 +0000224END
225
Paul Bakker367dae42009-06-28 21:50:27 +0000226close(TEST_FILE);