1 /*
2  * Copyright (c) 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 #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.h"
24 #include "dm_constants.h"
25 
26 #include "dfs_error.h"
27 #include "network/softbus/softbus_file_receive_listener.h"
28 #include "network/softbus/softbus_session_pool.h"
29 #include "socket_mock.h"
30 #include "utils_directory.h"
31 #include "utils_log.h"
32 
33 using namespace OHOS::DistributedHardware;
34 using namespace OHOS::FileManagement;
35 using namespace std;
36 
37 namespace {
38 bool g_mockGetTrustedDeviceList = true;
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 DistributedHardware {
GetTrustedDeviceList(const std::string & pkgName,const std::string & extra,std::vector<DmDeviceInfo> & deviceList)48 int32_t DeviceManagerImpl::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra,
49                                                 std::vector<DmDeviceInfo> &deviceList)
50 {
51     if (!g_mockGetTrustedDeviceList) {
52         return ERR_DM_INPUT_PARA_INVALID;
53     }
54 
55     DmDeviceInfo testInfo;
56     (void)memcpy_s(testInfo.networkId, DM_MAX_DEVICE_NAME_LEN - 1,
57                    TEST_NETWORKID.c_str(), TEST_NETWORKID.size());
58     testInfo.authForm = DmAuthForm::IDENTICAL_ACCOUNT;
59     deviceList.push_back(testInfo);
60 
61     DmDeviceInfo testInfo1;
62     (void)memcpy_s(testInfo1.networkId, DM_MAX_DEVICE_NAME_LEN - 1,
63                    TEST_NETWORKID_TWO.c_str(), TEST_NETWORKID_TWO.size());
64     testInfo1.authForm = DmAuthForm::PEER_TO_PEER;
65     deviceList.push_back(testInfo1);
66     return DM_OK;
67 }
68 }
69 }
70 
71 namespace OHOS {
72 namespace Storage {
73 namespace DistributedFile {
74 namespace Test {
75 using namespace testing;
76 using namespace testing::ext;
77 class SoftbusHandlerTest : public testing::Test {
78 public:
79     static void SetUpTestCase(void);
80     static void TearDownTestCase(void);
81     void SetUp();
82     void TearDown();
83     static inline shared_ptr<SocketMock> socketMock_ = nullptr;
84 };
85 
SetUpTestCase(void)86 void SoftbusHandlerTest::SetUpTestCase(void)
87 {
88     GTEST_LOG_(INFO) << "SetUpTestCase";
89     socketMock_ = make_shared<SocketMock>();
90     SocketMock::dfsSocket = socketMock_;
91 }
92 
TearDownTestCase(void)93 void SoftbusHandlerTest::TearDownTestCase(void)
94 {
95     GTEST_LOG_(INFO) << "TearDownTestCase";
96     SocketMock::dfsSocket = nullptr;
97     socketMock_ = nullptr;
98 }
99 
SetUp(void)100 void SoftbusHandlerTest::SetUp(void)
101 {
102     GTEST_LOG_(INFO) << "SetUp";
103 }
104 
TearDown(void)105 void SoftbusHandlerTest::TearDown(void)
106 {
107     GTEST_LOG_(INFO) << "TearDown";
108 }
109 
110 /**
111  * @tc.name: SoftbusHandlerTest_CreateSessionServer_0100
112  * @tc.desc: Verify the OccupySession by Cid function.
113  * @tc.type: FUNC
114  * @tc.require: I9JXPR
115  */
116 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_CreateSessionServer_0100, TestSize.Level1)
117 {
118     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CreateSessionServer_0100 start";
119     SoftBusHandler handler;
120     std::string packageName = "com.example.test";
121     std::string sessionName = "testSession";
122     DFS_CHANNEL_ROLE role = DFS_CHANNLE_ROLE_SOURCE;
123     std::string physicalPath = "/data/test";
124     EXPECT_CALL(*socketMock_, Socket(_)).WillOnce(Return(-1));
125     int32_t result = handler.CreateSessionServer(packageName, sessionName, role, physicalPath);
126     handler.serverIdMap_.erase(sessionName);
127     EXPECT_EQ(result, ERR_BAD_VALUE);
128 
129     EXPECT_CALL(*socketMock_, Socket(_)).WillOnce(Return(0));
130     EXPECT_CALL(*socketMock_, Listen(_, _, _, _)).WillOnce(Return(-1));
131     result = handler.CreateSessionServer(packageName, sessionName, role, physicalPath);
132     handler.serverIdMap_.erase(sessionName);
133     EXPECT_EQ(result, ERR_BAD_VALUE);
134 
135     EXPECT_CALL(*socketMock_, Socket(_)).WillOnce(Return(0));
136     EXPECT_CALL(*socketMock_, Listen(_, _, _, _)).WillOnce(Return(0));
137     result = handler.CreateSessionServer(packageName, sessionName, role, physicalPath);
138     EXPECT_EQ(result, E_OK);
139     if (handler.serverIdMap_.find(sessionName) != handler.serverIdMap_.end()) {
140         EXPECT_TRUE(true);
141     } else {
142         EXPECT_TRUE(false);
143     }
144     handler.serverIdMap_.erase(sessionName);
145     EXPECT_EQ(string(SoftBusFileReceiveListener::GetRecvPath()), physicalPath);
146     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CreateSessionServer_0100 end";
147 }
148 
149 /**
150  * @tc.name: SoftbusHandlerTest_CreateSessionServer_0200
151  * @tc.desc: Verify the OccupySession by Cid function.
152  * @tc.type: FUNC
153  * @tc.require: I9JXPR
154  */
155 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_CreateSessionServer_0200, TestSize.Level1)
156 {
157     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CreateSessionServer_0200 start";
158     SoftBusHandler handler;
159     std::string packageName = "com.example.test";
160     std::string sessionName = "testSession";
161     DFS_CHANNEL_ROLE role = DFS_CHANNLE_ROLE_SOURCE;
162     std::string physicalPath = "/data/test";
163 
164     auto result = handler.CreateSessionServer("", sessionName, role, physicalPath);
165     EXPECT_EQ(result, ERR_BAD_VALUE);
166 
167     result = handler.CreateSessionServer(packageName, "", role, physicalPath);
168     EXPECT_EQ(result, ERR_BAD_VALUE);
169 
170     result = handler.CreateSessionServer(packageName, sessionName, role, "");
171     EXPECT_EQ(result, ERR_BAD_VALUE);
172     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CreateSessionServer_0200 end";
173 }
174 
175 /**
176  * @tc.name: SoftbusHandlerTest_OpenSession_0100
177  * @tc.desc: Verify the OccupySession by Cid function.
178  * @tc.type: FUNC
179  * @tc.require: I9JXPR
180  */
181 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_OpenSession_0100, TestSize.Level1)
182 {
183     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OpenSession_0100 start";
184     SoftBusHandler handler;
185     std::string packageName = "com.example.test";
186     std::string sessionName = "testSession";
187     int32_t socketId;
188 
189     g_mockGetTrustedDeviceList = true;
190     EXPECT_CALL(*socketMock_, Socket(_)).WillOnce(Return(-1));
191     int32_t result = handler.OpenSession(packageName, sessionName, TEST_NETWORKID, socketId);
192     EXPECT_EQ(result, ERR_BAD_VALUE);
193 
194     EXPECT_CALL(*socketMock_, Socket(_)).WillOnce(Return(1));
195     EXPECT_CALL(*socketMock_, Bind(_, _, _, _)).WillOnce(Return(-1));
196     result = handler.OpenSession(packageName, sessionName, TEST_NETWORKID, socketId);
197     EXPECT_EQ(result, ERR_BAD_VALUE);
198 
199     EXPECT_CALL(*socketMock_, Socket(_)).WillOnce(Return(1));
200     EXPECT_CALL(*socketMock_, Bind(_, _, _, _)).WillOnce(Return(0));
201     result = handler.OpenSession(packageName, sessionName, TEST_NETWORKID, socketId);
202     EXPECT_EQ(result, 0);
203 
204     if (handler.clientSessNameMap_.find(socketId) != handler.clientSessNameMap_.end()) {
205         EXPECT_TRUE(true);
206     } else {
207         EXPECT_TRUE(false);
208     }
209 
210     handler.clientSessNameMap_.erase(socketId);
211     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OpenSession_0100 end";
212 }
213 
214 /**
215  * @tc.name: SoftbusHandlerTest_OpenSession_0200
216  * @tc.desc: Verify the OpenSession by Cid function.
217  * @tc.type: FUNC
218  * @tc.require: I9JXPR
219  */
220 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_OpenSession_0200, TestSize.Level1)
221 {
222     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OpenSession_0100 start";
223     SoftBusHandler handler;
224     std::string packageName = "com.example.test";
225     std::string sessionName = "testSession";
226     int32_t socketId;
227     std::string physicalPath = "/data/test";
228 
229     int32_t result = handler.OpenSession("", sessionName, physicalPath, socketId);
230     EXPECT_EQ(result, ERR_BAD_VALUE);
231 
232     result = handler.OpenSession(packageName, "", physicalPath, socketId);
233     EXPECT_EQ(result, ERR_BAD_VALUE);
234 
235     result = handler.OpenSession(packageName, sessionName, "", socketId);
236     EXPECT_EQ(result, ERR_BAD_VALUE);
237     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OpenSession_0100 end";
238 }
239 
240 /**
241  * @tc.name: SoftbusHandlerTest_ChangeOwnerIfNeeded_0100
242  * @tc.desc: Verify the ChangeOwnerIfNeeded by Cid function.
243  * @tc.type: FUNC
244  * @tc.require: I9JXPR
245  */
246 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_ChangeOwnerIfNeeded_0100, TestSize.Level1)
247 {
248     GTEST_LOG_(INFO) << "SoftbusHandlerTest_ChangeOwnerIfNeeded_0100 start";
249     bool res = true;
250     try {
251         SoftBusHandler handler;
252         handler.ChangeOwnerIfNeeded(1, "");
253         handler.ChangeOwnerIfNeeded(1, "sessionName");
254 
255         SoftBusSessionPool::SessionInfo sessionInfo1{.sessionId = SESSION_ID_ONE,
256                                                     .srcUri = "file://com.demo.a/test/1",
257                                                     .dstPath = "/data/test/1",
258                                                     .uid = UID_ONE};
259         string sessionName1 = "sessionName1";
260         SoftBusSessionPool::GetInstance().AddSessionInfo(sessionName1, sessionInfo1);
261         handler.ChangeOwnerIfNeeded(1, sessionName1);
262         SoftBusSessionPool::GetInstance().DeleteSessionInfo(sessionName1);
263     } catch(...) {
264         res = false;
265     }
266 
267     EXPECT_TRUE(res);
268     GTEST_LOG_(INFO) << "SoftbusHandlerTest_ChangeOwnerIfNeeded_0100 end";
269 }
270 
271 /**
272  * @tc.name: SoftbusHandlerTest_GetSessionName_0100
273  * @tc.desc: Verify the GetSessionName by Cid function.
274  * @tc.type: FUNC
275  * @tc.require: I9JXPR
276  */
277 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_GetSessionName_0100, TestSize.Level1)
278 {
279     GTEST_LOG_(INFO) << "SoftbusHandlerTest_GetSessionName_0100 start";
280     string testSessionName = "mySessionName";
281     int32_t sessionId = 1;
282     SoftBusHandler::clientSessNameMap_.insert(std::make_pair(sessionId, testSessionName));
283     SoftBusHandler handler;
284     string sessionName = handler.GetSessionName(sessionId);
285     EXPECT_EQ(sessionName, testSessionName);
286     SoftBusHandler::clientSessNameMap_.erase(sessionId);
287     sessionName = handler.GetSessionName(sessionId);
288     EXPECT_EQ(sessionName, "");
289     GTEST_LOG_(INFO) << "SoftbusHandlerTest_GetSessionName_0100 end";
290 }
291 
292 /**
293  * @tc.name: SoftbusHandlerTest_IsSameAccount_0100
294  * @tc.desc: Verify the IsSameAccount.
295  * @tc.type: FUNC
296  * @tc.require: I9JXPR
297  */
298 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_IsSameAccount_0100, TestSize.Level1)
299 {
300     GTEST_LOG_(INFO) << "SoftbusHandlerTest_IsSameAccount_0100 start";
301     g_mockGetTrustedDeviceList = true;
302     SoftBusHandler handler;
303     bool flag = handler.IsSameAccount(TEST_NETWORKID);
304     EXPECT_EQ(flag, true);
305 #ifdef SUPPORT_SAME_ACCOUNT
306     flag = handler.IsSameAccount(TEST_NETWORKID_TWO);
307     EXPECT_EQ(flag, false);
308     flag = handler.IsSameAccount(TEST_NETWORKID_THREE);
309     EXPECT_EQ(flag, false);
310     g_mockGetTrustedDeviceList = false;
311     flag = handler.IsSameAccount(TEST_NETWORKID);
312     EXPECT_EQ(flag, false);
313 #endif
314     g_mockGetTrustedDeviceList = true;
315     GTEST_LOG_(INFO) << "SoftbusHandlerTest_IsSameAccount_0100 end";
316 }
317 
318 /**
319  * @tc.name: SoftbusHandlerTest_OnSinkSessionOpened_0100
320  * @tc.desc: Verify the OnSinkSessionOpened.
321  * @tc.type: FUNC
322  * @tc.require: I9JXPR
323  */
324 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_OnSinkSessionOpened_0100, TestSize.Level1)
325 {
326     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OnSinkSessionOpened_0100 start";
327     g_mockGetTrustedDeviceList = true;
328     string sessionName1 = "sessionName1";
329     string sessionName2 = "sessionName2";
330     string sessionName3 = "sessionName3";
331     int32_t sessionId1 = 1;
332     int32_t sessionId2 = 2;
333     int32_t sessionId3 = 3;
334 
335     PeerSocketInfo info1 = {
336         .name = const_cast<char*>(sessionName1.c_str()),
337         .networkId = const_cast<char*>(TEST_NETWORKID.c_str()),
338     };
339 
340     PeerSocketInfo info2 = {
341         .name = const_cast<char*>(sessionName2.c_str()),
342         .networkId = const_cast<char*>(TEST_NETWORKID_TWO.c_str()),
343     };
344 
345     PeerSocketInfo info3 = {
346         .name = const_cast<char*>(sessionName3.c_str()),
347         .networkId = const_cast<char*>(TEST_NETWORKID_THREE.c_str()),
348     };
349 
350     SoftBusHandler handler;
351     handler.serverIdMap_.clear();
352     handler.serverIdMap_.insert(std::make_pair(sessionName2, 2));
353     handler.OnSinkSessionOpened(sessionId1, info1);
354     handler.OnSinkSessionOpened(sessionId2, info2);
355     handler.OnSinkSessionOpened(sessionId3, info3);
356 
357 #ifdef SUPPORT_SAME_ACCOUNT
358     auto iter = handler.serverIdMap_.find(sessionName2);
359     if (iter == handler.serverIdMap_.end()) {
360         EXPECT_TRUE(true);
361     } else {
362         EXPECT_TRUE(false);
363     }
364 #else
365     EXPECT_EQ(handler.GetSessionName(sessionId1), sessionName1);
366     EXPECT_EQ(handler.GetSessionName(sessionId2), sessionName2);
367     EXPECT_EQ(handler.GetSessionName(sessionId3), sessionName3);
368 #endif
369     handler.clientSessNameMap_.erase(sessionId1);
370     handler.clientSessNameMap_.erase(sessionId2);
371     handler.clientSessNameMap_.erase(sessionId3);
372     GTEST_LOG_(INFO) << "SoftbusHandlerTest_OnSinkSessionOpened_0100 end";
373 }
374 
375 /**
376  * @tc.name: SoftbusHandlerTest_CloseSession_0100
377  * @tc.desc: Verify the CloseSession.
378  * @tc.type: FUNC
379  * @tc.require: I9JXPR
380  */
381 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_CloseSession_0100, TestSize.Level1)
382 {
383     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CloseSession_0100 start";
384     string sessionName = "sessionName";
385     SoftBusHandler::GetInstance().serverIdMap_.clear();
386     SoftBusHandler::GetInstance().serverIdMap_.insert(std::make_pair(sessionName, 2));
387     SoftBusSessionPool::SessionInfo sessionInfo1{.sessionId = SESSION_ID_ONE,
388                                                  .srcUri = "file://com.demo.a/test/1",
389                                                  .dstPath = "/data/test/1",
390                                                  .uid = UID_ONE};
391     string sessionName1 = "sessionName1";
392     SoftBusSessionPool::GetInstance().AddSessionInfo(sessionName1, sessionInfo1);
393     SoftBusSessionPool::SessionInfo sessionInfo;
394     bool flag = SoftBusSessionPool::GetInstance().GetSessionInfo(sessionName1, sessionInfo);
395     EXPECT_EQ(flag, true);
396     SoftBusHandler::GetInstance().CloseSession(1, "sessionName1");
397     flag = SoftBusSessionPool::GetInstance().GetSessionInfo(sessionName1, sessionInfo);
398     EXPECT_EQ(flag, false);
399     EXPECT_EQ(SoftBusHandler::GetInstance().serverIdMap_.size(), 1);
400 
401     SoftBusSessionPool::GetInstance().AddSessionInfo(sessionName, sessionInfo1);
402     flag = SoftBusSessionPool::GetInstance().GetSessionInfo(sessionName, sessionInfo);
403     EXPECT_EQ(flag, true);
404     SoftBusHandler::GetInstance().CloseSession(1, "");
405     flag = SoftBusSessionPool::GetInstance().GetSessionInfo(sessionName, sessionInfo);
406     EXPECT_EQ(flag, true);
407     EXPECT_EQ(SoftBusHandler::GetInstance().serverIdMap_.size(), 1);
408     SoftBusHandler::GetInstance().CloseSession(1, sessionName);
409     EXPECT_EQ(SoftBusHandler::GetInstance().serverIdMap_.size(), 0);
410     flag = SoftBusSessionPool::GetInstance().GetSessionInfo(sessionName, sessionInfo);
411     EXPECT_EQ(flag, false);
412     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CloseSession_0100 end";
413 }
414 
415 /**
416  * @tc.name: SoftbusHandlerTest_CloseSessionWithSessionName_0100
417  * @tc.desc: Verify the CloseSession.
418  * @tc.type: FUNC
419  * @tc.require: I9JXPR
420  */
421 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_CloseSessionWithSessionName_0100, TestSize.Level1)
422 {
423     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CloseSessionWithSessionName_0100 start";
424 
425     SoftBusHandler::GetInstance().CloseSessionWithSessionName("");
426 
427     string sessionName = "sessionName";
428     SoftBusHandler::GetInstance().serverIdMap_.insert(std::make_pair(sessionName, 2));
429     SoftBusHandler::GetInstance().CloseSessionWithSessionName(sessionName);
430     auto iter = SoftBusHandler::GetInstance().serverIdMap_.find(sessionName);
431     if (iter == SoftBusHandler::GetInstance().serverIdMap_.end()) {
432         EXPECT_TRUE(false);
433     } else {
434         EXPECT_TRUE(true);
435     }
436     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CloseSessionWithSessionName_0100 end";
437 }
438 
439 /**
440  * @tc.name: SoftbusHandlerTest_CloseSessionWithSessionName_0200
441  * @tc.desc: Verify the CloseSessionWithSessionName.
442  * @tc.type: FUNC
443  * @tc.require: I9JXPR
444  */
445 HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_CloseSessionWithSessionName_0200, TestSize.Level1)
446 {
447     string sessionName = "sessionName";
448     SoftBusHandler::GetInstance().clientSessNameMap_.insert(make_pair(2, sessionName));
449     SoftBusHandler::GetInstance().clientSessNameMap_.insert(make_pair(1, "test"));
450     SoftBusHandler::GetInstance().serverIdMap_.insert(std::make_pair(sessionName, 2));
451     SoftBusHandler::GetInstance().CloseSessionWithSessionName(sessionName);
452     auto iter = SoftBusHandler::GetInstance().serverIdMap_.find(sessionName);
453     if (iter == SoftBusHandler::GetInstance().serverIdMap_.end()) {
454         EXPECT_TRUE(true);
455     } else {
456         EXPECT_TRUE(false);
457     }
458 
459     auto iterClient = SoftBusHandler::GetInstance().clientSessNameMap_.find(0);
460     if (iterClient == SoftBusHandler::GetInstance().clientSessNameMap_.end()) {
461         EXPECT_TRUE(true);
462     } else {
463         EXPECT_TRUE(false);
464     }
465 
466     iterClient = SoftBusHandler::GetInstance().clientSessNameMap_.find(1);
467     if (iterClient == SoftBusHandler::GetInstance().clientSessNameMap_.end()) {
468         EXPECT_TRUE(false);
469     } else {
470         EXPECT_TRUE(true);
471     }
472     GTEST_LOG_(INFO) << "SoftbusHandlerTest_CloseSessionWithSessionName_0200 end";
473 }
474 } // namespace Test
475 } // namespace DistributedFile
476 } // namespace Storage
477 } // namespace OHOS
478