1a8e1175bSopenharmony_ci#line 2 "suites/host_test.function"
2a8e1175bSopenharmony_ci
3a8e1175bSopenharmony_ci/**
4a8e1175bSopenharmony_ci * \brief       Verifies that string is in string parameter format i.e. "<str>"
5a8e1175bSopenharmony_ci *              It also strips enclosing '"' from the input string.
6a8e1175bSopenharmony_ci *
7a8e1175bSopenharmony_ci * \param str   String parameter.
8a8e1175bSopenharmony_ci *
9a8e1175bSopenharmony_ci * \return      0 if success else 1
10a8e1175bSopenharmony_ci */
11a8e1175bSopenharmony_ciint verify_string(char **str)
12a8e1175bSopenharmony_ci{
13a8e1175bSopenharmony_ci    if ((*str)[0] != '"' ||
14a8e1175bSopenharmony_ci        (*str)[strlen(*str) - 1] != '"') {
15a8e1175bSopenharmony_ci        mbedtls_fprintf(stderr,
16a8e1175bSopenharmony_ci                        "Expected string (with \"\") for parameter and got: %s\n", *str);
17a8e1175bSopenharmony_ci        return -1;
18a8e1175bSopenharmony_ci    }
19a8e1175bSopenharmony_ci
20a8e1175bSopenharmony_ci    (*str)++;
21a8e1175bSopenharmony_ci    (*str)[strlen(*str) - 1] = '\0';
22a8e1175bSopenharmony_ci
23a8e1175bSopenharmony_ci    return 0;
24a8e1175bSopenharmony_ci}
25a8e1175bSopenharmony_ci
26a8e1175bSopenharmony_ci/**
27a8e1175bSopenharmony_ci * \brief       Verifies that string is an integer. Also gives the converted
28a8e1175bSopenharmony_ci *              integer value.
29a8e1175bSopenharmony_ci *
30a8e1175bSopenharmony_ci * \param str   Input string.
31a8e1175bSopenharmony_ci * \param p_value Pointer to output value.
32a8e1175bSopenharmony_ci *
33a8e1175bSopenharmony_ci * \return      0 if success else 1
34a8e1175bSopenharmony_ci */
35a8e1175bSopenharmony_ciint verify_int(char *str, intmax_t *p_value)
36a8e1175bSopenharmony_ci{
37a8e1175bSopenharmony_ci    char *end = NULL;
38a8e1175bSopenharmony_ci    errno = 0;
39a8e1175bSopenharmony_ci    /* Limit the range to long: for large integers, the test framework will
40a8e1175bSopenharmony_ci     * use expressions anyway. */
41a8e1175bSopenharmony_ci    long value = strtol(str, &end, 0);
42a8e1175bSopenharmony_ci    if (errno == EINVAL || *end != '\0') {
43a8e1175bSopenharmony_ci        mbedtls_fprintf(stderr,
44a8e1175bSopenharmony_ci                        "Expected integer for parameter and got: %s\n", str);
45a8e1175bSopenharmony_ci        return KEY_VALUE_MAPPING_NOT_FOUND;
46a8e1175bSopenharmony_ci    }
47a8e1175bSopenharmony_ci    if (errno == ERANGE) {
48a8e1175bSopenharmony_ci        mbedtls_fprintf(stderr, "Integer out of range: %s\n", str);
49a8e1175bSopenharmony_ci        return KEY_VALUE_MAPPING_NOT_FOUND;
50a8e1175bSopenharmony_ci    }
51a8e1175bSopenharmony_ci    *p_value = value;
52a8e1175bSopenharmony_ci    return 0;
53a8e1175bSopenharmony_ci}
54a8e1175bSopenharmony_ci
55a8e1175bSopenharmony_ci
56a8e1175bSopenharmony_ci/**
57a8e1175bSopenharmony_ci * \brief       Usage string.
58a8e1175bSopenharmony_ci *
59a8e1175bSopenharmony_ci */
60a8e1175bSopenharmony_ci#define USAGE \
61a8e1175bSopenharmony_ci    "Usage: %s [OPTIONS] files...\n\n" \
62a8e1175bSopenharmony_ci    "   Command line arguments:\n" \
63a8e1175bSopenharmony_ci    "     files...          One or more test data files. If no file is\n" \
64a8e1175bSopenharmony_ci    "                       specified the following default test case\n" \
65a8e1175bSopenharmony_ci    "                       file is used:\n" \
66a8e1175bSopenharmony_ci    "                           %s\n\n" \
67a8e1175bSopenharmony_ci    "   Options:\n" \
68a8e1175bSopenharmony_ci    "     -v | --verbose    Display full information about each test\n" \
69a8e1175bSopenharmony_ci    "     -h | --help       Display this information\n\n", \
70a8e1175bSopenharmony_ci    argv[0], \
71a8e1175bSopenharmony_ci    "TESTCASE_FILENAME"
72a8e1175bSopenharmony_ci
73a8e1175bSopenharmony_ci
74a8e1175bSopenharmony_ci/**
75a8e1175bSopenharmony_ci * \brief       Read a line from the passed file pointer.
76a8e1175bSopenharmony_ci *
77a8e1175bSopenharmony_ci * \param f     FILE pointer
78a8e1175bSopenharmony_ci * \param buf   Pointer to memory to hold read line.
79a8e1175bSopenharmony_ci * \param len   Length of the buf.
80a8e1175bSopenharmony_ci *
81a8e1175bSopenharmony_ci * \return      0 if success else -1
82a8e1175bSopenharmony_ci */
83a8e1175bSopenharmony_ciint get_line(FILE *f, char *buf, size_t len)
84a8e1175bSopenharmony_ci{
85a8e1175bSopenharmony_ci    char *ret;
86a8e1175bSopenharmony_ci    int i = 0, str_len = 0, has_string = 0;
87a8e1175bSopenharmony_ci
88a8e1175bSopenharmony_ci    /* Read until we get a valid line */
89a8e1175bSopenharmony_ci    do {
90a8e1175bSopenharmony_ci        ret = fgets(buf, len, f);
91a8e1175bSopenharmony_ci        if (ret == NULL) {
92a8e1175bSopenharmony_ci            return -1;
93a8e1175bSopenharmony_ci        }
94a8e1175bSopenharmony_ci
95a8e1175bSopenharmony_ci        str_len = strlen(buf);
96a8e1175bSopenharmony_ci
97a8e1175bSopenharmony_ci        /* Skip empty line and comment */
98a8e1175bSopenharmony_ci        if (str_len == 0 || buf[0] == '#') {
99a8e1175bSopenharmony_ci            continue;
100a8e1175bSopenharmony_ci        }
101a8e1175bSopenharmony_ci        has_string = 0;
102a8e1175bSopenharmony_ci        for (i = 0; i < str_len; i++) {
103a8e1175bSopenharmony_ci            char c = buf[i];
104a8e1175bSopenharmony_ci            if (c != ' ' && c != '\t' && c != '\n' &&
105a8e1175bSopenharmony_ci                c != '\v' && c != '\f' && c != '\r') {
106a8e1175bSopenharmony_ci                has_string = 1;
107a8e1175bSopenharmony_ci                break;
108a8e1175bSopenharmony_ci            }
109a8e1175bSopenharmony_ci        }
110a8e1175bSopenharmony_ci    } while (!has_string);
111a8e1175bSopenharmony_ci
112a8e1175bSopenharmony_ci    /* Strip new line and carriage return */
113a8e1175bSopenharmony_ci    ret = buf + strlen(buf);
114a8e1175bSopenharmony_ci    if (ret-- > buf && *ret == '\n') {
115a8e1175bSopenharmony_ci        *ret = '\0';
116a8e1175bSopenharmony_ci    }
117a8e1175bSopenharmony_ci    if (ret-- > buf && *ret == '\r') {
118a8e1175bSopenharmony_ci        *ret = '\0';
119a8e1175bSopenharmony_ci    }
120a8e1175bSopenharmony_ci
121a8e1175bSopenharmony_ci    return 0;
122a8e1175bSopenharmony_ci}
123a8e1175bSopenharmony_ci
124a8e1175bSopenharmony_ci/**
125a8e1175bSopenharmony_ci * \brief       Splits string delimited by ':'. Ignores '\:'.
126a8e1175bSopenharmony_ci *
127a8e1175bSopenharmony_ci * \param buf           Input string
128a8e1175bSopenharmony_ci * \param len           Input string length
129a8e1175bSopenharmony_ci * \param params        Out params found
130a8e1175bSopenharmony_ci * \param params_len    Out params array len
131a8e1175bSopenharmony_ci *
132a8e1175bSopenharmony_ci * \return      Count of strings found.
133a8e1175bSopenharmony_ci */
134a8e1175bSopenharmony_cistatic int parse_arguments(char *buf, size_t len, char **params,
135a8e1175bSopenharmony_ci                           size_t params_len)
136a8e1175bSopenharmony_ci{
137a8e1175bSopenharmony_ci    size_t cnt = 0, i;
138a8e1175bSopenharmony_ci    char *cur = buf;
139a8e1175bSopenharmony_ci    char *p = buf, *q;
140a8e1175bSopenharmony_ci
141a8e1175bSopenharmony_ci    params[cnt++] = cur;
142a8e1175bSopenharmony_ci
143a8e1175bSopenharmony_ci    while (*p != '\0' && p < (buf + len)) {
144a8e1175bSopenharmony_ci        if (*p == '\\') {
145a8e1175bSopenharmony_ci            p++;
146a8e1175bSopenharmony_ci            p++;
147a8e1175bSopenharmony_ci            continue;
148a8e1175bSopenharmony_ci        }
149a8e1175bSopenharmony_ci        if (*p == ':') {
150a8e1175bSopenharmony_ci            if (p + 1 < buf + len) {
151a8e1175bSopenharmony_ci                cur = p + 1;
152a8e1175bSopenharmony_ci                TEST_HELPER_ASSERT(cnt < params_len);
153a8e1175bSopenharmony_ci                params[cnt++] = cur;
154a8e1175bSopenharmony_ci            }
155a8e1175bSopenharmony_ci            *p = '\0';
156a8e1175bSopenharmony_ci        }
157a8e1175bSopenharmony_ci
158a8e1175bSopenharmony_ci        p++;
159a8e1175bSopenharmony_ci    }
160a8e1175bSopenharmony_ci
161a8e1175bSopenharmony_ci    /* Replace backslash escapes in strings */
162a8e1175bSopenharmony_ci    for (i = 0; i < cnt; i++) {
163a8e1175bSopenharmony_ci        p = params[i];
164a8e1175bSopenharmony_ci        q = params[i];
165a8e1175bSopenharmony_ci
166a8e1175bSopenharmony_ci        while (*p != '\0') {
167a8e1175bSopenharmony_ci            if (*p == '\\') {
168a8e1175bSopenharmony_ci                ++p;
169a8e1175bSopenharmony_ci                switch (*p) {
170a8e1175bSopenharmony_ci                    case 'n':
171a8e1175bSopenharmony_ci                        *p = '\n';
172a8e1175bSopenharmony_ci                        break;
173a8e1175bSopenharmony_ci                    default:
174a8e1175bSopenharmony_ci                        // Fall through to copying *p
175a8e1175bSopenharmony_ci                        break;
176a8e1175bSopenharmony_ci                }
177a8e1175bSopenharmony_ci            }
178a8e1175bSopenharmony_ci            *(q++) = *(p++);
179a8e1175bSopenharmony_ci        }
180a8e1175bSopenharmony_ci        *q = '\0';
181a8e1175bSopenharmony_ci    }
182a8e1175bSopenharmony_ci
183a8e1175bSopenharmony_ci    return cnt;
184a8e1175bSopenharmony_ci}
185a8e1175bSopenharmony_ci
186a8e1175bSopenharmony_ci/**
187a8e1175bSopenharmony_ci * \brief       Converts parameters into test function consumable parameters.
188a8e1175bSopenharmony_ci *              Example: Input:  {"int", "0", "char*", "Hello",
189a8e1175bSopenharmony_ci *                                "hex", "abef", "exp", "1"}
190a8e1175bSopenharmony_ci *                      Output:  {
191a8e1175bSopenharmony_ci *                                0,                // Verified int
192a8e1175bSopenharmony_ci *                                "Hello",          // Verified string
193a8e1175bSopenharmony_ci *                                2, { 0xab, 0xef },// Converted len,hex pair
194a8e1175bSopenharmony_ci *                                9600              // Evaluated expression
195a8e1175bSopenharmony_ci *                               }
196a8e1175bSopenharmony_ci *
197a8e1175bSopenharmony_ci *
198a8e1175bSopenharmony_ci * \param cnt               Parameter array count.
199a8e1175bSopenharmony_ci * \param params            Out array of found parameters.
200a8e1175bSopenharmony_ci * \param int_params_store  Memory for storing processed integer parameters.
201a8e1175bSopenharmony_ci *
202a8e1175bSopenharmony_ci * \return      0 for success else 1
203a8e1175bSopenharmony_ci */
204a8e1175bSopenharmony_cistatic int convert_params(size_t cnt, char **params,
205a8e1175bSopenharmony_ci                          mbedtls_test_argument_t *int_params_store)
206a8e1175bSopenharmony_ci{
207a8e1175bSopenharmony_ci    char **cur = params;
208a8e1175bSopenharmony_ci    char **out = params;
209a8e1175bSopenharmony_ci    int ret = DISPATCH_TEST_SUCCESS;
210a8e1175bSopenharmony_ci
211a8e1175bSopenharmony_ci    while (cur < params + cnt) {
212a8e1175bSopenharmony_ci        char *type = *cur++;
213a8e1175bSopenharmony_ci        char *val = *cur++;
214a8e1175bSopenharmony_ci
215a8e1175bSopenharmony_ci        if (strcmp(type, "char*") == 0) {
216a8e1175bSopenharmony_ci            if (verify_string(&val) == 0) {
217a8e1175bSopenharmony_ci                *out++ = val;
218a8e1175bSopenharmony_ci            } else {
219a8e1175bSopenharmony_ci                ret = (DISPATCH_INVALID_TEST_DATA);
220a8e1175bSopenharmony_ci                break;
221a8e1175bSopenharmony_ci            }
222a8e1175bSopenharmony_ci        } else if (strcmp(type, "int") == 0) {
223a8e1175bSopenharmony_ci            if (verify_int(val, &int_params_store->sint) == 0) {
224a8e1175bSopenharmony_ci                *out++ = (char *) int_params_store++;
225a8e1175bSopenharmony_ci            } else {
226a8e1175bSopenharmony_ci                ret = (DISPATCH_INVALID_TEST_DATA);
227a8e1175bSopenharmony_ci                break;
228a8e1175bSopenharmony_ci            }
229a8e1175bSopenharmony_ci        } else if (strcmp(type, "hex") == 0) {
230a8e1175bSopenharmony_ci            if (verify_string(&val) == 0) {
231a8e1175bSopenharmony_ci                size_t len;
232a8e1175bSopenharmony_ci
233a8e1175bSopenharmony_ci                TEST_HELPER_ASSERT(
234a8e1175bSopenharmony_ci                    mbedtls_test_unhexify((unsigned char *) val, strlen(val),
235a8e1175bSopenharmony_ci                                          val, &len) == 0);
236a8e1175bSopenharmony_ci
237a8e1175bSopenharmony_ci                int_params_store->len = len;
238a8e1175bSopenharmony_ci                *out++ = val;
239a8e1175bSopenharmony_ci                *out++ = (char *) (int_params_store++);
240a8e1175bSopenharmony_ci            } else {
241a8e1175bSopenharmony_ci                ret = (DISPATCH_INVALID_TEST_DATA);
242a8e1175bSopenharmony_ci                break;
243a8e1175bSopenharmony_ci            }
244a8e1175bSopenharmony_ci        } else if (strcmp(type, "exp") == 0) {
245a8e1175bSopenharmony_ci            int exp_id = strtol(val, NULL, 10);
246a8e1175bSopenharmony_ci            if (get_expression(exp_id, &int_params_store->sint) == 0) {
247a8e1175bSopenharmony_ci                *out++ = (char *) int_params_store++;
248a8e1175bSopenharmony_ci            } else {
249a8e1175bSopenharmony_ci                ret = (DISPATCH_INVALID_TEST_DATA);
250a8e1175bSopenharmony_ci                break;
251a8e1175bSopenharmony_ci            }
252a8e1175bSopenharmony_ci        } else {
253a8e1175bSopenharmony_ci            ret = (DISPATCH_INVALID_TEST_DATA);
254a8e1175bSopenharmony_ci            break;
255a8e1175bSopenharmony_ci        }
256a8e1175bSopenharmony_ci    }
257a8e1175bSopenharmony_ci    return ret;
258a8e1175bSopenharmony_ci}
259a8e1175bSopenharmony_ci
260a8e1175bSopenharmony_ci/**
261a8e1175bSopenharmony_ci * \brief       Tests snprintf implementation with test input.
262a8e1175bSopenharmony_ci *
263a8e1175bSopenharmony_ci * \note
264a8e1175bSopenharmony_ci * At high optimization levels (e.g. gcc -O3), this function may be
265a8e1175bSopenharmony_ci * inlined in run_test_snprintf. This can trigger a spurious warning about
266a8e1175bSopenharmony_ci * potential misuse of snprintf from gcc -Wformat-truncation (observed with
267a8e1175bSopenharmony_ci * gcc 7.2). This warning makes tests in run_test_snprintf redundant on gcc
268a8e1175bSopenharmony_ci * only. They are still valid for other compilers. Avoid this warning by
269a8e1175bSopenharmony_ci * forbidding inlining of this function by gcc.
270a8e1175bSopenharmony_ci *
271a8e1175bSopenharmony_ci * \param n         Buffer test length.
272a8e1175bSopenharmony_ci * \param ref_buf   Expected buffer.
273a8e1175bSopenharmony_ci * \param ref_ret   Expected snprintf return value.
274a8e1175bSopenharmony_ci *
275a8e1175bSopenharmony_ci * \return      0 for success else 1
276a8e1175bSopenharmony_ci */
277a8e1175bSopenharmony_ci#if defined(__GNUC__)
278a8e1175bSopenharmony_ci__attribute__((__noinline__))
279a8e1175bSopenharmony_ci#endif
280a8e1175bSopenharmony_cistatic int test_snprintf(size_t n, const char *ref_buf, int ref_ret)
281a8e1175bSopenharmony_ci{
282a8e1175bSopenharmony_ci    int ret;
283a8e1175bSopenharmony_ci    char buf[10] = "xxxxxxxxx";
284a8e1175bSopenharmony_ci    const char ref[10] = "xxxxxxxxx";
285a8e1175bSopenharmony_ci
286a8e1175bSopenharmony_ci    if (n >= sizeof(buf)) {
287a8e1175bSopenharmony_ci        return -1;
288a8e1175bSopenharmony_ci    }
289a8e1175bSopenharmony_ci    ret = mbedtls_snprintf(buf, n, "%s", "123");
290a8e1175bSopenharmony_ci    if (ret < 0 || (size_t) ret >= n) {
291a8e1175bSopenharmony_ci        ret = -1;
292a8e1175bSopenharmony_ci    }
293a8e1175bSopenharmony_ci
294a8e1175bSopenharmony_ci    if (strncmp(ref_buf, buf, sizeof(buf)) != 0 ||
295a8e1175bSopenharmony_ci        ref_ret != ret ||
296a8e1175bSopenharmony_ci        memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) {
297a8e1175bSopenharmony_ci        return 1;
298a8e1175bSopenharmony_ci    }
299a8e1175bSopenharmony_ci
300a8e1175bSopenharmony_ci    return 0;
301a8e1175bSopenharmony_ci}
302a8e1175bSopenharmony_ci
303a8e1175bSopenharmony_ci/**
304a8e1175bSopenharmony_ci * \brief       Tests snprintf implementation.
305a8e1175bSopenharmony_ci *
306a8e1175bSopenharmony_ci * \return      0 for success else 1
307a8e1175bSopenharmony_ci */
308a8e1175bSopenharmony_cistatic int run_test_snprintf(void)
309a8e1175bSopenharmony_ci{
310a8e1175bSopenharmony_ci    return test_snprintf(0, "xxxxxxxxx",  -1) != 0 ||
311a8e1175bSopenharmony_ci           test_snprintf(1, "",           -1) != 0 ||
312a8e1175bSopenharmony_ci           test_snprintf(2, "1",          -1) != 0 ||
313a8e1175bSopenharmony_ci           test_snprintf(3, "12",         -1) != 0 ||
314a8e1175bSopenharmony_ci           test_snprintf(4, "123",         3) != 0 ||
315a8e1175bSopenharmony_ci           test_snprintf(5, "123",         3) != 0;
316a8e1175bSopenharmony_ci}
317a8e1175bSopenharmony_ci
318a8e1175bSopenharmony_ci/** \brief Write the description of the test case to the outcome CSV file.
319a8e1175bSopenharmony_ci *
320a8e1175bSopenharmony_ci * \param outcome_file  The file to write to.
321a8e1175bSopenharmony_ci *                      If this is \c NULL, this function does nothing.
322a8e1175bSopenharmony_ci * \param argv0         The test suite name.
323a8e1175bSopenharmony_ci * \param test_case     The test case description.
324a8e1175bSopenharmony_ci */
325a8e1175bSopenharmony_cistatic void write_outcome_entry(FILE *outcome_file,
326a8e1175bSopenharmony_ci                                const char *argv0,
327a8e1175bSopenharmony_ci                                const char *test_case)
328a8e1175bSopenharmony_ci{
329a8e1175bSopenharmony_ci    /* The non-varying fields are initialized on first use. */
330a8e1175bSopenharmony_ci    static const char *platform = NULL;
331a8e1175bSopenharmony_ci    static const char *configuration = NULL;
332a8e1175bSopenharmony_ci    static const char *test_suite = NULL;
333a8e1175bSopenharmony_ci
334a8e1175bSopenharmony_ci    if (outcome_file == NULL) {
335a8e1175bSopenharmony_ci        return;
336a8e1175bSopenharmony_ci    }
337a8e1175bSopenharmony_ci
338a8e1175bSopenharmony_ci    if (platform == NULL) {
339a8e1175bSopenharmony_ci        platform = getenv("MBEDTLS_TEST_PLATFORM");
340a8e1175bSopenharmony_ci        if (platform == NULL) {
341a8e1175bSopenharmony_ci            platform = "unknown";
342a8e1175bSopenharmony_ci        }
343a8e1175bSopenharmony_ci    }
344a8e1175bSopenharmony_ci    if (configuration == NULL) {
345a8e1175bSopenharmony_ci        configuration = getenv("MBEDTLS_TEST_CONFIGURATION");
346a8e1175bSopenharmony_ci        if (configuration == NULL) {
347a8e1175bSopenharmony_ci            configuration = "unknown";
348a8e1175bSopenharmony_ci        }
349a8e1175bSopenharmony_ci    }
350a8e1175bSopenharmony_ci    if (test_suite == NULL) {
351a8e1175bSopenharmony_ci        test_suite = strrchr(argv0, '/');
352a8e1175bSopenharmony_ci        if (test_suite != NULL) {
353a8e1175bSopenharmony_ci            test_suite += 1; // skip the '/'
354a8e1175bSopenharmony_ci        } else {
355a8e1175bSopenharmony_ci            test_suite = argv0;
356a8e1175bSopenharmony_ci        }
357a8e1175bSopenharmony_ci    }
358a8e1175bSopenharmony_ci
359a8e1175bSopenharmony_ci    /* Write the beginning of the outcome line.
360a8e1175bSopenharmony_ci     * Ignore errors: writing the outcome file is on a best-effort basis. */
361a8e1175bSopenharmony_ci    mbedtls_fprintf(outcome_file, "%s;%s;%s;%s;",
362a8e1175bSopenharmony_ci                    platform, configuration, test_suite, test_case);
363a8e1175bSopenharmony_ci}
364a8e1175bSopenharmony_ci
365a8e1175bSopenharmony_ci/** \brief Write the result of the test case to the outcome CSV file.
366a8e1175bSopenharmony_ci *
367a8e1175bSopenharmony_ci * \param outcome_file  The file to write to.
368a8e1175bSopenharmony_ci *                      If this is \c NULL, this function does nothing.
369a8e1175bSopenharmony_ci * \param unmet_dep_count            The number of unmet dependencies.
370a8e1175bSopenharmony_ci * \param unmet_dependencies         The array of unmet dependencies.
371a8e1175bSopenharmony_ci * \param missing_unmet_dependencies Non-zero if there was a problem tracking
372a8e1175bSopenharmony_ci *                                   all unmet dependencies, 0 otherwise.
373a8e1175bSopenharmony_ci * \param ret                        The test dispatch status (DISPATCH_xxx).
374a8e1175bSopenharmony_ci */
375a8e1175bSopenharmony_cistatic void write_outcome_result(FILE *outcome_file,
376a8e1175bSopenharmony_ci                                 size_t unmet_dep_count,
377a8e1175bSopenharmony_ci                                 int unmet_dependencies[],
378a8e1175bSopenharmony_ci                                 int missing_unmet_dependencies,
379a8e1175bSopenharmony_ci                                 int ret)
380a8e1175bSopenharmony_ci{
381a8e1175bSopenharmony_ci    if (outcome_file == NULL) {
382a8e1175bSopenharmony_ci        return;
383a8e1175bSopenharmony_ci    }
384a8e1175bSopenharmony_ci
385a8e1175bSopenharmony_ci    /* Write the end of the outcome line.
386a8e1175bSopenharmony_ci     * Ignore errors: writing the outcome file is on a best-effort basis. */
387a8e1175bSopenharmony_ci    switch (ret) {
388a8e1175bSopenharmony_ci        case DISPATCH_TEST_SUCCESS:
389a8e1175bSopenharmony_ci            if (unmet_dep_count > 0) {
390a8e1175bSopenharmony_ci                size_t i;
391a8e1175bSopenharmony_ci                mbedtls_fprintf(outcome_file, "SKIP");
392a8e1175bSopenharmony_ci                for (i = 0; i < unmet_dep_count; i++) {
393a8e1175bSopenharmony_ci                    mbedtls_fprintf(outcome_file, "%c%d",
394a8e1175bSopenharmony_ci                                    i == 0 ? ';' : ':',
395a8e1175bSopenharmony_ci                                    unmet_dependencies[i]);
396a8e1175bSopenharmony_ci                }
397a8e1175bSopenharmony_ci                if (missing_unmet_dependencies) {
398a8e1175bSopenharmony_ci                    mbedtls_fprintf(outcome_file, ":...");
399a8e1175bSopenharmony_ci                }
400a8e1175bSopenharmony_ci                break;
401a8e1175bSopenharmony_ci            }
402a8e1175bSopenharmony_ci            switch (mbedtls_test_get_result()) {
403a8e1175bSopenharmony_ci                case MBEDTLS_TEST_RESULT_SUCCESS:
404a8e1175bSopenharmony_ci                    mbedtls_fprintf(outcome_file, "PASS;");
405a8e1175bSopenharmony_ci                    break;
406a8e1175bSopenharmony_ci                case MBEDTLS_TEST_RESULT_SKIPPED:
407a8e1175bSopenharmony_ci                    mbedtls_fprintf(outcome_file, "SKIP;Runtime skip");
408a8e1175bSopenharmony_ci                    break;
409a8e1175bSopenharmony_ci                default:
410a8e1175bSopenharmony_ci                    mbedtls_fprintf(outcome_file, "FAIL;%s:%d:%s",
411a8e1175bSopenharmony_ci                                    mbedtls_get_test_filename(),
412a8e1175bSopenharmony_ci                                    mbedtls_test_get_line_no(),
413a8e1175bSopenharmony_ci                                    mbedtls_test_get_test());
414a8e1175bSopenharmony_ci                    break;
415a8e1175bSopenharmony_ci            }
416a8e1175bSopenharmony_ci            break;
417a8e1175bSopenharmony_ci        case DISPATCH_TEST_FN_NOT_FOUND:
418a8e1175bSopenharmony_ci            mbedtls_fprintf(outcome_file, "FAIL;Test function not found");
419a8e1175bSopenharmony_ci            break;
420a8e1175bSopenharmony_ci        case DISPATCH_INVALID_TEST_DATA:
421a8e1175bSopenharmony_ci            mbedtls_fprintf(outcome_file, "FAIL;Invalid test data");
422a8e1175bSopenharmony_ci            break;
423a8e1175bSopenharmony_ci        case DISPATCH_UNSUPPORTED_SUITE:
424a8e1175bSopenharmony_ci            mbedtls_fprintf(outcome_file, "SKIP;Unsupported suite");
425a8e1175bSopenharmony_ci            break;
426a8e1175bSopenharmony_ci        default:
427a8e1175bSopenharmony_ci            mbedtls_fprintf(outcome_file, "FAIL;Unknown cause");
428a8e1175bSopenharmony_ci            break;
429a8e1175bSopenharmony_ci    }
430a8e1175bSopenharmony_ci    mbedtls_fprintf(outcome_file, "\n");
431a8e1175bSopenharmony_ci    fflush(outcome_file);
432a8e1175bSopenharmony_ci}
433a8e1175bSopenharmony_ci
434a8e1175bSopenharmony_ci#if defined(__unix__) ||                                \
435a8e1175bSopenharmony_ci    (defined(__APPLE__) && defined(__MACH__))
436a8e1175bSopenharmony_ci#define MBEDTLS_HAVE_CHDIR
437a8e1175bSopenharmony_ci#endif
438a8e1175bSopenharmony_ci
439a8e1175bSopenharmony_ci#if defined(MBEDTLS_HAVE_CHDIR)
440a8e1175bSopenharmony_ci/** Try chdir to the directory containing argv0.
441a8e1175bSopenharmony_ci *
442a8e1175bSopenharmony_ci * Failures are silent.
443a8e1175bSopenharmony_ci */
444a8e1175bSopenharmony_cistatic void try_chdir_if_supported(const char *argv0)
445a8e1175bSopenharmony_ci{
446a8e1175bSopenharmony_ci    /* We might want to allow backslash as well, for Windows. But then we also
447a8e1175bSopenharmony_ci     * need to consider chdir() vs _chdir(), and different conventions
448a8e1175bSopenharmony_ci     * regarding paths in argv[0] (naively enabling this code with
449a8e1175bSopenharmony_ci     * backslash support on Windows leads to chdir into the wrong directory
450a8e1175bSopenharmony_ci     * on the CI). */
451a8e1175bSopenharmony_ci    const char *slash = strrchr(argv0, '/');
452a8e1175bSopenharmony_ci    if (slash == NULL) {
453a8e1175bSopenharmony_ci        return;
454a8e1175bSopenharmony_ci    }
455a8e1175bSopenharmony_ci    size_t path_size = slash - argv0 + 1;
456a8e1175bSopenharmony_ci    char *path = mbedtls_calloc(1, path_size);
457a8e1175bSopenharmony_ci    if (path == NULL) {
458a8e1175bSopenharmony_ci        return;
459a8e1175bSopenharmony_ci    }
460a8e1175bSopenharmony_ci    memcpy(path, argv0, path_size - 1);
461a8e1175bSopenharmony_ci    path[path_size - 1] = 0;
462a8e1175bSopenharmony_ci    int ret = chdir(path);
463a8e1175bSopenharmony_ci    if (ret != 0) {
464a8e1175bSopenharmony_ci        mbedtls_fprintf(stderr, "%s: note: chdir(\"%s\") failed.\n",
465a8e1175bSopenharmony_ci                        __func__, path);
466a8e1175bSopenharmony_ci    }
467a8e1175bSopenharmony_ci    mbedtls_free(path);
468a8e1175bSopenharmony_ci}
469a8e1175bSopenharmony_ci#else /* MBEDTLS_HAVE_CHDIR */
470a8e1175bSopenharmony_ci/* No chdir() or no support for parsing argv[0] on this platform. */
471a8e1175bSopenharmony_cistatic void try_chdir_if_supported(const char *argv0)
472a8e1175bSopenharmony_ci{
473a8e1175bSopenharmony_ci    (void) argv0;
474a8e1175bSopenharmony_ci    return;
475a8e1175bSopenharmony_ci}
476a8e1175bSopenharmony_ci#endif /* MBEDTLS_HAVE_CHDIR */
477a8e1175bSopenharmony_ci
478a8e1175bSopenharmony_ci/**
479a8e1175bSopenharmony_ci * \brief       Desktop implementation of execute_tests().
480a8e1175bSopenharmony_ci *              Parses command line and executes tests from
481a8e1175bSopenharmony_ci *              supplied or default data file.
482a8e1175bSopenharmony_ci *
483a8e1175bSopenharmony_ci * \param argc  Command line argument count.
484a8e1175bSopenharmony_ci * \param argv  Argument array.
485a8e1175bSopenharmony_ci *
486a8e1175bSopenharmony_ci * \return      Program exit status.
487a8e1175bSopenharmony_ci */
488a8e1175bSopenharmony_ciint execute_tests(int argc, const char **argv)
489a8e1175bSopenharmony_ci{
490a8e1175bSopenharmony_ci    /* Local Configurations and options */
491a8e1175bSopenharmony_ci    const char *default_filename = "DATA_FILE";
492a8e1175bSopenharmony_ci    const char *test_filename = NULL;
493a8e1175bSopenharmony_ci    const char **test_files = NULL;
494a8e1175bSopenharmony_ci    size_t testfile_count = 0;
495a8e1175bSopenharmony_ci    int option_verbose = 0;
496a8e1175bSopenharmony_ci    size_t function_id = 0;
497a8e1175bSopenharmony_ci
498a8e1175bSopenharmony_ci    /* Other Local variables */
499a8e1175bSopenharmony_ci    int arg_index = 1;
500a8e1175bSopenharmony_ci    const char *next_arg;
501a8e1175bSopenharmony_ci    size_t testfile_index, i, cnt;
502a8e1175bSopenharmony_ci    int ret;
503a8e1175bSopenharmony_ci    unsigned total_errors = 0, total_tests = 0, total_skipped = 0;
504a8e1175bSopenharmony_ci    FILE *file;
505a8e1175bSopenharmony_ci    char buf[5000];
506a8e1175bSopenharmony_ci    char *params[50];
507a8e1175bSopenharmony_ci    /* Store for processed integer params. */
508a8e1175bSopenharmony_ci    mbedtls_test_argument_t int_params[50];
509a8e1175bSopenharmony_ci    void *pointer;
510a8e1175bSopenharmony_ci#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
511a8e1175bSopenharmony_ci    int stdout_fd = -1;
512a8e1175bSopenharmony_ci#endif /* __unix__ || __APPLE__ __MACH__ */
513a8e1175bSopenharmony_ci    const char *outcome_file_name = getenv("MBEDTLS_TEST_OUTCOME_FILE");
514a8e1175bSopenharmony_ci    FILE *outcome_file = NULL;
515a8e1175bSopenharmony_ci
516a8e1175bSopenharmony_ci#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
517a8e1175bSopenharmony_ci    !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
518a8e1175bSopenharmony_ci    unsigned char alloc_buf[1000000];
519a8e1175bSopenharmony_ci    mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
520a8e1175bSopenharmony_ci#endif
521a8e1175bSopenharmony_ci
522a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_MUTEX_USAGE)
523a8e1175bSopenharmony_ci    mbedtls_test_mutex_usage_init();
524a8e1175bSopenharmony_ci#endif
525a8e1175bSopenharmony_ci
526a8e1175bSopenharmony_ci    /*
527a8e1175bSopenharmony_ci     * The C standard doesn't guarantee that all-bits-0 is the representation
528a8e1175bSopenharmony_ci     * of a NULL pointer. We do however use that in our code for initializing
529a8e1175bSopenharmony_ci     * structures, which should work on every modern platform. Let's be sure.
530a8e1175bSopenharmony_ci     */
531a8e1175bSopenharmony_ci    memset(&pointer, 0, sizeof(void *));
532a8e1175bSopenharmony_ci    if (pointer != NULL) {
533a8e1175bSopenharmony_ci        mbedtls_fprintf(stderr, "all-bits-zero is not a NULL pointer\n");
534a8e1175bSopenharmony_ci        return 1;
535a8e1175bSopenharmony_ci    }
536a8e1175bSopenharmony_ci
537a8e1175bSopenharmony_ci    /*
538a8e1175bSopenharmony_ci     * Make sure we have a snprintf that correctly zero-terminates
539a8e1175bSopenharmony_ci     */
540a8e1175bSopenharmony_ci    if (run_test_snprintf() != 0) {
541a8e1175bSopenharmony_ci        mbedtls_fprintf(stderr, "the snprintf implementation is broken\n");
542a8e1175bSopenharmony_ci        return 1;
543a8e1175bSopenharmony_ci    }
544a8e1175bSopenharmony_ci
545a8e1175bSopenharmony_ci    if (outcome_file_name != NULL && *outcome_file_name != '\0') {
546a8e1175bSopenharmony_ci        outcome_file = fopen(outcome_file_name, "a");
547a8e1175bSopenharmony_ci        if (outcome_file == NULL) {
548a8e1175bSopenharmony_ci            mbedtls_fprintf(stderr, "Unable to open outcome file. Continuing anyway.\n");
549a8e1175bSopenharmony_ci        }
550a8e1175bSopenharmony_ci    }
551a8e1175bSopenharmony_ci
552a8e1175bSopenharmony_ci    while (arg_index < argc) {
553a8e1175bSopenharmony_ci        next_arg = argv[arg_index];
554a8e1175bSopenharmony_ci
555a8e1175bSopenharmony_ci        if (strcmp(next_arg, "--verbose") == 0 ||
556a8e1175bSopenharmony_ci            strcmp(next_arg, "-v") == 0) {
557a8e1175bSopenharmony_ci            option_verbose = 1;
558a8e1175bSopenharmony_ci        } else if (strcmp(next_arg, "--help") == 0 ||
559a8e1175bSopenharmony_ci                   strcmp(next_arg, "-h") == 0) {
560a8e1175bSopenharmony_ci            mbedtls_fprintf(stdout, USAGE);
561a8e1175bSopenharmony_ci            mbedtls_exit(EXIT_SUCCESS);
562a8e1175bSopenharmony_ci        } else {
563a8e1175bSopenharmony_ci            /* Not an option, therefore treat all further arguments as the file
564a8e1175bSopenharmony_ci             * list.
565a8e1175bSopenharmony_ci             */
566a8e1175bSopenharmony_ci            test_files = &argv[arg_index];
567a8e1175bSopenharmony_ci            testfile_count = argc - arg_index;
568a8e1175bSopenharmony_ci            break;
569a8e1175bSopenharmony_ci        }
570a8e1175bSopenharmony_ci
571a8e1175bSopenharmony_ci        arg_index++;
572a8e1175bSopenharmony_ci    }
573a8e1175bSopenharmony_ci
574a8e1175bSopenharmony_ci    /* If no files were specified, assume a default */
575a8e1175bSopenharmony_ci    if (test_files == NULL || testfile_count == 0) {
576a8e1175bSopenharmony_ci        test_files = &default_filename;
577a8e1175bSopenharmony_ci        testfile_count = 1;
578a8e1175bSopenharmony_ci    }
579a8e1175bSopenharmony_ci
580a8e1175bSopenharmony_ci    /* Initialize the struct that holds information about the last test */
581a8e1175bSopenharmony_ci    mbedtls_test_info_reset();
582a8e1175bSopenharmony_ci
583a8e1175bSopenharmony_ci    /* Now begin to execute the tests in the testfiles */
584a8e1175bSopenharmony_ci    for (testfile_index = 0;
585a8e1175bSopenharmony_ci         testfile_index < testfile_count;
586a8e1175bSopenharmony_ci         testfile_index++) {
587a8e1175bSopenharmony_ci        size_t unmet_dep_count = 0;
588a8e1175bSopenharmony_ci        int unmet_dependencies[20];
589a8e1175bSopenharmony_ci        int missing_unmet_dependencies = 0;
590a8e1175bSopenharmony_ci
591a8e1175bSopenharmony_ci        test_filename = test_files[testfile_index];
592a8e1175bSopenharmony_ci
593a8e1175bSopenharmony_ci        file = fopen(test_filename, "r");
594a8e1175bSopenharmony_ci        if (file == NULL) {
595a8e1175bSopenharmony_ci            mbedtls_fprintf(stderr, "Failed to open test file: %s\n",
596a8e1175bSopenharmony_ci                            test_filename);
597a8e1175bSopenharmony_ci            if (outcome_file != NULL) {
598a8e1175bSopenharmony_ci                fclose(outcome_file);
599a8e1175bSopenharmony_ci            }
600a8e1175bSopenharmony_ci            return 1;
601a8e1175bSopenharmony_ci        }
602a8e1175bSopenharmony_ci
603a8e1175bSopenharmony_ci        while (!feof(file)) {
604a8e1175bSopenharmony_ci            if (unmet_dep_count > 0) {
605a8e1175bSopenharmony_ci                mbedtls_fprintf(stderr,
606a8e1175bSopenharmony_ci                                "FATAL: Dep count larger than zero at start of loop\n");
607a8e1175bSopenharmony_ci                mbedtls_exit(MBEDTLS_EXIT_FAILURE);
608a8e1175bSopenharmony_ci            }
609a8e1175bSopenharmony_ci            unmet_dep_count = 0;
610a8e1175bSopenharmony_ci            missing_unmet_dependencies = 0;
611a8e1175bSopenharmony_ci
612a8e1175bSopenharmony_ci            if ((ret = get_line(file, buf, sizeof(buf))) != 0) {
613a8e1175bSopenharmony_ci                break;
614a8e1175bSopenharmony_ci            }
615a8e1175bSopenharmony_ci            mbedtls_fprintf(stdout, "%s%.66s",
616a8e1175bSopenharmony_ci                            mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED ?
617a8e1175bSopenharmony_ci                            "\n" : "", buf);
618a8e1175bSopenharmony_ci            mbedtls_fprintf(stdout, " ");
619a8e1175bSopenharmony_ci            for (i = strlen(buf) + 1; i < 67; i++) {
620a8e1175bSopenharmony_ci                mbedtls_fprintf(stdout, ".");
621a8e1175bSopenharmony_ci            }
622a8e1175bSopenharmony_ci            mbedtls_fprintf(stdout, " ");
623a8e1175bSopenharmony_ci            fflush(stdout);
624a8e1175bSopenharmony_ci            write_outcome_entry(outcome_file, argv[0], buf);
625a8e1175bSopenharmony_ci
626a8e1175bSopenharmony_ci            total_tests++;
627a8e1175bSopenharmony_ci
628a8e1175bSopenharmony_ci            if ((ret = get_line(file, buf, sizeof(buf))) != 0) {
629a8e1175bSopenharmony_ci                break;
630a8e1175bSopenharmony_ci            }
631a8e1175bSopenharmony_ci            cnt = parse_arguments(buf, strlen(buf), params,
632a8e1175bSopenharmony_ci                                  sizeof(params) / sizeof(params[0]));
633a8e1175bSopenharmony_ci
634a8e1175bSopenharmony_ci            if (strcmp(params[0], "depends_on") == 0) {
635a8e1175bSopenharmony_ci                for (i = 1; i < cnt; i++) {
636a8e1175bSopenharmony_ci                    int dep_id = strtol(params[i], NULL, 10);
637a8e1175bSopenharmony_ci                    if (dep_check(dep_id) != DEPENDENCY_SUPPORTED) {
638a8e1175bSopenharmony_ci                        if (unmet_dep_count <
639a8e1175bSopenharmony_ci                            ARRAY_LENGTH(unmet_dependencies)) {
640a8e1175bSopenharmony_ci                            unmet_dependencies[unmet_dep_count] = dep_id;
641a8e1175bSopenharmony_ci                            unmet_dep_count++;
642a8e1175bSopenharmony_ci                        } else {
643a8e1175bSopenharmony_ci                            missing_unmet_dependencies = 1;
644a8e1175bSopenharmony_ci                        }
645a8e1175bSopenharmony_ci                    }
646a8e1175bSopenharmony_ci                }
647a8e1175bSopenharmony_ci
648a8e1175bSopenharmony_ci                if ((ret = get_line(file, buf, sizeof(buf))) != 0) {
649a8e1175bSopenharmony_ci                    break;
650a8e1175bSopenharmony_ci                }
651a8e1175bSopenharmony_ci                cnt = parse_arguments(buf, strlen(buf), params,
652a8e1175bSopenharmony_ci                                      sizeof(params) / sizeof(params[0]));
653a8e1175bSopenharmony_ci            }
654a8e1175bSopenharmony_ci
655a8e1175bSopenharmony_ci            // If there are no unmet dependencies execute the test
656a8e1175bSopenharmony_ci            if (unmet_dep_count == 0) {
657a8e1175bSopenharmony_ci                mbedtls_test_info_reset();
658a8e1175bSopenharmony_ci
659a8e1175bSopenharmony_ci#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
660a8e1175bSopenharmony_ci                /* Suppress all output from the library unless we're verbose
661a8e1175bSopenharmony_ci                 * mode
662a8e1175bSopenharmony_ci                 */
663a8e1175bSopenharmony_ci                if (!option_verbose) {
664a8e1175bSopenharmony_ci                    stdout_fd = redirect_output(stdout, "/dev/null");
665a8e1175bSopenharmony_ci                    if (stdout_fd == -1) {
666a8e1175bSopenharmony_ci                        /* Redirection has failed with no stdout so exit */
667a8e1175bSopenharmony_ci                        exit(1);
668a8e1175bSopenharmony_ci                    }
669a8e1175bSopenharmony_ci                }
670a8e1175bSopenharmony_ci#endif /* __unix__ || __APPLE__ __MACH__ */
671a8e1175bSopenharmony_ci
672a8e1175bSopenharmony_ci                function_id = strtoul(params[0], NULL, 10);
673a8e1175bSopenharmony_ci                if ((ret = check_test(function_id)) == DISPATCH_TEST_SUCCESS) {
674a8e1175bSopenharmony_ci                    ret = convert_params(cnt - 1, params + 1, int_params);
675a8e1175bSopenharmony_ci                    if (DISPATCH_TEST_SUCCESS == ret) {
676a8e1175bSopenharmony_ci                        ret = dispatch_test(function_id, (void **) (params + 1));
677a8e1175bSopenharmony_ci                    }
678a8e1175bSopenharmony_ci                }
679a8e1175bSopenharmony_ci
680a8e1175bSopenharmony_ci#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
681a8e1175bSopenharmony_ci                if (!option_verbose && restore_output(stdout, stdout_fd)) {
682a8e1175bSopenharmony_ci                    /* Redirection has failed with no stdout so exit */
683a8e1175bSopenharmony_ci                    exit(1);
684a8e1175bSopenharmony_ci                }
685a8e1175bSopenharmony_ci#endif /* __unix__ || __APPLE__ __MACH__ */
686a8e1175bSopenharmony_ci
687a8e1175bSopenharmony_ci            }
688a8e1175bSopenharmony_ci
689a8e1175bSopenharmony_ci            write_outcome_result(outcome_file,
690a8e1175bSopenharmony_ci                                 unmet_dep_count, unmet_dependencies,
691a8e1175bSopenharmony_ci                                 missing_unmet_dependencies,
692a8e1175bSopenharmony_ci                                 ret);
693a8e1175bSopenharmony_ci            if (unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE) {
694a8e1175bSopenharmony_ci                total_skipped++;
695a8e1175bSopenharmony_ci                mbedtls_fprintf(stdout, "----");
696a8e1175bSopenharmony_ci
697a8e1175bSopenharmony_ci                if (1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE) {
698a8e1175bSopenharmony_ci                    mbedtls_fprintf(stdout, "\n   Test Suite not enabled");
699a8e1175bSopenharmony_ci                }
700a8e1175bSopenharmony_ci
701a8e1175bSopenharmony_ci                if (1 == option_verbose && unmet_dep_count > 0) {
702a8e1175bSopenharmony_ci                    mbedtls_fprintf(stdout, "\n   Unmet dependencies: ");
703a8e1175bSopenharmony_ci                    for (i = 0; i < unmet_dep_count; i++) {
704a8e1175bSopenharmony_ci                        mbedtls_fprintf(stdout, "%d ",
705a8e1175bSopenharmony_ci                                        unmet_dependencies[i]);
706a8e1175bSopenharmony_ci                    }
707a8e1175bSopenharmony_ci                    if (missing_unmet_dependencies) {
708a8e1175bSopenharmony_ci                        mbedtls_fprintf(stdout, "...");
709a8e1175bSopenharmony_ci                    }
710a8e1175bSopenharmony_ci                }
711a8e1175bSopenharmony_ci                mbedtls_fprintf(stdout, "\n");
712a8e1175bSopenharmony_ci                fflush(stdout);
713a8e1175bSopenharmony_ci
714a8e1175bSopenharmony_ci                unmet_dep_count = 0;
715a8e1175bSopenharmony_ci                missing_unmet_dependencies = 0;
716a8e1175bSopenharmony_ci            } else if (ret == DISPATCH_TEST_SUCCESS) {
717a8e1175bSopenharmony_ci                if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_SUCCESS) {
718a8e1175bSopenharmony_ci                    mbedtls_fprintf(stdout, "PASS\n");
719a8e1175bSopenharmony_ci                } else if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_SKIPPED) {
720a8e1175bSopenharmony_ci                    mbedtls_fprintf(stdout, "----\n");
721a8e1175bSopenharmony_ci                    total_skipped++;
722a8e1175bSopenharmony_ci                } else {
723a8e1175bSopenharmony_ci                    char line_buffer[MBEDTLS_TEST_LINE_LENGTH];
724a8e1175bSopenharmony_ci
725a8e1175bSopenharmony_ci                    total_errors++;
726a8e1175bSopenharmony_ci                    mbedtls_fprintf(stdout, "FAILED\n");
727a8e1175bSopenharmony_ci                    mbedtls_fprintf(stdout, "  %s\n  at ",
728a8e1175bSopenharmony_ci                                    mbedtls_test_get_test());
729a8e1175bSopenharmony_ci                    if (mbedtls_test_get_step() != (unsigned long) (-1)) {
730a8e1175bSopenharmony_ci                        mbedtls_fprintf(stdout, "step %lu, ",
731a8e1175bSopenharmony_ci                                        mbedtls_test_get_step());
732a8e1175bSopenharmony_ci                    }
733a8e1175bSopenharmony_ci                    mbedtls_fprintf(stdout, "line %d, %s",
734a8e1175bSopenharmony_ci                                    mbedtls_test_get_line_no(),
735a8e1175bSopenharmony_ci                                    mbedtls_get_test_filename());
736a8e1175bSopenharmony_ci
737a8e1175bSopenharmony_ci                    mbedtls_test_get_line1(line_buffer);
738a8e1175bSopenharmony_ci                    if (line_buffer[0] != 0) {
739a8e1175bSopenharmony_ci                        mbedtls_fprintf(stdout, "\n  %s", line_buffer);
740a8e1175bSopenharmony_ci                    }
741a8e1175bSopenharmony_ci                    mbedtls_test_get_line2(line_buffer);
742a8e1175bSopenharmony_ci                    if (line_buffer[0] != 0) {
743a8e1175bSopenharmony_ci                        mbedtls_fprintf(stdout, "\n  %s", line_buffer);
744a8e1175bSopenharmony_ci                    }
745a8e1175bSopenharmony_ci                }
746a8e1175bSopenharmony_ci                fflush(stdout);
747a8e1175bSopenharmony_ci            } else if (ret == DISPATCH_INVALID_TEST_DATA) {
748a8e1175bSopenharmony_ci                mbedtls_fprintf(stderr, "FAILED: FATAL PARSE ERROR\n");
749a8e1175bSopenharmony_ci                fclose(file);
750a8e1175bSopenharmony_ci                mbedtls_exit(2);
751a8e1175bSopenharmony_ci            } else if (ret == DISPATCH_TEST_FN_NOT_FOUND) {
752a8e1175bSopenharmony_ci                mbedtls_fprintf(stderr, "FAILED: FATAL TEST FUNCTION NOT FOUND\n");
753a8e1175bSopenharmony_ci                fclose(file);
754a8e1175bSopenharmony_ci                mbedtls_exit(2);
755a8e1175bSopenharmony_ci            } else {
756a8e1175bSopenharmony_ci                total_errors++;
757a8e1175bSopenharmony_ci            }
758a8e1175bSopenharmony_ci        }
759a8e1175bSopenharmony_ci        fclose(file);
760a8e1175bSopenharmony_ci    }
761a8e1175bSopenharmony_ci
762a8e1175bSopenharmony_ci    if (outcome_file != NULL) {
763a8e1175bSopenharmony_ci        fclose(outcome_file);
764a8e1175bSopenharmony_ci    }
765a8e1175bSopenharmony_ci
766a8e1175bSopenharmony_ci    mbedtls_fprintf(stdout,
767a8e1175bSopenharmony_ci                    "\n----------------------------------------------------------------------------\n\n");
768a8e1175bSopenharmony_ci    if (total_errors == 0) {
769a8e1175bSopenharmony_ci        mbedtls_fprintf(stdout, "PASSED");
770a8e1175bSopenharmony_ci    } else {
771a8e1175bSopenharmony_ci        mbedtls_fprintf(stdout, "FAILED");
772a8e1175bSopenharmony_ci    }
773a8e1175bSopenharmony_ci
774a8e1175bSopenharmony_ci    mbedtls_fprintf(stdout, " (%u / %u tests (%u skipped))\n",
775a8e1175bSopenharmony_ci                    total_tests - total_errors, total_tests, total_skipped);
776a8e1175bSopenharmony_ci
777a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_MUTEX_USAGE)
778a8e1175bSopenharmony_ci    mbedtls_test_mutex_usage_end();
779a8e1175bSopenharmony_ci#endif
780a8e1175bSopenharmony_ci
781a8e1175bSopenharmony_ci#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
782a8e1175bSopenharmony_ci    !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
783a8e1175bSopenharmony_ci#if defined(MBEDTLS_MEMORY_DEBUG)
784a8e1175bSopenharmony_ci    mbedtls_memory_buffer_alloc_status();
785a8e1175bSopenharmony_ci#endif
786a8e1175bSopenharmony_ci    mbedtls_memory_buffer_alloc_free();
787a8e1175bSopenharmony_ci#endif
788a8e1175bSopenharmony_ci
789a8e1175bSopenharmony_ci    return total_errors != 0;
790a8e1175bSopenharmony_ci}
791