blob: 5892f7ba367d687f717713fdca4ba29a3e7a83cb [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:
24# TEST_FILENAME
25# SUITE_PRE_DEP
26# MAPPING_CODE
27# FUNCTION CODE
28# SUITE_POST_DEP
29# DEP_CHECK_CODE
30# DISPATCH_FUNCTION
31#
32# - common helper code file - 'helpers.function'
33# Common helper functions
34#
35# - test suite code file - file name in the form 'test_suite_xxx.function'
36# Code file that contains the actual test cases. The file contains a
37# series of code sequences delimited by the following:
38# BEGIN_HEADER / END_HEADER - list of headers files
39# BEGIN_SUITE_HELPERS / END_SUITE_HELPERS - helper functions common to
40# the test suite
41# BEGIN_CASE / END_CASE - the test cases in the test suite. Each test
42# case contains at least one function that is used to create the
43# dispatch code.
44#
45# - test data file - file name in the form 'test_suite_xxxx.data'
46# The test case parameters to to be used in execution of the test. The
47# file name is used to replace the symbol 'TEST_FILENAME' in the main code
48# file above.
49#
Paul Bakker367dae42009-06-28 21:50:27 +000050
51use strict;
52
53my $suite_dir = shift or die "Missing suite directory";
54my $suite_name = shift or die "Missing suite name";
Paul Bakker46c17942011-07-13 14:54:54 +000055my $data_name = shift or die "Missing data name";
Rich Evansf4253c72015-01-14 19:23:00 +000056my $test_main_file = do { my $arg = shift; defined($arg) ? $arg : $suite_dir."/main_test.function" };
Paul Bakker46c17942011-07-13 14:54:54 +000057my $test_file = $data_name.".c";
SimonB152ea182016-02-15 23:27:28 +000058my $test_common_helper_file = $suite_dir."/helpers.function";
Paul Bakker367dae42009-06-28 21:50:27 +000059my $test_case_file = $suite_dir."/".$suite_name.".function";
Paul Bakker19343182013-08-16 13:31:10 +020060my $test_case_data = $suite_dir."/".$data_name.".data";
Paul Bakker367dae42009-06-28 21:50:27 +000061
62my $line_separator = $/;
63undef $/;
64
SimonB152ea182016-02-15 23:27:28 +000065open(TEST_HELPERS, "$test_common_helper_file") or die "Opening test helpers
66'$test_common_helper_file': $!";
67my $test_common_helpers = <TEST_HELPERS>;
Paul Bakker367dae42009-06-28 21:50:27 +000068close(TEST_HELPERS);
69
Paul Bakker19343182013-08-16 13:31:10 +020070open(TEST_MAIN, "$test_main_file") or die "Opening test main '$test_main_file': $!";
71my $test_main = <TEST_MAIN>;
72close(TEST_MAIN);
73
Paul Bakker367dae42009-06-28 21:50:27 +000074open(TEST_CASES, "$test_case_file") or die "Opening test cases '$test_case_file': $!";
75my $test_cases = <TEST_CASES>;
76close(TEST_CASES);
Paul Bakker19343182013-08-16 13:31:10 +020077
78open(TEST_DATA, "$test_case_data") or die "Opening test data '$test_case_data': $!";
79my $test_data = <TEST_DATA>;
80close(TEST_DATA);
81
Paul Bakker33b43f12013-08-20 11:48:36 +020082my ( $suite_header ) = $test_cases =~ /\/\* BEGIN_HEADER \*\/\n(.*?)\n\/\* END_HEADER \*\//s;
83my ( $suite_defines ) = $test_cases =~ /\/\* BEGIN_DEPENDENCIES\n \* (.*?)\n \* END_DEPENDENCIES/s;
SimonB152ea182016-02-15 23:27:28 +000084my ( $suite_helpers ) = $test_cases =~ /\/\* BEGIN_SUITE_HELPERS \*\/\n(.*?)\n\/\* END_SUITE_HELPERS \*\//s;
Paul Bakker5690efc2011-05-26 13:16:06 +000085
86my $requirements;
87if ($suite_defines =~ /^depends_on:/)
88{
89 ( $requirements ) = $suite_defines =~ /^depends_on:(.*)$/;
90}
Paul Bakker19343182013-08-16 13:31:10 +020091
Paul Bakker5690efc2011-05-26 13:16:06 +000092my @var_req_arr = split(/:/, $requirements);
93my $suite_pre_code;
94my $suite_post_code;
Paul Bakker19343182013-08-16 13:31:10 +020095my $dispatch_code;
96my $mapping_code;
97my %mapping_values;
Paul Bakker5690efc2011-05-26 13:16:06 +000098
99while (@var_req_arr)
100{
101 my $req = shift @var_req_arr;
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +0100102 $req =~ s/(!?)(.*)/$1defined($2)/;
Paul Bakker5690efc2011-05-26 13:16:06 +0000103
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +0100104 $suite_pre_code .= "#if $req\n";
Paul Bakker5690efc2011-05-26 13:16:06 +0000105 $suite_post_code .= "#endif /* $req */\n";
106}
Paul Bakker367dae42009-06-28 21:50:27 +0000107
108$/ = $line_separator;
109
110open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!";
111print TEST_FILE << "END";
SimonB152ea182016-02-15 23:27:28 +0000112/*
113 * *** THIS FILE HAS BEEN MACHINE GENERATED ***
114 *
115 * This file has been machine generated using the script: $0
116 *
117 * Test file : $test_file
118 *
119 * The following files were used to create this file.
120 *
121 * Main code file : $test_main_file
122 * Helper file : $test_common_helper_file
123 * Test suite file : $test_case_file
Simon Butcher64d60da2016-03-01 18:35:02 +0000124 * Test suite data : $test_case_data
SimonB152ea182016-02-15 23:27:28 +0000125 *
126 *
127 * This file is part of mbed TLS (https://tls.mbed.org)
128 */
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000131#include <mbedtls/config.h>
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +0200132#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +0200134#endif
Paul Bakker5690efc2011-05-26 13:16:06 +0000135
SimonB152ea182016-02-15 23:27:28 +0000136
137/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000138/* Common helper code */
SimonB152ea182016-02-15 23:27:28 +0000139
140$test_common_helpers
141
142
143/*----------------------------------------------------------------------------*/
144/* Test Suite Code */
Rich Evans00ab4702015-02-06 13:43:58 +0000145
Paul Bakkerde56ca12013-09-15 17:05:21 +0200146$suite_pre_code
Paul Bakker367dae42009-06-28 21:50:27 +0000147$suite_header
SimonB152ea182016-02-15 23:27:28 +0000148$suite_helpers
Paul Bakkerde56ca12013-09-15 17:05:21 +0200149$suite_post_code
Paul Bakker367dae42009-06-28 21:50:27 +0000150
Paul Bakker367dae42009-06-28 21:50:27 +0000151END
152
Paul Bakkerb34fef22013-08-20 12:06:33 +0200153$test_main =~ s/SUITE_PRE_DEP/$suite_pre_code/;
154$test_main =~ s/SUITE_POST_DEP/$suite_post_code/;
155
Paul Bakker33b43f12013-08-20 11:48:36 +0200156while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg)
Paul Bakker367dae42009-06-28 21:50:27 +0000157{
Paul Bakker19343182013-08-16 13:31:10 +0200158 my $function_deps = $1;
Paul Bakker33b43f12013-08-20 11:48:36 +0200159 my $function_decl = $2;
160
161 # Sanity checks of function
162 if ($function_decl !~ /^void /)
163 {
164 die "Test function does not have 'void' as return type\n";
165 }
Paul Bakker318d0fe2014-07-10 14:59:25 +0200166 if ($function_decl !~ /^void (\w+)\(\s*(.*?)\s*\)\s*{(.*)}/ms)
Paul Bakker33b43f12013-08-20 11:48:36 +0200167 {
168 die "Function declaration not in expected format\n";
169 }
170 my $function_name = $1;
171 my $function_params = $2;
Paul Bakker19343182013-08-16 13:31:10 +0200172 my $function_pre_code;
173 my $function_post_code;
Paul Bakker19343182013-08-16 13:31:10 +0200174 my $param_defs;
175 my $param_checks;
176 my @dispatch_params;
Paul Bakker33b43f12013-08-20 11:48:36 +0200177 my @var_def_arr = split(/,\s*/, $function_params);
Paul Bakker19343182013-08-16 13:31:10 +0200178 my $i = 1;
179 my $mapping_regex = "".$function_name;
180 my $mapping_count = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000181
Paul Bakker33b43f12013-08-20 11:48:36 +0200182 $function_decl =~ s/^void /void test_suite_/;
183
Paul Bakker318d0fe2014-07-10 14:59:25 +0200184 # Add exit label if not present
185 if ($function_decl !~ /^exit:$/m)
186 {
187 $function_decl =~ s/}\s*$/\nexit:\n return;\n}/;
188 }
189
Paul Bakker19343182013-08-16 13:31:10 +0200190 if ($function_deps =~ /^depends_on:/)
Paul Bakkerccff1672009-10-03 19:57:10 +0000191 {
Paul Bakker19343182013-08-16 13:31:10 +0200192 ( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/;
Paul Bakkerccff1672009-10-03 19:57:10 +0000193 }
194
Paul Bakker19343182013-08-16 13:31:10 +0200195 foreach my $req (split(/:/, $function_deps))
Paul Bakker367dae42009-06-28 21:50:27 +0000196 {
Paul Bakker19343182013-08-16 13:31:10 +0200197 $function_pre_code .= "#ifdef $req\n";
198 $function_post_code .= "#endif /* $req */\n";
Paul Bakker367dae42009-06-28 21:50:27 +0000199 }
Paul Bakker367dae42009-06-28 21:50:27 +0000200
Paul Bakker19343182013-08-16 13:31:10 +0200201 foreach my $def (@var_def_arr)
202 {
203 # Handle the different parameter types
Paul Bakker33b43f12013-08-20 11:48:36 +0200204 if( substr($def, 0, 4) eq "int " )
Paul Bakker19343182013-08-16 13:31:10 +0200205 {
206 $param_defs .= " int param$i;\n";
SimonB8ca7bc42016-04-17 23:24:50 +0100207 $param_checks .= " if( verify_int( params[$i], &param$i ) != 0 ) return( DISPATCH_INVALID_TEST_DATA );\n";
Paul Bakker19343182013-08-16 13:31:10 +0200208 push @dispatch_params, "param$i";
Paul Bakker367dae42009-06-28 21:50:27 +0000209
Paul Bakker19343182013-08-16 13:31:10 +0200210 $mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)";
211 $mapping_count++;
212 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200213 elsif( substr($def, 0, 6) eq "char *" )
Paul Bakker19343182013-08-16 13:31:10 +0200214 {
215 $param_defs .= " char *param$i = params[$i];\n";
SimonB8ca7bc42016-04-17 23:24:50 +0100216 $param_checks .= " if( verify_string( &param$i ) != 0 ) return( DISPATCH_INVALID_TEST_DATA );\n";
Paul Bakker19343182013-08-16 13:31:10 +0200217 push @dispatch_params, "param$i";
Manuel Pégourié-Gonnard23c06082015-04-17 10:22:30 +0200218 $mapping_regex .= ":[^:\n]+";
Paul Bakker19343182013-08-16 13:31:10 +0200219 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200220 else
221 {
222 die "Parameter declaration not of supported type (int, char *)\n";
223 }
Paul Bakker19343182013-08-16 13:31:10 +0200224 $i++;
Paul Bakker367dae42009-06-28 21:50:27 +0000225
Paul Bakker19343182013-08-16 13:31:10 +0200226 }
227
228 # Find non-integer values we should map for this function
229 if( $mapping_count)
230 {
231 my @res = $test_data =~ /^$mapping_regex/msg;
232 foreach my $value (@res)
233 {
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200234 next unless ($value !~ /^\d+$/);
235 if ( $mapping_values{$value} ) {
236 ${ $mapping_values{$value} }{$function_pre_code} = 1;
237 } else {
238 $mapping_values{$value} = { $function_pre_code => 1 };
239 }
Paul Bakker19343182013-08-16 13:31:10 +0200240 }
241 }
242
243 my $call_params = join ", ", @dispatch_params;
244 my $param_count = @var_def_arr + 1;
245 $dispatch_code .= << "END";
246if( strcmp( params[0], "$function_name" ) == 0 )
247{
248$function_pre_code
249$param_defs
250 if( cnt != $param_count )
251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count );
SimonB8ca7bc42016-04-17 23:24:50 +0100253 return( DISPATCH_INVALID_TEST_DATA );
Paul Bakker19343182013-08-16 13:31:10 +0200254 }
255
256$param_checks
Paul Bakker33b43f12013-08-20 11:48:36 +0200257 test_suite_$function_name( $call_params );
SimonB8ca7bc42016-04-17 23:24:50 +0100258 return ( DISPATCH_TEST_SUCCESS );
Paul Bakker19343182013-08-16 13:31:10 +0200259$function_post_code
SimonB8ca7bc42016-04-17 23:24:50 +0100260 return ( DISPATCH_UNSUPPORTED_SUITE );
Paul Bakker19343182013-08-16 13:31:10 +0200261}
262else
263END
264
Paul Bakker33b43f12013-08-20 11:48:36 +0200265 my $function_code = $function_pre_code . $function_decl . "\n" . $function_post_code;
266 $test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/;
Paul Bakker19343182013-08-16 13:31:10 +0200267}
268
269# Find specific case dependencies that we should be able to check
270# and make check code
271my $dep_check_code;
272
273my @res = $test_data =~ /^depends_on:([\w:]+)/msg;
274my %case_deps;
275foreach my $deps (@res)
276{
277 foreach my $dep (split(/:/, $deps))
278 {
279 $case_deps{$dep} = 1;
280 }
281}
282while( my ($key, $value) = each(%case_deps) )
283{
284 $dep_check_code .= << "END";
285 if( strcmp( str, "$key" ) == 0 )
286 {
287#if defined($key)
SimonB8ca7bc42016-04-17 23:24:50 +0100288 return( DEPENDENCY_SUPPORTED );
Paul Bakker19343182013-08-16 13:31:10 +0200289#else
SimonB8ca7bc42016-04-17 23:24:50 +0100290 return( DEPENDENCY_NOT_SUPPORTED );
Paul Bakker19343182013-08-16 13:31:10 +0200291#endif
292 }
Paul Bakker367dae42009-06-28 21:50:27 +0000293END
294}
295
Paul Bakker19343182013-08-16 13:31:10 +0200296# Make mapping code
297while( my ($key, $value) = each(%mapping_values) )
298{
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200299 my $key_mapping_code = << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200300 if( strcmp( str, "$key" ) == 0 )
301 {
302 *value = ( $key );
SimonB8ca7bc42016-04-17 23:24:50 +0100303 return( KEY_VALUE_MAPPING_FOUND );
Paul Bakker19343182013-08-16 13:31:10 +0200304 }
305END
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200306
307 # handle depenencies, unless used at least one without depends
308 if ($value->{""}) {
309 $mapping_code .= $key_mapping_code;
310 next;
311 }
312 for my $ifdef ( keys %$value ) {
313 (my $endif = $ifdef) =~ s!ifdef!endif //!g;
314 $mapping_code .= $ifdef . $key_mapping_code . $endif;
315 }
Paul Bakker19343182013-08-16 13:31:10 +0200316}
317
318$dispatch_code =~ s/^(.+)/ $1/mg;
319
SimonB8ca7bc42016-04-17 23:24:50 +0100320$test_main =~ s/TEST_FILENAME/$test_case_data/g;
Paul Bakker19343182013-08-16 13:31:10 +0200321$test_main =~ s/FUNCTION_CODE//;
322$test_main =~ s/DEP_CHECK_CODE/$dep_check_code/;
323$test_main =~ s/DISPATCH_FUNCTION/$dispatch_code/;
324$test_main =~ s/MAPPING_CODE/$mapping_code/;
325
Paul Bakker367dae42009-06-28 21:50:27 +0000326print TEST_FILE << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200327$test_main
Paul Bakker367dae42009-06-28 21:50:27 +0000328END
329
Paul Bakker367dae42009-06-28 21:50:27 +0000330close(TEST_FILE);