Markus Pfeiffer | a26a005 | 2014-04-22 20:16:15 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
SimonB | 3ddf355 | 2016-02-10 23:50:28 +0000 | [diff] [blame] | 2 | |
| 3 | # generate_code.pl |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 4 | # |
Simon Butcher | 64d60da | 2016-03-01 18:35:02 +0000 | [diff] [blame] | 5 | # Copyright (c) 2009-2016, ARM Limited, All Rights Reserved |
Bence Szépkúti | 09b4f19 | 2020-05-26 01:54:15 +0200 | [diff] [blame^] | 6 | # 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 Butcher | 64d60da | 2016-03-01 18:35:02 +0000 | [diff] [blame] | 21 | # |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 22 | # Purpose |
| 23 | # |
SimonB | 3ddf355 | 2016-02-10 23:50:28 +0000 | [diff] [blame] | 24 | # 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] |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 29 | # |
| 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: |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 37 | # TESTCASE_FILENAME |
| 38 | # TESTCODE_FILENAME |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 39 | # SUITE_PRE_DEP |
| 40 | # MAPPING_CODE |
| 41 | # FUNCTION CODE |
| 42 | # SUITE_POST_DEP |
| 43 | # DEP_CHECK_CODE |
| 44 | # DISPATCH_FUNCTION |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 45 | # !LINE_NO! |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 46 | # |
| 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 |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 62 | # file name is used to replace the symbol 'TESTCASE_FILENAME' in the main |
| 63 | # code file above. |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 64 | # |
Gilles Peskine | b04e2c3 | 2017-09-29 15:45:12 +0200 | [diff] [blame] | 65 | # 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 Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 86 | |
| 87 | use strict; |
| 88 | |
| 89 | my $suite_dir = shift or die "Missing suite directory"; |
| 90 | my $suite_name = shift or die "Missing suite name"; |
Paul Bakker | 46c1794 | 2011-07-13 14:54:54 +0000 | [diff] [blame] | 91 | my $data_name = shift or die "Missing data name"; |
Rich Evans | f4253c7 | 2015-01-14 19:23:00 +0000 | [diff] [blame] | 92 | my $test_main_file = do { my $arg = shift; defined($arg) ? $arg : $suite_dir."/main_test.function" }; |
Paul Bakker | 46c1794 | 2011-07-13 14:54:54 +0000 | [diff] [blame] | 93 | my $test_file = $data_name.".c"; |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 94 | my $test_common_helper_file = $suite_dir."/helpers.function"; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 95 | my $test_case_file = $suite_dir."/".$suite_name.".function"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 96 | my $test_case_data = $suite_dir."/".$data_name.".data"; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 97 | |
| 98 | my $line_separator = $/; |
| 99 | undef $/; |
| 100 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 101 | |
| 102 | # |
| 103 | # Open and read in the input files |
| 104 | # |
| 105 | |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 106 | open(TEST_HELPERS, "$test_common_helper_file") or die "Opening test helpers |
| 107 | '$test_common_helper_file': $!"; |
| 108 | my $test_common_helpers = <TEST_HELPERS>; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 109 | close(TEST_HELPERS); |
| 110 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 111 | open(TEST_MAIN, "$test_main_file") or die "Opening test main '$test_main_file': $!"; |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 112 | my @test_main_lines = split/^/, <TEST_MAIN>; |
| 113 | my $test_main; |
SimonB | 43dba3d | 2016-05-02 21:31:51 +0100 | [diff] [blame] | 114 | my $index = 2; |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 115 | for my $line (@test_main_lines) { |
| 116 | $line =~ s/!LINE_NO!/$index/; |
| 117 | $test_main = $test_main.$line; |
| 118 | $index++; |
| 119 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 120 | close(TEST_MAIN); |
| 121 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 122 | open(TEST_CASES, "$test_case_file") or die "Opening test cases '$test_case_file': $!"; |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 123 | my @test_cases_lines = split/^/, <TEST_CASES>; |
| 124 | my $test_cases; |
SimonB | 43dba3d | 2016-05-02 21:31:51 +0100 | [diff] [blame] | 125 | my $index = 2; |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 126 | for my $line (@test_cases_lines) { |
SimonB | 37f2620 | 2016-05-02 21:58:19 +0100 | [diff] [blame] | 127 | if ($line =~ /^\/\* BEGIN_SUITE_HELPERS .*\*\//) |
| 128 | { |
| 129 | $line = $line."#line $index \"$test_case_file\"\n"; |
| 130 | } |
| 131 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 132 | if ($line =~ /^\/\* BEGIN_CASE .*\*\//) |
| 133 | { |
| 134 | $line = $line."#line $index \"$test_case_file\"\n"; |
| 135 | } |
| 136 | |
SimonB | c1d2eb3 | 2016-05-02 15:52:52 +0100 | [diff] [blame] | 137 | $line =~ s/!LINE_NO!/$index/; |
| 138 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 139 | $test_cases = $test_cases.$line; |
| 140 | $index++; |
| 141 | } |
| 142 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 143 | close(TEST_CASES); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 144 | |
| 145 | open(TEST_DATA, "$test_case_data") or die "Opening test data '$test_case_data': $!"; |
| 146 | my $test_data = <TEST_DATA>; |
| 147 | close(TEST_DATA); |
| 148 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 149 | |
| 150 | # |
| 151 | # Find the headers, dependencies, and suites in the test cases file |
| 152 | # |
| 153 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 154 | my ( $suite_header ) = $test_cases =~ /\/\* BEGIN_HEADER \*\/\n(.*?)\n\/\* END_HEADER \*\//s; |
| 155 | my ( $suite_defines ) = $test_cases =~ /\/\* BEGIN_DEPENDENCIES\n \* (.*?)\n \* END_DEPENDENCIES/s; |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 156 | my ( $suite_helpers ) = $test_cases =~ /\/\* BEGIN_SUITE_HELPERS \*\/\n(.*?)\n\/\* END_SUITE_HELPERS \*\//s; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 157 | |
| 158 | my $requirements; |
| 159 | if ($suite_defines =~ /^depends_on:/) |
| 160 | { |
| 161 | ( $requirements ) = $suite_defines =~ /^depends_on:(.*)$/; |
| 162 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 163 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 164 | my @var_req_arr = split(/:/, $requirements); |
| 165 | my $suite_pre_code; |
| 166 | my $suite_post_code; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 167 | my $dispatch_code; |
| 168 | my $mapping_code; |
| 169 | my %mapping_values; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 170 | |
| 171 | while (@var_req_arr) |
| 172 | { |
| 173 | my $req = shift @var_req_arr; |
Manuel Pégourié-Gonnard | e46c6c3 | 2015-03-23 13:59:10 +0100 | [diff] [blame] | 174 | $req =~ s/(!?)(.*)/$1defined($2)/; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 175 | |
Manuel Pégourié-Gonnard | e46c6c3 | 2015-03-23 13:59:10 +0100 | [diff] [blame] | 176 | $suite_pre_code .= "#if $req\n"; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 177 | $suite_post_code .= "#endif /* $req */\n"; |
| 178 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 179 | |
| 180 | $/ = $line_separator; |
| 181 | |
| 182 | open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!"; |
| 183 | print TEST_FILE << "END"; |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 184 | /* |
| 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 Butcher | 64d60da | 2016-03-01 18:35:02 +0000 | [diff] [blame] | 196 | * Test suite data : $test_case_data |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 197 | * |
| 198 | * |
| 199 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 200 | */ |
| 201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 203 | #include <mbedtls/config.h> |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 204 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 205 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 206 | #endif |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 207 | |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 208 | |
| 209 | /*----------------------------------------------------------------------------*/ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 210 | /* Common helper code */ |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 211 | |
| 212 | $test_common_helpers |
| 213 | |
| 214 | |
| 215 | /*----------------------------------------------------------------------------*/ |
| 216 | /* Test Suite Code */ |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 217 | |
Paul Bakker | de56ca1 | 2013-09-15 17:05:21 +0200 | [diff] [blame] | 218 | $suite_pre_code |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 219 | $suite_header |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 220 | $suite_helpers |
Paul Bakker | de56ca1 | 2013-09-15 17:05:21 +0200 | [diff] [blame] | 221 | $suite_post_code |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 222 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 223 | END |
| 224 | |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame] | 225 | $test_main =~ s/SUITE_PRE_DEP/$suite_pre_code/; |
| 226 | $test_main =~ s/SUITE_POST_DEP/$suite_post_code/; |
| 227 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 228 | while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 229 | { |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 230 | my $function_deps = $1; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 231 | my $function_decl = $2; |
| 232 | |
| 233 | # Sanity checks of function |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 234 | if ($function_decl !~ /^#line\s*.*\nvoid /) |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 235 | { |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 236 | die "Test function does not have 'void' as return type.\n" . |
| 237 | "Function declaration:\n" . |
| 238 | $function_decl; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 239 | } |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 240 | if ($function_decl !~ /^(#line\s*.*)\nvoid (\w+)\(\s*(.*?)\s*\)\s*{(.*)}/ms) |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 241 | { |
| 242 | die "Function declaration not in expected format\n"; |
| 243 | } |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 244 | my $line_directive = $1; |
| 245 | my $function_name = $2; |
| 246 | my $function_params = $3; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 247 | my $function_pre_code; |
| 248 | my $function_post_code; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 249 | my $param_defs; |
| 250 | my $param_checks; |
| 251 | my @dispatch_params; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 252 | my @var_def_arr = split(/,\s*/, $function_params); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 253 | my $i = 1; |
| 254 | my $mapping_regex = "".$function_name; |
| 255 | my $mapping_count = 0; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 256 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 257 | $function_decl =~ s/(^#line\s*.*)\nvoid /$1\nvoid test_suite_/; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 258 | |
Paul Bakker | 318d0fe | 2014-07-10 14:59:25 +0200 | [diff] [blame] | 259 | # 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 Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 265 | if ($function_deps =~ /^depends_on:/) |
Paul Bakker | ccff167 | 2009-10-03 19:57:10 +0000 | [diff] [blame] | 266 | { |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 267 | ( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/; |
Paul Bakker | ccff167 | 2009-10-03 19:57:10 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 270 | foreach my $req (split(/:/, $function_deps)) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 271 | { |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 272 | $function_pre_code .= "#ifdef $req\n"; |
| 273 | $function_post_code .= "#endif /* $req */\n"; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 274 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 275 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 276 | foreach my $def (@var_def_arr) |
| 277 | { |
| 278 | # Handle the different parameter types |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 279 | if( substr($def, 0, 4) eq "int " ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 280 | { |
| 281 | $param_defs .= " int param$i;\n"; |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 282 | $param_checks .= " if( verify_int( params[$i], ¶m$i ) != 0 ) return( DISPATCH_INVALID_TEST_DATA );\n"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 283 | push @dispatch_params, "param$i"; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 284 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 285 | $mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)"; |
| 286 | $mapping_count++; |
| 287 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 288 | elsif( substr($def, 0, 6) eq "char *" ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 289 | { |
| 290 | $param_defs .= " char *param$i = params[$i];\n"; |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 291 | $param_checks .= " if( verify_string( ¶m$i ) != 0 ) return( DISPATCH_INVALID_TEST_DATA );\n"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 292 | push @dispatch_params, "param$i"; |
Andres AG | 9060d4d | 2017-02-02 14:36:49 +0000 | [diff] [blame] | 293 | $mapping_regex .= ":(?:\\\\.|[^:\n])+"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 294 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 295 | else |
| 296 | { |
| 297 | die "Parameter declaration not of supported type (int, char *)\n"; |
| 298 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 299 | $i++; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 300 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 301 | } |
| 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é-Gonnard | 18c443d | 2013-10-17 14:58:24 +0200 | [diff] [blame] | 309 | 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 Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | my $call_params = join ", ", @dispatch_params; |
| 319 | my $param_count = @var_def_arr + 1; |
| 320 | $dispatch_code .= << "END"; |
| 321 | if( strcmp( params[0], "$function_name" ) == 0 ) |
| 322 | { |
| 323 | $function_pre_code |
| 324 | $param_defs |
| 325 | if( cnt != $param_count ) |
| 326 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 327 | mbedtls_fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count ); |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 328 | return( DISPATCH_INVALID_TEST_DATA ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | $param_checks |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 332 | test_suite_$function_name( $call_params ); |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 333 | return ( DISPATCH_TEST_SUCCESS ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 334 | $function_post_code |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 335 | return ( DISPATCH_UNSUPPORTED_SUITE ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 336 | } |
| 337 | else |
| 338 | END |
| 339 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 340 | my $function_code = $function_pre_code . $function_decl . "\n" . |
| 341 | $function_post_code; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 342 | $test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | # Find specific case dependencies that we should be able to check |
| 346 | # and make check code |
| 347 | my $dep_check_code; |
| 348 | |
Hanno Becker | f058f34 | 2017-07-23 10:24:22 +0100 | [diff] [blame] | 349 | my @res = $test_data =~ /^depends_on:([!:\w]+)/msg; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 350 | my %case_deps; |
| 351 | foreach my $deps (@res) |
| 352 | { |
| 353 | foreach my $dep (split(/:/, $deps)) |
| 354 | { |
| 355 | $case_deps{$dep} = 1; |
| 356 | } |
| 357 | } |
| 358 | while( my ($key, $value) = each(%case_deps) ) |
| 359 | { |
Hanno Becker | f058f34 | 2017-07-23 10:24:22 +0100 | [diff] [blame] | 360 | 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 | } |
| 372 | END |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | $dep_check_code .= << "END"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 377 | if( strcmp( str, "$key" ) == 0 ) |
| 378 | { |
| 379 | #if defined($key) |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 380 | return( DEPENDENCY_SUPPORTED ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 381 | #else |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 382 | return( DEPENDENCY_NOT_SUPPORTED ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 383 | #endif |
| 384 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 385 | END |
Hanno Becker | f058f34 | 2017-07-23 10:24:22 +0100 | [diff] [blame] | 386 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 389 | # Make mapping code |
| 390 | while( my ($key, $value) = each(%mapping_values) ) |
| 391 | { |
Manuel Pégourié-Gonnard | 18c443d | 2013-10-17 14:58:24 +0200 | [diff] [blame] | 392 | my $key_mapping_code = << "END"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 393 | if( strcmp( str, "$key" ) == 0 ) |
| 394 | { |
| 395 | *value = ( $key ); |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 396 | return( KEY_VALUE_MAPPING_FOUND ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 397 | } |
| 398 | END |
Manuel Pégourié-Gonnard | 18c443d | 2013-10-17 14:58:24 +0200 | [diff] [blame] | 399 | |
| 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 Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | $dispatch_code =~ s/^(.+)/ $1/mg; |
| 412 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 413 | $test_main =~ s/TESTCASE_FILENAME/$test_case_data/g; |
| 414 | $test_main =~ s/TESTCODE_FILENAME/$test_case_file/g; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 415 | $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 Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 420 | print TEST_FILE << "END"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 421 | $test_main |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 422 | END |
| 423 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 424 | close(TEST_FILE); |