1 /*
2  * Copyright (c) 2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdlib.h>
8 #include <zephyr/ztest.h>
9 #include <zephyr/types.h>
10 #include <zephyr/irq_offload.h>
11 #include <zephyr/ztest_error_hook.h>
12 
13 #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
14 #define THREAD_TEST_PRIORITY 0
15 #define TEST_TIMEOUT -20
16 #define PERIOD 50
17 #define DURATION 100
18 
19 static struct k_timer mytimer, sync_timer;
20 static struct k_thread tdata;
21 static K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
22 
thread_timer_start_null(void * p1,void * p2,void * p3)23 static void thread_timer_start_null(void *p1, void *p2, void *p3)
24 {
25 	ARG_UNUSED(p1);
26 	ARG_UNUSED(p2);
27 	ARG_UNUSED(p3);
28 
29 	ztest_set_fault_valid(true);
30 	k_timer_start(NULL, K_MSEC(DURATION), K_NO_WAIT);
31 
32 	/* should not go here*/
33 	ztest_test_fail();
34 }
35 
36 /**
37  * @brief Test k_timer_start() API
38  *
39  * @details Create a thread and set k_timer_start() input to NULL
40  * and set a duration and period.
41  *
42  * @ingroup kernel_timer_tests
43  *
44  * @see k_timer_start()
45  */
ZTEST_USER(timer_api_error,test_timer_start_null)46 ZTEST_USER(timer_api_error, test_timer_start_null)
47 {
48 #ifndef CONFIG_USERSPACE
49 	/* Skip on platforms with no userspace support */
50 	ztest_test_skip();
51 #endif
52 
53 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
54 			thread_timer_start_null,
55 			NULL, NULL, NULL,
56 			K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
57 			K_USER | K_INHERIT_PERMS, K_NO_WAIT);
58 
59 	k_thread_join(tid, K_FOREVER);
60 }
61 
thread_timer_stop_null(void * p1,void * p2,void * p3)62 static void thread_timer_stop_null(void *p1, void *p2, void *p3)
63 {
64 	ARG_UNUSED(p1);
65 	ARG_UNUSED(p2);
66 	ARG_UNUSED(p3);
67 
68 	ztest_set_fault_valid(true);
69 	k_timer_stop(NULL);
70 
71 	/* should not go here*/
72 	ztest_test_fail();
73 }
74 
75 /**
76  * @brief Test k_timer_stop() API
77  *
78  * @details Create a thread and set k_timer_stop() input to NULL
79  *
80  * @ingroup kernel_timer_tests
81  *
82  * @see k_timer_stop()
83  */
ZTEST_USER(timer_api_error,test_timer_stop_null)84 ZTEST_USER(timer_api_error, test_timer_stop_null)
85 {
86 #ifndef CONFIG_USERSPACE
87 	/* Skip on platforms with no userspace support */
88 	ztest_test_skip();
89 #endif
90 
91 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
92 			thread_timer_stop_null,
93 			NULL, NULL, NULL,
94 			K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
95 			K_USER | K_INHERIT_PERMS, K_NO_WAIT);
96 
97 	k_thread_join(tid, K_FOREVER);
98 }
99 
thread_timer_status_get_null(void * p1,void * p2,void * p3)100 static void thread_timer_status_get_null(void *p1, void *p2, void *p3)
101 {
102 	ARG_UNUSED(p1);
103 	ARG_UNUSED(p2);
104 	ARG_UNUSED(p3);
105 
106 	ztest_set_fault_valid(true);
107 	k_timer_status_get(NULL);
108 
109 	/* should not go here*/
110 	ztest_test_fail();
111 }
112 
113 /**
114  * @brief Test k_timer_status_get() API
115  *
116  * @details Create a thread and set k_timer_status_get() input to NULL
117  *
118  * @ingroup kernel_timer_tests
119  *
120  * @see k_timer_status_get()
121  */
ZTEST_USER(timer_api_error,test_timer_status_get_null)122 ZTEST_USER(timer_api_error, test_timer_status_get_null)
123 {
124 #ifndef CONFIG_USERSPACE
125 	/* Skip on platforms with no userspace support */
126 	ztest_test_skip();
127 #endif
128 
129 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
130 			thread_timer_status_get_null,
131 			NULL, NULL, NULL,
132 			K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
133 			K_USER | K_INHERIT_PERMS, K_NO_WAIT);
134 
135 	k_thread_join(tid, K_FOREVER);
136 }
137 
thread_timer_status_sync_null(void * p1,void * p2,void * p3)138 static void thread_timer_status_sync_null(void *p1, void *p2, void *p3)
139 {
140 	ARG_UNUSED(p1);
141 	ARG_UNUSED(p2);
142 	ARG_UNUSED(p3);
143 
144 	ztest_set_fault_valid(true);
145 	k_timer_status_sync(NULL);
146 
147 	/* should not go here*/
148 	ztest_test_fail();
149 }
150 
151 /**
152  * @brief Test k_timer_status_sync() API
153  *
154  * @details Create a thread and set k_timer_status_sync() input to NULL
155  *
156  * @ingroup kernel_timer_tests
157  *
158  * @see k_timer_status_sync()
159  */
ZTEST_USER(timer_api_error,test_timer_status_sync_null)160 ZTEST_USER(timer_api_error, test_timer_status_sync_null)
161 {
162 #ifndef CONFIG_USERSPACE
163 	/* Skip on platforms with no userspace support */
164 	ztest_test_skip();
165 #endif
166 
167 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
168 			thread_timer_status_sync_null,
169 			NULL, NULL, NULL,
170 			K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
171 			K_USER | K_INHERIT_PERMS, K_NO_WAIT);
172 
173 	k_thread_join(tid, K_FOREVER);
174 }
175 
thread_timer_remaining_ticks_null(void * p1,void * p2,void * p3)176 static void thread_timer_remaining_ticks_null(void *p1, void *p2, void *p3)
177 {
178 	ARG_UNUSED(p1);
179 	ARG_UNUSED(p2);
180 	ARG_UNUSED(p3);
181 
182 	ztest_set_fault_valid(true);
183 	k_timer_remaining_ticks(NULL);
184 
185 	/* should not go here*/
186 	ztest_test_fail();
187 }
188 
189 /**
190  * @brief Test k_timer_remaining_ticks() API
191  *
192  * @details Create a thread and set k_timer_remaining_ticks() input to NULL
193  *
194  * @ingroup kernel_timer_tests
195  *
196  * @see k_timer_remaining_ticks()
197  */
ZTEST_USER(timer_api_error,test_timer_remaining_ticks_null)198 ZTEST_USER(timer_api_error, test_timer_remaining_ticks_null)
199 {
200 #ifndef CONFIG_USERSPACE
201 	/* Skip on platforms with no userspace support */
202 	ztest_test_skip();
203 #endif
204 
205 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
206 			thread_timer_remaining_ticks_null,
207 			NULL, NULL, NULL,
208 			K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
209 			K_USER | K_INHERIT_PERMS, K_NO_WAIT);
210 
211 	k_thread_join(tid, K_FOREVER);
212 }
213 
thread_timer_expires_ticks_null(void * p1,void * p2,void * p3)214 static void thread_timer_expires_ticks_null(void *p1, void *p2, void *p3)
215 {
216 	ARG_UNUSED(p1);
217 	ARG_UNUSED(p2);
218 	ARG_UNUSED(p3);
219 
220 	ztest_set_fault_valid(true);
221 	k_timer_expires_ticks(NULL);
222 
223 	/* should not go here*/
224 	ztest_test_fail();
225 }
226 
227 /**
228  * @brief Test k_timer_expires_ticks() API
229  *
230  * @details Create a thread and set k_timer_expires_ticks() input to NULL
231  *
232  * @ingroup kernel_timer_tests
233  *
234  * @see k_timer_expires_ticks()
235  */
ZTEST_USER(timer_api_error,test_timer_expires_ticks_null)236 ZTEST_USER(timer_api_error, test_timer_expires_ticks_null)
237 {
238 #ifndef CONFIG_USERSPACE
239 	/* Skip on platforms with no userspace support */
240 	ztest_test_skip();
241 #endif
242 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
243 			thread_timer_expires_ticks_null,
244 			NULL, NULL, NULL,
245 			K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
246 			K_USER | K_INHERIT_PERMS, K_NO_WAIT);
247 
248 	k_thread_join(tid, K_FOREVER);
249 }
250 
thread_timer_user_data_get_null(void * p1,void * p2,void * p3)251 static void thread_timer_user_data_get_null(void *p1, void *p2, void *p3)
252 {
253 	ARG_UNUSED(p1);
254 	ARG_UNUSED(p2);
255 	ARG_UNUSED(p3);
256 
257 	ztest_set_fault_valid(true);
258 	k_timer_user_data_get(NULL);
259 
260 	/* should not go here*/
261 	ztest_test_fail();
262 }
263 
264 /**
265  * @brief Test k_timer_user_data_get() API
266  *
267  * @details Create a thread and set k_timer_user_data_get() input to NULL
268  *
269  * @ingroup kernel_timer_tests
270  *
271  * @see k_timer_user_data_get()
272  */
ZTEST_USER(timer_api_error,test_timer_user_data_get_null)273 ZTEST_USER(timer_api_error, test_timer_user_data_get_null)
274 {
275 #ifndef CONFIG_USERSPACE
276 	/* Skip on platforms with no userspace support */
277 	ztest_test_skip();
278 #endif
279 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
280 			thread_timer_user_data_get_null,
281 			NULL, NULL, NULL,
282 			K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
283 			K_USER | K_INHERIT_PERMS, K_NO_WAIT);
284 
285 	k_thread_join(tid, K_FOREVER);
286 }
287 
thread_timer_user_data_set_null(void * p1,void * p2,void * p3)288 static void thread_timer_user_data_set_null(void *p1, void *p2, void *p3)
289 {
290 	ARG_UNUSED(p1);
291 	ARG_UNUSED(p2);
292 	ARG_UNUSED(p3);
293 
294 	int user_data = 1;
295 
296 	ztest_set_fault_valid(true);
297 	k_timer_user_data_set(NULL, &user_data);
298 
299 	/* should not go here*/
300 	ztest_test_fail();
301 }
302 
303 /**
304  * @brief Test k_timer_user_data_set() API
305  *
306  * @details Create a thread and set k_timer_user_data_set() input to NULL
307  *
308  * @ingroup kernel_timer_tests
309  *
310  * @see k_timer_user_data_set()
311  */
ZTEST_USER(timer_api_error,test_timer_user_data_set_null)312 ZTEST_USER(timer_api_error, test_timer_user_data_set_null)
313 {
314 #ifndef CONFIG_USERSPACE
315 	/* Skip on platforms with no userspace support */
316 	ztest_test_skip();
317 #endif
318 
319 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
320 			thread_timer_user_data_set_null,
321 			NULL, NULL, NULL,
322 			K_PRIO_PREEMPT(THREAD_TEST_PRIORITY),
323 			K_USER | K_INHERIT_PERMS, K_NO_WAIT);
324 
325 	k_thread_join(tid, K_FOREVER);
326 }
327 
328 extern void z_add_timeout(struct _timeout *to, _timeout_func_t fn,
329 			  k_timeout_t timeout);
test_timer_handle(struct _timeout * t)330 static void test_timer_handle(struct _timeout *t)
331 {
332 	/**do nothing here**/
333 }
334 
ZTEST_USER(timer_api_error,test_timer_add_timeout)335 ZTEST_USER(timer_api_error, test_timer_add_timeout)
336 {
337 	struct _timeout tm;
338 	k_timeout_t timeout = K_FOREVER;
339 
340 	z_add_timeout(&tm, test_timer_handle, timeout);
341 	ztest_test_pass();
342 }
343 
344 /**
345  * @brief Tests for the Timer kernel object
346  * @defgroup kernel_timer_tests Timer
347  * @ingroup all_tests
348  * @{
349  * @}
350  */
351 
setup_timer_error_test(void)352 void *setup_timer_error_test(void)
353 {
354 	k_thread_access_grant(k_current_get(), &tdata, &tstack, &mytimer, &sync_timer);
355 
356 	return NULL;
357 }
358 
359 ZTEST_SUITE(timer_api_error, NULL, setup_timer_error_test, NULL, NULL, NULL);
360