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 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 
19 #include <cstdint>
20 
21 #include "common/init_flags.h"
22 #include "hci/controller_interface_mock.h"
23 #include "stack/acl/acl.h"
24 #include "stack/btm/btm_int_types.h"
25 #include "stack/btm/security_device_record.h"
26 #include "stack/include/acl_api.h"
27 #include "stack/include/acl_hci_link_interface.h"
28 #include "stack/include/hcidefs.h"
29 #include "test/common/mock_functions.h"
30 #include "test/mock/mock_main_shim_entry.h"
31 #include "types/hci_role.h"
32 #include "types/raw_address.h"
33 
34 tBTM_CB btm_cb;
35 
36 namespace {
37 const RawAddress kRawAddress = RawAddress({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
38 }  // namespace
39 
40 namespace bluetooth {
41 namespace testing {
42 
43 std::set<const RawAddress> copy_of_connected_with_both_public_and_random_set();
44 
45 }  // namespace testing
46 }  // namespace bluetooth
47 
48 class StackAclTest : public testing::Test {
49  protected:
SetUp()50   void SetUp() override {
51     reset_mock_function_count_map();
52     bluetooth::hci::testing::mock_controller_ = &controller_;
53   }
TearDown()54   void TearDown() override {
55     bluetooth::hci::testing::mock_controller_ = nullptr;
56   }
57 
58   tBTM_SEC_DEV_REC device_record_;
59   bluetooth::hci::testing::MockControllerInterface controller_;
60 };
61 
TEST_F(StackAclTest,nop)62 TEST_F(StackAclTest, nop) {}
63 
TEST_F(StackAclTest,acl_process_extended_features)64 TEST_F(StackAclTest, acl_process_extended_features) {
65   const uint16_t hci_handle = 0x123;
66   const tBT_TRANSPORT transport = BT_TRANSPORT_LE;
67   const tHCI_ROLE link_role = HCI_ROLE_CENTRAL;
68 
69   btm_acl_created(kRawAddress, hci_handle, link_role, transport);
70   tACL_CONN* p_acl = btm_acl_for_bda(kRawAddress, transport);
71   ASSERT_NE(nullptr, p_acl);
72 
73   // Handle typical case
74   {
75     const uint8_t max_page = 3;
76     memset((void*)p_acl->peer_lmp_feature_valid, 0,
77            HCI_EXT_FEATURES_PAGE_MAX + 1);
78     acl_process_extended_features(hci_handle, 1, max_page, 0xf123456789abcde);
79     acl_process_extended_features(hci_handle, 2, max_page, 0xef123456789abcd);
80     acl_process_extended_features(hci_handle, 3, max_page, 0xdef123456789abc);
81 
82     /* page 0 is the standard feature set */
83     ASSERT_FALSE(p_acl->peer_lmp_feature_valid[0]);
84     ASSERT_TRUE(p_acl->peer_lmp_feature_valid[1]);
85     ASSERT_TRUE(p_acl->peer_lmp_feature_valid[2]);
86     ASSERT_TRUE(p_acl->peer_lmp_feature_valid[3]);
87   }
88 
89   // Handle extreme case
90   {
91     const uint8_t max_page = 255;
92     memset((void*)p_acl->peer_lmp_feature_valid, 0,
93            HCI_EXT_FEATURES_PAGE_MAX + 1);
94     for (int i = 1; i < HCI_EXT_FEATURES_PAGE_MAX + 1; i++) {
95       acl_process_extended_features(hci_handle, static_cast<uint8_t>(i),
96                                     max_page, 0x123456789abcdef);
97     }
98     /* page 0 is the standard feature set */
99     ASSERT_FALSE(p_acl->peer_lmp_feature_valid[0]);
100     ASSERT_TRUE(p_acl->peer_lmp_feature_valid[1]);
101     ASSERT_TRUE(p_acl->peer_lmp_feature_valid[2]);
102     ASSERT_TRUE(p_acl->peer_lmp_feature_valid[3]);
103   }
104 
105   // Handle case where device returns max page of zero
106   {
107     memset((void*)p_acl->peer_lmp_feature_valid, 0,
108            HCI_EXT_FEATURES_PAGE_MAX + 1);
109     acl_process_extended_features(hci_handle, 1, 0, 0xdef123456789abc);
110     ASSERT_FALSE(p_acl->peer_lmp_feature_valid[0]);
111     ASSERT_TRUE(p_acl->peer_lmp_feature_valid[1]);
112     ASSERT_FALSE(p_acl->peer_lmp_feature_valid[2]);
113     ASSERT_FALSE(p_acl->peer_lmp_feature_valid[3]);
114   }
115 
116   btm_acl_removed(hci_handle);
117 }
118