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, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
44   ASSERT_FALSE(device.Exists());
45   ASSERT_FALSE(device.GetClassOfDevice());
46 
47   // An existing device
48   Address address2 = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
49   config.SetProperty(address2.ToString(), "Name", "hello");
50   Device device2(&config, &memory_only_config, address2, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
51   ASSERT_TRUE(device2.Exists());
52   ASSERT_THAT(device2.GetName(), Optional(StrEq("hello")));
53 
54   // devices with the same key address and config pointer are the same
55   Address address3 = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
56   Device device3(&config, &memory_only_config, address3, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
57   ASSERT_EQ(device2, device3);
58   ASSERT_TRUE(device3.Exists());
59   ASSERT_THAT(device3.GetName(), Optional(StrEq("hello")));
60 }
61 
TEST(DeviceTest,create_new_device_using_classic_address)62 TEST(DeviceTest, create_new_device_using_classic_address) {
63   ConfigCache config(10, Device::kLinkKeyProperties);
64   ConfigCache memory_only_config(10, {});
65 
66   // A new device
67   Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
68   Device device(&config, &memory_only_config, address, Device::ConfigKeyAddressType::CLASSIC_ADDRESS);
69   ASSERT_FALSE(device.Exists());
70   ASSERT_FALSE(device.GetClassOfDevice());
71 
72   // An existing device
73   Address address2 = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
74   config.SetProperty(address2.ToString(), "Name", "hello");
75   Device device2(&config, &memory_only_config, address2, Device::ConfigKeyAddressType::CLASSIC_ADDRESS);
76   ASSERT_TRUE(device2.Exists());
77   ASSERT_THAT(device2.GetName(), Optional(StrEq("hello")));
78 
79   // devices with the same key address and config pointer are the same
80   Address address3 = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
81   Device device3(&config, &memory_only_config, address3, Device::ConfigKeyAddressType::CLASSIC_ADDRESS);
82   ASSERT_EQ(device2, device3);
83   ASSERT_TRUE(device3.Exists());
84   ASSERT_THAT(device3.GetName(), Optional(StrEq("hello")));
85 }
86 
TEST(DeviceTest,create_new_device_using_le_identity_address)87 TEST(DeviceTest, create_new_device_using_le_identity_address) {
88   ConfigCache config(10, Device::kLinkKeyProperties);
89   ConfigCache memory_only_config(10, {});
90 
91   // A new device
92   Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
93   Device device(&config, &memory_only_config, address, Device::ConfigKeyAddressType::LE_IDENTITY_ADDRESS);
94   ASSERT_FALSE(device.Exists());
95   ASSERT_FALSE(device.GetClassOfDevice());
96 
97   // An existing device
98   Address pseudo_first_seen_address = {{0xab, 0xcd, 0xef, 0x12, 0x34, 0x56}};
99   Address le_identity_address = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
100   // first seen address used as key
101   config.SetProperty(pseudo_first_seen_address.ToString(), "Name", "hello");
102   config.SetProperty(pseudo_first_seen_address.ToString(), "LeIdentityAddr", le_identity_address.ToString());
103   config.SetProperty(address.ToString(), "Name", "world");
104   Device device2(&config, &memory_only_config, le_identity_address, Device::ConfigKeyAddressType::LE_IDENTITY_ADDRESS);
105   ASSERT_TRUE(device2.Exists());
106   ASSERT_THAT(device2.GetName(), Optional(StrEq("hello")));
107 }
108 
TEST(DeviceTest,set_property)109 TEST(DeviceTest, set_property) {
110   ConfigCache config(10, Device::kLinkKeyProperties);
111   ConfigCache memory_only_config(10, {});
112   Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
113   Device device(&config, &memory_only_config, address, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
114   ASSERT_FALSE(device.Exists());
115   ASSERT_FALSE(device.GetName());
116   Mutation mutation(&config, &memory_only_config);
117   mutation.Add(device.SetName("hello world!"));
118   mutation.Commit();
119   ASSERT_TRUE(device.Exists());
120   ASSERT_THAT(device.GetName(), Optional(StrEq("hello world!")));
121 }
122 
TEST(DeviceTest,set_device_type)123 TEST(DeviceTest, set_device_type) {
124   ConfigCache config(10, Device::kLinkKeyProperties);
125   ConfigCache memory_only_config(10, {});
126   Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
127   Device device(&config, &memory_only_config, address, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
128   ASSERT_FALSE(device.Exists());
129   ASSERT_FALSE(device.GetName());
130   {
131     Mutation mutation(&config, &memory_only_config);
132     mutation.Add(device.SetDeviceType(DeviceType::BR_EDR));
133     mutation.Commit();
134   }
135   ASSERT_THAT(device.GetDeviceType(), Optional(Eq(DeviceType::BR_EDR)));
136   {
137     Mutation mutation(&config, &memory_only_config);
138     mutation.Add(device.SetDeviceType(DeviceType::LE));
139     mutation.Commit();
140   }
141   ASSERT_THAT(device.GetDeviceType(), Optional(Eq(DeviceType::DUAL)));
142 }
143 
TEST(DeviceTest,get_le_and_bredr)144 TEST(DeviceTest, get_le_and_bredr) {
145   ConfigCache config(10, Device::kLinkKeyProperties);
146   ConfigCache memory_only_config(10, {});
147   Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
148   Device device(&config, &memory_only_config, address, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
149   ASSERT_FALSE(device.GetDeviceType());
150   ASSERT_DEATH({ device.Le(); }, MatchesRegex(".*"));
151   ASSERT_DEATH({ device.Classic(); }, MatchesRegex(".*"));
152 
153   // classic
154   {
155     Mutation mutation(&config, &memory_only_config);
156     mutation.Add(device.SetDeviceType(DeviceType::BR_EDR));
157     mutation.Commit();
158   }
159   ASSERT_THAT(device.GetDeviceType(), Optional(Eq(DeviceType::BR_EDR)));
160   auto classic_device = device.Classic();
161   ASSERT_THAT(classic_device.Parent(), Eq(device));
162 
163   // le
164   {
165     Mutation mutation(&config, &memory_only_config);
166     mutation.Add(device.RemoveDeviceType());
167     mutation.Commit();
168   }
169   ASSERT_FALSE(device.GetDeviceType());
170   {
171     Mutation mutation(&config, &memory_only_config);
172     mutation.Add(device.SetDeviceType(DeviceType::LE));
173     mutation.Commit();
174   }
175   ASSERT_THAT(device.GetDeviceType(), Optional(Eq(DeviceType::LE)));
176   auto le_device = device.Le();
177   ASSERT_THAT(le_device.Parent(), Eq(device));
178 
179   // dual
180   {
181     Mutation mutation(&config, &memory_only_config);
182     mutation.Add(device.RemoveDeviceType());
183     mutation.Commit();
184   }
185   ASSERT_FALSE(device.GetDeviceType());
186   {
187     Mutation mutation(&config, &memory_only_config);
188     mutation.Add(device.SetDeviceType(DeviceType::DUAL));
189     mutation.Commit();
190   }
191   ASSERT_THAT(device.GetDeviceType(), Optional(Eq(DeviceType::DUAL)));
192   classic_device = device.Classic();
193   ASSERT_THAT(classic_device.Parent(), Eq(device));
194   le_device = device.Le();
195   ASSERT_THAT(le_device.Parent(), Eq(device));
196 }
197 
TEST(DeviceTest,equality_test)198 TEST(DeviceTest, equality_test) {
199   ConfigCache config(10, Device::kLinkKeyProperties);
200   ConfigCache memory_only_config(10, {});
201   Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
202   Device device1(&config, &memory_only_config, address, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
203   Device device2(&config, &memory_only_config, address, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
204   ASSERT_EQ(device1, device2);
205 
206   // different config cache
207   ConfigCache config_alt(10, Device::kLinkKeyProperties);
208   ConfigCache memory_only_config_alt(10, {});
209   Device device3(&config_alt, &memory_only_config_alt, address, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
210   ASSERT_NE(device1, device3);
211 
212   // different address
213   Address address_alt = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x07}};
214   Device device4(&config, &memory_only_config, address_alt, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
215   ASSERT_NE(device1, device4);
216 
217   Device device5 = std::move(device2);
218   ASSERT_EQ(device1, device5);
219 
220   config.SetProperty(address.ToString(), "Name", "hello");
221   ASSERT_THAT(device5.GetName(), Optional(StrEq("hello")));
222   ASSERT_THAT(device1.GetName(), Optional(StrEq("hello")));
223 }
224 
TEST(DeviceTest,remove_config_test)225 TEST(DeviceTest, remove_config_test) {
226   ConfigCache config(10, Device::kLinkKeyProperties);
227   ConfigCache memory_only_config(10, {});
228   Address address = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
229   config.SetProperty(address.ToString(), "Name", "hello");
230   Device device(&config, &memory_only_config, address, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
231   ASSERT_TRUE(device.Exists());
232   ASSERT_THAT(device.GetName(), Optional(StrEq("hello")));
233   Mutation mutation(&config, &memory_only_config);
234   mutation.Add(device.RemoveFromConfig());
235   mutation.Commit();
236   ASSERT_FALSE(device.Exists());
237   ASSERT_FALSE(config.GetProperty(address.ToString(), "Name"));
238 }
239 
240