1 /*
2  * Copyright (c) 2021 Nordic Semiconductor
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "mesh_test.h"
8 #include <zephyr/bluetooth/mesh.h>
9 #include <zephyr/sys/reboot.h>
10 #include "mesh/net.h"
11 #include "mesh/app_keys.h"
12 #include "mesh/keys.h"
13 #include <bs_cmd_line.h>
14 
15 #define LOG_MODULE_NAME test_persistence
16 
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
19 
20 #define WAIT_TIME 60 /*seconds*/
21 
22 static bool provisioner_ready;
23 
24 extern const struct bt_mesh_comp comp;
25 
26 struct test_va_t {
27 	uint16_t addr;
28 	uint8_t uuid[16];
29 };
30 
31 struct test_appkey_t {
32 	uint8_t idx;
33 	uint8_t key[16];
34 };
35 
36 #define TEST_PROV_ADDR 0x0001
37 #define TEST_ADDR 0x0123
38 static uint8_t test_prov_uuid[16] = { 0x6c, 0x69, 0x6e, 0x67, 0x61, 0xaa };
39 static uint8_t test_dev_uuid[16] = { 0x6c, 0x69, 0x6e, 0x67, 0x61, 0x6f };
40 static int test_ividx = 0x123456;
41 static uint8_t test_flags;
42 static uint8_t test_netkey_idx = 0x77;
43 static uint8_t test_netkey[16] = { 0xaa };
44 static uint8_t test_devkey[16] = { 0xdd };
45 static uint8_t test_prov_devkey[16] = { 0x11 };
46 
47 #define TEST_GROUP_0 0xc001
48 #define TEST_GROUP_1 0xfab3
49 
50 #define TEST_VA_0_ADDR 0xb6f0
51 #define TEST_VA_0_UUID (uint8_t[16]) { 0xca, 0xcd, 0x13, 0xbd, 0x54, 0xfe, 0x43, 0xed, \
52 				       0x12, 0x3d, 0xa3, 0xe3, 0xb9, 0x03, 0x70, 0xaa }
53 #define TEST_VA_1_ADDR 0x8700
54 #define TEST_VA_1_UUID (uint8_t[16]) { 0xdf, 0xca, 0xa3, 0x54, 0x23, 0xfa, 0x33, 0xed, \
55 				       0x1a, 0xbe, 0xa0, 0xaa, 0xbd, 0xfa, 0x0f, 0xaf }
56 #define TEST_VA_1_ADDR_COL 0x8700
57 #define TEST_VA_1_UUID_COL (uint8_t[16]) { 0x01, 0xcc, 0x74, 0x51, 0x71, 0x9e, 0x56, 0x71, \
58 					   0x5b, 0x8a, 0x18, 0xaf, 0x13, 0x86, 0x0e, 0x4a }
59 
60 #define TEST_APPKEY_0_IDX 0x12
61 #define TEST_APPKEY_0_KEY { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, \
62 			    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }
63 #define TEST_APPKEY_1_IDX 0x43
64 #define TEST_APPKEY_1_KEY { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, \
65 			    0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f }
66 
67 #define TEST_MOD_PUB_PARAMS {                                                                      \
68 		.addr = TEST_ADDR,                                                                 \
69 		.uuid = NULL,                                                                      \
70 		.app_idx = TEST_APPKEY_0_IDX,                                                      \
71 		.cred_flag = true,                                                                 \
72 		.ttl = 5,                                                                          \
73 		.period = BT_MESH_PUB_PERIOD_10SEC(2),                                             \
74 		.transmit = BT_MESH_TRANSMIT(2, 20),                                               \
75 	}
76 
77 #define TEST_VND_MOD_PUB_PARAMS {                                                                  \
78 		.addr = TEST_VA_0_ADDR,                                                            \
79 		.uuid = TEST_VA_0_UUID,                                                            \
80 		.app_idx = TEST_APPKEY_1_IDX,                                                      \
81 		.cred_flag = true,                                                                 \
82 		.ttl = 5,                                                                          \
83 		.period = BT_MESH_PUB_PERIOD_10SEC(1),                                             \
84 		.transmit = BT_MESH_TRANSMIT(2, 20),                                               \
85 	}
86 
87 #define TEST_MOD_PUB_PARAMS_2 {                                                                    \
88 		.addr = TEST_GROUP_1,                                                              \
89 		.uuid = NULL,                                                                      \
90 		.app_idx = TEST_APPKEY_1_IDX,                                                      \
91 		.cred_flag = false,                                                                \
92 		.ttl = 3,                                                                          \
93 		.period = BT_MESH_PUB_PERIOD_10SEC(3),                                             \
94 		.transmit = BT_MESH_TRANSMIT(3, 20),                                               \
95 	}
96 
97 #define TEST_VND_MOD_PUB_PARAMS_2 {                                                                \
98 		.addr = TEST_VA_1_ADDR,                                                            \
99 		.uuid = TEST_VA_1_UUID,                                                            \
100 		.app_idx = TEST_APPKEY_0_IDX,                                                      \
101 		.cred_flag = false,                                                                \
102 		.ttl = 3,                                                                          \
103 		.period = BT_MESH_PUB_PERIOD_10SEC(2),                                             \
104 		.transmit = BT_MESH_TRANSMIT(3, 20),                                               \
105 	}
106 
107 #define DISABLED_MOD_PUB_PARAMS {                                                                  \
108 		.addr = 0,                                                                         \
109 		.uuid = NULL,                                                                      \
110 		.app_idx = 0,                                                                      \
111 		.cred_flag = false,                                                                \
112 		.ttl = 0,                                                                          \
113 		.period = 0,                                                                       \
114 		.transmit = 0,                                                                     \
115 	}
116 
117 #define TEST_MOD_DATA_NAME "tmdata"
118 static uint8_t test_mod_data[] = { 0xfa, 0xff, 0xf4, 0x43 };
119 #define TEST_VND_MOD_DATA_NAME "vtmdata"
120 static uint8_t vnd_test_mod_data[] = { 0xad, 0xdf, 0x14, 0x53, 0x54, 0x1f };
121 
122 struct access_cfg {
123 	struct bt_mesh_cfg_cli_mod_pub pub_params;
124 
125 	size_t appkeys_count;
126 	uint16_t appkeys[CONFIG_BT_MESH_MODEL_KEY_COUNT];
127 
128 	size_t subs_count;
129 	uint16_t subs[CONFIG_BT_MESH_MODEL_GROUP_COUNT];
130 
131 	size_t mod_data_len;
132 };
133 static const struct access_cfg (*current_access_cfg)[2];
134 enum {
135 	CONFIGURED,
136 	NEW_SUBS,
137 	NOT_CONFIGURED,
138 };
139 static const struct access_cfg access_cfgs[][2] = {
140 	[CONFIGURED] = {
141 		/* SIG model. */
142 		{
143 			.pub_params = TEST_MOD_PUB_PARAMS,
144 			.appkeys_count = 2, .appkeys = { TEST_APPKEY_0_IDX, TEST_APPKEY_1_IDX },
145 			.subs_count = 4, .subs = { TEST_GROUP_0, TEST_VA_0_ADDR, TEST_VA_1_ADDR,
146 						   TEST_VA_1_ADDR /* collision */ },
147 			.mod_data_len = sizeof(test_mod_data),
148 		},
149 
150 		/* Vendor model. */
151 		{
152 			.pub_params = TEST_VND_MOD_PUB_PARAMS,
153 			.appkeys_count = 2, .appkeys = { TEST_APPKEY_0_IDX, TEST_APPKEY_1_IDX },
154 			.subs_count = 4, .subs = { TEST_GROUP_0, TEST_VA_0_ADDR, TEST_VA_1_ADDR,
155 						   TEST_VA_1_ADDR /* collision */ },
156 			.mod_data_len = sizeof(vnd_test_mod_data),
157 		},
158 	},
159 
160 	[NEW_SUBS] = {
161 		/* SIG model. */
162 		{
163 			.pub_params = TEST_MOD_PUB_PARAMS_2,
164 			.appkeys_count = 2, .appkeys = { TEST_APPKEY_0_IDX, TEST_APPKEY_1_IDX },
165 			.subs_count = 1, .subs = { TEST_GROUP_0 },
166 			.mod_data_len = sizeof(test_mod_data),
167 		},
168 
169 		/* Vendor model. */
170 		{
171 			.pub_params = TEST_VND_MOD_PUB_PARAMS_2,
172 			.appkeys_count = 2, .appkeys = { TEST_APPKEY_0_IDX, TEST_APPKEY_1_IDX },
173 			.subs_count = 1, .subs = { TEST_VA_0_ADDR },
174 			.mod_data_len = sizeof(vnd_test_mod_data),
175 		},
176 	},
177 
178 	[NOT_CONFIGURED] = {
179 		/* SIG model. */
180 		{
181 			.pub_params = DISABLED_MOD_PUB_PARAMS,
182 			.appkeys_count = 0, .appkeys = {},
183 			.subs_count = 0, .subs = {},
184 			.mod_data_len = 0,
185 		},
186 
187 		/* Vendor model. */
188 		{
189 			.pub_params = DISABLED_MOD_PUB_PARAMS,
190 			.appkeys_count = 0, .appkeys = {},
191 			.subs_count = 0, .subs = {},
192 			.mod_data_len = 0,
193 		},
194 	},
195 };
196 
197 static const struct stack_cfg {
198 	uint8_t beacon;
199 	uint8_t ttl;
200 	uint8_t gatt_proxy;
201 	uint8_t friend;
202 	uint8_t net_transmit;
203 	struct {
204 		enum bt_mesh_feat_state state;
205 		uint8_t transmit;
206 	} relay;
207 #ifdef CONFIG_BT_MESH_PRIV_BEACONS
208 	uint8_t priv_beacon;
209 	uint8_t priv_beacon_int;
210 	uint8_t priv_beacon_gatt;
211 #endif
212 #ifdef CONFIG_BT_MESH_OD_PRIV_PROXY_SRV
213 	uint8_t priv_proxy_val;
214 #endif
215 } stack_cfgs[] = {
216 	{
217 		.beacon = 1,
218 		.ttl = 12,
219 		.gatt_proxy = 1,
220 		.friend = 1,
221 		.net_transmit = BT_MESH_TRANSMIT(3, 20),
222 		.relay = { .state = BT_MESH_FEATURE_ENABLED, .transmit = BT_MESH_TRANSMIT(2, 20) },
223 #ifdef CONFIG_BT_MESH_PRIV_BEACONS
224 		.priv_beacon = 1,
225 		.priv_beacon_int = 123,
226 		.priv_beacon_gatt = 0,
227 #endif
228 #ifdef CONFIG_BT_MESH_OD_PRIV_PROXY_SRV
229 		.priv_proxy_val = 10,
230 #endif
231 	},
232 	{
233 		.beacon = 0,
234 		.ttl = 0,
235 		.gatt_proxy = 0,
236 		.friend = 0,
237 		.net_transmit = BT_MESH_TRANSMIT(1, 30),
238 		.relay = { .state = BT_MESH_FEATURE_ENABLED, .transmit = BT_MESH_TRANSMIT(1, 10) },
239 #ifdef CONFIG_BT_MESH_PRIV_BEACONS
240 		.priv_beacon = 1,
241 		.priv_beacon_int = 100,
242 		.priv_beacon_gatt = 1,
243 #endif
244 #ifdef CONFIG_BT_MESH_OD_PRIV_PROXY_SRV
245 		.priv_proxy_val = 20,
246 #endif
247 	},
248 };
249 static const struct stack_cfg *current_stack_cfg;
250 
test_args_parse(int argc,char * argv[])251 static void test_args_parse(int argc, char *argv[])
252 {
253 	char *access_cfg_str = NULL;
254 	int32_t stack_cfg = -1;
255 
256 	bs_args_struct_t args_struct[] = {
257 		{
258 			.dest = &access_cfg_str,
259 			.type = 's',
260 			.name = "{configured, new-subs, not-configured}",
261 			.option = "access-cfg",
262 			.descript = ""
263 		},
264 		{
265 			.dest = &stack_cfg,
266 			.type = 'i',
267 			.name = "{0, 1}",
268 			.option = "stack-cfg",
269 			.descript = ""
270 		}
271 	};
272 
273 	bs_args_parse_all_cmd_line(argc, argv, args_struct);
274 
275 	if (access_cfg_str != NULL) {
276 		if (!strcmp(access_cfg_str, "configured")) {
277 			current_access_cfg = &access_cfgs[CONFIGURED];
278 		} else if (!strcmp(access_cfg_str, "new-subs")) {
279 			current_access_cfg = &access_cfgs[NEW_SUBS];
280 		} else if (!strcmp(access_cfg_str, "not-configured")) {
281 			current_access_cfg = &access_cfgs[NOT_CONFIGURED];
282 		}
283 	}
284 
285 	if (stack_cfg >= 0 && stack_cfg < ARRAY_SIZE(stack_cfgs)) {
286 		current_stack_cfg = &stack_cfgs[stack_cfg];
287 	}
288 }
289 
290 static struct k_sem prov_sem;
291 
prov_complete(uint16_t net_idx,uint16_t addr)292 static void prov_complete(uint16_t net_idx, uint16_t addr)
293 {
294 	LOG_INF("Device provisioning is complete, addr: %d", addr);
295 	k_sem_give(&prov_sem);
296 }
297 
device_reset(void)298 static void device_reset(void)
299 {
300 	LOG_INF("Device is reset");
301 	k_sem_give(&prov_sem);
302 }
303 
unprovisioned_beacon(uint8_t uuid[16],bt_mesh_prov_oob_info_t oob_info,uint32_t * uri_hash)304 static void unprovisioned_beacon(uint8_t uuid[16], bt_mesh_prov_oob_info_t oob_info,
305 				 uint32_t *uri_hash)
306 {
307 	static bool once;
308 
309 	/* Subnet may not be ready yet when provisioner receives a beacon. */
310 	if (!provisioner_ready) {
311 		LOG_INF("Provisioner is not ready yet");
312 		return;
313 	}
314 
315 	if (once) {
316 		return;
317 	}
318 
319 	once = !once;
320 
321 	ASSERT_OK(bt_mesh_provision_adv(uuid, test_netkey_idx, TEST_ADDR, 0));
322 }
323 
prov_node_added(uint16_t net_idx,uint8_t uuid[16],uint16_t addr,uint8_t num_elem)324 static void prov_node_added(uint16_t net_idx, uint8_t uuid[16], uint16_t addr, uint8_t num_elem)
325 {
326 	LOG_INF("Device 0x%04x provisioned", addr);
327 	k_sem_give(&prov_sem);
328 }
329 
check_mod_pub_params(const struct bt_mesh_cfg_cli_mod_pub * expected,const struct bt_mesh_cfg_cli_mod_pub * got)330 static void check_mod_pub_params(const struct bt_mesh_cfg_cli_mod_pub *expected,
331 				 const struct bt_mesh_cfg_cli_mod_pub *got)
332 {
333 	ASSERT_EQUAL(expected->addr, got->addr);
334 	ASSERT_EQUAL(expected->app_idx, got->app_idx);
335 	ASSERT_EQUAL(expected->cred_flag, got->cred_flag);
336 	ASSERT_EQUAL(expected->ttl, got->ttl);
337 	ASSERT_EQUAL(expected->period, got->period);
338 	ASSERT_EQUAL(expected->transmit, got->transmit);
339 }
340 
test_model_settings_set(const struct bt_mesh_model * model,const char * name,size_t len_rd,settings_read_cb read_cb,void * cb_arg)341 int test_model_settings_set(const struct bt_mesh_model *model,
342 			    const char *name, size_t len_rd,
343 			    settings_read_cb read_cb, void *cb_arg)
344 {
345 	uint8_t data[sizeof(test_mod_data)];
346 	ssize_t result;
347 
348 	ASSERT_TRUE(name != NULL);
349 	if (strncmp(name, TEST_MOD_DATA_NAME, strlen(TEST_MOD_DATA_NAME))) {
350 		FAIL("Invalid entry name: [%s]", name);
351 	}
352 
353 	settings_name_next(name, &name);
354 	ASSERT_TRUE(name == NULL);
355 
356 	ASSERT_TRUE(current_access_cfg != NULL);
357 	result = read_cb(cb_arg, &data, sizeof(data));
358 	ASSERT_EQUAL((*current_access_cfg)[0].mod_data_len, result);
359 
360 	if (memcmp(data, test_mod_data, (*current_access_cfg)[0].mod_data_len)) {
361 		FAIL("Incorrect data restored");
362 	}
363 
364 	return 0;
365 }
366 
test_model_reset(const struct bt_mesh_model * model)367 void test_model_reset(const struct bt_mesh_model *model)
368 {
369 	ASSERT_OK(bt_mesh_model_data_store(test_model, false, TEST_MOD_DATA_NAME, NULL, 0));
370 }
371 
test_vnd_model_settings_set(const struct bt_mesh_model * model,const char * name,size_t len_rd,settings_read_cb read_cb,void * cb_arg)372 int test_vnd_model_settings_set(const struct bt_mesh_model *model,
373 				const char *name, size_t len_rd,
374 				settings_read_cb read_cb, void *cb_arg)
375 {
376 	uint8_t data[sizeof(vnd_test_mod_data)];
377 	ssize_t result;
378 
379 	ASSERT_TRUE(name != NULL);
380 	if (strncmp(name, TEST_VND_MOD_DATA_NAME, strlen(TEST_VND_MOD_DATA_NAME))) {
381 		FAIL("Invalid entry name: %s", name);
382 	}
383 
384 	settings_name_next(name, &name);
385 	ASSERT_TRUE(name == NULL);
386 
387 	ASSERT_TRUE(current_access_cfg != NULL);
388 	result = read_cb(cb_arg, &data, sizeof(data));
389 	ASSERT_EQUAL((*current_access_cfg)[1].mod_data_len, result);
390 
391 	if (memcmp(data, vnd_test_mod_data, (*current_access_cfg)[1].mod_data_len)) {
392 		FAIL("Incorrect data restored");
393 	}
394 
395 	return 0;
396 }
397 
test_vnd_model_reset(const struct bt_mesh_model * model)398 void test_vnd_model_reset(const struct bt_mesh_model *model)
399 {
400 	ASSERT_OK(bt_mesh_model_data_store(test_vnd_model, true, TEST_VND_MOD_DATA_NAME, NULL, 0));
401 }
402 
device_setup(void)403 static void device_setup(void)
404 {
405 	static struct bt_mesh_prov prov = {
406 		.uuid = test_dev_uuid,
407 		.complete = prov_complete,
408 		.reset = device_reset,
409 	};
410 
411 	k_sem_init(&prov_sem, 0, 1);
412 
413 	bt_mesh_device_setup(&prov, &comp);
414 }
415 
device_setup_and_self_provision(void)416 static int device_setup_and_self_provision(void)
417 {
418 	device_setup();
419 
420 	return bt_mesh_provision(test_netkey, test_netkey_idx, test_flags, test_ividx, TEST_ADDR,
421 				 test_devkey);
422 }
423 
provisioner_setup(void)424 static void provisioner_setup(void)
425 {
426 	static struct bt_mesh_prov prov = {
427 		.uuid = test_prov_uuid,
428 		.unprovisioned_beacon = unprovisioned_beacon,
429 		.node_added = prov_node_added,
430 	};
431 	uint8_t primary_netkey[16] = { 0xad, 0xde, 0xfa, 0x32 };
432 	struct bt_mesh_cdb_subnet *subnet;
433 	uint8_t status;
434 	int err;
435 
436 	k_sem_init(&prov_sem, 0, 1);
437 
438 	bt_mesh_device_setup(&prov, &comp);
439 
440 	ASSERT_OK(bt_mesh_cdb_create(primary_netkey));
441 	ASSERT_OK(bt_mesh_provision(primary_netkey, 0, test_flags, test_ividx, TEST_PROV_ADDR,
442 				    test_prov_devkey));
443 
444 	/* Adding a subnet for test_netkey as it is not primary. */
445 	subnet = bt_mesh_cdb_subnet_alloc(test_netkey_idx);
446 	ASSERT_TRUE(subnet != NULL);
447 	err = bt_mesh_cdb_subnet_key_import(subnet, 0, test_netkey);
448 	if (err) {
449 		FAIL("Unable to import test_netkey (err: %d)", err);
450 	}
451 	bt_mesh_cdb_subnet_store(subnet);
452 
453 	err = bt_mesh_cfg_cli_net_key_add(0, TEST_PROV_ADDR, test_netkey_idx, test_netkey, &status);
454 	if (err || status) {
455 		FAIL("Failed to add test_netkey (err: %d, status: %d)", err, status);
456 	}
457 
458 	provisioner_ready = true;
459 }
460 
test_provisioning_data_save(void)461 static void test_provisioning_data_save(void)
462 {
463 	bt_mesh_test_cfg_set(NULL, WAIT_TIME);
464 
465 	if (device_setup_and_self_provision()) {
466 		FAIL("Mesh setup failed. Settings should not be loaded.");
467 	}
468 
469 	k_sleep(K_SECONDS(CONFIG_BT_MESH_STORE_TIMEOUT));
470 
471 	PASS();
472 }
473 
test_provisioning_data_load(void)474 static void test_provisioning_data_load(void)
475 {
476 	/* In this test stack should boot as provisioned */
477 	bt_mesh_test_cfg_set(NULL, WAIT_TIME);
478 
479 	if (device_setup_and_self_provision() != -EALREADY) {
480 		FAIL("Device should boot up as already provisioned");
481 	}
482 
483 	/* verify: */
484 	/* explicitly verify that the keys resolves for a given addr and net_idx */
485 	struct bt_mesh_msg_ctx ctx;
486 	struct bt_mesh_net_tx tx = { .ctx = &ctx };
487 	const struct bt_mesh_key *dkey;
488 	uint8_t aid;
489 	uint8_t net_key[16];
490 	uint8_t dev_key[16];
491 
492 	tx.ctx->addr = TEST_ADDR;
493 	tx.ctx->net_idx = test_netkey_idx;
494 	tx.ctx->app_idx = BT_MESH_KEY_DEV_REMOTE;	/* to resolve devkey */
495 
496 	int err = bt_mesh_keys_resolve(tx.ctx, &tx.sub, &dkey, &aid);
497 
498 	if (err) {
499 		FAIL("Failed to resolve keys");
500 	}
501 
502 	ASSERT_OK(bt_mesh_key_export(dev_key, dkey));
503 	LOG_HEXDUMP_INF(dev_key, sizeof(dev_key), "Exported device key:");
504 
505 	if (memcmp(dev_key, test_devkey, sizeof(test_devkey))) {
506 		FAIL("Resolved dev_key does not match");
507 	}
508 
509 	ASSERT_OK(bt_mesh_key_export(net_key, &tx.sub->keys[0].net));
510 	LOG_HEXDUMP_INF(net_key, sizeof(net_key), "Exported network key:");
511 
512 	if (memcmp(net_key, test_netkey, sizeof(test_netkey))) {
513 		FAIL("Resolved raw value of the net_key does not match");
514 	}
515 
516 	if (tx.sub->kr_phase != ((test_flags & 1) << 1)) {
517 		FAIL("Incorrect KR phase loaded");
518 	}
519 
520 	/* send TTL Get to verify Tx/Rx path works with loaded config */
521 	uint8_t ttl;
522 
523 	err = bt_mesh_cfg_cli_ttl_get(test_netkey_idx, TEST_ADDR, &ttl);
524 	if (err) {
525 		FAIL("Failed to read ttl value");
526 	}
527 
528 	/* verify IV index state */
529 	if (bt_mesh.iv_index != test_ividx ||
530 	    bt_mesh.ivu_duration != 0 ||
531 	    atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS)) {
532 		FAIL("IV loading verification failed");
533 	}
534 
535 	k_sleep(K_SECONDS(CONFIG_BT_MESH_STORE_TIMEOUT));
536 
537 	PASS();
538 }
539 
node_configure(void)540 static void node_configure(void)
541 {
542 	int err;
543 	uint8_t status;
544 	uint16_t va;
545 	struct bt_mesh_cfg_cli_mod_pub pub_params;
546 
547 	/* Set Network Transmit Count state on the device greater than on provisioner to increase
548 	 * probability of reception responses.
549 	 */
550 	uint8_t net_transmit;
551 
552 	net_transmit = BT_MESH_TRANSMIT(3, 50);
553 	err = bt_mesh_cfg_cli_net_transmit_set(test_netkey_idx, TEST_ADDR, net_transmit, &status);
554 	if (err || status != net_transmit) {
555 		FAIL("Net transmit set failed (err %d, transmit %x)", err, status);
556 	}
557 
558 	struct test_appkey_t test_appkeys[] = {
559 		{ .idx = TEST_APPKEY_0_IDX, .key = TEST_APPKEY_0_KEY },
560 		{ .idx = TEST_APPKEY_1_IDX, .key = TEST_APPKEY_1_KEY },
561 	};
562 
563 	for (size_t i = 0; i < ARRAY_SIZE(test_appkeys); i++) {
564 		err = bt_mesh_cfg_cli_app_key_add(test_netkey_idx, TEST_ADDR, test_netkey_idx,
565 					      test_appkeys[i].idx, test_appkeys[i].key, &status);
566 		if (err || status) {
567 			FAIL("AppKey add failed (err %d, status %u, i %d)", err, status, i);
568 		}
569 	}
570 
571 	/* SIG model. */
572 	for (size_t i = 0; i < ARRAY_SIZE(test_appkeys); i++) {
573 		err = bt_mesh_cfg_cli_mod_app_bind(test_netkey_idx, TEST_ADDR, TEST_ADDR,
574 					       test_appkeys[i].idx, TEST_MOD_ID, &status);
575 		if (err || status) {
576 			FAIL("Mod app bind failed (err %d, status %u, i %d)", err, status, i);
577 		}
578 	}
579 
580 	err = bt_mesh_cfg_cli_mod_sub_add(test_netkey_idx, TEST_ADDR, TEST_ADDR, TEST_GROUP_0,
581 				      TEST_MOD_ID, &status);
582 	if (err || status) {
583 		FAIL("Mod sub add failed (err %d, status %u)", err, status);
584 	}
585 
586 	struct {
587 		const uint8_t *uuid;
588 		uint16_t addr;
589 	} va_subs[] = {
590 		{ .uuid = TEST_VA_0_UUID,     .addr = TEST_VA_0_ADDR, },
591 		{ .uuid = TEST_VA_1_UUID,     .addr = TEST_VA_1_ADDR, },
592 		{ .uuid = TEST_VA_1_UUID_COL, .addr = TEST_VA_1_ADDR, },
593 	};
594 
595 	for (size_t i = 0; i < ARRAY_SIZE(va_subs); i++) {
596 		err = bt_mesh_cfg_cli_mod_sub_va_add(test_netkey_idx, TEST_ADDR, TEST_ADDR,
597 						     va_subs[i].uuid, TEST_MOD_ID, &va, &status);
598 		if (err || status) {
599 			FAIL("Mod sub add failed (err %d, status %u)", err, status);
600 		}
601 		ASSERT_EQUAL(va_subs[i].addr, va);
602 	}
603 
604 	memcpy(&pub_params, &(struct bt_mesh_cfg_cli_mod_pub)TEST_MOD_PUB_PARAMS,
605 	       sizeof(struct bt_mesh_cfg_cli_mod_pub));
606 	err = bt_mesh_cfg_cli_mod_pub_set(test_netkey_idx, TEST_ADDR, TEST_ADDR, TEST_MOD_ID,
607 				      &pub_params, &status);
608 	if (err || status) {
609 		FAIL("Mod pub set failed (err %d, status %u)", err, status);
610 	}
611 
612 	err = bt_mesh_model_data_store(test_model, false, TEST_MOD_DATA_NAME, test_mod_data,
613 				       sizeof(test_mod_data));
614 	if (err) {
615 		FAIL("Mod data store failed (err %d)", err);
616 	}
617 
618 	/* Vendor model. */
619 	for (size_t i = 0; i < ARRAY_SIZE(test_appkeys); i++) {
620 		err = bt_mesh_cfg_cli_mod_app_bind_vnd(test_netkey_idx, TEST_ADDR, TEST_ADDR,
621 						   test_appkeys[i].idx, TEST_VND_MOD_ID,
622 						   TEST_VND_COMPANY_ID, &status);
623 		if (err || status) {
624 			FAIL("Mod app bind failed (err %d, status %u, i %d)", err, status, i);
625 		}
626 	}
627 
628 	err = bt_mesh_cfg_cli_mod_sub_add_vnd(test_netkey_idx, TEST_ADDR, TEST_ADDR,
629 					  TEST_GROUP_0, TEST_VND_MOD_ID,
630 					  TEST_VND_COMPANY_ID, &status);
631 	if (err || status) {
632 		FAIL("Mod sub add failed (err %d, status %u)", err, status);
633 	}
634 
635 	for (size_t i = 0; i < ARRAY_SIZE(va_subs); i++) {
636 		err = bt_mesh_cfg_cli_mod_sub_va_add_vnd(test_netkey_idx, TEST_ADDR, TEST_ADDR,
637 							 va_subs[i].uuid, TEST_VND_MOD_ID,
638 							 TEST_VND_COMPANY_ID, &va, &status);
639 		if (err || status) {
640 			FAIL("Mod sub add failed (err %d, status %u)", err, status);
641 		}
642 		ASSERT_EQUAL(va_subs[i].addr, va);
643 	}
644 
645 	memcpy(&pub_params, &(struct bt_mesh_cfg_cli_mod_pub)TEST_VND_MOD_PUB_PARAMS,
646 	       sizeof(struct bt_mesh_cfg_cli_mod_pub));
647 	err = bt_mesh_cfg_cli_mod_pub_set_vnd(test_netkey_idx, TEST_ADDR, TEST_ADDR,
648 					      TEST_VND_MOD_ID, TEST_VND_COMPANY_ID, &pub_params,
649 					      &status);
650 	if (err || status) {
651 		FAIL("Mod pub set failed (err %d, status %u)", err, status);
652 	}
653 
654 	err = bt_mesh_model_data_store(test_vnd_model, true, TEST_VND_MOD_DATA_NAME,
655 				       vnd_test_mod_data, sizeof(vnd_test_mod_data));
656 	if (err) {
657 		FAIL("Vnd mod data store failed (err %d)", err);
658 		return;
659 	}
660 }
661 
test_access_data_save(void)662 static void test_access_data_save(void)
663 {
664 	bt_mesh_test_cfg_set(NULL, WAIT_TIME);
665 
666 	if (device_setup_and_self_provision()) {
667 		FAIL("Mesh setup failed. Settings should not be loaded.");
668 	}
669 
670 	node_configure();
671 
672 	k_sleep(K_SECONDS(CONFIG_BT_MESH_STORE_TIMEOUT));
673 
674 	PASS();
675 }
676 
node_configuration_check(const struct access_cfg (* cfg)[2])677 static void node_configuration_check(const struct access_cfg (*cfg)[2])
678 {
679 	uint16_t appkeys[CONFIG_BT_MESH_MODEL_KEY_COUNT + 1];
680 	size_t appkeys_count = ARRAY_SIZE(appkeys);
681 	uint16_t subs[CONFIG_BT_MESH_MODEL_GROUP_COUNT + 1];
682 	size_t subs_count = ARRAY_SIZE(subs);
683 	uint8_t status;
684 	int err;
685 
686 	for (size_t m = 0; m < 2; m++) {
687 		bool vnd = m == 1;
688 
689 		if (!vnd) {
690 			err = bt_mesh_cfg_cli_mod_app_get(test_netkey_idx, TEST_ADDR, TEST_ADDR,
691 						      TEST_MOD_ID, &status, appkeys,
692 						      &appkeys_count);
693 		} else {
694 			err = bt_mesh_cfg_cli_mod_app_get_vnd(test_netkey_idx, TEST_ADDR, TEST_ADDR,
695 							  TEST_VND_MOD_ID, TEST_VND_COMPANY_ID,
696 							  &status, appkeys, &appkeys_count);
697 		}
698 		if (err || status) {
699 			FAIL("Mod app get failed (err %d, status %u)", err, status);
700 		}
701 
702 		ASSERT_EQUAL((*cfg)[m].appkeys_count, appkeys_count);
703 		for (size_t i = 0; i < appkeys_count; i++) {
704 			ASSERT_EQUAL((*cfg)[m].appkeys[i], appkeys[i]);
705 		}
706 
707 		if (!vnd) {
708 			err = bt_mesh_cfg_cli_mod_sub_get(test_netkey_idx, TEST_ADDR, TEST_ADDR,
709 						      TEST_MOD_ID, &status, subs, &subs_count);
710 		} else {
711 			err = bt_mesh_cfg_cli_mod_sub_get_vnd(test_netkey_idx, TEST_ADDR, TEST_ADDR,
712 							  TEST_VND_MOD_ID, TEST_VND_COMPANY_ID,
713 							  &status, subs, &subs_count);
714 		}
715 		if (err || status) {
716 			FAIL("Mod sub get failed (err %d, status %u)", err, status);
717 		}
718 
719 		ASSERT_EQUAL((*cfg)[m].subs_count, subs_count);
720 		for (size_t i = 0; i < subs_count; i++) {
721 			ASSERT_EQUAL((*cfg)[m].subs[i], subs[i]);
722 		}
723 
724 		struct bt_mesh_cfg_cli_mod_pub pub_params = {};
725 
726 		if (!vnd) {
727 			err = bt_mesh_cfg_cli_mod_pub_get(test_netkey_idx, TEST_ADDR, TEST_ADDR,
728 						      TEST_MOD_ID, &pub_params, &status);
729 		} else {
730 			err = bt_mesh_cfg_cli_mod_pub_get_vnd(test_netkey_idx, TEST_ADDR, TEST_ADDR,
731 							  TEST_VND_MOD_ID, TEST_VND_COMPANY_ID,
732 							  &pub_params, &status);
733 		}
734 		if (err || status) {
735 			FAIL("Mod pub get failed (err %d, status %u)", err, status);
736 		}
737 
738 		check_mod_pub_params(&(*cfg)[m].pub_params, &pub_params);
739 	}
740 }
741 
test_access_data_load(void)742 static void test_access_data_load(void)
743 {
744 	ASSERT_TRUE(current_access_cfg != NULL);
745 
746 	/* In this test stack should boot as provisioned */
747 	bt_mesh_test_cfg_set(NULL, WAIT_TIME);
748 
749 	if (device_setup_and_self_provision() != -EALREADY) {
750 		FAIL("Device should boot up as already provisioned");
751 	}
752 
753 	node_configuration_check(current_access_cfg);
754 
755 	PASS();
756 }
757 
test_access_sub_overwrite(void)758 static void test_access_sub_overwrite(void)
759 {
760 	struct bt_mesh_cfg_cli_mod_pub pub_params;
761 	uint16_t va;
762 	uint8_t status;
763 	int err;
764 
765 	/* In this test stack should boot as provisioned */
766 	bt_mesh_test_cfg_set(NULL, WAIT_TIME);
767 
768 	if (device_setup_and_self_provision() != -EALREADY) {
769 		FAIL("Device should boot up as already provisioned");
770 	}
771 
772 	err = bt_mesh_cfg_cli_mod_sub_overwrite(test_netkey_idx, TEST_ADDR, TEST_ADDR,
773 					    TEST_GROUP_0, TEST_MOD_ID, &status);
774 	if (err || status) {
775 		FAIL("Mod sub overwrite failed (err %d, status %u)", err, status);
776 	}
777 
778 	memcpy(&pub_params, &(struct bt_mesh_cfg_cli_mod_pub)TEST_MOD_PUB_PARAMS_2,
779 	       sizeof(struct bt_mesh_cfg_cli_mod_pub));
780 	err = bt_mesh_cfg_cli_mod_pub_set(test_netkey_idx, TEST_ADDR, TEST_ADDR, TEST_MOD_ID,
781 				      &pub_params, &status);
782 	if (err || status) {
783 		FAIL("Mod pub set failed (err %d, status %u)", err, status);
784 	}
785 
786 	/* Vendor model. */
787 	err = bt_mesh_cfg_cli_mod_sub_va_overwrite_vnd(test_netkey_idx, TEST_ADDR, TEST_ADDR,
788 						   TEST_VA_0_UUID, TEST_VND_MOD_ID,
789 						   TEST_VND_COMPANY_ID, &va, &status);
790 	if (err || status) {
791 		FAIL("Mod sub va overwrite failed (err %d, status %u)", err, status);
792 	}
793 	ASSERT_EQUAL(TEST_VA_0_ADDR, va);
794 
795 	memcpy(&pub_params, &(struct bt_mesh_cfg_cli_mod_pub)TEST_VND_MOD_PUB_PARAMS_2,
796 	       sizeof(struct bt_mesh_cfg_cli_mod_pub));
797 	err = bt_mesh_cfg_cli_mod_pub_set_vnd(test_netkey_idx, TEST_ADDR, TEST_ADDR,
798 					      TEST_VND_MOD_ID, TEST_VND_COMPANY_ID, &pub_params,
799 					      &status);
800 	if (err || status) {
801 		FAIL("Mod pub set failed (err %d, status %u)", err, status);
802 	}
803 
804 	k_sleep(K_SECONDS(CONFIG_BT_MESH_STORE_TIMEOUT));
805 
806 	PASS();
807 }
808 
test_access_data_remove(void)809 static void test_access_data_remove(void)
810 {
811 	int err;
812 	uint8_t status;
813 	struct bt_mesh_cfg_cli_mod_pub pub_params;
814 
815 	/* In this test stack should boot as provisioned */
816 	bt_mesh_test_cfg_set(NULL, WAIT_TIME);
817 
818 	if (device_setup_and_self_provision() != -EALREADY) {
819 		FAIL("Device should boot up as already provisioned");
820 	}
821 
822 	struct test_appkey_t test_appkeys[] = {
823 		{ .idx = TEST_APPKEY_0_IDX, .key = TEST_APPKEY_0_KEY },
824 		{ .idx = TEST_APPKEY_1_IDX, .key = TEST_APPKEY_1_KEY },
825 	};
826 
827 	/* SIG Model. */
828 	for (size_t i = 0; i < ARRAY_SIZE(test_appkeys); i++) {
829 		err = bt_mesh_cfg_cli_mod_app_unbind(test_netkey_idx, TEST_ADDR, TEST_ADDR,
830 						 test_appkeys[i].idx, TEST_MOD_ID, &status);
831 		if (err || status) {
832 			FAIL("Mod app bind failed (err %d, status %u, i %d)", err, status, i);
833 		}
834 	}
835 
836 	err = bt_mesh_cfg_cli_mod_sub_del_all(test_netkey_idx, TEST_ADDR, TEST_ADDR, TEST_MOD_ID,
837 					  &status);
838 	if (err || status) {
839 		FAIL("Mod sub del all failed (err %d, status %u)", err, status);
840 	}
841 
842 	memcpy(&pub_params, &(struct bt_mesh_cfg_cli_mod_pub)TEST_MOD_PUB_PARAMS,
843 	       sizeof(struct bt_mesh_cfg_cli_mod_pub));
844 	pub_params.addr = BT_MESH_ADDR_UNASSIGNED;
845 	err = bt_mesh_cfg_cli_mod_pub_set(test_netkey_idx, TEST_ADDR, TEST_ADDR, TEST_MOD_ID,
846 				      &pub_params, &status);
847 	if (err || status) {
848 		FAIL("Mod pub set failed (err %d, status %u)", err, status);
849 	}
850 
851 	err = bt_mesh_model_data_store(test_model, false, TEST_MOD_DATA_NAME, NULL, 0);
852 	if (err) {
853 		FAIL("Mod data erase failed (err %d)", err);
854 	}
855 
856 	/* Vendor model. */
857 	for (size_t i = 0; i < ARRAY_SIZE(test_appkeys); i++) {
858 		err = bt_mesh_cfg_cli_mod_app_unbind_vnd(test_netkey_idx, TEST_ADDR, TEST_ADDR,
859 						     test_appkeys[i].idx, TEST_VND_MOD_ID,
860 						     TEST_VND_COMPANY_ID, &status);
861 		if (err || status) {
862 			FAIL("Mod app bind failed (err %d, status %u, i %d)", err, status, i);
863 		}
864 	}
865 
866 	err = bt_mesh_cfg_cli_mod_sub_del_all_vnd(test_netkey_idx, TEST_ADDR, TEST_ADDR,
867 					      TEST_VND_MOD_ID, TEST_VND_COMPANY_ID,
868 					      &status);
869 	if (err || status) {
870 		FAIL("Mod sub del all failed (err %d, status %u)", err, status);
871 	}
872 
873 	memcpy(&pub_params, &(struct bt_mesh_cfg_cli_mod_pub)TEST_VND_MOD_PUB_PARAMS,
874 	       sizeof(struct bt_mesh_cfg_cli_mod_pub));
875 	pub_params.addr = BT_MESH_ADDR_UNASSIGNED;
876 	pub_params.uuid = NULL;
877 	err = bt_mesh_cfg_cli_mod_pub_set_vnd(test_netkey_idx, TEST_ADDR, TEST_ADDR,
878 					      TEST_VND_MOD_ID, TEST_VND_COMPANY_ID, &pub_params,
879 					      &status);
880 	if (err || status) {
881 		FAIL("Mod pub set failed (err %d, status %u)", err, status);
882 	}
883 
884 	err = bt_mesh_model_data_store(test_vnd_model, true, TEST_VND_MOD_DATA_NAME, NULL, 0);
885 	if (err) {
886 		FAIL("Vnd mod data erase failed (err %d)", err);
887 		return;
888 	}
889 
890 	k_sleep(K_SECONDS(CONFIG_BT_MESH_STORE_TIMEOUT));
891 
892 	PASS();
893 }
894 
test_cfg_save(void)895 static void test_cfg_save(void)
896 {
897 	uint8_t transmit;
898 	uint8_t status;
899 	int err;
900 
901 	ASSERT_TRUE(current_stack_cfg != NULL);
902 
903 	bt_mesh_test_cfg_set(NULL, WAIT_TIME);
904 
905 	if (device_setup_and_self_provision()) {
906 		FAIL("Mesh setup failed. Settings should not be loaded.");
907 	}
908 
909 	err = bt_mesh_cfg_cli_beacon_set(test_netkey_idx, TEST_ADDR,
910 				     current_stack_cfg->beacon, &status);
911 	if (err || status != current_stack_cfg->beacon) {
912 		FAIL("Beacon set failed (err %d, status %u)", err, status);
913 	}
914 
915 	err = bt_mesh_cfg_cli_ttl_set(test_netkey_idx, TEST_ADDR,
916 				  current_stack_cfg->ttl, &status);
917 	if (err || status != current_stack_cfg->ttl) {
918 		FAIL("TTL set failed (err %d, status %u)", err, status);
919 	}
920 
921 	err = bt_mesh_cfg_cli_gatt_proxy_set(test_netkey_idx, TEST_ADDR,
922 					 current_stack_cfg->gatt_proxy, &status);
923 	if (err || status != current_stack_cfg->gatt_proxy) {
924 		FAIL("GATT Proxy set failed (err %d, status %u)", err, status);
925 	}
926 
927 	err = bt_mesh_cfg_cli_friend_set(test_netkey_idx, TEST_ADDR,
928 				     current_stack_cfg->friend, &status);
929 	if (err || status != current_stack_cfg->friend) {
930 		FAIL("Friend set failed (err %d, status %u)", err, status);
931 	}
932 
933 	err = bt_mesh_cfg_cli_net_transmit_set(test_netkey_idx, TEST_ADDR,
934 					   current_stack_cfg->net_transmit,
935 					   &transmit);
936 	if (err || transmit != current_stack_cfg->net_transmit) {
937 		FAIL("Net transmit set failed (err %d, transmit %x)", err, transmit);
938 	}
939 
940 	err = bt_mesh_cfg_cli_relay_set(test_netkey_idx, TEST_ADDR,
941 				    current_stack_cfg->relay.state,
942 				    current_stack_cfg->relay.transmit,
943 				    &status, &transmit);
944 	if (err || status  != current_stack_cfg->relay.state ||
945 	    transmit != current_stack_cfg->relay.transmit) {
946 		FAIL("Relay set failed (err %d, status %u, transmit %x)", err, status,
947 		     current_stack_cfg->relay.transmit);
948 	}
949 
950 #ifdef CONFIG_BT_MESH_PRIV_BEACONS
951 	struct bt_mesh_priv_beacon priv_beacon_state = {
952 		.enabled = current_stack_cfg->priv_beacon,
953 		.rand_interval = current_stack_cfg->priv_beacon_int,
954 	};
955 
956 	err = bt_mesh_priv_beacon_cli_set(test_netkey_idx, TEST_ADDR, &priv_beacon_state,
957 					  &priv_beacon_state);
958 	if (err) {
959 		FAIL("Failed to enable Private Beacon (err %d)", err);
960 	}
961 
962 	uint8_t priv_beacon_gatt = current_stack_cfg->priv_beacon_gatt;
963 
964 	err = bt_mesh_priv_beacon_cli_gatt_proxy_set(test_netkey_idx, TEST_ADDR, priv_beacon_gatt,
965 						     &priv_beacon_gatt);
966 	if (err) {
967 		FAIL("Failed to enable Private Beacon GATT proxy (err %d)", err);
968 	}
969 #endif
970 
971 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV) && defined(CONFIG_BT_MESH_OD_PRIV_PROXY_CLI)
972 	uint8_t priv_proxy_val;
973 
974 	err = bt_mesh_od_priv_proxy_cli_set(test_netkey_idx, TEST_ADDR,
975 					    current_stack_cfg->priv_proxy_val, &priv_proxy_val);
976 	if (err || priv_proxy_val != current_stack_cfg->priv_proxy_val) {
977 		FAIL("Failed to set OD Private proxy (err %d, value %d)", err, priv_proxy_val);
978 	}
979 #endif
980 
981 	k_sleep(K_SECONDS(CONFIG_BT_MESH_STORE_TIMEOUT));
982 
983 	PASS();
984 }
985 
test_cfg_load(void)986 static void test_cfg_load(void)
987 {
988 	uint8_t transmit;
989 	uint8_t status;
990 	int err;
991 
992 	ASSERT_TRUE(current_stack_cfg != NULL);
993 
994 	/* In this test stack should boot as provisioned */
995 	bt_mesh_test_cfg_set(NULL, WAIT_TIME);
996 
997 	if (device_setup_and_self_provision() != -EALREADY) {
998 		FAIL("Device should boot up as already provisioned");
999 	}
1000 
1001 	err = bt_mesh_cfg_cli_beacon_get(test_netkey_idx, TEST_ADDR, &status);
1002 	if (err || status != current_stack_cfg->beacon) {
1003 		FAIL("Beacon get failed (err %d, status %u)", err, status);
1004 	}
1005 
1006 	err = bt_mesh_cfg_cli_ttl_get(test_netkey_idx, TEST_ADDR, &status);
1007 	if (err || status != current_stack_cfg->ttl) {
1008 		FAIL("TTL get failed (err %d, status %u)", err, status);
1009 	}
1010 
1011 	err = bt_mesh_cfg_cli_gatt_proxy_get(test_netkey_idx, TEST_ADDR, &status);
1012 	if (err || status != current_stack_cfg->gatt_proxy) {
1013 		FAIL("GATT Proxy get failed (err %d, status %u)", err, status);
1014 	}
1015 
1016 	err = bt_mesh_cfg_cli_friend_get(test_netkey_idx, TEST_ADDR, &status);
1017 	if (err || status != current_stack_cfg->friend) {
1018 		FAIL("Friend get failed (err %d, status %u)", err, status);
1019 	}
1020 
1021 	err = bt_mesh_cfg_cli_net_transmit_get(test_netkey_idx, TEST_ADDR, &status);
1022 	if (err || status != current_stack_cfg->net_transmit) {
1023 		FAIL("Net transmit get failed (err %d, status %u)", err, status);
1024 	}
1025 
1026 	err = bt_mesh_cfg_cli_relay_get(test_netkey_idx, TEST_ADDR, &status, &transmit);
1027 	if (err || status != current_stack_cfg->relay.state ||
1028 	    transmit != current_stack_cfg->relay.transmit) {
1029 		FAIL("Relay get failed (err %d, state %u, transmit %x)", err, status, transmit);
1030 	}
1031 
1032 #ifdef CONFIG_BT_MESH_PRIV_BEACONS
1033 	struct bt_mesh_priv_beacon priv_beacon_state;
1034 	uint8_t priv_beacon_gatt;
1035 
1036 	err = bt_mesh_priv_beacon_cli_get(test_netkey_idx, TEST_ADDR, &priv_beacon_state);
1037 	if (err || priv_beacon_state.enabled != current_stack_cfg->priv_beacon ||
1038 	    priv_beacon_state.rand_interval != current_stack_cfg->priv_beacon_int) {
1039 		FAIL("Private beacon get failed (err %d, enabled %u, interval %x)", err,
1040 		     priv_beacon_state.enabled, priv_beacon_state.rand_interval);
1041 	}
1042 
1043 	err = bt_mesh_priv_beacon_cli_gatt_proxy_get(test_netkey_idx, TEST_ADDR, &priv_beacon_gatt);
1044 	if (err || priv_beacon_gatt != current_stack_cfg->priv_beacon_gatt) {
1045 		FAIL("Private beacon GATT proxy get failed (err %d, enabled %u)", err,
1046 		     priv_beacon_gatt);
1047 	}
1048 #endif
1049 
1050 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV) && defined(CONFIG_BT_MESH_OD_PRIV_PROXY_CLI)
1051 	uint8_t priv_proxy_val;
1052 
1053 	err = bt_mesh_od_priv_proxy_cli_get(test_netkey_idx, TEST_ADDR, &priv_proxy_val);
1054 	if (err || priv_proxy_val != current_stack_cfg->priv_proxy_val) {
1055 		FAIL("Private proxy get failed (err %d, value %u)", err, priv_proxy_val);
1056 	}
1057 #endif
1058 
1059 	PASS();
1060 }
1061 
mesh_settings_load_cb(const char * key,size_t len,settings_read_cb read_cb,void * cb_arg,void * param)1062 static int mesh_settings_load_cb(const char *key, size_t len, settings_read_cb read_cb,
1063 				 void *cb_arg, void *param)
1064 {
1065 	ASSERT_TRUE(len == 0);
1066 
1067 	return 0;
1068 }
1069 
1070 /** @brief Test reprovisioning with persistent storage, device side.
1071  *
1072  * Wait for being provisioned and configured, then wait for the node reset and store settings.
1073  */
test_reprovisioning_device(void)1074 static void test_reprovisioning_device(void)
1075 {
1076 	bt_mesh_test_cfg_set(NULL, WAIT_TIME);
1077 
1078 	device_setup();
1079 
1080 	ASSERT_FALSE(bt_mesh_is_provisioned());
1081 
1082 	ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_ADV));
1083 
1084 	LOG_INF("Waiting for being provisioned...");
1085 	ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(40)));
1086 
1087 	LOG_INF("Waiting for the node reset...");
1088 	ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(40)));
1089 
1090 	k_sleep(K_SECONDS(CONFIG_BT_MESH_STORE_TIMEOUT));
1091 
1092 	/* Check that all mesh settings were removed. */
1093 	settings_load_subtree_direct("bt/mesh", mesh_settings_load_cb, NULL);
1094 
1095 	PASS();
1096 }
1097 
1098 /** @brief Test reprovisioning with persistent storage, provisioner side.
1099  *
1100  * Verify that a device can clear its data from persistent storage after node reset.
1101  */
test_reprovisioning_provisioner(void)1102 static void test_reprovisioning_provisioner(void)
1103 {
1104 	int err;
1105 	bool status;
1106 
1107 	bt_mesh_test_cfg_set(NULL, WAIT_TIME);
1108 
1109 	provisioner_setup();
1110 
1111 	LOG_INF("Provisioning a remote device...");
1112 	ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(40)));
1113 
1114 	/* Verify that the remote device is not configured. */
1115 	node_configuration_check(&access_cfgs[NOT_CONFIGURED]);
1116 
1117 	/* Configure the remote device. */
1118 	node_configure();
1119 
1120 	/* Let the remote device store configuration. */
1121 	k_sleep(K_SECONDS(CONFIG_BT_MESH_STORE_TIMEOUT * 2));
1122 
1123 	err = bt_mesh_cfg_cli_node_reset(test_netkey_idx, TEST_ADDR, &status);
1124 	if (err || !status) {
1125 		FAIL("Reset failed (err %d, status: %d)", err, status);
1126 	}
1127 
1128 	/* Let the remote device store configuration. */
1129 	k_sleep(K_SECONDS(CONFIG_BT_MESH_STORE_TIMEOUT * 2));
1130 
1131 	PASS();
1132 }
1133 
1134 #define TEST_CASE(role, name, description)		   \
1135 	{						   \
1136 		.test_id = "persistence_" #role "_" #name, \
1137 		.test_args_f = test_args_parse,		   \
1138 		.test_descr = description,		   \
1139 		.test_tick_f = bt_mesh_test_timeout,       \
1140 		.test_main_f = test_##role##_##name,	   \
1141 	}
1142 
1143 static const struct bst_test_instance test_persistence[] = {
1144 	TEST_CASE(provisioning, data_save, "Save provisioning data"),
1145 	TEST_CASE(provisioning, data_load, "Load previously saved data and verify"),
1146 	TEST_CASE(access, data_save, "Save access data"),
1147 	TEST_CASE(access, data_load, "Load previously saved access data and verify"),
1148 	TEST_CASE(access, sub_overwrite, "Overwrite Subscription List and store"),
1149 	TEST_CASE(access, data_remove, "Remove stored access data"),
1150 	TEST_CASE(cfg, save, "Save mesh configuration"),
1151 	TEST_CASE(cfg, load, "Load previously stored mesh configuration and verify"),
1152 	TEST_CASE(reprovisioning, device, "Reprovisioning test, device role"),
1153 	TEST_CASE(reprovisioning, provisioner, "Reprovisioning test, provisioner role"),
1154 	BSTEST_END_MARKER
1155 };
1156 
test_persistence_install(struct bst_test_list * tests)1157 struct bst_test_list *test_persistence_install(struct bst_test_list *tests)
1158 {
1159 	return bst_add_tests(tests, test_persistence);
1160 }
1161