blob: 30ee6b01cd78a88f7a235669ed50ceaf2019c591 [file] [log] [blame]
Markus Pfeiffera26a0052014-04-22 20:16:15 +00001#!/usr/bin/env perl
SimonB3ddf3552016-02-10 23:50:28 +00002
3# generate_code.pl
Paul Bakker367dae42009-06-28 21:50:27 +00004#
SimonB152ea182016-02-15 23:27:28 +00005# Purpose
6#
SimonB3ddf3552016-02-10 23:50:28 +00007# Generates the test suite code given inputs of the test suite directory that
8# contain the test suites, and the test suite file names for the test code and
9# test data.
10#
11# Usage: generate_code.pl <suite dir> <code file> <data file> [main code file]
SimonB152ea182016-02-15 23:27:28 +000012#
13# Structure of files
14#
15# - main code file - 'main_test.function'
16# Template file that contains the main() function for the test suite,
17# test dispatch code as well as support functions. It contains the
18# following symbols which are substituted by this script during
19# processing:
20# TEST_FILENAME
21# SUITE_PRE_DEP
22# MAPPING_CODE
23# FUNCTION CODE
24# SUITE_POST_DEP
25# DEP_CHECK_CODE
26# DISPATCH_FUNCTION
27#
28# - common helper code file - 'helpers.function'
29# Common helper functions
30#
31# - test suite code file - file name in the form 'test_suite_xxx.function'
32# Code file that contains the actual test cases. The file contains a
33# series of code sequences delimited by the following:
34# BEGIN_HEADER / END_HEADER - list of headers files
35# BEGIN_SUITE_HELPERS / END_SUITE_HELPERS - helper functions common to
36# the test suite
37# BEGIN_CASE / END_CASE - the test cases in the test suite. Each test
38# case contains at least one function that is used to create the
39# dispatch code.
40#
41# - test data file - file name in the form 'test_suite_xxxx.data'
42# The test case parameters to to be used in execution of the test. The
43# file name is used to replace the symbol 'TEST_FILENAME' in the main code
44# file above.
45#
Paul Bakker367dae42009-06-28 21:50:27 +000046
47use strict;
48
49my $suite_dir = shift or die "Missing suite directory";
50my $suite_name = shift or die "Missing suite name";
Paul Bakker46c17942011-07-13 14:54:54 +000051my $data_name = shift or die "Missing data name";
Rich Evansf4253c72015-01-14 19:23:00 +000052my $test_main_file = do { my $arg = shift; defined($arg) ? $arg : $suite_dir."/main_test.function" };
Paul Bakker46c17942011-07-13 14:54:54 +000053my $test_file = $data_name.".c";
SimonB152ea182016-02-15 23:27:28 +000054my $test_common_helper_file = $suite_dir."/helpers.function";
Paul Bakker367dae42009-06-28 21:50:27 +000055my $test_case_file = $suite_dir."/".$suite_name.".function";
Paul Bakker19343182013-08-16 13:31:10 +020056my $test_case_data = $suite_dir."/".$data_name.".data";
Paul Bakker367dae42009-06-28 21:50:27 +000057
58my $line_separator = $/;
59undef $/;
60
SimonB152ea182016-02-15 23:27:28 +000061open(TEST_HELPERS, "$test_common_helper_file") or die "Opening test helpers
62'$test_common_helper_file': $!";
63my $test_common_helpers = <TEST_HELPERS>;
Paul Bakker367dae42009-06-28 21:50:27 +000064close(TEST_HELPERS);
65
Paul Bakker19343182013-08-16 13:31:10 +020066open(TEST_MAIN, "$test_main_file") or die "Opening test main '$test_main_file': $!";
67my $test_main = <TEST_MAIN>;
68close(TEST_MAIN);
69
Paul Bakker367dae42009-06-28 21:50:27 +000070open(TEST_CASES, "$test_case_file") or die "Opening test cases '$test_case_file': $!";
71my $test_cases = <TEST_CASES>;
72close(TEST_CASES);
Paul Bakker19343182013-08-16 13:31:10 +020073
74open(TEST_DATA, "$test_case_data") or die "Opening test data '$test_case_data': $!";
75my $test_data = <TEST_DATA>;
76close(TEST_DATA);
77
Paul Bakker33b43f12013-08-20 11:48:36 +020078my ( $suite_header ) = $test_cases =~ /\/\* BEGIN_HEADER \*\/\n(.*?)\n\/\* END_HEADER \*\//s;
79my ( $suite_defines ) = $test_cases =~ /\/\* BEGIN_DEPENDENCIES\n \* (.*?)\n \* END_DEPENDENCIES/s;
SimonB152ea182016-02-15 23:27:28 +000080my ( $suite_helpers ) = $test_cases =~ /\/\* BEGIN_SUITE_HELPERS \*\/\n(.*?)\n\/\* END_SUITE_HELPERS \*\//s;
Paul Bakker5690efc2011-05-26 13:16:06 +000081
82my $requirements;
83if ($suite_defines =~ /^depends_on:/)
84{
85 ( $requirements ) = $suite_defines =~ /^depends_on:(.*)$/;
86}
Paul Bakker19343182013-08-16 13:31:10 +020087
Paul Bakker5690efc2011-05-26 13:16:06 +000088my @var_req_arr = split(/:/, $requirements);
89my $suite_pre_code;
90my $suite_post_code;
Paul Bakker19343182013-08-16 13:31:10 +020091my $dispatch_code;
92my $mapping_code;
93my %mapping_values;
Paul Bakker5690efc2011-05-26 13:16:06 +000094
95while (@var_req_arr)
96{
97 my $req = shift @var_req_arr;
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +010098 $req =~ s/(!?)(.*)/$1defined($2)/;
Paul Bakker5690efc2011-05-26 13:16:06 +000099
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +0100100 $suite_pre_code .= "#if $req\n";
Paul Bakker5690efc2011-05-26 13:16:06 +0000101 $suite_post_code .= "#endif /* $req */\n";
102}
Paul Bakker367dae42009-06-28 21:50:27 +0000103
104$/ = $line_separator;
105
106open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!";
107print TEST_FILE << "END";
SimonB152ea182016-02-15 23:27:28 +0000108/*
109 * *** THIS FILE HAS BEEN MACHINE GENERATED ***
110 *
111 * This file has been machine generated using the script: $0
112 *
113 * Test file : $test_file
114 *
115 * The following files were used to create this file.
116 *
117 * Main code file : $test_main_file
118 * Helper file : $test_common_helper_file
119 * Test suite file : $test_case_file
120 * Test suite daya : $test_case_data
121 *
122 *
123 * This file is part of mbed TLS (https://tls.mbed.org)
124 */
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000127#include <mbedtls/config.h>
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +0200128#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +0200130#endif
Paul Bakker5690efc2011-05-26 13:16:06 +0000131
SimonB152ea182016-02-15 23:27:28 +0000132
133/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000134/* Common helper code */
SimonB152ea182016-02-15 23:27:28 +0000135
136$test_common_helpers
137
138
139/*----------------------------------------------------------------------------*/
140/* Test Suite Code */
Rich Evans00ab4702015-02-06 13:43:58 +0000141
Paul Bakkerde56ca12013-09-15 17:05:21 +0200142$suite_pre_code
Paul Bakker367dae42009-06-28 21:50:27 +0000143$suite_header
SimonB152ea182016-02-15 23:27:28 +0000144$suite_helpers
Paul Bakkerde56ca12013-09-15 17:05:21 +0200145$suite_post_code
Paul Bakker367dae42009-06-28 21:50:27 +0000146
Paul Bakker367dae42009-06-28 21:50:27 +0000147END
148
Paul Bakkerb34fef22013-08-20 12:06:33 +0200149$test_main =~ s/SUITE_PRE_DEP/$suite_pre_code/;
150$test_main =~ s/SUITE_POST_DEP/$suite_post_code/;
151
Paul Bakker33b43f12013-08-20 11:48:36 +0200152while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg)
Paul Bakker367dae42009-06-28 21:50:27 +0000153{
Paul Bakker19343182013-08-16 13:31:10 +0200154 my $function_deps = $1;
Paul Bakker33b43f12013-08-20 11:48:36 +0200155 my $function_decl = $2;
156
157 # Sanity checks of function
158 if ($function_decl !~ /^void /)
159 {
160 die "Test function does not have 'void' as return type\n";
161 }
Paul Bakker318d0fe2014-07-10 14:59:25 +0200162 if ($function_decl !~ /^void (\w+)\(\s*(.*?)\s*\)\s*{(.*)}/ms)
Paul Bakker33b43f12013-08-20 11:48:36 +0200163 {
164 die "Function declaration not in expected format\n";
165 }
166 my $function_name = $1;
167 my $function_params = $2;
Paul Bakker19343182013-08-16 13:31:10 +0200168 my $function_pre_code;
169 my $function_post_code;
Paul Bakker19343182013-08-16 13:31:10 +0200170 my $param_defs;
171 my $param_checks;
172 my @dispatch_params;
Paul Bakker33b43f12013-08-20 11:48:36 +0200173 my @var_def_arr = split(/,\s*/, $function_params);
Paul Bakker19343182013-08-16 13:31:10 +0200174 my $i = 1;
175 my $mapping_regex = "".$function_name;
176 my $mapping_count = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000177
Paul Bakker33b43f12013-08-20 11:48:36 +0200178 $function_decl =~ s/^void /void test_suite_/;
179
Paul Bakker318d0fe2014-07-10 14:59:25 +0200180 # Add exit label if not present
181 if ($function_decl !~ /^exit:$/m)
182 {
183 $function_decl =~ s/}\s*$/\nexit:\n return;\n}/;
184 }
185
Paul Bakker19343182013-08-16 13:31:10 +0200186 if ($function_deps =~ /^depends_on:/)
Paul Bakkerccff1672009-10-03 19:57:10 +0000187 {
Paul Bakker19343182013-08-16 13:31:10 +0200188 ( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/;
Paul Bakkerccff1672009-10-03 19:57:10 +0000189 }
190
Paul Bakker19343182013-08-16 13:31:10 +0200191 foreach my $req (split(/:/, $function_deps))
Paul Bakker367dae42009-06-28 21:50:27 +0000192 {
Paul Bakker19343182013-08-16 13:31:10 +0200193 $function_pre_code .= "#ifdef $req\n";
194 $function_post_code .= "#endif /* $req */\n";
Paul Bakker367dae42009-06-28 21:50:27 +0000195 }
Paul Bakker367dae42009-06-28 21:50:27 +0000196
Paul Bakker19343182013-08-16 13:31:10 +0200197 foreach my $def (@var_def_arr)
198 {
199 # Handle the different parameter types
Paul Bakker33b43f12013-08-20 11:48:36 +0200200 if( substr($def, 0, 4) eq "int " )
Paul Bakker19343182013-08-16 13:31:10 +0200201 {
202 $param_defs .= " int param$i;\n";
203 $param_checks .= " if( verify_int( params[$i], &param$i ) != 0 ) return( 2 );\n";
204 push @dispatch_params, "param$i";
Paul Bakker367dae42009-06-28 21:50:27 +0000205
Paul Bakker19343182013-08-16 13:31:10 +0200206 $mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)";
207 $mapping_count++;
208 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200209 elsif( substr($def, 0, 6) eq "char *" )
Paul Bakker19343182013-08-16 13:31:10 +0200210 {
211 $param_defs .= " char *param$i = params[$i];\n";
212 $param_checks .= " if( verify_string( &param$i ) != 0 ) return( 2 );\n";
213 push @dispatch_params, "param$i";
Manuel Pégourié-Gonnard23c06082015-04-17 10:22:30 +0200214 $mapping_regex .= ":[^:\n]+";
Paul Bakker19343182013-08-16 13:31:10 +0200215 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200216 else
217 {
218 die "Parameter declaration not of supported type (int, char *)\n";
219 }
Paul Bakker19343182013-08-16 13:31:10 +0200220 $i++;
Paul Bakker367dae42009-06-28 21:50:27 +0000221
Paul Bakker19343182013-08-16 13:31:10 +0200222 }
223
224 # Find non-integer values we should map for this function
225 if( $mapping_count)
226 {
227 my @res = $test_data =~ /^$mapping_regex/msg;
228 foreach my $value (@res)
229 {
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200230 next unless ($value !~ /^\d+$/);
231 if ( $mapping_values{$value} ) {
232 ${ $mapping_values{$value} }{$function_pre_code} = 1;
233 } else {
234 $mapping_values{$value} = { $function_pre_code => 1 };
235 }
Paul Bakker19343182013-08-16 13:31:10 +0200236 }
237 }
238
239 my $call_params = join ", ", @dispatch_params;
240 my $param_count = @var_def_arr + 1;
241 $dispatch_code .= << "END";
242if( strcmp( params[0], "$function_name" ) == 0 )
243{
244$function_pre_code
245$param_defs
246 if( cnt != $param_count )
247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count );
Paul Bakker19343182013-08-16 13:31:10 +0200249 return( 2 );
250 }
251
252$param_checks
Paul Bakker33b43f12013-08-20 11:48:36 +0200253 test_suite_$function_name( $call_params );
254 return ( 0 );
Paul Bakker19343182013-08-16 13:31:10 +0200255$function_post_code
256 return ( 3 );
257}
258else
259END
260
Paul Bakker33b43f12013-08-20 11:48:36 +0200261 my $function_code = $function_pre_code . $function_decl . "\n" . $function_post_code;
262 $test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/;
Paul Bakker19343182013-08-16 13:31:10 +0200263}
264
265# Find specific case dependencies that we should be able to check
266# and make check code
267my $dep_check_code;
268
269my @res = $test_data =~ /^depends_on:([\w:]+)/msg;
270my %case_deps;
271foreach my $deps (@res)
272{
273 foreach my $dep (split(/:/, $deps))
274 {
275 $case_deps{$dep} = 1;
276 }
277}
278while( my ($key, $value) = each(%case_deps) )
279{
280 $dep_check_code .= << "END";
281 if( strcmp( str, "$key" ) == 0 )
282 {
283#if defined($key)
284 return( 0 );
285#else
286 return( 1 );
287#endif
288 }
Paul Bakker367dae42009-06-28 21:50:27 +0000289END
290}
291
Paul Bakker19343182013-08-16 13:31:10 +0200292# Make mapping code
293while( my ($key, $value) = each(%mapping_values) )
294{
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200295 my $key_mapping_code = << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200296 if( strcmp( str, "$key" ) == 0 )
297 {
298 *value = ( $key );
299 return( 0 );
300 }
301END
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200302
303 # handle depenencies, unless used at least one without depends
304 if ($value->{""}) {
305 $mapping_code .= $key_mapping_code;
306 next;
307 }
308 for my $ifdef ( keys %$value ) {
309 (my $endif = $ifdef) =~ s!ifdef!endif //!g;
310 $mapping_code .= $ifdef . $key_mapping_code . $endif;
311 }
Paul Bakker19343182013-08-16 13:31:10 +0200312}
313
314$dispatch_code =~ s/^(.+)/ $1/mg;
315
316$test_main =~ s/TEST_FILENAME/$test_case_data/;
317$test_main =~ s/FUNCTION_CODE//;
318$test_main =~ s/DEP_CHECK_CODE/$dep_check_code/;
319$test_main =~ s/DISPATCH_FUNCTION/$dispatch_code/;
320$test_main =~ s/MAPPING_CODE/$mapping_code/;
321
Paul Bakker367dae42009-06-28 21:50:27 +0000322print TEST_FILE << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200323$test_main
Paul Bakker367dae42009-06-28 21:50:27 +0000324END
325
Paul Bakker367dae42009-06-28 21:50:27 +0000326close(TEST_FILE);