blob: 93c003b0132a5671d7f1eb463b209a93aad86e6f [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;
80my $index = 1;
81for 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;
91my $index = 1;
92for my $line (@test_cases_lines) {
93 if ($line =~ /^\/\* BEGIN_CASE .*\*\//)
94 {
95 $line = $line."#line $index \"$test_case_file\"\n";
96 }
97
98 $test_cases = $test_cases.$line;
99 $index++;
100}
101
Paul Bakker367dae42009-06-28 21:50:27 +0000102close(TEST_CASES);
Paul Bakker19343182013-08-16 13:31:10 +0200103
104open(TEST_DATA, "$test_case_data") or die "Opening test data '$test_case_data': $!";
105my $test_data = <TEST_DATA>;
106close(TEST_DATA);
107
SimonB15942102016-04-25 21:34:49 +0100108
109#
110# Find the headers, dependencies, and suites in the test cases file
111#
112
Paul Bakker33b43f12013-08-20 11:48:36 +0200113my ( $suite_header ) = $test_cases =~ /\/\* BEGIN_HEADER \*\/\n(.*?)\n\/\* END_HEADER \*\//s;
114my ( $suite_defines ) = $test_cases =~ /\/\* BEGIN_DEPENDENCIES\n \* (.*?)\n \* END_DEPENDENCIES/s;
SimonB152ea182016-02-15 23:27:28 +0000115my ( $suite_helpers ) = $test_cases =~ /\/\* BEGIN_SUITE_HELPERS \*\/\n(.*?)\n\/\* END_SUITE_HELPERS \*\//s;
Paul Bakker5690efc2011-05-26 13:16:06 +0000116
117my $requirements;
118if ($suite_defines =~ /^depends_on:/)
119{
120 ( $requirements ) = $suite_defines =~ /^depends_on:(.*)$/;
121}
Paul Bakker19343182013-08-16 13:31:10 +0200122
Paul Bakker5690efc2011-05-26 13:16:06 +0000123my @var_req_arr = split(/:/, $requirements);
124my $suite_pre_code;
125my $suite_post_code;
Paul Bakker19343182013-08-16 13:31:10 +0200126my $dispatch_code;
127my $mapping_code;
128my %mapping_values;
Paul Bakker5690efc2011-05-26 13:16:06 +0000129
130while (@var_req_arr)
131{
132 my $req = shift @var_req_arr;
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +0100133 $req =~ s/(!?)(.*)/$1defined($2)/;
Paul Bakker5690efc2011-05-26 13:16:06 +0000134
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +0100135 $suite_pre_code .= "#if $req\n";
Paul Bakker5690efc2011-05-26 13:16:06 +0000136 $suite_post_code .= "#endif /* $req */\n";
137}
Paul Bakker367dae42009-06-28 21:50:27 +0000138
139$/ = $line_separator;
140
141open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!";
142print TEST_FILE << "END";
SimonB152ea182016-02-15 23:27:28 +0000143/*
144 * *** THIS FILE HAS BEEN MACHINE GENERATED ***
145 *
146 * This file has been machine generated using the script: $0
147 *
148 * Test file : $test_file
149 *
150 * The following files were used to create this file.
151 *
152 * Main code file : $test_main_file
153 * Helper file : $test_common_helper_file
154 * Test suite file : $test_case_file
Simon Butcher64d60da2016-03-01 18:35:02 +0000155 * Test suite data : $test_case_data
SimonB152ea182016-02-15 23:27:28 +0000156 *
157 *
158 * This file is part of mbed TLS (https://tls.mbed.org)
159 */
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000162#include <mbedtls/config.h>
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +0200163#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +0200165#endif
Paul Bakker5690efc2011-05-26 13:16:06 +0000166
SimonB152ea182016-02-15 23:27:28 +0000167
168/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000169/* Common helper code */
SimonB152ea182016-02-15 23:27:28 +0000170
171$test_common_helpers
172
173
174/*----------------------------------------------------------------------------*/
175/* Test Suite Code */
Rich Evans00ab4702015-02-06 13:43:58 +0000176
Paul Bakkerde56ca12013-09-15 17:05:21 +0200177$suite_pre_code
Paul Bakker367dae42009-06-28 21:50:27 +0000178$suite_header
SimonB152ea182016-02-15 23:27:28 +0000179$suite_helpers
Paul Bakkerde56ca12013-09-15 17:05:21 +0200180$suite_post_code
Paul Bakker367dae42009-06-28 21:50:27 +0000181
Paul Bakker367dae42009-06-28 21:50:27 +0000182END
183
Paul Bakkerb34fef22013-08-20 12:06:33 +0200184$test_main =~ s/SUITE_PRE_DEP/$suite_pre_code/;
185$test_main =~ s/SUITE_POST_DEP/$suite_post_code/;
186
Paul Bakker33b43f12013-08-20 11:48:36 +0200187while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg)
Paul Bakker367dae42009-06-28 21:50:27 +0000188{
Paul Bakker19343182013-08-16 13:31:10 +0200189 my $function_deps = $1;
Paul Bakker33b43f12013-08-20 11:48:36 +0200190 my $function_decl = $2;
191
192 # Sanity checks of function
SimonB15942102016-04-25 21:34:49 +0100193 if ($function_decl !~ /^#line\s*.*\nvoid /)
Paul Bakker33b43f12013-08-20 11:48:36 +0200194 {
SimonB15942102016-04-25 21:34:49 +0100195 die "Test function does not have 'void' as return type.\n" .
196 "Function declaration:\n" .
197 $function_decl;
Paul Bakker33b43f12013-08-20 11:48:36 +0200198 }
SimonB15942102016-04-25 21:34:49 +0100199 if ($function_decl !~ /^(#line\s*.*)\nvoid (\w+)\(\s*(.*?)\s*\)\s*{(.*)}/ms)
Paul Bakker33b43f12013-08-20 11:48:36 +0200200 {
201 die "Function declaration not in expected format\n";
202 }
SimonB15942102016-04-25 21:34:49 +0100203 my $line_directive = $1;
204 my $function_name = $2;
205 my $function_params = $3;
Paul Bakker19343182013-08-16 13:31:10 +0200206 my $function_pre_code;
207 my $function_post_code;
Paul Bakker19343182013-08-16 13:31:10 +0200208 my $param_defs;
209 my $param_checks;
210 my @dispatch_params;
Paul Bakker33b43f12013-08-20 11:48:36 +0200211 my @var_def_arr = split(/,\s*/, $function_params);
Paul Bakker19343182013-08-16 13:31:10 +0200212 my $i = 1;
213 my $mapping_regex = "".$function_name;
214 my $mapping_count = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000215
SimonB15942102016-04-25 21:34:49 +0100216 $function_decl =~ s/(^#line\s*.*)\nvoid /$1\nvoid test_suite_/;
Paul Bakker33b43f12013-08-20 11:48:36 +0200217
Paul Bakker318d0fe2014-07-10 14:59:25 +0200218 # Add exit label if not present
219 if ($function_decl !~ /^exit:$/m)
220 {
221 $function_decl =~ s/}\s*$/\nexit:\n return;\n}/;
222 }
223
Paul Bakker19343182013-08-16 13:31:10 +0200224 if ($function_deps =~ /^depends_on:/)
Paul Bakkerccff1672009-10-03 19:57:10 +0000225 {
Paul Bakker19343182013-08-16 13:31:10 +0200226 ( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/;
Paul Bakkerccff1672009-10-03 19:57:10 +0000227 }
228
Paul Bakker19343182013-08-16 13:31:10 +0200229 foreach my $req (split(/:/, $function_deps))
Paul Bakker367dae42009-06-28 21:50:27 +0000230 {
Paul Bakker19343182013-08-16 13:31:10 +0200231 $function_pre_code .= "#ifdef $req\n";
232 $function_post_code .= "#endif /* $req */\n";
Paul Bakker367dae42009-06-28 21:50:27 +0000233 }
Paul Bakker367dae42009-06-28 21:50:27 +0000234
Paul Bakker19343182013-08-16 13:31:10 +0200235 foreach my $def (@var_def_arr)
236 {
237 # Handle the different parameter types
Paul Bakker33b43f12013-08-20 11:48:36 +0200238 if( substr($def, 0, 4) eq "int " )
Paul Bakker19343182013-08-16 13:31:10 +0200239 {
240 $param_defs .= " int param$i;\n";
SimonB8ca7bc42016-04-17 23:24:50 +0100241 $param_checks .= " if( verify_int( params[$i], &param$i ) != 0 ) return( DISPATCH_INVALID_TEST_DATA );\n";
Paul Bakker19343182013-08-16 13:31:10 +0200242 push @dispatch_params, "param$i";
Paul Bakker367dae42009-06-28 21:50:27 +0000243
Paul Bakker19343182013-08-16 13:31:10 +0200244 $mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)";
245 $mapping_count++;
246 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200247 elsif( substr($def, 0, 6) eq "char *" )
Paul Bakker19343182013-08-16 13:31:10 +0200248 {
249 $param_defs .= " char *param$i = params[$i];\n";
SimonB8ca7bc42016-04-17 23:24:50 +0100250 $param_checks .= " if( verify_string( &param$i ) != 0 ) return( DISPATCH_INVALID_TEST_DATA );\n";
Paul Bakker19343182013-08-16 13:31:10 +0200251 push @dispatch_params, "param$i";
Manuel Pégourié-Gonnard23c06082015-04-17 10:22:30 +0200252 $mapping_regex .= ":[^:\n]+";
Paul Bakker19343182013-08-16 13:31:10 +0200253 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200254 else
255 {
256 die "Parameter declaration not of supported type (int, char *)\n";
257 }
Paul Bakker19343182013-08-16 13:31:10 +0200258 $i++;
Paul Bakker367dae42009-06-28 21:50:27 +0000259
Paul Bakker19343182013-08-16 13:31:10 +0200260 }
261
262 # Find non-integer values we should map for this function
263 if( $mapping_count)
264 {
265 my @res = $test_data =~ /^$mapping_regex/msg;
266 foreach my $value (@res)
267 {
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200268 next unless ($value !~ /^\d+$/);
269 if ( $mapping_values{$value} ) {
270 ${ $mapping_values{$value} }{$function_pre_code} = 1;
271 } else {
272 $mapping_values{$value} = { $function_pre_code => 1 };
273 }
Paul Bakker19343182013-08-16 13:31:10 +0200274 }
275 }
276
277 my $call_params = join ", ", @dispatch_params;
278 my $param_count = @var_def_arr + 1;
279 $dispatch_code .= << "END";
280if( strcmp( params[0], "$function_name" ) == 0 )
281{
282$function_pre_code
283$param_defs
284 if( cnt != $param_count )
285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count );
SimonB8ca7bc42016-04-17 23:24:50 +0100287 return( DISPATCH_INVALID_TEST_DATA );
Paul Bakker19343182013-08-16 13:31:10 +0200288 }
289
290$param_checks
Paul Bakker33b43f12013-08-20 11:48:36 +0200291 test_suite_$function_name( $call_params );
SimonB8ca7bc42016-04-17 23:24:50 +0100292 return ( DISPATCH_TEST_SUCCESS );
Paul Bakker19343182013-08-16 13:31:10 +0200293$function_post_code
SimonB8ca7bc42016-04-17 23:24:50 +0100294 return ( DISPATCH_UNSUPPORTED_SUITE );
Paul Bakker19343182013-08-16 13:31:10 +0200295}
296else
297END
298
SimonB15942102016-04-25 21:34:49 +0100299 my $function_code = $function_pre_code . $function_decl . "\n" .
300 $function_post_code;
Paul Bakker33b43f12013-08-20 11:48:36 +0200301 $test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/;
Paul Bakker19343182013-08-16 13:31:10 +0200302}
303
304# Find specific case dependencies that we should be able to check
305# and make check code
306my $dep_check_code;
307
308my @res = $test_data =~ /^depends_on:([\w:]+)/msg;
309my %case_deps;
310foreach my $deps (@res)
311{
312 foreach my $dep (split(/:/, $deps))
313 {
314 $case_deps{$dep} = 1;
315 }
316}
317while( my ($key, $value) = each(%case_deps) )
318{
319 $dep_check_code .= << "END";
320 if( strcmp( str, "$key" ) == 0 )
321 {
322#if defined($key)
SimonB8ca7bc42016-04-17 23:24:50 +0100323 return( DEPENDENCY_SUPPORTED );
Paul Bakker19343182013-08-16 13:31:10 +0200324#else
SimonB8ca7bc42016-04-17 23:24:50 +0100325 return( DEPENDENCY_NOT_SUPPORTED );
Paul Bakker19343182013-08-16 13:31:10 +0200326#endif
327 }
Paul Bakker367dae42009-06-28 21:50:27 +0000328END
329}
330
Paul Bakker19343182013-08-16 13:31:10 +0200331# Make mapping code
332while( my ($key, $value) = each(%mapping_values) )
333{
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200334 my $key_mapping_code = << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200335 if( strcmp( str, "$key" ) == 0 )
336 {
337 *value = ( $key );
SimonB8ca7bc42016-04-17 23:24:50 +0100338 return( KEY_VALUE_MAPPING_FOUND );
Paul Bakker19343182013-08-16 13:31:10 +0200339 }
340END
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200341
342 # handle depenencies, unless used at least one without depends
343 if ($value->{""}) {
344 $mapping_code .= $key_mapping_code;
345 next;
346 }
347 for my $ifdef ( keys %$value ) {
348 (my $endif = $ifdef) =~ s!ifdef!endif //!g;
349 $mapping_code .= $ifdef . $key_mapping_code . $endif;
350 }
Paul Bakker19343182013-08-16 13:31:10 +0200351}
352
353$dispatch_code =~ s/^(.+)/ $1/mg;
354
SimonB15942102016-04-25 21:34:49 +0100355$test_main =~ s/TESTCASE_FILENAME/$test_case_data/g;
356$test_main =~ s/TESTCODE_FILENAME/$test_case_file/g;
Paul Bakker19343182013-08-16 13:31:10 +0200357$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);