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