1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "testing_common_defs.h"
8 
9 #include <zephyr/bluetooth/hci.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/ztest.h>
12 
13 #include <host/hci_core.h>
14 #include <host/id.h>
15 
16 ZTEST_SUITE(bt_id_create_invalid_inputs, NULL, NULL, NULL, NULL, NULL);
17 
18 /*
19  *  Test invalid input arguments to bt_id_create() using NULLs for address and IRK parameters.
20  *
21  *  Constraints:
22  *   - Input address is NULL
23  *   - Input IRK is NULL
24  *
25  *  Expected behaviour:
26  *   - '-EINVAL' error code is returned representing invalid values were used.
27  */
ZTEST(bt_id_create_invalid_inputs,test_null_addr_null_irk)28 ZTEST(bt_id_create_invalid_inputs, test_null_addr_null_irk)
29 {
30 	int err;
31 
32 	err = bt_id_create(NULL, NULL);
33 
34 	zassert_true(err == -EINVAL, "Unexpected error code '%d' was returned", err);
35 }
36 
37 /*
38  *  Test invalid input arguments to bt_id_create() using NULL for the address parameter
39  *  while the IRK parameter is a valid pointer.
40  *
41  *  Constraints:
42  *   - Input address is NULL
43  *   - Input IRK isn't NULL
44  *
45  *  Expected behaviour:
46  *   - '-EINVAL' error code is returned representing invalid values were used.
47  */
ZTEST(bt_id_create_invalid_inputs,test_null_addr_valid_irk_no_privacy_enabled)48 ZTEST(bt_id_create_invalid_inputs, test_null_addr_valid_irk_no_privacy_enabled)
49 {
50 	int err;
51 	uint8_t valid_irk_ptr[16];
52 
53 	err = bt_id_create(NULL, valid_irk_ptr);
54 
55 	zassert_true(err == -EINVAL, "Unexpected error code '%d' was returned", err);
56 }
57 
58 /*
59  *  Test invalid input arguments to bt_id_create() using NULLs for address and IRK parameters
60  *  while the identity list is full.
61  *
62  *  Constraints:
63  *   - Input address is NULL
64  *   - Input IRK is NULL
65  *   - Identity list is full
66  *
67  *  Expected behaviour:
68  *   - '-ENOMEM' error code is returned representing invalid values were used.
69  */
ZTEST(bt_id_create_invalid_inputs,test_id_list_is_full)70 ZTEST(bt_id_create_invalid_inputs, test_id_list_is_full)
71 {
72 	int err;
73 
74 	bt_dev.id_count = ARRAY_SIZE(bt_dev.id_addr);
75 
76 	err = bt_id_create(NULL, NULL);
77 
78 	zassert_true(err == -ENOMEM, "Unexpected error code '%d' was returned", err);
79 }
80 
81 /*
82  *  Test invalid input arguments to bt_id_create() by using a valid address of type public and using
83  *  NULL value for the IRK.
84  *
85  *  Constraints:
86  *   - A valid address of type public is used
87  *   - Input IRK is NULL
88  *
89  *  Expected behaviour:
90  *   - '-EINVAL' error code is returned representing invalid values were used.
91  */
ZTEST(bt_id_create_invalid_inputs,test_public_address)92 ZTEST(bt_id_create_invalid_inputs, test_public_address)
93 {
94 	int err;
95 
96 	if (IS_ENABLED(CONFIG_BT_HCI_SET_PUBLIC_ADDR)) {
97 		ztest_test_skip();
98 	}
99 
100 	err = bt_id_create(BT_LE_ADDR, NULL);
101 
102 	zassert_true(err == -EINVAL, "Unexpected error code '%d' was returned", err);
103 }
104 
105 /*
106  *  Test invalid input arguments to bt_id_create() by using a valid address of type RPA and using
107  *  NULL value for the IRK.
108  *
109  *  Constraints:
110  *   - An RPA address of type random is used
111  *   - Input IRK is NULL
112  *
113  *  Expected behaviour:
114  *   - '-EINVAL' error code is returned representing invalid values were used.
115  */
ZTEST(bt_id_create_invalid_inputs,test_rpa_address)116 ZTEST(bt_id_create_invalid_inputs, test_rpa_address)
117 {
118 	int err;
119 
120 	err = bt_id_create(BT_RPA_LE_ADDR, NULL);
121 
122 	zassert_true(err == -EINVAL, "Unexpected error code '%d' was returned", err);
123 }
124 
125 /*
126  *  Test invalid input arguments to bt_id_create() by using an address that already exists
127  *  in the identity list.
128  *
129  *  Constraints:
130  *   - A valid random static address is used
131  *   - Input address already exists in the identity list
132  *   - Input IRK is NULL
133  *
134  *  Expected behaviour:
135  *   - '-EALREADY' error code is returned representing invalid values were used.
136  */
ZTEST(bt_id_create_invalid_inputs,test_pa_address_exists_in_id_list)137 ZTEST(bt_id_create_invalid_inputs, test_pa_address_exists_in_id_list)
138 {
139 	int err;
140 
141 	bt_dev.id_count = 1;
142 	bt_addr_le_copy(&bt_dev.id_addr[0], BT_STATIC_RANDOM_LE_ADDR_1);
143 
144 	err = bt_id_create(BT_STATIC_RANDOM_LE_ADDR_1, NULL);
145 
146 	zassert_true(err == -EALREADY, "Unexpected error code '%d' was returned", err);
147 }
148 
149 /*
150  *  Test invalid input arguments to bt_id_create() by using a valid static random address and
151  *  a valid pointer to an IRK that's filled with zeros.
152  *
153  *  Constraints:
154  *   - A static random address is used
155  *   - Input IRK is filled with zeros
156  *
157  *  Expected behaviour:
158  *   - '-EINVAL' error code is returned representing invalid values were used.
159  */
ZTEST(bt_id_create_invalid_inputs,test_zero_irk_with_privacy)160 ZTEST(bt_id_create_invalid_inputs, test_zero_irk_with_privacy)
161 {
162 	int err;
163 	uint8_t zero_irk[16] = {0};
164 
165 	err = bt_id_create(BT_STATIC_RANDOM_LE_ADDR_1, zero_irk);
166 
167 	zassert_true(err == -EINVAL, "Unexpected error code '%d' was returned", err);
168 }
169