1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/net/buf.h>
9 #include <zephyr/bluetooth/mesh.h>
10 #include <zephyr/random/random.h>
11 
12 #include "net.h"
13 #include "access.h"
14 #include "delayable_msg.h"
15 
16 #define SRC_ADDR 0x0002
17 #define RX_ADDR  0xc000
18 
19 static void start_cb(uint16_t duration, int err, void *cb_data);
20 
21 struct bt_mesh_net bt_mesh;
22 
23 static struct bt_mesh_msg_ctx gctx = {.net_idx = 0,
24 				      .app_idx = 0,
25 				      .addr = 0,
26 				      .recv_dst = RX_ADDR,
27 				      .uuid = NULL,
28 				      .recv_rssi = 0,
29 				      .recv_ttl = 0x05,
30 				      .send_rel = false,
31 				      .rnd_delay = true,
32 				      .send_ttl = 0x06};
33 
34 static struct bt_mesh_send_cb send_cb = {
35 	.start = start_cb,
36 };
37 
38 static bool is_fake_random;
39 static bool check_expectations;
40 static bool accum_mask;
41 static bool do_not_call_cb;
42 static uint16_t fake_random;
43 static uint8_t *buf_data;
44 static size_t buf_data_size;
45 static uint16_t id_mask;
46 static int cb_err_status;
47 
48 K_SEM_DEFINE(delayed_msg_sent, 0, 1);
49 
50 /**** Mocked functions ****/
bt_mesh_access_send(struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf,uint16_t src_addr,const struct bt_mesh_send_cb * cb,void * cb_data)51 int bt_mesh_access_send(struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf, uint16_t src_addr,
52 			const struct bt_mesh_send_cb *cb, void *cb_data)
53 {
54 	if (check_expectations) {
55 		gctx.rnd_delay = false;
56 		ztest_check_expected_data(ctx, sizeof(struct bt_mesh_msg_ctx));
57 		gctx.rnd_delay = true;
58 		ztest_check_expected_value(src_addr);
59 		ztest_check_expected_data(cb, sizeof(struct bt_mesh_send_cb));
60 		ztest_check_expected_data(cb_data, sizeof(uint32_t));
61 		zexpect_mem_equal(buf->data, buf_data, buf_data_size, "Buffer data corrupted");
62 	}
63 
64 	if (cb && !do_not_call_cb) {
65 		cb->start(0x0, cb_err_status, cb_data);
66 	}
67 
68 	return cb_err_status;
69 }
70 
bt_rand(void * buf,size_t len)71 int bt_rand(void *buf, size_t len)
72 {
73 	if (is_fake_random) {
74 		*(uint16_t *)buf = fake_random;
75 	} else {
76 		sys_rand_get(buf, len);
77 	}
78 	return 0;
79 }
80 /**** Mocked functions ****/
81 
start_cb(uint16_t duration,int err,void * cb_data)82 static void start_cb(uint16_t duration, int err, void *cb_data)
83 {
84 
85 	zassert_equal(err, cb_err_status, "err: %d, cb_err_status: %d", err, cb_err_status);
86 
87 	if (accum_mask) {
88 		id_mask |= 1 << *(uint16_t *)cb_data;
89 	} else {
90 		k_sem_give(&delayed_msg_sent);
91 	}
92 }
93 
set_expectation(struct net_buf_simple * buf,uint32_t * buf_id)94 static void set_expectation(struct net_buf_simple *buf, uint32_t *buf_id)
95 {
96 	ztest_expect_data(bt_mesh_access_send, ctx, &gctx);
97 	ztest_expect_value(bt_mesh_access_send, src_addr, SRC_ADDR);
98 	ztest_expect_data(bt_mesh_access_send, cb, &send_cb);
99 	ztest_expect_data(bt_mesh_access_send, cb_data, buf_id);
100 	buf_data = buf->__buf;
101 	buf_data_size = buf->size;
102 	check_expectations = true;
103 }
104 
tc_setup(void * fixture)105 static void tc_setup(void *fixture)
106 {
107 	is_fake_random = false;
108 	check_expectations = false;
109 	accum_mask = false;
110 	id_mask = 0;
111 	do_not_call_cb = false;
112 	cb_err_status = 0;
113 	k_sem_reset(&delayed_msg_sent);
114 	bt_mesh_delayable_msg_init();
115 }
116 
tc_teardown(void * fixture)117 static void tc_teardown(void *fixture)
118 {
119 	zassert_equal(gctx.net_idx, 0);
120 	zassert_equal(gctx.app_idx, 0);
121 	zassert_equal(gctx.addr, 0);
122 	zassert_equal(gctx.recv_dst, RX_ADDR);
123 	zassert_is_null(gctx.uuid);
124 	zassert_equal(gctx.recv_rssi, 0);
125 	zassert_equal(gctx.recv_ttl, 0x05);
126 	zassert_false(gctx.send_rel);
127 	zassert_true(gctx.rnd_delay);
128 	zassert_equal(gctx.send_ttl, 0x06);
129 }
130 
131 ZTEST_SUITE(bt_mesh_delayable_msg, NULL, NULL, tc_setup, tc_teardown, NULL);
132 
133 /* Simple single message sending with full size.
134  */
ZTEST(bt_mesh_delayable_msg,test_single_sending)135 ZTEST(bt_mesh_delayable_msg, test_single_sending)
136 {
137 	uint32_t buf_id = 0x55aa55aa;
138 	uint8_t *payload;
139 
140 	NET_BUF_SIMPLE_DEFINE(buf, BT_MESH_TX_SDU_MAX);
141 
142 	payload = net_buf_simple_add(&buf, BT_MESH_TX_SDU_MAX);
143 	for (int i = 0; i < BT_MESH_TX_SDU_MAX; i++) {
144 		payload[i] = i;
145 	}
146 
147 	set_expectation(&buf, &buf_id);
148 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf, SRC_ADDR, &send_cb, &buf_id));
149 
150 	zassert_ok(k_sem_take(&delayed_msg_sent, K_SECONDS(1)),
151 		   "Delayed message has not been sent.");
152 }
153 
154 /* The test checks that the delayed message mechanism sorts
155  * the incoming messages according to the transmission start timestamp.
156  */
ZTEST(bt_mesh_delayable_msg,test_self_sorting)157 ZTEST(bt_mesh_delayable_msg, test_self_sorting)
158 {
159 	uint8_t tx_data[20];
160 	uint32_t buf1_id = 1;
161 	uint32_t buf2_id = 2;
162 	uint32_t buf3_id = 3;
163 	uint32_t buf4_id = 4;
164 
165 	NET_BUF_SIMPLE_DEFINE(buf1, 20);
166 	NET_BUF_SIMPLE_DEFINE(buf2, 20);
167 	NET_BUF_SIMPLE_DEFINE(buf3, 20);
168 	NET_BUF_SIMPLE_DEFINE(buf4, 20);
169 
170 	memset(tx_data, 1, 20);
171 	memcpy(net_buf_simple_add(&buf1, 20), tx_data, 20);
172 	memset(tx_data, 2, 20);
173 	memcpy(net_buf_simple_add(&buf2, 20), tx_data, 20);
174 	memset(tx_data, 3, 20);
175 	memcpy(net_buf_simple_add(&buf3, 20), tx_data, 20);
176 	memset(tx_data, 4, 20);
177 	memcpy(net_buf_simple_add(&buf4, 20), tx_data, 20);
178 
179 	is_fake_random = true;
180 	fake_random = 30;
181 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf1, SRC_ADDR, &send_cb, &buf1_id));
182 	fake_random = 10;
183 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf2, SRC_ADDR, &send_cb, &buf2_id));
184 	fake_random = 20;
185 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf3, SRC_ADDR, &send_cb, &buf3_id));
186 	fake_random = 40;
187 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf4, SRC_ADDR, &send_cb, &buf4_id));
188 
189 	set_expectation(&buf2, &buf2_id);
190 	zassert_ok(k_sem_take(&delayed_msg_sent, K_MSEC(100)),
191 		   "Delayed message has not been sent.");
192 	set_expectation(&buf3, &buf3_id);
193 	zassert_ok(k_sem_take(&delayed_msg_sent, K_MSEC(100)),
194 		   "Delayed message has not been sent.");
195 	set_expectation(&buf1, &buf1_id);
196 	zassert_ok(k_sem_take(&delayed_msg_sent, K_MSEC(100)),
197 		   "Delayed message has not been sent.");
198 	set_expectation(&buf4, &buf4_id);
199 	zassert_ok(k_sem_take(&delayed_msg_sent, K_MSEC(100)),
200 		   "Delayed message has not been sent.");
201 }
202 
203 /* The test checks that the delayed msg mechanism can allocate new context
204  * if all contexts are in use by sending the message, that is the closest to
205  * the tx time.
206  */
ZTEST(bt_mesh_delayable_msg,test_ctx_reallocation)207 ZTEST(bt_mesh_delayable_msg, test_ctx_reallocation)
208 {
209 	uint8_t tx_data[20];
210 	uint32_t buf_id1 = 0;
211 	uint32_t buf_id2 = 1;
212 	uint32_t buf_id3 = 2;
213 	uint32_t buf_id4 = 3;
214 	uint32_t buf_id5 = 4;
215 
216 	NET_BUF_SIMPLE_DEFINE(buf, 20);
217 
218 	memset(tx_data, 1, 20);
219 	memcpy(net_buf_simple_add(&buf, 20), tx_data, 20);
220 
221 	accum_mask = true;
222 	is_fake_random = true;
223 	fake_random = 10;
224 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf, SRC_ADDR, &send_cb, &buf_id1));
225 	fake_random = 30;
226 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf, SRC_ADDR, &send_cb, &buf_id2));
227 	fake_random = 20;
228 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf, SRC_ADDR, &send_cb, &buf_id3));
229 	fake_random = 40;
230 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf, SRC_ADDR, &send_cb, &buf_id4));
231 	fake_random = 40;
232 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf, SRC_ADDR, &send_cb, &buf_id5));
233 	zassert_equal(id_mask, 0x0001, "Delayed message context reallocation was broken");
234 	k_sleep(K_MSEC(500));
235 	zassert_equal(id_mask, 0x001F);
236 }
237 
238 /* The test checks that the delayed msg mechanism can allocate new chunks
239  * if all chunks are in use by sending the other messages.
240  */
ZTEST(bt_mesh_delayable_msg,test_chunk_reallocation)241 ZTEST(bt_mesh_delayable_msg, test_chunk_reallocation)
242 {
243 	uint8_t tx_data[BT_MESH_TX_SDU_MAX];
244 	uint32_t buf_id1 = 0;
245 	uint32_t buf_id2 = 1;
246 	uint32_t buf_id3 = 2;
247 	uint32_t buf_id4 = 3;
248 
249 	NET_BUF_SIMPLE_DEFINE(buf1, 20);
250 	NET_BUF_SIMPLE_DEFINE(buf2, BT_MESH_TX_SDU_MAX);
251 
252 	memset(tx_data, 1, BT_MESH_TX_SDU_MAX);
253 	memcpy(net_buf_simple_add(&buf1, 20), tx_data, 20);
254 	memcpy(net_buf_simple_add(&buf2, BT_MESH_TX_SDU_MAX), tx_data, BT_MESH_TX_SDU_MAX);
255 
256 	accum_mask = true;
257 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf1, SRC_ADDR, &send_cb, &buf_id1));
258 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf1, SRC_ADDR, &send_cb, &buf_id2));
259 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf1, SRC_ADDR, &send_cb, &buf_id3));
260 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf2, SRC_ADDR, &send_cb, &buf_id4));
261 	zassert_equal(id_mask, 0x0007, "Delayed message chunks reallocation was broken");
262 	k_sleep(K_MSEC(500));
263 	zassert_equal(id_mask, 0x000F);
264 }
265 
266 /* The test checks that the delayed msg mechanism can reschedule access messages
267  * transport layes doesn't have enough memory or buffers at the moment.
268  * Also it checks that the delayed msg mechanism can handle the other transport
269  * layer errors without rescheduling appropriate access messages.
270  */
ZTEST(bt_mesh_delayable_msg,test_cb_error_status)271 ZTEST(bt_mesh_delayable_msg, test_cb_error_status)
272 {
273 	uint32_t buf_id = 0x55aa55aa;
274 	uint8_t tx_data[BT_MESH_TX_SDU_MAX];
275 
276 	NET_BUF_SIMPLE_DEFINE(buf1, 20);
277 	NET_BUF_SIMPLE_DEFINE(buf2, 20);
278 	NET_BUF_SIMPLE_DEFINE(buf3, 20);
279 
280 	memset(tx_data, 1, BT_MESH_TX_SDU_MAX);
281 	memcpy(net_buf_simple_add(&buf1, 20), tx_data, 20);
282 	memcpy(net_buf_simple_add(&buf2, 20), tx_data, 20);
283 	memcpy(net_buf_simple_add(&buf3, 20), tx_data, 20);
284 
285 	cb_err_status = -ENOBUFS;
286 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf1, SRC_ADDR, &send_cb, &buf_id));
287 	zassert_ok(k_sem_take(&delayed_msg_sent, K_SECONDS(1)),
288 		   "Delayed message has not been handled.");
289 	cb_err_status = 0;
290 	zassert_ok(k_sem_take(&delayed_msg_sent, K_SECONDS(1)),
291 		   "Delayed message has not been sent.");
292 
293 	cb_err_status = -EBUSY;
294 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf2, SRC_ADDR, &send_cb, &buf_id));
295 	zassert_ok(k_sem_take(&delayed_msg_sent, K_SECONDS(1)),
296 		   "Delayed message has not been handled.");
297 	cb_err_status = 0;
298 	zassert_ok(k_sem_take(&delayed_msg_sent, K_SECONDS(1)),
299 		   "Delayed message has not been sent.");
300 
301 	cb_err_status = -EINVAL;
302 	do_not_call_cb = true;
303 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf3, SRC_ADDR, &send_cb, &buf_id));
304 	zassert_ok(k_sem_take(&delayed_msg_sent, K_SECONDS(1)),
305 		   "Delayed message has not been handled.");
306 	cb_err_status = 0;
307 	zexpect_not_ok(k_sem_take(&delayed_msg_sent, K_SECONDS(1)),
308 		       "Delayed message has not been handled.");
309 }
310 
311 /* The test checks that the delayed msg mechanism raises
312  * the model message callback with the appropriate error code after
313  * stopping the functionality.
314  */
ZTEST(bt_mesh_delayable_msg,test_stop_handler)315 ZTEST(bt_mesh_delayable_msg, test_stop_handler)
316 {
317 	uint8_t tx_data[20];
318 	uint32_t buf_id1 = 0;
319 	uint32_t buf_id2 = 1;
320 	uint32_t buf_id3 = 2;
321 	uint32_t buf_id4 = 3;
322 
323 	NET_BUF_SIMPLE_DEFINE(buf, 20);
324 
325 	memset(tx_data, 1, 20);
326 	memcpy(net_buf_simple_add(&buf, 20), tx_data, 20);
327 
328 	accum_mask = true;
329 	cb_err_status = -ENODEV;
330 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf, SRC_ADDR, &send_cb, &buf_id1));
331 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf, SRC_ADDR, &send_cb, &buf_id2));
332 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf, SRC_ADDR, &send_cb, &buf_id3));
333 	zexpect_ok(bt_mesh_delayable_msg_manage(&gctx, &buf, SRC_ADDR, &send_cb, &buf_id4));
334 	bt_mesh_delayable_msg_stop();
335 	zexpect_not_ok(k_sem_take(&delayed_msg_sent, K_SECONDS(1)),
336 		       "Delayed message has been sent after stopping.");
337 	zassert_equal(id_mask, 0x000F, "Not all scheduled messages were handled after stopping");
338 }
339