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 #include "argparse.h"
12
13 #include "friendship_common.h"
14
15 #define LOG_MODULE_NAME test_friendship
16
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
19
20 /*
21 * Friendship tests:
22 * Tests both the friend and the low power role in various scenarios.
23 */
24
25 #define GROUP_ADDR 0xc000
26 #define WAIT_TIME 70 /*seconds*/
27 #define LPN_ADDR_START 0x0003
28 #define POLL_TIMEOUT_MS (100 * CONFIG_BT_MESH_LPN_POLL_TIMEOUT)
29
30 extern enum bst_result_t bst_result;
31
32 static const struct bt_mesh_test_cfg friend_cfg = {
33 .addr = 0x0001,
34 .dev_key = { 0x01 },
35 };
36 static const struct bt_mesh_test_cfg other_cfg = {
37 .addr = 0x0002,
38 .dev_key = { 0x02 },
39 };
40 static struct bt_mesh_test_cfg lpn_cfg;
41
42 static uint8_t test_va_col_uuid[][16] = {
43 { 0xe3, 0x94, 0xe7, 0xc1, 0xc5, 0x14, 0x72, 0x11,
44 0x68, 0x36, 0x19, 0x30, 0x99, 0x34, 0x53, 0x62 },
45 { 0x5e, 0x49, 0x5a, 0xd9, 0x44, 0xdf, 0xae, 0xc0,
46 0x62, 0xd8, 0x0d, 0xed, 0x16, 0x82, 0xd1, 0x7d },
47 };
48 static uint16_t test_va_col_addr = 0x809D;
49
test_friend_init(void)50 static void test_friend_init(void)
51 {
52 bt_mesh_test_cfg_set(&friend_cfg, WAIT_TIME);
53 }
54
test_lpn_init(void)55 static void test_lpn_init(void)
56 {
57 /* As there may be multiple LPN devices, we'll set the address and
58 * devkey based on the device number, which is guaranteed to be unique
59 * for each device in the simulation.
60 */
61 lpn_cfg.addr = LPN_ADDR_START + get_device_nbr();
62 lpn_cfg.dev_key[0] = get_device_nbr();
63 bt_mesh_test_cfg_set(&lpn_cfg, WAIT_TIME);
64 }
65
test_other_init(void)66 static void test_other_init(void)
67 {
68 bt_mesh_test_cfg_set(&other_cfg, WAIT_TIME);
69 }
70
friend_wait_for_polls(int polls)71 static void friend_wait_for_polls(int polls)
72 {
73 /* Let LPN poll to get the sent message */
74 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_POLLED,
75 K_SECONDS(30)), "LPN never polled");
76
77 while (--polls) {
78 /* Wait for LPN to poll until the "no more data" message.
79 * At this point, the message has been delivered.
80 */
81 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_POLLED,
82 K_SECONDS(2)),
83 "LPN missing %d polls", polls);
84 }
85
86 if (bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_POLLED,
87 K_SECONDS(2)) != -EAGAIN) {
88 FAIL("Unexpected extra poll");
89 return;
90 }
91 }
92
93 /* Friend test functions */
94
95 /** Initialize as a friend and wait for the friendship to be established.
96 */
test_friend_est(void)97 static void test_friend_est(void)
98 {
99 bt_mesh_test_setup();
100 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
101 bt_mesh_friend_set(BT_MESH_FEATURE_ENABLED);
102
103 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_ESTABLISHED,
104 K_SECONDS(5)),
105 "Friendship not established");
106
107 PASS();
108 }
109
110 /** Initialize as a friend, and wait for multiple friendships to be established
111 * concurrently.
112 *
113 * Verify that all friendships survive the first poll timeout.
114 */
test_friend_est_multi(void)115 static void test_friend_est_multi(void)
116 {
117 int err;
118
119 bt_mesh_test_setup();
120 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
121 bt_mesh_friend_set(BT_MESH_FEATURE_ENABLED);
122
123 for (int i = 0; i < CONFIG_BT_MESH_FRIEND_LPN_COUNT; i++) {
124 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_ESTABLISHED,
125 K_SECONDS(5)),
126 "Friendship %d not established", i);
127 }
128
129 /* Wait for all friends to do at least one poll without terminating */
130 err = bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_TERMINATED,
131 K_MSEC(POLL_TIMEOUT_MS + 5 * MSEC_PER_SEC));
132 if (!err) {
133 FAIL("One or more friendships terminated");
134 }
135
136 PASS();
137 }
138
139 /** As a friend, send messages to the LPN.
140 *
141 * Verifies unsegmented, segmented and multiple packet sending and receiving.
142 */
test_friend_msg(void)143 static void test_friend_msg(void)
144 {
145 bt_mesh_test_setup();
146 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
147 bt_mesh_friend_set(BT_MESH_FEATURE_ENABLED);
148
149 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_ESTABLISHED,
150 K_SECONDS(5)),
151 "Friendship not established");
152 /* LPN polls on establishment. Clear the poll state */
153 bt_mesh_test_friendship_evt_clear(BT_MESH_TEST_FRIEND_POLLED);
154
155 k_sleep(K_SECONDS(1));
156
157 /* Send unsegmented message from friend to LPN: */
158 LOG_INF("Sending unsegmented message");
159 ASSERT_OK_MSG(bt_mesh_test_send(bt_mesh_test_friendship_addr_get(), NULL, 5, 0,
160 K_SECONDS(1)),
161 "Unseg send failed");
162
163 /* Wait for LPN to poll for message and the "no more messages" msg */
164 friend_wait_for_polls(2);
165
166 /* Send segmented message */
167 ASSERT_OK_MSG(bt_mesh_test_send(bt_mesh_test_friendship_addr_get(), NULL, 13, 0,
168 K_SECONDS(1)),
169 "Unseg send failed");
170
171 /* Two segments require 2 polls plus the "no more messages" msg */
172 friend_wait_for_polls(3);
173
174 /* Send two unsegmented messages before the next poll.
175 * This tests the friend role's re-encryption mechanism for the second
176 * message, as sending the first message through the network layer
177 * increases the seqnum by one, creating an inconsistency between the
178 * transport and network parts of the second packet.
179 * Ensures coverage for the regression reported in #32033.
180 */
181 ASSERT_OK_MSG(bt_mesh_test_send(bt_mesh_test_friendship_addr_get(), NULL,
182 BT_MESH_SDU_UNSEG_MAX, 0, K_SECONDS(1)),
183 "Unseg send failed");
184 ASSERT_OK_MSG(bt_mesh_test_send(bt_mesh_test_friendship_addr_get(), NULL,
185 BT_MESH_SDU_UNSEG_MAX, 0, K_SECONDS(1)),
186 "Unseg send failed");
187
188 /* Two messages require 2 polls plus the "no more messages" msg */
189 friend_wait_for_polls(3);
190
191 ASSERT_OK_MSG(bt_mesh_test_recv(5, cfg->addr, NULL, K_SECONDS(10)),
192 "Receive from LPN failed");
193
194 /* Receive a segmented message from the LPN. LPN should poll for the ack
195 * after sending the segments.
196 */
197 ASSERT_OK(bt_mesh_test_recv(15, cfg->addr, NULL, K_SECONDS(10)));
198 /* - 2 for each SegAck (SegAcks are sent faster than Friend Poll messages);
199 * - The last one with MD == 0;
200 */
201 friend_wait_for_polls(2);
202
203 PASS();
204 }
205
206 /** As a friend, overflow the message queue for the LPN with own packets.
207 *
208 * Verify that the LPN doesn't terminate the friendship during the poll for
209 * messages.
210 */
test_friend_overflow(void)211 static void test_friend_overflow(void)
212 {
213 bt_mesh_test_setup();
214 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
215 bt_mesh_friend_set(BT_MESH_FEATURE_ENABLED);
216
217 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_ESTABLISHED,
218 K_SECONDS(5)),
219 "Friendship not established");
220 bt_mesh_test_friendship_evt_clear(BT_MESH_TEST_FRIEND_POLLED);
221
222 k_sleep(K_SECONDS(3));
223
224 LOG_INF("Testing overflow with only unsegmented messages...");
225
226 /* Fill the queue */
227 for (int i = 0; i < CONFIG_BT_MESH_FRIEND_QUEUE_SIZE; i++) {
228 bt_mesh_test_send(bt_mesh_test_friendship_addr_get(), NULL, 5, 0, K_NO_WAIT);
229 }
230
231 /* Add one more message, which should overflow the queue and cause the
232 * first message to be discarded.
233 */
234 bt_mesh_test_send(bt_mesh_test_friendship_addr_get(), NULL, 5, 0, K_NO_WAIT);
235
236 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_POLLED,
237 K_SECONDS(35)),
238 "Friend never polled");
239
240 /* LPN verifies that no more messages are in Friend Queue. */
241 k_sleep(K_SECONDS(10));
242
243 LOG_INF("Testing overflow with unsegmented message preempting segmented one...");
244
245 /* Make room in the Friend Queue for only one unsegmented message. */
246 bt_mesh_test_send(bt_mesh_test_friendship_addr_get(),
247 NULL, BT_MESH_SDU_UNSEG_MAX *
248 (CONFIG_BT_MESH_FRIEND_QUEUE_SIZE - 1),
249 0, K_SECONDS(1)),
250 bt_mesh_test_send(bt_mesh_test_friendship_addr_get(), NULL, 5, 0, K_NO_WAIT);
251 /* This message should preempt the segmented one. */
252 bt_mesh_test_send(bt_mesh_test_friendship_addr_get(), NULL, 5, 0, K_NO_WAIT);
253
254 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_POLLED,
255 K_SECONDS(35)),
256 "Friend never polled");
257
258 /* LPN verifies that there are no more messages in the Friend Queue. */
259 k_sleep(K_SECONDS(10));
260
261 LOG_INF("Testing overflow with segmented message preempting another segmented...");
262
263 /* Make space in the Friend Queue for only 2 unsegmented messages so the next unsgemented
264 * message won't preempt this segmented message.
265 */
266 bt_mesh_test_send(bt_mesh_test_friendship_addr_get(),
267 NULL, BT_MESH_SDU_UNSEG_MAX *
268 (CONFIG_BT_MESH_FRIEND_QUEUE_SIZE - 2),
269 0, K_SECONDS(1));
270 bt_mesh_test_send(bt_mesh_test_friendship_addr_get(), NULL, 5, 0, K_NO_WAIT);
271 /* This segmented message should preempt the previous segmented message. */
272 bt_mesh_test_send(bt_mesh_test_friendship_addr_get(),
273 NULL, BT_MESH_SDU_UNSEG_MAX *
274 (CONFIG_BT_MESH_FRIEND_QUEUE_SIZE - 2),
275 0, K_SECONDS(1));
276 /* This message should fit in Friend Queue as well. */
277 bt_mesh_test_send(bt_mesh_test_friendship_addr_get(), NULL, 5, 0, K_NO_WAIT);
278
279 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_POLLED,
280 K_SECONDS(35)),
281 "Friend never polled");
282
283 if (bt_mesh_test_friendship_state_check(BT_MESH_TEST_FRIEND_TERMINATED)) {
284 FAIL("Friendship terminated unexpectedly");
285 }
286
287 PASS();
288 }
289
290 /** Establish a friendship, wait for communication between the LPN and a mesh
291 * device to finish, then send group and virtual addr messages to the LPN.
292 * Let the LPN add another group message, then send to that as well.
293 */
test_friend_group(void)294 static void test_friend_group(void)
295 {
296 const struct bt_mesh_va *va;
297
298 bt_mesh_test_setup();
299 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
300 bt_mesh_friend_set(BT_MESH_FEATURE_ENABLED);
301
302 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_ESTABLISHED,
303 K_SECONDS(5)),
304 "Friendship not established");
305 bt_mesh_test_friendship_evt_clear(BT_MESH_TEST_FRIEND_POLLED);
306
307 ASSERT_OK(bt_mesh_va_add(test_va_uuid, &va));
308
309 /* The other mesh device will send its messages in the first poll */
310 ASSERT_OK(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_POLLED,
311 K_SECONDS(10)));
312
313 k_sleep(K_SECONDS(2));
314
315 bt_mesh_test_friendship_evt_clear(BT_MESH_TEST_FRIEND_POLLED);
316
317 /* Send a group message to the LPN */
318 ASSERT_OK_MSG(bt_mesh_test_send(GROUP_ADDR, NULL, 5, 0, K_SECONDS(1)),
319 "Failed to send to LPN");
320 /* Send a virtual message to the LPN */
321 ASSERT_OK_MSG(bt_mesh_test_send(va->addr, va->uuid, 5, 0, K_SECONDS(1)),
322 "Failed to send to LPN");
323
324 /* Wait for the LPN to poll for each message, then for adding the
325 * group address:
326 */
327 friend_wait_for_polls(3);
328
329 /* Send a group message to an address the LPN added after the friendship
330 * was established.
331 */
332 ASSERT_OK_MSG(bt_mesh_test_send(GROUP_ADDR + 1, NULL, 5, 0, K_SECONDS(1)),
333 "Failed to send to LPN");
334
335 bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_POLLED, K_SECONDS(10));
336
337 PASS();
338 }
339
340
341 /* Friend no-establish test functions */
342
343 /** Initialize as a friend and no friendships to be established.
344 */
test_friend_no_est(void)345 static void test_friend_no_est(void)
346 {
347 bt_mesh_test_setup();
348 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
349 bt_mesh_friend_set(BT_MESH_FEATURE_ENABLED);
350
351 if (!bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_ESTABLISHED,
352 K_SECONDS(30))) {
353 FAIL("Friendship established unexpectedly");
354 }
355
356 PASS();
357 }
358
359 /** Send messages to 2 virtual addresses with collision and check that LPN correctly polls them.
360 */
test_friend_va_collision(void)361 static void test_friend_va_collision(void)
362 {
363 const struct bt_mesh_va *va[2];
364
365 bt_mesh_test_setup();
366 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
367 bt_mesh_friend_set(BT_MESH_FEATURE_ENABLED);
368
369 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_ESTABLISHED,
370 K_SECONDS(5)),
371 "Friendship not established");
372 bt_mesh_test_friendship_evt_clear(BT_MESH_TEST_FRIEND_POLLED);
373
374 for (int i = 0; i < ARRAY_SIZE(test_va_col_uuid); i++) {
375 ASSERT_OK(bt_mesh_va_add(test_va_col_uuid[i], &va[i]));
376 ASSERT_EQUAL(test_va_col_addr, va[i]->addr);
377 }
378
379 bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_FRIEND_POLLED, K_SECONDS(10));
380
381 LOG_INF("Step 1: Sending msgs to LPN.");
382
383 /* LPN shall receive the first 2 messages. */
384 for (int i = 0; i < ARRAY_SIZE(test_va_col_uuid); i++) {
385 /* Send a message to the first virtual address. LPN should receive it. */
386 ASSERT_OK_MSG(bt_mesh_test_send(test_va_col_addr, va[i]->uuid, 5, 0, K_SECONDS(1)),
387 "Failed to send to LPN");
388 }
389 /* One poll per message + Friend Update with md == 0 */
390 friend_wait_for_polls(3);
391
392 LOG_INF("Let LPN unsubscribe from the first address.");
393
394 /* Manual poll by LPN test case after removing the first Label UUID from subscription. */
395 friend_wait_for_polls(1);
396
397 LOG_INF("Step 2: Sending msgs to LPN.");
398
399 /* Friend will send both messages as the virtual address is the same, but LPN shall
400 * receive only the second message.
401 */
402 for (int i = 0; i < ARRAY_SIZE(test_va_col_uuid); i++) {
403 ASSERT_OK_MSG(bt_mesh_test_send(test_va_col_addr, va[i]->uuid, 5, 0, K_SECONDS(1)),
404 "Failed to send to LPN");
405 }
406 /* One poll per message + Friend Update with md == 0 */
407 friend_wait_for_polls(3);
408
409 LOG_INF("Let LPN unsubscribe from the second address.");
410
411 /* Manual poll by LPN test case after removing the second Label UUID from subscription.
412 * After this step, the virtual address shall be removed from the subscription list.
413 */
414 friend_wait_for_polls(1);
415
416 LOG_INF("Step 3: Sending msgs to LPN.");
417
418 /* Friend shall not send the messages to LPN because it is not subscribed to any virtual
419 * address.
420 */
421 for (int i = 0; i < ARRAY_SIZE(test_va_col_uuid); i++) {
422 ASSERT_OK_MSG(bt_mesh_test_send(test_va_col_addr, va[i]->uuid, 5, 0, K_SECONDS(1)),
423 "Failed to send to LPN");
424 }
425 /* Shall be only one Friend Poll as the Friend Queue is empty. */
426 friend_wait_for_polls(1);
427
428 PASS();
429 }
430
431
432 /* LPN test functions */
433
434 /** Enable the LPN role, and verify that the friendship is established.
435 *
436 * Verify that the friendship survives the first poll timeout.
437 */
test_lpn_est(void)438 static void test_lpn_est(void)
439 {
440 bt_mesh_test_setup();
441 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
442
443 /* This test is used to establish friendship with single lpn as well as
444 * with many lpn devices. If legacy advertiser is used friendship with
445 * many lpn devices is established normally due to bad precision of advertiser.
446 * If extended advertiser is used simultaneous lpn running causes the situation
447 * when Friend Request from several devices collide in emulated radio channel.
448 * This shift of start moment helps to avoid Friend Request collisions.
449 */
450 k_sleep(K_MSEC(10 * get_device_nbr()));
451
452 bt_mesh_lpn_set(true);
453
454 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_LPN_ESTABLISHED,
455 K_SECONDS(5)),
456 "LPN not established");
457 if (!bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_LPN_TERMINATED,
458 K_MSEC(POLL_TIMEOUT_MS + 5 * MSEC_PER_SEC))) {
459 FAIL("Friendship terminated unexpectedly");
460 }
461
462 PASS();
463 }
464
465 /** As an LPN, exchange messages with the friend node.
466 *
467 * Verifies sending and receiving of unsegmented, segmented and multiple
468 * messages to and from the connected friend node.
469 */
test_lpn_msg_frnd(void)470 static void test_lpn_msg_frnd(void)
471 {
472 bt_mesh_test_setup();
473 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
474 bt_mesh_lpn_set(true);
475
476 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_LPN_ESTABLISHED,
477 K_SECONDS(5)), "LPN not established");
478 /* LPN polls on establishment. Clear the poll state */
479 bt_mesh_test_friendship_evt_clear(BT_MESH_TEST_LPN_POLLED);
480
481 /* Give friend time to prepare the message */
482 k_sleep(K_SECONDS(3));
483
484 /* Receive unsegmented message */
485 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
486 ASSERT_OK_MSG(bt_mesh_test_recv(5, cfg->addr, NULL, K_SECONDS(1)),
487 "Failed to receive message");
488
489 /* Give friend time to prepare the message */
490 k_sleep(K_SECONDS(3));
491
492 /* Receive segmented message */
493 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
494 ASSERT_OK_MSG(bt_mesh_test_recv(13, cfg->addr, NULL, K_SECONDS(2)),
495 "Failed to receive message");
496
497 /* Give friend time to prepare the messages */
498 k_sleep(K_SECONDS(3));
499
500 /* Receive two unsegmented messages */
501 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
502 ASSERT_OK_MSG(bt_mesh_test_recv(BT_MESH_SDU_UNSEG_MAX, cfg->addr,
503 NULL, K_SECONDS(2)),
504 "Failed to receive message");
505 ASSERT_OK_MSG(bt_mesh_test_recv(BT_MESH_SDU_UNSEG_MAX, cfg->addr,
506 NULL, K_SECONDS(2)),
507 "Failed to receive message");
508
509 k_sleep(K_SECONDS(3));
510
511 /* Send an unsegmented message to the friend.
512 * Should not be affected by the LPN mode at all.
513 */
514 ASSERT_OK_MSG(bt_mesh_test_send(friend_cfg.addr, NULL, 5, 0, K_MSEC(500)),
515 "Send to friend failed");
516
517 k_sleep(K_SECONDS(5));
518
519 /* Send a segmented message to the friend. Should trigger a poll for the
520 * ack.
521 */
522 ASSERT_OK_MSG(bt_mesh_test_send(friend_cfg.addr, NULL, 15, 0, K_SECONDS(5)),
523 "Send to friend failed");
524
525 PASS();
526 }
527
528 /** As an LPN, exchange messages with a third party mesh node while in a
529 * friendship.
530 *
531 * Verifies sending and receiving of unsegmented and segmented messages to and
532 * from the third party node.
533 */
test_lpn_msg_mesh(void)534 static void test_lpn_msg_mesh(void)
535 {
536 bt_mesh_test_setup();
537 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
538 bt_mesh_lpn_set(true);
539
540 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_LPN_ESTABLISHED,
541 K_SECONDS(2)), "LPN not established");
542 /* LPN polls on establishment. Clear the poll state */
543 bt_mesh_test_friendship_evt_clear(BT_MESH_TEST_LPN_POLLED);
544
545 /* Send an unsegmented message to a third mesh node.
546 * Should not be affected by the LPN mode at all.
547 */
548 ASSERT_OK_MSG(bt_mesh_test_send(other_cfg.addr, NULL, 5, 0, K_NO_WAIT),
549 "Send to mesh failed");
550
551 /* Receive an unsegmented message back */
552 ASSERT_OK(bt_mesh_test_recv(5, cfg->addr, NULL, K_FOREVER));
553
554 /* Send a segmented message to the mesh node. */
555 ASSERT_OK_MSG(bt_mesh_test_send(other_cfg.addr, NULL, 15, 0, K_FOREVER),
556 "Send to other failed");
557
558 /* Receive a segmented message back */
559 ASSERT_OK(bt_mesh_test_recv(15, cfg->addr, NULL, K_FOREVER));
560
561 /* Send an unsegmented message with friend credentials to a third mesh
562 * node. The friend shall relay it.
563 */
564 test_model->pub->addr = other_cfg.addr;
565 test_model->pub->cred = true; /* Use friend credentials */
566 test_model->pub->ttl = BT_MESH_TTL_DEFAULT;
567
568 net_buf_simple_reset(test_model->pub->msg);
569 bt_mesh_model_msg_init(test_model->pub->msg, TEST_MSG_OP_1);
570 ASSERT_OK(bt_mesh_model_publish(test_model));
571
572 PASS();
573 }
574
575 /** As an LPN, establish and terminate a friendship with the same friend
576 * multiple times in a row to ensure that both parties are able to recover.
577 */
test_lpn_re_est(void)578 static void test_lpn_re_est(void)
579 {
580 bt_mesh_test_setup();
581 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
582
583 for (int i = 0; i < 4; i++) {
584 bt_mesh_lpn_set(true);
585 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_LPN_ESTABLISHED,
586 K_SECONDS(2)),
587 "LPN not established");
588
589 bt_mesh_lpn_set(false);
590 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_LPN_TERMINATED,
591 K_SECONDS(5)),
592 "LPN never terminated friendship");
593
594 k_sleep(K_SECONDS(2));
595 }
596
597 PASS();
598 }
599
600 /** Establish a friendship as an LPN, and verify that the friendship survives
601 * the first poll timeout without terminating
602 */
test_lpn_poll(void)603 static void test_lpn_poll(void)
604 {
605 bt_mesh_test_setup();
606 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
607
608 bt_mesh_lpn_set(true);
609 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_LPN_ESTABLISHED,
610 K_SECONDS(5)), "LPN not established");
611 bt_mesh_test_friendship_evt_clear(BT_MESH_TEST_LPN_POLLED);
612
613 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_LPN_POLLED,
614 K_MSEC(POLL_TIMEOUT_MS)),
615 "LPN failed to poll before the timeout");
616
617 k_sleep(K_SECONDS(10));
618 if (bt_mesh_test_friendship_state_check(BT_MESH_TEST_LPN_TERMINATED)) {
619 FAIL("LPN terminated.");
620 }
621
622 PASS();
623 }
624
625 /** Receive packets from a friend that overflowed its queue. Verify that the
626 * first packet is discarded because of the overflow.
627 */
test_lpn_overflow(void)628 static void test_lpn_overflow(void)
629 {
630 struct bt_mesh_test_msg msg;
631 int exp_seq;
632 int err;
633
634 bt_mesh_test_setup();
635 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
636
637 bt_mesh_lpn_set(true);
638 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_LPN_ESTABLISHED,
639 K_SECONDS(5)), "LPN not established");
640 bt_mesh_test_friendship_evt_clear(BT_MESH_TEST_LPN_POLLED);
641
642 k_sleep(K_SECONDS(5));
643 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
644
645 LOG_INF("Testing overflow with only unsegmented messages...");
646
647 for (int i = 0; i < CONFIG_BT_MESH_FRIEND_QUEUE_SIZE; i++) {
648 ASSERT_OK_MSG(bt_mesh_test_recv_msg(&msg, K_SECONDS(2)),
649 "Receive %d failed", i);
650
651 if (msg.len != 5) {
652 FAIL("Message %d: Invalid length %d", i, msg.len);
653 }
654
655 if (msg.ctx.recv_dst != cfg->addr) {
656 FAIL("Message %d: Invalid dst 0x%04x", i,
657 msg.ctx.recv_dst);
658 }
659
660 /* The first message (with seq=1) should have been discarded by
661 * the friend, so the first message should have seq=2:
662 */
663 if (msg.seq != i + 2) {
664 FAIL("Message %d: Invalid seq 0x%02x", i, msg.seq);
665 }
666 }
667
668 /* Not expecting any more messages from friend */
669 err = bt_mesh_test_recv_msg(&msg, K_SECONDS(10));
670 if (!err) {
671 FAIL("Unexpected additional message 0x%02x from 0x%04x",
672 msg.seq, msg.ctx.addr);
673 }
674
675 LOG_INF("Testing overflow with unsegmented message preempting segmented one...");
676
677 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
678
679 /* Last seq from the previous step. */
680 exp_seq = CONFIG_BT_MESH_FRIEND_QUEUE_SIZE + 1;
681
682 exp_seq += 2; /* Skipping the first message in Friend Queue. */
683 ASSERT_OK_MSG(bt_mesh_test_recv_msg(&msg, K_SECONDS(2)),
684 "Receive first unseg msg failed");
685 ASSERT_EQUAL(5, msg.len);
686 ASSERT_EQUAL(cfg->addr, msg.ctx.recv_dst);
687 ASSERT_EQUAL(exp_seq, msg.seq);
688
689 exp_seq++;
690 ASSERT_OK_MSG(bt_mesh_test_recv_msg(&msg, K_SECONDS(2)),
691 "Receive the second unseg msg failed");
692 ASSERT_EQUAL(5, msg.len);
693 ASSERT_EQUAL(cfg->addr, msg.ctx.recv_dst);
694 ASSERT_EQUAL(exp_seq, msg.seq);
695
696 /* Not expecting any more messages from friend */
697 err = bt_mesh_test_recv_msg(&msg, K_SECONDS(10));
698 if (!err) {
699 FAIL("Unexpected additional message 0x%02x from 0x%04x",
700 msg.seq, msg.ctx.addr);
701 }
702
703 LOG_INF("Testing overflow with segmented message preempting another segmented...");
704
705 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
706
707 exp_seq += 2; /* Skipping the first message in Friend Queue. */
708 ASSERT_OK_MSG(bt_mesh_test_recv_msg(&msg, K_SECONDS(2)),
709 "Receive the first unseg msg failed");
710 ASSERT_EQUAL(5, msg.len);
711 ASSERT_EQUAL(cfg->addr, msg.ctx.recv_dst);
712 ASSERT_EQUAL(exp_seq, msg.seq);
713
714 exp_seq++;
715 ASSERT_OK_MSG(bt_mesh_test_recv_msg(&msg, K_SECONDS(20)),
716 "Receive the seg msg failed");
717 ASSERT_EQUAL(BT_MESH_SDU_UNSEG_MAX * (CONFIG_BT_MESH_FRIEND_QUEUE_SIZE - 2),
718 msg.len);
719 ASSERT_EQUAL(cfg->addr, msg.ctx.recv_dst);
720 ASSERT_EQUAL(exp_seq, msg.seq);
721
722 ASSERT_OK_MSG(bt_mesh_test_recv_msg(&msg, K_SECONDS(2)),
723 "Receive the second unseg msg failed");
724 ASSERT_EQUAL(5, msg.len);
725 ASSERT_EQUAL(cfg->addr, msg.ctx.recv_dst);
726
727 /* Not expecting any more messages from friend */
728 err = bt_mesh_test_recv_msg(&msg, K_SECONDS(10));
729 if (!err) {
730 FAIL("Unexpected additional message 0x%02x from 0x%04x",
731 msg.seq, msg.ctx.addr);
732 }
733
734 PASS();
735 }
736
737 /** As an LPN, receive packets on group and virtual addresses from mesh device
738 * and friend. Then, add a second group address (while the friendship is
739 * established), and receive on that as well.
740 */
test_lpn_group(void)741 static void test_lpn_group(void)
742 {
743 struct bt_mesh_test_msg msg;
744 const struct bt_mesh_va *va;
745 uint16_t vaddr;
746 uint8_t status = 0;
747 int err;
748
749 bt_mesh_test_setup();
750 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
751
752 err = bt_mesh_cfg_cli_mod_sub_add(0, cfg->addr, cfg->addr, GROUP_ADDR,
753 TEST_MOD_ID, &status);
754 if (err || status) {
755 FAIL("Group addr add failed with err %d status 0x%x", err,
756 status);
757 }
758
759 err = bt_mesh_cfg_cli_mod_sub_va_add(0, cfg->addr, cfg->addr, test_va_uuid,
760 TEST_MOD_ID, &vaddr, &status);
761 if (err || status) {
762 FAIL("VA addr add failed with err %d status 0x%x", err, status);
763 }
764
765 va = bt_mesh_va_find(test_va_uuid);
766 ASSERT_TRUE(va != NULL);
767 ASSERT_EQUAL(vaddr, va->addr);
768
769 bt_mesh_lpn_set(true);
770 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_LPN_ESTABLISHED,
771 K_SECONDS(5)), "LPN not established");
772 bt_mesh_test_friendship_evt_clear(BT_MESH_TEST_LPN_POLLED);
773
774 /* Send a message to the other mesh device to indicate that the
775 * friendship has been established. Give the other device a time to
776 * start up first.
777 */
778 k_sleep(K_MSEC(10));
779 ASSERT_OK(bt_mesh_test_send(other_cfg.addr, NULL, 5, 0, K_SECONDS(1)));
780
781 k_sleep(K_SECONDS(5));
782 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
783
784 /* From other device */
785 ASSERT_OK(bt_mesh_test_recv_msg(&msg, K_SECONDS(1)));
786 if (msg.ctx.recv_dst != GROUP_ADDR || msg.ctx.addr != other_cfg.addr) {
787 FAIL("Unexpected message: 0x%04x -> 0x%04x", msg.ctx.addr,
788 msg.ctx.recv_dst);
789 }
790
791 ASSERT_OK(bt_mesh_test_recv_msg(&msg, K_SECONDS(1)));
792 if (msg.ctx.recv_dst != va->addr || msg.ctx.addr != other_cfg.addr ||
793 msg.ctx.uuid != va->uuid) {
794 FAIL("Unexpected message: 0x%04x -> 0x%04x", msg.ctx.addr,
795 msg.ctx.recv_dst);
796 }
797
798 k_sleep(K_SECONDS(5));
799 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
800
801 /* From friend */
802 ASSERT_OK(bt_mesh_test_recv_msg(&msg, K_SECONDS(1)));
803 if (msg.ctx.recv_dst != GROUP_ADDR || msg.ctx.addr != friend_cfg.addr) {
804 FAIL("Unexpected message: 0x%04x -> 0x%04x", msg.ctx.addr,
805 msg.ctx.recv_dst);
806 }
807
808 ASSERT_OK(bt_mesh_test_recv_msg(&msg, K_SECONDS(1)));
809 if (msg.ctx.recv_dst != va->addr || msg.ctx.addr != friend_cfg.addr) {
810 FAIL("Unexpected message: 0x%04x -> 0x%04x", msg.ctx.addr,
811 msg.ctx.recv_dst);
812 }
813
814 k_sleep(K_SECONDS(1));
815
816 LOG_INF("Adding second group addr");
817
818 /* Add a new group addr, then receive on it to ensure that the friend
819 * has added it to the subscription list.
820 */
821 err = bt_mesh_cfg_cli_mod_sub_add(0, cfg->addr, cfg->addr, GROUP_ADDR + 1,
822 TEST_MOD_ID, &status);
823 if (err || status) {
824 FAIL("Group addr add failed with err %d status 0x%x", err,
825 status);
826 }
827
828 k_sleep(K_SECONDS(5));
829 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
830
831 /* From friend on second group address */
832 ASSERT_OK(bt_mesh_test_recv_msg(&msg, K_SECONDS(1)));
833 if (msg.ctx.recv_dst != GROUP_ADDR + 1 ||
834 msg.ctx.addr != friend_cfg.addr) {
835 FAIL("Unexpected message: 0x%04x -> 0x%04x", msg.ctx.addr,
836 msg.ctx.recv_dst);
837 }
838
839 PASS();
840 }
841
842 /** As an LPN, send packets to own address to ensure that this is handled by
843 * loopback mechanism, and ignored by friend.
844 *
845 * Adds test coverage for regression in #30657.
846 */
test_lpn_loopback(void)847 static void test_lpn_loopback(void)
848 {
849 struct bt_mesh_test_msg msg;
850 const struct bt_mesh_va *va;
851 uint16_t vaddr;
852 uint8_t status = 0;
853 int err;
854
855 bt_mesh_test_setup();
856 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
857
858 err = bt_mesh_cfg_cli_mod_sub_add(0, cfg->addr, cfg->addr, GROUP_ADDR,
859 TEST_MOD_ID, &status);
860 if (err || status) {
861 FAIL("Group addr add failed with err %d status 0x%x", err,
862 status);
863 }
864
865 err = bt_mesh_cfg_cli_mod_sub_va_add(0, cfg->addr, cfg->addr, test_va_uuid,
866 TEST_MOD_ID, &vaddr, &status);
867 if (err || status) {
868 FAIL("VA addr add failed with err %d status 0x%x", err, status);
869 }
870
871 va = bt_mesh_va_find(test_va_uuid);
872 ASSERT_TRUE(va != NULL);
873 ASSERT_EQUAL(vaddr, va->addr);
874
875 bt_mesh_lpn_set(true);
876 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_LPN_ESTABLISHED,
877 K_SECONDS(5)),
878 "LPN not established");
879 bt_mesh_test_friendship_evt_clear(BT_MESH_TEST_LPN_POLLED);
880
881 k_sleep(K_SECONDS(1));
882
883 /* Loopback on unicast, shouldn't even leave the device */
884 ASSERT_OK(bt_mesh_test_send_async(cfg->addr, NULL, 5, 0, NULL, NULL));
885 ASSERT_OK(bt_mesh_test_recv(5, cfg->addr, NULL, K_SECONDS(1)));
886
887 /* Loopback on group address, should not come back from the friend */
888 ASSERT_OK(bt_mesh_test_send_async(GROUP_ADDR, NULL, 5, 0, NULL, NULL));
889 ASSERT_OK(bt_mesh_test_recv(5, GROUP_ADDR, NULL, K_SECONDS(1)));
890
891 /* Loopback on virtual address, should not come back from the friend */
892 ASSERT_OK(bt_mesh_test_send_async(va->addr, va->uuid, 5, 0, NULL, NULL));
893 ASSERT_OK(bt_mesh_test_recv(5, va->addr, va->uuid, K_SECONDS(1)));
894
895 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
896 err = bt_mesh_test_recv_msg(&msg, K_SECONDS(2));
897 if (err != -ETIMEDOUT) {
898 FAIL("Unexpected receive status: %d", err);
899 }
900
901 /* Loopback on virtual address, should not come back from the friend */
902 ASSERT_OK(bt_mesh_test_send_async(va->addr, va->uuid, 5, 0, NULL, NULL));
903 ASSERT_OK(bt_mesh_test_recv(5, va->addr, va->uuid, K_SECONDS(1)));
904
905 k_sleep(K_SECONDS(2));
906
907 /* Poll the friend and make sure we don't receive any messages: */
908 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
909 err = bt_mesh_test_recv_msg(&msg, K_SECONDS(5));
910 if (err != -ETIMEDOUT) {
911 FAIL("Unexpected receive status: %d", err);
912 }
913
914 PASS();
915 }
916
917 /* Mesh device test functions */
918
919 /** Without engaging in a friendship, communicate with an LPN through a friend
920 * node.
921 */
test_other_msg(void)922 static void test_other_msg(void)
923 {
924 uint8_t status;
925 int err;
926
927 bt_mesh_test_setup();
928 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
929
930 /* When this device and a friend device receive segments from LPN both start
931 * sending data. This device sends transport ack. Friend relays LPN's segment.
932 * As a consequence of this, the Friend loses transport ack, and the segmented
933 * transaction is never ended. To avoid such behavior this setting will stretch
934 * in time transport ack sending.
935 */
936 err = bt_mesh_cfg_cli_net_transmit_set(0, cfg->addr, BT_MESH_TRANSMIT(3, 30), &status);
937 if (err || status != BT_MESH_TRANSMIT(3, 30)) {
938 FAIL("Net transmit set failed (err %d, status %u)", err, status);
939 }
940
941 /* Receive an unsegmented message from the LPN. */
942 ASSERT_OK_MSG(bt_mesh_test_recv(5, cfg->addr, NULL, K_FOREVER),
943 "Failed to receive from LPN");
944
945 /* Minor delay that allows LPN's adv to complete sending. */
946 k_sleep(K_SECONDS(2));
947
948 /* Send an unsegmented message to the LPN */
949 ASSERT_OK_MSG(bt_mesh_test_send(LPN_ADDR_START, NULL, 5, 0, K_NO_WAIT),
950 "Failed to send to LPN");
951
952 /* Receive a segmented message from the LPN. */
953 ASSERT_OK_MSG(bt_mesh_test_recv(15, cfg->addr, NULL, K_FOREVER),
954 "Failed to receive from LPN");
955
956 /* Minor delay that allows LPN's adv to complete sending. */
957 k_sleep(K_SECONDS(2));
958
959 /* Send a segmented message to the friend. */
960 ASSERT_OK_MSG(bt_mesh_test_send(LPN_ADDR_START, NULL, 15, 0, K_FOREVER),
961 "Send to LPN failed");
962
963 /* Receive an unsegmented message from the LPN, originally sent with
964 * friend credentials.
965 */
966 ASSERT_OK_MSG(bt_mesh_test_recv(1, cfg->addr, NULL, K_FOREVER),
967 "Failed to receive from LPN");
968
969 PASS();
970 }
971
972 /** Without engaging in a friendship, send group and virtual addr messages to
973 * the LPN.
974 */
test_other_group(void)975 static void test_other_group(void)
976 {
977 const struct bt_mesh_va *va;
978
979 bt_mesh_test_setup();
980 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
981
982 ASSERT_OK(bt_mesh_va_add(test_va_uuid, &va));
983
984 /* Wait for LPN to send us a message after establishing the friendship */
985 ASSERT_OK(bt_mesh_test_recv(5, cfg->addr, NULL, K_SECONDS(1)));
986
987 /* Send a group message to the LPN */
988 ASSERT_OK_MSG(bt_mesh_test_send(GROUP_ADDR, NULL, 5, 0, K_SECONDS(1)),
989 "Failed to send to LPN");
990 /* Send a virtual message to the LPN */
991 ASSERT_OK_MSG(bt_mesh_test_send(va->addr, va->uuid, 5, 0, K_SECONDS(1)),
992 "Failed to send to LPN");
993
994 PASS();
995 }
996
997 /** LPN disable test.
998 *
999 * Check that toggling lpn_set() results in correct disabled state
1000 */
test_lpn_disable(void)1001 static void test_lpn_disable(void)
1002 {
1003 bt_mesh_test_setup();
1004 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
1005
1006 bt_mesh_lpn_set(true);
1007 bt_mesh_lpn_set(false);
1008
1009 if (!bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_LPN_POLLED, K_SECONDS(30))) {
1010 FAIL("LPN connection polled unexpectedly");
1011 }
1012
1013 PASS();
1014 }
1015
1016 /** LPN terminate cb test.
1017 *
1018 * Check that terminate cb is not triggered when there is no established
1019 * connection.
1020 */
test_lpn_term_cb_check(void)1021 static void test_lpn_term_cb_check(void)
1022 {
1023 bt_mesh_test_setup();
1024 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
1025
1026 bt_mesh_lpn_set(true);
1027 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_LPN_POLLED,
1028 K_MSEC(1000)), "Friend never polled");
1029 bt_mesh_lpn_set(false);
1030
1031 if (!bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_LPN_TERMINATED, K_SECONDS(30))) {
1032 FAIL("LPN terminate CB triggered unexpectedly");
1033 }
1034
1035 PASS();
1036 }
1037
1038 /** Test that LPN sends only one Subscription List Add and only one Subscription List Remove message
1039 * to Friend when LPN is subscribed to 2 virtual addresses with collision.
1040 */
test_lpn_va_collision(void)1041 static void test_lpn_va_collision(void)
1042 {
1043 struct bt_mesh_test_msg msg;
1044 const struct bt_mesh_va *va[2];
1045 uint16_t vaddr;
1046 uint8_t status = 0;
1047 int err;
1048
1049 bt_mesh_test_setup();
1050 bt_mesh_test_friendship_init(CONFIG_BT_MESH_FRIEND_LPN_COUNT);
1051
1052 /* Subscripbe LPN on both virtual address with collision. */
1053 for (int i = 0; i < ARRAY_SIZE(test_va_col_uuid); i++) {
1054 err = bt_mesh_cfg_cli_mod_sub_va_add(0, cfg->addr, cfg->addr, test_va_col_uuid[i],
1055 TEST_MOD_ID, &vaddr, &status);
1056 if (err || status) {
1057 FAIL("VA addr add failed with err %d status 0x%x", err, status);
1058 }
1059
1060 ASSERT_EQUAL(test_va_col_addr, vaddr);
1061
1062 va[i] = bt_mesh_va_find(test_va_col_uuid[i]);
1063 ASSERT_TRUE(va[i] != NULL);
1064 ASSERT_EQUAL(vaddr, va[i]->addr);
1065 }
1066
1067 bt_mesh_lpn_set(true);
1068 ASSERT_OK_MSG(bt_mesh_test_friendship_evt_wait(BT_MESH_TEST_LPN_ESTABLISHED,
1069 K_SECONDS(5)), "LPN not established");
1070 bt_mesh_test_friendship_evt_clear(BT_MESH_TEST_LPN_POLLED);
1071
1072 LOG_INF("Step 1: Waiting for msgs from Friend");
1073
1074 /* Let Friend prepare messages and the poll them. */
1075 k_sleep(K_SECONDS(3));
1076 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
1077 /* LPN shall receive both messages. */
1078 for (int i = 0; i < ARRAY_SIZE(test_va_col_uuid); i++) {
1079 ASSERT_OK(bt_mesh_test_recv_msg(&msg, K_SECONDS(10)));
1080 if (msg.ctx.recv_dst != va[i]->addr ||
1081 msg.ctx.uuid != va[i]->uuid ||
1082 msg.ctx.addr != friend_cfg.addr) {
1083 FAIL("Unexpected message: 0x%04x -> 0x%04x, uuid: %p", msg.ctx.addr,
1084 msg.ctx.recv_dst, msg.ctx.uuid);
1085 }
1086 }
1087 /* Wait for the extra poll timeout in friend_wait_for_polls(). */
1088 k_sleep(K_SECONDS(3));
1089
1090 LOG_INF("Unsubscribing from the first address.");
1091
1092 /* Remove the first virtual address from subscription and poll messages from Friend. This
1093 * call shall not generate Friend Subscription List Remove message because LPN is still
1094 * subscribed to another Label UUID with the same virtual address.
1095 */
1096 err = bt_mesh_cfg_cli_mod_sub_va_del(0, cfg->addr, cfg->addr, test_va_col_uuid[0],
1097 TEST_MOD_ID, &vaddr, &status);
1098 if (err || status) {
1099 FAIL("Virtual addr add failed with err %d status 0x%x", err, status);
1100 }
1101 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
1102 /* Wait for the extra poll timeout in friend_wait_for_polls(). */
1103 k_sleep(K_SECONDS(3));
1104
1105 LOG_INF("Step 2: Waiting for msgs from Friend");
1106
1107 /* LPN will still receive both messages as the virtual address is the same for both Label
1108 * UUIDs, but the first message shall not be decrypted and shall be dropped.
1109 */
1110 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
1111 ASSERT_OK(bt_mesh_test_recv_msg(&msg, K_SECONDS(1)));
1112 if (msg.ctx.recv_dst != va[1]->addr || msg.ctx.uuid != va[1]->uuid ||
1113 msg.ctx.addr != friend_cfg.addr) {
1114 FAIL("Unexpected message: 0x%04x -> 0x%04x, uuid: %p", msg.ctx.addr,
1115 msg.ctx.recv_dst, msg.ctx.uuid);
1116 }
1117
1118 /* Check that there are no more messages from Friend. */
1119 err = bt_mesh_test_recv_msg(&msg, K_SECONDS(1));
1120 if (!err) {
1121 FAIL("Unexpected message: 0x%04x -> 0x%04x, uuid: %p", msg.ctx.addr,
1122 msg.ctx.recv_dst, msg.ctx.uuid);
1123 }
1124 /* Wait for the extra poll timeout in friend_wait_for_polls(). */
1125 k_sleep(K_SECONDS(3));
1126
1127 LOG_INF("Unsubscribing from the second address.");
1128
1129 /* Unsubscribe from the second address. Now there are no subscriptions to the same virtual
1130 * address. LPN shall send Subscription List Remove message.
1131 */
1132 err = bt_mesh_cfg_cli_mod_sub_va_del(0, cfg->addr, cfg->addr, test_va_col_uuid[1],
1133 TEST_MOD_ID, &vaddr, &status);
1134 if (err || status) {
1135 FAIL("Virtual addr del failed with err %d status 0x%x", err, status);
1136 }
1137 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
1138 /* Wait for the extra poll timeout in friend_wait_for_polls(). */
1139 k_sleep(K_SECONDS(3));
1140
1141 LOG_INF("Step 3: Waiting for msgs from Friend");
1142
1143 /* As now there shall be no virtual addresses in the subscription list, Friend Queue shall
1144 * be empty.
1145 */
1146 ASSERT_OK_MSG(bt_mesh_lpn_poll(), "Poll failed");
1147 for (int i = 0; i < ARRAY_SIZE(test_va_col_uuid); i++) {
1148 err = bt_mesh_test_recv_msg(&msg, K_SECONDS(1));
1149 if (!err) {
1150 FAIL("Unexpected message: 0x%04x -> 0x%04x, uuid: %p", msg.ctx.addr,
1151 msg.ctx.recv_dst, msg.ctx.uuid);
1152 }
1153 }
1154
1155 PASS();
1156 }
1157
1158 #define TEST_CASE(role, name, description) \
1159 { \
1160 .test_id = "friendship_" #role "_" #name, \
1161 .test_descr = description, \
1162 .test_post_init_f = test_##role##_init, \
1163 .test_tick_f = bt_mesh_test_timeout, \
1164 .test_main_f = test_##role##_##name, \
1165 }
1166
1167 static const struct bst_test_instance test_connect[] = {
1168 TEST_CASE(friend, est, "Friend: establish friendship"),
1169 TEST_CASE(friend, est_multi, "Friend: establish multiple friendships"),
1170 TEST_CASE(friend, msg, "Friend: message exchange"),
1171 TEST_CASE(friend, overflow, "Friend: message queue overflow"),
1172 TEST_CASE(friend, group, "Friend: send to group addrs"),
1173 TEST_CASE(friend, no_est, "Friend: do not establish friendship"),
1174 TEST_CASE(friend, va_collision, "Friend: send to virtual addrs with collision"),
1175
1176 TEST_CASE(lpn, est, "LPN: establish friendship"),
1177 TEST_CASE(lpn, msg_frnd, "LPN: message exchange with friend"),
1178 TEST_CASE(lpn, msg_mesh, "LPN: message exchange with mesh"),
1179 TEST_CASE(lpn, re_est, "LPN: re-establish friendship"),
1180 TEST_CASE(lpn, poll, "LPN: poll before timeout"),
1181 TEST_CASE(lpn, overflow, "LPN: message queue overflow"),
1182 TEST_CASE(lpn, group, "LPN: receive on group addrs"),
1183 TEST_CASE(lpn, loopback, "LPN: send to loopback addrs"),
1184 TEST_CASE(lpn, disable, "LPN: disable LPN"),
1185 TEST_CASE(lpn, term_cb_check, "LPN: no terminate cb trigger"),
1186 TEST_CASE(lpn, va_collision, "LPN: receive on virtual addrs with collision"),
1187
1188 TEST_CASE(other, msg, "Other mesh device: message exchange"),
1189 TEST_CASE(other, group, "Other mesh device: send to group addrs"),
1190 BSTEST_END_MARKER
1191 };
1192
test_friendship_install(struct bst_test_list * tests)1193 struct bst_test_list *test_friendship_install(struct bst_test_list *tests)
1194 {
1195 tests = bst_add_tests(tests, test_connect);
1196 return tests;
1197 }
1198