1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "storage/device.h"
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include "storage/classic_device.h"
23 #include "storage/device.h"
24 #include "storage/le_device.h"
25 #include "storage/mutation.h"
26
27 using bluetooth::hci::Address;
28 using bluetooth::hci::DeviceType;
29 using bluetooth::storage::ConfigCache;
30 using bluetooth::storage::Device;
31 using bluetooth::storage::Mutation;
32 using ::testing::Eq;
33 using ::testing::MatchesRegex;
34 using ::testing::Optional;
35 using ::testing::StrEq;
36
TEST(DeviceTest,create_new_device_using_legacy_key_address)37 TEST(DeviceTest, create_new_device_using_legacy_key_address) {
38 ConfigCache config(10, Device::kLinkKeyProperties);
39 ConfigCache memory_only_config(10, {});
40
41 // A new device
42 Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
43 Device device(&config, &memory_only_config, address,
44 Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
45 ASSERT_FALSE(device.Exists());
46 ASSERT_FALSE(device.GetClassOfDevice());
47
48 // An existing device
49 Address address2 = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
50 config.SetProperty(address2.ToString(), "Name", "hello");
51 Device device2(&config, &memory_only_config, address2,
52 Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
53 ASSERT_TRUE(device2.Exists());
54 ASSERT_THAT(device2.GetName(), Optional(StrEq("hello")));
55
56 // devices with the same key address and config pointer are the same
57 Address address3 = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
58 Device device3(&config, &memory_only_config, address3,
59 Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
60 ASSERT_EQ(device2, device3);
61 ASSERT_TRUE(device3.Exists());
62 ASSERT_THAT(device3.GetName(), Optional(StrEq("hello")));
63 }
64
TEST(DeviceTest,create_new_device_using_classic_address)65 TEST(DeviceTest, create_new_device_using_classic_address) {
66 ConfigCache config(10, Device::kLinkKeyProperties);
67 ConfigCache memory_only_config(10, {});
68
69 // A new device
70 Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
71 Device device(&config, &memory_only_config, address,
72 Device::ConfigKeyAddressType::CLASSIC_ADDRESS);
73 ASSERT_FALSE(device.Exists());
74 ASSERT_FALSE(device.GetClassOfDevice());
75
76 // An existing device
77 Address address2 = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
78 config.SetProperty(address2.ToString(), "Name", "hello");
79 Device device2(&config, &memory_only_config, address2,
80 Device::ConfigKeyAddressType::CLASSIC_ADDRESS);
81 ASSERT_TRUE(device2.Exists());
82 ASSERT_THAT(device2.GetName(), Optional(StrEq("hello")));
83
84 // devices with the same key address and config pointer are the same
85 Address address3 = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
86 Device device3(&config, &memory_only_config, address3,
87 Device::ConfigKeyAddressType::CLASSIC_ADDRESS);
88 ASSERT_EQ(device2, device3);
89 ASSERT_TRUE(device3.Exists());
90 ASSERT_THAT(device3.GetName(), Optional(StrEq("hello")));
91 }
92
TEST(DeviceTest,create_new_device_using_le_identity_address)93 TEST(DeviceTest, create_new_device_using_le_identity_address) {
94 ConfigCache config(10, Device::kLinkKeyProperties);
95 ConfigCache memory_only_config(10, {});
96
97 // A new device
98 Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
99 Device device(&config, &memory_only_config, address,
100 Device::ConfigKeyAddressType::LE_IDENTITY_ADDRESS);
101 ASSERT_FALSE(device.Exists());
102 ASSERT_FALSE(device.GetClassOfDevice());
103
104 // An existing device
105 Address pseudo_first_seen_address = {{0xab, 0xcd, 0xef, 0x12, 0x34, 0x56}};
106 Address le_identity_address = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
107 // first seen address used as key
108 config.SetProperty(pseudo_first_seen_address.ToString(), "Name", "hello");
109 config.SetProperty(pseudo_first_seen_address.ToString(), "LeIdentityAddr",
110 le_identity_address.ToString());
111 config.SetProperty(address.ToString(), "Name", "world");
112 Device device2(&config, &memory_only_config, le_identity_address,
113 Device::ConfigKeyAddressType::LE_IDENTITY_ADDRESS);
114 ASSERT_TRUE(device2.Exists());
115 ASSERT_THAT(device2.GetName(), Optional(StrEq("hello")));
116 }
117
TEST(DeviceTest,set_property)118 TEST(DeviceTest, set_property) {
119 ConfigCache config(10, Device::kLinkKeyProperties);
120 ConfigCache memory_only_config(10, {});
121 Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
122 Device device(&config, &memory_only_config, address,
123 Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
124 ASSERT_FALSE(device.Exists());
125 ASSERT_FALSE(device.GetName());
126 Mutation mutation(&config, &memory_only_config);
127 mutation.Add(device.SetName("hello world!"));
128 mutation.Commit();
129 ASSERT_TRUE(device.Exists());
130 ASSERT_THAT(device.GetName(), Optional(StrEq("hello world!")));
131 }
132
TEST(DeviceTest,set_device_type)133 TEST(DeviceTest, set_device_type) {
134 ConfigCache config(10, Device::kLinkKeyProperties);
135 ConfigCache memory_only_config(10, {});
136 Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
137 Device device(&config, &memory_only_config, address,
138 Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
139 ASSERT_FALSE(device.Exists());
140 ASSERT_FALSE(device.GetName());
141 {
142 Mutation mutation(&config, &memory_only_config);
143 mutation.Add(device.SetDeviceType(DeviceType::BR_EDR));
144 mutation.Commit();
145 }
146 ASSERT_THAT(device.GetDeviceType(), Optional(Eq(DeviceType::BR_EDR)));
147 {
148 Mutation mutation(&config, &memory_only_config);
149 mutation.Add(device.SetDeviceType(DeviceType::LE));
150 mutation.Commit();
151 }
152 ASSERT_THAT(device.GetDeviceType(), Optional(Eq(DeviceType::DUAL)));
153 }
154
TEST(DeviceTest,get_le_and_bredr)155 TEST(DeviceTest, get_le_and_bredr) {
156 ConfigCache config(10, Device::kLinkKeyProperties);
157 ConfigCache memory_only_config(10, {});
158 Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
159 Device device(&config, &memory_only_config, address,
160 Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
161 ASSERT_FALSE(device.GetDeviceType());
162 ASSERT_DEATH({ device.Le(); }, MatchesRegex(".*"));
163 ASSERT_DEATH({ device.Classic(); }, MatchesRegex(".*"));
164
165 // classic
166 {
167 Mutation mutation(&config, &memory_only_config);
168 mutation.Add(device.SetDeviceType(DeviceType::BR_EDR));
169 mutation.Commit();
170 }
171 ASSERT_THAT(device.GetDeviceType(), Optional(Eq(DeviceType::BR_EDR)));
172 auto classic_device = device.Classic();
173 ASSERT_THAT(classic_device.Parent(), Eq(device));
174
175 // le
176 {
177 Mutation mutation(&config, &memory_only_config);
178 mutation.Add(device.RemoveDeviceType());
179 mutation.Commit();
180 }
181 ASSERT_FALSE(device.GetDeviceType());
182 {
183 Mutation mutation(&config, &memory_only_config);
184 mutation.Add(device.SetDeviceType(DeviceType::LE));
185 mutation.Commit();
186 }
187 ASSERT_THAT(device.GetDeviceType(), Optional(Eq(DeviceType::LE)));
188 auto le_device = device.Le();
189 ASSERT_THAT(le_device.Parent(), Eq(device));
190
191 // dual
192 {
193 Mutation mutation(&config, &memory_only_config);
194 mutation.Add(device.RemoveDeviceType());
195 mutation.Commit();
196 }
197 ASSERT_FALSE(device.GetDeviceType());
198 {
199 Mutation mutation(&config, &memory_only_config);
200 mutation.Add(device.SetDeviceType(DeviceType::DUAL));
201 mutation.Commit();
202 }
203 ASSERT_THAT(device.GetDeviceType(), Optional(Eq(DeviceType::DUAL)));
204 classic_device = device.Classic();
205 ASSERT_THAT(classic_device.Parent(), Eq(device));
206 le_device = device.Le();
207 ASSERT_THAT(le_device.Parent(), Eq(device));
208 }
209
TEST(DeviceTest,equality_test)210 TEST(DeviceTest, equality_test) {
211 ConfigCache config(10, Device::kLinkKeyProperties);
212 ConfigCache memory_only_config(10, {});
213 Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
214 Device device1(&config, &memory_only_config, address,
215 Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
216 Device device2(&config, &memory_only_config, address,
217 Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
218 ASSERT_EQ(device1, device2);
219
220 // different config cache
221 ConfigCache config_alt(10, Device::kLinkKeyProperties);
222 ConfigCache memory_only_config_alt(10, {});
223 Device device3(&config_alt, &memory_only_config_alt, address,
224 Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
225 ASSERT_NE(device1, device3);
226
227 // different address
228 Address address_alt = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x07}};
229 Device device4(&config, &memory_only_config, address_alt,
230 Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
231 ASSERT_NE(device1, device4);
232
233 Device device5 = std::move(device2);
234 ASSERT_EQ(device1, device5);
235
236 config.SetProperty(address.ToString(), "Name", "hello");
237 ASSERT_THAT(device5.GetName(), Optional(StrEq("hello")));
238 ASSERT_THAT(device1.GetName(), Optional(StrEq("hello")));
239 }
240
TEST(DeviceTest,remove_config_test)241 TEST(DeviceTest, remove_config_test) {
242 ConfigCache config(10, Device::kLinkKeyProperties);
243 ConfigCache memory_only_config(10, {});
244 Address address = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
245 config.SetProperty(address.ToString(), "Name", "hello");
246 Device device(&config, &memory_only_config, address,
247 Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
248 ASSERT_TRUE(device.Exists());
249 ASSERT_THAT(device.GetName(), Optional(StrEq("hello")));
250 Mutation mutation(&config, &memory_only_config);
251 mutation.Add(device.RemoveFromConfig());
252 mutation.Commit();
253 ASSERT_FALSE(device.Exists());
254 ASSERT_FALSE(config.GetProperty(address.ToString(), "Name"));
255 }
256
TEST(DeviceTest,operator_less_than)257 TEST(DeviceTest, operator_less_than) {
258 ConfigCache config1(10, Device::kLinkKeyProperties);
259 ConfigCache config2(10, Device::kLinkKeyProperties);
260 ASSERT_NE(&config1, &config2);
261 ConfigCache* smaller_config_ptr = &config1;
262 ConfigCache* larger_config_ptr = &config2;
263 if (&config2 < &config1) {
264 smaller_config_ptr = &config2;
265 larger_config_ptr = &config1;
266 }
267
268 ConfigCache memory_only_config1(10, {});
269 ConfigCache memory_only_config2(10, {});
270 ASSERT_NE(&memory_only_config1, &memory_only_config2);
271 ConfigCache* smaller_memory_only_config_ptr = &memory_only_config1;
272 ConfigCache* larger_memory_only_config_ptr = &memory_only_config2;
273 if (&memory_only_config2 < &memory_only_config1) {
274 smaller_memory_only_config_ptr = &memory_only_config2;
275 larger_memory_only_config_ptr = &memory_only_config1;
276 }
277
278 bluetooth::hci::Address smaller_address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
279 bluetooth::hci::Address larger_address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x07}};
280
281 {
282 Device device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
283 Device device2(larger_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
284 ASSERT_TRUE(device1 < device2);
285 }
286
287 {
288 Device device1(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
289 Device device2(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
290 ASSERT_FALSE(device1 < device2);
291 }
292
293 {
294 Device device1(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
295 Device device2(larger_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
296 ASSERT_TRUE(device1 < device2);
297 }
298
299 {
300 Device device1(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
301 Device device2(larger_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
302 ASSERT_TRUE(device1 < device2);
303 }
304
305 {
306 Device device1(larger_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
307 Device device2(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
308 ASSERT_FALSE(device1 < device2);
309 }
310
311 {
312 Device device1(larger_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
313 Device device2(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
314 ASSERT_FALSE(device1 < device2);
315 }
316
317 {
318 Device device1(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
319 Device device2(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
320 ASSERT_TRUE(device1 < device2);
321 }
322
323 {
324 Device device1(larger_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
325 Device device2(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
326 ASSERT_FALSE(device1 < device2);
327 }
328
329 {
330 Device device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
331 Device device2(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
332 ASSERT_TRUE(device1 < device2);
333 }
334
335 {
336 Device device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
337 Device device2(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
338 ASSERT_TRUE(device1 < device2);
339 }
340
341 {
342 Device device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
343 Device device2(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
344 ASSERT_TRUE(device1 < device2);
345 }
346
347 {
348 Device device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
349 Device device2(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
350 ASSERT_TRUE(device1 < device2);
351 }
352 }
353