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