1 /*
2  * Copyright (c) 2021 intel, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <ztest.h>
8 #include <kernel.h>
9 #include <ztest_error_hook.h>
10 
11 static struct k_poll_signal signal_err;
12 #define STACK_SIZE (1024 + CONFIG_TEST_EXTRA_STACKSIZE)
13 
14 #ifdef CONFIG_USERSPACE
15 /**
16  * @brief Test API k_poll with error number events in user mode
17  *
18  * @details Using API k_poll with error number
19  * as parameter to check if a error will be met.
20  *
21  * @see k_poll()
22  *
23  * @ingroup kernel_poll_tests
24  */
test_k_poll_user_num_err(void)25 void test_k_poll_user_num_err(void)
26 {
27 	struct k_poll_event events;
28 
29 	k_poll(&events, -1, K_NO_WAIT);
30 }
31 
32 /**
33  * @brief Test API k_poll with error member of events in user mode
34  *
35  * @details Using API k_poll with error member
36  * as parameter to check if a error will be met.
37  *
38  * @see k_poll()
39  *
40  * @ingroup kernel_poll_tests
41  */
test_k_poll_user_mem_err(void)42 void test_k_poll_user_mem_err(void)
43 {
44 	ztest_set_fault_valid(true);
45 	k_poll(NULL, 3, K_NO_WAIT);
46 }
47 
48 /**
49  * @brief Test API k_poll with NULL sem event in user mode
50  *
51  * @details Define a poll event, and using API k_poll with NULL sem
52  * as parameter to check if a error will be met.
53  *
54  * @see k_poll()
55  *
56  * @ingroup kernel_poll_tests
57  */
test_k_poll_user_type_sem_err(void)58 void test_k_poll_user_type_sem_err(void)
59 {
60 	struct k_poll_event event[] = {
61 		K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SEM_AVAILABLE,
62 					 K_POLL_MODE_NOTIFY_ONLY,
63 					 NULL),
64 	};
65 
66 	ztest_set_fault_valid(true);
67 	k_poll(event, 1, K_NO_WAIT);
68 }
69 
70 /**
71  * @brief Test API k_poll with NULL signal event in user mode
72  *
73  * @details Define a poll, and using API k_poll with NULL signal
74  * as parameter to check if a error will be met.
75  *
76  * @see k_poll()
77  *
78  * @ingroup kernel_poll_tests
79  */
test_k_poll_user_type_signal_err(void)80 void test_k_poll_user_type_signal_err(void)
81 {
82 	struct k_poll_event event[] = {
83 		K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL,
84 					 K_POLL_MODE_NOTIFY_ONLY,
85 					 NULL),
86 	};
87 
88 	ztest_set_fault_valid(true);
89 	k_poll(event, 1, K_NO_WAIT);
90 }
91 
92 /**
93  * @brief Test API k_poll with NULL fifo event in user mode
94  *
95  * @details Define a poll, and using API k_poll with NULL fifo
96  * as parameter to check if a error will be met.
97  *
98  * @see k_poll()
99  *
100  * @ingroup kernel_poll_tests
101  */
test_k_poll_user_type_fifo_err(void)102 void test_k_poll_user_type_fifo_err(void)
103 {
104 	struct k_poll_event event[] = {
105 		K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_FIFO_DATA_AVAILABLE,
106 					 K_POLL_MODE_NOTIFY_ONLY,
107 					 NULL),
108 	};
109 
110 	ztest_set_fault_valid(true);
111 	k_poll(event, 1, K_NO_WAIT);
112 }
113 
114 /**
115  * @brief Test API k_poll with NULL message queue event in user mode
116  *
117  * @details Define a poll, and using API k_poll with NULL message queue
118  * as parameter to check if a error will be met.
119  *
120  * @see k_poll()
121  *
122  * @ingroup kernel_poll_tests
123  */
test_k_poll_user_type_msgq_err(void)124 void test_k_poll_user_type_msgq_err(void)
125 {
126 	struct k_poll_event event[] = {
127 		K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_MSGQ_DATA_AVAILABLE,
128 					 K_POLL_MODE_NOTIFY_ONLY,
129 					 NULL),
130 	};
131 
132 	ztest_set_fault_valid(true);
133 	k_poll(event, 1, K_NO_WAIT);
134 }
135 
136 
137 /**
138  * @brief Test API k_poll_signal_init with NULL in user mode
139  *
140  * @details Using API k_poll_signal_init with NULL as
141  * parameter to check if a error will be met.
142  *
143  * @see k_poll_signal_init()
144  *
145  * @ingroup kernel_poll_tests
146  */
test_poll_signal_init_null(void)147 void test_poll_signal_init_null(void)
148 {
149 	ztest_set_fault_valid(true);
150 	k_poll_signal_init(NULL);
151 }
152 
153 /**
154  * @brief Test API k_poll_signal_check with NULL object in user mode
155  *
156  * @details Using API k_poll with NULL object
157  * as parameter to check if a error will be met.
158  *
159  * @see k_poll_signal_check()
160  *
161  * @ingroup kernel_poll_tests
162  */
test_poll_signal_check_obj(void)163 void test_poll_signal_check_obj(void)
164 {
165 	unsigned int signaled;
166 	int result;
167 
168 	ztest_set_fault_valid(true);
169 	k_poll_signal_check(NULL, &signaled, &result);
170 }
171 
172 /**
173  * @brief Test API k_poll_signal_check with unread address
174  * in user mode
175  *
176  * @details Using k_poll_signal_check with
177  * unread results as parameter to check if a error
178  * will be met.
179  *
180  * @see k_poll_signal_check()
181  *
182  * @ingroup kernel_poll_tests
183  */
test_poll_signal_check_signal(void)184 void test_poll_signal_check_signal(void)
185 {
186 	unsigned int result;
187 
188 	k_poll_signal_init(&signal_err);
189 
190 	ztest_set_fault_valid(true);
191 	k_poll_signal_check(&signal_err, NULL, &result);
192 }
193 
194 
195 
196 /**
197  * @brief Test API k_poll_signal_check with unread address
198  * in user mode
199  *
200  * @details Using k_poll_signal_check with
201  * unread signaled as parameter to check if a error
202  * will be met.
203  *
204  * @see k_poll_signal_check()
205  *
206  * @ingroup kernel_poll_tests
207  */
test_poll_signal_check_result(void)208 void test_poll_signal_check_result(void)
209 {
210 	int signaled;
211 
212 	k_poll_signal_init(&signal_err);
213 
214 	ztest_set_fault_valid(true);
215 	k_poll_signal_check(&signal_err, &signaled, NULL);
216 }
217 
218 /**
219  * @brief Test API k_poll_signal_raise with unread address
220  * in user mode
221  *
222  * @details Using k_poll_signal_raise with
223  * NULL as parameter to check if a error
224  * will be met.
225  *
226  * @see k_poll_signal_raise()
227  *
228  * @ingroup kernel_poll_tests
229  */
test_poll_signal_raise_null(void)230 void test_poll_signal_raise_null(void)
231 {
232 	int result = 0;
233 
234 	ztest_set_fault_valid(true);
235 	k_poll_signal_raise(NULL, result);
236 }
237 
238 /**
239  * @brief Test API k_poll_signal_reset with unread address
240  * in user mode
241  *
242  * @details Using k_poll_signal_reset with
243  * NULL as parameter to check if a error
244  * will be met.
245  *
246  * @see k_poll_signal_reset()
247  *
248  * @ingroup kernel_poll_tests
249  */
test_poll_signal_reset_null(void)250 void test_poll_signal_reset_null(void)
251 {
252 	ztest_set_fault_valid(true);
253 	k_poll_signal_reset(NULL);
254 }
255 #endif /* CONFIG_USERSPACE */
256 
test_poll_fail_grant_access(void)257 void test_poll_fail_grant_access(void)
258 {
259 	k_thread_access_grant(k_current_get(), &signal_err);
260 }
261