1 /*
2  * Copyright 2021 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 <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <string.h>
20 
21 #include <cstdint>
22 #include <map>
23 #include <memory>
24 #include <string>
25 
26 #include "bta/pan/bta_pan_int.h"
27 #include "test/common/main_handler.h"
28 #include "test/common/mock_functions.h"
29 #include "test/mock/mock_stack_pan_api.h"
30 
31 using namespace testing;
32 
33 namespace {
34 
35 struct UuidPiece {
36   uint16_t uuid16;
37   bool adding;
38 };
39 
40 struct EventPiece {
41   tBTA_PAN_EVT event;
42   tBTA_PAN data;
43 };
44 
45 std::function<void(tBTA_PAN_EVT, tBTA_PAN*)> bta_pan_event_closure;
BTA_PAN_CBACK(tBTA_PAN_EVT event,tBTA_PAN * p_data)46 void BTA_PAN_CBACK(tBTA_PAN_EVT event, tBTA_PAN* p_data) {
47   bta_pan_event_closure(event, p_data);
48 }
49 
50 std::function<void(uint16_t, bool)> bta_sys_eir_closure;
BTA_SYS_EIR_CBACK(uint16_t uuid16,bool adding)51 void BTA_SYS_EIR_CBACK(uint16_t uuid16, bool adding) {
52   bta_sys_eir_closure(uuid16, adding);
53 }
54 
55 constexpr tBTA_PAN kNoData = {};
56 
57 }  // namespace
58 
59 class BtaPanTest : public ::testing::Test {
60  protected:
SetUp()61   void SetUp() override {
62     uuids.clear();
63     main_thread_start_up();
64     bta_pan_event_closure = [this](tBTA_PAN_EVT event, tBTA_PAN* data) {
65       events.push_back({
66           .event = event,
67           .data = (data == nullptr) ? kNoData : *data,
68       });
69     };
70     tBTA_PAN_DATA data = {
71         .api_enable =
72             {
73                 .p_cback = BTA_PAN_CBACK,
74             },
75     };
76     bta_sys_eir_closure = [this](uint16_t uuid16, bool adding) {
77       uuids.push_back({
78           .uuid16 = uuid16,
79           .adding = adding,
80       });
81     };
82     bta_pan_enable(&data);
83     sync_main_handler();
84     auto e = events.front();
85     events.pop_front();
86     ASSERT_EQ(BTA_PAN_ENABLE_EVT, e.event);
87   }
88 
TearDown()89   void TearDown() override {
90     bta_pan_disable();
91     sync_main_handler();
92     main_thread_shut_down();
93   }
94 
95   std::deque<struct EventPiece> events;
96   std::deque<struct UuidPiece> uuids;
97 };
98 
99 class BtaPanRegisteredTest : public BtaPanTest {
100  protected:
SetUp()101   void SetUp() {
102     BtaPanTest::SetUp();
103     bta_sys_eir_register(BTA_SYS_EIR_CBACK);
104   }
105 
TearDown()106   void TearDown() {
107     bta_sys_eir_unregister();
108     BtaPanTest::TearDown();
109   }
110 };
111 
TEST_F(BtaPanTest,nop)112 TEST_F(BtaPanTest, nop) {}
113 
TEST_F(BtaPanRegisteredTest,BTA_PanSetRole_Null)114 TEST_F(BtaPanRegisteredTest, BTA_PanSetRole_Null) {
115   tBTA_PAN_ROLE role = BTA_PAN_ROLE_PANU | BTA_PAN_ROLE_NAP;
116   tBTA_PAN_ROLE_INFO user_info = {
117       .p_srv_name = std::string(),
118       .app_id = 12,
119   };
120 
121   tBTA_PAN_ROLE_INFO nap_info = {
122       .p_srv_name = std::string(),
123       .app_id = 34,
124   };
125 
126   BTA_PanSetRole(role, user_info, nap_info);
127 
128   // Wait for main thread to complete
129   sync_main_handler();
130 
131   ASSERT_EQ(12, bta_pan_cb.app_id[0]);
132   ASSERT_EQ(0, bta_pan_cb.app_id[1]);
133   ASSERT_EQ(34, bta_pan_cb.app_id[2]);
134 
135   ASSERT_EQ(2U, uuids.size());
136   ASSERT_EQ(0x1116, uuids[0].uuid16);
137   ASSERT_EQ(true, uuids[0].adding);
138   ASSERT_EQ(0x1115, uuids[1].uuid16);
139   ASSERT_EQ(true, uuids[1].adding);
140 }
141 
TEST_F(BtaPanRegisteredTest,BTA_PanSetRole_WithNames)142 TEST_F(BtaPanRegisteredTest, BTA_PanSetRole_WithNames) {
143   tBTA_PAN_ROLE role = BTA_PAN_ROLE_PANU | BTA_PAN_ROLE_NAP;
144   tBTA_PAN_ROLE_INFO user_info = {
145       .p_srv_name = "TestPanUser",
146       .app_id = 12,
147   };
148   tBTA_PAN_ROLE_INFO nap_info = {
149       .p_srv_name = "TestPanNap",
150       .app_id = 34,
151   };
152 
153   uint8_t stack_pan_role;
154   std::string stack_pan_user_name;
155   std::string stack_pan_nap_name;
156 
157   test::mock::stack_pan_api::PAN_SetRole.body =
158       [&stack_pan_role, &stack_pan_user_name, &stack_pan_nap_name](
159           uint8_t role, std::string user_name,
160           std::string nap_name) -> tPAN_RESULT {
161     stack_pan_role = role;
162     stack_pan_user_name = user_name;
163     stack_pan_nap_name = nap_name;
164     return PAN_SUCCESS;
165   };
166 
167   bta_sys_eir_register(BTA_SYS_EIR_CBACK);
168   BTA_PanSetRole(role, user_info, nap_info);
169 
170   // Wait for main thread to complete
171   sync_main_handler();
172 
173   ASSERT_EQ(1UL, events.size());
174   auto e = events.front();
175   events.pop_front();
176   ASSERT_EQ(BTA_PAN_SET_ROLE_EVT, e.event);
177   ASSERT_EQ(BTA_PAN_ROLE_PANU | BTA_PAN_ROLE_NAP, e.data.set_role.role);
178   ASSERT_EQ(BTA_PAN_SUCCESS, e.data.set_role.status);
179 
180   ASSERT_EQ(BTA_PAN_ROLE_PANU | BTA_PAN_ROLE_NAP, stack_pan_role);
181   ASSERT_THAT("TestPanUser", StrEq(stack_pan_user_name));
182   ASSERT_THAT("TestPanNap", StrEq(stack_pan_nap_name));
183 
184   test::mock::stack_pan_api::PAN_SetRole = {};
185 }
186 
187 constexpr size_t kBtaServiceNameLen = static_cast<size_t>(BTA_SERVICE_NAME_LEN);
188 
TEST_F(BtaPanRegisteredTest,BTA_PanSetRole_WithLongNames)189 TEST_F(BtaPanRegisteredTest, BTA_PanSetRole_WithLongNames) {
190   tBTA_PAN_ROLE role = BTA_PAN_ROLE_PANU | BTA_PAN_ROLE_NAP;
191   tBTA_PAN_ROLE_INFO user_info = {
192       .p_srv_name = std::string(200, 'A'),
193       .app_id = 12,
194   };
195   ASSERT_EQ(200UL, user_info.p_srv_name.size());
196 
197   tBTA_PAN_ROLE_INFO nap_info = {
198       .p_srv_name = std::string(201, 'A'),
199       .app_id = 34,
200   };
201   ASSERT_EQ(201UL, nap_info.p_srv_name.size());
202 
203   uint8_t stack_pan_role;
204   std::string stack_pan_user_name;
205   std::string stack_pan_nap_name;
206 
207   test::mock::stack_pan_api::PAN_SetRole.body =
208       [&stack_pan_role, &stack_pan_user_name, &stack_pan_nap_name](
209           uint8_t role, std::string user_name,
210           std::string nap_name) -> tPAN_RESULT {
211     stack_pan_role = role;
212     stack_pan_user_name = user_name;
213     stack_pan_nap_name = nap_name;
214     return PAN_SUCCESS;
215   };
216 
217   bta_sys_eir_register(BTA_SYS_EIR_CBACK);
218   BTA_PanSetRole(role, user_info, nap_info);
219 
220   // Wait for main thread to complete
221   sync_main_handler();
222 
223   ASSERT_EQ(1UL, events.size());
224   auto e = events.front();
225   events.pop_front();
226   ASSERT_EQ(BTA_PAN_SET_ROLE_EVT, e.event);
227   ASSERT_EQ(BTA_PAN_ROLE_PANU | BTA_PAN_ROLE_NAP, e.data.set_role.role);
228   ASSERT_EQ(BTA_PAN_SUCCESS, e.data.set_role.status);
229 
230   ASSERT_EQ(BTA_PAN_ROLE_PANU | BTA_PAN_ROLE_NAP, stack_pan_role);
231 
232   ASSERT_EQ(kBtaServiceNameLen, stack_pan_user_name.size());
233   ASSERT_EQ(kBtaServiceNameLen, stack_pan_nap_name.size());
234 
235   ASSERT_THAT(stack_pan_user_name, StrEq(std::string(kBtaServiceNameLen, 'A')));
236   ASSERT_THAT(stack_pan_user_name,
237               StrEq("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
238   ASSERT_THAT(stack_pan_nap_name, StrEq(std::string(kBtaServiceNameLen, 'A')));
239   ASSERT_THAT(stack_pan_nap_name, StrEq("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
240 
241   test::mock::stack_pan_api::PAN_SetRole = {};
242 }
243