1 /*
2  * Copyright (c) 2021 Nordic Semiconductor
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include "mesh_test.h"
7 #include "mesh/net.h"
8 #include "mesh/transport.h"
9 #include "mesh/va.h"
10 #include <zephyr/sys/byteorder.h>
11 
12 #include "mesh/crypto.h"
13 
14 #define LOG_MODULE_NAME test_transport
15 
16 #include <zephyr/logging/log.h>
17 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
18 
19 #include "common/bt_str.h"
20 
21 /*
22  * Transport layer tests:
23  *   This file contains tests for sending and receiving messages end-to-end in
24  *   all permutations. Covers:
25  *   - Address resolution
26  *   - Segmented messages
27  *     - Single segment
28  *     - Max length
29  *     - Groups
30  *   - Virtual addresses
31  *   - Loopback
32  *
33  *   Tests are divided into senders and receivers.
34  */
35 
assert_post_action(const char * file,unsigned int line)36 void assert_post_action(const char *file, unsigned int line)
37 {
38 	FAIL("Asserted at %s:%u", file, line);
39 }
40 
41 #define GROUP_ADDR 0xc000
42 #define WAIT_TIME 70 /*seconds*/
43 #define SYNC_CHAN 0
44 #define CLI_DEV 0
45 #define SRV1_DEV 1
46 
47 extern enum bst_result_t bst_result;
48 
49 static const struct bt_mesh_test_cfg tx_cfg = {
50 	.addr = 0x0001,
51 	.dev_key = { 0x01 },
52 };
53 static const struct bt_mesh_test_cfg rx_cfg = {
54 	.addr = 0x0002,
55 	.dev_key = { 0x02 },
56 };
57 
58 static int expected_send_err;
59 
60 static uint8_t test_va_col_uuid[][16] = {
61 	{
62 		0xe3, 0x94, 0xe7, 0xc1, 0xc5, 0x14, 0x72, 0x11,
63 		0x68, 0x36, 0x19, 0x30, 0x99, 0x34, 0x53, 0x62
64 	},
65 	{
66 		0x5e, 0x49, 0x5a, 0xd9, 0x44, 0xdf, 0xae, 0xc0,
67 		0x62, 0xd8, 0x0d, 0xed, 0x16, 0x82, 0xd1, 0x7d
68 	},
69 };
70 static const uint16_t test_va_col_addr = 0x809D;
71 
test_tx_init(void)72 static void test_tx_init(void)
73 {
74 	bt_mesh_test_cfg_set(&tx_cfg, WAIT_TIME);
75 }
76 
test_rx_init(void)77 static void test_rx_init(void)
78 {
79 	bt_mesh_test_cfg_set(&rx_cfg, WAIT_TIME);
80 }
81 
async_send_end(int err,void * data)82 static void async_send_end(int err, void *data)
83 {
84 	struct k_sem *sem = data;
85 
86 	if (err != expected_send_err) {
87 		FAIL("Async send failed: got %d, expected", err, expected_send_err);
88 	}
89 
90 	if (sem) {
91 		k_sem_give(sem);
92 	}
93 }
94 
rx_sar_conf(void)95 static void rx_sar_conf(void)
96 {
97 	/* Reconfigure SAR Receiver state so that the transport layer does
98 	 * generate Segmented Acks as rarely as possible.
99 	 */
100 	struct bt_mesh_sar_rx rx_set = {
101 		.seg_thresh = 0x1f,
102 		.ack_delay_inc = CONFIG_BT_MESH_SAR_RX_ACK_DELAY_INC,
103 		.discard_timeout = CONFIG_BT_MESH_SAR_RX_DISCARD_TIMEOUT,
104 		.rx_seg_int_step = CONFIG_BT_MESH_SAR_RX_SEG_INT_STEP,
105 		.ack_retrans_count = CONFIG_BT_MESH_SAR_RX_ACK_RETRANS_COUNT,
106 	};
107 
108 #if defined(CONFIG_BT_MESH_SAR_CFG)
109 	bt_mesh_test_sar_conf_set(NULL, &rx_set);
110 #else
111 	bt_mesh.sar_rx = rx_set;
112 #endif
113 }
114 
115 static const struct bt_mesh_send_cb async_send_cb = {
116 	.end = async_send_end,
117 };
118 
119 /** Test vector containing various permutations of transport messages. */
120 static const struct {
121 	uint16_t len;
122 	enum bt_mesh_test_send_flags flags;
123 } test_vector[] = {
124 	{.len = 1, .flags = 0},
125 	{.len = 1, .flags = FORCE_SEGMENTATION},
126 	{.len = BT_MESH_APP_SEG_SDU_MAX, .flags = 0},
127 	{.len = BT_MESH_APP_SEG_SDU_MAX, .flags = FORCE_SEGMENTATION},
128 
129 	/* segmented */
130 	{.len = BT_MESH_APP_SEG_SDU_MAX + 1, .flags = 0},
131 	{.len = 256, .flags = LONG_MIC},
132 	{.len = BT_MESH_TX_SDU_MAX - BT_MESH_MIC_SHORT, .flags = 0},
133 };
134 
135 /** Test sending of unicast messages using the test vector.
136  */
test_tx_unicast(void)137 static void test_tx_unicast(void)
138 {
139 	int err;
140 
141 	bt_mesh_test_setup();
142 
143 	for (int i = 0; i < ARRAY_SIZE(test_vector); i++) {
144 		err = bt_mesh_test_send(rx_cfg.addr, NULL, test_vector[i].len,
145 					test_vector[i].flags, K_SECONDS(10));
146 		ASSERT_OK_MSG(err, "Failed sending vector %d", i);
147 	}
148 
149 	PASS();
150 }
151 
152 /** Test sending of group messages using the test vector.
153  */
test_tx_group(void)154 static void test_tx_group(void)
155 {
156 	int err;
157 
158 	bt_mesh_test_setup();
159 
160 	for (int i = 0; i < ARRAY_SIZE(test_vector); i++) {
161 		err = bt_mesh_test_send(GROUP_ADDR, NULL, test_vector[i].len,
162 					test_vector[i].flags, K_SECONDS(20));
163 		ASSERT_OK_MSG(err, "Failed sending vector %d", i);
164 	}
165 
166 	PASS();
167 }
168 
169 /** Test sending to a fixed group address. */
test_tx_fixed(void)170 static void test_tx_fixed(void)
171 {
172 	bt_mesh_test_setup();
173 
174 	ASSERT_OK(bt_mesh_test_send(BT_MESH_ADDR_RELAYS, NULL, test_vector[0].len,
175 				    test_vector[0].flags, K_SECONDS(2)));
176 
177 	k_sleep(K_SECONDS(2));
178 
179 	ASSERT_OK(bt_mesh_test_send(BT_MESH_ADDR_RELAYS, NULL, test_vector[0].len,
180 				    test_vector[0].flags, K_SECONDS(2)));
181 
182 	k_sleep(K_SECONDS(2));
183 
184 	ASSERT_OK(bt_mesh_test_send(BT_MESH_ADDR_RELAYS, NULL, test_vector[0].len,
185 				    test_vector[0].flags, K_SECONDS(2)));
186 
187 	PASS();
188 }
189 
190 /** Test sending of virtual address messages using the test vector.
191  */
test_tx_va(void)192 static void test_tx_va(void)
193 {
194 	const struct bt_mesh_va *va;
195 	int err;
196 
197 	bt_mesh_test_setup();
198 
199 	err = bt_mesh_va_add(test_va_uuid, &va);
200 	ASSERT_OK_MSG(err, "Virtual addr add failed (err %d)", err);
201 
202 	/* Wait for the receiver to subscribe on address. */
203 	k_sleep(K_SECONDS(1));
204 
205 	for (int i = 0; i < ARRAY_SIZE(test_vector); i++) {
206 		err = bt_mesh_test_send(va->addr, va->uuid, test_vector[i].len,
207 					test_vector[i].flags, K_SECONDS(20));
208 		ASSERT_OK_MSG(err, "Failed sending vector %d", i);
209 	}
210 
211 	PASS();
212 }
213 
214 /** Test sending the test vector using virtual addresses with collision.
215  */
test_tx_va_collision(void)216 static void test_tx_va_collision(void)
217 {
218 	const struct bt_mesh_va *va[ARRAY_SIZE(test_va_col_uuid)];
219 	int err;
220 
221 	bt_mesh_test_setup();
222 
223 	for (int i = 0; i < ARRAY_SIZE(test_va_col_uuid); i++) {
224 		err = bt_mesh_va_add(test_va_col_uuid[i], &va[i]);
225 		ASSERT_OK_MSG(err, "Virtual addr add failed (err %d)", err);
226 		ASSERT_EQUAL(test_va_col_addr, va[i]->addr);
227 	}
228 
229 	/* Wait for the receiver to subscribe on address. */
230 	k_sleep(K_SECONDS(1));
231 
232 	for (int i = 0; i < ARRAY_SIZE(test_vector); i++) {
233 		for (int j = 0; j < ARRAY_SIZE(test_va_col_uuid); j++) {
234 			LOG_INF("Sending msg #%d to %s addr", i, (j == 0 ? "first" : "second"));
235 
236 			err = bt_mesh_test_send(test_va_col_addr, va[j]->uuid, test_vector[i].len,
237 						test_vector[i].flags, K_SECONDS(20));
238 			ASSERT_OK_MSG(err, "Failed sending vector %d", i);
239 		}
240 	}
241 
242 	PASS();
243 }
244 
245 /** Test sending of messages to own unicast address using the test vector.
246  */
test_tx_loopback(void)247 static void test_tx_loopback(void)
248 {
249 	int err;
250 
251 	bt_mesh_test_setup();
252 
253 	for (int i = 0; i < ARRAY_SIZE(test_vector); i++) {
254 		err = bt_mesh_test_send(cfg->addr, NULL, test_vector[i].len, test_vector[i].flags,
255 					K_NO_WAIT);
256 		ASSERT_OK_MSG(err, "Failed sending vector %d", i);
257 		bt_mesh_test_recv(test_vector[i].len, cfg->addr, NULL, K_SECONDS(2));
258 
259 		if (test_stats.received != i + 1) {
260 			FAIL("Didn't receive message %d", i);
261 		}
262 	}
263 
264 	PASS();
265 }
266 
267 /** Test sending of messages with an app key that's unknown to the receiver.
268  *
269  *  The sender should be able to send the message successfully, but the receiver
270  *  should fail the decryption step and ignore the packet.
271  */
test_tx_unknown_app(void)272 static void test_tx_unknown_app(void)
273 {
274 	uint8_t app_key[16] = { 0xba, 0xd0, 0x11, 0x22};
275 	uint8_t status = 0;
276 
277 	bt_mesh_test_setup();
278 
279 	ASSERT_OK_MSG(bt_mesh_cfg_cli_app_key_add(0, cfg->addr, 0, 1, app_key, &status),
280 		      "Failed adding additional appkey");
281 	if (status) {
282 		FAIL("App key add status: 0x%02x", status);
283 	}
284 
285 	ASSERT_OK_MSG(bt_mesh_cfg_cli_mod_app_bind(0, cfg->addr, cfg->addr, 1,
286 						   TEST_MOD_ID, &status),
287 		      "Failed binding additional appkey");
288 	if (status) {
289 		FAIL("App key add status: 0x%02x", status);
290 	}
291 
292 	test_send_ctx.app_idx = 1;
293 
294 	ASSERT_OK_MSG(bt_mesh_test_send(rx_cfg.addr, NULL, 5, 0, K_SECONDS(1)),
295 		      "Failed sending unsegmented");
296 
297 	ASSERT_OK_MSG(bt_mesh_test_send(rx_cfg.addr, NULL, 25, 0, K_SECONDS(1)),
298 		      "Failed sending segmented");
299 
300 	PASS();
301 }
302 
303 /** Test sending of messages using the test vector.
304  *
305  *  Messages are sent to a group address that both the sender and receiver
306  *  subscribes to, verifying that the loopback and advertiser paths both work
307  *  when used in combination.
308  */
test_tx_loopback_group(void)309 static void test_tx_loopback_group(void)
310 {
311 	uint8_t status;
312 	int err;
313 
314 	bt_mesh_test_setup();
315 
316 	err = bt_mesh_cfg_cli_mod_sub_add(0, cfg->addr, cfg->addr, GROUP_ADDR,
317 				      TEST_MOD_ID, &status);
318 	ASSERT_OK_MSG(err || status, "Mod sub add failed (err %d, status %u)",
319 		  err, status);
320 
321 	for (int i = 0; i < ARRAY_SIZE(test_vector); i++) {
322 		err = bt_mesh_test_send(GROUP_ADDR, NULL, test_vector[i].len,
323 					test_vector[i].flags,
324 					K_SECONDS(20));
325 
326 		ASSERT_OK_MSG(err, "Failed sending vector %d", i);
327 
328 		k_sleep(K_SECONDS(1));
329 		ASSERT_OK_MSG(bt_mesh_test_recv(test_vector[i].len, GROUP_ADDR,
330 						NULL, K_SECONDS(1)),
331 			      "Failed receiving loopback %d", i);
332 
333 		if (test_stats.received != i + 1) {
334 			FAIL("Didn't receive message %d", i);
335 		}
336 	}
337 
338 	PASS();
339 }
340 
341 /** Start sending multiple segmented messages to the same destination at the
342  *  same time.
343  *
344  *  The second message should be blocked until the first is finished, but
345  *  should still succeed.
346  */
test_tx_seg_block(void)347 static void test_tx_seg_block(void)
348 {
349 	bt_mesh_test_setup();
350 
351 
352 	ASSERT_OK(bt_mesh_test_send(rx_cfg.addr, NULL, 20, 0, K_NO_WAIT));
353 
354 	/* Send some more to the same address before the first is finished. */
355 	ASSERT_OK(bt_mesh_test_send(rx_cfg.addr, NULL, 20, 0, K_NO_WAIT));
356 	ASSERT_OK(bt_mesh_test_send(rx_cfg.addr, NULL, 20, 0, K_SECONDS(10)));
357 
358 	if (test_stats.sent != 3) {
359 		FAIL("Not all messages completed (%u/3)", test_stats.sent);
360 	}
361 
362 	PASS();
363 }
364 
365 /** Start sending multiple segmented messages to the same destination at the
366  *  same time.
367  *
368  *  The second message should be blocked until the first is finished, but
369  *  should still succeed.
370  */
test_tx_seg_concurrent(void)371 static void test_tx_seg_concurrent(void)
372 {
373 	struct k_sem sem;
374 
375 	k_sem_init(&sem, 0, 1);
376 	bt_mesh_test_setup();
377 
378 	ASSERT_OK(bt_mesh_test_send_async(rx_cfg.addr, NULL, 20, 0, &async_send_cb, &sem));
379 
380 	/* Send some more to the same address before the first is finished. */
381 	ASSERT_OK(bt_mesh_test_send(GROUP_ADDR, NULL, 20, 0, K_SECONDS(10)));
382 
383 	/* Ensure that the first message finishes as well */
384 	ASSERT_OK(k_sem_take(&sem, K_SECONDS(1)));
385 
386 	PASS();
387 }
388 
389 /** Start sending a segmented message, then before it's finished, start an IV
390  *  update.
391  *  After the first one finishes, the IV update state shall be active.
392  *  Send another message, then end the IV update state before it's finished.
393  *  The IV index should change when this message finishes.
394  *
395  *  The IV update should not interfere with the segmented message, and the
396  */
test_tx_seg_ivu(void)397 static void test_tx_seg_ivu(void)
398 {
399 	struct k_sem sem;
400 	uint32_t iv_index;
401 
402 	k_sem_init(&sem, 0, 1);
403 
404 	bt_mesh_test_setup();
405 
406 	/* Enable IV update test mode to override IV update timers */
407 	bt_mesh_iv_update_test(true);
408 
409 	iv_index = BT_MESH_NET_IVI_TX;
410 
411 	ASSERT_OK(bt_mesh_test_send_async(rx_cfg.addr, NULL, 255, 0, &async_send_cb,
412 					  &sem));
413 	/* Start IV update */
414 	bt_mesh_iv_update();
415 
416 	if (iv_index != BT_MESH_NET_IVI_TX) {
417 		FAIL("Should not change TX IV index before IV update ends");
418 	}
419 
420 	k_sem_take(&sem, K_SECONDS(20));
421 
422 	ASSERT_OK(bt_mesh_test_send_async(rx_cfg.addr, NULL, 255, 0, &async_send_cb,
423 					  &sem));
424 
425 	/* End IV update */
426 	bt_mesh_iv_update();
427 
428 	if (iv_index != BT_MESH_NET_IVI_TX) {
429 		FAIL("Should not change TX IV index until message finishes");
430 	}
431 
432 	k_sem_take(&sem, K_SECONDS(20));
433 
434 	if (iv_index + 1 != BT_MESH_NET_IVI_TX) {
435 		FAIL("Should have changed TX IV index when the message was completed");
436 	}
437 
438 	PASS();
439 }
440 
441 /** Send a segmented message to an unknown unicast address, expect it to fail
442  *  and return -ETIMEDOUT in the send end callback.
443  */
test_tx_seg_fail(void)444 static void test_tx_seg_fail(void)
445 {
446 	struct k_sem sem;
447 
448 	k_sem_init(&sem, 0, 1);
449 	bt_mesh_test_setup();
450 
451 	expected_send_err = -ETIMEDOUT;
452 	ASSERT_OK(bt_mesh_test_send_async(0x0fff, NULL, 20, 0, &async_send_cb, &sem));
453 	ASSERT_OK(k_sem_take(&sem, K_SECONDS(10)));
454 
455 	PASS();
456 }
457 
458 /* Receiver test functions */
459 
460 /** @brief Receive unicast messages using the test vector.
461  */
test_rx_unicast(void)462 static void test_rx_unicast(void)
463 {
464 	int err;
465 
466 	bt_mesh_test_setup();
467 	rx_sar_conf();
468 
469 	for (int i = 0; i < ARRAY_SIZE(test_vector); i++) {
470 		err = bt_mesh_test_recv(test_vector[i].len, cfg->addr,
471 					NULL, K_SECONDS(10));
472 		ASSERT_OK_MSG(err, "Failed receiving vector %d", i);
473 	}
474 
475 	PASS();
476 }
477 
478 /** @brief Receive group messages using the test vector.
479  */
test_rx_group(void)480 static void test_rx_group(void)
481 {
482 	uint8_t status;
483 	int err;
484 
485 	bt_mesh_test_setup();
486 
487 	err = bt_mesh_cfg_cli_mod_sub_add(0, cfg->addr, cfg->addr, GROUP_ADDR,
488 				      TEST_MOD_ID, &status);
489 	ASSERT_OK_MSG(err || status, "Mod sub add failed (err %d, status %u)", err, status);
490 
491 	for (int i = 0; i < ARRAY_SIZE(test_vector); i++) {
492 		err = bt_mesh_test_recv(test_vector[i].len, GROUP_ADDR,
493 					NULL, K_SECONDS(20));
494 		ASSERT_OK_MSG(err, "Failed receiving vector %d", i);
495 	}
496 
497 	PASS();
498 }
499 
500 /** Test that a node delivers a message to a model subscribed to a fixed group address even if the
501  *  corresponding feature is disabled.
502  */
test_rx_fixed(void)503 static void test_rx_fixed(void)
504 {
505 	uint8_t status;
506 	int err;
507 
508 	bt_mesh_test_setup();
509 
510 	/* Step 1.
511 	 *
512 	 * The model is on primary element, so it should receive the message if the Relay feature
513 	 * is enabled.
514 	 */
515 	err = bt_mesh_relay_set(BT_MESH_RELAY_ENABLED,
516 				BT_MESH_TRANSMIT(CONFIG_BT_MESH_RELAY_RETRANSMIT_COUNT,
517 						 CONFIG_BT_MESH_RELAY_RETRANSMIT_INTERVAL));
518 	ASSERT_EQUAL(err, -EALREADY);
519 
520 	ASSERT_OK(bt_mesh_test_recv(test_vector[0].len, BT_MESH_ADDR_RELAYS, NULL, K_SECONDS(4)));
521 
522 	/* Step 2.
523 	 *
524 	 * Disabling the Relay feature, but subscribing the model to the all-relays address. The
525 	 * model should receive the message.
526 	 */
527 	ASSERT_OK(bt_mesh_relay_set(BT_MESH_RELAY_DISABLED,
528 				    BT_MESH_TRANSMIT(CONFIG_BT_MESH_RELAY_RETRANSMIT_COUNT,
529 						     CONFIG_BT_MESH_RELAY_RETRANSMIT_INTERVAL)));
530 
531 	err = bt_mesh_cfg_cli_mod_sub_add(0, cfg->addr, cfg->addr, BT_MESH_ADDR_RELAYS,
532 					  TEST_MOD_ID, &status);
533 	ASSERT_OK_MSG(err || status, "Mod sub add failed (err %d, status %u)", err, status);
534 
535 	ASSERT_OK(bt_mesh_test_recv(test_vector[0].len, BT_MESH_ADDR_RELAYS, NULL, K_SECONDS(4)));
536 
537 	/* Step 3.
538 	 *
539 	 * Unsubscribing the model so that it doesn't receive the message.
540 	 */
541 	err = bt_mesh_cfg_cli_mod_sub_del(0, cfg->addr, cfg->addr, BT_MESH_ADDR_RELAYS,
542 					  TEST_MOD_ID, &status);
543 	ASSERT_OK_MSG(err || status, "Mod sub del failed (err %d, status %u)", err, status);
544 
545 	err = bt_mesh_test_recv(test_vector[0].len, BT_MESH_ADDR_RELAYS, NULL, K_SECONDS(4));
546 	ASSERT_EQUAL(err, -ETIMEDOUT);
547 
548 	PASS();
549 }
550 
551 /** @brief Receive virtual address messages using the test vector.
552  */
test_rx_va(void)553 static void test_rx_va(void)
554 {
555 	uint16_t virtual_addr;
556 	uint8_t status;
557 	int err;
558 
559 	bt_mesh_test_setup();
560 
561 	err = bt_mesh_cfg_cli_mod_sub_va_add(0, cfg->addr, cfg->addr, test_va_uuid,
562 					 TEST_MOD_ID, &virtual_addr, &status);
563 	ASSERT_OK_MSG(err || status, "Sub add failed (err %d, status %u)", err, status);
564 
565 	for (int i = 0; i < ARRAY_SIZE(test_vector); i++) {
566 		err = bt_mesh_test_recv(test_vector[i].len, virtual_addr,
567 					test_va_uuid, K_SECONDS(20));
568 		ASSERT_OK_MSG(err, "Failed receiving vector %d", i);
569 	}
570 
571 	PASS();
572 }
573 
574 /** @brief Receive the test vector using virtual addresses with collision.
575  */
test_rx_va_collision(void)576 static void test_rx_va_collision(void)
577 {
578 	uint16_t virtual_addr;
579 	uint8_t status;
580 	int err;
581 
582 	bt_mesh_test_setup();
583 
584 	for (int i = 0; i < ARRAY_SIZE(test_va_col_uuid); i++) {
585 		err = bt_mesh_cfg_cli_mod_sub_va_add(0, cfg->addr, cfg->addr, test_va_col_uuid[i],
586 						     TEST_MOD_ID, &virtual_addr, &status);
587 		ASSERT_OK_MSG(err || status, "Sub add failed (err %d, status %u)", err, status);
588 		ASSERT_EQUAL(test_va_col_addr, virtual_addr);
589 	}
590 
591 	for (int i = 0; i < ARRAY_SIZE(test_vector); i++) {
592 		for (int j = 0; j < ARRAY_SIZE(test_va_col_uuid); j++) {
593 			LOG_INF("Recv msg #%d from %s addr", i, (j == 0 ? "first" : "second"));
594 
595 			err = bt_mesh_test_recv(test_vector[i].len, test_va_col_addr,
596 						test_va_col_uuid[j], K_SECONDS(20));
597 			ASSERT_OK_MSG(err, "Failed receiving vector %d", i);
598 		}
599 	}
600 
601 	PASS();
602 }
603 
604 /** @brief Verify that this device doesn't receive any messages.
605  */
test_rx_none(void)606 static void test_rx_none(void)
607 {
608 	struct bt_mesh_test_msg msg;
609 	int err;
610 
611 	bt_mesh_test_setup();
612 
613 	err = bt_mesh_test_recv_msg(&msg, K_SECONDS(60));
614 	if (!err) {
615 		FAIL("Unexpected rx from 0x%04x", msg.ctx.addr);
616 	}
617 
618 	PASS();
619 }
620 
621 /** @brief Verify that this device doesn't receive any messages.
622  */
test_rx_seg_block(void)623 static void test_rx_seg_block(void)
624 {
625 	bt_mesh_test_setup();
626 
627 	ASSERT_OK_MSG(bt_mesh_test_recv(20, cfg->addr, NULL, K_SECONDS(2)), "RX fail");
628 	ASSERT_OK_MSG(bt_mesh_test_recv(20, cfg->addr, NULL, K_SECONDS(2)), "RX fail");
629 	ASSERT_OK_MSG(bt_mesh_test_recv(20, cfg->addr, NULL, K_SECONDS(2)), "RX fail");
630 
631 	PASS();
632 }
633 
634 /** @brief Verify that this device doesn't receive any messages.
635  */
test_rx_seg_concurrent(void)636 static void test_rx_seg_concurrent(void)
637 {
638 	uint8_t status;
639 	int err;
640 
641 	bt_mesh_test_setup();
642 
643 	/* Subscribe to group addr */
644 	err = bt_mesh_cfg_cli_mod_sub_add(0, cfg->addr, cfg->addr, GROUP_ADDR,
645 				      TEST_MOD_ID, &status);
646 	ASSERT_OK_MSG(err || status, "Mod sub add failed (err %d, status %u)", err, status);
647 
648 	/* Receive both messages from the sender.
649 	 * Note: The receive order is technically irrelevant, but the test_recv
650 	 * function fails if the order is wrong.
651 	 */
652 	ASSERT_OK_MSG(bt_mesh_test_recv(20, cfg->addr, NULL, K_SECONDS(2)), "RX fail");
653 	ASSERT_OK_MSG(bt_mesh_test_recv(20, GROUP_ADDR, NULL, K_SECONDS(2)), "RX fail");
654 
655 	PASS();
656 }
657 
658 /** @brief Verify that this device doesn't receive any messages.
659  */
test_rx_seg_ivu(void)660 static void test_rx_seg_ivu(void)
661 {
662 	bt_mesh_test_setup();
663 	rx_sar_conf();
664 
665 	ASSERT_OK_MSG(bt_mesh_test_recv(255, cfg->addr, NULL, K_SECONDS(5)), "RX fail");
666 	ASSERT_OK_MSG(bt_mesh_test_recv(255, cfg->addr, NULL, K_SECONDS(5)), "RX fail");
667 
668 	PASS();
669 }
670 
671 #define TEST_CASE(role, name, description)                                     \
672 	{                                                                      \
673 		.test_id = "transport_" #role "_" #name,                       \
674 		.test_descr = description,                                     \
675 		.test_post_init_f = test_##role##_init,                        \
676 		.test_tick_f = bt_mesh_test_timeout,                           \
677 		.test_main_f = test_##role##_##name,                           \
678 	}
679 
680 static const struct bst_test_instance test_connect[] = {
681 	TEST_CASE(tx, unicast,        "Transport: send to unicast addr"),
682 	TEST_CASE(tx, group,          "Transport: send to group addr"),
683 	TEST_CASE(tx, fixed,          "Transport: send to fixed group addr"),
684 	TEST_CASE(tx, va,             "Transport: send to virtual addr"),
685 	TEST_CASE(tx, va_collision,   "Transport: send to virtual addr"),
686 	TEST_CASE(tx, loopback,       "Transport: send loopback"),
687 	TEST_CASE(tx, loopback_group, "Transport: send loopback and group"),
688 	TEST_CASE(tx, unknown_app,    "Transport: send with unknown app key"),
689 	TEST_CASE(tx, seg_block,      "Transport: send blocked segmented"),
690 	TEST_CASE(tx, seg_concurrent, "Transport: send concurrent segmented"),
691 	TEST_CASE(tx, seg_ivu,        "Transport: send segmented during IV update"),
692 	TEST_CASE(tx, seg_fail,       "Transport: send segmented to unused addr"),
693 
694 	TEST_CASE(rx, unicast,        "Transport: receive on unicast addr"),
695 	TEST_CASE(rx, group,          "Transport: receive on group addr"),
696 	TEST_CASE(rx, fixed,          "Transport: receive on fixed group addr"),
697 	TEST_CASE(rx, va,             "Transport: receive on virtual addr"),
698 	TEST_CASE(rx, va_collision,   "Transport: receive on virtual addr"),
699 	TEST_CASE(rx, none,           "Transport: receive no messages"),
700 	TEST_CASE(rx, seg_block,      "Transport: receive blocked segmented"),
701 	TEST_CASE(rx, seg_concurrent, "Transport: receive concurrent segmented"),
702 	TEST_CASE(rx, seg_ivu,        "Transport: receive segmented during IV update"),
703 
704 	BSTEST_END_MARKER
705 };
706 
test_transport_install(struct bst_test_list * tests)707 struct bst_test_list *test_transport_install(struct bst_test_list *tests)
708 {
709 	tests = bst_add_tests(tests, test_connect);
710 	return tests;
711 }
712