• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 
16 #include <cstdio>
17 #include <cstdlib>
18 
19 #include <dirent.h>
20 #include <fcntl.h>
21 
22 #include <errors.h>
23 #include <file_ex.h>
24 #include <gtest/gtest.h>
25 
26 #include "b_filesystem/b_dir.h"
27 #include "b_process/b_process.h"
28 #include "test_manager.h"
29 
30 namespace OHOS::FileManagement::Backup {
31 using namespace std;
32 
33 class BDirTest : public testing::Test {
34 public:
SetUpTestCase(void)35     static void SetUpTestCase(void) {};
TearDownTestCase()36     static void TearDownTestCase() {};
SetUp()37     void SetUp() {};
TearDown()38     void TearDown() {};
39 };
40 
41 /**
42  * @tc.number: SUB_backup_b_dir_GetDirFiles_0100
43  * @tc.name: b_dir_GetDirFiles_0100
44  * @tc.desc: Test function of GetDirFiles interface for SUCCESS.
45  * @tc.size: MEDIUM
46  * @tc.type: FUNC
47  * @tc.level Level 0
48  * @tc.require: I6F3GV
49  */
50 HWTEST_F(BDirTest, b_dir_GetDirFiles_0100, testing::ext::TestSize.Level0)
51 {
52     GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetDirFiles_0100";
53     try {
54         TestManager tm("b_dir_GetDirFiles_0100");
55 
56         string preparedDir = tm.GetRootDirCurTest();
57         string touchFilePrefix = string("touch ") + preparedDir;
58         system(touchFilePrefix.append("a.txt").c_str());
59         system(touchFilePrefix.append("b.txt").c_str());
60         system(touchFilePrefix.append("c.txt").c_str());
61 
62         bool bSucc;
63         vector<string> out;
64         tie(bSucc, out) = BDir::GetDirFiles(preparedDir);
65 
66         vector<string> expectedRes = {preparedDir.append("a.txt"), preparedDir.append("b.txt"),
67                                       preparedDir.append("c.txt")};
68         EXPECT_EQ(out, expectedRes);
69 
70         tie(bSucc, out) = BDir::GetDirFiles("dev");
71         EXPECT_EQ(bSucc, true);
72     } catch (...) {
73         EXPECT_TRUE(false);
74         GTEST_LOG_(INFO) << "BDirTest-an exception occurred.";
75     }
76     GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetDirFiles_0100";
77 }
78 
79 /**
80  * @tc.number: SUB_backup_b_dir_GetBigFiles_0100
81  * @tc.name: b_dir_GetBigFiles_0100
82  * @tc.desc: 测试GetBigFiles接口是否能成功获取大文件
83  * @tc.size: MEDIUM
84  * @tc.type: FUNC
85  * @tc.level Level 1
86  * @tc.require: I6F3GV
87  */
88 HWTEST_F(BDirTest, b_dir_GetBigFiles_0100, testing::ext::TestSize.Level1)
89 {
90     GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0100";
91     try {
92         TestManager tm("b_dir_GetBigFiles_0100");
93         string rootDir = tm.GetRootDirCurTest();
94         string filePath1 = rootDir + "a.txt";
95         string filePath2 = rootDir + "b.txt";
96         // 文件大小大于等于1GB(1024MB)的文件属于大文件,因此这里创建大小为1025MB和1026MB的大文件
97         auto [bFatalErr, ret] =
98             BProcess::ExecuteCmd({"dd", "if=/dev/urandom", ("of=" + filePath1).c_str(), "bs=1M", "count=1025"});
99         EXPECT_FALSE(bFatalErr);
100         EXPECT_EQ(ret, 0);
101         tie(bFatalErr, ret) =
102             BProcess::ExecuteCmd({"dd", "if=/dev/urandom", ("of=" + filePath2).c_str(), "bs=1M", "count=1026"});
103         EXPECT_FALSE(bFatalErr);
104         EXPECT_EQ(ret, 0);
105         vector<string> includes = {rootDir};
106         vector<string> excludes = {filePath2};
107         auto [errCode, mpNameToStat] = BDir::GetBigFiles(includes, excludes);
108         EXPECT_EQ(errCode, ERR_OK);
109         EXPECT_EQ(mpNameToStat.at(filePath1).st_size, 1024 * 1024 * 1025);
110         EXPECT_EQ(mpNameToStat.find(filePath2), mpNameToStat.end());
111     } catch (...) {
112         EXPECT_TRUE(false);
113         GTEST_LOG_(INFO) << "BDirTest-an exception occurred.";
114     }
115     GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0100";
116 }
117 
118 /**
119  * @tc.number: SUB_backup_b_dir_GetBigFiles_0200
120  * @tc.name: b_dir_GetBigFiles_0200
121  * @tc.desc: 测试GetBigFiles接口 分支逻辑
122  * @tc.size: MEDIUM
123  * @tc.type: FUNC
124  * @tc.level Level 1
125  * @tc.require: I6F3GV
126  */
127 HWTEST_F(BDirTest, b_dir_GetBigFiles_0200, testing::ext::TestSize.Level1)
128 {
129     GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0200";
130     try {
131         vector<string> includes = {{}, {}};
132         vector<string> excludes = {{}};
133         auto [errCode, mpNameToStat] = BDir::GetBigFiles(includes, excludes);
134         EXPECT_EQ(errCode, ERR_OK);
135     } catch (...) {
136         EXPECT_TRUE(false);
137         GTEST_LOG_(INFO) << "BDirTest-an exception occurred.";
138     }
139     GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0200";
140 }
141 
142 /**
143  * @tc.number: SUB_backup_b_dir_GetBigFiles_0300
144  * @tc.name: b_dir_GetBigFiles_0300
145  * @tc.desc: 测试GetBigFiles接口 分支逻辑
146  * @tc.size: MEDIUM
147  * @tc.type: FUNC
148  * @tc.level Level 1
149  * @tc.require: I6F3GV
150  */
151 HWTEST_F(BDirTest, b_dir_GetBigFiles_0300, testing::ext::TestSize.Level1)
152 {
153     GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0300";
154     try {
155         TestManager tm("b_dir_GetBigFiles_0300");
156         string preparedDir = tm.GetRootDirCurTest();
157         string cmdMkdir = string("mkdir -p ") + preparedDir + string("test/test1/test2");
158         system(cmdMkdir.c_str());
159         string touchFilePrefix = string("touch ") + preparedDir;
160         system(touchFilePrefix.append("a.txt").c_str());
161         system(touchFilePrefix.append("b.txt").c_str());
162         system(touchFilePrefix.append("c.txt").c_str());
163 
164         touchFilePrefix = string("touch ") + preparedDir + string("test/");
165         system(touchFilePrefix.append("a.txt").c_str());
166         system(touchFilePrefix.append("b.txt").c_str());
167         system(touchFilePrefix.append("c.txt").c_str());
168         touchFilePrefix = string("touch ") + preparedDir + string("test/test1/test2");
169         system(touchFilePrefix.append("a.txt").c_str());
170         system(touchFilePrefix.append("b.txt").c_str());
171         system(touchFilePrefix.append("c.txt").c_str());
172         vector<string> includes = {preparedDir + string("/*"), preparedDir + string("test")};
173         vector<string> excludes = {preparedDir + string("/test/test1/test2"), {}};
174         auto [errCode, mpNameToStat] = BDir::GetBigFiles(includes, excludes);
175         EXPECT_EQ(errCode, ERR_OK);
176     } catch (...) {
177         EXPECT_TRUE(false);
178         GTEST_LOG_(INFO) << "BDirTest-an exception occurred.";
179     }
180     GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0300";
181 }
182 } // namespace OHOS::FileManagement::Backup