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 #include "device/device_manager_agent.h"
16 
17 #include <atomic>
18 #include <exception>
19 #include <gtest/gtest.h>
20 #include <memory>
21 #include <unistd.h>
22 
23 #include "device_manager_impl_mock.h"
24 #include "dfs_error.h"
25 #include "mock_other_method.h"
26 #include "mountpoint/mount_point.h"
27 #include "network/softbus/softbus_agent.h"
28 
29 namespace OHOS {
30 namespace Storage {
31 namespace DistributedFile {
32 namespace Test {
33 using namespace testing;
34 using namespace testing::ext;
35 using namespace std;
36 
37 const int32_t MOUNT_DFS_COUNT_ONE = 1;
38 const int32_t INVALID_USER_ID = -1;
39 
40 DistributedHardware::DmDeviceInfo deviceInfo = {
41     .deviceId = "testdevid",
42     .deviceName = "testdevname",
43     .deviceTypeId = 1,
44     .networkId = "testNetWork",
45 };
46 
47 class DeviceManagerAgentSupTest : public testing::Test {
48 public:
49     static void SetUpTestCase(void);
50     static void TearDownTestCase(void);
51     void SetUp();
52     void TearDown();
53     static inline shared_ptr<DfsDeviceOtherMethodMock> otherMethodMock_ = nullptr;
54     static inline shared_ptr<DeviceManagerImplMock> deviceManagerImplMock_ = nullptr;
55 };
56 
SetUpTestCase(void)57 void DeviceManagerAgentSupTest::SetUpTestCase(void)
58 {
59     // input testsuit setup step,setup invoked before all testcases
60     otherMethodMock_ = make_shared<DfsDeviceOtherMethodMock>();
61     DfsDeviceOtherMethodMock::otherMethod = otherMethodMock_;
62     deviceManagerImplMock_ = make_shared<DeviceManagerImplMock>();
63     DeviceManagerImplMock::dfsDeviceManagerImpl = deviceManagerImplMock_;
64     EXPECT_CALL(*deviceManagerImplMock_, InitDeviceManager(_, _)).WillRepeatedly(Return(0));
65     EXPECT_CALL(*deviceManagerImplMock_, RegisterDevStateCallback(_, _, _)).WillRepeatedly(Return(0));
66     EXPECT_CALL(*deviceManagerImplMock_, GetLocalNodeDeviceInfo(_, _)).WillRepeatedly(Return(0));
67     EXPECT_CALL(*deviceManagerImplMock_, UnRegisterDevStateCallback(_)).WillRepeatedly(Return(0));
68     EXPECT_CALL(*deviceManagerImplMock_, UnInitDeviceManager(_)).WillRepeatedly(Return(0));
69     EXPECT_CALL(*deviceManagerImplMock_, UnInitDeviceManager(_)).WillRepeatedly(Return(0));
70     EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _)).WillRepeatedly(Return(0));
71 }
72 
TearDownTestCase(void)73 void DeviceManagerAgentSupTest::TearDownTestCase(void)
74 {
75     // input testsuit teardown step,teardown invoked after all testcases
76     DfsDeviceOtherMethodMock::otherMethod = nullptr;
77     otherMethodMock_ = nullptr;
78     DeviceManagerImplMock::dfsDeviceManagerImpl = nullptr;
79     deviceManagerImplMock_ = nullptr;
80 }
81 
SetUp(void)82 void DeviceManagerAgentSupTest::SetUp(void)
83 {
84     // input testcase setup step,setup invoked before each testcases
85 }
86 
TearDown(void)87 void DeviceManagerAgentSupTest::TearDown(void)
88 {
89     // input testcase teardown step,teardown invoked after each testcases
90 }
91 
92 /**
93  * @tc.name: DeviceManagerAgentTest_MountDfsCountOnly_0100
94  * @tc.desc: Verify the MountDfsCountOnly function.
95  * @tc.type: FUNC
96  * @tc.require: SR000H0387
97  */
98 HWTEST_F(DeviceManagerAgentSupTest, DeviceManagerAgentTest_MountDfsCountOnly_0100, TestSize.Level1)
99 {
100     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_MountDfsCountOnly_0100 start";
101     auto testPtr = DeviceManagerAgent::GetInstance();
102     bool flag = testPtr->MountDfsCountOnly("");
103     EXPECT_EQ(flag, false);
104     flag = testPtr->MountDfsCountOnly("test");
105     EXPECT_EQ(flag, false);
106     testPtr->mountDfsCount_["test"] = 1;
107     flag = testPtr->MountDfsCountOnly("test");
108     EXPECT_EQ(flag, true);
109     testPtr->mountDfsCount_["test"] = -3;
110     flag = testPtr->MountDfsCountOnly("test");
111     EXPECT_EQ(flag, false);
112     testPtr->mountDfsCount_.erase("test");
113     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_MountDfsCountOnly_0100 end";
114 }
115 
116 /**
117  * @tc.name: DeviceManagerAgentTest_UMountDfsCountOnly_0100
118  * @tc.desc: Verify the UMountDfsCountOnly function.
119  * @tc.type: FUNC
120  * @tc.require: SR000H0387
121  */
122 HWTEST_F(DeviceManagerAgentSupTest, DeviceManagerAgentTest_UMountDfsCountOnly_0100, TestSize.Level1)
123 {
124     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_UMountDfsCountOnly_0100 start";
125     auto testPtr = DeviceManagerAgent::GetInstance();
126     bool flag = testPtr->UMountDfsCountOnly("", true);
127     EXPECT_EQ(flag, false);
128     flag = testPtr->UMountDfsCountOnly("test", true);
129     EXPECT_EQ(flag, true);
130 
131     testPtr->mountDfsCount_["test"] = MOUNT_DFS_COUNT_ONE + 1;
132     flag = testPtr->UMountDfsCountOnly("test", true);
133     EXPECT_EQ(flag, false);
134     EXPECT_EQ(testPtr->mountDfsCount_["test"], 0);
135 
136     testPtr->mountDfsCount_["test"] = MOUNT_DFS_COUNT_ONE + 1;
137     flag = testPtr->UMountDfsCountOnly("test", false);
138     EXPECT_EQ(flag, true);
139     EXPECT_EQ(testPtr->mountDfsCount_["test"], MOUNT_DFS_COUNT_ONE);
140 
141     flag = testPtr->UMountDfsCountOnly("test", false);
142     EXPECT_EQ(flag, false);
143 
144     auto itCount = testPtr->mountDfsCount_.find("test");
145     if (itCount == testPtr->mountDfsCount_.end()) {
146         EXPECT_TRUE(false);
147     } else {
148         EXPECT_TRUE(true);
149     }
150     testPtr->mountDfsCount_.erase("test");
151     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_UMountDfsCountOnly_0100 end";
152 }
153 
154 /**
155  * @tc.name: DeviceManagerAgentTest_GetCurrentUserId_0100
156  * @tc.desc: Verify the GetCurrentUserId function.
157  * @tc.type: FUNC
158  * @tc.require: SR000H0387
159  */
160 HWTEST_F(DeviceManagerAgentSupTest, DeviceManagerAgentTest_GetCurrentUserId_0100, TestSize.Level1)
161 {
162     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_GetCurrentUserId_0100 start";
163     EXPECT_CALL(*otherMethodMock_, QueryActiveOsAccountIds(_)).WillOnce(Return(INVALID_USER_ID));
164     auto testPtr = DeviceManagerAgent::GetInstance();
165     auto userId = testPtr->GetCurrentUserId();
166     EXPECT_EQ(userId, INVALID_USER_ID);
167 
168     EXPECT_CALL(*otherMethodMock_, QueryActiveOsAccountIds(_)).WillOnce(Return(NO_ERROR));
169     userId = testPtr->GetCurrentUserId();
170     EXPECT_EQ(userId, INVALID_USER_ID);
171 
172     std::vector<int32_t> userIds{100, 101};
173     EXPECT_CALL(*otherMethodMock_, QueryActiveOsAccountIds(_))
174         .WillOnce(DoAll(SetArgReferee<0>(userIds), Return(NO_ERROR)));
175     userId = testPtr->GetCurrentUserId();
176     EXPECT_EQ(userId, userIds[0]);
177     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_GetCurrentUserId_0100 end";
178 }
179 
180 /**
181  * @tc.name: DeviceManagerAgentTest_AddNetworkId_0100
182  * @tc.desc: Verify the AddNetworkId function.
183  * @tc.type: FUNC
184  * @tc.require: SR000H0387
185  */
186 HWTEST_F(DeviceManagerAgentSupTest, DeviceManagerAgentTest_AddNetworkId_0100, TestSize.Level1)
187 {
188     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_AddNetworkId_0100 start";
189     unordered_set<string> exceptSet;
190     exceptSet.insert("test1");
191     exceptSet.insert("test2");
192     auto testPtr = DeviceManagerAgent::GetInstance();
193     testPtr->AddNetworkId(100, "test1");
194     testPtr->AddNetworkId(100, "test2");
195     EXPECT_EQ(exceptSet, testPtr->GetNetworkIds(100));
196     exceptSet.clear();
197     testPtr->RemoveNetworkId(100);
198     EXPECT_EQ(exceptSet, testPtr->GetNetworkIds(100));
199     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_AddNetworkId_0100 end";
200 }
201 
202 /**
203  * @tc.name: DeviceManagerAgentTest_RemoveNetworkIdByOne_0100
204  * @tc.desc: Verify the RemoveNetworkIdByOne function.
205  * @tc.type: FUNC
206  * @tc.require: SR000H0387
207  */
208 HWTEST_F(DeviceManagerAgentSupTest, DeviceManagerAgentTest_RemoveNetworkIdByOne_0100, TestSize.Level1)
209 {
210     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_RemoveNetworkIdByOne_0100 start";
211     unordered_set<string> exceptSet;
212     exceptSet.insert("test1");
213     exceptSet.insert("test2");
214     auto testPtr = DeviceManagerAgent::GetInstance();
215     testPtr->AddNetworkId(100, "test1");
216     testPtr->AddNetworkId(100, "test2");
217     EXPECT_EQ(exceptSet, testPtr->GetNetworkIds(100));
218     testPtr->RemoveNetworkIdByOne(100, "test1");
219     exceptSet.erase("test1");
220     EXPECT_EQ(exceptSet, testPtr->GetNetworkIds(100));
221 
222     testPtr->RemoveNetworkIdByOne(100, "test2");
223     exceptSet.erase("test2");
224     auto it = testPtr->networkIdMap_.find(100);
225     if (it != testPtr->networkIdMap_.end()) {
226         EXPECT_TRUE(false);
227     } else {
228         EXPECT_TRUE(true);
229     }
230 
231     testPtr->RemoveNetworkIdByOne(100, "test2");
232     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_RemoveNetworkIdByOne_0100 end";
233 }
234 
235 /**
236  * @tc.name: DeviceManagerAgentTest_RemoveNetworkIdForAllToken_0100
237  * @tc.desc: Verify the RemoveNetworkIdForAllToken function.
238  * @tc.type: FUNC
239  * @tc.require: SR000H0387
240  */
241 HWTEST_F(DeviceManagerAgentSupTest, DeviceManagerAgentTest_RemoveNetworkIdForAllToken_0100, TestSize.Level1)
242 {
243     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_RemoveNetworkIdForAllToken_0100 start";
244     auto testPtr = DeviceManagerAgent::GetInstance();
245     testPtr->AddNetworkId(100, "test1");
246     testPtr->AddNetworkId(100, "test2");
247     testPtr->RemoveNetworkIdForAllToken("");
248     testPtr->RemoveNetworkIdForAllToken("test1");
249     unordered_set<string> exceptSet;
250     exceptSet.insert("test2");
251     EXPECT_EQ(exceptSet, testPtr->GetNetworkIds(100));
252 
253     testPtr->RemoveNetworkIdForAllToken("test2");
254     exceptSet.clear();
255     EXPECT_EQ(exceptSet, testPtr->GetNetworkIds(100));
256     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_RemoveNetworkIdForAllToken_0100 end";
257 }
258 
259 /**
260  * @tc.name: DeviceManagerAgentTest_ClearNetworkId_0100
261  * @tc.desc: Verify the ClearNetworkId function.
262  * @tc.type: FUNC
263  * @tc.require: SR000H0387
264  */
265 HWTEST_F(DeviceManagerAgentSupTest, DeviceManagerAgentTest_ClearNetworkId_0100, TestSize.Level1)
266 {
267     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_ClearNetworkId_0100 start";
268     unordered_set<string> exceptSet;
269     exceptSet.insert("test1");
270     exceptSet.insert("test2");
271     auto testPtr = DeviceManagerAgent::GetInstance();
272     testPtr->AddNetworkId(100, "test1");
273     testPtr->AddNetworkId(100, "test2");
274     EXPECT_EQ(exceptSet, testPtr->GetNetworkIds(100));
275     testPtr->ClearNetworkId();
276     EXPECT_EQ(testPtr->networkIdMap_.size(), 0);
277     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_ClearNetworkId_0100 end";
278 }
279 
280 /**
281  * @tc.name: DeviceManagerAgentTest_MountDfsDocs_0100
282  * @tc.desc: Verify the MountDfsDocs function.
283  * @tc.type: FUNC
284  * @tc.require: SR000H0387
285  */
286 HWTEST_F(DeviceManagerAgentSupTest, DeviceManagerAgentTest_MountDfsDocs_0100, TestSize.Level1)
287 {
288     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_MountDfsDocs_0100 start";
289     auto testPtr = DeviceManagerAgent::GetInstance();
290     constexpr int32_t testCount = 1;
291     testPtr->MountDfsDocs("", "");
292     testPtr->MountDfsDocs("test", "");
293     testPtr->mountDfsCount_["deviceId"] = testCount;
294     testPtr->MountDfsDocs("test", "deviceId");
295     EXPECT_EQ(testPtr->mountDfsCount_["deviceId"], testCount + 1);
296 
297     testPtr->mountDfsCount_["deviceId"] = 0;
298     EXPECT_CALL(*otherMethodMock_, QueryActiveOsAccountIds(_)).WillOnce(Return(INVALID_USER_ID));
299     testPtr->MountDfsDocs("test", "deviceId");
300     EXPECT_EQ(testPtr->mountDfsCount_["deviceId"], 0);
301     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_MountDfsDocs_0100 end";
302 }
303 
304 /**
305  * @tc.name: DeviceManagerAgentTest_UMountDfsDocs_0100
306  * @tc.desc: Verify the UMountDfsDocs function.
307  * @tc.type: FUNC
308  * @tc.require: SR000H0387
309  */
310 HWTEST_F(DeviceManagerAgentSupTest, DeviceManagerAgentTest_UMountDfsDocs_0100, TestSize.Level1)
311 {
312     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_UMountDfsDocs_0100 start";
313     auto testPtr = DeviceManagerAgent::GetInstance();
314     testPtr->UMountDfsDocs("", "", false);
315     testPtr->UMountDfsDocs("test", "", false);
316     testPtr->mountDfsCount_["deviceId"] = MOUNT_DFS_COUNT_ONE + 1;
317     testPtr->UMountDfsDocs("test", "deviceId", false);
318     EXPECT_EQ(testPtr->mountDfsCount_["deviceId"], MOUNT_DFS_COUNT_ONE);
319 
320     EXPECT_CALL(*otherMethodMock_, QueryActiveOsAccountIds(_)).WillOnce(Return(INVALID_USER_ID));
321     testPtr->UMountDfsDocs("test", "deviceId", true);
322     auto it = testPtr->mountDfsCount_.find("deviceId");
323     if (it != testPtr->mountDfsCount_.end()) {
324         EXPECT_TRUE(false);
325     } else {
326         EXPECT_TRUE(true);
327     }
328     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_UMountDfsDocs_0100 end";
329 }
330 
331 /**
332  * @tc.name: DeviceManagerAgentTest_IsSupportedDevice_0100
333  * @tc.desc: Verify the IsSupportedDevice function.
334  * @tc.type: FUNC
335  * @tc.require: IA6UD3
336  */
337 HWTEST_F(DeviceManagerAgentSupTest, DeviceManagerAgentTest_IsSupportedDevice_0100, TestSize.Level1)
338 {
339     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_IsSupportedDevice_0100 start";
340     DmDeviceInfo testInfo;
341     EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _)).WillOnce(Return(0));
342     auto testPtr = DeviceManagerAgent::GetInstance();
343     EXPECT_EQ(testPtr->IsSupportedDevice(testInfo), FileManagement::ERR_BAD_VALUE);
344 
345     std::vector<DmDeviceInfo> deviceList;
346     deviceList.push_back(deviceInfo);
347     EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _))
348         .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0)));
349     EXPECT_EQ(testPtr->IsSupportedDevice(testInfo), FileManagement::ERR_BAD_VALUE);
350     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_IsSupportedDevice_0100 end";
351 }
352 
353 /**
354  * @tc.name: DeviceManagerAgentTest_IsSupportedDevice_0200
355  * @tc.desc: Verify the IsSupportedDevice function.
356  * @tc.type: FUNC
357  * @tc.require: IA6UD3
358  */
359 HWTEST_F(DeviceManagerAgentSupTest, DeviceManagerAgentTest_IsSupportedDevice_0200, TestSize.Level1)
360 {
361     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_IsSupportedDevice_0200 start";
362     DmDeviceInfo testInfo = {
363         .deviceId = "testdevid",
364         .deviceName = "testdevname",
365         .deviceTypeId = 1,
366         .networkId = "testNetWork",
367     };
368 
369     deviceInfo.extraData = "test";
370     std::vector<DmDeviceInfo> deviceList;
371     deviceList.push_back(deviceInfo);
372     EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _))
373         .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0)));
374     auto testPtr = DeviceManagerAgent::GetInstance();
375     EXPECT_EQ(testPtr->IsSupportedDevice(testInfo), FileManagement::ERR_BAD_VALUE);
376 
377     deviceInfo.extraData = R"({"OS_TYPE":"3"})";
378     deviceList.clear();
379     deviceList.push_back(deviceInfo);
380     EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _))
381         .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0)));
382     EXPECT_EQ(testPtr->IsSupportedDevice(testInfo), FileManagement::ERR_BAD_VALUE);
383 
384     deviceInfo.extraData = R"({"OS_TYPE":3})";
385     deviceList.clear();
386     deviceList.push_back(deviceInfo);
387     EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _))
388         .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0)));
389     EXPECT_EQ(testPtr->IsSupportedDevice(testInfo), FileManagement::ERR_BAD_VALUE);
390     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_IsSupportedDevice_0200 end";
391 }
392 
393 /**
394  * @tc.name: DeviceManagerAgentTest_IsSupportedDevice_0300
395  * @tc.desc: Verify the IsSupportedDevice function.
396  * @tc.type: FUNC
397  * @tc.require: IA6UD3
398  */
399 HWTEST_F(DeviceManagerAgentSupTest, DeviceManagerAgentTest_IsSupportedDevice_0300, TestSize.Level1)
400 {
401     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_IsSupportedDevice_0300 start";
402     DmDeviceInfo testInfo = {
403         .deviceId = "testdevid",
404         .deviceName = "testdevname",
405         .deviceTypeId = 1,
406         .networkId = "testNetWork",
407     };
408 
409     std::vector<DmDeviceInfo> deviceList;
410     deviceInfo.extraData = R"({"OS_TYPE":10})";
411     deviceList.push_back(deviceInfo);
412     EXPECT_CALL(*deviceManagerImplMock_, GetTrustedDeviceList(_, _, _))
413         .WillOnce(DoAll(SetArgReferee<2>(deviceList), Return(0)));
414     auto testPtr = DeviceManagerAgent::GetInstance();
415     EXPECT_EQ(testPtr->IsSupportedDevice(testInfo), FileManagement::ERR_OK);
416     GTEST_LOG_(INFO) << "DeviceManagerAgentTest_IsSupportedDevice_0300 end";
417 }
418 } // namespace Test
419 } // namespace DistributedFile
420 } // namespace Storage
421 } // namespace OHOS
422