1 /*
2  * Copyright 2022 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 "gatt_api.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include "btm/btm_dev.h"
22 #include "btm/btm_sec_cb.h"
23 #include "gatt/gatt_int.h"
24 #include "osi/include/allocator.h"
25 
26 extern tBTM_SEC_CB btm_sec_cb;
27 
28 static const size_t QUEUE_SIZE_MAX = 10;
29 
make_bonded_ble_device(const RawAddress & bda,const RawAddress & rra)30 static tBTM_SEC_DEV_REC* make_bonded_ble_device(const RawAddress& bda, const RawAddress& rra) {
31   tBTM_SEC_DEV_REC* dev = btm_sec_allocate_dev_rec();
32   dev->sec_rec.sec_flags |= BTM_SEC_LE_LINK_KEY_KNOWN;
33   dev->bd_addr = bda;
34   dev->ble.pseudo_addr = rra;
35   dev->sec_rec.ble_keys.key_type = BTM_LE_KEY_PID | BTM_LE_KEY_PENC | BTM_LE_KEY_LENC;
36   return dev;
37 }
38 
make_bonded_dual_device(const RawAddress & bda,const RawAddress & rra)39 static tBTM_SEC_DEV_REC* make_bonded_dual_device(const RawAddress& bda, const RawAddress& rra) {
40   tBTM_SEC_DEV_REC* dev = make_bonded_ble_device(bda, rra);
41   dev->sec_rec.sec_flags |= BTM_SEC_LINK_KEY_KNOWN;
42   return dev;
43 }
44 
45 extern std::optional<bool> OVERRIDE_GATT_LOAD_BONDED;
46 
47 class GattApiTest : public ::testing::Test {
48 protected:
49   GattApiTest() = default;
50 
51   virtual ~GattApiTest() = default;
52 
SetUp()53   void SetUp() override {
54     btm_sec_cb.sec_dev_rec = list_new(osi_free);
55     gatt_cb.srv_chg_clt_q = fixed_queue_new(QUEUE_SIZE_MAX);
56     logging::SetMinLogLevel(-2);
57   }
58 
TearDown()59   void TearDown() override { list_free(btm_sec_cb.sec_dev_rec); }
60 };
61 
62 static const RawAddress SAMPLE_PUBLIC_BDA = {{0x00, 0x00, 0x11, 0x22, 0x33, 0x44}};
63 
64 static const RawAddress SAMPLE_RRA_BDA = {{0xAA, 0xAA, 0x11, 0x22, 0x33, 0x44}};
65 
TEST_F(GattApiTest,test_gatt_load_bonded_ble_only)66 TEST_F(GattApiTest, test_gatt_load_bonded_ble_only) {
67   OVERRIDE_GATT_LOAD_BONDED = std::optional{true};
68   make_bonded_ble_device(SAMPLE_PUBLIC_BDA, SAMPLE_RRA_BDA);
69 
70   gatt_load_bonded();
71 
72   ASSERT_TRUE(gatt_is_bda_in_the_srv_chg_clt_list(SAMPLE_RRA_BDA));
73   ASSERT_FALSE(gatt_is_bda_in_the_srv_chg_clt_list(SAMPLE_PUBLIC_BDA));
74   OVERRIDE_GATT_LOAD_BONDED.reset();
75 }
76 
TEST_F(GattApiTest,test_gatt_load_bonded_dual)77 TEST_F(GattApiTest, test_gatt_load_bonded_dual) {
78   OVERRIDE_GATT_LOAD_BONDED = std::optional{true};
79   make_bonded_dual_device(SAMPLE_PUBLIC_BDA, SAMPLE_RRA_BDA);
80 
81   gatt_load_bonded();
82 
83   ASSERT_TRUE(gatt_is_bda_in_the_srv_chg_clt_list(SAMPLE_RRA_BDA));
84   ASSERT_TRUE(gatt_is_bda_in_the_srv_chg_clt_list(SAMPLE_PUBLIC_BDA));
85   OVERRIDE_GATT_LOAD_BONDED.reset();
86 }
87