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