1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 
8 #include <zephyr/ztest.h>
9 #include <zephyr/kernel_version.h>
10 #include <zephyr/sys/speculation.h>
11 #include <zephyr/version.h>
12 
13 /**
14  * @defgroup kernel_common_tests Common Tests
15  * @ingroup all_tests
16  * @{
17  * @}
18  *
19  */
20 
21 #ifndef CONFIG_PRINTK
ZTEST(printk,test_printk)22 ZTEST(printk, test_printk)
23 {
24 	ztest_test_skip();
25 }
26 #endif
27 
28 /**
29  * @brief Test sys_kernel_version_get() functionality
30  *
31  * @ingroup kernel_common_tests
32  *
33  * @see sys_kernel_version_get()
34  */
ZTEST(common,test_version)35 ZTEST(common, test_version)
36 {
37 	uint32_t version = sys_kernel_version_get();
38 
39 	zassert_true(SYS_KERNEL_VER_MAJOR(version) == KERNEL_VERSION_MAJOR,
40 		     "major version mismatch");
41 	zassert_true(SYS_KERNEL_VER_MINOR(version) == KERNEL_VERSION_MINOR,
42 		     "minor version mismatch");
43 	zassert_true(SYS_KERNEL_VER_PATCHLEVEL(version) == KERNEL_PATCHLEVEL,
44 		     "patchlevel version match");
45 
46 }
47 
ZTEST(common,test_bounds_check_mitigation)48 ZTEST(common, test_bounds_check_mitigation)
49 {
50 	/* Very hard to test against speculation attacks, but we can
51 	 * at least assert that logically this function does
52 	 * what it says it does.
53 	 */
54 
55 	int index = 17;
56 
57 	index = k_array_index_sanitize(index, 24);
58 	zassert_equal(index, 17, "bad index");
59 
60 #ifdef CONFIG_USERSPACE
61 	index = k_array_index_sanitize(index, 5);
62 	zassert_equal(index, 0, "bad index");
63 #endif
64 }
65 
66 extern struct k_stack eno_stack;
67 extern struct k_thread eno_thread;
68 
common_setup(void)69 void *common_setup(void)
70 {
71 #if CONFIG_USERSPACE
72 	k_thread_access_grant(k_current_get(), &eno_thread, &eno_stack);
73 #endif
74 
75 	return NULL;
76 }
77 
78 ZTEST_SUITE(common, NULL, common_setup, NULL, NULL, NULL);
79