blob: 6c2ac6e0fdb01ad7c908d7ee6b460f9852e4d264 [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 Bakkerde56ca12013-09-15 17:05:21 +020064$suite_pre_code
Paul Bakker367dae42009-06-28 21:50:27 +000065$suite_header
Paul Bakkerde56ca12013-09-15 17:05:21 +020066$suite_post_code
Paul Bakker367dae42009-06-28 21:50:27 +000067
68$test_helpers
69
Paul Bakker367dae42009-06-28 21:50:27 +000070END
71
Paul Bakkerb34fef22013-08-20 12:06:33 +020072$test_main =~ s/SUITE_PRE_DEP/$suite_pre_code/;
73$test_main =~ s/SUITE_POST_DEP/$suite_post_code/;
74
Paul Bakker33b43f12013-08-20 11:48:36 +020075while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg)
Paul Bakker367dae42009-06-28 21:50:27 +000076{
Paul Bakker19343182013-08-16 13:31:10 +020077 my $function_deps = $1;
Paul Bakker33b43f12013-08-20 11:48:36 +020078 my $function_decl = $2;
79
80 # Sanity checks of function
81 if ($function_decl !~ /^void /)
82 {
83 die "Test function does not have 'void' as return type\n";
84 }
85 if ($function_decl !~ /^void (\w+)\(\s*(.*?)\s*\)\s*{(.*?)}/ms)
86 {
87 die "Function declaration not in expected format\n";
88 }
89 my $function_name = $1;
90 my $function_params = $2;
Paul Bakker19343182013-08-16 13:31:10 +020091 my $function_pre_code;
92 my $function_post_code;
Paul Bakker19343182013-08-16 13:31:10 +020093 my $param_defs;
94 my $param_checks;
95 my @dispatch_params;
Paul Bakker33b43f12013-08-20 11:48:36 +020096 my @var_def_arr = split(/,\s*/, $function_params);
Paul Bakker19343182013-08-16 13:31:10 +020097 my $i = 1;
98 my $mapping_regex = "".$function_name;
99 my $mapping_count = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000100
Paul Bakker33b43f12013-08-20 11:48:36 +0200101 $function_decl =~ s/^void /void test_suite_/;
102
Paul Bakker19343182013-08-16 13:31:10 +0200103 if ($function_deps =~ /^depends_on:/)
Paul Bakkerccff1672009-10-03 19:57:10 +0000104 {
Paul Bakker19343182013-08-16 13:31:10 +0200105 ( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/;
Paul Bakkerccff1672009-10-03 19:57:10 +0000106 }
107
Paul Bakker19343182013-08-16 13:31:10 +0200108 foreach my $req (split(/:/, $function_deps))
Paul Bakker367dae42009-06-28 21:50:27 +0000109 {
Paul Bakker19343182013-08-16 13:31:10 +0200110 $function_pre_code .= "#ifdef $req\n";
111 $function_post_code .= "#endif /* $req */\n";
Paul Bakker367dae42009-06-28 21:50:27 +0000112 }
Paul Bakker367dae42009-06-28 21:50:27 +0000113
Paul Bakker19343182013-08-16 13:31:10 +0200114 foreach my $def (@var_def_arr)
115 {
116 # Handle the different parameter types
Paul Bakker33b43f12013-08-20 11:48:36 +0200117 if( substr($def, 0, 4) eq "int " )
Paul Bakker19343182013-08-16 13:31:10 +0200118 {
119 $param_defs .= " int param$i;\n";
120 $param_checks .= " if( verify_int( params[$i], &param$i ) != 0 ) return( 2 );\n";
121 push @dispatch_params, "param$i";
Paul Bakker367dae42009-06-28 21:50:27 +0000122
Paul Bakker19343182013-08-16 13:31:10 +0200123 $mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)";
124 $mapping_count++;
125 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200126 elsif( substr($def, 0, 6) eq "char *" )
Paul Bakker19343182013-08-16 13:31:10 +0200127 {
128 $param_defs .= " char *param$i = params[$i];\n";
129 $param_checks .= " if( verify_string( &param$i ) != 0 ) return( 2 );\n";
130 push @dispatch_params, "param$i";
Paul Bakker19343182013-08-16 13:31:10 +0200131 $mapping_regex .= ":[^:]+";
132 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200133 else
134 {
135 die "Parameter declaration not of supported type (int, char *)\n";
136 }
Paul Bakker19343182013-08-16 13:31:10 +0200137 $i++;
Paul Bakker367dae42009-06-28 21:50:27 +0000138
Paul Bakker19343182013-08-16 13:31:10 +0200139 }
140
141 # Find non-integer values we should map for this function
142 if( $mapping_count)
143 {
144 my @res = $test_data =~ /^$mapping_regex/msg;
145 foreach my $value (@res)
146 {
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200147 next unless ($value !~ /^\d+$/);
148 if ( $mapping_values{$value} ) {
149 ${ $mapping_values{$value} }{$function_pre_code} = 1;
150 } else {
151 $mapping_values{$value} = { $function_pre_code => 1 };
152 }
Paul Bakker19343182013-08-16 13:31:10 +0200153 }
154 }
155
156 my $call_params = join ", ", @dispatch_params;
157 my $param_count = @var_def_arr + 1;
158 $dispatch_code .= << "END";
159if( strcmp( params[0], "$function_name" ) == 0 )
160{
161$function_pre_code
162$param_defs
163 if( cnt != $param_count )
164 {
165 fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count );
166 return( 2 );
167 }
168
169$param_checks
Paul Bakker33b43f12013-08-20 11:48:36 +0200170 test_suite_$function_name( $call_params );
171 return ( 0 );
Paul Bakker19343182013-08-16 13:31:10 +0200172$function_post_code
173 return ( 3 );
174}
175else
176END
177
Paul Bakker33b43f12013-08-20 11:48:36 +0200178 my $function_code = $function_pre_code . $function_decl . "\n" . $function_post_code;
179 $test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/;
Paul Bakker19343182013-08-16 13:31:10 +0200180}
181
182# Find specific case dependencies that we should be able to check
183# and make check code
184my $dep_check_code;
185
186my @res = $test_data =~ /^depends_on:([\w:]+)/msg;
187my %case_deps;
188foreach my $deps (@res)
189{
190 foreach my $dep (split(/:/, $deps))
191 {
192 $case_deps{$dep} = 1;
193 }
194}
195while( my ($key, $value) = each(%case_deps) )
196{
197 $dep_check_code .= << "END";
198 if( strcmp( str, "$key" ) == 0 )
199 {
200#if defined($key)
201 return( 0 );
202#else
203 return( 1 );
204#endif
205 }
Paul Bakker367dae42009-06-28 21:50:27 +0000206END
207}
208
Paul Bakker19343182013-08-16 13:31:10 +0200209# Make mapping code
210while( my ($key, $value) = each(%mapping_values) )
211{
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200212 my $key_mapping_code = << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200213 if( strcmp( str, "$key" ) == 0 )
214 {
215 *value = ( $key );
216 return( 0 );
217 }
218END
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200219
220 # handle depenencies, unless used at least one without depends
221 if ($value->{""}) {
222 $mapping_code .= $key_mapping_code;
223 next;
224 }
225 for my $ifdef ( keys %$value ) {
226 (my $endif = $ifdef) =~ s!ifdef!endif //!g;
227 $mapping_code .= $ifdef . $key_mapping_code . $endif;
228 }
Paul Bakker19343182013-08-16 13:31:10 +0200229}
230
231$dispatch_code =~ s/^(.+)/ $1/mg;
232
233$test_main =~ s/TEST_FILENAME/$test_case_data/;
234$test_main =~ s/FUNCTION_CODE//;
235$test_main =~ s/DEP_CHECK_CODE/$dep_check_code/;
236$test_main =~ s/DISPATCH_FUNCTION/$dispatch_code/;
237$test_main =~ s/MAPPING_CODE/$mapping_code/;
238
Paul Bakker367dae42009-06-28 21:50:27 +0000239print TEST_FILE << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200240$test_main
Paul Bakker367dae42009-06-28 21:50:27 +0000241END
242
Paul Bakker367dae42009-06-28 21:50:27 +0000243close(TEST_FILE);