1 /*
2  *
3  *  Copyright 2020 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <map>
22 
23 #include "btif/include/btif_hh.h"
24 #include "hci/include/hci_layer.h"
25 #include "hci/include/hci_packet_factory.h"
26 #include "hci/include/packet_fragmenter.h"
27 #include "internal_include/stack_config.h"
28 #include "osi/include/osi.h"
29 #include "stack/btm/btm_int_types.h"
30 #include "stack/include/acl_api.h"
31 #include "stack/include/acl_hci_link_interface.h"
32 #include "stack/include/btm_client_interface.h"
33 #include "stack/l2cap/l2c_int.h"
34 #include "test/mock/mock_hcic_hcicmds.h"
35 #include "types/raw_address.h"
36 
37 namespace mock = test::mock::hcic_hcicmds;
38 
39 extern tBTM_CB btm_cb;
40 
41 uint8_t appl_trace_level = BT_TRACE_LEVEL_VERBOSE;
42 btif_hh_cb_t btif_hh_cb;
43 tL2C_CB l2cb;
44 
hci_packet_factory_get_interface()45 const hci_packet_factory_t* hci_packet_factory_get_interface() {
46   return nullptr;
47 }
hci_layer_get_interface()48 const hci_t* hci_layer_get_interface() { return nullptr; }
49 
LogMsg(uint32_t trace_set_mask,const char * fmt_str,...)50 void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...) {}
51 
52 const std::string kSmpOptions("mock smp options");
53 
get_trace_config_enabled(void)54 bool get_trace_config_enabled(void) { return false; }
get_pts_avrcp_test(void)55 bool get_pts_avrcp_test(void) { return false; }
get_pts_secure_only_mode(void)56 bool get_pts_secure_only_mode(void) { return false; }
get_pts_conn_updates_disabled(void)57 bool get_pts_conn_updates_disabled(void) { return false; }
get_pts_crosskey_sdp_disable(void)58 bool get_pts_crosskey_sdp_disable(void) { return false; }
get_pts_smp_options(void)59 const std::string* get_pts_smp_options(void) { return &kSmpOptions; }
get_pts_smp_failure_case(void)60 int get_pts_smp_failure_case(void) { return 123; }
get_all(void)61 config_t* get_all(void) { return nullptr; }
packet_fragmenter_get_interface()62 const packet_fragmenter_t* packet_fragmenter_get_interface() { return nullptr; }
63 
64 stack_config_t mock_stack_config{
65     .get_trace_config_enabled = get_trace_config_enabled,
66     .get_pts_avrcp_test = get_pts_avrcp_test,
67     .get_pts_secure_only_mode = get_pts_secure_only_mode,
68     .get_pts_conn_updates_disabled = get_pts_conn_updates_disabled,
69     .get_pts_crosskey_sdp_disable = get_pts_crosskey_sdp_disable,
70     .get_pts_smp_options = get_pts_smp_options,
71     .get_pts_smp_failure_case = get_pts_smp_failure_case,
72     .get_all = get_all,
73 };
stack_config_get_interface(void)74 const stack_config_t* stack_config_get_interface(void) {
75   return &mock_stack_config;
76 }
77 
78 std::map<std::string, int> mock_function_count_map;
79 
80 extern bool MOCK_bluetooth_shim_is_gd_acl_enabled_;
81 
82 namespace {
83 
84 using testing::_;
85 using testing::DoAll;
86 using testing::NotNull;
87 using testing::Pointee;
88 using testing::Return;
89 using testing::SaveArg;
90 using testing::SaveArgPointee;
91 using testing::StrEq;
92 using testing::StrictMock;
93 using testing::Test;
94 
95 class StackBtmTest : public Test {
96  public:
97  protected:
SetUp()98   void SetUp() override { mock_function_count_map.clear(); }
TearDown()99   void TearDown() override {}
100 };
101 
TEST_F(StackBtmTest,GlobalLifecycle)102 TEST_F(StackBtmTest, GlobalLifecycle) {
103   get_btm_client_interface().lifecycle.btm_init();
104   get_btm_client_interface().lifecycle.btm_free();
105 }
106 
TEST_F(StackBtmTest,DynamicLifecycle)107 TEST_F(StackBtmTest, DynamicLifecycle) {
108   auto* btm = new tBTM_CB();
109   delete btm;
110 }
111 
TEST_F(StackBtmTest,InformClientOnConnectionSuccess)112 TEST_F(StackBtmTest, InformClientOnConnectionSuccess) {
113   MOCK_bluetooth_shim_is_gd_acl_enabled_ = true;
114 
115   get_btm_client_interface().lifecycle.btm_init();
116 
117   RawAddress bda({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
118 
119   btm_acl_connected(bda, 2, HCI_SUCCESS, false);
120   ASSERT_EQ(static_cast<size_t>(1),
121             mock_function_count_map.count("BTA_dm_acl_up"));
122 
123   get_btm_client_interface().lifecycle.btm_free();
124 }
125 
TEST_F(StackBtmTest,NoInformClientOnConnectionFail)126 TEST_F(StackBtmTest, NoInformClientOnConnectionFail) {
127   MOCK_bluetooth_shim_is_gd_acl_enabled_ = true;
128 
129   get_btm_client_interface().lifecycle.btm_init();
130 
131   RawAddress bda({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
132 
133   btm_acl_connected(bda, 2, HCI_ERR_NO_CONNECTION, false);
134   ASSERT_EQ(static_cast<size_t>(0),
135             mock_function_count_map.count("BTA_dm_acl_up"));
136 
137   get_btm_client_interface().lifecycle.btm_free();
138 }
139 
TEST_F(StackBtmTest,default_packet_type)140 TEST_F(StackBtmTest, default_packet_type) {
141   get_btm_client_interface().lifecycle.btm_init();
142 
143   btm_cb.acl_cb_.SetDefaultPacketTypeMask(0x4321);
144   ASSERT_EQ(0x4321, btm_cb.acl_cb_.DefaultPacketTypes());
145 
146   get_btm_client_interface().lifecycle.btm_free();
147 }
148 
TEST_F(StackBtmTest,change_packet_type)149 TEST_F(StackBtmTest, change_packet_type) {
150   int cnt = 0;
151   get_btm_client_interface().lifecycle.btm_init();
152 
153   btm_cb.acl_cb_.SetDefaultPacketTypeMask(0xffff);
154   ASSERT_EQ(0xffff, btm_cb.acl_cb_.DefaultPacketTypes());
155 
156   // Create connection
157   RawAddress bda({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
158   btm_acl_created(bda, 0x123, HCI_ROLE_CENTRAL, BT_TRANSPORT_BR_EDR);
159 
160   uint64_t features = 0xffffffffffffffff;
161   acl_process_supported_features(0x123, features);
162 
163   mock::btsnd_hcic_change_conn_type = {};
164   uint16_t pkt_types = 0x55aa;
165   btm_set_packet_types_from_address(bda, pkt_types);
166   ASSERT_EQ(++cnt, mock_function_count_map["btsnd_hcic_change_conn_type"]);
167   ASSERT_EQ(0x123, mock::btsnd_hcic_change_conn_type.handle);
168   ASSERT_EQ(0x4400, mock::btsnd_hcic_change_conn_type.packet_types);
169 
170   mock::btsnd_hcic_change_conn_type = {};
171   btm_set_packet_types_from_address(bda, 0xffff);
172   ASSERT_EQ(++cnt, mock_function_count_map["btsnd_hcic_change_conn_type"]);
173   ASSERT_EQ(0x123, mock::btsnd_hcic_change_conn_type.handle);
174   ASSERT_EQ(0xcc00, mock::btsnd_hcic_change_conn_type.packet_types);
175 
176   mock::btsnd_hcic_change_conn_type = {};
177   btm_set_packet_types_from_address(bda, 0x0);
178   // NOTE: The call should not be executed with no bits set
179   ASSERT_EQ(0x0, mock::btsnd_hcic_change_conn_type.handle);
180   ASSERT_EQ(0x0, mock::btsnd_hcic_change_conn_type.packet_types);
181 
182   get_btm_client_interface().lifecycle.btm_free();
183 }
184 
185 }  // namespace
186