xref: /nrf52832-nimble/packages/NimBLE-latest/nimble/host/test/src/ble_hs_id_test.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include <stddef.h>
21 #include <errno.h>
22 #include <string.h>
23 #include "testutil/testutil.h"
24 #include "nimble/hci_common.h"
25 #include "host/ble_hs_adv.h"
26 #include "host/ble_hs_test.h"
27 #include "ble_hs_test_util.h"
28 
29 static int
ble_hs_id_test_util_infer_auto(int privacy,uint8_t * own_addr_type)30 ble_hs_id_test_util_infer_auto(int privacy, uint8_t *own_addr_type)
31 {
32     int rc;
33 
34     rc = ble_hs_id_infer_auto(privacy, own_addr_type);
35 
36     return rc;
37 }
38 
TEST_CASE(ble_hs_id_test_case_auto_none)39 TEST_CASE(ble_hs_id_test_case_auto_none)
40 {
41     uint8_t own_addr_type;
42     int rc;
43 
44     ble_hs_test_util_init();
45 
46     /* Clear public address. */
47     ble_hs_id_set_pub((uint8_t[6]){ 0, 0, 0, 0, 0, 0 });
48 
49     rc = ble_hs_id_test_util_infer_auto(0, &own_addr_type);
50     TEST_ASSERT_FATAL(rc == BLE_HS_ENOADDR);
51 }
52 
TEST_CASE(ble_hs_id_test_case_auto_public)53 TEST_CASE(ble_hs_id_test_case_auto_public)
54 {
55     uint8_t own_addr_type;
56     int rc;
57 
58     ble_hs_test_util_init();
59 
60     rc = ble_hs_id_test_util_infer_auto(0, &own_addr_type);
61     TEST_ASSERT_FATAL(rc == 0);
62     TEST_ASSERT(own_addr_type == BLE_OWN_ADDR_PUBLIC);
63 }
64 
TEST_CASE(ble_hs_id_test_case_auto_random)65 TEST_CASE(ble_hs_id_test_case_auto_random)
66 {
67     uint8_t own_addr_type;
68     int rc;
69 
70     ble_hs_test_util_init();
71 
72     /* Configure a random address. */
73     ble_hs_test_util_set_static_rnd_addr((uint8_t[6]){ 1, 2, 3, 4, 5, 0xc0 });
74 
75     rc = ble_hs_id_test_util_infer_auto(0, &own_addr_type);
76     TEST_ASSERT_FATAL(rc == 0);
77     TEST_ASSERT(own_addr_type == BLE_OWN_ADDR_RANDOM);
78 }
79 
TEST_CASE(ble_hs_id_test_case_auto_rpa_pub)80 TEST_CASE(ble_hs_id_test_case_auto_rpa_pub)
81 {
82     uint8_t own_addr_type;
83     int rc;
84 
85     ble_hs_test_util_init();
86 
87     rc = ble_hs_id_test_util_infer_auto(1, &own_addr_type);
88     TEST_ASSERT_FATAL(rc == 0);
89     TEST_ASSERT(own_addr_type == BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT);
90 }
91 
TEST_CASE(ble_hs_id_test_case_auto_rpa_rnd)92 TEST_CASE(ble_hs_id_test_case_auto_rpa_rnd)
93 {
94     uint8_t own_addr_type;
95     int rc;
96 
97     ble_hs_test_util_init();
98 
99     /* Configure a random address. */
100     ble_hs_test_util_set_static_rnd_addr((uint8_t[6]){ 1, 2, 3, 4, 5, 0xc0 });
101 
102     rc = ble_hs_id_test_util_infer_auto(1, &own_addr_type);
103     TEST_ASSERT_FATAL(rc == 0);
104     TEST_ASSERT(own_addr_type == BLE_OWN_ADDR_RPA_RANDOM_DEFAULT);
105 }
106 
TEST_SUITE(ble_hs_id_test_suite_auto)107 TEST_SUITE(ble_hs_id_test_suite_auto)
108 {
109     tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
110 
111     ble_hs_id_test_case_auto_none();
112     ble_hs_id_test_case_auto_public();
113     ble_hs_id_test_case_auto_random();
114     ble_hs_id_test_case_auto_rpa_pub();
115     ble_hs_id_test_case_auto_rpa_rnd();
116 }
117 
118 int
ble_hs_id_test_all(void)119 ble_hs_id_test_all(void)
120 {
121     ble_hs_id_test_suite_auto();
122 
123     return tu_any_failed;
124 }
125