• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "connection_detector.h"
16 
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 
22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
24 
25 #include <file_ex.h>
26 
27 namespace {
28 bool g_mockQueryActiveOsAccountIds = true;
29 bool g_mockQueryActiveOsAccountIdsEmpty = true;
30 constexpr int32_t TEST_USERID = 100;
31 constexpr int32_t INVALID_USER_ID = -1;
32 }
33 namespace OHOS {
34 namespace AccountSA {
QueryActiveOsAccountIds(std::vector<int32_t> & ids)35 ErrCode OsAccountManager::QueryActiveOsAccountIds(std::vector<int32_t>& ids)
36 {
37     if (g_mockQueryActiveOsAccountIds) {
38         if (!g_mockQueryActiveOsAccountIdsEmpty) {
39             ids.push_back(TEST_USERID);
40         }
41 
42         return NO_ERROR;
43     }
44 
45     return INVALID_USER_ID;
46 }
47 }
48 }
49 
50 namespace OHOS::Storage::DistributedFile::Test {
51 using namespace testing;
52 using namespace testing::ext;
53 using namespace std;
54 
55 constexpr int32_t E_OK = 0;
56 constexpr int32_t E_ERR = -1;
57 
58 class ConnectionDetectorTest : public testing::Test {
59 public:
60     static void SetUpTestCase(void);
61     static void TearDownTestCase(void);
62     void SetUp();
63     void TearDown();
64 };
65 
SetUpTestCase(void)66 void ConnectionDetectorTest::SetUpTestCase(void)
67 {
68     GTEST_LOG_(INFO) << "SetUpTestCase";
69 }
70 
TearDownTestCase(void)71 void ConnectionDetectorTest::TearDownTestCase(void)
72 {
73     GTEST_LOG_(INFO) << "TearDownTestCase";
74 }
75 
SetUp(void)76 void ConnectionDetectorTest::SetUp(void)
77 {
78     GTEST_LOG_(INFO) << "SetUp";
79 }
80 
TearDown(void)81 void ConnectionDetectorTest::TearDown(void)
82 {
83     GTEST_LOG_(INFO) << "TearDown";
84 }
85 
86 HWTEST_F(ConnectionDetectorTest, DfsService_GetConnectionStatus_001, TestSize.Level1)
87 {
88     GTEST_LOG_(INFO) << "DfsService_GetConnectionStatus_001_Start";
89     string targetDir = "/test/file";
90     string networkId = "test$file@txt";
91     bool ret = ConnectionDetector::GetConnectionStatus(targetDir, networkId);
92     EXPECT_EQ(ret, false);
93     GTEST_LOG_(INFO) << "DfsService_GetConnectionStatus_001_End";
94 }
95 
96 HWTEST_F(ConnectionDetectorTest, DfsService_GetConnectionStatus_002, TestSize.Level1)
97 {
98     GTEST_LOG_(INFO) << "DfsService_GetConnectionStatus_002_Start";
99     string targetDir;
100     string networkId;
101     bool ret = ConnectionDetector::GetConnectionStatus(targetDir, networkId);
102     EXPECT_EQ(ret, false);
103     GTEST_LOG_(INFO) << "DfsService_GetConnectionStatus_002_End";
104 }
105 
106 HWTEST_F(ConnectionDetectorTest, DfsService_MocklispHash_001, TestSize.Level1)
107 {
108     GTEST_LOG_(INFO) << "DfsService_MocklispHash_001_Start";
109     string str;
110     uint64_t ret = ConnectionDetector::MocklispHash(str);
111     EXPECT_NE(ret, E_OK);
112     GTEST_LOG_(INFO) << "DfsService_MocklispHash_001_End";
113 }
114 
115 HWTEST_F(ConnectionDetectorTest, DfsService_ParseHmdfsPath_001, TestSize.Level1)
116 {
117     GTEST_LOG_(INFO) << "DfsService_ParseHmdfsPath_001_Start";
118     g_mockQueryActiveOsAccountIds = false;
119     string realPath = ConnectionDetector::ParseHmdfsPath();
120     EXPECT_EQ(realPath, "");
121 
122     g_mockQueryActiveOsAccountIds = true;
123     g_mockQueryActiveOsAccountIdsEmpty = true;
124     realPath = ConnectionDetector::ParseHmdfsPath();
125     EXPECT_EQ(realPath, "");
126 
127     g_mockQueryActiveOsAccountIdsEmpty = false;
128     realPath = ConnectionDetector::ParseHmdfsPath();
129     EXPECT_EQ(realPath, "/mnt/hmdfs/100/account");
130     GTEST_LOG_(INFO) << "DfsService_ParseHmdfsPath_001_End";
131 }
132 
133 HWTEST_F(ConnectionDetectorTest, DfsService_RepeatGetConnectionStatus_001, TestSize.Level1)
134 {
135     GTEST_LOG_(INFO) << "DfsService_RepeatGetConnectionStatus_001_Start";
136     string targetDir;
137     string networkId;
138     int32_t ret = ConnectionDetector::RepeatGetConnectionStatus(targetDir, networkId);
139     EXPECT_EQ(ret, E_ERR);
140     GTEST_LOG_(INFO) << "DfsService_RepeatGetConnectionStatus_001_End";
141 }
142 
143 HWTEST_F(ConnectionDetectorTest, DfsService_RepeatGetConnectionStatus_002, TestSize.Level1)
144 {
145     GTEST_LOG_(INFO) << "DfsService_RepeatGetConnectionStatus_002_Start";
146     string targetDir = "testDir";
147     string networkId = "test@4#";
148     int32_t ret = ConnectionDetector::RepeatGetConnectionStatus(targetDir, networkId);
149     EXPECT_EQ(ret, E_ERR);
150     GTEST_LOG_(INFO) << "DfsService_RepeatGetConnectionStatus_002_End";
151 }
152 
153 HWTEST_F(ConnectionDetectorTest, DfsService_GetCellByIndex_001, TestSize.Level1)
154 {
155     GTEST_LOG_(INFO) << "DfsService_GetCellByIndex_001_Start";
156     string str;
157     int targetIndex = 0;
158     ConnectionDetector connectionDetector;
159     string ret = connectionDetector.GetCellByIndex(str, targetIndex);
160     EXPECT_EQ(ret, "");
161     GTEST_LOG_(INFO) << "DfsService_GetCellByIndex_001_End";
162 }
163 
164 HWTEST_F(ConnectionDetectorTest, DfsService_GetCellByIndex_002, TestSize.Level1)
165 {
166     GTEST_LOG_(INFO) << "DfsService_GetCellByIndex_002_Start";
167     string str = "test  %/ lld";
168     int targetIndex = 0;
169     ConnectionDetector connectionDetector;
170     string ret = connectionDetector.GetCellByIndex(str, targetIndex);
171     EXPECT_EQ(ret, "lld");
172     GTEST_LOG_(INFO) << "DfsService_GetCellByIndex_002_End";
173 }
174 
175 HWTEST_F(ConnectionDetectorTest, DfsService_GetCellByIndex_003, TestSize.Level1)
176 {
177     GTEST_LOG_(INFO) << "DfsService_GetCellByIndex_003_Start";
178     string str = "test  %/ lld test %//d    txt jpg";
179     int targetIndex = 0;
180     ConnectionDetector connectionDetector;
181     string ret = connectionDetector.GetCellByIndex(str, targetIndex);
182     EXPECT_EQ(ret, "jpg");
183     GTEST_LOG_(INFO) << "DfsService_GetCellByIndex_003_End";
184 }
185 
186 HWTEST_F(ConnectionDetectorTest, DfsService_GetCellByIndex_004, TestSize.Level1)
187 {
188     GTEST_LOG_(INFO) << "DfsService_GetCellByIndex_004_Start";
189     string str = "       test";
190     int targetIndex = 0;
191     ConnectionDetector connectionDetector;
192     string ret = connectionDetector.GetCellByIndex(str, targetIndex);
193     EXPECT_EQ(ret, "test");
194     GTEST_LOG_(INFO) << "DfsService_GetCellByIndex_004_End";
195 }
196 
197 HWTEST_F(ConnectionDetectorTest, DfsService_MatchConnectionStatus_001, TestSize.Level1)
198 {
199     GTEST_LOG_(INFO) << "DfsService_MatchConnectionStatus_001_Start";
200     ifstream inputFile;
201     ConnectionDetector connectionDetector;
202     bool ret = connectionDetector.MatchConnectionStatus(inputFile);
203     EXPECT_EQ(ret, false);
204     GTEST_LOG_(INFO) << "DfsService_MatchConnectionStatus_001_End";
205 }
206 
207 HWTEST_F(ConnectionDetectorTest, DfsService_MatchConnectionStatus_002, TestSize.Level1)
208 {
209     GTEST_LOG_(INFO) << "DfsService_MatchConnectionStatus_002_Start";
210     ifstream inputFile("testName");
211     ConnectionDetector connectionDetector;
212     bool ret = connectionDetector.MatchConnectionStatus(inputFile);
213     EXPECT_EQ(ret, false);
214     GTEST_LOG_(INFO) << "DfsService_MatchConnectionStatus_002_End";
215 }
216 
217 HWTEST_F(ConnectionDetectorTest, DfsService_MatchConnectionGroup_001, TestSize.Level1)
218 {
219     GTEST_LOG_(INFO) << "DfsService_MatchConnectionGroup_001_Start";
220     string fileName;
221     string networkId;
222     ConnectionDetector connectionDetector;
223     bool ret = connectionDetector.MatchConnectionGroup(fileName, networkId);
224     EXPECT_EQ(ret, false);
225     GTEST_LOG_(INFO) << "DfsService_MatchConnectionGroup_001_End";
226 }
227 
228 HWTEST_F(ConnectionDetectorTest, DfsService_MatchConnectionGroup_002, TestSize.Level1)
229 {
230     GTEST_LOG_(INFO) << "DfsService_MatchConnectionGroup_002_Start";
231     string fileName = "testName";
232     string networkId = "test@txt#1";
233     ConnectionDetector connectionDetector;
234     bool ret = connectionDetector.MatchConnectionGroup(fileName, networkId);
235     EXPECT_EQ(ret, false);
236     GTEST_LOG_(INFO) << "DfsService_MatchConnectionGroup_002_End";
237 }
238 
239 /**
240  * @tc.name: DfsService_MatchConnectionGroup_003
241  * @tc.desc: Verify the MatchConnectionGroup func.
242  * @tc.type: FUNC
243  * @tc.require: I9KNY6
244  */
245 HWTEST_F(ConnectionDetectorTest, DfsService_MatchConnectionGroup_003, TestSize.Level1)
246 {
247     GTEST_LOG_(INFO) << "DfsService_MatchConnectionGroup_003 start";
248     // file not exist
249     string testPath = "/data/test/1.txt";
250     string networkId = "abc";
251     bool flag = ConnectionDetector::MatchConnectionGroup(testPath, networkId);
252     EXPECT_EQ(flag, false);
253 
254     int fd = open(testPath.c_str(), O_RDWR | O_CREAT);
255     ASSERT_TRUE(fd != -1) << "Failed to open file in DfsService_MatchConnectionGroup_003! " << errno;
256     close(fd);
257 
258     // file exist but content is null
259     flag = ConnectionDetector::MatchConnectionGroup(testPath, networkId);
260     EXPECT_EQ(flag, false);
261 
262     int ret = unlink(testPath.c_str());
263     ASSERT_TRUE(ret != -1) <<
264         "Failed to delete file in DfsService_MatchConnectionGroup_003! " << errno;
265 
266     GTEST_LOG_(INFO) << "DfsService_MatchConnectionGroup_003 end";
267 }
268 
269 /**
270  * @tc.name: DfsService_MatchConnectionGroup_004
271  * @tc.desc: Verify the MatchConnectionGroup func.
272  * @tc.type: FUNC
273  * @tc.require: I9KNY6
274  */
275 HWTEST_F(ConnectionDetectorTest, DfsService_MatchConnectionGroup_004, TestSize.Level1)
276 {
277     GTEST_LOG_(INFO) << "DfsService_MatchConnectionGroup_004 start";
278     string testPath = "/data/test/1.txt";
279     string networkId = "abc";
280     int fd = open(testPath.c_str(), O_RDWR | O_CREAT);
281     ASSERT_TRUE(fd != -1) << "Failed to open file in DfsService_MatchConnectionGroup_004! " << errno;
282     close(fd);
283 
284     string content = "\ntest\nconnection_status\n1 2 3";
285     bool contentCreate = SaveStringToFile(testPath, content, false);
286     ASSERT_TRUE(contentCreate) <<
287         "Failed to SaveStringToFile 1 in DfsService_MatchConnectionGroup_004! " << errno;
288     bool flag = ConnectionDetector::MatchConnectionGroup(testPath, networkId);
289     EXPECT_EQ(flag, false);
290 
291     content.clear();
292     content = "\nabc\ntest\n1 2 3";
293     contentCreate = SaveStringToFile(testPath, content, false);
294     ASSERT_TRUE(contentCreate) <<
295         "Failed to SaveStringToFile 2 in DfsService_MatchConnectionGroup_004! " << errno;
296     flag = ConnectionDetector::MatchConnectionGroup(testPath, networkId);
297     EXPECT_EQ(flag, false);
298 
299     int ret = unlink(testPath.c_str());
300     ASSERT_TRUE(ret != -1) <<
301         "Failed to delete file in DfsService_MatchConnectionGroup_004! " << errno;
302     GTEST_LOG_(INFO) << "DfsService_MatchConnectionGroup_004 end";
303 }
304 
305 /**
306  * @tc.name: DfsService_MatchConnectionGroup_005
307  * @tc.desc: Verify the MatchConnectionGroup.
308  * @tc.type: FUNC
309  * @tc.require: I9KNY6
310  */
311 HWTEST_F(ConnectionDetectorTest, DfsService_MatchConnectionGroup_005, TestSize.Level1)
312 {
313     GTEST_LOG_(INFO) << "DfsService_MatchConnectionGroup_005 start";
314     string testPath = "/data/test/1.txt";
315     string networkId = "abc";
316     int fd = open(testPath.c_str(), O_RDWR | O_CREAT);
317     ASSERT_TRUE(fd != -1) << "Failed to open file in DfsService_MatchConnectionGroup_004! " << errno;
318     close(fd);
319 
320     string content = "\nabc\nconnection_status\n1 2 3";
321     bool contentCreate = SaveStringToFile(testPath, content, true);
322     ASSERT_TRUE(contentCreate) <<
323         "Failed to SaveStringToFile 1 in DfsService_MatchConnectionGroup_005! " << errno;
324     bool flag = ConnectionDetector::MatchConnectionGroup(testPath, networkId);
325     EXPECT_EQ(flag, true);
326 
327     content.clear();
328     content = "\nabc\nconnection_status\n1 2 2";
329     contentCreate = SaveStringToFile(testPath, content, true);
330     ASSERT_TRUE(contentCreate) <<
331         "Failed to SaveStringToFile 2  in DfsService_MatchConnectionGroup_005! " << errno;
332     flag = ConnectionDetector::MatchConnectionGroup(testPath, networkId);
333     EXPECT_EQ(flag, true);
334 
335     int ret = unlink(testPath.c_str());
336     ASSERT_TRUE(ret != -1) <<
337         "Failed to delete file in DfsService_MatchConnectionGroup_005! " << errno;
338     GTEST_LOG_(INFO) << "DfsService_MatchConnectionGroup_005 end";
339 }
340 
341 /**
342  * @tc.name: DfsService_MatchConnectionGroup_006
343  * @tc.desc: Verify the MatchConnectionGroup.
344  * @tc.type: FUNC
345  * @tc.require: I9KNY6
346  */
347 HWTEST_F(ConnectionDetectorTest, DfsService_MatchConnectionGroup_006, TestSize.Level1)
348 {
349     GTEST_LOG_(INFO) << "DfsService_MatchConnectionGroup_006 start";
350     string testPath = "/data/test/1.txt";
351     string networkId = "abc";
352     int fd = open(testPath.c_str(), O_RDWR | O_CREAT);
353     ASSERT_TRUE(fd != -1) << "Failed to open file in DfsService_MatchConnectionGroup_006! " << errno;
354     close(fd);
355 
356     string content = "\nabc\nconnection_status\n1 1 3";
357     bool contentCreate = SaveStringToFile(testPath, content, true);
358     ASSERT_TRUE(contentCreate) <<
359         "Failed to SaveStringToFile 1 in DfsService_MatchConnectionGroup_006! " << errno;
360     bool flag = ConnectionDetector::MatchConnectionGroup(testPath, networkId);
361     EXPECT_EQ(flag, false);
362 
363     content.clear();
364     content = "\nabc\nconnection_status\n1 2 1";
365     contentCreate = SaveStringToFile(testPath, content, true);
366     ASSERT_TRUE(contentCreate) <<
367         "Failed to SaveStringToFile 2  in DfsService_MatchConnectionGroup_006! " << errno;
368     flag = ConnectionDetector::MatchConnectionGroup(testPath, networkId);
369     EXPECT_EQ(flag, false);
370 
371     int ret = unlink(testPath.c_str());
372     ASSERT_TRUE(ret != -1) <<
373         "Failed to delete file in DfsService_MatchConnectionGroup_005! " << errno;
374     GTEST_LOG_(INFO) << "DfsService_MatchConnectionGroup_005 end";
375 }
376 
377 HWTEST_F(ConnectionDetectorTest, DfsService_CheckValidDir_001, TestSize.Level1)
378 {
379     GTEST_LOG_(INFO) << "DfsService_CheckValidDir_001_Start";
380     string path;
381     ConnectionDetector connectionDetector;
382     bool ret = connectionDetector.CheckValidDir(path);
383     EXPECT_EQ(ret, false);
384     GTEST_LOG_(INFO) << "DfsService_CheckValidDir_001_End";
385 }
386 
387 HWTEST_F(ConnectionDetectorTest, DfsService_CheckValidDir_002, TestSize.Level1)
388 {
389     GTEST_LOG_(INFO) << "DfsService_CheckValidDir_002_Start";
390     string path = "test@txt#1";
391     ConnectionDetector connectionDetector;
392     bool ret = connectionDetector.CheckValidDir(path);
393     EXPECT_EQ(ret, false);
394     GTEST_LOG_(INFO) << "DfsService_CheckValidDir_002_End";
395 }
396 
397 /**
398  * @tc.name: DfsService_CheckValidDir_003
399  * @tc.desc: Verify the CheckValidDir func.
400  * @tc.type: FUNC
401  * @tc.require: I9KNY6
402  */
403 HWTEST_F(ConnectionDetectorTest, DfsService_CheckValidDir_003, TestSize.Level1)
404 {
405     GTEST_LOG_(INFO) << "ConnectionDetectorTest_CheckValidDir_0100 start";
406     bool flag = ConnectionDetector::CheckValidDir("/data/test");
407     EXPECT_EQ(flag, true);
408     flag = ConnectionDetector::CheckValidDir("/data/test2");
409     EXPECT_EQ(flag, false);
410     string testFile = "/data/test/test.txt";
411     int32_t fd = open(testFile.c_str(), O_RDWR | O_CREAT);
412     ASSERT_TRUE(fd != -1) << "DfsService_CheckValidDir_003 Create File Failed!";
413     close(fd);
414     flag = ConnectionDetector::CheckValidDir(testFile);
415     EXPECT_EQ(flag, false);
416 
417     int32_t ret = unlink(testFile.c_str());
418     ASSERT_TRUE(ret != -1) << "Failed to delete file in DfsService_CheckValidDir_003! " << errno;
419     GTEST_LOG_(INFO) << "DfsService_CheckValidDir_003 end";
420 }
421 
422 HWTEST_F(ConnectionDetectorTest, DfsService_GetCurrentUserId_001, TestSize.Level1)
423 {
424     GTEST_LOG_(INFO) << "DfsService_GetCurrentUserId_001_Start";
425     int32_t ret = ConnectionDetector::GetCurrentUserId();
426     EXPECT_EQ(ret, 100);
427     GTEST_LOG_(INFO) << "DfsService_GetCurrentUserId_001_End";
428 }
429 }