• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class SessionPoolTest : public testing::Test {
91 public:
92     static void SetUpTestCase(void);
93     static void TearDownTestCase(void);
94     void SetUp();
95     void TearDown();
96 };
97 
SetUpTestCase(void)98 void SessionPoolTest::SetUpTestCase(void)
99 {
100     GTEST_LOG_(INFO) << "SetUpTestCase";
101 }
102 
TearDownTestCase(void)103 void SessionPoolTest::TearDownTestCase(void)
104 {
105     GTEST_LOG_(INFO) << "TearDownTestCase";
106 }
107 
SetUp(void)108 void SessionPoolTest::SetUp(void)
109 {
110     GTEST_LOG_(INFO) << "SetUp";
111 }
112 
TearDown(void)113 void SessionPoolTest::TearDown(void)
114 {
115     GTEST_LOG_(INFO) << "TearDown";
116 }
117 
118 /**
119  * @tc.name: SessionPoolTest_HoldSession_0100
120  * @tc.desc: Verify the HoldSession function.
121  * @tc.type: FUNC
122  * @tc.require: IA4TFG
123  */
124 HWTEST_F(SessionPoolTest, SessionPoolTest_HoldSession_0100, TestSize.Level1)
125 {
126     GTEST_LOG_(INFO) << "SessionPoolTest_HoldSession_0100 start";
127     std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
128     auto session = make_shared<SoftbusSession>(TEST_SESSION_ID,  peerDeviceId);
129     auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
130     weak_ptr<MountPoint> wmp = smp;
__anon6e0244560202(NotifyParam &param) 131     auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam &param) {}, [](const std::string &) {});
132     shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
133     bool res = true;
134     try {
135         auto size = pool->usrSpaceSessionPool_.size();
136         pool->HoldSession(session, "Server");
137         EXPECT_EQ(pool->usrSpaceSessionPool_.size(), size + 1);
138     } catch (const exception &e) {
139         res = false;
140         LOGE("%{public}s", e.what());
141     }
142 
143     EXPECT_TRUE(res == true);
144     GTEST_LOG_(INFO) << "SessionPoolTest_HoldSession_0100 end";
145 }
146 
147 /**
148  * @tc.name: SessionPoolTest_HoldSession_0101
149  * @tc.desc: Verify the HoldSession function.
150  * @tc.type: FUNC
151  * @tc.require: IA4TFG
152  */
153 HWTEST_F(SessionPoolTest, SessionPoolTest_HoldSession_0101, TestSize.Level1)
154 {
155     GTEST_LOG_(INFO) << "SessionPoolTest_HoldSession_0101 start";
156     std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
157     auto session = make_shared<SoftbusSession>(TEST_SESSION_ID,  peerDeviceId);
158     auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
159     weak_ptr<MountPoint> wmp = smp;
__anon6e0244560402(NotifyParam &param) 160     auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam &param) {}, [](const std::string &) {});
161     shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
162     bool res = true;
163     try {
164         auto size = pool->usrSpaceSessionPool_.size();
165         pool->talker_ = nullptr;
166         pool->HoldSession(session, "Server");
167         EXPECT_EQ(pool->usrSpaceSessionPool_.size(), size + 1);
168     } catch (const exception &e) {
169         res = false;
170         LOGE("%{public}s", e.what());
171     }
172 
173     EXPECT_TRUE(res == true);
174     GTEST_LOG_(INFO) << "SessionPoolTest_HoldSession_0101 end";
175 }
176 
177 /**
178  * @tc.name: SessionPoolTest_ReleaseSession_Fd_0100
179  * @tc.desc: Verify the ReleaseSession by Fd function.
180  * @tc.type: FUNC
181  * @tc.require: IA4TFG
182  */
183 HWTEST_F(SessionPoolTest, SessionPoolTest_ReleaseSession_Fd_0100, TestSize.Level1)
184 {
185     GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Fd_0100 start";
186     auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
187     weak_ptr<MountPoint> wmp = smp;
__anon6e0244560602(NotifyParam &param) 188     auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam &param) {}, [](const std::string &) {});
189     shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
190     std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
191     auto session = make_shared<SoftbusSession>(TEST_SESSION_ID,  peerDeviceId);
192     pool->usrSpaceSessionPool_.push_back(session);
193     pool->ReleaseSession(0);
194     EXPECT_EQ(pool->usrSpaceSessionPool_.size(), 1); // 1: session size
195 
196     pool->ReleaseSession(-1); // -1: session fd
197     EXPECT_EQ(pool->usrSpaceSessionPool_.size(), 0); // 0: session size
198     GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Fd_0100 end";
199 }
200 
201 /**
202  * @tc.name: SessionPoolTest_ReleaseSession_Cid_0100
203  * @tc.desc: Verify the ReleaseSession by Cid function.
204  * @tc.type: FUNC
205  * @tc.require: SR000H0387
206  */
207 HWTEST_F(SessionPoolTest, SessionPoolTest_ReleaseSession_Cid_0100, TestSize.Level1)
208 {
209     GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Cid_0100 start";
210     auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
211     weak_ptr<MountPoint> wmp = smp;
__anon6e0244560802(NotifyParam &param) 212     auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam &param) {}, [](const std::string &) {});
213     shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
214 
215     std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
216     auto session = make_shared<SoftbusSession>(TEST_SESSION_ID,  peerDeviceId);
217     session->SetFromServer(true);
218     pool->usrSpaceSessionPool_.push_back(session);
219 
220     std::string peerDeviceId1 = "f6d4c0864707aefte7a78f09473aa122ff57fc7";
221     auto session1 = make_shared<SoftbusSession>(TEST_SESSION_ID, peerDeviceId1);
222     session1->SetFromServer(false);
223     pool->usrSpaceSessionPool_.push_back(session1);
224 
225     auto session2 = make_shared<SoftbusSession>(TEST_SESSION_ID,  peerDeviceId);
226     session2->SetFromServer(false);
227     pool->usrSpaceSessionPool_.push_back(session2);
228     bool ifReleaseService = false;
229     size_t len = 3; // 3: session size;
230 
231     pool->ReleaseSession("test", ifReleaseService);
232     EXPECT_EQ(pool->usrSpaceSessionPool_.size(), len);
233 
234     pool->ReleaseSession(peerDeviceId, ifReleaseService);
235     EXPECT_EQ(pool->usrSpaceSessionPool_.size(), len - 1); // 1: remove session
236 
237     pool->ReleaseSession(peerDeviceId1, ifReleaseService);
238     EXPECT_EQ(pool->usrSpaceSessionPool_.size(), len - 2); // 2: remove session1
239 
240     ifReleaseService = true;
241     pool->ReleaseSession(peerDeviceId, ifReleaseService);
242     EXPECT_EQ(pool->usrSpaceSessionPool_.size(), 0); // 1: remove one
243     pool->talker_ = nullptr;
244     pool->ReleaseSession(peerDeviceId, ifReleaseService);
245     EXPECT_EQ(pool->usrSpaceSessionPool_.size(), 0);
246     GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Cid_0100 end";
247 }
248 
249 /**
250  * @tc.name: SessionPoolTest_ReleaseAllSession_0100
251  * @tc.desc: Verify the ReleaseAllSession function.
252  * @tc.type: FUNC
253  * @tc.require: SR000H0387
254  */
255 HWTEST_F(SessionPoolTest, SessionPoolTest_ReleaseAllSession_0100, TestSize.Level1)
256 {
257     GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseAllSession_0100 start";
258     auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
259     weak_ptr<MountPoint> wmp = smp;
__anon6e0244560a02(NotifyParam &param) 260     auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam &param) {}, [](const std::string &) {});
261     shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
262     std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
263     auto session = make_shared<SoftbusSession>(TEST_SESSION_ID,  peerDeviceId);
264     pool->usrSpaceSessionPool_.push_back(session);
265 
266     bool res = true;
267     try {
268         pool->ReleaseAllSession();
269     } catch (const exception &e) {
270         res = false;
271         LOGE("%{public}s", e.what());
272     }
273 
274     EXPECT_TRUE(res == true);
275     GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseAllSession_0100 end";
276 }
277 
278 /**
279  * @tc.name: SessionPoolTest_AddSessionToPool_0100
280  * @tc.desc: Verify the AddSessionToPool function.
281  * @tc.type: FUNC
282  * @tc.require: SR000H0387
283  */
284 HWTEST_F(SessionPoolTest, SessionPoolTest_AddSessionToPool_0100, TestSize.Level1)
285 {
286     GTEST_LOG_(INFO) << "SessionPoolTest_AddSessionToPool_0100 start";
287     std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
288     auto session = make_shared<SoftbusSession>(TEST_SESSION_ID, peerDeviceId);
289     auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
290     weak_ptr<MountPoint> wmp = smp;
__anon6e0244560c02(NotifyParam &param) 291     auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam &param) {}, [](const std::string &) {});
292     shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
293 
294     bool res = true;
295     try {
296         pool->AddSessionToPool(session);
297     } catch (const exception &e) {
298         res = false;
299         LOGE("%{public}s", e.what());
300     }
301 
302     EXPECT_TRUE(res == true);
303     GTEST_LOG_(INFO) << "SessionPoolTest_AddSessionToPool_0100 end";
304 }
305 
306 /**
307  * @tc.name: SessionPoolTest_CheckIfGetSession_0100
308  * @tc.desc: Verify the CheckIfGetSession function.
309  * @tc.type: FUNC
310  * @tc.require: SR000H0387
311  */
312 HWTEST_F(SessionPoolTest, SessionPoolTest_CheckIfGetSession_0100, TestSize.Level1)
313 {
314     GTEST_LOG_(INFO) << "SessionPoolTest_CheckIfGetSession_0100 start";
315     std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
316     auto session = make_shared<SoftbusSession>(TEST_SESSION_ID, peerDeviceId);
317     auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
318     weak_ptr<MountPoint> wmp = smp;
__anon6e0244560e02(NotifyParam &param) 319     auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam &param) {}, [](const std::string &) {});
320     shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
321     int32_t fd = -1; // -1: fd
322     bool ret = pool->CheckIfGetSession(fd);
323     EXPECT_EQ(ret, false);
324 
325     pool->usrSpaceSessionPool_.push_back(session);
326     ret = pool->CheckIfGetSession(fd);
327     EXPECT_EQ(ret, true);
328     GTEST_LOG_(INFO) << "SessionPoolTest_CheckIfGetSession_0100 end";
329 }
330 
331 /**
332  * @tc.name: SessionPoolTest_SinkOffLine_0100
333  * @tc.desc: Verify the SinkOffLine function.
334  * @tc.type: FUNC
335  * @tc.require: SR000H0387
336  */
337 HWTEST_F(SessionPoolTest, SessionPoolTest_SinkOffLine_0100, TestSize.Level1)
338 {
339     GTEST_LOG_(INFO) << "SessionPoolTest_SinkOffLine_0100 start";
340     std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
341     auto session = make_shared<SoftbusSession>(TEST_SESSION_ID, peerDeviceId);
342     auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
343     weak_ptr<MountPoint> wmp = smp;
__anon6e0244561002(NotifyParam &param) 344     auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam &param) {}, [](const std::string &) {});
345     shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
346     pool->usrSpaceSessionPool_.push_back(session);
347     pool->SinkOffline(peerDeviceId);
348     pool->SinkOffline("test");
349     GTEST_LOG_(INFO) << "SessionPoolTest_SinkOffLine_0100 end";
350 }
351 
352 /**
353  * @tc.name: SessionPoolTest_SinkOffLine_0101
354  * @tc.desc: Verify the SinkOffLine function.
355  * @tc.type: FUNC
356  * @tc.require: SR000H0387
357  */
358 HWTEST_F(SessionPoolTest, SessionPoolTest_SinkOffLine_0101, TestSize.Level1)
359 {
360     GTEST_LOG_(INFO) << "SessionPoolTest_SinkOffLine_0101 start";
361     std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
362     auto session = make_shared<SoftbusSession>(TEST_SESSION_ID, peerDeviceId);
363     auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
364     weak_ptr<MountPoint> wmp = smp;
__anon6e0244561202(NotifyParam &param) 365     auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam &param) {}, [](const std::string &) {});
366     shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
367     pool->usrSpaceSessionPool_.push_back(session);
368     pool->talker_ = nullptr;
369     pool->SinkOffline(peerDeviceId);
370     pool->SinkOffline("test");
371     GTEST_LOG_(INFO) << "SessionPoolTest_SinkOffLine_0101 end";
372 }
373 
374 /**
375  * @tc.name: SessionPoolTest_FindSocketId_0100
376  * @tc.desc: Verify the FindSocketId function.
377  * @tc.type: FUNC
378  * @tc.require: SR000H0387
379  */
380 HWTEST_F(SessionPoolTest, SessionPoolTest_FindSocketId_0100, TestSize.Level1)
381 {
382     GTEST_LOG_(INFO) << "SessionPoolTest_FindSocketId_0100 start";
383     std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
384     auto session = make_shared<SoftbusSession>(TEST_SESSION_ID, peerDeviceId);
385     auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account"));
386     weak_ptr<MountPoint> wmp = smp;
__anon6e0244561402(NotifyParam &param) 387     auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam &param) {}, [](const std::string &) {});
388     shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker);
389     pool->usrSpaceSessionPool_.push_back(session);
390     bool ret = pool->FindSocketId(-1); // -1: session id
391     EXPECT_EQ(ret, false);
392 
393     ret = pool->FindSocketId(TEST_SESSION_ID);
394     EXPECT_EQ(ret, true);
395     GTEST_LOG_(INFO) << "SessionPoolTest_FindSocketId_0100 end";
396 }
397 } // namespace Test
398 } // namespace DistributedFile
399 } // namespace Storage
400 } // namespace OHOS
401