aboutsummaryrefslogtreecommitdiff
path: root/system/gd/storage/device_test.cc
blob: 307f403e9771410822e19ded93d09ae0d9b49e78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "storage/device.h"

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "storage/classic_device.h"
#include "storage/device.h"
#include "storage/le_device.h"
#include "storage/mutation.h"

using bluetooth::hci::Address;
using bluetooth::hci::DeviceType;
using bluetooth::storage::ConfigCache;
using bluetooth::storage::Device;
using bluetooth::storage::Mutation;
using ::testing::Eq;
using ::testing::MatchesRegex;
using ::testing::Optional;
using ::testing::StrEq;

TEST(DeviceTest, create_new_device_using_legacy_key_address) {
  ConfigCache config(10, Device::kLinkKeyProperties);
  ConfigCache memory_only_config(10, {});

  // A new device
  Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  Device device(&config, &memory_only_config, address, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
  ASSERT_FALSE(device.Exists());
  ASSERT_FALSE(device.GetClassOfDevice());

  // An existing device
  Address address2 = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
  config.SetProperty(address2.ToString(), "Name", "hello");
  Device device2(&config, &memory_only_config, address2, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
  ASSERT_TRUE(device2.Exists());
  ASSERT_THAT(device2.GetName(), Optional(StrEq("hello")));

  // devices with the same key address and config pointer are the same
  Address address3 = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
  Device device3(&config, &memory_only_config, address3, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
  ASSERT_EQ(device2, device3);
  ASSERT_TRUE(device3.Exists());
  ASSERT_THAT(device3.GetName(), Optional(StrEq("hello")));
}

TEST(DeviceTest, create_new_device_using_classic_address) {
  ConfigCache config(10, Device::kLinkKeyProperties);
  ConfigCache memory_only_config(10, {});

  // A new device
  Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  Device device(&config, &memory_only_config, address, Device::ConfigKeyAddressType::CLASSIC_ADDRESS);
  ASSERT_FALSE(device.Exists());
  ASSERT_FALSE(device.GetClassOfDevice());

  // An existing device
  Address address2 = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
  config.SetProperty(address2.ToString(), "Name", "hello");
  Device device2(&config, &memory_only_config, address2, Device::ConfigKeyAddressType::CLASSIC_ADDRESS);
  ASSERT_TRUE(device2.Exists());
  ASSERT_THAT(device2.GetName(), Optional(StrEq("hello")));

  // devices with the same key address and config pointer are the same
  Address address3 = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
  Device device3(&config, &memory_only_config, address3, Device::ConfigKeyAddressType::CLASSIC_ADDRESS);
  ASSERT_EQ(device2, device3);
  ASSERT_TRUE(device3.Exists());
  ASSERT_THAT(device3.GetName(), Optional(StrEq("hello")));
}

TEST(DeviceTest, create_new_device_using_le_identity_address) {
  ConfigCache config(10, Device::kLinkKeyProperties);
  ConfigCache memory_only_config(10, {});

  // A new device
  Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  Device device(&config, &memory_only_config, address, Device::ConfigKeyAddressType::LE_IDENTITY_ADDRESS);
  ASSERT_FALSE(device.Exists());
  ASSERT_FALSE(device.GetClassOfDevice());

  // An existing device
  Address pseudo_first_seen_address = {{0xab, 0xcd, 0xef, 0x12, 0x34, 0x56}};
  Address le_identity_address = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
  // first seen address used as key
  config.SetProperty(pseudo_first_seen_address.ToString(), "Name", "hello");
  config.SetProperty(pseudo_first_seen_address.ToString(), "LeIdentityAddr", le_identity_address.ToString());
  config.SetProperty(address.ToString(), "Name", "world");
  Device device2(&config, &memory_only_config, le_identity_address, Device::ConfigKeyAddressType::LE_IDENTITY_ADDRESS);
  ASSERT_TRUE(device2.Exists());
  ASSERT_THAT(device2.GetName(), Optional(StrEq("hello")));
}

TEST(DeviceTest, set_property) {
  ConfigCache config(10, Device::kLinkKeyProperties);
  ConfigCache memory_only_config(10, {});
  Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  Device device(&config, &memory_only_config, address, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
  ASSERT_FALSE(device.Exists());
  ASSERT_FALSE(device.GetName());
  Mutation mutation(&config, &memory_only_config);
  mutation.Add(device.SetName("hello world!"));
  mutation.Commit();
  ASSERT_TRUE(device.Exists());
  ASSERT_THAT(device.GetName(), Optional(StrEq("hello world!")));
}

TEST(DeviceTest, set_device_type) {
  ConfigCache config(10, Device::kLinkKeyProperties);
  ConfigCache memory_only_config(10, {});
  Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  Device device(&config, &memory_only_config, address, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
  ASSERT_FALSE(device.Exists());
  ASSERT_FALSE(device.GetName());
  {
    Mutation mutation(&config, &memory_only_config);
    mutation.Add(device.SetDeviceType(DeviceType::BR_EDR));
    mutation.Commit();
  }
  ASSERT_THAT(device.GetDeviceType(), Optional(Eq(DeviceType::BR_EDR)));
  {
    Mutation mutation(&config, &memory_only_config);
    mutation.Add(device.SetDeviceType(DeviceType::LE));
    mutation.Commit();
  }
  ASSERT_THAT(device.GetDeviceType(), Optional(Eq(DeviceType::DUAL)));
}

TEST(DeviceTest, get_le_and_bredr) {
  ConfigCache config(10, Device::kLinkKeyProperties);
  ConfigCache memory_only_config(10, {});
  Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  Device device(&config, &memory_only_config, address, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
  ASSERT_FALSE(device.GetDeviceType());
  ASSERT_DEATH({ device.Le(); }, MatchesRegex(".*"));
  ASSERT_DEATH({ device.Classic(); }, MatchesRegex(".*"));

  // classic
  {
    Mutation mutation(&config, &memory_only_config);
    mutation.Add(device.SetDeviceType(DeviceType::BR_EDR));
    mutation.Commit();
  }
  ASSERT_THAT(device.GetDeviceType(), Optional(Eq(DeviceType::BR_EDR)));
  auto classic_device = device.Classic();
  ASSERT_THAT(classic_device.Parent(), Eq(device));

  // le
  {
    Mutation mutation(&config, &memory_only_config);
    mutation.Add(device.RemoveDeviceType());
    mutation.Commit();
  }
  ASSERT_FALSE(device.GetDeviceType());
  {
    Mutation mutation(&config, &memory_only_config);
    mutation.Add(device.SetDeviceType(DeviceType::LE));
    mutation.Commit();
  }
  ASSERT_THAT(device.GetDeviceType(), Optional(Eq(DeviceType::LE)));
  auto le_device = device.Le();
  ASSERT_THAT(le_device.Parent(), Eq(device));

  // dual
  {
    Mutation mutation(&config, &memory_only_config);
    mutation.Add(device.RemoveDeviceType());
    mutation.Commit();
  }
  ASSERT_FALSE(device.GetDeviceType());
  {
    Mutation mutation(&config, &memory_only_config);
    mutation.Add(device.SetDeviceType(DeviceType::DUAL));
    mutation.Commit();
  }
  ASSERT_THAT(device.GetDeviceType(), Optional(Eq(DeviceType::DUAL)));
  classic_device = device.Classic();
  ASSERT_THAT(classic_device.Parent(), Eq(device));
  le_device = device.Le();
  ASSERT_THAT(le_device.Parent(), Eq(device));
}

TEST(DeviceTest, equality_test) {
  ConfigCache config(10, Device::kLinkKeyProperties);
  ConfigCache memory_only_config(10, {});
  Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  Device device1(&config, &memory_only_config, address, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
  Device device2(&config, &memory_only_config, address, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
  ASSERT_EQ(device1, device2);

  // different config cache
  ConfigCache config_alt(10, Device::kLinkKeyProperties);
  ConfigCache memory_only_config_alt(10, {});
  Device device3(&config_alt, &memory_only_config_alt, address, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
  ASSERT_NE(device1, device3);

  // different address
  Address address_alt = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x07}};
  Device device4(&config, &memory_only_config, address_alt, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
  ASSERT_NE(device1, device4);

  Device device5 = std::move(device2);
  ASSERT_EQ(device1, device5);

  config.SetProperty(address.ToString(), "Name", "hello");
  ASSERT_THAT(device5.GetName(), Optional(StrEq("hello")));
  ASSERT_THAT(device1.GetName(), Optional(StrEq("hello")));
}

TEST(DeviceTest, remove_config_test) {
  ConfigCache config(10, Device::kLinkKeyProperties);
  ConfigCache memory_only_config(10, {});
  Address address = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
  config.SetProperty(address.ToString(), "Name", "hello");
  Device device(&config, &memory_only_config, address, Device::ConfigKeyAddressType::LEGACY_KEY_ADDRESS);
  ASSERT_TRUE(device.Exists());
  ASSERT_THAT(device.GetName(), Optional(StrEq("hello")));
  Mutation mutation(&config, &memory_only_config);
  mutation.Add(device.RemoveFromConfig());
  mutation.Commit();
  ASSERT_FALSE(device.Exists());
  ASSERT_FALSE(config.GetProperty(address.ToString(), "Name"));
}

TEST(DeviceTest, operator_less_than) {
  ConfigCache config1(10, Device::kLinkKeyProperties);
  ConfigCache config2(10, Device::kLinkKeyProperties);
  ASSERT_NE(&config1, &config2);
  ConfigCache* smaller_config_ptr = &config1;
  ConfigCache* larger_config_ptr = &config2;
  if (&config2 < &config1) {
    smaller_config_ptr = &config2;
    larger_config_ptr = &config1;
  }

  ConfigCache memory_only_config1(10, {});
  ConfigCache memory_only_config2(10, {});
  ASSERT_NE(&memory_only_config1, &memory_only_config2);
  ConfigCache* smaller_memory_only_config_ptr = &memory_only_config1;
  ConfigCache* larger_memory_only_config_ptr = &memory_only_config2;
  if (&memory_only_config2 < &memory_only_config1) {
    smaller_memory_only_config_ptr = &memory_only_config2;
    larger_memory_only_config_ptr = &memory_only_config1;
  }

  bluetooth::hci::Address smaller_address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  bluetooth::hci::Address larger_address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x07}};

  {
    Device device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    Device device2(larger_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }

  {
    Device device1(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    Device device2(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
    ASSERT_FALSE(device1 < device2);
  }

  {
    Device device1(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
    Device device2(larger_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }

  {
    Device device1(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
    Device device2(larger_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }

  {
    Device device1(larger_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
    Device device2(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
    ASSERT_FALSE(device1 < device2);
  }

  {
    Device device1(larger_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
    Device device2(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_FALSE(device1 < device2);
  }

  {
    Device device1(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
    Device device2(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }

  {
    Device device1(larger_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
    Device device2(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_FALSE(device1 < device2);
  }

  {
    Device device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    Device device2(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }

  {
    Device device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    Device device2(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }

  {
    Device device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    Device device2(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }

  {
    Device device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    Device device2(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }
}