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 struct bt_mesh_key priv_beacon_key;
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 	uint64_t pp_hash;
319 	uint64_t pp_random;
320 	bt_addr_le_t adv_addr;
321 #endif
322 	bool (*process_cb)(const uint8_t *net_id, void *ctx);
323 	void *user_ctx;
324 } beacon;
325 
beacon_scan_cb(const bt_addr_le_t * addr,int8_t rssi,uint8_t adv_type,struct net_buf_simple * buf)326 static void beacon_scan_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type,
327 			   struct net_buf_simple *buf)
328 {
329 	const uint8_t *net_id;
330 	uint8_t ad_data_type, beacon_type, length;
331 
332 	ASSERT_EQUAL(BT_GAP_ADV_TYPE_ADV_NONCONN_IND, adv_type);
333 
334 	length = net_buf_simple_pull_u8(buf);
335 	ASSERT_EQUAL(buf->len, length);
336 	ad_data_type = net_buf_simple_pull_u8(buf);
337 
338 	if (ad_data_type != BT_DATA_MESH_BEACON) {
339 		return;
340 	}
341 
342 	beacon_type = net_buf_simple_pull_u8(buf);
343 	if (expected_beacon == BEACON_TYPE_SECURE) {
344 		ASSERT_EQUAL(expected_beacon, beacon_type);
345 		beacon.flags = net_buf_simple_pull_u8(buf);
346 		net_id = net_buf_simple_pull_mem(buf, 8);
347 		beacon.iv_index = net_buf_simple_pull_be32(buf);
348 	}
349 #if CONFIG_BT_MESH_V1d1
350 	else if (expected_beacon == BEACON_TYPE_PRIVATE) {
351 		uint8_t private_beacon_data[5];
352 
353 		ASSERT_EQUAL(expected_beacon, beacon_type);
354 		memcpy(beacon.random, buf->data, 13);
355 		bt_addr_le_copy(&beacon.adv_addr, addr);
356 
357 		bt_mesh_beacon_decrypt(&priv_beacon_key, &buf->data[0], &buf->data[13],
358 						 &buf->data[20], private_beacon_data);
359 		beacon.flags = private_beacon_data[0];
360 		beacon.iv_index = sys_get_be32(&private_beacon_data[1]);
361 	}
362 #endif
363 	if (!beacon.process_cb || beacon.process_cb(net_id, beacon.user_ctx)) {
364 		k_sem_give(&observer_sem);
365 	}
366 }
367 
368 /* 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)369 static bool wait_for_beacon(bt_le_scan_cb_t scan_cb, uint16_t wait,
370 			    bool (*process_cb)(const uint8_t *net_id, void *ctx), void *ctx)
371 {
372 	struct bt_le_scan_param scan_param = {
373 			.type       = BT_HCI_LE_SCAN_PASSIVE,
374 			.options    = BT_LE_SCAN_OPT_NONE,
375 			.interval   = BT_MESH_ADV_SCAN_UNIT(1000),
376 			.window     = BT_MESH_ADV_SCAN_UNIT(1000)
377 	};
378 	bool received = false;
379 	int err;
380 
381 	beacon.process_cb = process_cb;
382 	beacon.user_ctx = ctx;
383 
384 	err = bt_le_scan_start(&scan_param, scan_cb);
385 	if (err && err != -EALREADY) {
386 		FAIL("starting scan failed (err %d)", err);
387 	}
388 
389 	/* Listen to beacons ONLY for one beacon interval.
390 	 * Tests start quite often the waiting for the next beacon after
391 	 * transmission or receiving the previous one. If start waiting timer
392 	 * for BEACON_INTERVAL interval then timer expiration and receiving of
393 	 * the beacon happen about the same time. That is possible unstable behavior
394 	 * or failing some tests. To avoid this it is worth to add 1 second to
395 	 * waiting time (BEACON_INTERVAL + 1) to guarantee that beacon comes
396 	 * before timer expiration.
397 	 */
398 	err = k_sem_take(&observer_sem, K_SECONDS(wait));
399 	if (!err) {
400 		received = true;
401 	} else {
402 		LOG_WRN("Didn't receive beacon in time (err: %d)", err);
403 	}
404 
405 	err = bt_le_scan_stop();
406 	if (err && err != -EALREADY) {
407 		FAIL("stopping scan failed (err %d)", err);
408 	}
409 
410 	/* Sleep a little to get to the next beacon interval. Otherwise, calling this function
411 	 * again will catch the old beacon. This happens due to a known bug in legacy advertiser,
412 	 * which transmits advertisements longer than should.
413 	 */
414 	if (received && IS_ENABLED(CONFIG_BT_MESH_ADV_LEGACY)) {
415 		k_sleep(K_SECONDS(1));
416 	}
417 
418 	return received;
419 }
420 
send_beacon(struct net_buf_simple * buf)421 static void send_beacon(struct net_buf_simple *buf)
422 {
423 	struct bt_data ad;
424 	int err;
425 
426 	ad.type = BT_DATA_MESH_BEACON;
427 	ad.data = buf->data;
428 	ad.data_len = buf->len;
429 
430 	err = bt_le_adv_start(BT_LE_ADV_NCONN, &ad, 1, NULL, 0);
431 	if (err) {
432 		FAIL("Advertising failed to start (err %d)\n", err);
433 	}
434 
435 	LOG_INF("Advertising started\n");
436 
437 	k_sleep(K_MSEC(100));
438 
439 	err = bt_le_adv_stop();
440 	if (err) {
441 		FAIL("Unable to stop advertising");
442 	}
443 }
444 
beacon_create(struct net_buf_simple * buf,const uint8_t net_key[16],uint8_t flags,uint32_t iv_index)445 static void beacon_create(struct net_buf_simple *buf, const uint8_t net_key[16], uint8_t flags,
446 			  uint32_t iv_index)
447 {
448 	struct bt_mesh_key beacon_key;
449 	uint8_t net_id[8];
450 	uint8_t auth[8];
451 	int err;
452 
453 	err = bt_mesh_k3(net_key, net_id);
454 	if (err) {
455 		FAIL("Unable to generate Net ID");
456 	}
457 
458 	err = bt_mesh_beacon_key(net_key, &beacon_key);
459 	if (err) {
460 		FAIL("Unable to generate beacon key");
461 	}
462 
463 	err = bt_mesh_beacon_auth(&beacon_key, flags, net_id, iv_index, auth);
464 	if (err) {
465 		FAIL("Unable to generate auth value");
466 	}
467 
468 	err = bt_mesh_key_destroy(&beacon_key);
469 	if (err) {
470 		FAIL("Unable to destroy beacon key");
471 	}
472 
473 	net_buf_simple_reset(buf);
474 	net_buf_simple_add_u8(buf, BEACON_TYPE_SECURE);
475 	net_buf_simple_add_u8(buf, flags);
476 	net_buf_simple_add_mem(buf, net_id, 8);
477 	net_buf_simple_add_be32(buf, iv_index);
478 	net_buf_simple_add_mem(buf, auth, 8);
479 }
480 
481 /* Test reception of invalid beacons. */
corrupted_beacon_test(const uint8_t * offsets,ssize_t field_count,struct net_buf_simple * buf)482 static void corrupted_beacon_test(const uint8_t *offsets,
483 				  ssize_t field_count,
484 				  struct net_buf_simple *buf)
485 {
486 	/* Send corrupted beacons  */
487 	for (int i = 0; i < field_count; i++) {
488 		buf->data[offsets[i]] ^= 0xFF;
489 		send_beacon(buf);
490 		buf->data[offsets[i]] ^= 0xFF;
491 		/* Ensure that interval is not affected. */
492 		ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
493 		ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
494 		ASSERT_EQUAL(0x00, beacon.flags);
495 		ASSERT_EQUAL(0x0000, beacon.iv_index);
496 	}
497 
498 	/* Now the beacon payload is valid and it shall trigger IV Update on the node. It shall also
499 	 * increase the beacon interval.
500 	 */
501 	send_beacon(buf);
502 
503 	/* The beacon interval shall be changed and the node shall skip transmission of the next
504 	 * beacon.
505 	 */
506 	ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
507 	ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
508 	ASSERT_EQUAL(0x02, beacon.flags);
509 	ASSERT_EQUAL(0x0001, beacon.iv_index);
510 }
511 
test_tx_invalid(void)512 static void test_tx_invalid(void)
513 {
514 	NET_BUF_SIMPLE_DEFINE(buf, 22);
515 	/* Offsets of data to be corrupted: Flags, Network ID, IV Index, Authentication value */
516 	uint8_t fields_offsets[4] = {1, 2, 10, 14};
517 	int err;
518 
519 	bt_mesh_test_cfg_set(&tx_cfg, 130);
520 	bt_mesh_crypto_init();
521 	k_sem_init(&observer_sem, 0, 1);
522 
523 	err = bt_enable(NULL);
524 	if (err) {
525 		FAIL("Bluetooth init failed (err %d)", err);
526 		return;
527 	}
528 
529 	LOG_INF("Bluetooth initialized");
530 
531 	/* Let the rx node send the first beacon. */
532 	k_sleep(K_SECONDS(5));
533 
534 	/* Create a valid beacon with IV Update Flag set to 1 and new IV Index. */
535 	beacon_create(&buf, test_net_key, 0x02, 0x0001);
536 
537 	corrupted_beacon_test(fields_offsets, ARRAY_SIZE(fields_offsets), &buf);
538 
539 	PASS();
540 }
541 
542 /* Test reception of invalid beacons. */
test_rx_invalid(void)543 static void test_rx_invalid(void)
544 {
545 	bt_mesh_test_cfg_set(&rx_cfg, 130);
546 	bt_mesh_test_setup();
547 	bt_mesh_iv_update_test(true);
548 
549 	k_sleep(K_SECONDS(10));
550 
551 	for (size_t i = 0; i < 4; i++) {
552 		ASSERT_FALSE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
553 		ASSERT_EQUAL(0, bt_mesh.iv_index);
554 
555 		k_sleep(K_SECONDS((BEACON_INTERVAL + 1) * 2));
556 	}
557 
558 	/* Only the last beacon shall change IV Update state. */
559 	ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
560 	ASSERT_EQUAL(1, bt_mesh.iv_index);
561 
562 	PASS();
563 }
564 
565 /* Test beacons reception with Key Refresh and IV Update on primary subnet. */
test_tx_kr_old_key(void)566 static void test_tx_kr_old_key(void)
567 {
568 	NET_BUF_SIMPLE_DEFINE(buf, 22);
569 	int err;
570 
571 	bt_mesh_test_cfg_set(&tx_cfg, 170);
572 	bt_mesh_crypto_init();
573 	k_sem_init(&observer_sem, 0, 1);
574 
575 	err = bt_enable(NULL);
576 	if (err) {
577 		FAIL("Bluetooth init failed (err %d)", err);
578 		return;
579 	}
580 
581 	LOG_INF("Bluetooth initialized");
582 
583 	/* Let the rx node send the first beacon. */
584 	k_sleep(K_SECONDS(5));
585 
586 	/* The node has added a new Net Key. */
587 
588 	/* Send a beacon with Key Refresh flag set to 1, but secured with the old Net Key. The
589 	 * beacon shall not change Key Refresh phase, but should still be processed. The beacon
590 	 * interval shall be increased.
591 	 */
592 	beacon_create(&buf, test_net_key, 0x01, 0x0000);
593 	send_beacon(&buf);
594 	ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
595 	ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
596 	ASSERT_EQUAL(0x00, beacon.flags);
597 	ASSERT_EQUAL(0x0000, beacon.iv_index);
598 
599 	/* The old Net Key can still initiate IV Index update. */
600 	beacon_create(&buf, test_net_key, 0x02, 0x0001);
601 	send_beacon(&buf);
602 	ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
603 	ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
604 	ASSERT_EQUAL(0x02, beacon.flags);
605 	ASSERT_EQUAL(0x0001, beacon.iv_index);
606 
607 	/* Send beacon with Key Refresh flag set to 1, IV Update flag set to 1, but secured with
608 	 * the new Net Key. The node shall set Key Refresh phase to 2. The beacon interval shall
609 	 * be increased.
610 	 */
611 	beacon_create(&buf, test_net_key_secondary, 0x03, 0x0001);
612 	send_beacon(&buf);
613 	ASSERT_FALSE(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 	/* Send beacon with Key Refresh flag set to 1, IV Update flag set to 0, but secured with
619 	 * the old Net Key. The beacon shall be rejected. The beacon interval shall not be changed.
620 	 */
621 	beacon_create(&buf, test_net_key, 0x01, 0x0001);
622 	send_beacon(&buf);
623 	ASSERT_TRUE(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(0x03, beacon.flags);
626 	ASSERT_EQUAL(0x0001, beacon.iv_index);
627 
628 	/* Try the same with the new Net Key. Now the node shall change Key Refresh phase to 0. The
629 	 * beacon interval shall be increased.
630 	 */
631 	beacon_create(&buf, test_net_key_secondary, 0x02, 0x0001);
632 	send_beacon(&buf);
633 	ASSERT_FALSE(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 	/* Send beacon with IV Update flag set to 0 and secured with the old Net Key. The beacon
639 	 * shall be ignored. The beacon interval shall not be changed.
640 	 */
641 	beacon_create(&buf, test_net_key, 0x00, 0x0001);
642 	send_beacon(&buf);
643 	ASSERT_TRUE(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(0x02, beacon.flags);
646 	ASSERT_EQUAL(0x0001, beacon.iv_index);
647 
648 	/* Do the same, but secure beacon with the new Net Key. Now the node shall change IV Update
649 	 * flag to 0. The beacon interval shall be increased.
650 	 */
651 	beacon_create(&buf, test_net_key_secondary, 0x00, 0x0001);
652 	send_beacon(&buf);
653 	ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
654 	ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
655 	ASSERT_EQUAL(0x00, beacon.flags);
656 	ASSERT_EQUAL(0x0001, beacon.iv_index);
657 
658 	PASS();
659 }
660 
661 /* Test beacons reception with Key Refresh and IV Update on primary subnet. */
test_rx_kr_old_key(void)662 static void test_rx_kr_old_key(void)
663 {
664 	uint8_t phase;
665 	uint8_t status;
666 	int err;
667 
668 	bt_mesh_test_cfg_set(&rx_cfg, 170);
669 	bt_mesh_test_setup();
670 	bt_mesh_iv_update_test(true);
671 
672 	err = bt_mesh_cfg_cli_net_key_update(0, cfg->addr, 0, test_net_key_secondary, &status);
673 	if (err || status) {
674 		FAIL("Net Key update failed (err %d, status %u)", err, status);
675 	}
676 
677 	static struct {
678 		uint8_t phase;
679 		bool ivu;
680 		uint32_t ivi;
681 	} test_vector[] = {
682 		/* Old Net Key, attempt to change Key Refresh phase to 2.  */
683 		{ .phase = BT_MESH_KR_PHASE_1, .ivu = false, .ivi = 0 },
684 		/* Old Net Key, changing IV Update state. */
685 		{ .phase = BT_MESH_KR_PHASE_1, .ivu = true, .ivi = 1 },
686 		/* New Net Key, changing Key Refresh phase. */
687 		{ .phase = BT_MESH_KR_PHASE_2, .ivu = true, .ivi = 1 },
688 		/* Old Net Key, attempt to change IV Update state. */
689 		{ .phase = BT_MESH_KR_PHASE_2, .ivu = true, .ivi = 1 },
690 		/* New Net Key, changing Key Refresh phase to 0. */
691 		{ .phase = BT_MESH_KR_NORMAL, .ivu = true, .ivi = 1 },
692 		/* Old Net Key, attempt to change IV Update state to Idle.*/
693 		{ .phase = BT_MESH_KR_NORMAL, .ivu = true, .ivi = 1 },
694 		/* Net Net Key, changing IV Update state to Idle. */
695 		{ .phase = BT_MESH_KR_NORMAL, .ivu = false, .ivi = 1 },
696 	};
697 
698 	k_sleep(K_SECONDS(8));
699 
700 	for (size_t i = 0; i < ARRAY_SIZE(test_vector); i++) {
701 		status = bt_mesh_subnet_kr_phase_get(0, &phase);
702 		if (status != STATUS_SUCCESS) {
703 			FAIL("Unable to populate Key Refresh phase (status: %d)", status);
704 		}
705 
706 		ASSERT_EQUAL(test_vector[i].phase, phase);
707 		ASSERT_EQUAL(test_vector[i].ivu, atomic_test_bit(bt_mesh.flags,
708 								 BT_MESH_IVU_IN_PROGRESS));
709 		ASSERT_EQUAL(test_vector[i].ivi, bt_mesh.iv_index);
710 
711 		k_sleep(K_SECONDS((BEACON_INTERVAL + 1) * 2));
712 	}
713 
714 	PASS();
715 }
716 
beacon_confirm_by_subnet(const uint8_t * net_id,void * ctx)717 static bool beacon_confirm_by_subnet(const uint8_t *net_id, void *ctx)
718 {
719 	const uint8_t *expected_net_id = ctx;
720 
721 	return !memcmp(expected_net_id, net_id, 8);
722 }
723 
beacon_confirm_all_subnets(const uint8_t * net_id,void * ctx)724 static bool beacon_confirm_all_subnets(const uint8_t *net_id, void *ctx)
725 {
726 	static uint32_t counter;
727 	int err;
728 
729 	for (size_t i = 0; i < ARRAY_SIZE(net_key_pairs); i++) {
730 		uint8_t expected_net_id[8];
731 
732 		err = bt_mesh_k3(net_key_pairs[i].secondary, expected_net_id);
733 		if (err) {
734 			FAIL("Unable to generate Net ID");
735 		}
736 
737 		if (!memcmp(expected_net_id, net_id, 8)) {
738 			LOG_INF("Received beacon for Net Key Idx %d", (i + 1));
739 			counter |= 1 << i;
740 
741 			ASSERT_EQUAL(0x00, beacon.flags);
742 			ASSERT_EQUAL(0x0000, beacon.iv_index);
743 		}
744 	}
745 
746 	if (counter == BIT_MASK(ARRAY_SIZE(net_key_pairs))) {
747 		counter = 0;
748 		return true;
749 	} else {
750 		return false;
751 	}
752 }
753 
754 /* Test beacons rejection with multiple Net Keys. */
test_tx_multiple_netkeys(void)755 static void test_tx_multiple_netkeys(void)
756 {
757 	NET_BUF_SIMPLE_DEFINE(buf, 22);
758 	int err;
759 
760 	bt_mesh_test_cfg_set(&tx_cfg, MULT_NETKEYS_WAIT_TIME);
761 	bt_mesh_crypto_init();
762 	k_sem_init(&observer_sem, 0, 1);
763 
764 	err = bt_enable(NULL);
765 	if (err) {
766 		FAIL("Bluetooth init failed (err %d)", err);
767 		return;
768 	}
769 
770 	LOG_INF("Bluetooth initialized");
771 
772 	/* Let the rx node send the first beacon. */
773 	k_sleep(K_SECONDS(5));
774 
775 	/* The node has added new Net Keys. */
776 
777 	for (size_t i = 0; i < ARRAY_SIZE(net_key_pairs); i++) {
778 		uint8_t net_id_secondary[8];
779 
780 		err = bt_mesh_k3(net_key_pairs[i].secondary, net_id_secondary);
781 		if (err) {
782 			FAIL("Unable to generate Net ID");
783 		}
784 
785 		/* Send beacon with Key Refresh flag set to 1, but secured with the old Net Key.
786 		 * The beacon shall be processed, but shall not change Key Refresh phase.
787 		 */
788 		beacon_create(&buf, net_key_pairs[i].primary, 0x01, 0x0000);
789 		send_beacon(&buf);
790 		ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1,
791 					     beacon_confirm_by_subnet, &buf.data[2]));
792 		ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1,
793 					    beacon_confirm_by_subnet, &buf.data[2]));
794 		ASSERT_EQUAL(0x00, beacon.flags);
795 		ASSERT_EQUAL(0x0000, beacon.iv_index);
796 
797 		/* Do the same, but secure beacon with the new Net Key. The node shall set Key
798 		 * Refresh phase to 2.
799 		 */
800 		beacon_create(&buf, net_key_pairs[i].secondary, 0x01, 0x0000);
801 		send_beacon(&buf);
802 		ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1,
803 					     beacon_confirm_by_subnet, net_id_secondary));
804 		ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1,
805 					    beacon_confirm_by_subnet, net_id_secondary));
806 		ASSERT_EQUAL(0x01, beacon.flags);
807 		ASSERT_EQUAL(0x0000, beacon.iv_index);
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 		/* Do the same with the new Net Key. Now the node shall change Key Refresh phase
822 		 * to 0. The beacon interval shall be increased.
823 		 */
824 		beacon_create(&buf, net_key_pairs[i].secondary, 0x00, 0x0000);
825 		send_beacon(&buf);
826 		ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1,
827 					     beacon_confirm_by_subnet, net_id_secondary));
828 		ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1,
829 					    beacon_confirm_by_subnet, net_id_secondary));
830 		ASSERT_EQUAL(0x00, beacon.flags);
831 		ASSERT_EQUAL(0x0000, beacon.iv_index);
832 	}
833 
834 	/* Create a valid beacon secured with unknown Net Key. The node shall ignore the beacon and
835 	 * continue sending beacons regularly.
836 	 */
837 	uint8_t unknown_net_key[16] = {0xde, 0xad, 0xbe, 0xef};
838 
839 	beacon_create(&buf, unknown_net_key, 0x00, 0x0000);
840 	send_beacon(&buf);
841 	/* Ensure that interval is not affected. */
842 	ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, beacon_confirm_all_subnets,
843 				    NULL));
844 	ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, beacon_confirm_all_subnets,
845 				    NULL));
846 
847 	PASS();
848 }
849 
850 /* Test beacons rejection with multiple Net Keys. */
test_rx_multiple_netkeys(void)851 static void test_rx_multiple_netkeys(void)
852 {
853 	uint8_t phase;
854 	uint8_t status;
855 	int err;
856 
857 	bt_mesh_test_cfg_set(&rx_cfg, MULT_NETKEYS_WAIT_TIME);
858 	bt_mesh_test_setup();
859 	bt_mesh_iv_update_test(true);
860 
861 	/* Add new Net Keys and switch Key Refresh phase to 1 so that beacons can trigger Key
862 	 * Refresh procedure.
863 	 */
864 	for (size_t i = 0; i < ARRAY_SIZE(net_key_pairs); i++) {
865 		err = bt_mesh_cfg_cli_net_key_add(0, cfg->addr, i + 1, net_key_pairs[i].primary,
866 					      &status);
867 		if (err || status) {
868 			FAIL("Net Key add failed (err %d, status %u)", err, status);
869 		}
870 
871 		err = bt_mesh_cfg_cli_net_key_update(0, cfg->addr, i + 1,
872 						     net_key_pairs[i].secondary, &status);
873 		if (err || status) {
874 			FAIL("Net Key update failed (err %d, status %u)", err, status);
875 		}
876 	}
877 
878 	for (size_t i = 0; i < ARRAY_SIZE(net_key_pairs); i++) {
879 		/* Tx device shall change Key Refresh phase to 2. */
880 		k_sleep(K_SECONDS(40));
881 
882 		status = bt_mesh_subnet_kr_phase_get(i + 1, &phase);
883 		if (status != STATUS_SUCCESS) {
884 			FAIL("Unable to populate Key Refresh phase (status: %d)", status);
885 		}
886 
887 		ASSERT_EQUAL(BT_MESH_KR_PHASE_2, phase);
888 
889 		/* Tx device shall change Key Refresh phase to 0. */
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_NORMAL, phase);
898 	}
899 
900 	PASS();
901 
902 }
903 
904 static struct k_work_delayable beacon_timer;
905 
secure_beacon_send(struct k_work * work)906 static void secure_beacon_send(struct k_work *work)
907 {
908 	NET_BUF_SIMPLE_DEFINE(buf, 22);
909 	beacon_create(&buf, test_net_key, 0, 0);
910 	send_beacon(&buf);
911 	/**
912 	 * Sending SNB(secure network beacon) faster to guarantee
913 	 * at least one beacon is received by tx node in 10s period.
914 	 */
915 	k_work_schedule(&beacon_timer, K_SECONDS(2));
916 }
917 
test_tx_secure_beacon_interval(void)918 static void test_tx_secure_beacon_interval(void)
919 {
920 	bt_mesh_test_cfg_set(&tx_cfg, BEACON_INTERVAL_WAIT_TIME);
921 	k_sleep(K_SECONDS(2));
922 	bt_mesh_test_setup();
923 	PASS();
924 }
925 
test_rx_secure_beacon_interval(void)926 static void test_rx_secure_beacon_interval(void)
927 {
928 	NET_BUF_SIMPLE_DEFINE(buf, 22);
929 	int err;
930 	int64_t beacon_recv_time;
931 	int64_t delta;
932 
933 	bt_mesh_test_cfg_set(&rx_cfg, BEACON_INTERVAL_WAIT_TIME);
934 	bt_mesh_crypto_init();
935 	k_sem_init(&observer_sem, 0, 1);
936 	k_work_init_delayable(&beacon_timer, secure_beacon_send);
937 
938 	err = bt_enable(NULL);
939 	if (err) {
940 		FAIL("Bluetooth init failed (err %d)", err);
941 	}
942 
943 	beacon_create(&buf, test_net_key, 0, 0);
944 	k_sleep(K_SECONDS(5));
945 	/*wait provisioned tx node to send the first beacon*/
946 	ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
947 	k_sleep(K_SECONDS(2));
948 
949 	/**
950 	 * Sending 2 SNB 20ms apart by only sending for even values of loop variable.
951 	 * And verify that tx node adapts to 20s SNB interval after sending enough
952 	 * beacons in for loop.
953 	 */
954 	for (size_t i = 1; i < 5; i++) {
955 		if (i % 2) {
956 			send_beacon(&buf);
957 			ASSERT_FALSE(
958 				wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
959 		} else {
960 			ASSERT_TRUE(
961 				wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
962 		}
963 	}
964 
965 	/**
966 	 * Verify that tx node keeps the 20s SNB interval until adapts itself and
967 	 * sends SNB in 10s again.
968 	 */
969 	ASSERT_FALSE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
970 	ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
971 	ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
972 	beacon_recv_time = k_uptime_get();
973 	/* Start sending SNB */
974 	k_work_schedule(&beacon_timer, K_NO_WAIT);
975 
976 	/**
977 	 * Send SNB so that the tx node stays silent and eventually sends
978 	 * an SNB after 600s, which is the maximum time for SNB interval.
979 	 * Sending beacon with 2sec interval.
980 	 */
981 	delta = 0;
982 	for (size_t i = 0; i < 60; i++) {
983 		if (wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL)) {
984 			delta = k_uptime_delta(&beacon_recv_time);
985 			break;
986 		}
987 	}
988 
989 	ASSERT_TRUE(delta >= (600 * MSEC_PER_SEC));
990 	PASS();
991 }
992 
993 #if CONFIG_BT_MESH_V1d1
private_beacon_check(const uint8_t * net_id,void * ctx)994 static bool private_beacon_check(const uint8_t *net_id, void *ctx)
995 {
996 	bool ret;
997 	bool same_random = *(bool *)ctx;
998 
999 	if (memcmp(beacon.adv_addr.a.val, last_beacon_adv_addr.a.val, BT_ADDR_SIZE) == 0) {
1000 		return false;
1001 	}
1002 
1003 	memcpy(&last_beacon_adv_addr.a.val, beacon.adv_addr.a.val, BT_ADDR_SIZE);
1004 
1005 	if (same_random) {
1006 		ret = memcmp(beacon.random, last_random, 13) == 0;
1007 	} else {
1008 		ret = memcmp(beacon.random, last_random, 13) != 0;
1009 	}
1010 
1011 	memcpy(&last_random, beacon.random, 13);
1012 
1013 	return ret;
1014 }
1015 
provision(const struct bt_mesh_test_cfg * dev_cfg)1016 static void provision(const struct bt_mesh_test_cfg *dev_cfg)
1017 {
1018 	int err;
1019 
1020 	err = bt_mesh_provision(net_key, 0, 0, 0, dev_cfg->addr, dev_cfg->dev_key);
1021 	if (err) {
1022 		FAIL("Provisioning failed (err %d)", err);
1023 	}
1024 }
1025 
tx_priv_setup(void)1026 static void tx_priv_setup(void)
1027 {
1028 	uint8_t status;
1029 	struct bt_mesh_priv_beacon val;
1030 	int err;
1031 
1032 	bt_mesh_test_cfg_set(NULL, WAIT_TIME);
1033 	bt_mesh_device_setup(&prov, &prb_comp);
1034 	provision(&tx_cfg);
1035 
1036 	val.enabled = 1;
1037 	val.rand_interval = random_interval;
1038 
1039 	err = bt_mesh_cfg_cli_beacon_set(0, tx_cfg.addr, 0, &status);
1040 	if (err || status != 0) {
1041 		FAIL("Beacon set failed (err %d, status %u)", err, status);
1042 	}
1043 
1044 	err = bt_mesh_priv_beacon_cli_set(0, tx_cfg.addr, &val);
1045 	if (err) {
1046 		FAIL("Failed to enable Private Beacon (err=%d)", err);
1047 	}
1048 }
1049 
test_tx_priv_on_iv_update(void)1050 static void test_tx_priv_on_iv_update(void)
1051 {
1052 	tx_priv_setup();
1053 
1054 	tx_on_iv_update_test();
1055 }
1056 
test_tx_priv_on_key_refresh(void)1057 static void test_tx_priv_on_key_refresh(void)
1058 {
1059 	tx_priv_setup();
1060 
1061 	tx_on_key_refresh_test();
1062 }
1063 
test_tx_priv_adv(void)1064 static void test_tx_priv_adv(void)
1065 {
1066 	uint8_t status;
1067 	struct bt_mesh_priv_beacon val;
1068 	int err;
1069 
1070 	bt_mesh_test_cfg_set(NULL, BEACON_INTERVAL_WAIT_TIME);
1071 	bt_mesh_device_setup(&prov, &prb_comp);
1072 	provision(&tx_cfg);
1073 
1074 	err = bt_mesh_cfg_cli_beacon_set(0, tx_cfg.addr, 0, &status);
1075 	if (err || status != 0) {
1076 		FAIL("Beacon set failed (err %d, status %u)", err, status);
1077 	}
1078 
1079 	val.enabled = 1;
1080 	val.rand_interval = 1;
1081 
1082 	err = bt_mesh_priv_beacon_cli_set(0, tx_cfg.addr, &val);
1083 	if (err) {
1084 		FAIL("Failed to enable Private Beacon (err=%d)", err);
1085 	}
1086 
1087 	k_sleep(K_SECONDS(6 * BEACON_INTERVAL));
1088 
1089 	val.rand_interval = 0;
1090 
1091 	err = bt_mesh_priv_beacon_cli_set(0, tx_cfg.addr, &val);
1092 	if (err) {
1093 		FAIL("Failed to enable Private Beacon (err=%d)", err);
1094 	}
1095 
1096 	k_sleep(K_SECONDS(6 * BEACON_INTERVAL));
1097 
1098 	val.rand_interval = 0;
1099 
1100 	err = bt_mesh_priv_beacon_cli_set(0, tx_cfg.addr, &val);
1101 	if (err) {
1102 		FAIL("Failed to enable Private Beacon (err=%d)", err);
1103 	}
1104 
1105 	k_sleep(K_SECONDS(6 * BEACON_INTERVAL));
1106 
1107 	val.rand_interval = 3;
1108 
1109 	err = bt_mesh_priv_beacon_cli_set(0, tx_cfg.addr, &val);
1110 	if (err) {
1111 		FAIL("Failed to enable Private Beacon (err=%d)", err);
1112 	}
1113 
1114 	PASS();
1115 }
1116 
test_rx_priv_adv(void)1117 static void test_rx_priv_adv(void)
1118 {
1119 	bool same_random;
1120 	int err, i;
1121 
1122 	bt_mesh_test_cfg_set(&rx_cfg, BEACON_INTERVAL_WAIT_TIME);
1123 	k_sem_init(&observer_sem, 0, 1);
1124 
1125 	err = bt_enable(NULL);
1126 	if (err) {
1127 		FAIL("Bluetooth init failed (err %d)", err);
1128 	}
1129 
1130 	expected_beacon = BEACON_TYPE_PRIVATE;
1131 
1132 	same_random = false;
1133 	/* TX device is advertising with Random Interval = 1  for 6 intervals
1134 	 * and with Random Interval = 0 for another 6
1135 	 */
1136 	for (i = 0; i < 12; i++) {
1137 		wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, private_beacon_check,
1138 				&same_random);
1139 	}
1140 
1141 	/* TX device is advertising with Random Interval = 3 */
1142 	for (i = 0; i < 2; i++) {
1143 		same_random = true;
1144 
1145 		for (int j = 0; j < 2; j++) {
1146 			wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, private_beacon_check,
1147 					&same_random);
1148 		}
1149 
1150 		k_sleep(K_SECONDS(BEACON_INTERVAL));
1151 
1152 		/* Beacon random should change here */
1153 		same_random = true;
1154 		wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, private_beacon_check,
1155 				&same_random);
1156 	}
1157 
1158 	PASS();
1159 }
1160 
private_beacon_create(struct net_buf_simple * buf,const uint8_t * net_key,uint8_t flags,uint32_t iv_index)1161 static void private_beacon_create(struct net_buf_simple *buf, const uint8_t *net_key, uint8_t flags,
1162 			  uint32_t iv_index)
1163 {
1164 	uint8_t net_id[8];
1165 	uint8_t auth[8];
1166 	uint8_t data[5];
1167 	uint8_t random_val[13];
1168 	int err;
1169 
1170 	err = bt_mesh_k3(net_key, net_id);
1171 	if (err) {
1172 		FAIL("Unable to generate Net ID");
1173 	}
1174 
1175 	err = bt_mesh_private_beacon_key(net_key, &priv_beacon_key);
1176 	if (err) {
1177 		FAIL("Unable to generate beacon key");
1178 	}
1179 
1180 	bt_rand(random_val, sizeof(random_val));
1181 	bt_mesh_beacon_encrypt(&priv_beacon_key, flags, bt_mesh.iv_index + 1,
1182 			       random_val, data, auth);
1183 
1184 	net_buf_simple_reset(buf);
1185 	net_buf_simple_add_u8(buf, BEACON_TYPE_PRIVATE);
1186 	net_buf_simple_add_mem(buf, random_val, 13);
1187 	net_buf_simple_add_mem(buf, data, 5);
1188 	net_buf_simple_add_mem(buf, auth, 8);
1189 }
1190 
test_tx_priv_invalid(void)1191 static void test_tx_priv_invalid(void)
1192 {
1193 	uint8_t fields_offsets[4] = {1, 14, 15, 19};
1194 
1195 	NET_BUF_SIMPLE_DEFINE(buf, 27);
1196 	int err;
1197 
1198 	bt_mesh_test_cfg_set(&tx_cfg, 130);
1199 	bt_mesh_crypto_init();
1200 	k_sem_init(&observer_sem, 0, 1);
1201 
1202 	err = bt_enable(NULL);
1203 	if (err) {
1204 		FAIL("Bluetooth init failed (err %d)", err);
1205 	}
1206 
1207 	LOG_INF("Bluetooth initialized");
1208 
1209 	/* Let the rx node send the first beacon. */
1210 	k_sleep(K_SECONDS(5));
1211 
1212 	/* Create a valid beacon with IV Update Flag set to 1 and new IV Index. */
1213 	private_beacon_create(&buf, net_key, 0x02, 0x0001);
1214 
1215 	expected_beacon = BEACON_TYPE_PRIVATE;
1216 
1217 	corrupted_beacon_test(fields_offsets, ARRAY_SIZE(fields_offsets), &buf);
1218 
1219 	PASS();
1220 }
1221 
test_rx_priv_invalid(void)1222 static void test_rx_priv_invalid(void)
1223 {
1224 	uint8_t status;
1225 	struct bt_mesh_priv_beacon val;
1226 	int err;
1227 
1228 	bt_mesh_test_cfg_set(NULL, 130);
1229 	bt_mesh_device_setup(&prov, &prb_comp);
1230 	provision(&rx_cfg);
1231 	bt_mesh_iv_update_test(true);
1232 
1233 	val.enabled = 1;
1234 	val.rand_interval = random_interval;
1235 
1236 	err = bt_mesh_cfg_cli_beacon_set(0, rx_cfg.addr, 0, &status);
1237 	if (err || status != 0) {
1238 		FAIL("Beacon set failed (err %d, status %u)", err, status);
1239 	}
1240 
1241 	err = bt_mesh_priv_beacon_cli_set(0, rx_cfg.addr, &val);
1242 	if (err) {
1243 		FAIL("Failed to enable Private Beacon (err=%d)", err);
1244 	}
1245 
1246 	for (size_t i = 0; i < 4; i++) {
1247 		ASSERT_FALSE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
1248 		ASSERT_EQUAL(0, bt_mesh.iv_index);
1249 
1250 		k_sleep(K_SECONDS((BEACON_INTERVAL + 1) * 2));
1251 	}
1252 
1253 	/* Only the last beacon shall change IV Update state. */
1254 	ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
1255 	ASSERT_EQUAL(1, bt_mesh.iv_index);
1256 
1257 	PASS();
1258 }
1259 
toggle_priv_beacon(uint16_t addr,uint8_t enabled)1260 static void toggle_priv_beacon(uint16_t addr, uint8_t enabled)
1261 {
1262 	int err;
1263 	uint8_t status;
1264 	struct bt_mesh_priv_beacon val;
1265 
1266 	err = bt_mesh_cfg_cli_beacon_set(0, addr, !enabled, &status);
1267 	if (err || status != !enabled) {
1268 		FAIL("Beacon set failed (err %d, status %u)", err, status);
1269 	}
1270 
1271 	val.enabled = enabled;
1272 	val.rand_interval = 1;
1273 
1274 	err = bt_mesh_priv_beacon_cli_set(0, addr, &val);
1275 	if (err) {
1276 		FAIL("Failed to enable Private Beacon (err=%d)", err);
1277 	}
1278 }
1279 
test_tx_priv_interleave(void)1280 static void test_tx_priv_interleave(void)
1281 {
1282 	uint8_t phase;
1283 	uint8_t status;
1284 	struct bt_mesh_subnet *sub;
1285 
1286 
1287 	bt_mesh_test_cfg_set(NULL, BEACON_INTERVAL_WAIT_TIME);
1288 	bt_mesh_device_setup(&prov, &prb_comp);
1289 	provision(&tx_cfg);
1290 
1291 	sub = bt_mesh_subnet_get(0);
1292 	ASSERT_TRUE(sub != NULL);
1293 
1294 	ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR));
1295 	ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
1296 	ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_PENDING));
1297 	ASSERT_TRUE(!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST));
1298 	ASSERT_TRUE(bt_mesh.iv_index == 0);
1299 
1300 	status = bt_mesh_subnet_kr_phase_get(0, &phase);
1301 	ASSERT_TRUE(status == STATUS_SUCCESS);
1302 	ASSERT_TRUE(phase == BT_MESH_KR_NORMAL);
1303 
1304 	/* Wait for SNB being advertised and switch beacon type between Beacon Intervals */
1305 	k_sleep(K_SECONDS(BEACON_INTERVAL + 5));
1306 
1307 	toggle_priv_beacon(tx_cfg.addr, 1);
1308 
1309 	bt_mesh_iv_update_test(true);
1310 	ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_TEST));
1311 
1312 	ASSERT_TRUE(bt_mesh_iv_update());
1313 	ASSERT_TRUE(atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS));
1314 	ASSERT_TRUE(bt_mesh.iv_index == 1);
1315 
1316 	/* Switch beacon type between Beacon Intervals */
1317 	k_sleep(K_SECONDS(BEACON_INTERVAL + 5));
1318 
1319 	toggle_priv_beacon(tx_cfg.addr, 0);
1320 
1321 	status = bt_mesh_subnet_update(BT_MESH_KEY_PRIMARY, net_key_new);
1322 	ASSERT_TRUE(status == STATUS_SUCCESS);
1323 	status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
1324 	ASSERT_TRUE(status == STATUS_SUCCESS);
1325 	ASSERT_TRUE(phase == BT_MESH_KR_PHASE_1);
1326 
1327 	phase = BT_MESH_KR_PHASE_2;
1328 	status = bt_mesh_subnet_kr_phase_set(BT_MESH_KEY_PRIMARY, &phase);
1329 	ASSERT_TRUE(status == STATUS_SUCCESS);
1330 	status = bt_mesh_subnet_kr_phase_get(BT_MESH_KEY_PRIMARY, &phase);
1331 	ASSERT_TRUE(status == STATUS_SUCCESS);
1332 	ASSERT_TRUE(phase == BT_MESH_KR_PHASE_2);
1333 
1334 	k_sleep(K_SECONDS(BEACON_INTERVAL + 5));
1335 	toggle_priv_beacon(tx_cfg.addr, 1);
1336 
1337 	PASS();
1338 }
1339 
test_rx_priv_interleave(void)1340 static void test_rx_priv_interleave(void)
1341 {
1342 	int err;
1343 	bool same_random = false;
1344 
1345 	bt_mesh_test_cfg_set(&rx_cfg, BEACON_INTERVAL_WAIT_TIME);
1346 	bt_mesh_crypto_init();
1347 	k_sem_init(&observer_sem, 0, 1);
1348 
1349 	err = bt_mesh_private_beacon_key(net_key, &priv_beacon_key);
1350 	if (err) {
1351 		FAIL("Unable to generate beacon key");
1352 	}
1353 
1354 	err = bt_enable(NULL);
1355 	if (err) {
1356 		FAIL("Bluetooth init failed (err %d)", err);
1357 	}
1358 
1359 	expected_beacon = BEACON_TYPE_SECURE;
1360 	ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
1361 
1362 	expected_beacon = BEACON_TYPE_PRIVATE;
1363 	ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, private_beacon_check,
1364 				    &same_random));
1365 
1366 	/* IVU was started here */
1367 	ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, private_beacon_check,
1368 				    &same_random));
1369 	ASSERT_EQUAL(0x02, beacon.flags);
1370 	ASSERT_EQUAL(0x0001, beacon.iv_index);
1371 
1372 	memset(&beacon, 0, sizeof(beacon));
1373 	expected_beacon = BEACON_TYPE_SECURE;
1374 
1375 	ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
1376 	ASSERT_EQUAL(0x02, beacon.flags);
1377 	ASSERT_EQUAL(0x0001, beacon.iv_index);
1378 
1379 	/* KR was started here */
1380 	ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, NULL, NULL));
1381 	ASSERT_EQUAL(0x03, beacon.flags);
1382 	ASSERT_EQUAL(0x0001, beacon.iv_index);
1383 
1384 	expected_beacon = BEACON_TYPE_PRIVATE;
1385 
1386 	err = bt_mesh_private_beacon_key(net_key_new, &priv_beacon_key);
1387 
1388 	ASSERT_TRUE(wait_for_beacon(beacon_scan_cb, BEACON_INTERVAL + 1, private_beacon_check,
1389 				    &same_random));
1390 	ASSERT_EQUAL(0x03, beacon.flags);
1391 	ASSERT_EQUAL(0x0001, beacon.iv_index);
1392 
1393 	PASS();
1394 }
1395 
1396 #if IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)
1397 
1398 #define BEACON_TYPE_PRIVATE_NET_ID  2
1399 #define BEACON_TYPE_PRIVATE_NODE_ID 3
1400 #define BEACON_TYPE_PRIVATE_LEN     28
1401 #define TEST_NET_IDX1               0
1402 #define TEST_NET_IDX2               1
1403 #define MAX_TIMEOUT ((CONFIG_BT_MESH_NODE_ID_TIMEOUT * 1000) / 6)
1404 
1405 #define PP_NET_ID_WAIT_TIME 610 /*seconds*/
1406 #define PP_NODE_ID_WAIT_TIME 80 /*seconds*/
1407 #define PP_MULT_NET_ID_WAIT_TIME 50 /*seconds*/
1408 
1409 struct pp_netkey_ctx {
1410 	uint8_t *net_key;
1411 	uint8_t net_id[8];
1412 	struct bt_mesh_key id_key;
1413 };
1414 
1415 static struct pp_netkey_ctx pp_net0 = {.net_key = (uint8_t *)test_net_key};
1416 static struct pp_netkey_ctx pp_net1 = {.net_key = (uint8_t *)test_net_key_secondary};
1417 
1418 struct priv_test_ctx {
1419 	uint8_t beacon_type;
1420 	uint16_t *node_id_addr;
1421 };
1422 
pp_netkey_ctx_init(struct pp_netkey_ctx * net)1423 static void pp_netkey_ctx_init(struct pp_netkey_ctx *net)
1424 {
1425 	ASSERT_OK_MSG(bt_mesh_identity_key(net->net_key, &net->id_key),
1426 		      "Failed to generate ID key");
1427 	ASSERT_OK_MSG(bt_mesh_k3(net->net_key, net->net_id), "Failed to generate Net ID");
1428 }
1429 
pp_type_check(uint16_t expected_beacon,uint8_t adv_type,struct net_buf_simple * buf)1430 static bool pp_type_check(uint16_t expected_beacon, uint8_t adv_type, struct net_buf_simple *buf)
1431 {
1432 	if (adv_type != BT_GAP_ADV_TYPE_ADV_IND || buf->len != BEACON_TYPE_PRIVATE_LEN) {
1433 		return false;
1434 	}
1435 
1436 	/* Remove Header */
1437 	(void)net_buf_simple_pull_mem(buf, 11);
1438 
1439 	uint8_t beacon_type = net_buf_simple_pull_u8(buf);
1440 
1441 	if (beacon_type != expected_beacon) {
1442 		return false;
1443 	}
1444 
1445 	return true;
1446 }
1447 
pp_hash_calc(struct pp_netkey_ctx * net,uint64_t random,uint16_t * addr)1448 static uint64_t pp_hash_calc(struct pp_netkey_ctx *net, uint64_t random, uint16_t *addr)
1449 {
1450 	uint64_t hash;
1451 	uint8_t tmp[16] = {0, 0, 0, 0, 0, 3};
1452 
1453 	if (addr) {
1454 		memcpy(&tmp[6], &random, 8);
1455 		sys_put_be16(*addr, &tmp[14]);
1456 
1457 	} else {
1458 		memcpy(&tmp[0], net->net_id, 8);
1459 		memcpy(&tmp[8], &random, 8);
1460 	}
1461 
1462 	bt_mesh_encrypt(&net->id_key, tmp, tmp);
1463 	memcpy(&hash, &tmp[8], 8);
1464 
1465 	return hash;
1466 }
1467 
pp_beacon_check(const uint8_t * net_id,void * ctx)1468 static bool pp_beacon_check(const uint8_t *net_id, void *ctx)
1469 {
1470 	struct priv_test_ctx *test_ctx = (struct priv_test_ctx *)ctx;
1471 
1472 	ASSERT_EQUAL(beacon.pp_hash,
1473 		     pp_hash_calc(&pp_net0, beacon.pp_random, test_ctx->node_id_addr));
1474 
1475 	if (memcmp(beacon.adv_addr.a.val, last_beacon_adv_addr.a.val, BT_ADDR_SIZE) == 0) {
1476 		return false;
1477 	}
1478 
1479 	memcpy(&last_beacon_adv_addr.a.val, beacon.adv_addr.a.val, BT_ADDR_SIZE);
1480 
1481 	return true;
1482 }
1483 
priv_scan_cb(const bt_addr_le_t * addr,int8_t rssi,uint8_t adv_type,struct net_buf_simple * buf)1484 static void priv_scan_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type,
1485 				struct net_buf_simple *buf)
1486 {
1487 	struct priv_test_ctx *ctx = (struct priv_test_ctx *)beacon.user_ctx;
1488 
1489 	if (!pp_type_check(ctx->beacon_type, adv_type, buf)) {
1490 		/* Wrong message type */
1491 		return;
1492 	}
1493 
1494 	bt_addr_le_copy(&beacon.adv_addr, addr);
1495 
1496 	beacon.pp_hash = net_buf_simple_pull_le64(buf);
1497 	beacon.pp_random = net_buf_simple_pull_le64(buf);
1498 
1499 	if (!beacon.process_cb || beacon.process_cb(NULL, beacon.user_ctx)) {
1500 		k_sem_give(&observer_sem);
1501 	}
1502 }
1503 
rx_priv_common_init(uint16_t wait)1504 static void rx_priv_common_init(uint16_t wait)
1505 {
1506 	bt_mesh_test_cfg_set(&rx_cfg, wait);
1507 	bt_mesh_crypto_init();
1508 	pp_netkey_ctx_init(&pp_net0);
1509 	k_sem_init(&observer_sem, 0, 1);
1510 	ASSERT_OK_MSG(bt_enable(NULL), "Bluetooth init failed");
1511 }
1512 
tx_priv_common_init(uint16_t wait)1513 static void tx_priv_common_init(uint16_t wait)
1514 {
1515 	bt_mesh_test_cfg_set(NULL, wait);
1516 	bt_mesh_device_setup(&prov, &prb_comp);
1517 	provision(&tx_cfg);
1518 
1519 	/* Disable GATT proxy */
1520 	ASSERT_OK_MSG(bt_mesh_gatt_proxy_set(BT_MESH_GATT_PROXY_DISABLED),
1521 		      "Failed to disable gatt proxy");
1522 }
1523 
test_tx_priv_net_id(void)1524 static void test_tx_priv_net_id(void)
1525 {
1526 	tx_priv_common_init(PP_NET_ID_WAIT_TIME);
1527 
1528 	/* Enable private GATT proxy */
1529 	ASSERT_OK_MSG(bt_mesh_priv_gatt_proxy_set(BT_MESH_GATT_PROXY_ENABLED),
1530 		      "Failed to set private gatt proxy");
1531 
1532 	PASS();
1533 }
1534 
test_rx_priv_net_id(void)1535 static void test_rx_priv_net_id(void)
1536 {
1537 	struct priv_test_ctx ctx = {
1538 		.beacon_type = BEACON_TYPE_PRIVATE_NET_ID,
1539 		.node_id_addr = NULL,
1540 	};
1541 
1542 	rx_priv_common_init(PP_NET_ID_WAIT_TIME);
1543 
1544 	/* Scan for first net ID */
1545 	ASSERT_TRUE(
1546 		wait_for_beacon(priv_scan_cb, 1, pp_beacon_check, &ctx));
1547 
1548 	uint64_t last_pp_random = beacon.pp_random;
1549 
1550 	/* Wait for 10 minutes, then scan for another net
1551 	 * ID beacon and verify that random field has changed
1552 	 */
1553 	k_sleep(K_SECONDS(600));
1554 	ASSERT_TRUE(
1555 		wait_for_beacon(priv_scan_cb, 1, pp_beacon_check, &ctx));
1556 	ASSERT_FALSE(beacon.pp_random == last_pp_random);
1557 
1558 	PASS();
1559 }
1560 
test_tx_priv_node_id(void)1561 static void test_tx_priv_node_id(void)
1562 {
1563 	enum bt_mesh_feat_state state;
1564 
1565 	tx_priv_common_init(PP_NODE_ID_WAIT_TIME);
1566 
1567 	/* Start first node advertisement */
1568 	ASSERT_OK_MSG(bt_mesh_subnet_priv_node_id_set(TEST_NET_IDX1, BT_MESH_NODE_IDENTITY_RUNNING),
1569 		      "Failed to set private node ID");
1570 
1571 	/* Wait for Node ID advertising to end */
1572 	k_sleep(K_SECONDS(65));
1573 
1574 	/* Check that advertisment has stopped */
1575 	ASSERT_OK_MSG(bt_mesh_subnet_priv_node_id_get(TEST_NET_IDX1, &state),
1576 		      "Failed to get private node ID");
1577 	ASSERT_EQUAL(state, BT_MESH_NODE_IDENTITY_STOPPED);
1578 
1579 	/* Start second node advertisement */
1580 	ASSERT_OK_MSG(bt_mesh_subnet_priv_node_id_set(TEST_NET_IDX1, BT_MESH_NODE_IDENTITY_RUNNING),
1581 		      "Failed to set private node ID");
1582 
1583 	/* Wait to let node ID advertise for a while */
1584 	k_sleep(K_SECONDS(5));
1585 
1586 	PASS();
1587 }
1588 
test_rx_priv_node_id(void)1589 static void test_rx_priv_node_id(void)
1590 {
1591 	struct priv_test_ctx ctx = {
1592 		.beacon_type = BEACON_TYPE_PRIVATE_NODE_ID,
1593 		.node_id_addr = (uint16_t *)&tx_cfg.addr,
1594 	};
1595 
1596 	rx_priv_common_init(PP_NODE_ID_WAIT_TIME);
1597 
1598 	/* Scan for first node ID */
1599 	ASSERT_TRUE(
1600 		wait_for_beacon(priv_scan_cb, 1, pp_beacon_check, &ctx));
1601 
1602 	uint64_t last_pp_random = beacon.pp_random;
1603 
1604 	/* Wait for first node ID advertisment to finish, then scan for
1605 	 * second node ID and verify that random field has changed
1606 	 */
1607 
1608 	k_sleep(K_SECONDS(65));
1609 	ASSERT_TRUE(
1610 		wait_for_beacon(priv_scan_cb, 1, pp_beacon_check, &ctx));
1611 	ASSERT_FALSE(beacon.pp_random == last_pp_random);
1612 
1613 	PASS();
1614 }
1615 
test_tx_priv_multi_net_id(void)1616 static void test_tx_priv_multi_net_id(void)
1617 {
1618 	tx_priv_common_init(PP_MULT_NET_ID_WAIT_TIME);
1619 
1620 	/* TODO: This should be removed as soon as
1621 	 * SNB/proxy service advertising issue has
1622 	 * been resolved.
1623 	 */
1624 	bt_mesh_beacon_set(false);
1625 
1626 	/* Add second network */
1627 	ASSERT_OK_MSG(bt_mesh_subnet_add(TEST_NET_IDX2, test_net_key_secondary),
1628 		      "Failed to add second subnet");
1629 
1630 	/* Enable private GATT proxy */
1631 	ASSERT_OK_MSG(bt_mesh_priv_gatt_proxy_set(BT_MESH_GATT_PROXY_ENABLED),
1632 		      "Failed to set private gatt proxy");
1633 
1634 	PASS();
1635 }
1636 
test_rx_priv_multi_net_id(void)1637 static void test_rx_priv_multi_net_id(void)
1638 {
1639 	rx_priv_common_init(PP_MULT_NET_ID_WAIT_TIME);
1640 	pp_netkey_ctx_init(&pp_net1);
1641 
1642 	struct priv_test_ctx ctx = {
1643 		.beacon_type = BEACON_TYPE_PRIVATE_NET_ID,
1644 		.node_id_addr = NULL,
1645 	};
1646 
1647 	uint16_t itr = 4;
1648 	static uint8_t old_idx = 0xff;
1649 	static struct {
1650 		struct pp_netkey_ctx *net;
1651 		uint16_t recv_cnt;
1652 		int64_t start;
1653 	} net_ctx[2] = {
1654 		{.net = &pp_net0},
1655 
1656 		{.net = &pp_net1},
1657 	};
1658 
1659 	while (itr) {
1660 		/* Scan for net ID from both networks  */
1661 		ASSERT_TRUE(wait_for_beacon(priv_scan_cb, 2, NULL, &ctx));
1662 
1663 		for (size_t i = 0; i < ARRAY_SIZE(net_ctx); i++) {
1664 			if (beacon.pp_hash ==
1665 			    pp_hash_calc(net_ctx[i].net, beacon.pp_random, NULL)) {
1666 				if (old_idx == 0xff) {
1667 					/* Received first Net ID advertisment */
1668 					old_idx = i;
1669 					net_ctx[i].start = k_uptime_get();
1670 					net_ctx[i].recv_cnt++;
1671 				} else if (old_idx != i) {
1672 					/* Received Net ID adv for new subnet */
1673 
1674 					/* Verify last Net ID adv result */
1675 					ASSERT_IN_RANGE(k_uptime_get() - net_ctx[old_idx].start,
1676 							MAX_TIMEOUT - 1000, MAX_TIMEOUT);
1677 					ASSERT_IN_RANGE(net_ctx[old_idx].recv_cnt, 9, 10);
1678 					net_ctx[old_idx].recv_cnt = 0;
1679 					old_idx = i;
1680 
1681 					/* The test ends when all itterations are completed */
1682 					itr--;
1683 
1684 					net_ctx[i].start = k_uptime_get();
1685 					net_ctx[i].recv_cnt++;
1686 				} else {
1687 					/* Received another Net ID adv from same subnet*/
1688 					net_ctx[i].recv_cnt++;
1689 				}
1690 
1691 				break;
1692 			}
1693 		}
1694 	}
1695 
1696 	PASS();
1697 }
1698 #endif
1699 
1700 #endif /* CONFIG_BT_MESH_V1d1 */
1701 
1702 #define TEST_CASE(role, name, description)                     \
1703 	{                                                      \
1704 		.test_id = "beacon_" #role "_" #name,          \
1705 		.test_descr = description,                     \
1706 		.test_pre_init_f = test_##role##_init,         \
1707 		.test_tick_f = bt_mesh_test_timeout,           \
1708 		.test_main_f = test_##role##_##name,           \
1709 		.test_args_f = test_args_parse,                \
1710 	}
1711 
1712 static const struct bst_test_instance test_beacon[] = {
1713 	TEST_CASE(tx, on_iv_update,   "Beacon: send on IV update"),
1714 	TEST_CASE(tx, on_key_refresh,  "Beacon: send on key refresh"),
1715 	TEST_CASE(tx, invalid, "Beacon: send invalid beacon"),
1716 	TEST_CASE(tx, kr_old_key, "Beacon: send old Net Key"),
1717 	TEST_CASE(tx, multiple_netkeys, "Beacon: multiple Net Keys"),
1718 	TEST_CASE(tx, secure_beacon_interval, "Beacon: send secure beacons"),
1719 #if CONFIG_BT_MESH_V1d1
1720 	TEST_CASE(tx, priv_on_iv_update,   "Private Beacon: send on IV update"),
1721 	TEST_CASE(tx, priv_on_key_refresh,   "Private Beacon: send on Key Refresh"),
1722 	TEST_CASE(tx, priv_adv,   "Private Beacon: advertise Private Beacons"),
1723 	TEST_CASE(tx, priv_invalid,   "Private Beacon: advertise invalid beacons"),
1724 	TEST_CASE(tx, priv_interleave,   "Private Beacon: advertise interleaved with SNB"),
1725 #if CONFIG_BT_MESH_GATT_PROXY
1726 	TEST_CASE(tx, priv_net_id,   "Private Proxy: advertise Net ID"),
1727 	TEST_CASE(tx, priv_node_id,   "Private Proxy: advertise Node ID"),
1728 	TEST_CASE(tx, priv_multi_net_id,   "Private Proxy: advertise multiple Net ID"),
1729 #endif
1730 #endif
1731 
1732 	TEST_CASE(rx, on_iv_update,   "Beacon: receive with IV update flag"),
1733 	TEST_CASE(rx, on_key_refresh,  "Beacon: receive with key refresh flag"),
1734 	TEST_CASE(rx, invalid, "Beacon: receive invalid beacon"),
1735 	TEST_CASE(rx, kr_old_key, "Beacon: receive old Net Key"),
1736 	TEST_CASE(rx, multiple_netkeys, "Beacon: multiple Net Keys"),
1737 	TEST_CASE(rx, secure_beacon_interval, "Beacon: receive and send secure beacons"),
1738 #if CONFIG_BT_MESH_V1d1
1739 	TEST_CASE(rx, priv_adv,   "Private Beacon: verify random regeneration"),
1740 	TEST_CASE(rx, priv_invalid,   "Private Beacon: receive invalid beacons"),
1741 	TEST_CASE(rx, priv_interleave,   "Private Beacon: interleaved with SNB"),
1742 #if CONFIG_BT_MESH_GATT_PROXY
1743 	TEST_CASE(rx, priv_net_id,   "Private Proxy: scan for Net ID"),
1744 	TEST_CASE(rx, priv_node_id,   "Private Proxy: scan for Node ID"),
1745 	TEST_CASE(rx, priv_multi_net_id,   "Private Proxy: scan for multiple Net ID"),
1746 #endif
1747 #endif
1748 	BSTEST_END_MARKER
1749 };
1750 
test_beacon_install(struct bst_test_list * tests)1751 struct bst_test_list *test_beacon_install(struct bst_test_list *tests)
1752 {
1753 	tests = bst_add_tests(tests, test_beacon);
1754 	return tests;
1755 }
1756