1 /*
2 * Copyright (c) 2024 Måns Ansgariusson <mansgariusson@gmail.com>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <stdint.h>
7 #include <zephyr/kernel.h>
8 #include <zephyr/ztest.h>
9 #include <zephyr/random/random.h>
10
11 ZTEST_SUITE(k_pipe_basic, NULL, NULL, NULL, NULL, NULL);
12
mkrandom(uint8_t * buffer,size_t size)13 static void mkrandom(uint8_t *buffer, size_t size)
14 {
15 sys_rand_get(buffer, size);
16 }
17
18 K_PIPE_DEFINE(test_define, 256, 4);
19
20 static struct k_pipe pipe;
21
ZTEST(k_pipe_basic,test_init)22 ZTEST(k_pipe_basic, test_init)
23 {
24 uint8_t buffer[10];
25
26 k_pipe_init(&pipe, buffer, sizeof(buffer));
27 zassert_true(pipe.flags == PIPE_FLAG_OPEN, "Unexpected pipe flags");
28 }
29
ZTEST(k_pipe_basic,test_write_read_one)30 ZTEST(k_pipe_basic, test_write_read_one)
31 {
32 uint8_t buffer[10];
33 uint8_t data = 0x55;
34 uint8_t read_data;
35
36 k_pipe_init(&pipe, buffer, sizeof(buffer));
37 zassert_true(k_pipe_write(&pipe, &data, 1, K_NO_WAIT) == 1,
38 "Failed to write to pipe");
39 zassert_true(k_pipe_read(&pipe, &read_data, 1, K_NO_WAIT) == 1,
40 "Failed to read from pipe");
41 zassert_true(read_data == data, "Unexpected data received from pipe");
42 }
43
ZTEST(k_pipe_basic,test_write_read_multiple)44 ZTEST(k_pipe_basic, test_write_read_multiple)
45 {
46 uint8_t buffer[10];
47 uint8_t data = 0x55;
48 uint8_t read_data;
49
50 k_pipe_init(&pipe, buffer, sizeof(buffer));
51 zassert_true(k_pipe_write(&pipe, &data, 1, K_NO_WAIT) == 1, "Failed to write to pipe");
52 zassert_true(k_pipe_write(&pipe, &data, 1, K_NO_WAIT) == 1, "Failed to write to pipe");
53 zassert_true(k_pipe_read(&pipe, &read_data, 1, K_NO_WAIT) == 1, "Failed to read from pipe");
54 zassert_true(read_data == data, "Unexpected data received from pipe");
55 zassert_true(k_pipe_read(&pipe, &read_data, 1, K_NO_WAIT) == 1, "Failed to read from pipe");
56 zassert_true(read_data == data, "Unexpected data received from pipe");
57 }
58
ZTEST(k_pipe_basic,test_write_full)59 ZTEST(k_pipe_basic, test_write_full)
60 {
61 uint8_t buffer[10];
62 uint8_t data[10];
63
64 k_pipe_init(&pipe, buffer, sizeof(buffer));
65 zassert_true(k_pipe_write(&pipe, data, sizeof(data), K_NO_WAIT) == 10,
66 "Failed to write multiple bytes to pipe");
67 zassert_true(k_pipe_write(&pipe, data, sizeof(data), K_MSEC(1000)) == -EAGAIN,
68 "Should not be able to write to full pipe");
69 }
70
ZTEST(k_pipe_basic,test_read_empty)71 ZTEST(k_pipe_basic, test_read_empty)
72 {
73 uint8_t buffer[10];
74 uint8_t read_data;
75
76 k_pipe_init(&pipe, buffer, sizeof(buffer));
77 zassert_true(k_pipe_read(&pipe, &read_data, 1, K_MSEC(1000)) == -EAGAIN,
78 "Should not be able to read from empty pipe");
79 }
80
ZTEST(k_pipe_basic,test_read_write_full)81 ZTEST(k_pipe_basic, test_read_write_full)
82 {
83 uint8_t buffer[10];
84 uint8_t input[10];
85 uint8_t res[10];
86
87 mkrandom(input, sizeof(input));
88 k_pipe_init(&pipe, buffer, sizeof(buffer));
89 zassert_true(k_pipe_write(&pipe, input, sizeof(input), K_NO_WAIT) == sizeof(input),
90 "Failed to write multiple bytes to pipe");
91 zassert_true(k_pipe_read(&pipe, res, sizeof(res), K_NO_WAIT) == sizeof(res),
92 "Failed to read multiple bytes from pipe");
93 zassert_true(memcmp(input, res, sizeof(input)) == 0,
94 "Unexpected data received from pipe");
95 }
96
ZTEST(k_pipe_basic,test_read_write_wrapp_around)97 ZTEST(k_pipe_basic, test_read_write_wrapp_around)
98 {
99 uint8_t buffer[12];
100 uint8_t input[8];
101 uint8_t res[16];
102
103 mkrandom(input, sizeof(input));
104 k_pipe_init(&pipe, buffer, sizeof(buffer));
105 zassert_true(k_pipe_write(&pipe, input, sizeof(input), K_NO_WAIT) == sizeof(input),
106 "Failed to write bytes to pipe");
107 zassert_true(k_pipe_read(&pipe, res, 5, K_NO_WAIT) == 5,
108 "Failed to read bytes from pipe");
109 zassert_true(memcmp(input, res, 5) == 0, "Unexpected data received from pipe");
110
111 zassert_true(k_pipe_write(&pipe, input, sizeof(input), K_NO_WAIT) == sizeof(input),
112 "Failed to write bytes to pipe");
113 zassert_true(k_pipe_read(&pipe, res, sizeof(input) * 2 - 5, K_NO_WAIT) ==
114 sizeof(input) * 2 - 5, "Failed to read remaining bytes from pipe");
115
116 zassert_true(memcmp(&input[5], res, sizeof(input) - 5) == 0,
117 "Unexpected data received from pipe");
118 zassert_true(memcmp(input, &res[sizeof(input) - 5], sizeof(input)) == 0,
119 "Unexpected data received from pipe");
120 }
121
ZTEST(k_pipe_basic,test_reset)122 ZTEST(k_pipe_basic, test_reset)
123 {
124 uint8_t buffer[10];
125 uint8_t data = 0x55;
126 uint8_t read_data;
127
128 k_pipe_init(&pipe, buffer, sizeof(buffer));
129
130 /* reset an empty pipe, & no waiting should not produce any side-effects*/
131 k_pipe_reset(&pipe);
132 zassert_true(k_pipe_write(&pipe, &data, 1, K_NO_WAIT) == 1,
133 "Failed to write to resetted pipe");
134 zassert_true(k_pipe_read(&pipe, &read_data, 1, K_NO_WAIT) == 1,
135 "Failed to read from resetted pipe");
136 zassert_true(read_data == data, "Unexpected data received from pipe");
137 }
138
ZTEST(k_pipe_basic,test_close)139 ZTEST(k_pipe_basic, test_close)
140 {
141 uint8_t buffer[12];
142 uint8_t input[8];
143 uint8_t res[16];
144
145 mkrandom(input, sizeof(input));
146 k_pipe_init(&pipe, buffer, sizeof(buffer));
147 zassert_true(k_pipe_write(&pipe, input, sizeof(input), K_NO_WAIT) == sizeof(input),
148 "Failed to write bytes to pipe");
149 k_pipe_close(&pipe);
150
151 zassert_true(k_pipe_write(&pipe, input, sizeof(input), K_NO_WAIT) == -EPIPE,
152 "should not be able to write to closed pipe");
153 zassert_true(k_pipe_read(&pipe, res, 5, K_NO_WAIT) == 5,
154 "You should be able to read from closed pipe");
155 zassert_true(memcmp(input, res, 5) == 0, "Sequence should be equal");
156
157 zassert_true(k_pipe_read(&pipe, res, 5, K_NO_WAIT) == 3,
158 "you should be able to read remaining bytes from closed pipe");
159 zassert_true(memcmp(&input[5], res, 3) == 0, "Written and read bytes should be equal");
160 zassert_true(k_pipe_read(&pipe, res, 5, K_NO_WAIT) == -EPIPE,
161 "Closed and empty pipe should return -EPIPE");
162 }
163