1 /*
2 * Copyright (c) 2022-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 "gtest/gtest.h"
17 #include <memory>
18 #include <unistd.h>
19
20 #include "device_manager_impl.h"
21 #include "dm_constants.h"
22
23 #include "network/kernel_talker.h"
24 #include "network/session_pool.h"
25 #include "network/softbus/softbus_session.h"
26 #include "network/kernel_talker.h"
27 #include "utils_log.h"
28
29 namespace {
30 bool g_mockGetTrustedDeviceList = true;
31 }
32
33 namespace OHOS {
34 namespace DistributedHardware {
GetTrustedDeviceList(const std::string & pkgName,const std::string & extra,std::vector<DmDeviceInfo> & deviceList)35 int32_t DeviceManagerImpl::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra,
36 std::vector<DmDeviceInfo> &deviceList)
37 {
38 if (!g_mockGetTrustedDeviceList) {
39 return ERR_DM_INPUT_PARA_INVALID;
40 }
41
42 DmDeviceInfo deviceInfo{
43 .deviceId = "testdevid",
44 .deviceName = "testdevname",
45 .deviceTypeId = 1,
46 .networkId = "testNetWork",
47 };
48 deviceList.push_back(deviceInfo);
49 return DM_OK;
50 }
51 }
52 }
53
54 namespace OHOS {
55 namespace Storage {
56 namespace DistributedFile {
SinkSessionTokernel(std::shared_ptr<BaseSession> session,const std::string backStage)57 void KernelTalker::SinkSessionTokernel(std::shared_ptr<BaseSession> session, const std::string backStage)
58 {
59 return;
60 }
61
SinkOfflineCmdToKernel(std::string cid)62 void KernelTalker::SinkOfflineCmdToKernel(std::string cid)
63 {
64 return;
65 }
66
CreatePollThread()67 void KernelTalker::CreatePollThread()
68 {
69 return;
70 }
71
WaitForPollThreadExited()72 void KernelTalker::WaitForPollThreadExited()
73 {
74 return;
75 }
76 }
77 }
78 }
79
80 namespace OHOS {
81 namespace Storage {
82 namespace DistributedFile {
83 namespace Test {
84 using namespace testing::ext;
85 using namespace testing;
86 using namespace std;
87
88 constexpr int USER_ID = 100;
89 constexpr int TEST_SESSION_ID = 10;
90 constexpr int TEST_SESSION_ID_TWO = 20;
91 constexpr uint8_t LINK_TYPE_P2P = 2;
92 constexpr uint8_t LINK_TYPE_AP = 1;
93 constexpr int32_t MOUNT_DFS_COUNT_ONE = 1;
94
95 class SessionPoolTest : public testing::Test {
96 public:
97 static void SetUpTestCase(void);
98 static void TearDownTestCase(void);
99 void SetUp();
100 void TearDown();
101 };
102
SetUpTestCase(void)103 void SessionPoolTest::SetUpTestCase(void)
104 {
105 GTEST_LOG_(INFO) << "SetUpTestCase";
106 }
107
TearDownTestCase(void)108 void SessionPoolTest::TearDownTestCase(void)
109 {
110 GTEST_LOG_(INFO) << "TearDownTestCase";
111 }
112
SetUp(void)113 void SessionPoolTest::SetUp(void)
114 {
115 GTEST_LOG_(INFO) << "SetUp";
116 }
117
TearDown(void)118 void SessionPoolTest::TearDown(void)
119 {
120 GTEST_LOG_(INFO) << "TearDown";
121 }
122
123 /**
124 * @tc.name: SessionPoolTest_OccupySession_0100
125 * @tc.desc: Verify the OccupySession by Cid function.
126 * @tc.type: FUNC
127 * @tc.require: IA4TFG
128 */
129 HWTEST_F(SessionPoolTest, SessionPoolTest_OccupySession_0100, TestSize.Level1)
130 {
131 GTEST_LOG_(INFO) << "SessionPoolTest_OccupySession_0100 start";
132 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
133 weak_ptr<MountPoint> wmp = smp;
__anon2a640b6c0302(int32_t fd) 134 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](int32_t fd) {});
135 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
136
137 bool res = true;
138 try {
139 pool->OccupySession(TEST_SESSION_ID, 1);
140 } catch (const exception &e) {
141 res = false;
142 GTEST_LOG_(INFO) << e.what();
143 }
144
145 EXPECT_TRUE(res == true);
146 GTEST_LOG_(INFO) << "SessionPoolTest_OccupySession_0100 end";
147 }
148
149 /**
150 * @tc.name: SessionPoolTest_FindSession_0100
151 * @tc.desc: Verify the FindSession by Cid function.
152 * @tc.type: FUNC
153 * @tc.require: IA4TFG
154 */
155 HWTEST_F(SessionPoolTest, SessionPoolTest_FindSession_0100, TestSize.Level1)
156 {
157 GTEST_LOG_(INFO) << "SessionPoolTest_FindSession_0100 start";
158 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
159 weak_ptr<MountPoint> wmp = smp;
__anon2a640b6c0502(int32_t fd) 160 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](int32_t fd) {});
161 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
162
163 bool res = true;
164 try {
165 pool->occupySession_.erase(TEST_SESSION_ID);
166 pool->occupySession_.insert(make_pair(TEST_SESSION_ID, LINK_TYPE_AP));
167 bool flag = pool->FindSession(TEST_SESSION_ID);
168 EXPECT_EQ(flag, true);
169 pool->occupySession_.erase(TEST_SESSION_ID);
170 } catch (const exception &e) {
171 res = false;
172 LOGE("%{public}s", e.what());
173 }
174
175 EXPECT_TRUE(res == true);
176 GTEST_LOG_(INFO) << "SessionPoolTest_FindSession_0100 end";
177 }
178
179 /**
180 * @tc.name: SessionPoolTest_FindSession_0200
181 * @tc.desc: Verify the FindSession by Cid function.
182 * @tc.type: FUNC
183 * @tc.require: IA4TFG
184 */
185 HWTEST_F(SessionPoolTest, SessionPoolTest_FindSession_0200, TestSize.Level1)
186 {
187 GTEST_LOG_(INFO) << "SessionPoolTest_FindSession_0200 start";
188 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
189 weak_ptr<MountPoint> wmp = smp;
__anon2a640b6c0702(int32_t fd) 190 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](int32_t fd) {});
191 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
192
193 bool res = true;
194 try {
195 pool->occupySession_.erase(TEST_SESSION_ID_TWO);
196 bool flag = pool->FindSession(TEST_SESSION_ID_TWO);
197 EXPECT_EQ(flag, false);
198 } catch (const exception &e) {
199 res = false;
200 LOGE("%{public}s", e.what());
201 }
202
203 EXPECT_TRUE(res == true);
204 GTEST_LOG_(INFO) << "SessionPoolTest_FindSession_0200 end";
205 }
206
207 /**
208 * @tc.name: SessionPoolTest_HoldSession_0100
209 * @tc.desc: Verify the HoldSession function.
210 * @tc.type: FUNC
211 * @tc.require: IA4TFG
212 */
213 HWTEST_F(SessionPoolTest, SessionPoolTest_HoldSession_0100, TestSize.Level1)
214 {
215 GTEST_LOG_(INFO) << "SessionPoolTest_HoldSession_0100 start";
216 std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
217 auto session = make_shared<SoftbusSession>(TEST_SESSION_ID, peerDeviceId);
218 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
219 weak_ptr<MountPoint> wmp = smp;
__anon2a640b6c0902(int32_t fd) 220 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](int32_t fd) {});
221 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
222 bool res = true;
223 try {
224 auto size = pool->usrSpaceSessionPool_.size();
225 pool->HoldSession(session, "Server");
226 EXPECT_EQ(pool->usrSpaceSessionPool_.size(), size + 1);
227 } catch (const exception &e) {
228 res = false;
229 LOGE("%{public}s", e.what());
230 }
231
232 EXPECT_TRUE(res == true);
233 GTEST_LOG_(INFO) << "SessionPoolTest_HoldSession_0100 end";
234 }
235
236 /**
237 * @tc.name: SessionPoolTest_HoldSession_0200
238 * @tc.desc: Verify the HoldSession function.
239 * @tc.type: FUNC
240 * @tc.require: IA4TFG
241 */
242 HWTEST_F(SessionPoolTest, SessionPoolTest_HoldSession_0200, TestSize.Level1)
243 {
244 GTEST_LOG_(INFO) << "SessionPoolTest_HoldSession_0200 start";
245 auto session = make_shared<SoftbusSession>(TEST_SESSION_ID, "testNetWork");
246 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
247 weak_ptr<MountPoint> wmp = smp;
__anon2a640b6c0a02(NotifyParam ¶m) 248 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](int32_t fd) {});
249 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
250
251 pool->occupySession_.insert(make_pair(TEST_SESSION_ID, LINK_TYPE_P2P));
252 pool->occupySession_.erase(TEST_SESSION_ID);
253 pool->occupySession_.insert(make_pair(TEST_SESSION_ID, LINK_TYPE_P2P));
254 pool->deviceConnectCount_["testNetWork_2"] = 1;
255 bool res = true;
256 try {
257 auto size = pool->usrSpaceSessionPool_.size();
258 pool->HoldSession(session, "Server");
259 EXPECT_EQ(pool->usrSpaceSessionPool_.size(), size);
260 } catch (const exception &e) {
261 res = false;
262 LOGE("%{public}s", e.what());
263 }
264
265 EXPECT_TRUE(res == true);
266 GTEST_LOG_(INFO) << "SessionPoolTest_HoldSession_0200 end";
267 }
268
269 /**
270 * @tc.name: SessionPoolTest_ReleaseSession_Fd_0100
271 * @tc.desc: Verify the ReleaseSession by Fd function.
272 * @tc.type: FUNC
273 * @tc.require: IA4TFG
274 */
275 HWTEST_F(SessionPoolTest, SessionPoolTest_ReleaseSession_Fd_0100, TestSize.Level1)
276 {
277 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Fd_0100 start";
278 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
279 weak_ptr<MountPoint> wmp = smp;
__anon2a640b6c0c02(NotifyParam ¶m) 280 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](int32_t fd) {});
281 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
282
283 bool res = true;
284 try {
285 pool->ReleaseSession(1);
286 } catch (const exception &e) {
287 res = false;
288 LOGE("%{public}s", e.what());
289 }
290
291 EXPECT_TRUE(res == true);
292 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Fd_0100 end";
293 }
294
295 /**
296 * @tc.name: SessionPoolTest_ReleaseSession_Fd_0200
297 * @tc.desc: Verify the ReleaseSession by Fd function.
298 * @tc.type: FUNC
299 * @tc.require: IA4TFG
300 */
301 HWTEST_F(SessionPoolTest, SessionPoolTest_ReleaseSession_Fd_0200, TestSize.Level1)
302 {
303 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Fd_0200 start";
304 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
305 weak_ptr<MountPoint> wmp = smp;
__anon2a640b6c0e02(NotifyParam ¶m) 306 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](int32_t fd) {});
307 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
308
309 std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
310 auto session = make_shared<SoftbusSession>(TEST_SESSION_ID, peerDeviceId);
311 session->socketFd_ = 1;
312 pool->usrSpaceSessionPool_.push_back(session);
313
314 std::string peerDeviceId2 = "f6d4c0864707aefte7a78f09473aa122ff57ed9";
315 auto session2 = make_shared<SoftbusSession>(TEST_SESSION_ID_TWO, peerDeviceId2);
316 session2->socketFd_ = 1;
317 pool->usrSpaceSessionPool_.push_back(session2);
318
319 auto size = pool->usrSpaceSessionPool_.size();
320 pool->occupySession_.insert(make_pair(TEST_SESSION_ID_TWO, LINK_TYPE_P2P));
321 string key = "deviceId1_" + to_string(LINK_TYPE_P2P);
322 pool->deviceConnectCount_[key] = MOUNT_DFS_COUNT_ONE + 1;
323 EXPECT_EQ(pool->ReleaseSession(1), LINK_TYPE_P2P);
324 EXPECT_EQ(pool->usrSpaceSessionPool_.size(), size - 1);
325
326 auto iter = pool->occupySession_.find(TEST_SESSION_ID_TWO);
327 if (iter == pool->occupySession_.end()) {
328 EXPECT_TRUE(true);
329 } else {
330 EXPECT_TRUE(false);
331 }
332
333 pool->deviceConnectCount_["test"] = 0;
334 pool->ReleaseSession(-2);
335 EXPECT_EQ(pool->deviceConnectCount_.size(), 0);
336
337 pool->usrSpaceSessionPool_.clear();
338 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Fd_0200 end";
339 }
340
341 /**
342 * @tc.name: SessionPoolTest_ReleaseSession_Cid_0100
343 * @tc.desc: Verify the ReleaseSession by Cid function.
344 * @tc.type: FUNC
345 * @tc.require: SR000H0387
346 */
347 HWTEST_F(SessionPoolTest, SessionPoolTest_ReleaseSession_Cid_0100, TestSize.Level1)
348 {
349 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Cid_0100 start";
350 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
351 weak_ptr<MountPoint> wmp = smp;
__anon2a640b6c1002(NotifyParam ¶m) 352 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](int32_t fd) {});
353 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
354
355 bool res = true;
356 try {
357 pool->ReleaseSession("testSession", 1);
358 } catch (const exception &e) {
359 res = false;
360 LOGE("%{public}s", e.what());
361 }
362
363 EXPECT_TRUE(res == true);
364 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Cid_0100 end";
365 }
366
367 /**
368 * @tc.name: SessionPoolTest_ReleaseSession_Cid_0200
369 * @tc.desc: Verify the ReleaseSession by Cid function.
370 * @tc.type: FUNC
371 * @tc.require: IA4TFG
372 */
373 HWTEST_F(SessionPoolTest, SessionPoolTest_ReleaseSession_Cid_0200, TestSize.Level1)
374 {
375 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Cid_0200 start";
376 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
377 weak_ptr<MountPoint> wmp = smp;
__anon2a640b6c1202(NotifyParam ¶m) 378 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](int32_t fd) {});
379 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
380
381 std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
382 auto session = make_shared<SoftbusSession>(TEST_SESSION_ID, peerDeviceId);
383 pool->usrSpaceSessionPool_.push_back(session);
384
385 bool res = true;
386 try {
387 pool->ReleaseSession("testSession", 1);
388 pool->ReleaseSession(peerDeviceId, 1);
389 pool->occupySession_.insert(make_pair(TEST_SESSION_ID, LINK_TYPE_P2P));
390 pool->ReleaseSession(peerDeviceId, 1);
391 auto iter = pool->occupySession_.find(TEST_SESSION_ID);
392 if (iter == pool->occupySession_.end()) {
393 EXPECT_TRUE(false);
394 } else {
395 EXPECT_TRUE(true);
396 }
397 pool->occupySession_.erase(TEST_SESSION_ID);
398 pool->occupySession_.insert(make_pair(TEST_SESSION_ID, 0));
399 pool->ReleaseSession(peerDeviceId, 1);
400 iter = pool->occupySession_.find(TEST_SESSION_ID);
401 if (iter == pool->occupySession_.end()) {
402 EXPECT_TRUE(true);
403 } else {
404 EXPECT_TRUE(false);
405 }
406 } catch (const exception &e) {
407 res = false;
408 LOGE("%{public}s", e.what());
409 }
410 EXPECT_TRUE(res);
411 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Cid_0200 end";
412 }
413
414 /**
415 * @tc.name: SessionPoolTest_ReleaseSession_Cid_0300
416 * @tc.desc: Verify the ReleaseSession by Cid function.
417 * @tc.type: FUNC
418 * @tc.require: IA4TFG
419 */
420 HWTEST_F(SessionPoolTest, SessionPoolTest_ReleaseSession_Cid_0300, TestSize.Level1)
421 {
422 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Cid_0300 start";
423 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
424 weak_ptr<MountPoint> wmp = smp;
__anon2a640b6c1402(NotifyParam ¶m) 425 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](int32_t fd) {});
426 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
427
428 std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
429 auto session = make_shared<SoftbusSession>(TEST_SESSION_ID, peerDeviceId);
430 pool->usrSpaceSessionPool_.push_back(session);
431
432 string key = "deviceId2_2";
433 pool->deviceConnectCount_[key] = MOUNT_DFS_COUNT_ONE + 1;
434
435 bool res = true;
436 try {
437 pool->occupySession_.erase(TEST_SESSION_ID);
438 pool->occupySession_.insert(make_pair(TEST_SESSION_ID, 0));
439 pool->ReleaseSession(peerDeviceId, LINK_TYPE_P2P);
440 auto iter = pool->occupySession_.find(TEST_SESSION_ID);
441 if (iter == pool->occupySession_.end()) {
442 EXPECT_TRUE(true);
443 } else {
444 EXPECT_TRUE(false);
445 }
446 } catch (const exception &e) {
447 res = false;
448 LOGE("%{public}s", e.what());
449 }
450 EXPECT_TRUE(res);
451 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Cid_0300 end";
452 }
453
454 /**
455 * @tc.name: SessionPoolTest_ReleaseAllSession_0100
456 * @tc.desc: Verify the ReleaseAllSession function.
457 * @tc.type: FUNC
458 * @tc.require: SR000H0387
459 */
460 HWTEST_F(SessionPoolTest, SessionPoolTest_ReleaseAllSession_0100, TestSize.Level1)
461 {
462 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseAllSession_0100 start";
463 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
464 weak_ptr<MountPoint> wmp = smp;
__anon2a640b6c1602(NotifyParam ¶m) 465 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](int32_t fd) {});
466 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
467
468 bool res = true;
469 try {
470 pool->ReleaseAllSession();
471 } catch (const exception &e) {
472 res = false;
473 LOGE("%{public}s", e.what());
474 }
475
476 EXPECT_TRUE(res == true);
477 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseAllSession_0100 end";
478 }
479
480 /**
481 * @tc.name: SessionPoolTest_AddSessionToPool_0100
482 * @tc.desc: Verify the AddSessionToPool function.
483 * @tc.type: FUNC
484 * @tc.require: SR000H0387
485 */
486 HWTEST_F(SessionPoolTest, SessionPoolTest_AddSessionToPool_0100, TestSize.Level1)
487 {
488 GTEST_LOG_(INFO) << "SessionPoolTest_AddSessionToPool_0100 start";
489 std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
490 auto session = make_shared<SoftbusSession>(TEST_SESSION_ID, peerDeviceId);
491 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
492 weak_ptr<MountPoint> wmp = smp;
__anon2a640b6c1802(NotifyParam ¶m) 493 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](int32_t fd) {});
494 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
495
496 bool res = true;
497 try {
498 pool->AddSessionToPool(session);
499 } catch (const exception &e) {
500 res = false;
501 LOGE("%{public}s", e.what());
502 }
503
504 EXPECT_TRUE(res == true);
505 GTEST_LOG_(INFO) << "SessionPoolTest_AddSessionToPool_0100 end";
506 }
507
508 /**
509 * @tc.name: SessionPoolTest_DeviceDisconnectCountOnly_0100
510 * @tc.desc: Verify the DeviceDisconnectCountOnly function.
511 * @tc.type: FUNC
512 * @tc.require: IA4TFG
513 */
514 HWTEST_F(SessionPoolTest, SessionPoolTest_DeviceDisconnectCountOnly_0100, TestSize.Level1)
515 {
516 GTEST_LOG_(INFO) << "SessionPoolTest_DeviceDisconnectCountOnly_0100 start";
517 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
518 weak_ptr<MountPoint> wmp = smp;
__anon2a640b6c1a02(NotifyParam ¶m) 519 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](int32_t fd) {});
520 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
521
522 EXPECT_EQ(pool->DeviceDisconnectCountOnly("", LINK_TYPE_AP, false), false);
523
524 EXPECT_EQ(pool->DeviceDisconnectCountOnly("cid", LINK_TYPE_P2P, false), false);
525
526 string key = "cid_2";
527 pool->deviceConnectCount_[key] = 2;
528
529 EXPECT_EQ(pool->DeviceDisconnectCountOnly("cid", LINK_TYPE_P2P, true), false);
530
531 auto itCount = pool->deviceConnectCount_.find(key);
532 if (itCount == pool->deviceConnectCount_.end()) {
533 EXPECT_TRUE(true);
534 } else {
535 EXPECT_TRUE(false);
536 }
537 GTEST_LOG_(INFO) << "SessionPoolTest_DeviceDisconnectCountOnly_0100 end";
538 }
539
540 /**
541 * @tc.name: SessionPoolTest_DeviceDisconnectCountOnly_0200
542 * @tc.desc: Verify the DeviceDisconnectCountOnly function.
543 * @tc.type: FUNC
544 * @tc.require: IA4TFG
545 */
546 HWTEST_F(SessionPoolTest, SessionPoolTest_DeviceDisconnectCountOnly_0200, TestSize.Level1)
547 {
548 GTEST_LOG_(INFO) << "SessionPoolTest_DeviceDisconnectCountOnly_0200 start";
549 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
550 weak_ptr<MountPoint> wmp = smp;
__anon2a640b6c1c02(NotifyParam ¶m) 551 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](int32_t fd) {});
552 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
553
554 string key = "cid_2";
555 pool->deviceConnectCount_[key] = MOUNT_DFS_COUNT_ONE + 1;
556
557 EXPECT_EQ(pool->DeviceDisconnectCountOnly("cid", LINK_TYPE_P2P, false), true);
558
559 EXPECT_EQ(pool->deviceConnectCount_[key], MOUNT_DFS_COUNT_ONE);
560 EXPECT_EQ(pool->DeviceDisconnectCountOnly("cid", LINK_TYPE_P2P, false), false);
561
562 auto itCount = pool->deviceConnectCount_.find(key);
563 if (itCount == pool->deviceConnectCount_.end()) {
564 EXPECT_TRUE(true);
565 } else {
566 EXPECT_TRUE(false);
567 }
568 GTEST_LOG_(INFO) << "SessionPoolTest_DeviceDisconnectCountOnly_0200 end";
569 }
570
571 /**
572 * @tc.name: SessionPoolTest_DeviceConnectCountOnly_0100
573 * @tc.desc: Verify the DeviceConnectCountOnly function.
574 * @tc.type: FUNC
575 * @tc.require: IA4TFG
576 */
577 HWTEST_F(SessionPoolTest, SessionPoolTest_DeviceConnectCountOnly_0100, TestSize.Level1)
578 {
579 GTEST_LOG_(INFO) << "SessionPoolTest_DeviceConnectCountOnly_0100 start";
580 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
581 weak_ptr<MountPoint> wmp = smp;
__anon2a640b6c1e02(NotifyParam ¶m) 582 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](int32_t fd) {});
583 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
584 std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
585 auto session = make_shared<SoftbusSession>(TEST_SESSION_ID, peerDeviceId);
586 EXPECT_EQ(pool->DeviceConnectCountOnly(session), false);
587
588 auto session2 = make_shared<SoftbusSession>(TEST_SESSION_ID, "");
589 EXPECT_EQ(pool->DeviceConnectCountOnly(session2), false);
590
591 auto session3 = make_shared<SoftbusSession>(TEST_SESSION_ID, "testNetWork");
592 pool->occupySession_.insert(make_pair(TEST_SESSION_ID, LINK_TYPE_AP));
593 EXPECT_EQ(pool->DeviceConnectCountOnly(session3), false);
594
595 pool->occupySession_.erase(TEST_SESSION_ID);
596 pool->occupySession_.insert(make_pair(TEST_SESSION_ID, LINK_TYPE_P2P));
597 pool->deviceConnectCount_["testNetWork_2"] = 0;
598 EXPECT_EQ(pool->DeviceConnectCountOnly(session3), false);
599 EXPECT_EQ(pool->deviceConnectCount_["testNetWork_2"], 1);
600
601 EXPECT_EQ(pool->DeviceConnectCountOnly(session3), true);
602 EXPECT_EQ(pool->deviceConnectCount_["testNetWork_2"], 2);
603 GTEST_LOG_(INFO) << "SessionPoolTest_DeviceConnectCountOnly_0100 end";
604 }
605 } // namespace Test
606 } // namespace DistributedFile
607 } // namespace Storage
608 } // namespace OHOS
609