• 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 <copy/file_copy_manager.h>
16 #include <copy/file_size_utils.h>
17 #include <gtest/gtest.h>
18 #include <securec.h>
19 
20 #include "dfs_error.h"
21 
22 #include "directory_ex.h"
23 
24 namespace OHOS::Storage::DistributedFile::Test {
25 using namespace OHOS::FileManagement;
26 using namespace testing;
27 using namespace testing::ext;
28 using namespace std;
29 
30 class FileSizeUtilsTest : public testing::Test {
31 public:
32     static void SetUpTestCase(void);
33     static void TearDownTestCase(void);
34     void SetUp();
35     void TearDown();
36 
37     uint64_t process = 0;
38     uint64_t fileSize = 0;
39     using callBack = std::function<void(uint64_t processSize, uint64_t totalFileSize)>;
__anonde96467a0102(uint64_t processSize, uint64_t totalFileSize) 40     callBack listener = [&](uint64_t processSize, uint64_t totalFileSize) {
41         process = processSize;
42         fileSize = totalFileSize;
43     };
44 };
45 
SetUpTestCase(void)46 void FileSizeUtilsTest::SetUpTestCase(void)
47 {
48     GTEST_LOG_(INFO) << "SetUpTestCase";
49 }
50 
TearDownTestCase(void)51 void FileSizeUtilsTest::TearDownTestCase(void)
52 {
53     GTEST_LOG_(INFO) << "TearDownTestCase";
54 }
55 
SetUp(void)56 void FileSizeUtilsTest::SetUp(void)
57 {
58     GTEST_LOG_(INFO) << "SetUp";
59 }
60 
TearDown(void)61 void FileSizeUtilsTest::TearDown(void)
62 {
63     GTEST_LOG_(INFO) << "TearDown";
64 }
65 
66 /**
67 * @tc.name: FileSizeUtils_0001
68 * @tc.desc: The execution of the GetSize failed.
69 * @tc.type: FUNC
70 * @tc.require: I7TDJK
71  */
72 HWTEST_F(FileSizeUtilsTest, FileSizeUtils_0001, TestSize.Level0)
73 {
74     GTEST_LOG_(INFO) << "FileSizeUtils_0001 Start";
75     string srcuri = "file://docs/storage/media/100/local/files/Docs/aa/";
76     bool isSrcUri = true;
77     auto ptr = std::make_shared<FileSizeUtils>();
78     int32_t ret = ptr->GetSize(srcuri, isSrcUri, fileSize);
79     EXPECT_EQ(ret, 2);
80 
81     ret = ptr->GetSize("", isSrcUri, fileSize);
82     EXPECT_EQ(ret, ERR_BAD_VALUE);
83 
84     srcuri = "/data/test/FileSizeUtils_0001";
85     if (!ForceCreateDirectory(srcuri)) {
86         GTEST_LOG_(INFO) << "GetGetDirSize_0001 create dir err:" << srcuri;
87     }
88     ret = ptr->GetSize(srcuri, isSrcUri, fileSize);
89     EXPECT_EQ(ret, E_OK);
90 
91     std::string testFile = srcuri + "/1.txt"; // 1: file name
92     int fd = open(testFile.c_str(), O_RDWR | O_CREAT);
93     if (fd < 0) {
94         GTEST_LOG_(INFO) << "GetGetDirSize_0001 create file err" << testFile;
95     } else {
96         close(fd);
97     }
98     ret = ptr->GetSize(testFile, isSrcUri, fileSize);
99     EXPECT_EQ(ret, E_OK);
100     if (!ForceCreateDirectory(srcuri)) {
101         GTEST_LOG_(INFO) << "GetGetDirSize_0001 create dir err:" << srcuri;
102     }
103     GTEST_LOG_(INFO) << "FileSizeUtils_0001 End";
104 }
105 
106 /**
107 * @tc.name: IsDirectory_0001
108 * @tc.desc: Judge is directory
109 * @tc.type: FUNC
110 * @tc.require: I7TDJK
111  */
112 HWTEST_F(FileSizeUtilsTest, IsDirectory_0001, TestSize.Level0)
113 {
114     GTEST_LOG_(INFO) << "IsDirectory_0001 Start";
115     string srcuri = "/data/test/IsDirectory_0001";
116     bool isSrcUri = true;
117     bool isDirectory = false;
118     auto ptr = std::make_shared<FileSizeUtils>();
119     int32_t ret = ptr->IsDirectory("", isSrcUri, isDirectory);
120     if (!ForceCreateDirectory(srcuri)) {
121         GTEST_LOG_(INFO) << "GetGetDirSize_0001 create dir err:" << srcuri;
122     }
123     EXPECT_EQ(ret, ERR_BAD_VALUE);
124     GTEST_LOG_(INFO) << "IsDirectory_0001 End";
125 }
126 
127 /**
128 * @tc.name: FilterFunc_0001
129 * @tc.desc: filter file
130 * @tc.type: FUNC
131 * @tc.require: I7TDJK
132  */
133 HWTEST_F(FileSizeUtilsTest, FilterFunc_0001, TestSize.Level0)
134 {
135     GTEST_LOG_(INFO) << "FilterFunc_0001 Start";
136     dirent dir;
137     strcpy_s(dir.d_name, sizeof(dir.d_name), ".");
138     auto ptr = std::make_shared<FileSizeUtils>();
139     int ret = ptr->FilterFunc(&dir);
140     EXPECT_EQ(ret, 0); // 0: dismatch
141 
142     strcpy_s(dir.d_name, sizeof(dir.d_name), "..");
143     ret = ptr->FilterFunc(&dir);
144     EXPECT_EQ(ret, 0); // 0: dismatch
145 
146     strcpy_s(dir.d_name, sizeof(dir.d_name), "test");
147     ret = ptr->FilterFunc(&dir);
148     EXPECT_EQ(ret, 1); // 1: match
149     GTEST_LOG_(INFO) << "FilterFunc_0001 End";
150 }
151 
152 /**
153 * @tc.name: FilterFunc_0001
154 * @tc.desc: get real path
155 * @tc.type: FUNC
156 * @tc.require: I7TDJK
157  */
158 HWTEST_F(FileSizeUtilsTest, GetRealPath_0001, TestSize.Level0)
159 {
160     GTEST_LOG_(INFO) << "GetRealPath_0001 Start";
161     auto ptr = std::make_shared<FileSizeUtils>();
162     std::string path = ".";
163     std::string retStr = ptr->GetRealPath(path);
164     EXPECT_EQ(retStr, "");
165 
166     path = "..";
167     retStr = ptr->GetRealPath(path);
168     EXPECT_EQ(retStr, "");
169     GTEST_LOG_(INFO) << "GetRealPath_0001 End";
170 }
171 
172 /**
173 * @tc.name: GetDirSize_0001
174 * @tc.desc: get dir size
175 * @tc.type: FUNC
176 * @tc.require: I7TDJK
177  */
178 HWTEST_F(FileSizeUtilsTest, GetGetDirSize_0001, TestSize.Level0)
179 {
180     GTEST_LOG_(INFO) << "GetGetDirSize_0001 Start";
181     std::string path = "/data/test/GetGetDirSize_0001";
182     uint64_t size = 0;
183     if (!ForceCreateDirectory(path)) {
184         GTEST_LOG_(INFO) << "GetGetDirSize_0001 create dir err:" << path;
185     }
186     std::string tmpDir = path + "/dir";
187     if (!ForceCreateDirectory(tmpDir)) {
188         GTEST_LOG_(INFO) << "GetGetDirSize_0001 create dir err:" << tmpDir;
189     }
190     std::string testFile = path + "/1.txt"; // 1: file name
191     int fd = open(testFile.c_str(), O_RDWR | O_CREAT);
192     if (fd < 0) {
193         GTEST_LOG_(INFO) << "GetGetDirSize_0001 create file err" << testFile;
194     } else {
195         close(fd);
196     }
197     std::string testLink = path + "/2"; // 2: link file name
198     if (symlink(testFile.c_str(), testLink.c_str())) {
199         GTEST_LOG_(INFO) << "GetGetDirSize_0001 create linkfile err" << testLink;
200     }
201     auto ptr = std::make_shared<FileSizeUtils>();
202     int32_t ret = ptr->GetDirSize(path, size);
203     EXPECT_EQ(ret, E_OK);
204     if (!ForceRemoveDirectory(path)) {
205         GTEST_LOG_(INFO) << "GetGetDirSize_0001 create dir err";
206     }
207     GTEST_LOG_(INFO) << "GetGetDirSize_0001 End";
208 }
209 
210 /**
211 * @tc.name: IsFile_0001
212 * @tc.desc: Judge is file
213 * @tc.type: FUNC
214 * @tc.require: I7TDJK
215  */
216 HWTEST_F(FileSizeUtilsTest, IsFile_0001, TestSize.Level0)
217 {
218     GTEST_LOG_(INFO) << "IsFile_0001 Start";
219     auto ptr = std::make_shared<FileSizeUtils>();
220     bool isFile = false;
221     int32_t ret = ptr->IsFile("", isFile);
222     EXPECT_EQ(ret, 2); // 2: file not found
223     EXPECT_EQ(isFile, false);
224     GTEST_LOG_(INFO) << "IsFile_0001 End";
225 }
226 
227 /**
228 * @tc.name: GetRealUri_0001
229 * @tc.desc: Judge is file
230 * @tc.type: FUNC
231 * @tc.require: I7TDJK
232  */
233 HWTEST_F(FileSizeUtilsTest, GetRealUri_0001, TestSize.Level0)
234 {
235     GTEST_LOG_(INFO) << "GetRealUri_0001 Start";
236     std::string testUri = "file://docs/storage/";
237     std::string remoteInfo = "?networkid=123456";
238     std::string realUri = FileSizeUtils::GetRealUri(testUri);
239     EXPECT_EQ(testUri, realUri);
240     realUri = FileSizeUtils::GetRealUri(testUri + remoteInfo);
241     EXPECT_EQ(testUri, realUri);
242     GTEST_LOG_(INFO) << "GetRealUri_0001 End";
243 }
244 
245 /**
246  * @tc.name: IsFilePathValid001
247  * @tc.desc: Verify the IsFilePathValid function
248  * @tc.type: FUNC
249  * @tc.require: I6H5MH
250  */
251 HWTEST_F(FileSizeUtilsTest, IsFilePathValid001, TestSize.Level0)
252 {
253     GTEST_LOG_(INFO) << "IsFilePathValid001 Start";
254     try {
255         bool isValid = FileSizeUtils::IsFilePathValid("../test../test1");
256         EXPECT_FALSE(isValid);
257         isValid = FileSizeUtils::IsFilePathValid("/../test../test1");
258         EXPECT_FALSE(isValid);
259         isValid = FileSizeUtils::IsFilePathValid("test../../test");
260         EXPECT_FALSE(isValid);
261         isValid = FileSizeUtils::IsFilePathValid("test../../");
262         EXPECT_FALSE(isValid);
263         isValid = FileSizeUtils::IsFilePathValid("test../test../..");
264         EXPECT_FALSE(isValid);
265         isValid = FileSizeUtils::IsFilePathValid("/test/..test/..");
266         EXPECT_FALSE(isValid);
267 
268         isValid = FileSizeUtils::IsFilePathValid("test");
269         EXPECT_TRUE(isValid);
270         isValid = FileSizeUtils::IsFilePathValid("/test/test../test");
271         EXPECT_TRUE(isValid);
272         isValid = FileSizeUtils::IsFilePathValid("/test../test../test");
273         EXPECT_TRUE(isValid);
274         isValid = FileSizeUtils::IsFilePathValid("/test../test../test../");
275         EXPECT_TRUE(isValid);
276         isValid = FileSizeUtils::IsFilePathValid("/test../test../test../..test");
277         EXPECT_TRUE(isValid);
278     } catch (...) {
279         GTEST_LOG_(INFO) << " IsFilePathValid001 ERROR";
280     }
281     GTEST_LOG_(INFO) << "IsFilePathValid001 End";
282 }
283 
284 /**
285  * @tc.name: Deleter_0001
286  * @tc.desc: Deleter
287  * @tc.type: FUNC
288  * @tc.require: I7TDJK
289  */
290 HWTEST_F(FileSizeUtilsTest, Deleter_0001, TestSize.Level1)
291 {
292     GTEST_LOG_(INFO) << "Deleter_0001 Start";
293     NameList* arg = new NameList();
294     arg->namelist = new struct dirent*[2];
295     arg->namelist[0] = new struct dirent();
296     arg->namelist[1] = nullptr;
297     arg->direntNum = 2;
298     FileSizeUtils::Deleter(arg);
299     GTEST_LOG_(INFO) << "Deleter_0001 End";
300 }
301 }
302