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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include "b_error/b_error.h"
20 #include "file_ex.h"
21 #include "tar_file.h"
22 #include "test_manager.h"
23
24 namespace OHOS::FileManagement::Backup {
25 using namespace std;
26 using namespace testing;
27
28 class TarFileTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase();
SetUp()32 void SetUp() override {};
TearDown()33 void TearDown() override {};
34 };
35
SetUpTestCase()36 void TarFileTest::SetUpTestCase()
37 {
38 GTEST_LOG_(INFO) << "SetUpTestCase enter";
39 }
40
TearDownTestCase()41 void TarFileTest::TearDownTestCase()
42 {
43 GTEST_LOG_(INFO) << "TearDownTestCase enter";
44 }
45
ClearCache()46 static void ClearCache()
47 {
48 TarFile::GetInstance().fileCount_ = 0;
49 TarFile::GetInstance().tarMap_.clear();
50 TarFile::GetInstance().rootPath_.clear();
51 TarFile::GetInstance().packagePath_.clear();
52 TarFile::GetInstance().baseTarName_.clear();
53 TarFile::GetInstance().tarFileName_.clear();
54 TarFile::GetInstance().ioBuffer_.clear();
55 TarFile::GetInstance().currentTarName_.clear();
56 TarFile::GetInstance().currentTarFileSize_ = 0;
57 TarFile::GetInstance().tarFileCount_ = 0;
58 TarFile::GetInstance().currentFileName_.clear();
59 if (TarFile::GetInstance().currentTarFile_ != nullptr) {
60 fclose(TarFile::GetInstance().currentTarFile_);
61 TarFile::GetInstance().currentTarFile_ = nullptr;
62 }
63 }
64
65 /**
66 * @tc.number: SUB_Tar_File_GetInstance_0100
67 * @tc.name: SUB_Tar_File_GetInstance_0100
68 * @tc.desc: 测试 GetInstance 接口
69 * @tc.size: MEDIUM
70 * @tc.type: FUNC
71 * @tc.level Level 1
72 * @tc.require: I6F3GV
73 */
74 HWTEST_F(TarFileTest, SUB_Tar_File_GetInstance_0100, testing::ext::TestSize.Level1)
75 {
76 GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_GetInstance_0100";
77 try {
78 TarFile::GetInstance();
79 EXPECT_TRUE(true);
80 } catch (...) {
81 EXPECT_TRUE(false);
82 GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile.";
83 }
84 GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_GetInstance_0100";
85 }
86
87 /**
88 * @tc.number: SUB_Tar_File_Packet_0100
89 * @tc.name: SUB_Tar_File_Packet_0100
90 * @tc.desc: 测试 Packet 接口
91 * @tc.size: MEDIUM
92 * @tc.type: FUNC
93 * @tc.level Level 1
94 * @tc.require: I6F3GV
95 */
96 HWTEST_F(TarFileTest, SUB_Tar_File_Packet_0100, testing::ext::TestSize.Level1)
97 {
98 GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_Packet_0100";
99 try {
100 vector<string> srcFiles = {};
101 string tarFileName = "";
102 string pkPath = "";
103 TarMap tarMap;
__anon424e31410102(std::string path, int err) 104 auto reportCb = [](std::string path, int err) {
105 return;
106 };
107 bool ret = TarFile::GetInstance().Packet(srcFiles, tarFileName, pkPath, tarMap, reportCb);
108 EXPECT_TRUE(tarMap.empty());
109 EXPECT_FALSE(ret);
110
111 TestManager tm("SUB_Tar_File_Packet_0100");
112 string root = tm.GetRootDirCurTest();
113 pkPath = root;
114 ret = TarFile::GetInstance().Packet(srcFiles, tarFileName, pkPath, tarMap, reportCb);
115 EXPECT_TRUE(tarMap.empty());
116 EXPECT_FALSE(ret);
117 } catch (...) {
118 EXPECT_TRUE(false);
119 GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile.";
120 }
121 GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_Packet_0100";
122 }
123
124 /**
125 * @tc.number: SUB_Tar_File_Packet_0200
126 * @tc.name: SUB_Tar_File_Packet_0200
127 * @tc.desc: 测试 Packet 接口
128 * @tc.size: MEDIUM
129 * @tc.type: FUNC
130 * @tc.level Level 1
131 * @tc.require: I6F3GV
132 */
133 HWTEST_F(TarFileTest, SUB_Tar_File_Packet_0200, testing::ext::TestSize.Level1)
134 {
135 GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_Packet_0200";
136 try {
137 TestManager tm("SUB_Tar_File_Packet_0200");
138 string root = tm.GetRootDirCurTest();
139 string testDir = root + "/testdir";
140 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
141 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
142 throw BError(errno);
143 }
144
145 vector<string> srcFiles = {testDir};
146 TarMap tarMap;
147 string tarFileName = "part";
148 string pkPath = root;
__anon424e31410202(std::string path, int err) 149 auto reportCb = [](std::string path, int err) {
150 return;
151 };
152 bool ret = TarFile::GetInstance().Packet(srcFiles, tarFileName, pkPath, tarMap, reportCb);
153 EXPECT_TRUE(ret);
154 EXPECT_EQ(tarMap.size(), 1);
155 ClearCache();
156 } catch (...) {
157 EXPECT_TRUE(false);
158 GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile.";
159 }
160 GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_Packet_0200";
161 }
162
163 /**
164 * @tc.number: SUB_Tar_File_Packet_0300
165 * @tc.name: SUB_Tar_File_Packet_0300
166 * @tc.desc: 测试 Packet 接口
167 * @tc.size: MEDIUM
168 * @tc.type: FUNC
169 * @tc.level Level 1
170 * @tc.require: I6F3GV
171 */
172 HWTEST_F(TarFileTest, SUB_Tar_File_Packet_0300, testing::ext::TestSize.Level1)
173 {
174 GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_Packet_0300";
175 try {
176 TestManager tm("SUB_Tar_File_Packet_0300");
177 string root = tm.GetRootDirCurTest();
178 string testDir = root + "/testdir/";
179 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
180 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
181 throw BError(errno);
182 }
183
184 vector<string> srcFiles = {testDir};
185 string pkPath = root;
186 string tarFileName = "part";
187 TarMap tarMap;
__anon424e31410302(std::string path, int err) 188 auto reportCb = [](std::string path, int err) {
189 return;
190 };
191 bool ret = TarFile::GetInstance().Packet(srcFiles, tarFileName, pkPath, tarMap, reportCb);
192 EXPECT_TRUE(ret);
193 EXPECT_EQ(tarMap.size(), 1);
194 ClearCache();
195 } catch (...) {
196 EXPECT_TRUE(false);
197 GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile.";
198 }
199 GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_Packet_0300";
200 }
201
202 /**
203 * @tc.number: SUB_Tar_File_Packet_0400
204 * @tc.name: SUB_Tar_File_Packet_0400
205 * @tc.desc: 测试 Packet 接口
206 * @tc.size: MEDIUM
207 * @tc.type: FUNC
208 * @tc.level Level 1
209 * @tc.require: I6F3GV
210 */
211 HWTEST_F(TarFileTest, SUB_Tar_File_Packet_0400, testing::ext::TestSize.Level1)
212 {
213 GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_Packet_0400";
214 try {
215 vector<string> srcFiles = {"xxx"};
216 string tarFileName = "part";
217 string pkPath = "/data/storage/el2/backup/backup";
218 TarMap tarMap;
__anon424e31410402(std::string path, int err) 219 auto reportCb = [](std::string path, int err) {
220 return;
221 };
222 bool ret = TarFile::GetInstance().Packet(srcFiles, tarFileName, pkPath, tarMap, reportCb);
223 EXPECT_FALSE(ret);
224 EXPECT_TRUE(tarMap.empty());
225 } catch (...) {
226 EXPECT_TRUE(true);
227 GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile.";
228 }
229 GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_Packet_0400";
230 }
231
232 /**
233 * @tc.number: SUB_Tar_File_Packet_0500
234 * @tc.name: SUB_Tar_File_Packet_0500
235 * @tc.desc: 测试 Packet 接口
236 * @tc.size: MEDIUM
237 * @tc.type: FUNC
238 * @tc.level Level 1
239 * @tc.require: I6F3GV
240 */
241 HWTEST_F(TarFileTest, SUB_Tar_File_Packet_0500, testing::ext::TestSize.Level1)
242 {
243 GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_Packet_0500";
244 try {
245 // 预置文件和目录
246 TestManager tm("SUB_Tar_File_Packet_0500");
247 string root = tm.GetRootDirCurTest();
248 string testDir = root + "/testdir";
249 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
250 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
251 throw BError(errno);
252 }
253 string bFile = testDir + "/b.txt";
254 string aFile = testDir + "/a.txt";
255 SaveStringToFile(aFile, "hello");
256 SaveStringToFile(bFile, "world");
257
258 // 调用Packet打包
259 vector<string> srcFiles = {aFile, bFile};
260 string tarFileName = "part";
261 string pkPath = root;
262 TarMap tarMap;
__anon424e31410502(std::string path, int err) 263 auto reportCb = [](std::string path, int err) {
264 return;
265 };
266 bool ret = TarFile::GetInstance().Packet(srcFiles, tarFileName, pkPath, tarMap, reportCb);
267 EXPECT_TRUE(ret);
268 EXPECT_EQ(tarMap.size(), 1);
269 ClearCache();
270 } catch (...) {
271 EXPECT_TRUE(false);
272 GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile.";
273 }
274 GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_Packet_0500";
275 }
276
277 /**
278 * @tc.number: SUB_Tar_File_Packet_0600
279 * @tc.name: SUB_Tar_File_Packet_0600
280 * @tc.desc: 测试 Packet 接口
281 * @tc.size: MEDIUM
282 * @tc.type: FUNC
283 * @tc.level Level 1
284 * @tc.require: I6F3GV
285 */
286 HWTEST_F(TarFileTest, SUB_Tar_File_Packet_0600, testing::ext::TestSize.Level1)
287 {
288 GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_Packet_0600";
289 try {
290 // 预置文件和目录
291 TestManager tm("SUB_Tar_File_Packet_0600");
292 string root = tm.GetRootDirCurTest();
293 string testDir = root + "/testdir";
294 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
295 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
296 throw BError(errno);
297 }
298 vector<string> srcFiles {};
299 const uint32_t FILE_COUNT = 6010;
300 for (uint32_t i = 0; i < FILE_COUNT; ++i) {
301 string file = testDir + "/a_" + to_string(i) + ".txt";
302 SaveStringToFile(file, "hello");
303 srcFiles.emplace_back(file);
304 }
305 // 调用Packet打包
306 TarMap tarMap;
307 string tarFileName = "part";
308 string pkPath = root;
__anon424e31410602(std::string path, int err) 309 auto reportCb = [](std::string path, int err) {
310 return;
311 };
312 bool ret = TarFile::GetInstance().Packet(srcFiles, tarFileName, pkPath, tarMap, reportCb);
313 EXPECT_TRUE(ret);
314 EXPECT_EQ(tarMap.size(), 2);
315 ClearCache();
316 } catch (...) {
317 EXPECT_TRUE(false);
318 GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile.";
319 }
320 GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_Packet_0600";
321 }
322
323 /**
324 * @tc.number: SUB_Tar_File_Packet_0700
325 * @tc.name: SUB_Tar_File_Packet_0700
326 * @tc.desc: 测试 Packet 接口
327 * @tc.size: MEDIUM
328 * @tc.type: FUNC
329 * @tc.level Level 1
330 * @tc.require: I6F3GV
331 */
332 HWTEST_F(TarFileTest, SUB_Tar_File_Packet_0700, testing::ext::TestSize.Level1)
333 {
334 GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_Packet_0700";
335 try {
336 // 预置文件和目录
337 TestManager tm("SUB_Tar_File_Packet_0700");
338 string root = tm.GetRootDirCurTest();
339 string testDir = root + "/testdir";
340 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
341 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
342 throw BError(errno);
343 }
344 vector<string> srcFiles {};
345 const int32_t FILE_COUNT = 120;
346 string cmd("");
347 string fileName("");
348 for (int i = 0; i < FILE_COUNT; i++) {
349 fileName = root + "test_" + to_string(i);
350 cmd = "dd if=/dev/urandom of=" + fileName + " bs=1M count=1";
351 int ret = system(cmd.c_str());
352 EXPECT_EQ(ret, 0);
353 srcFiles.emplace_back(fileName);
354 }
355
356 // 调用Packet打包
357 string tarFileName = "part";
358 string pkPath = root;
359 TarMap tarMap;
__anon424e31410702(std::string path, int err) 360 auto reportCb = [](std::string path, int err) {
361 return;
362 };
363 bool ret = TarFile::GetInstance().Packet(srcFiles, tarFileName, pkPath, tarMap, reportCb);
364 EXPECT_TRUE(ret);
365 EXPECT_EQ(tarMap.size(), 2);
366 ClearCache();
367 } catch (...) {
368 EXPECT_TRUE(false);
369 GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile.";
370 }
371 GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_Packet_0700";
372 }
373 } // namespace OHOS::FileManagement::Backup