blob: 1e33ac612b36453a5b94723cb4f7cb44ee727c79 [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 {
147 $mapping_values{$value} = 1 if ($value !~ /^\d+$/);
148 }
149 }
150
151 my $call_params = join ", ", @dispatch_params;
152 my $param_count = @var_def_arr + 1;
153 $dispatch_code .= << "END";
154if( strcmp( params[0], "$function_name" ) == 0 )
155{
156$function_pre_code
157$param_defs
158 if( cnt != $param_count )
159 {
160 fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count );
161 return( 2 );
162 }
163
164$param_checks
Paul Bakker33b43f12013-08-20 11:48:36 +0200165 test_suite_$function_name( $call_params );
166 return ( 0 );
Paul Bakker19343182013-08-16 13:31:10 +0200167$function_post_code
168 return ( 3 );
169}
170else
171END
172
Paul Bakker33b43f12013-08-20 11:48:36 +0200173 my $function_code = $function_pre_code . $function_decl . "\n" . $function_post_code;
174 $test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/;
Paul Bakker19343182013-08-16 13:31:10 +0200175}
176
177# Find specific case dependencies that we should be able to check
178# and make check code
179my $dep_check_code;
180
181my @res = $test_data =~ /^depends_on:([\w:]+)/msg;
182my %case_deps;
183foreach my $deps (@res)
184{
185 foreach my $dep (split(/:/, $deps))
186 {
187 $case_deps{$dep} = 1;
188 }
189}
190while( my ($key, $value) = each(%case_deps) )
191{
192 $dep_check_code .= << "END";
193 if( strcmp( str, "$key" ) == 0 )
194 {
195#if defined($key)
196 return( 0 );
197#else
198 return( 1 );
199#endif
200 }
Paul Bakker367dae42009-06-28 21:50:27 +0000201END
202}
203
Paul Bakker19343182013-08-16 13:31:10 +0200204# Make mapping code
205while( my ($key, $value) = each(%mapping_values) )
206{
207 $mapping_code .= << "END";
208 if( strcmp( str, "$key" ) == 0 )
209 {
210 *value = ( $key );
211 return( 0 );
212 }
213END
214}
215
216$dispatch_code =~ s/^(.+)/ $1/mg;
217
218$test_main =~ s/TEST_FILENAME/$test_case_data/;
219$test_main =~ s/FUNCTION_CODE//;
220$test_main =~ s/DEP_CHECK_CODE/$dep_check_code/;
221$test_main =~ s/DISPATCH_FUNCTION/$dispatch_code/;
222$test_main =~ s/MAPPING_CODE/$mapping_code/;
223
Paul Bakker367dae42009-06-28 21:50:27 +0000224print TEST_FILE << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200225$test_main
Paul Bakker367dae42009-06-28 21:50:27 +0000226END
227
Paul Bakker367dae42009-06-28 21:50:27 +0000228close(TEST_FILE);