blob: 565ded3768a1466b5994c4e86bc6fead2535d33a [file] [log] [blame]
Mohammad Azim Khan95402612017-07-19 10:15:54 +01001#line 2 "suites/host_test.function"
Mohammad Azim Khanfff49042017-03-28 01:48:31 +01002
3/**
Azim Khan8d686bf2018-07-04 23:29:46 +01004 * \brief Verifies that string is in string parameter format i.e. "<str>"
Mohammad Azim Khanfff49042017-03-28 01:48:31 +01005 * It also strips enclosing '"' from the input string.
6 *
7 * \param str String parameter.
8 *
9 * \return 0 if success else 1
10 */
Gilles Peskine449bd832023-01-11 14:50:10 +010011int verify_string(char **str)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010012{
Gilles Peskine449bd832023-01-11 14:50:10 +010013 if ((*str)[0] != '"' ||
14 (*str)[strlen(*str) - 1] != '"') {
15 mbedtls_fprintf(stderr,
16 "Expected string (with \"\") for parameter and got: %s\n", *str);
17 return -1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010018 }
19
Gilles Peskine449bd832023-01-11 14:50:10 +010020 (*str)++;
21 (*str)[strlen(*str) - 1] = '\0';
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010022
Gilles Peskine449bd832023-01-11 14:50:10 +010023 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010024}
25
26/**
Azim Khan8d686bf2018-07-04 23:29:46 +010027 * \brief Verifies that string is an integer. Also gives the converted
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010028 * integer value.
29 *
30 * \param str Input string.
31 * \param value Pointer to int for output value.
32 *
33 * \return 0 if success else 1
34 */
Gilles Peskine5226eb52022-12-04 00:28:56 +010035int verify_int(char *str, int32_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;
39 long value = strtol(str, &end, 0);
40 if (errno == EINVAL || *end != '\0') {
41 mbedtls_fprintf(stderr,
42 "Expected integer for parameter and got: %s\n", str);
43 return KEY_VALUE_MAPPING_NOT_FOUND;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010044 }
Gilles Peskine5226eb52022-12-04 00:28:56 +010045 if (errno == ERANGE
46#if LONG_MAX > 0x7fffffff
47 || value > 0x7fffffffL || value < -0x80000000L
48#endif
49 ) {
50 mbedtls_fprintf(stderr, "Integer out of range: %s\n", str);
51 return KEY_VALUE_MAPPING_NOT_FOUND;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010052 }
Gilles Peskine5226eb52022-12-04 00:28:56 +010053 *p_value = value;
54 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010055}
56
57
58/**
59 * \brief Usage string.
60 *
61 */
62#define USAGE \
63 "Usage: %s [OPTIONS] files...\n\n" \
64 " Command line arguments:\n" \
Mohammad Azim Khand2d01122018-07-18 17:48:37 +010065 " files... One or more test data files. If no file is\n" \
66 " specified the following default test case\n" \
67 " file is used:\n" \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010068 " %s\n\n" \
69 " Options:\n" \
70 " -v | --verbose Display full information about each test\n" \
71 " -h | --help Display this information\n\n", \
72 argv[0], \
73 "TESTCASE_FILENAME"
74
75
76/**
77 * \brief Read a line from the passed file pointer.
78 *
79 * \param f FILE pointer
80 * \param buf Pointer to memory to hold read line.
81 * \param len Length of the buf.
82 *
83 * \return 0 if success else -1
84 */
Gilles Peskine449bd832023-01-11 14:50:10 +010085int get_line(FILE *f, char *buf, size_t len)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010086{
87 char *ret;
88 int i = 0, str_len = 0, has_string = 0;
89
90 /* Read until we get a valid line */
Gilles Peskine449bd832023-01-11 14:50:10 +010091 do {
92 ret = fgets(buf, len, f);
93 if (ret == NULL) {
94 return -1;
95 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010096
Gilles Peskine449bd832023-01-11 14:50:10 +010097 str_len = strlen(buf);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010098
99 /* Skip empty line and comment */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 if (str_len == 0 || buf[0] == '#') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100101 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100103 has_string = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 for (i = 0; i < str_len; i++) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100105 char c = buf[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (c != ' ' && c != '\t' && c != '\n' &&
107 c != '\v' && c != '\f' && c != '\r') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100108 has_string = 1;
109 break;
110 }
111 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 } while (!has_string);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100113
114 /* Strip new line and carriage return */
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 ret = buf + strlen(buf);
116 if (ret-- > buf && *ret == '\n') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100117 *ret = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 }
119 if (ret-- > buf && *ret == '\r') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100120 *ret = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100124}
125
126/**
127 * \brief Splits string delimited by ':'. Ignores '\:'.
128 *
129 * \param buf Input string
130 * \param len Input string length
131 * \param params Out params found
132 * \param params_len Out params array len
133 *
134 * \return Count of strings found.
135 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136static int parse_arguments(char *buf, size_t len, char **params,
137 size_t params_len)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100138{
139 size_t cnt = 0, i;
140 char *cur = buf;
141 char *p = buf, *q;
142
143 params[cnt++] = cur;
144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 while (*p != '\0' && p < (buf + len)) {
146 if (*p == '\\') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100147 p++;
148 p++;
149 continue;
150 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (*p == ':') {
152 if (p + 1 < buf + len) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100153 cur = p + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 TEST_HELPER_ASSERT(cnt < params_len);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100155 params[cnt++] = cur;
156 }
157 *p = '\0';
158 }
159
160 p++;
161 }
162
Gilles Peskine1a248952022-12-03 23:48:25 +0100163 /* Replace backslash escapes in strings */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 for (i = 0; i < cnt; i++) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100165 p = params[i];
166 q = params[i];
167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 while (*p != '\0') {
Gilles Peskine1a248952022-12-03 23:48:25 +0100169 if (*p == '\\') {
170 ++p;
171 switch (*p) {
172 case 'n':
173 *p = '\n';
174 break;
175 default:
176 // Fall through to copying *p
177 break;
178 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100179 }
Gilles Peskine1a248952022-12-03 23:48:25 +0100180 *(q++) = *(p++);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100181 }
182 *q = '\0';
183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return cnt;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100186}
187
188/**
189 * \brief Converts parameters into test function consumable parameters.
190 * Example: Input: {"int", "0", "char*", "Hello",
191 * "hex", "abef", "exp", "1"}
192 * Output: {
193 * 0, // Verified int
194 * "Hello", // Verified string
195 * 2, { 0xab, 0xef },// Converted len,hex pair
196 * 9600 // Evaluated expression
197 * }
198 *
199 *
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100200 * \param cnt Parameter array count.
201 * \param params Out array of found parameters.
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100202 * \param int_params_store Memory for storing processed integer parameters.
203 *
204 * \return 0 for success else 1
205 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100206static int convert_params(size_t cnt, char **params, int32_t *int_params_store)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100207{
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 char **cur = params;
209 char **out = params;
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100210 int ret = DISPATCH_TEST_SUCCESS;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 while (cur < params + cnt) {
213 char *type = *cur++;
214 char *val = *cur++;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 if (strcmp(type, "char*") == 0) {
217 if (verify_string(&val) == 0) {
218 *out++ = val;
219 } else {
220 ret = (DISPATCH_INVALID_TEST_DATA);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100221 break;
222 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 } else if (strcmp(type, "int") == 0) {
224 if (verify_int(val, int_params_store) == 0) {
225 *out++ = (char *) int_params_store++;
226 } else {
227 ret = (DISPATCH_INVALID_TEST_DATA);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100228 break;
229 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 } else if (strcmp(type, "hex") == 0) {
231 if (verify_string(&val) == 0) {
Ronald Crona0c25392020-06-18 10:10:46 +0200232 size_t len;
233
234 TEST_HELPER_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 mbedtls_test_unhexify((unsigned char *) val, strlen(val),
236 val, &len) == 0);
Ronald Crona0c25392020-06-18 10:10:46 +0200237
238 *int_params_store = len;
Azim Khan184447e2017-05-31 20:29:36 +0100239 *out++ = val;
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 *out++ = (char *) (int_params_store++);
241 } else {
242 ret = (DISPATCH_INVALID_TEST_DATA);
Azim Khan184447e2017-05-31 20:29:36 +0100243 break;
244 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 } else if (strcmp(type, "exp") == 0) {
246 int exp_id = strtol(val, NULL, 10);
247 if (get_expression(exp_id, int_params_store) == 0) {
248 *out++ = (char *) int_params_store++;
249 } else {
250 ret = (DISPATCH_INVALID_TEST_DATA);
251 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100252 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 } else {
254 ret = (DISPATCH_INVALID_TEST_DATA);
255 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100256 }
257 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return ret;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100259}
260
261/**
262 * \brief Tests snprintf implementation with test input.
263 *
Azim Khan191e9042017-06-09 12:39:00 +0100264 * \note
265 * At high optimization levels (e.g. gcc -O3), this function may be
266 * inlined in run_test_snprintf. This can trigger a spurious warning about
267 * potential misuse of snprintf from gcc -Wformat-truncation (observed with
268 * gcc 7.2). This warning makes tests in run_test_snprintf redundant on gcc
269 * only. They are still valid for other compilers. Avoid this warning by
270 * forbidding inlining of this function by gcc.
271 *
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100272 * \param n Buffer test length.
273 * \param ref_buf Expected buffer.
274 * \param ref_ret Expected snprintf return value.
275 *
276 * \return 0 for success else 1
277 */
Azim Khan191e9042017-06-09 12:39:00 +0100278#if defined(__GNUC__)
279__attribute__((__noinline__))
280#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100281static int test_snprintf(size_t n, const char *ref_buf, int ref_ret)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100282{
283 int ret;
284 char buf[10] = "xxxxxxxxx";
285 const char ref[10] = "xxxxxxxxx";
286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if (n >= sizeof(buf)) {
288 return -1;
289 }
290 ret = mbedtls_snprintf(buf, n, "%s", "123");
291 if (ret < 0 || (size_t) ret >= n) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100292 ret = -1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100293 }
294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (strncmp(ref_buf, buf, sizeof(buf)) != 0 ||
296 ref_ret != ret ||
297 memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) {
298 return 1;
299 }
300
301 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100302}
303
304/**
305 * \brief Tests snprintf implementation.
306 *
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100307 * \return 0 for success else 1
308 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309static int run_test_snprintf(void)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100310{
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return test_snprintf(0, "xxxxxxxxx", -1) != 0 ||
312 test_snprintf(1, "", -1) != 0 ||
313 test_snprintf(2, "1", -1) != 0 ||
314 test_snprintf(3, "12", -1) != 0 ||
315 test_snprintf(4, "123", 3) != 0 ||
316 test_snprintf(5, "123", 3) != 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100317}
318
Gilles Peskine51dcc242019-09-16 15:13:48 +0200319/** \brief Write the description of the test case to the outcome CSV file.
320 *
321 * \param outcome_file The file to write to.
322 * If this is \c NULL, this function does nothing.
323 * \param argv0 The test suite name.
324 * \param test_case The test case description.
325 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326static void write_outcome_entry(FILE *outcome_file,
327 const char *argv0,
328 const char *test_case)
Gilles Peskine51dcc242019-09-16 15:13:48 +0200329{
330 /* The non-varying fields are initialized on first use. */
331 static const char *platform = NULL;
332 static const char *configuration = NULL;
333 static const char *test_suite = NULL;
334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if (outcome_file == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200336 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (platform == NULL) {
340 platform = getenv("MBEDTLS_TEST_PLATFORM");
341 if (platform == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200342 platform = "unknown";
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200344 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (configuration == NULL) {
346 configuration = getenv("MBEDTLS_TEST_CONFIGURATION");
347 if (configuration == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200348 configuration = "unknown";
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200350 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (test_suite == NULL) {
352 test_suite = strrchr(argv0, '/');
353 if (test_suite != NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200354 test_suite += 1; // skip the '/'
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 } else {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200356 test_suite = argv0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200358 }
359
360 /* Write the beginning of the outcome line.
361 * Ignore errors: writing the outcome file is on a best-effort basis. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 mbedtls_fprintf(outcome_file, "%s;%s;%s;%s;",
363 platform, configuration, test_suite, test_case);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200364}
365
366/** \brief Write the result of the test case to the outcome CSV file.
367 *
368 * \param outcome_file The file to write to.
369 * If this is \c NULL, this function does nothing.
Ronald Cron67a8a372020-04-01 16:04:41 +0200370 * \param unmet_dep_count The number of unmet dependencies.
371 * \param unmet_dependencies The array of unmet dependencies.
372 * \param missing_unmet_dependencies Non-zero if there was a problem tracking
373 * all unmet dependencies, 0 otherwise.
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100374 * \param ret The test dispatch status (DISPATCH_xxx).
375 * \param info A pointer to the test info structure.
Gilles Peskine51dcc242019-09-16 15:13:48 +0200376 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100377static void write_outcome_result(FILE *outcome_file,
378 size_t unmet_dep_count,
379 int unmet_dependencies[],
380 int missing_unmet_dependencies,
381 int ret,
382 const mbedtls_test_info_t *info)
Gilles Peskine51dcc242019-09-16 15:13:48 +0200383{
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (outcome_file == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200385 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200387
388 /* Write the end of the outcome line.
389 * Ignore errors: writing the outcome file is on a best-effort basis. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 switch (ret) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200391 case DISPATCH_TEST_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (unmet_dep_count > 0) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200393 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 mbedtls_fprintf(outcome_file, "SKIP");
395 for (i = 0; i < unmet_dep_count; i++) {
396 mbedtls_fprintf(outcome_file, "%c%d",
397 i == 0 ? ';' : ':',
398 unmet_dependencies[i]);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200399 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (missing_unmet_dependencies) {
401 mbedtls_fprintf(outcome_file, ":...");
402 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200403 break;
404 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 switch (info->result) {
Chris Jonese60e2ae2021-01-20 17:51:47 +0000406 case MBEDTLS_TEST_RESULT_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 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:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 mbedtls_fprintf(outcome_file, "SKIP;Runtime skip");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200411 break;
412 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 mbedtls_fprintf(outcome_file, "FAIL;%s:%d:%s",
414 info->filename, info->line_no,
415 info->test);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200416 break;
417 }
418 break;
419 case DISPATCH_TEST_FN_NOT_FOUND:
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 mbedtls_fprintf(outcome_file, "FAIL;Test function not found");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200421 break;
422 case DISPATCH_INVALID_TEST_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 mbedtls_fprintf(outcome_file, "FAIL;Invalid test data");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200424 break;
425 case DISPATCH_UNSUPPORTED_SUITE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 mbedtls_fprintf(outcome_file, "SKIP;Unsupported suite");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200427 break;
428 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 mbedtls_fprintf(outcome_file, "FAIL;Unknown cause");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200430 break;
431 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 mbedtls_fprintf(outcome_file, "\n");
433 fflush(outcome_file);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200434}
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100435
436/**
437 * \brief Desktop implementation of execute_tests().
438 * Parses command line and executes tests from
439 * supplied or default data file.
440 *
441 * \param argc Command line argument count.
442 * \param argv Argument array.
443 *
444 * \return Program exit status.
445 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100446int execute_tests(int argc, const char **argv)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100447{
448 /* Local Configurations and options */
449 const char *default_filename = "DATA_FILE";
450 const char *test_filename = NULL;
451 const char **test_files = NULL;
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200452 size_t testfile_count = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100453 int option_verbose = 0;
k-stachowiak03954f22019-09-16 10:23:10 +0200454 size_t function_id = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100455
456 /* Other Local variables */
457 int arg_index = 1;
458 const char *next_arg;
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200459 size_t testfile_index, i, cnt;
460 int ret;
461 unsigned total_errors = 0, total_tests = 0, total_skipped = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100462 FILE *file;
463 char buf[5000];
464 char *params[50];
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800465 /* Store for processed integer params. */
Gilles Peskinea7a43062021-05-18 16:39:33 +0200466 int32_t int_params[50];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100467 void *pointer;
468#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
469 int stdout_fd = -1;
470#endif /* __unix__ || __APPLE__ __MACH__ */
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 const char *outcome_file_name = getenv("MBEDTLS_TEST_OUTCOME_FILE");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200472 FILE *outcome_file = NULL;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100473
474#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
475 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
476 unsigned char alloc_buf[1000000];
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100478#endif
479
Gilles Peskine1061ec62021-01-29 21:17:11 +0100480#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 mbedtls_test_mutex_usage_init();
Gilles Peskine1061ec62021-01-29 21:17:11 +0100482#endif
483
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100484 /*
485 * The C standard doesn't guarantee that all-bits-0 is the representation
486 * of a NULL pointer. We do however use that in our code for initializing
487 * structures, which should work on every modern platform. Let's be sure.
488 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 memset(&pointer, 0, sizeof(void *));
490 if (pointer != NULL) {
491 mbedtls_fprintf(stderr, "all-bits-zero is not a NULL pointer\n");
492 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100493 }
494
495 /*
496 * Make sure we have a snprintf that correctly zero-terminates
497 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (run_test_snprintf() != 0) {
499 mbedtls_fprintf(stderr, "the snprintf implementation is broken\n");
500 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100501 }
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (outcome_file_name != NULL && *outcome_file_name != '\0') {
504 outcome_file = fopen(outcome_file_name, "a");
505 if (outcome_file == NULL) {
506 mbedtls_fprintf(stderr, "Unable to open outcome file. Continuing anyway.\n");
Gilles Peskine9c673232020-01-21 18:03:56 +0100507 }
508 }
509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if (strcmp(next_arg, "--verbose") == 0 ||
514 strcmp(next_arg, "-v") == 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100515 option_verbose = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 } else if (strcmp(next_arg, "--help") == 0 ||
517 strcmp(next_arg, "-h") == 0) {
518 mbedtls_fprintf(stdout, USAGE);
519 mbedtls_exit(EXIT_SUCCESS);
520 } else {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100521 /* Not an option, therefore treat all further arguments as the file
522 * list.
523 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 test_files = &argv[arg_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100525 testfile_count = argc - arg_index;
Tuvshinzaya Erdenekhuu8988e232022-06-17 10:19:56 +0100526 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100527 }
528
529 arg_index++;
530 }
531
532 /* If no files were specified, assume a default */
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (test_files == NULL || testfile_count == 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100534 test_files = &default_filename;
535 testfile_count = 1;
536 }
537
538 /* Initialize the struct that holds information about the last test */
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 mbedtls_test_info_reset();
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100540
541 /* Now begin to execute the tests in the testfiles */
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 for (testfile_index = 0;
543 testfile_index < testfile_count;
544 testfile_index++) {
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200545 size_t unmet_dep_count = 0;
Gilles Peskine06725c92020-03-30 21:39:09 +0200546 int unmet_dependencies[20];
Ronald Cron67a8a372020-04-01 16:04:41 +0200547 int missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 test_filename = test_files[testfile_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 file = fopen(test_filename, "r");
552 if (file == NULL) {
553 mbedtls_fprintf(stderr, "Failed to open test file: %s\n",
554 test_filename);
555 if (outcome_file != NULL) {
556 fclose(outcome_file);
557 }
558 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100559 }
560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 while (!feof(file)) {
562 if (unmet_dep_count > 0) {
563 mbedtls_fprintf(stderr,
564 "FATAL: Dep count larger than zero at start of loop\n");
565 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100566 }
567 unmet_dep_count = 0;
Ronald Cron67a8a372020-04-01 16:04:41 +0200568 missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if ((ret = get_line(file, buf, sizeof(buf))) != 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100571 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 }
573 mbedtls_fprintf(stdout, "%s%.66s",
574 mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED ?
575 "\n" : "", buf);
576 mbedtls_fprintf(stdout, " ");
577 for (i = strlen(buf) + 1; i < 67; i++) {
578 mbedtls_fprintf(stdout, ".");
579 }
580 mbedtls_fprintf(stdout, " ");
581 fflush(stdout);
582 write_outcome_entry(outcome_file, argv[0], buf);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100583
584 total_tests++;
585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if ((ret = get_line(file, buf, sizeof(buf))) != 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100587 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 }
589 cnt = parse_arguments(buf, strlen(buf), params,
590 sizeof(params) / sizeof(params[0]));
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 if (strcmp(params[0], "depends_on") == 0) {
593 for (i = 1; i < cnt; i++) {
594 int dep_id = strtol(params[i], NULL, 10);
595 if (dep_check(dep_id) != DEPENDENCY_SUPPORTED) {
596 if (unmet_dep_count <
597 ARRAY_LENGTH(unmet_dependencies)) {
Ronald Crone1a05a52020-04-01 15:52:06 +0200598 unmet_dependencies[unmet_dep_count] = dep_id;
599 unmet_dep_count++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 } else {
Ronald Cron67a8a372020-04-01 16:04:41 +0200601 missing_unmet_dependencies = 1;
602 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100603 }
604 }
605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if ((ret = get_line(file, buf, sizeof(buf))) != 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100607 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 }
609 cnt = parse_arguments(buf, strlen(buf), params,
610 sizeof(params) / sizeof(params[0]));
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100611 }
612
613 // If there are no unmet dependencies execute the test
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (unmet_dep_count == 0) {
615 mbedtls_test_info_reset();
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100616
617#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
618 /* Suppress all output from the library unless we're verbose
619 * mode
620 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 if (!option_verbose) {
622 stdout_fd = redirect_output(stdout, "/dev/null");
623 if (stdout_fd == -1) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100624 /* Redirection has failed with no stdout so exit */
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 exit(1);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100626 }
627 }
628#endif /* __unix__ || __APPLE__ __MACH__ */
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 function_id = strtoul(params[0], NULL, 10);
631 if ((ret = check_test(function_id)) == DISPATCH_TEST_SUCCESS) {
632 ret = convert_params(cnt - 1, params + 1, int_params);
633 if (DISPATCH_TEST_SUCCESS == ret) {
634 ret = dispatch_test(function_id, (void **) (params + 1));
Azim Khan13c6bfb2017-06-15 14:45:56 +0100635 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100636 }
637
638#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (!option_verbose && restore_output(stdout, stdout_fd)) {
640 /* Redirection has failed with no stdout so exit */
641 exit(1);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100642 }
643#endif /* __unix__ || __APPLE__ __MACH__ */
644
645 }
646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 write_outcome_result(outcome_file,
648 unmet_dep_count, unmet_dependencies,
649 missing_unmet_dependencies,
650 ret, &mbedtls_test_info);
651 if (unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100652 total_skipped++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 mbedtls_fprintf(stdout, "----");
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE) {
656 mbedtls_fprintf(stdout, "\n Test Suite not enabled");
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100657 }
658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (1 == option_verbose && unmet_dep_count > 0) {
660 mbedtls_fprintf(stdout, "\n Unmet dependencies: ");
661 for (i = 0; i < unmet_dep_count; i++) {
662 mbedtls_fprintf(stdout, "%d ",
663 unmet_dependencies[i]);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100664 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (missing_unmet_dependencies) {
666 mbedtls_fprintf(stdout, "...");
667 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100668 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 mbedtls_fprintf(stdout, "\n");
670 fflush(stdout);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100671
672 unmet_dep_count = 0;
Ronald Cron67a8a372020-04-01 16:04:41 +0200673 missing_unmet_dependencies = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 } else if (ret == DISPATCH_TEST_SUCCESS) {
675 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS) {
676 mbedtls_fprintf(stdout, "PASS\n");
677 } else if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SKIPPED) {
678 mbedtls_fprintf(stdout, "----\n");
Hanno Beckere69d0152019-07-05 13:31:30 +0100679 total_skipped++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 } else {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100681 total_errors++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 mbedtls_fprintf(stdout, "FAILED\n");
683 mbedtls_fprintf(stdout, " %s\n at ",
684 mbedtls_test_info.test);
685 if (mbedtls_test_info.step != (unsigned long) (-1)) {
686 mbedtls_fprintf(stdout, "step %lu, ",
687 mbedtls_test_info.step);
Gilles Peskine56055912019-03-01 14:26:30 +0100688 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 mbedtls_fprintf(stdout, "line %d, %s",
690 mbedtls_test_info.line_no,
691 mbedtls_test_info.filename);
692 if (mbedtls_test_info.line1[0] != 0) {
693 mbedtls_fprintf(stdout, "\n %s",
694 mbedtls_test_info.line1);
695 }
696 if (mbedtls_test_info.line2[0] != 0) {
697 mbedtls_fprintf(stdout, "\n %s",
698 mbedtls_test_info.line2);
699 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100700 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 fflush(stdout);
702 } else if (ret == DISPATCH_INVALID_TEST_DATA) {
703 mbedtls_fprintf(stderr, "FAILED: FATAL PARSE ERROR\n");
704 fclose(file);
705 mbedtls_exit(2);
706 } else if (ret == DISPATCH_TEST_FN_NOT_FOUND) {
707 mbedtls_fprintf(stderr, "FAILED: FATAL TEST FUNCTION NOT FOUND\n");
708 fclose(file);
709 mbedtls_exit(2);
710 } else {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100711 total_errors++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100713 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 fclose(file);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100715 }
716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (outcome_file != NULL) {
718 fclose(outcome_file);
719 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 mbedtls_fprintf(stdout,
722 "\n----------------------------------------------------------------------------\n\n");
723 if (total_errors == 0) {
724 mbedtls_fprintf(stdout, "PASSED");
725 } else {
726 mbedtls_fprintf(stdout, "FAILED");
727 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 mbedtls_fprintf(stdout, " (%u / %u tests (%u skipped))\n",
730 total_tests - total_errors, total_tests, total_skipped);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100731
732#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
733 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
734#if defined(MBEDTLS_MEMORY_DEBUG)
735 mbedtls_memory_buffer_alloc_status();
736#endif
737 mbedtls_memory_buffer_alloc_free();
738#endif
739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 return total_errors != 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100741}