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 "test_manager.h"
22 #include "untar_file.h"
23
24 namespace OHOS::FileManagement::Backup {
25 using namespace std;
26 using namespace testing;
27
28 class UntarFileTest : 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 UntarFileTest::SetUpTestCase()
37 {
38 GTEST_LOG_(INFO) << "SetUpTestCase enter";
39 }
40
TearDownTestCase()41 void UntarFileTest::TearDownTestCase()
42 {
43 GTEST_LOG_(INFO) << "TearDownTestCase enter";
44 }
45
ClearCache()46 static void ClearCache()
47 {
48 UntarFile::GetInstance().rootPath_.clear();
49 UntarFile::GetInstance().tarFileSize_ = 0;
50 UntarFile::GetInstance().tarFileBlockCnt_ = 0;
51 UntarFile::GetInstance().pos_ = 0;
52 UntarFile::GetInstance().readCnt_ = 0;
53 if (UntarFile::GetInstance().tarFilePtr_ != nullptr) {
54 fclose(UntarFile::GetInstance().tarFilePtr_);
55 UntarFile::GetInstance().tarFilePtr_ = nullptr;
56 }
57 }
58
59 /**
60 * @tc.number: SUB_Untar_File_GetInstance_0100
61 * @tc.name: SUB_Untar_File_GetInstance_0100
62 * @tc.desc: 测试 GetInstance 接口
63 * @tc.size: MEDIUM
64 * @tc.type: FUNC
65 * @tc.level Level 1
66 * @tc.require: I6F3GV
67 */
68 HWTEST_F(UntarFileTest, SUB_Untar_File_GetInstance_0100, testing::ext::TestSize.Level1)
69 {
70 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_GetInstance_0100";
71 try {
72 UntarFile::GetInstance();
73 EXPECT_TRUE(true);
74 } catch (...) {
75 EXPECT_TRUE(false);
76 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
77 }
78 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_GetInstance_0100";
79 }
80
81 /**
82 * @tc.number: SUB_Untar_File_UnPacket_0100
83 * @tc.name: SUB_Untar_File_UnPacket_0100
84 * @tc.desc: 测试 UnPacket 接口
85 * @tc.size: MEDIUM
86 * @tc.type: FUNC
87 * @tc.level Level 1
88 * @tc.require: I6F3GV
89 */
90 HWTEST_F(UntarFileTest, SUB_Untar_File_UnPacket_0100, testing::ext::TestSize.Level1)
91 {
92 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_UnPacket_0100";
93 try {
94 string tarFile("");
95 string rootPath("");
96 int ret = UntarFile::GetInstance().UnPacket(tarFile, rootPath);
97 EXPECT_EQ(ret, ENOENT);
98 ClearCache();
99 } catch (...) {
100 EXPECT_TRUE(false);
101 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
102 }
103 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_UnPacket_0100";
104 }
105
106 /**
107 * @tc.number: SUB_Untar_File_UnPacket_0200
108 * @tc.name: SUB_Untar_File_UnPacket_0200
109 * @tc.desc: 测试 UnPacket 接口
110 * @tc.size: MEDIUM
111 * @tc.type: FUNC
112 * @tc.level Level 1
113 * @tc.require: I6F3GV
114 */
115 HWTEST_F(UntarFileTest, SUB_Untar_File_UnPacket_0200, testing::ext::TestSize.Level1)
116 {
117 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_UnPacket_0200";
118 try {
119 // 预置文件和目录
120 TestManager tm("SUB_Untar_File_UnPacket_0200");
121 string root = tm.GetRootDirCurTest();
122 string testDir = root + "/testdir";
123 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
124 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
125 throw BError(errno);
126 }
127 string aFile = testDir + "/a.txt";
128 string bFile = testDir + "/b.txt";
129 SaveStringToFile(aFile, "hello");
130 SaveStringToFile(bFile, "world");
131
132 string tarFile = root + "test.tar";
133 string cmd = "tar -cvf " + tarFile + " " + testDir;
134 if (system(cmd.c_str()) != 0) {
135 GTEST_LOG_(INFO) << " execute tar failure, errno :" << errno;
136 throw BError(errno);
137 }
138
139 string rootPath(root);
140 int ret = UntarFile::GetInstance().UnPacket(tarFile, rootPath);
141 EXPECT_EQ(ret, 0);
142 ClearCache();
143 } catch (...) {
144 EXPECT_TRUE(false);
145 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
146 }
147 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_UnPacket_0200";
148 }
149
150 /**
151 * @tc.number: SUB_Untar_File_UnPacket_0300
152 * @tc.name: SUB_Untar_File_UnPacket_0300
153 * @tc.desc: 测试 UnPacket 接口
154 * @tc.size: MEDIUM
155 * @tc.type: FUNC
156 * @tc.level Level 1
157 * @tc.require: I6F3GV
158 */
159 HWTEST_F(UntarFileTest, SUB_Untar_File_UnPacket_0300, testing::ext::TestSize.Level1)
160 {
161 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_UnPacket_0300";
162 try {
163 // 预置文件和目录
164 TestManager tm("SUB_Untar_File_UnPacket_0300");
165 string root = tm.GetRootDirCurTest();
166 string testDir = root + "/testdir";
167 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
168 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
169 throw BError(errno);
170 }
171 string aFile = testDir + "/a.txt";
172 SaveStringToFile(aFile, "hello");
173
174 string rootPath(root);
175 int ret = UntarFile::GetInstance().UnPacket(aFile, rootPath);
176 EXPECT_EQ(ret, 0);
177 ClearCache();
178 } catch (...) {
179 EXPECT_TRUE(false);
180 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
181 }
182 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_UnPacket_0300";
183 }
184
185 /**
186 * @tc.number: SUB_Untar_File_UnPacket_0400
187 * @tc.name: SUB_Untar_File_UnPacket_0400
188 * @tc.desc: 测试 UnPacket 接口
189 * @tc.size: MEDIUM
190 * @tc.type: FUNC
191 * @tc.level Level 1
192 * @tc.require: I6F3GV
193 */
194 HWTEST_F(UntarFileTest, SUB_Untar_File_UnPacket_0400, testing::ext::TestSize.Level1)
195 {
196 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_UnPacket_0400";
197 try {
198 // 预置文件和目录
199 TestManager tm("SUB_Untar_File_UnPacket_0400");
200 string root = tm.GetRootDirCurTest();
201 string testDir = root + "/testdir";
202 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
203 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
204 throw BError(errno);
205 }
206 string tarFile = root + "test.tar";
207 string cmd = "tar -cvf " + tarFile + " " + testDir;
208 if (system(cmd.c_str()) != 0) {
209 GTEST_LOG_(INFO) << " execute tar failure, errno :" << errno;
210 throw BError(errno);
211 }
212
213 string rootPath(root);
214 int ret = UntarFile::GetInstance().UnPacket(tarFile, rootPath);
215 EXPECT_EQ(ret, 0);
216 ClearCache();
217 } catch (...) {
218 EXPECT_TRUE(false);
219 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
220 }
221 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_UnPacket_0400";
222 }
223 } // namespace OHOS::FileManagement::Backup