Merge pull request #6535 from davidhorstmann-arm/change-test-templating-syntax

Change test templating syntax to be valid C
This commit is contained in:
Gilles Peskine 2022-11-10 12:05:55 +01:00 committed by GitHub
commit b4eb444a5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 50 deletions

View File

@ -126,33 +126,39 @@ code that is generated or read from helpers and platform files.
This script replaces following fields in the template and generates This script replaces following fields in the template and generates
the test source file: the test source file:
$test_common_helpers <-- All common code from helpers.function __MBEDTLS_TEST_TEMPLATE__TEST_COMMON_HELPERS
is substituted here. All common code from helpers.function
$functions_code <-- Test functions are substituted here is substituted here.
from the input test_suit_xyz.function __MBEDTLS_TEST_TEMPLATE__FUNCTIONS_CODE
file. C preprocessor checks are generated Test functions are substituted here
for the build dependencies specified from the input test_suit_xyz.function
in the input file. This script also file. C preprocessor checks are generated
generates wrappers for the test for the build dependencies specified
functions with code to expand the in the input file. This script also
string parameters read from the data generates wrappers for the test
file. functions with code to expand the
$expression_code <-- This script enumerates the string parameters read from the data
expressions in the .data file and file.
generates code to handle enumerated __MBEDTLS_TEST_TEMPLATE__EXPRESSION_CODE
expression Ids and return the values. This script enumerates the
$dep_check_code <-- This script enumerates all expressions in the .data file and
build dependencies and generate generates code to handle enumerated
code to handle enumerated build expression Ids and return the values.
dependency Id and return status: if __MBEDTLS_TEST_TEMPLATE__DEP_CHECK_CODE
the dependency is defined or not. This script enumerates all
$dispatch_code <-- This script enumerates the functions build dependencies and generate
specified in the input test data file code to handle enumerated build
and generates the initializer for the dependency Id and return status: if
function table in the template the dependency is defined or not.
file. __MBEDTLS_TEST_TEMPLATE__DISPATCH_CODE
$platform_code <-- Platform specific setup and test This script enumerates the functions
dispatch code. specified in the input test data file
and generates the initializer for the
function table in the template
file.
__MBEDTLS_TEST_TEMPLATE__PLATFORM_CODE
Platform specific setup and test
dispatch code.
""" """
@ -974,11 +980,27 @@ def write_test_source_file(template_file, c_file, snippets):
:param snippets: Generated and code snippets :param snippets: Generated and code snippets
:return: :return:
""" """
# Create a placeholder pattern with the correct named capture groups
# to override the default provided with Template.
# Match nothing (no way of escaping placeholders).
escaped = "(?P<escaped>(?!))"
# Match the "__MBEDTLS_TEST_TEMPLATE__PLACEHOLDER_NAME" pattern.
named = "__MBEDTLS_TEST_TEMPLATE__(?P<named>[A-Z][_A-Z0-9]*)"
# Match nothing (no braced placeholder syntax).
braced = "(?P<braced>(?!))"
# If not already matched, a "__MBEDTLS_TEST_TEMPLATE__" prefix is invalid.
invalid = "(?P<invalid>__MBEDTLS_TEST_TEMPLATE__)"
placeholder_pattern = re.compile("|".join([escaped, named, braced, invalid]))
with open(template_file, 'r') as template_f, open(c_file, 'w') as c_f: with open(template_file, 'r') as template_f, open(c_file, 'w') as c_f:
for line_no, line in enumerate(template_f.readlines(), 1): for line_no, line in enumerate(template_f.readlines(), 1):
# Update line number. +1 as #line directive sets next line number # Update line number. +1 as #line directive sets next line number
snippets['line_no'] = line_no + 1 snippets['line_no'] = line_no + 1
code = string.Template(line).substitute(**snippets) template = string.Template(line)
template.pattern = placeholder_pattern
snippets = {k.upper():v for (k, v) in snippets.items()}
code = template.substitute(**snippets)
c_f.write(code) c_f.write(code)

View File

