File: test_suite_debug.function

package info (click to toggle)
mbedtls 3.6.5-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,488 kB
  • sloc: ansic: 164,842; sh: 25,443; python: 15,512; makefile: 3,131; perl: 1,043; tcl: 4
file content (330 lines) | stat: -rw-r--r-- 9,692 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* BEGIN_HEADER */
#include "debug_internal.h"
#include "string.h"
#include "mbedtls/pk.h"
#include <test/ssl_helpers.h>

#if defined(_WIN32)
#   include <stdlib.h>
#   include <crtdbg.h>
#endif

// Dummy type for builds without MBEDTLS_HAVE_TIME
#if !defined(MBEDTLS_HAVE_TIME)
typedef int64_t mbedtls_ms_time_t;
#endif

typedef enum {
    PRINTF_SIZET,
    PRINTF_LONGLONG,
    PRINTF_MS_TIME,
} printf_format_indicator_t;

const char *const printf_formats[] = {
    [PRINTF_SIZET]    = "%" MBEDTLS_PRINTF_SIZET,
    [PRINTF_LONGLONG] = "%" MBEDTLS_PRINTF_LONGLONG,
    [PRINTF_MS_TIME]  = "%" MBEDTLS_PRINTF_MS_TIME,
};

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(int format_indicator, 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 = printf_formats[format_indicator];
    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;

    MD_PSA_INIT();

    mbedtls_ssl_init(&ssl);
    mbedtls_ssl_config_init(&conf);
    memset(buffer.buf, 0, 2000);
    buffer.ptr = buffer.buf;

    TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
                                           MBEDTLS_SSL_IS_CLIENT,
                                           MBEDTLS_SSL_TRANSPORT_STREAM,
                                           MBEDTLS_SSL_PRESET_DEFAULT),
               0);
    mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
    mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);

    TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);

    mbedtls_debug_set_threshold(threshold);

    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);
    MD_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;

    MD_PSA_INIT();

    mbedtls_ssl_init(&ssl);
    mbedtls_ssl_config_init(&conf);
    memset(buffer.buf, 0, 2000);
    buffer.ptr = buffer.buf;

    TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
                                           MBEDTLS_SSL_IS_CLIENT,
                                           MBEDTLS_SSL_TRANSPORT_STREAM,
                                           MBEDTLS_SSL_PRESET_DEFAULT),
               0);
    mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
    mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);

    TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);

    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);
    MD_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;

    MD_PSA_INIT();

    mbedtls_ssl_init(&ssl);
    mbedtls_ssl_config_init(&conf);
    memset(buffer.buf, 0, 2000);
    buffer.ptr = buffer.buf;

    TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
                                           MBEDTLS_SSL_IS_CLIENT,
                                           MBEDTLS_SSL_TRANSPORT_STREAM,
                                           MBEDTLS_SSL_PRESET_DEFAULT),
               0);
    mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
    mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);

    TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);

    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);
    MD_PSA_DONE();
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
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);
    MD_OR_USE_PSA_INIT();

    memset(buffer.buf, 0, 2000);
    buffer.ptr = buffer.buf;

    TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
                                           MBEDTLS_SSL_IS_CLIENT,
                                           MBEDTLS_SSL_TRANSPORT_STREAM,
                                           MBEDTLS_SSL_PRESET_DEFAULT),
               0);
    mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
    mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);

    TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);

    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);
    MD_OR_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;

    MD_PSA_INIT();

    mbedtls_ssl_init(&ssl);
    mbedtls_ssl_config_init(&conf);
    mbedtls_mpi_init(&val);
    memset(buffer.buf, 0, 2000);
    buffer.ptr = buffer.buf;

    TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
                                           MBEDTLS_SSL_IS_CLIENT,
                                           MBEDTLS_SSL_TRANSPORT_STREAM,
                                           MBEDTLS_SSL_PRESET_DEFAULT),
               0);
    mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
    mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);

    TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);

    TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);

    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);
    MD_PSA_DONE();
}
/* END_CASE */