summaryrefslogtreecommitdiff
path: root/system/stack/test/stack_acl_test.cc
blob: 6b741e2d19a62894e141ebf3f94479e4126f019b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0(the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <cstdint>
#include <deque>

#include "common/init_flags.h"
#include "osi/include/log.h"
#include "stack/acl/acl.h"
#include "stack/btm/btm_int_types.h"
#include "stack/btm/security_device_record.h"
#include "stack/include/acl_api.h"
#include "stack/include/acl_hci_link_interface.h"
#include "stack/include/hci_error_code.h"
#include "test/common/mock_functions.h"
#include "test/mock/mock_main_shim_acl_api.h"
#include "types/ble_address_with_type.h"
#include "types/hci_role.h"
#include "types/raw_address.h"

tBTM_CB btm_cb;

void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...) {}

namespace {
const char* test_flags[] = {
    "INIT_logging_debug_enabled_for_all=true",
    nullptr,
};

const RawAddress kRawAddress = RawAddress({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
}  // namespace

namespace bluetooth {
namespace testing {

std::set<const RawAddress> copy_of_connected_with_both_public_and_random_set();

}  // namespace testing
}  // namespace bluetooth

void BTM_update_version_info(const RawAddress& bd_addr,
                             const remote_version_info& remote_version_info) {}

void btm_sec_role_changed(tHCI_STATUS hci_status, const RawAddress& bd_addr,
                          tHCI_ROLE new_role) {}

class StackAclTest : public testing::Test {
 protected:
  void SetUp() override {
    mock_function_count_map.clear();
    bluetooth::common::InitFlags::Load(test_flags);
  }
  void TearDown() override {}

  tBTM_SEC_DEV_REC device_record_;
};

TEST_F(StackAclTest, nop) {}

TEST_F(StackAclTest, acl_process_extended_features) {
  const uint16_t hci_handle = 0x123;
  const tBT_TRANSPORT transport = BT_TRANSPORT_LE;
  const tHCI_ROLE link_role = HCI_ROLE_CENTRAL;

  btm_acl_created(kRawAddress, hci_handle, link_role, transport);
  tACL_CONN* p_acl = btm_acl_for_bda(kRawAddress, transport);
  ASSERT_NE(nullptr, p_acl);

  // Handle typical case
  {
    const uint8_t max_page = 3;
    memset((void*)p_acl->peer_lmp_feature_valid, 0,
           HCI_EXT_FEATURES_PAGE_MAX + 1);
    acl_process_extended_features(hci_handle, 1, max_page, 0xf123456789abcde);
    acl_process_extended_features(hci_handle, 2, max_page, 0xef123456789abcd);
    acl_process_extended_features(hci_handle, 3, max_page, 0xdef123456789abc);

    /* page 0 is the standard feature set */
    ASSERT_FALSE(p_acl->peer_lmp_feature_valid[0]);
    ASSERT_TRUE(p_acl->peer_lmp_feature_valid[1]);
    ASSERT_TRUE(p_acl->peer_lmp_feature_valid[2]);
    ASSERT_TRUE(p_acl->peer_lmp_feature_valid[3]);
  }

  // Handle extreme case
  {
    const uint8_t max_page = 255;
    memset((void*)p_acl->peer_lmp_feature_valid, 0,
           HCI_EXT_FEATURES_PAGE_MAX + 1);
    for (int i = 1; i < HCI_EXT_FEATURES_PAGE_MAX + 1; i++) {
      acl_process_extended_features(hci_handle, static_cast<uint8_t>(i),
                                    max_page, 0x123456789abcdef);
    }
    /* page 0 is the standard feature set */
    ASSERT_FALSE(p_acl->peer_lmp_feature_valid[0]);
    ASSERT_TRUE(p_acl->peer_lmp_feature_valid[1]);
    ASSERT_TRUE(p_acl->peer_lmp_feature_valid[2]);
    ASSERT_TRUE(p_acl->peer_lmp_feature_valid[3]);
  }

  // Handle case where device returns max page of zero
  {
    memset((void*)p_acl->peer_lmp_feature_valid, 0,
           HCI_EXT_FEATURES_PAGE_MAX + 1);
    acl_process_extended_features(hci_handle, 1, 0, 0xdef123456789abc);
    ASSERT_FALSE(p_acl->peer_lmp_feature_valid[0]);
    ASSERT_TRUE(p_acl->peer_lmp_feature_valid[1]);
    ASSERT_FALSE(p_acl->peer_lmp_feature_valid[2]);
    ASSERT_FALSE(p_acl->peer_lmp_feature_valid[3]);
  }

  btm_acl_removed(hci_handle);
}