blob: 17824c77500bef17d9e226f90b8b022d36bf2567 [file] [log] [blame]
Markus Pfeiffera26a0052014-04-22 20:16:15 +00001#!/usr/bin/env perl
SimonBe39088a2016-02-10 23:50:28 +00002
3# generate_code.pl
4#
Simon Butcher60411a82016-03-01 18:35:02 +00005# Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
6#
SimonB0284f582016-02-15 23:27:28 +00007# Purpose
8#
SimonBe39088a2016-02-10 23:50:28 +00009# Generates the test suite code given inputs of the test suite directory that
10# contain the test suites, and the test suite file names for the test code and
11# test data.
12#
13# Usage: generate_code.pl <suite dir> <code file> <data file> [main code file]
Paul Bakker367dae42009-06-28 21:50:27 +000014#
SimonB0284f582016-02-15 23:27:28 +000015# Structure of files
16#
17# - main code file - 'main_test.function'
18# Template file that contains the main() function for the test suite,
19# test dispatch code as well as support functions. It contains the
20# following symbols which are substituted by this script during
21# processing:
22# TEST_FILENAME
23# SUITE_PRE_DEP
24# MAPPING_CODE
25# FUNCTION CODE
26# SUITE_POST_DEP
27# DEP_CHECK_CODE
28# DISPATCH_FUNCTION
29#
30# - common helper code file - 'helpers.function'
31# Common helper functions
32#
33# - test suite code file - file name in the form 'test_suite_xxx.function'
34# Code file that contains the actual test cases. The file contains a
35# series of code sequences delimited by the following:
36# BEGIN_HEADER / END_HEADER - list of headers files
37# BEGIN_SUITE_HELPERS / END_SUITE_HELPERS - helper functions common to
38# the test suite
39# BEGIN_CASE / END_CASE - the test cases in the test suite. Each test
40# case contains at least one function that is used to create the
41# dispatch code.
42#
43# - test data file - file name in the form 'test_suite_xxxx.data'
44# The test case parameters to to be used in execution of the test. The
45# file name is used to replace the symbol 'TEST_FILENAME' in the main
46# code file above.
47#
Gilles Peskinee38900b2017-09-29 15:45:12 +020048# A test data file consists of a sequence of paragraphs separated by
49# a single empty line. Line breaks may be in Unix (LF) or Windows (CRLF)
50# format. Lines starting with the character '#' are ignored
51# (the parser behaves as if they were not present).
52#
53# Each paragraph describes one test case and must consist of: (1) one
54# line which is the test case name; (2) an optional line starting with
55# the 11-character prefix "depends_on:"; (3) a line containing the test
56# function to execute and its parameters.
57#
58# A depends_on: line consists of a list of compile-time options
59# separated by the character ':', with no whitespace. The test case
60# is executed only if this compilation option is enabled in config.h.
61#
62# The last line of each paragraph contains a test function name and
63# a list of parameters separated by the character ':'. Running the
64# test case calls this function with the specified parameters. Each
65# parameter may either be an integer written in decimal or hexadecimal,
66# or a string surrounded by double quotes which may not contain the
67# ':' character.
SimonB0284f582016-02-15 23:27:28 +000068#
Paul Bakker367dae42009-06-28 21:50:27 +000069
70use strict;
71
72my $suite_dir = shift or die "Missing suite directory";
73my $suite_name = shift or die "Missing suite name";
Paul Bakker46c17942011-07-13 14:54:54 +000074my $data_name = shift or die "Missing data name";
Rich Evansf4253c72015-01-14 19:23:00 +000075my $test_main_file = do { my $arg = shift; defined($arg) ? $arg : $suite_dir."/main_test.function" };
Paul Bakker46c17942011-07-13 14:54:54 +000076my $test_file = $data_name.".c";
SimonB0284f582016-02-15 23:27:28 +000077my $test_common_helper_file = $suite_dir."/helpers.function";
Paul Bakker367dae42009-06-28 21:50:27 +000078my $test_case_file = $suite_dir."/".$suite_name.".function";
Paul Bakker19343182013-08-16 13:31:10 +020079my $test_case_data = $suite_dir."/".$data_name.".data";
Paul Bakker367dae42009-06-28 21:50:27 +000080
81my $line_separator = $/;
82undef $/;
83
SimonB0284f582016-02-15 23:27:28 +000084open(TEST_HELPERS, "$test_common_helper_file") or die "Opening test helpers
85'$test_common_helper_file': $!";
86my $test_common_helpers = <TEST_HELPERS>;
Paul Bakker367dae42009-06-28 21:50:27 +000087close(TEST_HELPERS);
88
Paul Bakker19343182013-08-16 13:31:10 +020089open(TEST_MAIN, "$test_main_file") or die "Opening test main '$test_main_file': $!";
90my $test_main = <TEST_MAIN>;
91close(TEST_MAIN);
92
Paul Bakker367dae42009-06-28 21:50:27 +000093open(TEST_CASES, "$test_case_file") or die "Opening test cases '$test_case_file': $!";
94my $test_cases = <TEST_CASES>;
95close(TEST_CASES);
Paul Bakker19343182013-08-16 13:31:10 +020096
97open(TEST_DATA, "$test_case_data") or die "Opening test data '$test_case_data': $!";
98my $test_data = <TEST_DATA>;
99close(TEST_DATA);
100
Paul Bakker33b43f12013-08-20 11:48:36 +0200101my ( $suite_header ) = $test_cases =~ /\/\* BEGIN_HEADER \*\/\n(.*?)\n\/\* END_HEADER \*\//s;
102my ( $suite_defines ) = $test_cases =~ /\/\* BEGIN_DEPENDENCIES\n \* (.*?)\n \* END_DEPENDENCIES/s;
SimonB0284f582016-02-15 23:27:28 +0000103my ( $suite_helpers ) = $test_cases =~ /\/\* BEGIN_SUITE_HELPERS \*\/\n(.*?)\n\/\* END_SUITE_HELPERS \*\//s;
Paul Bakker5690efc2011-05-26 13:16:06 +0000104
105my $requirements;
106if ($suite_defines =~ /^depends_on:/)
107{
108 ( $requirements ) = $suite_defines =~ /^depends_on:(.*)$/;
109}
Paul Bakker19343182013-08-16 13:31:10 +0200110
Paul Bakker5690efc2011-05-26 13:16:06 +0000111my @var_req_arr = split(/:/, $requirements);
112my $suite_pre_code;
113my $suite_post_code;
Paul Bakker19343182013-08-16 13:31:10 +0200114my $dispatch_code;
115my $mapping_code;
116my %mapping_values;
Paul Bakker5690efc2011-05-26 13:16:06 +0000117
118while (@var_req_arr)
119{
120 my $req = shift @var_req_arr;
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +0100121 $req =~ s/(!?)(.*)/$1defined($2)/;
Paul Bakker5690efc2011-05-26 13:16:06 +0000122
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +0100123 $suite_pre_code .= "#if $req\n";
Paul Bakker5690efc2011-05-26 13:16:06 +0000124 $suite_post_code .= "#endif /* $req */\n";
125}
Paul Bakker367dae42009-06-28 21:50:27 +0000126
127$/ = $line_separator;
128
129open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!";
130print TEST_FILE << "END";
SimonB0284f582016-02-15 23:27:28 +0000131/*
132 * *** THIS FILE HAS BEEN MACHINE GENERATED ***
133 *
134 * This file has been machine generated using the script: $0
135 *
136 * Test file : $test_file
137 *
138 * The following files were used to create this file.
139 *
140 * Main code file : $test_main_file
141 * Helper file : $test_common_helper_file
142 * Test suite file : $test_case_file
Simon Butcher60411a82016-03-01 18:35:02 +0000143 * Test suite data : $test_case_data
SimonB0284f582016-02-15 23:27:28 +0000144 *
145 *
146 * This file is part of mbed TLS (https://tls.mbed.org)
147 */
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000150#include <mbedtls/config.h>
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +0200151#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +0200153#endif
Paul Bakker5690efc2011-05-26 13:16:06 +0000154
SimonB0284f582016-02-15 23:27:28 +0000155
156/*----------------------------------------------------------------------------*/
SimonB8bcd5492016-02-17 23:34:30 +0000157/* Common helper code */
SimonB0284f582016-02-15 23:27:28 +0000158
159$test_common_helpers
160
161
162/*----------------------------------------------------------------------------*/
163/* Test Suite Code */
Rich Evans00ab4702015-02-06 13:43:58 +0000164
Paul Bakkerde56ca12013-09-15 17:05:21 +0200165$suite_pre_code
Paul Bakker367dae42009-06-28 21:50:27 +0000166$suite_header
SimonB0284f582016-02-15 23:27:28 +0000167$suite_helpers
Paul Bakkerde56ca12013-09-15 17:05:21 +0200168$suite_post_code
Paul Bakker367dae42009-06-28 21:50:27 +0000169
Paul Bakker367dae42009-06-28 21:50:27 +0000170END
171
Paul Bakkerb34fef22013-08-20 12:06:33 +0200172$test_main =~ s/SUITE_PRE_DEP/$suite_pre_code/;
173$test_main =~ s/SUITE_POST_DEP/$suite_post_code/;
174
Paul Bakker33b43f12013-08-20 11:48:36 +0200175while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg)
Paul Bakker367dae42009-06-28 21:50:27 +0000176{
Paul Bakker19343182013-08-16 13:31:10 +0200177 my $function_deps = $1;
Paul Bakker33b43f12013-08-20 11:48:36 +0200178 my $function_decl = $2;
179
180 # Sanity checks of function
181 if ($function_decl !~ /^void /)
182 {
183 die "Test function does not have 'void' as return type\n";
184 }
Paul Bakker318d0fe2014-07-10 14:59:25 +0200185 if ($function_decl !~ /^void (\w+)\(\s*(.*?)\s*\)\s*{(.*)}/ms)
Paul Bakker33b43f12013-08-20 11:48:36 +0200186 {
187 die "Function declaration not in expected format\n";
188 }
189 my $function_name = $1;
190 my $function_params = $2;
Paul Bakker19343182013-08-16 13:31:10 +0200191 my $function_pre_code;
192 my $function_post_code;
Paul Bakker19343182013-08-16 13:31:10 +0200193 my $param_defs;
194 my $param_checks;
195 my @dispatch_params;
Paul Bakker33b43f12013-08-20 11:48:36 +0200196 my @var_def_arr = split(/,\s*/, $function_params);
Paul Bakker19343182013-08-16 13:31:10 +0200197 my $i = 1;
198 my $mapping_regex = "".$function_name;
199 my $mapping_count = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000200
Paul Bakker33b43f12013-08-20 11:48:36 +0200201 $function_decl =~ s/^void /void test_suite_/;
202
Paul Bakker318d0fe2014-07-10 14:59:25 +0200203 # Add exit label if not present
204 if ($function_decl !~ /^exit:$/m)
205 {
206 $function_decl =~ s/}\s*$/\nexit:\n return;\n}/;
207 }
208
Paul Bakker19343182013-08-16 13:31:10 +0200209 if ($function_deps =~ /^depends_on:/)
Paul Bakkerccff1672009-10-03 19:57:10 +0000210 {
Paul Bakker19343182013-08-16 13:31:10 +0200211 ( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/;
Paul Bakkerccff1672009-10-03 19:57:10 +0000212 }
213
Paul Bakker19343182013-08-16 13:31:10 +0200214 foreach my $req (split(/:/, $function_deps))
Paul Bakker367dae42009-06-28 21:50:27 +0000215 {
Paul Bakker19343182013-08-16 13:31:10 +0200216 $function_pre_code .= "#ifdef $req\n";
217 $function_post_code .= "#endif /* $req */\n";
Paul Bakker367dae42009-06-28 21:50:27 +0000218 }
Paul Bakker367dae42009-06-28 21:50:27 +0000219
Paul Bakker19343182013-08-16 13:31:10 +0200220 foreach my $def (@var_def_arr)
221 {
222 # Handle the different parameter types
Paul Bakker33b43f12013-08-20 11:48:36 +0200223 if( substr($def, 0, 4) eq "int " )
Paul Bakker19343182013-08-16 13:31:10 +0200224 {
225 $param_defs .= " int param$i;\n";
226 $param_checks .= " if( verify_int( params[$i], &param$i ) != 0 ) return( 2 );\n";
227 push @dispatch_params, "param$i";
Paul Bakker367dae42009-06-28 21:50:27 +0000228
Paul Bakker19343182013-08-16 13:31:10 +0200229 $mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)";
230 $mapping_count++;
231 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200232 elsif( substr($def, 0, 6) eq "char *" )
Paul Bakker19343182013-08-16 13:31:10 +0200233 {
234 $param_defs .= " char *param$i = params[$i];\n";
235 $param_checks .= " if( verify_string( &param$i ) != 0 ) return( 2 );\n";
236 push @dispatch_params, "param$i";
Andres AGf083b312017-02-02 14:36:49 +0000237 $mapping_regex .= ":(?:\\\\.|[^:\n])+";
Paul Bakker19343182013-08-16 13:31:10 +0200238 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200239 else
240 {
241 die "Parameter declaration not of supported type (int, char *)\n";
242 }
Paul Bakker19343182013-08-16 13:31:10 +0200243 $i++;
Paul Bakker367dae42009-06-28 21:50:27 +0000244
Paul Bakker19343182013-08-16 13:31:10 +0200245 }
246
247 # Find non-integer values we should map for this function
248 if( $mapping_count)
249 {
250 my @res = $test_data =~ /^$mapping_regex/msg;
251 foreach my $value (@res)
252 {
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200253 next unless ($value !~ /^\d+$/);
254 if ( $mapping_values{$value} ) {
255 ${ $mapping_values{$value} }{$function_pre_code} = 1;
256 } else {
257 $mapping_values{$value} = { $function_pre_code => 1 };
258 }
Paul Bakker19343182013-08-16 13:31:10 +0200259 }
260 }
261
262 my $call_params = join ", ", @dispatch_params;
263 my $param_count = @var_def_arr + 1;
264 $dispatch_code .= << "END";
265if( strcmp( params[0], "$function_name" ) == 0 )
266{
267$function_pre_code
268$param_defs
269 if( cnt != $param_count )
270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count );
Paul Bakker19343182013-08-16 13:31:10 +0200272 return( 2 );
273 }
274
275$param_checks
Paul Bakker33b43f12013-08-20 11:48:36 +0200276 test_suite_$function_name( $call_params );
277 return ( 0 );
Paul Bakker19343182013-08-16 13:31:10 +0200278$function_post_code
279 return ( 3 );
280}
281else
282END
283
Paul Bakker33b43f12013-08-20 11:48:36 +0200284 my $function_code = $function_pre_code . $function_decl . "\n" . $function_post_code;
285 $test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/;
Paul Bakker19343182013-08-16 13:31:10 +0200286}
287
288# Find specific case dependencies that we should be able to check
289# and make check code
290my $dep_check_code;
291
Hanno Becker276d5302017-07-23 10:24:22 +0100292my @res = $test_data =~ /^depends_on:([!:\w]+)/msg;
Paul Bakker19343182013-08-16 13:31:10 +0200293my %case_deps;
294foreach my $deps (@res)
295{
296 foreach my $dep (split(/:/, $deps))
297 {
298 $case_deps{$dep} = 1;
299 }
300}
301while( my ($key, $value) = each(%case_deps) )
302{
Hanno Becker276d5302017-07-23 10:24:22 +0100303 if( substr($key, 0, 1) eq "!" )
304 {
305 my $key = substr($key, 1);
306 $dep_check_code .= << "END";
307 if( strcmp( str, "!$key" ) == 0 )
308 {
309#if !defined($key)
310 return( 0 );
311#else
312 return( 1 );
313#endif
314 }
315END
316 }
317 else
318 {
319 $dep_check_code .= << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200320 if( strcmp( str, "$key" ) == 0 )
321 {
322#if defined($key)
323 return( 0 );
324#else
325 return( 1 );
326#endif
327 }
Paul Bakker367dae42009-06-28 21:50:27 +0000328END
Hanno Becker276d5302017-07-23 10:24:22 +0100329 }
Paul Bakker367dae42009-06-28 21:50:27 +0000330}
331
Paul Bakker19343182013-08-16 13:31:10 +0200332# Make mapping code
333while( my ($key, $value) = each(%mapping_values) )
334{
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200335 my $key_mapping_code = << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200336 if( strcmp( str, "$key" ) == 0 )
337 {
338 *value = ( $key );
339 return( 0 );
340 }
341END
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200342
343 # handle depenencies, unless used at least one without depends
344 if ($value->{""}) {
345 $mapping_code .= $key_mapping_code;
346 next;
347 }
348 for my $ifdef ( keys %$value ) {
349 (my $endif = $ifdef) =~ s!ifdef!endif //!g;
350 $mapping_code .= $ifdef . $key_mapping_code . $endif;
351 }
Paul Bakker19343182013-08-16 13:31:10 +0200352}
353
354$dispatch_code =~ s/^(.+)/ $1/mg;
355
356$test_main =~ s/TEST_FILENAME/$test_case_data/;
357$test_main =~ s/FUNCTION_CODE//;
358$test_main =~ s/DEP_CHECK_CODE/$dep_check_code/;
359$test_main =~ s/DISPATCH_FUNCTION/$dispatch_code/;
360$test_main =~ s/MAPPING_CODE/$mapping_code/;
361
Paul Bakker367dae42009-06-28 21:50:27 +0000362print TEST_FILE << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200363$test_main
Paul Bakker367dae42009-06-28 21:50:27 +0000364END
365
Paul Bakker367dae42009-06-28 21:50:27 +0000366close(TEST_FILE);