blob: 4c24c7c0967164eefdfaeb648a2813e37acac33b [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#
Gilles Peskineb04e2c32017-09-29 15:45:12 +020052# A test data file consists of a sequence of paragraphs separated by
53# a single empty line. Line breaks may be in Unix (LF) or Windows (CRLF)
54# format. Lines starting with the character '#' are ignored
55# (the parser behaves as if they were not present).
56#
57# Each paragraph describes one test case and must consist of: (1) one
58# line which is the test case name; (2) an optional line starting with
59# the 11-character prefix "depends_on:"; (3) a line containing the test
60# function to execute and its parameters.
61#
62# A depends_on: line consists of a list of compile-time options
63# separated by the character ':', with no whitespace. The test case
64# is executed only if this compilation option is enabled in config.h.
65#
66# The last line of each paragraph contains a test function name and
67# a list of parameters separated by the character ':'. Running the
68# test case calls this function with the specified parameters. Each
69# parameter may either be an integer written in decimal or hexadecimal,
70# or a string surrounded by double quotes which may not contain the
71# ':' character.
72#
Paul Bakker367dae42009-06-28 21:50:27 +000073
74use strict;
75
76my $suite_dir = shift or die "Missing suite directory";
77my $suite_name = shift or die "Missing suite name";
Paul Bakker46c17942011-07-13 14:54:54 +000078my $data_name = shift or die "Missing data name";
Rich Evansf4253c72015-01-14 19:23:00 +000079my $test_main_file = do { my $arg = shift; defined($arg) ? $arg : $suite_dir."/main_test.function" };
Paul Bakker46c17942011-07-13 14:54:54 +000080my $test_file = $data_name.".c";
SimonB152ea182016-02-15 23:27:28 +000081my $test_common_helper_file = $suite_dir."/helpers.function";
Paul Bakker367dae42009-06-28 21:50:27 +000082my $test_case_file = $suite_dir."/".$suite_name.".function";
Paul Bakker19343182013-08-16 13:31:10 +020083my $test_case_data = $suite_dir."/".$data_name.".data";
Paul Bakker367dae42009-06-28 21:50:27 +000084
85my $line_separator = $/;
86undef $/;
87
SimonB15942102016-04-25 21:34:49 +010088
89#
90# Open and read in the input files
91#
92
SimonB152ea182016-02-15 23:27:28 +000093open(TEST_HELPERS, "$test_common_helper_file") or die "Opening test helpers
94'$test_common_helper_file': $!";
95my $test_common_helpers = <TEST_HELPERS>;
Paul Bakker367dae42009-06-28 21:50:27 +000096close(TEST_HELPERS);
97
Paul Bakker19343182013-08-16 13:31:10 +020098open(TEST_MAIN, "$test_main_file") or die "Opening test main '$test_main_file': $!";
SimonB15942102016-04-25 21:34:49 +010099my @test_main_lines = split/^/, <TEST_MAIN>;
100my $test_main;
SimonB43dba3d2016-05-02 21:31:51 +0100101my $index = 2;
SimonB15942102016-04-25 21:34:49 +0100102for my $line (@test_main_lines) {
103 $line =~ s/!LINE_NO!/$index/;
104 $test_main = $test_main.$line;
105 $index++;
106}
Paul Bakker19343182013-08-16 13:31:10 +0200107close(TEST_MAIN);
108
Paul Bakker367dae42009-06-28 21:50:27 +0000109open(TEST_CASES, "$test_case_file") or die "Opening test cases '$test_case_file': $!";
SimonB15942102016-04-25 21:34:49 +0100110my @test_cases_lines = split/^/, <TEST_CASES>;
111my $test_cases;
SimonB43dba3d2016-05-02 21:31:51 +0100112my $index = 2;
SimonB15942102016-04-25 21:34:49 +0100113for my $line (@test_cases_lines) {
Gilles Peskine2ba437a2017-09-26 12:52:15 +0200114 if ($line =~ /^\/\* BEGIN_.*\*\//)
SimonB15942102016-04-25 21:34:49 +0100115 {
116 $line = $line."#line $index \"$test_case_file\"\n";
117 }
118
SimonBc1d2eb32016-05-02 15:52:52 +0100119 $line =~ s/!LINE_NO!/$index/;
120
SimonB15942102016-04-25 21:34:49 +0100121 $test_cases = $test_cases.$line;
122 $index++;
123}
124
Paul Bakker367dae42009-06-28 21:50:27 +0000125close(TEST_CASES);
Paul Bakker19343182013-08-16 13:31:10 +0200126
127open(TEST_DATA, "$test_case_data") or die "Opening test data '$test_case_data': $!";
128my $test_data = <TEST_DATA>;
129close(TEST_DATA);
130
SimonB15942102016-04-25 21:34:49 +0100131
132#
133# Find the headers, dependencies, and suites in the test cases file
134#
135
Paul Bakker33b43f12013-08-20 11:48:36 +0200136my ( $suite_header ) = $test_cases =~ /\/\* BEGIN_HEADER \*\/\n(.*?)\n\/\* END_HEADER \*\//s;
137my ( $suite_defines ) = $test_cases =~ /\/\* BEGIN_DEPENDENCIES\n \* (.*?)\n \* END_DEPENDENCIES/s;
SimonB152ea182016-02-15 23:27:28 +0000138my ( $suite_helpers ) = $test_cases =~ /\/\* BEGIN_SUITE_HELPERS \*\/\n(.*?)\n\/\* END_SUITE_HELPERS \*\//s;
Paul Bakker5690efc2011-05-26 13:16:06 +0000139
140my $requirements;
141if ($suite_defines =~ /^depends_on:/)
142{
143 ( $requirements ) = $suite_defines =~ /^depends_on:(.*)$/;
144}
Paul Bakker19343182013-08-16 13:31:10 +0200145
Paul Bakker5690efc2011-05-26 13:16:06 +0000146my @var_req_arr = split(/:/, $requirements);
147my $suite_pre_code;
148my $suite_post_code;
Paul Bakker19343182013-08-16 13:31:10 +0200149my $dispatch_code;
150my $mapping_code;
151my %mapping_values;
Paul Bakker5690efc2011-05-26 13:16:06 +0000152
153while (@var_req_arr)
154{
155 my $req = shift @var_req_arr;
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +0100156 $req =~ s/(!?)(.*)/$1defined($2)/;
Paul Bakker5690efc2011-05-26 13:16:06 +0000157
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +0100158 $suite_pre_code .= "#if $req\n";
Paul Bakker5690efc2011-05-26 13:16:06 +0000159 $suite_post_code .= "#endif /* $req */\n";
160}
Paul Bakker367dae42009-06-28 21:50:27 +0000161
162$/ = $line_separator;
163
164open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!";
165print TEST_FILE << "END";
SimonB152ea182016-02-15 23:27:28 +0000166/*
167 * *** THIS FILE HAS BEEN MACHINE GENERATED ***
168 *
169 * This file has been machine generated using the script: $0
170 *
171 * Test file : $test_file
172 *
173 * The following files were used to create this file.
174 *
175 * Main code file : $test_main_file
176 * Helper file : $test_common_helper_file
177 * Test suite file : $test_case_file
Simon Butcher64d60da2016-03-01 18:35:02 +0000178 * Test suite data : $test_case_data
SimonB152ea182016-02-15 23:27:28 +0000179 *
180 *
181 * This file is part of mbed TLS (https://tls.mbed.org)
182 */
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000185#include <mbedtls/config.h>
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +0200186#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +0200188#endif
Paul Bakker5690efc2011-05-26 13:16:06 +0000189
SimonB152ea182016-02-15 23:27:28 +0000190
191/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000192/* Common helper code */
SimonB152ea182016-02-15 23:27:28 +0000193
194$test_common_helpers
195
196
197/*----------------------------------------------------------------------------*/
198/* Test Suite Code */
Rich Evans00ab4702015-02-06 13:43:58 +0000199
Paul Bakkerde56ca12013-09-15 17:05:21 +0200200$suite_pre_code
Paul Bakker367dae42009-06-28 21:50:27 +0000201$suite_header
SimonB152ea182016-02-15 23:27:28 +0000202$suite_helpers
Paul Bakkerde56ca12013-09-15 17:05:21 +0200203$suite_post_code
Paul Bakker367dae42009-06-28 21:50:27 +0000204
Paul Bakker367dae42009-06-28 21:50:27 +0000205END
206
Paul Bakkerb34fef22013-08-20 12:06:33 +0200207$test_main =~ s/SUITE_PRE_DEP/$suite_pre_code/;
208$test_main =~ s/SUITE_POST_DEP/$suite_post_code/;
209
Paul Bakker33b43f12013-08-20 11:48:36 +0200210while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg)
Paul Bakker367dae42009-06-28 21:50:27 +0000211{
Paul Bakker19343182013-08-16 13:31:10 +0200212 my $function_deps = $1;
Paul Bakker33b43f12013-08-20 11:48:36 +0200213 my $function_decl = $2;
214
215 # Sanity checks of function
SimonB15942102016-04-25 21:34:49 +0100216 if ($function_decl !~ /^#line\s*.*\nvoid /)
Paul Bakker33b43f12013-08-20 11:48:36 +0200217 {
SimonB15942102016-04-25 21:34:49 +0100218 die "Test function does not have 'void' as return type.\n" .
219 "Function declaration:\n" .
220 $function_decl;
Paul Bakker33b43f12013-08-20 11:48:36 +0200221 }
SimonB15942102016-04-25 21:34:49 +0100222 if ($function_decl !~ /^(#line\s*.*)\nvoid (\w+)\(\s*(.*?)\s*\)\s*{(.*)}/ms)
Paul Bakker33b43f12013-08-20 11:48:36 +0200223 {
224 die "Function declaration not in expected format\n";
225 }
SimonB15942102016-04-25 21:34:49 +0100226 my $line_directive = $1;
227 my $function_name = $2;
228 my $function_params = $3;
Paul Bakker19343182013-08-16 13:31:10 +0200229 my $function_pre_code;
230 my $function_post_code;
Paul Bakker19343182013-08-16 13:31:10 +0200231 my $param_defs;
232 my $param_checks;
233 my @dispatch_params;
Paul Bakker33b43f12013-08-20 11:48:36 +0200234 my @var_def_arr = split(/,\s*/, $function_params);
Paul Bakker19343182013-08-16 13:31:10 +0200235 my $i = 1;
236 my $mapping_regex = "".$function_name;
237 my $mapping_count = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000238
SimonB15942102016-04-25 21:34:49 +0100239 $function_decl =~ s/(^#line\s*.*)\nvoid /$1\nvoid test_suite_/;
Paul Bakker33b43f12013-08-20 11:48:36 +0200240
Paul Bakker318d0fe2014-07-10 14:59:25 +0200241 # Add exit label if not present
242 if ($function_decl !~ /^exit:$/m)
243 {
244 $function_decl =~ s/}\s*$/\nexit:\n return;\n}/;
245 }
246
Paul Bakker19343182013-08-16 13:31:10 +0200247 if ($function_deps =~ /^depends_on:/)
Paul Bakkerccff1672009-10-03 19:57:10 +0000248 {
Paul Bakker19343182013-08-16 13:31:10 +0200249 ( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/;
Paul Bakkerccff1672009-10-03 19:57:10 +0000250 }
251
Paul Bakker19343182013-08-16 13:31:10 +0200252 foreach my $req (split(/:/, $function_deps))
Paul Bakker367dae42009-06-28 21:50:27 +0000253 {
Paul Bakker19343182013-08-16 13:31:10 +0200254 $function_pre_code .= "#ifdef $req\n";
255 $function_post_code .= "#endif /* $req */\n";
Paul Bakker367dae42009-06-28 21:50:27 +0000256 }
Paul Bakker367dae42009-06-28 21:50:27 +0000257
Paul Bakker19343182013-08-16 13:31:10 +0200258 foreach my $def (@var_def_arr)
259 {
260 # Handle the different parameter types
Paul Bakker33b43f12013-08-20 11:48:36 +0200261 if( substr($def, 0, 4) eq "int " )
Paul Bakker19343182013-08-16 13:31:10 +0200262 {
263 $param_defs .= " int param$i;\n";
SimonB8ca7bc42016-04-17 23:24:50 +0100264 $param_checks .= " if( verify_int( params[$i], &param$i ) != 0 ) return( DISPATCH_INVALID_TEST_DATA );\n";
Paul Bakker19343182013-08-16 13:31:10 +0200265 push @dispatch_params, "param$i";
Paul Bakker367dae42009-06-28 21:50:27 +0000266
Paul Bakker19343182013-08-16 13:31:10 +0200267 $mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)";
268 $mapping_count++;
269 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200270 elsif( substr($def, 0, 6) eq "char *" )
Paul Bakker19343182013-08-16 13:31:10 +0200271 {
272 $param_defs .= " char *param$i = params[$i];\n";
SimonB8ca7bc42016-04-17 23:24:50 +0100273 $param_checks .= " if( verify_string( &param$i ) != 0 ) return( DISPATCH_INVALID_TEST_DATA );\n";
Paul Bakker19343182013-08-16 13:31:10 +0200274 push @dispatch_params, "param$i";
Andres AG9060d4d2017-02-02 14:36:49 +0000275 $mapping_regex .= ":(?:\\\\.|[^:\n])+";
Paul Bakker19343182013-08-16 13:31:10 +0200276 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200277 else
278 {
279 die "Parameter declaration not of supported type (int, char *)\n";
280 }
Paul Bakker19343182013-08-16 13:31:10 +0200281 $i++;
Paul Bakker367dae42009-06-28 21:50:27 +0000282
Paul Bakker19343182013-08-16 13:31:10 +0200283 }
284
285 # Find non-integer values we should map for this function
286 if( $mapping_count)
287 {
288 my @res = $test_data =~ /^$mapping_regex/msg;
289 foreach my $value (@res)
290 {
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200291 next unless ($value !~ /^\d+$/);
292 if ( $mapping_values{$value} ) {
293 ${ $mapping_values{$value} }{$function_pre_code} = 1;
294 } else {
295 $mapping_values{$value} = { $function_pre_code => 1 };
296 }
Paul Bakker19343182013-08-16 13:31:10 +0200297 }
298 }
299
300 my $call_params = join ", ", @dispatch_params;
301 my $param_count = @var_def_arr + 1;
302 $dispatch_code .= << "END";
303if( strcmp( params[0], "$function_name" ) == 0 )
304{
305$function_pre_code
306$param_defs
307 if( cnt != $param_count )
308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count );
SimonB8ca7bc42016-04-17 23:24:50 +0100310 return( DISPATCH_INVALID_TEST_DATA );
Paul Bakker19343182013-08-16 13:31:10 +0200311 }
312
313$param_checks
Paul Bakker33b43f12013-08-20 11:48:36 +0200314 test_suite_$function_name( $call_params );
SimonB8ca7bc42016-04-17 23:24:50 +0100315 return ( DISPATCH_TEST_SUCCESS );
Paul Bakker19343182013-08-16 13:31:10 +0200316$function_post_code
SimonB8ca7bc42016-04-17 23:24:50 +0100317 return ( DISPATCH_UNSUPPORTED_SUITE );
Paul Bakker19343182013-08-16 13:31:10 +0200318}
319else
320END
321
SimonB15942102016-04-25 21:34:49 +0100322 my $function_code = $function_pre_code . $function_decl . "\n" .
323 $function_post_code;
Paul Bakker33b43f12013-08-20 11:48:36 +0200324 $test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/;
Paul Bakker19343182013-08-16 13:31:10 +0200325}
326
327# Find specific case dependencies that we should be able to check
328# and make check code
329my $dep_check_code;
330
Hanno Beckerf058f342017-07-23 10:24:22 +0100331my @res = $test_data =~ /^depends_on:([!:\w]+)/msg;
Paul Bakker19343182013-08-16 13:31:10 +0200332my %case_deps;
333foreach my $deps (@res)
334{
335 foreach my $dep (split(/:/, $deps))
336 {
337 $case_deps{$dep} = 1;
338 }
339}
340while( my ($key, $value) = each(%case_deps) )
341{
Hanno Beckerf058f342017-07-23 10:24:22 +0100342 if( substr($key, 0, 1) eq "!" )
343 {
344 my $key = substr($key, 1);
345 $dep_check_code .= << "END";
346 if( strcmp( str, "!$key" ) == 0 )
347 {
348#if !defined($key)
349 return( DEPENDENCY_SUPPORTED );
350#else
351 return( DEPENDENCY_NOT_SUPPORTED );
352#endif
353 }
354END
355 }
356 else
357 {
358 $dep_check_code .= << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200359 if( strcmp( str, "$key" ) == 0 )
360 {
361#if defined($key)
SimonB8ca7bc42016-04-17 23:24:50 +0100362 return( DEPENDENCY_SUPPORTED );
Paul Bakker19343182013-08-16 13:31:10 +0200363#else
SimonB8ca7bc42016-04-17 23:24:50 +0100364 return( DEPENDENCY_NOT_SUPPORTED );
Paul Bakker19343182013-08-16 13:31:10 +0200365#endif
366 }
Paul Bakker367dae42009-06-28 21:50:27 +0000367END
Hanno Beckerf058f342017-07-23 10:24:22 +0100368 }
Paul Bakker367dae42009-06-28 21:50:27 +0000369}
370
Paul Bakker19343182013-08-16 13:31:10 +0200371# Make mapping code
372while( my ($key, $value) = each(%mapping_values) )
373{
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200374 my $key_mapping_code = << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200375 if( strcmp( str, "$key" ) == 0 )
376 {
377 *value = ( $key );
SimonB8ca7bc42016-04-17 23:24:50 +0100378 return( KEY_VALUE_MAPPING_FOUND );
Paul Bakker19343182013-08-16 13:31:10 +0200379 }
380END
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200381
382 # handle depenencies, unless used at least one without depends
383 if ($value->{""}) {
384 $mapping_code .= $key_mapping_code;
385 next;
386 }
387 for my $ifdef ( keys %$value ) {
388 (my $endif = $ifdef) =~ s!ifdef!endif //!g;
389 $mapping_code .= $ifdef . $key_mapping_code . $endif;
390 }
Paul Bakker19343182013-08-16 13:31:10 +0200391}
392
393$dispatch_code =~ s/^(.+)/ $1/mg;
394
SimonB15942102016-04-25 21:34:49 +0100395$test_main =~ s/TESTCASE_FILENAME/$test_case_data/g;
396$test_main =~ s/TESTCODE_FILENAME/$test_case_file/g;
Paul Bakker19343182013-08-16 13:31:10 +0200397$test_main =~ s/FUNCTION_CODE//;
398$test_main =~ s/DEP_CHECK_CODE/$dep_check_code/;
399$test_main =~ s/DISPATCH_FUNCTION/$dispatch_code/;
400$test_main =~ s/MAPPING_CODE/$mapping_code/;
401
Paul Bakker367dae42009-06-28 21:50:27 +0000402print TEST_FILE << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200403$test_main
Paul Bakker367dae42009-06-28 21:50:27 +0000404END
405
Paul Bakker367dae42009-06-28 21:50:27 +0000406close(TEST_FILE);