blob: cc286973cf0135a93a1d4ff6c8ed43cdf3571770 [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.
Gilles Peskineb3c2eaf2022-12-04 13:10:55 +010031 * \param p_value Pointer to output value.
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010032 *
33 * \return 0 if success else 1
34 */
Gilles Peskine872948c2022-12-04 15:32:54 +010035int verify_int(char *str, intmax_t *p_value)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010036{
Gilles Peskine5226eb52022-12-04 00:28:56 +010037 char *end = NULL;
38 errno = 0;
Gilles Peskine872948c2022-12-04 15:32:54 +010039 /* Limit the range to long: for large integers, the test framework will
40 * use expressions anyway. */
Gilles Peskine5226eb52022-12-04 00:28:56 +010041 long value = strtol(str, &end, 0);
42 if (errno == EINVAL || *end != '\0') {
43 mbedtls_fprintf(stderr,
44 "Expected integer for parameter and got: %s\n", str);
45 return KEY_VALUE_MAPPING_NOT_FOUND;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010046 }
Gilles Peskine872948c2022-12-04 15:32:54 +010047 if (errno == ERANGE) {
Gilles Peskine5226eb52022-12-04 00:28:56 +010048 mbedtls_fprintf(stderr, "Integer out of range: %s\n", str);
49 return KEY_VALUE_MAPPING_NOT_FOUND;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010050 }
Gilles Peskine5226eb52022-12-04 00:28:56 +010051 *p_value = value;
52 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010053}
54
55
56/**
57 * \brief Usage string.
58 *
59 */
60#define USAGE \
61 "Usage: %s [OPTIONS] files...\n\n" \
62 " Command line arguments:\n" \
Mohammad Azim Khand2d01122018-07-18 17:48:37 +010063 " files... One or more test data files. If no file is\n" \
64 " specified the following default test case\n" \
65 " file is used:\n" \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010066 " %s\n\n" \
67 " Options:\n" \
68 " -v | --verbose Display full information about each test\n" \
69 " -h | --help Display this information\n\n", \
70 argv[0], \
71 "TESTCASE_FILENAME"
72
73
74/**
75 * \brief Read a line from the passed file pointer.
76 *
77 * \param f FILE pointer
78 * \param buf Pointer to memory to hold read line.
79 * \param len Length of the buf.
80 *
81 * \return 0 if success else -1
82 */
Gilles Peskine449bd832023-01-11 14:50:10 +010083int get_line(FILE *f, char *buf, size_t len)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010084{
85 char *ret;
86 int i = 0, str_len = 0, has_string = 0;
87
88 /* Read until we get a valid line */
Gilles Peskine449bd832023-01-11 14:50:10 +010089 do {
90 ret = fgets(buf, len, f);
91 if (ret == NULL) {
92 return -1;
93 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010094
Gilles Peskine449bd832023-01-11 14:50:10 +010095 str_len = strlen(buf);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010096
97 /* Skip empty line and comment */
Gilles Peskine449bd832023-01-11 14:50:10 +010098 if (str_len == 0 || buf[0] == '#') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010099 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100101 has_string = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 for (i = 0; i < str_len; i++) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100103 char c = buf[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 if (c != ' ' && c != '\t' && c != '\n' &&
105 c != '\v' && c != '\f' && c != '\r') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100106 has_string = 1;
107 break;
108 }
109 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 } while (!has_string);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100111
112 /* Strip new line and carriage return */
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 ret = buf + strlen(buf);
114 if (ret-- > buf && *ret == '\n') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100115 *ret = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 }
117 if (ret-- > buf && *ret == '\r') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100118 *ret = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100122}
123
124/**
125 * \brief Splits string delimited by ':'. Ignores '\:'.
126 *
127 * \param buf Input string
128 * \param len Input string length
129 * \param params Out params found
130 * \param params_len Out params array len
131 *
132 * \return Count of strings found.
133 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100134static int parse_arguments(char *buf, size_t len, char **params,
135 size_t params_len)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100136{
137 size_t cnt = 0, i;
138 char *cur = buf;
139 char *p = buf, *q;
140
141 params[cnt++] = cur;
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 while (*p != '\0' && p < (buf + len)) {
144 if (*p == '\\') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100145 p++;
146 p++;
147 continue;
148 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if (*p == ':') {
150 if (p + 1 < buf + len) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100151 cur = p + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 TEST_HELPER_ASSERT(cnt < params_len);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100153 params[cnt++] = cur;
154 }
155 *p = '\0';
156 }
157
158 p++;
159 }
160
Gilles Peskine1a248952022-12-03 23:48:25 +0100161 /* Replace backslash escapes in strings */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 for (i = 0; i < cnt; i++) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100163 p = params[i];
164 q = params[i];
165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 while (*p != '\0') {
Gilles Peskine1a248952022-12-03 23:48:25 +0100167 if (*p == '\\') {
168 ++p;
169 switch (*p) {
170 case 'n':
171 *p = '\n';
172 break;
173 default:
174 // Fall through to copying *p
175 break;
176 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100177 }
Gilles Peskine1a248952022-12-03 23:48:25 +0100178 *(q++) = *(p++);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100179 }
180 *q = '\0';
181 }
182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return cnt;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100184}
185
186/**
187 * \brief Converts parameters into test function consumable parameters.
188 * Example: Input: {"int", "0", "char*", "Hello",
189 * "hex", "abef", "exp", "1"}
190 * Output: {
191 * 0, // Verified int
192 * "Hello", // Verified string
193 * 2, { 0xab, 0xef },// Converted len,hex pair
194 * 9600 // Evaluated expression
195 * }
196 *
197 *
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100198 * \param cnt Parameter array count.
199 * \param params Out array of found parameters.
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100200 * \param int_params_store Memory for storing processed integer parameters.
201 *
202 * \return 0 for success else 1
203 */
Gilles Peskineb3c2eaf2022-12-04 13:10:55 +0100204static int convert_params(size_t cnt, char **params,
205 mbedtls_test_argument_t *int_params_store)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100206{
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 char **cur = params;
208 char **out = params;
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100209 int ret = DISPATCH_TEST_SUCCESS;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 while (cur < params + cnt) {
212 char *type = *cur++;
213 char *val = *cur++;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 if (strcmp(type, "char*") == 0) {
216 if (verify_string(&val) == 0) {
217 *out++ = val;
218 } else {
219 ret = (DISPATCH_INVALID_TEST_DATA);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100220 break;
221 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 } else if (strcmp(type, "int") == 0) {
Gilles Peskine872948c2022-12-04 15:32:54 +0100223 if (verify_int(val, &int_params_store->sint) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 *out++ = (char *) int_params_store++;
225 } else {
226 ret = (DISPATCH_INVALID_TEST_DATA);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100227 break;
228 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 } else if (strcmp(type, "hex") == 0) {
230 if (verify_string(&val) == 0) {
Ronald Crona0c25392020-06-18 10:10:46 +0200231 size_t len;
232
233 TEST_HELPER_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 mbedtls_test_unhexify((unsigned char *) val, strlen(val),
235 val, &len) == 0);
Ronald Crona0c25392020-06-18 10:10:46 +0200236
Gilles Peskineb3c2eaf2022-12-04 13:10:55 +0100237 int_params_store->len = len;
Azim Khan184447e2017-05-31 20:29:36 +0100238 *out++ = val;
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 *out++ = (char *) (int_params_store++);
240 } else {
241 ret = (DISPATCH_INVALID_TEST_DATA);
Azim Khan184447e2017-05-31 20:29:36 +0100242 break;
243 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 } else if (strcmp(type, "exp") == 0) {
245 int exp_id = strtol(val, NULL, 10);
Gilles Peskine872948c2022-12-04 15:32:54 +0100246 if (get_expression(exp_id, &int_params_store->sint) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 *out++ = (char *) int_params_store++;
248 } else {
249 ret = (DISPATCH_INVALID_TEST_DATA);
250 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100251 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 } else {
253 ret = (DISPATCH_INVALID_TEST_DATA);
254 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100255 }
256 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return ret;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100258}
259
260/**
261 * \brief Tests snprintf implementation with test input.
262 *
Azim Khan191e9042017-06-09 12:39:00 +0100263 * \note
264 * At high optimization levels (e.g. gcc -O3), this function may be
265 * inlined in run_test_snprintf. This can trigger a spurious warning about
266 * potential misuse of snprintf from gcc -Wformat-truncation (observed with
267 * gcc 7.2). This warning makes tests in run_test_snprintf redundant on gcc
268 * only. They are still valid for other compilers. Avoid this warning by
269 * forbidding inlining of this function by gcc.
270 *
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100271 * \param n Buffer test length.
272 * \param ref_buf Expected buffer.
273 * \param ref_ret Expected snprintf return value.
274 *
275 * \return 0 for success else 1
276 */
Azim Khan191e9042017-06-09 12:39:00 +0100277#if defined(__GNUC__)
278__attribute__((__noinline__))
279#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100280static int test_snprintf(size_t n, const char *ref_buf, int ref_ret)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100281{
282 int ret;
283 char buf[10] = "xxxxxxxxx";
284 const char ref[10] = "xxxxxxxxx";
285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 if (n >= sizeof(buf)) {
287 return -1;
288 }
289 ret = mbedtls_snprintf(buf, n, "%s", "123");
290 if (ret < 0 || (size_t) ret >= n) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100291 ret = -1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100292 }
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (strncmp(ref_buf, buf, sizeof(buf)) != 0 ||
295 ref_ret != ret ||
296 memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) {
297 return 1;
298 }
299
300 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100301}
302
303/**
304 * \brief Tests snprintf implementation.
305 *
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100306 * \return 0 for success else 1
307 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100308static int run_test_snprintf(void)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100309{
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return test_snprintf(0, "xxxxxxxxx", -1) != 0 ||
311 test_snprintf(1, "", -1) != 0 ||
312 test_snprintf(2, "1", -1) != 0 ||
313 test_snprintf(3, "12", -1) != 0 ||
314 test_snprintf(4, "123", 3) != 0 ||
315 test_snprintf(5, "123", 3) != 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100316}
317
Gilles Peskine51dcc242019-09-16 15:13:48 +0200318/** \brief Write the description of the test case to the outcome CSV file.
319 *
320 * \param outcome_file The file to write to.
321 * If this is \c NULL, this function does nothing.
322 * \param argv0 The test suite name.
323 * \param test_case The test case description.
324 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100325static void write_outcome_entry(FILE *outcome_file,
326 const char *argv0,
327 const char *test_case)
Gilles Peskine51dcc242019-09-16 15:13:48 +0200328{
329 /* The non-varying fields are initialized on first use. */
330 static const char *platform = NULL;
331 static const char *configuration = NULL;
332 static const char *test_suite = NULL;
333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (outcome_file == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200335 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (platform == NULL) {
339 platform = getenv("MBEDTLS_TEST_PLATFORM");
340 if (platform == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200341 platform = "unknown";
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200343 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (configuration == NULL) {
345 configuration = getenv("MBEDTLS_TEST_CONFIGURATION");
346 if (configuration == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200347 configuration = "unknown";
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200349 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (test_suite == NULL) {
351 test_suite = strrchr(argv0, '/');
352 if (test_suite != NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200353 test_suite += 1; // skip the '/'
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 } else {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200355 test_suite = argv0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200357 }
358
359 /* Write the beginning of the outcome line.
360 * Ignore errors: writing the outcome file is on a best-effort basis. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 mbedtls_fprintf(outcome_file, "%s;%s;%s;%s;",
362 platform, configuration, test_suite, test_case);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200363}
364
365/** \brief Write the result of the test case to the outcome CSV file.
366 *
367 * \param outcome_file The file to write to.
368 * If this is \c NULL, this function does nothing.
Ronald Cron67a8a372020-04-01 16:04:41 +0200369 * \param unmet_dep_count The number of unmet dependencies.
370 * \param unmet_dependencies The array of unmet dependencies.
371 * \param missing_unmet_dependencies Non-zero if there was a problem tracking
372 * all unmet dependencies, 0 otherwise.
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100373 * \param ret The test dispatch status (DISPATCH_xxx).
374 * \param info A pointer to the test info structure.
Gilles Peskine51dcc242019-09-16 15:13:48 +0200375 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376static void write_outcome_result(FILE *outcome_file,
377 size_t unmet_dep_count,
378 int unmet_dependencies[],
379 int missing_unmet_dependencies,
380 int ret,
381 const mbedtls_test_info_t *info)
Gilles Peskine51dcc242019-09-16 15:13:48 +0200382{
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (outcome_file == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200384 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200386
387 /* Write the end of the outcome line.
388 * Ignore errors: writing the outcome file is on a best-effort basis. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 switch (ret) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200390 case DISPATCH_TEST_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (unmet_dep_count > 0) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200392 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 mbedtls_fprintf(outcome_file, "SKIP");
394 for (i = 0; i < unmet_dep_count; i++) {
395 mbedtls_fprintf(outcome_file, "%c%d",
396 i == 0 ? ';' : ':',
397 unmet_dependencies[i]);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200398 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if (missing_unmet_dependencies) {
400 mbedtls_fprintf(outcome_file, ":...");
401 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200402 break;
403 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 switch (info->result) {
Chris Jonese60e2ae2021-01-20 17:51:47 +0000405 case MBEDTLS_TEST_RESULT_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 mbedtls_fprintf(outcome_file, "PASS;");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200407 break;
Chris Jonese60e2ae2021-01-20 17:51:47 +0000408 case MBEDTLS_TEST_RESULT_SKIPPED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 mbedtls_fprintf(outcome_file, "SKIP;Runtime skip");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200410 break;
411 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 mbedtls_fprintf(outcome_file, "FAIL;%s:%d:%s",
413 info->filename, info->line_no,
414 info->test);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200415 break;
416 }
417 break;
418 case DISPATCH_TEST_FN_NOT_FOUND:
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 mbedtls_fprintf(outcome_file, "FAIL;Test function not found");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200420 break;
421 case DISPATCH_INVALID_TEST_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 mbedtls_fprintf(outcome_file, "FAIL;Invalid test data");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200423 break;
424 case DISPATCH_UNSUPPORTED_SUITE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 mbedtls_fprintf(outcome_file, "SKIP;Unsupported suite");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200426 break;
427 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 mbedtls_fprintf(outcome_file, "FAIL;Unknown cause");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200429 break;
430 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 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
Gilles Peskine21bff212023-10-02 20:09:35 +0200435#if defined(__unix__) || \
Gilles Peskineca260822023-10-03 10:01:43 +0200436 (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine21bff212023-10-02 20:09:35 +0200437#define MBEDTLS_HAVE_CHDIR
438#endif
439
440#if defined(MBEDTLS_HAVE_CHDIR)
441/** Try chdir to the directory containing argv0.
442 *
443 * Failures are silent.
444 */
Gilles Peskinebf3c3fa2023-10-25 17:40:19 +0200445static void try_chdir_if_supported(const char *argv0)
Gilles Peskine21bff212023-10-02 20:09:35 +0200446{
Gilles Peskinec7600192023-10-05 17:23:58 +0200447 /* We might want to allow backslash as well, for Windows. But then we also
448 * need to consider chdir() vs _chdir(), and different conventions
449 * regarding paths in argv[0] (naively enabling this code with
450 * backslash support on Windows leads to chdir into the wrong directory
451 * on the CI). */
Gilles Peskine21bff212023-10-02 20:09:35 +0200452 const char *slash = strrchr(argv0, '/');
453 if (slash == NULL) {
454 return;
455 }
456 size_t path_size = slash - argv0 + 1;
457 char *path = mbedtls_calloc(1, path_size);
458 if (path == NULL) {
459 return;
460 }
461 memcpy(path, argv0, path_size - 1);
462 path[path_size - 1] = 0;
Gilles Peskineca260822023-10-03 10:01:43 +0200463 int ret = chdir(path);
464 if (ret != 0) {
465 mbedtls_fprintf(stderr, "%s: note: chdir(\"%s\") failed.\n",
466 __func__, path);
467 }
Gilles Peskine21bff212023-10-02 20:09:35 +0200468 mbedtls_free(path);
469}
Gilles Peskinebf3c3fa2023-10-25 17:40:19 +0200470#else /* MBEDTLS_HAVE_CHDIR */
471/* No chdir() or no support for parsing argv[0] on this platform. */
472static void try_chdir_if_supported(const char *argv0)
473{
474 (void) argv0;
475 return;
476}
Gilles Peskine21bff212023-10-02 20:09:35 +0200477#endif /* MBEDTLS_HAVE_CHDIR */
478
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100479/**
480 * \brief Desktop implementation of execute_tests().
481 * Parses command line and executes tests from
482 * supplied or default data file.
483 *
484 * \param argc Command line argument count.
485 * \param argv Argument array.
486 *
487 * \return Program exit status.
488 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100489int execute_tests(int argc, const char **argv)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100490{
491 /* Local Configurations and options */
492 const char *default_filename = "DATA_FILE";
493 const char *test_filename = NULL;
494 const char **test_files = NULL;
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200495 size_t testfile_count = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100496 int option_verbose = 0;
k-stachowiak03954f22019-09-16 10:23:10 +0200497 size_t function_id = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100498
499 /* Other Local variables */
500 int arg_index = 1;
501 const char *next_arg;
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200502 size_t testfile_index, i, cnt;
503 int ret;
504 unsigned total_errors = 0, total_tests = 0, total_skipped = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100505 FILE *file;
506 char buf[5000];
507 char *params[50];
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800508 /* Store for processed integer params. */
Gilles Peskineb3c2eaf2022-12-04 13:10:55 +0100509 mbedtls_test_argument_t int_params[50];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100510 void *pointer;
511#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
512 int stdout_fd = -1;
513#endif /* __unix__ || __APPLE__ __MACH__ */
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 const char *outcome_file_name = getenv("MBEDTLS_TEST_OUTCOME_FILE");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200515 FILE *outcome_file = NULL;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100516
517#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
518 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
519 unsigned char alloc_buf[1000000];
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100521#endif
522
Gilles Peskine1061ec62021-01-29 21:17:11 +0100523#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 mbedtls_test_mutex_usage_init();
Gilles Peskine1061ec62021-01-29 21:17:11 +0100525#endif
526
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100527 /*
528 * The C standard doesn't guarantee that all-bits-0 is the representation
529 * of a NULL pointer. We do however use that in our code for initializing
530 * structures, which should work on every modern platform. Let's be sure.
531 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 memset(&pointer, 0, sizeof(void *));
533 if (pointer != NULL) {
534 mbedtls_fprintf(stderr, "all-bits-zero is not a NULL pointer\n");
535 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100536 }
537
538 /*
539 * Make sure we have a snprintf that correctly zero-terminates
540 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if (run_test_snprintf() != 0) {
542 mbedtls_fprintf(stderr, "the snprintf implementation is broken\n");
543 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100544 }
545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (outcome_file_name != NULL && *outcome_file_name != '\0') {
547 outcome_file = fopen(outcome_file_name, "a");
548 if (outcome_file == NULL) {
549 mbedtls_fprintf(stderr, "Unable to open outcome file. Continuing anyway.\n");
Gilles Peskine9c673232020-01-21 18:03:56 +0100550 }
551 }
552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 while (arg_index < argc) {
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100554 next_arg = argv[arg_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (strcmp(next_arg, "--verbose") == 0 ||
557 strcmp(next_arg, "-v") == 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100558 option_verbose = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 } else if (strcmp(next_arg, "--help") == 0 ||
560 strcmp(next_arg, "-h") == 0) {
561 mbedtls_fprintf(stdout, USAGE);
562 mbedtls_exit(EXIT_SUCCESS);
563 } else {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100564 /* Not an option, therefore treat all further arguments as the file
565 * list.
566 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 test_files = &argv[arg_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100568 testfile_count = argc - arg_index;
Tuvshinzaya Erdenekhuu8988e232022-06-17 10:19:56 +0100569 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100570 }
571
572 arg_index++;
573 }
574
575 /* If no files were specified, assume a default */
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if (test_files == NULL || testfile_count == 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100577 test_files = &default_filename;
578 testfile_count = 1;
579 }
580
581 /* Initialize the struct that holds information about the last test */
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 mbedtls_test_info_reset();
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100583
584 /* Now begin to execute the tests in the testfiles */
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 for (testfile_index = 0;
586 testfile_index < testfile_count;
587 testfile_index++) {
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200588 size_t unmet_dep_count = 0;
Gilles Peskine06725c92020-03-30 21:39:09 +0200589 int unmet_dependencies[20];
Ronald Cron67a8a372020-04-01 16:04:41 +0200590 int missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 test_filename = test_files[testfile_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 file = fopen(test_filename, "r");
595 if (file == NULL) {
596 mbedtls_fprintf(stderr, "Failed to open test file: %s\n",
597 test_filename);
598 if (outcome_file != NULL) {
599 fclose(outcome_file);
600 }
601 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100602 }
603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 while (!feof(file)) {
605 if (unmet_dep_count > 0) {
606 mbedtls_fprintf(stderr,
607 "FATAL: Dep count larger than zero at start of loop\n");
608 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100609 }
610 unmet_dep_count = 0;
Ronald Cron67a8a372020-04-01 16:04:41 +0200611 missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 if ((ret = get_line(file, buf, sizeof(buf))) != 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100614 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 }
616 mbedtls_fprintf(stdout, "%s%.66s",
617 mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED ?
618 "\n" : "", buf);
619 mbedtls_fprintf(stdout, " ");
620 for (i = strlen(buf) + 1; i < 67; i++) {
621 mbedtls_fprintf(stdout, ".");
622 }
623 mbedtls_fprintf(stdout, " ");
624 fflush(stdout);
625 write_outcome_entry(outcome_file, argv[0], buf);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100626
627 total_tests++;
628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if ((ret = get_line(file, buf, sizeof(buf))) != 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100630 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 }
632 cnt = parse_arguments(buf, strlen(buf), params,
633 sizeof(params) / sizeof(params[0]));
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if (strcmp(params[0], "depends_on") == 0) {
636 for (i = 1; i < cnt; i++) {
637 int dep_id = strtol(params[i], NULL, 10);
638 if (dep_check(dep_id) != DEPENDENCY_SUPPORTED) {
639 if (unmet_dep_count <
640 ARRAY_LENGTH(unmet_dependencies)) {
Ronald Crone1a05a52020-04-01 15:52:06 +0200641 unmet_dependencies[unmet_dep_count] = dep_id;
642 unmet_dep_count++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 } else {
Ronald Cron67a8a372020-04-01 16:04:41 +0200644 missing_unmet_dependencies = 1;
645 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100646 }
647 }
648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if ((ret = get_line(file, buf, sizeof(buf))) != 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100650 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 }
652 cnt = parse_arguments(buf, strlen(buf), params,
653 sizeof(params) / sizeof(params[0]));
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100654 }
655
656 // If there are no unmet dependencies execute the test
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if (unmet_dep_count == 0) {
658 mbedtls_test_info_reset();
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100659
660#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
661 /* Suppress all output from the library unless we're verbose
662 * mode
663 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (!option_verbose) {
665 stdout_fd = redirect_output(stdout, "/dev/null");
666 if (stdout_fd == -1) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100667 /* Redirection has failed with no stdout so exit */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 exit(1);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100669 }
670 }
671#endif /* __unix__ || __APPLE__ __MACH__ */
672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 function_id = strtoul(params[0], NULL, 10);
674 if ((ret = check_test(function_id)) == DISPATCH_TEST_SUCCESS) {
675 ret = convert_params(cnt - 1, params + 1, int_params);
676 if (DISPATCH_TEST_SUCCESS == ret) {
677 ret = dispatch_test(function_id, (void **) (params + 1));
Azim Khan13c6bfb2017-06-15 14:45:56 +0100678 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100679 }
680
681#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (!option_verbose && restore_output(stdout, stdout_fd)) {
683 /* Redirection has failed with no stdout so exit */
684 exit(1);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100685 }
686#endif /* __unix__ || __APPLE__ __MACH__ */
687
688 }
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 write_outcome_result(outcome_file,
691 unmet_dep_count, unmet_dependencies,
692 missing_unmet_dependencies,
693 ret, &mbedtls_test_info);
694 if (unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100695 total_skipped++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 mbedtls_fprintf(stdout, "----");
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE) {
699 mbedtls_fprintf(stdout, "\n Test Suite not enabled");
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100700 }
701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if (1 == option_verbose && unmet_dep_count > 0) {
703 mbedtls_fprintf(stdout, "\n Unmet dependencies: ");
704 for (i = 0; i < unmet_dep_count; i++) {
705 mbedtls_fprintf(stdout, "%d ",
706 unmet_dependencies[i]);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100707 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (missing_unmet_dependencies) {
709 mbedtls_fprintf(stdout, "...");
710 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100711 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 mbedtls_fprintf(stdout, "\n");
713 fflush(stdout);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100714
715 unmet_dep_count = 0;
Ronald Cron67a8a372020-04-01 16:04:41 +0200716 missing_unmet_dependencies = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 } else if (ret == DISPATCH_TEST_SUCCESS) {
718 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS) {
719 mbedtls_fprintf(stdout, "PASS\n");
720 } else if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SKIPPED) {
721 mbedtls_fprintf(stdout, "----\n");
Hanno Beckere69d0152019-07-05 13:31:30 +0100722 total_skipped++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 } else {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100724 total_errors++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 mbedtls_fprintf(stdout, "FAILED\n");
726 mbedtls_fprintf(stdout, " %s\n at ",
727 mbedtls_test_info.test);
728 if (mbedtls_test_info.step != (unsigned long) (-1)) {
729 mbedtls_fprintf(stdout, "step %lu, ",
730 mbedtls_test_info.step);
Gilles Peskine56055912019-03-01 14:26:30 +0100731 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 mbedtls_fprintf(stdout, "line %d, %s",
733 mbedtls_test_info.line_no,
734 mbedtls_test_info.filename);
735 if (mbedtls_test_info.line1[0] != 0) {
736 mbedtls_fprintf(stdout, "\n %s",
737 mbedtls_test_info.line1);
738 }
739 if (mbedtls_test_info.line2[0] != 0) {
740 mbedtls_fprintf(stdout, "\n %s",
741 mbedtls_test_info.line2);
742 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100743 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 fflush(stdout);
745 } else if (ret == DISPATCH_INVALID_TEST_DATA) {
746 mbedtls_fprintf(stderr, "FAILED: FATAL PARSE ERROR\n");
747 fclose(file);
748 mbedtls_exit(2);
749 } else if (ret == DISPATCH_TEST_FN_NOT_FOUND) {
750 mbedtls_fprintf(stderr, "FAILED: FATAL TEST FUNCTION NOT FOUND\n");
751 fclose(file);
752 mbedtls_exit(2);
753 } else {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100754 total_errors++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100756 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 fclose(file);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100758 }
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if (outcome_file != NULL) {
761 fclose(outcome_file);
762 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 mbedtls_fprintf(stdout,
765 "\n----------------------------------------------------------------------------\n\n");
766 if (total_errors == 0) {
767 mbedtls_fprintf(stdout, "PASSED");
768 } else {
769 mbedtls_fprintf(stdout, "FAILED");
770 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 mbedtls_fprintf(stdout, " (%u / %u tests (%u skipped))\n",
773 total_tests - total_errors, total_tests, total_skipped);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100774
Paul Elliottf25d8312023-11-23 18:49:43 +0000775#if defined(MBEDTLS_TEST_MUTEX_USAGE)
776 mbedtls_test_mutex_usage_end();
777#endif
778
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100779#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
780 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
781#if defined(MBEDTLS_MEMORY_DEBUG)
782 mbedtls_memory_buffer_alloc_status();
783#endif
784 mbedtls_memory_buffer_alloc_free();
785#endif
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 return total_errors != 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100788}