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