• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <gtest/gtest.h>
17 #include <chrono>
18 #include <thread>
19 
20 #include "accesstoken_kit.h"
21 #include "nativetoken_kit.h"
22 #include "token_setproc.h"
23 #include "avsession_errors.h"
24 #include "avsession_log.h"
25 #include "migrate_avsession_constant.h"
26 #include "migrate_avsession_server.h"
27 #define private public
28 #define protected public
29 #include "softbus_distributed_data_manager.h"
30 #include "softbus_session_manager.h"
31 #undef protected
32 #undef private
33 
34 using namespace testing::ext;
35 using namespace OHOS::AVSession;
36 
37 static const char* g_perms[] = {"ohos.permission.DISTRIBUTED_SOFTBUS_CENTER", "ohos.permission.DISTRIBUTED_DATASYNC",
38     "ohos.permission.ACCESS_SERVICE_DM"};
39 static const int SIZE = 3;
40 class SoftbusSessionManagerTest : public testing::Test {
41 public:
42     static void SetUpTestCase();
43     static void TearDownTestCase();
44     void SetUp();
45     void TearDown();
46     void NativeTokenGet(const char *perms[], int size);
47 
48     std::shared_ptr<SoftbusSessionManager> manager_;
49     std::shared_ptr<SoftbusDistributedDataManager> distributed_;
50 };
51 
SetUpTestCase()52 void SoftbusSessionManagerTest::SetUpTestCase() {}
53 
TearDownTestCase()54 void SoftbusSessionManagerTest::TearDownTestCase() {}
55 
SetUp()56 void SoftbusSessionManagerTest::SetUp()
57 {
58     manager_ = std::make_shared<SoftbusSessionManager>();
59     distributed_ = std::make_shared<SoftbusDistributedDataManager>();
60     NativeTokenGet(g_perms, SIZE);
61 }
62 
TearDown()63 void SoftbusSessionManagerTest::TearDown()
64 {
65     manager_ = nullptr;
66     distributed_ = nullptr;
67     NativeTokenGet(nullptr, 0);
68 }
69 
NativeTokenGet(const char * perms[],int size)70 void SoftbusSessionManagerTest::NativeTokenGet(const char *perms[], int size)
71 {
72     uint64_t tokenId;
73     NativeTokenInfoParams infoInstance = {
74         .dcapsNum = 0,
75         .permsNum = size,
76         .aclsNum = 0,
77         .dcaps = nullptr,
78         .perms = perms,
79         .acls = nullptr,
80         .aplStr = "system_basic",
81     };
82 
83     infoInstance.processName = "softbus_session_manager_test";
84     tokenId = GetAccessTokenId(&infoInstance);
85     SetSelfTokenID(tokenId);
86     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
87 }
88 
89 /**
90 * @tc.name: SoftbusDistributedTest001
91 * @tc.desc:
92 * @tc.type: FUNC
93 * @tc.require:
94 */
95 static HWTEST_F(SoftbusSessionManagerTest, SoftbusDistributedTest001, TestSize.Level1)
96 {
97     SLOGI("SoftbusDistributedTest001 begin");
98     distributed_->Init();
99     std::string pkg = "AVSESSION";
100     distributed_->InitSessionServer(pkg);
101 
102     std::shared_ptr<MigrateAVSessionServer> server = std::make_shared<MigrateAVSessionServer>();
103     distributed_->CreateServer(server);
104     EXPECT_EQ(distributed_->serverMap_.size() > 0, true);
105 
106     int32_t sessionId = 1;
107     char infoName[] = "testInfoName";
108     char infoNetworkId[] = "testInfoNetworkId";
109     char infoPkgName[] = "testInfoPkgName";
110     PeerSocketInfo info = {
111         .name = infoName,
112         .networkId = infoNetworkId,
113         .pkgName = infoPkgName,
114         .dataType = DATA_TYPE_BYTES,
115     };
116     distributed_->SessionOpened(sessionId, info);
117     distributed_->SessionClosed(sessionId);
118     std::string data = "111";
119     int32_t socket = 1;
120     distributed_->MessageReceived(sessionId, data);
121     distributed_->BytesReceived(sessionId, data);
122     distributed_->OnSessionServerOpened();
123     distributed_->OnSessionServerClosed(sessionId);
124     distributed_->OnMessageHandleReceived(sessionId, data);
125     distributed_->OnBytesServerReceived(socket, data);
126 
127     distributed_->ReleaseServer(server);
128     EXPECT_EQ(distributed_->serverMap_.size() == 0, true);
129 
130     distributed_->DestroySessionServer(pkg);
131     SLOGI("SoftbusDistributedTest001 end");
132 }
133 
134 /**
135 * @tc.name: CreateSessionServer001
136 * @tc.desc:
137 * @tc.type: FUNC
138 * @tc.require:
139 */
140 static HWTEST_F(SoftbusSessionManagerTest, CreateSessionServer001, TestSize.Level1)
141 {
142     SLOGI("CreateSessionServer001 begin");
143     std::string pkg = "111";
144     int32_t ret = manager_->Socket(pkg);
145     EXPECT_EQ(ret <= 0, true);
146     SLOGI("CreateSessionServer001 end");
147 }
148 
149 /**
150 * @tc.name: RemoveSessionServer001
151 * @tc.desc:
152 * @tc.type: FUNC
153 * @tc.require:
154 */
155 static HWTEST_F(SoftbusSessionManagerTest, RemoveSessionServer001, TestSize.Level1)
156 {
157     SLOGI("RemoveSessionServer001 begin");
158     int32_t sessionId = 123;
159     manager_->Shutdown(sessionId);
160     EXPECT_EQ(sessionId, 123);
161     SLOGI("RemoveSessionServer001 end");
162 }
163 
164 /**
165 * @tc.name: SendMessage001
166 * @tc.desc:
167 * @tc.type: FUNC
168 * @tc.require:
169 */
170 static HWTEST_F(SoftbusSessionManagerTest, SendMessage001, TestSize.Level1)
171 {
172     SLOGI("SendMessage001 begin");
173     int32_t sessionId = 123;
174     std::string data = "";
175     int32_t ret = manager_->SendMessage(sessionId, data);
176     EXPECT_EQ(ret, -1);
177     SLOGI("SendMessage001 end");
178 }
179 
180 /**
181 * @tc.name: SendByte001
182 * @tc.desc:
183 * @tc.type: FUNC
184 * @tc.require:
185 */
186 static HWTEST_F(SoftbusSessionManagerTest, SendByte001, TestSize.Level1)
187 {
188     SLOGI("SendByte001 begin");
189     int32_t sessionId = 123;
190     std::string data = "";
191     int32_t ret = manager_->SendBytes(sessionId, data);
192     EXPECT_EQ(ret, -1);
193     SLOGI("SendByte001 end");
194 }
195 
196 /**
197 * @tc.name: ObtainPeerDeviceId001
198 * @tc.desc:
199 * @tc.type: FUNC
200 * @tc.require:
201 */
202 static HWTEST_F(SoftbusSessionManagerTest, ObtainPeerDeviceId001, TestSize.Level1)
203 {
204     SLOGI("ObtainPeerDeviceId001 begin");
205     int32_t sessionId = 0;
206     std::string deviceId;
207     int32_t ret = manager_->ObtainPeerDeviceId(sessionId, deviceId);
208     EXPECT_EQ(ret, -1);
209     SLOGI("ObtainPeerDeviceId001 end");
210 }
211 
212 /**
213 * @tc.name: OnSessionOpened001
214 * @tc.desc:
215 * @tc.type: FUNC
216 * @tc.require:
217 */
218 static HWTEST_F(SoftbusSessionManagerTest, OnSessionOpened001, TestSize.Level1)
219 {
220     SLOGI("OnSessionOpened001 begin");
221     int32_t sessionId = 123;
222     char infoName[] = "testInfoName";
223     char infoNetworkId[] = "testInfoNetworkId";
224     char infoPkgName[] = "testInfoPkgName";
225     PeerSocketInfo info = {
226         .name = infoName,
227         .networkId = infoNetworkId,
228         .pkgName = infoPkgName,
229         .dataType = DATA_TYPE_BYTES,
230     };
231     EXPECT_TRUE(manager_ != nullptr);
232     manager_->OnBind(sessionId, info);
233     SLOGI("OnSessionOpened001 end");
234 }
235 
236 
237 /**
238 * @tc.name: SendMessage002
239 * @tc.desc: test SendMessage
240 * @tc.type: FUNC
241 * @tc.require:
242 */
243 static HWTEST_F(SoftbusSessionManagerTest, SendMessage002, TestSize.Level1)
244 {
245     SLOGI("SendMessage002 begin");
246     int32_t sessionId = -1;
247     std::string data = "";
248     int32_t ret = manager_->SendMessage(sessionId, data);
249     EXPECT_EQ(ret, -1);
250     SLOGI("SendMessage002 end");
251 }
252 
253 /**
254 * @tc.name: SendMessage003
255 * @tc.desc: test SendMessage
256 * @tc.type: FUNC
257 * @tc.require:
258 */
259 static HWTEST_F(SoftbusSessionManagerTest, SendMessage003, TestSize.Level1)
260 {
261     SLOGI("SendMessage003 begin");
262     int32_t sessionId = -1;
263     std::string data = "123";
264     int32_t ret = manager_->SendMessage(sessionId, data);
265     EXPECT_EQ(ret, -1);
266     SLOGI("SendMessage003 end");
267 }
268 
269 /**
270 * @tc.name: SendMessage004
271 * @tc.desc: test SendMessage
272 * @tc.type: FUNC
273 * @tc.require:
274 */
275 static HWTEST_F(SoftbusSessionManagerTest, SendMessage004, TestSize.Level1)
276 {
277     SLOGI("SendMessage004 begin");
278     int32_t sessionId = 100;
279     std::string data = "123";
280     int32_t ret = manager_->SendMessage(sessionId, data);
281     EXPECT_EQ(ret, -1);
282     SLOGI("SendMessage004 end");
283 }
284 
285 /**
286 * @tc.name: SendByte002
287 * @tc.desc: test SendBytes
288 * @tc.type: FUNC
289 * @tc.require:
290 */
291 static HWTEST_F(SoftbusSessionManagerTest, SendByte002, TestSize.Level1)
292 {
293     SLOGI("SendByte002 begin");
294     int32_t sessionId = -1;
295     std::string data = "";
296     int32_t ret = manager_->SendBytes(sessionId, data);
297     EXPECT_EQ(ret, -1);
298     SLOGI("SendByte002 end");
299 }
300 
301 /**
302 * @tc.name: SendByte003
303 * @tc.desc: test SendBytes
304 * @tc.type: FUNC
305 * @tc.require:
306 */
307 static HWTEST_F(SoftbusSessionManagerTest, SendByte003, TestSize.Level1)
308 {
309     SLOGI("SendByte003 begin");
310     int32_t sessionId = -1;
311     std::string data = "123";
312     int32_t ret = manager_->SendBytes(sessionId, data);
313     EXPECT_EQ(ret, -1);
314     SLOGI("SendByte003 end");
315 }
316 
317 /**
318 * @tc.name: SendByte004
319 * @tc.desc: test SendBytes
320 * @tc.type: FUNC
321 * @tc.require:
322 */
323 static HWTEST_F(SoftbusSessionManagerTest, SendByte004, TestSize.Level1)
324 {
325     SLOGI("SendByte004 begin");
326     int32_t sessionId = 100;
327     std::string data = "123";
328     int32_t ret = manager_->SendBytes(sessionId, data);
329     EXPECT_EQ(ret, -1);
330     SLOGI("SendByte004 end");
331 }
332 
333 /**
334 * @tc.name: ObtainPeerDeviceId002
335 * @tc.desc: test ObtainPeerDeviceId
336 * @tc.type: FUNC
337 * @tc.require:
338 */
339 static HWTEST_F(SoftbusSessionManagerTest, ObtainPeerDeviceId002, TestSize.Level1)
340 {
341     SLOGI("ObtainPeerDeviceId002 begin");
342     int32_t sessionId = 1230;
343     std::string deviceId;
344     int32_t ret = manager_->ObtainPeerDeviceId(sessionId, deviceId);
345     EXPECT_EQ(ret, -1);
346     SLOGI("ObtainPeerDeviceId002 end");
347 }
348 
349 /**
350 * @tc.name: AddSessionListener001
351 * @tc.desc: test AddSessionListener
352 * @tc.type: FUNC
353 * @tc.require:
354 */
355 static HWTEST_F(SoftbusSessionManagerTest, AddSessionListener001, TestSize.Level1)
356 {
357     SLOGI("AddSessionListener001 begin");
358     std::shared_ptr<SoftbusSessionListener> softbusSessionListener;
359     manager_->AddSessionListener(softbusSessionListener);
360     EXPECT_EQ(softbusSessionListener, nullptr);
361     SLOGI("AddSessionListener001 end");
362 }
363 
364 /**
365 * @tc.name: OnBind001
366 * @tc.desc: test OnBind
367 * @tc.type: FUNC
368 * @tc.require:
369 */
370 static HWTEST_F(SoftbusSessionManagerTest, OnBind001, TestSize.Level1)
371 {
372     SLOGI("OnBind001 begin");
373     int32_t socket = 1231;
374     PeerSocketInfo info;
375     EXPECT_TRUE(manager_ != nullptr);
376     manager_->OnBind(socket, info);
377     SLOGI("OnBind001 end");
378 }
379 
380 /**
381 * @tc.name: OnMessage001
382 * @tc.desc: test OnMessage
383 * @tc.type: FUNC
384 * @tc.require:
385 */
386 static HWTEST_F(SoftbusSessionManagerTest, OnMessage001, TestSize.Level1)
387 {
388     SLOGI("OnMessage001 begin");
389     int32_t socket = 1231;
390     void *data = nullptr;
391     int32_t dataLen = 10;
392     EXPECT_TRUE(manager_ != nullptr);
393     manager_->OnMessage(socket, data, dataLen);
394     SLOGI("OnMessage001 end");
395 }
396 
397 /**
398 * @tc.name: OnMessage002
399 * @tc.desc: test OnMessage
400 * @tc.type: FUNC
401 * @tc.require:
402 */
403 static HWTEST_F(SoftbusSessionManagerTest, OnMessage002, TestSize.Level1)
404 {
405     SLOGI("OnMessage002 begin");
406     int32_t socket = 1231;
407     int *ptr = &socket;
408     void *data = static_cast<void*>(ptr);
409     int32_t dataLen = 10;
410     EXPECT_TRUE(manager_ != nullptr);
411     manager_->OnMessage(socket, data, dataLen);
412     SLOGI("OnMessage002 end");
413 }
414 
415 /**
416 * @tc.name: CreateServer001
417 * @tc.desc: test CreateServer
418 * @tc.type: FUNC
419 * @tc.require:
420 */
421 static HWTEST_F(SoftbusSessionManagerTest, CreateServer001, TestSize.Level1)
422 {
423     SLOGI("CreateServer001 begin");
424     EXPECT_TRUE(distributed_ != nullptr);
425     distributed_->Init();
426     std::string pkg = "AVSESSION";
427     distributed_->InitSessionServer(pkg);
428 
429     std::shared_ptr<MigrateAVSessionServer> server;
430     distributed_->CreateServer(server);
431     EXPECT_EQ(server, nullptr);
432     SLOGI("CreateServer001 end");
433 }
434 
435 /**
436 * @tc.name: ReleaseServer001
437 * @tc.desc: test ReleaseServer
438 * @tc.type: FUNC
439 * @tc.require:
440 */
441 static HWTEST_F(SoftbusSessionManagerTest, ReleaseServer001, TestSize.Level1)
442 {
443     SLOGI("ReleaseServer001 begin");
444     EXPECT_TRUE(distributed_ != nullptr);
445     distributed_->Init();
446     std::string pkg = "AVSESSION";
447     distributed_->InitSessionServer(pkg);
448     std::shared_ptr<MigrateAVSessionServer> server = std::make_shared<MigrateAVSessionServer>();
449     distributed_->ReleaseServer(server);
450     EXPECT_TRUE(server != nullptr);
451     SLOGI("ReleaseServer001 end");
452 }
453 
454 /**
455 * @tc.name: OnBytes001
456 * @tc.desc: set data to nullptr
457 * @tc.type: FUNC
458 * @tc.require:
459 */
460 static HWTEST_F(SoftbusSessionManagerTest, OnBytes001, TestSize.Level1)
461 {
462     SLOGI("OnBytes001 begin");
463     int32_t socket = 1001;
464     void *data = nullptr;
465     int32_t dataLen = 10;
466     manager_->OnBytes(socket, data, dataLen);
467     EXPECT_TRUE(data == nullptr);
468     SLOGI("OnBytes001 end");
469 }
470 
471 /**
472 * @tc.name: OnBytes002
473 * @tc.desc: set data to not nullptr
474 * @tc.type: FUNC
475 * @tc.require:
476 */
477 static HWTEST_F(SoftbusSessionManagerTest, OnBytes002, TestSize.Level1)
478 {
479     SLOGI("OnBytes002 begin");
480     int32_t socket = 1001;
481     int32_t dataArr[] = {1, 0, 1, 0};
482     void *data = static_cast<void*>(dataArr);
483     int32_t dataLen = 10;
484     manager_->OnBytes(socket, data, dataLen);
485     EXPECT_TRUE(data != nullptr);
486     SLOGI("OnBytes002 end");
487 }
488 
489 /**
490 * @tc.name: Bind001
491 * @tc.desc: the size of peerNetworkId is zero
492 * @tc.type: FUNC
493 * @tc.require:
494 */
495 static HWTEST_F(SoftbusSessionManagerTest, Bind001, TestSize.Level1)
496 {
497     SLOGI("Bind001 begin");
498     std::string peerNetworkId = "";
499     std::string pkgName = "";
500     int32_t ret = manager_->Bind(peerNetworkId, pkgName);
501     EXPECT_TRUE(ret == AVSESSION_ERROR);
502     SLOGI("Bind001 end");
503 }
504 
505 /**
506 * @tc.name: Bind002
507 * @tc.desc: the size of peerNetworkId bigger than zero
508 * @tc.type: FUNC
509 * @tc.require:
510 */
511 static HWTEST_F(SoftbusSessionManagerTest, Bind002, TestSize.Level1)
512 {
513     SLOGI("Bind002 begin");
514     std::string peerNetworkId = "0.0.0.0";
515     std::string pkgName = "test";
516     int32_t ret = manager_->Bind(peerNetworkId, pkgName);
517     EXPECT_TRUE(ret == AVSESSION_ERROR);
518     SLOGI("Bind002 end");
519 }