Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2016 Intel Corporation
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : :
7 : : #include <stdlib.h>
8 : : #include <zephyr/ztest.h>
9 : :
10 : : #include <zephyr/app_memory/app_memdomain.h>
11 : : #ifdef CONFIG_USERSPACE
12 : : #include <zephyr/sys/libc-hooks.h>
13 : : #endif
14 : : #include <zephyr/logging/log_ctrl.h>
15 : : #include <zephyr/sys/reboot.h>
16 : :
17 : : #include <zephyr/llext/symbol.h>
18 : :
19 : : #include <zephyr/sys/barrier.h>
20 : :
21 : : #ifdef KERNEL
22 : : static struct k_thread ztest_thread;
23 : : #endif
24 : : static bool failed_expectation;
25 : :
26 : : #ifdef CONFIG_ZTEST_SHELL
27 : : #include <zephyr/shell/shell.h>
28 : : #endif
29 : :
30 : : #ifdef CONFIG_ZTEST_SHUFFLE
31 : : #include <time.h>
32 : : #include <zephyr/random/random.h>
33 : : #ifndef CONFIG_ZTEST_REPEAT
34 : : #define NUM_ITER_PER_SUITE CONFIG_ZTEST_SHUFFLE_SUITE_REPEAT_COUNT
35 : : #define NUM_ITER_PER_TEST CONFIG_ZTEST_SHUFFLE_TEST_REPEAT_COUNT
36 : : #endif
37 : : #endif /* CONFIG_ZTEST_SHUFFLE */
38 : :
39 : : #ifdef CONFIG_ZTEST_REPEAT
40 : : #define NUM_ITER_PER_SUITE CONFIG_ZTEST_SUITE_REPEAT_COUNT
41 : : #define NUM_ITER_PER_TEST CONFIG_ZTEST_TEST_REPEAT_COUNT
42 : : #else
43 : : #ifndef CONFIG_ZTEST_SHUFFLE
44 : : #define NUM_ITER_PER_SUITE 1
45 : : #define NUM_ITER_PER_TEST 1
46 : : #endif
47 : : #endif
48 : :
49 : : #ifdef CONFIG_ZTEST_COVERAGE_RESET_BEFORE_TESTS
50 : : #include <coverage.h>
51 : : #endif
52 : :
53 : : /* ZTEST_DMEM and ZTEST_BMEM are used for the application shared memory test */
54 : :
55 : : /**
56 : : * @brief The current status of the test binary
57 : : */
58 : : enum ztest_status {
59 : : ZTEST_STATUS_OK,
60 : : ZTEST_STATUS_HAS_FAILURE,
61 : : ZTEST_STATUS_CRITICAL_ERROR
62 : : };
63 : :
64 : : /**
65 : : * @brief Tracks the current phase that ztest is operating in.
66 : : */
67 : : ZTEST_DMEM enum ztest_phase cur_phase = TEST_PHASE_FRAMEWORK;
68 : :
69 : : static ZTEST_BMEM enum ztest_status test_status = ZTEST_STATUS_OK;
70 : :
71 : : extern ZTEST_DMEM const struct ztest_arch_api ztest_api;
72 : :
73 : : static void __ztest_show_suite_summary(void);
74 : :
75 : 1 : static void end_report(void)
76 : : {
77 : 1 : __ztest_show_suite_summary();
78 [ - + ]: 1 : if (test_status) {
79 : 0 : TC_END_REPORT(TC_FAIL);
80 : : } else {
81 : 1 : TC_END_REPORT(TC_PASS);
82 : : }
83 : : }
84 : :
85 : 42 : static int cleanup_test(struct ztest_unit_test *test)
86 : : {
87 : 42 : int ret = TC_PASS;
88 : 42 : int mock_status;
89 : :
90 : 42 : mock_status = z_cleanup_mock();
91 : :
92 : : #ifdef KERNEL
93 : : /* we need to remove the ztest_thread information from the timeout_q.
94 : : * Because we reuse the same k_thread structure this would
95 : : * causes some problems.
96 : : */
97 : 42 : if (IS_ENABLED(CONFIG_MULTITHREADING)) {
98 : 42 : k_thread_abort(&ztest_thread);
99 : : }
100 : : #endif
101 : :
102 : 42 : if (!ret && mock_status == 1) {
103 : : PRINT_DATA("Test %s failed: Unused mock parameter values\n", test->name);
104 : : ret = TC_FAIL;
105 : 42 : } else if (!ret && mock_status == 2) {
106 : : PRINT_DATA("Test %s failed: Unused mock return values\n", test->name);
107 : : ret = TC_FAIL;
108 : : } else {
109 : 42 : ;
110 : : }
111 : :
112 : 42 : return ret;
113 : : }
114 : :
115 : : #ifdef KERNEL
116 : :
117 : : #if defined(CONFIG_SMP) && (CONFIG_MP_MAX_NUM_CPUS > 1)
118 : : #define MAX_NUM_CPUHOLD (CONFIG_MP_MAX_NUM_CPUS - 1)
119 : : #define CPUHOLD_STACK_SZ (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
120 : :
121 : : struct cpuhold_pool_item {
122 : : struct k_thread thread;
123 : : bool used;
124 : : };
125 : :
126 : : static struct cpuhold_pool_item cpuhold_pool_items[MAX_NUM_CPUHOLD + 1];
127 : :
128 : : K_KERNEL_STACK_ARRAY_DEFINE(cpuhold_stacks, MAX_NUM_CPUHOLD + 1, CPUHOLD_STACK_SZ);
129 : :
130 : : static struct k_sem cpuhold_sem;
131 : :
132 : : volatile int cpuhold_active;
133 : : volatile bool cpuhold_spawned;
134 : :
135 : : static int find_unused_thread(void)
136 : : {
137 : : for (unsigned int i = 0; i <= MAX_NUM_CPUHOLD; i++) {
138 : : if (!cpuhold_pool_items[i].used) {
139 : : return i;
140 : : }
141 : : }
142 : :
143 : : return -1;
144 : : }
145 : :
146 : : static void mark_thread_unused(struct k_thread *thread)
147 : : {
148 : : for (unsigned int i = 0; i <= MAX_NUM_CPUHOLD; i++) {
149 : : if (&cpuhold_pool_items[i].thread == thread) {
150 : : cpuhold_pool_items[i].used = false;
151 : : }
152 : : }
153 : : }
154 : :
155 : : static inline void wait_for_thread_to_switch_out(struct k_thread *thread)
156 : : {
157 : : unsigned int key = arch_irq_lock();
158 : : volatile void **shp = (void *)&thread->switch_handle;
159 : :
160 : : while (*shp == NULL) {
161 : : arch_spin_relax();
162 : : }
163 : : /* Read barrier: don't allow any subsequent loads in the
164 : : * calling code to reorder before we saw switch_handle go
165 : : * non-null.
166 : : */
167 : : barrier_dmem_fence_full();
168 : :
169 : : arch_irq_unlock(key);
170 : : }
171 : :
172 : : /* "Holds" a CPU for use with the "1cpu" test cases. Note that we
173 : : * can't use tools like the cpumask feature because we have tests that
174 : : * may need to control that configuration themselves. We do this at
175 : : * the lowest level, but locking interrupts directly and spinning.
176 : : */
177 : : static void cpu_hold(void *arg1, void *arg2, void *arg3)
178 : : {
179 : : struct k_thread *thread = arg1;
180 : : unsigned int idx = (unsigned int)(uintptr_t)arg2;
181 : : char tname[CONFIG_THREAD_MAX_NAME_LEN];
182 : :
183 : : ARG_UNUSED(arg3);
184 : :
185 : : if (arch_proc_id() == 0) {
186 : : int i;
187 : :
188 : : i = find_unused_thread();
189 : :
190 : : __ASSERT_NO_MSG(i != -1);
191 : :
192 : : cpuhold_spawned = false;
193 : :
194 : : cpuhold_pool_items[i].used = true;
195 : : k_thread_create(&cpuhold_pool_items[i].thread, cpuhold_stacks[i], CPUHOLD_STACK_SZ,
196 : : cpu_hold, k_current_get(), (void *)(uintptr_t)idx, NULL,
197 : : K_HIGHEST_THREAD_PRIO, 0, K_NO_WAIT);
198 : :
199 : : /*
200 : : * Busy-wait until we know the spawned thread is running to
201 : : * ensure it does not spawn on CPU0.
202 : : */
203 : :
204 : : while (!cpuhold_spawned) {
205 : : k_busy_wait(1000);
206 : : }
207 : :
208 : : return;
209 : : }
210 : :
211 : : if (thread != NULL) {
212 : : cpuhold_spawned = true;
213 : :
214 : : /* Busywait until a new thread is scheduled in on CPU0 */
215 : :
216 : : wait_for_thread_to_switch_out(thread);
217 : :
218 : : mark_thread_unused(thread);
219 : : }
220 : :
221 : : if (IS_ENABLED(CONFIG_THREAD_NAME)) {
222 : : snprintk(tname, CONFIG_THREAD_MAX_NAME_LEN, "cpuhold%02d", idx);
223 : : k_thread_name_set(k_current_get(), tname);
224 : : }
225 : :
226 : : uint32_t dt, start_ms = k_uptime_get_32();
227 : : unsigned int key = arch_irq_lock();
228 : :
229 : : k_sem_give(&cpuhold_sem);
230 : :
231 : : #if (defined(CONFIG_ARM64) || defined(CONFIG_RISCV)) && defined(CONFIG_FPU_SHARING)
232 : : /*
233 : : * We'll be spinning with IRQs disabled. The flush-your-FPU request
234 : : * IPI will never be serviced during that time. Therefore we flush
235 : : * the FPU preemptively here to prevent any other CPU waiting after
236 : : * this CPU forever and deadlock the system.
237 : : */
238 : : k_float_disable(_current_cpu->arch.fpu_owner);
239 : : #endif
240 : :
241 : : while (cpuhold_active) {
242 : : k_busy_wait(1000);
243 : : }
244 : :
245 : : /* Holding the CPU via spinning is expensive, and abusing this
246 : : * for long-running test cases tends to overload the CI system
247 : : * (qemu runs separate CPUs in different threads, but the CI
248 : : * logic views it as one "job") and cause other test failures.
249 : : */
250 : : dt = k_uptime_get_32() - start_ms;
251 : : zassert_true(dt < CONFIG_ZTEST_CPU_HOLD_TIME_MS, "1cpu test took too long (%d ms)", dt);
252 : : arch_irq_unlock(key);
253 : : }
254 : : #endif /* CONFIG_SMP && (CONFIG_MP_MAX_NUM_CPUS > 1) */
255 : :
256 : 0 : void z_impl_z_test_1cpu_start(void)
257 : : {
258 : : #if defined(CONFIG_SMP) && (CONFIG_MP_MAX_NUM_CPUS > 1)
259 : : unsigned int num_cpus = arch_num_cpus();
260 : : int j;
261 : :
262 : : cpuhold_active = 1;
263 : :
264 : : k_sem_init(&cpuhold_sem, 0, 999);
265 : :
266 : : /* Spawn N-1 threads to "hold" the other CPUs, waiting for
267 : : * each to signal us that it's locked and spinning.
268 : : */
269 : : for (int i = 0; i < num_cpus - 1; i++) {
270 : : j = find_unused_thread();
271 : :
272 : : __ASSERT_NO_MSG(j != -1);
273 : :
274 : : cpuhold_pool_items[j].used = true;
275 : : k_thread_create(&cpuhold_pool_items[j].thread, cpuhold_stacks[j], CPUHOLD_STACK_SZ,
276 : : cpu_hold, NULL, (void *)(uintptr_t)i, NULL, K_HIGHEST_THREAD_PRIO,
277 : : 0, K_NO_WAIT);
278 : : k_sem_take(&cpuhold_sem, K_FOREVER);
279 : : }
280 : : #endif
281 : 0 : }
282 : :
283 : 0 : void z_impl_z_test_1cpu_stop(void)
284 : : {
285 : : #if defined(CONFIG_SMP) && (CONFIG_MP_MAX_NUM_CPUS > 1)
286 : : cpuhold_active = 0;
287 : :
288 : : for (int i = 0; i <= MAX_NUM_CPUHOLD; i++) {
289 : : if (cpuhold_pool_items[i].used) {
290 : : k_thread_abort(&cpuhold_pool_items[i].thread);
291 : : cpuhold_pool_items[i].used = false;
292 : : }
293 : : }
294 : : #endif
295 : 0 : }
296 : :
297 : : #ifdef CONFIG_USERSPACE
298 : : void z_vrfy_z_test_1cpu_start(void)
299 : : {
300 : : z_impl_z_test_1cpu_start();
301 : : }
302 : : #include <zephyr/syscalls/z_test_1cpu_start_mrsh.c>
303 : :
304 : : void z_vrfy_z_test_1cpu_stop(void)
305 : : {
306 : : z_impl_z_test_1cpu_stop();
307 : : }
308 : : #include <zephyr/syscalls/z_test_1cpu_stop_mrsh.c>
309 : : #endif /* CONFIG_USERSPACE */
310 : : #endif
311 : :
312 : 84 : __maybe_unused static void run_test_rules(bool is_before, struct ztest_unit_test *test, void *data)
313 : : {
314 : 84 : for (struct ztest_test_rule *rule = _ztest_test_rule_list_start;
315 [ + + ]: 168 : rule < _ztest_test_rule_list_end; ++rule) {
316 [ + + - + ]: 84 : if (is_before && rule->before_each) {
317 : 0 : rule->before_each(test, data);
318 [ + - ]: 42 : } else if (!is_before && rule->after_each) {
319 : 42 : rule->after_each(test, data);
320 : : }
321 : : }
322 : 84 : }
323 : :
324 : 42 : static void run_test_functions(struct ztest_suite_node *suite, struct ztest_unit_test *test,
325 : : void *data)
326 : : {
327 : 42 : __ztest_set_test_phase(TEST_PHASE_TEST);
328 : 42 : test->test(data);
329 : 42 : }
330 : :
331 : : COND_CODE_1(KERNEL, (ZTEST_BMEM), ()) static enum ztest_result test_result;
332 : :
333 : 42 : static int get_final_test_result(const struct ztest_unit_test *test, int ret)
334 : : {
335 : 42 : enum ztest_expected_result expected_result = -1;
336 : :
337 : 42 : for (struct ztest_expected_result_entry *expectation =
338 : : _ztest_expected_result_entry_list_start;
339 [ - + ]: 42 : expectation < _ztest_expected_result_entry_list_end; ++expectation) {
340 [ # # ]: 0 : if (strcmp(expectation->test_name, test->name) == 0 &&
341 [ # # ]: 0 : strcmp(expectation->test_suite_name, test->test_suite_name) == 0) {
342 : 0 : expected_result = expectation->expected_result;
343 : 0 : break;
344 : : }
345 : : }
346 : :
347 [ - + ]: 42 : if (expected_result == ZTEST_EXPECTED_RESULT_FAIL) {
348 : : /* Expected a failure:
349 : : * - If we got a failure, return TC_PASS
350 : : * - Otherwise force a failure
351 : : */
352 : 0 : return (ret == TC_FAIL) ? TC_PASS : TC_FAIL;
353 : : }
354 [ - + ]: 42 : if (expected_result == ZTEST_EXPECTED_RESULT_SKIP) {
355 : : /* Expected a skip:
356 : : * - If we got a skip, return TC_PASS
357 : : * - Otherwise force a failure
358 : : */
359 : 0 : return (ret == TC_SKIP) ? TC_PASS : TC_FAIL;
360 : : }
361 : : /* No expectation was made, no change is needed. */
362 : : return ret;
363 : : }
364 : :
365 : : /**
366 : : * @brief Get a friendly name string for a given test phrase.
367 : : *
368 : : * @param phase an enum ztest_phase value describing the desired test phase
369 : : * @returns a string name for `phase`
370 : : */
371 : 0 : static inline const char *get_friendly_phase_name(enum ztest_phase phase)
372 : : {
373 [ # # ]: 0 : switch (phase) {
374 : : case TEST_PHASE_SETUP:
375 : : return "setup";
376 : : case TEST_PHASE_BEFORE:
377 : : return "before";
378 : : case TEST_PHASE_TEST:
379 : : return "test";
380 : : case TEST_PHASE_AFTER:
381 : : return "after";
382 : : case TEST_PHASE_TEARDOWN:
383 : : return "teardown";
384 : : case TEST_PHASE_FRAMEWORK:
385 : : return "framework";
386 : : default:
387 : : return "(unknown)";
388 : : }
389 : : }
390 : :
391 : : static bool current_test_failed_assumption;
392 : 0 : void ztest_skip_failed_assumption(void)
393 : : {
394 : 0 : if (IS_ENABLED(CONFIG_ZTEST_FAIL_ON_ASSUME)) {
395 : : current_test_failed_assumption = true;
396 : : ztest_test_fail();
397 : : } else {
398 : 0 : ztest_test_skip();
399 : : }
400 : 0 : }
401 : :
402 : : #ifndef KERNEL
403 : :
404 : : /* Static code analysis tool can raise a violation that the standard header
405 : : * <setjmp.h> shall not be used.
406 : : *
407 : : * setjmp is using in a test code, not in a runtime code, it is acceptable.
408 : : * It is a deliberate deviation.
409 : : */
410 : : #include <setjmp.h> /* parasoft-suppress MISRAC2012-RULE_21_4-a MISRAC2012-RULE_21_4-b*/
411 : : #include <signal.h>
412 : : #include <string.h>
413 : :
414 : : #define FAIL_FAST 0
415 : :
416 : : static jmp_buf test_fail;
417 : : static jmp_buf test_pass;
418 : : static jmp_buf test_skip;
419 : : static jmp_buf stack_fail;
420 : : static jmp_buf test_suite_fail;
421 : :
422 : : void ztest_test_fail(void)
423 : : {
424 : : switch (cur_phase) {
425 : : case TEST_PHASE_SETUP:
426 : : PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
427 : : longjmp(test_suite_fail, 1);
428 : : case TEST_PHASE_BEFORE:
429 : : case TEST_PHASE_TEST:
430 : : PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
431 : : longjmp(test_fail, 1);
432 : : case TEST_PHASE_AFTER:
433 : : case TEST_PHASE_TEARDOWN:
434 : : case TEST_PHASE_FRAMEWORK:
435 : : PRINT_DATA(" ERROR: cannot fail in test phase '%s()', bailing\n",
436 : : get_friendly_phase_name(cur_phase));
437 : : longjmp(stack_fail, 1);
438 : : }
439 : : }
440 : : EXPORT_SYMBOL(ztest_test_fail);
441 : :
442 : : void ztest_test_pass(void)
443 : : {
444 : : if (cur_phase == TEST_PHASE_TEST) {
445 : : longjmp(test_pass, 1);
446 : : }
447 : : PRINT_DATA(" ERROR: cannot pass in test phase '%s()', bailing\n",
448 : : get_friendly_phase_name(cur_phase));
449 : : longjmp(stack_fail, 1);
450 : : }
451 : : EXPORT_SYMBOL(ztest_test_pass);
452 : :
453 : : void ztest_test_skip(void)
454 : : {
455 : : switch (cur_phase) {
456 : : case TEST_PHASE_SETUP:
457 : : case TEST_PHASE_BEFORE:
458 : : case TEST_PHASE_TEST:
459 : : longjmp(test_skip, 1);
460 : : default:
461 : : PRINT_DATA(" ERROR: cannot skip in test phase '%s()', bailing\n",
462 : : get_friendly_phase_name(cur_phase));
463 : : longjmp(stack_fail, 1);
464 : : }
465 : : }
466 : : EXPORT_SYMBOL(ztest_test_skip);
467 : :
468 : : void ztest_test_expect_fail(void)
469 : : {
470 : : failed_expectation = true;
471 : :
472 : : switch (cur_phase) {
473 : : case TEST_PHASE_SETUP:
474 : : PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
475 : : break;
476 : : case TEST_PHASE_BEFORE:
477 : : case TEST_PHASE_TEST:
478 : : PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
479 : : break;
480 : : case TEST_PHASE_AFTER:
481 : : case TEST_PHASE_TEARDOWN:
482 : : case TEST_PHASE_FRAMEWORK:
483 : : PRINT_DATA(" ERROR: cannot fail in test phase '%s()', bailing\n",
484 : : get_friendly_phase_name(cur_phase));
485 : : longjmp(stack_fail, 1);
486 : : }
487 : : }
488 : :
489 : : static int run_test(struct ztest_suite_node *suite, struct ztest_unit_test *test, void *data)
490 : : {
491 : : int ret = TC_PASS;
492 : :
493 : : TC_START(test->name);
494 : : __ztest_set_test_phase(TEST_PHASE_BEFORE);
495 : :
496 : : if (test_result == ZTEST_RESULT_SUITE_FAIL) {
497 : : ret = TC_FAIL;
498 : : goto out;
499 : : }
500 : :
501 : : if (setjmp(test_fail)) {
502 : : ret = TC_FAIL;
503 : : goto out;
504 : : }
505 : :
506 : : if (setjmp(test_pass)) {
507 : : ret = TC_PASS;
508 : : goto out;
509 : : }
510 : :
511 : : if (setjmp(test_skip)) {
512 : : ret = TC_SKIP;
513 : : goto out;
514 : : }
515 : :
516 : : run_test_rules(/*is_before=*/true, test, data);
517 : : if (suite->before) {
518 : : suite->before(data);
519 : : }
520 : : run_test_functions(suite, test, data);
521 : : out:
522 : : if (failed_expectation) {
523 : : failed_expectation = false;
524 : : ret = TC_FAIL;
525 : : }
526 : :
527 : : __ztest_set_test_phase(TEST_PHASE_AFTER);
528 : : if (test_result != ZTEST_RESULT_SUITE_FAIL) {
529 : : if (suite->after != NULL) {
530 : : suite->after(data);
531 : : }
532 : : run_test_rules(/*is_before=*/false, test, data);
533 : : }
534 : : __ztest_set_test_phase(TEST_PHASE_FRAMEWORK);
535 : : ret |= cleanup_test(test);
536 : :
537 : : ret = get_final_test_result(test, ret);
538 : : Z_TC_END_RESULT(ret, test->name);
539 : : if (ret == TC_SKIP && current_test_failed_assumption) {
540 : : test_status = ZTEST_STATUS_HAS_FAILURE;
541 : : }
542 : :
543 : : return ret;
544 : : }
545 : :
546 : : #else /* KERNEL */
547 : :
548 : : /* Zephyr's probably going to cause all tests to fail if one test fails, so
549 : : * skip the rest of tests if one of them fails
550 : : */
551 : : #ifdef CONFIG_ZTEST_FAIL_FAST
552 : : #define FAIL_FAST 1
553 : : #else
554 : : #define FAIL_FAST 0
555 : : #endif
556 : :
557 : : K_THREAD_STACK_DEFINE(ztest_thread_stack, CONFIG_ZTEST_STACK_SIZE + CONFIG_TEST_EXTRA_STACK_SIZE);
558 : :
559 : 0 : static void test_finalize(void)
560 : : {
561 : 0 : if (IS_ENABLED(CONFIG_MULTITHREADING)) {
562 : 0 : k_thread_abort(&ztest_thread);
563 [ # # ]: 0 : if (k_is_in_isr()) {
564 : 0 : return;
565 : : }
566 : :
567 : 0 : k_thread_abort(k_current_get());
568 : 0 : CODE_UNREACHABLE;
569 : : }
570 : : }
571 : :
572 : 0 : void ztest_test_fail(void)
573 : : {
574 [ # # # ]: 0 : switch (cur_phase) {
575 : 0 : case TEST_PHASE_SETUP:
576 : 0 : __ztest_set_test_result(ZTEST_RESULT_SUITE_FAIL);
577 : 0 : break;
578 : 0 : case TEST_PHASE_BEFORE:
579 : : case TEST_PHASE_TEST:
580 : 0 : __ztest_set_test_result(ZTEST_RESULT_FAIL);
581 : 0 : test_finalize();
582 : 0 : break;
583 : 0 : default:
584 : 0 : PRINT_DATA(" ERROR: cannot fail in test phase '%s()', bailing\n",
585 : : get_friendly_phase_name(cur_phase));
586 : 0 : test_status = ZTEST_STATUS_CRITICAL_ERROR;
587 : 0 : break;
588 : : }
589 : 0 : }
590 : : EXPORT_SYMBOL(ztest_test_fail);
591 : :
592 : 0 : void ztest_test_pass(void)
593 : : {
594 [ # # ]: 0 : switch (cur_phase) {
595 : 0 : case TEST_PHASE_TEST:
596 : 0 : __ztest_set_test_result(ZTEST_RESULT_PASS);
597 : 0 : test_finalize();
598 : 0 : break;
599 : 0 : default:
600 : 0 : PRINT_DATA(" ERROR: cannot pass in test phase '%s()', bailing\n",
601 : : get_friendly_phase_name(cur_phase));
602 : 0 : test_status = ZTEST_STATUS_CRITICAL_ERROR;
603 [ # # ]: 0 : if (cur_phase == TEST_PHASE_BEFORE) {
604 : 0 : test_finalize();
605 : : }
606 : : break;
607 : : }
608 : 0 : }
609 : : EXPORT_SYMBOL(ztest_test_pass);
610 : :
611 : 0 : void ztest_test_skip(void)
612 : : {
613 [ # # # ]: 0 : switch (cur_phase) {
614 : 0 : case TEST_PHASE_SETUP:
615 : 0 : __ztest_set_test_result(ZTEST_RESULT_SUITE_SKIP);
616 : 0 : break;
617 : 0 : case TEST_PHASE_BEFORE:
618 : : case TEST_PHASE_TEST:
619 : 0 : __ztest_set_test_result(ZTEST_RESULT_SKIP);
620 : 0 : test_finalize();
621 : 0 : break;
622 : 0 : default:
623 : 0 : PRINT_DATA(" ERROR: cannot skip in test phase '%s()', bailing\n",
624 : : get_friendly_phase_name(cur_phase));
625 : 0 : test_status = ZTEST_STATUS_CRITICAL_ERROR;
626 : 0 : break;
627 : : }
628 : 0 : }
629 : : EXPORT_SYMBOL(ztest_test_skip);
630 : :
631 : 0 : void ztest_test_expect_fail(void)
632 : : {
633 : 0 : failed_expectation = true;
634 : 0 : }
635 : :
636 : 0 : void ztest_simple_1cpu_before(void *data)
637 : : {
638 : 0 : ARG_UNUSED(data);
639 : 0 : z_test_1cpu_start();
640 : 0 : }
641 : :
642 : 0 : void ztest_simple_1cpu_after(void *data)
643 : : {
644 : 0 : ARG_UNUSED(data);
645 : 0 : z_test_1cpu_stop();
646 : 0 : }
647 : :
648 : 42 : static void test_cb(void *a, void *b, void *c)
649 : : {
650 : 42 : struct ztest_suite_node *suite = a;
651 : 42 : struct ztest_unit_test *test = b;
652 : 42 : const bool config_user_mode = FIELD_GET(K_USER, test->thread_options) != 0;
653 : :
654 [ + - ]: 42 : if (!k_is_user_context()) {
655 : 42 : __ztest_set_test_result(ZTEST_RESULT_PENDING);
656 : 42 : run_test_rules(/*is_before=*/true, test, /*data=*/c);
657 [ - + ]: 42 : if (suite->before) {
658 : 0 : suite->before(/*data=*/c);
659 : : }
660 : : if (IS_ENABLED(CONFIG_USERSPACE) && config_user_mode) {
661 : : k_thread_user_mode_enter(test_cb, a, b, c);
662 : : }
663 : : }
664 : 42 : run_test_functions(suite, test, c);
665 : 42 : __ztest_set_test_result(ZTEST_RESULT_PASS);
666 : 42 : }
667 : :
668 : 42 : static int run_test(struct ztest_suite_node *suite, struct ztest_unit_test *test, void *data)
669 : : {
670 : 42 : int ret = TC_PASS;
671 : :
672 : : #if CONFIG_ZTEST_TEST_DELAY_MS > 0
673 : : k_busy_wait(CONFIG_ZTEST_TEST_DELAY_MS * USEC_PER_MSEC);
674 : : #endif
675 : 42 : TC_START(test->name);
676 : :
677 : 42 : __ztest_set_test_phase(TEST_PHASE_BEFORE);
678 : :
679 : : /* If the suite's setup function marked us as skipped, don't bother
680 : : * running the tests.
681 : : */
682 : 42 : if (IS_ENABLED(CONFIG_MULTITHREADING)) {
683 : 42 : get_start_time_cyc();
684 : 42 : k_thread_create(&ztest_thread, ztest_thread_stack,
685 : : K_THREAD_STACK_SIZEOF(ztest_thread_stack), test_cb, suite, test,
686 : 42 : data, CONFIG_ZTEST_THREAD_PRIORITY, K_INHERIT_PERMS, K_FOREVER);
687 : :
688 : 42 : k_thread_access_grant(&ztest_thread, suite, test, suite->stats);
689 [ + - ]: 42 : if (test->name != NULL) {
690 : 42 : k_thread_name_set(&ztest_thread, test->name);
691 : : }
692 : : /* Only start the thread if we're not skipping the suite */
693 [ + - ]: 42 : if (test_result != ZTEST_RESULT_SUITE_SKIP &&
694 : : test_result != ZTEST_RESULT_SUITE_FAIL) {
695 : 42 : k_thread_start(&ztest_thread);
696 : 42 : k_thread_join(&ztest_thread, K_FOREVER);
697 : : }
698 : : } else if (test_result != ZTEST_RESULT_SUITE_SKIP &&
699 : : test_result != ZTEST_RESULT_SUITE_FAIL) {
700 : : __ztest_set_test_result(ZTEST_RESULT_PENDING);
701 : : get_start_time_cyc();
702 : : run_test_rules(/*is_before=*/true, test, data);
703 : : if (suite->before) {
704 : : suite->before(data);
705 : : }
706 : : run_test_functions(suite, test, data);
707 : : }
708 : :
709 : 42 : __ztest_set_test_phase(TEST_PHASE_AFTER);
710 [ - + ]: 42 : if (suite->after != NULL) {
711 : 0 : suite->after(data);
712 : : }
713 : 42 : run_test_rules(/*is_before=*/false, test, data);
714 : :
715 : 42 : get_test_duration_ms();
716 [ - + ]: 42 : if (tc_spend_time > test->stats->duration_worst_ms) {
717 : 0 : test->stats->duration_worst_ms = tc_spend_time;
718 : : }
719 : :
720 : 42 : __ztest_set_test_phase(TEST_PHASE_FRAMEWORK);
721 : :
722 : : /* Flush all logs in case deferred mode and default logging thread are used. */
723 : 42 : while (IS_ENABLED(CONFIG_TEST_LOGGING_FLUSH_AFTER_TEST) &&
724 : : IS_ENABLED(CONFIG_LOG_PROCESS_THREAD) && log_data_pending()) {
725 : : k_msleep(100);
726 : : }
727 : :
728 [ + - - + ]: 42 : if (test_result == ZTEST_RESULT_FAIL || test_result == ZTEST_RESULT_SUITE_FAIL ||
729 : : failed_expectation) {
730 : 0 : ret = TC_FAIL;
731 : 0 : failed_expectation = false;
732 [ - + ]: 42 : } else if (test_result == ZTEST_RESULT_SKIP || test_result == ZTEST_RESULT_SUITE_SKIP) {
733 : 0 : ret = TC_SKIP;
734 : : }
735 : :
736 : 42 : if (test_result == ZTEST_RESULT_PASS || !FAIL_FAST) {
737 : 42 : ret |= cleanup_test(test);
738 : : }
739 : :
740 : 42 : ret = get_final_test_result(test, ret);
741 : 42 : Z_TC_END_RESULT(ret, test->name);
742 [ - + - - ]: 42 : if (ret == TC_SKIP && current_test_failed_assumption) {
743 : 0 : test_status = ZTEST_STATUS_HAS_FAILURE;
744 : : }
745 : :
746 : 42 : return ret;
747 : : }
748 : :
749 : : #endif /* !KERNEL */
750 : :
751 : 42 : static struct ztest_suite_node *ztest_find_test_suite(const char *name)
752 : : {
753 : 42 : struct ztest_suite_node *node;
754 : :
755 [ + - ]: 42 : for (node = _ztest_suite_node_list_start; node < _ztest_suite_node_list_end; ++node) {
756 [ + - ]: 42 : if (strcmp(name, node->name) == 0) {
757 : 42 : return node;
758 : : }
759 : : }
760 : :
761 : : return NULL;
762 : : }
763 : :
764 : 129 : struct ztest_unit_test *z_ztest_get_next_test(const char *suite, struct ztest_unit_test *prev)
765 : : {
766 [ + + ]: 129 : struct ztest_unit_test *test = (prev == NULL) ? _ztest_unit_test_list_start : prev + 1;
767 : :
768 [ + + ]: 129 : for (; test < _ztest_unit_test_list_end; ++test) {
769 [ + - ]: 126 : if (strcmp(suite, test->test_suite_name) == 0) {
770 : 126 : return test;
771 : : }
772 : : }
773 : : return NULL;
774 : : }
775 : :
776 : : #if CONFIG_ZTEST_SHUFFLE
777 : : static void z_ztest_shuffle(bool shuffle, void *dest[], intptr_t start, size_t num_items,
778 : : size_t element_size)
779 : : {
780 : : /* Initialize dest array */
781 : : for (size_t i = 0; i < num_items; ++i) {
782 : : dest[i] = (void *)(start + (i * element_size));
783 : : }
784 : : void *tmp;
785 : :
786 : : /* Shuffle dest array */
787 : : if (shuffle) {
788 : : for (size_t i = num_items - 1; i > 0; i--) {
789 : : int j = sys_rand32_get() % (i + 1);
790 : :
791 : : if (i != j) {
792 : : tmp = dest[j];
793 : : dest[j] = dest[i];
794 : : dest[i] = tmp;
795 : : }
796 : : }
797 : : }
798 : : }
799 : : #endif
800 : :
801 : 1 : static int z_ztest_run_test_suite_ptr(struct ztest_suite_node *suite, bool shuffle, int suite_iter,
802 : : int case_iter, void *param)
803 : : {
804 : 1 : struct ztest_unit_test *test = NULL;
805 : 1 : void *data = NULL;
806 : 1 : int fail = 0;
807 : 1 : int tc_result = TC_PASS;
808 : :
809 : 1 : if (FAIL_FAST && test_status != ZTEST_STATUS_OK) {
810 : : return test_status;
811 : : }
812 : :
813 [ - + ]: 1 : if (suite == NULL) {
814 : 0 : test_status = ZTEST_STATUS_CRITICAL_ERROR;
815 : 0 : return -1;
816 : : }
817 : :
818 : : #ifndef KERNEL
819 : : if (setjmp(stack_fail)) {
820 : : PRINT_DATA("TESTSUITE crashed.\n");
821 : : test_status = ZTEST_STATUS_CRITICAL_ERROR;
822 : : end_report();
823 : : exit(1);
824 : : }
825 : : #else
826 : 1 : k_object_access_all_grant(&ztest_thread);
827 : : #endif
828 : :
829 : 1 : TC_SUITE_START(suite->name);
830 : 1 : current_test_failed_assumption = false;
831 : 1 : __ztest_set_test_result(ZTEST_RESULT_PENDING);
832 : 1 : __ztest_set_test_phase(TEST_PHASE_SETUP);
833 : : #ifndef KERNEL
834 : : if (setjmp(test_suite_fail)) {
835 : : __ztest_set_test_result(ZTEST_RESULT_SUITE_FAIL);
836 : : }
837 : : #endif
838 [ + - - + ]: 1 : if (test_result != ZTEST_RESULT_SUITE_FAIL && suite->setup != NULL) {
839 : 0 : data = suite->setup();
840 : : }
841 [ - + ]: 1 : if (param != NULL) {
842 : 0 : data = param;
843 : : }
844 : :
845 [ + + ]: 2 : for (int i = 0; i < case_iter; i++) {
846 : : #ifdef CONFIG_ZTEST_SHUFFLE
847 : : struct ztest_unit_test *tests_to_run[ZTEST_TEST_COUNT];
848 : :
849 : : memset(tests_to_run, 0, ZTEST_TEST_COUNT * sizeof(struct ztest_unit_test *));
850 : : z_ztest_shuffle(shuffle, (void **)tests_to_run,
851 : : (intptr_t)_ztest_unit_test_list_start, ZTEST_TEST_COUNT,
852 : : sizeof(struct ztest_unit_test));
853 : : for (size_t j = 0; j < ZTEST_TEST_COUNT; ++j) {
854 : : test = tests_to_run[j];
855 : : /* Make sure that the test belongs to this suite */
856 : : if (strcmp(suite->name, test->test_suite_name) != 0) {
857 : : continue;
858 : : }
859 : : if (ztest_api.should_test_run(suite->name, test->name)) {
860 : : test->stats->run_count++;
861 : : tc_result = run_test(suite, test, data);
862 : : if (tc_result == TC_PASS) {
863 : : test->stats->pass_count++;
864 : : } else if (tc_result == TC_SKIP) {
865 : : test->stats->skip_count++;
866 : : } else if (tc_result == TC_FAIL) {
867 : : test->stats->fail_count++;
868 : : }
869 : : if (tc_result == TC_FAIL) {
870 : : fail++;
871 : : }
872 : : }
873 : :
874 : : if ((fail && FAIL_FAST) || test_status == ZTEST_STATUS_CRITICAL_ERROR) {
875 : : break;
876 : : }
877 : : }
878 : : #else
879 [ + + ]: 43 : while (((test = z_ztest_get_next_test(suite->name, test)) != NULL)) {
880 [ - + ]: 42 : if (ztest_api.should_test_run(suite->name, test->name)) {
881 : 42 : test->stats->run_count++;
882 : 42 : tc_result = run_test(suite, test, data);
883 [ + - ]: 42 : if (tc_result == TC_PASS) {
884 : 42 : test->stats->pass_count++;
885 [ # # ]: 0 : } else if (tc_result == TC_SKIP) {
886 : 0 : test->stats->skip_count++;
887 [ # # ]: 0 : } else if (tc_result == TC_FAIL) {
888 : 0 : test->stats->fail_count++;
889 : : }
890 : :
891 [ + - ]: 42 : if (tc_result == TC_FAIL) {
892 : 0 : fail++;
893 : : }
894 : : }
895 : :
896 [ + - ]: 42 : if ((fail && FAIL_FAST) || test_status == ZTEST_STATUS_CRITICAL_ERROR) {
897 : : break;
898 : : }
899 : : }
900 : : #endif
901 [ + - - + ]: 1 : if (test_status == ZTEST_STATUS_OK && fail != 0) {
902 : 0 : test_status = ZTEST_STATUS_HAS_FAILURE;
903 : : }
904 : : }
905 : :
906 [ + - ]: 1 : TC_SUITE_END(suite->name, (fail > 0 ? TC_FAIL : TC_PASS));
907 : 1 : __ztest_set_test_phase(TEST_PHASE_TEARDOWN);
908 [ - + ]: 1 : if (suite->teardown != NULL) {
909 : 0 : suite->teardown(data);
910 : : }
911 : :
912 : : return fail;
913 : : }
914 : :
915 : 0 : int z_ztest_run_test_suite(const char *name, bool shuffle,
916 : : int suite_iter, int case_iter, void *param)
917 : : {
918 : 0 : return z_ztest_run_test_suite_ptr(ztest_find_test_suite(name), shuffle, suite_iter,
919 : : case_iter, param);
920 : : }
921 : :
922 : : #ifdef CONFIG_USERSPACE
923 : : K_APPMEM_PARTITION_DEFINE(ztest_mem_partition);
924 : : #endif
925 : :
926 : : /* Show one line summary for a test suite.
927 : : */
928 : 1 : static void __ztest_show_suite_summary_oneline(struct ztest_suite_node *suite)
929 : : {
930 : 1 : int distinct_pass = 0, distinct_fail = 0, distinct_skip = 0, distinct_total = 0;
931 : 1 : int effective_total = 0;
932 : 1 : int expanded_pass = 0, expanded_passrate = 0;
933 : 1 : int passrate_major = 0, passrate_minor = 0, passrate_tail = 0;
934 : 1 : int suite_result = TC_PASS;
935 : :
936 : 1 : struct ztest_unit_test *test = NULL;
937 : 1 : unsigned int suite_duration_worst_ms = 0;
938 : :
939 : : /** summary of distinct run */
940 [ + + ]: 43 : while (((test = z_ztest_get_next_test(suite->name, test)) != NULL)) {
941 : 42 : distinct_total++;
942 : 42 : suite_duration_worst_ms += test->stats->duration_worst_ms;
943 [ - + ]: 42 : if (test->stats->skip_count == test->stats->run_count) {
944 : 0 : distinct_skip++;
945 [ + - ]: 42 : } else if (test->stats->pass_count == test->stats->run_count) {
946 : 42 : distinct_pass++;
947 : : } else {
948 : 0 : distinct_fail++;
949 : : }
950 : : }
951 : :
952 [ + - ]: 1 : if (distinct_skip == distinct_total) {
953 : : suite_result = TC_SKIP;
954 : : passrate_major = passrate_minor = 0;
955 : : } else {
956 : 1 : suite_result = (distinct_fail > 0) ? TC_FAIL : TC_PASS;
957 : 1 : effective_total = distinct_total - distinct_skip;
958 : 1 : expanded_pass = distinct_pass * 100000;
959 : 1 : expanded_passrate = expanded_pass / effective_total;
960 : 1 : passrate_major = expanded_passrate / 1000;
961 : 1 : passrate_minor = (expanded_passrate - passrate_major * 1000) / 10;
962 : 1 : passrate_tail = expanded_passrate - passrate_major * 1000 - passrate_minor * 10;
963 [ - + ]: 1 : if (passrate_tail >= 5) { /* rounding */
964 : 0 : passrate_minor++;
965 : : }
966 : : }
967 : :
968 : 1 : TC_SUMMARY_PRINT("SUITE %s - %3d.%02d%% [%s]: pass = %d, fail = %d, "
969 : : "skip = %d, total = %d duration = %u.%03u seconds\n",
970 : : TC_RESULT_TO_STR(suite_result), passrate_major, passrate_minor,
971 : : suite->name, distinct_pass, distinct_fail, distinct_skip, distinct_total,
972 : : suite_duration_worst_ms / 1000, suite_duration_worst_ms % 1000);
973 : 1 : log_flush();
974 : 1 : }
975 : :
976 : 1 : static void __ztest_show_suite_summary_verbose(struct ztest_suite_node *suite)
977 : : {
978 : 1 : struct ztest_unit_test *test = NULL;
979 : 1 : int tc_result = TC_PASS;
980 : 1 : int flush_frequency = 0;
981 : :
982 : 1 : if (IS_ENABLED(CONFIG_ZTEST_VERBOSE_SUMMARY) == 0) {
983 : : return;
984 : : }
985 : :
986 [ + + ]: 43 : while (((test = z_ztest_get_next_test(suite->name, test)) != NULL)) {
987 [ + - ]: 42 : if (test->stats->skip_count == test->stats->run_count) {
988 : : tc_result = TC_SKIP;
989 [ - + ]: 42 : } else if (test->stats->pass_count == test->stats->run_count) {
990 : : tc_result = TC_PASS;
991 [ # # ]: 0 : } else if (test->stats->pass_count == 0) {
992 : : tc_result = TC_FAIL;
993 : : } else {
994 : 0 : tc_result = TC_FLAKY;
995 : : }
996 : :
997 : 0 : if (tc_result == TC_FLAKY) {
998 : 0 : TC_SUMMARY_PRINT(
999 : : " - %s - [%s.%s] - (Failed %d of %d attempts)"
1000 : : " - duration = %u.%03u seconds\n",
1001 : : TC_RESULT_TO_STR(tc_result), test->test_suite_name, test->name,
1002 : : test->stats->run_count - test->stats->pass_count,
1003 : : test->stats->run_count, test->stats->duration_worst_ms / 1000,
1004 : : test->stats->duration_worst_ms % 1000);
1005 : : } else {
1006 : 42 : TC_SUMMARY_PRINT(" - %s - [%s.%s] duration = %u.%03u seconds\n",
1007 : : TC_RESULT_TO_STR(tc_result), test->test_suite_name,
1008 : : test->name, test->stats->duration_worst_ms / 1000,
1009 : : test->stats->duration_worst_ms % 1000);
1010 : : }
1011 : :
1012 : 42 : if (flush_frequency % 3 == 0) {
1013 : : /** Reduce the flush frequency a bit to speed up the output */
1014 : : log_flush();
1015 : : }
1016 : 42 : flush_frequency++;
1017 : : }
1018 : 1 : TC_SUMMARY_PRINT("\n");
1019 : 1 : log_flush();
1020 : : }
1021 : :
1022 : 1 : static void __ztest_show_suite_summary(void)
1023 : : {
1024 : 1 : if (IS_ENABLED(CONFIG_ZTEST_SUMMARY) == 0) {
1025 : : return;
1026 : : }
1027 : : /* Flush the log a lot to ensure that no summary content
1028 : : * is dropped if it goes through the logging subsystem.
1029 : : */
1030 : 1 : log_flush();
1031 : 1 : TC_SUMMARY_PRINT("\n------ TESTSUITE SUMMARY START ------\n\n");
1032 : 1 : log_flush();
1033 : 1 : for (struct ztest_suite_node *ptr = _ztest_suite_node_list_start;
1034 [ + + ]: 2 : ptr < _ztest_suite_node_list_end; ++ptr) {
1035 : :
1036 : 1 : __ztest_show_suite_summary_oneline(ptr);
1037 : 1 : __ztest_show_suite_summary_verbose(ptr);
1038 : : }
1039 : 1 : TC_SUMMARY_PRINT("------ TESTSUITE SUMMARY END ------\n\n");
1040 : 1 : log_flush();
1041 : : }
1042 : :
1043 : 1 : static int __ztest_run_test_suite(struct ztest_suite_node *ptr, const void *state, bool shuffle,
1044 : : int suite_iter, int case_iter, void *param)
1045 : : {
1046 : 1 : struct ztest_suite_stats *stats = ptr->stats;
1047 : 1 : int count = 0;
1048 : :
1049 [ + + ]: 2 : for (int i = 0; i < suite_iter; i++) {
1050 [ + - ]: 1 : if (ztest_api.should_suite_run(state, ptr)) {
1051 : 1 : int fail = z_ztest_run_test_suite_ptr(ptr, shuffle,
1052 : : suite_iter, case_iter, param);
1053 : :
1054 : 1 : count++;
1055 : 1 : stats->run_count++;
1056 [ + - ]: 2 : stats->fail_count += (fail != 0) ? 1 : 0;
1057 : : } else {
1058 : 0 : stats->skip_count++;
1059 : : }
1060 : : }
1061 : :
1062 : 1 : return count;
1063 : : }
1064 : :
1065 : 1 : int z_impl_ztest_run_test_suites(const void *state, bool shuffle, int suite_iter, int case_iter)
1066 : : {
1067 : 1 : int count = 0;
1068 : 1 : void *param = NULL;
1069 [ + - ]: 1 : if (test_status == ZTEST_STATUS_CRITICAL_ERROR) {
1070 : : return count;
1071 : : }
1072 : :
1073 : : #ifdef CONFIG_ZTEST_COVERAGE_RESET_BEFORE_TESTS
1074 : : gcov_reset_all_counts();
1075 : : #endif
1076 : :
1077 : : #ifdef CONFIG_ZTEST_SHUFFLE
1078 : : struct ztest_suite_node *suites_to_run[ZTEST_SUITE_COUNT];
1079 : :
1080 : : memset(suites_to_run, 0, ZTEST_SUITE_COUNT * sizeof(struct ztest_suite_node *));
1081 : : z_ztest_shuffle(shuffle, (void **)suites_to_run, (intptr_t)_ztest_suite_node_list_start,
1082 : : ZTEST_SUITE_COUNT, sizeof(struct ztest_suite_node));
1083 : : for (size_t i = 0; i < ZTEST_SUITE_COUNT; ++i) {
1084 : : count += __ztest_run_test_suite(suites_to_run[i], state, shuffle, suite_iter,
1085 : : case_iter, param);
1086 : : /* Stop running tests if we have a critical error or if we have a failure and
1087 : : * FAIL_FAST was set
1088 : : */
1089 : : if (test_status == ZTEST_STATUS_CRITICAL_ERROR ||
1090 : : (test_status == ZTEST_STATUS_HAS_FAILURE && FAIL_FAST)) {
1091 : : break;
1092 : : }
1093 : : }
1094 : : #else
1095 : : for (struct ztest_suite_node *ptr = _ztest_suite_node_list_start;
1096 [ + + ]: 2 : ptr < _ztest_suite_node_list_end; ++ptr) {
1097 : 1 : count += __ztest_run_test_suite(ptr, state, shuffle, suite_iter, case_iter, param);
1098 : : /* Stop running tests if we have a critical error or if we have a failure and
1099 : : * FAIL_FAST was set
1100 : : */
1101 [ + - ]: 1 : if (test_status == ZTEST_STATUS_CRITICAL_ERROR ||
1102 : : (test_status == ZTEST_STATUS_HAS_FAILURE && FAIL_FAST)) {
1103 : : break;
1104 : : }
1105 : : }
1106 : : #endif
1107 : :
1108 : : return count;
1109 : : }
1110 : :
1111 : 85 : void z_impl___ztest_set_test_result(enum ztest_result new_result)
1112 : : {
1113 : 85 : test_result = new_result;
1114 : 85 : }
1115 : :
1116 : 170 : void z_impl___ztest_set_test_phase(enum ztest_phase new_phase)
1117 : : {
1118 : 170 : cur_phase = new_phase;
1119 : 170 : }
1120 : :
1121 : : #ifdef CONFIG_USERSPACE
1122 : : void z_vrfy___ztest_set_test_result(enum ztest_result new_result)
1123 : : {
1124 : : z_impl___ztest_set_test_result(new_result);
1125 : : }
1126 : : #include <zephyr/syscalls/__ztest_set_test_result_mrsh.c>
1127 : :
1128 : : void z_vrfy___ztest_set_test_phase(enum ztest_phase new_phase)
1129 : : {
1130 : : z_impl___ztest_set_test_phase(new_phase);
1131 : : }
1132 : : #include <zephyr/syscalls/__ztest_set_test_phase_mrsh.c>
1133 : : #endif /* CONFIG_USERSPACE */
1134 : :
1135 : 1 : void ztest_verify_all_test_suites_ran(void)
1136 : : {
1137 : 1 : bool all_tests_run = true;
1138 : 1 : struct ztest_suite_node *suite;
1139 : 1 : struct ztest_unit_test *test;
1140 : :
1141 : 1 : if (IS_ENABLED(CONFIG_ZTEST_VERIFY_RUN_ALL)) {
1142 [ + + ]: 2 : for (suite = _ztest_suite_node_list_start; suite < _ztest_suite_node_list_end;
1143 : 1 : ++suite) {
1144 [ - + ]: 1 : if (suite->stats->run_count < 1) {
1145 : 0 : PRINT_DATA("ERROR: Test suite '%s' did not run.\n", suite->name);
1146 : 0 : all_tests_run = false;
1147 : : }
1148 : : }
1149 : :
1150 [ + + ]: 43 : for (test = _ztest_unit_test_list_start; test < _ztest_unit_test_list_end; ++test) {
1151 : 42 : suite = ztest_find_test_suite(test->test_suite_name);
1152 [ - + ]: 42 : if (suite == NULL) {
1153 : 0 : PRINT_DATA("ERROR: Test '%s' assigned to test suite '%s' which "
1154 : : "doesn't "
1155 : : "exist\n",
1156 : : test->name, test->test_suite_name);
1157 : 0 : all_tests_run = false;
1158 : : }
1159 : : }
1160 : :
1161 [ - + ]: 1 : if (!all_tests_run) {
1162 : 0 : test_status = ZTEST_STATUS_HAS_FAILURE;
1163 : : }
1164 : : }
1165 : :
1166 [ + + ]: 43 : for (test = _ztest_unit_test_list_start; test < _ztest_unit_test_list_end; ++test) {
1167 : 42 : if (test->stats->fail_count + test->stats->pass_count + test->stats->skip_count !=
1168 [ - + ]: 42 : test->stats->run_count) {
1169 : 0 : PRINT_DATA("Bad stats for %s.%s\n", test->test_suite_name, test->name);
1170 : 0 : test_status = ZTEST_STATUS_HAS_FAILURE;
1171 : : }
1172 : : }
1173 : 1 : }
1174 : :
1175 : 1 : void ztest_run_all(const void *state, bool shuffle, int suite_iter, int case_iter)
1176 : : {
1177 : 1 : ztest_api.run_all(state, shuffle, suite_iter, case_iter);
1178 : 1 : }
1179 : :
1180 : 1 : void __weak test_main(void)
1181 : : {
1182 : : #if CONFIG_ZTEST_SHUFFLE
1183 : : ztest_run_all(NULL, true, NUM_ITER_PER_SUITE, NUM_ITER_PER_TEST);
1184 : : #else
1185 : 1 : ztest_run_all(NULL, false, NUM_ITER_PER_SUITE, NUM_ITER_PER_TEST);
1186 : : #endif
1187 : 1 : ztest_verify_all_test_suites_ran();
1188 : 1 : }
1189 : :
1190 : : #ifndef KERNEL
1191 : : int main(void)
1192 : : {
1193 : : z_init_mock();
1194 : : test_main();
1195 : : end_report();
1196 : : #ifdef CONFIG_ZTEST_NO_YIELD
1197 : : /*
1198 : : * Rather than yielding to idle thread, keep the part awake so debugger can
1199 : : * still access it, since some SOCs cannot be debugged in low power states.
1200 : : */
1201 : : uint32_t key = irq_lock();
1202 : :
1203 : : while (1) {
1204 : : ; /* Spin */
1205 : : }
1206 : : irq_unlock(key);
1207 : : #endif
1208 : : return test_status;
1209 : : }
1210 : : #else
1211 : :
1212 : : /* Shell */
1213 : :
1214 : : #ifdef CONFIG_ZTEST_SHELL
1215 : : static int cmd_list_suites(const struct shell *sh, size_t argc, char **argv)
1216 : : {
1217 : : struct ztest_suite_node *suite;
1218 : :
1219 : : for (suite = _ztest_suite_node_list_start; suite < _ztest_suite_node_list_end; ++suite) {
1220 : : shell_print(sh, "%s", suite->name);
1221 : : }
1222 : : return 0;
1223 : : }
1224 : :
1225 : : static int cmd_list_cases(const struct shell *sh, size_t argc, char **argv)
1226 : : {
1227 : : struct ztest_suite_node *ptr;
1228 : : struct ztest_unit_test *test = NULL;
1229 : : int test_count = 0;
1230 : :
1231 : : for (ptr = _ztest_suite_node_list_start; ptr < _ztest_suite_node_list_end; ++ptr) {
1232 : : test = NULL;
1233 : : while ((test = z_ztest_get_next_test(ptr->name, test)) != NULL) {
1234 : : shell_print(sh, "%s::%s", test->test_suite_name, test->name);
1235 : : test_count++;
1236 : : }
1237 : : }
1238 : : return 0;
1239 : : }
1240 : : extern void ztest_set_test_args(char *argv);
1241 : : extern void ztest_reset_test_args(void);
1242 : :
1243 : : static int cmd_runall(const struct shell *sh, size_t argc, char **argv)
1244 : : {
1245 : : ztest_reset_test_args();
1246 : : ztest_run_all(NULL, false, 1, 1);
1247 : : end_report();
1248 : : return 0;
1249 : : }
1250 : :
1251 : : #ifdef CONFIG_ZTEST_SHUFFLE
1252 : : static int cmd_shuffle(const struct shell *sh, size_t argc, char **argv)
1253 : : {
1254 : :
1255 : : struct sys_getopt_state *state;
1256 : : int opt;
1257 : : static struct sys_getopt_option long_options[] = {
1258 : : {"suite_iter", sys_getopt_required_argument, 0, 's'},
1259 : : {"case_iter", sys_getopt_required_argument, 0, 'c'},
1260 : : {0, 0, 0, 0}};
1261 : : int opt_index = 0;
1262 : : int val;
1263 : : int opt_num = 0;
1264 : :
1265 : : int suite_iter = 1;
1266 : : int case_iter = 1;
1267 : :
1268 : : while ((opt = sys_getopt_long(argc, argv, "s:c:", long_options, &opt_index)) != -1) {
1269 : : state = sys_getopt_state_get();
1270 : : switch (opt) {
1271 : : case 's':
1272 : : val = atoi(state->optarg);
1273 : : if (val < 1) {
1274 : : shell_error(sh, "Invalid number of suite iterations");
1275 : : return -ENOEXEC;
1276 : : }
1277 : : suite_iter = val;
1278 : : opt_num++;
1279 : : break;
1280 : : case 'c':
1281 : : val = atoi(state->optarg);
1282 : : if (val < 1) {
1283 : : shell_error(sh, "Invalid number of case iterations");
1284 : : return -ENOEXEC;
1285 : : }
1286 : : case_iter = val;
1287 : : opt_num++;
1288 : : break;
1289 : : default:
1290 : : shell_error(sh, "Invalid option or option usage: %s",
1291 : : argv[opt_index + 1]);
1292 : : return -ENOEXEC;
1293 : : }
1294 : : }
1295 : : ztest_reset_test_args();
1296 : : ztest_run_all(NULL, true, suite_iter, case_iter);
1297 : : end_report();
1298 : : return 0;
1299 : : }
1300 : : #endif
1301 : :
1302 : : static int cmd_run_suite(const struct shell *sh, size_t argc, char **argv)
1303 : : {
1304 : : struct sys_getopt_state *state;
1305 : : int opt;
1306 : : static struct sys_getopt_option long_options[] = {
1307 : : {"repeat_iter", sys_getopt_required_argument, NULL, 'r'},
1308 : : {NULL, 0, NULL, 0}};
1309 : : int opt_index = 0;
1310 : : int val;
1311 : : int opt_num = 0;
1312 : : void *param = NULL;
1313 : : int repeat_iter = 1;
1314 : :
1315 : : while ((opt = sys_getopt_long(argc, argv, "r:p:", long_options, &opt_index)) != -1) {
1316 : : state = sys_getopt_state_get();
1317 : : switch (opt) {
1318 : : case 'r':
1319 : : val = atoi(state->optarg);
1320 : : if (val < 1) {
1321 : : shell_fprintf(sh, SHELL_ERROR,
1322 : : "Invalid number of suite interations\n");
1323 : : return -ENOEXEC;
1324 : : }
1325 : : repeat_iter = val;
1326 : : opt_num++;
1327 : : break;
1328 : : case 'p':
1329 : : param = state->optarg;
1330 : : opt_num++;
1331 : : break;
1332 : : default:
1333 : : shell_fprintf(sh, SHELL_ERROR,
1334 : : "Invalid option or option usage: %s\n", argv[opt_index + 1]);
1335 : : return -ENOEXEC;
1336 : : }
1337 : : }
1338 : : int count = 0;
1339 : : bool shuffle = false;
1340 : : const char *shell_command = argv[0];
1341 : :
1342 : : /*
1343 : : * This if statement determines which argv contains the test name.
1344 : : * If the optional argument is used, the test name is in the third
1345 : : * argv instead of the first.
1346 : : */
1347 : : if (opt_num == 1) {
1348 : : ztest_set_test_args(argv[3]);
1349 : : } else {
1350 : : ztest_set_test_args(argv[1]);
1351 : : }
1352 : :
1353 : : for (struct ztest_suite_node *ptr = _ztest_suite_node_list_start;
1354 : : ptr < _ztest_suite_node_list_end; ++ptr) {
1355 : : if (strcmp(shell_command, "run-testcase") == 0) {
1356 : : count += __ztest_run_test_suite(ptr, NULL, shuffle, 1, repeat_iter, param);
1357 : : } else if (strcmp(shell_command, "run-testsuite") == 0) {
1358 : : count += __ztest_run_test_suite(ptr, NULL, shuffle, repeat_iter, 1, NULL);
1359 : : }
1360 : : if (test_status == ZTEST_STATUS_CRITICAL_ERROR ||
1361 : : (test_status == ZTEST_STATUS_HAS_FAILURE && FAIL_FAST)) {
1362 : : break;
1363 : : }
1364 : : }
1365 : : return 0;
1366 : : }
1367 : :
1368 : : static void testsuite_list_get(size_t idx, struct shell_static_entry *entry);
1369 : :
1370 : : SHELL_DYNAMIC_CMD_CREATE(testsuite_names, testsuite_list_get);
1371 : :
1372 : : static size_t testsuite_get_all_static(struct ztest_suite_node const **suites)
1373 : : {
1374 : : *suites = _ztest_suite_node_list_start;
1375 : : return _ztest_suite_node_list_end - _ztest_suite_node_list_start;
1376 : : }
1377 : :
1378 : : static const struct ztest_suite_node *suite_lookup(size_t idx, const char *prefix)
1379 : : {
1380 : : size_t match_idx = 0;
1381 : : const struct ztest_suite_node *suite;
1382 : : size_t len = testsuite_get_all_static(&suite);
1383 : : const struct ztest_suite_node *suite_end = suite + len;
1384 : :
1385 : : while (suite < suite_end) {
1386 : : if ((suite->name != NULL) && (strlen(suite->name) != 0) &&
1387 : : ((prefix == NULL) || (strncmp(prefix, suite->name, strlen(prefix)) == 0))) {
1388 : : if (match_idx == idx) {
1389 : : return suite;
1390 : : }
1391 : : ++match_idx;
1392 : : }
1393 : : ++suite;
1394 : : }
1395 : :
1396 : : return NULL;
1397 : : }
1398 : :
1399 : : static void testsuite_list_get(size_t idx, struct shell_static_entry *entry)
1400 : : {
1401 : : const struct ztest_suite_node *suite = suite_lookup(idx, "");
1402 : :
1403 : : entry->syntax = (suite != NULL) ? suite->name : NULL;
1404 : : entry->handler = NULL;
1405 : : entry->help = NULL;
1406 : : entry->subcmd = NULL;
1407 : : }
1408 : :
1409 : : /* clang-format off */
1410 : : SHELL_STATIC_SUBCMD_SET_CREATE(
1411 : : sub_ztest_cmds,
1412 : : SHELL_CMD_ARG(run-all, NULL, "Run all tests", cmd_runall, 0, 0),
1413 : : #ifdef CONFIG_ZTEST_SHUFFLE
1414 : : SHELL_COND_CMD_ARG(CONFIG_ZTEST_SHUFFLE, shuffle, NULL,
1415 : : "Shuffle tests", cmd_shuffle, 0, 2),
1416 : : #endif
1417 : : SHELL_CMD_ARG(list-testsuites, NULL,
1418 : : "List all test suites", cmd_list_suites, 0, 0),
1419 : : SHELL_CMD_ARG(list-testcases, NULL,
1420 : : "List all test cases", cmd_list_cases, 0, 0),
1421 : : SHELL_CMD_ARG(run-testsuite, &testsuite_names,
1422 : : "Run test suite", cmd_run_suite, 2, 2),
1423 : : SHELL_CMD_ARG(run-testcase, NULL, "Run testcase", cmd_run_suite, 2, 2),
1424 : : SHELL_SUBCMD_SET_END /* Array terminated. */
1425 : : );
1426 : : /* clang-format on */
1427 : :
1428 : : SHELL_CMD_REGISTER(ztest, &sub_ztest_cmds, "Ztest commands", NULL);
1429 : : #endif /* CONFIG_ZTEST_SHELL */
1430 : :
1431 : 1 : int main(void)
1432 : : {
1433 : : #ifdef CONFIG_USERSPACE
1434 : : /* Partition containing globals tagged with ZTEST_DMEM and ZTEST_BMEM
1435 : : * macros. Any variables that user code may reference need to be
1436 : : * placed in this partition if no other memory domain configuration
1437 : : * is made.
1438 : : */
1439 : : k_mem_domain_add_partition(&k_mem_domain_default, &ztest_mem_partition);
1440 : : #ifdef Z_MALLOC_PARTITION_EXISTS
1441 : : /* Allow access to malloc() memory */
1442 : : k_mem_domain_add_partition(&k_mem_domain_default, &z_malloc_partition);
1443 : : #endif
1444 : : #endif /* CONFIG_USERSPACE */
1445 : :
1446 : 1 : z_init_mock();
1447 : : #ifndef CONFIG_ZTEST_SHELL
1448 : 1 : test_main();
1449 : 1 : end_report();
1450 : : log_flush();
1451 : : LOG_PANIC();
1452 : : if (IS_ENABLED(CONFIG_ZTEST_RETEST_IF_PASSED)) {
1453 : : static __noinit struct {
1454 : : uint32_t magic;
1455 : : uint32_t boots;
1456 : : } state;
1457 : : const uint32_t magic = 0x152ac523;
1458 : :
1459 : : if (state.magic != magic) {
1460 : : state.magic = magic;
1461 : : state.boots = 0;
1462 : : }
1463 : : state.boots += 1;
1464 : : if (test_status == 0) {
1465 : : PRINT_DATA("Reset board #%u to test again\n", state.boots);
1466 : : k_msleep(10);
1467 : : sys_reboot(SYS_REBOOT_COLD);
1468 : : } else {
1469 : : PRINT_DATA("Failed after %u attempts\n", state.boots);
1470 : : state.boots = 0;
1471 : : }
1472 : : }
1473 : : #ifdef CONFIG_ZTEST_NO_YIELD
1474 : : /*
1475 : : * Rather than yielding to idle thread, keep the part awake so debugger can
1476 : : * still access it, since some SOCs cannot be debugged in low power states.
1477 : : */
1478 : : uint32_t key = irq_lock();
1479 : :
1480 : : while (1) {
1481 : : ; /* Spin */
1482 : : }
1483 : : irq_unlock(key);
1484 : : #endif /* CONFIG_ZTEST_NO_YIELD */
1485 : : #endif /* CONFIG_ZTEST_SHELL */
1486 : : return 0;
1487 : : }
1488 : : #endif
|