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