From 6f5082bf4db21ccdbdc41bb2bc1468e58f72a036 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Sun, 4 Dec 2022 15:57:49 +0100 Subject: [PATCH] Allow more signed integer types in test function arguments Now that the C code supports the full range of intmax_t, allow any size of signed integer type in the .data file parser. Signed-off-by: Gilles Peskine --- tests/scripts/generate_test_code.py | 21 +++++++++++++--- tests/scripts/test_generate_test_code.py | 4 +-- tests/suites/test_suite_platform_printf.data | 4 +++ .../test_suite_platform_printf.function | 25 +++++++++++++++++++ 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/tests/scripts/generate_test_code.py b/tests/scripts/generate_test_code.py index a3f6937c70..f900fd2ba6 100755 --- a/tests/scripts/generate_test_code.py +++ b/tests/scripts/generate_test_code.py @@ -171,8 +171,23 @@ import string import argparse -# Types regognized as integer arguments in test functions. -INTEGER_TYPES = frozenset(['int']) +# Types regognized as signed integer arguments in test functions. +SIGNED_INTEGER_TYPES = frozenset([ + 'char', + 'short', + 'short int', + 'int', + 'int8_t', + 'int16_t', + 'int32_t', + 'int64_t', + 'intmax_t', + 'long', + 'long int', + 'long long int', + 'mbedtls_mpi_sint', + 'psa_status_t', +]) # Types recognized as string arguments in test functions. STRING_TYPES = frozenset(['char*', 'const char*', 'char const*']) # Types recognized as hex data arguments in test functions. @@ -471,7 +486,7 @@ def parse_function_argument(arg, arg_idx, args, local_vars, args_dispatch): # E.g. "int x[42]" return None typ, _ = m.groups() - if typ in INTEGER_TYPES: + if typ in SIGNED_INTEGER_TYPES: args.append('int') args_dispatch.append('((mbedtls_test_argument_t*)params[%d])->sint' % arg_idx) return 1 diff --git a/tests/scripts/test_generate_test_code.py b/tests/scripts/test_generate_test_code.py index 7c0ac0c315..effa46b589 100755 --- a/tests/scripts/test_generate_test_code.py +++ b/tests/scripts/test_generate_test_code.py @@ -507,10 +507,10 @@ class ParseFuncSignature(TestCase): def test_unsupported_arg(self): """ - Test unsupported arguments (not among int, char * and data_t) + Test unsupported argument type :return: """ - line = 'void entropy_threshold( char * a, data_t * h, char result )' + line = 'void entropy_threshold( char * a, data_t * h, unknown_t result )' self.assertRaises(ValueError, parse_function_arguments, line) def test_empty_params(self): diff --git a/tests/suites/test_suite_platform_printf.data b/tests/suites/test_suite_platform_printf.data index 1ed68d5a2e..e6d335296b 100644 --- a/tests/suites/test_suite_platform_printf.data +++ b/tests/suites/test_suite_platform_printf.data @@ -56,6 +56,10 @@ printf_int:"%d":-2147483648:"-2147483648" printf "%d", -0x80000000 printf_int:"%d":-0x80000000:"-2147483648" +# Test that LONG_MAX is coming out untruncated through the test framework. +printf "%lx", LONG_MAX +printf_long_max:"%lx":LONG_MAX + # The next few test cases exercise how the test framework handles special # characters in strings. printf "%c", SPACE, SPACE diff --git a/tests/suites/test_suite_platform_printf.function b/tests/suites/test_suite_platform_printf.function index f050d6e47e..7c5e1e2cdc 100644 --- a/tests/suites/test_suite_platform_printf.function +++ b/tests/suites/test_suite_platform_printf.function @@ -31,6 +31,31 @@ exit: } /* END_CASE */ +/* BEGIN_CASE */ +void printf_long_max(const char *format, long value) +{ + char *expected = NULL; + char *output = NULL; + /* "0x" plus 2 hex digits per byte */ + const size_t n = sizeof(value) * 2; + + /* We assume that long has no padding bits! */ + ASSERT_ALLOC(expected, n + 1); + expected[0] = '7'; + memset(expected + 1, 'f', sizeof(value) * 2 - 1); + + ASSERT_ALLOC(output, n + 1); + TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, value)); + ASSERT_COMPARE(expected, n + 1, output, n + 1); + mbedtls_free(output); + output = NULL; + +exit: + mbedtls_free(output); + mbedtls_free(expected); +} +/* END_CASE */ + /* BEGIN_CASE */ void printf_char2(char *format, int arg1, int arg2, char *result) {