blob: 84e949dfad72f804b5c614a29614e3da3e55e07d [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#
SimonB8ca7bc42016-04-17 23:24:50 +01005# This file is part of mbed TLS (https://tls.mbed.org)
6#
Simon Butcher64d60da2016-03-01 18:35:02 +00007# Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
8#
SimonB152ea182016-02-15 23:27:28 +00009# Purpose
10#
SimonB3ddf3552016-02-10 23:50:28 +000011# Generates the test suite code given inputs of the test suite directory that
12# contain the test suites, and the test suite file names for the test code and
13# test data.
14#
15# Usage: generate_code.pl <suite dir> <code file> <data file> [main code file]
SimonB152ea182016-02-15 23:27:28 +000016#
17# Structure of files
18#
19# - main code file - 'main_test.function'
20# Template file that contains the main() function for the test suite,
21# test dispatch code as well as support functions. It contains the
22# following symbols which are substituted by this script during
23# processing:
SimonB15942102016-04-25 21:34:49 +010024# TESTCASE_FILENAME
25# TESTCODE_FILENAME
SimonB152ea182016-02-15 23:27:28 +000026# SUITE_PRE_DEP
27# MAPPING_CODE
28# FUNCTION CODE
29# SUITE_POST_DEP
30# DEP_CHECK_CODE
31# DISPATCH_FUNCTION
SimonB15942102016-04-25 21:34:49 +010032# !LINE_NO!
SimonB152ea182016-02-15 23:27:28 +000033#
34# - common helper code file - 'helpers.function'
35# Common helper functions
36#
37# - test suite code file - file name in the form 'test_suite_xxx.function'
38# Code file that contains the actual test cases. The file contains a
39# series of code sequences delimited by the following:
40# BEGIN_HEADER / END_HEADER - list of headers files
41# BEGIN_SUITE_HELPERS / END_SUITE_HELPERS - helper functions common to
42# the test suite
43# BEGIN_CASE / END_CASE - the test cases in the test suite. Each test
44# case contains at least one function that is used to create the
45# dispatch code.
46#
47# - test data file - file name in the form 'test_suite_xxxx.data'
48# The test case parameters to to be used in execution of the test. The
SimonB15942102016-04-25 21:34:49 +010049# file name is used to replace the symbol 'TESTCASE_FILENAME' in the main
50# code file above.
SimonB152ea182016-02-15 23:27:28 +000051#
Paul Bakker367dae42009-06-28 21:50:27 +000052
53use strict;
54
55my $suite_dir = shift or die "Missing suite directory";
56my $suite_name = shift or die "Missing suite name";
Paul Bakker46c17942011-07-13 14:54:54 +000057my $data_name = shift or die "Missing data name";
Rich Evansf4253c72015-01-14 19:23:00 +000058my $test_main_file = do { my $arg = shift; defined($arg) ? $arg : $suite_dir."/main_test.function" };
Paul Bakker46c17942011-07-13 14:54:54 +000059my $test_file = $data_name.".c";
SimonB152ea182016-02-15 23:27:28 +000060my $test_common_helper_file = $suite_dir."/helpers.function";
Paul Bakker367dae42009-06-28 21:50:27 +000061my $test_case_file = $suite_dir."/".$suite_name.".function";
Paul Bakker19343182013-08-16 13:31:10 +020062my $test_case_data = $suite_dir."/".$data_name.".data";
Paul Bakker367dae42009-06-28 21:50:27 +000063
64my $line_separator = $/;
65undef $/;
66
SimonB15942102016-04-25 21:34:49 +010067
68#
69# Open and read in the input files
70#
71
SimonB152ea182016-02-15 23:27:28 +000072open(TEST_HELPERS, "$test_common_helper_file") or die "Opening test helpers
73'$test_common_helper_file': $!";
74my $test_common_helpers = <TEST_HELPERS>;
Paul Bakker367dae42009-06-28 21:50:27 +000075close(TEST_HELPERS);
76
Paul Bakker19343182013-08-16 13:31:10 +020077open(TEST_MAIN, "$test_main_file") or die "Opening test main '$test_main_file': $!";
SimonB15942102016-04-25 21:34:49 +010078my @test_main_lines = split/^/, <TEST_MAIN>;
79my $test_main;
SimonB43dba3d2016-05-02 21:31:51 +010080my $index = 2;
SimonB15942102016-04-25 21:34:49 +010081for my $line (@test_main_lines) {
82 $line =~ s/!LINE_NO!/$index/;
83 $test_main = $test_main.$line;
84 $index++;
85}
Paul Bakker19343182013-08-16 13:31:10 +020086close(TEST_MAIN);
87
Paul Bakker367dae42009-06-28 21:50:27 +000088open(TEST_CASES, "$test_case_file") or die "Opening test cases '$test_case_file': $!";
SimonB15942102016-04-25 21:34:49 +010089my @test_cases_lines = split/^/, <TEST_CASES>;
90my $test_cases;
SimonB43dba3d2016-05-02 21:31:51 +010091my $index = 2;
SimonB15942102016-04-25 21:34:49 +010092for my $line (@test_cases_lines) {
SimonB37f26202016-05-02 21:58:19 +010093 if ($line =~ /^\/\* BEGIN_SUITE_HELPERS .*\*\//)
94 {
95 $line = $line."#line $index \"$test_case_file\"\n";
96 }
97
SimonB15942102016-04-25 21:34:49 +010098 if ($line =~ /^\/\* BEGIN_CASE .*\*\//)
99 {
100 $line = $line."#line $index \"$test_case_file\"\n";
101 }
102
SimonBc1d2eb32016-05-02 15:52:52 +0100103 $line =~ s/!LINE_NO!/$index/;
104
SimonB15942102016-04-25 21:34:49 +0100105 $test_cases = $test_cases.$line;
106 $index++;
107}
108
Paul Bakker367dae42009-06-28 21:50:27 +0000109close(TEST_CASES);
Paul Bakker19343182013-08-16 13:31:10 +0200110
111open(TEST_DATA, "$test_case_data") or die "Opening test data '$test_case_data': $!";
112my $test_data = <TEST_DATA>;
113close(TEST_DATA);
114
SimonB15942102016-04-25 21:34:49 +0100115
116#
117# Find the headers, dependencies, and suites in the test cases file
118#
119
Paul Bakker33b43f12013-08-20 11:48:36 +0200120my ( $suite_header ) = $test_cases =~ /\/\* BEGIN_HEADER \*\/\n(.*?)\n\/\* END_HEADER \*\//s;
121my ( $suite_defines ) = $test_cases =~ /\/\* BEGIN_DEPENDENCIES\n \* (.*?)\n \* END_DEPENDENCIES/s;
SimonB152ea182016-02-15 23:27:28 +0000122my ( $suite_helpers ) = $test_cases =~ /\/\* BEGIN_SUITE_HELPERS \*\/\n(.*?)\n\/\* END_SUITE_HELPERS \*\//s;
Paul Bakker5690efc2011-05-26 13:16:06 +0000123
124my $requirements;
125if ($suite_defines =~ /^depends_on:/)
126{
127 ( $requirements ) = $suite_defines =~ /^depends_on:(.*)$/;
128}
Paul Bakker19343182013-08-16 13:31:10 +0200129
Paul Bakker5690efc2011-05-26 13:16:06 +0000130my @var_req_arr = split(/:/, $requirements);
131my $suite_pre_code;
132my $suite_post_code;
Paul Bakker19343182013-08-16 13:31:10 +0200133my $dispatch_code;
134my $mapping_code;
135my %mapping_values;
Paul Bakker5690efc2011-05-26 13:16:06 +0000136
137while (@var_req_arr)
138{
139 my $req = shift @var_req_arr;
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +0100140 $req =~ s/(!?)(.*)/$1defined($2)/;
Paul Bakker5690efc2011-05-26 13:16:06 +0000141
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +0100142 $suite_pre_code .= "#if $req\n";
Paul Bakker5690efc2011-05-26 13:16:06 +0000143 $suite_post_code .= "#endif /* $req */\n";
144}
Paul Bakker367dae42009-06-28 21:50:27 +0000145
146$/ = $line_separator;
147
148open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!";
149print TEST_FILE << "END";
SimonB152ea182016-02-15 23:27:28 +0000150/*
151 * *** THIS FILE HAS BEEN MACHINE GENERATED ***
152 *
153 * This file has been machine generated using the script: $0
154 *
155 * Test file : $test_file
156 *
157 * The following files were used to create this file.
158 *
159 * Main code file : $test_main_file
160 * Helper file : $test_common_helper_file
161 * Test suite file : $test_case_file
Simon Butcher64d60da2016-03-01 18:35:02 +0000162 * Test suite data : $test_case_data
SimonB152ea182016-02-15 23:27:28 +0000163 *
164 *
165 * This file is part of mbed TLS (https://tls.mbed.org)
166 */
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000169#include <mbedtls/config.h>
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +0200170#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +0200172#endif
Paul Bakker5690efc2011-05-26 13:16:06 +0000173
SimonB152ea182016-02-15 23:27:28 +0000174
175/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000176/* Common helper code */
SimonB152ea182016-02-15 23:27:28 +0000177
178$test_common_helpers
179
180
181/*----------------------------------------------------------------------------*/
182/* Test Suite Code */
Rich Evans00ab4702015-02-06 13:43:58 +0000183
Paul Bakkerde56ca12013-09-15 17:05:21 +0200184$suite_pre_code
Paul Bakker367dae42009-06-28 21:50:27 +0000185$suite_header
SimonB152ea182016-02-15 23:27:28 +0000186$suite_helpers
Paul Bakkerde56ca12013-09-15 17:05:21 +0200187$suite_post_code
Paul Bakker367dae42009-06-28 21:50:27 +0000188
Paul Bakker367dae42009-06-28 21:50:27 +0000189END
190
Paul Bakkerb34fef22013-08-20 12:06:33 +0200191$test_main =~ s/SUITE_PRE_DEP/$suite_pre_code/;
192$test_main =~ s/SUITE_POST_DEP/$suite_post_code/;
193
Paul Bakker33b43f12013-08-20 11:48:36 +0200194while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg)
Paul Bakker367dae42009-06-28 21:50:27 +0000195{
Paul Bakker19343182013-08-16 13:31:10 +0200196 my $function_deps = $1;
Paul Bakker33b43f12013-08-20 11:48:36 +0200197 my $function_decl = $2;
198
199 # Sanity checks of function
SimonB15942102016-04-25 21:34:49 +0100200 if ($function_decl !~ /^#line\s*.*\nvoid /)
Paul Bakker33b43f12013-08-20 11:48:36 +0200201 {
SimonB15942102016-04-25 21:34:49 +0100202 die "Test function does not have 'void' as return type.\n" .
203 "Function declaration:\n" .
204 $function_decl;
Paul Bakker33b43f12013-08-20 11:48:36 +0200205 }
SimonB15942102016-04-25 21:34:49 +0100206 if ($function_decl !~ /^(#line\s*.*)\nvoid (\w+)\(\s*(.*?)\s*\)\s*{(.*)}/ms)
Paul Bakker33b43f12013-08-20 11:48:36 +0200207 {
208 die "Function declaration not in expected format\n";
209 }
SimonB15942102016-04-25 21:34:49 +0100210 my $line_directive = $1;
211 my $function_name = $2;
212 my $function_params = $3;
Paul Bakker19343182013-08-16 13:31:10 +0200213 my $function_pre_code;
214 my $function_post_code;
Paul Bakker19343182013-08-16 13:31:10 +0200215 my $param_defs;
216 my $param_checks;
217 my @dispatch_params;
Paul Bakker33b43f12013-08-20 11:48:36 +0200218 my @var_def_arr = split(/,\s*/, $function_params);
Paul Bakker19343182013-08-16 13:31:10 +0200219 my $i = 1;
220 my $mapping_regex = "".$function_name;
221 my $mapping_count = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000222
SimonB15942102016-04-25 21:34:49 +0100223 $function_decl =~ s/(^#line\s*.*)\nvoid /$1\nvoid test_suite_/;
Paul Bakker33b43f12013-08-20 11:48:36 +0200224
Paul Bakker318d0fe2014-07-10 14:59:25 +0200225 # Add exit label if not present
226 if ($function_decl !~ /^exit:$/m)
227 {
228 $function_decl =~ s/}\s*$/\nexit:\n return;\n}/;
229 }
230
Paul Bakker19343182013-08-16 13:31:10 +0200231 if ($function_deps =~ /^depends_on:/)
Paul Bakkerccff1672009-10-03 19:57:10 +0000232 {
Paul Bakker19343182013-08-16 13:31:10 +0200233 ( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/;
Paul Bakkerccff1672009-10-03 19:57:10 +0000234 }
235
Paul Bakker19343182013-08-16 13:31:10 +0200236 foreach my $req (split(/:/, $function_deps))
Paul Bakker367dae42009-06-28 21:50:27 +0000237 {
Paul Bakker19343182013-08-16 13:31:10 +0200238 $function_pre_code .= "#ifdef $req\n";
239 $function_post_code .= "#endif /* $req */\n";
Paul Bakker367dae42009-06-28 21:50:27 +0000240 }
Paul Bakker367dae42009-06-28 21:50:27 +0000241
Paul Bakker19343182013-08-16 13:31:10 +0200242 foreach my $def (@var_def_arr)
243 {
244 # Handle the different parameter types
Paul Bakker33b43f12013-08-20 11:48:36 +0200245 if( substr($def, 0, 4) eq "int " )
Paul Bakker19343182013-08-16 13:31:10 +0200246 {
247 $param_defs .= " int param$i;\n";
SimonB8ca7bc42016-04-17 23:24:50 +0100248 $param_checks .= " if( verify_int( params[$i], &param$i ) != 0 ) return( DISPATCH_INVALID_TEST_DATA );\n";
Paul Bakker19343182013-08-16 13:31:10 +0200249 push @dispatch_params, "param$i";
Paul Bakker367dae42009-06-28 21:50:27 +0000250
Paul Bakker19343182013-08-16 13:31:10 +0200251 $mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)";
252 $mapping_count++;
253 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200254 elsif( substr($def, 0, 6) eq "char *" )
Paul Bakker19343182013-08-16 13:31:10 +0200255 {
256 $param_defs .= " char *param$i = params[$i];\n";
SimonB8ca7bc42016-04-17 23:24:50 +0100257 $param_checks .= " if( verify_string( &param$i ) != 0 ) return( DISPATCH_INVALID_TEST_DATA );\n";
Paul Bakker19343182013-08-16 13:31:10 +0200258 push @dispatch_params, "param$i";
Andres AG9060d4d2017-02-02 14:36:49 +0000259 $mapping_regex .= ":(?:\\\\.|[^:\n])+";
Paul Bakker19343182013-08-16 13:31:10 +0200260 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200261 else
262 {
263 die "Parameter declaration not of supported type (int, char *)\n";
264 }
Paul Bakker19343182013-08-16 13:31:10 +0200265 $i++;
Paul Bakker367dae42009-06-28 21:50:27 +0000266
Paul Bakker19343182013-08-16 13:31:10 +0200267 }
268
269 # Find non-integer values we should map for this function
270 if( $mapping_count)
271 {
272 my @res = $test_data =~ /^$mapping_regex/msg;
273 foreach my $value (@res)
274 {
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200275 next unless ($value !~ /^\d+$/);
276 if ( $mapping_values{$value} ) {
277 ${ $mapping_values{$value} }{$function_pre_code} = 1;
278 } else {
279 $mapping_values{$value} = { $function_pre_code => 1 };
280 }
Paul Bakker19343182013-08-16 13:31:10 +0200281 }
282 }
283
284 my $call_params = join ", ", @dispatch_params;
285 my $param_count = @var_def_arr + 1;
286 $dispatch_code .= << "END";
287if( strcmp( params[0], "$function_name" ) == 0 )
288{
289$function_pre_code
290$param_defs
291 if( cnt != $param_count )
292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count );
SimonB8ca7bc42016-04-17 23:24:50 +0100294 return( DISPATCH_INVALID_TEST_DATA );
Paul Bakker19343182013-08-16 13:31:10 +0200295 }
296
297$param_checks
Paul Bakker33b43f12013-08-20 11:48:36 +0200298 test_suite_$function_name( $call_params );
SimonB8ca7bc42016-04-17 23:24:50 +0100299 return ( DISPATCH_TEST_SUCCESS );
Paul Bakker19343182013-08-16 13:31:10 +0200300$function_post_code
SimonB8ca7bc42016-04-17 23:24:50 +0100301 return ( DISPATCH_UNSUPPORTED_SUITE );
Paul Bakker19343182013-08-16 13:31:10 +0200302}
303else
304END
305
SimonB15942102016-04-25 21:34:49 +0100306 my $function_code = $function_pre_code . $function_decl . "\n" .
307 $function_post_code;
Paul Bakker33b43f12013-08-20 11:48:36 +0200308 $test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/;
Paul Bakker19343182013-08-16 13:31:10 +0200309}
310
311# Find specific case dependencies that we should be able to check
312# and make check code
313my $dep_check_code;
314
315my @res = $test_data =~ /^depends_on:([\w:]+)/msg;
316my %case_deps;
317foreach my $deps (@res)
318{
319 foreach my $dep (split(/:/, $deps))
320 {
321 $case_deps{$dep} = 1;
322 }
323}
324while( my ($key, $value) = each(%case_deps) )
325{
326 $dep_check_code .= << "END";
327 if( strcmp( str, "$key" ) == 0 )
328 {
329#if defined($key)
SimonB8ca7bc42016-04-17 23:24:50 +0100330 return( DEPENDENCY_SUPPORTED );
Paul Bakker19343182013-08-16 13:31:10 +0200331#else
SimonB8ca7bc42016-04-17 23:24:50 +0100332 return( DEPENDENCY_NOT_SUPPORTED );
Paul Bakker19343182013-08-16 13:31:10 +0200333#endif
334 }
Paul Bakker367dae42009-06-28 21:50:27 +0000335END
336}
337
Paul Bakker19343182013-08-16 13:31:10 +0200338# Make mapping code
339while( my ($key, $value) = each(%mapping_values) )
340{
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200341 my $key_mapping_code = << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200342 if( strcmp( str, "$key" ) == 0 )
343 {
344 *value = ( $key );
SimonB8ca7bc42016-04-17 23:24:50 +0100345 return( KEY_VALUE_MAPPING_FOUND );
Paul Bakker19343182013-08-16 13:31:10 +0200346 }
347END
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200348
349 # handle depenencies, unless used at least one without depends
350 if ($value->{""}) {
351 $mapping_code .= $key_mapping_code;
352 next;
353 }
354 for my $ifdef ( keys %$value ) {
355 (my $endif = $ifdef) =~ s!ifdef!endif //!g;
356 $mapping_code .= $ifdef . $key_mapping_code . $endif;
357 }
Paul Bakker19343182013-08-16 13:31:10 +0200358}
359
360$dispatch_code =~ s/^(.+)/ $1/mg;
361
SimonB15942102016-04-25 21:34:49 +0100362$test_main =~ s/TESTCASE_FILENAME/$test_case_data/g;
363$test_main =~ s/TESTCODE_FILENAME/$test_case_file/g;
Paul Bakker19343182013-08-16 13:31:10 +0200364$test_main =~ s/FUNCTION_CODE//;
365$test_main =~ s/DEP_CHECK_CODE/$dep_check_code/;
366$test_main =~ s/DISPATCH_FUNCTION/$dispatch_code/;
367$test_main =~ s/MAPPING_CODE/$mapping_code/;
368
Paul Bakker367dae42009-06-28 21:50:27 +0000369print TEST_FILE << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200370$test_main
Paul Bakker367dae42009-06-28 21:50:27 +0000371END
372
Paul Bakker367dae42009-06-28 21:50:27 +0000373close(TEST_FILE);