• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <cstdio>
17 #include <cstring>
18 #include <gtest/gtest.h>
19 #include <securec.h>
20 
21 #include "common_list.h"
22 #include "softbus_conn_interface.h"
23 #include "softbus_conn_manager.h"
24 #include "softbus_def.h"
25 #include "softbus_errcode.h"
26 #include "softbus_feature_config.h"
27 #include "softbus_log.h"
28 #include "softbus_ble_connection.c"
29 
30 using namespace testing::ext;
31 namespace OHOS {
32 static uint32_t g_connId;
33 
ConnectedCB(unsigned int connectionId,const ConnectionInfo * info)34 void ConnectedCB(unsigned int connectionId, const ConnectionInfo *info)
35 {
36     if (info->type == CONNECT_BLE) {
37         g_connId = connectionId;
38     }
39 }
40 
DisConnectCB(unsigned int connectionId,const ConnectionInfo * info)41 void DisConnectCB(unsigned int connectionId, const ConnectionInfo *info) {}
DataReceivedCB(unsigned int connectionId,ConnModule moduleId,int64_t seq,char * data,int len)42 void DataReceivedCB(unsigned int connectionId, ConnModule moduleId, int64_t seq, char *data, int len) {}
43 
44 class ConnectionBleTest : public testing::Test {
45 public:
46     static void SetUpTestCase();
TearDownTestCase()47     static void TearDownTestCase() {}
SetUp()48     void SetUp() override {}
TearDown()49     void TearDown() override {}
50 };
51 
SetUpTestCase()52 void ConnectionBleTest::SetUpTestCase()
53 {
54     SoftbusConfigInit();
55     ConnServerInit();
56     LooperInit();
57     BleConnLooperInit();
58     SoftBusMutexAttr attr;
59     attr.type = SOFTBUS_MUTEX_RECURSIVE;
60     SoftBusMutexInit(&g_connectionLock, &attr);
61     BleQueueInit();
62 }
63 
64 /*
65 * @tc.name: ManagerTest001
66 * @tc.desc: test ConnTypeIsSupport
67 * @tc.type: FUNC
68 * @tc.require:
69 */
70 HWTEST_F(ConnectionBleTest, ManagerTest001, TestSize.Level1)
71 {
72     int ret;
73     SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "ManagerTest001");
74     ret = ConnTypeIsSupport(CONNECT_BLE);
75     EXPECT_NE(ret, SOFTBUS_OK);
76 }
77 
78 /*
79 * @tc.name: ManagerTest002
80 * @tc.desc: test invalid param
81 * @tc.type: FUNC
82 * @tc.require:
83 */
84 HWTEST_F(ConnectionBleTest, ManagerTest002, TestSize.Level1)
85 {
86     SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "ManagerTest002");
87     int ret = ConnSetConnectCallback(static_cast<ConnModule>(0), nullptr);
88     ASSERT_TRUE(ret != SOFTBUS_OK);
89     ret = ConnConnectDevice(nullptr, 0, nullptr);
90     ASSERT_TRUE(ret != SOFTBUS_OK);
91     ret = ConnDisconnectDevice(0);
92     ASSERT_TRUE(ret != SOFTBUS_OK);
93     ret = ConnPostBytes(0, nullptr);
94     ASSERT_TRUE(ret != SOFTBUS_OK);
95     ret = ConnStartLocalListening(nullptr);
96     ASSERT_TRUE(ret != SOFTBUS_OK);
97     ret = ConnStopLocalListening(nullptr);
98     ASSERT_TRUE(ret != SOFTBUS_OK);
99     ConnUnSetConnectCallback(static_cast<ConnModule>(0));
100     EXPECT_EQ(SOFTBUS_OK, SOFTBUS_OK);
101 }
102 
103 /*
104 * @tc.name: ManagerTest003
105 * @tc.desc: test set unset callback and post disconnect without connect
106 * @tc.type: FUNC
107 * @tc.require:
108 */
109 HWTEST_F(ConnectionBleTest, ManagerTest003, TestSize.Level1)
110 {
111     SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "ManagerTest003");
112     ConnectCallback connCb;
113     connCb.OnConnected = ConnectedCB;
114     connCb.OnDisconnected = DisConnectCB;
115     connCb.OnDataReceived = DataReceivedCB;
116     int ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
117     EXPECT_EQ(SOFTBUS_OK, ret);
118     ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
119     g_connId = 0;
120 }
121 
122 /*
123 * @tc.name: ManagerTest004
124 * @tc.desc: Test start stop listening.
125 * @tc.in: Test module, Test number,Test Levels.
126 * @tc.out: NonZero
127 * @tc.type: FUNC
128 * @tc.require: The ConnStartLocalListening and ConnStopLocalListening operates normally.
129 */
130 HWTEST_F(ConnectionBleTest, ManagerTest004, TestSize.Level1)
131 {
132     SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "ManagerTest004");
133     ConnectCallback connCb;
134     connCb.OnConnected = ConnectedCB;
135     connCb.OnDisconnected = DisConnectCB;
136     connCb.OnDataReceived = DataReceivedCB;
137     int ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
138     EXPECT_EQ(ret, SOFTBUS_OK);
139 
140     LocalListenerInfo info;
141     info.type = CONNECT_BLE;
142     ret = ConnStartLocalListening(&info);
143     EXPECT_NE(ret, SOFTBUS_OK);
144     ret = ConnStopLocalListening(&info);
145     EXPECT_NE(ret, SOFTBUS_OK);
146     ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
147     g_connId = 0;
148 }
149 
150 /*
151 * @tc.name: ManagerTest005
152 * @tc.desc: Test ConnTypeIsSupport.
153 * @tc.in: Test module, Test number, Test Levels.
154 * @tc.out: NonZero
155 * @tc.type: FUNC
156 * @tc.require: The ConnTypeIsSupport operates normally.
157 */
158 HWTEST_F(ConnectionBleTest, ManagerTest005, TestSize.Level1)
159 {
160     SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "ManagerTest005");
161     int ret = ConnTypeIsSupport(CONNECT_P2P);
162     EXPECT_EQ(SOFTBUS_CONN_MANAGER_OP_NOT_SUPPORT, ret);
163 }
164 
165 /*
166 * @tc.name: ManagerTest006
167 * @tc.desc: Test ConnTypeIsSupport.
168 * @tc.in: Test module, Test number, Test Levels.
169 * @tc.out: Zero
170 * @tc.type: FUNC
171 * @tc.require: The ConnTypeIsSupport operates normally.
172 */
173 HWTEST_F(ConnectionBleTest, ManagerTest006, TestSize.Level1)
174 {
175     SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "ManagerTest006");
176     int ret = ConnTypeIsSupport(CONNECT_BR);
177     EXPECT_EQ(SOFTBUS_OK, ret);
178 }
179 
180 /*
181 * @tc.name: ManagerTest007
182 * @tc.desc: Test ConnTypeIsSupport.
183 * @tc.in: Test module, Test number, Test Levels.
184 * @tc.out: Zero
185 * @tc.type: FUNC
186 * @tc.require: The ConnTypeIsSupport operates normally.
187 */
188 HWTEST_F(ConnectionBleTest, ManagerTest007, TestSize.Level1)
189 {
190     SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "ManagerTest007");
191     int ret = ConnTypeIsSupport(CONNECT_TCP);
192     EXPECT_EQ(SOFTBUS_OK, ret);
193 }
194 
195 /*
196  * @tc.name: PackRequest
197  * @tc.desc: test reference process is valid
198  * @tc.in: test module, test number, test levels.
199  * @tc.out: Zero
200  * @tc.type: FUNC
201  * @tc.require: NA
202  */
203 HWTEST_F(ConnectionBleTest, PackRequest, TestSize.Level1)
204 {
205     BleConnectionInfo *info = CreateBleConnectionNode();
206     info->refCount = CONNECT_REF_INCRESE;
207     ListAdd(&g_connection_list, &info->node);
208     PackRequest(CONNECT_REF_DECRESE, info->connId);
209     EXPECT_EQ(info->state, BLE_CONNECTION_STATE_CLOSING);
210 
211     ListDelete(&info->node);
212     SoftBusFree(info);
213 }
214 
215 /*
216  * @tc.name: OnPackResponse
217  * @tc.desc: test reference process is valid
218  * @tc.in: test module, test number, test levels.
219  * @tc.out: Zero
220  * @tc.type: FUNC
221  * @tc.require: NA
222  */
223 HWTEST_F(ConnectionBleTest, OnPackResponse, TestSize.Level1)
224 {
225     BleConnectionInfo *info = CreateBleConnectionNode();
226     info->state = BLE_CONNECTION_STATE_CLOSING;
227     ListAdd(&g_connection_list, &info->node);
228     OnPackResponse(CONNECT_REF_INCRESE, CONNECT_REF_INCRESE, info->connId);
229     EXPECT_EQ(info->state, BLE_CONNECTION_STATE_CONNECTED);
230 
231     ListDelete(&info->node);
232     SoftBusFree(info);
233 }
234 
235 /*
236  * @tc.name: BleConnectionMsgHandler
237  * @tc.desc: test message handle function
238  * @tc.in: test module, test number, test levels.
239  * @tc.out: Zero
240  * @tc.type: FUNC
241  * @tc.require: NA
242  */
243 HWTEST_F(ConnectionBleTest, BleConnectionMsgHandler, TestSize.Level1)
244 {
245     SoftBusMessage *message = MallocMessage();
246     message->what = BLE_CONNECTION_DISCONNECT_OUT;
247     BleConnectionMsgHandler(message);
248 
249     BleConnectionInfo *info = CreateBleConnectionNode();
250     info->state = BLE_CONNECTION_STATE_CLOSED;
251     ListAdd(&g_connection_list, &info->node);
252     message->arg1 = info->connId;
253     BleConnectionMsgHandler(message);
254     EXPECT_EQ(info->state, BLE_CONNECTION_STATE_CLOSED);
255 
256     FreeMessage(message);
257     ListDelete(&info->node);
258     SoftBusFree(info);
259 }
260 
261 /*
262  * @tc.name: BleConnectionRemoveMessageFunc
263  * @tc.desc: remove message function
264  * @tc.in: test module, test number, test levels.
265  * @tc.out: Zero
266  * @tc.type: FUNC
267  * @tc.require: NA
268  */
269 HWTEST_F(ConnectionBleTest, BleConnectionRemoveMessageFunc, TestSize.Level1)
270 {
271     int64_t clientId = INT32_MAX;
272     SoftBusMessage *message = MallocMessage();
273     message->what = BLE_CONNECTION_DISCONNECT_OUT;
274     message->arg1 = clientId;
275     BleConnectionRemoveMessageFunc(message, &clientId);
276 }
277 }