• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 #include "network/softbus/softbus_handler.h"
16 
17 #include "gtest/gtest.h"
18 #include <memory>
19 #include <unistd.h>
20 #include <utility>
21 #include <securec.h>
22 
23 #include "device_manager_impl_mock.h"
24 #include "dm_constants.h"
25 
26 #include "dfs_error.h"
27 #include "mock_other_method.h"
28 #include "network/softbus/softbus_file_receive_listener.h"
29 #include "network/softbus/softbus_session_pool.h"
30 #include "socket_mock.h"
31 #include "utils_directory.h"
32 #include "utils_log.h"
33 
34 using namespace OHOS::DistributedHardware;
35 using namespace OHOS::FileManagement;
36 using namespace std;
37 
38 namespace {
39 const string TEST_NETWORKID = "45656596896323231";
40 const string TEST_NETWORKID_TWO = "45656596896323232";
41 const string TEST_NETWORKID_THREE = "45656596896323233";
42 constexpr int SESSION_ID_ONE = 1;
43 constexpr int UID_ONE = 1;
44 }
45 
46 namespace OHOS {
47 namespace Storage {
48 namespace DistributedFile {
49 namespace Test {
50 using namespace testing;
51 using namespace testing::ext;
52 class SoftbusHandlerTest : public testing::Test {
53 public:
54     static void SetUpTestCase(void);
55     static void TearDownTestCase(void);
56     void SetUp();
57     void TearDown();
58     void CheckSrcSameAccountPass();
59     void CheckSrcDiffAccountPass();
60     void CheckSrcBothSamePass();
61     void CheckSrcBothDiffPass();
62     static inline shared_ptr<DfsDeviceOtherMethodMock> otherMethodMock_ = nullptr;
63     static inline shared_ptr<SocketMock> socketMock_ = nullptr;
64     static inline shared_ptr<DeviceManagerImplMock> deviceManagerImplMock_ = nullptr;
65 };
66 
CheckSrcSameAccountPass()67 void SoftbusHandlerTest::CheckSrcSameAccountPass()
68 {
69     AccountSA::OhosAccountInfo osAccountInfo;
70     osAccountInfo.uid_ = "test";
71     std::vector<int32_t> userIds{100, 101};
72     EXPECT_CALL(*otherMethodMock_, QueryActiveOsAccountIds(_))
73         .WillOnce(DoAll(SetArgReferee<0>(userIds), Return(FileManagement::E_OK)));
74     EXPECT_CALL(*otherMethodMock_, GetOhosAccountInfo(_))
75         .WillOnce(DoAll(SetArgReferee<0>(osAccountInfo), Return(FileManagement::E_OK)));
76     EXPECT_CALL(*deviceManagerImplMock_, GetLocalDeviceInfo(_, _)).WillOnce(Return(0));
77     EXPECT_CALL(*deviceManagerImplMock_, CheckSrcIsSameAccount(_, _)).WillOnce(Return(true));
78 }
79 
CheckSrcDiffAccountPass()80 void SoftbusHandlerTest::CheckSrcDiffAccountPass()
81 {
82     std::vector<int32_t> userIds{100, 101};
83     EXPECT_CALL(*otherMethodMock_, QueryActiveOsAccountIds(_))
84         .WillOnce(DoAll(SetArgReferee<0>(userIds), Return(FileManagement::E_OK)));
85     EXPECT_CALL(*deviceManagerImplMock_, GetLocalDeviceInfo(_, _)).WillOnce(Return(0));
86 }
87 
CheckSrcBothSamePass()88 void SoftbusHandlerTest::CheckSrcBothSamePass()
89 {
90     AccountSA::OhosAccountInfo osAccountInfo;
91     osAccountInfo.uid_ = "test";
92     std::vector<int32_t> userIds{100, 101};
93     EXPECT_CALL(*otherMethodMock_, QueryActiveOsAccountIds(_))
94         .WillOnce(DoAll(SetArgReferee<0>(userIds), Return(FileManagement::E_OK)))
95         .WillOnce(DoAll(SetArgReferee<0>(userIds), Return(FileManagement::E_OK)));
96     EXPECT_CALL(*otherMethodMock_, GetOhosAccountInfo(_))
97         .WillOnce(DoAll(SetArgReferee<0>(osAccountInfo), Return(FileManagement::E_OK)))
98         .WillOnce(DoAll(SetArgReferee<0>(osAccountInfo), Return(FileManagement::E_OK)));
99     EXPECT_CALL(*deviceManagerImplMock_, GetLocalDeviceInfo(_, _)).WillOnce(Return(0)).WillOnce(Return(0));
100     EXPECT_CALL(*deviceManagerImplMock_, CheckSrcIsSameAccount(_, _)).WillOnce(Return(true));
101     EXPECT_CALL(*socketMock_, SetAccessInfo(_, _)).WillOnce(Return(FileManagement::E_OK));
102 }
103 
CheckSrcBothDiffPass()104 void SoftbusHandlerTest::CheckSrcBothDiffPass()
105 {
106     std::vector<int32_t> userIds{100, 101};
107     EXPECT_CALL(*otherMethodMock_, QueryActiveOsAccountIds(_))
108         .WillOnce(DoAll(SetArgReferee<0>(userIds), Return(FileManagement::E_OK)))
109         .WillOnce(DoAll(SetArgReferee<0>(userIds), Return(FileManagement::E_OK)));
110     EXPECT_CALL(*deviceManagerImplMock_, GetLocalDeviceInfo(_, _)).WillOnce(Return(0)).WillOnce(Return(0));
111 }
112 
SetUpTestCase(void)113 void SoftbusHandlerTest::SetUpTestCase(void)
114 {
115     GTEST_LOG_(INFO) << "SetUpTestCase";
116     otherMethodMock_ = make_shared<DfsDeviceOtherMethodMock>();
117     DfsDeviceOtherMethodMock::otherMethod = otherMethodMock_;
118     socketMock_ = make_shared<SocketMock>();
119     SocketMock::dfsSocket = socketMock_;
120     deviceManagerImplMock_ = make_shared<DeviceManagerImplMock>();
121     DeviceManagerImplMock::dfsDeviceManagerImpl = deviceManagerImplMock_;
122 }
123 
TearDownTestCase(void)124 void SoftbusHandlerTest::TearDownTestCase(void)
125 {
126     GTEST_LOG_(INFO) << "TearDownTestCase";
127     SocketMock::dfsSocket = nullptr;
128     socketMock_ = nullptr;
129     DeviceManagerImplMock::dfsDeviceManagerImpl = nullptr;
130     deviceManagerImplMock_ = nullptr;
131     DfsDeviceOtherMethodMock::otherMethod = nullptr;
132     otherMethodMock_ = nullptr;
133 }
134 
SetUp(void)135 void SoftbusHandlerTest::SetUp(void)
136 {
137     GTEST_LOG_(INFO) << "SetUp";
138 }
139 
TearDown(void)140 void SoftbusHandlerTest::TearDown(void)
141 {
142     SoftBusHandler::GetInstance().clientSessNameMap_.clear();
143     SoftBusHandler::GetInstance().serverIdMap_.clear();
144     GTEST_LOG_(INFO) << "TearDown";
145 }
146 
147 /**
148  * @tc.name: SoftbusHandlerTest_CreateSessionServer_0100
149  * @tc.desc: Verify the OccupySession by Cid function.
150  * @tc.type: FUNC
151  * @tc.require: I9JXPR
152  */
153 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_CreateSessionServer_0100, TestSize.Level1)
154 {
155     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CreateSessionServer_0100 start";
156     SoftBusHandler handler;
157     std::string packageName = "com.example.test";
158     std::string sessionName = "testSession";
159     DFS_CHANNEL_ROLE role = DFS_CHANNLE_ROLE_SOURCE;
160     std::string physicalPath = "/data/test";
161     EXPECT_CALL(*socketMock_, Socket(_)).WillOnce(Return(-1));
162     int32_t result = handler.CreateSessionServer(packageName, sessionName, role, physicalPath);
163     handler.serverIdMap_.erase(sessionName);
164     EXPECT_EQ(result, ERR_BAD_VALUE);
165 
166     EXPECT_CALL(*socketMock_, Socket(_)).WillOnce(Return(0));
167     EXPECT_CALL(*socketMock_, Listen(_, _, _, _)).WillOnce(Return(-1));
168     result = handler.CreateSessionServer(packageName, sessionName, role, physicalPath);
169     handler.serverIdMap_.erase(sessionName);
170     EXPECT_EQ(result, ERR_BAD_VALUE);
171 
172     EXPECT_CALL(*socketMock_, Socket(_)).WillOnce(Return(0));
173     EXPECT_CALL(*socketMock_, Listen(_, _, _, _)).WillOnce(Return(0));
174     result = handler.CreateSessionServer(packageName, sessionName, role, physicalPath);
175     EXPECT_EQ(result, E_OK);
176     if (handler.serverIdMap_.find(sessionName) != handler.serverIdMap_.end()) {
177         EXPECT_TRUE(true);
178     } else {
179         EXPECT_TRUE(false);
180     }
181     handler.serverIdMap_.erase(sessionName);
182     EXPECT_EQ(string(SoftBusFileReceiveListener::GetRecvPath()), physicalPath);
183     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CreateSessionServer_0100 end";
184 }
185 
186 /**
187  * @tc.name: SoftbusHandlerTest_CreateSessionServer_0200
188  * @tc.desc: Verify the OccupySession by Cid function.
189  * @tc.type: FUNC
190  * @tc.require: I9JXPR
191  */
192 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_CreateSessionServer_0200, TestSize.Level1)
193 {
194     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CreateSessionServer_0200 start";
195     SoftBusHandler handler;
196     std::string packageName = "com.example.test";
197     std::string sessionName = "testSession";
198     DFS_CHANNEL_ROLE role = DFS_CHANNLE_ROLE_SOURCE;
199     std::string physicalPath = "/data/test";
200 
201     auto result = handler.CreateSessionServer("", sessionName, role, physicalPath);
202     EXPECT_EQ(result, ERR_BAD_VALUE);
203 
204     result = handler.CreateSessionServer(packageName, "", role, physicalPath);
205     EXPECT_EQ(result, ERR_BAD_VALUE);
206 
207     result = handler.CreateSessionServer(packageName, sessionName, role, "");
208     EXPECT_EQ(result, ERR_BAD_VALUE);
209     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CreateSessionServer_0200 end";
210 }
211 
212 /**
213  * @tc.name: SoftbusHandlerTest_CreateSessionServer_0300
214  * @tc.desc: Verify the CreateSessionServer by Cid function.
215  * @tc.type: FUNC
216  * @tc.require: I9JXPR
217  */
218 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_CreateSessionServer_0300, TestSize.Level1)
219 {
220     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CreateSessionServer_0300 start";
221     std::string packageName = "com.example.test";
222     std::string sessionName = "sessionName";
223     DFS_CHANNEL_ROLE role = DFS_CHANNLE_ROLE_SOURCE;
224     std::string physicalPath = "/data/test";
225 
226     auto result = SoftBusHandler::GetInstance().CreateSessionServer("", sessionName, role, physicalPath);
227     EXPECT_EQ(result, ERR_BAD_VALUE);
228     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CreateSessionServer_0300 end";
229 }
230 
231 /**
232  * @tc.name: SoftbusHandlerTest_OpenSession_0100
233  * @tc.desc: Verify the OccupySession by Cid function.
234  * @tc.type: FUNC
235  * @tc.require: I9JXPR
236  */
237 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_OpenSession_0100, TestSize.Level1)
238 {
239 #ifndef SUPPORT_SAME_ACCOUNT
240     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OpenSession_0100 start";
241     SoftBusHandler handler;
242     std::string packageName = "com.example.test";
243     std::string sessionName = "testSession";
244     int32_t socketId;
245 
246     CheckSrcDiffAccountPass();
247     EXPECT_CALL(*socketMock_, Socket(_)).WillOnce(Return(-1));
248     int32_t result = handler.OpenSession(packageName, sessionName, TEST_NETWORKID, socketId);
249     EXPECT_EQ(result, ERR_BAD_VALUE);
250 
251     CheckSrcBothDiffPass();
252     EXPECT_CALL(*socketMock_, Socket(_)).WillOnce(Return(1));
253     EXPECT_CALL(*socketMock_, Bind(_, _, _, _)).WillOnce(Return(-1));
254     result = handler.OpenSession(packageName, sessionName, TEST_NETWORKID, socketId);
255     EXPECT_EQ(result, ERR_BAD_VALUE);
256 
257     CheckSrcBothDiffPass();
258     EXPECT_CALL(*socketMock_, Socket(_)).WillOnce(Return(1));
259     EXPECT_CALL(*socketMock_, Bind(_, _, _, _)).WillOnce(Return(0));
260     result = handler.OpenSession(packageName, sessionName, TEST_NETWORKID, socketId);
261     EXPECT_EQ(result, 0);
262 
263     if (handler.clientSessNameMap_.find(socketId) != handler.clientSessNameMap_.end()) {
264         EXPECT_TRUE(true);
265     } else {
266         EXPECT_TRUE(false);
267     }
268 
269     handler.clientSessNameMap_.erase(socketId);
270 #endif
271     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OpenSession_0100 end";
272 }
273 
274 /**
275  * @tc.name: SoftbusHandlerTest_OpenSession_0200
276  * @tc.desc: Verify the OccupySession by Cid function.
277  * @tc.type: FUNC
278  * @tc.require: I9JXPR
279  */
280 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_OpenSession_0200, TestSize.Level1)
281 {
282     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OpenSession_0200 start";
283 #ifdef SUPPORT_SAME_ACCOUNT
284     SoftBusHandler handler;
285     std::string packageName = "com.example.test";
286     std::string sessionName = "testSession";
287     int32_t socketId;
288 
289     CheckSrcSameAccountPass();
290     std::vector<DmDeviceInfo> deviceList;
291     DmDeviceInfo deviceInfo;
292     deviceInfo.authForm = DmAuthForm::IDENTICAL_ACCOUNT;
293     deviceInfo.extraData = "{\"OS_TYPE\":10}";
294     auto res = strcpy_s(deviceInfo.networkId, DM_MAX_DEVICE_ID_LEN, TEST_NETWORKID.c_str());
295     if (res != 0) {
296         GTEST_LOG_(INFO) << "strcpy_s failed";
297         return;
298     }
299     deviceList.push_back(deviceInfo);
300     EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _))
301         .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0)));
302     EXPECT_CALL(*socketMock_, Socket(_)).WillOnce(Return(-1));
303     int32_t result = handler.OpenSession(packageName, sessionName, TEST_NETWORKID, socketId);
304     EXPECT_EQ(result, ERR_BAD_VALUE);
305 
306     CheckSrcBothSamePass();
307     EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _))
308         .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0)));
309     EXPECT_CALL(*socketMock_, Socket(_)).WillOnce(Return(1));
310     EXPECT_CALL(*socketMock_, Bind(_, _, _, _)).WillOnce(Return(-1));
311     result = handler.OpenSession(packageName, sessionName, TEST_NETWORKID, socketId);
312     EXPECT_EQ(result, ERR_BAD_VALUE);
313 
314     CheckSrcBothSamePass();
315     EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _))
316         .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0)));
317     EXPECT_CALL(*socketMock_, Socket(_)).WillOnce(Return(1));
318     EXPECT_CALL(*socketMock_, Bind(_, _, _, _)).WillOnce(Return(0));
319     result = handler.OpenSession(packageName, sessionName, TEST_NETWORKID, socketId);
320     EXPECT_EQ(result, 0);
321 
322     if (handler.clientSessNameMap_.find(socketId) != handler.clientSessNameMap_.end()) {
323         EXPECT_TRUE(true);
324     } else {
325         EXPECT_TRUE(false);
326     }
327 
328     handler.clientSessNameMap_.erase(socketId);
329 #endif
330     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OpenSession_0200 end";
331 }
332 
333 /**
334  * @tc.name: SoftbusHandlerTest_OpenSession_0300
335  * @tc.desc: Verify the OpenSession by Cid function.
336  * @tc.type: FUNC
337  * @tc.require: I9JXPR
338  */
339 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_OpenSession_0300, TestSize.Level1)
340 {
341     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OpenSession_0300 start";
342 #ifndef SUPPORT_SAME_ACCOUNT
343     SoftBusHandler handler;
344     std::string packageName = "com.example.test";
345     std::string sessionName = "testSession";
346     int32_t socketId;
347     std::string physicalPath = "/data/test";
348     CheckSrcDiffAccountPass();
349     int32_t result = handler.OpenSession("", sessionName, physicalPath, socketId);
350     EXPECT_EQ(result, ERR_BAD_VALUE);
351 
352     CheckSrcDiffAccountPass();
353     result = handler.OpenSession(packageName, "", physicalPath, socketId);
354     EXPECT_EQ(result, ERR_BAD_VALUE);
355 
356     CheckSrcDiffAccountPass();
357     result = handler.OpenSession(packageName, sessionName, "", socketId);
358     EXPECT_EQ(result, ERR_BAD_VALUE);
359 #endif
360     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OpenSession_0300 end";
361 }
362 
363 /**
364  * @tc.name: SoftbusHandlerTest_OpenSession_0400
365  * @tc.desc: Verify the OpenSession by Cid function.
366  * @tc.type: FUNC
367  * @tc.require: I9JXPR
368  */
369 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_OpenSession_0400, TestSize.Level1)
370 {
371     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OpenSession_0400 start";
372 #ifdef SUPPORT_SAME_ACCOUNT
373     SoftBusHandler handler;
374     std::string packageName = "com.example.test";
375     std::string sessionName = "testSession";
376     int32_t socketId;
377     std::string physicalPath = "/data/test";
378     CheckSrcSameAccountPass();
379     int32_t result = handler.OpenSession("", sessionName, physicalPath, socketId);
380     EXPECT_EQ(result, ERR_BAD_VALUE);
381 
382     CheckSrcSameAccountPass();
383     result = handler.OpenSession(packageName, "", physicalPath, socketId);
384     EXPECT_EQ(result, ERR_BAD_VALUE);
385 
386     CheckSrcSameAccountPass();
387     result = handler.OpenSession(packageName, sessionName, "", socketId);
388     EXPECT_EQ(result, ERR_BAD_VALUE);
389 #endif
390     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OpenSession_0400 end";
391 }
392 
393 /**
394  * @tc.name: SoftbusHandlerTest_ChangeOwnerIfNeeded_0100
395  * @tc.desc: Verify the ChangeOwnerIfNeeded by Cid function.
396  * @tc.type: FUNC
397  * @tc.require: I9JXPR
398  */
399 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_ChangeOwnerIfNeeded_0100, TestSize.Level1)
400 {
401     GTEST_LOG_(INFO) << "SoftbusHandlerTest_ChangeOwnerIfNeeded_0100 start";
402     bool res = true;
403     try {
404         SoftBusHandler handler;
405         handler.ChangeOwnerIfNeeded(1, "");
406         handler.ChangeOwnerIfNeeded(1, "sessionName");
407 
408         SoftBusSessionPool::SessionInfo sessionInfo1{.sessionId = SESSION_ID_ONE,
409                                                     .srcUri = "file://com.demo.a/test/1",
410                                                     .dstPath = "/data/test/1",
411                                                     .uid = UID_ONE};
412         string sessionName1 = "sessionName1";
413         SoftBusSessionPool::GetInstance().AddSessionInfo(sessionName1, sessionInfo1);
414         handler.ChangeOwnerIfNeeded(1, sessionName1);
415         SoftBusSessionPool::GetInstance().DeleteSessionInfo(sessionName1);
416     } catch(...) {
417         res = false;
418     }
419 
420     EXPECT_TRUE(res);
421     GTEST_LOG_(INFO) << "SoftbusHandlerTest_ChangeOwnerIfNeeded_0100 end";
422 }
423 
424 /**
425  * @tc.name: SoftbusHandlerTest_GetSessionName_0100
426  * @tc.desc: Verify the GetSessionName by Cid function.
427  * @tc.type: FUNC
428  * @tc.require: I9JXPR
429  */
430 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_GetSessionName_0100, TestSize.Level1)
431 {
432     GTEST_LOG_(INFO) << "SoftbusHandlerTest_GetSessionName_0100 start";
433     string testSessionName = "mySessionName";
434     int32_t sessionId = 1;
435     SoftBusHandler::clientSessNameMap_.insert(std::make_pair(sessionId, testSessionName));
436     SoftBusHandler handler;
437     string sessionName = handler.GetSessionName(sessionId);
438     EXPECT_EQ(sessionName, testSessionName);
439     SoftBusHandler::clientSessNameMap_.erase(sessionId);
440     sessionName = handler.GetSessionName(sessionId);
441     EXPECT_EQ(sessionName, "");
442     GTEST_LOG_(INFO) << "SoftbusHandlerTest_GetSessionName_0100 end";
443 }
444 
445 /**
446  * @tc.name: SoftbusHandlerTest_OnSinkSessionOpened_0100
447  * @tc.desc: Verify the OnSinkSessionOpened.
448  * @tc.type: FUNC
449  * @tc.require: I9JXPR
450  */
451 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_OnSinkSessionOpened_0100, TestSize.Level1)
452 {
453     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OnSinkSessionOpened_0100 start";
454     string sessionName1 = "sessionName1";
455     string sessionName2 = "sessionName2";
456     string sessionName3 = "sessionName3";
457     int32_t sessionId1 = 1;
458     int32_t sessionId2 = 2;
459     int32_t sessionId3 = 3;
460 
461     PeerSocketInfo info1 = {
462         .name = const_cast<char*>(sessionName1.c_str()),
463         .networkId = const_cast<char*>(TEST_NETWORKID.c_str()),
464     };
465 
466     PeerSocketInfo info2 = {
467         .name = const_cast<char*>(sessionName2.c_str()),
468         .networkId = const_cast<char*>(TEST_NETWORKID_TWO.c_str()),
469     };
470 
471     PeerSocketInfo info3 = {
472         .name = const_cast<char*>(sessionName3.c_str()),
473         .networkId = const_cast<char*>(TEST_NETWORKID_THREE.c_str()),
474     };
475 
476     SoftBusHandler handler;
477     handler.serverIdMap_.clear();
478     handler.serverIdMap_.insert(std::make_pair(sessionName2, 2));
479 
480     std::vector<DmDeviceInfo> deviceList;
481     DmDeviceInfo deviceInfo1;
482     deviceInfo1.authForm = DmAuthForm::IDENTICAL_ACCOUNT;
483     auto res = strcpy_s(deviceInfo1.networkId, DM_MAX_DEVICE_ID_LEN, TEST_NETWORKID.c_str());
484     if (res != 0) {
485         GTEST_LOG_(INFO) << "strcpy_s failed";
486         return;
487     }
488     deviceList.push_back(deviceInfo1);
489 
490     DmDeviceInfo deviceInfo2;
491     deviceInfo2.authForm = DmAuthForm::SHARE;
492     res = strcpy_s(deviceInfo2.networkId, DM_MAX_DEVICE_ID_LEN, TEST_NETWORKID_TWO.c_str());
493     if (res != 0) {
494         GTEST_LOG_(INFO) << "strcpy_s failed";
495         return;
496     }
497     deviceList.push_back(deviceInfo2);
498 
499     DmDeviceInfo deviceInfo3;
500     deviceInfo3.authForm = DmAuthForm::SHARE;
501     res = strcpy_s(deviceInfo3.networkId, DM_MAX_DEVICE_ID_LEN, TEST_NETWORKID_THREE.c_str());
502     if (res != 0) {
503         GTEST_LOG_(INFO) << "strcpy_s failed";
504         return;
505     }
506     deviceList.push_back(deviceInfo3);
507 
508     EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _))
509         .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0)));
510     handler.OnSinkSessionOpened(sessionId1, info1);
511 
512     EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _))
513         .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0)));
514     handler.OnSinkSessionOpened(sessionId2, info2);
515 
516     EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _))
517         .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0)));
518     handler.OnSinkSessionOpened(sessionId3, info3);
519 
520 #ifdef SUPPORT_SAME_ACCOUNT
521     auto iter = handler.serverIdMap_.find(sessionName2);
522     if (iter == handler.serverIdMap_.end()) {
523         EXPECT_TRUE(true);
524     } else {
525         EXPECT_TRUE(false);
526     }
527 #else
528     EXPECT_EQ(handler.GetSessionName(sessionId1), sessionName1);
529     EXPECT_EQ(handler.GetSessionName(sessionId2), sessionName2);
530     EXPECT_EQ(handler.GetSessionName(sessionId3), sessionName3);
531 #endif
532     handler.clientSessNameMap_.clear();
533     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OnSinkSessionOpened_0100 end";
534 }
535 
536 /**
537  * @tc.name: SoftbusHandlerTest_CloseSession_0100
538  * @tc.desc: Verify the CloseSession.
539  * @tc.type: FUNC
540  * @tc.require: I9JXPR
541  */
542 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_CloseSession_0100, TestSize.Level1)
543 {
544     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CloseSession_0100 start";
545     string sessionName = "sessionName";
546     SoftBusHandler::GetInstance().serverIdMap_.clear();
547     SoftBusHandler::GetInstance().serverIdMap_.insert(std::make_pair(sessionName, 2));
548     SoftBusSessionPool::SessionInfo sessionInfo1{.sessionId = SESSION_ID_ONE,
549                                                  .srcUri = "file://com.demo.a/test/1",
550                                                  .dstPath = "/data/test/1",
551                                                  .uid = UID_ONE};
552     string sessionName1 = "sessionName1";
553     SoftBusSessionPool::GetInstance().AddSessionInfo(sessionName1, sessionInfo1);
554     SoftBusSessionPool::SessionInfo sessionInfo;
555     bool flag = SoftBusSessionPool::GetInstance().GetSessionInfo(sessionName1, sessionInfo);
556     EXPECT_EQ(flag, true);
557     SoftBusHandler::GetInstance().CloseSession(1, "sessionName1");
558     flag = SoftBusSessionPool::GetInstance().GetSessionInfo(sessionName1, sessionInfo);
559     EXPECT_EQ(flag, false);
560     EXPECT_EQ(SoftBusHandler::GetInstance().serverIdMap_.size(), 1);
561 
562     SoftBusSessionPool::GetInstance().AddSessionInfo(sessionName, sessionInfo1);
563     flag = SoftBusSessionPool::GetInstance().GetSessionInfo(sessionName, sessionInfo);
564     EXPECT_EQ(flag, true);
565     SoftBusHandler::GetInstance().CloseSession(1, "");
566     flag = SoftBusSessionPool::GetInstance().GetSessionInfo(sessionName, sessionInfo);
567     EXPECT_EQ(flag, true);
568     EXPECT_EQ(SoftBusHandler::GetInstance().serverIdMap_.size(), 1);
569     SoftBusHandler::GetInstance().CloseSession(1, sessionName);
570     EXPECT_EQ(SoftBusHandler::GetInstance().serverIdMap_.size(), 0);
571     flag = SoftBusSessionPool::GetInstance().GetSessionInfo(sessionName, sessionInfo);
572     EXPECT_EQ(flag, false);
573     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CloseSession_0100 end";
574 }
575 
576 /**
577  * @tc.name: SoftbusHandlerTest_CloseSession_0200
578  * @tc.desc: Verify the CloseSession.
579  * @tc.type: FUNC
580  * @tc.require: I9JXPR
581  */
582 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_CloseSession_0200, TestSize.Level1)
583 {
584     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CloseSession_0200 end";
585     string sessionName = "sessionName";
586     SoftBusHandler::serverIdMap_.insert({"testSession", 0});
587     SoftBusHandler::clientSessNameMap_.insert({0, "test"});
588     SoftBusHandler::GetInstance().CloseSession(1, sessionName); // 1: testSessionId
589     EXPECT_EQ(SoftBusHandler::serverIdMap_.size(), 1); // 1: size
590     EXPECT_EQ(SoftBusHandler::clientSessNameMap_.size(), 1); // 1: size
591     SoftBusHandler::serverIdMap_.clear();
592     SoftBusHandler::clientSessNameMap_.clear();
593     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CloseSession_0200 end";
594 }
595 
596 /**
597  * @tc.name: SoftbusHandlerTest_CloseSessionWithSessionName_0100
598  * @tc.desc: Verify the CloseSession.
599  * @tc.type: FUNC
600  * @tc.require: I9JXPR
601  */
602 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_CloseSessionWithSessionName_0100, TestSize.Level1)
603 {
604     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CloseSessionWithSessionName_0100 start";
605 
606     SoftBusHandler::GetInstance().CloseSessionWithSessionName("");
607 
608     string sessionName = "sessionName";
609     SoftBusHandler::GetInstance().serverIdMap_.insert(std::make_pair(sessionName, 2));
610     SoftBusHandler::GetInstance().CloseSessionWithSessionName(sessionName);
611     auto iter = SoftBusHandler::GetInstance().serverIdMap_.find(sessionName);
612     if (iter == SoftBusHandler::GetInstance().serverIdMap_.end()) {
613         EXPECT_TRUE(false);
614     } else {
615         EXPECT_TRUE(true);
616     }
617     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CloseSessionWithSessionName_0100 end";
618 }
619 
620 /**
621  * @tc.name: SoftbusHandlerTest_CloseSessionWithSessionName_0200
622  * @tc.desc: Verify the CloseSessionWithSessionName.
623  * @tc.type: FUNC
624  * @tc.require: I9JXPR
625  */
626 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_CloseSessionWithSessionName_0200, TestSize.Level1)
627 {
628     string sessionName = "sessionName";
629     SoftBusHandler::GetInstance().clientSessNameMap_.insert(make_pair(2, sessionName));
630     SoftBusHandler::GetInstance().clientSessNameMap_.insert(make_pair(1, "test"));
631     SoftBusHandler::GetInstance().serverIdMap_.insert(std::make_pair(sessionName, 2));
632     SoftBusHandler::GetInstance().CloseSessionWithSessionName(sessionName);
633     auto iter = SoftBusHandler::GetInstance().serverIdMap_.find(sessionName);
634     if (iter == SoftBusHandler::GetInstance().serverIdMap_.end()) {
635         EXPECT_TRUE(true);
636     } else {
637         EXPECT_TRUE(false);
638     }
639 
640     auto iterClient = SoftBusHandler::GetInstance().clientSessNameMap_.find(0);
641     if (iterClient == SoftBusHandler::GetInstance().clientSessNameMap_.end()) {
642         EXPECT_TRUE(true);
643     } else {
644         EXPECT_TRUE(false);
645     }
646 
647     iterClient = SoftBusHandler::GetInstance().clientSessNameMap_.find(1);
648     if (iterClient == SoftBusHandler::GetInstance().clientSessNameMap_.end()) {
649         EXPECT_TRUE(false);
650     } else {
651         EXPECT_TRUE(true);
652     }
653     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CloseSessionWithSessionName_0200 end";
654 }
655 } // namespace Test
656 } // namespace DistributedFile
657 } // namespace Storage
658 } // namespace OHOS
659