1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "test_queue.h"
8 
9 #define TIMEOUT K_MSEC(100)
10 #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
11 #define LIST_LEN 2
12 
13 static K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
14 static struct k_thread tdata;
15 K_SEM_DEFINE(sem, 0, 1);
16 
17 /*test cases*/
18 /**
19  * @brief Test k_queue_get() failure scenario
20  * @ingroup kernel_queue_tests
21  * @see k_queue_get()
22  */
ZTEST(queue_api_1cpu,test_queue_get_fail)23 ZTEST(queue_api_1cpu, test_queue_get_fail)
24 {
25 	static struct k_queue queue;
26 
27 	k_queue_init(&queue);
28 	/**TESTPOINT: queue get returns NULL*/
29 	zassert_is_null(k_queue_get(&queue, K_NO_WAIT), NULL);
30 	zassert_is_null(k_queue_get(&queue, TIMEOUT), NULL);
31 }
32 
33 /* The sub-thread entry */
tThread_entry(void * p1,void * p2,void * p3)34 static void tThread_entry(void *p1, void *p2, void *p3)
35 {
36 	k_sem_give(&sem);
37 
38 	/* wait the queue for data */
39 	qdata_t *p = k_queue_get((struct k_queue *)p1, K_FOREVER);
40 
41 	zassert_equal(p, p2, "Failed to append a unnormal list");
42 }
43 
44 /**
45  * @brief Test k_queue_append_list() failure scenario
46  *
47  * @details According to the API k_queue_append_list to
48  * design some error condition to verify error branch of
49  * the API.
50  *	1. Verify that the list's head is empty.
51  *	2. Verify that the list's tail is empty.
52  *	3. Verify that append list to the queue when a
53  * sub-thread is waiting for data.
54  *
55  * @ingroup kernel_queue_tests
56  *
57  * @see k_queue_append_list()
58  */
ZTEST(queue_api,test_queue_append_list_error)59 ZTEST(queue_api, test_queue_append_list_error)
60 {
61 	static qdata_t data_l[2];
62 	static struct k_queue queue;
63 	static qdata_t *head = NULL, *tail = &data_l[1];
64 
65 	k_queue_init(&queue);
66 	memset(data_l, 0, sizeof(data_l));
67 
68 	/* Check if the list of head is equal to null */
69 	zassert_true(k_queue_append_list(&queue, (uint32_t *)head,
70 			(uint32_t *)tail) == -EINVAL,
71 			"failed to CHECKIF head == NULL");
72 	/* Check if the list of tail is equal to null */
73 	head = &data_l[0];
74 	tail = NULL;
75 	zassert_true(k_queue_append_list(&queue, (uint32_t *)head,
76 			(uint32_t *)tail) == -EINVAL,
77 			"failed to CHECKIF tail == NULL");
78 	/* Initializing the queue for re-using below */
79 	k_queue_init(&queue);
80 
81 	/* Append unnormal list(just one node)into the queue for sub-thread */
82 	head = &data_l[0];
83 	head->snode.next = NULL;
84 
85 	k_thread_create(&tdata, tstack, STACK_SIZE, tThread_entry, &queue,
86 			head, NULL, K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
87 	/* Delay for thread initializing */
88 	k_sem_take(&sem, K_FOREVER);
89 
90 	k_queue_append_list(&queue, (uint32_t *)head, (uint32_t *)head);
91 
92 	k_thread_join(&tdata, K_FOREVER);
93 }
94 
95 /**
96  * @brief Test k_queue_merge_slist() failure scenario
97  *
98  * @details Verify the API k_queue_merge_slist when
99  * a slist is empty or a slist's tail is null.
100  *
101  * @ingroup kernel_queue_tests
102  *
103  * @see k_queue_merge_slist()
104  */
ZTEST(queue_api,test_queue_merge_list_error)105 ZTEST(queue_api, test_queue_merge_list_error)
106 {
107 	qdata_t data_sl[2];
108 	static struct k_queue queue;
109 	sys_slist_t slist;
110 
111 	k_queue_init(&queue);
112 	sys_slist_init(&slist);
113 	memset(data_sl, 0, sizeof(data_sl));
114 
115 	/* Check if the slist is empty */
116 	zassert_true(k_queue_merge_slist(&queue, &slist) == -EINVAL,
117 			"Failed to CHECKIF slist is empty");
118 	/* Check if the tail of the slist is null */
119 	sys_slist_append(&slist, (sys_snode_t *)&(data_sl[0].snode));
120 	sys_slist_append(&slist, (sys_snode_t *)&(data_sl[1].snode));
121 	slist.tail = NULL;
122 	zassert_true(k_queue_merge_slist(&queue, &slist) != 0,
123 			"Failed to CHECKIF the tail of slist == null");
124 }
125 
126 #ifdef CONFIG_USERSPACE
127 /**
128  * @brief Test k_queue_init() failure scenario
129  *
130  * @details Verify that the parameter of API k_queue_init() is
131  * NULL, what will happen.
132  *
133  * @ingroup kernel_queue_tests
134  *
135  * @see k_queue_init()
136  */
ZTEST_USER(queue_api,test_queue_init_null)137 ZTEST_USER(queue_api, test_queue_init_null)
138 {
139 	ztest_set_fault_valid(true);
140 	k_queue_init(NULL);
141 }
142 
143 /**
144  * @brief Test k_queue_alloc_append() failure scenario
145  *
146  * @details Verify that the parameter of the API is
147  * NULL, what will happen.
148  *
149  * @ingroup kernel_queue_tests
150  *
151  * @see k_queue_alloc_append()
152  */
ZTEST_USER(queue_api,test_queue_alloc_append_null)153 ZTEST_USER(queue_api, test_queue_alloc_append_null)
154 {
155 	qdata_t data;
156 
157 	memset(&data, 0, sizeof(data));
158 	ztest_set_fault_valid(true);
159 	k_queue_alloc_append(NULL, &data);
160 }
161 
162 /**
163  * @brief Test k_queue_alloc_prepend() failure scenario
164  *
165  * @details Verify that the parameter of the API is
166  * NULL, what will happen.
167  *
168  * @ingroup kernel_queue_tests
169  *
170  * @see k_queue_alloc_prepend()
171  */
ZTEST_USER(queue_api,test_queue_alloc_prepend_null)172 ZTEST_USER(queue_api, test_queue_alloc_prepend_null)
173 {
174 	qdata_t data;
175 
176 	memset(&data, 0, sizeof(data));
177 	ztest_set_fault_valid(true);
178 	k_queue_alloc_prepend(NULL, &data);
179 }
180 
181 /**
182  * @brief Test k_queue_get() failure scenario
183  *
184  * @details Verify that the parameter of the API is
185  * NULL, what will happen.
186  *
187  * @ingroup kernel_queue_tests
188  *
189  * @see k_queue_get()
190  */
ZTEST_USER(queue_api,test_queue_get_null)191 ZTEST_USER(queue_api, test_queue_get_null)
192 {
193 	ztest_set_fault_valid(true);
194 	k_queue_get(NULL, K_FOREVER);
195 }
196 
197 /**
198  * @brief Test k_queue_is_empty() failure scenario
199  *
200  * @details Verify that the parameter of the API is
201  * NULL, what will happen.
202  *
203  * @ingroup kernel_queue_tests
204  *
205  * @see k_queue_is_empty()
206  */
ZTEST_USER(queue_api,test_queue_is_empty_null)207 ZTEST_USER(queue_api, test_queue_is_empty_null)
208 {
209 	ztest_set_fault_valid(true);
210 	k_queue_is_empty(NULL);
211 }
212 
213 /**
214  * @brief Test k_queue_peek_head() failure scenario
215  *
216  * @details Verify that the parameter of the API is
217  * NULL, what will happen.
218  *
219  * @ingroup kernel_queue_tests
220  *
221  * @see k_queue_peek_head()
222  */
ZTEST_USER(queue_api,test_queue_peek_head_null)223 ZTEST_USER(queue_api, test_queue_peek_head_null)
224 {
225 	ztest_set_fault_valid(true);
226 	k_queue_peek_head(NULL);
227 }
228 
229 /**
230  * @brief Test k_queue_peek_tail() failure scenario
231  *
232  * @details Verify that the parameter of the API is
233  * NULL, what will happen.
234  *
235  * @ingroup kernel_queue_tests
236  *
237  * @see k_queue_peek_tail()
238  */
ZTEST_USER(queue_api,test_queue_peek_tail_null)239 ZTEST_USER(queue_api, test_queue_peek_tail_null)
240 {
241 	ztest_set_fault_valid(true);
242 	k_queue_peek_tail(NULL);
243 }
244 
245 /**
246  * @brief Test k_queue_merge_slist() failure scenario
247  *
248  * @details Verify that the parameter of the API is
249  * NULL, what will happen.
250  *
251  * @ingroup kernel_queue_tests
252  *
253  * @see k_queue_merge_slist()
254  */
ZTEST_USER(queue_api,test_queue_cancel_wait_error)255 ZTEST_USER(queue_api, test_queue_cancel_wait_error)
256 {
257 	struct k_queue *q;
258 
259 	q = k_object_alloc(K_OBJ_QUEUE);
260 	zassert_not_null(q, "no memory for allocated queue object");
261 	k_queue_init(q);
262 
263 	/* Check if cancel a qeueu that no thread to wait */
264 	k_queue_cancel_wait(q);
265 
266 	/* Check if cancel a null pointer */
267 	ztest_set_fault_valid(true);
268 	k_queue_cancel_wait(NULL);
269 }
270 
271 #endif /* CONFIG_USERSPACE */
272