1 /*
2 * Copyright (c) 2021 Lingao Meng
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <stdlib.h>
7
8 #include "mesh_test.h"
9 #include "mesh/access.h"
10 #include "mesh/net.h"
11 #include "mesh/crypto.h"
12 #include "argparse.h"
13 #include <bs_pc_backchannel.h>
14 #include <time_machine.h>
15
16 #if defined CONFIG_BT_MESH_USES_MBEDTLS_PSA
17 #include <psa/crypto.h>
18 #else
19 #error "Unknown crypto library has been chosen"
20 #endif
21
22 #include <zephyr/sys/byteorder.h>
23
24 #define LOG_MODULE_NAME mesh_prov
25
26 #include <zephyr/logging/log.h>
27 #include "mesh/rpr.h"
28
29 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
30
31 /*
32 * Provision layer tests:
33 * Tests both the provisioner and device role in various scenarios.
34 */
35
36 #define PROV_MULTI_COUNT 3
37 #define PROV_REPROV_COUNT 3
38 #define WAIT_TIME 120 /*seconds*/
39 #define IS_RPR_PRESENT (CONFIG_BT_MESH_RPR_SRV && CONFIG_BT_MESH_RPR_CLI)
40 #define IMPOSTER_MODEL_ID 0xe000
41
42 enum test_flags {
43 IS_PROVISIONER,
44
45 TEST_FLAGS,
46 };
47
48 static uint8_t static_key1[] = {0x6E, 0x6F, 0x72, 0x64, 0x69, 0x63, 0x5F,
49 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x5F, 0x31};
50 static uint8_t static_key2[] = {0x6E, 0x6F, 0x72, 0x64, 0x69, 0x63, 0x5F};
51 static uint8_t static_key3[] = {0x45, 0x6E, 0x68, 0x61, 0x6E, 0x63, 0x65, 0x64, 0x20, 0x70, 0x72,
52 0x6F, 0x76, 0x69, 0x73, 0x69, 0x6F, 0x6E, 0x69, 0x6E, 0x67, 0x20,
53 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x20, 0x4F, 0x4F, 0x42};
54
55 static uint8_t private_key_be[32];
56 static uint8_t public_key_be[64];
57
58 static struct oob_auth_test_vector_s {
59 const uint8_t *static_val;
60 uint8_t static_val_len;
61 uint8_t output_size;
62 uint16_t output_actions;
63 uint8_t input_size;
64 uint16_t input_actions;
65 } oob_auth_test_vector[] = {
66 {NULL, 0, 0, 0, 0, 0},
67 {static_key1, sizeof(static_key1), 0, 0, 0, 0},
68 {static_key2, sizeof(static_key2), 0, 0, 0, 0},
69 {static_key3, sizeof(static_key3), 0, 0, 0, 0},
70 {NULL, 0, 3, BT_MESH_BLINK, 0, 0},
71 {NULL, 0, 5, BT_MESH_BEEP, 0, 0},
72 {NULL, 0, 6, BT_MESH_VIBRATE, 0, 0},
73 {NULL, 0, 7, BT_MESH_DISPLAY_NUMBER, 0, 0},
74 {NULL, 0, 8, BT_MESH_DISPLAY_STRING, 0, 0},
75 {NULL, 0, 0, 0, 4, BT_MESH_PUSH},
76 {NULL, 0, 0, 0, 5, BT_MESH_TWIST},
77 {NULL, 0, 0, 0, 8, BT_MESH_ENTER_NUMBER},
78 {NULL, 0, 0, 0, 7, BT_MESH_ENTER_STRING},
79 };
80
81 static ATOMIC_DEFINE(test_flags, TEST_FLAGS);
82
83 extern const struct bt_mesh_comp comp;
84 extern const uint8_t test_net_key[16];
85 extern const uint8_t test_app_key[16];
86
87 /* Timeout semaphore */
88 static struct k_sem prov_sem;
89 static K_SEM_DEFINE(link_open_sem, 0, 1);
90 static uint16_t prov_addr = 0x0002;
91 static uint16_t current_dev_addr;
92 static const uint8_t dev_key[16] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
93 static uint8_t dev_uuid[16] = { 0x6c, 0x69, 0x6e, 0x67, 0x61, 0x6f };
94 static uint8_t *uuid_to_provision;
95 static struct k_sem reprov_sem;
96 static uint32_t link_close_timestamp;
97
98 #if IS_RPR_PRESENT
99 static struct k_sem pdu_send_sem;
100 static struct k_sem scan_sem;
101 /* Remote Provisioning models related variables. */
102 static uint8_t *uuid_to_provision_remote;
103 static void rpr_scan_report(struct bt_mesh_rpr_cli *cli, const struct bt_mesh_rpr_node *srv,
104 struct bt_mesh_rpr_unprov *unprov, struct net_buf_simple *adv_data);
105
106 static struct bt_mesh_rpr_cli rpr_cli = {
107 .scan_report = rpr_scan_report,
108 };
109
110 static const struct bt_mesh_comp rpr_cli_comp = {
111 .elem =
112 (const struct bt_mesh_elem[]){
113 BT_MESH_ELEM(1,
114 MODEL_LIST(BT_MESH_MODEL_CFG_SRV,
115 BT_MESH_MODEL_CFG_CLI(&(struct bt_mesh_cfg_cli){}),
116 BT_MESH_MODEL_RPR_CLI(&rpr_cli)),
117 BT_MESH_MODEL_NONE),
118 },
119 .elem_count = 1,
120 };
121
122 static const struct bt_mesh_comp rpr_srv_comp = {
123 .elem =
124 (const struct bt_mesh_elem[]){
125 BT_MESH_ELEM(1,
126 MODEL_LIST(BT_MESH_MODEL_CFG_SRV,
127 BT_MESH_MODEL_RPR_SRV),
128 BT_MESH_MODEL_NONE),
129 },
130 .elem_count = 1,
131 };
132
133 static const struct bt_mesh_comp rpr_cli_srv_comp = {
134 .elem =
135 (const struct bt_mesh_elem[]){
136 BT_MESH_ELEM(1,
137 MODEL_LIST(BT_MESH_MODEL_CFG_SRV,
138 BT_MESH_MODEL_CFG_CLI(&(struct bt_mesh_cfg_cli){}),
139 BT_MESH_MODEL_RPR_CLI(&rpr_cli),
140 BT_MESH_MODEL_RPR_SRV),
141 BT_MESH_MODEL_NONE),
142 },
143 .elem_count = 1,
144 };
145
mock_pdu_send(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)146 static int mock_pdu_send(const struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
147 struct net_buf_simple *buf)
148 {
149 /* Device becomes unresponsive and doesn't communicate with other nodes anymore */
150 k_sleep(K_MSEC(10));
151 bt_mesh_suspend();
152
153 k_sem_give(&pdu_send_sem);
154
155 return 0;
156 }
157
158 static const struct bt_mesh_model_op model_rpr_op1[] = {
159 { RPR_OP_PDU_SEND, 0, mock_pdu_send },
160 BT_MESH_MODEL_OP_END
161 };
162
mock_model_init(const struct bt_mesh_model * mod)163 static int mock_model_init(const struct bt_mesh_model *mod)
164 {
165 mod->keys[0] = BT_MESH_KEY_DEV_LOCAL;
166 mod->rt->flags |= BT_MESH_MOD_DEVKEY_ONLY;
167
168 return 0;
169 }
170
171 const struct bt_mesh_model_cb mock_model_cb = {
172 .init = mock_model_init
173 };
174
175 static const struct bt_mesh_comp rpr_srv_comp_unresponsive = {
176 .elem =
177 (const struct bt_mesh_elem[]){
178 BT_MESH_ELEM(1,
179 MODEL_LIST(BT_MESH_MODEL_CFG_SRV,
180 BT_MESH_MODEL_CB(IMPOSTER_MODEL_ID,
181 model_rpr_op1, NULL, NULL,
182 &mock_model_cb),
183 BT_MESH_MODEL_RPR_SRV,),
184 BT_MESH_MODEL_NONE),
185 },
186 .elem_count = 1,
187 };
188
189 static const uint8_t elem_offset1[2] = {1, 2};
190 static const uint8_t elem_offset2[3] = {4, 5, 6};
191 static const uint8_t additional_data[2] = {100, 200};
192
193 static const struct bt_mesh_comp2_record comp_rec[2] = {
194 {.id = 1,
195 .version.x = 2,
196 .version.y = 3,
197 .version.z = 4,
198 .elem_offset_cnt = sizeof(elem_offset1),
199 .elem_offset = elem_offset1,
200 .data_len = 0},
201 {.id = 10,
202 .version.x = 20,
203 .version.y = 30,
204 .version.z = 40,
205 .elem_offset_cnt = sizeof(elem_offset2),
206 .elem_offset = elem_offset2,
207 .data_len = sizeof(additional_data),
208 .data = additional_data},
209 };
210
211 static const struct bt_mesh_comp2 comp_p2_1 = {.record_cnt = 1, .record = comp_rec};
212 static const struct bt_mesh_comp2 comp_p2_2 = {.record_cnt = 2, .record = comp_rec};
213
214 static const struct bt_mesh_comp rpr_srv_comp_2_elem = {
215 .elem =
216 (const struct bt_mesh_elem[]){
217 BT_MESH_ELEM(1,
218 MODEL_LIST(BT_MESH_MODEL_CFG_SRV,
219 BT_MESH_MODEL_RPR_SRV),
220 BT_MESH_MODEL_NONE),
221 BT_MESH_ELEM(2,
222 MODEL_LIST(BT_MESH_MODEL_CB(TEST_MOD_ID, BT_MESH_MODEL_NO_OPS,
223 NULL, NULL, NULL)),
224 BT_MESH_MODEL_NONE),
225 },
226 .elem_count = 2,
227 };
228 #endif /* IS_RPR_PRESENT */
229
230 /* Delayed work to avoid requesting OOB info before generation of this. */
231 static struct k_work_delayable oob_timer;
232 static void delayed_input(struct k_work *work);
233
234 static uint *oob_channel_id;
235 static bool is_oob_auth;
236
test_device_init(void)237 static void test_device_init(void)
238 {
239 /* Ensure that the UUID is unique: */
240 dev_uuid[6] = '0' + get_device_nbr();
241
242 bt_mesh_test_cfg_set(NULL, WAIT_TIME);
243 k_work_init_delayable(&oob_timer, delayed_input);
244 }
245
test_provisioner_init(void)246 static void test_provisioner_init(void)
247 {
248 atomic_set_bit(test_flags, IS_PROVISIONER);
249 bt_mesh_test_cfg_set(NULL, WAIT_TIME);
250 k_work_init_delayable(&oob_timer, delayed_input);
251 }
252
test_terminate(void)253 static void test_terminate(void)
254 {
255 if (oob_channel_id) {
256 bs_clean_back_channels();
257 }
258 }
259
unprovisioned_beacon(uint8_t uuid[16],bt_mesh_prov_oob_info_t oob_info,uint32_t * uri_hash)260 static void unprovisioned_beacon(uint8_t uuid[16],
261 bt_mesh_prov_oob_info_t oob_info,
262 uint32_t *uri_hash)
263 {
264 if (!atomic_test_bit(test_flags, IS_PROVISIONER)) {
265 return;
266 }
267
268 if (uuid_to_provision && memcmp(uuid, uuid_to_provision, 16)) {
269 return;
270 }
271
272 bt_mesh_provision_adv(uuid, 0, prov_addr, 0);
273 }
274
prov_complete(uint16_t net_idx,uint16_t addr)275 static void prov_complete(uint16_t net_idx, uint16_t addr)
276 {
277 if (!atomic_test_bit(test_flags, IS_PROVISIONER)) {
278 k_sem_give(&prov_sem);
279 }
280 }
281
prov_link_open(bt_mesh_prov_bearer_t bearer)282 static void prov_link_open(bt_mesh_prov_bearer_t bearer)
283 {
284 k_sem_give(&link_open_sem);
285 }
286
prov_link_close(bt_mesh_prov_bearer_t bearer)287 static void prov_link_close(bt_mesh_prov_bearer_t bearer)
288 {
289 link_close_timestamp = k_uptime_get_32();
290 }
291
prov_node_added(uint16_t net_idx,uint8_t uuid[16],uint16_t addr,uint8_t num_elem)292 static void prov_node_added(uint16_t net_idx, uint8_t uuid[16], uint16_t addr,
293 uint8_t num_elem)
294 {
295 LOG_INF("Device 0x%04x provisioned", prov_addr);
296 current_dev_addr = prov_addr++;
297 k_sem_give(&prov_sem);
298 }
299
prov_reprovisioned(uint16_t addr)300 static void prov_reprovisioned(uint16_t addr)
301 {
302 LOG_INF("Device reprovisioned. New address: 0x%04x", addr);
303 k_sem_give(&reprov_sem);
304 }
305
prov_reset(void)306 static void prov_reset(void)
307 {
308 ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_ADV));
309 }
310
311 static bt_mesh_input_action_t gact;
312 static uint8_t gsize;
input(bt_mesh_input_action_t act,uint8_t size)313 static int input(bt_mesh_input_action_t act, uint8_t size)
314 {
315 /* The test system requests the input OOB data earlier than
316 * the output OOB is generated. Need to release context here
317 * to allow output OOB creation. OOB will be inserted later
318 * after the delay.
319 */
320 gact = act;
321 gsize = size;
322
323 k_work_reschedule(&oob_timer, K_SECONDS(1));
324
325 return 0;
326 }
327
delayed_input(struct k_work * work)328 static void delayed_input(struct k_work *work)
329 {
330 char oob_str[16];
331 uint32_t oob_number;
332 int size = bs_bc_is_msg_received(*oob_channel_id);
333
334 if (size <= 0) {
335 FAIL("OOB data is not gotten");
336 }
337
338 switch (gact) {
339 case BT_MESH_PUSH:
340 case BT_MESH_TWIST:
341 case BT_MESH_ENTER_NUMBER:
342 ASSERT_TRUE(size == sizeof(uint32_t));
343 bs_bc_receive_msg(*oob_channel_id, (uint8_t *)&oob_number, size);
344 ASSERT_OK(bt_mesh_input_number(oob_number));
345 break;
346 case BT_MESH_ENTER_STRING:
347 bs_bc_receive_msg(*oob_channel_id, (uint8_t *)oob_str, size);
348 ASSERT_OK(bt_mesh_input_string(oob_str));
349 break;
350 default:
351 FAIL("Unknown input action %u (size %u) requested!", gact, gsize);
352 }
353 }
354
prov_input_complete(void)355 static void prov_input_complete(void)
356 {
357 LOG_INF("Input OOB data completed");
358 }
359
360 static int output_number(bt_mesh_output_action_t action, uint32_t number);
361 static int output_string(const char *str);
362 static void capabilities(const struct bt_mesh_dev_capabilities *cap);
363 static struct bt_mesh_prov prov = {
364 .uuid = dev_uuid,
365 .unprovisioned_beacon = unprovisioned_beacon,
366 .complete = prov_complete,
367 .link_open = prov_link_open,
368 .link_close = prov_link_close,
369 .reprovisioned = prov_reprovisioned,
370 .node_added = prov_node_added,
371 .output_number = output_number,
372 .output_string = output_string,
373 .input = input,
374 .input_complete = prov_input_complete,
375 .capabilities = capabilities,
376 .reset = prov_reset,
377 };
378
output_number(bt_mesh_output_action_t action,uint32_t number)379 static int output_number(bt_mesh_output_action_t action, uint32_t number)
380 {
381 LOG_INF("OOB Number: %u", number);
382
383 bs_bc_send_msg(*oob_channel_id, (uint8_t *)&number, sizeof(uint32_t));
384 return 0;
385 }
386
output_string(const char * str)387 static int output_string(const char *str)
388 {
389 LOG_INF("OOB String: %s", str);
390
391 bs_bc_send_msg(*oob_channel_id, (uint8_t *)str, strlen(str) + 1);
392 return 0;
393 }
394
capabilities(const struct bt_mesh_dev_capabilities * cap)395 static void capabilities(const struct bt_mesh_dev_capabilities *cap)
396 {
397 if (cap->oob_type & BT_MESH_STATIC_OOB_AVAILABLE) {
398 LOG_INF("Static OOB authentication");
399 ASSERT_OK(bt_mesh_auth_method_set_static(prov.static_val, prov.static_val_len));
400 } else if (cap->output_actions) {
401 LOG_INF("Output OOB authentication");
402 ASSERT_OK(bt_mesh_auth_method_set_output(prov.output_actions, prov.output_size));
403 } else if (cap->input_actions) {
404 LOG_INF("Input OOB authentication");
405 ASSERT_OK(bt_mesh_auth_method_set_input(prov.input_actions, prov.input_size));
406 } else if (!is_oob_auth) {
407 bt_mesh_auth_method_set_none();
408 } else {
409 FAIL("No OOB in capability frame");
410 }
411 }
412
oob_auth_set(int test_step)413 static void oob_auth_set(int test_step)
414 {
415 struct oob_auth_test_vector_s dummy = {0};
416
417 ASSERT_TRUE(test_step < ARRAY_SIZE(oob_auth_test_vector));
418
419 if (memcmp(&oob_auth_test_vector[test_step], &dummy,
420 sizeof(struct oob_auth_test_vector_s))) {
421 is_oob_auth = true;
422 } else {
423 is_oob_auth = false;
424 }
425
426 prov.static_val = oob_auth_test_vector[test_step].static_val;
427 prov.static_val_len = oob_auth_test_vector[test_step].static_val_len;
428 prov.output_size = oob_auth_test_vector[test_step].output_size;
429 prov.output_actions = oob_auth_test_vector[test_step].output_actions;
430 prov.input_size = oob_auth_test_vector[test_step].input_size;
431 prov.input_actions = oob_auth_test_vector[test_step].input_actions;
432 }
433
generate_oob_key_pair(void)434 static void generate_oob_key_pair(void)
435 {
436 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
437 psa_key_id_t priv_key_id = PSA_KEY_ID_NULL;
438 psa_status_t status;
439 size_t key_len;
440 uint8_t public_key_repr[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(256)];
441
442 /* Crypto settings for ECDH using the SHA256 hashing algorithm,
443 * the secp256r1 curve
444 */
445 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
446 psa_set_key_lifetime(&key_attributes, PSA_KEY_LIFETIME_VOLATILE);
447 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
448 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
449 psa_set_key_bits(&key_attributes, 256);
450
451 /* Generate a key pair */
452 status = psa_generate_key(&key_attributes, &priv_key_id);
453 ASSERT_TRUE(status == PSA_SUCCESS);
454
455 status = psa_export_public_key(priv_key_id, public_key_repr, sizeof(public_key_repr),
456 &key_len);
457 ASSERT_TRUE(status == PSA_SUCCESS);
458
459 ASSERT_TRUE(key_len == PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(256));
460
461 status = psa_export_key(priv_key_id, private_key_be, sizeof(private_key_be), &key_len);
462 ASSERT_TRUE(status == PSA_SUCCESS);
463
464 ASSERT_TRUE(key_len == sizeof(private_key_be));
465
466 memcpy(public_key_be, public_key_repr + 1, 64);
467 }
468
oob_device(bool use_oob_pk)469 static void oob_device(bool use_oob_pk)
470 {
471 k_sem_init(&prov_sem, 0, 1);
472
473 bt_mesh_device_setup(&prov, &comp);
474
475 if (use_oob_pk) {
476 generate_oob_key_pair();
477 prov.public_key_be = public_key_be;
478 prov.private_key_be = private_key_be;
479 bs_bc_send_msg(*oob_channel_id, public_key_be, 64);
480 LOG_HEXDUMP_INF(public_key_be, 64, "OOB Public Key:");
481 }
482
483 for (int i = 0; i < ARRAY_SIZE(oob_auth_test_vector); i++) {
484 oob_auth_set(i);
485
486 ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_ADV));
487
488 /* Keep a long timeout so the prov multi case has time to finish: */
489 ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(40)));
490
491 /* Delay to complete procedure with Provisioning Complete PDU frame.
492 * Device shall start later provisioner was able to set OOB public key.
493 */
494 k_sleep(K_SECONDS(2));
495
496 bt_mesh_reset();
497 }
498 }
499
oob_provisioner(bool read_oob_pk,bool use_oob_pk)500 static void oob_provisioner(bool read_oob_pk, bool use_oob_pk)
501 {
502 k_sem_init(&prov_sem, 0, 1);
503
504 bt_mesh_device_setup(&prov, &comp);
505
506 if (read_oob_pk) {
507 /* Delay to complete procedure public key generation on provisioning device. */
508 k_sleep(K_SECONDS(1));
509
510 int size = bs_bc_is_msg_received(*oob_channel_id);
511
512 if (size <= 0) {
513 FAIL("OOB public key is not gotten");
514 }
515
516 bs_bc_receive_msg(*oob_channel_id, public_key_be, 64);
517 LOG_HEXDUMP_INF(public_key_be, 64, "OOB Public Key:");
518 }
519
520 ASSERT_OK(bt_mesh_cdb_create(test_net_key));
521
522 ASSERT_OK(bt_mesh_provision(test_net_key, 0, 0, 0, 0x0001, dev_key));
523
524 for (int i = 0; i < ARRAY_SIZE(oob_auth_test_vector); i++) {
525 oob_auth_set(i);
526
527 if (use_oob_pk) {
528 ASSERT_OK(bt_mesh_prov_remote_pub_key_set(public_key_be));
529 }
530
531 ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(40)));
532
533 bt_mesh_cdb_node_del(bt_mesh_cdb_node_get(prov_addr - 1), true);
534
535 /* Delay to complete procedure with cleaning of the public key.
536 * This is important that the provisioner started the new cycle loop
537 * earlier than device to get OOB public key before capabilities frame.
538 */
539 k_sleep(K_SECONDS(1));
540 }
541
542 bt_mesh_reset();
543 }
544
545 /** Configures the health server on a node at current_dev_addr address and sends node reset.
546 */
node_configure_and_reset(void)547 static void node_configure_and_reset(void)
548 {
549 uint8_t status;
550 size_t subs_count = 1;
551 uint16_t sub;
552 struct bt_mesh_cfg_cli_mod_pub healthpub = { 0 };
553 struct bt_mesh_cdb_node *node;
554
555 /* Check that publication and subscription are reset after last iteration */
556 ASSERT_OK(bt_mesh_cfg_cli_mod_sub_get(0, current_dev_addr, current_dev_addr,
557 BT_MESH_MODEL_ID_HEALTH_SRV, &status, &sub,
558 &subs_count));
559 ASSERT_EQUAL(0, status);
560 ASSERT_TRUE(subs_count == 0);
561
562 ASSERT_OK(bt_mesh_cfg_cli_mod_pub_get(0, current_dev_addr, current_dev_addr,
563 BT_MESH_MODEL_ID_HEALTH_SRV, &healthpub,
564 &status));
565 ASSERT_EQUAL(0, status);
566 ASSERT_TRUE_MSG(healthpub.addr == BT_MESH_ADDR_UNASSIGNED, "Pub not cleared\n");
567
568 /* Set pub and sub to check that they are reset */
569 healthpub.addr = 0xc001;
570 healthpub.app_idx = 0;
571 healthpub.cred_flag = false;
572 healthpub.ttl = 10;
573 healthpub.period = BT_MESH_PUB_PERIOD_10SEC(1);
574 healthpub.transmit = BT_MESH_TRANSMIT(3, 100);
575
576 ASSERT_OK(bt_mesh_cfg_cli_app_key_add(0, current_dev_addr, 0, 0, test_app_key,
577 &status));
578 ASSERT_EQUAL(0, status);
579
580 k_sleep(K_SECONDS(2));
581
582 ASSERT_OK(bt_mesh_cfg_cli_mod_app_bind(0, current_dev_addr, current_dev_addr, 0x0,
583 BT_MESH_MODEL_ID_HEALTH_SRV, &status));
584 ASSERT_EQUAL(0, status);
585
586 k_sleep(K_SECONDS(2));
587
588 ASSERT_OK(bt_mesh_cfg_cli_mod_sub_add(0, current_dev_addr, current_dev_addr, 0xc000,
589 BT_MESH_MODEL_ID_HEALTH_SRV, &status));
590 ASSERT_EQUAL(0, status);
591
592 k_sleep(K_SECONDS(2));
593
594 ASSERT_OK(bt_mesh_cfg_cli_mod_pub_set(0, current_dev_addr, current_dev_addr,
595 BT_MESH_MODEL_ID_HEALTH_SRV, &healthpub,
596 &status));
597 ASSERT_EQUAL(0, status);
598
599 k_sleep(K_SECONDS(2));
600
601 ASSERT_OK(bt_mesh_cfg_cli_node_reset(0, current_dev_addr, (bool *)&status));
602
603 node = bt_mesh_cdb_node_get(current_dev_addr);
604 bt_mesh_cdb_node_del(node, true);
605 }
606
607 /** @brief Verify that this device pb-adv provision.
608 */
test_device_pb_adv_no_oob(void)609 static void test_device_pb_adv_no_oob(void)
610 {
611 k_sem_init(&prov_sem, 0, 1);
612
613 bt_mesh_device_setup(&prov, &comp);
614
615 ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_ADV));
616
617 LOG_INF("Mesh initialized\n");
618
619 /* Keep a long timeout so the prov multi case has time to finish: */
620 ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(40)));
621
622 PASS();
623 }
624
625 /** @brief Verify that this device can be reprovisioned after resets
626 */
test_device_pb_adv_reprovision(void)627 static void test_device_pb_adv_reprovision(void)
628 {
629 k_sem_init(&prov_sem, 0, 1);
630
631 bt_mesh_device_setup(&prov, &comp);
632
633 ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_ADV));
634
635 LOG_INF("Mesh initialized\n");
636
637 for (int i = 0; i < PROV_REPROV_COUNT; i++) {
638 /* Keep a long timeout so the prov multi case has time to finish: */
639 LOG_INF("Dev prov loop #%d, waiting for prov ...\n", i);
640 ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(20)));
641 }
642
643 PASS();
644 }
645
646 /** @brief Verify that this provisioner pb-adv provision.
647 */
test_provisioner_pb_adv_no_oob(void)648 static void test_provisioner_pb_adv_no_oob(void)
649 {
650 k_sem_init(&prov_sem, 0, 1);
651
652 bt_mesh_device_setup(&prov, &comp);
653
654 ASSERT_OK(bt_mesh_cdb_create(test_net_key));
655
656 ASSERT_OK(bt_mesh_provision(test_net_key, 0, 0, 0, 0x0001, dev_key));
657
658 ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(5)));
659
660 PASS();
661 }
662
test_device_pb_adv_oob_auth(void)663 static void test_device_pb_adv_oob_auth(void)
664 {
665 oob_device(false);
666
667 PASS();
668 }
669
test_provisioner_pb_adv_oob_auth(void)670 static void test_provisioner_pb_adv_oob_auth(void)
671 {
672 oob_provisioner(false, false);
673
674 PASS();
675 }
676
test_back_channel_pre_init(void)677 static void test_back_channel_pre_init(void)
678 {
679 oob_channel_id = bs_open_back_channel(get_device_nbr(),
680 (uint[]){(get_device_nbr() + 1) % 2}, (uint[]){0}, 1);
681 if (!oob_channel_id) {
682 FAIL("Can't open OOB interface\n");
683 }
684 }
685
test_device_pb_adv_oob_public_key(void)686 static void test_device_pb_adv_oob_public_key(void)
687 {
688 oob_device(true);
689
690 PASS();
691 }
692
test_provisioner_pb_adv_oob_public_key(void)693 static void test_provisioner_pb_adv_oob_public_key(void)
694 {
695 oob_provisioner(true, true);
696
697 PASS();
698 }
699
test_provisioner_pb_adv_oob_auth_no_oob_public_key(void)700 static void test_provisioner_pb_adv_oob_auth_no_oob_public_key(void)
701 {
702 oob_provisioner(true, false);
703
704 PASS();
705 }
706
707 /** @brief Verify that the provisioner can provision multiple devices in a row
708 */
test_provisioner_pb_adv_multi(void)709 static void test_provisioner_pb_adv_multi(void)
710 {
711 k_sem_init(&prov_sem, 0, 1);
712
713 bt_mesh_device_setup(&prov, &comp);
714
715 ASSERT_OK(bt_mesh_cdb_create(test_net_key));
716
717 ASSERT_OK(bt_mesh_provision(test_net_key, 0, 0, 0, 0x0001, dev_key));
718
719 for (int i = 0; i < PROV_MULTI_COUNT; i++) {
720 ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(20)));
721 }
722
723 PASS();
724 }
725
726 /** @brief Verify that when the IV Update flag is set to zero at the
727 * time of provisioning, internal IV update counter is also zero.
728 */
test_provisioner_iv_update_flag_zero(void)729 static void test_provisioner_iv_update_flag_zero(void)
730 {
731 uint8_t flags = 0x00;
732
733 bt_mesh_device_setup(&prov, &comp);
734
735 ASSERT_OK(bt_mesh_provision(test_net_key, 0, flags, 0, 0x0001, dev_key));
736
737 if (bt_mesh.ivu_duration != 0) {
738 FAIL("IV Update duration counter is not 0 when IV Update flag is zero");
739 }
740
741 PASS();
742 }
743
744 /** @brief Verify that when the IV Update flag is set to one at the
745 * time of provisioning, internal IV update counter is set to 96 hours.
746 */
test_provisioner_iv_update_flag_one(void)747 static void test_provisioner_iv_update_flag_one(void)
748 {
749 uint8_t flags = 0x02; /* IV Update flag bit set to 1 */
750
751 bt_mesh_device_setup(&prov, &comp);
752
753 ASSERT_OK(bt_mesh_provision(test_net_key, 0, flags, 0, 0x0001, dev_key));
754
755 if (bt_mesh.ivu_duration != 96) {
756 FAIL("IV Update duration counter is not 96 when IV Update flag is one");
757 }
758
759 bt_mesh_reset();
760
761 if (bt_mesh.ivu_duration != 0) {
762 FAIL("IV Update duration counter is not reset to 0");
763 }
764
765 PASS();
766 }
767
768 /** @brief Verify that the provisioner can provision a device multiple times after resets
769 */
test_provisioner_pb_adv_reprovision(void)770 static void test_provisioner_pb_adv_reprovision(void)
771 {
772 k_sem_init(&prov_sem, 0, 1);
773
774 bt_mesh_device_setup(&prov, &comp);
775
776 ASSERT_OK(bt_mesh_cdb_create(test_net_key));
777
778 ASSERT_OK(bt_mesh_provision(test_net_key, 0, 0, 0, 0x0001, dev_key));
779
780 for (int i = 0; i < PROV_REPROV_COUNT; i++) {
781 LOG_INF("Provisioner prov loop #%d, waiting for prov ...\n", i);
782 ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(20)));
783
784 node_configure_and_reset();
785 }
786
787 PASS();
788 }
789
790 /** @brief Device starts unprovisioned. Stops being responsive to Mesh message after initial setup.
791 * Later becomes responsive but becomes unresponsive again after provisioning link opens.
792 * Then becomes responsive again allowing successful provisioning. Never stops advertising
793 * Unprovisioned Device beacons.
794 */
test_device_unresponsive(void)795 static void test_device_unresponsive(void)
796 {
797 bt_mesh_device_setup(&prov, &comp);
798
799 k_sem_init(&prov_sem, 0, 1);
800
801 ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_ADV));
802
803 /* stop responding for 30s to timeout PB-ADV link establishment. */
804 bt_mesh_scan_disable();
805 k_sleep(K_SECONDS(30));
806 bt_mesh_scan_enable();
807
808 k_sem_take(&link_open_sem, K_SECONDS(20));
809 /* stop responding for 60s to timeout protocol */
810 bt_mesh_scan_disable();
811 k_sleep(K_SECONDS(60));
812 bt_mesh_scan_enable();
813
814 k_sem_take(&prov_sem, K_SECONDS(20));
815 PASS();
816 }
817
818 #if IS_RPR_PRESENT
provision_adv(uint8_t dev_idx,uint16_t * addr)819 static int provision_adv(uint8_t dev_idx, uint16_t *addr)
820 {
821 static uint8_t uuid[16];
822 int err;
823
824 memcpy(uuid, dev_uuid, 16);
825 uuid[6] = '0' + dev_idx;
826 uuid_to_provision = uuid;
827
828 LOG_INF("Waiting for a device with RPR Server to be provisioned over PB-Adv...");
829 err = k_sem_take(&prov_sem, K_SECONDS(10));
830 *addr = current_dev_addr;
831
832 return err;
833 }
834
provision_remote(struct bt_mesh_rpr_node * srv,uint8_t dev_idx,uint16_t * addr)835 static int provision_remote(struct bt_mesh_rpr_node *srv, uint8_t dev_idx, uint16_t *addr)
836 {
837 static uint8_t uuid[16];
838 struct bt_mesh_rpr_scan_status scan_status;
839 int err;
840
841 memcpy(uuid, dev_uuid, 16);
842 uuid[6] = '0' + dev_idx;
843 uuid_to_provision_remote = uuid;
844
845 LOG_INF("Starting scanning for an unprov device...");
846 ASSERT_OK(bt_mesh_rpr_scan_start(&rpr_cli, srv, NULL, 5, 1, &scan_status));
847 ASSERT_EQUAL(BT_MESH_RPR_SUCCESS, scan_status.status);
848 ASSERT_EQUAL(BT_MESH_RPR_SCAN_MULTI, scan_status.scan);
849 ASSERT_EQUAL(1, scan_status.max_devs);
850 ASSERT_EQUAL(5, scan_status.timeout);
851
852 err = k_sem_take(&prov_sem, K_SECONDS(20));
853 *addr = current_dev_addr;
854
855 return err;
856 }
857
rpr_scan_report(struct bt_mesh_rpr_cli * cli,const struct bt_mesh_rpr_node * srv,struct bt_mesh_rpr_unprov * unprov,struct net_buf_simple * adv_data)858 static void rpr_scan_report(struct bt_mesh_rpr_cli *cli, const struct bt_mesh_rpr_node *srv,
859 struct bt_mesh_rpr_unprov *unprov, struct net_buf_simple *adv_data)
860 {
861 if (!uuid_to_provision_remote || memcmp(uuid_to_provision_remote, unprov->uuid, 16)) {
862 return;
863 }
864
865 LOG_INF("Remote device discovered. Provisioning...");
866 ASSERT_OK(bt_mesh_provision_remote(cli, srv, unprov->uuid, 0, prov_addr));
867 }
868
prov_node_added_rpr(uint16_t net_idx,uint8_t uuid[16],uint16_t addr,uint8_t num_elem)869 static void prov_node_added_rpr(uint16_t net_idx, uint8_t uuid[16], uint16_t addr,
870 uint8_t num_elem)
871 {
872 LOG_INF("Device 0x%04x reprovisioned", addr);
873 k_sem_give(&reprov_sem);
874 }
875
provisioner_pb_remote_client_setup(void)876 static void provisioner_pb_remote_client_setup(void)
877 {
878 k_sem_init(&prov_sem, 0, 1);
879 k_sem_init(&reprov_sem, 0, 1);
880
881 bt_mesh_device_setup(&prov, &rpr_cli_comp);
882
883 ASSERT_OK(bt_mesh_cdb_create(test_net_key));
884 ASSERT_OK(bt_mesh_provision(test_net_key, 0, 0, 0, 0x0001, dev_key));
885 }
886
device_pb_remote_server_setup(const struct bt_mesh_comp * comp,bool pb_adv_prov)887 static void device_pb_remote_server_setup(const struct bt_mesh_comp *comp, bool pb_adv_prov)
888 {
889 k_sem_init(&prov_sem, 0, 1);
890 k_sem_init(&reprov_sem, 0, 1);
891
892 bt_mesh_device_setup(&prov, comp);
893
894 if (pb_adv_prov) {
895 ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_ADV));
896
897 LOG_INF("Waiting for being provisioned...");
898 ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(20)));
899 } else {
900 ASSERT_TRUE(bt_mesh_is_provisioned());
901 }
902
903 LOG_INF("Enabling PB-Remote server");
904 ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_REMOTE));
905 }
906
device_pb_remote_server_setup_unproved(const struct bt_mesh_comp * comp,const struct bt_mesh_comp2 * comp_p2)907 static void device_pb_remote_server_setup_unproved(const struct bt_mesh_comp *comp,
908 const struct bt_mesh_comp2 *comp_p2)
909 {
910 device_pb_remote_server_setup(comp, true);
911 bt_mesh_comp2_register(comp_p2);
912 }
913
device_pb_remote_server_setup_proved(const struct bt_mesh_comp * comp,const struct bt_mesh_comp2 * comp_p2)914 static void device_pb_remote_server_setup_proved(const struct bt_mesh_comp *comp,
915 const struct bt_mesh_comp2 *comp_p2)
916 {
917 device_pb_remote_server_setup(comp, false);
918 bt_mesh_comp2_register(comp_p2);
919 }
920
921 /** @brief Verify that the provisioner can provision a device multiple times after resets using
922 * PB-Remote and RPR models.
923 */
test_provisioner_pb_remote_client_reprovision(void)924 static void test_provisioner_pb_remote_client_reprovision(void)
925 {
926 uint16_t pb_remote_server_addr;
927
928 provisioner_pb_remote_client_setup();
929
930 /* Provision the 2nd device over PB-Adv. */
931 ASSERT_OK(provision_adv(1, &pb_remote_server_addr));
932
933 for (int i = 0; i < PROV_REPROV_COUNT; i++) {
934 struct bt_mesh_rpr_node srv = {
935 .addr = pb_remote_server_addr,
936 .net_idx = 0,
937 .ttl = 3,
938 };
939
940 LOG_INF("Provisioner prov loop #%d, waiting for prov ...\n", i);
941 ASSERT_OK(provision_remote(&srv, 2, &srv.addr));
942
943 node_configure_and_reset();
944 }
945
946 PASS();
947 }
948
rpr_scan_report_parallel(struct bt_mesh_rpr_cli * cli,const struct bt_mesh_rpr_node * srv,struct bt_mesh_rpr_unprov * unprov,struct net_buf_simple * adv_data)949 static void rpr_scan_report_parallel(struct bt_mesh_rpr_cli *cli,
950 const struct bt_mesh_rpr_node *srv,
951 struct bt_mesh_rpr_unprov *unprov,
952 struct net_buf_simple *adv_data)
953 {
954 if (!uuid_to_provision_remote || memcmp(uuid_to_provision_remote, unprov->uuid, 16)) {
955 return;
956 }
957
958 LOG_INF("Scanning dev idx 2 succeeded.\n");
959 k_sem_give(&scan_sem);
960 }
961
test_provisioner_pb_remote_client_parallel(void)962 static void test_provisioner_pb_remote_client_parallel(void)
963 {
964 static uint8_t uuid[16];
965 uint16_t pb_remote_server_addr;
966 struct bt_mesh_rpr_scan_status scan_status;
967
968 memcpy(uuid, dev_uuid, 16);
969
970 k_sem_init(&prov_sem, 0, 1);
971 k_sem_init(&scan_sem, 0, 1);
972
973 bt_mesh_device_setup(&prov, &rpr_cli_comp);
974
975 ASSERT_OK(bt_mesh_cdb_create(test_net_key));
976 ASSERT_OK(bt_mesh_provision(test_net_key, 0, 0, 0, 0x0001, dev_key));
977
978 /* Provision the 2nd device over PB-Adv. */
979 ASSERT_OK(provision_adv(1, &pb_remote_server_addr));
980
981 struct bt_mesh_rpr_node srv = {
982 .addr = pb_remote_server_addr,
983 .net_idx = 0,
984 .ttl = 3,
985 };
986
987 rpr_cli.scan_report = rpr_scan_report_parallel;
988
989 LOG_INF("Scanning dev idx 2 and provisioning dev idx 3 in parallel ...\n");
990 /* provisioning device with dev index 2 */
991 uuid[6] = '0' + 2;
992 ASSERT_OK(bt_mesh_provision_remote(&rpr_cli, &srv, uuid, 0, prov_addr));
993 /* scanning device with dev index 3 */
994 uuid[6] = '0' + 3;
995 uuid_to_provision_remote = uuid;
996 ASSERT_OK(bt_mesh_rpr_scan_start(&rpr_cli, &srv, uuid, 15, 1, &scan_status));
997 ASSERT_EQUAL(BT_MESH_RPR_SUCCESS, scan_status.status);
998 ASSERT_EQUAL(BT_MESH_RPR_SCAN_SINGLE, scan_status.scan);
999 ASSERT_EQUAL(1, scan_status.max_devs);
1000 ASSERT_EQUAL(15, scan_status.timeout);
1001
1002 ASSERT_OK(k_sem_take(&scan_sem, K_SECONDS(20)));
1003 ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(20)));
1004
1005 /* Provisioning device index 3. Need it to succeed provisionee test scenario. */
1006 ASSERT_OK(bt_mesh_provision_remote(&rpr_cli, &srv, uuid, 0, prov_addr));
1007 ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(20)));
1008
1009 PASS();
1010 }
1011
1012 /** @brief Test Provisioning procedure on Remote Provisioning client:
1013 * - verify procedure timeouts on unresponsive unprovisioned device.
1014 */
test_provisioner_pb_remote_client_provision_timeout(void)1015 static void test_provisioner_pb_remote_client_provision_timeout(void)
1016 {
1017 uint16_t pb_remote_server_addr;
1018 uint8_t uuid[16];
1019 uint32_t link_close_wait_start;
1020 struct bt_mesh_rpr_scan_status scan_status;
1021
1022 k_sem_init(&scan_sem, 0, 1);
1023
1024 provisioner_pb_remote_client_setup();
1025 bt_mesh_test_cfg_set(NULL, 300);
1026
1027 /* Provision the 2nd device over PB-Adv. */
1028 ASSERT_OK(provision_adv(1, &pb_remote_server_addr));
1029
1030 /* Provision the 3rd device over PB-Remote. */
1031 struct bt_mesh_rpr_node srv = {
1032 .addr = pb_remote_server_addr,
1033 .net_idx = 0,
1034 .ttl = 3,
1035 };
1036
1037 rpr_cli.scan_report = rpr_scan_report_parallel;
1038
1039 /* Offset timeline of test to give some time to 3rd device to setup and disable scanning */
1040 k_sleep(K_SECONDS(10));
1041
1042 memcpy(uuid, dev_uuid, 16);
1043 uuid[6] = '0' + 2;
1044 uuid_to_provision_remote = uuid;
1045
1046 LOG_INF("Starting scanning for an unprov device...");
1047 ASSERT_OK(bt_mesh_rpr_scan_start(&rpr_cli, &srv, uuid, 5, 1, &scan_status));
1048 ASSERT_EQUAL(BT_MESH_RPR_SUCCESS, scan_status.status);
1049 ASSERT_EQUAL(BT_MESH_RPR_SCAN_SINGLE, scan_status.scan);
1050 ASSERT_EQUAL(1, scan_status.max_devs);
1051 ASSERT_EQUAL(5, scan_status.timeout);
1052
1053 ASSERT_OK(k_sem_take(&scan_sem, K_SECONDS(20)));
1054
1055 /* Invalidate earlier timestamp */
1056 link_close_timestamp = -1;
1057 ASSERT_OK(bt_mesh_provision_remote(&rpr_cli, &srv, uuid, 0, prov_addr));
1058 link_close_wait_start = k_uptime_get_32();
1059 ASSERT_EQUAL(k_sem_take(&prov_sem, K_SECONDS(20)), -EAGAIN);
1060 ASSERT_EQUAL((link_close_timestamp - link_close_wait_start) / MSEC_PER_SEC, 10);
1061
1062 /* 3rd device should now respond but stop again after link is opened */
1063 link_close_timestamp = -1;
1064 ASSERT_OK(bt_mesh_provision_remote(&rpr_cli, &srv, uuid, 0, prov_addr));
1065 ASSERT_OK(k_sem_take(&link_open_sem, K_SECONDS(20)));
1066 link_close_wait_start = k_uptime_get_32();
1067 ASSERT_EQUAL(k_sem_take(&prov_sem, K_SECONDS(61)), -EAGAIN);
1068 ASSERT_EQUAL((link_close_timestamp - link_close_wait_start) / MSEC_PER_SEC, 60);
1069
1070 PASS();
1071 }
1072
reprovision_remote_devkey_client(struct bt_mesh_rpr_node * srv,struct bt_mesh_cdb_node * node)1073 static void reprovision_remote_devkey_client(struct bt_mesh_rpr_node *srv,
1074 struct bt_mesh_cdb_node *node)
1075 {
1076 uint8_t status;
1077 uint8_t prev_node_dev_key[16];
1078
1079 ASSERT_OK_MSG(bt_mesh_cdb_node_key_export(node, prev_node_dev_key),
1080 "Can't export device key from cdb");
1081
1082 bt_mesh_reprovision_remote(&rpr_cli, srv, current_dev_addr, false);
1083
1084 ASSERT_OK(k_sem_take(&reprov_sem, K_SECONDS(20)));
1085
1086 /* Check that CDB has updated Device Key for the node. */
1087 ASSERT_TRUE(bt_mesh_key_compare(prev_node_dev_key, &node->dev_key));
1088 ASSERT_OK_MSG(bt_mesh_cdb_node_key_export(node, prev_node_dev_key),
1089 "Can't export device key from cdb");
1090
1091 /* Check device key by adding appkey. */
1092 ASSERT_OK(bt_mesh_cfg_cli_app_key_add(0, current_dev_addr, 0, 0, test_app_key,
1093 &status));
1094 ASSERT_OK(status);
1095
1096 /* Let RPR Server verify Device Key. */
1097 k_sleep(K_SECONDS(2));
1098 }
1099
reprovision_remote_comp_data_client(struct bt_mesh_rpr_node * srv,struct bt_mesh_cdb_node * node,struct net_buf_simple * dev_comp)1100 static void reprovision_remote_comp_data_client(struct bt_mesh_rpr_node *srv,
1101 struct bt_mesh_cdb_node *node,
1102 struct net_buf_simple *dev_comp)
1103 {
1104 NET_BUF_SIMPLE_DEFINE(new_dev_comp, BT_MESH_RX_SDU_MAX);
1105 uint8_t prev_node_dev_key[16];
1106 uint8_t page;
1107
1108 ASSERT_OK_MSG(bt_mesh_cdb_node_key_export(node, prev_node_dev_key),
1109 "Can't export device key from cdb");
1110
1111 bt_mesh_reprovision_remote(&rpr_cli, srv, current_dev_addr, true);
1112
1113 ASSERT_OK(k_sem_take(&reprov_sem, K_SECONDS(20)));
1114
1115 /* Check that CDB has updated Device Key for the node. */
1116 ASSERT_TRUE(bt_mesh_key_compare(prev_node_dev_key, &node->dev_key));
1117 ASSERT_OK_MSG(bt_mesh_cdb_node_key_export(node, prev_node_dev_key),
1118 "Can't export device key from cdb");
1119
1120 /* Check that Composition Data Page 128 is now Page 0. */
1121 net_buf_simple_reset(&new_dev_comp);
1122 ASSERT_OK(bt_mesh_cfg_cli_comp_data_get(0, current_dev_addr, 0, &page,
1123 &new_dev_comp));
1124
1125 ASSERT_EQUAL(0, page);
1126 ASSERT_EQUAL(dev_comp->len, new_dev_comp.len);
1127 if (memcmp(dev_comp->data, new_dev_comp.data, dev_comp->len)) {
1128 FAIL("Wrong composition data page 0");
1129 }
1130
1131 /* Let RPR Server verify Device Key. */
1132 k_sleep(K_SECONDS(2));
1133 }
1134
reprovision_remote_address_client(struct bt_mesh_rpr_node * srv,struct bt_mesh_cdb_node * node)1135 static void reprovision_remote_address_client(struct bt_mesh_rpr_node *srv,
1136 struct bt_mesh_cdb_node *node)
1137 {
1138 uint8_t status;
1139 uint8_t prev_node_dev_key[16];
1140
1141 ASSERT_OK_MSG(bt_mesh_cdb_node_key_export(node, prev_node_dev_key),
1142 "Can't export device key from cdb");
1143
1144 bt_mesh_reprovision_remote(&rpr_cli, srv, current_dev_addr + 1, false);
1145
1146 ASSERT_OK(k_sem_take(&reprov_sem, K_SECONDS(20)));
1147
1148 current_dev_addr++;
1149 srv->addr++;
1150
1151 /* Check that device doesn't respond to old address with old and new device key. */
1152 struct bt_mesh_cdb_node *prev_node;
1153 uint8_t tmp[16];
1154
1155 prev_node = bt_mesh_cdb_node_alloc((uint8_t[16]) {}, current_dev_addr - 1, 1, 0);
1156 ASSERT_TRUE(node);
1157 ASSERT_OK_MSG(bt_mesh_cdb_node_key_import(prev_node, prev_node_dev_key),
1158 "Can't import device key into cdb");
1159 ASSERT_EQUAL(-ETIMEDOUT, bt_mesh_cfg_cli_app_key_add(0, current_dev_addr - 1, 0, 0,
1160 test_app_key, &status));
1161 ASSERT_OK_MSG(bt_mesh_cdb_node_key_export(node, tmp),
1162 "Can't export device key from cdb");
1163 ASSERT_OK_MSG(bt_mesh_cdb_node_key_import(prev_node, tmp),
1164 "Can't import device key into cdb");
1165 ASSERT_EQUAL(-ETIMEDOUT, bt_mesh_cfg_cli_app_key_add(0, current_dev_addr - 1, 0, 0,
1166 test_app_key, &status));
1167 bt_mesh_cdb_node_del(prev_node, false);
1168
1169 /* Check that CDB has updated Device Key for the node. */
1170 ASSERT_TRUE(bt_mesh_key_compare(prev_node_dev_key, &node->dev_key));
1171 ASSERT_OK_MSG(bt_mesh_cdb_node_key_export(node, prev_node_dev_key),
1172 "Can't export device key from cdb");
1173
1174 /* Check new device address by adding appkey. */
1175 ASSERT_OK(bt_mesh_cfg_cli_app_key_add(0, current_dev_addr, 0, 0, test_app_key,
1176 &status));
1177 ASSERT_OK(status);
1178
1179 /* Let RPR Server verify Device Key. */
1180 k_sleep(K_SECONDS(2));
1181
1182 }
1183
1184 /** @brief Verify robustness of NPPI procedures on a RPR Client by running Device Key Refresh,
1185 * Node Composition Refresh and Node Address Refresh procedures.
1186 */
test_provisioner_pb_remote_client_nppi_robustness(void)1187 static void test_provisioner_pb_remote_client_nppi_robustness(void)
1188 {
1189 NET_BUF_SIMPLE_DEFINE(dev_comp, BT_MESH_RX_SDU_MAX);
1190 uint8_t page;
1191 uint16_t pb_remote_server_addr;
1192 uint8_t status;
1193 struct bt_mesh_cdb_node *node;
1194
1195 provisioner_pb_remote_client_setup();
1196
1197 /* Provision the 2nd device over PB-Adv. */
1198 ASSERT_OK(provision_adv(1, &pb_remote_server_addr));
1199
1200 /* Provision a remote device with RPR Server. */
1201 struct bt_mesh_rpr_node srv = {
1202 .addr = pb_remote_server_addr,
1203 .net_idx = 0,
1204 .ttl = 3,
1205 };
1206
1207 ASSERT_OK(provision_remote(&srv, 2, &srv.addr));
1208
1209 /* Check device key by adding appkey. */
1210 ASSERT_OK(bt_mesh_cfg_cli_app_key_add(0, current_dev_addr, 0, 0, test_app_key, &status));
1211 ASSERT_OK(status);
1212
1213 /* Swap callback to catch when device reprovisioned. */
1214 prov.node_added = prov_node_added_rpr;
1215
1216 /* Store initial Composition Data Page 0. */
1217 ASSERT_OK(bt_mesh_cfg_cli_comp_data_get(0, current_dev_addr, 0, &page, &dev_comp));
1218
1219 node = bt_mesh_cdb_node_get(current_dev_addr);
1220 ASSERT_TRUE(node);
1221
1222 LOG_INF("Testing DevKey refresh...");
1223 for (int i = 0; i < PROV_REPROV_COUNT; i++) {
1224 LOG_INF("Refreshing device key #%d...\n", i);
1225 reprovision_remote_devkey_client(&srv, node);
1226 }
1227
1228 LOG_INF("Testing Composition Data refresh...");
1229 for (int i = 0; i < PROV_REPROV_COUNT; i++) {
1230 LOG_INF("Changing Composition Data #%d...\n", i);
1231 reprovision_remote_comp_data_client(&srv, node, &dev_comp);
1232 }
1233
1234 LOG_INF("Testing address refresh...");
1235 for (int i = 0; i < PROV_REPROV_COUNT; i++) {
1236 LOG_INF("Changing address #%d...\n", i);
1237 reprovision_remote_address_client(&srv, node);
1238 }
1239
1240 PASS();
1241 }
1242
1243 /** @brief A device running a Remote Provisioning server that is used to provision unprovisioned
1244 * devices over PB-Remote. Always starts unprovisioned.
1245 */
test_device_pb_remote_server_unproved(void)1246 static void test_device_pb_remote_server_unproved(void)
1247 {
1248 device_pb_remote_server_setup_unproved(&rpr_srv_comp, &comp_p2_1);
1249
1250 PASS();
1251 }
1252
1253 /** @brief A device running a Remote Provisioning server that is used to provision unprovisioned
1254 * devices over PB-Remote. Always starts unprovisioned. Stops being responsive after receives
1255 * Remote Provisioning PDU Send message from RPR Client
1256 */
test_device_pb_remote_server_unproved_unresponsive(void)1257 static void test_device_pb_remote_server_unproved_unresponsive(void)
1258 {
1259 device_pb_remote_server_setup_unproved(&rpr_srv_comp_unresponsive, NULL);
1260
1261 k_sem_init(&pdu_send_sem, 0, 1);
1262 ASSERT_OK(k_sem_take(&pdu_send_sem, K_SECONDS(200)));
1263
1264 PASS();
1265 }
1266
1267 /** @brief A device running a Remote Provisioning server that is used to provision unprovisioned
1268 * devices over PB-Remote. Starts provisioned.
1269 */
test_device_pb_remote_server_proved(void)1270 static void test_device_pb_remote_server_proved(void)
1271 {
1272 device_pb_remote_server_setup_proved(&rpr_srv_comp, &comp_p2_1);
1273
1274 PASS();
1275 }
1276
reprovision_remote_devkey_server(const uint16_t initial_addr)1277 static void reprovision_remote_devkey_server(const uint16_t initial_addr)
1278 {
1279 uint8_t prev_dev_key[16];
1280 uint8_t dev_key[16];
1281
1282 ASSERT_OK(bt_mesh_key_export(prev_dev_key, &bt_mesh.dev_key));
1283
1284 ASSERT_OK(k_sem_take(&reprov_sem, K_SECONDS(30)));
1285 ASSERT_EQUAL(initial_addr, bt_mesh_primary_addr());
1286
1287 /* Let Configuration Client activate the new Device Key and verify that it has
1288 * been changed.
1289 */
1290 k_sleep(K_SECONDS(2));
1291 ASSERT_OK(bt_mesh_key_export(dev_key, &bt_mesh.dev_key));
1292 ASSERT_TRUE(memcmp(&prev_dev_key, dev_key, sizeof(dev_key)));
1293 }
1294
reprovision_remote_comp_data_server(const uint16_t initial_addr)1295 static void reprovision_remote_comp_data_server(const uint16_t initial_addr)
1296 {
1297 u_int8_t prev_dev_key[16];
1298 u_int8_t dev_key[16];
1299
1300 /* The RPR Server won't let to run Node Composition Refresh procedure without first
1301 * setting the BT_MESH_COMP_DIRTY flag. The flag is set on a boot if there is a
1302 * "bt/mesh/cmp" entry in settings. The entry is added by the
1303 * `bt_mesh_comp_change_prepare() call. The test suite is not compiled
1304 * with CONFIG_BT_SETTINGS, so the flag will never be set. Since the purpose of the
1305 * test is to check RPR Server behavior, but not the actual swap of the Composition
1306 * Data, the flag is toggled directly from the test.
1307 */
1308 atomic_set_bit(bt_mesh.flags, BT_MESH_COMP_DIRTY);
1309 ASSERT_OK(bt_mesh_key_export(prev_dev_key, &bt_mesh.dev_key));
1310
1311 ASSERT_OK(k_sem_take(&reprov_sem, K_SECONDS(30)));
1312
1313 /* Drop the flag manually as CONFIG_BT_SETTINGS is not enabled. */
1314 atomic_clear_bit(bt_mesh.flags, BT_MESH_COMP_DIRTY);
1315
1316 ASSERT_EQUAL(initial_addr, bt_mesh_primary_addr());
1317
1318 /* Let Configuration Client activate the new Device Key and verify that it has
1319 * been changed.
1320 */
1321 k_sleep(K_SECONDS(2));
1322 ASSERT_OK(bt_mesh_key_export(dev_key, &bt_mesh.dev_key));
1323 ASSERT_TRUE(memcmp(prev_dev_key, dev_key, sizeof(dev_key)));
1324 }
1325
reprovision_remote_address_server(const uint16_t initial_addr)1326 static void reprovision_remote_address_server(const uint16_t initial_addr)
1327 {
1328 uint8_t prev_dev_key[16];
1329 uint8_t dev_key[16];
1330
1331 ASSERT_OK(bt_mesh_key_export(prev_dev_key, &bt_mesh.dev_key));
1332
1333 ASSERT_OK(k_sem_take(&reprov_sem, K_SECONDS(30)));
1334 ASSERT_EQUAL(initial_addr + 1, bt_mesh_primary_addr());
1335
1336 /* Let Configuration Client activate the new Device Key and verify that it has
1337 * been changed.
1338 */
1339 k_sleep(K_SECONDS(2));
1340 ASSERT_OK(bt_mesh_key_export(dev_key, &bt_mesh.dev_key));
1341 ASSERT_TRUE(memcmp(prev_dev_key, dev_key, sizeof(dev_key)));
1342 }
1343
1344 /** @brief Verify robustness of NPPI procedures on a RPR Server by running Device Key Refresh,
1345 * Node Composition Refresh and Node Address Refresh procedures multiple times each.
1346 */
test_device_pb_remote_server_nppi_robustness(void)1347 static void test_device_pb_remote_server_nppi_robustness(void)
1348 {
1349 k_sem_init(&prov_sem, 0, 1);
1350 k_sem_init(&reprov_sem, 0, 1);
1351
1352 bt_mesh_device_setup(&prov, &rpr_srv_comp);
1353
1354 ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_ADV));
1355
1356 LOG_INF("Mesh initialized\n");
1357
1358 ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(20)));
1359 const uint16_t initial_addr = bt_mesh_primary_addr();
1360
1361 LOG_INF("Enabling PB-Remote server");
1362 ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_REMOTE));
1363
1364 /* Test Device Key Refresh procedure robustness. */
1365 for (int i = 0; i < PROV_REPROV_COUNT; i++) {
1366 LOG_INF("Devkey refresh loop #%d, waiting for being reprov ...\n", i);
1367 reprovision_remote_devkey_server(initial_addr);
1368 }
1369
1370 /* Test Node Composition Refresh procedure robustness. */
1371 for (int i = 0; i < PROV_REPROV_COUNT; i++) {
1372 LOG_INF("Composition data refresh loop #%d, waiting for being reprov ...\n", i);
1373 reprovision_remote_comp_data_server(initial_addr);
1374 }
1375
1376 /* Node Address Refresh robustness. */
1377 for (int i = 0; i < PROV_REPROV_COUNT; i++) {
1378 LOG_INF("Address refresh loop #%d, waiting for being reprov ...\n", i);
1379 reprovision_remote_address_server(initial_addr+i);
1380 }
1381
1382 PASS();
1383 }
1384
1385 /** @brief Test Node Composition Refresh procedure on Remote Provisioning client:
1386 * - provision a device over PB-Adv,
1387 * - provision a remote device over PB-Remote.
1388 */
test_provisioner_pb_remote_client_ncrp_provision(void)1389 static void test_provisioner_pb_remote_client_ncrp_provision(void)
1390 {
1391 uint16_t pb_remote_server_addr;
1392 uint8_t status;
1393
1394 provisioner_pb_remote_client_setup();
1395
1396 /* Provision the 2nd device over PB-Adv. */
1397 ASSERT_OK(provision_adv(1, &pb_remote_server_addr));
1398
1399 /* Provision the 3rd device over PB-Remote. */
1400 struct bt_mesh_rpr_node srv = {
1401 .addr = pb_remote_server_addr,
1402 .net_idx = 0,
1403 .ttl = 3,
1404 };
1405
1406 ASSERT_OK(provision_remote(&srv, 2, &srv.addr));
1407
1408 /* Check device key by adding appkey. */
1409 ASSERT_OK(bt_mesh_cfg_cli_app_key_add(0, pb_remote_server_addr, 0, 0, test_app_key,
1410 &status));
1411 ASSERT_OK(status);
1412
1413 PASS();
1414 }
1415
1416 /** @brief A device running a Remote Provisioning client and server that is used to reprovision
1417 * another device and it self with the client.
1418 */
test_device_pb_remote_client_server_same_dev(void)1419 static void test_device_pb_remote_client_server_same_dev(void)
1420 {
1421 NET_BUF_SIMPLE_DEFINE(dev_comp, BT_MESH_RX_SDU_MAX);
1422 uint8_t status;
1423 struct bt_mesh_cdb_node *node;
1424 uint8_t page;
1425 uint8_t prev_dev_key[16];
1426 uint16_t test_vector[] = { 0x0002, 0x0001 };
1427
1428 k_sem_init(&prov_sem, 0, 1);
1429 k_sem_init(&reprov_sem, 0, 1);
1430
1431 bt_mesh_device_setup(&prov, &rpr_cli_srv_comp);
1432
1433 ASSERT_OK(bt_mesh_cdb_create(test_net_key));
1434 ASSERT_OK(bt_mesh_provision(test_net_key, 0, 0, 0, 0x0001, dev_key));
1435
1436 LOG_INF("Enabling PB-Remote server");
1437 ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_REMOTE));
1438
1439 /* Provision a remote device with RPR Client and Server with local RPR Server. */
1440 current_dev_addr = 0x0001;
1441 struct bt_mesh_rpr_node srv = {
1442 .addr = current_dev_addr,
1443 .net_idx = 0,
1444 .ttl = 3,
1445 };
1446
1447 LOG_INF("Provisioner prov, waiting for prov ...\n");
1448 ASSERT_OK(provision_remote(&srv, 1, &srv.addr));
1449
1450 ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(20)));
1451
1452 /* Check device key by adding bt_mesh_reprovision_remote appkey. */
1453 ASSERT_OK(bt_mesh_cfg_cli_app_key_add(0, current_dev_addr, 0, 0, test_app_key, &status));
1454 ASSERT_OK(status);
1455
1456 /* Swap callback to catch when device reprovisioned. */
1457 prov.node_added = prov_node_added_rpr;
1458
1459 /* Reprovision a device with both RPR Client and Server. */
1460 for (int i = 0; i < ARRAY_SIZE(test_vector); i++) {
1461 current_dev_addr = test_vector[i];
1462 srv.addr = current_dev_addr;
1463 bool self_reprov = (bool)(current_dev_addr == bt_mesh_primary_addr());
1464
1465 /* Store initial Composition Data Page 0. */
1466 net_buf_simple_reset(&dev_comp);
1467 ASSERT_OK(bt_mesh_cfg_cli_comp_data_get(0, current_dev_addr, 0, &page, &dev_comp));
1468
1469 node = bt_mesh_cdb_node_get(current_dev_addr);
1470 ASSERT_TRUE(node);
1471
1472 LOG_INF("Refreshing 0x%04x device key ...\n", srv.addr);
1473 ASSERT_OK(bt_mesh_key_export(prev_dev_key, &bt_mesh.dev_key));
1474 reprovision_remote_devkey_client(&srv, node);
1475 if (self_reprov) {
1476 uint8_t dev_key[16];
1477
1478 ASSERT_EQUAL(current_dev_addr, bt_mesh_primary_addr());
1479
1480 /* Let Configuration Client activate the new Device Key
1481 * and verify that it has been changed.
1482 */
1483 ASSERT_OK(bt_mesh_key_export(dev_key, &bt_mesh.dev_key));
1484 ASSERT_TRUE(memcmp(prev_dev_key, dev_key, sizeof(dev_key)));
1485 }
1486
1487 LOG_INF("Changing 0x%04x Composition Data ...\n", srv.addr);
1488 ASSERT_OK(bt_mesh_key_export(prev_dev_key, &bt_mesh.dev_key));
1489 reprovision_remote_comp_data_client(&srv, node, &dev_comp);
1490 if (self_reprov) {
1491 uint8_t dev_key[16];
1492
1493 ASSERT_EQUAL(current_dev_addr, bt_mesh_primary_addr());
1494
1495 /* Let Configuration Client activate the new Device Key
1496 * and verify that it has been changed.
1497 */
1498 ASSERT_OK(bt_mesh_key_export(dev_key, &bt_mesh.dev_key));
1499 ASSERT_TRUE(memcmp(prev_dev_key, dev_key, sizeof(struct bt_mesh_key)));
1500 }
1501
1502 LOG_INF("Changing 0x%04x address ...\n", srv.addr);
1503 ASSERT_OK(bt_mesh_key_export(prev_dev_key, &bt_mesh.dev_key));
1504 reprovision_remote_address_client(&srv, node);
1505 if (self_reprov) {
1506 uint8_t dev_key[16];
1507
1508 ASSERT_EQUAL(current_dev_addr, bt_mesh_primary_addr());
1509
1510 /* Let Configuration Client activate the new Device Key
1511 * and verify that it has been changed.
1512 */
1513 ASSERT_OK(bt_mesh_key_export(dev_key, &bt_mesh.dev_key));
1514 ASSERT_TRUE(memcmp(prev_dev_key, dev_key, sizeof(dev_key)));
1515 }
1516 }
1517
1518 PASS();
1519 }
1520
1521 /** @brief Verify that the Remote Provisioning client and server is able to be reprovision
1522 * by another device with a Remote Provisioning client and server.
1523 */
test_device_pb_remote_server_same_dev(void)1524 static void test_device_pb_remote_server_same_dev(void)
1525 {
1526 k_sem_init(&prov_sem, 0, 1);
1527 k_sem_init(&reprov_sem, 0, 1);
1528
1529 bt_mesh_device_setup(&prov, &rpr_cli_srv_comp);
1530
1531 ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_ADV));
1532
1533 LOG_INF("Waiting for being provisioned...");
1534 ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(20)));
1535
1536 LOG_INF("Enabling PB-Remote server");
1537 ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_REMOTE));
1538
1539 /* Swap callback to catch when device reprovisioned. */
1540 prov.node_added = prov_node_added_rpr;
1541
1542 const uint16_t initial_addr = bt_mesh_primary_addr();
1543
1544 LOG_INF("Devkey refresh, waiting for being reprov ...\n");
1545 reprovision_remote_devkey_server(initial_addr);
1546
1547 LOG_INF("Composition data refresh, waiting for being reprov ...\n");
1548 reprovision_remote_comp_data_server(initial_addr);
1549
1550 LOG_INF("Address refresh, waiting for being reprov ...\n");
1551 reprovision_remote_address_server(initial_addr);
1552
1553 PASS();
1554 }
1555
comp_data_get(uint16_t server_addr,uint8_t page,struct net_buf_simple * comp)1556 static void comp_data_get(uint16_t server_addr, uint8_t page, struct net_buf_simple *comp)
1557 {
1558 uint8_t page_rsp;
1559
1560 /* Let complete advertising of the transaction to prevent collisions. */
1561 k_sleep(K_SECONDS(3));
1562
1563 net_buf_simple_reset(comp);
1564 ASSERT_OK(bt_mesh_cfg_cli_comp_data_get(0, server_addr, page, &page_rsp, comp));
1565 ASSERT_EQUAL(page, page_rsp);
1566 }
1567
comp_data_compare(struct net_buf_simple * comp1,struct net_buf_simple * comp2,bool expect_equal)1568 static void comp_data_compare(struct net_buf_simple *comp1, struct net_buf_simple *comp2,
1569 bool expect_equal)
1570 {
1571 if (expect_equal) {
1572 ASSERT_EQUAL(comp1->len, comp2->len);
1573 if (memcmp(comp1->data, comp2->data, comp1->len)) {
1574 FAIL("Composition data is not equal");
1575 }
1576 } else {
1577 if (comp1->len == comp2->len) {
1578 if (!memcmp(comp1->data, comp2->data, comp1->len)) {
1579 FAIL("Composition data is equal");
1580 }
1581 }
1582 }
1583 }
1584
1585 /** @brief Test Node Composition Refresh procedure on Remote Provisioning client:
1586 * - initiate Node Composition Refresh procedure on a 3rd device.
1587 */
test_provisioner_pb_remote_client_ncrp(void)1588 static void test_provisioner_pb_remote_client_ncrp(void)
1589 {
1590 NET_BUF_SIMPLE_DEFINE(dev_comp_p0, BT_MESH_RX_SDU_MAX);
1591 NET_BUF_SIMPLE_DEFINE(dev_comp_p1, BT_MESH_RX_SDU_MAX);
1592 NET_BUF_SIMPLE_DEFINE(dev_comp_p2, BT_MESH_RX_SDU_MAX);
1593 NET_BUF_SIMPLE_DEFINE(dev_comp_p128, BT_MESH_RX_SDU_MAX);
1594 NET_BUF_SIMPLE_DEFINE(dev_comp_p129, BT_MESH_RX_SDU_MAX);
1595 NET_BUF_SIMPLE_DEFINE(dev_comp_p130, BT_MESH_RX_SDU_MAX);
1596
1597 uint16_t pb_remote_server_addr = 0x0003;
1598
1599 k_sem_init(&prov_sem, 0, 1);
1600 k_sem_init(&reprov_sem, 0, 1);
1601
1602 bt_mesh_device_setup(&prov, &rpr_cli_comp);
1603
1604 /* Store Composition Data Page 0, 1, 2, 128, 129 and 130. */
1605 comp_data_get(pb_remote_server_addr, 0, &dev_comp_p0);
1606 comp_data_get(pb_remote_server_addr, 128, &dev_comp_p128);
1607 comp_data_compare(&dev_comp_p0, &dev_comp_p128, false);
1608
1609 comp_data_get(pb_remote_server_addr, 1, &dev_comp_p1);
1610 comp_data_get(pb_remote_server_addr, 129, &dev_comp_p129);
1611 comp_data_compare(&dev_comp_p1, &dev_comp_p129, false);
1612
1613 comp_data_get(pb_remote_server_addr, 2, &dev_comp_p2);
1614 comp_data_get(pb_remote_server_addr, 130, &dev_comp_p130);
1615 comp_data_compare(&dev_comp_p2, &dev_comp_p130, false);
1616
1617
1618 LOG_INF("Start Node Composition Refresh procedure...\n");
1619 struct bt_mesh_rpr_node srv = {
1620 .addr = pb_remote_server_addr,
1621 .net_idx = 0,
1622 .ttl = 3,
1623 };
1624
1625 /* Swap callback to catch when device reprovisioned. */
1626 prov.node_added = prov_node_added_rpr;
1627
1628 ASSERT_OK(bt_mesh_reprovision_remote(&rpr_cli, &srv, pb_remote_server_addr, true));
1629 ASSERT_OK(k_sem_take(&reprov_sem, K_SECONDS(20)));
1630
1631 /* Check that Composition Data Page 128 still exists and is now equal to Page 0. */
1632 comp_data_get(pb_remote_server_addr, 0, &dev_comp_p0);
1633 comp_data_compare(&dev_comp_p0, &dev_comp_p128, true);
1634 comp_data_get(pb_remote_server_addr, 128, &dev_comp_p128);
1635 comp_data_compare(&dev_comp_p0, &dev_comp_p128, true);
1636
1637 /* Check that Composition Data Page 129 still exists and is now equal to Page 1. */
1638 comp_data_get(pb_remote_server_addr, 1, &dev_comp_p1);
1639 comp_data_compare(&dev_comp_p1, &dev_comp_p129, true);
1640 comp_data_get(pb_remote_server_addr, 129, &dev_comp_p129);
1641 comp_data_compare(&dev_comp_p1, &dev_comp_p129, true);
1642
1643 /* Check that Composition Data Page 130 still exists and is now equal to Page 2. */
1644 comp_data_get(pb_remote_server_addr, 2, &dev_comp_p2);
1645 comp_data_compare(&dev_comp_p2, &dev_comp_p130, true);
1646 comp_data_get(pb_remote_server_addr, 130, &dev_comp_p130);
1647 comp_data_compare(&dev_comp_p2, &dev_comp_p130, true);
1648
1649 PASS();
1650 }
1651
comp_data_pages_get_and_equal_check(uint16_t server_addr,uint8_t page1,uint8_t page2)1652 static void comp_data_pages_get_and_equal_check(uint16_t server_addr, uint8_t page1, uint8_t page2)
1653 {
1654 NET_BUF_SIMPLE_DEFINE(comp_1, BT_MESH_RX_SDU_MAX);
1655 NET_BUF_SIMPLE_DEFINE(comp_2, BT_MESH_RX_SDU_MAX);
1656
1657 comp_data_get(server_addr, page1, &comp_1);
1658 comp_data_get(server_addr, page2, &comp_2);
1659 comp_data_compare(&comp_1, &comp_2, true);
1660 }
1661
1662 /** @brief Test Node Composition Refresh procedure on Remote Provisioning client:
1663 * - verify that Composition Data Page 0 is now equal to Page 128 after reboot.
1664 */
test_provisioner_pb_remote_client_ncrp_second_time(void)1665 static void test_provisioner_pb_remote_client_ncrp_second_time(void)
1666 {
1667 uint16_t pb_remote_server_addr = 0x0003;
1668 int err;
1669
1670 k_sem_init(&prov_sem, 0, 1);
1671 k_sem_init(&reprov_sem, 0, 1);
1672
1673 bt_mesh_device_setup(&prov, &rpr_cli_comp);
1674
1675 comp_data_pages_get_and_equal_check(pb_remote_server_addr, 0, 128);
1676 comp_data_pages_get_and_equal_check(pb_remote_server_addr, 1, 129);
1677 comp_data_pages_get_and_equal_check(pb_remote_server_addr, 2, 130);
1678
1679 LOG_INF("Start Node Composition Refresh procedure...\n");
1680 struct bt_mesh_rpr_node srv = {
1681 .addr = pb_remote_server_addr,
1682 .net_idx = 0,
1683 .ttl = 3,
1684 };
1685
1686 /* Swap callback to catch when device reprovisioned. */
1687 prov.node_added = prov_node_added_rpr;
1688
1689 ASSERT_OK(bt_mesh_reprovision_remote(&rpr_cli, &srv, pb_remote_server_addr, true));
1690 err = k_sem_take(&reprov_sem, K_SECONDS(20));
1691 ASSERT_EQUAL(-EAGAIN, err);
1692
1693 PASS();
1694 }
1695
1696 /** @brief Test Node Composition Refresh procedure on Remote Provisioning server:
1697 * - wait for being provisioned over PB-Adv,
1698 * - prepare Composition Data Page 128.
1699 */
test_device_pb_remote_server_ncrp_prepare(void)1700 static void test_device_pb_remote_server_ncrp_prepare(void)
1701 {
1702 device_pb_remote_server_setup_unproved(&rpr_srv_comp, &comp_p2_1);
1703
1704 LOG_INF("Preparing for Composition Data change");
1705 bt_mesh_comp_change_prepare();
1706
1707 PASS();
1708 }
1709
1710 /** @brief Test Node Composition Refresh procedure on Remote Provisioning server:
1711 * - start device with new Composition Data
1712 * - wait for being re-provisioned.
1713 */
test_device_pb_remote_server_ncrp(void)1714 static void test_device_pb_remote_server_ncrp(void)
1715 {
1716 device_pb_remote_server_setup_proved(&rpr_srv_comp_2_elem, &comp_p2_2);
1717
1718 LOG_INF("Waiting for being re-provisioned.");
1719 ASSERT_OK(k_sem_take(&reprov_sem, K_SECONDS(30)));
1720
1721 PASS();
1722 }
1723
1724 /** @brief Test Node Composition Refresh procedure on Remote Provisioning server:
1725 * - verify that Composition Data Page 0 is replaced by Page 128 after being re-provisioned and
1726 * rebooted.
1727 */
test_device_pb_remote_server_ncrp_second_time(void)1728 static void test_device_pb_remote_server_ncrp_second_time(void)
1729 {
1730 int err;
1731
1732 device_pb_remote_server_setup_proved(&rpr_srv_comp_2_elem, &comp_p2_2);
1733
1734 LOG_INF("Wait to verify that node is not re-provisioned...");
1735 err = k_sem_take(&reprov_sem, K_SECONDS(30));
1736 ASSERT_EQUAL(-EAGAIN, err);
1737
1738 PASS();
1739 }
1740 #endif /* IS_RPR_PRESENT */
1741
1742 #define TEST_CASE(role, name, description) \
1743 { \
1744 .test_id = "prov_" #role "_" #name, .test_descr = description, \
1745 .test_post_init_f = test_##role##_init, \
1746 .test_tick_f = bt_mesh_test_timeout, \
1747 .test_main_f = test_##role##_##name, \
1748 .test_delete_f = test_terminate \
1749 }
1750 #define TEST_CASE_WBACKCHANNEL(role, name, description) \
1751 { \
1752 .test_id = "prov_" #role "_" #name, .test_descr = description, \
1753 .test_post_init_f = test_##role##_init, \
1754 .test_pre_init_f = test_back_channel_pre_init, \
1755 .test_tick_f = bt_mesh_test_timeout, \
1756 .test_main_f = test_##role##_##name, \
1757 .test_delete_f = test_terminate \
1758 }
1759
1760 static const struct bst_test_instance test_connect[] = {
1761 TEST_CASE(device, pb_adv_no_oob,
1762 "Device: pb-adv provisioning use no-oob method"),
1763 TEST_CASE_WBACKCHANNEL(device, pb_adv_oob_auth,
1764 "Device: pb-adv provisioning use oob authentication"),
1765 TEST_CASE_WBACKCHANNEL(device, pb_adv_oob_public_key,
1766 "Device: pb-adv provisioning use oob public key"),
1767 TEST_CASE(device, pb_adv_reprovision,
1768 "Device: pb-adv provisioning, reprovision"),
1769 TEST_CASE(device, unresponsive,
1770 "Device: pb-adv provisioning, stops and resumes responding to provisioning"),
1771 #if IS_RPR_PRESENT
1772 TEST_CASE(device, pb_remote_server_unproved,
1773 "Device: used for remote provisioning, starts unprovisioned"),
1774 TEST_CASE(device, pb_remote_server_nppi_robustness,
1775 "Device: pb-remote reprovisioning, NPPI robustness"),
1776 TEST_CASE(device, pb_remote_server_unproved_unresponsive,
1777 "Device: used for remote provisioning, starts unprovisioned, stops responding"),
1778 TEST_CASE(device, pb_remote_client_server_same_dev,
1779 "Device: used for remote provisioning, with both client and server"),
1780 TEST_CASE(device, pb_remote_server_same_dev,
1781 "Device: used for remote reprovisioning, with both client and server"),
1782 #endif
1783
1784 TEST_CASE(provisioner, pb_adv_no_oob,
1785 "Provisioner: pb-adv provisioning use no-oob method"),
1786 TEST_CASE(provisioner, pb_adv_multi,
1787 "Provisioner: pb-adv provisioning multiple devices"),
1788 TEST_CASE(provisioner, iv_update_flag_zero,
1789 "Provisioner: effect on ivu_duration when IV Update flag is set to zero"),
1790 TEST_CASE(provisioner, iv_update_flag_one,
1791 "Provisioner: effect on ivu_duration when IV Update flag is set to one"),
1792 TEST_CASE_WBACKCHANNEL(provisioner, pb_adv_oob_auth,
1793 "Provisioner: pb-adv provisioning use oob authentication"),
1794 TEST_CASE_WBACKCHANNEL(provisioner, pb_adv_oob_public_key,
1795 "Provisioner: pb-adv provisioning use oob public key"),
1796 TEST_CASE_WBACKCHANNEL(provisioner, pb_adv_oob_auth_no_oob_public_key,
1797 "Provisioner: pb-adv provisioning use oob authentication, ignore oob public key"),
1798 TEST_CASE(provisioner, pb_adv_reprovision,
1799 "Provisioner: pb-adv provisioning, resetting and reprovisioning multiple times."),
1800 #if IS_RPR_PRESENT
1801 TEST_CASE(provisioner, pb_remote_client_reprovision,
1802 "Provisioner: pb-remote provisioning, resetting and reprov-ing multiple times."),
1803 TEST_CASE(provisioner, pb_remote_client_nppi_robustness,
1804 "Provisioner: pb-remote provisioning, NPPI robustness."),
1805 TEST_CASE(provisioner, pb_remote_client_parallel,
1806 "Provisioner: pb-remote provisioning, parallel scanning and provisioning."),
1807 TEST_CASE(provisioner, pb_remote_client_provision_timeout,
1808 "Provisioner: provisioning test, devices stop responding"),
1809 #endif
1810
1811 BSTEST_END_MARKER
1812 };
1813
test_provision_install(struct bst_test_list * tests)1814 struct bst_test_list *test_provision_install(struct bst_test_list *tests)
1815 {
1816 tests = bst_add_tests(tests, test_connect);
1817 return tests;
1818 }
1819
1820 #if IS_RPR_PRESENT
1821 static const struct bst_test_instance test_connect_pst[] = {
1822 TEST_CASE(device, pb_remote_server_unproved,
1823 "Device: used for remote provisioning, starts unprovisioned"),
1824 TEST_CASE(device, pb_remote_server_proved,
1825 "Device: used for remote provisioning, starts provisioned"),
1826
1827 TEST_CASE(device, pb_remote_server_ncrp_prepare,
1828 "Device: NCRP test, prepares for Composition Data change."),
1829 TEST_CASE(device, pb_remote_server_ncrp,
1830 "Device: NCRP test, Composition Data change."),
1831 TEST_CASE(device, pb_remote_server_ncrp_second_time,
1832 "Device: NCRP test, Composition Data change after reboot."),
1833
1834 TEST_CASE(provisioner, pb_remote_client_ncrp_provision,
1835 "Provisioner: NCRP test, devices provisioning."),
1836 TEST_CASE(provisioner, pb_remote_client_ncrp,
1837 "Provisioner: NCRP test, initiates Node Composition Refresh procedure."),
1838 TEST_CASE(provisioner, pb_remote_client_ncrp_second_time,
1839 "Provisioner: NCRP test, initiates NCR procedure the second time."),
1840
1841 BSTEST_END_MARKER
1842 };
1843
test_provision_pst_install(struct bst_test_list * tests)1844 struct bst_test_list *test_provision_pst_install(struct bst_test_list *tests)
1845 {
1846 tests = bst_add_tests(tests, test_connect_pst);
1847 return tests;
1848 }
1849 #endif /* IS_RPR_PRESENT */
1850