mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2026-03-29 08:31:06 +02:00
The Windows CRT treats any invalid format specifiers passed to the CRT as fatal assertion failures. Disable thie behaviour temporarily while testing if the format specifiers we use are supported. Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
292 lines
7.8 KiB
C
292 lines
7.8 KiB
C
/* BEGIN_HEADER */
|
|
#include "mbedtls/debug.h"
|
|
#include "string.h"
|
|
|
|
#if defined(_WIN32)
|
|
# include <stdlib.h>
|
|
# include <crtdbg.h>
|
|
#endif
|
|
|
|
struct buffer_data {
|
|
char buf[2000];
|
|
char *ptr;
|
|
};
|
|
|
|
#if defined(MBEDTLS_SSL_TLS_C)
|
|
static void string_debug(void *data, int level, const char *file, int line, const char *str)
|
|
{
|
|
struct buffer_data *buffer = (struct buffer_data *) data;
|
|
char *p = buffer->ptr;
|
|
((void) level);
|
|
|
|
memcpy(p, file, strlen(file));
|
|
p += strlen(file);
|
|
|
|
*p++ = '(';
|
|
*p++ = '0' + (line / 1000) % 10;
|
|
*p++ = '0' + (line / 100) % 10;
|
|
*p++ = '0' + (line / 10) % 10;
|
|
*p++ = '0' + (line / 1) % 10;
|
|
*p++ = ')';
|
|
*p++ = ':';
|
|
*p++ = ' ';
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
/* Skip "thread ID" (up to the first space) as it is not predictable */
|
|
while (*str++ != ' ') {
|
|
;
|
|
}
|
|
#endif
|
|
|
|
memcpy(p, str, strlen(str));
|
|
p += strlen(str);
|
|
|
|
/* Detect if debug messages output partial lines and mark them */
|
|
if (p[-1] != '\n') {
|
|
*p++ = '*';
|
|
}
|
|
|
|
buffer->ptr = p;
|
|
}
|
|
#endif /* MBEDTLS_SSL_TLS_C */
|
|
|
|
#if defined(_WIN32)
|
|
static void noop_invalid_parameter_handler(
|
|
const wchar_t *expression,
|
|
const wchar_t *function,
|
|
const wchar_t *file,
|
|
unsigned int line,
|
|
uintptr_t pReserved)
|
|
{
|
|
(void) expression;
|
|
(void) function;
|
|
(void) file;
|
|
(void) line;
|
|
(void) pReserved;
|
|
}
|
|
#endif /* _WIN32 */
|
|
|
|
/* END_HEADER */
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
* depends_on:MBEDTLS_DEBUG_C
|
|
* END_DEPENDENCIES
|
|
*/
|
|
|
|
/* BEGIN_CASE */
|
|
void printf_int_expr(intmax_t smuggle_format_expr, /* TODO: teach test framework about string expressions */
|
|
intmax_t sizeof_x, intmax_t x, char *result)
|
|
{
|
|
#if defined(_WIN32)
|
|
/* Windows treats any invalid format specifiers passsed to the CRT as fatal assertion failures.
|
|
Disable this behaviour temporarily, so the rest of the test cases can complete. */
|
|
_invalid_parameter_handler saved_handler =
|
|
_set_invalid_parameter_handler(noop_invalid_parameter_handler);
|
|
|
|
// Disable assertion pop-up window in Debug builds
|
|
int saved_report_mode = _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_REPORT_MODE);
|
|
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
|
|
#endif
|
|
|
|
const char *format = (char *) ((uintptr_t) smuggle_format_expr);
|
|
char *output = NULL;
|
|
const size_t n = strlen(result);
|
|
|
|
/* Nominal case: buffer just large enough */
|
|
TEST_CALLOC(output, n + 1);
|
|
if ((size_t) sizeof_x <= sizeof(int)) { // Any smaller integers would be promoted to an int due to calling a vararg function
|
|
TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (int) x));
|
|
} else if (sizeof_x == sizeof(long)) {
|
|
TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long) x));
|
|
} else if (sizeof_x == sizeof(long long)) {
|
|
TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long long) x));
|
|
} else {
|
|
TEST_FAIL(
|
|
"sizeof_x <= sizeof(int) || sizeof_x == sizeof(long) || sizeof_x == sizeof(long long)");
|
|
}
|
|
TEST_MEMORY_COMPARE(result, n + 1, output, n + 1);
|
|
|
|
exit:
|
|
mbedtls_free(output);
|
|
output = NULL;
|
|
|
|
#if defined(_WIN32)
|
|
// Restore default Windows behaviour
|
|
_set_invalid_parameter_handler(saved_handler);
|
|
_CrtSetReportMode(_CRT_ASSERT, saved_report_mode);
|
|
(void) saved_report_mode;
|
|
#endif
|
|
}
|
|
/* END_CASE */
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
|
|
void debug_print_msg_threshold(int threshold, int level, char *file,
|
|
int line, char *result_str)
|
|
{
|
|
mbedtls_ssl_context ssl;
|
|
mbedtls_ssl_config conf;
|
|
struct buffer_data buffer;
|
|
|
|
mbedtls_ssl_init(&ssl);
|
|
mbedtls_ssl_config_init(&conf);
|
|
memset(buffer.buf, 0, 2000);
|
|
buffer.ptr = buffer.buf;
|
|
USE_PSA_INIT();
|
|
|
|
TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
|
|
|
|
mbedtls_debug_set_threshold(threshold);
|
|
mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
|
|
|
|
mbedtls_debug_print_msg(&ssl, level, file, line,
|
|
"Text message, 2 == %d", 2);
|
|
|
|
TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
|
|
|
|
exit:
|
|
mbedtls_ssl_free(&ssl);
|
|
mbedtls_ssl_config_free(&conf);
|
|
USE_PSA_DONE();
|
|
}
|
|
/* END_CASE */
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
|
|
void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
|
|
char *result_str)
|
|
{
|
|
mbedtls_ssl_context ssl;
|
|
mbedtls_ssl_config conf;
|
|
struct buffer_data buffer;
|
|
|
|
mbedtls_ssl_init(&ssl);
|
|
mbedtls_ssl_config_init(&conf);
|
|
memset(buffer.buf, 0, 2000);
|
|
buffer.ptr = buffer.buf;
|
|
USE_PSA_INIT();
|
|
|
|
TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
|
|
|
|
mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
|
|
|
|
mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
|
|
|
|
TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
|
|
|
|
exit:
|
|
mbedtls_ssl_free(&ssl);
|
|
mbedtls_ssl_config_free(&conf);
|
|
USE_PSA_DONE();
|
|
}
|
|
/* END_CASE */
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
|
|
void mbedtls_debug_print_buf(char *file, int line, char *text,
|
|
data_t *data, char *result_str)
|
|
{
|
|
mbedtls_ssl_context ssl;
|
|
mbedtls_ssl_config conf;
|
|
struct buffer_data buffer;
|
|
|
|
mbedtls_ssl_init(&ssl);
|
|
mbedtls_ssl_config_init(&conf);
|
|
memset(buffer.buf, 0, 2000);
|
|
buffer.ptr = buffer.buf;
|
|
USE_PSA_INIT();
|
|
|
|
TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
|
|
|
|
mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
|
|
|
|
mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
|
|
|
|
TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
|
|
|
|
exit:
|
|
mbedtls_ssl_free(&ssl);
|
|
mbedtls_ssl_config_free(&conf);
|
|
USE_PSA_DONE();
|
|
}
|
|
/* END_CASE */
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
|
|
void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
|
|
char *prefix, char *result_str)
|
|
{
|
|
mbedtls_x509_crt crt;
|
|
mbedtls_ssl_context ssl;
|
|
mbedtls_ssl_config conf;
|
|
struct buffer_data buffer;
|
|
|
|
mbedtls_ssl_init(&ssl);
|
|
mbedtls_ssl_config_init(&conf);
|
|
mbedtls_x509_crt_init(&crt);
|
|
USE_PSA_INIT();
|
|
|
|
memset(buffer.buf, 0, 2000);
|
|
buffer.ptr = buffer.buf;
|
|
|
|
TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
|
|
|
|
mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
|
|
|
|
TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
|
|
mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
|
|
|
|
TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
|
|
|
|
exit:
|
|
mbedtls_x509_crt_free(&crt);
|
|
mbedtls_ssl_free(&ssl);
|
|
mbedtls_ssl_config_free(&conf);
|
|
USE_PSA_DONE();
|
|
}
|
|
/* END_CASE */
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_BIGNUM_C */
|
|
void mbedtls_debug_print_mpi(char *value, char *file, int line,
|
|
char *prefix, char *result_str)
|
|
{
|
|
mbedtls_ssl_context ssl;
|
|
mbedtls_ssl_config conf;
|
|
struct buffer_data buffer;
|
|
mbedtls_mpi val;
|
|
|
|
mbedtls_ssl_init(&ssl);
|
|
mbedtls_ssl_config_init(&conf);
|
|
mbedtls_mpi_init(&val);
|
|
memset(buffer.buf, 0, 2000);
|
|
buffer.ptr = buffer.buf;
|
|
USE_PSA_INIT();
|
|
|
|
TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
|
|
|
|
mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
|
|
|
|
mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
|
|
|
|
TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
|
|
|
|
exit:
|
|
mbedtls_mpi_free(&val);
|
|
mbedtls_ssl_free(&ssl);
|
|
mbedtls_ssl_config_free(&conf);
|
|
USE_PSA_DONE();
|
|
}
|
|
/* END_CASE */
|
|
|
|
/* BEGIN_CASE */
|
|
void check_mbedtls_calloc_overallocation(int num, int size)
|
|
{
|
|
unsigned char *buf;
|
|
buf = mbedtls_calloc((size_t) num * SIZE_MAX/2, (size_t) size * SIZE_MAX/2);
|
|
/* Dummy usage of the pointer to prevent optimizing it */
|
|
mbedtls_printf("calloc pointer : %p\n", buf);
|
|
TEST_ASSERT(buf == NULL);
|
|
|
|
exit:
|
|
mbedtls_free(buf);
|
|
}
|
|
/* END_CASE */
|