1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "br_mock.h"
17 #include "softbus_conn_br_connection.h"
18 #include "softbus_conn_br_manager.h"
19 #include "softbus_conn_br_trans.h"
20
21 #include <cstdio>
22 #include <cstdlib>
23 #include <cstring>
24 #include <gtest/gtest.h>
25 #include <securec.h>
26
27 #include "common_list.h"
28 #include "conn_log.h"
29 #include "softbus_adapter_mem.h"
30 #include "softbus_adapter_thread.h"
31 #include "softbus_errcode.h"
32 #include "wrapper_br_interface.h"
33
34 #include "softbus_conn_br_pending_packet.h"
35 #include "softbus_conn_br_send_queue.h"
36 #include "softbus_conn_interface.h"
37 #include "softbus_conn_manager.h"
38
39 using namespace testing::ext;
40 using namespace testing;
41 using namespace std;
42
43 #define BR_READ_FAILED (-1)
44 #define BR_WRITE_FAILED (-2)
45 namespace OHOS {
46 extern "C" {
OnConnected(uint32_t connectionId,const ConnectionInfo * info)47 void OnConnected(uint32_t connectionId, const ConnectionInfo *info)
48 {
49 (void)connectionId;
50 (void)info;
51 return;
52 }
53
OnReusedConnected(uint32_t connectionId,const ConnectionInfo * info)54 void OnReusedConnected(uint32_t connectionId, const ConnectionInfo *info)
55 {
56 (void)connectionId;
57 (void)info;
58 return;
59 }
60
OnDisconnected(uint32_t connectionId,const ConnectionInfo * info)61 void OnDisconnected(uint32_t connectionId, const ConnectionInfo *info)
62 {
63 (void)connectionId;
64 (void)info;
65 return;
66 }
67
OnDataReceived(uint32_t connectionId,ConnModule moduleId,int64_t seq,char * data,int32_t len)68 void OnDataReceived(uint32_t connectionId, ConnModule moduleId, int64_t seq, char *data, int32_t len)
69 {
70 (void)connectionId;
71 (void)moduleId;
72 (void)seq;
73 (void)data;
74 (void)len;
75 return;
76 }
77
Init(const struct tagSppSocketDriver * sppDriver)78 void Init(const struct tagSppSocketDriver *sppDriver)
79 {
80 (void)sppDriver;
81 return;
82 }
83
Read(int32_t clientFd,uint8_t * buf,const int32_t length)84 int32_t Read(int32_t clientFd, uint8_t *buf, const int32_t length)
85 {
86 (void)clientFd;
87 (void)buf;
88 if (length <= 0) {
89 return BR_READ_FAILED;
90 }
91 return SOFTBUS_OK;
92 }
93
Write(int32_t clientFd,const uint8_t * buf,const int32_t length)94 int32_t Write(int32_t clientFd, const uint8_t *buf, const int32_t length)
95 {
96 (void)clientFd;
97 (void)buf;
98 if (length <= 0) {
99 return BR_WRITE_FAILED;
100 }
101 return SOFTBUS_OK;
102 }
103
OnPostByteFinshed(uint32_t connectionId,uint32_t len,int32_t pid,int32_t flag,int32_t module,int64_t seq,int32_t error)104 void OnPostByteFinshed(
105 uint32_t connectionId, uint32_t len, int32_t pid, int32_t flag, int32_t module, int64_t seq, int32_t error)
106 {
107 (void)connectionId;
108 (void)len;
109 (void)pid;
110 (void)flag;
111 (void)module;
112 (void)seq;
113 (void)error;
114 return;
115 }
116
OnConnectSuccessed(uint32_t requestId,uint32_t connectionId,const ConnectionInfo * info)117 void OnConnectSuccessed(uint32_t requestId, uint32_t connectionId, const ConnectionInfo *info)
118 {
119 (void)requestId;
120 (void)connectionId;
121 (void)info;
122 return;
123 }
124
OnConnectFailed(uint32_t requestId,int32_t reason)125 void OnConnectFailed(uint32_t requestId, int32_t reason)
126 {
127 (void)requestId;
128 (void)reason;
129 return;
130 }
131 }
132 class ConnectionBrConnectionTest : public testing::Test {
133 public:
ConnectionBrConnectionTest()134 ConnectionBrConnectionTest() { }
~ConnectionBrConnectionTest()135 ~ConnectionBrConnectionTest() { }
136 static void SetUpTestCase(void);
137 static void TearDownTestCase(void);
138 void SetUp();
139 void TearDown();
140 };
141
SetUpTestCase(void)142 void ConnectionBrConnectionTest::SetUpTestCase(void)
143 {
144 NiceMock<ConnectionBrInterfaceMock>InitMock;
145 EXPECT_CALL(InitMock, SoftBusThreadCreate).WillOnce(Return(SOFTBUS_OK));
146 LooperInit();
147 }
148
TearDownTestCase(void)149 void ConnectionBrConnectionTest::TearDownTestCase(void)
150 {
151 LooperDeinit();
152 }
153
SetUp(void)154 void ConnectionBrConnectionTest::SetUp(void) { }
155
TearDown(void)156 void ConnectionBrConnectionTest::TearDown(void) { }
157
158 SppSocketDriver g_sppDriver = {
159 .Init = Init,
160 .Read = Read,
161 .Write = Write,
162 };
163
164 ConnBrTransEventListener g_transEventlistener = {
165 .onPostByteFinshed = OnPostByteFinshed,
166 };
167
168 ConnectFuncInterface *connectFuncInterface = NULL;
169 ConnectFuncInterface *g_connectFuncInterface = NULL;
170
ConnInit(void)171 ConnectFuncInterface *ConnInit(void)
172 {
173 LooperInit();
174 ConnectCallback callback = {
175 .OnConnected = OnConnected,
176 .OnDisconnected = OnDisconnected,
177 .OnDataReceived = OnDataReceived,
178 };
179 NiceMock<ConnectionBrInterfaceMock>brMock;
180
181 EXPECT_CALL(brMock, InitSppSocketDriver).WillOnce(Return(&g_sppDriver));
182 EXPECT_CALL(brMock, SoftbusGetConfig)
183 .WillOnce(ConnectionBrInterfaceMock::ActionOfSoftbusGetConfig1)
184 .WillOnce(ConnectionBrInterfaceMock::ActionOfSoftbusGetConfig2);
185 EXPECT_CALL(brMock, ConnBrInnerQueueInit).WillOnce(Return(SOFTBUS_OK));
186 EXPECT_CALL(brMock, SoftBusThreadCreate)
187 .WillOnce(Return(SOFTBUS_OK))
188 .WillOnce(Return(SOFTBUS_OK));
189 EXPECT_CALL(brMock, SoftBusAddBtStateListener).WillOnce(Return(SOFTBUS_OK));
190 EXPECT_CALL(brMock, ConnBrInitBrPendingPacket)
191 .WillOnce(Return(SOFTBUS_OK))
192 .WillOnce(Return(SOFTBUS_OK));
193
194 connectFuncInterface = ConnInitBr(&callback);
195 return connectFuncInterface;
196 }
197
198 HWTEST_F(ConnectionBrConnectionTest, BrManagerTest001, TestSize.Level1)
199 {
200 CONN_LOGI(CONN_BR, "ConnInitBr1, Start");
201
202 ConnectCallback callback = {
203 .OnConnected = OnConnected,
204 .OnDisconnected = OnDisconnected,
205 .OnDataReceived = OnDataReceived,
206 };
207 NiceMock<ConnectionBrInterfaceMock>brMock;
208
209 EXPECT_CALL(brMock, InitSppSocketDriver).WillOnce(Return(&g_sppDriver));
210 EXPECT_CALL(brMock, SoftbusGetConfig).WillOnce(Return(SOFTBUS_NO_INIT));
211 ConnectFuncInterface *ret = ConnInitBr(&callback);
212 EXPECT_EQ(NULL, ret);
213
214 EXPECT_CALL(brMock, InitSppSocketDriver).WillOnce(Return(&g_sppDriver));
215 EXPECT_CALL(brMock, SoftbusGetConfig).WillOnce(Return(SOFTBUS_OK));
216 ret = ConnInitBr(&callback);
217 EXPECT_EQ(NULL, ret);
218
219 EXPECT_CALL(brMock, InitSppSocketDriver).WillOnce(Return(&g_sppDriver));
220 EXPECT_CALL(brMock, SoftbusGetConfig)
221 .WillOnce(ConnectionBrInterfaceMock::ActionOfSoftbusGetConfig1)
222 .WillOnce(Return(SOFTBUS_NO_INIT));
223 ret = ConnInitBr(&callback);
224 EXPECT_EQ(NULL, ret);
225 }
226
227 HWTEST_F(ConnectionBrConnectionTest, BrManagerTest002, TestSize.Level1)
228 {
229 CONN_LOGI(CONN_BR, "ConnInitBr2, Start");
230
231 ConnectCallback callback = {
232 .OnConnected = OnConnected,
233 .OnDisconnected = OnDisconnected,
234 .OnDataReceived = OnDataReceived,
235 };
236 NiceMock<ConnectionBrInterfaceMock>brMock;
237
238 EXPECT_CALL(brMock, InitSppSocketDriver).WillOnce(Return(&g_sppDriver));
239 EXPECT_CALL(brMock, SoftbusGetConfig)
240 .WillOnce(ConnectionBrInterfaceMock::ActionOfSoftbusGetConfig1)
241 .WillOnce(ConnectionBrInterfaceMock::ActionOfSoftbusGetConfig2);
242 EXPECT_CALL(brMock, ConnBrInnerQueueInit).WillOnce(Return(SOFTBUS_NO_INIT));
243 ConnectFuncInterface *ret = ConnInitBr(&callback);
244 EXPECT_EQ(NULL, ret);
245
246 EXPECT_CALL(brMock, InitSppSocketDriver).WillOnce(Return(&g_sppDriver));
247 EXPECT_CALL(brMock, SoftbusGetConfig)
248 .WillOnce(ConnectionBrInterfaceMock::ActionOfSoftbusGetConfig1)
249 .WillOnce(ConnectionBrInterfaceMock::ActionOfSoftbusGetConfig2);
250 EXPECT_CALL(brMock, ConnBrInnerQueueInit).WillOnce(Return(SOFTBUS_OK));
251 EXPECT_CALL(brMock, SoftBusThreadCreate)
252 .WillOnce(Return(SOFTBUS_OK))
253 .WillOnce(Return(SOFTBUS_INVALID_PARAM));
254 ret = ConnInitBr(&callback);
255 EXPECT_EQ(NULL, ret);
256 }
257
258 HWTEST_F(ConnectionBrConnectionTest, BrManagerTest003, TestSize.Level1)
259 {
260 CONN_LOGI(CONN_BR, "ConnInitBr3, Start");
261
262 NiceMock<ConnectionBrInterfaceMock>brMock;
263 ConnectOption *option = (ConnectOption *)SoftBusCalloc(sizeof(ConnectOption));
264 option->type = CONNECT_BR;
265 (void)strcpy_s(option->brOption.brMac, BT_MAC_LEN, "24:DA:33:6A:06:EC");
266 uint32_t requestId = 1;
267 ConnectResult result = {
268 .OnConnectSuccessed = OnConnectSuccessed,
269 .OnConnectFailed = OnConnectFailed,
270 };
271
272 g_connectFuncInterface = ConnInit();
273 int32_t ret = g_connectFuncInterface->ConnectDevice(option, requestId, &result);
274 EXPECT_EQ(SOFTBUS_OK, ret);
275 }
276
277 HWTEST_F(ConnectionBrConnectionTest, BrManagerTest004, TestSize.Level1)
278 {
279 CONN_LOGI(CONN_BR, "ConnInitBr4, Start");
280
281 NiceMock<ConnectionBrInterfaceMock>brMock;
282 uint32_t connectionId = 1;
283 uint8_t *data1 = (uint8_t *)SoftBusCalloc(sizeof(uint8_t));
284 (void)memset_s(data1, sizeof(uint8_t), 0, sizeof(uint8_t));
285 int32_t pid = 1;
286 int32_t flag = 1;
287 int64_t seq = 1;
288 int32_t ret = g_connectFuncInterface->PostBytes(connectionId, data1, 0, pid, flag, MODULE_BLE_CONN, seq);
289 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
290
291 uint8_t *data2 = (uint8_t *)SoftBusCalloc(sizeof(uint8_t));
292 (void)memset_s(data2, sizeof(uint8_t), 0, sizeof(uint8_t));
293 ConnBrDevice *device = (ConnBrDevice *)SoftBusCalloc(sizeof(ConnBrDevice));
294 (void)strcpy_s(device->addr, BT_MAC_LEN, "24:DA:33:6A:06:EC");
295 ret = g_connectFuncInterface->PostBytes(connectionId, data2, 3, pid, flag, MODULE_BLE_CONN, seq);
296 EXPECT_EQ(SOFTBUS_CONN_BR_CONNECTION_NOT_EXIST_ERR, ret);
297
298 uint8_t *data3 = (uint8_t *)SoftBusCalloc(sizeof(uint8_t));
299 (void)memset_s(data3, sizeof(uint8_t), 0, sizeof(uint8_t));
300 ConnBrConnection *connection = ConnBrCreateConnection(device->addr, CONN_SIDE_CLIENT, INVALID_SOCKET_HANDLE);
301 ConnBrSaveConnection(connection);
302 ret = g_connectFuncInterface->PostBytes(connection->connectionId, data3, 3, pid, flag, MODULE_BLE_CONN, seq);
303 EXPECT_EQ(SOFTBUS_CONN_BR_CONNECTION_NOT_READY_ERR, ret);
304
305 uint8_t *data4 = (uint8_t *)SoftBusCalloc(sizeof(uint8_t));
306 (void)memset_s(data4, sizeof(uint8_t), 0, sizeof(uint8_t));
307 EXPECT_CALL(brMock, ConnBrEnqueueNonBlock).WillOnce(Return(SOFTBUS_OK));
308 ret = g_connectFuncInterface->PostBytes(connection->connectionId, data4, 3, pid, flag, MODULE_CONNECTION, seq);
309 EXPECT_EQ(SOFTBUS_OK, ret);
310 }
311
312 HWTEST_F(ConnectionBrConnectionTest, BrManagerTest005, TestSize.Level1)
313 {
314 CONN_LOGI(CONN_BR, "ConnInitBr5, Start");
315
316 ConnBrDevice *device = (ConnBrDevice *)SoftBusCalloc(sizeof(ConnBrDevice));
317 (void)strcpy_s(device->addr, BT_MAC_LEN, "24:DA:33:6A:06:EC");
318
319 g_connectFuncInterface = ConnInit();
320 ConnBrConnection *connection = ConnBrCreateConnection(device->addr, CONN_SIDE_CLIENT, INVALID_SOCKET_HANDLE);
321 ConnBrSaveConnection(connection);
322 int32_t ret = g_connectFuncInterface->DisconnectDevice(connection->connectionId);
323 EXPECT_EQ(SOFTBUS_OK, ret);
324 }
325
326 HWTEST_F(ConnectionBrConnectionTest, BrManagerTest006, TestSize.Level1)
327 {
328 CONN_LOGI(CONN_BR, "ConnInitBr6, Start");
329
330 ConnectOption *option = (ConnectOption *)SoftBusCalloc(sizeof(ConnectOption));
331 option->type = CONNECT_BR;
332 option->brOption.sideType = CONN_SIDE_ANY;
333 (void)strcpy_s(option->brOption.brMac, BT_MAC_LEN, "24:DA:33:6A:06:EC");
334 ConnBrDevice *device = (ConnBrDevice *)SoftBusCalloc(sizeof(ConnBrDevice));
335 (void)strcpy_s(device->addr, BT_MAC_LEN, "24:DA:33:6A:06:EC");
336
337 g_connectFuncInterface = ConnInit();
338 ConnBrConnection *connection = ConnBrCreateConnection(device->addr, CONN_SIDE_CLIENT, INVALID_SOCKET_HANDLE);
339 ConnBrSaveConnection(connection);
340 int32_t ret = g_connectFuncInterface->DisconnectDeviceNow(option);
341 EXPECT_EQ(SOFTBUS_OK, ret);
342 }
343
344 HWTEST_F(ConnectionBrConnectionTest, BrManagerTest007, TestSize.Level1)
345 {
346 CONN_LOGI(CONN_BR, "ConnInitBr7, Start");
347
348 ConnBrDevice *device = (ConnBrDevice *)SoftBusCalloc(sizeof(ConnBrDevice));
349 (void)strcpy_s(device->addr, BT_MAC_LEN, "24:DA:33:6A:06:EC");
350 ConnectionInfo *info = (ConnectionInfo *)SoftBusCalloc(sizeof(ConnectionInfo));
351 (void)strcpy_s(info->brInfo.brMac, BT_MAC_LEN, "24:DA:33:6A:06:EC");
352
353 g_connectFuncInterface = ConnInit();
354 ConnBrConnection *connection = ConnBrCreateConnection(device->addr, CONN_SIDE_CLIENT, INVALID_SOCKET_HANDLE);
355 ConnBrSaveConnection(connection);
356 int32_t ret = g_connectFuncInterface->GetConnectionInfo(connection->connectionId, info);
357 EXPECT_EQ(SOFTBUS_OK, ret);
358 }
359
360 HWTEST_F(ConnectionBrConnectionTest, BrManagerTest008, TestSize.Level1)
361 {
362 CONN_LOGI(CONN_BR, "ConnInitBr8, Start");
363
364 NiceMock<ConnectionBrInterfaceMock>brMock;
365 LocalListenerInfo *info = (LocalListenerInfo *)SoftBusCalloc(sizeof(LocalListenerInfo));
366 info->type = CONNECT_BR;
367 EXPECT_CALL(brMock, SoftBusThreadCreate).WillOnce(Return(SOFTBUS_ERR));
368 int32_t ret = g_connectFuncInterface->StartLocalListening(info);
369 EXPECT_EQ(SOFTBUS_ERR, ret);
370
371 EXPECT_CALL(brMock, SoftBusThreadCreate).WillOnce(Return(SOFTBUS_OK));
372 ret = g_connectFuncInterface->StartLocalListening(info);
373 EXPECT_EQ(SOFTBUS_OK, ret);
374
375 ret = g_connectFuncInterface->StopLocalListening(info);
376 EXPECT_EQ(SOFTBUS_OK, ret);
377 }
378
379 HWTEST_F(ConnectionBrConnectionTest, BrManagerTest009, TestSize.Level1)
380 {
381 CONN_LOGI(CONN_BR, "ConnInitBr9, Start");
382
383 ConnectOption *option = (ConnectOption *)SoftBusCalloc(sizeof(ConnectOption));
384 option->type = CONNECT_BR;
385 option->brOption.sideType = CONN_SIDE_ANY;
386 (void)strcpy_s(option->brOption.brMac, BT_MAC_LEN, "24:DA:33:6A:06:EC");
387 uint32_t time = 1;
388
389 int32_t ret = g_connectFuncInterface->PreventConnection(option, time);
390 EXPECT_EQ(SOFTBUS_OK, ret);
391 }
392 }