1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 
8 #include <zephyr/ztest.h>
9 #include <zephyr/ztest_error_hook.h>
10 
11 #define TIMEOUT K_MSEC(100)
12 #define PIPE_LEN 8
13 
14 static ZTEST_DMEM unsigned char __aligned(4) data[] = "abcd1234";
15 struct k_pipe put_get_pipe;
16 
put_fail(struct k_pipe * p)17 static void put_fail(struct k_pipe *p)
18 {
19 	size_t wt_byte = 0;
20 
21 	zassert_false(k_pipe_put(p, data, PIPE_LEN, &wt_byte,
22 				 1, K_FOREVER), NULL);
23 	/**TESTPOINT: pipe put returns -EIO*/
24 	zassert_equal(k_pipe_put(p, data, PIPE_LEN, &wt_byte,
25 				 1, K_NO_WAIT), -EIO, NULL);
26 	zassert_false(wt_byte);
27 	/**TESTPOINT: pipe put returns -EAGAIN*/
28 	zassert_equal(k_pipe_put(p, data, PIPE_LEN, &wt_byte,
29 				 1, TIMEOUT), -EAGAIN, NULL);
30 	zassert_true(wt_byte < 1);
31 	zassert_equal(k_pipe_put(p, data, PIPE_LEN, &wt_byte,
32 				 PIPE_LEN + 1, TIMEOUT), -EINVAL, NULL);
33 
34 }
35 
36 /**
37  * @brief Test pipe put failure scenario
38  * @ingroup kernel_pipe_tests
39  * @see k_pipe_init(), k_pipe_put()
40  */
ZTEST(pipe_api_1cpu,test_pipe_put_fail)41 ZTEST(pipe_api_1cpu, test_pipe_put_fail)
42 {
43 	k_pipe_init(&put_get_pipe, data, PIPE_LEN);
44 
45 	put_fail(&put_get_pipe);
46 }
47 /**
48  * @brief Test pipe put by a user thread
49  * @ingroup kernel_pipe_tests
50  * @see k_pipe_put()
51  */
52 #ifdef CONFIG_USERSPACE
ZTEST_USER(pipe_api_1cpu,test_pipe_user_put_fail)53 ZTEST_USER(pipe_api_1cpu, test_pipe_user_put_fail)
54 {
55 	struct k_pipe *p = k_object_alloc(K_OBJ_PIPE);
56 
57 	zassert_true(p != NULL);
58 	zassert_false(k_pipe_alloc_init(p, PIPE_LEN));
59 	/* check the number of bytes that may be read from pipe. */
60 	zassert_equal(k_pipe_read_avail(p), 0);
61 	/* check the number of bytes that may be written to pipe.*/
62 	zassert_equal(k_pipe_write_avail(p), PIPE_LEN);
63 
64 	put_fail(p);
65 }
66 #endif
67 
get_fail(struct k_pipe * p)68 static void get_fail(struct k_pipe *p)
69 {
70 	unsigned char rx_data[PIPE_LEN];
71 	size_t rd_byte = 0;
72 
73 	/**TESTPOINT: pipe put returns -EIO*/
74 	zassert_equal(k_pipe_get(p, rx_data, PIPE_LEN, &rd_byte, 1,
75 				 K_NO_WAIT), -EIO, NULL);
76 	zassert_false(rd_byte);
77 	/**TESTPOINT: pipe put returns -EAGAIN*/
78 	zassert_equal(k_pipe_get(p, rx_data, PIPE_LEN, &rd_byte, 1,
79 				 TIMEOUT), -EAGAIN, NULL);
80 	zassert_true(rd_byte < 1);
81 	zassert_equal(k_pipe_get(p, rx_data, PIPE_LEN, &rd_byte, 1,
82 				 TIMEOUT), -EAGAIN, NULL);
83 }
84 
85 /**
86  * @brief Test pipe get failure scenario
87  * @ingroup kernel_pipe_tests
88  * @see k_pipe_init(), k_pipe_get()
89  */
ZTEST(pipe_api,test_pipe_get_fail)90 ZTEST(pipe_api, test_pipe_get_fail)
91 {
92 	k_pipe_init(&put_get_pipe, data, PIPE_LEN);
93 
94 	get_fail(&put_get_pipe);
95 }
96 
97 #ifdef CONFIG_USERSPACE
98 static unsigned char user_unreach[PIPE_LEN];
99 static size_t unreach_byte;
100 
101 /**
102  * @brief Test pipe get by a user thread
103  * @ingroup kernel_pipe_tests
104  * @see k_pipe_alloc_init()
105  */
ZTEST_USER(pipe_api,test_pipe_user_get_fail)106 ZTEST_USER(pipe_api, test_pipe_user_get_fail)
107 {
108 	struct k_pipe *p = k_object_alloc(K_OBJ_PIPE);
109 
110 	zassert_true(p != NULL);
111 	zassert_false(k_pipe_alloc_init(p, PIPE_LEN));
112 
113 	get_fail(p);
114 }
115 
116 /**
117  * @brief Test k_pipe_alloc_init() failure scenario
118  *
119  * @details See what will happen if an uninitialized
120  * k_pipe is passed to k_pipe_alloc_init().
121  *
122  * @ingroup kernel_pipe_tests
123  *
124  * @see k_pipe_alloc_init()
125  */
ZTEST_USER(pipe_api,test_pipe_alloc_not_init)126 ZTEST_USER(pipe_api, test_pipe_alloc_not_init)
127 {
128 	struct k_pipe pipe;
129 
130 	ztest_set_fault_valid(true);
131 	k_pipe_alloc_init(&pipe, PIPE_LEN);
132 }
133 
134 /**
135  * @brief Test k_pipe_get() failure scenario
136  *
137  * @details See what will happen if an uninitialized
138  * k_pipe is passed to k_pipe_get().
139  *
140  * @ingroup kernel_pipe_tests
141  *
142  * @see k_pipe_get()
143  */
ZTEST_USER(pipe_api,test_pipe_get_null)144 ZTEST_USER(pipe_api, test_pipe_get_null)
145 {
146 	unsigned char rx_data[PIPE_LEN];
147 	size_t rd_byte = 0;
148 
149 	ztest_set_fault_valid(true);
150 	k_pipe_get(NULL, rx_data, PIPE_LEN,
151 	&rd_byte, 1, TIMEOUT);
152 }
153 
154 /**
155  * @brief Test k_pipe_get() failure scenario
156  *
157  * @details See what will happen if the parameter
158  * address is accessed deny to test k_pipe_get
159  *
160  * @ingroup kernel_pipe_tests
161  *
162  * @see k_pipe_get()
163  */
ZTEST_USER(pipe_api,test_pipe_get_unreach_data)164 ZTEST_USER(pipe_api, test_pipe_get_unreach_data)
165 {
166 	struct k_pipe *p = k_object_alloc(K_OBJ_PIPE);
167 	size_t rd_byte = 0;
168 
169 	zassert_true(p != NULL);
170 	zassert_false(k_pipe_alloc_init(p, PIPE_LEN));
171 
172 	ztest_set_fault_valid(true);
173 	k_pipe_get(p, user_unreach, PIPE_LEN,
174 	&rd_byte, 1, TIMEOUT);
175 
176 }
177 
178 /**
179  * @brief Test k_pipe_get() failure scenario
180  *
181  * @details See what will happen if the parameter
182  * address is accessed deny to test k_pipe_get
183  *
184  * @ingroup kernel_pipe_tests
185  *
186  * @see k_pipe_get()
187  */
ZTEST_USER(pipe_api,test_pipe_get_unreach_size)188 ZTEST_USER(pipe_api, test_pipe_get_unreach_size)
189 {
190 	struct k_pipe *p = k_object_alloc(K_OBJ_PIPE);
191 	unsigned char rx_data[PIPE_LEN];
192 
193 	zassert_true(p != NULL);
194 	zassert_false(k_pipe_alloc_init(p, PIPE_LEN));
195 
196 	ztest_set_fault_valid(true);
197 	k_pipe_get(p, rx_data, PIPE_LEN,
198 	&unreach_byte, 1, TIMEOUT);
199 
200 }
201 
202 /**
203  * @brief Test k_pipe_put() failure scenario
204  *
205  * @details See what will happen if a null pointer
206  * is passed into the k_pipe_put as a parameter
207  *
208  * @ingroup kernel_pipe_tests
209  *
210  * @see k_pipe_put()
211  */
ZTEST_USER(pipe_api,test_pipe_put_null)212 ZTEST_USER(pipe_api, test_pipe_put_null)
213 {
214 	unsigned char tx_data = 0xa;
215 	size_t to_wt = 0, wt_byte = 0;
216 
217 	ztest_set_fault_valid(true);
218 	k_pipe_put(NULL, &tx_data, to_wt,
219 		&wt_byte, 1, TIMEOUT);
220 }
221 
222 /**
223  * @brief Test k_pipe_put() failure scenario
224  *
225  * @details See what will happen if the parameter
226  * address is accessed deny to test k_pipe_put
227  *
228  * @ingroup kernel_pipe_tests
229  *
230  * @see k_pipe_put()
231  */
ZTEST_USER(pipe_api,test_pipe_put_unreach_data)232 ZTEST_USER(pipe_api, test_pipe_put_unreach_data)
233 {
234 	struct k_pipe *p = k_object_alloc(K_OBJ_PIPE);
235 	size_t to_wt = 0, wt_byte = 0;
236 
237 	zassert_true(p != NULL);
238 	zassert_false(k_pipe_alloc_init(p, PIPE_LEN));
239 
240 	ztest_set_fault_valid(true);
241 	k_pipe_put(p, &user_unreach[0], to_wt,
242 		&wt_byte, 1, TIMEOUT);
243 
244 }
245 
246 /**
247  * @brief Test k_pipe_put() failure scenario
248  *
249  * @details See what will happen if the parameter
250  * address is accessed deny to test k_pipe_put
251  *
252  * @ingroup kernel_pipe_tests
253  *
254  * @see k_pipe_put()
255  */
ZTEST_USER(pipe_api,test_pipe_put_unreach_size)256 ZTEST_USER(pipe_api, test_pipe_put_unreach_size)
257 {
258 	struct k_pipe *p = k_object_alloc(K_OBJ_PIPE);
259 	unsigned char tx_data = 0xa;
260 	size_t to_wt = 0;
261 
262 	zassert_true(p != NULL);
263 	zassert_false(k_pipe_alloc_init(p, PIPE_LEN));
264 
265 	ztest_set_fault_valid(true);
266 	k_pipe_put(p, &tx_data, to_wt,
267 		&unreach_byte, 1, TIMEOUT);
268 }
269 
270 /**
271  * @brief Test k_pipe_read_avail() failure scenario
272  *
273  * @details See what will happen if a null pointer
274  * is passed into the k_pipe_read_avail as a parameter
275  *
276  * @ingroup kernel_pipe_tests
277  *
278  * @see k_pipe_read_avail()
279  */
ZTEST_USER(pipe_api,test_pipe_read_avail_null)280 ZTEST_USER(pipe_api, test_pipe_read_avail_null)
281 {
282 	ztest_set_fault_valid(true);
283 	k_pipe_read_avail(NULL);
284 }
285 
286 /**
287  * @brief Test k_pipe_write_avail() failure scenario
288  *
289  * @details See what will happen if a null pointer
290  * is passed into the k_pipe_write_avail as a parameter
291  *
292  * @ingroup kernel_pipe_tests
293  *
294  * @see k_pipe_write_avail()
295  */
ZTEST_USER(pipe_api,test_pipe_write_avail_null)296 ZTEST_USER(pipe_api, test_pipe_write_avail_null)
297 {
298 	ztest_set_fault_valid(true);
299 	k_pipe_write_avail(NULL);
300 }
301 #endif
302