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 | # |
Bence Szépkúti | 44bfbe3 | 2020-08-19 16:54:51 +0200 | [diff] [blame] | 5 | # Copyright The Mbed TLS Contributors |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 6 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 7 | # |
| 8 | # This file is provided under the Apache License 2.0, or the |
| 9 | # GNU General Public License v2.0 or later. |
| 10 | # |
| 11 | # ********** |
| 12 | # Apache License 2.0: |
Bence Szépkúti | 09b4f19 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 13 | # |
| 14 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 15 | # not use this file except in compliance with the License. |
| 16 | # You may obtain a copy of the License at |
| 17 | # |
| 18 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | # |
| 20 | # Unless required by applicable law or agreed to in writing, software |
| 21 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 22 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 23 | # See the License for the specific language governing permissions and |
| 24 | # limitations under the License. |
| 25 | # |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 26 | # ********** |
| 27 | # |
| 28 | # ********** |
| 29 | # GNU General Public License v2.0 or later: |
| 30 | # |
| 31 | # This program is free software; you can redistribute it and/or modify |
| 32 | # it under the terms of the GNU General Public License as published by |
| 33 | # the Free Software Foundation; either version 2 of the License, or |
| 34 | # (at your option) any later version. |
| 35 | # |
| 36 | # This program is distributed in the hope that it will be useful, |
| 37 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 38 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 39 | # GNU General Public License for more details. |
| 40 | # |
| 41 | # You should have received a copy of the GNU General Public License along |
| 42 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 43 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 44 | # |
| 45 | # ********** |
| 46 | # |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 47 | # Purpose |
| 48 | # |
SimonB | 3ddf355 | 2016-02-10 23:50:28 +0000 | [diff] [blame] | 49 | # Generates the test suite code given inputs of the test suite directory that |
| 50 | # contain the test suites, and the test suite file names for the test code and |
| 51 | # test data. |
| 52 | # |
| 53 | # Usage: generate_code.pl <suite dir> <code file> <data file> [main code file] |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 54 | # |
| 55 | # Structure of files |
| 56 | # |
| 57 | # - main code file - 'main_test.function' |
| 58 | # Template file that contains the main() function for the test suite, |
| 59 | # test dispatch code as well as support functions. It contains the |
| 60 | # following symbols which are substituted by this script during |
| 61 | # processing: |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 62 | # TESTCASE_FILENAME |
| 63 | # TESTCODE_FILENAME |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 64 | # SUITE_PRE_DEP |
| 65 | # MAPPING_CODE |
| 66 | # FUNCTION CODE |
| 67 | # SUITE_POST_DEP |
| 68 | # DEP_CHECK_CODE |
| 69 | # DISPATCH_FUNCTION |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 70 | # !LINE_NO! |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 71 | # |
| 72 | # - common helper code file - 'helpers.function' |
| 73 | # Common helper functions |
| 74 | # |
| 75 | # - test suite code file - file name in the form 'test_suite_xxx.function' |
| 76 | # Code file that contains the actual test cases. The file contains a |
| 77 | # series of code sequences delimited by the following: |
| 78 | # BEGIN_HEADER / END_HEADER - list of headers files |
| 79 | # BEGIN_SUITE_HELPERS / END_SUITE_HELPERS - helper functions common to |
| 80 | # the test suite |
| 81 | # BEGIN_CASE / END_CASE - the test cases in the test suite. Each test |
| 82 | # case contains at least one function that is used to create the |
| 83 | # dispatch code. |
| 84 | # |
| 85 | # - test data file - file name in the form 'test_suite_xxxx.data' |
| 86 | # 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] | 87 | # file name is used to replace the symbol 'TESTCASE_FILENAME' in the main |
| 88 | # code file above. |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 89 | # |
Gilles Peskine | b04e2c3 | 2017-09-29 15:45:12 +0200 | [diff] [blame] | 90 | # A test data file consists of a sequence of paragraphs separated by |
| 91 | # a single empty line. Line breaks may be in Unix (LF) or Windows (CRLF) |
| 92 | # format. Lines starting with the character '#' are ignored |
| 93 | # (the parser behaves as if they were not present). |
| 94 | # |
| 95 | # Each paragraph describes one test case and must consist of: (1) one |
| 96 | # line which is the test case name; (2) an optional line starting with |
| 97 | # the 11-character prefix "depends_on:"; (3) a line containing the test |
| 98 | # function to execute and its parameters. |
| 99 | # |
| 100 | # A depends_on: line consists of a list of compile-time options |
| 101 | # separated by the character ':', with no whitespace. The test case |
| 102 | # is executed only if this compilation option is enabled in config.h. |
| 103 | # |
| 104 | # The last line of each paragraph contains a test function name and |
| 105 | # a list of parameters separated by the character ':'. Running the |
| 106 | # test case calls this function with the specified parameters. Each |
| 107 | # parameter may either be an integer written in decimal or hexadecimal, |
| 108 | # or a string surrounded by double quotes which may not contain the |
| 109 | # ':' character. |
| 110 | # |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 111 | |
| 112 | use strict; |
| 113 | |
| 114 | my $suite_dir = shift or die "Missing suite directory"; |
| 115 | my $suite_name = shift or die "Missing suite name"; |
Paul Bakker | 46c1794 | 2011-07-13 14:54:54 +0000 | [diff] [blame] | 116 | my $data_name = shift or die "Missing data name"; |
Rich Evans | f4253c7 | 2015-01-14 19:23:00 +0000 | [diff] [blame] | 117 | 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] | 118 | my $test_file = $data_name.".c"; |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 119 | my $test_common_helper_file = $suite_dir."/helpers.function"; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 120 | my $test_case_file = $suite_dir."/".$suite_name.".function"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 121 | my $test_case_data = $suite_dir."/".$data_name.".data"; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 122 | |
| 123 | my $line_separator = $/; |
| 124 | undef $/; |
| 125 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 126 | |
| 127 | # |
| 128 | # Open and read in the input files |
| 129 | # |
| 130 | |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 131 | open(TEST_HELPERS, "$test_common_helper_file") or die "Opening test helpers |
| 132 | '$test_common_helper_file': $!"; |
| 133 | my $test_common_helpers = <TEST_HELPERS>; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 134 | close(TEST_HELPERS); |
| 135 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 136 | 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] | 137 | my @test_main_lines = split/^/, <TEST_MAIN>; |
| 138 | my $test_main; |
SimonB | 43dba3d | 2016-05-02 21:31:51 +0100 | [diff] [blame] | 139 | my $index = 2; |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 140 | for my $line (@test_main_lines) { |
| 141 | $line =~ s/!LINE_NO!/$index/; |
| 142 | $test_main = $test_main.$line; |
| 143 | $index++; |
| 144 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 145 | close(TEST_MAIN); |
| 146 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 147 | 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] | 148 | my @test_cases_lines = split/^/, <TEST_CASES>; |
| 149 | my $test_cases; |
SimonB | 43dba3d | 2016-05-02 21:31:51 +0100 | [diff] [blame] | 150 | my $index = 2; |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 151 | for my $line (@test_cases_lines) { |
SimonB | 37f2620 | 2016-05-02 21:58:19 +0100 | [diff] [blame] | 152 | if ($line =~ /^\/\* BEGIN_SUITE_HELPERS .*\*\//) |
| 153 | { |
| 154 | $line = $line."#line $index \"$test_case_file\"\n"; |
| 155 | } |
| 156 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 157 | if ($line =~ /^\/\* BEGIN_CASE .*\*\//) |
| 158 | { |
| 159 | $line = $line."#line $index \"$test_case_file\"\n"; |
| 160 | } |
| 161 | |
SimonB | c1d2eb3 | 2016-05-02 15:52:52 +0100 | [diff] [blame] | 162 | $line =~ s/!LINE_NO!/$index/; |
| 163 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 164 | $test_cases = $test_cases.$line; |
| 165 | $index++; |
| 166 | } |
| 167 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 168 | close(TEST_CASES); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 169 | |
| 170 | open(TEST_DATA, "$test_case_data") or die "Opening test data '$test_case_data': $!"; |
| 171 | my $test_data = <TEST_DATA>; |
| 172 | close(TEST_DATA); |
| 173 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 174 | |
| 175 | # |
| 176 | # Find the headers, dependencies, and suites in the test cases file |
| 177 | # |
| 178 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 179 | my ( $suite_header ) = $test_cases =~ /\/\* BEGIN_HEADER \*\/\n(.*?)\n\/\* END_HEADER \*\//s; |
| 180 | my ( $suite_defines ) = $test_cases =~ /\/\* BEGIN_DEPENDENCIES\n \* (.*?)\n \* END_DEPENDENCIES/s; |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 181 | 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] | 182 | |
| 183 | my $requirements; |
| 184 | if ($suite_defines =~ /^depends_on:/) |
| 185 | { |
| 186 | ( $requirements ) = $suite_defines =~ /^depends_on:(.*)$/; |
| 187 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 188 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 189 | my @var_req_arr = split(/:/, $requirements); |
| 190 | my $suite_pre_code; |
| 191 | my $suite_post_code; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 192 | my $dispatch_code; |
| 193 | my $mapping_code; |
| 194 | my %mapping_values; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 195 | |
| 196 | while (@var_req_arr) |
| 197 | { |
| 198 | my $req = shift @var_req_arr; |
Manuel Pégourié-Gonnard | e46c6c3 | 2015-03-23 13:59:10 +0100 | [diff] [blame] | 199 | $req =~ s/(!?)(.*)/$1defined($2)/; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 200 | |
Manuel Pégourié-Gonnard | e46c6c3 | 2015-03-23 13:59:10 +0100 | [diff] [blame] | 201 | $suite_pre_code .= "#if $req\n"; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 202 | $suite_post_code .= "#endif /* $req */\n"; |
| 203 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 204 | |
| 205 | $/ = $line_separator; |
| 206 | |
| 207 | open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!"; |
| 208 | print TEST_FILE << "END"; |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 209 | /* |
| 210 | * *** THIS FILE HAS BEEN MACHINE GENERATED *** |
| 211 | * |
| 212 | * This file has been machine generated using the script: $0 |
| 213 | * |
| 214 | * Test file : $test_file |
| 215 | * |
| 216 | * The following files were used to create this file. |
| 217 | * |
| 218 | * Main code file : $test_main_file |
| 219 | * Helper file : $test_common_helper_file |
| 220 | * Test suite file : $test_case_file |
Simon Butcher | 64d60da | 2016-03-01 18:35:02 +0000 | [diff] [blame] | 221 | * Test suite data : $test_case_data |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 222 | * |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 223 | */ |
| 224 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 226 | #include <mbedtls/config.h> |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 227 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 229 | #endif |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 230 | |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 231 | |
| 232 | /*----------------------------------------------------------------------------*/ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 233 | /* Common helper code */ |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 234 | |
| 235 | $test_common_helpers |
| 236 | |
| 237 | |
| 238 | /*----------------------------------------------------------------------------*/ |
| 239 | /* Test Suite Code */ |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 240 | |
Paul Bakker | de56ca1 | 2013-09-15 17:05:21 +0200 | [diff] [blame] | 241 | $suite_pre_code |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 242 | $suite_header |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 243 | $suite_helpers |
Paul Bakker | de56ca1 | 2013-09-15 17:05:21 +0200 | [diff] [blame] | 244 | $suite_post_code |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 245 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 246 | END |
| 247 | |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame] | 248 | $test_main =~ s/SUITE_PRE_DEP/$suite_pre_code/; |
| 249 | $test_main =~ s/SUITE_POST_DEP/$suite_post_code/; |
| 250 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 251 | while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 252 | { |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 253 | my $function_deps = $1; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 254 | my $function_decl = $2; |
| 255 | |
| 256 | # Sanity checks of function |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 257 | if ($function_decl !~ /^#line\s*.*\nvoid /) |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 258 | { |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 259 | die "Test function does not have 'void' as return type.\n" . |
| 260 | "Function declaration:\n" . |
| 261 | $function_decl; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 262 | } |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 263 | if ($function_decl !~ /^(#line\s*.*)\nvoid (\w+)\(\s*(.*?)\s*\)\s*{(.*)}/ms) |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 264 | { |
| 265 | die "Function declaration not in expected format\n"; |
| 266 | } |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 267 | my $line_directive = $1; |
| 268 | my $function_name = $2; |
| 269 | my $function_params = $3; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 270 | my $function_pre_code; |
| 271 | my $function_post_code; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 272 | my $param_defs; |
| 273 | my $param_checks; |
| 274 | my @dispatch_params; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 275 | my @var_def_arr = split(/,\s*/, $function_params); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 276 | my $i = 1; |
| 277 | my $mapping_regex = "".$function_name; |
| 278 | my $mapping_count = 0; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 279 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 280 | $function_decl =~ s/(^#line\s*.*)\nvoid /$1\nvoid test_suite_/; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 281 | |
Paul Bakker | 318d0fe | 2014-07-10 14:59:25 +0200 | [diff] [blame] | 282 | # Add exit label if not present |
| 283 | if ($function_decl !~ /^exit:$/m) |
| 284 | { |
| 285 | $function_decl =~ s/}\s*$/\nexit:\n return;\n}/; |
| 286 | } |
| 287 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 288 | if ($function_deps =~ /^depends_on:/) |
Paul Bakker | ccff167 | 2009-10-03 19:57:10 +0000 | [diff] [blame] | 289 | { |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 290 | ( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/; |
Paul Bakker | ccff167 | 2009-10-03 19:57:10 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 293 | foreach my $req (split(/:/, $function_deps)) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 294 | { |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 295 | $function_pre_code .= "#ifdef $req\n"; |
| 296 | $function_post_code .= "#endif /* $req */\n"; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 297 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 298 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 299 | foreach my $def (@var_def_arr) |
| 300 | { |
| 301 | # Handle the different parameter types |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 302 | if( substr($def, 0, 4) eq "int " ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 303 | { |
| 304 | $param_defs .= " int param$i;\n"; |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 305 | $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] | 306 | push @dispatch_params, "param$i"; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 307 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 308 | $mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)"; |
| 309 | $mapping_count++; |
| 310 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 311 | elsif( substr($def, 0, 6) eq "char *" ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 312 | { |
| 313 | $param_defs .= " char *param$i = params[$i];\n"; |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 314 | $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] | 315 | push @dispatch_params, "param$i"; |
Andres AG | 9060d4d | 2017-02-02 14:36:49 +0000 | [diff] [blame] | 316 | $mapping_regex .= ":(?:\\\\.|[^:\n])+"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 317 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 318 | else |
| 319 | { |
| 320 | die "Parameter declaration not of supported type (int, char *)\n"; |
| 321 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 322 | $i++; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 323 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | # Find non-integer values we should map for this function |
| 327 | if( $mapping_count) |
| 328 | { |
| 329 | my @res = $test_data =~ /^$mapping_regex/msg; |
| 330 | foreach my $value (@res) |
| 331 | { |
Manuel Pégourié-Gonnard | 18c443d | 2013-10-17 14:58:24 +0200 | [diff] [blame] | 332 | next unless ($value !~ /^\d+$/); |
| 333 | if ( $mapping_values{$value} ) { |
| 334 | ${ $mapping_values{$value} }{$function_pre_code} = 1; |
| 335 | } else { |
| 336 | $mapping_values{$value} = { $function_pre_code => 1 }; |
| 337 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | my $call_params = join ", ", @dispatch_params; |
| 342 | my $param_count = @var_def_arr + 1; |
| 343 | $dispatch_code .= << "END"; |
| 344 | if( strcmp( params[0], "$function_name" ) == 0 ) |
| 345 | { |
| 346 | $function_pre_code |
| 347 | $param_defs |
| 348 | if( cnt != $param_count ) |
| 349 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | mbedtls_fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count ); |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 351 | return( DISPATCH_INVALID_TEST_DATA ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | $param_checks |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 355 | test_suite_$function_name( $call_params ); |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 356 | return ( DISPATCH_TEST_SUCCESS ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 357 | $function_post_code |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 358 | return ( DISPATCH_UNSUPPORTED_SUITE ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 359 | } |
| 360 | else |
| 361 | END |
| 362 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 363 | my $function_code = $function_pre_code . $function_decl . "\n" . |
| 364 | $function_post_code; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 365 | $test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | # Find specific case dependencies that we should be able to check |
| 369 | # and make check code |
| 370 | my $dep_check_code; |
| 371 | |
Hanno Becker | f058f34 | 2017-07-23 10:24:22 +0100 | [diff] [blame] | 372 | my @res = $test_data =~ /^depends_on:([!:\w]+)/msg; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 373 | my %case_deps; |
| 374 | foreach my $deps (@res) |
| 375 | { |
| 376 | foreach my $dep (split(/:/, $deps)) |
| 377 | { |
| 378 | $case_deps{$dep} = 1; |
| 379 | } |
| 380 | } |
| 381 | while( my ($key, $value) = each(%case_deps) ) |
| 382 | { |
Hanno Becker | f058f34 | 2017-07-23 10:24:22 +0100 | [diff] [blame] | 383 | if( substr($key, 0, 1) eq "!" ) |
| 384 | { |
| 385 | my $key = substr($key, 1); |
| 386 | $dep_check_code .= << "END"; |
| 387 | if( strcmp( str, "!$key" ) == 0 ) |
| 388 | { |
| 389 | #if !defined($key) |
| 390 | return( DEPENDENCY_SUPPORTED ); |
| 391 | #else |
| 392 | return( DEPENDENCY_NOT_SUPPORTED ); |
| 393 | #endif |
| 394 | } |
| 395 | END |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | $dep_check_code .= << "END"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 400 | if( strcmp( str, "$key" ) == 0 ) |
| 401 | { |
| 402 | #if defined($key) |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 403 | return( DEPENDENCY_SUPPORTED ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 404 | #else |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 405 | return( DEPENDENCY_NOT_SUPPORTED ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 406 | #endif |
| 407 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 408 | END |
Hanno Becker | f058f34 | 2017-07-23 10:24:22 +0100 | [diff] [blame] | 409 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 412 | # Make mapping code |
| 413 | while( my ($key, $value) = each(%mapping_values) ) |
| 414 | { |
Manuel Pégourié-Gonnard | 18c443d | 2013-10-17 14:58:24 +0200 | [diff] [blame] | 415 | my $key_mapping_code = << "END"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 416 | if( strcmp( str, "$key" ) == 0 ) |
| 417 | { |
| 418 | *value = ( $key ); |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 419 | return( KEY_VALUE_MAPPING_FOUND ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 420 | } |
| 421 | END |
Manuel Pégourié-Gonnard | 18c443d | 2013-10-17 14:58:24 +0200 | [diff] [blame] | 422 | |
| 423 | # handle depenencies, unless used at least one without depends |
| 424 | if ($value->{""}) { |
| 425 | $mapping_code .= $key_mapping_code; |
| 426 | next; |
| 427 | } |
| 428 | for my $ifdef ( keys %$value ) { |
| 429 | (my $endif = $ifdef) =~ s!ifdef!endif //!g; |
| 430 | $mapping_code .= $ifdef . $key_mapping_code . $endif; |
| 431 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | $dispatch_code =~ s/^(.+)/ $1/mg; |
| 435 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 436 | $test_main =~ s/TESTCASE_FILENAME/$test_case_data/g; |
| 437 | $test_main =~ s/TESTCODE_FILENAME/$test_case_file/g; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 438 | $test_main =~ s/FUNCTION_CODE//; |
| 439 | $test_main =~ s/DEP_CHECK_CODE/$dep_check_code/; |
| 440 | $test_main =~ s/DISPATCH_FUNCTION/$dispatch_code/; |
| 441 | $test_main =~ s/MAPPING_CODE/$mapping_code/; |
| 442 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 443 | print TEST_FILE << "END"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 444 | $test_main |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 445 | END |
| 446 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 447 | close(TEST_FILE); |