blob: 43c71fc81bc4d68afed6571d3fc7e4afdc620d97 [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 Bakker367dae42009-06-28 21:50:27 +000034my ( $suite_header ) = $test_cases =~ /BEGIN_HEADER\n(.*?)\nEND_HEADER/s;
Paul Bakker5690efc2011-05-26 13:16:06 +000035my ( $suite_defines ) = $test_cases =~ /BEGIN_DEPENDENCIES\n(.*?)\nEND_DEPENDENCIES/s;
36
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 Bakker19343182013-08-16 13:31:10 +020072while($test_cases =~ /BEGIN_CASE *([\w:]*)\n(\w+):([^\n]*)\n\{\n(.*?)\}\nEND_CASE/sg)
Paul Bakker367dae42009-06-28 21:50:27 +000073{
Paul Bakker19343182013-08-16 13:31:10 +020074 my $function_deps = $1;
75 my $function_name = $2;
76 my $function_params = $3;
77 my $function_code = $4;
78 my $function_pre_code;
79 my $function_post_code;
80 my @param_decl;
81 my $param_defs;
82 my $param_checks;
83 my @dispatch_params;
84 my @var_def_arr = split(/:/, $function_params);
85 my $i = 1;
86 my $mapping_regex = "".$function_name;
87 my $mapping_count = 0;
Paul Bakker367dae42009-06-28 21:50:27 +000088
Paul Bakker19343182013-08-16 13:31:10 +020089 if ($function_deps =~ /^depends_on:/)
Paul Bakkerccff1672009-10-03 19:57:10 +000090 {
Paul Bakker19343182013-08-16 13:31:10 +020091 ( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/;
Paul Bakkerccff1672009-10-03 19:57:10 +000092 }
93
Paul Bakker19343182013-08-16 13:31:10 +020094 foreach my $req (split(/:/, $function_deps))
Paul Bakker367dae42009-06-28 21:50:27 +000095 {
Paul Bakker19343182013-08-16 13:31:10 +020096 $function_pre_code .= "#ifdef $req\n";
97 $function_post_code .= "#endif /* $req */\n";
Paul Bakker367dae42009-06-28 21:50:27 +000098 }
Paul Bakker367dae42009-06-28 21:50:27 +000099
Paul Bakker19343182013-08-16 13:31:10 +0200100 foreach my $def (@var_def_arr)
101 {
102 # Handle the different parameter types
Paul Bakker367dae42009-06-28 21:50:27 +0000103
Paul Bakker19343182013-08-16 13:31:10 +0200104 if( substr($def, 0, 1) eq "#" )
105 {
106 $param_defs .= " int param$i;\n";
107 $param_checks .= " if( verify_int( params[$i], &param$i ) != 0 ) return( 2 );\n";
108 push @dispatch_params, "param$i";
109 $def =~ s/#//;
110 push @param_decl, "int $def";
Paul Bakker367dae42009-06-28 21:50:27 +0000111
Paul Bakker19343182013-08-16 13:31:10 +0200112 $mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)";
113 $mapping_count++;
114 }
115 else
116 {
117 $param_defs .= " char *param$i = params[$i];\n";
118 $param_checks .= " if( verify_string( &param$i ) != 0 ) return( 2 );\n";
119 push @dispatch_params, "param$i";
120 push @param_decl, "char *$def";
121 $mapping_regex .= ":[^:]+";
122 }
123 $i++;
Paul Bakker367dae42009-06-28 21:50:27 +0000124
Paul Bakker19343182013-08-16 13:31:10 +0200125 $function_code =~ s/\{$def\}/$def/g;
126 }
127
128 # Find non-integer values we should map for this function
129 if( $mapping_count)
130 {
131 my @res = $test_data =~ /^$mapping_regex/msg;
132 foreach my $value (@res)
133 {
134 $mapping_values{$value} = 1 if ($value !~ /^\d+$/);
135 }
136 }
137
138 my $call_params = join ", ", @dispatch_params;
139 my $param_count = @var_def_arr + 1;
140 $dispatch_code .= << "END";
141if( strcmp( params[0], "$function_name" ) == 0 )
142{
143$function_pre_code
144$param_defs
145 if( cnt != $param_count )
146 {
147 fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count );
148 return( 2 );
149 }
150
151$param_checks
152 ret = test_suite_$function_name( $call_params );
153 return ( ret != 0 );
154$function_post_code
155 return ( 3 );
156}
157else
158END
159
160 my $function_def = "int test_suite_$function_name(";
161 $function_def .= join ", ", @param_decl;
162 $function_def .= ")\n{\n";
163
164 $function_code = $function_pre_code . $function_def . $function_code . "\n return( 0 );\n}\n";
165 $function_code .= $function_post_code;
166 $test_main =~ s/FUNCTION_CODE/$function_code\n\nFUNCTION_CODE/;
167}
168
169# Find specific case dependencies that we should be able to check
170# and make check code
171my $dep_check_code;
172
173my @res = $test_data =~ /^depends_on:([\w:]+)/msg;
174my %case_deps;
175foreach my $deps (@res)
176{
177 foreach my $dep (split(/:/, $deps))
178 {
179 $case_deps{$dep} = 1;
180 }
181}
182while( my ($key, $value) = each(%case_deps) )
183{
184 $dep_check_code .= << "END";
185 if( strcmp( str, "$key" ) == 0 )
186 {
187#if defined($key)
188 return( 0 );
189#else
190 return( 1 );
191#endif
192 }
Paul Bakker367dae42009-06-28 21:50:27 +0000193END
194}
195
Paul Bakker19343182013-08-16 13:31:10 +0200196# Make mapping code
197while( my ($key, $value) = each(%mapping_values) )
198{
199 $mapping_code .= << "END";
200 if( strcmp( str, "$key" ) == 0 )
201 {
202 *value = ( $key );
203 return( 0 );
204 }
205END
206}
207
208$dispatch_code =~ s/^(.+)/ $1/mg;
209
210$test_main =~ s/TEST_FILENAME/$test_case_data/;
211$test_main =~ s/FUNCTION_CODE//;
212$test_main =~ s/DEP_CHECK_CODE/$dep_check_code/;
213$test_main =~ s/DISPATCH_FUNCTION/$dispatch_code/;
214$test_main =~ s/MAPPING_CODE/$mapping_code/;
215
Paul Bakker367dae42009-06-28 21:50:27 +0000216print TEST_FILE << "END";
Paul Bakker5690efc2011-05-26 13:16:06 +0000217$suite_post_code
Paul Bakker5690efc2011-05-26 13:16:06 +0000218
Paul Bakker19343182013-08-16 13:31:10 +0200219$test_main
Paul Bakker367dae42009-06-28 21:50:27 +0000220END
221
Paul Bakker367dae42009-06-28 21:50:27 +0000222close(TEST_FILE);