1 /*
2 * Copyright (c) 2021 Nordic Semiconductor
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <zephyr/kernel.h>
7 #include <zephyr/bluetooth/hci.h>
8 #include "mesh_test.h"
9 #include "mesh/net.h"
10 #include "mesh/beacon.h"
11 #include "mesh/mesh.h"
12 #include "mesh/foundation.h"
13 #include "mesh/crypto.h"
14 #include "argparse.h"
15 #include "mesh/proxy_cli.h"
16 #include "mesh/proxy.h"
17
18 #define LOG_MODULE_NAME test_beacon
19
20 #include <zephyr/logging/log.h>
21 LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_INF);
22
23 #define GROUP_ADDR 0xc000
24 #define WAIT_TIME 60 /*seconds*/
25 #define MULT_NETKEYS_WAIT_TIME 350 /*seconds*/
26 #define BEACON_INTERVAL_WAIT_TIME 750 /*seconds*/
27 #define BEACON_INTERVAL 10 /*seconds*/
28
29 #define BEACON_TYPE_SECURE 0x01
30 #define BEACON_TYPE_PRIVATE 0x02
31
32 static uint8_t test_net_key_2[16] = { 0xca, 0x11, 0xab, 0x1e };
33 static struct {
34 uint8_t primary[16];
35 uint8_t secondary[16];
36 } net_key_pairs[] = {
37 { "\x01\x02", "\x03\x04" },
38 { "\x11\x12", "\x13\x14" },
39 { "\x21\x22", "\x23\x24" },
40 { "\x31\x32", "\x33\x34" },
41 };
42
43 extern enum bst_result_t bst_result;
44
45 static const struct bt_mesh_test_cfg tx_cfg = {
46 .addr = 0x0001,
47 .dev_key = { 0x01 },
48 };
49 static const struct bt_mesh_test_cfg rx_cfg = {
50 .addr = 0x0002,
51 .dev_key = { 0x02 },
52 };
53
54 typedef void (*snb_cb)(const struct bt_mesh_snb *snb);
55
56 static snb_cb snb_cb_ptr;
57 static struct k_sem beacon_sem;
58
snb_received(const struct bt_mesh_snb * snb)59 static void snb_received(const struct bt_mesh_snb *snb)
60 {
61 if (snb_cb_ptr) {
62 snb_cb_ptr(snb);
63 }
64 }
65
66 BT_MESH_BEACON_CB_DEFINE(snb) = {
67 .snb_received = snb_received,
68 };
69
70 /* Setting for scanner defining what beacon is expected next, SNB as default */
71 static uint8_t expected_beacon = BEACON_TYPE_SECURE;
72 static struct bt_mesh_cfg_cli cfg_cli;
73
74 static struct bt_mesh_priv_beacon_cli priv_beacon_cli;
75
76 static const struct bt_mesh_comp prb_comp = {
77 .elem =
78 (const struct bt_mesh_elem[]){
79 BT_MESH_ELEM(1,
80 MODEL_LIST(BT_MESH_MODEL_CFG_SRV,
81 BT_MESH_MODEL_CFG_CLI(&cfg_cli),
82 BT_MESH_MODEL_PRIV_BEACON_SRV,
83 BT_MESH_MODEL_PRIV_BEACON_CLI(&priv_beacon_cli)),
84 BT_MESH_MODEL_NONE),
85 },
86 .elem_count = 1,
87 };
88
89 static struct bt_mesh_prov prov;
90 static uint8_t net_key[16] = { 1, 2, 3 };
91 const uint8_t app_key[16] = { 4, 5, 6 };
92 static uint8_t net_key_new[16] = { 7, 8, 9 };
93
94 static uint8_t last_random[13];
95
96 static bt_addr_le_t last_beacon_adv_addr;
97
98 static struct bt_mesh_key priv_beacon_key;
99
100 static int random_interval;
101
test_args_parse(int argc,char * argv[])102 static void test_args_parse(int argc, char *argv[])
103 {
104 bs_args_struct_t args_struct[] = {
105 {
106 .dest = &random_interval,
107 .type = 'i',
108 .name = "{Random interval}",
109 .option = "rand-int",
110 .descript = "Random interval to be set for Private Beacon"
111 },
112 };
113
114 bs_args_parse_all_cmd_line(argc, argv, args_struct);
115 }
116
test_tx_init(void)117 static void test_tx_init(void)
118 {
119 bt_mesh_test_cfg_set(&tx_cfg, WAIT_TIME);
120 }
121
test_rx_init(void)122 static void test_rx_init(void)
123 {
124 bt_mesh_test_cfg_set(&rx_cfg, WAIT_TIME);
125 }
126
ivu_log(void)127 static void ivu_log(void)
128 {
129 LOG_DBG("ivi: %i", bt_mesh.iv_index);
130 LOG_DBG("flags:");
131
132 if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR)) {
133 LOG_DBG("IVU initiator");
134 }
135
136 if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS)) {
137 LOG_DBG("IVU in progress");
138 }
139
140 if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_PENDING)) {
141 LOG_DBG("IVU pending");
142 }
143
144 if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST)) {
145 LOG_DBG("IVU in test mode");
146 }
147 }
148
tx_on_iv_update_test(void)149 static void tx_on_iv_update_test(void)
150 {
151 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR));
152 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
153 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_PENDING));
154 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST));
155 ASSERT_TRUE(bt_mesh.iv_index == 0);
156
157 /* shift beaconing time line to avoid boundary cases. */
158 k_sleep(K_SECONDS(1));
159
160 bt_mesh_iv_update_test(true);
161 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST));
162
163 ASSERT_TRUE(bt_mesh_iv_update());
164 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
165 ASSERT_TRUE(bt_mesh.iv_index == 1);
166
167 k_sleep(K_SECONDS(BEACON_INTERVAL));
168
169 ASSERT_TRUE(!bt_mesh_iv_update());
170 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
171 ASSERT_TRUE(bt_mesh.iv_index == 1);
172
173 k_sleep(K_SECONDS(BEACON_INTERVAL));
174
175 ASSERT_TRUE(bt_mesh_iv_update());
176 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
177 ASSERT_TRUE(bt_mesh.iv_index == 2);
178
179 k_sleep(K_SECONDS(BEACON_INTERVAL));
180
181 PASS();
182 }
183
test_tx_on_iv_update(void)184 static void test_tx_on_iv_update(void)
185 {
186 bt_mesh_test_setup();
187 tx_on_iv_update_test();
188 }
189
test_rx_on_iv_update(void)190 static void test_rx_on_iv_update(void)
191 {
192 bt_mesh_test_setup();
193 /* disable beaconing from Rx device to prevent
194 * the time line adaptation due to observation algorithm.
195 */
196 bt_mesh_beacon_disable();
197 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR));
198 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
199 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_PENDING));
200 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST));
201 ASSERT_TRUE(bt_mesh.iv_index == 0);
202
203 /* shift beaconing time line to avoid boundary cases. */
204 k_sleep(K_SECONDS(1));
205
206 bt_mesh_iv_update_test(true);
207 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST));
208 ivu_log();
209
210 k_sleep(K_SECONDS(BEACON_INTERVAL));
211
212 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
213 ASSERT_TRUE(bt_mesh.iv_index == 1);
214 ivu_log();
215
216 k_sleep(K_SECONDS(BEACON_INTERVAL));
217
218 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
219 ASSERT_TRUE(bt_mesh.iv_index == 1);
220 ivu_log();
221
222 k_sleep(K_SECONDS(BEACON_INTERVAL));
223
224 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
225 ASSERT_TRUE(bt_mesh.iv_index == 2);
226 ivu_log();
227
228 PASS();
229 }
230
tx_on_key_refresh_test(void)231 static void tx_on_key_refresh_test(void)
232 {
233 const uint8_t new_key[16] = {0x01};
234 uint8_t phase;
235 uint8_t status;
236
237 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
238 ASSERT_TRUE(status == STATUS_SUCCESS);
239 ASSERT_TRUE(phase == BT_MESH_KR_NORMAL);
240
241 /* shift beaconing time line to avoid boundary cases. */
242 k_sleep(K_SECONDS(1));
243
244 status = bt_mesh_subnet_update(BT_MESH_KEY_PRIMARY, new_key);
245 ASSERT_TRUE(status == STATUS_SUCCESS);
246 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
247 ASSERT_TRUE(status == STATUS_SUCCESS);
248 ASSERT_TRUE(phase == BT_MESH_KR_PHASE_1);
249
250 k_sleep(K_SECONDS(BEACON_INTERVAL));
251
252 phase = BT_MESH_KR_PHASE_2;
253 status = bt_mesh_subnet_kr_phase_set(BT_MESH_KEY_PRIMARY, &phase);
254 ASSERT_TRUE(status == STATUS_SUCCESS);
255 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
256 ASSERT_TRUE(status == STATUS_SUCCESS);
257 ASSERT_TRUE(phase == BT_MESH_KR_PHASE_2);
258
259 k_sleep(K_SECONDS(BEACON_INTERVAL));
260
261 phase = BT_MESH_KR_PHASE_3;
262 status = bt_mesh_subnet_kr_phase_set(BT_MESH_KEY_PRIMARY, &phase);
263 ASSERT_TRUE(status == STATUS_SUCCESS);
264 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
265 ASSERT_TRUE(status == STATUS_SUCCESS);
266 ASSERT_TRUE(phase == BT_MESH_KR_NORMAL);
267
268 k_sleep(K_SECONDS(BEACON_INTERVAL));
269
270 PASS();
271 }
272
test_tx_on_key_refresh(void)273 static void test_tx_on_key_refresh(void)
274 {
275 bt_mesh_test_setup();
276 tx_on_key_refresh_test();
277 }
278
test_rx_on_key_refresh(void)279 static void test_rx_on_key_refresh(void)
280 {
281 const uint8_t new_key[16] = {0x01};
282 uint8_t phase;
283 uint8_t status;
284
285 bt_mesh_test_setup();
286 /* disable beaconing from Rx device to prevent
287 * the time line adaptation due to observation algorithm.
288 */
289 bt_mesh_beacon_disable();
290
291 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
292 ASSERT_TRUE(status == STATUS_SUCCESS);
293 ASSERT_TRUE(phase == BT_MESH_KR_NORMAL);
294
295 /* shift beaconing time line to avoid boundary cases. */
296 k_sleep(K_SECONDS(1));
297
298 status = bt_mesh_subnet_update(BT_MESH_KEY_PRIMARY, new_key);
299 ASSERT_TRUE(status == STATUS_SUCCESS);
300 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
301 ASSERT_TRUE(status == STATUS_SUCCESS);
302 ASSERT_TRUE(phase == BT_MESH_KR_PHASE_1);
303
304 k_sleep(K_SECONDS(BEACON_INTERVAL));
305
306 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
307 ASSERT_TRUE(status == STATUS_SUCCESS);
308 ASSERT_TRUE(phase == BT_MESH_KR_PHASE_1);
309
310 k_sleep(K_SECONDS(BEACON_INTERVAL));
311
312 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
313 ASSERT_TRUE(status == STATUS_SUCCESS);
314 ASSERT_TRUE(phase == BT_MESH_KR_PHASE_2);
315
316 k_sleep(K_SECONDS(BEACON_INTERVAL));
317
318 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
319 ASSERT_TRUE(status == STATUS_SUCCESS);
320 ASSERT_TRUE(phase == BT_MESH_KR_NORMAL);
321
322 PASS();
323 }
324
325 static struct k_sem observer_sem;
326 static struct {
327 uint8_t flags;
328 uint32_t iv_index;
329 uint8_t random[13];
330 uint64_t pp_hash;
331 uint64_t pp_random;
332 uint64_t net_id;
333 bt_addr_le_t adv_addr;
334 bool (*process_cb)(const uint8_t *net_id, void *ctx);
335 void *user_ctx;
336 } beacon;
337
beacon_scan_cb(const bt_addr_le_t * addr,int8_t rssi,uint8_t adv_type,struct net_buf_simple * buf)338 static void beacon_scan_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type,
339 struct net_buf_simple *buf)
340 {
341 const uint8_t *net_id;
342 uint8_t ad_data_type, beacon_type, length;
343
344 ASSERT_EQUAL(BT_GAP_ADV_TYPE_ADV_NONCONN_IND, adv_type);
345
346 length = net_buf_simple_pull_u8(buf);
347 ASSERT_EQUAL(buf->len, length);
348 ad_data_type = net_buf_simple_pull_u8(buf);
349
350 if (ad_data_type != BT_DATA_MESH_BEACON) {
351 return;
352 }
353
354 beacon_type = net_buf_simple_pull_u8(buf);
355 if (expected_beacon == BEACON_TYPE_SECURE) {
356 ASSERT_EQUAL(expected_beacon, beacon_type);
357 beacon.flags = net_buf_simple_pull_u8(buf);
358 net_id = net_buf_simple_pull_mem(buf, 8);
359 beacon.iv_index = net_buf_simple_pull_be32(buf);
360 }
361 else if (expected_beacon == BEACON_TYPE_PRIVATE) {
362 uint8_t private_beacon_data[5];
363
364 ASSERT_EQUAL(expected_beacon, beacon_type);
365 memcpy(beacon.random, buf->data, 13);
366 bt_addr_le_copy(&beacon.adv_addr, addr);
367
368 bt_mesh_beacon_decrypt(&priv_beacon_key, &buf->data[0], &buf->data[13],
369 &buf->data[20], private_beacon_data);
370 beacon.flags = private_beacon_data[0];
371 beacon.iv_index = sys_get_be32(&private_beacon_data[1]);
372 }
373
374 if (!beacon.process_cb || beacon.process_cb(net_id, beacon.user_ctx)) {
375 k_sem_give(&observer_sem);
376 }
377 }
378
379 /* Listens to beacons */
wait_for_beacon(bt_le_scan_cb_t scan_cb,uint16_t wait,bool (* process_cb)(const uint8_t * net_id,void * ctx),void * ctx)380 static bool wait_for_beacon(bt_le_scan_cb_t scan_cb, uint16_t wait,
381 bool (*process_cb)(const uint8_t *net_id, void *ctx), void *ctx)
382 {
383 beacon.process_cb = process_cb;
384 beacon.user_ctx = ctx;
385
386 /* Listen to beacons ONLY for one beacon interval.
387 * Tests start quite often the waiting for the next beacon after
388 * transmission or receiving the previous one. If start waiting timer
389 * for BEACON_INTERVAL interval then timer expiration and receiving of
390 * the beacon happen about the same time. That is possible unstable behavior
391 * or failing some tests. To avoid this it is worth to add 1 second to
392 * waiting time (BEACON_INTERVAL + 1) to guarantee that beacon comes
393 * before timer expiration.
394 */
395 bool received = !bt_mesh_test_wait_for_packet(scan_cb, &observer_sem, wait);
396
397 /* Sleep a little to get to the next beacon interval. Otherwise, calling this function
398 * again will catch the old beacon. This happens due to a known bug in legacy advertiser,
399 * which transmits advertisements longer than should.
400 */
401 if (received && IS_ENABLED(CONFIG_BT_MESH_ADV_LEGACY)) {
402 k_sleep(K_SECONDS(1));
403 }
404
405 return received;
406 }
407
send_beacon(struct net_buf_simple * buf)408 static void send_beacon(struct net_buf_simple *buf)
409 {
410 struct bt_data ad;
411 int err;
412
413 ad.type = BT_DATA_MESH_BEACON;
414 ad.data = buf->data;
415 ad.data_len = buf->len;
416
417 err = bt_le_adv_start(BT_LE_ADV_NCONN, &ad, 1, NULL, 0);
418 if (err) {
419 FAIL("Advertising failed to start (err %d)\n", err);
420 }
421
422 LOG_INF("Advertising started\n");
423
424 k_sleep(K_MSEC(100));
425
426 err = bt_le_adv_stop();
427 if (err) {
428 FAIL("Unable to stop advertising");
429 }
430 }
431
beacon_create(struct net_buf_simple * buf,const uint8_t net_key[16],uint8_t flags,uint32_t iv_index)432 static void beacon_create(struct net_buf_simple *buf, const uint8_t net_key[16], uint8_t flags,
433 uint32_t iv_index)
434 {
435 struct bt_mesh_key beacon_key;
436 uint8_t net_id[8];
437 uint8_t auth[8];
438 int err;
439
440 err = bt_mesh_k3(net_key, net_id);
441 if (err) {
442 FAIL("Unable to generate Net ID");
443 }
444
445 err = bt_mesh_beacon_key(net_key, &beacon_key);
446 if (err) {
447 FAIL("Unable to generate beacon key");
448 }
449
450 err = bt_mesh_beacon_auth(&beacon_key, flags, net_id, iv_index, auth);
451 if (err) {
452 FAIL("Unable to generate auth value");
453 }
454
455 err = bt_mesh_key_destroy(&beacon_key);
456 if (err) {
457 FAIL("Unable to destroy beacon key");
458 }
459
460 net_buf_simple_reset(buf);
461 net_buf_simple_add_u8(buf, BEACON_TYPE_SECURE);
462 net_buf_simple_add_u8(buf, flags);
463 net_buf_simple_add_mem(buf, net_id, 8);
464 net_buf_simple_add_be32(buf, iv_index);
465 net_buf_simple_add_mem(buf, auth, 8);
466 }
467
468 /* Test reception of invalid beacons. */
corrupted_beacon_test(const uint8_t * offsets,ssize_t field_count,struct net_buf_simple * buf)469 static void corrupted_beacon_test(const uint8_t *offsets,
470 ssize_t field_count,
471 struct net_buf_simple *buf)
472 {
473 /* Send corrupted beacons */
474 for (int i = 0; i < field_count; i++) {
475 buf->data[offsets[i]] ^= 0xFF;
476 send_beacon(buf);
477 buf->data[offsets[i]] ^= 0xFF;
478 /* Ensure that interval is not affected. */
479 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
480 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
481 ASSERT_EQUAL(0x00, beacon.flags);
482 ASSERT_EQUAL(0x0000, beacon.iv_index);
483 }
484
485 /* Now the beacon payload is valid and it shall trigger IV Update on the node. It shall also
486 * increase the beacon interval. We delay the outgoing beacon for a couple of seconds to
487 * avoid near perfect syncing with the beacon interval cycle of the device we just received
488 * a beacon from.
489 */
490 k_sleep(K_SECONDS(3));
491 send_beacon(buf);
492
493 /* The beacon interval shall be changed and the node shall skip transmission of the next
494 * beacon.
495 */
496 ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
497 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
498 ASSERT_EQUAL(0x02, beacon.flags);
499 ASSERT_EQUAL(0x0001, beacon.iv_index);
500 }
501
test_tx_invalid(void)502 static void test_tx_invalid(void)
503 {
504 NET_BUF_SIMPLE_DEFINE(buf, 22);
505 /* Offsets of data to be corrupted: Flags, Network ID, IV Index, Authentication value */
506 uint8_t fields_offsets[4] = {1, 2, 10, 14};
507 int err;
508
509 bt_mesh_test_cfg_set(&tx_cfg, 130);
510 bt_mesh_crypto_init();
511 k_sem_init(&observer_sem, 0, 1);
512
513 err = bt_enable(NULL);
514 if (err) {
515 FAIL("Bluetooth init failed (err %d)", err);
516 return;
517 }
518
519 LOG_INF("Bluetooth initialized");
520
521 /* Let the rx node send the first beacon. */
522 k_sleep(K_SECONDS(5));
523
524 /* Create a valid beacon with IV Update Flag set to 1 and new IV Index. */
525 beacon_create(&buf, test_net_key, 0x02, 0x0001);
526
527 corrupted_beacon_test(fields_offsets, ARRAY_SIZE(fields_offsets), &buf);
528
529 PASS();
530 }
531
532 /* Test reception of invalid beacons. */
test_rx_invalid(void)533 static void test_rx_invalid(void)
534 {
535 bt_mesh_test_cfg_set(&rx_cfg, 130);
536 bt_mesh_test_setup();
537 bt_mesh_iv_update_test(true);
538
539 k_sleep(K_SECONDS(10));
540
541 for (size_t i = 0; i < 4; i++) {
542 ASSERT_FALSE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
543 ASSERT_EQUAL(0, bt_mesh.iv_index);
544
545 k_sleep(K_SECONDS((BEACON_INTERVAL + 1) * 2));
546 }
547
548 /* Only the last beacon shall change IV Update state. */
549 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
550 ASSERT_EQUAL(1, bt_mesh.iv_index);
551
552 PASS();
553 }
554
555 /* Test beacons reception with Key Refresh and IV Update on primary subnet. */
test_tx_kr_old_key(void)556 static void test_tx_kr_old_key(void)
557 {
558 NET_BUF_SIMPLE_DEFINE(buf, 22);
559 int err;
560
561 bt_mesh_test_cfg_set(&tx_cfg, 170);
562 bt_mesh_crypto_init();
563 k_sem_init(&observer_sem, 0, 1);
564
565 err = bt_enable(NULL);
566 if (err) {
567 FAIL("Bluetooth init failed (err %d)", err);
568 return;
569 }
570
571 LOG_INF("Bluetooth initialized");
572
573 /* Let the rx node send the first beacon. */
574 k_sleep(K_SECONDS(5));
575
576 /* The node has added a new Net Key. */
577
578 /* Send a beacon with Key Refresh flag set to 1, but secured with the old Net Key. The
579 * beacon shall not change Key Refresh phase, but should still be processed. The beacon
580 * interval shall be increased.
581 */
582 beacon_create(&buf, test_net_key, 0x01, 0x0000);
583 send_beacon(&buf);
584 ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
585 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
586 ASSERT_EQUAL(0x00, beacon.flags);
587 ASSERT_EQUAL(0x0000, beacon.iv_index);
588
589 /* The old Net Key can still initiate IV Index update. */
590 beacon_create(&buf, test_net_key, 0x02, 0x0001);
591 send_beacon(&buf);
592 ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
593 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
594 ASSERT_EQUAL(0x02, beacon.flags);
595 ASSERT_EQUAL(0x0001, beacon.iv_index);
596
597 /* Send beacon with Key Refresh flag set to 1, IV Update flag set to 1, but secured with
598 * the new Net Key. The node shall set Key Refresh phase to 2. The beacon interval shall
599 * be increased.
600 */
601 beacon_create(&buf, test_net_key_2, 0x03, 0x0001);
602 send_beacon(&buf);
603 ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
604 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
605 ASSERT_EQUAL(0x03, beacon.flags);
606 ASSERT_EQUAL(0x0001, beacon.iv_index);
607
608 /* Send beacon with Key Refresh flag set to 1, IV Update flag set to 0, but secured with
609 * the old Net Key. The beacon shall be rejected. The beacon interval shall not be changed.
610 */
611 beacon_create(&buf, test_net_key, 0x01, 0x0001);
612 send_beacon(&buf);
613 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
614 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
615 ASSERT_EQUAL(0x03, beacon.flags);
616 ASSERT_EQUAL(0x0001, beacon.iv_index);
617
618 /* Try the same with the new Net Key. Now the node shall change Key Refresh phase to 0. The
619 * beacon interval shall be increased.
620 */
621 beacon_create(&buf, test_net_key_2, 0x02, 0x0001);
622 send_beacon(&buf);
623 ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
624 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
625 ASSERT_EQUAL(0x02, beacon.flags);
626 ASSERT_EQUAL(0x0001, beacon.iv_index);
627
628 /* Send beacon with IV Update flag set to 0 and secured with the old Net Key. The beacon
629 * shall be ignored. The beacon interval shall not be changed.
630 */
631 beacon_create(&buf, test_net_key, 0x00, 0x0001);
632 send_beacon(&buf);
633 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
634 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
635 ASSERT_EQUAL(0x02, beacon.flags);
636 ASSERT_EQUAL(0x0001, beacon.iv_index);
637
638 /* Do the same, but secure beacon with the new Net Key. Now the node shall change IV Update
639 * flag to 0. The beacon interval shall be increased.
640 */
641 beacon_create(&buf, test_net_key_2, 0x00, 0x0001);
642 send_beacon(&buf);
643 ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
644 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
645 ASSERT_EQUAL(0x00, beacon.flags);
646 ASSERT_EQUAL(0x0001, beacon.iv_index);
647
648 PASS();
649 }
650
651 /* Test beacons reception with Key Refresh and IV Update on primary subnet. */
test_rx_kr_old_key(void)652 static void test_rx_kr_old_key(void)
653 {
654 uint8_t phase;
655 uint8_t status;
656 int err;
657
658 bt_mesh_test_cfg_set(&rx_cfg, 170);
659 bt_mesh_test_setup();
660 bt_mesh_iv_update_test(true);
661
662 err = bt_mesh_cfg_cli_net_key_update(0, cfg->addr, 0, test_net_key_2, &status);
663 if (err || status) {
664 FAIL("Net Key update failed (err %d, status %u)", err, status);
665 }
666
667 static struct {
668 uint8_t phase;
669 bool ivu;
670 uint32_t ivi;
671 } test_vector[] = {
672 /* Old Net Key, attempt to change Key Refresh phase to 2. */
673 { .phase = BT_MESH_KR_PHASE_1, .ivu = false, .ivi = 0 },
674 /* Old Net Key, changing IV Update state. */
675 { .phase = BT_MESH_KR_PHASE_1, .ivu = true, .ivi = 1 },
676 /* New Net Key, changing Key Refresh phase. */
677 { .phase = BT_MESH_KR_PHASE_2, .ivu = true, .ivi = 1 },
678 /* Old Net Key, attempt to change IV Update state. */
679 { .phase = BT_MESH_KR_PHASE_2, .ivu = true, .ivi = 1 },
680 /* New Net Key, changing Key Refresh phase to 0. */
681 { .phase = BT_MESH_KR_NORMAL, .ivu = true, .ivi = 1 },
682 /* Old Net Key, attempt to change IV Update state to Idle.*/
683 { .phase = BT_MESH_KR_NORMAL, .ivu = true, .ivi = 1 },
684 /* Net Net Key, changing IV Update state to Idle. */
685 { .phase = BT_MESH_KR_NORMAL, .ivu = false, .ivi = 1 },
686 };
687
688 k_sleep(K_SECONDS(8));
689
690 for (size_t i = 0; i < ARRAY_SIZE(test_vector); i++) {
691 status = bt_mesh_subnet_kr_phase_get(0, &phase);
692 if (status != STATUS_SUCCESS) {
693 FAIL("Unable to populate Key Refresh phase (status: %d)", status);
694 }
695
696 ASSERT_EQUAL(test_vector[i].phase, phase);
697 ASSERT_EQUAL(test_vector[i].ivu, atomic_test_bit(bt_mesh.flags,
698 BT_MESH_IVU_IN_PROGRESS));
699 ASSERT_EQUAL(test_vector[i].ivi, bt_mesh.iv_index);
700
701 k_sleep(K_SECONDS((BEACON_INTERVAL + 1) * 2));
702 }
703
704 PASS();
705 }
706
beacon_confirm_by_subnet(const uint8_t * net_id,void * ctx)707 static bool beacon_confirm_by_subnet(const uint8_t *net_id, void *ctx)
708 {
709 const uint8_t *expected_net_id = ctx;
710
711 return !memcmp(expected_net_id, net_id, 8);
712 }
713
beacon_confirm_all_subnets(const uint8_t * net_id,void * ctx)714 static bool beacon_confirm_all_subnets(const uint8_t *net_id, void *ctx)
715 {
716 static uint32_t counter;
717 int err;
718
719 for (size_t i = 0; i < ARRAY_SIZE(net_key_pairs); i++) {
720 uint8_t expected_net_id[8];
721
722 err = bt_mesh_k3(net_key_pairs[i].secondary, expected_net_id);
723 if (err) {
724 FAIL("Unable to generate Net ID");
725 }
726
727 if (!memcmp(expected_net_id, net_id, 8)) {
728 LOG_INF("Received beacon for Net Key Idx %d", (i + 1));
729 counter |= 1 << i;
730
731 ASSERT_EQUAL(0x00, beacon.flags);
732 ASSERT_EQUAL(0x0000, beacon.iv_index);
733 }
734 }
735
736 if (counter == BIT_MASK(ARRAY_SIZE(net_key_pairs))) {
737 counter = 0;
738 return true;
739 } else {
740 return false;
741 }
742 }
743
744 /* Test beacons rejection with multiple Net Keys. */
test_tx_multiple_netkeys(void)745 static void test_tx_multiple_netkeys(void)
746 {
747 NET_BUF_SIMPLE_DEFINE(buf, 22);
748 int err;
749
750 bt_mesh_test_cfg_set(&tx_cfg, MULT_NETKEYS_WAIT_TIME);
751 bt_mesh_crypto_init();
752 k_sem_init(&observer_sem, 0, 1);
753
754 err = bt_enable(NULL);
755 if (err) {
756 FAIL("Bluetooth init failed (err %d)", err);
757 return;
758 }
759
760 LOG_INF("Bluetooth initialized");
761
762 /* Let the rx node send the first beacon. */
763 k_sleep(K_SECONDS(5));
764
765 /* The node has added new Net Keys. */
766
767 for (size_t i = 0; i < ARRAY_SIZE(net_key_pairs); i++) {
768 uint8_t net_id_secondary[8];
769
770 err = bt_mesh_k3(net_key_pairs[i].secondary, net_id_secondary);
771 if (err) {
772 FAIL("Unable to generate Net ID");
773 }
774
775 /* Send beacon with Key Refresh flag set to 1, but secured with the old Net Key.
776 * The beacon shall be processed, but shall not change Key Refresh phase.
777 */
778 beacon_create(&buf, net_key_pairs[i].primary, 0x01, 0x0000);
779 send_beacon(&buf);
780 ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1,
781 beacon_confirm_by_subnet, &buf.data[2]));
782 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1,
783 beacon_confirm_by_subnet, &buf.data[2]));
784 ASSERT_EQUAL(0x00, beacon.flags);
785 ASSERT_EQUAL(0x0000, beacon.iv_index);
786
787 /* Wait for end of sending all beacons from the rx node before sending beacon back
788 * to prevent beacon collision.
789 */
790 k_sleep(K_MSEC(500));
791
792 /* Do the same, but secure beacon with the new Net Key. The node shall set Key
793 * Refresh phase to 2.
794 */
795 beacon_create(&buf, net_key_pairs[i].secondary, 0x01, 0x0000);
796 send_beacon(&buf);
797 ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1,
798 beacon_confirm_by_subnet, net_id_secondary));
799 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1,
800 beacon_confirm_by_subnet, net_id_secondary));
801 ASSERT_EQUAL(0x01, beacon.flags);
802 ASSERT_EQUAL(0x0000, beacon.iv_index);
803
804 /* Wait for end of sending all beacons from the rx node before sending beacon back
805 * to prevent beacon collision.
806 */
807 k_sleep(K_MSEC(500));
808
809 /* Send beacon with Key Refresh flag set to 0, but secured with the old Net Key.
810 * The beacon shall be rejected. The beacon interval shall not be changed.
811 */
812 beacon_create(&buf, net_key_pairs[i].primary, 0x00, 0x0000);
813 send_beacon(&buf);
814 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1,
815 beacon_confirm_by_subnet, net_id_secondary));
816 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1,
817 beacon_confirm_by_subnet, net_id_secondary));
818 ASSERT_EQUAL(0x01, beacon.flags);
819 ASSERT_EQUAL(0x0000, beacon.iv_index);
820
821 /* Wait for end of sending all beacons from the rx node before sending beacon back
822 * to prevent beacon collision.
823 */
824 k_sleep(K_MSEC(500));
825
826 /* Do the same with the new Net Key. Now the node shall change Key Refresh phase
827 * to 0. The beacon interval shall be increased.
828 */
829 beacon_create(&buf, net_key_pairs[i].secondary, 0x00, 0x0000);
830 send_beacon(&buf);
831 ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1,
832 beacon_confirm_by_subnet, net_id_secondary));
833 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1,
834 beacon_confirm_by_subnet, net_id_secondary));
835 ASSERT_EQUAL(0x00, beacon.flags);
836 ASSERT_EQUAL(0x0000, beacon.iv_index);
837
838 /* Wait for end of sending all beacons from the rx node before sending beacon back
839 * to prevent beacon collision.
840 */
841 k_sleep(K_MSEC(500));
842 }
843
844 /* Create a valid beacon secured with unknown Net Key. The node shall ignore the beacon and
845 * continue sending beacons regularly.
846 */
847 uint8_t unknown_net_key[16] = {0xde, 0xad, 0xbe, 0xef};
848
849 beacon_create(&buf, unknown_net_key, 0x00, 0x0000);
850 send_beacon(&buf);
851 /* Ensure that interval is not affected. */
852 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, beacon_confirm_all_subnets,
853 NULL));
854 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, beacon_confirm_all_subnets,
855 NULL));
856
857 PASS();
858 }
859
860 /* Test beacons rejection with multiple Net Keys. */
test_rx_multiple_netkeys(void)861 static void test_rx_multiple_netkeys(void)
862 {
863 uint8_t phase;
864 uint8_t status;
865 int err;
866
867 bt_mesh_test_cfg_set(&rx_cfg, MULT_NETKEYS_WAIT_TIME);
868 bt_mesh_test_setup();
869 bt_mesh_iv_update_test(true);
870
871 /* Add new Net Keys and switch Key Refresh phase to 1 so that beacons can trigger Key
872 * Refresh procedure.
873 */
874 for (size_t i = 0; i < ARRAY_SIZE(net_key_pairs); i++) {
875 err = bt_mesh_cfg_cli_net_key_add(0, cfg->addr, i + 1, net_key_pairs[i].primary,
876 &status);
877 if (err || status) {
878 FAIL("Net Key add failed (err %d, status %u)", err, status);
879 }
880
881 err = bt_mesh_cfg_cli_net_key_update(0, cfg->addr, i + 1,
882 net_key_pairs[i].secondary, &status);
883 if (err || status) {
884 FAIL("Net Key update failed (err %d, status %u)", err, status);
885 }
886 }
887
888 for (size_t i = 0; i < ARRAY_SIZE(net_key_pairs); i++) {
889 /* Tx device shall change Key Refresh phase to 2. */
890 k_sleep(K_SECONDS(40));
891
892 status = bt_mesh_subnet_kr_phase_get(i + 1, &phase);
893 if (status != STATUS_SUCCESS) {
894 FAIL("Unable to populate Key Refresh phase (status: %d)", status);
895 }
896
897 ASSERT_EQUAL(BT_MESH_KR_PHASE_2, phase);
898
899 /* Tx device shall change Key Refresh phase to 0. */
900 k_sleep(K_SECONDS(40));
901
902 status = bt_mesh_subnet_kr_phase_get(i + 1, &phase);
903 if (status != STATUS_SUCCESS) {
904 FAIL("Unable to populate Key Refresh phase (status: %d)", status);
905 }
906
907 ASSERT_EQUAL(BT_MESH_KR_NORMAL, phase);
908 }
909
910 PASS();
911
912 }
913
914 static struct k_work_delayable beacon_timer;
915
secure_beacon_send(struct k_work * work)916 static void secure_beacon_send(struct k_work *work)
917 {
918 NET_BUF_SIMPLE_DEFINE(buf, 22);
919 beacon_create(&buf, test_net_key, 0, 0);
920 send_beacon(&buf);
921 /**
922 * Sending SNB(secure network beacon) faster to guarantee
923 * at least one beacon is received by tx node in 10s period.
924 */
925 k_work_schedule(&beacon_timer, K_SECONDS(2));
926 }
927
test_tx_secure_beacon_interval(void)928 static void test_tx_secure_beacon_interval(void)
929 {
930 bt_mesh_test_cfg_set(&tx_cfg, BEACON_INTERVAL_WAIT_TIME);
931 k_sleep(K_SECONDS(2));
932 bt_mesh_test_setup();
933 PASS();
934 }
935
test_rx_secure_beacon_interval(void)936 static void test_rx_secure_beacon_interval(void)
937 {
938 NET_BUF_SIMPLE_DEFINE(buf, 22);
939 int err;
940 int64_t beacon_recv_time;
941 int64_t delta;
942
943 bt_mesh_test_cfg_set(&rx_cfg, BEACON_INTERVAL_WAIT_TIME);
944 bt_mesh_crypto_init();
945 k_sem_init(&observer_sem, 0, 1);
946 k_work_init_delayable(&beacon_timer, secure_beacon_send);
947
948 err = bt_enable(NULL);
949 if (err) {
950 FAIL("Bluetooth init failed (err %d)", err);
951 }
952
953 beacon_create(&buf, test_net_key, 0, 0);
954 k_sleep(K_SECONDS(5));
955 /*wait provisioned tx node to send the first beacon*/
956 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
957 k_sleep(K_SECONDS(2));
958
959 /**
960 * Sending 2 SNB 20ms apart by only sending for even values of loop variable.
961 * And verify that tx node adapts to 20s SNB interval after sending enough
962 * beacons in for loop.
963 */
964 for (size_t i = 1; i < 5; i++) {
965 if (i % 2) {
966 send_beacon(&buf);
967 ASSERT_FALSE(
968 wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
969 } else {
970 ASSERT_TRUE(
971 wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
972 }
973 }
974
975 /**
976 * Verify that tx node keeps the 20s SNB interval until adapts itself and
977 * sends SNB in 10s again.
978 */
979 ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
980 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
981 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
982 beacon_recv_time = k_uptime_get();
983 /* Start sending SNB */
984 k_work_schedule(&beacon_timer, K_NO_WAIT);
985
986 /**
987 * Send SNB so that the tx node stays silent and eventually sends
988 * an SNB after 600s, which is the maximum time for SNB interval.
989 * Sending beacon with 2sec interval.
990 */
991 delta = 0;
992 for (size_t i = 0; i < 60; i++) {
993 if (wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL)) {
994 delta = k_uptime_delta(&beacon_recv_time);
995 break;
996 }
997 }
998
999 ASSERT_TRUE(delta >= (600 * MSEC_PER_SEC));
1000 PASS();
1001 }
1002
1003 static uint8_t snb_cnt;
1004
snb_recv(const struct bt_mesh_snb * snb)1005 static void snb_recv(const struct bt_mesh_snb *snb)
1006 {
1007 /* IV idx of 2 marks end of test */
1008 if (snb->iv_idx == 2) {
1009 k_sem_give(&beacon_sem);
1010 return;
1011 }
1012
1013 ASSERT_EQUAL(snb->flags, 0x02);
1014 ASSERT_EQUAL(snb->iv_idx, 1);
1015 snb_cnt++;
1016 }
1017
test_rx_beacon_cache(void)1018 static void test_rx_beacon_cache(void)
1019 {
1020 k_sem_init(&beacon_sem, 0, 1);
1021 snb_cb_ptr = snb_recv;
1022
1023 bt_mesh_test_cfg_set(&rx_cfg, WAIT_TIME);
1024 bt_mesh_test_setup();
1025
1026 /* Wait for secondary SNB to end test. */
1027 ASSERT_OK_MSG(k_sem_take(&beacon_sem, K_SECONDS(40)),
1028 "Didn't receive SNB in time");
1029
1030 /* Verify that only one SNB for IV_idx=1 was handled. */
1031 ASSERT_EQUAL(snb_cnt, 1);
1032 PASS();
1033 }
1034
test_tx_beacon_cache(void)1035 static void test_tx_beacon_cache(void)
1036 {
1037 bt_mesh_test_cfg_set(NULL, WAIT_TIME);
1038 bt_mesh_crypto_init();
1039 ASSERT_OK_MSG(bt_enable(NULL), "Bluetooth init failed");
1040
1041 NET_BUF_SIMPLE_DEFINE(iv1, 22);
1042 NET_BUF_SIMPLE_DEFINE(iv2, 22);
1043 beacon_create(&iv1, test_net_key, 0x02, 0x0001);
1044 beacon_create(&iv2, test_net_key, 0x02, 0x0002);
1045
1046 /* Send two copies of the same SNB. */
1047 for (size_t i = 0; i < 2; i++) {
1048 k_sleep(K_SECONDS(5));
1049 send_beacon(&iv1);
1050 }
1051
1052 /* Send secondary SNB to mark end of test. */
1053 k_sleep(K_SECONDS(5));
1054 send_beacon(&iv2);
1055
1056 PASS();
1057 }
1058
1059 typedef void (*priv_beacon_cb)(const struct bt_mesh_prb *prb);
1060
1061 static priv_beacon_cb priv_beacon_cb_ptr;
1062
priv_received(const struct bt_mesh_prb * prb)1063 static void priv_received(const struct bt_mesh_prb *prb)
1064 {
1065 if (priv_beacon_cb_ptr) {
1066 priv_beacon_cb_ptr(prb);
1067 }
1068 }
1069
1070 BT_MESH_BEACON_CB_DEFINE(priv_beacon) = {
1071 .priv_received = priv_received,
1072 };
1073
private_beacon_check(const uint8_t * net_id,void * ctx)1074 static bool private_beacon_check(const uint8_t *net_id, void *ctx)
1075 {
1076 bool ret;
1077 bool same_random = *(bool *)ctx;
1078
1079 if (memcmp(beacon.adv_addr.a.val, last_beacon_adv_addr.a.val, BT_ADDR_SIZE) == 0) {
1080 return false;
1081 }
1082
1083 memcpy(&last_beacon_adv_addr.a.val, beacon.adv_addr.a.val, BT_ADDR_SIZE);
1084
1085 if (same_random) {
1086 ret = memcmp(beacon.random, last_random, 13) == 0;
1087 } else {
1088 ret = memcmp(beacon.random, last_random, 13) != 0;
1089 }
1090
1091 memcpy(&last_random, beacon.random, 13);
1092
1093 return ret;
1094 }
1095
provision(const struct bt_mesh_test_cfg * dev_cfg)1096 static void provision(const struct bt_mesh_test_cfg *dev_cfg)
1097 {
1098 int err;
1099
1100 err = bt_mesh_provision(net_key, 0, 0, 0, dev_cfg->addr, dev_cfg->dev_key);
1101 if (err) {
1102 FAIL("Provisioning failed (err %d)", err);
1103 }
1104 }
1105
tx_priv_setup(void)1106 static void tx_priv_setup(void)
1107 {
1108 uint8_t status;
1109 struct bt_mesh_priv_beacon val;
1110 int err;
1111
1112 bt_mesh_test_cfg_set(NULL, WAIT_TIME);
1113 bt_mesh_device_setup(&prov, &prb_comp);
1114 provision(&tx_cfg);
1115
1116 val.enabled = 1;
1117 val.rand_interval = random_interval;
1118
1119 err = bt_mesh_cfg_cli_beacon_set(0, tx_cfg.addr, 0, &status);
1120 if (err || status != 0) {
1121 FAIL("Beacon set failed (err %d, status %u)", err, status);
1122 }
1123
1124 err = bt_mesh_priv_beacon_cli_set(0, tx_cfg.addr, &val, &val);
1125 if (err) {
1126 FAIL("Failed to enable Private Beacon (err=%d)", err);
1127 }
1128 }
1129
test_tx_priv_on_iv_update(void)1130 static void test_tx_priv_on_iv_update(void)
1131 {
1132 tx_priv_setup();
1133
1134 tx_on_iv_update_test();
1135 }
1136
test_tx_priv_on_key_refresh(void)1137 static void test_tx_priv_on_key_refresh(void)
1138 {
1139 tx_priv_setup();
1140
1141 tx_on_key_refresh_test();
1142 }
1143
test_tx_priv_adv(void)1144 static void test_tx_priv_adv(void)
1145 {
1146 uint8_t status;
1147 struct bt_mesh_priv_beacon val;
1148 int err;
1149
1150 bt_mesh_test_cfg_set(NULL, BEACON_INTERVAL_WAIT_TIME);
1151 bt_mesh_device_setup(&prov, &prb_comp);
1152 provision(&tx_cfg);
1153
1154 err = bt_mesh_cfg_cli_beacon_set(0, tx_cfg.addr, 0, &status);
1155 if (err || status != 0) {
1156 FAIL("Beacon set failed (err %d, status %u)", err, status);
1157 }
1158
1159 val.enabled = 1;
1160 val.rand_interval = 1;
1161
1162 err = bt_mesh_priv_beacon_cli_set(0, tx_cfg.addr, &val, &val);
1163 if (err) {
1164 FAIL("Failed to enable Private Beacon (err=%d)", err);
1165 }
1166
1167 k_sleep(K_SECONDS(6 * BEACON_INTERVAL));
1168
1169 val.rand_interval = 0;
1170
1171 err = bt_mesh_priv_beacon_cli_set(0, tx_cfg.addr, &val, &val);
1172 if (err) {
1173 FAIL("Failed to enable Private Beacon (err=%d)", err);
1174 }
1175
1176 k_sleep(K_SECONDS(6 * BEACON_INTERVAL));
1177
1178 val.rand_interval = 0;
1179
1180 err = bt_mesh_priv_beacon_cli_set(0, tx_cfg.addr, &val, &val);
1181 if (err) {
1182 FAIL("Failed to enable Private Beacon (err=%d)", err);
1183 }
1184
1185 k_sleep(K_SECONDS(6 * BEACON_INTERVAL));
1186
1187 val.rand_interval = 3;
1188
1189 err = bt_mesh_priv_beacon_cli_set(0, tx_cfg.addr, &val, &val);
1190 if (err) {
1191 FAIL("Failed to enable Private Beacon (err=%d)", err);
1192 }
1193
1194 PASS();
1195 }
1196
test_rx_priv_adv(void)1197 static void test_rx_priv_adv(void)
1198 {
1199 bool same_random;
1200 int err, i;
1201
1202 bt_mesh_test_cfg_set(&rx_cfg, BEACON_INTERVAL_WAIT_TIME);
1203 k_sem_init(&observer_sem, 0, 1);
1204
1205 err = bt_enable(NULL);
1206 if (err) {
1207 FAIL("Bluetooth init failed (err %d)", err);
1208 }
1209
1210 expected_beacon = BEACON_TYPE_PRIVATE;
1211
1212 same_random = false;
1213 /* TX device is advertising with Random Interval = 1 for 6 intervals
1214 * and with Random Interval = 0 for another 6
1215 */
1216 for (i = 0; i < 12; i++) {
1217 wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, private_beacon_check,
1218 &same_random);
1219 }
1220
1221 /* TX device is advertising with Random Interval = 3 */
1222 for (i = 0; i < 2; i++) {
1223 same_random = true;
1224
1225 for (int j = 0; j < 2; j++) {
1226 wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, private_beacon_check,
1227 &same_random);
1228 }
1229
1230 k_sleep(K_SECONDS(BEACON_INTERVAL));
1231
1232 /* Beacon random should change here */
1233 same_random = true;
1234 wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, private_beacon_check,
1235 &same_random);
1236 }
1237
1238 PASS();
1239 }
1240
private_beacon_create(struct net_buf_simple * buf,const uint8_t * net_key,uint8_t flags,uint32_t iv_index)1241 static void private_beacon_create(struct net_buf_simple *buf, const uint8_t *net_key, uint8_t flags,
1242 uint32_t iv_index)
1243 {
1244 uint8_t net_id[8];
1245 uint8_t auth[8];
1246 uint8_t data[5];
1247 uint8_t random_val[13];
1248 int err;
1249
1250 err = bt_mesh_k3(net_key, net_id);
1251 if (err) {
1252 FAIL("Unable to generate Net ID");
1253 }
1254
1255 err = bt_mesh_private_beacon_key(net_key, &priv_beacon_key);
1256 if (err) {
1257 FAIL("Unable to generate beacon key");
1258 }
1259
1260 bt_rand(random_val, sizeof(random_val));
1261 bt_mesh_beacon_encrypt(&priv_beacon_key, flags, iv_index,
1262 random_val, data, auth);
1263
1264 net_buf_simple_reset(buf);
1265 net_buf_simple_add_u8(buf, BEACON_TYPE_PRIVATE);
1266 net_buf_simple_add_mem(buf, random_val, 13);
1267 net_buf_simple_add_mem(buf, data, 5);
1268 net_buf_simple_add_mem(buf, auth, 8);
1269 }
1270
test_tx_priv_invalid(void)1271 static void test_tx_priv_invalid(void)
1272 {
1273 uint8_t fields_offsets[4] = {1, 14, 15, 19};
1274
1275 NET_BUF_SIMPLE_DEFINE(buf, 27);
1276 int err;
1277
1278 bt_mesh_test_cfg_set(&tx_cfg, 130);
1279 bt_mesh_crypto_init();
1280 k_sem_init(&observer_sem, 0, 1);
1281
1282 err = bt_enable(NULL);
1283 if (err) {
1284 FAIL("Bluetooth init failed (err %d)", err);
1285 }
1286
1287 LOG_INF("Bluetooth initialized");
1288
1289 /* Let the rx node send the first beacon. */
1290 k_sleep(K_SECONDS(5));
1291
1292 /* Create a valid beacon with IV Update Flag set to 1 and new IV Index. */
1293 private_beacon_create(&buf, net_key, 0x02, 0x0001);
1294
1295 expected_beacon = BEACON_TYPE_PRIVATE;
1296
1297 corrupted_beacon_test(fields_offsets, ARRAY_SIZE(fields_offsets), &buf);
1298
1299 PASS();
1300 }
1301
test_rx_priv_invalid(void)1302 static void test_rx_priv_invalid(void)
1303 {
1304 uint8_t status;
1305 struct bt_mesh_priv_beacon val;
1306 int err;
1307
1308 bt_mesh_test_cfg_set(NULL, 130);
1309 bt_mesh_device_setup(&prov, &prb_comp);
1310 provision(&rx_cfg);
1311 bt_mesh_iv_update_test(true);
1312
1313 val.enabled = 1;
1314 val.rand_interval = random_interval;
1315
1316 err = bt_mesh_cfg_cli_beacon_set(0, rx_cfg.addr, 0, &status);
1317 if (err || status != 0) {
1318 FAIL("Beacon set failed (err %d, status %u)", err, status);
1319 }
1320
1321 err = bt_mesh_priv_beacon_cli_set(0, rx_cfg.addr, &val, &val);
1322 if (err) {
1323 FAIL("Failed to enable Private Beacon (err=%d)", err);
1324 }
1325
1326 for (size_t i = 0; i < 4; i++) {
1327 ASSERT_FALSE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
1328 ASSERT_EQUAL(0, bt_mesh.iv_index);
1329
1330 k_sleep(K_SECONDS((BEACON_INTERVAL + 1) * 2));
1331 }
1332
1333 /* Only the last beacon shall change IV Update state. */
1334 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
1335 ASSERT_EQUAL(1, bt_mesh.iv_index);
1336
1337 PASS();
1338 }
1339
toggle_priv_beacon(uint16_t addr,uint8_t enabled)1340 static void toggle_priv_beacon(uint16_t addr, uint8_t enabled)
1341 {
1342 int err;
1343 uint8_t status;
1344 struct bt_mesh_priv_beacon val;
1345
1346 err = bt_mesh_cfg_cli_beacon_set(0, addr, !enabled, &status);
1347 if (err || status != !enabled) {
1348 FAIL("Beacon set failed (err %d, status %u)", err, status);
1349 }
1350
1351 val.enabled = enabled;
1352 val.rand_interval = 1;
1353
1354 err = bt_mesh_priv_beacon_cli_set(0, addr, &val, &val);
1355 if (err) {
1356 FAIL("Failed to enable Private Beacon (err=%d)", err);
1357 }
1358 }
1359
test_tx_priv_interleave(void)1360 static void test_tx_priv_interleave(void)
1361 {
1362 uint8_t phase;
1363 uint8_t status;
1364 struct bt_mesh_subnet *sub;
1365
1366
1367 bt_mesh_test_cfg_set(NULL, WAIT_TIME);
1368 bt_mesh_device_setup(&prov, &prb_comp);
1369 provision(&tx_cfg);
1370
1371 sub = bt_mesh_subnet_get(0);
1372 ASSERT_TRUE(sub != NULL);
1373
1374 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR));
1375 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
1376 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_PENDING));
1377 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST));
1378 ASSERT_TRUE(bt_mesh.iv_index == 0);
1379
1380 status = bt_mesh_subnet_kr_phase_get(0, &phase);
1381 ASSERT_TRUE(status == STATUS_SUCCESS);
1382 ASSERT_TRUE(phase == BT_MESH_KR_NORMAL);
1383
1384 /* Wait for SNB being advertised and switch beacon type between Beacon Intervals */
1385 k_sleep(K_SECONDS(BEACON_INTERVAL + 5));
1386
1387 toggle_priv_beacon(tx_cfg.addr, 1);
1388
1389 bt_mesh_iv_update_test(true);
1390 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST));
1391
1392 ASSERT_TRUE(bt_mesh_iv_update());
1393 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
1394 ASSERT_TRUE(bt_mesh.iv_index == 1);
1395
1396 /* Switch beacon type between Beacon Intervals */
1397 k_sleep(K_SECONDS(BEACON_INTERVAL + 5));
1398
1399 toggle_priv_beacon(tx_cfg.addr, 0);
1400 /* Small delay to let beacons complete before subnet update is applied */
1401 k_sleep(K_MSEC(20));
1402
1403 status = bt_mesh_subnet_update(BT_MESH_KEY_PRIMARY, net_key_new);
1404 ASSERT_TRUE(status == STATUS_SUCCESS);
1405 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
1406 ASSERT_TRUE(status == STATUS_SUCCESS);
1407 ASSERT_TRUE(phase == BT_MESH_KR_PHASE_1);
1408
1409 phase = BT_MESH_KR_PHASE_2;
1410 status = bt_mesh_subnet_kr_phase_set(BT_MESH_KEY_PRIMARY, &phase);
1411 ASSERT_TRUE(status == STATUS_SUCCESS);
1412 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
1413 ASSERT_TRUE(status == STATUS_SUCCESS);
1414 ASSERT_TRUE(phase == BT_MESH_KR_PHASE_2);
1415
1416 k_sleep(K_SECONDS(BEACON_INTERVAL + 7));
1417 toggle_priv_beacon(tx_cfg.addr, 1);
1418
1419 PASS();
1420 }
1421
test_rx_priv_interleave(void)1422 static void test_rx_priv_interleave(void)
1423 {
1424 int err;
1425 bool same_random = false;
1426
1427 bt_mesh_test_cfg_set(&rx_cfg, WAIT_TIME);
1428 bt_mesh_crypto_init();
1429 k_sem_init(&observer_sem, 0, 1);
1430
1431 err = bt_mesh_private_beacon_key(net_key, &priv_beacon_key);
1432 if (err) {
1433 FAIL("Unable to generate beacon key");
1434 }
1435
1436 err = bt_enable(NULL);
1437 if (err) {
1438 FAIL("Bluetooth init failed (err %d)", err);
1439 }
1440
1441 expected_beacon = BEACON_TYPE_SECURE;
1442 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
1443
1444 expected_beacon = BEACON_TYPE_PRIVATE;
1445 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, private_beacon_check,
1446 &same_random));
1447
1448 /* IVU was started here */
1449 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, private_beacon_check,
1450 &same_random));
1451 ASSERT_EQUAL(0x02, beacon.flags);
1452 ASSERT_EQUAL(0x0001, beacon.iv_index);
1453
1454 memset(&beacon, 0, sizeof(beacon));
1455 expected_beacon = BEACON_TYPE_SECURE;
1456
1457 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
1458 ASSERT_EQUAL(0x02, beacon.flags);
1459 ASSERT_EQUAL(0x0001, beacon.iv_index);
1460
1461 /* KR was started here */
1462 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
1463 ASSERT_EQUAL(0x03, beacon.flags);
1464 ASSERT_EQUAL(0x0001, beacon.iv_index);
1465
1466 expected_beacon = BEACON_TYPE_PRIVATE;
1467
1468 err = bt_mesh_private_beacon_key(net_key_new, &priv_beacon_key);
1469
1470 ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, private_beacon_check,
1471 &same_random));
1472 ASSERT_EQUAL(0x03, beacon.flags);
1473 ASSERT_EQUAL(0x0001, beacon.iv_index);
1474
1475 PASS();
1476 }
1477
1478 static uint8_t prb_cnt;
1479
priv_beacon_recv(const struct bt_mesh_prb * prb)1480 static void priv_beacon_recv(const struct bt_mesh_prb *prb)
1481 {
1482 /* IV idx of 2 marks end of test */
1483 if (prb->iv_idx == 2) {
1484 k_sem_give(&beacon_sem);
1485 return;
1486 }
1487
1488 ASSERT_EQUAL(prb->flags, 0x02);
1489 ASSERT_EQUAL(prb->iv_idx, 1);
1490 prb_cnt++;
1491 }
1492
test_rx_priv_beacon_cache(void)1493 static void test_rx_priv_beacon_cache(void)
1494 {
1495 k_sem_init(&beacon_sem, 0, 1);
1496 priv_beacon_cb_ptr = priv_beacon_recv;
1497
1498 bt_mesh_test_cfg_set(&rx_cfg, WAIT_TIME);
1499 bt_mesh_device_setup(&prov, &prb_comp);
1500 provision(&rx_cfg);
1501
1502 /* Wait for secondary private beacon to end test. */
1503 ASSERT_OK_MSG(k_sem_take(&beacon_sem, K_SECONDS(40)),
1504 "Didn't receive private beacon in time");
1505
1506 /* Verify that only one private beacon for IV_idx=1 was handled. */
1507 ASSERT_EQUAL(prb_cnt, 1);
1508 PASS();
1509 }
1510
test_tx_priv_beacon_cache(void)1511 static void test_tx_priv_beacon_cache(void)
1512 {
1513 bt_mesh_test_cfg_set(NULL, WAIT_TIME);
1514 bt_mesh_crypto_init();
1515 ASSERT_OK_MSG(bt_enable(NULL), "Bluetooth init failed");
1516
1517 NET_BUF_SIMPLE_DEFINE(iv1, 27);
1518 NET_BUF_SIMPLE_DEFINE(iv2, 27);
1519 private_beacon_create(&iv1, test_net_key, 0x02, 0x0001);
1520 private_beacon_create(&iv2, test_net_key, 0x02, 0x0002);
1521
1522 /* Send two copies of the same private beacon. */
1523 for (size_t i = 0; i < 2; i++) {
1524 k_sleep(K_SECONDS(5));
1525 send_beacon(&iv1);
1526 }
1527
1528 /* Send secondary private beacon to mark end of test. */
1529 k_sleep(K_SECONDS(5));
1530 send_beacon(&iv2);
1531
1532 PASS();
1533 }
1534
1535 #if defined(CONFIG_BT_MESH_GATT_PROXY)
1536
1537 static uint8_t test_net_key_3[16] = {0x12, 0x54, 0xab, 0x1e};
1538
1539 #define UNTIL_UPTIME(time) (k_uptime_get() > (time) ? K_NO_WAIT : K_MSEC((time) - k_uptime_get()))
1540 #define BEACON_TYPE_NET_ID 0
1541 #define BEACON_TYPE_NODE_ID 1
1542 #define BEACON_TYPE_PRIVATE_NET_ID 2
1543 #define BEACON_TYPE_PRIVATE_NODE_ID 3
1544 #define BEACON_TYPE_PRIVATE_LEN 28
1545 #define TEST_NET_IDX1 0
1546 #define TEST_NET_IDX2 1
1547 #define TEST_NET_IDX3 2
1548 #define MAX_TIMEOUT ((CONFIG_BT_MESH_NODE_ID_TIMEOUT * 1000) / 6)
1549
1550 #define PP_NET_ID_WAIT_TIME 610 /*seconds*/
1551 #define PP_NODE_ID_WAIT_TIME 80 /*seconds*/
1552 #define PP_MULT_NET_ID_WAIT_TIME 50 /*seconds*/
1553 #define PROXY_ADV_MULTI_SUBNET_COEX_WAIT_TIME 151 /*seconds*/
1554
1555 struct netkey_ctx {
1556 uint8_t *net_key;
1557 uint8_t net_id[8];
1558 uint8_t net_idx;
1559 struct bt_mesh_key id_key;
1560 };
1561
1562 static struct netkey_ctx pp_net0 = {.net_key = (uint8_t *)test_net_key, .net_idx = 0};
1563 static struct netkey_ctx pp_net1 = {.net_key = (uint8_t *)test_net_key_2, .net_idx = 1};
1564 static struct netkey_ctx pp_net2 = {.net_key = (uint8_t *)test_net_key_3, .net_idx = 2};
1565
1566 struct priv_test_ctx {
1567 uint8_t beacon_type;
1568 uint16_t *node_id_addr;
1569 };
1570
pp_netkey_ctx_init(struct netkey_ctx * net)1571 static void pp_netkey_ctx_init(struct netkey_ctx *net)
1572 {
1573 ASSERT_OK_MSG(bt_mesh_identity_key(net->net_key, &net->id_key),
1574 "Failed to generate ID key");
1575 ASSERT_OK_MSG(bt_mesh_k3(net->net_key, net->net_id), "Failed to generate Net ID");
1576 }
1577
proxy_adv_type_get(uint8_t adv_type,struct net_buf_simple * buf)1578 static uint8_t proxy_adv_type_get(uint8_t adv_type, struct net_buf_simple *buf)
1579 {
1580 uint8_t type;
1581 uint8_t len = buf->len;
1582
1583 if (adv_type != BT_GAP_ADV_TYPE_ADV_IND || len < 12) {
1584 return 0xFF;
1585 }
1586
1587 (void)net_buf_simple_pull_mem(buf, 11);
1588 type = net_buf_simple_pull_u8(buf);
1589 /* BEACON_TYPE_NET_ID is 20 bytes long, while the three other accepted types are 28 bytes*/
1590 if (len != ((type == BEACON_TYPE_NET_ID) ? 20 : 28)) {
1591 return 0xFF;
1592 }
1593
1594 return type;
1595 }
1596
proxy_adv_hash_calc(struct netkey_ctx * net,uint64_t random,uint16_t * addr,bool is_priv)1597 static uint64_t proxy_adv_hash_calc(struct netkey_ctx *net, uint64_t random, uint16_t *addr,
1598 bool is_priv)
1599 {
1600 uint64_t hash;
1601 uint8_t tmp[16] = {0};
1602
1603 tmp[5] = is_priv ? 3 : 0;
1604
1605 if (addr) {
1606 memcpy(&tmp[6], &random, 8);
1607 sys_put_be16(*addr, &tmp[14]);
1608
1609 } else {
1610 memcpy(&tmp[0], net->net_id, 8);
1611 memcpy(&tmp[8], &random, 8);
1612 }
1613
1614 bt_mesh_encrypt(&net->id_key, tmp, tmp);
1615 memcpy(&hash, &tmp[8], 8);
1616
1617 return hash;
1618 }
1619
pp_beacon_check(const uint8_t * net_id,void * ctx)1620 static bool pp_beacon_check(const uint8_t *net_id, void *ctx)
1621 {
1622 struct priv_test_ctx *test_ctx = (struct priv_test_ctx *)ctx;
1623
1624 ASSERT_EQUAL(beacon.pp_hash,
1625 proxy_adv_hash_calc(&pp_net0, beacon.pp_random, test_ctx->node_id_addr, true));
1626
1627 if (memcmp(beacon.adv_addr.a.val, last_beacon_adv_addr.a.val, BT_ADDR_SIZE) == 0) {
1628 return false;
1629 }
1630
1631 memcpy(&last_beacon_adv_addr.a.val, beacon.adv_addr.a.val, BT_ADDR_SIZE);
1632
1633 return true;
1634 }
1635
priv_scan_cb(const bt_addr_le_t * addr,int8_t rssi,uint8_t adv_type,struct net_buf_simple * buf)1636 static void priv_scan_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type,
1637 struct net_buf_simple *buf)
1638 {
1639 struct priv_test_ctx *ctx = (struct priv_test_ctx *)beacon.user_ctx;
1640
1641 if (proxy_adv_type_get(adv_type, buf) != ctx->beacon_type) {
1642 /* Wrong message type */
1643 return;
1644 }
1645
1646 bt_addr_le_copy(&beacon.adv_addr, addr);
1647
1648 if (ctx->beacon_type == BEACON_TYPE_NET_ID) {
1649 beacon.net_id = net_buf_simple_pull_le64(buf);
1650 } else {
1651 beacon.pp_hash = net_buf_simple_pull_le64(buf);
1652 beacon.pp_random = net_buf_simple_pull_le64(buf);
1653 }
1654
1655 if (!beacon.process_cb || beacon.process_cb(NULL, beacon.user_ctx)) {
1656 k_sem_give(&observer_sem);
1657 }
1658 }
1659
1660 struct proxy_adv_beacon {
1661 uint8_t evt_type;
1662 uint8_t net_idx;
1663 int64_t rx_timestamp;
1664 union {
1665 uint64_t net_id;
1666 struct {
1667 uint64_t hash;
1668 uint64_t random;
1669 } enc;
1670 } ctx;
1671 };
1672
proxy_adv_scan_all_cb(const bt_addr_le_t * addr,int8_t rssi,uint8_t adv_type,struct net_buf_simple * buf)1673 static void proxy_adv_scan_all_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type,
1674 struct net_buf_simple *buf)
1675 {
1676 struct proxy_adv_beacon *beac = (struct proxy_adv_beacon *)beacon.user_ctx;
1677
1678 beac->evt_type = proxy_adv_type_get(adv_type, buf);
1679 if (beac->evt_type == 0xFF) {
1680 /* Not a related beacon type */
1681 return;
1682 }
1683
1684 bt_addr_le_copy(&beacon.adv_addr, addr);
1685 beac->rx_timestamp = k_uptime_get();
1686
1687 if (beac->evt_type == BEACON_TYPE_NET_ID) {
1688 beac->ctx.net_id = net_buf_simple_pull_le64(buf);
1689 } else {
1690 beac->ctx.enc.hash = net_buf_simple_pull_le64(buf);
1691 beac->ctx.enc.random = net_buf_simple_pull_le64(buf);
1692 }
1693
1694 if (!beacon.process_cb || beacon.process_cb(NULL, beacon.user_ctx)) {
1695 k_sem_give(&observer_sem);
1696 }
1697 }
1698
rx_priv_common_init(uint16_t wait)1699 static void rx_priv_common_init(uint16_t wait)
1700 {
1701 bt_mesh_test_cfg_set(&rx_cfg, wait);
1702 bt_mesh_crypto_init();
1703 pp_netkey_ctx_init(&pp_net0);
1704 k_sem_init(&observer_sem, 0, 1);
1705 ASSERT_OK_MSG(bt_enable(NULL), "Bluetooth init failed");
1706 }
1707
tx_proxy_adv_common_init(uint16_t wait,const struct bt_mesh_test_cfg * cfg)1708 static void tx_proxy_adv_common_init(uint16_t wait, const struct bt_mesh_test_cfg *cfg)
1709 {
1710 bt_mesh_test_cfg_set(NULL, wait);
1711 bt_mesh_device_setup(&prov, &prb_comp);
1712 provision(cfg);
1713
1714 /* Disable GATT proxy */
1715 ASSERT_OK_MSG(bt_mesh_gatt_proxy_set(BT_MESH_GATT_PROXY_DISABLED),
1716 "Failed to disable gatt proxy");
1717 }
1718
test_tx_priv_net_id(void)1719 static void test_tx_priv_net_id(void)
1720 {
1721 tx_proxy_adv_common_init(PP_NET_ID_WAIT_TIME, &tx_cfg);
1722
1723 /* Enable private GATT proxy */
1724 ASSERT_OK_MSG(bt_mesh_priv_gatt_proxy_set(BT_MESH_GATT_PROXY_ENABLED),
1725 "Failed to set private gatt proxy");
1726
1727 PASS();
1728 }
1729
test_rx_priv_net_id(void)1730 static void test_rx_priv_net_id(void)
1731 {
1732 struct priv_test_ctx ctx = {
1733 .beacon_type = BEACON_TYPE_PRIVATE_NET_ID,
1734 .node_id_addr = NULL,
1735 };
1736
1737 rx_priv_common_init(PP_NET_ID_WAIT_TIME);
1738
1739 /* Scan for first net ID */
1740 ASSERT_TRUE(
1741 wait_for_beacon(priv_scan_cb, 5, pp_beacon_check, &ctx));
1742
1743 uint64_t last_pp_random = beacon.pp_random;
1744
1745 /* Wait for 10 minutes, then scan for another net
1746 * ID beacon and verify that random field has changed
1747 */
1748 k_sleep(K_SECONDS(600));
1749 ASSERT_TRUE(
1750 wait_for_beacon(priv_scan_cb, 5, pp_beacon_check, &ctx));
1751 ASSERT_FALSE(beacon.pp_random == last_pp_random);
1752
1753 PASS();
1754 }
1755
test_tx_priv_node_id(void)1756 static void test_tx_priv_node_id(void)
1757 {
1758 enum bt_mesh_feat_state state;
1759
1760 tx_proxy_adv_common_init(PP_NODE_ID_WAIT_TIME, &tx_cfg);
1761
1762 /* Start first node advertisement */
1763 ASSERT_OK_MSG(bt_mesh_subnet_priv_node_id_set(TEST_NET_IDX1, BT_MESH_NODE_IDENTITY_RUNNING),
1764 "Failed to set private node ID");
1765
1766 /* Wait for Node ID advertising to end */
1767 k_sleep(K_SECONDS(65));
1768
1769 /* Check that advertisment has stopped */
1770 ASSERT_OK_MSG(bt_mesh_subnet_priv_node_id_get(TEST_NET_IDX1, &state),
1771 "Failed to get private node ID");
1772 ASSERT_EQUAL(state, BT_MESH_NODE_IDENTITY_STOPPED);
1773
1774 /* Start second node advertisement */
1775 ASSERT_OK_MSG(bt_mesh_subnet_priv_node_id_set(TEST_NET_IDX1, BT_MESH_NODE_IDENTITY_RUNNING),
1776 "Failed to set private node ID");
1777
1778 /* Wait to let node ID advertise for a while */
1779 k_sleep(K_SECONDS(5));
1780
1781 PASS();
1782 }
1783
test_rx_priv_node_id(void)1784 static void test_rx_priv_node_id(void)
1785 {
1786 struct priv_test_ctx ctx = {
1787 .beacon_type = BEACON_TYPE_PRIVATE_NODE_ID,
1788 .node_id_addr = (uint16_t *)&tx_cfg.addr,
1789 };
1790
1791 rx_priv_common_init(PP_NODE_ID_WAIT_TIME);
1792
1793 /* Scan for first node ID */
1794 ASSERT_TRUE(
1795 wait_for_beacon(priv_scan_cb, 5, pp_beacon_check, &ctx));
1796
1797 uint64_t last_pp_random = beacon.pp_random;
1798
1799 /* Wait for first node ID advertisment to finish, then scan for
1800 * second node ID and verify that random field has changed
1801 */
1802
1803 k_sleep(K_SECONDS(65));
1804 ASSERT_TRUE(
1805 wait_for_beacon(priv_scan_cb, 5, pp_beacon_check, &ctx));
1806 ASSERT_FALSE(beacon.pp_random == last_pp_random);
1807
1808 PASS();
1809 }
1810
test_tx_priv_multi_net_id(void)1811 static void test_tx_priv_multi_net_id(void)
1812 {
1813 tx_proxy_adv_common_init(PP_MULT_NET_ID_WAIT_TIME, &tx_cfg);
1814
1815 /* Add second network */
1816 ASSERT_OK_MSG(bt_mesh_subnet_add(TEST_NET_IDX2, test_net_key_2),
1817 "Failed to add second subnet");
1818
1819 /* Enable private GATT proxy */
1820 ASSERT_OK_MSG(bt_mesh_priv_gatt_proxy_set(BT_MESH_GATT_PROXY_ENABLED),
1821 "Failed to set private gatt proxy");
1822
1823 PASS();
1824 }
1825
proxy_adv_subnet_find(struct proxy_adv_beacon * beac,struct netkey_ctx ** nets,uint8_t net_cnt)1826 static void proxy_adv_subnet_find(struct proxy_adv_beacon *beac, struct netkey_ctx **nets,
1827 uint8_t net_cnt)
1828 {
1829 for (size_t i = 0; i < net_cnt; i++) {
1830
1831 switch (beac->evt_type) {
1832 case BEACON_TYPE_NET_ID:
1833 if (!memcmp(nets[i]->net_id, &beac->ctx.net_id, 8)) {
1834 beac->net_idx = nets[i]->net_idx;
1835 return;
1836 }
1837 break;
1838 case BEACON_TYPE_NODE_ID:
1839 if (beac->ctx.enc.hash ==
1840 proxy_adv_hash_calc(nets[i], beac->ctx.enc.random,
1841 (uint16_t *)&tx_cfg.addr, false)) {
1842 beac->net_idx = nets[i]->net_idx;
1843 return;
1844 }
1845 break;
1846 case BEACON_TYPE_PRIVATE_NET_ID:
1847 if (beac->ctx.enc.hash ==
1848 proxy_adv_hash_calc(nets[i], beac->ctx.enc.random,
1849 NULL, true)) {
1850 beac->net_idx = nets[i]->net_idx;
1851 return;
1852 }
1853 break;
1854 case BEACON_TYPE_PRIVATE_NODE_ID:
1855 if (beac->ctx.enc.hash ==
1856 proxy_adv_hash_calc(nets[i], beac->ctx.enc.random,
1857 (uint16_t *)&tx_cfg.addr, true)) {
1858 beac->net_idx = nets[i]->net_idx;
1859 return;
1860 }
1861 break;
1862
1863 default:
1864 FAIL("Unexpected beacon type");
1865 break;
1866 }
1867 }
1868
1869 FAIL("Could not find matching subnet for incoming proxy adv beacon");
1870 }
1871
1872 static const char *const proxy_adv_str[] = {"Net_ID", "Node_ID", "Priv_Net_ID", "Priv_Node_ID"};
1873 struct expected_proxy_adv_evt {
1874 uint8_t evt_type;
1875 uint8_t net_idx;
1876 uint16_t evt_cnt;
1877 struct {
1878 int64_t after;
1879 int64_t before;
1880 } time;
1881 };
1882
proxy_adv_register_evt(struct proxy_adv_beacon * beac,struct expected_proxy_adv_evt * exp_evts,uint8_t cnt)1883 static void proxy_adv_register_evt(struct proxy_adv_beacon *beac,
1884 struct expected_proxy_adv_evt *exp_evts, uint8_t cnt)
1885 {
1886 for (int i = 0; i < cnt; i++) {
1887 if ((exp_evts[i].evt_cnt) && (beac->evt_type == exp_evts[i].evt_type) &&
1888 (beac->net_idx == exp_evts[i].net_idx) &&
1889 (beac->rx_timestamp >= exp_evts[i].time.after) &&
1890 (beac->rx_timestamp <= exp_evts[i].time.before)) {
1891 exp_evts[i].evt_cnt--;
1892 }
1893 }
1894 }
1895
proxy_adv_confirm_evt(struct expected_proxy_adv_evt * exp_evts,uint8_t cnt)1896 static void proxy_adv_confirm_evt(struct expected_proxy_adv_evt *exp_evts, uint8_t cnt)
1897 {
1898 bool missing_evts = false;
1899
1900 for (int i = 0; i < cnt; i++) {
1901 if (exp_evts[i].evt_cnt) {
1902 LOG_ERR("Missing %d expected %s idx %d events in period %llums-%llums",
1903 exp_evts[i].evt_cnt, proxy_adv_str[exp_evts[i].evt_type],
1904 exp_evts[i].net_idx, exp_evts[i].time.after,
1905 exp_evts[i].time.before);
1906 missing_evts = true;
1907 }
1908 }
1909
1910 if (missing_evts) {
1911 FAIL("Test failed due to missing events");
1912 }
1913 }
1914
proxy_adv_scan_all(struct netkey_ctx ** nets,uint16_t net_cnt,struct expected_proxy_adv_evt * exp_evt,uint16_t exp_evt_cnt,int64_t timeout)1915 static void proxy_adv_scan_all(struct netkey_ctx **nets, uint16_t net_cnt,
1916 struct expected_proxy_adv_evt *exp_evt, uint16_t exp_evt_cnt,
1917 int64_t timeout)
1918 {
1919 struct proxy_adv_beacon beac;
1920
1921 while (k_uptime_get() < timeout) {
1922
1923 ASSERT_TRUE(wait_for_beacon(proxy_adv_scan_all_cb, 2, NULL, &beac));
1924 proxy_adv_subnet_find(&beac, nets, net_cnt);
1925 proxy_adv_register_evt(&beac, exp_evt, exp_evt_cnt);
1926
1927 /** We want to monitor an even distribution of adv events.
1928 * To ensure this, we wait a little less than the minimum
1929 * proxy adv period (1 second) before scanning for the next
1930 * evt.
1931 */
1932 k_sleep(K_MSEC(990));
1933 }
1934
1935 proxy_adv_confirm_evt(exp_evt, exp_evt_cnt);
1936 }
1937
1938 #define PROXY_ADV_MULTI_CHECKPOINT_1 20000
1939 #define PROXY_ADV_MULTI_CHECKPOINT_2 50000
1940 #define PROXY_ADV_MULTI_CHECKPOINT_3 110000
1941 #define PROXY_ADV_MULTI_CHECKPOINT_4 130000
1942 #define PROXY_ADV_MULTI_CHECKPOINT_END 150000
1943
test_tx_proxy_adv_multi_subnet_coex(void)1944 static void test_tx_proxy_adv_multi_subnet_coex(void)
1945 {
1946 tx_proxy_adv_common_init(PROXY_ADV_MULTI_SUBNET_COEX_WAIT_TIME, &tx_cfg);
1947
1948 /* Enable GATT proxy */
1949 ASSERT_OK_MSG(bt_mesh_gatt_proxy_set(BT_MESH_GATT_PROXY_ENABLED),
1950 "Failed to Enable gatt proxy");
1951
1952 k_sleep(UNTIL_UPTIME(PROXY_ADV_MULTI_CHECKPOINT_1));
1953 /* Add second and third network */
1954 ASSERT_OK_MSG(bt_mesh_subnet_add(TEST_NET_IDX2, test_net_key_2),
1955 "Failed to add second subnet");
1956 ASSERT_OK_MSG(bt_mesh_subnet_add(TEST_NET_IDX3, test_net_key_3),
1957 "Failed to add third subnet");
1958
1959 k_sleep(UNTIL_UPTIME(PROXY_ADV_MULTI_CHECKPOINT_2));
1960 /* Start Node Identity on second network */
1961 bt_mesh_proxy_identity_start(bt_mesh_subnet_get(TEST_NET_IDX2), false);
1962
1963 k_sleep(UNTIL_UPTIME(PROXY_ADV_MULTI_CHECKPOINT_3));
1964 /* Prepare for solicitation */
1965 ASSERT_OK_MSG(bt_mesh_gatt_proxy_set(BT_MESH_GATT_PROXY_DISABLED),
1966 "Failed to Enable gatt proxy");
1967 ASSERT_OK_MSG(bt_mesh_od_priv_proxy_set(20), "Failed to set OD priv proxy state");
1968
1969 k_sleep(UNTIL_UPTIME(PROXY_ADV_MULTI_CHECKPOINT_4));
1970 /* Re-enable GATT proxy and remove second and third network */
1971 ASSERT_OK_MSG(bt_mesh_gatt_proxy_set(BT_MESH_GATT_PROXY_ENABLED),
1972 "Failed to Enable gatt proxy");
1973 ASSERT_OK_MSG(bt_mesh_subnet_del(TEST_NET_IDX2), "Failed to delete subnet");
1974 ASSERT_OK_MSG(bt_mesh_subnet_del(TEST_NET_IDX3), "Failed to delete subnet");
1975
1976 PASS();
1977 }
1978
1979 static const struct bt_mesh_test_cfg solicit_trigger_cfg = {
1980 .addr = 0x0003,
1981 .dev_key = { 0x03 },
1982 };
1983
test_tx_proxy_adv_solicit_trigger(void)1984 static void test_tx_proxy_adv_solicit_trigger(void)
1985 {
1986 tx_proxy_adv_common_init(PROXY_ADV_MULTI_SUBNET_COEX_WAIT_TIME, &solicit_trigger_cfg);
1987 /* Disable SNB. */
1988 bt_mesh_beacon_set(false);
1989 ASSERT_OK_MSG(bt_mesh_subnet_add(TEST_NET_IDX2, test_net_key_2),
1990 "Failed to add second subnet");
1991
1992 k_sleep(UNTIL_UPTIME(PROXY_ADV_MULTI_CHECKPOINT_3));
1993
1994 /* Solicit first and second network */
1995 ASSERT_OK_MSG(bt_mesh_proxy_solicit(TEST_NET_IDX1),
1996 "Failed to start solicitation");
1997 ASSERT_OK_MSG(bt_mesh_proxy_solicit(TEST_NET_IDX2),
1998 "Failed to start solicitation");
1999
2000 PASS();
2001 }
2002
test_rx_proxy_adv_multi_subnet_coex(void)2003 static void test_rx_proxy_adv_multi_subnet_coex(void)
2004 {
2005 rx_priv_common_init(PROXY_ADV_MULTI_SUBNET_COEX_WAIT_TIME);
2006 /* Disable SNB. */
2007 bt_mesh_beacon_set(false);
2008 pp_netkey_ctx_init(&pp_net1);
2009 pp_netkey_ctx_init(&pp_net2);
2010
2011 struct netkey_ctx *nets[] = {&pp_net0, &pp_net1, &pp_net2};
2012 struct expected_proxy_adv_evt exp_evt[] = {
2013 /** A single subnet is active on the device with GATT Proxy
2014 * enabled. Verify that the single subnet has exclusive
2015 * access to the adv medium.
2016 */
2017 {.evt_type = BEACON_TYPE_NET_ID, .net_idx = 0, .evt_cnt = 19,
2018 .time = {.after = 0, .before = PROXY_ADV_MULTI_CHECKPOINT_1}},
2019
2020 /** Two additional subnets are added to the device.
2021 * Check that the subnets are sharing the adv medium,
2022 * advertising NET_ID beacons.
2023 */
2024 {.evt_type = BEACON_TYPE_NET_ID, .net_idx = 0, .evt_cnt = 8,
2025 .time = {.after = PROXY_ADV_MULTI_CHECKPOINT_1,
2026 .before = PROXY_ADV_MULTI_CHECKPOINT_2}},
2027 {.evt_type = BEACON_TYPE_NET_ID, .net_idx = 1, .evt_cnt = 8,
2028 .time = {.after = PROXY_ADV_MULTI_CHECKPOINT_1,
2029 .before = PROXY_ADV_MULTI_CHECKPOINT_2}},
2030 {.evt_type = BEACON_TYPE_NET_ID, .net_idx = 2, .evt_cnt = 8,
2031 .time = {.after = PROXY_ADV_MULTI_CHECKPOINT_1,
2032 .before = PROXY_ADV_MULTI_CHECKPOINT_2}},
2033
2034 /** The second subnet enables Node Identity. Check that NODE_ID
2035 * is advertised by this subnet, and that the two others
2036 * continues to advertise NET_ID.
2037 */
2038 {.evt_type = BEACON_TYPE_NET_ID, .net_idx = 0, .evt_cnt = 16,
2039 .time = {.after = PROXY_ADV_MULTI_CHECKPOINT_2,
2040 .before = PROXY_ADV_MULTI_CHECKPOINT_3}},
2041 {.evt_type = BEACON_TYPE_NODE_ID, .net_idx = 1, .evt_cnt = 16,
2042 .time = {.after = PROXY_ADV_MULTI_CHECKPOINT_2,
2043 .before = PROXY_ADV_MULTI_CHECKPOINT_3}},
2044 {.evt_type = BEACON_TYPE_NET_ID, .net_idx = 2, .evt_cnt = 16,
2045 .time = {.after = PROXY_ADV_MULTI_CHECKPOINT_2,
2046 .before = PROXY_ADV_MULTI_CHECKPOINT_3}},
2047
2048 /** The first and second subnet gets solicited. Check that
2049 * PRIVATE_NET_ID is advertised by these subnet,
2050 */
2051 {.evt_type = BEACON_TYPE_PRIVATE_NET_ID, .net_idx = 0, .evt_cnt = 8,
2052 .time = {.after = PROXY_ADV_MULTI_CHECKPOINT_3,
2053 .before = PROXY_ADV_MULTI_CHECKPOINT_4}},
2054 {.evt_type = BEACON_TYPE_PRIVATE_NET_ID, .net_idx = 1, .evt_cnt = 8,
2055 .time = {.after = PROXY_ADV_MULTI_CHECKPOINT_3,
2056 .before = PROXY_ADV_MULTI_CHECKPOINT_4}},
2057
2058 /** Second and third subnet are disabled. Verify that the single
2059 * subnet has exclusive access to the adv medium.
2060 */
2061 {.evt_type = BEACON_TYPE_NET_ID, .net_idx = 0, .evt_cnt = 18,
2062 .time = {.after = PROXY_ADV_MULTI_CHECKPOINT_4,
2063 .before = PROXY_ADV_MULTI_CHECKPOINT_END}},
2064 };
2065
2066 proxy_adv_scan_all(nets, ARRAY_SIZE(nets), exp_evt, ARRAY_SIZE(exp_evt),
2067 PROXY_ADV_MULTI_CHECKPOINT_END);
2068 PASS();
2069 }
2070
test_rx_priv_multi_net_id(void)2071 static void test_rx_priv_multi_net_id(void)
2072 {
2073 rx_priv_common_init(PP_MULT_NET_ID_WAIT_TIME);
2074 pp_netkey_ctx_init(&pp_net1);
2075
2076 struct priv_test_ctx ctx = {
2077 .beacon_type = BEACON_TYPE_PRIVATE_NET_ID,
2078 .node_id_addr = NULL,
2079 };
2080
2081 uint16_t itr = 4;
2082 static uint8_t old_idx = 0xff;
2083 static struct {
2084 struct netkey_ctx *net;
2085 uint16_t recv_cnt;
2086 int64_t start;
2087 } net_ctx[2] = {
2088 {.net = &pp_net0},
2089
2090 {.net = &pp_net1},
2091 };
2092
2093 while (itr) {
2094 /* Scan for net ID from both networks */
2095 ASSERT_TRUE(wait_for_beacon(priv_scan_cb, 5, NULL, &ctx));
2096
2097 for (size_t i = 0; i < ARRAY_SIZE(net_ctx); i++) {
2098 if (beacon.pp_hash ==
2099 proxy_adv_hash_calc(net_ctx[i].net, beacon.pp_random, NULL, true)) {
2100 if (old_idx == 0xff) {
2101 /* Received first Net ID advertisment */
2102 old_idx = i;
2103 net_ctx[i].start = k_uptime_get();
2104 net_ctx[i].recv_cnt++;
2105 } else if (old_idx != i) {
2106 /* Received Net ID adv for new subnet */
2107
2108 /* Verify last Net ID adv result */
2109 ASSERT_IN_RANGE(k_uptime_get() - net_ctx[old_idx].start,
2110 MAX_TIMEOUT - 1000, MAX_TIMEOUT + 1000);
2111 ASSERT_IN_RANGE(net_ctx[old_idx].recv_cnt, 9, 12);
2112 net_ctx[old_idx].recv_cnt = 0;
2113 old_idx = i;
2114
2115 /* The test ends when all itterations are completed */
2116 itr--;
2117
2118 net_ctx[i].start = k_uptime_get();
2119 net_ctx[i].recv_cnt++;
2120 } else {
2121 /* Received another Net ID adv from same subnet*/
2122 net_ctx[i].recv_cnt++;
2123 }
2124
2125 break;
2126 }
2127 }
2128 }
2129
2130 PASS();
2131 }
2132
test_tx_priv_gatt_proxy(void)2133 static void test_tx_priv_gatt_proxy(void)
2134 {
2135 bt_mesh_test_cfg_set(NULL, WAIT_TIME);
2136 bt_mesh_device_setup(&prov, &prb_comp);
2137 provision(&tx_cfg);
2138 bt_mesh_iv_update_test(true);
2139
2140 ASSERT_TRUE(bt_mesh.iv_index == 0);
2141
2142 /* Disable SNB. */
2143 bt_mesh_beacon_set(false);
2144 ASSERT_OK_MSG(bt_mesh_scan_disable(), "Failed to disable scanner");
2145 ASSERT_OK_MSG(bt_mesh_gatt_proxy_set(BT_MESH_GATT_PROXY_DISABLED),
2146 "Failed to disable gatt proxy");
2147 ASSERT_OK_MSG(bt_mesh_priv_gatt_proxy_set(BT_MESH_PRIV_GATT_PROXY_ENABLED),
2148 "Failed to set private gatt proxy");
2149
2150 /* Wait for proxy connection to complete. */
2151 WAIT_FOR_COND(bt_mesh_proxy_srv_connected_cnt() == 1, 10);
2152
2153 /* Wait a bit so RX device can disable scanner, then start IV update */
2154 k_sleep(K_SECONDS(2));
2155 ASSERT_TRUE(bt_mesh_iv_update());
2156
2157 /* Check that IV index has updated */
2158 ASSERT_TRUE(bt_mesh.iv_index == 1);
2159 PASS();
2160 }
2161
test_rx_priv_gatt_proxy(void)2162 static void test_rx_priv_gatt_proxy(void)
2163 {
2164 bt_mesh_test_cfg_set(NULL, WAIT_TIME);
2165 bt_mesh_device_setup(&prov, &prb_comp);
2166 provision(&rx_cfg);
2167 bt_mesh_iv_update_test(true);
2168
2169 ASSERT_TRUE(bt_mesh.iv_index == 0);
2170
2171 /* Disable SNB. */
2172 bt_mesh_beacon_set(false);
2173 ASSERT_OK_MSG(bt_mesh_gatt_proxy_set(BT_MESH_GATT_PROXY_DISABLED),
2174 "Failed to disable gatt proxy");
2175 ASSERT_OK_MSG(bt_mesh_priv_gatt_proxy_set(BT_MESH_PRIV_GATT_PROXY_ENABLED),
2176 "Failed to set private gatt proxy");
2177 ASSERT_OK_MSG(bt_mesh_proxy_connect(TEST_NET_IDX1), "Failed to connect over proxy");
2178
2179 /* Wait for connection to complete, then disable scanner
2180 * to ensure that all RX communication arrives over GATT.
2181 */
2182 WAIT_FOR_COND(bt_mesh_proxy_cli_is_connected(TEST_NET_IDX1), 10);
2183 ASSERT_OK_MSG(bt_mesh_scan_disable(), "Failed to disable scanner");
2184
2185 /* Wait for the IV index to update.
2186 * Verifying that IV index has changed proves that a private
2187 * beacon arrived successfully over the GATT connection.
2188 */
2189 WAIT_FOR_COND(bt_mesh.iv_index == 1, 10);
2190
2191 PASS();
2192 }
2193
2194 #endif
2195
2196 #define TEST_CASE(role, name, description) \
2197 { \
2198 .test_id = "beacon_" #role "_" #name, \
2199 .test_descr = description, \
2200 .test_pre_init_f = test_##role##_init, \
2201 .test_tick_f = bt_mesh_test_timeout, \
2202 .test_main_f = test_##role##_##name, \
2203 .test_args_f = test_args_parse, \
2204 }
2205
2206 static const struct bst_test_instance test_beacon[] = {
2207 TEST_CASE(tx, on_iv_update, "Beacon: send on IV update"),
2208 TEST_CASE(tx, on_key_refresh, "Beacon: send on key refresh"),
2209 TEST_CASE(tx, invalid, "Beacon: send invalid beacon"),
2210 TEST_CASE(tx, kr_old_key, "Beacon: send old Net Key"),
2211 TEST_CASE(tx, multiple_netkeys, "Beacon: multiple Net Keys"),
2212 TEST_CASE(tx, secure_beacon_interval, "Beacon: send secure beacons"),
2213 TEST_CASE(tx, beacon_cache, "Beacon: advertise duplicate SNBs"),
2214 TEST_CASE(tx, priv_on_iv_update, "Private Beacon: send on IV update"),
2215 TEST_CASE(tx, priv_on_key_refresh, "Private Beacon: send on Key Refresh"),
2216 TEST_CASE(tx, priv_adv, "Private Beacon: advertise Private Beacons"),
2217 TEST_CASE(tx, priv_invalid, "Private Beacon: advertise invalid beacons"),
2218 TEST_CASE(tx, priv_interleave, "Private Beacon: advertise interleaved with SNB"),
2219 TEST_CASE(tx, priv_beacon_cache, "Private Beacon: advertise duplicate Private Beacons"),
2220 #if CONFIG_BT_MESH_GATT_PROXY
2221 TEST_CASE(tx, priv_net_id, "Private Proxy: advertise Net ID"),
2222 TEST_CASE(tx, priv_node_id, "Private Proxy: advertise Node ID"),
2223 TEST_CASE(tx, priv_multi_net_id, "Private Proxy: advertise multiple Net ID"),
2224 TEST_CASE(tx, priv_gatt_proxy, "Private Proxy: Send Private Beacons over GATT"),
2225 TEST_CASE(tx, proxy_adv_multi_subnet_coex, "Proxy Adv: Multi subnet coex proxy adv"),
2226 TEST_CASE(tx, proxy_adv_solicit_trigger, "Proxy Adv: Trigger Solicitation"),
2227 #endif
2228
2229 TEST_CASE(rx, on_iv_update, "Beacon: receive with IV update flag"),
2230 TEST_CASE(rx, on_key_refresh, "Beacon: receive with key refresh flag"),
2231 TEST_CASE(rx, invalid, "Beacon: receive invalid beacon"),
2232 TEST_CASE(rx, kr_old_key, "Beacon: receive old Net Key"),
2233 TEST_CASE(rx, multiple_netkeys, "Beacon: multiple Net Keys"),
2234 TEST_CASE(rx, secure_beacon_interval, "Beacon: receive and send secure beacons"),
2235 TEST_CASE(rx, beacon_cache, "Beacon: receive duplicate SNBs"),
2236 TEST_CASE(rx, priv_adv, "Private Beacon: verify random regeneration"),
2237 TEST_CASE(rx, priv_invalid, "Private Beacon: receive invalid beacons"),
2238 TEST_CASE(rx, priv_interleave, "Private Beacon: interleaved with SNB"),
2239 TEST_CASE(rx, priv_beacon_cache, "Private Beacon: receive duplicate Private Beacons"),
2240 #if CONFIG_BT_MESH_GATT_PROXY
2241 TEST_CASE(rx, priv_net_id, "Private Proxy: scan for Net ID"),
2242 TEST_CASE(rx, priv_node_id, "Private Proxy: scan for Node ID"),
2243 TEST_CASE(rx, priv_multi_net_id, "Private Proxy: scan for multiple Net ID"),
2244 TEST_CASE(rx, priv_gatt_proxy, "Private Proxy: Receive Private Beacons over GATT"),
2245 TEST_CASE(rx, proxy_adv_multi_subnet_coex, "Proxy Adv: Multi subnet coex proxy adv"),
2246 #endif
2247 BSTEST_END_MARKER
2248 };
2249
test_beacon_install(struct bst_test_list * tests)2250 struct bst_test_list *test_beacon_install(struct bst_test_list *tests)
2251 {
2252 tests = bst_add_tests(tests, test_beacon);
2253 return tests;
2254 }
2255