@ -3,17 +3,17 @@
* *** THIS FILE HAS BEEN MACHINE GENERATED *** * *** THIS FILE HAS BEEN MACHINE GENERATED ***
* *
* This file has been machine generated using the script: * This file has been machine generated using the script:
* $generator_script * __MBEDTLS_TEST_TEMPLATE__GENERATOR_SCRIPT
* *
* Test file : $test_file * Test file : __MBEDTLS_TEST_TEMPLATE__TEST_FILE
* *
* The following files were used to create this file. * The following files were used to create this file.
* *
* Main code file : $test_main_file * Main code file : __MBEDTLS_TEST_TEMPLATE__TEST_MAIN_FILE
* Platform code file : $test_platform_file * Platform code file : __MBEDTLS_TEST_TEMPLATE__TEST_PLATFORM_FILE
* Helper file : $test_common_helper_file * Helper file : __MBEDTLS_TEST_TEMPLATE__TEST_COMMON_HELPER_FILE
* Test suite file : $test_case_file * Test suite file : __MBEDTLS_TEST_TEMPLATE__TEST_CASE_FILE
* Test suite data : $test_case_data_file * Test suite data : __MBEDTLS_TEST_TEMPLATE__TEST_CASE_DATA_FILE
* *
*/ */
@ -37,9 +37,9 @@
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* Common helper code */ /* Common helper code */
$test_common_helpers __MBEDTLS_TEST_TEMPLATE__TEST_COMMON_HELPERS
#line $line_no "suites/main_test.function" #line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
@ -48,9 +48,9 @@ $test_common_helpers
#define TEST_SUITE_ACTIVE #define TEST_SUITE_ACTIVE
$functions_code __MBEDTLS_TEST_TEMPLATE__FUNCTIONS_CODE
#line $line_no "suites/main_test.function" #line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
@ -62,7 +62,7 @@ $functions_code
* For optimizing space for embedded targets each expression/macro * For optimizing space for embedded targets each expression/macro
* is identified by a unique identifier instead of string literals. * is identified by a unique identifier instead of string literals.
* Identifiers and evaluation code is generated by script: * Identifiers and evaluation code is generated by script:
* $generator_script * __MBEDTLS_TEST_TEMPLATE__GENERATOR_SCRIPT
* *
* \param exp_id Expression identifier. * \param exp_id Expression identifier.
* \param out_value Pointer to int to hold the integer. * \param out_value Pointer to int to hold the integer.
@ -78,8 +78,8 @@ int get_expression( int32_t exp_id, int32_t * out_value )
switch( exp_id ) switch( exp_id )
{ {
$expression_code __MBEDTLS_TEST_TEMPLATE__EXPRESSION_CODE
#line $line_no "suites/main_test.function" #line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
default: default:
{ {
ret = KEY_VALUE_MAPPING_NOT_FOUND; ret = KEY_VALUE_MAPPING_NOT_FOUND;
@ -95,7 +95,7 @@ $expression_code
* For optimizing space for embedded targets each dependency * For optimizing space for embedded targets each dependency
* is identified by a unique identifier instead of string literals. * is identified by a unique identifier instead of string literals.
* Identifiers and check code is generated by script: * Identifiers and check code is generated by script:
* $generator_script * __MBEDTLS_TEST_TEMPLATE__GENERATOR_SCRIPT
* *
* \param dep_id Dependency identifier. * \param dep_id Dependency identifier.
* *
@ -109,8 +109,8 @@ int dep_check( int dep_id )
switch( dep_id ) switch( dep_id )
{ {
$dep_check_code __MBEDTLS_TEST_TEMPLATE__DEP_CHECK_CODE
#line $line_no "suites/main_test.function" #line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
default: default:
break; break;
} }
@ -137,13 +137,13 @@ typedef void (*TestWrapper_t)( void **param_array );
/** /**
* \brief Table of test function wrappers. Used by dispatch_test(). * \brief Table of test function wrappers. Used by dispatch_test().
* This table is populated by script: * This table is populated by script:
* $generator_script * __MBEDTLS_TEST_TEMPLATE__GENERATOR_SCRIPT
* *
*/ */
TestWrapper_t test_funcs[] = TestWrapper_t test_funcs[] =
{ {
$dispatch_code __MBEDTLS_TEST_TEMPLATE__DISPATCH_CODE
#line $line_no "suites/main_test.function" #line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
}; };
/** /**
@ -219,9 +219,9 @@ int check_test( size_t func_idx )
} }
$platform_code __MBEDTLS_TEST_TEMPLATE__PLATFORM_CODE
#line $line_no "suites/main_test.function" #line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* Main Test code */ /* Main Test code */