blob: 475a9c835bf65505c2eb68be24d808d3ca65140c [file] [log] [blame]
Mohammad Azim Khan95402612017-07-19 10:15:54 +01001#line 2 "suites/host_test.function"
Mohammad Azim Khanfff49042017-03-28 01:48:31 +01002
3/**
Azim Khan8d686bf2018-07-04 23:29:46 +01004 * \brief Verifies that string is in string parameter format i.e. "<str>"
Mohammad Azim Khanfff49042017-03-28 01:48:31 +01005 * It also strips enclosing '"' from the input string.
6 *
7 * \param str String parameter.
8 *
9 * \return 0 if success else 1
10 */
Gilles Peskine449bd832023-01-11 14:50:10 +010011int verify_string(char **str)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010012{
Gilles Peskine449bd832023-01-11 14:50:10 +010013 if ((*str)[0] != '"' ||
14 (*str)[strlen(*str) - 1] != '"') {
15 mbedtls_fprintf(stderr,
16 "Expected string (with \"\") for parameter and got: %s\n", *str);
17 return -1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010018 }
19
Gilles Peskine449bd832023-01-11 14:50:10 +010020 (*str)++;
21 (*str)[strlen(*str) - 1] = '\0';
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010022
Gilles Peskine449bd832023-01-11 14:50:10 +010023 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010024}
25
26/**
Azim Khan8d686bf2018-07-04 23:29:46 +010027 * \brief Verifies that string is an integer. Also gives the converted
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010028 * integer value.
29 *
30 * \param str Input string.
31 * \param value Pointer to int for output value.
32 *
33 * \return 0 if success else 1
34 */
Gilles Peskine449bd832023-01-11 14:50:10 +010035int verify_int(char *str, int32_t *value)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010036{
37 size_t i;
38 int minus = 0;
39 int digits = 1;
40 int hex = 0;
41
Gilles Peskine449bd832023-01-11 14:50:10 +010042 for (i = 0; i < strlen(str); i++) {
43 if (i == 0 && str[i] == '-') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010044 minus = 1;
45 continue;
46 }
47
Gilles Peskine449bd832023-01-11 14:50:10 +010048 if (((minus && i == 2) || (!minus && i == 1)) &&
49 str[i - 1] == '0' && (str[i] == 'x' || str[i] == 'X')) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010050 hex = 1;
51 continue;
52 }
53
Gilles Peskine449bd832023-01-11 14:50:10 +010054 if (!((str[i] >= '0' && str[i] <= '9') ||
55 (hex && ((str[i] >= 'a' && str[i] <= 'f') ||
56 (str[i] >= 'A' && str[i] <= 'F'))))) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010057 digits = 0;
58 break;
59 }
60 }
61
Gilles Peskine449bd832023-01-11 14:50:10 +010062 if (digits) {
63 if (hex) {
64 *value = strtol(str, NULL, 16);
65 } else {
66 *value = strtol(str, NULL, 10);
67 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010070 }
71
Gilles Peskine449bd832023-01-11 14:50:10 +010072 mbedtls_fprintf(stderr,
73 "Expected integer for parameter and got: %s\n", str);
74 return KEY_VALUE_MAPPING_NOT_FOUND;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010075}
76
77
78/**
79 * \brief Usage string.
80 *
81 */
82#define USAGE \
83 "Usage: %s [OPTIONS] files...\n\n" \
84 " Command line arguments:\n" \
Mohammad Azim Khand2d01122018-07-18 17:48:37 +010085 " files... One or more test data files. If no file is\n" \
86 " specified the following default test case\n" \
87 " file is used:\n" \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010088 " %s\n\n" \
89 " Options:\n" \
90 " -v | --verbose Display full information about each test\n" \
91 " -h | --help Display this information\n\n", \
92 argv[0], \
93 "TESTCASE_FILENAME"
94
95
96/**
97 * \brief Read a line from the passed file pointer.
98 *
99 * \param f FILE pointer
100 * \param buf Pointer to memory to hold read line.
101 * \param len Length of the buf.
102 *
103 * \return 0 if success else -1
104 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105int get_line(FILE *f, char *buf, size_t len)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100106{
107 char *ret;
108 int i = 0, str_len = 0, has_string = 0;
109
110 /* Read until we get a valid line */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 do {
112 ret = fgets(buf, len, f);
113 if (ret == NULL) {
114 return -1;
115 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 str_len = strlen(buf);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100118
119 /* Skip empty line and comment */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (str_len == 0 || buf[0] == '#') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100121 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100123 has_string = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 for (i = 0; i < str_len; i++) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100125 char c = buf[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if (c != ' ' && c != '\t' && c != '\n' &&
127 c != '\v' && c != '\f' && c != '\r') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100128 has_string = 1;
129 break;
130 }
131 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 } while (!has_string);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100133
134 /* Strip new line and carriage return */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 ret = buf + strlen(buf);
136 if (ret-- > buf && *ret == '\n') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100137 *ret = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 }
139 if (ret-- > buf && *ret == '\r') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100140 *ret = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100144}
145
146/**
147 * \brief Splits string delimited by ':'. Ignores '\:'.
148 *
149 * \param buf Input string
150 * \param len Input string length
151 * \param params Out params found
152 * \param params_len Out params array len
153 *
154 * \return Count of strings found.
155 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156static int parse_arguments(char *buf, size_t len, char **params,
157 size_t params_len)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100158{
159 size_t cnt = 0, i;
160 char *cur = buf;
161 char *p = buf, *q;
162
163 params[cnt++] = cur;
164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 while (*p != '\0' && p < (buf + len)) {
166 if (*p == '\\') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100167 p++;
168 p++;
169 continue;
170 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (*p == ':') {
172 if (p + 1 < buf + len) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100173 cur = p + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 TEST_HELPER_ASSERT(cnt < params_len);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100175 params[cnt++] = cur;
176 }
177 *p = '\0';
178 }
179
180 p++;
181 }
182
183 /* Replace newlines, question marks and colons in strings */
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 for (i = 0; i < cnt; i++) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100185 p = params[i];
186 q = params[i];
187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 while (*p != '\0') {
189 if (*p == '\\' && *(p + 1) == 'n') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100190 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 *(q++) = '\n';
192 } else if (*p == '\\' && *(p + 1) == ':') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100193 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 *(q++) = ':';
195 } else if (*p == '\\' && *(p + 1) == '?') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100196 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 *(q++) = '?';
198 } else {
199 *(q++) = *(p++);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100200 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100201 }
202 *q = '\0';
203 }
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return cnt;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100206}
207
208/**
209 * \brief Converts parameters into test function consumable parameters.
210 * Example: Input: {"int", "0", "char*", "Hello",
211 * "hex", "abef", "exp", "1"}
212 * Output: {
213 * 0, // Verified int
214 * "Hello", // Verified string
215 * 2, { 0xab, 0xef },// Converted len,hex pair
216 * 9600 // Evaluated expression
217 * }
218 *
219 *
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100220 * \param cnt Parameter array count.
221 * \param params Out array of found parameters.
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100222 * \param int_params_store Memory for storing processed integer parameters.
223 *
224 * \return 0 for success else 1
225 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226static int convert_params(size_t cnt, char **params, int32_t *int_params_store)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100227{
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 char **cur = params;
229 char **out = params;
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100230 int ret = DISPATCH_TEST_SUCCESS;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 while (cur < params + cnt) {
233 char *type = *cur++;
234 char *val = *cur++;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 if (strcmp(type, "char*") == 0) {
237 if (verify_string(&val) == 0) {
238 *out++ = val;
239 } else {
240 ret = (DISPATCH_INVALID_TEST_DATA);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100241 break;
242 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 } else if (strcmp(type, "int") == 0) {
244 if (verify_int(val, int_params_store) == 0) {
245 *out++ = (char *) int_params_store++;
246 } else {
247 ret = (DISPATCH_INVALID_TEST_DATA);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100248 break;
249 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 } else if (strcmp(type, "hex") == 0) {
251 if (verify_string(&val) == 0) {
Ronald Crona0c25392020-06-18 10:10:46 +0200252 size_t len;
253
254 TEST_HELPER_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 mbedtls_test_unhexify((unsigned char *) val, strlen(val),
256 val, &len) == 0);
Ronald Crona0c25392020-06-18 10:10:46 +0200257
258 *int_params_store = len;
Azim Khan184447e2017-05-31 20:29:36 +0100259 *out++ = val;
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 *out++ = (char *) (int_params_store++);
261 } else {
262 ret = (DISPATCH_INVALID_TEST_DATA);
Azim Khan184447e2017-05-31 20:29:36 +0100263 break;
264 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 } else if (strcmp(type, "exp") == 0) {
266 int exp_id = strtol(val, NULL, 10);
267 if (get_expression(exp_id, int_params_store) == 0) {
268 *out++ = (char *) int_params_store++;
269 } else {
270 ret = (DISPATCH_INVALID_TEST_DATA);
271 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100272 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 } else {
274 ret = (DISPATCH_INVALID_TEST_DATA);
275 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100276 }
277 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return ret;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100279}
280
281/**
282 * \brief Tests snprintf implementation with test input.
283 *
Azim Khan191e9042017-06-09 12:39:00 +0100284 * \note
285 * At high optimization levels (e.g. gcc -O3), this function may be
286 * inlined in run_test_snprintf. This can trigger a spurious warning about
287 * potential misuse of snprintf from gcc -Wformat-truncation (observed with
288 * gcc 7.2). This warning makes tests in run_test_snprintf redundant on gcc
289 * only. They are still valid for other compilers. Avoid this warning by
290 * forbidding inlining of this function by gcc.
291 *
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100292 * \param n Buffer test length.
293 * \param ref_buf Expected buffer.
294 * \param ref_ret Expected snprintf return value.
295 *
296 * \return 0 for success else 1
297 */
Azim Khan191e9042017-06-09 12:39:00 +0100298#if defined(__GNUC__)
299__attribute__((__noinline__))
300#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100301static int test_snprintf(size_t n, const char *ref_buf, int ref_ret)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100302{
303 int ret;
304 char buf[10] = "xxxxxxxxx";
305 const char ref[10] = "xxxxxxxxx";
306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if (n >= sizeof(buf)) {
308 return -1;
309 }
310 ret = mbedtls_snprintf(buf, n, "%s", "123");
311 if (ret < 0 || (size_t) ret >= n) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100312 ret = -1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100313 }
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (strncmp(ref_buf, buf, sizeof(buf)) != 0 ||
316 ref_ret != ret ||
317 memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) {
318 return 1;
319 }
320
321 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100322}
323
324/**
325 * \brief Tests snprintf implementation.
326 *
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100327 * \return 0 for success else 1
328 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100329static int run_test_snprintf(void)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100330{
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return test_snprintf(0, "xxxxxxxxx", -1) != 0 ||
332 test_snprintf(1, "", -1) != 0 ||
333 test_snprintf(2, "1", -1) != 0 ||
334 test_snprintf(3, "12", -1) != 0 ||
335 test_snprintf(4, "123", 3) != 0 ||
336 test_snprintf(5, "123", 3) != 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100337}
338
Gilles Peskine51dcc242019-09-16 15:13:48 +0200339/** \brief Write the description of the test case to the outcome CSV file.
340 *
341 * \param outcome_file The file to write to.
342 * If this is \c NULL, this function does nothing.
343 * \param argv0 The test suite name.
344 * \param test_case The test case description.
345 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346static void write_outcome_entry(FILE *outcome_file,
347 const char *argv0,
348 const char *test_case)
Gilles Peskine51dcc242019-09-16 15:13:48 +0200349{
350 /* The non-varying fields are initialized on first use. */
351 static const char *platform = NULL;
352 static const char *configuration = NULL;
353 static const char *test_suite = NULL;
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (outcome_file == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200356 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if (platform == NULL) {
360 platform = getenv("MBEDTLS_TEST_PLATFORM");
361 if (platform == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200362 platform = "unknown";
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200364 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (configuration == NULL) {
366 configuration = getenv("MBEDTLS_TEST_CONFIGURATION");
367 if (configuration == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200368 configuration = "unknown";
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200370 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (test_suite == NULL) {
372 test_suite = strrchr(argv0, '/');
373 if (test_suite != NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200374 test_suite += 1; // skip the '/'
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 } else {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200376 test_suite = argv0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200378 }
379
380 /* Write the beginning of the outcome line.
381 * Ignore errors: writing the outcome file is on a best-effort basis. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 mbedtls_fprintf(outcome_file, "%s;%s;%s;%s;",
383 platform, configuration, test_suite, test_case);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200384}
385
386/** \brief Write the result of the test case to the outcome CSV file.
387 *
388 * \param outcome_file The file to write to.
389 * If this is \c NULL, this function does nothing.
Ronald Cron67a8a372020-04-01 16:04:41 +0200390 * \param unmet_dep_count The number of unmet dependencies.
391 * \param unmet_dependencies The array of unmet dependencies.
392 * \param missing_unmet_dependencies Non-zero if there was a problem tracking
393 * all unmet dependencies, 0 otherwise.
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100394 * \param ret The test dispatch status (DISPATCH_xxx).
395 * \param info A pointer to the test info structure.
Gilles Peskine51dcc242019-09-16 15:13:48 +0200396 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100397static void write_outcome_result(FILE *outcome_file,
398 size_t unmet_dep_count,
399 int unmet_dependencies[],
400 int missing_unmet_dependencies,
401 int ret,
402 const mbedtls_test_info_t *info)
Gilles Peskine51dcc242019-09-16 15:13:48 +0200403{
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (outcome_file == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200405 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200407
408 /* Write the end of the outcome line.
409 * Ignore errors: writing the outcome file is on a best-effort basis. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 switch (ret) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200411 case DISPATCH_TEST_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (unmet_dep_count > 0) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200413 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 mbedtls_fprintf(outcome_file, "SKIP");
415 for (i = 0; i < unmet_dep_count; i++) {
416 mbedtls_fprintf(outcome_file, "%c%d",
417 i == 0 ? ';' : ':',
418 unmet_dependencies[i]);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200419 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 if (missing_unmet_dependencies) {
421 mbedtls_fprintf(outcome_file, ":...");
422 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200423 break;
424 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 switch (info->result) {
Chris Jonese60e2ae2021-01-20 17:51:47 +0000426 case MBEDTLS_TEST_RESULT_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 mbedtls_fprintf(outcome_file, "PASS;");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200428 break;
Chris Jonese60e2ae2021-01-20 17:51:47 +0000429 case MBEDTLS_TEST_RESULT_SKIPPED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 mbedtls_fprintf(outcome_file, "SKIP;Runtime skip");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200431 break;
432 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 mbedtls_fprintf(outcome_file, "FAIL;%s:%d:%s",
434 info->filename, info->line_no,
435 info->test);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200436 break;
437 }
438 break;
439 case DISPATCH_TEST_FN_NOT_FOUND:
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 mbedtls_fprintf(outcome_file, "FAIL;Test function not found");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200441 break;
442 case DISPATCH_INVALID_TEST_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 mbedtls_fprintf(outcome_file, "FAIL;Invalid test data");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200444 break;
445 case DISPATCH_UNSUPPORTED_SUITE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 mbedtls_fprintf(outcome_file, "SKIP;Unsupported suite");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200447 break;
448 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 mbedtls_fprintf(outcome_file, "FAIL;Unknown cause");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200450 break;
451 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 mbedtls_fprintf(outcome_file, "\n");
453 fflush(outcome_file);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200454}
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100455
456/**
457 * \brief Desktop implementation of execute_tests().
458 * Parses command line and executes tests from
459 * supplied or default data file.
460 *
461 * \param argc Command line argument count.
462 * \param argv Argument array.
463 *
464 * \return Program exit status.
465 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100466int execute_tests(int argc, const char **argv)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100467{
468 /* Local Configurations and options */
469 const char *default_filename = "DATA_FILE";
470 const char *test_filename = NULL;
471 const char **test_files = NULL;
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200472 size_t testfile_count = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100473 int option_verbose = 0;
k-stachowiak03954f22019-09-16 10:23:10 +0200474 size_t function_id = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100475
476 /* Other Local variables */
477 int arg_index = 1;
478 const char *next_arg;
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200479 size_t testfile_index, i, cnt;
480 int ret;
481 unsigned total_errors = 0, total_tests = 0, total_skipped = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100482 FILE *file;
483 char buf[5000];
484 char *params[50];
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800485 /* Store for processed integer params. */
Gilles Peskinea7a43062021-05-18 16:39:33 +0200486 int32_t int_params[50];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100487 void *pointer;
488#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
489 int stdout_fd = -1;
490#endif /* __unix__ || __APPLE__ __MACH__ */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 const char *outcome_file_name = getenv("MBEDTLS_TEST_OUTCOME_FILE");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200492 FILE *outcome_file = NULL;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100493
494#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
495 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
496 unsigned char alloc_buf[1000000];
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100498#endif
499
Gilles Peskine1061ec62021-01-29 21:17:11 +0100500#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 mbedtls_test_mutex_usage_init();
Gilles Peskine1061ec62021-01-29 21:17:11 +0100502#endif
503
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100504 /*
505 * The C standard doesn't guarantee that all-bits-0 is the representation
506 * of a NULL pointer. We do however use that in our code for initializing
507 * structures, which should work on every modern platform. Let's be sure.
508 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 memset(&pointer, 0, sizeof(void *));
510 if (pointer != NULL) {
511 mbedtls_fprintf(stderr, "all-bits-zero is not a NULL pointer\n");
512 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100513 }
514
515 /*
516 * Make sure we have a snprintf that correctly zero-terminates
517 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 if (run_test_snprintf() != 0) {
519 mbedtls_fprintf(stderr, "the snprintf implementation is broken\n");
520 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100521 }
522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (outcome_file_name != NULL && *outcome_file_name != '\0') {
524 outcome_file = fopen(outcome_file_name, "a");
525 if (outcome_file == NULL) {
526 mbedtls_fprintf(stderr, "Unable to open outcome file. Continuing anyway.\n");
Gilles Peskine9c673232020-01-21 18:03:56 +0100527 }
528 }
529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 while (arg_index < argc) {
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100531 next_arg = argv[arg_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (strcmp(next_arg, "--verbose") == 0 ||
534 strcmp(next_arg, "-v") == 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100535 option_verbose = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 } else if (strcmp(next_arg, "--help") == 0 ||
537 strcmp(next_arg, "-h") == 0) {
538 mbedtls_fprintf(stdout, USAGE);
539 mbedtls_exit(EXIT_SUCCESS);
540 } else {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100541 /* Not an option, therefore treat all further arguments as the file
542 * list.
543 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 test_files = &argv[arg_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100545 testfile_count = argc - arg_index;
Tuvshinzaya Erdenekhuu8988e232022-06-17 10:19:56 +0100546 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100547 }
548
549 arg_index++;
550 }
551
552 /* If no files were specified, assume a default */
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if (test_files == NULL || testfile_count == 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100554 test_files = &default_filename;
555 testfile_count = 1;
556 }
557
558 /* Initialize the struct that holds information about the last test */
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 mbedtls_test_info_reset();
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100560
561 /* Now begin to execute the tests in the testfiles */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 for (testfile_index = 0;
563 testfile_index < testfile_count;
564 testfile_index++) {
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200565 size_t unmet_dep_count = 0;
Gilles Peskine06725c92020-03-30 21:39:09 +0200566 int unmet_dependencies[20];
Ronald Cron67a8a372020-04-01 16:04:41 +0200567 int missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 test_filename = test_files[testfile_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 file = fopen(test_filename, "r");
572 if (file == NULL) {
573 mbedtls_fprintf(stderr, "Failed to open test file: %s\n",
574 test_filename);
575 if (outcome_file != NULL) {
576 fclose(outcome_file);
577 }
578 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100579 }
580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 while (!feof(file)) {
582 if (unmet_dep_count > 0) {
583 mbedtls_fprintf(stderr,
584 "FATAL: Dep count larger than zero at start of loop\n");
585 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100586 }
587 unmet_dep_count = 0;
Ronald Cron67a8a372020-04-01 16:04:41 +0200588 missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if ((ret = get_line(file, buf, sizeof(buf))) != 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100591 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 }
593 mbedtls_fprintf(stdout, "%s%.66s",
594 mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED ?
595 "\n" : "", buf);
596 mbedtls_fprintf(stdout, " ");
597 for (i = strlen(buf) + 1; i < 67; i++) {
598 mbedtls_fprintf(stdout, ".");
599 }
600 mbedtls_fprintf(stdout, " ");
601 fflush(stdout);
602 write_outcome_entry(outcome_file, argv[0], buf);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100603
604 total_tests++;
605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if ((ret = get_line(file, buf, sizeof(buf))) != 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100607 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 }
609 cnt = parse_arguments(buf, strlen(buf), params,
610 sizeof(params) / sizeof(params[0]));
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (strcmp(params[0], "depends_on") == 0) {
613 for (i = 1; i < cnt; i++) {
614 int dep_id = strtol(params[i], NULL, 10);
615 if (dep_check(dep_id) != DEPENDENCY_SUPPORTED) {
616 if (unmet_dep_count <
617 ARRAY_LENGTH(unmet_dependencies)) {
Ronald Crone1a05a52020-04-01 15:52:06 +0200618 unmet_dependencies[unmet_dep_count] = dep_id;
619 unmet_dep_count++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 } else {
Ronald Cron67a8a372020-04-01 16:04:41 +0200621 missing_unmet_dependencies = 1;
622 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100623 }
624 }
625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if ((ret = get_line(file, buf, sizeof(buf))) != 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100627 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 }
629 cnt = parse_arguments(buf, strlen(buf), params,
630 sizeof(params) / sizeof(params[0]));
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100631 }
632
633 // If there are no unmet dependencies execute the test
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (unmet_dep_count == 0) {
635 mbedtls_test_info_reset();
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100636
637#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
638 /* Suppress all output from the library unless we're verbose
639 * mode
640 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (!option_verbose) {
642 stdout_fd = redirect_output(stdout, "/dev/null");
643 if (stdout_fd == -1) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100644 /* Redirection has failed with no stdout so exit */
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 exit(1);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100646 }
647 }
648#endif /* __unix__ || __APPLE__ __MACH__ */
649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 function_id = strtoul(params[0], NULL, 10);
651 if ((ret = check_test(function_id)) == DISPATCH_TEST_SUCCESS) {
652 ret = convert_params(cnt - 1, params + 1, int_params);
653 if (DISPATCH_TEST_SUCCESS == ret) {
654 ret = dispatch_test(function_id, (void **) (params + 1));
Azim Khan13c6bfb2017-06-15 14:45:56 +0100655 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100656 }
657
658#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (!option_verbose && restore_output(stdout, stdout_fd)) {
660 /* Redirection has failed with no stdout so exit */
661 exit(1);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100662 }
663#endif /* __unix__ || __APPLE__ __MACH__ */
664
665 }
666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 write_outcome_result(outcome_file,
668 unmet_dep_count, unmet_dependencies,
669 missing_unmet_dependencies,
670 ret, &mbedtls_test_info);
671 if (unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100672 total_skipped++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 mbedtls_fprintf(stdout, "----");
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if (1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE) {
676 mbedtls_fprintf(stdout, "\n Test Suite not enabled");
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100677 }
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (1 == option_verbose && unmet_dep_count > 0) {
680 mbedtls_fprintf(stdout, "\n Unmet dependencies: ");
681 for (i = 0; i < unmet_dep_count; i++) {
682 mbedtls_fprintf(stdout, "%d ",
683 unmet_dependencies[i]);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100684 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (missing_unmet_dependencies) {
686 mbedtls_fprintf(stdout, "...");
687 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100688 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 mbedtls_fprintf(stdout, "\n");
690 fflush(stdout);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100691
692 unmet_dep_count = 0;
Ronald Cron67a8a372020-04-01 16:04:41 +0200693 missing_unmet_dependencies = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 } else if (ret == DISPATCH_TEST_SUCCESS) {
695 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS) {
696 mbedtls_fprintf(stdout, "PASS\n");
697 } else if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SKIPPED) {
698 mbedtls_fprintf(stdout, "----\n");
Hanno Beckere69d0152019-07-05 13:31:30 +0100699 total_skipped++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 } else {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100701 total_errors++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 mbedtls_fprintf(stdout, "FAILED\n");
703 mbedtls_fprintf(stdout, " %s\n at ",
704 mbedtls_test_info.test);
705 if (mbedtls_test_info.step != (unsigned long) (-1)) {
706 mbedtls_fprintf(stdout, "step %lu, ",
707 mbedtls_test_info.step);
Gilles Peskine56055912019-03-01 14:26:30 +0100708 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 mbedtls_fprintf(stdout, "line %d, %s",
710 mbedtls_test_info.line_no,
711 mbedtls_test_info.filename);
712 if (mbedtls_test_info.line1[0] != 0) {
713 mbedtls_fprintf(stdout, "\n %s",
714 mbedtls_test_info.line1);
715 }
716 if (mbedtls_test_info.line2[0] != 0) {
717 mbedtls_fprintf(stdout, "\n %s",
718 mbedtls_test_info.line2);
719 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100720 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 fflush(stdout);
722 } else if (ret == DISPATCH_INVALID_TEST_DATA) {
723 mbedtls_fprintf(stderr, "FAILED: FATAL PARSE ERROR\n");
724 fclose(file);
725 mbedtls_exit(2);
726 } else if (ret == DISPATCH_TEST_FN_NOT_FOUND) {
727 mbedtls_fprintf(stderr, "FAILED: FATAL TEST FUNCTION NOT FOUND\n");
728 fclose(file);
729 mbedtls_exit(2);
730 } else {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100731 total_errors++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100733 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 fclose(file);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100735 }
736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (outcome_file != NULL) {
738 fclose(outcome_file);
739 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 mbedtls_fprintf(stdout,
742 "\n----------------------------------------------------------------------------\n\n");
743 if (total_errors == 0) {
744 mbedtls_fprintf(stdout, "PASSED");
745 } else {
746 mbedtls_fprintf(stdout, "FAILED");
747 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 mbedtls_fprintf(stdout, " (%u / %u tests (%u skipped))\n",
750 total_tests - total_errors, total_tests, total_skipped);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100751
752#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
753 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
754#if defined(MBEDTLS_MEMORY_DEBUG)
755 mbedtls_memory_buffer_alloc_status();
756#endif
757 mbedtls_memory_buffer_alloc_free();
758#endif
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 return total_errors != 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100761}