blob: 1c7a281d7a1b628f7cf0602e7c979377712cac34 [file] [log] [blame]
Markus Pfeiffera26a0052014-04-22 20:16:15 +00001#!/usr/bin/env perl
Paul Bakker367dae42009-06-28 21:50:27 +00002#
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";
Rich Evansf4253c72015-01-14 19:23:00 +00009my $test_main_file = do { my $arg = shift; defined($arg) ? $arg : $suite_dir."/main_test.function" };
Paul Bakker46c17942011-07-13 14:54:54 +000010my $test_file = $data_name.".c";
Paul Bakker367dae42009-06-28 21:50:27 +000011my $test_helper_file = $suite_dir."/helpers.function";
12my $test_case_file = $suite_dir."/".$suite_name.".function";
Paul Bakker19343182013-08-16 13:31:10 +020013my $test_case_data = $suite_dir."/".$data_name.".data";
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;
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +010053 $req =~ s/(!?)(.*)/$1defined($2)/;
Paul Bakker5690efc2011-05-26 13:16:06 +000054
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +010055 $suite_pre_code .= "#if $req\n";
Paul Bakker5690efc2011-05-26 13:16:06 +000056 $suite_post_code .= "#endif /* $req */\n";
57}
Paul Bakker367dae42009-06-28 21:50:27 +000058
59$/ = $line_separator;
60
61open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!";
62print TEST_FILE << "END";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include <mbedtls/config.h>
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020065#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020067#endif
Paul Bakker5690efc2011-05-26 13:16:06 +000068
Rich Evans00ab4702015-02-06 13:43:58 +000069$test_helpers
70
Paul Bakkerde56ca12013-09-15 17:05:21 +020071$suite_pre_code
Paul Bakker367dae42009-06-28 21:50:27 +000072$suite_header
Paul Bakkerde56ca12013-09-15 17:05:21 +020073$suite_post_code
Paul Bakker367dae42009-06-28 21:50:27 +000074
Paul Bakker367dae42009-06-28 21:50:27 +000075END
76
Paul Bakkerb34fef22013-08-20 12:06:33 +020077$test_main =~ s/SUITE_PRE_DEP/$suite_pre_code/;
78$test_main =~ s/SUITE_POST_DEP/$suite_post_code/;
79
Paul Bakker33b43f12013-08-20 11:48:36 +020080while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg)
Paul Bakker367dae42009-06-28 21:50:27 +000081{
Paul Bakker19343182013-08-16 13:31:10 +020082 my $function_deps = $1;
Paul Bakker33b43f12013-08-20 11:48:36 +020083 my $function_decl = $2;
84
85 # Sanity checks of function
86 if ($function_decl !~ /^void /)
87 {
88 die "Test function does not have 'void' as return type\n";
89 }
Paul Bakker318d0fe2014-07-10 14:59:25 +020090 if ($function_decl !~ /^void (\w+)\(\s*(.*?)\s*\)\s*{(.*)}/ms)
Paul Bakker33b43f12013-08-20 11:48:36 +020091 {
92 die "Function declaration not in expected format\n";
93 }
94 my $function_name = $1;
95 my $function_params = $2;
Paul Bakker19343182013-08-16 13:31:10 +020096 my $function_pre_code;
97 my $function_post_code;
Paul Bakker19343182013-08-16 13:31:10 +020098 my $param_defs;
99 my $param_checks;
100 my @dispatch_params;
Paul Bakker33b43f12013-08-20 11:48:36 +0200101 my @var_def_arr = split(/,\s*/, $function_params);
Paul Bakker19343182013-08-16 13:31:10 +0200102 my $i = 1;
103 my $mapping_regex = "".$function_name;
104 my $mapping_count = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000105
Paul Bakker33b43f12013-08-20 11:48:36 +0200106 $function_decl =~ s/^void /void test_suite_/;
107
Paul Bakker318d0fe2014-07-10 14:59:25 +0200108 # Add exit label if not present
109 if ($function_decl !~ /^exit:$/m)
110 {
111 $function_decl =~ s/}\s*$/\nexit:\n return;\n}/;
112 }
113
Paul Bakker19343182013-08-16 13:31:10 +0200114 if ($function_deps =~ /^depends_on:/)
Paul Bakkerccff1672009-10-03 19:57:10 +0000115 {
Paul Bakker19343182013-08-16 13:31:10 +0200116 ( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/;
Paul Bakkerccff1672009-10-03 19:57:10 +0000117 }
118
Paul Bakker19343182013-08-16 13:31:10 +0200119 foreach my $req (split(/:/, $function_deps))
Paul Bakker367dae42009-06-28 21:50:27 +0000120 {
Paul Bakker19343182013-08-16 13:31:10 +0200121 $function_pre_code .= "#ifdef $req\n";
122 $function_post_code .= "#endif /* $req */\n";
Paul Bakker367dae42009-06-28 21:50:27 +0000123 }
Paul Bakker367dae42009-06-28 21:50:27 +0000124
Paul Bakker19343182013-08-16 13:31:10 +0200125 foreach my $def (@var_def_arr)
126 {
127 # Handle the different parameter types
Paul Bakker33b43f12013-08-20 11:48:36 +0200128 if( substr($def, 0, 4) eq "int " )
Paul Bakker19343182013-08-16 13:31:10 +0200129 {
130 $param_defs .= " int param$i;\n";
131 $param_checks .= " if( verify_int( params[$i], &param$i ) != 0 ) return( 2 );\n";
132 push @dispatch_params, "param$i";
Paul Bakker367dae42009-06-28 21:50:27 +0000133
Paul Bakker19343182013-08-16 13:31:10 +0200134 $mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)";
135 $mapping_count++;
136 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200137 elsif( substr($def, 0, 6) eq "char *" )
Paul Bakker19343182013-08-16 13:31:10 +0200138 {
139 $param_defs .= " char *param$i = params[$i];\n";
140 $param_checks .= " if( verify_string( &param$i ) != 0 ) return( 2 );\n";
141 push @dispatch_params, "param$i";
Manuel Pégourié-Gonnard23c06082015-04-17 10:22:30 +0200142 $mapping_regex .= ":[^:\n]+";
Paul Bakker19343182013-08-16 13:31:10 +0200143 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200144 else
145 {
146 die "Parameter declaration not of supported type (int, char *)\n";
147 }
Paul Bakker19343182013-08-16 13:31:10 +0200148 $i++;
Paul Bakker367dae42009-06-28 21:50:27 +0000149
Paul Bakker19343182013-08-16 13:31:10 +0200150 }
151
152 # Find non-integer values we should map for this function
153 if( $mapping_count)
154 {
155 my @res = $test_data =~ /^$mapping_regex/msg;
156 foreach my $value (@res)
157 {
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200158 next unless ($value !~ /^\d+$/);
159 if ( $mapping_values{$value} ) {
160 ${ $mapping_values{$value} }{$function_pre_code} = 1;
161 } else {
162 $mapping_values{$value} = { $function_pre_code => 1 };
163 }
Paul Bakker19343182013-08-16 13:31:10 +0200164 }
165 }
166
167 my $call_params = join ", ", @dispatch_params;
168 my $param_count = @var_def_arr + 1;
169 $dispatch_code .= << "END";
170if( strcmp( params[0], "$function_name" ) == 0 )
171{
172$function_pre_code
173$param_defs
174 if( cnt != $param_count )
175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count );
Paul Bakker19343182013-08-16 13:31:10 +0200177 return( 2 );
178 }
179
180$param_checks
Paul Bakker33b43f12013-08-20 11:48:36 +0200181 test_suite_$function_name( $call_params );
182 return ( 0 );
Paul Bakker19343182013-08-16 13:31:10 +0200183$function_post_code
184 return ( 3 );
185}
186else
187END
188
Paul Bakker33b43f12013-08-20 11:48:36 +0200189 my $function_code = $function_pre_code . $function_decl . "\n" . $function_post_code;
190 $test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/;
Paul Bakker19343182013-08-16 13:31:10 +0200191}
192
193# Find specific case dependencies that we should be able to check
194# and make check code
195my $dep_check_code;
196
197my @res = $test_data =~ /^depends_on:([\w:]+)/msg;
198my %case_deps;
199foreach my $deps (@res)
200{
201 foreach my $dep (split(/:/, $deps))
202 {
203 $case_deps{$dep} = 1;
204 }
205}
206while( my ($key, $value) = each(%case_deps) )
207{
208 $dep_check_code .= << "END";
209 if( strcmp( str, "$key" ) == 0 )
210 {
211#if defined($key)
212 return( 0 );
213#else
214 return( 1 );
215#endif
216 }
Paul Bakker367dae42009-06-28 21:50:27 +0000217END
218}
219
Paul Bakker19343182013-08-16 13:31:10 +0200220# Make mapping code
221while( my ($key, $value) = each(%mapping_values) )
222{
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200223 my $key_mapping_code = << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200224 if( strcmp( str, "$key" ) == 0 )
225 {
226 *value = ( $key );
227 return( 0 );
228 }
229END
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200230
231 # handle depenencies, unless used at least one without depends
232 if ($value->{""}) {
233 $mapping_code .= $key_mapping_code;
234 next;
235 }
236 for my $ifdef ( keys %$value ) {
237 (my $endif = $ifdef) =~ s!ifdef!endif //!g;
238 $mapping_code .= $ifdef . $key_mapping_code . $endif;
239 }
Paul Bakker19343182013-08-16 13:31:10 +0200240}
241
242$dispatch_code =~ s/^(.+)/ $1/mg;
243
244$test_main =~ s/TEST_FILENAME/$test_case_data/;
245$test_main =~ s/FUNCTION_CODE//;
246$test_main =~ s/DEP_CHECK_CODE/$dep_check_code/;
247$test_main =~ s/DISPATCH_FUNCTION/$dispatch_code/;
248$test_main =~ s/MAPPING_CODE/$mapping_code/;
249
Paul Bakker367dae42009-06-28 21:50:27 +0000250print TEST_FILE << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200251$test_main
Paul Bakker367dae42009-06-28 21:50:27 +0000252END
253
Paul Bakker367dae42009-06-28 21:50:27 +0000254close(TEST_FILE);