blob: b2ce406769ee7f6ba04a96296ccbc89d1bb75209 [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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020011int verify_string(char **str)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010012{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020013 if ((*str)[0] != '"' || (*str)[strlen(*str) - 1] != '"') {
14 mbedtls_fprintf(
15 stderr, "Expected string (with \"\") for parameter and got: %s\n",
16 *str);
17 return -1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010018 }
19
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020020 (*str)++;
21 (*str)[strlen(*str) - 1] = '\0';
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010022
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020023 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020035int 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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020042 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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020048 if (((minus && i == 2) || (!minus && i == 1)) && str[i - 1] == '0' &&
49 (str[i] == 'x' || str[i] == 'X')) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010050 hex = 1;
51 continue;
52 }
53
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020054 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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020062 if (digits) {
63 if (hex)
64 *value = strtol(str, NULL, 16);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010065 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020066 *value = strtol(str, NULL, 10);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010067
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020068 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010069 }
70
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020071 mbedtls_fprintf(stderr, "Expected integer for parameter and got: %s\n",
72 str);
73 return KEY_VALUE_MAPPING_NOT_FOUND;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010074}
75
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010076/**
77 * \brief Usage string.
78 *
79 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020080#define USAGE \
81 "Usage: %s [OPTIONS] files...\n\n" \
82 " Command line arguments:\n" \
Mohammad Azim Khand2d01122018-07-18 17:48:37 +010083 " files... One or more test data files. If no file is\n" \
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020084 " specified the following default test case\n" \
85 " file is used:\n" \
86 " %s\n\n" \
87 " Options:\n" \
88 " -v | --verbose Display full information about each test\n" \
89 " -h | --help Display this information\n\n", \
90 argv[0], "TESTCASE_FILENAME"
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010091
92/**
93 * \brief Read a line from the passed file pointer.
94 *
95 * \param f FILE pointer
96 * \param buf Pointer to memory to hold read line.
97 * \param len Length of the buf.
98 *
99 * \return 0 if success else -1
100 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200101int get_line(FILE *f, char *buf, size_t len)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100102{
103 char *ret;
104 int i = 0, str_len = 0, has_string = 0;
105
106 /* Read until we get a valid line */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200107 do {
108 ret = fgets(buf, len, f);
109 if (ret == NULL)
110 return -1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100111
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200112 str_len = strlen(buf);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100113
114 /* Skip empty line and comment */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200115 if (str_len == 0 || buf[0] == '#')
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100116 continue;
117 has_string = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200118 for (i = 0; i < str_len; i++) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100119 char c = buf[i];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200120 if (c != ' ' && c != '\t' && c != '\n' && c != '\v' && c != '\f' &&
121 c != '\r') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100122 has_string = 1;
123 break;
124 }
125 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200126 } while (!has_string);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100127
128 /* Strip new line and carriage return */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200129 ret = buf + strlen(buf);
130 if (ret-- > buf && *ret == '\n')
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100131 *ret = '\0';
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200132 if (ret-- > buf && *ret == '\r')
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100133 *ret = '\0';
134
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200135 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100136}
137
138/**
139 * \brief Splits string delimited by ':'. Ignores '\:'.
140 *
141 * \param buf Input string
142 * \param len Input string length
143 * \param params Out params found
144 * \param params_len Out params array len
145 *
146 * \return Count of strings found.
147 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200148static int
149parse_arguments(char *buf, size_t len, char **params, size_t params_len)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100150{
151 size_t cnt = 0, i;
152 char *cur = buf;
153 char *p = buf, *q;
154
155 params[cnt++] = cur;
156
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200157 while (*p != '\0' && p < (buf + len)) {
158 if (*p == '\\') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100159 p++;
160 p++;
161 continue;
162 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200163 if (*p == ':') {
164 if (p + 1 < buf + len) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100165 cur = p + 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200166 TEST_HELPER_ASSERT(cnt < params_len);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100167 params[cnt++] = cur;
168 }
169 *p = '\0';
170 }
171
172 p++;
173 }
174
175 /* Replace newlines, question marks and colons in strings */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200176 for (i = 0; i < cnt; i++) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100177 p = params[i];
178 q = params[i];
179
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200180 while (*p != '\0') {
181 if (*p == '\\' && *(p + 1) == 'n') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100182 p += 2;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200183 *(q++) = '\n';
184 } else if (*p == '\\' && *(p + 1) == ':') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100185 p += 2;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200186 *(q++) = ':';
187 } else if (*p == '\\' && *(p + 1) == '?') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100188 p += 2;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200189 *(q++) = '?';
190 } else
191 *(q++) = *(p++);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100192 }
193 *q = '\0';
194 }
195
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200196 return cnt;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100197}
198
199/**
200 * \brief Converts parameters into test function consumable parameters.
201 * Example: Input: {"int", "0", "char*", "Hello",
202 * "hex", "abef", "exp", "1"}
203 * Output: {
204 * 0, // Verified int
205 * "Hello", // Verified string
206 * 2, { 0xab, 0xef },// Converted len,hex pair
207 * 9600 // Evaluated expression
208 * }
209 *
210 *
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100211 * \param cnt Parameter array count.
212 * \param params Out array of found parameters.
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100213 * \param int_params_store Memory for storing processed integer parameters.
214 *
215 * \return 0 for success else 1
216 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200217static int convert_params(size_t cnt, char **params, int32_t *int_params_store)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100218{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200219 char **cur = params;
220 char **out = params;
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100221 int ret = DISPATCH_TEST_SUCCESS;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100222
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200223 while (cur < params + cnt) {
224 char *type = *cur++;
225 char *val = *cur++;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100226
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200227 if (strcmp(type, "char*") == 0) {
228 if (verify_string(&val) == 0) {
229 *out++ = val;
230 } else {
231 ret = (DISPATCH_INVALID_TEST_DATA);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100232 break;
233 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200234 } else if (strcmp(type, "int") == 0) {
235 if (verify_int(val, int_params_store) == 0) {
236 *out++ = (char *)int_params_store++;
237 } else {
238 ret = (DISPATCH_INVALID_TEST_DATA);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100239 break;
240 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200241 } else if (strcmp(type, "hex") == 0) {
242 if (verify_string(&val) == 0) {
Ronald Crona0c25392020-06-18 10:10:46 +0200243 size_t len;
244
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200245 TEST_HELPER_ASSERT(mbedtls_test_unhexify((unsigned char *)val,
246 strlen(val), val,
247 &len) == 0);
Ronald Crona0c25392020-06-18 10:10:46 +0200248
249 *int_params_store = len;
Azim Khan184447e2017-05-31 20:29:36 +0100250 *out++ = val;
251 *out++ = (char *)(int_params_store++);
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200252 } else {
253 ret = (DISPATCH_INVALID_TEST_DATA);
Azim Khan184447e2017-05-31 20:29:36 +0100254 break;
255 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200256 } else if (strcmp(type, "exp") == 0) {
257 int exp_id = strtol(val, NULL, 10);
258 if (get_expression(exp_id, int_params_store) == 0) {
259 *out++ = (char *)int_params_store++;
260 } else {
261 ret = (DISPATCH_INVALID_TEST_DATA);
262 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100263 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200264 } else {
265 ret = (DISPATCH_INVALID_TEST_DATA);
266 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100267 }
268 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200269 return ret;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100270}
271
272/**
273 * \brief Tests snprintf implementation with test input.
274 *
Azim Khan191e9042017-06-09 12:39:00 +0100275 * \note
276 * At high optimization levels (e.g. gcc -O3), this function may be
277 * inlined in run_test_snprintf. This can trigger a spurious warning about
278 * potential misuse of snprintf from gcc -Wformat-truncation (observed with
279 * gcc 7.2). This warning makes tests in run_test_snprintf redundant on gcc
280 * only. They are still valid for other compilers. Avoid this warning by
281 * forbidding inlining of this function by gcc.
282 *
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100283 * \param n Buffer test length.
284 * \param ref_buf Expected buffer.
285 * \param ref_ret Expected snprintf return value.
286 *
287 * \return 0 for success else 1
288 */
Azim Khan191e9042017-06-09 12:39:00 +0100289#if defined(__GNUC__)
290__attribute__((__noinline__))
291#endif
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200292static int
293test_snprintf(size_t n, const char *ref_buf, int ref_ret)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100294{
295 int ret;
296 char buf[10] = "xxxxxxxxx";
297 const char ref[10] = "xxxxxxxxx";
298
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200299 if (n >= sizeof(buf))
300 return -1;
301 ret = mbedtls_snprintf(buf, n, "%s", "123");
302 if (ret < 0 || (size_t)ret >= n)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100303 ret = -1;
304
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200305 if (strncmp(ref_buf, buf, sizeof(buf)) != 0 || ref_ret != ret ||
306 memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) {
307 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100308 }
309
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200310 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100311}
312
313/**
314 * \brief Tests snprintf implementation.
315 *
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100316 * \return 0 for success else 1
317 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200318static int run_test_snprintf(void)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100319{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200320 return (test_snprintf(0, "xxxxxxxxx", -1) != 0 ||
321 test_snprintf(1, "", -1) != 0 || test_snprintf(2, "1", -1) != 0 ||
322 test_snprintf(3, "12", -1) != 0 ||
323 test_snprintf(4, "123", 3) != 0 || test_snprintf(5, "123", 3) != 0);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100324}
325
Gilles Peskine51dcc242019-09-16 15:13:48 +0200326/** \brief Write the description of the test case to the outcome CSV file.
327 *
328 * \param outcome_file The file to write to.
329 * If this is \c NULL, this function does nothing.
330 * \param argv0 The test suite name.
331 * \param test_case The test case description.
332 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200333static void write_outcome_entry(FILE *outcome_file,
334 const char *argv0,
335 const char *test_case)
Gilles Peskine51dcc242019-09-16 15:13:48 +0200336{
337 /* The non-varying fields are initialized on first use. */
338 static const char *platform = NULL;
339 static const char *configuration = NULL;
340 static const char *test_suite = NULL;
341
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200342 if (outcome_file == NULL)
Gilles Peskine51dcc242019-09-16 15:13:48 +0200343 return;
344
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200345 if (platform == NULL) {
346 platform = getenv("MBEDTLS_TEST_PLATFORM");
347 if (platform == NULL)
Gilles Peskine51dcc242019-09-16 15:13:48 +0200348 platform = "unknown";
349 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200350 if (configuration == NULL) {
351 configuration = getenv("MBEDTLS_TEST_CONFIGURATION");
352 if (configuration == NULL)
Gilles Peskine51dcc242019-09-16 15:13:48 +0200353 configuration = "unknown";
354 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200355 if (test_suite == NULL) {
356 test_suite = strrchr(argv0, '/');
357 if (test_suite != NULL)
Gilles Peskine51dcc242019-09-16 15:13:48 +0200358 test_suite += 1; // skip the '/'
359 else
360 test_suite = argv0;
361 }
362
363 /* Write the beginning of the outcome line.
364 * Ignore errors: writing the outcome file is on a best-effort basis. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200365 mbedtls_fprintf(outcome_file, "%s;%s;%s;%s;", platform, configuration,
366 test_suite, test_case);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200367}
368
369/** \brief Write the result of the test case to the outcome CSV file.
370 *
371 * \param outcome_file The file to write to.
372 * If this is \c NULL, this function does nothing.
Ronald Cron67a8a372020-04-01 16:04:41 +0200373 * \param unmet_dep_count The number of unmet dependencies.
374 * \param unmet_dependencies The array of unmet dependencies.
375 * \param missing_unmet_dependencies Non-zero if there was a problem tracking
376 * all unmet dependencies, 0 otherwise.
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100377 * \param ret The test dispatch status (DISPATCH_xxx).
378 * \param info A pointer to the test info structure.
Gilles Peskine51dcc242019-09-16 15:13:48 +0200379 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200380static void write_outcome_result(FILE *outcome_file,
381 size_t unmet_dep_count,
382 int unmet_dependencies[],
383 int missing_unmet_dependencies,
384 int ret,
385 const mbedtls_test_info_t *info)
Gilles Peskine51dcc242019-09-16 15:13:48 +0200386{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200387 if (outcome_file == NULL)
Gilles Peskine51dcc242019-09-16 15:13:48 +0200388 return;
389
390 /* Write the end of the outcome line.
391 * Ignore errors: writing the outcome file is on a best-effort basis. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200392 switch (ret) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200393 case DISPATCH_TEST_SUCCESS:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200394 if (unmet_dep_count > 0) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200395 size_t i;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200396 mbedtls_fprintf(outcome_file, "SKIP");
397 for (i = 0; i < unmet_dep_count; i++) {
398 mbedtls_fprintf(outcome_file, "%c%d", i == 0 ? ';' : ':',
399 unmet_dependencies[i]);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200400 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200401 if (missing_unmet_dependencies)
402 mbedtls_fprintf(outcome_file, ":...");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200403 break;
404 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200405 switch (info->result) {
Chris Jonese60e2ae2021-01-20 17:51:47 +0000406 case MBEDTLS_TEST_RESULT_SUCCESS:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200407 mbedtls_fprintf(outcome_file, "PASS;");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200408 break;
Chris Jonese60e2ae2021-01-20 17:51:47 +0000409 case MBEDTLS_TEST_RESULT_SKIPPED:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200410 mbedtls_fprintf(outcome_file, "SKIP;Runtime skip");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200411 break;
412 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200413 mbedtls_fprintf(outcome_file, "FAIL;%s:%d:%s",
414 info->filename, info->line_no, info->test);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200415 break;
416 }
417 break;
418 case DISPATCH_TEST_FN_NOT_FOUND:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200419 mbedtls_fprintf(outcome_file, "FAIL;Test function not found");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200420 break;
421 case DISPATCH_INVALID_TEST_DATA:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200422 mbedtls_fprintf(outcome_file, "FAIL;Invalid test data");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200423 break;
424 case DISPATCH_UNSUPPORTED_SUITE:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200425 mbedtls_fprintf(outcome_file, "SKIP;Unsupported suite");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200426 break;
427 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200428 mbedtls_fprintf(outcome_file, "FAIL;Unknown cause");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200429 break;
430 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200431 mbedtls_fprintf(outcome_file, "\n");
432 fflush(outcome_file);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200433}
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100434
435/**
436 * \brief Desktop implementation of execute_tests().
437 * Parses command line and executes tests from
438 * supplied or default data file.
439 *
440 * \param argc Command line argument count.
441 * \param argv Argument array.
442 *
443 * \return Program exit status.
444 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200445int execute_tests(int argc, const char **argv)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100446{
447 /* Local Configurations and options */
448 const char *default_filename = "DATA_FILE";
449 const char *test_filename = NULL;
450 const char **test_files = NULL;
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200451 size_t testfile_count = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100452 int option_verbose = 0;
k-stachowiak03954f22019-09-16 10:23:10 +0200453 size_t function_id = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100454
455 /* Other Local variables */
456 int arg_index = 1;
457 const char *next_arg;
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200458 size_t testfile_index, i, cnt;
459 int ret;
460 unsigned total_errors = 0, total_tests = 0, total_skipped = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100461 FILE *file;
462 char buf[5000];
463 char *params[50];
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100464 /* Store for proccessed integer params. */
Gilles Peskinea7a43062021-05-18 16:39:33 +0200465 int32_t int_params[50];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100466 void *pointer;
467#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
468 int stdout_fd = -1;
469#endif /* __unix__ || __APPLE__ __MACH__ */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200470 const char *outcome_file_name = getenv("MBEDTLS_TEST_OUTCOME_FILE");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200471 FILE *outcome_file = NULL;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100472
473#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
474 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
475 unsigned char alloc_buf[1000000];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200476 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100477#endif
478
Gilles Peskine1061ec62021-01-29 21:17:11 +0100479#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200480 mbedtls_test_mutex_usage_init();
Gilles Peskine1061ec62021-01-29 21:17:11 +0100481#endif
482
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100483 /*
484 * The C standard doesn't guarantee that all-bits-0 is the representation
485 * of a NULL pointer. We do however use that in our code for initializing
486 * structures, which should work on every modern platform. Let's be sure.
487 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200488 memset(&pointer, 0, sizeof(void *));
489 if (pointer != NULL) {
490 mbedtls_fprintf(stderr, "all-bits-zero is not a NULL pointer\n");
491 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100492 }
493
494 /*
495 * Make sure we have a snprintf that correctly zero-terminates
496 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200497 if (run_test_snprintf() != 0) {
498 mbedtls_fprintf(stderr, "the snprintf implementation is broken\n");
499 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100500 }
501
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200502 if (outcome_file_name != NULL && *outcome_file_name != '\0') {
503 outcome_file = fopen(outcome_file_name, "a");
504 if (outcome_file == NULL) {
505 mbedtls_fprintf(
506 stderr, "Unable to open outcome file. Continuing anyway.\n");
Gilles Peskine9c673232020-01-21 18:03:56 +0100507 }
508 }
509
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200510 while (arg_index < argc) {
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100511 next_arg = argv[arg_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100512
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200513 if (strcmp(next_arg, "--verbose") == 0 || strcmp(next_arg, "-v") == 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100514 option_verbose = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200515 } else if (strcmp(next_arg, "--help") == 0 ||
516 strcmp(next_arg, "-h") == 0) {
517 mbedtls_fprintf(stdout, USAGE);
518 mbedtls_exit(EXIT_SUCCESS);
519 } else {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100520 /* Not an option, therefore treat all further arguments as the file
521 * list.
522 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200523 test_files = &argv[arg_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100524 testfile_count = argc - arg_index;
525 }
526
527 arg_index++;
528 }
529
530 /* If no files were specified, assume a default */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200531 if (test_files == NULL || testfile_count == 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100532 test_files = &default_filename;
533 testfile_count = 1;
534 }
535
536 /* Initialize the struct that holds information about the last test */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200537 mbedtls_test_info_reset();
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100538
539 /* Now begin to execute the tests in the testfiles */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200540 for (testfile_index = 0; testfile_index < testfile_count;
541 testfile_index++) {
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200542 size_t unmet_dep_count = 0;
Gilles Peskine06725c92020-03-30 21:39:09 +0200543 int unmet_dependencies[20];
Ronald Cron67a8a372020-04-01 16:04:41 +0200544 int missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100545
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200546 test_filename = test_files[testfile_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100547
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200548 file = fopen(test_filename, "r");
549 if (file == NULL) {
550 mbedtls_fprintf(stderr, "Failed to open test file: %s\n",
551 test_filename);
552 if (outcome_file != NULL)
553 fclose(outcome_file);
554 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100555 }
556
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200557 while (!feof(file)) {
558 if (unmet_dep_count > 0) {
559 mbedtls_fprintf(
560 stderr,
561 "FATAL: Dep count larger than zero at start of loop\n");
562 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100563 }
564 unmet_dep_count = 0;
Ronald Cron67a8a372020-04-01 16:04:41 +0200565 missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100566
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200567 if ((ret = get_line(file, buf, sizeof(buf))) != 0)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100568 break;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200569 mbedtls_fprintf(
570 stdout, "%s%.66s",
571 mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED ? "\n" :
572 "",
573 buf);
574 mbedtls_fprintf(stdout, " ");
575 for (i = strlen(buf) + 1; i < 67; i++)
576 mbedtls_fprintf(stdout, ".");
577 mbedtls_fprintf(stdout, " ");
578 fflush(stdout);
579 write_outcome_entry(outcome_file, argv[0], buf);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100580
581 total_tests++;
582
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200583 if ((ret = get_line(file, buf, sizeof(buf))) != 0)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100584 break;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200585 cnt = parse_arguments(buf, strlen(buf), params,
586 sizeof(params) / sizeof(params[0]));
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100587
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200588 if (strcmp(params[0], "depends_on") == 0) {
589 for (i = 1; i < cnt; i++) {
590 int dep_id = strtol(params[i], NULL, 10);
591 if (dep_check(dep_id) != DEPENDENCY_SUPPORTED) {
592 if (unmet_dep_count <
593 ARRAY_LENGTH(unmet_dependencies)) {
Ronald Crone1a05a52020-04-01 15:52:06 +0200594 unmet_dependencies[unmet_dep_count] = dep_id;
595 unmet_dep_count++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200596 } else {
Ronald Cron67a8a372020-04-01 16:04:41 +0200597 missing_unmet_dependencies = 1;
598 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100599 }
600 }
601
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200602 if ((ret = get_line(file, buf, sizeof(buf))) != 0)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100603 break;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200604 cnt = parse_arguments(buf, strlen(buf), params,
605 sizeof(params) / sizeof(params[0]));
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100606 }
607
608 // If there are no unmet dependencies execute the test
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200609 if (unmet_dep_count == 0) {
610 mbedtls_test_info_reset();
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100611
612#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
613 /* Suppress all output from the library unless we're verbose
614 * mode
615 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200616 if (!option_verbose) {
617 stdout_fd = redirect_output(stdout, "/dev/null");
618 if (stdout_fd == -1) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100619 /* Redirection has failed with no stdout so exit */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200620 exit(1);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100621 }
622 }
623#endif /* __unix__ || __APPLE__ __MACH__ */
624
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200625 function_id = strtoul(params[0], NULL, 10);
626 if ((ret = check_test(function_id)) == DISPATCH_TEST_SUCCESS) {
627 ret = convert_params(cnt - 1, params + 1, int_params);
628 if (DISPATCH_TEST_SUCCESS == ret) {
629 ret = dispatch_test(function_id, (void **)(params + 1));
Azim Khan13c6bfb2017-06-15 14:45:56 +0100630 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100631 }
632
633#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200634 if (!option_verbose && restore_output(stdout, stdout_fd)) {
635 /* Redirection has failed with no stdout so exit */
636 exit(1);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100637 }
638#endif /* __unix__ || __APPLE__ __MACH__ */
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100639 }
640
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200641 write_outcome_result(outcome_file, unmet_dep_count,
642 unmet_dependencies, missing_unmet_dependencies,
643 ret, &mbedtls_test_info);
644 if (unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100645 total_skipped++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200646 mbedtls_fprintf(stdout, "----");
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100647
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200648 if (1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE) {
649 mbedtls_fprintf(stdout, "\n Test Suite not enabled");
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100650 }
651
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200652 if (1 == option_verbose && unmet_dep_count > 0) {
653 mbedtls_fprintf(stdout, "\n Unmet dependencies: ");
654 for (i = 0; i < unmet_dep_count; i++) {
655 mbedtls_fprintf(stdout, "%d ", unmet_dependencies[i]);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100656 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200657 if (missing_unmet_dependencies)
658 mbedtls_fprintf(stdout, "...");
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100659 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200660 mbedtls_fprintf(stdout, "\n");
661 fflush(stdout);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100662
663 unmet_dep_count = 0;
Ronald Cron67a8a372020-04-01 16:04:41 +0200664 missing_unmet_dependencies = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200665 } else if (ret == DISPATCH_TEST_SUCCESS) {
666 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS) {
667 mbedtls_fprintf(stdout, "PASS\n");
668 } else if (mbedtls_test_info.result ==
669 MBEDTLS_TEST_RESULT_SKIPPED) {
670 mbedtls_fprintf(stdout, "----\n");
Hanno Beckere69d0152019-07-05 13:31:30 +0100671 total_skipped++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200672 } else {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100673 total_errors++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200674 mbedtls_fprintf(stdout, "FAILED\n");
675 mbedtls_fprintf(stdout, " %s\n at ",
676 mbedtls_test_info.test);
677 if (mbedtls_test_info.step != (unsigned long)(-1)) {
678 mbedtls_fprintf(stdout, "step %lu, ",
679 mbedtls_test_info.step);
Gilles Peskine56055912019-03-01 14:26:30 +0100680 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200681 mbedtls_fprintf(stdout, "line %d, %s",
682 mbedtls_test_info.line_no,
683 mbedtls_test_info.filename);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100684 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200685 fflush(stdout);
686 } else if (ret == DISPATCH_INVALID_TEST_DATA) {
687 mbedtls_fprintf(stderr, "FAILED: FATAL PARSE ERROR\n");
688 fclose(file);
689 mbedtls_exit(2);
690 } else if (ret == DISPATCH_TEST_FN_NOT_FOUND) {
691 mbedtls_fprintf(stderr,
692 "FAILED: FATAL TEST FUNCTION NOT FOUND\n");
693 fclose(file);
694 mbedtls_exit(2);
695 } else
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100696 total_errors++;
697 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200698 fclose(file);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100699 }
700
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200701 if (outcome_file != NULL)
702 fclose(outcome_file);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200703
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200704 mbedtls_fprintf(
705 stdout,
706 "\n----------------------------------------------------------------------------\n\n");
707 if (total_errors == 0)
708 mbedtls_fprintf(stdout, "PASSED");
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100709 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200710 mbedtls_fprintf(stdout, "FAILED");
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100711
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200712 mbedtls_fprintf(stdout, " (%u / %u tests (%u skipped))\n",
713 total_tests - total_errors, total_tests, total_skipped);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100714
715#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
716 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200717# if defined(MBEDTLS_MEMORY_DEBUG)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100718 mbedtls_memory_buffer_alloc_status();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200719# endif
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100720 mbedtls_memory_buffer_alloc_free();
721#endif
722
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200723 return total_errors != 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100724}