blob: d1676053e006d5672df8889f0e763dac23242adc [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 Bakker5690efc2011-05-26 13:16:06 +000068$suite_pre_code
69
Paul Bakker367dae42009-06-28 21:50:27 +000070END
71
Paul Bakker33b43f12013-08-20 11:48:36 +020072while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg)
Paul Bakker367dae42009-06-28 21:50:27 +000073{
Paul Bakker19343182013-08-16 13:31:10 +020074 my $function_deps = $1;
Paul Bakker33b43f12013-08-20 11:48:36 +020075 my $function_decl = $2;
76
77 # Sanity checks of function
78 if ($function_decl !~ /^void /)
79 {
80 die "Test function does not have 'void' as return type\n";
81 }
82 if ($function_decl !~ /^void (\w+)\(\s*(.*?)\s*\)\s*{(.*?)}/ms)
83 {
84 die "Function declaration not in expected format\n";
85 }
86 my $function_name = $1;
87 my $function_params = $2;
Paul Bakker19343182013-08-16 13:31:10 +020088 my $function_pre_code;
89 my $function_post_code;
Paul Bakker19343182013-08-16 13:31:10 +020090 my $param_defs;
91 my $param_checks;
92 my @dispatch_params;
Paul Bakker33b43f12013-08-20 11:48:36 +020093 my @var_def_arr = split(/,\s*/, $function_params);
Paul Bakker19343182013-08-16 13:31:10 +020094 my $i = 1;
95 my $mapping_regex = "".$function_name;
96 my $mapping_count = 0;
Paul Bakker367dae42009-06-28 21:50:27 +000097
Paul Bakker33b43f12013-08-20 11:48:36 +020098 $function_decl =~ s/^void /void test_suite_/;
99
Paul Bakker19343182013-08-16 13:31:10 +0200100 if ($function_deps =~ /^depends_on:/)
Paul Bakkerccff1672009-10-03 19:57:10 +0000101 {
Paul Bakker19343182013-08-16 13:31:10 +0200102 ( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/;
Paul Bakkerccff1672009-10-03 19:57:10 +0000103 }
104
Paul Bakker19343182013-08-16 13:31:10 +0200105 foreach my $req (split(/:/, $function_deps))
Paul Bakker367dae42009-06-28 21:50:27 +0000106 {
Paul Bakker19343182013-08-16 13:31:10 +0200107 $function_pre_code .= "#ifdef $req\n";
108 $function_post_code .= "#endif /* $req */\n";
Paul Bakker367dae42009-06-28 21:50:27 +0000109 }
Paul Bakker367dae42009-06-28 21:50:27 +0000110
Paul Bakker19343182013-08-16 13:31:10 +0200111 foreach my $def (@var_def_arr)
112 {
113 # Handle the different parameter types
Paul Bakker33b43f12013-08-20 11:48:36 +0200114 if( substr($def, 0, 4) eq "int " )
Paul Bakker19343182013-08-16 13:31:10 +0200115 {
116 $param_defs .= " int param$i;\n";
117 $param_checks .= " if( verify_int( params[$i], &param$i ) != 0 ) return( 2 );\n";
118 push @dispatch_params, "param$i";
Paul Bakker367dae42009-06-28 21:50:27 +0000119
Paul Bakker19343182013-08-16 13:31:10 +0200120 $mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)";
121 $mapping_count++;
122 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200123 elsif( substr($def, 0, 6) eq "char *" )
Paul Bakker19343182013-08-16 13:31:10 +0200124 {
125 $param_defs .= " char *param$i = params[$i];\n";
126 $param_checks .= " if( verify_string( &param$i ) != 0 ) return( 2 );\n";
127 push @dispatch_params, "param$i";
Paul Bakker19343182013-08-16 13:31:10 +0200128 $mapping_regex .= ":[^:]+";
129 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200130 else
131 {
132 die "Parameter declaration not of supported type (int, char *)\n";
133 }
Paul Bakker19343182013-08-16 13:31:10 +0200134 $i++;
Paul Bakker367dae42009-06-28 21:50:27 +0000135
Paul Bakker19343182013-08-16 13:31:10 +0200136 }
137
138 # Find non-integer values we should map for this function
139 if( $mapping_count)
140 {
141 my @res = $test_data =~ /^$mapping_regex/msg;
142 foreach my $value (@res)
143 {
144 $mapping_values{$value} = 1 if ($value !~ /^\d+$/);
145 }
146 }
147
148 my $call_params = join ", ", @dispatch_params;
149 my $param_count = @var_def_arr + 1;
150 $dispatch_code .= << "END";
151if( strcmp( params[0], "$function_name" ) == 0 )
152{
153$function_pre_code
154$param_defs
155 if( cnt != $param_count )
156 {
157 fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count );
158 return( 2 );
159 }
160
161$param_checks
Paul Bakker33b43f12013-08-20 11:48:36 +0200162 test_suite_$function_name( $call_params );
163 return ( 0 );
Paul Bakker19343182013-08-16 13:31:10 +0200164$function_post_code
165 return ( 3 );
166}
167else
168END
169
Paul Bakker33b43f12013-08-20 11:48:36 +0200170 my $function_code = $function_pre_code . $function_decl . "\n" . $function_post_code;
171 $test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/;
Paul Bakker19343182013-08-16 13:31:10 +0200172}
173
174# Find specific case dependencies that we should be able to check
175# and make check code
176my $dep_check_code;
177
178my @res = $test_data =~ /^depends_on:([\w:]+)/msg;
179my %case_deps;
180foreach my $deps (@res)
181{
182 foreach my $dep (split(/:/, $deps))
183 {
184 $case_deps{$dep} = 1;
185 }
186}
187while( my ($key, $value) = each(%case_deps) )
188{
189 $dep_check_code .= << "END";
190 if( strcmp( str, "$key" ) == 0 )
191 {
192#if defined($key)
193 return( 0 );
194#else
195 return( 1 );
196#endif
197 }
Paul Bakker367dae42009-06-28 21:50:27 +0000198END
199}
200
Paul Bakker19343182013-08-16 13:31:10 +0200201# Make mapping code
202while( my ($key, $value) = each(%mapping_values) )
203{
204 $mapping_code .= << "END";
205 if( strcmp( str, "$key" ) == 0 )
206 {
207 *value = ( $key );
208 return( 0 );
209 }
210END
211}
212
213$dispatch_code =~ s/^(.+)/ $1/mg;
214
215$test_main =~ s/TEST_FILENAME/$test_case_data/;
216$test_main =~ s/FUNCTION_CODE//;
217$test_main =~ s/DEP_CHECK_CODE/$dep_check_code/;
218$test_main =~ s/DISPATCH_FUNCTION/$dispatch_code/;
219$test_main =~ s/MAPPING_CODE/$mapping_code/;
220
Paul Bakker367dae42009-06-28 21:50:27 +0000221print TEST_FILE << "END";
Paul Bakker5690efc2011-05-26 13:16:06 +0000222$suite_post_code
Paul Bakker5690efc2011-05-26 13:16:06 +0000223
Paul Bakker19343182013-08-16 13:31:10 +0200224$test_main
Paul Bakker367dae42009-06-28 21:50:27 +0000225END
226
Paul Bakker367dae42009-06-28 21:50:27 +0000227close(TEST_FILE);