1 /*
2 * Copyright (c) 2022 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 "softbus_adapter_test.h"
17
18 #include "accesstoken_kit.h"
19 #include "nativetoken_kit.h"
20 #include "token_setproc.h"
21
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace DistributedHardware {
SetUpTestCase(void)26 void SoftbusAdapterTest::SetUpTestCase(void) {}
27
TearDownTestCase(void)28 void SoftbusAdapterTest::TearDownTestCase(void) {}
29
SetUp(void)30 void SoftbusAdapterTest::SetUp(void)
31 {
32 uint64_t tokenId;
33 const char *perms[2];
34 perms[0] = OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER;
35 perms[1] = OHOS_PERMISSION_DISTRIBUTED_DATASYNC;
36 NativeTokenInfoParams infoInstance = {
37 .dcapsNum = 0,
38 .permsNum = 2,
39 .aclsNum = 0,
40 .dcaps = NULL,
41 .perms = perms,
42 .acls = NULL,
43 .processName = "softbus_adapter_test",
44 .aplStr = "system_basic",
45 };
46 tokenId = GetAccessTokenId(&infoInstance);
47 SetSelfTokenID(tokenId);
48 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
49 }
50
TearDown(void)51 void SoftbusAdapterTest::TearDown(void) {}
52
ScreenOnSoftbusSessionOpened(int32_t sessionId,int32_t result)53 static int32_t ScreenOnSoftbusSessionOpened(int32_t sessionId, int32_t result)
54 {
55 return 0;
56 }
57
ScreenOnSoftbusSessionClosed(int32_t sessionId)58 static void ScreenOnSoftbusSessionClosed(int32_t sessionId) {}
59
ScreenOnBytesReceived(int32_t sessionId,const void * data,uint32_t dataLen)60 static void ScreenOnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen) {}
61
ScreenOnStreamReceived(int32_t sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * frameInfo)62 static void ScreenOnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext,
63 const StreamFrameInfo *frameInfo) {}
64
ScreenOnMessageReceived(int sessionId,const void * data,unsigned int dataLen)65 static void ScreenOnMessageReceived(int sessionId, const void *data, unsigned int dataLen) {}
66
ScreenOnQosEvent(int sessionId,int eventId,int tvCount,const QosTv * tvList)67 static void ScreenOnQosEvent(int sessionId, int eventId, int tvCount, const QosTv *tvList) {}
68
69 /**
70 * @tc.name: CreateSoftbusSessionServer_001
71 * @tc.desc: Verify the CreateSoftbusSessionServer function.
72 * @tc.type: FUNC
73 * @tc.require: Issue Number
74 */
75 HWTEST_F(SoftbusAdapterTest, CreateSoftbusSessionServer_001, TestSize.Level1)
76 {
77 softbusAdapter.sessListener_.OnSessionOpened = ScreenOnSoftbusSessionOpened;
78 softbusAdapter.sessListener_.OnSessionClosed = ScreenOnSoftbusSessionClosed;
79 softbusAdapter.sessListener_.OnBytesReceived = ScreenOnBytesReceived;
80 softbusAdapter.sessListener_.OnStreamReceived = ScreenOnStreamReceived;
81 softbusAdapter.sessListener_.OnMessageReceived = ScreenOnMessageReceived;
82 softbusAdapter.sessListener_.OnQosEvent = ScreenOnQosEvent;
83
84 std::string pkgname = PKG_NAME;
85 std::string sessionName = DATA_SESSION_NAME;
86 std::string peerDevId = "peerDevId";
87 int32_t sessionId = 0;
88 void *data = nullptr;
89 uint32_t dataLen = 0;
90 StreamData sData;
91 sData.bufLen = 0;
92 sData.buf = reinterpret_cast<char*>(&dataLen);
93 StreamData *streamData = nullptr;
94 StreamData *ext = nullptr;
95 StreamFrameInfo *frameInfo = nullptr;
96 softbusAdapter.OnBytesReceived(sessionId, data, dataLen);
97 softbusAdapter.OnStreamReceived(sessionId, streamData, ext, frameInfo);
98 data = reinterpret_cast<void*>(&dataLen);
99 streamData = &sData;
100 softbusAdapter.OnBytesReceived(sessionId, data, dataLen);
101 softbusAdapter.OnStreamReceived(sessionId, streamData, ext, frameInfo);
102 dataLen = 100;
103 sData.bufLen = 100;
104 softbusAdapter.OnBytesReceived(sessionId, data, dataLen);
105 softbusAdapter.OnStreamReceived(sessionId, streamData, ext, frameInfo);
106 int32_t actual = softbusAdapter.CreateSoftbusSessionServer(pkgname, sessionName, peerDevId);
107 EXPECT_EQ(DH_SUCCESS, actual);
108 softbusAdapter.RemoveSoftbusSessionServer(pkgname, sessionName, peerDevId);
109 }
110
111 /**
112 * @tc.name: CreateSoftbusSessionServer_002
113 * @tc.desc: Verify the CreateSoftbusSessionServer function.
114 * @tc.type: FUNC
115 * @tc.require: Issue Number
116 */
117 HWTEST_F(SoftbusAdapterTest, CreateSoftbusSessionServer_002, TestSize.Level1)
118 {
119 softbusAdapter.mapSessionSet_.clear();
120 std::string pkgname = PKG_NAME;
121 std::string sessionName = DATA_SESSION_NAME;
122 std::string peerDevId = "peerDevId";
123 softbusAdapter.mapSessionSet_[sessionName].insert(peerDevId);
124
125 int32_t actual = softbusAdapter.CreateSoftbusSessionServer(pkgname, sessionName, peerDevId);
126 EXPECT_EQ(DH_SUCCESS, actual);
127 softbusAdapter.RemoveSoftbusSessionServer(pkgname, sessionName, peerDevId);
128 }
129
130 /**
131 * @tc.name: RegisterSoftbusListener_001
132 * @tc.desc: Verify the RegisterSoftbusListener function.
133 * @tc.type: FUNC
134 * @tc.require: Issue Number
135 */
136 HWTEST_F(SoftbusAdapterTest, RegisterSoftbusListener_001, TestSize.Level1)
137 {
138 std::shared_ptr<ISoftbusListener> listener = std::make_shared<MockSoftbusListener>();
139 std::string sessionName = DATA_SESSION_NAME;
140 std::string peerDevId = "testDevId";
141
142 int32_t actual = softbusAdapter.RegisterSoftbusListener(listener, sessionName, peerDevId);
143 EXPECT_EQ(DH_SUCCESS, actual);
144
145 actual = softbusAdapter.UnRegisterSoftbusListener(sessionName, peerDevId);
146 EXPECT_EQ(DH_SUCCESS, actual);
147 }
148
149 /**
150 * @tc.name: RegisterSoftbusListener_002
151 * @tc.desc: Verify the RegisterSoftbusListener function.
152 * @tc.type: FUNC
153 * @tc.require: Issue Number
154 */
155 HWTEST_F(SoftbusAdapterTest, RegisterSoftbusListener_002, TestSize.Level1)
156 {
157 std::shared_ptr<ISoftbusListener> listener = std::make_shared<MockSoftbusListener>();
158 std::string sessionName = DATA_SESSION_NAME;
159 std::string peerDevId = "testDevId";
160
161 int32_t actual = softbusAdapter.RegisterSoftbusListener(listener, sessionName, peerDevId);
162 EXPECT_EQ(DH_SUCCESS, actual);
163
164 actual = softbusAdapter.RegisterSoftbusListener(listener, sessionName, peerDevId);
165 EXPECT_EQ(ERR_DH_SCREEN_ADAPTER_REGISTER_SOFTBUS_LISTENER_FAIL, actual);
166 }
167
168 /**
169 * @tc.name: RegisterSoftbusListener_003
170 * @tc.desc: Verify the RegisterSoftbusListener function.
171 * @tc.type: FUNC
172 * @tc.require: Issue Number
173 */
174 HWTEST_F(SoftbusAdapterTest, RegisterSoftbusListener_003, TestSize.Level1)
175 {
176 std::shared_ptr<ISoftbusListener> listener = nullptr;
177 std::string sessionName = DATA_SESSION_NAME;
178 std::string peerDevId = "testDevId";
179
180 int32_t actual = softbusAdapter.RegisterSoftbusListener(listener, sessionName, peerDevId);
181 EXPECT_EQ(ERR_DH_SCREEN_ADAPTER_REGISTER_SOFTBUS_LISTENER_FAIL, actual);
182 }
183
184 /**
185 * @tc.name: UnRegisterSoftbusListener_001
186 * @tc.desc: Verify the UnRegisterSoftbusListener function.
187 * @tc.type: FUNC
188 * @tc.require: Issue Number
189 */
190 HWTEST_F(SoftbusAdapterTest, UnRegisterSoftbusListener_001, TestSize.Level1)
191 {
192 std::string sessionName = DATA_SESSION_NAME;
193 std::string peerDevId = "testDevId";
194
195 int32_t actual = softbusAdapter.UnRegisterSoftbusListener(sessionName, peerDevId);
196
197 EXPECT_EQ(DH_SUCCESS, actual);
198 }
199
200 /**
201 * @tc.name: RemoveSoftbusSessionServer_001
202 * @tc.desc: Verify the RemoveSoftbusSessionServer function.
203 * @tc.type: FUNC
204 * @tc.require: Issue Number
205 */
206 HWTEST_F(SoftbusAdapterTest, RemoveSoftbusSessionServer_001, TestSize.Level1)
207 {
208 std::string pkgname = PKG_NAME;
209 std::string sessionName = DATA_SESSION_NAME;
210 std::string peerDevId = "testDevId";
211
212 int32_t actual = softbusAdapter.RemoveSoftbusSessionServer(pkgname, sessionName, peerDevId);
213
214 EXPECT_EQ(ERR_DH_SCREEN_TRANS_ILLEGAL_OPERATION, actual);
215 }
216
217 /**
218 * @tc.name: RemoveSoftbusSessionServer_002
219 * @tc.desc: Verify the RemoveSoftbusSessionServer function.
220 * @tc.type: FUNC
221 * @tc.require: Issue Number
222 */
223 HWTEST_F(SoftbusAdapterTest, RemoveSoftbusSessionServer_002, TestSize.Level1)
224 {
225 std::string pkgname = PKG_NAME;
226 std::string sessionName = DATA_SESSION_NAME;
227 std::string peerDevId = "peerDevId";
228 softbusAdapter.mapSessionSet_[sessionName].insert("peerDevIds");
229
230 int32_t actual = softbusAdapter.RemoveSoftbusSessionServer(pkgname, sessionName, peerDevId);
231 EXPECT_EQ(ERR_DH_SCREEN_TRANS_ILLEGAL_OPERATION, actual);
232 }
233
234 /**
235 * @tc.name: OpenSoftbusSession_001
236 * @tc.desc: Verify the OpenSoftbusSession function.
237 * @tc.type: FUNC
238 * @tc.require: Issue Number
239 */
240 HWTEST_F(SoftbusAdapterTest, OpenSoftbusSession_001, TestSize.Level1)
241 {
242 std::string mySessionName = DATA_SESSION_NAME;
243 std::string peerSessionName = DATA_SESSION_NAME;
244 std::string peerDevId = "testDevId";
245
246 int32_t actual = softbusAdapter.OpenSoftbusSession(mySessionName, peerSessionName, peerDevId);
247
248 EXPECT_EQ(ERR_DH_SCREEN_ADAPTER_OPEN_SESSION_FAIL, actual);
249 }
250
251 /**
252 * @tc.name: OpenSoftbusSession_002
253 * @tc.desc: Verify the OpenSoftbusSession function.
254 * @tc.type: FUNC
255 * @tc.require: Issue Number
256 */
257 HWTEST_F(SoftbusAdapterTest, OpenSoftbusSession_002, TestSize.Level1)
258 {
259 std::string mySessionName = "mySessionName";
260 std::string peerSessionName = DATA_SESSION_NAME;
261 std::string peerDevId = "testDevId";
262
263 int32_t actual = softbusAdapter.OpenSoftbusSession(mySessionName, peerSessionName, peerDevId);
264
265 EXPECT_EQ(ERR_DH_SCREEN_ADAPTER_OPEN_SESSION_FAIL, actual);
266 }
267
268 /**
269 * @tc.name: CloseSoftbusSession_001
270 * @tc.desc: Verify the CloseSoftbusSession function.
271 * @tc.type: FUNC
272 * @tc.require: Issue Number
273 */
274 HWTEST_F(SoftbusAdapterTest, CloseSoftbusSession_001, TestSize.Level1)
275 {
276 int32_t sessionId = 0;
277
278 int32_t actual = softbusAdapter.CloseSoftbusSession(sessionId);
279
280 EXPECT_EQ(DH_SUCCESS, actual);
281 }
282
283 /**
284 * @tc.name: SendSoftbusBytes_001
285 * @tc.desc: Verify the SendSoftbusBytes function.
286 * @tc.type: FUNC
287 * @tc.require: Issue Number
288 */
289 HWTEST_F(SoftbusAdapterTest, SendSoftbusBytes_001, TestSize.Level1)
290 {
291 int32_t sessionId = 0;
292 void *data = nullptr;
293 int32_t dataLen = 0;
294
295 int32_t actual = softbusAdapter.SendSoftbusBytes(sessionId, data, dataLen);
296
297 EXPECT_EQ(ERR_DH_SCREEN_TRANS_ERROR, actual);
298 }
299
300 /**
301 * @tc.name: SendSoftbusStream_001
302 * @tc.desc: Verify the SendSoftbusStream function.
303 * @tc.type: FUNC
304 * @tc.require: Issue Number
305 */
306 HWTEST_F(SoftbusAdapterTest, SendSoftbusStream_001, TestSize.Level1)
307 {
308 int32_t sessionId = 0;
309 StreamData *data = nullptr;
310 StreamData *ext = nullptr;
311 StreamFrameInfo *param = nullptr;
312
313 int32_t actual = softbusAdapter.SendSoftbusStream(sessionId, data, ext, param);
314
315 EXPECT_EQ(ERR_DH_SCREEN_TRANS_ERROR, actual);
316 }
317
318 /**
319 * @tc.name: GetSoftbusListener_001
320 * @tc.desc: Verify the GetSoftbusListener function.
321 * @tc.type: FUNC
322 * @tc.require: Issue Number
323 */
324 HWTEST_F(SoftbusAdapterTest, GetSoftbusListener_001, TestSize.Level1)
325 {
326 int32_t sessionId = 0;
327
328 std::shared_ptr<ISoftbusListener> actual = softbusAdapter.GetSoftbusListenerById(sessionId);
329
330 EXPECT_EQ(nullptr, actual);
331 }
332
333 /**
334 * @tc.name: GetSoftbusListener_002
335 * @tc.desc: Verify the GetSoftbusListener function.
336 * @tc.type: FUNC
337 * @tc.require: Issue Number
338 */
339 HWTEST_F(SoftbusAdapterTest, GetSoftbusListener_002, TestSize.Level1)
340 {
341 int32_t sessionId = 0;
342
343 SessionInfo sessionInfo;
344 sessionInfo.sessionName = "hello";
345 sessionInfo.peerDevId = "world";
346 softbusAdapter.mapSessionInfos_[sessionId] = sessionInfo;
347 std::shared_ptr<ISoftbusListener> listener = nullptr;
348 softbusAdapter.mapListeners_["hello_world"] = listener;
349 std::shared_ptr<ISoftbusListener> actual = softbusAdapter.GetSoftbusListenerById(sessionId);
350
351 EXPECT_EQ(nullptr, actual);
352 }
353
354 /**
355 * @tc.name: OnSoftbusSessionOpened_001
356 * @tc.desc: Verify the OnSoftbusSessionOpened function.
357 * @tc.type: FUNC
358 * @tc.require: Issue Number
359 */
360 HWTEST_F(SoftbusAdapterTest, OnSoftbusSessionOpened_001, TestSize.Level1)
361 {
362 int32_t sessionId = 0;
363 int32_t result = 0;
364
365 int32_t actual = softbusAdapter.OnSoftbusSessionOpened(sessionId, result);
366
367 EXPECT_EQ(ERR_DH_SCREEN_TRANS_ERROR, actual);
368 }
369
370 /**
371 * @tc.name: OnSoftbusSessionOpened_002
372 * @tc.desc: Verify the OnSoftbusSessionOpened function.
373 * @tc.type: FUNC
374 * @tc.require: Issue Number
375 */
376 HWTEST_F(SoftbusAdapterTest, OnSoftbusSessionOpened_002, TestSize.Level1)
377 {
378 int32_t sessionId = 0;
379 int32_t result = -1;
380
381 int32_t actual = softbusAdapter.OnSoftbusSessionOpened(sessionId, result);
382
383 EXPECT_EQ(ERR_DH_SCREEN_ADAPTER_OPEN_SESSION_FAIL, actual);
384 }
385
386 /**
387 * @tc.name: OnSoftbusSessionClosed_001
388 * @tc.desc: Verify the OnSoftbusSessionClosed function.
389 * @tc.type: FUNC
390 * @tc.require: Issue Number
391 */
392 HWTEST_F(SoftbusAdapterTest, OnSoftbusSessionClosed_001, TestSize.Level1)
393 {
394 int32_t sessionId = 0;
395
396 softbusAdapter.OnSoftbusSessionClosed(sessionId);
397 }
398
399 /**
400 * @tc.name: OnSoftbusSessionClosed_002
401 * @tc.desc: Verify the OnSoftbusSessionClosed function.
402 * @tc.type: FUNC
403 * @tc.require: Issue Number
404 */
405 HWTEST_F(SoftbusAdapterTest, OnSoftbusSessionClosed_002, TestSize.Level1)
406 {
407 int32_t sessionId = 0;
408
409 SessionInfo sessionInfo;
410 sessionInfo.sessionName = "hello";
411 sessionInfo.peerDevId = "world";
412 softbusAdapter.mapSessionInfos_[sessionId] = sessionInfo;
413 std::shared_ptr<ISoftbusListener> listener = nullptr;
414 softbusAdapter.mapListeners_["hello_world"] = listener;
415
416 softbusAdapter.OnSoftbusSessionClosed(sessionId);
417 }
418
419 /**
420 * @tc.name: GetSoftbusListenerById_001
421 * @tc.desc: Verify the GetSoftbusListenerById function.
422 * @tc.type: FUNC
423 * @tc.require: Issue Number
424 */
425 HWTEST_F(SoftbusAdapterTest, GetSoftbusListenerById_001, TestSize.Level1)
426 {
427 int32_t sessionId = 0;
428 softbusAdapter.mapSessListeners_[sessionId] = nullptr;
429 std::shared_ptr<ISoftbusListener> listener = softbusAdapter.GetSoftbusListenerById(sessionId);
430 EXPECT_EQ(nullptr, listener);
431 }
432 } // DistributedHardware
433 } // OHOS