|
4 | 4 | #include "mbedtls/pk.h" |
5 | 5 | #include <test/ssl_helpers.h> |
6 | 6 |
|
| 7 | +#if defined(_WIN32) |
| 8 | +# include <stdlib.h> |
| 9 | +# include <crtdbg.h> |
| 10 | +#endif |
| 11 | + |
| 12 | +// Dummy type for builds without MBEDTLS_HAVE_TIME |
| 13 | +#if !defined(MBEDTLS_HAVE_TIME) |
| 14 | +typedef int64_t mbedtls_ms_time_t; |
| 15 | +#endif |
| 16 | + |
| 17 | +typedef enum { |
| 18 | + PRINTF_SIZET, |
| 19 | + PRINTF_LONGLONG, |
| 20 | + PRINTF_MS_TIME, |
| 21 | +} printf_format_indicator_t; |
| 22 | + |
| 23 | +const char *const printf_formats[] = { |
| 24 | + [PRINTF_SIZET] = "%" MBEDTLS_PRINTF_SIZET, |
| 25 | + [PRINTF_LONGLONG] = "%" MBEDTLS_PRINTF_LONGLONG, |
| 26 | + [PRINTF_MS_TIME] = "%" MBEDTLS_PRINTF_MS_TIME, |
| 27 | +}; |
| 28 | + |
7 | 29 | struct buffer_data { |
8 | 30 | char buf[2000]; |
9 | 31 | char *ptr; |
10 | 32 | }; |
11 | 33 |
|
| 34 | +#if defined(MBEDTLS_SSL_TLS_C) |
12 | 35 | static void string_debug(void *data, int level, const char *file, int line, const char *str) |
13 | 36 | { |
14 | 37 | struct buffer_data *buffer = (struct buffer_data *) data; |
@@ -44,14 +67,77 @@ static void string_debug(void *data, int level, const char *file, int line, cons |
44 | 67 |
|
45 | 68 | buffer->ptr = p; |
46 | 69 | } |
| 70 | +#endif /* MBEDTLS_SSL_TLS_C */ |
| 71 | + |
| 72 | +#if defined(_WIN32) |
| 73 | +static void noop_invalid_parameter_handler( |
| 74 | + const wchar_t *expression, |
| 75 | + const wchar_t *function, |
| 76 | + const wchar_t *file, |
| 77 | + unsigned int line, |
| 78 | + uintptr_t pReserved) |
| 79 | +{ |
| 80 | + (void) expression; |
| 81 | + (void) function; |
| 82 | + (void) file; |
| 83 | + (void) line; |
| 84 | + (void) pReserved; |
| 85 | +} |
| 86 | +#endif /* _WIN32 */ |
| 87 | + |
47 | 88 | /* END_HEADER */ |
48 | 89 |
|
49 | 90 | /* BEGIN_DEPENDENCIES |
50 | | - * depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C |
| 91 | + * depends_on:MBEDTLS_DEBUG_C |
51 | 92 | * END_DEPENDENCIES |
52 | 93 | */ |
53 | 94 |
|
54 | 95 | /* BEGIN_CASE */ |
| 96 | +void printf_int_expr(int format_indicator, intmax_t sizeof_x, intmax_t x, char *result) |
| 97 | +{ |
| 98 | +#if defined(_WIN32) |
| 99 | + /* Windows treats any invalid format specifiers passsed to the CRT as fatal assertion failures. |
| 100 | + Disable this behaviour temporarily, so the rest of the test cases can complete. */ |
| 101 | + _invalid_parameter_handler saved_handler = |
| 102 | + _set_invalid_parameter_handler(noop_invalid_parameter_handler); |
| 103 | + |
| 104 | + // Disable assertion pop-up window in Debug builds |
| 105 | + int saved_report_mode = _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_REPORT_MODE); |
| 106 | + _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); |
| 107 | +#endif |
| 108 | + |
| 109 | + const char *format = printf_formats[format_indicator]; |
| 110 | + char *output = NULL; |
| 111 | + const size_t n = strlen(result); |
| 112 | + |
| 113 | + /* Nominal case: buffer just large enough */ |
| 114 | + TEST_CALLOC(output, n + 1); |
| 115 | + if ((size_t) sizeof_x <= sizeof(int)) { // Any smaller integers would be promoted to an int due to calling a vararg function |
| 116 | + TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (int) x)); |
| 117 | + } else if (sizeof_x == sizeof(long)) { |
| 118 | + TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long) x)); |
| 119 | + } else if (sizeof_x == sizeof(long long)) { |
| 120 | + TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long long) x)); |
| 121 | + } else { |
| 122 | + TEST_FAIL( |
| 123 | + "sizeof_x <= sizeof(int) || sizeof_x == sizeof(long) || sizeof_x == sizeof(long long)"); |
| 124 | + } |
| 125 | + TEST_MEMORY_COMPARE(result, n + 1, output, n + 1); |
| 126 | + |
| 127 | +exit: |
| 128 | + mbedtls_free(output); |
| 129 | + output = NULL; |
| 130 | + |
| 131 | +#if defined(_WIN32) |
| 132 | + // Restore default Windows behaviour |
| 133 | + _set_invalid_parameter_handler(saved_handler); |
| 134 | + _CrtSetReportMode(_CRT_ASSERT, saved_report_mode); |
| 135 | + (void) saved_report_mode; |
| 136 | +#endif |
| 137 | +} |
| 138 | +/* END_CASE */ |
| 139 | + |
| 140 | +/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */ |
55 | 141 | void debug_print_msg_threshold(int threshold, int level, char *file, |
56 | 142 | int line, char *result_str) |
57 | 143 | { |
|
89 | 175 | } |
90 | 176 | /* END_CASE */ |
91 | 177 |
|
92 | | -/* BEGIN_CASE */ |
| 178 | +/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */ |
93 | 179 | void mbedtls_debug_print_ret(char *file, int line, char *text, int value, |
94 | 180 | char *result_str) |
95 | 181 | { |
@@ -124,7 +210,7 @@ exit: |
124 | 210 | } |
125 | 211 | /* END_CASE */ |
126 | 212 |
|
127 | | -/* BEGIN_CASE */ |
| 213 | +/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */ |
128 | 214 | void mbedtls_debug_print_buf(char *file, int line, char *text, |
129 | 215 | data_t *data, char *result_str) |
130 | 216 | { |
@@ -159,7 +245,7 @@ exit: |
159 | 245 | } |
160 | 246 | /* END_CASE */ |
161 | 247 |
|
162 | | -/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ |
| 248 | +/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ |
163 | 249 | void mbedtls_debug_print_crt(char *crt_file, char *file, int line, |
164 | 250 | char *prefix, char *result_str) |
165 | 251 | { |
@@ -199,7 +285,7 @@ exit: |
199 | 285 | } |
200 | 286 | /* END_CASE */ |
201 | 287 |
|
202 | | -/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */ |
| 288 | +/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_BIGNUM_C */ |
203 | 289 | void mbedtls_debug_print_mpi(char *value, char *file, int line, |
204 | 290 | char *prefix, char *result_str) |
205 | 291 | { |
|
0 commit comments