• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "btif/include/btif_config_cache.h"
18 
19 #include <filesystem>
20 
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 
24 namespace {
25 
26 const int kCapacity = 3;
27 const int kTestRepeatCount = 30;
28 const std::string kBtAddr1 = "11:22:33:44:55:66";
29 const std::string kBtAddr2 = "AA:BB:CC:DD:EE:FF";
30 const std::string kBtAddr3 = "AB:CD:EF:12:34:56";
31 const std::string kBtAddr4 = "11:AA:22:BB:33:CC";
32 const std::string kBtAddr5 = "11:AA:22:BB:33:CD";
33 const std::string kBtLocalAddr = "12:34:56:78:90:AB";
34 const std::string kBtInfo = "Info";
35 const std::string kBtMetrics = "Metrics";
36 const std::string kBtAdapter = "Adapter";
37 const std::string kBtAddrInvalid1 = "AB:CD:EF:12:34";
38 const std::string kBtAddrInvalid2 = "AB:CD:EF:12:34:56:78";
39 const std::string kBtAddrInvalid3 = "ABCDEF123456";
40 const std::string kBtAddrInvalid4 = "AB-CD-EF-12-34-56";
41 const std::string kBtSectionInvalid1 = "Invalid Section";
42 const std::filesystem::path kTestConfigFile =
43     std::filesystem::temp_directory_path() / "config_cache_test.conf";
44 const char* TEST_CONFIG_FILE = kTestConfigFile.c_str();
45 
46 }  // namespace
47 
48 namespace testing {
49 
50 /* Test to basic btif_config_cache set up
51  * 1. when received Local device sections information, the sections can be put
52  * into btif config cache
53  * 2. the device sections and key-value will be set to Btif config cache when
54  * receiving different device sections
55  * 3. limit the capacity of unpacire devices cache to 3, test the oldest device
56  * section will be ruled out when receiveing 4 different device sections.
57  */
TEST(BtifConfigCacheTest,test_setup_btif_config_cache)58 TEST(BtifConfigCacheTest, test_setup_btif_config_cache) {
59   BtifConfigCache test_btif_config_cache(kCapacity);
60   // Info section
61   test_btif_config_cache.SetString(kBtInfo, "FileSource", "");
62   test_btif_config_cache.SetString(kBtInfo, "TimeCreated",
63                                    "2020-06-05 12:12:12");
64   // Metrics section
65   test_btif_config_cache.SetString(kBtMetrics, "Salt256Bit",
66                                    "92a331174d20f2bb");
67   // Adapter Section
68   test_btif_config_cache.SetString(kBtAdapter, "Address", kBtLocalAddr);
69   EXPECT_TRUE(test_btif_config_cache.HasSection(kBtAdapter));
70 
71   // bt_device_1
72   test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1");
73   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Name"));
74 
75   test_btif_config_cache.SetInt(kBtAddr1, "Property_Int", 1);
76   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Property_Int"));
77 
78   // bt_device_2
79   test_btif_config_cache.SetString(kBtAddr2, "Name", "Headset_2");
80   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr2, "Name"));
81 
82   // bt_device_3
83   test_btif_config_cache.SetString(kBtAddr3, "Name", "Headset_3");
84   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr3, "Name"));
85 
86   // bt_device_4
87   test_btif_config_cache.SetString(kBtAddr4, "Name", "Headset_4");
88   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr4, "Name"));
89 
90   // out out the capacty of unpair devices cache, the bt_device_1 be ruled out
91   EXPECT_FALSE(test_btif_config_cache.HasSection(kBtAddr1));
92   EXPECT_TRUE(test_btif_config_cache.HasSection(kBtAddr2));
93   EXPECT_TRUE(test_btif_config_cache.HasSection(kBtAddr3));
94   EXPECT_TRUE(test_btif_config_cache.HasSection(kBtAddr4));
95 }
96 
97 /* Test to set up btif_config_cache with invalid bt address or section name
98  * when received Invalid bt address or section, it's not allowed to put invalid
99  * section to paired devices list section
100  */
TEST(BtifConfigCacheTest,test_set_up_config_cache_with_invalid_section)101 TEST(BtifConfigCacheTest, test_set_up_config_cache_with_invalid_section) {
102   BtifConfigCache test_btif_config_cache(kCapacity);
103 
104   // kBtAddrInvalid1
105   test_btif_config_cache.SetString(kBtAddrInvalid1, "Name", "Headset_1");
106   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid1, "Name"));
107   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid1));
108   // get the LinkKey
109   test_btif_config_cache.SetString(kBtAddrInvalid1, "LinkKey",
110                                    "1122334455667788");
111   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid1, "LinkKey"));
112   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid1));
113   EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddrInvalid1));
114 
115   // kBtAddrInvalid2
116   test_btif_config_cache.SetString(kBtAddrInvalid2, "Name", "Headset_1");
117   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid2, "Name"));
118   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid2));
119   // get the LinkKey
120   test_btif_config_cache.SetString(kBtAddrInvalid2, "LinkKey",
121                                    "1122334455667788");
122   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid2, "LinkKey"));
123   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid2));
124   EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddrInvalid2));
125 
126   // kBtAddrInvalid3
127   test_btif_config_cache.SetString(kBtAddrInvalid3, "Name", "Headset_1");
128   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid3, "Name"));
129   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid3));
130   // get the LinkKey
131   test_btif_config_cache.SetString(kBtAddrInvalid3, "LinkKey",
132                                    "1122334455667788");
133   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid3, "LinkKey"));
134   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid3));
135   EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddrInvalid3));
136 
137   // kBtAddrInvalid4
138   test_btif_config_cache.SetString(kBtAddrInvalid4, "Name", "Headset_1");
139   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid4, "Name"));
140   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid4));
141   // get the LinkKey
142   test_btif_config_cache.SetString(kBtAddrInvalid4, "LinkKey",
143                                    "1122334455667788");
144   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddrInvalid4, "LinkKey"));
145   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddrInvalid4));
146   EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddrInvalid4));
147 
148   // kBtSectionInvalid1
149   test_btif_config_cache.SetString(kBtSectionInvalid1, "Name", "Headset_1");
150   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtSectionInvalid1, "Name"));
151   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtSectionInvalid1));
152   // get the LinkKey
153   test_btif_config_cache.SetString(kBtSectionInvalid1, "LinkKey",
154                                    "1122334455667788");
155   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtSectionInvalid1, "LinkKey"));
156   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtSectionInvalid1));
157   EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtSectionInvalid1));
158 }
159 
160 /* Stress test to set and get key values
161  * 1. stress test to set different type key value to unpaired device cache and
162  * get the different type key values in the unpaired cache section to check if
163  * we get the key-values the same with we set in unpaired device cache.
164  * 2. stress test to set different type key value to paired device section and
165  * get the different type key values in the paired cache section to check if we
166  * get the key-values the same with we set in paired device cache.
167  */
TEST(BtifConfigCacheTest,test_get_set_key_value_test)168 TEST(BtifConfigCacheTest, test_get_set_key_value_test) {
169   BtifConfigCache test_btif_config_cache(kCapacity);
170   // test in unpaired cache
171   test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1");
172   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Name"));
173   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
174               Optional(StrEq("Headset_1")));
175 
176   test_btif_config_cache.SetInt(kBtAddr1, "Property_Int", 65536);
177   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Property_Int"));
178   EXPECT_THAT(test_btif_config_cache.GetInt(kBtAddr1, "Property_Int"),
179               Optional(Eq(65536)));
180 
181   test_btif_config_cache.SetUint64(kBtAddr1, "Property_64", 4294967296);
182   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Property_64"));
183   EXPECT_THAT(test_btif_config_cache.GetUint64(kBtAddr1, "Property_64"),
184               Optional(Eq(uint64_t(4294967296))));
185 
186   test_btif_config_cache.SetBool(kBtAddr1, "Property_Bool", true);
187   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Property_Bool"));
188   EXPECT_THAT(test_btif_config_cache.GetBool(kBtAddr1, "Property_Bool"),
189               Optional(IsTrue()));
190 
191   // empty value
192   test_btif_config_cache.SetString(kBtAddr1, "Name", "");
193   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Name"));
194   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
195               Optional(StrEq("")));
196 
197   // get the LinkKey
198   test_btif_config_cache.SetString(kBtAddr1, "LinkKey", "1122334455667788");
199   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "LinkKey"));
200   EXPECT_FALSE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
201   EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
202 
203   // test in unpaired cache
204   test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1");
205   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Name"));
206   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
207               Optional(StrEq("Headset_1")));
208 
209   test_btif_config_cache.SetInt(kBtAddr1, "Property_Int", 65536);
210   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Property_Int"));
211   EXPECT_THAT(test_btif_config_cache.GetInt(kBtAddr1, "Property_Int"),
212               Optional(Eq(65536)));
213 
214   test_btif_config_cache.SetUint64(kBtAddr1, "Property_64", 4294967296);
215   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Property_64"));
216   EXPECT_THAT(test_btif_config_cache.GetUint64(kBtAddr1, "Property_64"),
217               Optional(Eq(uint64_t(4294967296))));
218 
219   test_btif_config_cache.SetBool(kBtAddr1, "Property_Bool", true);
220   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Property_Bool"));
221   EXPECT_THAT(test_btif_config_cache.GetBool(kBtAddr1, "Property_Bool"),
222               Optional(IsTrue()));
223 
224   // empty section is disallowed
225   EXPECT_DEATH({ test_btif_config_cache.SetString("", "name", "Headset_1"); },
226                "Empty section not allowed");
227   // empty key is disallowed
228   EXPECT_DEATH({ test_btif_config_cache.SetString(kBtAddr1, "", "Headset_1"); },
229                "Empty key not allowed");
230   EXPECT_FALSE(test_btif_config_cache.HasKey(kBtAddr1, ""));
231   // empty value is allowed
232   test_btif_config_cache.SetString(kBtAddr1, "Name", "");
233   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "Name"));
234   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
235               Optional(StrEq("")));
236 }
237 
238 /* Test to set values in the same key
239  * Receiving the same key with different values in a section, the new incoming
240  * value will be updated but the key will not be added repeatedly. test this
241  * feature in both unpaired devic cache and paired device list cache
242  */
TEST(BtifConfigCacheTest,test_set_values_in_the_same_key)243 TEST(BtifConfigCacheTest, test_set_values_in_the_same_key) {
244   BtifConfigCache test_btif_config_cache(kCapacity);
245   // add new a key "Name"
246   test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1");
247   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
248               Optional(StrEq("Headset_1")));
249   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
250 
251   // add the same key "Name" with different value
252   test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1A");
253   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
254               Optional(StrEq("Headset_1A")));
255 
256   // add the same key "Name" with different value
257   test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_2A");
258   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
259               Optional(StrEq("Headset_2A")));
260   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
261 
262   // add new a key "Property_Int"
263   test_btif_config_cache.SetInt(kBtAddr1, "Property_Int", 65536);
264   EXPECT_THAT(test_btif_config_cache.GetInt(kBtAddr1, "Property_Int"),
265               Optional(Eq(65536)));
266 
267   // add the same key "Property_Int" with different value
268   test_btif_config_cache.SetInt(kBtAddr1, "Property_Int", 256);
269   EXPECT_THAT(test_btif_config_cache.GetInt(kBtAddr1, "Property_Int"),
270               Optional(Eq(256)));
271 
272   test_btif_config_cache.SetUint64(kBtAddr1, "Property_64", 4294967296);
273   EXPECT_THAT(test_btif_config_cache.GetUint64(kBtAddr1, "Property_64"),
274               Optional(Eq(uint64_t(4294967296))));
275 
276   // get the LinkKey and set values in the same key in paired device list
277   test_btif_config_cache.SetString(kBtAddr1, "LinkKey", "1122334455667788");
278   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "LinkKey"));
279   EXPECT_FALSE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
280   EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
281 
282   // add the same key "Name" with the different value
283   test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1A");
284   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
285               Optional(StrEq("Headset_1A")));
286 
287   // add the same key "Name" with the value different
288   test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_2A");
289   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "Name"),
290               Optional(StrEq("Headset_2A")));
291 
292   test_btif_config_cache.SetInt(kBtAddr1, "Property_Int", 64);
293   EXPECT_THAT(test_btif_config_cache.GetInt(kBtAddr1, "Property_Int"),
294               Optional(Eq(64)));
295 
296   test_btif_config_cache.SetUint64(kBtAddr1, "Property_64", 65537);
297   EXPECT_THAT(test_btif_config_cache.GetUint64(kBtAddr1, "Property_64"),
298               Optional(Eq(uint64_t(65537))));
299 
300   EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
301 }
302 
303 /* Stress test to pair with device then unpair device
304  * 1. paired with device by adding a "LinKey" to device and check the device be
305  * moved into paired devices list
306  * 2. unpaired with the device by removing the "LinkKey" and check the device be
307  * moved back to unpaired devices cache
308  * 3. loop for 30 times
309  */
TEST(BtifConfigCacheTest,test_pair_unpair_device_stress_test)310 TEST(BtifConfigCacheTest, test_pair_unpair_device_stress_test) {
311   BtifConfigCache test_btif_config_cache(kCapacity);
312 
313   // pair with Headset_1 11:22:33:44:55:66
314   test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1");
315   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
316   EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
317 
318   for (int i = 0; i < kTestRepeatCount; ++i) {
319     // get the LinkKey, the device will be moved from the unpaired cache to
320     // paired cache
321     test_btif_config_cache.SetString(kBtAddr1, "LinkKey", "1122334455667788");
322     EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "LinkKey"));
323     EXPECT_FALSE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
324     EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
325 
326     // remove the LinkKey, the device will be moved from the paired cache to
327     // unpaired cache
328     test_btif_config_cache.RemoveKey(kBtAddr1, "LinkKey");
329     EXPECT_FALSE(test_btif_config_cache.HasKey(kBtAddr1, "LinkKey"));
330     EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
331     EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
332   }
333 }
334 
335 /* Stress test to pair with multi-devices and unpair with multi-devices
336  * 1. Pired with 4 devices with Link-Key type key in order, to check these 4
337  * devices are in the paired devices list cache
338  * 2. unpair with these 4 devices by removed Link-Key type key in order, to
339  * check the fisrt device was ruled-out from unpaired devices cache due to
340  * capacity limitation, and other 3 devices are be moved to unpaired device
341  * cache.
342  */
TEST(BtifConfigCacheTest,test_multi_pair_unpair_with_devices)343 TEST(BtifConfigCacheTest, test_multi_pair_unpair_with_devices) {
344   BtifConfigCache test_btif_config_cache(kCapacity);
345   // pair with 4 bt address devices by add different type linkkey.
346   test_btif_config_cache.SetString(kBtAddr1, "name", "kBtAddr1");
347   test_btif_config_cache.SetString(kBtAddr1, "LinkKey", "1122334455667788");
348   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "LinkKey"));
349 
350   test_btif_config_cache.SetString(kBtAddr2, "name", "kBtAddr2");
351   test_btif_config_cache.SetString(kBtAddr2, "LE_KEY_PENC", "aabbccddeeff9900");
352   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr2, "LE_KEY_PENC"));
353 
354   test_btif_config_cache.SetString(kBtAddr3, "name", "kBtAddr3");
355   test_btif_config_cache.SetString(kBtAddr3, "LE_KEY_PID", "a1b2c3d4e5feeeee");
356   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr3, "LE_KEY_PID"));
357 
358   test_btif_config_cache.SetString(kBtAddr4, "LE_KEY_PCSRK",
359                                    "aaaabbbbccccdddd");
360   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr4, "LE_KEY_PCSRK"));
361 
362   test_btif_config_cache.SetString(kBtAddr5, "name", "kBtAddr5");
363   test_btif_config_cache.SetString(kBtAddr5, "LE_KEY_LENC", "jilkjlkjlkn");
364   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr5, "LE_KEY_LENC"));
365 
366   // checking these 4 devices are in paired list cache and the content are
367   // correct.
368   EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
369   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "LinkKey"),
370               Optional(StrEq("1122334455667788")));
371   EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr2));
372   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr2, "LE_KEY_PENC"),
373               Optional(StrEq("aabbccddeeff9900")));
374   EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr3));
375   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr3, "LE_KEY_PID"),
376               Optional(StrEq("a1b2c3d4e5feeeee")));
377   EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr4));
378   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr4, "LE_KEY_PCSRK"),
379               Optional(StrEq("aaaabbbbccccdddd")));
380   EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr5));
381   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr5, "LE_KEY_LENC"),
382               Optional(StrEq("jilkjlkjlkn")));
383 
384   // unpair with these 4 bt address devices by removed the linkkey.
385   // unpair kBtAddr1 11:22:33:44:55:66
386   test_btif_config_cache.RemoveKey(kBtAddr1, "LinkKey");
387   EXPECT_FALSE(test_btif_config_cache.HasKey(kBtAddr1, "LinkKey"));
388   // no empty section is moved to unpaired
389   EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
390   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
391   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "name"),
392               Optional(StrEq("kBtAddr1")));
393 
394   // unpair with kBtAddr2 aa:bb:cc:dd:ee:ff
395   test_btif_config_cache.RemoveKey(kBtAddr2, "LE_KEY_PENC");
396   EXPECT_FALSE(test_btif_config_cache.HasKey(kBtAddr2, "LE_KEY_PENC"));
397   EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddr2));
398   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr2));
399   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr2, "name"),
400               Optional(StrEq("kBtAddr2")));
401 
402   // unpair with kBtAddr3 AB:CD:EF:12:34:56
403   test_btif_config_cache.RemoveKey(kBtAddr3, "LE_KEY_PID");
404   EXPECT_FALSE(test_btif_config_cache.HasKey(kBtAddr3, "LE_KEY_PID"));
405   EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddr3));
406   // no empty section is moved to unpaired
407   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr3));
408   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr3, "name"),
409               Optional(StrEq("kBtAddr3")));
410 
411   // unpair with kBtAddr4 11:AA:22:BB:33:CC
412   test_btif_config_cache.RemoveKey(kBtAddr4, "LE_KEY_PCSRK");
413   EXPECT_FALSE(test_btif_config_cache.HasKey(kBtAddr4, "LE_KEY_PCSRK"));
414   EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddr4));
415   // empty section is removed
416   EXPECT_FALSE(test_btif_config_cache.HasUnpairedSection(kBtAddr4));
417 
418   // unpair with kBtAddr5 11:AA:22:BB:33:CD
419   test_btif_config_cache.RemoveKey(kBtAddr5, "LE_KEY_LENC");
420   EXPECT_FALSE(test_btif_config_cache.HasKey(kBtAddr5, "LE_KEY_LENC"));
421   EXPECT_FALSE(test_btif_config_cache.HasPersistentSection(kBtAddr5));
422   // no empty section is moved to unpaired
423   EXPECT_TRUE(test_btif_config_cache.HasUnpairedSection(kBtAddr5));
424   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr5, "name"),
425               Optional(StrEq("kBtAddr5")));
426 
427   // checking the oldest unpaired device kBtAddr1 was ruled out from cache due
428   // to capacity limitation (3) in unpaired cache.
429   EXPECT_FALSE(test_btif_config_cache.HasUnpairedSection(kBtAddr1));
430 }
431 
432 /* Test to remove sections with the specific key
433  * paired with sections with the specific "Restricted" key and then removed the
434  * "Restricted" key, check if the sections with the specific "Restricted" key
435  * are removed.
436  */
TEST(BtifConfigCacheTest,test_remove_sections_with_key)437 TEST(BtifConfigCacheTest, test_remove_sections_with_key) {
438   BtifConfigCache test_btif_config_cache(kCapacity);
439   // pair with Headset_1 (kBtAddr1), Headset_2 (kBtAddr1), Heasdet_3 (kBtAddr3)
440   // , and Headset_1 (kBtAddr1), Headset_3 (kBtAddr3) have sepcific "Restricted"
441   // key
442   test_btif_config_cache.SetString(kBtAddr1, "Name", "Headset_1");
443   test_btif_config_cache.SetString(kBtAddr1, "Restricted", "1");
444   test_btif_config_cache.SetString(kBtAddr1, "LinkKey", "1122334455667788");
445   test_btif_config_cache.SetString(kBtAddr2, "Name", "Headset_2");
446   test_btif_config_cache.SetString(kBtAddr2, "LinkKey", "aabbccddeeff9900");
447   test_btif_config_cache.SetString(kBtAddr3, "Name", "Headset_3");
448   test_btif_config_cache.SetString(kBtAddr3, "LinkKey", "a1b2c3d4e5feeeee");
449   test_btif_config_cache.SetString(kBtAddr3, "Restricted", "1");
450 
451   // remove sections with "Restricted" key
452   test_btif_config_cache.RemovePersistentSectionsWithKey("Restricted");
453 
454   // checking the kBtAddr1 and kBtAddr3 can not be found in config cache, only
455   // keep kBtAddr2 in config cache.
456   EXPECT_FALSE(test_btif_config_cache.HasSection(kBtAddr1));
457   EXPECT_TRUE(test_btif_config_cache.HasSection(kBtAddr2));
458   EXPECT_FALSE(test_btif_config_cache.HasSection(kBtAddr3));
459 }
460 
461 /* Test PersistentSectionCopy and Init */
TEST(BtifConfigCacheTest,test_PersistentSectionCopy_Init)462 TEST(BtifConfigCacheTest, test_PersistentSectionCopy_Init) {
463   BtifConfigCache test_btif_config_cache(kCapacity);
464   config_t config_paired = {};
465   // pair with 3 bt devices, kBtAddr1, kBtAddr2, kBtAddr3
466   test_btif_config_cache.SetString(kBtAddr1, "LinkKey", "1122334455667788");
467   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr1, "LinkKey"));
468   EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr1));
469   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr1, "LinkKey"),
470               Optional(StrEq("1122334455667788")));
471 
472   test_btif_config_cache.SetString(kBtAddr2, "LE_KEY_PENC", "aabbccddeeff9900");
473   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr2, "LE_KEY_PENC"));
474   EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr2));
475   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr2, "LE_KEY_PENC"),
476               Optional(StrEq("aabbccddeeff9900")));
477 
478   test_btif_config_cache.SetString(kBtAddr3, "LE_KEY_PID", "a1b2c3d4e5feeeee");
479   EXPECT_TRUE(test_btif_config_cache.HasKey(kBtAddr3, "LE_KEY_PID"));
480   EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(kBtAddr3));
481   EXPECT_THAT(test_btif_config_cache.GetString(kBtAddr3, "LE_KEY_PID"),
482               Optional(StrEq("a1b2c3d4e5feeeee")));
483 
484   // check GetPersistentSections
485   int num_of_paired_devices = 0;
486   for (const auto& sec_name :
487        test_btif_config_cache.GetPersistentSectionNames()) {
488     EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(sec_name));
489     num_of_paired_devices++;
490   }
491   EXPECT_EQ(num_of_paired_devices, 3);
492 
493   // copy persistent sections
494   int num_of_copy_paired_devices = 0;
495   config_paired = test_btif_config_cache.PersistentSectionCopy();
496   for (const section_t& sec : config_paired.sections) {
497     EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(sec.name));
498     num_of_copy_paired_devices++;
499   }
500   EXPECT_EQ(num_of_copy_paired_devices, 3);
501 
502   // write persistent sections to temp test config file
503   EXPECT_TRUE(config_save(config_paired, TEST_CONFIG_FILE));
504   // get persistent sections from temp test config file
505   int num_of_save_paired_devices = 0;
506   std::unique_ptr<config_t> config_source = config_new(TEST_CONFIG_FILE);
507   for (const section_t& sec : config_paired.sections) {
508     EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(sec.name));
509     num_of_save_paired_devices++;
510   }
511   EXPECT_EQ(num_of_save_paired_devices, 3);
512 
513   // Clear all btif config cache sections
514   test_btif_config_cache.Clear();
515 
516   // move the persistent sections to btif config paired list
517   int num_of_init_paired_devices = 0;
518   test_btif_config_cache.Init(std::move(config_source));
519   for (const section_t& sec : config_paired.sections) {
520     EXPECT_TRUE(test_btif_config_cache.HasPersistentSection(sec.name));
521     num_of_init_paired_devices++;
522   }
523   EXPECT_EQ(num_of_init_paired_devices, 3);
524 
525   EXPECT_TRUE(std::filesystem::remove(kTestConfigFile));
526 }
527 
528 }  // namespace testing
529