1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 
9 #define STACK_SIZE	(1024 + CONFIG_TEST_EXTRA_STACK_SIZE)
10 #define PIPE_LEN	(4 * 16)
11 #define BYTES_TO_WRITE	16
12 #define BYTES_TO_READ BYTES_TO_WRITE
13 
14 K_HEAP_DEFINE(mpool, PIPE_LEN * 1);
15 
16 static ZTEST_DMEM unsigned char __aligned(4) data[] =
17 "abcd1234$%^&PIPEefgh5678!/?*EPIPijkl9012[]<>PEPImnop3456{}()IPEP";
18 BUILD_ASSERT(sizeof(data) >= PIPE_LEN);
19 
20 /**TESTPOINT: init via K_PIPE_DEFINE*/
21 K_PIPE_DEFINE(kpipe, PIPE_LEN, 4);
22 K_PIPE_DEFINE(khalfpipe, (PIPE_LEN / 2), 4);
23 K_PIPE_DEFINE(kpipe1, PIPE_LEN, 4);
24 K_PIPE_DEFINE(pipe_test_alloc, PIPE_LEN, 4);
25 K_PIPE_DEFINE(ksmallpipe, 10, 2);
26 struct k_pipe pipe, pipe1;
27 
28 K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
29 K_THREAD_STACK_DEFINE(tstack1, STACK_SIZE);
30 K_THREAD_STACK_DEFINE(tstack2, STACK_SIZE);
31 struct k_thread tdata;
32 struct k_thread tdata1;
33 struct k_thread tdata2;
34 K_SEM_DEFINE(end_sema, 0, 1);
35 
36 #ifdef CONFIG_64BIT
37 #define SZ 256
38 #else
39 #define SZ 128
40 #endif
41 K_HEAP_DEFINE(test_pool, SZ * 4);
42 
43 struct mem_block {
44 	void *data;
45 };
46 
tpipe_put(struct k_pipe * ppipe,k_timeout_t timeout)47 static void tpipe_put(struct k_pipe *ppipe, k_timeout_t timeout)
48 {
49 	size_t to_wt, wt_byte = 0;
50 
51 	for (int i = 0; i < PIPE_LEN; i += wt_byte) {
52 		/**TESTPOINT: pipe put*/
53 		to_wt = (PIPE_LEN - i) >= BYTES_TO_WRITE ?
54 			BYTES_TO_WRITE : (PIPE_LEN - i);
55 		zassert_false(k_pipe_put(ppipe, &data[i], to_wt,
56 					 &wt_byte, 1, timeout), NULL);
57 		zassert_true(wt_byte == to_wt || wt_byte == 1);
58 	}
59 }
60 
tpipe_get(struct k_pipe * ppipe,k_timeout_t timeout)61 static void tpipe_get(struct k_pipe *ppipe, k_timeout_t timeout)
62 {
63 	unsigned char rx_data[PIPE_LEN];
64 	size_t to_rd, rd_byte = 0;
65 
66 	/*get pipe data from "pipe_put"*/
67 	for (int i = 0; i < PIPE_LEN; i += rd_byte) {
68 		/**TESTPOINT: pipe get*/
69 		to_rd = (PIPE_LEN - i) >= BYTES_TO_READ ?
70 			BYTES_TO_READ : (PIPE_LEN - i);
71 		zassert_false(k_pipe_get(ppipe, &rx_data[i], to_rd,
72 					 &rd_byte, 1, timeout), NULL);
73 		zassert_true(rd_byte == to_rd || rd_byte == 1);
74 	}
75 	for (int i = 0; i < PIPE_LEN; i++) {
76 		zassert_equal(rx_data[i], data[i]);
77 	}
78 }
79 
tThread_entry(void * p1,void * p2,void * p3)80 static void tThread_entry(void *p1, void *p2, void *p3)
81 {
82 	tpipe_get((struct k_pipe *)p1, K_FOREVER);
83 	k_sem_give(&end_sema);
84 
85 	tpipe_put((struct k_pipe *)p1, K_NO_WAIT);
86 	k_sem_give(&end_sema);
87 }
88 
tpipe_thread_thread(struct k_pipe * ppipe)89 static void tpipe_thread_thread(struct k_pipe *ppipe)
90 {
91 	/**TESTPOINT: thread-thread data passing via pipe*/
92 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
93 				      tThread_entry, ppipe, NULL, NULL,
94 				      K_PRIO_PREEMPT(0),
95 				      K_INHERIT_PERMS | K_USER, K_NO_WAIT);
96 
97 	tpipe_put(ppipe, K_NO_WAIT);
98 	k_sem_take(&end_sema, K_FOREVER);
99 
100 	k_sem_take(&end_sema, K_FOREVER);
101 	tpipe_get(ppipe, K_FOREVER);
102 
103 	/* clear the spawned thread avoid side effect */
104 	k_thread_abort(tid);
105 }
106 
tpipe_kthread_to_kthread(struct k_pipe * ppipe)107 static void tpipe_kthread_to_kthread(struct k_pipe *ppipe)
108 {
109 	/**TESTPOINT: thread-thread data passing via pipe*/
110 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
111 				      tThread_entry, ppipe, NULL, NULL,
112 				      K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
113 
114 	tpipe_put(ppipe, K_NO_WAIT);
115 	k_sem_take(&end_sema, K_FOREVER);
116 
117 	k_sem_take(&end_sema, K_FOREVER);
118 	tpipe_get(ppipe, K_FOREVER);
119 
120 	/* clear the spawned thread avoid side effect */
121 	k_thread_abort(tid);
122 }
123 
tpipe_put_no_wait(struct k_pipe * ppipe)124 static void tpipe_put_no_wait(struct k_pipe *ppipe)
125 {
126 	size_t to_wt, wt_byte = 0;
127 
128 	for (int i = 0; i < PIPE_LEN; i += wt_byte) {
129 	/**TESTPOINT: pipe put*/
130 		to_wt = (PIPE_LEN - i) >= BYTES_TO_WRITE ?
131 			BYTES_TO_WRITE : (PIPE_LEN - i);
132 		zassert_false(k_pipe_put(ppipe, &data[i], to_wt,
133 					&wt_byte, 1, K_NO_WAIT), NULL);
134 		zassert_true(wt_byte == to_wt || wt_byte == 1);
135 	}
136 }
137 
tpipe_put_small_size(struct k_pipe * ppipe,k_timeout_t timeout)138 static void tpipe_put_small_size(struct k_pipe *ppipe, k_timeout_t timeout)
139 {
140 	size_t to_wt, wt_byte = 0;
141 
142 	for (int i = 0; i < PIPE_LEN; i += wt_byte) {
143 		/**TESTPOINT: pipe put*/
144 		to_wt = 15;
145 		zassert_false(k_pipe_put(ppipe, &data[i], to_wt, &wt_byte,
146 		1, timeout) != 0, NULL);
147 
148 	}
149 }
150 
tpipe_get_small_size(struct k_pipe * ppipe,k_timeout_t timeout)151 static void tpipe_get_small_size(struct k_pipe *ppipe, k_timeout_t timeout)
152 {
153 	unsigned char rx_data[PIPE_LEN];
154 	size_t to_rd, rd_byte = 0;
155 
156 	/*get pipe data from "pipe_put"*/
157 	for (int i = 0; i < PIPE_LEN; i += rd_byte) {
158 		/**TESTPOINT: pipe get*/
159 		to_rd = 15;
160 		zassert_false(k_pipe_get(ppipe, &rx_data[i], to_rd,
161 					 &rd_byte, 1, timeout), NULL);
162 	}
163 }
164 
165 
tpipe_get_large_size(struct k_pipe * ppipe,k_timeout_t timeout)166 static void tpipe_get_large_size(struct k_pipe *ppipe, k_timeout_t timeout)
167 {
168 	unsigned char rx_data[PIPE_LEN];
169 	size_t to_rd, rd_byte = 0;
170 
171 	/*get pipe data from "pipe_put"*/
172 	for (int i = 0; i < PIPE_LEN; i += rd_byte) {
173 		/**TESTPOINT: pipe get*/
174 		to_rd = (PIPE_LEN - i) >= 128 ?
175 			128 : (PIPE_LEN - i);
176 		zassert_false(k_pipe_get(ppipe, &rx_data[i], to_rd,
177 					 &rd_byte, 1, timeout), NULL);
178 	}
179 }
180 
181 
182 /**
183  * @brief Test Initialization and buffer allocation of pipe,
184  * with various parameters
185  * @see k_pipe_alloc_init(), k_pipe_cleanup()
186  */
ZTEST(pipe_api_1cpu,test_pipe_alloc)187 ZTEST(pipe_api_1cpu, test_pipe_alloc)
188 {
189 	int ret;
190 
191 	zassert_false(k_pipe_alloc_init(&pipe_test_alloc, PIPE_LEN));
192 
193 	tpipe_kthread_to_kthread(&pipe_test_alloc);
194 	k_pipe_cleanup(&pipe_test_alloc);
195 
196 	zassert_false(k_pipe_alloc_init(&pipe_test_alloc, 0));
197 	k_pipe_cleanup(&pipe_test_alloc);
198 
199 	ret = k_pipe_alloc_init(&pipe_test_alloc, 2048);
200 	zassert_true(ret == -ENOMEM,
201 		"resource pool max block size is not smaller then requested buffer");
202 }
203 
thread_for_get_forever(void * p1,void * p2,void * p3)204 static void thread_for_get_forever(void *p1, void *p2, void *p3)
205 {
206 	tpipe_get((struct k_pipe *)p1, K_FOREVER);
207 }
208 
ZTEST(pipe_api,test_pipe_cleanup)209 ZTEST(pipe_api, test_pipe_cleanup)
210 {
211 	int ret;
212 
213 	zassert_false(k_pipe_alloc_init(&pipe_test_alloc, PIPE_LEN));
214 
215 	/**TESTPOINT: test if a dynamically allocated buffer can be freed*/
216 	ret = k_pipe_cleanup(&pipe_test_alloc);
217 	zassert_true((ret == 0) && (pipe_test_alloc.buffer == NULL),
218 			"Failed to free buffer with k_pipe_cleanup().");
219 
220 	/**TESTPOINT: nothing to do with k_pipe_cleanup() for static buffer in pipe*/
221 	ret = k_pipe_cleanup(&kpipe);
222 	zassert_true((ret == 0) && (kpipe.buffer != NULL),
223 			"Static buffer should not be freed.");
224 
225 	zassert_false(k_pipe_alloc_init(&pipe_test_alloc, PIPE_LEN));
226 
227 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
228 			thread_for_get_forever, &pipe_test_alloc, NULL,
229 			NULL, K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
230 	k_sleep(K_MSEC(100));
231 
232 	ret = k_pipe_cleanup(&pipe_test_alloc);
233 	zassert_true(ret == -EAGAIN, "k_pipe_cleanup() should not return with 0.");
234 	k_thread_abort(tid);
235 }
236 
thread_handler(void * p1,void * p2,void * p3)237 static void thread_handler(void *p1, void *p2, void *p3)
238 {
239 	tpipe_put_no_wait((struct k_pipe *)p1);
240 	k_sem_give(&end_sema);
241 }
242 
243 /**
244  * @addtogroup kernel_pipe_tests
245  * @{
246  */
247 
248 /**
249  * @brief Test pipe data passing between threads
250  *
251  * @ingroup kernel_pipe_tests
252  *
253  * @details
254  * Test Objective:
255  * - Verify data passing with "pipe put/get" APIs between
256  * threads
257  *
258  * Testing techniques:
259  * - function and block box testing,Interface testing,
260  * Dynamic analysis and testing.
261  *
262  * Prerequisite Conditions:
263  * - CONFIG_TEST_USERSPACE.
264  *
265  * Input Specifications:
266  * - N/A
267  *
268  * Test Procedure:
269  * -# Initialize a pipe, which is defined at run time.
270  * -# Create a thread (A).
271  * -# In A thread, check if it can get data, which is sent
272  * by main thread via the pipe.
273  * -# In A thread, send data to main thread via the pipe.
274  * -# In main thread, send data to A thread  via the pipe.
275  * -# In main thread, check if it can get data, which is sent
276  * by A thread.
277  * -# Do the same testing with a pipe, which is defined at compile
278  * time
279  *
280  * Expected Test Result:
281  * - Data can be sent/received between threads.
282  *
283  * Pass/Fail Criteria:
284  * - Successful if check points in test procedure are all passed, otherwise failure.
285  *
286  * Assumptions and Constraints:
287  * - N/A
288  *
289  * @see k_pipe_init(), k_pipe_put(), #K_PIPE_DEFINE(x)
290  */
291 
ZTEST(pipe_api_1cpu,test_pipe_thread2thread)292 ZTEST(pipe_api_1cpu, test_pipe_thread2thread)
293 {
294 	/**TESTPOINT: test k_pipe_init pipe*/
295 	k_pipe_init(&pipe, data, PIPE_LEN);
296 	tpipe_thread_thread(&pipe);
297 
298 	/**TESTPOINT: test K_PIPE_DEFINE pipe*/
299 	tpipe_thread_thread(&kpipe);
300 }
301 
302 #ifdef CONFIG_USERSPACE
303 /**
304  * @brief Test data passing using pipes between user threads
305  * @see k_pipe_init(), k_pipe_put(), #K_PIPE_DEFINE(x)
306  */
ZTEST_USER(pipe_api_1cpu,test_pipe_user_thread2thread)307 ZTEST_USER(pipe_api_1cpu, test_pipe_user_thread2thread)
308 {
309 	/**TESTPOINT: test k_object_alloc pipe*/
310 	struct k_pipe *p = k_object_alloc(K_OBJ_PIPE);
311 
312 	zassert_true(p != NULL);
313 
314 	/**TESTPOINT: test k_pipe_alloc_init*/
315 	zassert_false(k_pipe_alloc_init(p, PIPE_LEN));
316 	tpipe_thread_thread(p);
317 
318 }
319 #endif
320 
321 /**
322  * @brief Test resource pool free
323  * @see k_heap_alloc()
324  */
325 #ifdef CONFIG_USERSPACE
ZTEST(pipe_api,test_resource_pool_auto_free)326 ZTEST(pipe_api, test_resource_pool_auto_free)
327 {
328 	/* Pool has 2 blocks, both should succeed if kernel object and pipe
329 	 * buffer are auto-freed when the allocating threads exit
330 	 */
331 	zassert_true(k_heap_alloc(&test_pool, 64, K_NO_WAIT) != NULL);
332 	zassert_true(k_heap_alloc(&test_pool, 64, K_NO_WAIT) != NULL);
333 }
334 #endif
335 
tThread_half_pipe_put(void * p1,void * p2,void * p3)336 static void tThread_half_pipe_put(void *p1, void *p2, void *p3)
337 {
338 	tpipe_put((struct k_pipe *)p1, K_FOREVER);
339 }
340 
tThread_half_pipe_get(void * p1,void * p2,void * p3)341 static void tThread_half_pipe_get(void *p1, void *p2, void *p3)
342 {
343 	tpipe_get((struct k_pipe *)p1, K_FOREVER);
344 }
345 
346 /**
347  * @brief Test put/get with smaller pipe buffer
348  * @see k_pipe_put(), k_pipe_get()
349  */
ZTEST(pipe_api,test_half_pipe_put_get)350 ZTEST(pipe_api, test_half_pipe_put_get)
351 {
352 	unsigned char rx_data[PIPE_LEN];
353 	size_t rd_byte = 0;
354 	int ret;
355 
356 	memset(rx_data, 0, sizeof(rx_data));
357 
358 	/* TESTPOINT: min_xfer > bytes_to_read */
359 	ret = k_pipe_put(&kpipe, &rx_data[0], 1, &rd_byte, 24, K_NO_WAIT);
360 	zassert_true(ret == -EINVAL);
361 	ret = k_pipe_put(&kpipe, &rx_data[0], 24, NULL, 1, K_NO_WAIT);
362 	zassert_true(ret == -EINVAL);
363 
364 	/**TESTPOINT: thread-thread data passing via pipe*/
365 	k_tid_t tid1 = k_thread_create(&tdata1, tstack1, STACK_SIZE,
366 				      tThread_half_pipe_get, &khalfpipe,
367 				      NULL, NULL, K_PRIO_PREEMPT(0),
368 				      K_INHERIT_PERMS | K_USER, K_NO_WAIT);
369 
370 	k_tid_t tid2 = k_thread_create(&tdata2, tstack2, STACK_SIZE,
371 				      tThread_half_pipe_get, &khalfpipe,
372 				      NULL, NULL, K_PRIO_PREEMPT(0),
373 				      K_INHERIT_PERMS | K_USER, K_NO_WAIT);
374 
375 	k_sleep(K_MSEC(100));
376 	tpipe_put_small_size(&khalfpipe, K_NO_WAIT);
377 
378 	/* clear the spawned thread avoid side effect */
379 	k_thread_abort(tid1);
380 	k_thread_abort(tid2);
381 }
382 
ZTEST(pipe_api,test_pipe_get_put)383 ZTEST(pipe_api, test_pipe_get_put)
384 {
385 	unsigned char rx_data[PIPE_LEN];
386 	size_t rd_byte = 0;
387 	int ret;
388 
389 	/* TESTPOINT: min_xfer > bytes_to_read */
390 	ret = k_pipe_get(&kpipe, &rx_data[0], 1, &rd_byte, 24, K_NO_WAIT);
391 	zassert_true(ret == -EINVAL);
392 	ret = k_pipe_get(&kpipe, &rx_data[0], 24, NULL, 1, K_NO_WAIT);
393 	zassert_true(ret == -EINVAL);
394 
395 	/**TESTPOINT: thread-thread data passing via pipe*/
396 	k_tid_t tid1 = k_thread_create(&tdata1, tstack1, STACK_SIZE,
397 				      tThread_half_pipe_put, &khalfpipe,
398 				      NULL, NULL, K_PRIO_PREEMPT(0),
399 				      K_INHERIT_PERMS | K_USER, K_NO_WAIT);
400 
401 	k_tid_t tid2 = k_thread_create(&tdata2, tstack2, STACK_SIZE,
402 				      tThread_half_pipe_put, &khalfpipe,
403 				      NULL, NULL, K_PRIO_PREEMPT(0),
404 				      K_INHERIT_PERMS | K_USER, K_NO_WAIT);
405 
406 	k_sleep(K_MSEC(100));
407 	tpipe_get_small_size(&khalfpipe, K_NO_WAIT);
408 
409 	/* clear the spawned thread avoid side effect */
410 	k_thread_abort(tid1);
411 	k_thread_abort(tid2);
412 }
413 
ZTEST(pipe_api,test_pipe_get_large)414 ZTEST(pipe_api, test_pipe_get_large)
415 {
416 	/**TESTPOINT: thread-thread data passing via pipe*/
417 	k_tid_t tid1 = k_thread_create(&tdata1, tstack1, STACK_SIZE,
418 				      tThread_half_pipe_put, &khalfpipe,
419 				      NULL, NULL, K_PRIO_PREEMPT(0),
420 				      K_INHERIT_PERMS | K_USER, K_NO_WAIT);
421 
422 	k_tid_t tid2 = k_thread_create(&tdata2, tstack2, STACK_SIZE,
423 				      tThread_half_pipe_put, &khalfpipe,
424 				      NULL, NULL, K_PRIO_PREEMPT(0),
425 				      K_INHERIT_PERMS | K_USER, K_NO_WAIT);
426 
427 	k_sleep(K_MSEC(100));
428 	tpipe_get_large_size(&khalfpipe, K_NO_WAIT);
429 
430 	/* clear the spawned thread avoid side effect */
431 	k_thread_abort(tid1);
432 	k_thread_abort(tid2);
433 }
434 
435 
436 /**
437  * @brief Test pending reader in pipe
438  * @see k_pipe_put(), k_pipe_get()
439  */
ZTEST(pipe_api,test_pipe_reader_wait)440 ZTEST(pipe_api, test_pipe_reader_wait)
441 {
442 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
443 					thread_handler, &kpipe1, NULL, NULL,
444 					K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
445 
446 	tpipe_get(&kpipe1, K_FOREVER);
447 	k_sem_take(&end_sema, K_FOREVER);
448 	k_thread_abort(tid);
449 }
450 
451 /**
452  * @}
453  */
454