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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
/*
* 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 "btif/include/btif_config_cache.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <filesystem>
#include "stack/include/bt_octets.h"
namespace {
const int kCapacity = 3;
const int kTestRepeatCount = 30;
const std::string kBtAddr1 = "11:22:33:44:55:66";
const std::string kBtAddr2 = "AA:BB:CC:DD:EE:FF";
const std::string kBtAddr3 = "AB:CD:EF:12:34:56";
const std::string kBtAddr4 = "11:AA:22:BB:33:CC";
const std::string kBtAddr5 = "11:AA:22:BB:33:CD";
const std::string kBtLocalAddr = "12:34:56:78:90:AB";
const std::string kBtInfo = "Info";
const std::string kBtMetrics = "Metrics";
const std::string kBtAdapter = "Adapter";
const std::string kBtAddrInvalid1 = "AB:CD:EF:12:34";
const std::string kBtAddrInvalid2 = "AB:CD:EF:12:34:56:78";
const std::string kBtAddrInvalid3 = "ABCDEF123456";
const std::string kBtAddrInvalid4 = "AB-CD-EF-12-34-56";
const std::string kBtSectionInvalid1 = "Invalid Section";
const std::filesystem::path kTestConfigFile =
std::filesystem::temp_directory_path() / "config_cache_test.conf";
const char* TEST_CONFIG_FILE = kTestConfigFile.c_str();
} // namespace
namespace testing {
/* Test to basic btif_config_cache set up
* 1. when received Local device sections information, the sections can be put
* into btif config cache
* 2. the device sections and key-value will be set to Btif config cache when
* receiving different device sections
* 3. limit the capacity of unpacire devices cache to 3, test the oldest device
* section will be ruled out when receiveing 4 different device sections.
*/
TEST(BtifConfigCacheTest, test_setup_btif_config_cache) {
BtifConfigCache test_btif_config_cache(kCapacity);
// Info section
test_btif_config_cache.SetString(kBtInfo, "FileSource", "");
test_btif_config_cache.SetString(kBtInfo, "TimeCreated",
"2020-06-05 12:12:12");
// Metrics section
test_btif_config_cache.SetString(kBtMetrics, "Salt256Bit",
"92a331174d20f2bb");
// Adapter Section
test_btif_config_cache.SetString(kBtAdapter, "Address", kBtLocalAddr);
EXPECT_TRUE(test_btif_config_cache.HasSection(kBtAdapter));
// bt_device_1
test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Name"));
test_btif_config_cache.SetInt(kBtAddr1, "Property_Int", 1);
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Property_Int"));
// bt_device_2
test_btif_config_cache.SetString(kBtAddr2, "Name", "Headset_2");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr2, "Name"));
// bt_device_3
test_btif_config_cache.SetString(kBtAddr3, "Name", "Headset_3");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr3, "Name"));
// bt_device_4
test_btif_config_cache.SetString(kBtAddr4, "Name", "Headset_4");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr4, "Name"));
// out out the capacty of unpair devices cache, the bt_device_1 be ruled out
EXPECT_FALSE(test_btif_config_cache.HasSection(kBtAddr1));
EXPECT_TRUE(test_btif_config_cache.HasSection(kBtAddr2));
EXPECT_TRUE(test_btif_config_cache.HasSection(kBtAddr3));
EXPECT_TRUE(test_btif_config_cache.HasSection(kBtAddr4));
}
/* Test to set up btif_config_cache with invalid bt address or section name
* when received Invalid bt address or section, it's not allowed to put invalid
* section to paired devices list section
*/
TEST(BtifConfigCacheTest, test_set_up_config_cache_with_invalid_section) {
BtifConfigCache test_btif_config_cache(kCapacity);
// kBtAddrInvalid1
test_btif_config_cache.SetString(kBtAddrInvalid1, "Name", "Headset_1");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid1, "Name"));
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid1));
// get the LinkKey
test_btif_config_cache.SetString(kBtAddrInvalid1, "LinkKey",
"1122334455667788");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid1, "LinkKey"));
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid1));
EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddrInvalid1));
// kBtAddrInvalid2
test_btif_config_cache.SetString(kBtAddrInvalid2, "Name", "Headset_1");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid2, "Name"));
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid2));
// get the LinkKey
test_btif_config_cache.SetString(kBtAddrInvalid2, "LinkKey",
"1122334455667788");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid2, "LinkKey"));
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid2));
EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddrInvalid2));
// kBtAddrInvalid3
test_btif_config_cache.SetString(kBtAddrInvalid3, "Name", "Headset_1");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid3, "Name"));
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid3));
// get the LinkKey
test_btif_config_cache.SetString(kBtAddrInvalid3, "LinkKey",
"1122334455667788");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid3, "LinkKey"));
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid3));
EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddrInvalid3));
// kBtAddrInvalid4
test_btif_config_cache.SetString(kBtAddrInvalid4, "Name", "Headset_1");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid4, "Name"));
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid4));
// get the LinkKey
test_btif_config_cache.SetString(kBtAddrInvalid4, "LinkKey",
"1122334455667788");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid4, "LinkKey"));
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid4));
EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddrInvalid4));
// kBtSectionInvalid1
test_btif_config_cache.SetString(kBtSectionInvalid1, "Name", "Headset_1");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtSectionInvalid1, "Name"));
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtSectionInvalid1));
// get the LinkKey
test_btif_config_cache.SetString(kBtSectionInvalid1, "LinkKey",
"1122334455667788");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtSectionInvalid1, "LinkKey"));
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtSectionInvalid1));
EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtSectionInvalid1));
}
/* Stress test to set and get key values
* 1. stress test to set different type key value to unpaired device cache and
* get the different type key values in the unpaired cache section to check if
* we get the key-values the same with we set in unpaired device cache.
* 2. stress test to set different type key value to paired device section and
* get the different type key values in the paired cache section to check if we
* get the key-values the same with we set in paired device cache.
*/
TEST(BtifConfigCacheTest, test_get_set_key_value_test) {
BtifConfigCache test_btif_config_cache(kCapacity);
// test in unpaired cache
test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Name"));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
Optional(StrEq("Headset_1")));
test_btif_config_cache.SetInt(kBtAddr1, "Property_Int", 65536);
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Property_Int"));
EXPECT_THAT(test_btif_config_cache.GetInt(kBtAddr1, "Property_Int"),
Optional(Eq(65536)));
test_btif_config_cache.SetUint64(kBtAddr1, "Property_64", 4294967296);
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Property_64"));
EXPECT_THAT(test_btif_config_cache.GetUint64(kBtAddr1, "Property_64"),
Optional(Eq(uint64_t(4294967296))));
test_btif_config_cache.SetBool(kBtAddr1, "Property_Bool", true);
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Property_Bool"));
EXPECT_THAT(test_btif_config_cache.GetBool(kBtAddr1, "Property_Bool"),
Optional(IsTrue()));
// empty value
test_btif_config_cache.SetString(kBtAddr1, "Name", "");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Name"));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
Optional(StrEq("")));
// get the LinkKey
test_btif_config_cache.SetString(kBtAddr1, "LinkKey", "1122334455667788");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "LinkKey"));
EXPECT_FALSE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
// test in unpaired cache
test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Name"));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
Optional(StrEq("Headset_1")));
test_btif_config_cache.SetInt(kBtAddr1, "Property_Int", 65536);
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Property_Int"));
EXPECT_THAT(test_btif_config_cache.GetInt(kBtAddr1, "Property_Int"),
Optional(Eq(65536)));
test_btif_config_cache.SetUint64(kBtAddr1, "Property_64", 4294967296);
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Property_64"));
EXPECT_THAT(test_btif_config_cache.GetUint64(kBtAddr1, "Property_64"),
Optional(Eq(uint64_t(4294967296))));
test_btif_config_cache.SetBool(kBtAddr1, "Property_Bool", true);
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Property_Bool"));
EXPECT_THAT(test_btif_config_cache.GetBool(kBtAddr1, "Property_Bool"),
Optional(IsTrue()));
// empty section is disallowed
EXPECT_DEATH({ test_btif_config_cache.SetString("", "name", "Headset_1"); },
"Empty section not allowed");
// empty key is disallowed
EXPECT_DEATH({ test_btif_config_cache.SetString(kBtAddr1, "", "Headset_1"); },
"Empty key not allowed");
EXPECT_FALSE(test_btif_config_cache.HasKey(kBtAddr1, ""));
// empty value is allowed
test_btif_config_cache.SetString(kBtAddr1, "Name", "");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Name"));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
Optional(StrEq("")));
}
/* Test to set values in the same key
* Receiving the same key with different values in a section, the new incoming
* value will be updated but the key will not be added repeatedly. test this
* feature in both unpaired devic cache and paired device list cache
*/
TEST(BtifConfigCacheTest, test_set_values_in_the_same_key) {
BtifConfigCache test_btif_config_cache(kCapacity);
// add new a key "Name"
test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1");
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
Optional(StrEq("Headset_1")));
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
// add the same key "Name" with different value
test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1A");
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
Optional(StrEq("Headset_1A")));
// add the same key "Name" with different value
test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_2A");
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
Optional(StrEq("Headset_2A")));
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
// add new a key "Property_Int"
test_btif_config_cache.SetInt(kBtAddr1, "Property_Int", 65536);
EXPECT_THAT(test_btif_config_cache.GetInt(kBtAddr1, "Property_Int"),
Optional(Eq(65536)));
// add the same key "Property_Int" with different value
test_btif_config_cache.SetInt(kBtAddr1, "Property_Int", 256);
EXPECT_THAT(test_btif_config_cache.GetInt(kBtAddr1, "Property_Int"),
Optional(Eq(256)));
test_btif_config_cache.SetUint64(kBtAddr1, "Property_64", 4294967296);
EXPECT_THAT(test_btif_config_cache.GetUint64(kBtAddr1, "Property_64"),
Optional(Eq(uint64_t(4294967296))));
// get the LinkKey and set values in the same key in paired device list
test_btif_config_cache.SetString(kBtAddr1, "LinkKey", "1122334455667788");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "LinkKey"));
EXPECT_FALSE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
// add the same key "Name" with the different value
test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1A");
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
Optional(StrEq("Headset_1A")));
// add the same key "Name" with the value different
test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_2A");
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
Optional(StrEq("Headset_2A")));
test_btif_config_cache.SetInt(kBtAddr1, "Property_Int", 64);
EXPECT_THAT(test_btif_config_cache.GetInt(kBtAddr1, "Property_Int"),
Optional(Eq(64)));
test_btif_config_cache.SetUint64(kBtAddr1, "Property_64", 65537);
EXPECT_THAT(test_btif_config_cache.GetUint64(kBtAddr1, "Property_64"),
Optional(Eq(uint64_t(65537))));
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
}
/* Stress test to pair with device then unpair device
* 1. paired with device by adding a "LinKey" to device and check the device be
* moved into paired devices list
* 2. unpaired with the device by removing the "LinkKey" and check the device be
* moved back to unpaired devices cache
* 3. loop for 30 times
*/
TEST(BtifConfigCacheTest, test_pair_unpair_device_stress_test) {
BtifConfigCache test_btif_config_cache(kCapacity);
// pair with Headset_1 11:22:33:44:55:66
test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1");
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
for (int i = 0; i < kTestRepeatCount; ++i) {
// get the LinkKey, the device will be moved from the unpaired cache to
// paired cache
test_btif_config_cache.SetString(kBtAddr1, "LinkKey", "1122334455667788");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "LinkKey"));
EXPECT_FALSE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
// remove the LinkKey, the device will be moved from the paired cache to
// unpaired cache
test_btif_config_cache.RemoveKey(kBtAddr1, "LinkKey");
EXPECT_FALSE(test_btif_config_cache.HasKey(kBtAddr1, "LinkKey"));
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
}
}
/* Stress test to pair with multi-devices and unpair with multi-devices
* 1. Pired with 4 devices with Link-Key type key in order, to check these 4
* devices are in the paired devices list cache
* 2. unpair with these 4 devices by removed Link-Key type key in order, to
* check the fisrt device was ruled-out from unpaired devices cache due to
* capacity limitation, and other 3 devices are be moved to unpaired device
* cache.
*/
TEST(BtifConfigCacheTest, test_multi_pair_unpair_with_devices) {
BtifConfigCache test_btif_config_cache(kCapacity);
// pair with 4 bt address devices by add different type linkkey.
test_btif_config_cache.SetString(kBtAddr1, "name", "kBtAddr1");
test_btif_config_cache.SetString(kBtAddr1, "LinkKey", "1122334455667788");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "LinkKey"));
test_btif_config_cache.SetString(kBtAddr2, "name", "kBtAddr2");
test_btif_config_cache.SetString(kBtAddr2, "LE_KEY_PENC", "aabbccddeeff9900");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr2, "LE_KEY_PENC"));
test_btif_config_cache.SetString(kBtAddr3, "name", "kBtAddr3");
test_btif_config_cache.SetString(kBtAddr3, "LE_KEY_PID", "a1b2c3d4e5feeeee");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr3, "LE_KEY_PID"));
test_btif_config_cache.SetString(kBtAddr4, "LE_KEY_PCSRK",
"aaaabbbbccccdddd");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr4, "LE_KEY_PCSRK"));
test_btif_config_cache.SetString(kBtAddr5, "name", "kBtAddr5");
test_btif_config_cache.SetString(kBtAddr5, "LE_KEY_LENC", "jilkjlkjlkn");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr5, "LE_KEY_LENC"));
// checking these 4 devices are in paired list cache and the content are
// correct.
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "LinkKey"),
Optional(StrEq("1122334455667788")));
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr2));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr2, "LE_KEY_PENC"),
Optional(StrEq("aabbccddeeff9900")));
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr3));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr3, "LE_KEY_PID"),
Optional(StrEq("a1b2c3d4e5feeeee")));
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr4));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr4, "LE_KEY_PCSRK"),
Optional(StrEq("aaaabbbbccccdddd")));
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr5));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr5, "LE_KEY_LENC"),
Optional(StrEq("jilkjlkjlkn")));
// unpair with these 4 bt address devices by removed the linkkey.
// unpair kBtAddr1 11:22:33:44:55:66
test_btif_config_cache.RemoveKey(kBtAddr1, "LinkKey");
EXPECT_FALSE(test_btif_config_cache.HasKey(kBtAddr1, "LinkKey"));
// no empty section is moved to unpaired
EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "name"),
Optional(StrEq("kBtAddr1")));
// unpair with kBtAddr2 aa:bb:cc:dd:ee:ff
test_btif_config_cache.RemoveKey(kBtAddr2, "LE_KEY_PENC");
EXPECT_FALSE(test_btif_config_cache.HasKey(kBtAddr2, "LE_KEY_PENC"));
EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddr2));
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr2));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr2, "name"),
Optional(StrEq("kBtAddr2")));
// unpair with kBtAddr3 AB:CD:EF:12:34:56
test_btif_config_cache.RemoveKey(kBtAddr3, "LE_KEY_PID");
EXPECT_FALSE(test_btif_config_cache.HasKey(kBtAddr3, "LE_KEY_PID"));
EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddr3));
// no empty section is moved to unpaired
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr3));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr3, "name"),
Optional(StrEq("kBtAddr3")));
// unpair with kBtAddr4 11:AA:22:BB:33:CC
test_btif_config_cache.RemoveKey(kBtAddr4, "LE_KEY_PCSRK");
EXPECT_FALSE(test_btif_config_cache.HasKey(kBtAddr4, "LE_KEY_PCSRK"));
EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddr4));
// empty section is removed
EXPECT_FALSE(test_btif_config_cache.HasUnpairedSection(kBtAddr4));
// unpair with kBtAddr5 11:AA:22:BB:33:CD
test_btif_config_cache.RemoveKey(kBtAddr5, "LE_KEY_LENC");
EXPECT_FALSE(test_btif_config_cache.HasKey(kBtAddr5, "LE_KEY_LENC"));
EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddr5));
// no empty section is moved to unpaired
EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr5));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr5, "name"),
Optional(StrEq("kBtAddr5")));
// checking the oldest unpaired device kBtAddr1 was ruled out from cache due
// to capacity limitation (3) in unpaired cache.
EXPECT_FALSE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
}
/* Test to remove sections with the specific key
* paired with sections with the specific "Restricted" key and then removed the
* "Restricted" key, check if the sections with the specific "Restricted" key
* are removed.
*/
TEST(BtifConfigCacheTest, test_remove_sections_with_key) {
BtifConfigCache test_btif_config_cache(kCapacity);
// pair with Headset_1 (kBtAddr1), Headset_2 (kBtAddr1), Heasdet_3 (kBtAddr3)
// , and Headset_1 (kBtAddr1), Headset_3 (kBtAddr3) have sepcific "Restricted"
// key
test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1");
test_btif_config_cache.SetString(kBtAddr1, "Restricted", "1");
test_btif_config_cache.SetString(kBtAddr1, "LinkKey", "1122334455667788");
test_btif_config_cache.SetString(kBtAddr2, "Name", "Headset_2");
test_btif_config_cache.SetString(kBtAddr2, "LinkKey", "aabbccddeeff9900");
test_btif_config_cache.SetString(kBtAddr3, "Name", "Headset_3");
test_btif_config_cache.SetString(kBtAddr3, "LinkKey", "a1b2c3d4e5feeeee");
test_btif_config_cache.SetString(kBtAddr3, "Restricted", "1");
// remove sections with "Restricted" key
test_btif_config_cache.RemovePersistentSectionsWithKey("Restricted");
// checking the kBtAddr1 and kBtAddr3 can not be found in config cache, only
// keep kBtAddr2 in config cache.
EXPECT_FALSE(test_btif_config_cache.HasSection(kBtAddr1));
EXPECT_TRUE(test_btif_config_cache.HasSection(kBtAddr2));
EXPECT_FALSE(test_btif_config_cache.HasSection(kBtAddr3));
}
/* Test PersistentSectionCopy and Init */
TEST(BtifConfigCacheTest, test_PersistentSectionCopy_Init) {
BtifConfigCache test_btif_config_cache(kCapacity);
config_t config_paired = {};
// pair with 3 bt devices, kBtAddr1, kBtAddr2, kBtAddr3
test_btif_config_cache.SetString(kBtAddr1, "LinkKey", "1122334455667788");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "LinkKey"));
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "LinkKey"),
Optional(StrEq("1122334455667788")));
test_btif_config_cache.SetString(kBtAddr2, "LE_KEY_PENC", "aabbccddeeff9900");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr2, "LE_KEY_PENC"));
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr2));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr2, "LE_KEY_PENC"),
Optional(StrEq("aabbccddeeff9900")));
test_btif_config_cache.SetString(kBtAddr3, "LE_KEY_PID", "a1b2c3d4e5feeeee");
EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr3, "LE_KEY_PID"));
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr3));
EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr3, "LE_KEY_PID"),
Optional(StrEq("a1b2c3d4e5feeeee")));
// check GetPersistentSections
int num_of_paired_devices = 0;
for (const auto& sec_name :
test_btif_config_cache.GetPersistentSectionNames()) {
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(sec_name));
num_of_paired_devices++;
}
EXPECT_EQ(num_of_paired_devices, 3);
// copy persistent sections
int num_of_copy_paired_devices = 0;
config_paired = test_btif_config_cache.PersistentSectionCopy();
for (const section_t& sec : config_paired.sections) {
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(sec.name));
num_of_copy_paired_devices++;
}
EXPECT_EQ(num_of_copy_paired_devices, 3);
// write persistent sections to temp test config file
EXPECT_TRUE(config_save(config_paired, TEST_CONFIG_FILE));
// get persistent sections from temp test config file
int num_of_save_paired_devices = 0;
std::unique_ptr<config_t> config_source = config_new(TEST_CONFIG_FILE);
for (const section_t& sec : config_paired.sections) {
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(sec.name));
num_of_save_paired_devices++;
}
EXPECT_EQ(num_of_save_paired_devices, 3);
// Clear all btif config cache sections
test_btif_config_cache.Clear();
// move the persistent sections to btif config paired list
int num_of_init_paired_devices = 0;
test_btif_config_cache.Init(std::move(config_source));
for (const section_t& sec : config_paired.sections) {
EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(sec.name));
num_of_init_paired_devices++;
}
EXPECT_EQ(num_of_init_paired_devices, 3);
EXPECT_TRUE(std::filesystem::remove(kTestConfigFile));
}
} // namespace testing
|