Tom Rini | 0344c60 | 2024-10-08 13:56:50 -0600 | [diff] [blame^] | 1 | /* BEGIN_HEADER */ |
| 2 | #include "debug_internal.h" |
| 3 | #include "string.h" |
| 4 | #include "mbedtls/pk.h" |
| 5 | #include <test/ssl_helpers.h> |
| 6 | |
| 7 | struct buffer_data { |
| 8 | char buf[2000]; |
| 9 | char *ptr; |
| 10 | }; |
| 11 | |
| 12 | void string_debug(void *data, int level, const char *file, int line, const char *str) |
| 13 | { |
| 14 | struct buffer_data *buffer = (struct buffer_data *) data; |
| 15 | char *p = buffer->ptr; |
| 16 | ((void) level); |
| 17 | |
| 18 | memcpy(p, file, strlen(file)); |
| 19 | p += strlen(file); |
| 20 | |
| 21 | *p++ = '('; |
| 22 | *p++ = '0' + (line / 1000) % 10; |
| 23 | *p++ = '0' + (line / 100) % 10; |
| 24 | *p++ = '0' + (line / 10) % 10; |
| 25 | *p++ = '0' + (line / 1) % 10; |
| 26 | *p++ = ')'; |
| 27 | *p++ = ':'; |
| 28 | *p++ = ' '; |
| 29 | |
| 30 | #if defined(MBEDTLS_THREADING_C) |
| 31 | /* Skip "thread ID" (up to the first space) as it is not predictable */ |
| 32 | while (*str++ != ' ') { |
| 33 | ; |
| 34 | } |
| 35 | #endif |
| 36 | |
| 37 | memcpy(p, str, strlen(str)); |
| 38 | p += strlen(str); |
| 39 | |
| 40 | /* Detect if debug messages output partial lines and mark them */ |
| 41 | if (p[-1] != '\n') { |
| 42 | *p++ = '*'; |
| 43 | } |
| 44 | |
| 45 | buffer->ptr = p; |
| 46 | } |
| 47 | /* END_HEADER */ |
| 48 | |
| 49 | /* BEGIN_DEPENDENCIES |
| 50 | * depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C |
| 51 | * END_DEPENDENCIES |
| 52 | */ |
| 53 | |
| 54 | /* BEGIN_CASE */ |
| 55 | void debug_print_msg_threshold(int threshold, int level, char *file, |
| 56 | int line, char *result_str) |
| 57 | { |
| 58 | mbedtls_ssl_context ssl; |
| 59 | mbedtls_ssl_config conf; |
| 60 | struct buffer_data buffer; |
| 61 | |
| 62 | MD_PSA_INIT(); |
| 63 | |
| 64 | mbedtls_ssl_init(&ssl); |
| 65 | mbedtls_ssl_config_init(&conf); |
| 66 | memset(buffer.buf, 0, 2000); |
| 67 | buffer.ptr = buffer.buf; |
| 68 | |
| 69 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, |
| 70 | MBEDTLS_SSL_IS_CLIENT, |
| 71 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 72 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 73 | 0); |
| 74 | mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL); |
| 75 | mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer); |
| 76 | |
| 77 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
| 78 | |
| 79 | mbedtls_debug_set_threshold(threshold); |
| 80 | |
| 81 | mbedtls_debug_print_msg(&ssl, level, file, line, |
| 82 | "Text message, 2 == %d", 2); |
| 83 | |
| 84 | TEST_ASSERT(strcmp(buffer.buf, result_str) == 0); |
| 85 | |
| 86 | exit: |
| 87 | mbedtls_ssl_free(&ssl); |
| 88 | mbedtls_ssl_config_free(&conf); |
| 89 | MD_PSA_DONE(); |
| 90 | } |
| 91 | /* END_CASE */ |
| 92 | |
| 93 | /* BEGIN_CASE */ |
| 94 | void mbedtls_debug_print_ret(char *file, int line, char *text, int value, |
| 95 | char *result_str) |
| 96 | { |
| 97 | mbedtls_ssl_context ssl; |
| 98 | mbedtls_ssl_config conf; |
| 99 | struct buffer_data buffer; |
| 100 | |
| 101 | MD_PSA_INIT(); |
| 102 | |
| 103 | mbedtls_ssl_init(&ssl); |
| 104 | mbedtls_ssl_config_init(&conf); |
| 105 | memset(buffer.buf, 0, 2000); |
| 106 | buffer.ptr = buffer.buf; |
| 107 | |
| 108 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, |
| 109 | MBEDTLS_SSL_IS_CLIENT, |
| 110 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 111 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 112 | 0); |
| 113 | mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL); |
| 114 | mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer); |
| 115 | |
| 116 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
| 117 | |
| 118 | mbedtls_debug_print_ret(&ssl, 0, file, line, text, value); |
| 119 | |
| 120 | TEST_ASSERT(strcmp(buffer.buf, result_str) == 0); |
| 121 | |
| 122 | exit: |
| 123 | mbedtls_ssl_free(&ssl); |
| 124 | mbedtls_ssl_config_free(&conf); |
| 125 | MD_PSA_DONE(); |
| 126 | } |
| 127 | /* END_CASE */ |
| 128 | |
| 129 | /* BEGIN_CASE */ |
| 130 | void mbedtls_debug_print_buf(char *file, int line, char *text, |
| 131 | data_t *data, char *result_str) |
| 132 | { |
| 133 | mbedtls_ssl_context ssl; |
| 134 | mbedtls_ssl_config conf; |
| 135 | struct buffer_data buffer; |
| 136 | |
| 137 | MD_PSA_INIT(); |
| 138 | |
| 139 | mbedtls_ssl_init(&ssl); |
| 140 | mbedtls_ssl_config_init(&conf); |
| 141 | memset(buffer.buf, 0, 2000); |
| 142 | buffer.ptr = buffer.buf; |
| 143 | |
| 144 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, |
| 145 | MBEDTLS_SSL_IS_CLIENT, |
| 146 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 147 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 148 | 0); |
| 149 | mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL); |
| 150 | mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer); |
| 151 | |
| 152 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
| 153 | |
| 154 | mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len); |
| 155 | |
| 156 | TEST_ASSERT(strcmp(buffer.buf, result_str) == 0); |
| 157 | |
| 158 | exit: |
| 159 | mbedtls_ssl_free(&ssl); |
| 160 | mbedtls_ssl_config_free(&conf); |
| 161 | MD_PSA_DONE(); |
| 162 | } |
| 163 | /* END_CASE */ |
| 164 | |
| 165 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ |
| 166 | void mbedtls_debug_print_crt(char *crt_file, char *file, int line, |
| 167 | char *prefix, char *result_str) |
| 168 | { |
| 169 | mbedtls_x509_crt crt; |
| 170 | mbedtls_ssl_context ssl; |
| 171 | mbedtls_ssl_config conf; |
| 172 | struct buffer_data buffer; |
| 173 | |
| 174 | mbedtls_ssl_init(&ssl); |
| 175 | mbedtls_ssl_config_init(&conf); |
| 176 | mbedtls_x509_crt_init(&crt); |
| 177 | MD_OR_USE_PSA_INIT(); |
| 178 | |
| 179 | memset(buffer.buf, 0, 2000); |
| 180 | buffer.ptr = buffer.buf; |
| 181 | |
| 182 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, |
| 183 | MBEDTLS_SSL_IS_CLIENT, |
| 184 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 185 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 186 | 0); |
| 187 | mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL); |
| 188 | mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer); |
| 189 | |
| 190 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
| 191 | |
| 192 | TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0); |
| 193 | mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt); |
| 194 | |
| 195 | TEST_ASSERT(strcmp(buffer.buf, result_str) == 0); |
| 196 | |
| 197 | exit: |
| 198 | mbedtls_x509_crt_free(&crt); |
| 199 | mbedtls_ssl_free(&ssl); |
| 200 | mbedtls_ssl_config_free(&conf); |
| 201 | MD_OR_USE_PSA_DONE(); |
| 202 | } |
| 203 | /* END_CASE */ |
| 204 | |
| 205 | /* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */ |
| 206 | void mbedtls_debug_print_mpi(char *value, char *file, int line, |
| 207 | char *prefix, char *result_str) |
| 208 | { |
| 209 | mbedtls_ssl_context ssl; |
| 210 | mbedtls_ssl_config conf; |
| 211 | struct buffer_data buffer; |
| 212 | mbedtls_mpi val; |
| 213 | |
| 214 | MD_PSA_INIT(); |
| 215 | |
| 216 | mbedtls_ssl_init(&ssl); |
| 217 | mbedtls_ssl_config_init(&conf); |
| 218 | mbedtls_mpi_init(&val); |
| 219 | memset(buffer.buf, 0, 2000); |
| 220 | buffer.ptr = buffer.buf; |
| 221 | |
| 222 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, |
| 223 | MBEDTLS_SSL_IS_CLIENT, |
| 224 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 225 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 226 | 0); |
| 227 | mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL); |
| 228 | mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer); |
| 229 | |
| 230 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
| 231 | |
| 232 | TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0); |
| 233 | |
| 234 | mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val); |
| 235 | |
| 236 | TEST_ASSERT(strcmp(buffer.buf, result_str) == 0); |
| 237 | |
| 238 | exit: |
| 239 | mbedtls_mpi_free(&val); |
| 240 | mbedtls_ssl_free(&ssl); |
| 241 | mbedtls_ssl_config_free(&conf); |
| 242 | MD_PSA_DONE(); |
| 243 | } |
| 244 | /* END_CASE */ |