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