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/adv.h"
10 #include "mesh/net.h"
11 #include "mesh/beacon.h"
12 #include "mesh/mesh.h"
13 #include "mesh/foundation.h"
14 #include "mesh/crypto.h"
15 #include "argparse.h"
16
17 #define LOG_MODULE_NAME test_beacon
18
19 #include <zephyr/logging/log.h>
20 LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_INF);
21
22 #define GROUP_ADDR 0xc000
23 #define WAIT_TIME 60 /*seconds*/
24 #define MULT_NETKEYS_WAIT_TIME 350 /*seconds*/
25 #define BEACON_INTERVAL_WAIT_TIME 750 /*seconds*/
26 #define BEACON_INTERVAL 10 /*seconds*/
27
28 #define BEACON_TYPE_SECURE 0x01
29 #if CONFIG_BT_MESH_V1d1
30 #define BEACON_TYPE_PRIVATE 0x02
31 #endif
32
33 static uint8_t test_net_key_secondary[16] = { 0xca, 0x11, 0xab, 0x1e };
34 static struct {
35 uint8_t primary[16];
36 uint8_t secondary[16];
37 } net_key_pairs[] = {
38 { "\x01\x02", "\x03\x04" },
39 { "\x11\x12", "\x13\x14" },
40 { "\x21\x22", "\x23\x24" },
41 { "\x31\x32", "\x33\x34" },
42 };
43
44 extern enum bst_result_t bst_result;
45
46 static const struct bt_mesh_test_cfg tx_cfg = {
47 .addr = 0x0001,
48 .dev_key = { 0x01 },
49 };
50 static const struct bt_mesh_test_cfg rx_cfg = {
51 .addr = 0x0002,
52 .dev_key = { 0x02 },
53 };
54
55 /* Setting for scanner defining what beacon is expected next, SNB as default */
56 static uint8_t expected_beacon = BEACON_TYPE_SECURE;
57 #if CONFIG_BT_MESH_V1d1
58 static struct bt_mesh_cfg_cli cfg_cli;
59
60 static struct bt_mesh_priv_beacon_cli priv_beacon_cli;
61
62 static const struct bt_mesh_comp prb_comp = {
63 .elem =
64 (struct bt_mesh_elem[]){
65 BT_MESH_ELEM(1,
66 MODEL_LIST(BT_MESH_MODEL_CFG_SRV,
67 BT_MESH_MODEL_CFG_CLI(&cfg_cli),
68 BT_MESH_MODEL_PRIV_BEACON_SRV,
69 BT_MESH_MODEL_PRIV_BEACON_CLI(&priv_beacon_cli)),
70 BT_MESH_MODEL_NONE),
71 },
72 .elem_count = 1,
73 };
74
75 static struct bt_mesh_prov prov;
76 static uint8_t net_key[16] = { 1, 2, 3 };
77 const uint8_t app_key[16] = { 4, 5, 6 };
78 static uint8_t net_key_new[16] = { 7, 8, 9 };
79
80 static uint8_t last_random[13];
81
82 static bt_addr_le_t last_beacon_adv_addr;
83
84 static uint8_t priv_beacon_key[16];
85 #endif /* CONFIG_BT_MESH_V1d1 */
86
87 static int random_interval;
88
test_args_parse(int argc,char * argv[])89 static void test_args_parse(int argc, char *argv[])
90 {
91 bs_args_struct_t args_struct[] = {
92 {
93 .dest = &random_interval,
94 .type = 'i',
95 .name = "{Random interval}",
96 .option = "rand-int",
97 .descript = "Random interval to be set for Private Beacon"
98 },
99 };
100
101 bs_args_parse_all_cmd_line(argc, argv, args_struct);
102 }
103
test_tx_init(void)104 static void test_tx_init(void)
105 {
106 bt_mesh_test_cfg_set(&tx_cfg, WAIT_TIME);
107 }
108
test_rx_init(void)109 static void test_rx_init(void)
110 {
111 bt_mesh_test_cfg_set(&rx_cfg, WAIT_TIME);
112 }
113
ivu_log(void)114 static void ivu_log(void)
115 {
116 LOG_DBG("ivi: %i", bt_mesh.iv_index);
117 LOG_DBG("flags:");
118
119 if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR)) {
120 LOG_DBG("IVU initiator");
121 }
122
123 if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS)) {
124 LOG_DBG("IVU in progress");
125 }
126
127 if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_PENDING)) {
128 LOG_DBG("IVU pending");
129 }
130
131 if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST)) {
132 LOG_DBG("IVU in test mode");
133 }
134 }
135
tx_on_iv_update_test(void)136 static void tx_on_iv_update_test(void)
137 {
138 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR));
139 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
140 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_PENDING));
141 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST));
142 ASSERT_TRUE(bt_mesh.iv_index == 0);
143
144 /* shift beaconing time line to avoid boundary cases. */
145 k_sleep(K_SECONDS(1));
146
147 bt_mesh_iv_update_test(true);
148 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST));
149
150 ASSERT_TRUE(bt_mesh_iv_update());
151 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
152 ASSERT_TRUE(bt_mesh.iv_index == 1);
153
154 k_sleep(K_SECONDS(BEACON_INTERVAL));
155
156 ASSERT_TRUE(!bt_mesh_iv_update());
157 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
158 ASSERT_TRUE(bt_mesh.iv_index == 1);
159
160 k_sleep(K_SECONDS(BEACON_INTERVAL));
161
162 ASSERT_TRUE(bt_mesh_iv_update());
163 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
164 ASSERT_TRUE(bt_mesh.iv_index == 2);
165
166 k_sleep(K_SECONDS(BEACON_INTERVAL));
167
168 PASS();
169 }
170
test_tx_on_iv_update(void)171 static void test_tx_on_iv_update(void)
172 {
173 bt_mesh_test_setup();
174 tx_on_iv_update_test();
175 }
176
test_rx_on_iv_update(void)177 static void test_rx_on_iv_update(void)
178 {
179 bt_mesh_test_setup();
180 /* disable beaconing from Rx device to prevent
181 * the time line adaptation due to observation algorithm.
182 */
183 bt_mesh_beacon_disable();
184 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR));
185 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
186 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_PENDING));
187 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST));
188 ASSERT_TRUE(bt_mesh.iv_index == 0);
189
190 /* shift beaconing time line to avoid boundary cases. */
191 k_sleep(K_SECONDS(1));
192
193 bt_mesh_iv_update_test(true);
194 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST));
195 ivu_log();
196
197 k_sleep(K_SECONDS(BEACON_INTERVAL));
198
199 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
200 ASSERT_TRUE(bt_mesh.iv_index == 1);
201 ivu_log();
202
203 k_sleep(K_SECONDS(BEACON_INTERVAL));
204
205 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
206 ASSERT_TRUE(bt_mesh.iv_index == 1);
207 ivu_log();
208
209 k_sleep(K_SECONDS(BEACON_INTERVAL));
210
211 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
212 ASSERT_TRUE(bt_mesh.iv_index == 2);
213 ivu_log();
214
215 PASS();
216 }
217
tx_on_key_refresh_test(void)218 static void tx_on_key_refresh_test(void)
219 {
220 const uint8_t new_key[16] = {0x01};
221 uint8_t phase;
222 uint8_t status;
223
224 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
225 ASSERT_TRUE(status == STATUS_SUCCESS);
226 ASSERT_TRUE(phase == BT_MESH_KR_NORMAL);
227
228 /* shift beaconing time line to avoid boundary cases. */
229 k_sleep(K_SECONDS(1));
230
231 status = bt_mesh_subnet_update(BT_MESH_KEY_PRIMARY, new_key);
232 ASSERT_TRUE(status == STATUS_SUCCESS);
233 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
234 ASSERT_TRUE(status == STATUS_SUCCESS);
235 ASSERT_TRUE(phase == BT_MESH_KR_PHASE_1);
236
237 k_sleep(K_SECONDS(BEACON_INTERVAL));
238
239 phase = BT_MESH_KR_PHASE_2;
240 status = bt_mesh_subnet_kr_phase_set(BT_MESH_KEY_PRIMARY, &phase);
241 ASSERT_TRUE(status == STATUS_SUCCESS);
242 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
243 ASSERT_TRUE(status == STATUS_SUCCESS);
244 ASSERT_TRUE(phase == BT_MESH_KR_PHASE_2);
245
246 k_sleep(K_SECONDS(BEACON_INTERVAL));
247
248 phase = BT_MESH_KR_PHASE_3;
249 status = bt_mesh_subnet_kr_phase_set(BT_MESH_KEY_PRIMARY, &phase);
250 ASSERT_TRUE(status == STATUS_SUCCESS);
251 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
252 ASSERT_TRUE(status == STATUS_SUCCESS);
253 ASSERT_TRUE(phase == BT_MESH_KR_NORMAL);
254
255 k_sleep(K_SECONDS(BEACON_INTERVAL));
256
257 PASS();
258 }
259
test_tx_on_key_refresh(void)260 static void test_tx_on_key_refresh(void)
261 {
262 bt_mesh_test_setup();
263 tx_on_key_refresh_test();
264 }
265
test_rx_on_key_refresh(void)266 static void test_rx_on_key_refresh(void)
267 {
268 const uint8_t new_key[16] = {0x01};
269 uint8_t phase;
270 uint8_t status;
271
272 bt_mesh_test_setup();
273 /* disable beaconing from Rx device to prevent
274 * the time line adaptation due to observation algorithm.
275 */
276 bt_mesh_beacon_disable();
277
278 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
279 ASSERT_TRUE(status == STATUS_SUCCESS);
280 ASSERT_TRUE(phase == BT_MESH_KR_NORMAL);
281
282 /* shift beaconing time line to avoid boundary cases. */
283 k_sleep(K_SECONDS(1));
284
285 status = bt_mesh_subnet_update(BT_MESH_KEY_PRIMARY, new_key);
286 ASSERT_TRUE(status == STATUS_SUCCESS);
287 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
288 ASSERT_TRUE(status == STATUS_SUCCESS);
289 ASSERT_TRUE(phase == BT_MESH_KR_PHASE_1);
290
291 k_sleep(K_SECONDS(BEACON_INTERVAL));
292
293 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
294 ASSERT_TRUE(status == STATUS_SUCCESS);
295 ASSERT_TRUE(phase == BT_MESH_KR_PHASE_1);
296
297 k_sleep(K_SECONDS(BEACON_INTERVAL));
298
299 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
300 ASSERT_TRUE(status == STATUS_SUCCESS);
301 ASSERT_TRUE(phase == BT_MESH_KR_PHASE_2);
302
303 k_sleep(K_SECONDS(BEACON_INTERVAL));
304
305 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
306 ASSERT_TRUE(status == STATUS_SUCCESS);
307 ASSERT_TRUE(phase == BT_MESH_KR_NORMAL);
308
309 PASS();
310 }
311
312 static struct k_sem observer_sem;
313 static struct {
314 uint8_t flags;
315 uint32_t iv_index;
316 #if CONFIG_BT_MESH_V1d1
317 uint8_t random[13];
318 bt_addr_le_t adv_addr;
319 #endif
320 bool (*process_cb)(const uint8_t *net_id, void *ctx);
321 void *user_ctx;
322 } beacon;
323
beacon_scan_cb(const bt_addr_le_t * addr,int8_t rssi,uint8_t adv_type,struct net_buf_simple * buf)324 static void beacon_scan_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type,
325 struct net_buf_simple *buf)
326 {
327 const uint8_t *net_id;
328 uint8_t ad_data_type, beacon_type, length;
329
330 ASSERT_EQUAL(BT_GAP_ADV_TYPE_ADV_NONCONN_IND, adv_type);
331
332 length = net_buf_simple_pull_u8(buf);
333 ASSERT_EQUAL(buf->len, length);
334 ad_data_type = net_buf_simple_pull_u8(buf);
335
336 if (ad_data_type != BT_DATA_MESH_BEACON) {
337 return;
338 }
339
340 beacon_type = net_buf_simple_pull_u8(buf);
341 if (expected_beacon == BEACON_TYPE_SECURE) {
342 ASSERT_EQUAL(expected_beacon, beacon_type);
343 beacon.flags = net_buf_simple_pull_u8(buf);
344 net_id = net_buf_simple_pull_mem(buf, 8);
345 beacon.iv_index = net_buf_simple_pull_be32(buf);
346 }
347 #if CONFIG_BT_MESH_V1d1
348 else if (expected_beacon == BEACON_TYPE_PRIVATE) {
349 uint8_t private_beacon_data[5];
350
351 ASSERT_EQUAL(expected_beacon, beacon_type);
352 memcpy(beacon.random, buf->data, 13);
353 bt_addr_le_copy(&beacon.adv_addr, addr);
354
355 bt_mesh_beacon_decrypt(priv_beacon_key, &buf->data[0], &buf->data[13],
356 &buf->data[20], private_beacon_data);
357 beacon.flags = private_beacon_data[0];
358 beacon.iv_index = sys_get_be32(&private_beacon_data[1]);
359 }
360 #endif
361 if (!beacon.process_cb || beacon.process_cb(net_id, beacon.user_ctx)) {
362 k_sem_give(&observer_sem);
363 }
364 }
365
366 /* Listens to beacons for one beacon interval (10 seconds). */
wait_for_beacon(bool (* process_cb)(const uint8_t * net_id,void * ctx),void * ctx)367 static bool wait_for_beacon(bool (*process_cb)(const uint8_t *net_id, void *ctx), void *ctx)
368 {
369 struct bt_le_scan_param scan_param = {
370 .type = BT_HCI_LE_SCAN_PASSIVE,
371 .options = BT_LE_SCAN_OPT_NONE,
372 .interval = BT_MESH_ADV_SCAN_UNIT(1000),
373 .window = BT_MESH_ADV_SCAN_UNIT(1000)
374 };
375 bool received = false;
376 int err;
377
378 beacon.process_cb = process_cb;
379 beacon.user_ctx = ctx;
380
381 err = bt_le_scan_start(&scan_param, beacon_scan_cb);
382 if (err && err != -EALREADY) {
383 FAIL("starting scan failed (err %d)", err);
384 }
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 err = k_sem_take(&observer_sem, K_SECONDS(BEACON_INTERVAL + 1));
396 if (!err) {
397 received = true;
398 } else {
399 LOG_WRN("Didn't receive beacon in time (err: %d)", err);
400 }
401
402 err = bt_le_scan_stop();
403 if (err && err != -EALREADY) {
404 FAIL("stopping scan failed (err %d)", err);
405 }
406
407 /* Sleep a little to get to the next beacon interval. Otherwise, calling this function
408 * again will catch the old beacon. This happens due to a known bug in legacy advertiser,
409 * which transmits advertisements longer than should.
410 */
411 if (received && IS_ENABLED(CONFIG_BT_MESH_ADV_LEGACY)) {
412 k_sleep(K_SECONDS(1));
413 }
414
415 return received;
416 }
417
send_beacon(struct net_buf_simple * buf)418 static void send_beacon(struct net_buf_simple *buf)
419 {
420 struct bt_data ad;
421 int err;
422
423 ad.type = BT_DATA_MESH_BEACON;
424 ad.data = buf->data;
425 ad.data_len = buf->len;
426
427 err = bt_le_adv_start(BT_LE_ADV_NCONN, &ad, 1, NULL, 0);
428 if (err) {
429 FAIL("Advertising failed to start (err %d)\n", err);
430 }
431
432 LOG_INF("Advertising started\n");
433
434 k_sleep(K_MSEC(100));
435
436 err = bt_le_adv_stop();
437 if (err) {
438 FAIL("Unable to stop advertising");
439 }
440 }
441
beacon_create(struct net_buf_simple * buf,const uint8_t * net_key,uint8_t flags,uint32_t iv_index)442 static void beacon_create(struct net_buf_simple *buf, const uint8_t *net_key, uint8_t flags,
443 uint32_t iv_index)
444 {
445 uint8_t beacon_key[16];
446 uint8_t net_id[8];
447 uint8_t auth[8];
448 int err;
449
450 err = bt_mesh_k3(net_key, net_id);
451 if (err) {
452 FAIL("Unable to generate Net ID");
453 }
454
455 err = bt_mesh_beacon_key(net_key, beacon_key);
456 if (err) {
457 FAIL("Unable to generate beacon key");
458 }
459
460 err = bt_mesh_beacon_auth(beacon_key, flags, net_id, iv_index, auth);
461 if (err) {
462 FAIL("Unable to generate auth value");
463 }
464
465 net_buf_simple_reset(buf);
466 net_buf_simple_add_u8(buf, BEACON_TYPE_SECURE);
467 net_buf_simple_add_u8(buf, flags);
468 net_buf_simple_add_mem(buf, net_id, 8);
469 net_buf_simple_add_be32(buf, iv_index);
470 net_buf_simple_add_mem(buf, auth, 8);
471 }
472
473 /* Test reception of invalid beacons. */
corrupted_beacon_test(const uint8_t * offsets,ssize_t field_count,struct net_buf_simple * buf)474 static void corrupted_beacon_test(const uint8_t *offsets,
475 ssize_t field_count,
476 struct net_buf_simple *buf)
477 {
478 /* Send corrupted beacons */
479 for (int i = 0; i < field_count; i++) {
480 buf->data[offsets[i]] ^= 0xFF;
481 send_beacon(buf);
482 buf->data[offsets[i]] ^= 0xFF;
483 /* Ensure that interval is not affected. */
484 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
485 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
486 ASSERT_EQUAL(0x00, beacon.flags);
487 ASSERT_EQUAL(0x0000, beacon.iv_index);
488 }
489
490 /* Now the beacon payload is valid and it shall trigger IV Update on the node. It shall also
491 * increase the beacon interval.
492 */
493 send_beacon(buf);
494
495 /* The beacon interval shall be changed and the node shall skip transmission of the next
496 * beacon.
497 */
498 ASSERT_FALSE(wait_for_beacon(NULL, NULL));
499 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
500 ASSERT_EQUAL(0x02, beacon.flags);
501 ASSERT_EQUAL(0x0001, beacon.iv_index);
502 }
503
test_tx_invalid(void)504 static void test_tx_invalid(void)
505 {
506 NET_BUF_SIMPLE_DEFINE(buf, 22);
507 /* Offsets of data to be corrupted: Flags, Network ID, IV Index, Authentication value */
508 uint8_t fields_offsets[4] = {1, 2, 10, 14};
509 int err;
510
511 bt_mesh_test_cfg_set(&tx_cfg, 130);
512 bt_mesh_crypto_init();
513 k_sem_init(&observer_sem, 0, 1);
514
515 err = bt_enable(NULL);
516 if (err) {
517 FAIL("Bluetooth init failed (err %d)", err);
518 return;
519 }
520
521 LOG_INF("Bluetooth initialized");
522
523 /* Let the rx node send the first beacon. */
524 k_sleep(K_SECONDS(5));
525
526 /* Create a valid beacon with IV Update Flag set to 1 and new IV Index. */
527 beacon_create(&buf, test_net_key, 0x02, 0x0001);
528
529 corrupted_beacon_test(fields_offsets, ARRAY_SIZE(fields_offsets), &buf);
530
531 PASS();
532 }
533
534 /* Test reception of invalid beacons. */
test_rx_invalid(void)535 static void test_rx_invalid(void)
536 {
537 bt_mesh_test_cfg_set(&rx_cfg, 130);
538 bt_mesh_test_setup();
539 bt_mesh_iv_update_test(true);
540
541 k_sleep(K_SECONDS(10));
542
543 for (size_t i = 0; i < 4; i++) {
544 ASSERT_FALSE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
545 ASSERT_EQUAL(0, bt_mesh.iv_index);
546
547 k_sleep(K_SECONDS((BEACON_INTERVAL + 1) * 2));
548 }
549
550 /* Only the last beacon shall change IV Update state. */
551 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
552 ASSERT_EQUAL(1, bt_mesh.iv_index);
553
554 PASS();
555 }
556
557 /* Test beacons reception with Key Refresh and IV Update on primary subnet. */
test_tx_kr_old_key(void)558 static void test_tx_kr_old_key(void)
559 {
560 NET_BUF_SIMPLE_DEFINE(buf, 22);
561 int err;
562
563 bt_mesh_test_cfg_set(&tx_cfg, 170);
564 bt_mesh_crypto_init();
565 k_sem_init(&observer_sem, 0, 1);
566
567 err = bt_enable(NULL);
568 if (err) {
569 FAIL("Bluetooth init failed (err %d)", err);
570 return;
571 }
572
573 LOG_INF("Bluetooth initialized");
574
575 /* Let the rx node send the first beacon. */
576 k_sleep(K_SECONDS(5));
577
578 /* The node has added a new Net Key. */
579
580 /* Send a beacon with Key Refresh flag set to 1, but secured with the old Net Key. The
581 * beacon shall not change Key Refresh phase, but should still be processed. The beacon
582 * interval shall be increased.
583 */
584 beacon_create(&buf, test_net_key, 0x01, 0x0000);
585 send_beacon(&buf);
586 ASSERT_FALSE(wait_for_beacon(NULL, NULL));
587 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
588 ASSERT_EQUAL(0x00, beacon.flags);
589 ASSERT_EQUAL(0x0000, beacon.iv_index);
590
591 /* The old Net Key can still initiate IV Index update. */
592 beacon_create(&buf, test_net_key, 0x02, 0x0001);
593 send_beacon(&buf);
594 ASSERT_FALSE(wait_for_beacon(NULL, NULL));
595 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
596 ASSERT_EQUAL(0x02, beacon.flags);
597 ASSERT_EQUAL(0x0001, beacon.iv_index);
598
599 /* Send beacon with Key Refresh flag set to 1, IV Update flag set to 1, but secured with
600 * the new Net Key. The node shall set Key Refresh phase to 2. The beacon interval shall
601 * be increased.
602 */
603 beacon_create(&buf, test_net_key_secondary, 0x03, 0x0001);
604 send_beacon(&buf);
605 ASSERT_FALSE(wait_for_beacon(NULL, NULL));
606 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
607 ASSERT_EQUAL(0x03, beacon.flags);
608 ASSERT_EQUAL(0x0001, beacon.iv_index);
609
610 /* Send beacon with Key Refresh flag set to 1, IV Update flag set to 0, but secured with
611 * the old Net Key. The beacon shall be rejected. The beacon interval shall not be changed.
612 */
613 beacon_create(&buf, test_net_key, 0x01, 0x0001);
614 send_beacon(&buf);
615 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
616 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
617 ASSERT_EQUAL(0x03, beacon.flags);
618 ASSERT_EQUAL(0x0001, beacon.iv_index);
619
620 /* Try the same with the new Net Key. Now the node shall change Key Refresh phase to 0. The
621 * beacon interval shall be increased.
622 */
623 beacon_create(&buf, test_net_key_secondary, 0x02, 0x0001);
624 send_beacon(&buf);
625 ASSERT_FALSE(wait_for_beacon(NULL, NULL));
626 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
627 ASSERT_EQUAL(0x02, beacon.flags);
628 ASSERT_EQUAL(0x0001, beacon.iv_index);
629
630 /* Send beacon with IV Update flag set to 0 and secured with the old Net Key. The beacon
631 * shall be ignored. The beacon interval shall not be changed.
632 */
633 beacon_create(&buf, test_net_key, 0x00, 0x0001);
634 send_beacon(&buf);
635 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
636 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
637 ASSERT_EQUAL(0x02, beacon.flags);
638 ASSERT_EQUAL(0x0001, beacon.iv_index);
639
640 /* Do the same, but secure beacon with the new Net Key. Now the node shall change IV Update
641 * flag to 0. The beacon interval shall be increased.
642 */
643 beacon_create(&buf, test_net_key_secondary, 0x00, 0x0001);
644 send_beacon(&buf);
645 ASSERT_FALSE(wait_for_beacon(NULL, NULL));
646 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
647 ASSERT_EQUAL(0x00, beacon.flags);
648 ASSERT_EQUAL(0x0001, beacon.iv_index);
649
650 PASS();
651 }
652
653 /* Test beacons reception with Key Refresh and IV Update on primary subnet. */
test_rx_kr_old_key(void)654 static void test_rx_kr_old_key(void)
655 {
656 uint8_t phase;
657 uint8_t status;
658 int err;
659
660 bt_mesh_test_cfg_set(&rx_cfg, 170);
661 bt_mesh_test_setup();
662 bt_mesh_iv_update_test(true);
663
664 err = bt_mesh_cfg_cli_net_key_update(0, cfg->addr, 0, test_net_key_secondary, &status);
665 if (err || status) {
666 FAIL("Net Key update failed (err %d, status %u)", err, status);
667 }
668
669 static struct {
670 uint8_t phase;
671 bool ivu;
672 uint32_t ivi;
673 } test_vector[] = {
674 /* Old Net Key, attempt to change Key Refresh phase to 2. */
675 { .phase = BT_MESH_KR_PHASE_1, .ivu = false, .ivi = 0 },
676 /* Old Net Key, changing IV Update state. */
677 { .phase = BT_MESH_KR_PHASE_1, .ivu = true, .ivi = 1 },
678 /* New Net Key, changing Key Refresh phase. */
679 { .phase = BT_MESH_KR_PHASE_2, .ivu = true, .ivi = 1 },
680 /* Old Net Key, attempt to change IV Update state. */
681 { .phase = BT_MESH_KR_PHASE_2, .ivu = true, .ivi = 1 },
682 /* New Net Key, changing Key Refresh phase to 0. */
683 { .phase = BT_MESH_KR_NORMAL, .ivu = true, .ivi = 1 },
684 /* Old Net Key, attempt to change IV Update state to Idle.*/
685 { .phase = BT_MESH_KR_NORMAL, .ivu = true, .ivi = 1 },
686 /* Net Net Key, changing IV Update state to Idle. */
687 { .phase = BT_MESH_KR_NORMAL, .ivu = false, .ivi = 1 },
688 };
689
690 k_sleep(K_SECONDS(8));
691
692 for (size_t i = 0; i < ARRAY_SIZE(test_vector); i++) {
693 status = bt_mesh_subnet_kr_phase_get(0, &phase);
694 if (status != STATUS_SUCCESS) {
695 FAIL("Unable to populate Key Refresh phase (status: %d)", status);
696 }
697
698 ASSERT_EQUAL(test_vector[i].phase, phase);
699 ASSERT_EQUAL(test_vector[i].ivu, atomic_test_bit(bt_mesh.flags,
700 BT_MESH_IVU_IN_PROGRESS));
701 ASSERT_EQUAL(test_vector[i].ivi, bt_mesh.iv_index);
702
703 k_sleep(K_SECONDS((BEACON_INTERVAL + 1) * 2));
704 }
705
706 PASS();
707 }
708
beacon_confirm_by_subnet(const uint8_t * net_id,void * ctx)709 static bool beacon_confirm_by_subnet(const uint8_t *net_id, void *ctx)
710 {
711 const uint8_t *expected_net_id = ctx;
712
713 return !memcmp(expected_net_id, net_id, 8);
714 }
715
beacon_confirm_all_subnets(const uint8_t * net_id,void * ctx)716 static bool beacon_confirm_all_subnets(const uint8_t *net_id, void *ctx)
717 {
718 static uint32_t counter;
719 int err;
720
721 for (size_t i = 0; i < ARRAY_SIZE(net_key_pairs); i++) {
722 uint8_t expected_net_id[8];
723
724 err = bt_mesh_k3(net_key_pairs[i].secondary, expected_net_id);
725 if (err) {
726 FAIL("Unable to generate Net ID");
727 }
728
729 if (!memcmp(expected_net_id, net_id, 8)) {
730 LOG_INF("Received beacon for Net Key Idx %d", (i + 1));
731 counter |= 1 << i;
732
733 ASSERT_EQUAL(0x00, beacon.flags);
734 ASSERT_EQUAL(0x0000, beacon.iv_index);
735 }
736 }
737
738 if (counter == BIT_MASK(ARRAY_SIZE(net_key_pairs))) {
739 counter = 0;
740 return true;
741 } else {
742 return false;
743 }
744 }
745
746 /* Test beacons rejection with multiple Net Keys. */
test_tx_multiple_netkeys(void)747 static void test_tx_multiple_netkeys(void)
748 {
749 NET_BUF_SIMPLE_DEFINE(buf, 22);
750 int err;
751
752 bt_mesh_test_cfg_set(&tx_cfg, MULT_NETKEYS_WAIT_TIME);
753 bt_mesh_crypto_init();
754 k_sem_init(&observer_sem, 0, 1);
755
756 err = bt_enable(NULL);
757 if (err) {
758 FAIL("Bluetooth init failed (err %d)", err);
759 return;
760 }
761
762 LOG_INF("Bluetooth initialized");
763
764 /* Let the rx node send the first beacon. */
765 k_sleep(K_SECONDS(5));
766
767 /* The node has added new Net Keys. */
768
769 for (size_t i = 0; i < ARRAY_SIZE(net_key_pairs); i++) {
770 uint8_t net_id_secondary[8];
771
772 err = bt_mesh_k3(net_key_pairs[i].secondary, net_id_secondary);
773 if (err) {
774 FAIL("Unable to generate Net ID");
775 }
776
777 /* Send beacon with Key Refresh flag set to 1, but secured with the old Net Key.
778 * The beacon shall be processed, but shall not change Key Refresh phase.
779 */
780 beacon_create(&buf, net_key_pairs[i].primary, 0x01, 0x0000);
781 send_beacon(&buf);
782 ASSERT_FALSE(wait_for_beacon(beacon_confirm_by_subnet, &buf.data[2]));
783 ASSERT_TRUE(wait_for_beacon(beacon_confirm_by_subnet, &buf.data[2]));
784 ASSERT_EQUAL(0x00, beacon.flags);
785 ASSERT_EQUAL(0x0000, beacon.iv_index);
786
787 /* Do the same, but secure beacon with the new Net Key. The node shall set Key
788 * Refresh phase to 2.
789 */
790 beacon_create(&buf, net_key_pairs[i].secondary, 0x01, 0x0000);
791 send_beacon(&buf);
792 ASSERT_FALSE(wait_for_beacon(beacon_confirm_by_subnet, net_id_secondary));
793 ASSERT_TRUE(wait_for_beacon(beacon_confirm_by_subnet, net_id_secondary));
794 ASSERT_EQUAL(0x01, beacon.flags);
795 ASSERT_EQUAL(0x0000, beacon.iv_index);
796
797 /* Send beacon with Key Refresh flag set to 0, but secured with the old Net Key.
798 * The beacon shall be rejected. The beacon interval shall not be changed.
799 */
800 beacon_create(&buf, net_key_pairs[i].primary, 0x00, 0x0000);
801 send_beacon(&buf);
802 ASSERT_TRUE(wait_for_beacon(beacon_confirm_by_subnet, net_id_secondary));
803 ASSERT_TRUE(wait_for_beacon(beacon_confirm_by_subnet, net_id_secondary));
804 ASSERT_EQUAL(0x01, beacon.flags);
805 ASSERT_EQUAL(0x0000, beacon.iv_index);
806
807 /* Do the same with the new Net Key. Now the node shall change Key Refresh phase
808 * to 0. The beacon interval shall be increased.
809 */
810 beacon_create(&buf, net_key_pairs[i].secondary, 0x00, 0x0000);
811 send_beacon(&buf);
812 ASSERT_FALSE(wait_for_beacon(beacon_confirm_by_subnet, net_id_secondary));
813 ASSERT_TRUE(wait_for_beacon(beacon_confirm_by_subnet, net_id_secondary));
814 ASSERT_EQUAL(0x00, beacon.flags);
815 ASSERT_EQUAL(0x0000, beacon.iv_index);
816 }
817
818 /* Create a valid beacon secured with unknown Net Key. The node shall ignore the beacon and
819 * continue sending beacons regularly.
820 */
821 uint8_t unknown_net_key[16] = { 0xde, 0xad, 0xbe, 0xef };
822
823 beacon_create(&buf, unknown_net_key, 0x00, 0x0000);
824 send_beacon(&buf);
825 /* Ensure that interval is not affected. */
826 ASSERT_TRUE(wait_for_beacon(beacon_confirm_all_subnets, NULL));
827 ASSERT_TRUE(wait_for_beacon(beacon_confirm_all_subnets, NULL));
828
829 PASS();
830 }
831
832 /* Test beacons rejection with multiple Net Keys. */
test_rx_multiple_netkeys(void)833 static void test_rx_multiple_netkeys(void)
834 {
835 uint8_t phase;
836 uint8_t status;
837 int err;
838
839 bt_mesh_test_cfg_set(&rx_cfg, MULT_NETKEYS_WAIT_TIME);
840 bt_mesh_test_setup();
841 bt_mesh_iv_update_test(true);
842
843 /* Add new Net Keys and switch Key Refresh phase to 1 so that beacons can trigger Key
844 * Refresh procedure.
845 */
846 for (size_t i = 0; i < ARRAY_SIZE(net_key_pairs); i++) {
847 err = bt_mesh_cfg_cli_net_key_add(0, cfg->addr, i + 1, net_key_pairs[i].primary,
848 &status);
849 if (err || status) {
850 FAIL("Net Key add failed (err %d, status %u)", err, status);
851 }
852
853 err = bt_mesh_cfg_cli_net_key_update(0, cfg->addr, i + 1,
854 net_key_pairs[i].secondary, &status);
855 if (err || status) {
856 FAIL("Net Key update failed (err %d, status %u)", err, status);
857 }
858 }
859
860 for (size_t i = 0; i < ARRAY_SIZE(net_key_pairs); i++) {
861 /* Tx device shall change Key Refresh phase to 2. */
862 k_sleep(K_SECONDS(40));
863
864 status = bt_mesh_subnet_kr_phase_get(i + 1, &phase);
865 if (status != STATUS_SUCCESS) {
866 FAIL("Unable to populate Key Refresh phase (status: %d)", status);
867 }
868
869 ASSERT_EQUAL(BT_MESH_KR_PHASE_2, phase);
870
871 /* Tx device shall change Key Refresh phase to 0. */
872 k_sleep(K_SECONDS(40));
873
874 status = bt_mesh_subnet_kr_phase_get(i + 1, &phase);
875 if (status != STATUS_SUCCESS) {
876 FAIL("Unable to populate Key Refresh phase (status: %d)", status);
877 }
878
879 ASSERT_EQUAL(BT_MESH_KR_NORMAL, phase);
880 }
881
882 PASS();
883
884 }
885
886 static struct k_work_delayable beacon_timer;
887
secure_beacon_send(struct k_work * work)888 static void secure_beacon_send(struct k_work *work)
889 {
890 NET_BUF_SIMPLE_DEFINE(buf, 22);
891 beacon_create(&buf, test_net_key, 0, 0);
892 send_beacon(&buf);
893 /**
894 * Sending SNB(secure network beacon) faster to guarantee
895 * at least one beacon is received by tx node in 10s period.
896 */
897 k_work_schedule(&beacon_timer, K_SECONDS(2));
898 }
899
test_tx_secure_beacon_interval(void)900 static void test_tx_secure_beacon_interval(void)
901 {
902 bt_mesh_test_cfg_set(&tx_cfg, BEACON_INTERVAL_WAIT_TIME);
903 k_sleep(K_SECONDS(2));
904 bt_mesh_test_setup();
905 PASS();
906 }
907
test_rx_secure_beacon_interval(void)908 static void test_rx_secure_beacon_interval(void)
909 {
910 NET_BUF_SIMPLE_DEFINE(buf, 22);
911 int err;
912 int64_t beacon_recv_time;
913 int64_t delta;
914
915 bt_mesh_test_cfg_set(&rx_cfg, BEACON_INTERVAL_WAIT_TIME);
916 bt_mesh_crypto_init();
917 k_sem_init(&observer_sem, 0, 1);
918 k_work_init_delayable(&beacon_timer, secure_beacon_send);
919
920 err = bt_enable(NULL);
921 if (err) {
922 FAIL("Bluetooth init failed (err %d)", err);
923 }
924
925 beacon_create(&buf, test_net_key, 0, 0);
926 k_sleep(K_SECONDS(5));
927 /*wait provisioned tx node to send the first beacon*/
928 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
929 k_sleep(K_SECONDS(2));
930
931 /**
932 * Sending 2 SNB 20ms apart by only sending for even values of loop variable.
933 * And verify that tx node adapts to 20s SNB interval after sending enough
934 * beacons in for loop.
935 */
936 for (size_t i = 1; i < 5; i++) {
937 if (i % 2) {
938 send_beacon(&buf);
939 ASSERT_FALSE(wait_for_beacon(NULL, NULL));
940 } else {
941 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
942 }
943 }
944
945 /**
946 * Verify that tx node keeps the 20s SNB interval until adapts itself and
947 * sends SNB in 10s again.
948 */
949 ASSERT_FALSE(wait_for_beacon(NULL, NULL));
950 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
951 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
952 beacon_recv_time = k_uptime_get();
953 /* Start sending SNB */
954 k_work_schedule(&beacon_timer, K_NO_WAIT);
955
956 /**
957 * Send SNB so that the tx node stays silent and eventually sends
958 * an SNB after 600s, which is the maximum time for SNB interval.
959 * Sending beacon with 2sec interval.
960 */
961 delta = 0;
962 for (size_t i = 0; i < 60; i++) {
963 if (wait_for_beacon(NULL, NULL)) {
964 delta = k_uptime_delta(&beacon_recv_time);
965 break;
966 }
967 }
968
969 ASSERT_TRUE(delta >= (600 * MSEC_PER_SEC));
970 PASS();
971 }
972
973 #if CONFIG_BT_MESH_V1d1
private_beacon_check(const uint8_t * net_id,void * ctx)974 static bool private_beacon_check(const uint8_t *net_id, void *ctx)
975 {
976 bool ret;
977 bool same_random = (bool *)ctx;
978
979 if (memcmp(beacon.adv_addr.a.val, &last_beacon_adv_addr, BT_ADDR_SIZE) == 0) {
980 return false;
981 }
982
983 memcpy(&last_beacon_adv_addr, beacon.adv_addr.a.val, BT_ADDR_SIZE);
984
985 if (same_random) {
986 ret = memcmp(beacon.random, last_random, 13) == 0;
987 } else {
988 ret = memcmp(beacon.random, last_random, 13) != 0;
989 }
990
991 memcpy(&last_random, beacon.random, 13);
992
993 return ret;
994 }
995
provision(const struct bt_mesh_test_cfg * dev_cfg)996 static void provision(const struct bt_mesh_test_cfg *dev_cfg)
997 {
998 int err;
999
1000 err = bt_mesh_provision(net_key, 0, 0, 0, dev_cfg->addr, dev_cfg->dev_key);
1001 if (err) {
1002 FAIL("Provisioning failed (err %d)", err);
1003 }
1004 }
1005
tx_priv_setup(void)1006 static void tx_priv_setup(void)
1007 {
1008 uint8_t status;
1009 struct bt_mesh_priv_beacon val;
1010 int err;
1011
1012 bt_mesh_test_cfg_set(NULL, WAIT_TIME);
1013 bt_mesh_device_setup(&prov, &prb_comp);
1014 provision(&tx_cfg);
1015
1016 val.enabled = 1;
1017 val.rand_interval = random_interval;
1018
1019 err = bt_mesh_cfg_cli_beacon_set(0, tx_cfg.addr, 0, &status);
1020 if (err || status != 0) {
1021 FAIL("Beacon set failed (err %d, status %u)", err, status);
1022 }
1023
1024 err = bt_mesh_priv_beacon_cli_set(0, tx_cfg.addr, &val);
1025 if (err) {
1026 FAIL("Failed to enable Private Beacon (err=%d)", err);
1027 }
1028 }
1029
test_tx_priv_on_iv_update(void)1030 static void test_tx_priv_on_iv_update(void)
1031 {
1032 tx_priv_setup();
1033
1034 tx_on_iv_update_test();
1035 }
1036
test_tx_priv_on_key_refresh(void)1037 static void test_tx_priv_on_key_refresh(void)
1038 {
1039 tx_priv_setup();
1040
1041 tx_on_key_refresh_test();
1042 }
1043
test_tx_priv_adv(void)1044 static void test_tx_priv_adv(void)
1045 {
1046 uint8_t status;
1047 struct bt_mesh_priv_beacon val;
1048 int err;
1049
1050 bt_mesh_test_cfg_set(NULL, BEACON_INTERVAL_WAIT_TIME);
1051 bt_mesh_device_setup(&prov, &prb_comp);
1052 provision(&tx_cfg);
1053
1054 err = bt_mesh_cfg_cli_beacon_set(0, tx_cfg.addr, 0, &status);
1055 if (err || status != 0) {
1056 FAIL("Beacon set failed (err %d, status %u)", err, status);
1057 }
1058
1059 val.enabled = 1;
1060 val.rand_interval = 1;
1061
1062 err = bt_mesh_priv_beacon_cli_set(0, tx_cfg.addr, &val);
1063 if (err) {
1064 FAIL("Failed to enable Private Beacon (err=%d)", err);
1065 }
1066
1067 k_sleep(K_SECONDS(6 * BEACON_INTERVAL));
1068
1069 val.rand_interval = 0;
1070
1071 err = bt_mesh_priv_beacon_cli_set(0, tx_cfg.addr, &val);
1072 if (err) {
1073 FAIL("Failed to enable Private Beacon (err=%d)", err);
1074 }
1075
1076 k_sleep(K_SECONDS(6 * BEACON_INTERVAL));
1077
1078 val.rand_interval = 0;
1079
1080 err = bt_mesh_priv_beacon_cli_set(0, tx_cfg.addr, &val);
1081 if (err) {
1082 FAIL("Failed to enable Private Beacon (err=%d)", err);
1083 }
1084
1085 k_sleep(K_SECONDS(6 * BEACON_INTERVAL));
1086
1087 val.rand_interval = 3;
1088
1089 err = bt_mesh_priv_beacon_cli_set(0, tx_cfg.addr, &val);
1090 if (err) {
1091 FAIL("Failed to enable Private Beacon (err=%d)", err);
1092 }
1093
1094 PASS();
1095 }
1096
test_rx_priv_adv(void)1097 static void test_rx_priv_adv(void)
1098 {
1099 bool same_random;
1100 int err, i;
1101
1102 bt_mesh_test_cfg_set(&rx_cfg, BEACON_INTERVAL_WAIT_TIME);
1103 k_sem_init(&observer_sem, 0, 1);
1104
1105 err = bt_enable(NULL);
1106 if (err) {
1107 FAIL("Bluetooth init failed (err %d)", err);
1108 }
1109
1110 expected_beacon = BEACON_TYPE_PRIVATE;
1111
1112 same_random = false;
1113 /* TX device is advertising with Random Interval = 1 for 6 intervals
1114 * and with Random Interval = 0 for another 6
1115 */
1116 for (i = 0; i < 12; i++) {
1117 wait_for_beacon(private_beacon_check, &same_random);
1118 }
1119
1120 /* TX device is advertising with Random Interval = 3 */
1121 for (i = 0; i < 2; i++) {
1122 same_random = true;
1123
1124 for (int j = 0; j < 2; j++) {
1125 wait_for_beacon(private_beacon_check, &same_random);
1126 }
1127
1128 k_sleep(K_SECONDS(BEACON_INTERVAL));
1129
1130 /* Beacon random should change here */
1131 same_random = true;
1132 wait_for_beacon(private_beacon_check, &same_random);
1133 }
1134
1135 PASS();
1136 }
1137
private_beacon_create(struct net_buf_simple * buf,const uint8_t * net_key,uint8_t flags,uint32_t iv_index)1138 static void private_beacon_create(struct net_buf_simple *buf, const uint8_t *net_key, uint8_t flags,
1139 uint32_t iv_index)
1140 {
1141 uint8_t net_id[8];
1142 uint8_t auth[8];
1143 uint8_t data[5];
1144 uint8_t random_val[13];
1145 int err;
1146
1147 err = bt_mesh_k3(net_key, net_id);
1148 if (err) {
1149 FAIL("Unable to generate Net ID");
1150 }
1151
1152 err = bt_mesh_private_beacon_key(net_key, priv_beacon_key);
1153 if (err) {
1154 FAIL("Unable to generate beacon key");
1155 }
1156
1157 err = bt_mesh_beacon_auth(priv_beacon_key, flags, net_id, iv_index, auth);
1158 if (err) {
1159 FAIL("Unable to generate auth value");
1160 }
1161
1162 bt_rand(random_val, sizeof(random_val));
1163 bt_mesh_beacon_encrypt(priv_beacon_key, flags, bt_mesh.iv_index + 1,
1164 random_val, data, auth);
1165
1166 net_buf_simple_reset(buf);
1167 net_buf_simple_add_u8(buf, BEACON_TYPE_PRIVATE);
1168 net_buf_simple_add_mem(buf, random_val, 13);
1169 net_buf_simple_add_mem(buf, data, 5);
1170 net_buf_simple_add_mem(buf, auth, 8);
1171
1172 }
1173
test_tx_priv_invalid(void)1174 static void test_tx_priv_invalid(void)
1175 {
1176 uint8_t fields_offsets[4] = {1, 14, 15, 19};
1177
1178 NET_BUF_SIMPLE_DEFINE(buf, 27);
1179 int err;
1180
1181 bt_mesh_test_cfg_set(&tx_cfg, 130);
1182 bt_mesh_crypto_init();
1183 k_sem_init(&observer_sem, 0, 1);
1184
1185 err = bt_enable(NULL);
1186 if (err) {
1187 FAIL("Bluetooth init failed (err %d)", err);
1188 }
1189
1190 LOG_INF("Bluetooth initialized");
1191
1192 /* Let the rx node send the first beacon. */
1193 k_sleep(K_SECONDS(5));
1194
1195 /* Create a valid beacon with IV Update Flag set to 1 and new IV Index. */
1196 private_beacon_create(&buf, net_key, 0x02, 0x0001);
1197
1198 expected_beacon = BEACON_TYPE_PRIVATE;
1199
1200 corrupted_beacon_test(fields_offsets, ARRAY_SIZE(fields_offsets), &buf);
1201
1202 PASS();
1203 }
1204
test_rx_priv_invalid(void)1205 static void test_rx_priv_invalid(void)
1206 {
1207 uint8_t status;
1208 struct bt_mesh_priv_beacon val;
1209 int err;
1210
1211 bt_mesh_test_cfg_set(NULL, 130);
1212 bt_mesh_device_setup(&prov, &prb_comp);
1213 provision(&rx_cfg);
1214 bt_mesh_iv_update_test(true);
1215
1216 val.enabled = 1;
1217 val.rand_interval = random_interval;
1218
1219 err = bt_mesh_cfg_cli_beacon_set(0, rx_cfg.addr, 0, &status);
1220 if (err || status != 0) {
1221 FAIL("Beacon set failed (err %d, status %u)", err, status);
1222 }
1223
1224 err = bt_mesh_priv_beacon_cli_set(0, rx_cfg.addr, &val);
1225 if (err) {
1226 FAIL("Failed to enable Private Beacon (err=%d)", err);
1227 }
1228
1229 for (size_t i = 0; i < 4; i++) {
1230 ASSERT_FALSE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
1231 ASSERT_EQUAL(0, bt_mesh.iv_index);
1232
1233 k_sleep(K_SECONDS((BEACON_INTERVAL + 1) * 2));
1234 }
1235
1236 /* Only the last beacon shall change IV Update state. */
1237 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
1238 ASSERT_EQUAL(1, bt_mesh.iv_index);
1239
1240 PASS();
1241 }
1242
toggle_priv_beacon(uint16_t addr,uint8_t enabled)1243 static void toggle_priv_beacon(uint16_t addr, uint8_t enabled)
1244 {
1245 int err;
1246 uint8_t status;
1247 struct bt_mesh_priv_beacon val;
1248
1249 err = bt_mesh_cfg_cli_beacon_set(0, addr, !enabled, &status);
1250 if (err || status != !enabled) {
1251 FAIL("Beacon set failed (err %d, status %u)", err, status);
1252 }
1253
1254 val.enabled = enabled;
1255 val.rand_interval = 1;
1256
1257 err = bt_mesh_priv_beacon_cli_set(0, addr, &val);
1258 if (err) {
1259 FAIL("Failed to enable Private Beacon (err=%d)", err);
1260 }
1261 }
1262
test_tx_priv_interleave(void)1263 static void test_tx_priv_interleave(void)
1264 {
1265 uint8_t phase;
1266 uint8_t status;
1267 struct bt_mesh_subnet *sub;
1268
1269
1270 bt_mesh_test_cfg_set(NULL, BEACON_INTERVAL_WAIT_TIME);
1271 bt_mesh_device_setup(&prov, &prb_comp);
1272 provision(&tx_cfg);
1273
1274 sub = bt_mesh_subnet_get(0);
1275 ASSERT_TRUE(sub != NULL);
1276
1277 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR));
1278 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
1279 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_PENDING));
1280 ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST));
1281 ASSERT_TRUE(bt_mesh.iv_index == 0);
1282
1283 status = bt_mesh_subnet_kr_phase_get(0, &phase);
1284 ASSERT_TRUE(status == STATUS_SUCCESS);
1285 ASSERT_TRUE(phase == BT_MESH_KR_NORMAL);
1286
1287 /* Wait for SNB being advertised and switch beacon type between Beacon Intervals */
1288 k_sleep(K_SECONDS(BEACON_INTERVAL + 5));
1289
1290 toggle_priv_beacon(tx_cfg.addr, 1);
1291
1292 bt_mesh_iv_update_test(true);
1293 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST));
1294
1295 ASSERT_TRUE(bt_mesh_iv_update());
1296 ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
1297 ASSERT_TRUE(bt_mesh.iv_index == 1);
1298
1299 /* Switch beacon type between Beacon Intervals */
1300 k_sleep(K_SECONDS(BEACON_INTERVAL + 5));
1301
1302 toggle_priv_beacon(tx_cfg.addr, 0);
1303
1304 status = bt_mesh_subnet_update(BT_MESH_KEY_PRIMARY, net_key_new);
1305 ASSERT_TRUE(status == STATUS_SUCCESS);
1306 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
1307 ASSERT_TRUE(status == STATUS_SUCCESS);
1308 ASSERT_TRUE(phase == BT_MESH_KR_PHASE_1);
1309
1310 phase = BT_MESH_KR_PHASE_2;
1311 status = bt_mesh_subnet_kr_phase_set(BT_MESH_KEY_PRIMARY, &phase);
1312 ASSERT_TRUE(status == STATUS_SUCCESS);
1313 status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
1314 ASSERT_TRUE(status == STATUS_SUCCESS);
1315 ASSERT_TRUE(phase == BT_MESH_KR_PHASE_2);
1316
1317 k_sleep(K_SECONDS(BEACON_INTERVAL + 5));
1318 toggle_priv_beacon(tx_cfg.addr, 1);
1319
1320 PASS();
1321 }
1322
test_rx_priv_interleave(void)1323 static void test_rx_priv_interleave(void)
1324 {
1325 int err;
1326
1327 bt_mesh_test_cfg_set(&rx_cfg, BEACON_INTERVAL_WAIT_TIME);
1328 k_sem_init(&observer_sem, 0, 1);
1329
1330 err = bt_mesh_private_beacon_key(net_key, priv_beacon_key);
1331 if (err) {
1332 FAIL("Unable to generate beacon key");
1333 }
1334
1335 err = bt_enable(NULL);
1336 if (err) {
1337 FAIL("Bluetooth init failed (err %d)", err);
1338 }
1339
1340 expected_beacon = BEACON_TYPE_SECURE;
1341 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
1342
1343 expected_beacon = BEACON_TYPE_PRIVATE;
1344 ASSERT_TRUE(wait_for_beacon(private_beacon_check, false));
1345
1346 /* IVU was started here */
1347 ASSERT_TRUE(wait_for_beacon(private_beacon_check, false));
1348 ASSERT_EQUAL(0x02, beacon.flags);
1349 ASSERT_EQUAL(0x0001, beacon.iv_index);
1350
1351 memset(&beacon, 0, sizeof(beacon));
1352 expected_beacon = BEACON_TYPE_SECURE;
1353
1354 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
1355 ASSERT_EQUAL(0x02, beacon.flags);
1356 ASSERT_EQUAL(0x0001, beacon.iv_index);
1357
1358 /* KR was started here */
1359 ASSERT_TRUE(wait_for_beacon(NULL, NULL));
1360 ASSERT_EQUAL(0x03, beacon.flags);
1361 ASSERT_EQUAL(0x0001, beacon.iv_index);
1362
1363 expected_beacon = BEACON_TYPE_PRIVATE;
1364
1365 err = bt_mesh_private_beacon_key(net_key_new, priv_beacon_key);
1366
1367 ASSERT_TRUE(wait_for_beacon(private_beacon_check, false));
1368 ASSERT_EQUAL(0x03, beacon.flags);
1369 ASSERT_EQUAL(0x0001, beacon.iv_index);
1370
1371 PASS();
1372 }
1373 #endif /* CONFIG_BT_MESH_V1d1 */
1374
1375 #define TEST_CASE(role, name, description) \
1376 { \
1377 .test_id = "beacon_" #role "_" #name, \
1378 .test_descr = description, \
1379 .test_pre_init_f = test_##role##_init, \
1380 .test_tick_f = bt_mesh_test_timeout, \
1381 .test_main_f = test_##role##_##name, \
1382 .test_args_f = test_args_parse, \
1383 }
1384
1385 static const struct bst_test_instance test_beacon[] = {
1386 TEST_CASE(tx, on_iv_update, "Beacon: send on IV update"),
1387 TEST_CASE(tx, on_key_refresh, "Beacon: send on key refresh"),
1388 TEST_CASE(tx, invalid, "Beacon: send invalid beacon"),
1389 TEST_CASE(tx, kr_old_key, "Beacon: send old Net Key"),
1390 TEST_CASE(tx, multiple_netkeys, "Beacon: multiple Net Keys"),
1391 TEST_CASE(tx, secure_beacon_interval, "Beacon: send secure beacons"),
1392 #if CONFIG_BT_MESH_V1d1
1393 TEST_CASE(tx, priv_on_iv_update, "Private Beacon: send on IV update"),
1394 TEST_CASE(tx, priv_on_key_refresh, "Private Beacon: send on Key Refresh"),
1395 TEST_CASE(tx, priv_adv, "Private Beacon: advertise Private Beacons"),
1396 TEST_CASE(tx, priv_invalid, "Private Beacon: advertise invalid beacons"),
1397 TEST_CASE(tx, priv_interleave, "Private Beacon: advertise interleaved with SNB"),
1398 #endif
1399
1400 TEST_CASE(rx, on_iv_update, "Beacon: receive with IV update flag"),
1401 TEST_CASE(rx, on_key_refresh, "Beacon: receive with key refresh flag"),
1402 TEST_CASE(rx, invalid, "Beacon: receive invalid beacon"),
1403 TEST_CASE(rx, kr_old_key, "Beacon: receive old Net Key"),
1404 TEST_CASE(rx, multiple_netkeys, "Beacon: multiple Net Keys"),
1405 TEST_CASE(rx, secure_beacon_interval, "Beacon: receive and send secure beacons"),
1406 #if CONFIG_BT_MESH_V1d1
1407 TEST_CASE(rx, priv_adv, "Private Beacon: verify random regeneration"),
1408 TEST_CASE(rx, priv_invalid, "Private Beacon: receive invalid beacons"),
1409 TEST_CASE(rx, priv_interleave, "Private Beacon: interleaved with SNB"),
1410 #endif
1411 BSTEST_END_MARKER
1412 };
1413
test_beacon_install(struct bst_test_list * tests)1414 struct bst_test_list *test_beacon_install(struct bst_test_list *tests)
1415 {
1416 tests = bst_add_tests(tests, test_beacon);
1417 return tests;
1418 }
1419