• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "installd_un_tar_file.h"
20 
21 #include <dirent.h>
22 #include <file_ex.h>
23 #include <filesystem>
24 #include <unistd.h>
25 
26 #include "b_error/b_error.h"
27 #include "test_manager.h"
28 
29 #include <sys/stat.h>
30 
31 namespace OHOS::FileManagement::Backup {
32 using namespace std;
33 using namespace testing;
34 
35 class InstalldUnTarFileTest : public testing::Test {
36 public:
37     static void SetUpTestCase(void);
38     static void TearDownTestCase();
SetUp()39     void SetUp() override {};
TearDown()40     void TearDown() override {};
41 };
42 
SetUpTestCase()43 void InstalldUnTarFileTest::SetUpTestCase()
44 {
45     GTEST_LOG_(INFO) << "InstalldUnTarFileTest::SetUpTestCase enter";
46 }
47 
TearDownTestCase()48 void InstalldUnTarFileTest::TearDownTestCase()
49 {
50     GTEST_LOG_(INFO) << "InstalldUnTarFileTest::TearDownTestCase enter";
51 }
52 
CreateUnSplitTar(const string & rootPath,const string & rootPathSubPath,int32_t innerFileCount)53 static string CreateUnSplitTar(const string &rootPath, const string &rootPathSubPath, int32_t innerFileCount)
54 {
55     try {
56         string testDir = rootPath + rootPathSubPath;
57         if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
58             GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
59             throw BError(errno);
60         }
61         for (int i = 0; i < innerFileCount; ++i) {
62             string file = testDir + "/" + to_string(i + 1) + ".txt";
63             SaveStringToFile(file, "hello" + to_string(i));
64             GTEST_LOG_(INFO) << "InstalldUnTarFileTest-an access: " << access(file.c_str(), F_OK);
65         }
66 
67         string tarFile = rootPath + "testUnSplit.tar";
68         string cmd = "tar -cvf " + tarFile + " " + testDir;
69         if (system(cmd.c_str()) != 0) {
70             GTEST_LOG_(INFO) << " execute tar failure, errno :" << errno;
71             throw BError(errno);
72         }
73         return tarFile;
74     } catch (...) {
75         EXPECT_TRUE(false);
76         GTEST_LOG_(INFO) << "InstalldUnTarFileTest-an exception occurred by CreateUnSplitTar.";
77     }
78     return "";
79 }
80 
CreateSplitTar(const string & rootPath,const string & rootPathSubPath)81 static vector<string> CreateSplitTar(const string& rootPath, const string& rootPathSubPath)
82 {
83     try {
84         const string testDir = rootPath + rootPathSubPath;
85         vector<string> needCreateDir = {testDir, testDir + "/dir1", testDir + "/dir1/dir2", testDir + "/dir3",
86                                         testDir + "/dir4"};
87         for (const auto &dir : needCreateDir) {
88             if (mkdir(dir.data(), S_IRWXU) && errno != EEXIST) {
89                 throw BError(errno);
90             }
91         }
92         constexpr int32_t fileCount = 5;
93         for (auto i = 0; i < needCreateDir.size(); ++i) {
94             string file = needCreateDir[i] + "/" + to_string(i) + ".txt";
95             string data = "";
96             for (auto j = 0; j < fileCount * (i + 1); ++j) {
97                 data += "test data 123456789";
98             }
99             SaveStringToFile(file, data);
100         }
101 
102         string tarDir = rootPath + "testTar";
103         if (mkdir(tarDir.data(), S_IRWXU) && errno != EEXIST) {
104             throw BError(errno);
105         }
106 
107         string tarPath = tarDir + "/test.tar";
108         string softlinkCmd = "cd " + testDir + "/dir4" + " && ln -s " + testDir + "/dir4/4.txt ./4_new.txt";
109         string tarCmd = "tar -cvf " + tarPath + " " + testDir;
110         string splitTarCmd = "cd " + tarDir + " && split -b 4k -a 2 test.tar test.tar.";
111         vector<string> cmds = {softlinkCmd, tarCmd, splitTarCmd};
112 
113         for (const string& cmd : cmds) {
114             if (system(cmd.c_str()) != 0) {
115                 throw BError(errno);
116             }
117         }
118         vector<string> res;
119         for (const auto& entry : std::filesystem::directory_iterator(tarDir)) {
120             if (entry.path().filename() != "test.tar") {
121                 res.push_back(entry.path().string());
122             }
123         }
124         return res;
125     } catch (...) {
126         GTEST_LOG_(INFO) << "InstalldUnTarFileTest-an exception occurred by CreateSplitTar.";
127     }
128     return {};
129 }
130 
131 /**
132  * @tc.number: Installd_Un_Tar_File_UnTarFile_0100
133  * @tc.name: Installd_Un_Tar_File_UnTarFile_0100
134  * @tc.desc: test File_UnTarFile gen
135  * @tc.size: MEDIUM
136  * @tc.type: FUNC
137  * @tc.level Level 1
138  * @tc.require: IC15LE
139  */
140 HWTEST_F(InstalldUnTarFileTest, Installd_Un_Tar_File_UnTarFile_0100, testing::ext::TestSize.Level1)
141 {
142     GTEST_LOG_(INFO) << "InstalldUnTarFileTest-begin Installd_Un_Tar_File_UnTarFile_0100";
143     try {
144         char *tarPath = nullptr;
145         installd::UnTarFile unTarFile(tarPath);
146 
147         const char *tarPath2 = string("/data/test/backup/test.tar").c_str();
148         installd::UnTarFile unTarFile2(tarPath2);
149 
150         EXPECT_TRUE(true);
151     } catch (...) {
152         EXPECT_TRUE(false);
153         GTEST_LOG_(INFO) << "InstalldUnTarFileTest-an exception occurred by InstalldUnTarFile.";
154     }
155     GTEST_LOG_(INFO) << "InstalldUnTarFileTest-end Installd_Un_Tar_File_UnTarFile_0100";
156 }
157 
158 /**
159  * @tc.number: Installd_Un_Tar_File_CheckIsSplitTar_0100
160  * @tc.name: Installd_Un_Tar_File_CheckIsSplitTar_0100
161  * @tc.desc: test File_CheckIsSplitTar method
162  * @tc.size: MEDIUM
163  * @tc.type: FUNC
164  * @tc.level Level 1
165  * @tc.require: IC15LE
166  */
167 HWTEST_F(InstalldUnTarFileTest, Installd_Un_Tar_File_CheckIsSplitTar_0100, testing::ext::TestSize.Level1)
168 {
169     GTEST_LOG_(INFO) << "InstalldUnTarFileTest-begin Installd_Un_Tar_File_CheckIsSplitTar_0100";
170     try {
171         const TestManager tm("Installd_Un_Tar_File_CheckIsSplitTar_0100");
172         const string rootPath = tm.GetRootDirCurTest();
173         constexpr int32_t innerFileCount = 10;
174         const string tarPath = CreateUnSplitTar(rootPath, "testTarPath", innerFileCount);
175         EXPECT_FALSE(tarPath.empty());
176         installd::UnTarFile unTarFile(rootPath.c_str());
177         const bool ret = unTarFile.CheckIsSplitTar(tarPath, rootPath);
178         EXPECT_FALSE(ret);
179     } catch (...) {
180         EXPECT_TRUE(false);
181         GTEST_LOG_(INFO) << "InstalldUnTarFileTest-an exception occurred by InstalldUnTarFile.";
182     }
183     GTEST_LOG_(INFO) << "InstalldUnTarFileTest-end Installd_Un_Tar_File_CheckIsSplitTar_0100";
184 }
185 
186 /**
187  * @tc.number: Installd_Un_Tar_File_IsEmptyBlock_0100
188  * @tc.name: Installd_Un_Tar_File_IsEmptyBlock_0100
189  * @tc.desc: test File_IsEmptyBlock method
190  * @tc.size: MEDIUM
191  * @tc.type: FUNC
192  * @tc.level Level 1
193  * @tc.require: IC15LE
194  */
195 HWTEST_F(InstalldUnTarFileTest, Installd_Un_Tar_File_IsEmptyBlock_0100, testing::ext::TestSize.Level1)
196 {
197     GTEST_LOG_(INFO) << "InstalldUnTarFileTest-begin Installd_Un_Tar_File_IsEmptyBlock_0100";
198     try {
199         const char *p = "\0";
200         installd::UnTarFile unTarFile(nullptr);
201         bool ret = unTarFile.IsEmptyBlock(p);
202         EXPECT_TRUE(ret);
203 
204         const char *p1 = "test";
205         installd::UnTarFile unTarFile1(nullptr);
206         ret = unTarFile.IsEmptyBlock(p1);
207         EXPECT_FALSE(ret);
208     } catch (...) {
209         EXPECT_TRUE(false);
210         GTEST_LOG_(INFO) << "InstalldUnTarFileTest-an exception occurred by InstalldUnTarFile.";
211     }
212     GTEST_LOG_(INFO) << "InstalldUnTarFileTest-end Installd_Un_Tar_File_IsEmptyBlock_0100";
213 }
214 
215 /**
216  * @tc.number: Installd_Un_Tar_File_CreateDirWithRecursive_0100
217  * @tc.name: Installd_Un_Tar_File_CreateDirWithRecursive_0100
218  * @tc.desc: test File_CreateDirWithRecursive method
219  * @tc.size: MEDIUM
220  * @tc.type: FUNC
221  * @tc.level Level 1
222  * @tc.require: IC15LE
223  */
224 HWTEST_F(InstalldUnTarFileTest, Installd_Un_Tar_File_CreateDirWithRecursive_0100, testing::ext::TestSize.Level1)
225 {
226     GTEST_LOG_(INFO) << "InstalldUnTarFileTest-begin Installd_Un_Tar_File_CreateDirWithRecursive_0100";
227     try {
228         installd::UnTarFile unTarFile(nullptr);
229         mode_t mode = 448;
230         bool ret = unTarFile.CreateDirWithRecursive("", mode);
231         EXPECT_FALSE(ret);
232 
233         TestManager tm("Installd_Un_Tar_File_CreateDirWithRecursive_0100");
234         string root = tm.GetRootDirCurTest();
235         ret = unTarFile.CreateDirWithRecursive(root, mode);
236         EXPECT_TRUE(ret);
237 
238         string destPath(root + "/testDir/test1/test2");
239         ret = unTarFile.CreateDirWithRecursive(destPath, mode);
240         EXPECT_TRUE(ret);
241     } catch (...) {
242         EXPECT_TRUE(false);
243         GTEST_LOG_(INFO) << "InstalldUnTarFileTest-an exception occurred by InstalldUnTarFile.";
244     }
245     GTEST_LOG_(INFO) << "InstalldUnTarFileTest-end Installd_Un_Tar_File_CreateDirWithRecursive_0100";
246 }
247 
248 /**
249  * @tc.number: Installd_Un_Tar_File_UnSplitTar_0100
250  * @tc.name: Installd_Un_Tar_File_UnSplitTar_0100
251  * @tc.desc: test File_UnSplitTar method
252  * @tc.size: MEDIUM
253  * @tc.type: FUNC
254  * @tc.level Level 1
255  * @tc.require: IC15LE
256  */
257 HWTEST_F(InstalldUnTarFileTest, Installd_Un_Tar_File_UnSplitTar_0100, testing::ext::TestSize.Level1)
258 {
259     GTEST_LOG_(INFO) << "InstalldUnTarFileTest-begin Installd_Un_Tar_File_UnSplitTar_0100";
260     try {
261         TestManager tm("Installd_Un_Tar_File_UnSplitTar_0100");
262         string rootPath = tm.GetRootDirCurTest();
263         vector<string> splitTarList = CreateSplitTar(rootPath, "testTarFile");
264         EXPECT_FALSE(splitTarList.empty());
265 
266         installd::UnTarFile unTarFile(nullptr);
267         for (auto& tarName : splitTarList) {
268             GTEST_LOG_(INFO) << "InstalldUnTarFileTest-begin tarName: " << tarName;
269             auto ret = unTarFile.UnSplitTar(tarName, rootPath);
270             EXPECT_EQ(ret, 0);
271         }
272     } catch (...) {
273         EXPECT_TRUE(false);
274         GTEST_LOG_(INFO) << "InstalldUnTarFileTest-an exception occurred by InstalldUnTarFile.";
275     }
276     GTEST_LOG_(INFO) << "InstalldUnTarFileTest-end Installd_Un_Tar_File_UnSplitTar_0100";
277 }
278 
279 /**
280  * @tc.number: Installd_Un_Tar_File_FreePointer_0100
281  * @tc.name: Installd_Un_Tar_File_FreePointer_0100
282  * @tc.desc: test File_FreePointer method
283  * @tc.size: MEDIUM
284  * @tc.type: FUNC
285  * @tc.level Level 1
286  * @tc.require: IC15LE
287  */
288 HWTEST_F(InstalldUnTarFileTest, Installd_Un_Tar_File_FreePointer_0100, testing::ext::TestSize.Level1)
289 {
290     GTEST_LOG_(INFO) << "InstalldUnTarFileTest-begin Installd_Un_Tar_File_FreePointer_0100";
291     try {
292         installd::ParseTarPath parseTarPath = {};
293         parseTarPath.fullPath = (char *)malloc(sizeof(char));
294         parseTarPath.longName = (char *)malloc(sizeof(char));
295         parseTarPath.longLink = (char *)malloc(sizeof(char));
296         installd::UnTarFile unTarFile(nullptr);
297         unTarFile.FreePointer(&parseTarPath);
298         EXPECT_TRUE(parseTarPath.fullPath == nullptr);
299         EXPECT_TRUE(parseTarPath.longName == nullptr);
300         EXPECT_TRUE(parseTarPath.longLink == nullptr);
301     } catch (...) {
302         EXPECT_TRUE(false);
303         GTEST_LOG_(INFO) << "InstalldUnTarFileTest-an exception occurred by InstalldUnTarFile.";
304     }
305     GTEST_LOG_(INFO) << "InstalldUnTarFileTest-end Installd_Un_Tar_File_FreePointer_0100";
306 }
307 
308 /**
309  * @tc.number: Installd_Un_Tar_File_FreeLongTypePointer_0100
310  * @tc.name: Installd_Un_Tar_File_FreeLongTypePointer_0100
311  * @tc.desc: test File_FreeLongTypePointer method
312  * @tc.size: MEDIUM
313  * @tc.type: FUNC
314  * @tc.level Level 1
315  * @tc.require: IC15LE
316  */
317 HWTEST_F(InstalldUnTarFileTest, Installd_Un_Tar_File_FreeLongTypePointer_0100, testing::ext::TestSize.Level1)
318 {
319     GTEST_LOG_(INFO) << "InstalldUnTarFileTest-begin Installd_Un_Tar_File_FreeLongTypePointer_0100";
320     try {
321         installd::ParseTarPath parseTarPath = {};
322         parseTarPath.longName = (char *)malloc(sizeof(char));
323         parseTarPath.longLink = (char *)malloc(sizeof(char));
324         installd::UnTarFile unTarFile(nullptr);
325         unTarFile.FreeLongTypePointer(&parseTarPath);
326         EXPECT_TRUE(parseTarPath.longName == nullptr);
327         EXPECT_TRUE(parseTarPath.longLink == nullptr);
328     } catch (...) {
329         EXPECT_TRUE(false);
330         GTEST_LOG_(INFO) << "InstalldUnTarFileTest-an exception occurred by InstalldUnTarFile.";
331     }
332     GTEST_LOG_(INFO) << "InstalldUnTarFileTest-end Installd_Un_Tar_File_FreeLongTypePointer_0100";
333 }
334 } // namespace OHOS::FileManagement::Backup