From a9946952b4f442f706cb22cd9b3acedb90ef9b6b Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Sat, 3 Dec 2022 23:50:05 +0100 Subject: [PATCH] Exercise string parsing in the test framework Signed-off-by: Gilles Peskine --- tests/suites/test_suite_platform_printf.data | 43 ++++++++++++++++++- .../test_suite_platform_printf.function | 25 +++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/tests/suites/test_suite_platform_printf.data b/tests/suites/test_suite_platform_printf.data index 693f06938..1ed68d5a2 100644 --- a/tests/suites/test_suite_platform_printf.data +++ b/tests/suites/test_suite_platform_printf.data @@ -1,6 +1,6 @@ # The test cases for printf and integers have two purposes: they exercise # the printf function family, and they exercise the passing of integers -# through the test framework. +# and strings through the test framework. printf "%d", 0 printf_int:"%d":0:"0" @@ -55,3 +55,44 @@ printf_int:"%d":-2147483648:"-2147483648" printf "%d", -0x80000000 printf_int:"%d":-0x80000000:"-2147483648" + +# The next few test cases exercise how the test framework handles special +# characters in strings. +printf "%c", SPACE, SPACE +printf_char2:"%c%c":SPACE_CHAR:SPACE_CHAR:" " + +printf "%c", NEWLINE, SPACE +printf_char2:"%c%c":NEWLINE_CHAR:SPACE_CHAR:"\n " + +printf "%c", DOUBLE QUOTE, SPACE +printf_char2:"%c%c":DOUBLE_QUOTE_CHAR:SPACE_CHAR:"\" " + +printf "%c", COLON, SPACE +printf_char2:"%c%c":COLON_CHAR:SPACE_CHAR:"\: " + +printf "%c", BACKSLASH, SPACE +printf_char2:"%c%c":BACKSLASH_CHAR:SPACE_CHAR:"\\ " + +printf "%c", SPACE, BACKSLASH +printf_char2:"%c%c":SPACE_CHAR:BACKSLASH_CHAR:" \\" + +printf "%c%c", COLON, COLON +printf_char2:"%c%c":COLON_CHAR:COLON_CHAR:"\:\:" + +printf "%c%c", COLON, NEWLINE +printf_char2:"%c%c":COLON_CHAR:NEWLINE_CHAR:"\:\n" + +printf "%c%c", BACKSLASH, NEWLINE +printf_char2:"%c%c":BACKSLASH_CHAR:NEWLINE_CHAR:"\\\n" + +printf "%c%c", BACKSLASH, DOUBLE QUOTE +printf_char2:"%c%c":BACKSLASH_CHAR:DOUBLE_QUOTE_CHAR:"\\\"" + +printf "%c%c", BACKSLASH, COLON +printf_char2:"%c%c":BACKSLASH_CHAR:COLON_CHAR:"\\\:" + +printf "%c%c", BACKSLASH, BACKSLASH +printf_char2:"%c%c":BACKSLASH_CHAR:BACKSLASH_CHAR:"\\\\" + +printf "%c%c", BACKSLASH, n +printf_char2:"%c%c":BACKSLASH_CHAR:LOWERCASE_N_CHAR:"\\n" diff --git a/tests/suites/test_suite_platform_printf.function b/tests/suites/test_suite_platform_printf.function index 51d3c6d06..f050d6e47 100644 --- a/tests/suites/test_suite_platform_printf.function +++ b/tests/suites/test_suite_platform_printf.function @@ -4,6 +4,13 @@ #include #include #include + +#define NEWLINE_CHAR '\n' +#define SPACE_CHAR ' ' +#define DOUBLE_QUOTE_CHAR '"' +#define COLON_CHAR ':' +#define BACKSLASH_CHAR '\\' +#define LOWERCASE_N_CHAR 'n' /* END_HEADER */ /* BEGIN_CASE */ @@ -23,3 +30,21 @@ exit: mbedtls_free(output); } /* END_CASE */ + +/* BEGIN_CASE */ +void printf_char2(char *format, int arg1, int arg2, char *result) +{ + char *output = NULL; + const size_t n = strlen(result); + + /* Nominal case: buffer just large enough */ + ASSERT_ALLOC(output, n + 1); + TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, arg1, arg2)); + ASSERT_COMPARE(result, n + 1, output, n + 1); + mbedtls_free(output); + output = NULL; + +exit: + mbedtls_free(output); +} +/* END_CASE */