blob: 549cca3a68880739a57c7d566967ac734c45ef3d [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#
Simon Butcher64d60da2016-03-01 18:35:02 +00005# Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
Bence Szépkúti09b4f192020-05-26 01:54:15 +02006# SPDX-License-Identifier: Apache-2.0
7#
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19#
20# This file is part of Mbed TLS (https://tls.mbed.org)
Simon Butcher64d60da2016-03-01 18:35:02 +000021#
SimonB152ea182016-02-15 23:27:28 +000022# Purpose
23#
SimonB3ddf3552016-02-10 23:50:28 +000024# Generates the test suite code given inputs of the test suite directory that
25# contain the test suites, and the test suite file names for the test code and
26# test data.
27#
28# Usage: generate_code.pl <suite dir> <code file> <data file> [main code file]
SimonB152ea182016-02-15 23:27:28 +000029#
30# Structure of files
31#
32# - main code file - 'main_test.function'
33# Template file that contains the main() function for the test suite,
34# test dispatch code as well as support functions. It contains the
35# following symbols which are substituted by this script during
36# processing:
SimonB15942102016-04-25 21:34:49 +010037# TESTCASE_FILENAME
38# TESTCODE_FILENAME
SimonB152ea182016-02-15 23:27:28 +000039# SUITE_PRE_DEP
40# MAPPING_CODE
41# FUNCTION CODE
42# SUITE_POST_DEP
43# DEP_CHECK_CODE
44# DISPATCH_FUNCTION
SimonB15942102016-04-25 21:34:49 +010045# !LINE_NO!
SimonB152ea182016-02-15 23:27:28 +000046#
47# - common helper code file - 'helpers.function'
48# Common helper functions
49#
50# - test suite code file - file name in the form 'test_suite_xxx.function'
51# Code file that contains the actual test cases. The file contains a
52# series of code sequences delimited by the following:
53# BEGIN_HEADER / END_HEADER - list of headers files
54# BEGIN_SUITE_HELPERS / END_SUITE_HELPERS - helper functions common to
55# the test suite
56# BEGIN_CASE / END_CASE - the test cases in the test suite. Each test
57# case contains at least one function that is used to create the
58# dispatch code.
59#
60# - test data file - file name in the form 'test_suite_xxxx.data'
61# The test case parameters to to be used in execution of the test. The
SimonB15942102016-04-25 21:34:49 +010062# file name is used to replace the symbol 'TESTCASE_FILENAME' in the main
63# code file above.
SimonB152ea182016-02-15 23:27:28 +000064#
Gilles Peskineb04e2c32017-09-29 15:45:12 +020065# A test data file consists of a sequence of paragraphs separated by
66# a single empty line. Line breaks may be in Unix (LF) or Windows (CRLF)
67# format. Lines starting with the character '#' are ignored
68# (the parser behaves as if they were not present).
69#
70# Each paragraph describes one test case and must consist of: (1) one
71# line which is the test case name; (2) an optional line starting with
72# the 11-character prefix "depends_on:"; (3) a line containing the test
73# function to execute and its parameters.
74#
75# A depends_on: line consists of a list of compile-time options
76# separated by the character ':', with no whitespace. The test case
77# is executed only if this compilation option is enabled in config.h.
78#
79# The last line of each paragraph contains a test function name and
80# a list of parameters separated by the character ':'. Running the
81# test case calls this function with the specified parameters. Each
82# parameter may either be an integer written in decimal or hexadecimal,
83# or a string surrounded by double quotes which may not contain the
84# ':' character.
85#
Paul Bakker367dae42009-06-28 21:50:27 +000086
87use strict;
88
89my $suite_dir = shift or die "Missing suite directory";
90my $suite_name = shift or die "Missing suite name";
Paul Bakker46c17942011-07-13 14:54:54 +000091my $data_name = shift or die "Missing data name";
Rich Evansf4253c72015-01-14 19:23:00 +000092my $test_main_file = do { my $arg = shift; defined($arg) ? $arg : $suite_dir."/main_test.function" };
Paul Bakker46c17942011-07-13 14:54:54 +000093my $test_file = $data_name.".c";
SimonB152ea182016-02-15 23:27:28 +000094my $test_common_helper_file = $suite_dir."/helpers.function";
Paul Bakker367dae42009-06-28 21:50:27 +000095my $test_case_file = $suite_dir."/".$suite_name.".function";
Paul Bakker19343182013-08-16 13:31:10 +020096my $test_case_data = $suite_dir."/".$data_name.".data";
Paul Bakker367dae42009-06-28 21:50:27 +000097
98my $line_separator = $/;
99undef $/;
100
SimonB15942102016-04-25 21:34:49 +0100101
102#
103# Open and read in the input files
104#
105
SimonB152ea182016-02-15 23:27:28 +0000106open(TEST_HELPERS, "$test_common_helper_file") or die "Opening test helpers
107'$test_common_helper_file': $!";
108my $test_common_helpers = <TEST_HELPERS>;
Paul Bakker367dae42009-06-28 21:50:27 +0000109close(TEST_HELPERS);
110
Paul Bakker19343182013-08-16 13:31:10 +0200111open(TEST_MAIN, "$test_main_file") or die "Opening test main '$test_main_file': $!";
SimonB15942102016-04-25 21:34:49 +0100112my @test_main_lines = split/^/, <TEST_MAIN>;
113my $test_main;
SimonB43dba3d2016-05-02 21:31:51 +0100114my $index = 2;
SimonB15942102016-04-25 21:34:49 +0100115for my $line (@test_main_lines) {
116 $line =~ s/!LINE_NO!/$index/;
117 $test_main = $test_main.$line;
118 $index++;
119}
Paul Bakker19343182013-08-16 13:31:10 +0200120close(TEST_MAIN);
121
Paul Bakker367dae42009-06-28 21:50:27 +0000122open(TEST_CASES, "$test_case_file") or die "Opening test cases '$test_case_file': $!";
SimonB15942102016-04-25 21:34:49 +0100123my @test_cases_lines = split/^/, <TEST_CASES>;
124my $test_cases;
SimonB43dba3d2016-05-02 21:31:51 +0100125my $index = 2;
SimonB15942102016-04-25 21:34:49 +0100126for my $line (@test_cases_lines) {
SimonB37f26202016-05-02 21:58:19 +0100127 if ($line =~ /^\/\* BEGIN_SUITE_HELPERS .*\*\//)
128 {
129 $line = $line."#line $index \"$test_case_file\"\n";
130 }
131
SimonB15942102016-04-25 21:34:49 +0100132 if ($line =~ /^\/\* BEGIN_CASE .*\*\//)
133 {
134 $line = $line."#line $index \"$test_case_file\"\n";
135 }
136
SimonBc1d2eb32016-05-02 15:52:52 +0100137 $line =~ s/!LINE_NO!/$index/;
138
SimonB15942102016-04-25 21:34:49 +0100139 $test_cases = $test_cases.$line;
140 $index++;
141}
142
Paul Bakker367dae42009-06-28 21:50:27 +0000143close(TEST_CASES);
Paul Bakker19343182013-08-16 13:31:10 +0200144
145open(TEST_DATA, "$test_case_data") or die "Opening test data '$test_case_data': $!";
146my $test_data = <TEST_DATA>;
147close(TEST_DATA);
148
SimonB15942102016-04-25 21:34:49 +0100149
150#
151# Find the headers, dependencies, and suites in the test cases file
152#
153
Paul Bakker33b43f12013-08-20 11:48:36 +0200154my ( $suite_header ) = $test_cases =~ /\/\* BEGIN_HEADER \*\/\n(.*?)\n\/\* END_HEADER \*\//s;
155my ( $suite_defines ) = $test_cases =~ /\/\* BEGIN_DEPENDENCIES\n \* (.*?)\n \* END_DEPENDENCIES/s;
SimonB152ea182016-02-15 23:27:28 +0000156my ( $suite_helpers ) = $test_cases =~ /\/\* BEGIN_SUITE_HELPERS \*\/\n(.*?)\n\/\* END_SUITE_HELPERS \*\//s;
Paul Bakker5690efc2011-05-26 13:16:06 +0000157
158my $requirements;
159if ($suite_defines =~ /^depends_on:/)
160{
161 ( $requirements ) = $suite_defines =~ /^depends_on:(.*)$/;
162}
Paul Bakker19343182013-08-16 13:31:10 +0200163
Paul Bakker5690efc2011-05-26 13:16:06 +0000164my @var_req_arr = split(/:/, $requirements);
165my $suite_pre_code;
166my $suite_post_code;
Paul Bakker19343182013-08-16 13:31:10 +0200167my $dispatch_code;
168my $mapping_code;
169my %mapping_values;
Paul Bakker5690efc2011-05-26 13:16:06 +0000170
171while (@var_req_arr)
172{
173 my $req = shift @var_req_arr;
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +0100174 $req =~ s/(!?)(.*)/$1defined($2)/;
Paul Bakker5690efc2011-05-26 13:16:06 +0000175
Manuel Pégourié-Gonnarde46c6c32015-03-23 13:59:10 +0100176 $suite_pre_code .= "#if $req\n";
Paul Bakker5690efc2011-05-26 13:16:06 +0000177 $suite_post_code .= "#endif /* $req */\n";
178}
Paul Bakker367dae42009-06-28 21:50:27 +0000179
180$/ = $line_separator;
181
182open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!";
183print TEST_FILE << "END";
SimonB152ea182016-02-15 23:27:28 +0000184/*
185 * *** THIS FILE HAS BEEN MACHINE GENERATED ***
186 *
187 * This file has been machine generated using the script: $0
188 *
189 * Test file : $test_file
190 *
191 * The following files were used to create this file.
192 *
193 * Main code file : $test_main_file
194 * Helper file : $test_common_helper_file
195 * Test suite file : $test_case_file
Simon Butcher64d60da2016-03-01 18:35:02 +0000196 * Test suite data : $test_case_data
SimonB152ea182016-02-15 23:27:28 +0000197 *
198 *
199 * This file is part of mbed TLS (https://tls.mbed.org)
200 */
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000203#include <mbedtls/config.h>
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +0200204#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +0200206#endif
Paul Bakker5690efc2011-05-26 13:16:06 +0000207
SimonB152ea182016-02-15 23:27:28 +0000208
209/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000210/* Common helper code */
SimonB152ea182016-02-15 23:27:28 +0000211
212$test_common_helpers
213
214
215/*----------------------------------------------------------------------------*/
216/* Test Suite Code */
Rich Evans00ab4702015-02-06 13:43:58 +0000217
Paul Bakkerde56ca12013-09-15 17:05:21 +0200218$suite_pre_code
Paul Bakker367dae42009-06-28 21:50:27 +0000219$suite_header
SimonB152ea182016-02-15 23:27:28 +0000220$suite_helpers
Paul Bakkerde56ca12013-09-15 17:05:21 +0200221$suite_post_code
Paul Bakker367dae42009-06-28 21:50:27 +0000222
Paul Bakker367dae42009-06-28 21:50:27 +0000223END
224
Paul Bakkerb34fef22013-08-20 12:06:33 +0200225$test_main =~ s/SUITE_PRE_DEP/$suite_pre_code/;
226$test_main =~ s/SUITE_POST_DEP/$suite_post_code/;
227
Paul Bakker33b43f12013-08-20 11:48:36 +0200228while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg)
Paul Bakker367dae42009-06-28 21:50:27 +0000229{
Paul Bakker19343182013-08-16 13:31:10 +0200230 my $function_deps = $1;
Paul Bakker33b43f12013-08-20 11:48:36 +0200231 my $function_decl = $2;
232
233 # Sanity checks of function
SimonB15942102016-04-25 21:34:49 +0100234 if ($function_decl !~ /^#line\s*.*\nvoid /)
Paul Bakker33b43f12013-08-20 11:48:36 +0200235 {
SimonB15942102016-04-25 21:34:49 +0100236 die "Test function does not have 'void' as return type.\n" .
237 "Function declaration:\n" .
238 $function_decl;
Paul Bakker33b43f12013-08-20 11:48:36 +0200239 }
SimonB15942102016-04-25 21:34:49 +0100240 if ($function_decl !~ /^(#line\s*.*)\nvoid (\w+)\(\s*(.*?)\s*\)\s*{(.*)}/ms)
Paul Bakker33b43f12013-08-20 11:48:36 +0200241 {
242 die "Function declaration not in expected format\n";
243 }
SimonB15942102016-04-25 21:34:49 +0100244 my $line_directive = $1;
245 my $function_name = $2;
246 my $function_params = $3;
Paul Bakker19343182013-08-16 13:31:10 +0200247 my $function_pre_code;
248 my $function_post_code;
Paul Bakker19343182013-08-16 13:31:10 +0200249 my $param_defs;
250 my $param_checks;
251 my @dispatch_params;
Paul Bakker33b43f12013-08-20 11:48:36 +0200252 my @var_def_arr = split(/,\s*/, $function_params);
Paul Bakker19343182013-08-16 13:31:10 +0200253 my $i = 1;
254 my $mapping_regex = "".$function_name;
255 my $mapping_count = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000256
SimonB15942102016-04-25 21:34:49 +0100257 $function_decl =~ s/(^#line\s*.*)\nvoid /$1\nvoid test_suite_/;
Paul Bakker33b43f12013-08-20 11:48:36 +0200258
Paul Bakker318d0fe2014-07-10 14:59:25 +0200259 # Add exit label if not present
260 if ($function_decl !~ /^exit:$/m)
261 {
262 $function_decl =~ s/}\s*$/\nexit:\n return;\n}/;
263 }
264
Paul Bakker19343182013-08-16 13:31:10 +0200265 if ($function_deps =~ /^depends_on:/)
Paul Bakkerccff1672009-10-03 19:57:10 +0000266 {
Paul Bakker19343182013-08-16 13:31:10 +0200267 ( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/;
Paul Bakkerccff1672009-10-03 19:57:10 +0000268 }
269
Paul Bakker19343182013-08-16 13:31:10 +0200270 foreach my $req (split(/:/, $function_deps))
Paul Bakker367dae42009-06-28 21:50:27 +0000271 {
Paul Bakker19343182013-08-16 13:31:10 +0200272 $function_pre_code .= "#ifdef $req\n";
273 $function_post_code .= "#endif /* $req */\n";
Paul Bakker367dae42009-06-28 21:50:27 +0000274 }
Paul Bakker367dae42009-06-28 21:50:27 +0000275
Paul Bakker19343182013-08-16 13:31:10 +0200276 foreach my $def (@var_def_arr)
277 {
278 # Handle the different parameter types
Paul Bakker33b43f12013-08-20 11:48:36 +0200279 if( substr($def, 0, 4) eq "int " )
Paul Bakker19343182013-08-16 13:31:10 +0200280 {
281 $param_defs .= " int param$i;\n";
SimonB8ca7bc42016-04-17 23:24:50 +0100282 $param_checks .= " if( verify_int( params[$i], &param$i ) != 0 ) return( DISPATCH_INVALID_TEST_DATA );\n";
Paul Bakker19343182013-08-16 13:31:10 +0200283 push @dispatch_params, "param$i";
Paul Bakker367dae42009-06-28 21:50:27 +0000284
Paul Bakker19343182013-08-16 13:31:10 +0200285 $mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)";
286 $mapping_count++;
287 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200288 elsif( substr($def, 0, 6) eq "char *" )
Paul Bakker19343182013-08-16 13:31:10 +0200289 {
290 $param_defs .= " char *param$i = params[$i];\n";
SimonB8ca7bc42016-04-17 23:24:50 +0100291 $param_checks .= " if( verify_string( &param$i ) != 0 ) return( DISPATCH_INVALID_TEST_DATA );\n";
Paul Bakker19343182013-08-16 13:31:10 +0200292 push @dispatch_params, "param$i";
Andres AG9060d4d2017-02-02 14:36:49 +0000293 $mapping_regex .= ":(?:\\\\.|[^:\n])+";
Paul Bakker19343182013-08-16 13:31:10 +0200294 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200295 else
296 {
297 die "Parameter declaration not of supported type (int, char *)\n";
298 }
Paul Bakker19343182013-08-16 13:31:10 +0200299 $i++;
Paul Bakker367dae42009-06-28 21:50:27 +0000300
Paul Bakker19343182013-08-16 13:31:10 +0200301 }
302
303 # Find non-integer values we should map for this function
304 if( $mapping_count)
305 {
306 my @res = $test_data =~ /^$mapping_regex/msg;
307 foreach my $value (@res)
308 {
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200309 next unless ($value !~ /^\d+$/);
310 if ( $mapping_values{$value} ) {
311 ${ $mapping_values{$value} }{$function_pre_code} = 1;
312 } else {
313 $mapping_values{$value} = { $function_pre_code => 1 };
314 }
Paul Bakker19343182013-08-16 13:31:10 +0200315 }
316 }
317
318 my $call_params = join ", ", @dispatch_params;
319 my $param_count = @var_def_arr + 1;
320 $dispatch_code .= << "END";
321if( strcmp( params[0], "$function_name" ) == 0 )
322{
323$function_pre_code
324$param_defs
325 if( cnt != $param_count )
326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count );
SimonB8ca7bc42016-04-17 23:24:50 +0100328 return( DISPATCH_INVALID_TEST_DATA );
Paul Bakker19343182013-08-16 13:31:10 +0200329 }
330
331$param_checks
Paul Bakker33b43f12013-08-20 11:48:36 +0200332 test_suite_$function_name( $call_params );
SimonB8ca7bc42016-04-17 23:24:50 +0100333 return ( DISPATCH_TEST_SUCCESS );
Paul Bakker19343182013-08-16 13:31:10 +0200334$function_post_code
SimonB8ca7bc42016-04-17 23:24:50 +0100335 return ( DISPATCH_UNSUPPORTED_SUITE );
Paul Bakker19343182013-08-16 13:31:10 +0200336}
337else
338END
339
SimonB15942102016-04-25 21:34:49 +0100340 my $function_code = $function_pre_code . $function_decl . "\n" .
341 $function_post_code;
Paul Bakker33b43f12013-08-20 11:48:36 +0200342 $test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/;
Paul Bakker19343182013-08-16 13:31:10 +0200343}
344
345# Find specific case dependencies that we should be able to check
346# and make check code
347my $dep_check_code;
348
Hanno Beckerf058f342017-07-23 10:24:22 +0100349my @res = $test_data =~ /^depends_on:([!:\w]+)/msg;
Paul Bakker19343182013-08-16 13:31:10 +0200350my %case_deps;
351foreach my $deps (@res)
352{
353 foreach my $dep (split(/:/, $deps))
354 {
355 $case_deps{$dep} = 1;
356 }
357}
358while( my ($key, $value) = each(%case_deps) )
359{
Hanno Beckerf058f342017-07-23 10:24:22 +0100360 if( substr($key, 0, 1) eq "!" )
361 {
362 my $key = substr($key, 1);
363 $dep_check_code .= << "END";
364 if( strcmp( str, "!$key" ) == 0 )
365 {
366#if !defined($key)
367 return( DEPENDENCY_SUPPORTED );
368#else
369 return( DEPENDENCY_NOT_SUPPORTED );
370#endif
371 }
372END
373 }
374 else
375 {
376 $dep_check_code .= << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200377 if( strcmp( str, "$key" ) == 0 )
378 {
379#if defined($key)
SimonB8ca7bc42016-04-17 23:24:50 +0100380 return( DEPENDENCY_SUPPORTED );
Paul Bakker19343182013-08-16 13:31:10 +0200381#else
SimonB8ca7bc42016-04-17 23:24:50 +0100382 return( DEPENDENCY_NOT_SUPPORTED );
Paul Bakker19343182013-08-16 13:31:10 +0200383#endif
384 }
Paul Bakker367dae42009-06-28 21:50:27 +0000385END
Hanno Beckerf058f342017-07-23 10:24:22 +0100386 }
Paul Bakker367dae42009-06-28 21:50:27 +0000387}
388
Paul Bakker19343182013-08-16 13:31:10 +0200389# Make mapping code
390while( my ($key, $value) = each(%mapping_values) )
391{
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200392 my $key_mapping_code = << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200393 if( strcmp( str, "$key" ) == 0 )
394 {
395 *value = ( $key );
SimonB8ca7bc42016-04-17 23:24:50 +0100396 return( KEY_VALUE_MAPPING_FOUND );
Paul Bakker19343182013-08-16 13:31:10 +0200397 }
398END
Manuel Pégourié-Gonnard18c443d2013-10-17 14:58:24 +0200399
400 # handle depenencies, unless used at least one without depends
401 if ($value->{""}) {
402 $mapping_code .= $key_mapping_code;
403 next;
404 }
405 for my $ifdef ( keys %$value ) {
406 (my $endif = $ifdef) =~ s!ifdef!endif //!g;
407 $mapping_code .= $ifdef . $key_mapping_code . $endif;
408 }
Paul Bakker19343182013-08-16 13:31:10 +0200409}
410
411$dispatch_code =~ s/^(.+)/ $1/mg;
412
SimonB15942102016-04-25 21:34:49 +0100413$test_main =~ s/TESTCASE_FILENAME/$test_case_data/g;
414$test_main =~ s/TESTCODE_FILENAME/$test_case_file/g;
Paul Bakker19343182013-08-16 13:31:10 +0200415$test_main =~ s/FUNCTION_CODE//;
416$test_main =~ s/DEP_CHECK_CODE/$dep_check_code/;
417$test_main =~ s/DISPATCH_FUNCTION/$dispatch_code/;
418$test_main =~ s/MAPPING_CODE/$mapping_code/;
419
Paul Bakker367dae42009-06-28 21:50:27 +0000420print TEST_FILE << "END";
Paul Bakker19343182013-08-16 13:31:10 +0200421$test_main
Paul Bakker367dae42009-06-28 21:50:27 +0000422END
423
Paul Bakker367dae42009-06-28 21:50:27 +0000424close(TEST_FILE);