1 /*
2 * Copyright (c) 2023 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 #define private public
17 #include "parcel.h"
18 #include "coordination_softbus_adapter_test.h"
19
20 namespace OHOS {
21 namespace Msdp {
22 namespace DeviceStatus {
23 using namespace testing::ext;
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "CoordinationSoftbusAdapterTest" };
26 auto g_adapter = CoordinationSoftbusAdapter::GetInstance();
27 bool g_sendable { false };
28 bool g_init { false };
29 bool g_cond { false };
30 const std::string LOCAL_NETWORKID { "testLocalNetworkId" };
31 const std::string REMOTE_NETWORKID { "testRemoteNetworkId" };
32 const std::string ORIGIN_NETWORKID { "testOriginNetworkId" };
33 const std::string DEVICE_ID { "testDeviceId" };
34 constexpr int32_t SOCKET { 1 };
35 } // namespace
36
ClearCoordinationSoftbusAdapter()37 void ClearCoordinationSoftbusAdapter()
38 {
39 COOR_SOFTBUS_ADAPTER->socketFd_ = -1;
40 COOR_SOFTBUS_ADAPTER->localSessionName_ = "";
41 COOR_SOFTBUS_ADAPTER->onRecvDataCallback_ = nullptr;
42 COOR_SOFTBUS_ADAPTER->sessionDevs_.clear();
43 }
44
SendMsg(int32_t sessionId,const std::string & message)45 int32_t CoordinationSoftbusAdapter::SendMsg(int32_t sessionId, const std::string &message)
46 {
47 return g_sendable == true ? RET_OK : RET_ERR;
48 }
49
Init()50 int32_t CoordinationSoftbusAdapter::Init()
51 {
52 return g_init == true ? RET_OK : RET_ERR;
53 }
54
WaitSessionOpend(const std::string & remoteNetworkId,int32_t sessionId)55 int32_t CoordinationSoftbusAdapter::WaitSessionOpend(const std::string &remoteNetworkId, int32_t sessionId)
56 {
57 return g_cond == true ? RET_OK : RET_ERR;
58 }
59
SetUpTestCase()60 void CoordinationSoftbusAdapterTest::SetUpTestCase() {}
61
TearDownTestCase()62 void CoordinationSoftbusAdapterTest::TearDownTestCase() {}
63
SetUp()64 void CoordinationSoftbusAdapterTest::SetUp() {}
65
TearDown()66 void CoordinationSoftbusAdapterTest::TearDown() {}
67
68 /**
69 * @tc.name: CoordinationSoftbusAdapterTest001
70 * @tc.desc: Test func named StartRemoteCoordination, sessionDevs_ is null
71 * @tc.type: FUNC
72 */
73 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest001, TestSize.Level0)
74 {
75 CALL_TEST_DEBUG;
76 ASSERT_NE(g_adapter, nullptr);
77 int32_t ret = g_adapter->StartRemoteCoordination(LOCAL_NETWORKID, REMOTE_NETWORKID, false);
78 EXPECT_TRUE(ret == RET_ERR);
79 ret = g_adapter->StopRemoteCoordination(REMOTE_NETWORKID, true);
80 EXPECT_TRUE(ret == RET_ERR);
81 }
82
83 /**
84 * @tc.name: CoordinationSoftbusAdapterTest002
85 * @tc.desc: Test func named StartRemoteCoordination, sessionDevs_ is not null, sendMsg is err or ok
86 * @tc.type: FUNC
87 */
88 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest002, TestSize.Level0)
89 {
90 CALL_TEST_DEBUG;
91 ASSERT_NE(g_adapter, nullptr);
92 g_adapter->sessionDevs_[REMOTE_NETWORKID] = 1;
93 g_sendable = false;
94 int32_t ret = g_adapter->StartRemoteCoordination(LOCAL_NETWORKID, REMOTE_NETWORKID, false);
95 EXPECT_TRUE(ret == RET_ERR);
96 ret = g_adapter->StopRemoteCoordination(REMOTE_NETWORKID, true);
97 EXPECT_TRUE(ret == RET_ERR);
98 g_sendable = true;
99 ret = g_adapter->StartRemoteCoordination(LOCAL_NETWORKID, REMOTE_NETWORKID, false);
100 EXPECT_TRUE(ret == RET_OK);
101 ret = g_adapter->StopRemoteCoordination(REMOTE_NETWORKID, true);
102 EXPECT_TRUE(ret == RET_OK);
103 ClearCoordinationSoftbusAdapter();
104 }
105
106 /**
107 * @tc.name: CoordinationSoftbusAdapterTest003
108 * @tc.desc: Test func named StartRemoteCoordinationResult, sessionDevs_ is null
109 * @tc.type: FUNC
110 */
111 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest003, TestSize.Level0)
112 {
113 CALL_TEST_DEBUG;
114 ASSERT_NE(g_adapter, nullptr);
115 int32_t ret = g_adapter->StartRemoteCoordinationResult(REMOTE_NETWORKID, true, REMOTE_NETWORKID, 0, 0);
116 EXPECT_TRUE(ret == RET_ERR);
117 ret = g_adapter->StopRemoteCoordinationResult(REMOTE_NETWORKID, true);
118 EXPECT_TRUE(ret == RET_ERR);
119 ClearCoordinationSoftbusAdapter();
120 }
121
122 /**
123 * @tc.name: CoordinationSoftbusAdapterTest004
124 * @tc.desc: Test func named StartRemoteCoordinationResult, sessionDevs_ is not null, sendMsg is err or ok
125 * @tc.type: FUNC
126 */
127 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest004, TestSize.Level0)
128 {
129 CALL_TEST_DEBUG;
130 ASSERT_NE(g_adapter, nullptr);
131 g_adapter->sessionDevs_[REMOTE_NETWORKID] = 1;
132 g_sendable = false;
133 int32_t ret = g_adapter->StartRemoteCoordinationResult(REMOTE_NETWORKID, true, LOCAL_NETWORKID, 0, 0);
134 EXPECT_TRUE(ret == RET_ERR);
135 ret = g_adapter->StopRemoteCoordinationResult(REMOTE_NETWORKID, true);
136 EXPECT_TRUE(ret == RET_ERR);
137 g_sendable = true;
138 ret = g_adapter->StartRemoteCoordinationResult(REMOTE_NETWORKID, true, LOCAL_NETWORKID, 0, 0);
139 EXPECT_TRUE(ret == RET_OK);
140 ret = g_adapter->StopRemoteCoordinationResult(REMOTE_NETWORKID, true);
141 EXPECT_TRUE(ret == RET_OK);
142 ClearCoordinationSoftbusAdapter();
143 }
144
145 /**
146 * @tc.name: CoordinationSoftbusAdapterTest005
147 * @tc.desc: Test func named StartCoordinationOtherResult, sessionDevs_ is null
148 * @tc.type: FUNC
149 */
150 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest005, TestSize.Level0)
151 {
152 CALL_TEST_DEBUG;
153 ASSERT_NE(g_adapter, nullptr);
154 int32_t ret = g_adapter->StartCoordinationOtherResult(ORIGIN_NETWORKID, REMOTE_NETWORKID);
155 EXPECT_TRUE(ret == RET_ERR);
156 }
157
158 /**
159 * @tc.name: CoordinationSoftbusAdapterTest006
160 * @tc.desc: Test func named StartCoordinationOtherResult, sessionDevs_ is not null, sendMsg is err or ok
161 * @tc.type: FUNC
162 */
163 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest006, TestSize.Level0)
164 {
165 CALL_TEST_DEBUG;
166 ASSERT_NE(g_adapter, nullptr);
167 g_adapter->sessionDevs_[ORIGIN_NETWORKID] = 1;
168 g_sendable = false;
169 int32_t ret = g_adapter->StartCoordinationOtherResult(ORIGIN_NETWORKID, REMOTE_NETWORKID);
170 EXPECT_TRUE(ret == RET_ERR);
171 g_sendable = true;
172 ret = g_adapter->StartCoordinationOtherResult(ORIGIN_NETWORKID, REMOTE_NETWORKID);
173 EXPECT_TRUE(ret == RET_OK);
174 ClearCoordinationSoftbusAdapter();
175 }
176
177 /**
178 * @tc.name: CoordinationSoftbusAdapterTest007
179 * @tc.desc: Test func named NotifyFilterAdded, sessionDevs_ is null
180 * @tc.type: FUNC
181 */
182 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest007, TestSize.Level0)
183 {
184 CALL_TEST_DEBUG;
185 ASSERT_NE(g_adapter, nullptr);
186 int32_t ret = g_adapter->NotifyFilterAdded(REMOTE_NETWORKID);
187 EXPECT_TRUE(ret == RET_ERR);
188 }
189
190 /**
191 * @tc.name: CoordinationSoftbusAdapterTest008
192 * @tc.desc: Test func named NotifyFilterAdded, sessionDevs_ is not null, sendMsg is err or ok
193 * @tc.type: FUNC
194 */
195 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest008, TestSize.Level0)
196 {
197 CALL_TEST_DEBUG;
198 ASSERT_NE(g_adapter, nullptr);
199 g_adapter->sessionDevs_[REMOTE_NETWORKID] = 1;
200 g_sendable = false;
201 int32_t ret = g_adapter->NotifyFilterAdded(REMOTE_NETWORKID);
202 EXPECT_TRUE(ret == RET_ERR);
203 g_sendable = true;
204 ret = g_adapter->NotifyFilterAdded(REMOTE_NETWORKID);
205 EXPECT_TRUE(ret == RET_OK);
206 ClearCoordinationSoftbusAdapter();
207 }
208
209 /**
210 * @tc.name: CoordinationSoftbusAdapterTest009
211 * @tc.desc: test normal func named OpenInputSoftbus and CloseInputSoftbus in devicestatus
212 * @tc.type: FUNC
213 */
214 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest009, TestSize.Level0)
215 {
216 CALL_TEST_DEBUG;
217 ASSERT_NE(g_adapter, nullptr);
218 g_adapter->sessionDevs_[REMOTE_NETWORKID] = SOCKET;
219 int32_t ret = g_adapter->OpenInputSoftbus(REMOTE_NETWORKID);
220 EXPECT_EQ(ret, RET_OK);
221 g_adapter->CloseInputSoftbus(REMOTE_NETWORKID);
222 ClearCoordinationSoftbusAdapter();
223 }
224
225 /**
226 * @tc.name: CoordinationSoftbusAdapterTest010
227 * @tc.desc: test abnormal func named OpenInputSoftbus and CloseInputSoftbus in devicestatus
228 * @tc.type: FUNC
229 */
230 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest010, TestSize.Level0)
231 {
232 CALL_TEST_DEBUG;
233 ASSERT_NE(g_adapter, nullptr);
234 g_init = false;
235 int32_t ret = g_adapter->OpenInputSoftbus(REMOTE_NETWORKID);
236 ASSERT_NE(ret, RET_OK);
237 g_adapter->CloseInputSoftbus(REMOTE_NETWORKID);
238 }
239
240 /**
241 * @tc.name: CoordinationSoftbusAdapterTest011
242 * @tc.desc: test abnormal func named OpenInputSoftbus and CloseInputSoftbus in devicestatus
243 * @tc.type: FUNC
244 */
245 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest011, TestSize.Level0)
246 {
247 CALL_TEST_DEBUG;
248 ASSERT_NE(g_adapter, nullptr);
249 g_init = true;
250 int32_t ret = g_adapter->OpenInputSoftbus(REMOTE_NETWORKID);
251 ASSERT_NE(ret, RET_OK);
252 g_adapter->CloseInputSoftbus(REMOTE_NETWORKID);
253 }
254
255 /**
256 * @tc.name: CoordinationSoftbusAdapterTest012
257 * @tc.desc: test abnormal func named OpenInputSoftbus and CloseInputSoftbus in devicestatus
258 * @tc.type: FUNC
259 */
260 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest012, TestSize.Level0)
261 {
262 CALL_TEST_DEBUG;
263 ASSERT_NE(g_adapter, nullptr);
264 g_init = true;
265 g_cond = false;
266 int32_t ret = g_adapter->OpenInputSoftbus(REMOTE_NETWORKID);
267 ASSERT_NE(ret, RET_OK);
268 g_adapter->CloseInputSoftbus(REMOTE_NETWORKID);
269 }
270
271 /**
272 * @tc.name: CoordinationSoftbusAdapterTest013
273 * @tc.desc: test normal func named OnBind and OnShutdown in devicestatus
274 * @tc.type: FUNC
275 */
276 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest013, TestSize.Level0)
277 {
278 CALL_TEST_DEBUG;
279 ASSERT_NE(g_adapter, nullptr);
280 PeerSocketInfo info;
281 char deviceId[] = "softbus";
282 info.networkId = deviceId;
283 int32_t ret = g_adapter->OnBind(SOCKET, info);
284 EXPECT_EQ(ret, RET_OK);
285 g_adapter->OnShutdown(SOCKET, SHUTDOWN_REASON_UNKNOWN);
286 }
287
288 /**
289 * @tc.name: CoordinationSoftbusAdapterTest014
290 * @tc.desc: test normal func named OnBind and OnShutdown in devicestatus
291 * @tc.type: FUNC
292 */
293 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest014, TestSize.Level0)
294 {
295 CALL_TEST_DEBUG;
296 ASSERT_NE(g_adapter, nullptr);
297 PeerSocketInfo info;
298 char deviceId[] = "softbus";
299 info.networkId = deviceId;
300 int32_t ret = g_adapter->OnBind(SOCKET, info);
301 EXPECT_EQ(ret, RET_OK);
302 g_adapter->OnShutdown(SOCKET, SHUTDOWN_REASON_UNKNOWN);
303 }
304
305 /**
306 * @tc.name: CoordinationSoftbusAdapterTest015
307 * @tc.desc: test abnormal func named SendData in devicestatus
308 * @tc.type: FUNC
309 */
310 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest015, TestSize.Level0)
311 {
312 CALL_TEST_DEBUG;
313 ASSERT_NE(g_adapter, nullptr);
314 std::string data = "TestSendData";
315 Parcel parcel;
316 if (!parcel.WriteBuffer(reinterpreter_cast<void *> (const_cast<void *> (data.c_str())), data.size())) {
317 EXPECT_EQ(RET_ERR, RET_ERR);
318 }
319 g_adapter->sessionDevs_[DEVICE_ID] = SOCKET;
320 int32_t ret = g_adapter->SendData(DEVICE_ID, parcel);
321 EXPECT_EQ(ret, RET_ERR);
322 ClearCoordinationSoftbusAdapter();
323 }
324
325 /**
326 * @tc.name: CoordinationSoftbusAdapterTest016
327 * @tc.desc: Test func named NotifyUnchainedResult, sessionDevs_ is null
328 * @tc.type: FUNC
329 */
330 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest016, TestSize.Level0)
331 {
332 CALL_TEST_DEBUG;
333 ASSERT_NE(g_adapter, nullptr);
334 int32_t ret = g_adapter->NotifyUnchainedResult(LOCAL_NETWORKID, REMOTE_NETWORKID, true);
335 EXPECT_TRUE(ret == RET_ERR);
336 }
337
338 /**
339 * @tc.name: CoordinationSoftbusAdapterTest017
340 * @tc.desc: Test func named NotifyUnchainedResult, sessionDevs_ is not null, sendMsg is err or ok
341 * @tc.type: FUNC
342 */
343 HWTEST_F(CoordinationSoftbusAdapterTest, CoordinationSoftbusAdapterTest017, TestSize.Level0)
344 {
345 CALL_TEST_DEBUG;
346 ASSERT_NE(g_adapter, nullptr);
347 g_adapter->sessionDevs_[REMOTE_NETWORKID] = 1;
348 g_sendable = false;
349 int32_t ret = g_adapter->NotifyUnchainedResult(LOCAL_NETWORKID, REMOTE_NETWORKID, true);
350 EXPECT_TRUE(ret == RET_ERR);
351 g_sendable = true;
352 ret = g_adapter->NotifyUnchainedResult(LOCAL_NETWORKID, REMOTE_NETWORKID, true);
353 EXPECT_TRUE(ret == RET_OK);
354 g_adapter->sessionDevs_.clear();
355 }
356 } // namespace DeviceStatus
357 } // namespace Msdp
358 } // namespace OHOS
359