Mohammad Azim Khan | 1ec7e6f | 2018-04-11 23:46:37 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 2 | # Test suites code generator. |
| 3 | # |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | # Copyright The Mbed TLS Contributors |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | # not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 18 | |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 19 | """ |
Azim Khan | aee05bb | 2018-07-02 16:01:04 +0100 | [diff] [blame] | 20 | This script is a key part of Mbed TLS test suites framework. For |
| 21 | understanding the script it is important to understand the |
| 22 | framework. This doc string contains a summary of the framework |
| 23 | and explains the function of this script. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 24 | |
Azim Khan | aee05bb | 2018-07-02 16:01:04 +0100 | [diff] [blame] | 25 | Mbed TLS test suites: |
| 26 | ===================== |
| 27 | Scope: |
| 28 | ------ |
| 29 | The test suites focus on unit testing the crypto primitives and also |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 30 | include x509 parser tests. Tests can be added to test any Mbed TLS |
Azim Khan | aee05bb | 2018-07-02 16:01:04 +0100 | [diff] [blame] | 31 | module. However, the framework is not capable of testing SSL |
| 32 | protocol, since that requires full stack execution and that is best |
| 33 | tested as part of the system test. |
| 34 | |
| 35 | Test case definition: |
| 36 | --------------------- |
| 37 | Tests are defined in a test_suite_<module>[.<optional sub module>].data |
| 38 | file. A test definition contains: |
| 39 | test name |
| 40 | optional build macro dependencies |
| 41 | test function |
| 42 | test parameters |
| 43 | |
| 44 | Test dependencies are build macros that can be specified to indicate |
| 45 | the build config in which the test is valid. For example if a test |
| 46 | depends on a feature that is only enabled by defining a macro. Then |
| 47 | that macro should be specified as a dependency of the test. |
| 48 | |
| 49 | Test function is the function that implements the test steps. This |
| 50 | function is specified for different tests that perform same steps |
| 51 | with different parameters. |
| 52 | |
| 53 | Test parameters are specified in string form separated by ':'. |
| 54 | Parameters can be of type string, binary data specified as hex |
| 55 | string and integer constants specified as integer, macro or |
| 56 | as an expression. Following is an example test definition: |
| 57 | |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 58 | AES 128 GCM Encrypt and decrypt 8 bytes |
| 59 | depends_on:MBEDTLS_AES_C:MBEDTLS_GCM_C |
| 60 | enc_dec_buf:MBEDTLS_CIPHER_AES_128_GCM:"AES-128-GCM":128:8:-1 |
Azim Khan | aee05bb | 2018-07-02 16:01:04 +0100 | [diff] [blame] | 61 | |
| 62 | Test functions: |
| 63 | --------------- |
| 64 | Test functions are coded in C in test_suite_<module>.function files. |
| 65 | Functions file is itself not compilable and contains special |
| 66 | format patterns to specify test suite dependencies, start and end |
| 67 | of functions and function dependencies. Check any existing functions |
| 68 | file for example. |
| 69 | |
| 70 | Execution: |
| 71 | ---------- |
| 72 | Tests are executed in 3 steps: |
| 73 | - Generating test_suite_<module>[.<optional sub module>].c file |
| 74 | for each corresponding .data file. |
| 75 | - Building each source file into executables. |
| 76 | - Running each executable and printing report. |
| 77 | |
| 78 | Generating C test source requires more than just the test functions. |
| 79 | Following extras are required: |
| 80 | - Process main() |
| 81 | - Reading .data file and dispatching test cases. |
| 82 | - Platform specific test case execution |
| 83 | - Dependency checking |
| 84 | - Integer expression evaluation |
| 85 | - Test function dispatch |
| 86 | |
| 87 | Build dependencies and integer expressions (in the test parameters) |
| 88 | are specified as strings in the .data file. Their run time value is |
| 89 | not known at the generation stage. Hence, they need to be translated |
| 90 | into run time evaluations. This script generates the run time checks |
| 91 | for dependencies and integer expressions. |
| 92 | |
| 93 | Similarly, function names have to be translated into function calls. |
| 94 | This script also generates code for function dispatch. |
| 95 | |
| 96 | The extra code mentioned here is either generated by this script |
| 97 | or it comes from the input files: helpers file, platform file and |
| 98 | the template file. |
| 99 | |
| 100 | Helper file: |
| 101 | ------------ |
| 102 | Helpers file contains common helper/utility functions and data. |
| 103 | |
| 104 | Platform file: |
| 105 | -------------- |
| 106 | Platform file contains platform specific setup code and test case |
| 107 | dispatch code. For example, host_test.function reads test data |
| 108 | file from host's file system and dispatches tests. |
Azim Khan | aee05bb | 2018-07-02 16:01:04 +0100 | [diff] [blame] | 109 | |
| 110 | Template file: |
| 111 | --------- |
| 112 | Template file for example main_test.function is a template C file in |
| 113 | which generated code and code from input files is substituted to |
| 114 | generate a compilable C file. It also contains skeleton functions for |
| 115 | dependency checks, expression evaluation and function dispatch. These |
| 116 | functions are populated with checks and return codes by this script. |
| 117 | |
| 118 | Template file contains "replacement" fields that are formatted |
Mohammad Azim Khan | 5cb7017 | 2018-07-19 11:32:30 +0100 | [diff] [blame] | 119 | strings processed by Python string.Template.substitute() method. |
Azim Khan | aee05bb | 2018-07-02 16:01:04 +0100 | [diff] [blame] | 120 | |
| 121 | This script: |
| 122 | ============ |
| 123 | Core function of this script is to fill the template file with |
| 124 | code that is generated or read from helpers and platform files. |
| 125 | |
| 126 | This script replaces following fields in the template and generates |
| 127 | the test source file: |
| 128 | |
David Horstmann | 8eff06f | 2022-11-09 17:27:33 +0000 | [diff] [blame] | 129 | __MBEDTLS_TEST_TEMPLATE__TEST_COMMON_HELPERS |
| 130 | All common code from helpers.function |
| 131 | is substituted here. |
| 132 | __MBEDTLS_TEST_TEMPLATE__FUNCTIONS_CODE |
| 133 | Test functions are substituted here |
| 134 | from the input test_suit_xyz.function |
| 135 | file. C preprocessor checks are generated |
| 136 | for the build dependencies specified |
| 137 | in the input file. This script also |
| 138 | generates wrappers for the test |
| 139 | functions with code to expand the |
| 140 | string parameters read from the data |
| 141 | file. |
| 142 | __MBEDTLS_TEST_TEMPLATE__EXPRESSION_CODE |
| 143 | This script enumerates the |
| 144 | expressions in the .data file and |
| 145 | generates code to handle enumerated |
| 146 | expression Ids and return the values. |
| 147 | __MBEDTLS_TEST_TEMPLATE__DEP_CHECK_CODE |
| 148 | This script enumerates all |
| 149 | build dependencies and generate |
| 150 | code to handle enumerated build |
| 151 | dependency Id and return status: if |
| 152 | the dependency is defined or not. |
| 153 | __MBEDTLS_TEST_TEMPLATE__DISPATCH_CODE |
| 154 | This script enumerates the functions |
| 155 | specified in the input test data file |
| 156 | and generates the initializer for the |
| 157 | function table in the template |
| 158 | file. |
| 159 | __MBEDTLS_TEST_TEMPLATE__PLATFORM_CODE |
| 160 | Platform specific setup and test |
| 161 | dispatch code. |
Azim Khan | aee05bb | 2018-07-02 16:01:04 +0100 | [diff] [blame] | 162 | |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 163 | """ |
| 164 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 165 | |
Mohammad Azim Khan | 1ec7e6f | 2018-04-11 23:46:37 +0100 | [diff] [blame] | 166 | import io |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 167 | import os |
| 168 | import re |
Mohammad Azim Khan | 1ec7e6f | 2018-04-11 23:46:37 +0100 | [diff] [blame] | 169 | import sys |
Mohammad Azim Khan | 5cb7017 | 2018-07-19 11:32:30 +0100 | [diff] [blame] | 170 | import string |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 171 | import argparse |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 172 | |
| 173 | |
Gilles Peskine | ba72662 | 2022-12-04 15:57:49 +0100 | [diff] [blame] | 174 | # Types regognized as signed integer arguments in test functions. |
| 175 | SIGNED_INTEGER_TYPES = frozenset([ |
| 176 | 'char', |
| 177 | 'short', |
| 178 | 'short int', |
| 179 | 'int', |
| 180 | 'int8_t', |
| 181 | 'int16_t', |
| 182 | 'int32_t', |
| 183 | 'int64_t', |
| 184 | 'intmax_t', |
| 185 | 'long', |
| 186 | 'long int', |
| 187 | 'long long int', |
| 188 | 'mbedtls_mpi_sint', |
| 189 | 'psa_status_t', |
| 190 | ]) |
Gilles Peskine | 615be63 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 191 | # Types recognized as string arguments in test functions. |
| 192 | STRING_TYPES = frozenset(['char*', 'const char*', 'char const*']) |
| 193 | # Types recognized as hex data arguments in test functions. |
| 194 | DATA_TYPES = frozenset(['data_t*', 'const data_t*', 'data_t const*']) |
| 195 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 196 | BEGIN_HEADER_REGEX = r'/\*\s*BEGIN_HEADER\s*\*/' |
| 197 | END_HEADER_REGEX = r'/\*\s*END_HEADER\s*\*/' |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 198 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 199 | BEGIN_SUITE_HELPERS_REGEX = r'/\*\s*BEGIN_SUITE_HELPERS\s*\*/' |
| 200 | END_SUITE_HELPERS_REGEX = r'/\*\s*END_SUITE_HELPERS\s*\*/' |
Mohammad Azim Khan | b522929 | 2018-02-06 13:08:01 +0000 | [diff] [blame] | 201 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 202 | BEGIN_DEP_REGEX = r'BEGIN_DEPENDENCIES' |
| 203 | END_DEP_REGEX = r'END_DEPENDENCIES' |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 204 | |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 205 | BEGIN_CASE_REGEX = r'/\*\s*BEGIN_CASE\s*(?P<depends_on>.*?)\s*\*/' |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 206 | END_CASE_REGEX = r'/\*\s*END_CASE\s*\*/' |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 207 | |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 208 | DEPENDENCY_REGEX = r'depends_on:(?P<dependencies>.*)' |
Ron Eldor | b9b3813 | 2018-11-27 16:35:20 +0200 | [diff] [blame] | 209 | C_IDENTIFIER_REGEX = r'!?[a-z_][a-z0-9_]*' |
| 210 | CONDITION_OPERATOR_REGEX = r'[!=]=|[<>]=?' |
| 211 | # forbid 0ddd which might be accidentally octal or accidentally decimal |
| 212 | CONDITION_VALUE_REGEX = r'[-+]?(0x[0-9a-f]+|0|[1-9][0-9]*)' |
| 213 | CONDITION_REGEX = r'({})(?:\s*({})\s*({}))?$'.format(C_IDENTIFIER_REGEX, |
| 214 | CONDITION_OPERATOR_REGEX, |
| 215 | CONDITION_VALUE_REGEX) |
Azim Khan | fcdf685 | 2018-07-05 17:31:46 +0100 | [diff] [blame] | 216 | TEST_FUNCTION_VALIDATION_REGEX = r'\s*void\s+(?P<func_name>\w+)\s*\(' |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 217 | FUNCTION_ARG_LIST_END_REGEX = r'.*\)' |
| 218 | EXIT_LABEL_REGEX = r'^exit:' |
| 219 | |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 220 | |
Mohammad Azim Khan | 3b06f22 | 2018-06-26 14:35:25 +0100 | [diff] [blame] | 221 | class GeneratorInputError(Exception): |
| 222 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 223 | Exception to indicate error in the input files to this script. |
| 224 | This includes missing patterns, test function names and other |
| 225 | parsing errors. |
Mohammad Azim Khan | 3b06f22 | 2018-06-26 14:35:25 +0100 | [diff] [blame] | 226 | """ |
| 227 | pass |
| 228 | |
| 229 | |
Gilles Peskine | 184c096 | 2020-03-24 18:25:17 +0100 | [diff] [blame] | 230 | class FileWrapper(io.FileIO): |
Azim Khan | 4b54323 | 2017-06-30 09:35:21 +0100 | [diff] [blame] | 231 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 232 | This class extends built-in io.FileIO class with attribute line_no, |
| 233 | that indicates line number for the line that is read. |
Azim Khan | 4b54323 | 2017-06-30 09:35:21 +0100 | [diff] [blame] | 234 | """ |
| 235 | |
| 236 | def __init__(self, file_name): |
| 237 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 238 | Instantiate the base class and initialize the line number to 0. |
Mohammad Azim Khan | b73159d | 2018-06-13 16:31:26 +0100 | [diff] [blame] | 239 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 240 | :param file_name: File path to open. |
Azim Khan | 4b54323 | 2017-06-30 09:35:21 +0100 | [diff] [blame] | 241 | """ |
Gilles Peskine | 5f0057d | 2022-11-10 19:33:25 +0100 | [diff] [blame] | 242 | super().__init__(file_name, 'r') |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 243 | self._line_no = 0 |
Azim Khan | 4b54323 | 2017-06-30 09:35:21 +0100 | [diff] [blame] | 244 | |
Gilles Peskine | 5f0057d | 2022-11-10 19:33:25 +0100 | [diff] [blame] | 245 | def __next__(self): |
Azim Khan | 4b54323 | 2017-06-30 09:35:21 +0100 | [diff] [blame] | 246 | """ |
Gilles Peskine | 5f0057d | 2022-11-10 19:33:25 +0100 | [diff] [blame] | 247 | This method overrides base class's __next__ method and extends it |
| 248 | method to count the line numbers as each line is read. |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 249 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 250 | :return: Line read from file. |
Azim Khan | 4b54323 | 2017-06-30 09:35:21 +0100 | [diff] [blame] | 251 | """ |
Gilles Peskine | 5f0057d | 2022-11-10 19:33:25 +0100 | [diff] [blame] | 252 | line = super().__next__() |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 253 | if line is not None: |
| 254 | self._line_no += 1 |
Azim Khan | 936ea93 | 2018-06-28 16:47:12 +0100 | [diff] [blame] | 255 | # Convert byte array to string with correct encoding and |
| 256 | # strip any whitespaces added in the decoding process. |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 257 | return line.decode(sys.getdefaultencoding()).rstrip() + '\n' |
Mohammad Azim Khan | 1ec7e6f | 2018-04-11 23:46:37 +0100 | [diff] [blame] | 258 | return None |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 259 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 260 | def get_line_no(self): |
| 261 | """ |
| 262 | Gives current line number. |
| 263 | """ |
| 264 | return self._line_no |
| 265 | |
| 266 | line_no = property(get_line_no) |
Azim Khan | 4b54323 | 2017-06-30 09:35:21 +0100 | [diff] [blame] | 267 | |
| 268 | |
| 269 | def split_dep(dep): |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 270 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 271 | Split NOT character '!' from dependency. Used by gen_dependencies() |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 272 | |
| 273 | :param dep: Dependency list |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 274 | :return: string tuple. Ex: ('!', MACRO) for !MACRO and ('', MACRO) for |
| 275 | MACRO. |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 276 | """ |
Azim Khan | 4b54323 | 2017-06-30 09:35:21 +0100 | [diff] [blame] | 277 | return ('!', dep[1:]) if dep[0] == '!' else ('', dep) |
| 278 | |
| 279 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 280 | def gen_dependencies(dependencies): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 281 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 282 | Test suite data and functions specifies compile time dependencies. |
| 283 | This function generates C preprocessor code from the input |
| 284 | dependency list. Caller uses the generated preprocessor code to |
| 285 | wrap dependent code. |
| 286 | A dependency in the input list can have a leading '!' character |
| 287 | to negate a condition. '!' is separated from the dependency using |
| 288 | function split_dep() and proper preprocessor check is generated |
| 289 | accordingly. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 290 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 291 | :param dependencies: List of dependencies. |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 292 | :return: if defined and endif code with macro annotations for |
| 293 | readability. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 294 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 295 | dep_start = ''.join(['#if %sdefined(%s)\n' % (x, y) for x, y in |
| 296 | map(split_dep, dependencies)]) |
| 297 | dep_end = ''.join(['#endif /* %s */\n' % |
| 298 | x for x in reversed(dependencies)]) |
Azim Khan | 4b54323 | 2017-06-30 09:35:21 +0100 | [diff] [blame] | 299 | |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 300 | return dep_start, dep_end |
| 301 | |
| 302 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 303 | def gen_dependencies_one_line(dependencies): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 304 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 305 | Similar to gen_dependencies() but generates dependency checks in one line. |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 306 | Useful for generating code with #else block. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 307 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 308 | :param dependencies: List of dependencies. |
| 309 | :return: Preprocessor check code |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 310 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 311 | defines = '#if ' if dependencies else '' |
| 312 | defines += ' && '.join(['%sdefined(%s)' % (x, y) for x, y in map( |
| 313 | split_dep, dependencies)]) |
Azim Khan | 4b54323 | 2017-06-30 09:35:21 +0100 | [diff] [blame] | 314 | return defines |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 315 | |
| 316 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 317 | def gen_function_wrapper(name, local_vars, args_dispatch): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 318 | """ |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 319 | Creates test function wrapper code. A wrapper has the code to |
| 320 | unpack parameters from parameters[] array. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 321 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 322 | :param name: Test function name |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 323 | :param local_vars: Local variables declaration code |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 324 | :param args_dispatch: List of dispatch arguments. |
| 325 | Ex: ['(char *)params[0]', '*((int *)params[1])'] |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 326 | :return: Test function wrapper. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 327 | """ |
| 328 | # Then create the wrapper |
| 329 | wrapper = ''' |
| 330 | void {name}_wrapper( void ** params ) |
| 331 | {{ |
Gilles Peskine | 7776141 | 2018-06-18 17:51:40 +0200 | [diff] [blame] | 332 | {unused_params}{locals} |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 333 | {name}( {args} ); |
| 334 | }} |
Gilles Peskine | 7776141 | 2018-06-18 17:51:40 +0200 | [diff] [blame] | 335 | '''.format(name=name, |
Mohammad Azim Khan | c3521df | 2018-06-26 14:06:52 +0100 | [diff] [blame] | 336 | unused_params='' if args_dispatch else ' (void)params;\n', |
Azim Khan | 4b54323 | 2017-06-30 09:35:21 +0100 | [diff] [blame] | 337 | args=', '.join(args_dispatch), |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 338 | locals=local_vars) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 339 | return wrapper |
| 340 | |
| 341 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 342 | def gen_dispatch(name, dependencies): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 343 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 344 | Test suite code template main_test.function defines a C function |
| 345 | array to contain test case functions. This function generates an |
| 346 | initializer entry for a function in that array. The entry is |
| 347 | composed of a compile time check for the test function |
| 348 | dependencies. At compile time the test function is assigned when |
| 349 | dependencies are met, else NULL is assigned. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 350 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 351 | :param name: Test function name |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 352 | :param dependencies: List of dependencies |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 353 | :return: Dispatch code. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 354 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 355 | if dependencies: |
| 356 | preprocessor_check = gen_dependencies_one_line(dependencies) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 357 | dispatch_code = ''' |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 358 | {preprocessor_check} |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 359 | {name}_wrapper, |
| 360 | #else |
| 361 | NULL, |
| 362 | #endif |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 363 | '''.format(preprocessor_check=preprocessor_check, name=name) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 364 | else: |
| 365 | dispatch_code = ''' |
| 366 | {name}_wrapper, |
| 367 | '''.format(name=name) |
| 368 | |
| 369 | return dispatch_code |
| 370 | |
| 371 | |
Mohammad Azim Khan | b522929 | 2018-02-06 13:08:01 +0000 | [diff] [blame] | 372 | def parse_until_pattern(funcs_f, end_regex): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 373 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 374 | Matches pattern end_regex to the lines read from the file object. |
| 375 | Returns the lines read until end pattern is matched. |
Mohammad Azim Khan | b73159d | 2018-06-13 16:31:26 +0100 | [diff] [blame] | 376 | |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 377 | :param funcs_f: file object for .function file |
Mohammad Azim Khan | b522929 | 2018-02-06 13:08:01 +0000 | [diff] [blame] | 378 | :param end_regex: Pattern to stop parsing |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 379 | :return: Lines read before the end pattern |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 380 | """ |
Azim Khan | 4b54323 | 2017-06-30 09:35:21 +0100 | [diff] [blame] | 381 | headers = '#line %d "%s"\n' % (funcs_f.line_no + 1, funcs_f.name) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 382 | for line in funcs_f: |
Mohammad Azim Khan | b522929 | 2018-02-06 13:08:01 +0000 | [diff] [blame] | 383 | if re.search(end_regex, line): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 384 | break |
| 385 | headers += line |
| 386 | else: |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 387 | raise GeneratorInputError("file: %s - end pattern [%s] not found!" % |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 388 | (funcs_f.name, end_regex)) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 389 | |
Azim Khan | 4b54323 | 2017-06-30 09:35:21 +0100 | [diff] [blame] | 390 | return headers |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 391 | |
| 392 | |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 393 | def validate_dependency(dependency): |
| 394 | """ |
| 395 | Validates a C macro and raises GeneratorInputError on invalid input. |
| 396 | :param dependency: Input macro dependency |
| 397 | :return: input dependency stripped of leading & trailing white spaces. |
| 398 | """ |
| 399 | dependency = dependency.strip() |
Ron Eldor | b9b3813 | 2018-11-27 16:35:20 +0200 | [diff] [blame] | 400 | if not re.match(CONDITION_REGEX, dependency, re.I): |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 401 | raise GeneratorInputError('Invalid dependency %s' % dependency) |
| 402 | return dependency |
| 403 | |
| 404 | |
| 405 | def parse_dependencies(inp_str): |
| 406 | """ |
| 407 | Parses dependencies out of inp_str, validates them and returns a |
| 408 | list of macros. |
| 409 | |
| 410 | :param inp_str: Input string with macros delimited by ':'. |
| 411 | :return: list of dependencies |
| 412 | """ |
Gilles Peskine | 8b02235 | 2020-03-24 18:36:56 +0100 | [diff] [blame] | 413 | dependencies = list(map(validate_dependency, inp_str.split(':'))) |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 414 | return dependencies |
| 415 | |
| 416 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 417 | def parse_suite_dependencies(funcs_f): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 418 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 419 | Parses test suite dependencies specified at the top of a |
| 420 | .function file, that starts with pattern BEGIN_DEPENDENCIES |
| 421 | and end with END_DEPENDENCIES. Dependencies are specified |
| 422 | after pattern 'depends_on:' and are delimited by ':'. |
Mohammad Azim Khan | b73159d | 2018-06-13 16:31:26 +0100 | [diff] [blame] | 423 | |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 424 | :param funcs_f: file object for .function file |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 425 | :return: List of test suite dependencies. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 426 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 427 | dependencies = [] |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 428 | for line in funcs_f: |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 429 | match = re.search(DEPENDENCY_REGEX, line.strip()) |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 430 | if match: |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 431 | try: |
| 432 | dependencies = parse_dependencies(match.group('dependencies')) |
| 433 | except GeneratorInputError as error: |
| 434 | raise GeneratorInputError( |
| 435 | str(error) + " - %s:%d" % (funcs_f.name, funcs_f.line_no)) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 436 | if re.search(END_DEP_REGEX, line): |
| 437 | break |
| 438 | else: |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 439 | raise GeneratorInputError("file: %s - end dependency pattern [%s]" |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 440 | " not found!" % (funcs_f.name, |
| 441 | END_DEP_REGEX)) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 442 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 443 | return dependencies |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 444 | |
| 445 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 446 | def parse_function_dependencies(line): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 447 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 448 | Parses function dependencies, that are in the same line as |
| 449 | comment BEGIN_CASE. Dependencies are specified after pattern |
| 450 | 'depends_on:' and are delimited by ':'. |
Mohammad Azim Khan | b73159d | 2018-06-13 16:31:26 +0100 | [diff] [blame] | 451 | |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 452 | :param line: Line from .function file that has dependencies. |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 453 | :return: List of dependencies. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 454 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 455 | dependencies = [] |
| 456 | match = re.search(BEGIN_CASE_REGEX, line) |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 457 | dep_str = match.group('depends_on') |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 458 | if dep_str: |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 459 | match = re.search(DEPENDENCY_REGEX, dep_str) |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 460 | if match: |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 461 | dependencies += parse_dependencies(match.group('dependencies')) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 462 | |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 463 | return dependencies |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 464 | |
Azim Khan | 4084ec7 | 2018-07-05 14:20:08 +0100 | [diff] [blame] | 465 | |
Gilles Peskine | 615be63 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 466 | ARGUMENT_DECLARATION_REGEX = re.compile(r'(.+?) ?(?:\bconst\b)? ?(\w+)\Z', re.S) |
Gilles Peskine | f153c56 | 2022-12-04 14:10:39 +0100 | [diff] [blame] | 467 | def parse_function_argument(arg, arg_idx, args, local_vars, args_dispatch): |
| 468 | """ |
| 469 | Parses one test function's argument declaration. |
| 470 | |
| 471 | :param arg: argument declaration. |
| 472 | :param arg_idx: current wrapper argument index. |
| 473 | :param args: accumulator of arguments' internal types. |
| 474 | :param local_vars: accumulator of internal variable declarations. |
| 475 | :param args_dispatch: accumulator of argument usage expressions. |
| 476 | :return: the number of new wrapper arguments, |
| 477 | or None if the argument declaration is invalid. |
| 478 | """ |
Gilles Peskine | 615be63 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 479 | # Normalize whitespace |
Gilles Peskine | f153c56 | 2022-12-04 14:10:39 +0100 | [diff] [blame] | 480 | arg = arg.strip() |
Gilles Peskine | 615be63 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 481 | arg = re.sub(r'\s*\*\s*', r'*', arg) |
| 482 | arg = re.sub(r'\s+', r' ', arg) |
| 483 | # Extract name and type |
| 484 | m = ARGUMENT_DECLARATION_REGEX.search(arg) |
| 485 | if not m: |
| 486 | # E.g. "int x[42]" |
| 487 | return None |
| 488 | typ, _ = m.groups() |
Gilles Peskine | ba72662 | 2022-12-04 15:57:49 +0100 | [diff] [blame] | 489 | if typ in SIGNED_INTEGER_TYPES: |
Gilles Peskine | f153c56 | 2022-12-04 14:10:39 +0100 | [diff] [blame] | 490 | args.append('int') |
Gilles Peskine | 6494d92 | 2022-12-04 15:32:54 +0100 | [diff] [blame] | 491 | args_dispatch.append('((mbedtls_test_argument_t*)params[%d])->sint' % arg_idx) |
Gilles Peskine | f153c56 | 2022-12-04 14:10:39 +0100 | [diff] [blame] | 492 | return 1 |
Gilles Peskine | 615be63 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 493 | if typ in STRING_TYPES: |
Gilles Peskine | f153c56 | 2022-12-04 14:10:39 +0100 | [diff] [blame] | 494 | args.append('char*') |
| 495 | args_dispatch.append('(char *) params[%d]' % arg_idx) |
| 496 | return 1 |
Gilles Peskine | 615be63 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 497 | if typ in DATA_TYPES: |
Gilles Peskine | f153c56 | 2022-12-04 14:10:39 +0100 | [diff] [blame] | 498 | args.append('hex') |
| 499 | # create a structure |
| 500 | pointer_initializer = '(uint8_t *) params[%d]' % arg_idx |
| 501 | len_initializer = '((mbedtls_test_argument_t*)params[%d])->len' % (arg_idx+1) |
| 502 | local_vars.append(' data_t data%d = {%s, %s};\n' % |
| 503 | (arg_idx, pointer_initializer, len_initializer)) |
| 504 | args_dispatch.append('&data%d' % arg_idx) |
| 505 | return 2 |
| 506 | return None |
| 507 | |
Gilles Peskine | 46476e0 | 2022-12-04 14:29:06 +0100 | [diff] [blame] | 508 | ARGUMENT_LIST_REGEX = re.compile(r'\((.*?)\)', re.S) |
Azim Khan | fcdf685 | 2018-07-05 17:31:46 +0100 | [diff] [blame] | 509 | def parse_function_arguments(line): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 510 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 511 | Parses test function signature for validation and generates |
| 512 | a dispatch wrapper function that translates input test vectors |
| 513 | read from the data file into test function arguments. |
Mohammad Azim Khan | b73159d | 2018-06-13 16:31:26 +0100 | [diff] [blame] | 514 | |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 515 | :param line: Line from .function file that has a function |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 516 | signature. |
Azim Khan | fcdf685 | 2018-07-05 17:31:46 +0100 | [diff] [blame] | 517 | :return: argument list, local variables for |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 518 | wrapper function and argument dispatch code. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 519 | """ |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 520 | # Process arguments, ex: <type> arg1, <type> arg2 ) |
| 521 | # This script assumes that the argument list is terminated by ')' |
| 522 | # i.e. the test functions will not have a function pointer |
| 523 | # argument. |
Gilles Peskine | 46476e0 | 2022-12-04 14:29:06 +0100 | [diff] [blame] | 524 | m = ARGUMENT_LIST_REGEX.search(line) |
| 525 | arg_list = m.group(1).strip() |
| 526 | if arg_list in ['', 'void']: |
| 527 | return [], '', [] |
| 528 | args = [] |
| 529 | local_vars = [] |
| 530 | args_dispatch = [] |
| 531 | arg_idx = 0 |
| 532 | for arg in arg_list.split(','): |
Gilles Peskine | f153c56 | 2022-12-04 14:10:39 +0100 | [diff] [blame] | 533 | indexes = parse_function_argument(arg, arg_idx, |
| 534 | args, local_vars, args_dispatch) |
| 535 | if indexes is None: |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 536 | raise ValueError("Test function arguments can only be 'int', " |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 537 | "'char *' or 'data_t'\n%s" % line) |
Gilles Peskine | f153c56 | 2022-12-04 14:10:39 +0100 | [diff] [blame] | 538 | arg_idx += indexes |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 539 | |
Gilles Peskine | 3a37f19 | 2022-12-04 14:00:32 +0100 | [diff] [blame] | 540 | return args, ''.join(local_vars), args_dispatch |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 541 | |
| 542 | |
Mohammad Azim Khan | 32cbcda | 2018-07-06 00:29:09 +0100 | [diff] [blame] | 543 | def generate_function_code(name, code, local_vars, args_dispatch, |
| 544 | dependencies): |
| 545 | """ |
| 546 | Generate function code with preprocessor checks and parameter dispatch |
| 547 | wrapper. |
| 548 | |
| 549 | :param name: Function name |
| 550 | :param code: Function code |
| 551 | :param local_vars: Local variables for function wrapper |
| 552 | :param args_dispatch: Argument dispatch code |
| 553 | :param dependencies: Preprocessor dependencies list |
| 554 | :return: Final function code |
| 555 | """ |
| 556 | # Add exit label if not present |
| 557 | if code.find('exit:') == -1: |
| 558 | split_code = code.rsplit('}', 1) |
| 559 | if len(split_code) == 2: |
| 560 | code = """exit: |
| 561 | ; |
| 562 | }""".join(split_code) |
| 563 | |
| 564 | code += gen_function_wrapper(name, local_vars, args_dispatch) |
| 565 | preprocessor_check_start, preprocessor_check_end = \ |
| 566 | gen_dependencies(dependencies) |
| 567 | return preprocessor_check_start + code + preprocessor_check_end |
| 568 | |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 569 | COMMENT_START_REGEX = re.compile(r'/[*/]') |
| 570 | |
| 571 | def skip_comments(line, stream): |
| 572 | """Remove comments in line. |
| 573 | |
| 574 | If the line contains an unfinished comment, read more lines from stream |
| 575 | until the line that contains the comment. |
| 576 | |
| 577 | :return: The original line with inner comments replaced by spaces. |
| 578 | Trailing comments and whitespace may be removed completely. |
| 579 | """ |
| 580 | pos = 0 |
| 581 | while True: |
| 582 | opening = COMMENT_START_REGEX.search(line, pos) |
| 583 | if not opening: |
| 584 | break |
| 585 | if line[opening.start(0) + 1] == '/': # //... |
| 586 | continuation = line |
Gilles Peskine | aec4bec | 2022-11-30 16:38:49 +0100 | [diff] [blame] | 587 | # Count the number of line breaks, to keep line numbers aligned |
| 588 | # in the output. |
| 589 | line_count = 1 |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 590 | while continuation.endswith('\\\n'): |
| 591 | # This errors out if the file ends with an unfinished line |
Gilles Peskine | 43febf2 | 2022-11-18 22:26:03 +0100 | [diff] [blame] | 592 | # comment. That's acceptable to not complicate the code further. |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 593 | continuation = next(stream) |
Gilles Peskine | aec4bec | 2022-11-30 16:38:49 +0100 | [diff] [blame] | 594 | line_count += 1 |
| 595 | return line[:opening.start(0)].rstrip() + '\n' * line_count |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 596 | # Parsing /*...*/, looking for the end |
| 597 | closing = line.find('*/', opening.end(0)) |
| 598 | while closing == -1: |
| 599 | # This errors out if the file ends with an unfinished block |
Gilles Peskine | 43febf2 | 2022-11-18 22:26:03 +0100 | [diff] [blame] | 600 | # comment. That's acceptable to not complicate the code further. |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 601 | line += next(stream) |
| 602 | closing = line.find('*/', opening.end(0)) |
| 603 | pos = closing + 2 |
Gilles Peskine | 7e8d4b6 | 2022-11-18 22:27:37 +0100 | [diff] [blame] | 604 | # Replace inner comment by spaces. There needs to be at least one space |
| 605 | # for things like 'int/*ihatespaces*/foo'. Go further and preserve the |
Gilles Peskine | 07995fd | 2022-11-29 22:03:32 +0100 | [diff] [blame] | 606 | # width of the comment and line breaks, this way positions in error |
| 607 | # messages remain correct. |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 608 | line = (line[:opening.start(0)] + |
Gilles Peskine | 07995fd | 2022-11-29 22:03:32 +0100 | [diff] [blame] | 609 | re.sub(r'.', r' ', line[opening.start(0):pos]) + |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 610 | line[pos:]) |
Gilles Peskine | 07995fd | 2022-11-29 22:03:32 +0100 | [diff] [blame] | 611 | # Strip whitespace at the end of lines (it's irrelevant to error messages). |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 612 | return re.sub(r' +(\n|\Z)', r'\1', line) |
Mohammad Azim Khan | 32cbcda | 2018-07-06 00:29:09 +0100 | [diff] [blame] | 613 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 614 | def parse_function_code(funcs_f, dependencies, suite_dependencies): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 615 | """ |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 616 | Parses out a function from function file object and generates |
| 617 | function and dispatch code. |
Mohammad Azim Khan | b73159d | 2018-06-13 16:31:26 +0100 | [diff] [blame] | 618 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 619 | :param funcs_f: file object of the functions file. |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 620 | :param dependencies: List of dependencies |
| 621 | :param suite_dependencies: List of test suite dependencies |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 622 | :return: Function name, arguments, function code and dispatch code. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 623 | """ |
Azim Khan | fcdf685 | 2018-07-05 17:31:46 +0100 | [diff] [blame] | 624 | line_directive = '#line %d "%s"\n' % (funcs_f.line_no + 1, funcs_f.name) |
| 625 | code = '' |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 626 | has_exit_label = False |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 627 | for line in funcs_f: |
Azim Khan | fcdf685 | 2018-07-05 17:31:46 +0100 | [diff] [blame] | 628 | # Check function signature. Function signature may be split |
| 629 | # across multiple lines. Here we try to find the start of |
| 630 | # arguments list, then remove '\n's and apply the regex to |
| 631 | # detect function start. |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 632 | line = skip_comments(line, funcs_f) |
Azim Khan | fcdf685 | 2018-07-05 17:31:46 +0100 | [diff] [blame] | 633 | up_to_arg_list_start = code + line[:line.find('(') + 1] |
| 634 | match = re.match(TEST_FUNCTION_VALIDATION_REGEX, |
| 635 | up_to_arg_list_start.replace('\n', ' '), re.I) |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 636 | if match: |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 637 | # check if we have full signature i.e. split in more lines |
Azim Khan | fcdf685 | 2018-07-05 17:31:46 +0100 | [diff] [blame] | 638 | name = match.group('func_name') |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 639 | if not re.match(FUNCTION_ARG_LIST_END_REGEX, line): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 640 | for lin in funcs_f: |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 641 | line += skip_comments(lin, funcs_f) |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 642 | if re.search(FUNCTION_ARG_LIST_END_REGEX, line): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 643 | break |
Azim Khan | fcdf685 | 2018-07-05 17:31:46 +0100 | [diff] [blame] | 644 | args, local_vars, args_dispatch = parse_function_arguments( |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 645 | line) |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 646 | code += line |
Azim Khan | fcdf685 | 2018-07-05 17:31:46 +0100 | [diff] [blame] | 647 | break |
| 648 | code += line |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 649 | else: |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 650 | raise GeneratorInputError("file: %s - Test functions not found!" % |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 651 | funcs_f.name) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 652 | |
Azim Khan | fcdf685 | 2018-07-05 17:31:46 +0100 | [diff] [blame] | 653 | # Prefix test function name with 'test_' |
| 654 | code = code.replace(name, 'test_' + name, 1) |
| 655 | name = 'test_' + name |
| 656 | |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 657 | for line in funcs_f: |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 658 | if re.search(END_CASE_REGEX, line): |
| 659 | break |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 660 | if not has_exit_label: |
| 661 | has_exit_label = \ |
| 662 | re.search(EXIT_LABEL_REGEX, line.strip()) is not None |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 663 | code += line |
| 664 | else: |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 665 | raise GeneratorInputError("file: %s - end case pattern [%s] not " |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 666 | "found!" % (funcs_f.name, END_CASE_REGEX)) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 667 | |
Mohammad Azim Khan | 32cbcda | 2018-07-06 00:29:09 +0100 | [diff] [blame] | 668 | code = line_directive + code |
| 669 | code = generate_function_code(name, code, local_vars, args_dispatch, |
| 670 | dependencies) |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 671 | dispatch_code = gen_dispatch(name, suite_dependencies + dependencies) |
Mohammad Azim Khan | 32cbcda | 2018-07-06 00:29:09 +0100 | [diff] [blame] | 672 | return (name, args, code, dispatch_code) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 673 | |
| 674 | |
| 675 | def parse_functions(funcs_f): |
| 676 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 677 | Parses a test_suite_xxx.function file and returns information |
| 678 | for generating a C source file for the test suite. |
Mohammad Azim Khan | b73159d | 2018-06-13 16:31:26 +0100 | [diff] [blame] | 679 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 680 | :param funcs_f: file object of the functions file. |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 681 | :return: List of test suite dependencies, test function dispatch |
| 682 | code, function code and a dict with function identifiers |
| 683 | and arguments info. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 684 | """ |
Mohammad Azim Khan | b522929 | 2018-02-06 13:08:01 +0000 | [diff] [blame] | 685 | suite_helpers = '' |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 686 | suite_dependencies = [] |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 687 | suite_functions = '' |
| 688 | func_info = {} |
| 689 | function_idx = 0 |
| 690 | dispatch_code = '' |
| 691 | for line in funcs_f: |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 692 | if re.search(BEGIN_HEADER_REGEX, line): |
Mohammad Azim Khan | 32cbcda | 2018-07-06 00:29:09 +0100 | [diff] [blame] | 693 | suite_helpers += parse_until_pattern(funcs_f, END_HEADER_REGEX) |
Mohammad Azim Khan | b522929 | 2018-02-06 13:08:01 +0000 | [diff] [blame] | 694 | elif re.search(BEGIN_SUITE_HELPERS_REGEX, line): |
Mohammad Azim Khan | 32cbcda | 2018-07-06 00:29:09 +0100 | [diff] [blame] | 695 | suite_helpers += parse_until_pattern(funcs_f, |
| 696 | END_SUITE_HELPERS_REGEX) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 697 | elif re.search(BEGIN_DEP_REGEX, line): |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 698 | suite_dependencies += parse_suite_dependencies(funcs_f) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 699 | elif re.search(BEGIN_CASE_REGEX, line): |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 700 | try: |
| 701 | dependencies = parse_function_dependencies(line) |
| 702 | except GeneratorInputError as error: |
| 703 | raise GeneratorInputError( |
| 704 | "%s:%d: %s" % (funcs_f.name, funcs_f.line_no, |
| 705 | str(error))) |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 706 | func_name, args, func_code, func_dispatch =\ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 707 | parse_function_code(funcs_f, dependencies, suite_dependencies) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 708 | suite_functions += func_code |
| 709 | # Generate dispatch code and enumeration info |
Mohammad Azim Khan | 3b06f22 | 2018-06-26 14:35:25 +0100 | [diff] [blame] | 710 | if func_name in func_info: |
| 711 | raise GeneratorInputError( |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 712 | "file: %s - function %s re-declared at line %d" % |
Mohammad Azim Khan | 3b06f22 | 2018-06-26 14:35:25 +0100 | [diff] [blame] | 713 | (funcs_f.name, func_name, funcs_f.line_no)) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 714 | func_info[func_name] = (function_idx, args) |
| 715 | dispatch_code += '/* Function Id: %d */\n' % function_idx |
| 716 | dispatch_code += func_dispatch |
| 717 | function_idx += 1 |
| 718 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 719 | func_code = (suite_helpers + |
| 720 | suite_functions).join(gen_dependencies(suite_dependencies)) |
| 721 | return suite_dependencies, dispatch_code, func_code, func_info |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 722 | |
| 723 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 724 | def escaped_split(inp_str, split_char): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 725 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 726 | Split inp_str on character split_char but ignore if escaped. |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 727 | Since, return value is used to write back to the intermediate |
| 728 | data file, any escape characters in the input are retained in the |
| 729 | output. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 730 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 731 | :param inp_str: String to split |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 732 | :param split_char: Split character |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 733 | :return: List of splits |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 734 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 735 | if len(split_char) > 1: |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 736 | raise ValueError('Expected split character. Found string!') |
Azim Khan | 6302813 | 2018-07-05 17:53:11 +0100 | [diff] [blame] | 737 | out = re.sub(r'(\\.)|' + split_char, |
| 738 | lambda m: m.group(1) or '\n', inp_str, |
| 739 | len(inp_str)).split('\n') |
Mohammad Azim Khan | 32cbcda | 2018-07-06 00:29:09 +0100 | [diff] [blame] | 740 | out = [x for x in out if x] |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 741 | return out |
| 742 | |
| 743 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 744 | def parse_test_data(data_f): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 745 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 746 | Parses .data file for each test case name, test function name, |
| 747 | test dependencies and test arguments. This information is |
| 748 | correlated with the test functions file for generating an |
| 749 | intermediate data file replacing the strings for test function |
| 750 | names, dependencies and integer constant expressions with |
| 751 | identifiers. Mainly for optimising space for on-target |
| 752 | execution. |
Mohammad Azim Khan | b73159d | 2018-06-13 16:31:26 +0100 | [diff] [blame] | 753 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 754 | :param data_f: file object of the data file. |
Gilles Peskine | f122aed | 2022-12-03 22:58:52 +0100 | [diff] [blame] | 755 | :return: Generator that yields line number, test name, function name, |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 756 | dependency list and function argument list. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 757 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 758 | __state_read_name = 0 |
| 759 | __state_read_args = 1 |
| 760 | state = __state_read_name |
| 761 | dependencies = [] |
Azim Khan | 5e2ac1f | 2017-07-03 13:58:20 +0100 | [diff] [blame] | 762 | name = '' |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 763 | for line in data_f: |
| 764 | line = line.strip() |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 765 | # Skip comments |
| 766 | if line.startswith('#'): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 767 | continue |
| 768 | |
Azim Khan | 5e2ac1f | 2017-07-03 13:58:20 +0100 | [diff] [blame] | 769 | # Blank line indicates end of test |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 770 | if not line: |
| 771 | if state == __state_read_args: |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 772 | raise GeneratorInputError("[%s:%d] Newline before arguments. " |
| 773 | "Test function and arguments " |
| 774 | "missing for %s" % |
| 775 | (data_f.name, data_f.line_no, name)) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 776 | continue |
| 777 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 778 | if state == __state_read_name: |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 779 | # Read test name |
| 780 | name = line |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 781 | state = __state_read_args |
| 782 | elif state == __state_read_args: |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 783 | # Check dependencies |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 784 | match = re.search(DEPENDENCY_REGEX, line) |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 785 | if match: |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 786 | try: |
| 787 | dependencies = parse_dependencies( |
| 788 | match.group('dependencies')) |
| 789 | except GeneratorInputError as error: |
| 790 | raise GeneratorInputError( |
| 791 | str(error) + " - %s:%d" % |
| 792 | (data_f.name, data_f.line_no)) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 793 | else: |
| 794 | # Read test vectors |
| 795 | parts = escaped_split(line, ':') |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 796 | test_function = parts[0] |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 797 | args = parts[1:] |
Gilles Peskine | f122aed | 2022-12-03 22:58:52 +0100 | [diff] [blame] | 798 | yield data_f.line_no, name, test_function, dependencies, args |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 799 | dependencies = [] |
| 800 | state = __state_read_name |
| 801 | if state == __state_read_args: |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 802 | raise GeneratorInputError("[%s:%d] Newline before arguments. " |
| 803 | "Test function and arguments missing for " |
| 804 | "%s" % (data_f.name, data_f.line_no, name)) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 805 | |
| 806 | |
| 807 | def gen_dep_check(dep_id, dep): |
| 808 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 809 | Generate code for checking dependency with the associated |
| 810 | identifier. |
Mohammad Azim Khan | b73159d | 2018-06-13 16:31:26 +0100 | [diff] [blame] | 811 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 812 | :param dep_id: Dependency identifier |
| 813 | :param dep: Dependency macro |
| 814 | :return: Dependency check code |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 815 | """ |
Mohammad Azim Khan | 3b06f22 | 2018-06-26 14:35:25 +0100 | [diff] [blame] | 816 | if dep_id < 0: |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 817 | raise GeneratorInputError("Dependency Id should be a positive " |
| 818 | "integer.") |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 819 | _not, dep = ('!', dep[1:]) if dep[0] == '!' else ('', dep) |
| 820 | if not dep: |
Mohammad Azim Khan | 3b06f22 | 2018-06-26 14:35:25 +0100 | [diff] [blame] | 821 | raise GeneratorInputError("Dependency should not be an empty string.") |
Ron Eldor | b9b3813 | 2018-11-27 16:35:20 +0200 | [diff] [blame] | 822 | |
| 823 | dependency = re.match(CONDITION_REGEX, dep, re.I) |
| 824 | if not dependency: |
| 825 | raise GeneratorInputError('Invalid dependency %s' % dep) |
| 826 | |
| 827 | _defined = '' if dependency.group(2) else 'defined' |
| 828 | _cond = dependency.group(2) if dependency.group(2) else '' |
| 829 | _value = dependency.group(3) if dependency.group(3) else '' |
| 830 | |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 831 | dep_check = ''' |
Azim Khan | b1c2d0f | 2017-07-07 17:14:02 +0100 | [diff] [blame] | 832 | case {id}: |
| 833 | {{ |
Ron Eldor | b9b3813 | 2018-11-27 16:35:20 +0200 | [diff] [blame] | 834 | #if {_not}{_defined}({macro}{_cond}{_value}) |
Azim Khan | b1c2d0f | 2017-07-07 17:14:02 +0100 | [diff] [blame] | 835 | ret = DEPENDENCY_SUPPORTED; |
Azim Khan | d61b837 | 2017-07-10 11:54:01 +0100 | [diff] [blame] | 836 | #else |
Azim Khan | b1c2d0f | 2017-07-07 17:14:02 +0100 | [diff] [blame] | 837 | ret = DEPENDENCY_NOT_SUPPORTED; |
Azim Khan | d61b837 | 2017-07-10 11:54:01 +0100 | [diff] [blame] | 838 | #endif |
Azim Khan | b1c2d0f | 2017-07-07 17:14:02 +0100 | [diff] [blame] | 839 | }} |
Ron Eldor | b9b3813 | 2018-11-27 16:35:20 +0200 | [diff] [blame] | 840 | break;'''.format(_not=_not, _defined=_defined, |
| 841 | macro=dependency.group(1), id=dep_id, |
| 842 | _cond=_cond, _value=_value) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 843 | return dep_check |
| 844 | |
| 845 | |
| 846 | def gen_expression_check(exp_id, exp): |
| 847 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 848 | Generates code for evaluating an integer expression using |
| 849 | associated expression Id. |
Mohammad Azim Khan | b73159d | 2018-06-13 16:31:26 +0100 | [diff] [blame] | 850 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 851 | :param exp_id: Expression Identifier |
| 852 | :param exp: Expression/Macro |
| 853 | :return: Expression check code |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 854 | """ |
Mohammad Azim Khan | 3b06f22 | 2018-06-26 14:35:25 +0100 | [diff] [blame] | 855 | if exp_id < 0: |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 856 | raise GeneratorInputError("Expression Id should be a positive " |
| 857 | "integer.") |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 858 | if not exp: |
Mohammad Azim Khan | 3b06f22 | 2018-06-26 14:35:25 +0100 | [diff] [blame] | 859 | raise GeneratorInputError("Expression should not be an empty string.") |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 860 | exp_code = ''' |
Azim Khan | b1c2d0f | 2017-07-07 17:14:02 +0100 | [diff] [blame] | 861 | case {exp_id}: |
| 862 | {{ |
| 863 | *out_value = {expression}; |
| 864 | }} |
| 865 | break;'''.format(exp_id=exp_id, expression=exp) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 866 | return exp_code |
| 867 | |
| 868 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 869 | def write_dependencies(out_data_f, test_dependencies, unique_dependencies): |
Azim Khan | 5e2ac1f | 2017-07-03 13:58:20 +0100 | [diff] [blame] | 870 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 871 | Write dependencies to intermediate test data file, replacing |
| 872 | the string form with identifiers. Also, generates dependency |
| 873 | check code. |
Mohammad Azim Khan | b73159d | 2018-06-13 16:31:26 +0100 | [diff] [blame] | 874 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 875 | :param out_data_f: Output intermediate data file |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 876 | :param test_dependencies: Dependencies |
| 877 | :param unique_dependencies: Mutable list to track unique dependencies |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 878 | that are global to this re-entrant function. |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 879 | :return: returns dependency check code. |
Azim Khan | 5e2ac1f | 2017-07-03 13:58:20 +0100 | [diff] [blame] | 880 | """ |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 881 | dep_check_code = '' |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 882 | if test_dependencies: |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 883 | out_data_f.write('depends_on') |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 884 | for dep in test_dependencies: |
| 885 | if dep not in unique_dependencies: |
| 886 | unique_dependencies.append(dep) |
| 887 | dep_id = unique_dependencies.index(dep) |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 888 | dep_check_code += gen_dep_check(dep_id, dep) |
| 889 | else: |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 890 | dep_id = unique_dependencies.index(dep) |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 891 | out_data_f.write(':' + str(dep_id)) |
| 892 | out_data_f.write('\n') |
| 893 | return dep_check_code |
| 894 | |
| 895 | |
Gilles Peskine | a299043 | 2022-12-04 00:28:56 +0100 | [diff] [blame] | 896 | INT_VAL_REGEX = re.compile(r'-?(\d+|0x[0-9a-f]+)$', re.I) |
| 897 | def val_is_int(val: str) -> bool: |
| 898 | """Whether val is suitable as an 'int' parameter in the .datax file.""" |
| 899 | if not INT_VAL_REGEX.match(val): |
| 900 | return False |
| 901 | # Limit the range to what is guaranteed to get through strtol() |
| 902 | return abs(int(val, 0)) <= 0x7fffffff |
| 903 | |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 904 | def write_parameters(out_data_f, test_args, func_args, unique_expressions): |
| 905 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 906 | Writes test parameters to the intermediate data file, replacing |
| 907 | the string form with identifiers. Also, generates expression |
| 908 | check code. |
Mohammad Azim Khan | b73159d | 2018-06-13 16:31:26 +0100 | [diff] [blame] | 909 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 910 | :param out_data_f: Output intermediate data file |
| 911 | :param test_args: Test parameters |
| 912 | :param func_args: Function arguments |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 913 | :param unique_expressions: Mutable list to track unique |
| 914 | expressions that are global to this re-entrant function. |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 915 | :return: Returns expression check code. |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 916 | """ |
| 917 | expression_code = '' |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 918 | for i, _ in enumerate(test_args): |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 919 | typ = func_args[i] |
| 920 | val = test_args[i] |
| 921 | |
Gilles Peskine | a299043 | 2022-12-04 00:28:56 +0100 | [diff] [blame] | 922 | # Pass small integer constants literally. This reduces the size of |
| 923 | # the C code. Register anything else as an expression. |
| 924 | if typ == 'int' and not val_is_int(val): |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 925 | typ = 'exp' |
| 926 | if val not in unique_expressions: |
| 927 | unique_expressions.append(val) |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 928 | # exp_id can be derived from len(). But for |
| 929 | # readability and consistency with case of existing |
| 930 | # let's use index(). |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 931 | exp_id = unique_expressions.index(val) |
| 932 | expression_code += gen_expression_check(exp_id, val) |
| 933 | val = exp_id |
| 934 | else: |
| 935 | val = unique_expressions.index(val) |
| 936 | out_data_f.write(':' + typ + ':' + str(val)) |
| 937 | out_data_f.write('\n') |
| 938 | return expression_code |
| 939 | |
| 940 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 941 | def gen_suite_dep_checks(suite_dependencies, dep_check_code, expression_code): |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 942 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 943 | Generates preprocessor checks for test suite dependencies. |
Mohammad Azim Khan | b73159d | 2018-06-13 16:31:26 +0100 | [diff] [blame] | 944 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 945 | :param suite_dependencies: Test suite dependencies read from the |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 946 | .function file. |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 947 | :param dep_check_code: Dependency check code |
| 948 | :param expression_code: Expression check code |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 949 | :return: Dependency and expression code guarded by test suite |
| 950 | dependencies. |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 951 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 952 | if suite_dependencies: |
| 953 | preprocessor_check = gen_dependencies_one_line(suite_dependencies) |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 954 | dep_check_code = ''' |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 955 | {preprocessor_check} |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 956 | {code} |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 957 | #endif |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 958 | '''.format(preprocessor_check=preprocessor_check, code=dep_check_code) |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 959 | expression_code = ''' |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 960 | {preprocessor_check} |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 961 | {code} |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 962 | #endif |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 963 | '''.format(preprocessor_check=preprocessor_check, code=expression_code) |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 964 | return dep_check_code, expression_code |
Azim Khan | 5e2ac1f | 2017-07-03 13:58:20 +0100 | [diff] [blame] | 965 | |
| 966 | |
Gilles Peskine | ab56a69 | 2022-12-04 17:27:25 +0100 | [diff] [blame] | 967 | def get_function_info(func_info, function_name, line_no): |
| 968 | """Look up information about a test function by name. |
| 969 | |
| 970 | Raise an informative expression if function_name is not found. |
| 971 | |
| 972 | :param func_info: dictionary mapping function names to their information. |
| 973 | :param function_name: the function name as written in the .function and |
| 974 | .data files. |
| 975 | :param line_no: line number for error messages. |
| 976 | :return Function information (id, args). |
| 977 | """ |
| 978 | test_function_name = 'test_' + function_name |
| 979 | if test_function_name not in func_info: |
| 980 | raise GeneratorInputError("%d: Function %s not found!" % |
| 981 | (line_no, test_function_name)) |
| 982 | return func_info[test_function_name] |
| 983 | |
| 984 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 985 | def gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 986 | """ |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 987 | This function reads test case name, dependencies and test vectors |
| 988 | from the .data file. This information is correlated with the test |
| 989 | functions file for generating an intermediate data file replacing |
| 990 | the strings for test function names, dependencies and integer |
| 991 | constant expressions with identifiers. Mainly for optimising |
| 992 | space for on-target execution. |
| 993 | It also generates test case dependency check code and expression |
| 994 | evaluation code. |
Mohammad Azim Khan | b73159d | 2018-06-13 16:31:26 +0100 | [diff] [blame] | 995 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 996 | :param data_f: Data file object |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 997 | :param out_data_f: Output intermediate data file |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 998 | :param func_info: Dict keyed by function and with function id |
| 999 | and arguments info |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1000 | :param suite_dependencies: Test suite dependencies |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 1001 | :return: Returns dependency and expression check code |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1002 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1003 | unique_dependencies = [] |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1004 | unique_expressions = [] |
| 1005 | dep_check_code = '' |
| 1006 | expression_code = '' |
Gilles Peskine | f122aed | 2022-12-03 22:58:52 +0100 | [diff] [blame] | 1007 | for line_no, test_name, function_name, test_dependencies, test_args in \ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1008 | parse_test_data(data_f): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1009 | out_data_f.write(test_name + '\n') |
| 1010 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1011 | # Write dependencies |
| 1012 | dep_check_code += write_dependencies(out_data_f, test_dependencies, |
| 1013 | unique_dependencies) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1014 | |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 1015 | # Write test function name |
Gilles Peskine | ab56a69 | 2022-12-04 17:27:25 +0100 | [diff] [blame] | 1016 | func_id, func_args = \ |
| 1017 | get_function_info(func_info, function_name, line_no) |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 1018 | out_data_f.write(str(func_id)) |
| 1019 | |
| 1020 | # Write parameters |
Mohammad Azim Khan | 3b06f22 | 2018-06-26 14:35:25 +0100 | [diff] [blame] | 1021 | if len(test_args) != len(func_args): |
Gilles Peskine | f122aed | 2022-12-03 22:58:52 +0100 | [diff] [blame] | 1022 | raise GeneratorInputError("%d: Invalid number of arguments in test " |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1023 | "%s. See function %s signature." % |
Gilles Peskine | f122aed | 2022-12-03 22:58:52 +0100 | [diff] [blame] | 1024 | (line_no, test_name, function_name)) |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 1025 | expression_code += write_parameters(out_data_f, test_args, func_args, |
| 1026 | unique_expressions) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1027 | |
Azim Khan | 599cd24 | 2017-07-06 17:34:27 +0100 | [diff] [blame] | 1028 | # Write a newline as test case separator |
| 1029 | out_data_f.write('\n') |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1030 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1031 | dep_check_code, expression_code = gen_suite_dep_checks( |
| 1032 | suite_dependencies, dep_check_code, expression_code) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1033 | return dep_check_code, expression_code |
| 1034 | |
| 1035 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1036 | def add_input_info(funcs_file, data_file, template_file, |
| 1037 | c_file, snippets): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1038 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1039 | Add generator input info in snippets. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1040 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 1041 | :param funcs_file: Functions file object |
| 1042 | :param data_file: Data file object |
| 1043 | :param template_file: Template file object |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 1044 | :param c_file: Output C file object |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1045 | :param snippets: Dictionary to contain code pieces to be |
| 1046 | substituted in the template. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1047 | :return: |
| 1048 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1049 | snippets['test_file'] = c_file |
| 1050 | snippets['test_main_file'] = template_file |
| 1051 | snippets['test_case_file'] = funcs_file |
| 1052 | snippets['test_case_data_file'] = data_file |
| 1053 | |
| 1054 | |
| 1055 | def read_code_from_input_files(platform_file, helpers_file, |
| 1056 | out_data_file, snippets): |
| 1057 | """ |
| 1058 | Read code from input files and create substitutions for replacement |
| 1059 | strings in the template file. |
| 1060 | |
| 1061 | :param platform_file: Platform file object |
| 1062 | :param helpers_file: Helper functions file object |
| 1063 | :param out_data_file: Output intermediate data file object |
| 1064 | :param snippets: Dictionary to contain code pieces to be |
| 1065 | substituted in the template. |
| 1066 | :return: |
| 1067 | """ |
| 1068 | # Read helpers |
| 1069 | with open(helpers_file, 'r') as help_f, open(platform_file, 'r') as \ |
| 1070 | platform_f: |
| 1071 | snippets['test_common_helper_file'] = helpers_file |
| 1072 | snippets['test_common_helpers'] = help_f.read() |
| 1073 | snippets['test_platform_file'] = platform_file |
| 1074 | snippets['platform_code'] = platform_f.read().replace( |
| 1075 | 'DATA_FILE', out_data_file.replace('\\', '\\\\')) # escape '\' |
| 1076 | |
| 1077 | |
| 1078 | def write_test_source_file(template_file, c_file, snippets): |
| 1079 | """ |
| 1080 | Write output source file with generated source code. |
| 1081 | |
| 1082 | :param template_file: Template file name |
| 1083 | :param c_file: Output source file |
| 1084 | :param snippets: Generated and code snippets |
| 1085 | :return: |
| 1086 | """ |
David Horstmann | 14bae83 | 2022-11-03 17:49:29 +0000 | [diff] [blame] | 1087 | |
| 1088 | # Create a placeholder pattern with the correct named capture groups |
| 1089 | # to override the default provided with Template. |
| 1090 | # Match nothing (no way of escaping placeholders). |
| 1091 | escaped = "(?P<escaped>(?!))" |
| 1092 | # Match the "__MBEDTLS_TEST_TEMPLATE__PLACEHOLDER_NAME" pattern. |
| 1093 | named = "__MBEDTLS_TEST_TEMPLATE__(?P<named>[A-Z][_A-Z0-9]*)" |
| 1094 | # Match nothing (no braced placeholder syntax). |
| 1095 | braced = "(?P<braced>(?!))" |
| 1096 | # If not already matched, a "__MBEDTLS_TEST_TEMPLATE__" prefix is invalid. |
| 1097 | invalid = "(?P<invalid>__MBEDTLS_TEST_TEMPLATE__)" |
David Horstmann | 8eff06f | 2022-11-09 17:27:33 +0000 | [diff] [blame] | 1098 | placeholder_pattern = re.compile("|".join([escaped, named, braced, invalid])) |
David Horstmann | 14bae83 | 2022-11-03 17:49:29 +0000 | [diff] [blame] | 1099 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1100 | with open(template_file, 'r') as template_f, open(c_file, 'w') as c_f: |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 1101 | for line_no, line in enumerate(template_f.readlines(), 1): |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1102 | # Update line number. +1 as #line directive sets next line number |
| 1103 | snippets['line_no'] = line_no + 1 |
David Horstmann | 14bae83 | 2022-11-03 17:49:29 +0000 | [diff] [blame] | 1104 | template = string.Template(line) |
| 1105 | template.pattern = placeholder_pattern |
| 1106 | snippets = {k.upper():v for (k, v) in snippets.items()} |
| 1107 | code = template.substitute(**snippets) |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1108 | c_f.write(code) |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1109 | |
| 1110 | |
| 1111 | def parse_function_file(funcs_file, snippets): |
| 1112 | """ |
| 1113 | Parse function file and generate function dispatch code. |
| 1114 | |
| 1115 | :param funcs_file: Functions file name |
| 1116 | :param snippets: Dictionary to contain code pieces to be |
| 1117 | substituted in the template. |
| 1118 | :return: |
| 1119 | """ |
| 1120 | with FileWrapper(funcs_file) as funcs_f: |
| 1121 | suite_dependencies, dispatch_code, func_code, func_info = \ |
| 1122 | parse_functions(funcs_f) |
| 1123 | snippets['functions_code'] = func_code |
| 1124 | snippets['dispatch_code'] = dispatch_code |
| 1125 | return suite_dependencies, func_info |
| 1126 | |
| 1127 | |
| 1128 | def generate_intermediate_data_file(data_file, out_data_file, |
| 1129 | suite_dependencies, func_info, snippets): |
| 1130 | """ |
| 1131 | Generates intermediate data file from input data file and |
| 1132 | information read from functions file. |
| 1133 | |
| 1134 | :param data_file: Data file name |
| 1135 | :param out_data_file: Output/Intermediate data file |
| 1136 | :param suite_dependencies: List of suite dependencies. |
| 1137 | :param func_info: Function info parsed from functions file. |
| 1138 | :param snippets: Dictionary to contain code pieces to be |
| 1139 | substituted in the template. |
| 1140 | :return: |
| 1141 | """ |
| 1142 | with FileWrapper(data_file) as data_f, \ |
| 1143 | open(out_data_file, 'w') as out_data_f: |
| 1144 | dep_check_code, expression_code = gen_from_test_data( |
| 1145 | data_f, out_data_f, func_info, suite_dependencies) |
| 1146 | snippets['dep_check_code'] = dep_check_code |
| 1147 | snippets['expression_code'] = expression_code |
| 1148 | |
| 1149 | |
| 1150 | def generate_code(**input_info): |
| 1151 | """ |
| 1152 | Generates C source code from test suite file, data file, common |
| 1153 | helpers file and platform file. |
| 1154 | |
| 1155 | input_info expands to following parameters: |
| 1156 | funcs_file: Functions file object |
| 1157 | data_file: Data file object |
| 1158 | template_file: Template file object |
| 1159 | platform_file: Platform file object |
| 1160 | helpers_file: Helper functions file object |
| 1161 | suites_dir: Test suites dir |
| 1162 | c_file: Output C file object |
| 1163 | out_data_file: Output intermediate data file object |
| 1164 | :return: |
| 1165 | """ |
| 1166 | funcs_file = input_info['funcs_file'] |
| 1167 | data_file = input_info['data_file'] |
| 1168 | template_file = input_info['template_file'] |
| 1169 | platform_file = input_info['platform_file'] |
| 1170 | helpers_file = input_info['helpers_file'] |
| 1171 | suites_dir = input_info['suites_dir'] |
| 1172 | c_file = input_info['c_file'] |
| 1173 | out_data_file = input_info['out_data_file'] |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1174 | for name, path in [('Functions file', funcs_file), |
| 1175 | ('Data file', data_file), |
| 1176 | ('Template file', template_file), |
| 1177 | ('Platform file', platform_file), |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 1178 | ('Helpers code file', helpers_file), |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1179 | ('Suites dir', suites_dir)]: |
| 1180 | if not os.path.exists(path): |
| 1181 | raise IOError("ERROR: %s [%s] not found!" % (name, path)) |
| 1182 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1183 | snippets = {'generator_script': os.path.basename(__file__)} |
| 1184 | read_code_from_input_files(platform_file, helpers_file, |
| 1185 | out_data_file, snippets) |
| 1186 | add_input_info(funcs_file, data_file, template_file, |
| 1187 | c_file, snippets) |
| 1188 | suite_dependencies, func_info = parse_function_file(funcs_file, snippets) |
| 1189 | generate_intermediate_data_file(data_file, out_data_file, |
| 1190 | suite_dependencies, func_info, snippets) |
| 1191 | write_test_source_file(template_file, c_file, snippets) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1192 | |
| 1193 | |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 1194 | def main(): |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1195 | """ |
| 1196 | Command line parser. |
| 1197 | |
| 1198 | :return: |
| 1199 | """ |
Azim Khan | 040b6a2 | 2018-06-28 16:49:13 +0100 | [diff] [blame] | 1200 | parser = argparse.ArgumentParser( |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 1201 | description='Dynamically generate test suite code.') |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1202 | |
| 1203 | parser.add_argument("-f", "--functions-file", |
| 1204 | dest="funcs_file", |
| 1205 | help="Functions file", |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 1206 | metavar="FUNCTIONS_FILE", |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1207 | required=True) |
| 1208 | |
| 1209 | parser.add_argument("-d", "--data-file", |
| 1210 | dest="data_file", |
| 1211 | help="Data file", |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 1212 | metavar="DATA_FILE", |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1213 | required=True) |
| 1214 | |
| 1215 | parser.add_argument("-t", "--template-file", |
| 1216 | dest="template_file", |
| 1217 | help="Template file", |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 1218 | metavar="TEMPLATE_FILE", |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1219 | required=True) |
| 1220 | |
| 1221 | parser.add_argument("-s", "--suites-dir", |
| 1222 | dest="suites_dir", |
| 1223 | help="Suites dir", |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 1224 | metavar="SUITES_DIR", |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1225 | required=True) |
| 1226 | |
Azim Khan | e3b26af | 2018-06-29 02:36:57 +0100 | [diff] [blame] | 1227 | parser.add_argument("--helpers-file", |
| 1228 | dest="helpers_file", |
| 1229 | help="Helpers file", |
| 1230 | metavar="HELPERS_FILE", |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1231 | required=True) |
| 1232 | |
| 1233 | parser.add_argument("-p", "--platform-file", |
| 1234 | dest="platform_file", |
| 1235 | help="Platform code file", |
| 1236 | metavar="PLATFORM_FILE", |
| 1237 | required=True) |
| 1238 | |
| 1239 | parser.add_argument("-o", "--out-dir", |
| 1240 | dest="out_dir", |
| 1241 | help="Dir where generated code and scripts are copied", |
| 1242 | metavar="OUT_DIR", |
| 1243 | required=True) |
| 1244 | |
| 1245 | args = parser.parse_args() |
| 1246 | |
| 1247 | data_file_name = os.path.basename(args.data_file) |
| 1248 | data_name = os.path.splitext(data_file_name)[0] |
| 1249 | |
| 1250 | out_c_file = os.path.join(args.out_dir, data_name + '.c') |
Mohammad Azim Khan | 00c4b09 | 2018-06-28 13:10:19 +0100 | [diff] [blame] | 1251 | out_data_file = os.path.join(args.out_dir, data_name + '.datax') |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1252 | |
| 1253 | out_c_file_dir = os.path.dirname(out_c_file) |
| 1254 | out_data_file_dir = os.path.dirname(out_data_file) |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1255 | for directory in [out_c_file_dir, out_data_file_dir]: |
| 1256 | if not os.path.exists(directory): |
| 1257 | os.makedirs(directory) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1258 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1259 | generate_code(funcs_file=args.funcs_file, data_file=args.data_file, |
| 1260 | template_file=args.template_file, |
| 1261 | platform_file=args.platform_file, |
| 1262 | helpers_file=args.helpers_file, suites_dir=args.suites_dir, |
| 1263 | c_file=out_c_file, out_data_file=out_data_file) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 1264 | |
| 1265 | |
| 1266 | if __name__ == "__main__": |
Mohammad Azim Khan | 3b06f22 | 2018-06-26 14:35:25 +0100 | [diff] [blame] | 1267 | try: |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 1268 | main() |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame] | 1269 | except GeneratorInputError as err: |
Mohammad Azim Khan | 440d873 | 2018-07-18 12:50:49 +0100 | [diff] [blame] | 1270 | sys.exit("%s: input error: %s" % |
| 1271 | (os.path.basename(sys.argv[0]), str(err))) |