1 /*
2  * Copyright (c) 2022-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 <gtest/gtest.h>
16 #include <memory>
17 #include <thread>
18 
19 #include "zip.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace LIBZIP {
24 using namespace testing::ext;
25 
26 namespace {
27 const std::string BASE_PATH = "/data/app/el2/100/base/";
28 const std::string APP_PATH = "com.example.zlib/com.example.zlib/com.example.zlib.MainAbility/files/";
29 const std::string TEST_ZIP_OK = "/data/test/resource/bms/test_zip/resourceManagerTest.hap";
30 const std::string TEST_ZIP_DMAGED = "/data/test/resource/bms/test_zip/ohos_test.xml";
31 }  // namespac
32 class ZipTest : public testing::Test {
33 public:
ZipTest()34     ZipTest()
35     {}
~ZipTest()36     ~ZipTest()
37     {}
38 
39     static void SetUpTestCase(void);
40     static void TearDownTestCase(void);
41     void SetUp();
42     void TearDown();
43 };
44 
SetUpTestCase(void)45 void ZipTest::SetUpTestCase(void)
46 {}
47 
TearDownTestCase(void)48 void ZipTest::TearDownTestCase(void)
49 {}
50 
SetUp()51 void ZipTest::SetUp()
52 {}
53 
TearDown()54 void ZipTest::TearDown()
55 {}
56 
ZipCallBack(int result)57 void ZipCallBack(int result)
58 {
59     printf("--Zip--callback--result=%d--\n", result);
60 }
UnzipCallBack(int result)61 void UnzipCallBack(int result)
62 {
63     printf("--UnZip--callback--result=%d--\n", result);
64 }
65 
66 /**
67  * @tc.number: APPEXECFWK_LIBZIP_zip_0100_8file
68  * @tc.name: zip_0100_8file
69  * @tc.desc:
70  */
71 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_zip_0100_8file, Function | MediumTest | Level1)
72 {
73     std::string src = BASE_PATH + APP_PATH + "test";
74     std::string dest = BASE_PATH + APP_PATH + "result/8file.zip";
75 
76     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
77     OPTIONS options;
78     auto ret = Zip(src, dest, options, false, zlibCallbackInfo);
79     EXPECT_TRUE(ret);
80     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
81 }
82 
83 /**
84  * @tc.number: APPEXECFWK_LIBZIP_zip_0200_1file
85  * @tc.name: zip_0200_1file
86  * @tc.desc:
87  */
88 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_zip_0200_1file, Function | MediumTest | Level1)
89 {
90     std::string src = BASE_PATH + APP_PATH + "test/01";
91     std::string dest = BASE_PATH + APP_PATH + "result/1file.zip";
92 
93     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
94     OPTIONS options;
95     auto ret = Zip(src, dest, options, false, zlibCallbackInfo);
96     EXPECT_TRUE(ret);
97     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
98 }
99 
100 /**
101  * @tc.number: APPEXECFWK_LIBZIP_zip_0100_zip1file
102  * @tc.name: zip_0100_zip1file
103  * @tc.desc:
104  */
105 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_zip_0100_zip1file, Function | MediumTest | Level1)
106 {
107     std::string src = BASE_PATH + APP_PATH + "test/01/zip1.txt";
108     std::string dest = BASE_PATH + APP_PATH + "result/zip1file.zip";
109 
110     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
111     OPTIONS options;
112     auto ret = Zip(src, dest, options, false, zlibCallbackInfo);
113     EXPECT_TRUE(ret);
114     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
115 }
116 
117 /**
118  * @tc.number: APPEXECFWK_LIBZIP_unzip_0100_8file
119  * @tc.name: unzip_0100_8file
120  * @tc.desc:
121  */
122 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_unzip_0100_8file, Function | MediumTest | Level1)
123 {
124     std::string src = BASE_PATH + APP_PATH + "result/8file.zip";
125     std::string dest = BASE_PATH + APP_PATH + "unzip/01";
126 
127     OPTIONS options;
128     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
129     auto ret = Unzip(src, dest, options, zlibCallbackInfo);
130     EXPECT_FALSE(ret);
131     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
132 }
133 /**
134  * @tc.number: APPEXECFWK_LIBZIP_unzip_single_0200_1file
135  * @tc.name: unzip_single_0200_1file
136  * @tc.desc:
137  */
138 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_unzip_single_0200_1file, Function | MediumTest | Level1)
139 {
140     std::string src = BASE_PATH + APP_PATH + "result/1file.zip";
141     std::string dest = BASE_PATH + APP_PATH + "unzip/02";
142 
143     OPTIONS options;
144     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
145     auto ret = Unzip(src, dest, options, zlibCallbackInfo);
146     EXPECT_FALSE(ret);
147     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
148 }
149 /**
150  * @tc.number: APPEXECFWK_LIBZIP_zip_0100_zip1file
151  * @tc.name: zip_0100_zip1file
152  * @tc.desc:
153  */
154 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_unzip_0100_zip1file, Function | MediumTest | Level1)
155 {
156     std::string src = BASE_PATH + APP_PATH + "result/zip1file.zip";
157     std::string dest = BASE_PATH + APP_PATH + "unzip/zip1file";
158 
159     OPTIONS options;
160     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
161     auto ret = Unzip(src, dest, options, zlibCallbackInfo);
162     EXPECT_FALSE(ret);
163     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
164 }
165 
166 /**
167  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0100
168  * @tc.name: Checkzip_0100
169  * @tc.desc:
170  */
171 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0100, Function | MediumTest | Level1)
172 {
173     std::string src = BASE_PATH + APP_PATH + "test";
174     std::string dest = BASE_PATH + APP_PATH + "check";
175     FilePath destFile(dest);
176 
177     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
178     OPTIONS options;
179     auto ret = Zip(src, dest, options, false, zlibCallbackInfo);
180     destFile.CheckDestDirTail();
181     FilePath newDestFile(destFile.CheckDestDirTail());
182     std::cout << newDestFile.Value() << std::endl;
183     EXPECT_TRUE(ret);
184     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
185 }
186 
187 /**
188  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0200
189  * @tc.name: Checkzip_0200
190  * @tc.desc:
191  */
192 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0200, Function | MediumTest | Level1)
193 {
194     std::string src = BASE_PATH + APP_PATH + "test";
195     std::string dest = BASE_PATH + APP_PATH + "error/check.zip";
196 
197     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
198     OPTIONS options;
199     auto ret = Zip(src, dest, options, false, zlibCallbackInfo);
200     EXPECT_TRUE(ret);
201     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
202 }
203 
204 /**
205  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0300
206  * @tc.name: Checkzip_0300
207  * @tc.desc:
208  */
209 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0300, Function | MediumTest | Level1)
210 {
211     std::string src = BASE_PATH + APP_PATH + "error";
212     std::string src1 = BASE_PATH + APP_PATH + "#%#@$%";
213     std::string src2 = BASE_PATH + APP_PATH + "error/error1";
214     std::string dest = BASE_PATH + APP_PATH + "/check.zip";
215 
216     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
217     OPTIONS options;
218     auto ret1 = Zip(src, dest, options, false, zlibCallbackInfo);
219     auto ret2 = Zip(src1, dest, options, false, zlibCallbackInfo);
220     auto ret3 = Zip(src2, dest, options, false, zlibCallbackInfo);
221     EXPECT_TRUE(ret1);
222     EXPECT_TRUE(ret2);
223     EXPECT_TRUE(ret3);
224     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
225 }
226 
227 /**
228  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0400
229  * @tc.name: Checkzip_0400
230  * @tc.desc:
231  */
232 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0400, Function | MediumTest | Level1)
233 {
234     std::string src1 = BASE_PATH + APP_PATH + "error.txt";
235     std::string dest = BASE_PATH + APP_PATH + "check.zip";
236     FilePath srcFile1(src1);
237 
238     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
239     OPTIONS options;
240     auto ret = Zip(src1, dest, options, false, zlibCallbackInfo);
241     EXPECT_TRUE(ret);
242     std::cout << "srcFile1  DirName: " << srcFile1.DirName().Value() << std::endl;
243     std::cout << "srcFile1  Value:   " << srcFile1.Value() << std::endl;
244     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
245 }
246 
247 /**
248  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0500
249  * @tc.name: Checkzip_0500
250  * @tc.desc:
251  */
252 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0500, Function | MediumTest | Level1)
253 {
254     std::string src = BASE_PATH + APP_PATH + "error";
255     std::string dest = BASE_PATH + APP_PATH + "error1";
256 
257     OPTIONS options;
258     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
259     auto ret = Unzip(src, dest, options, zlibCallbackInfo);
260     EXPECT_FALSE(ret);
261     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
262 }
263 
264 /**
265  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0600
266  * @tc.name: Checkzip_0600
267  * @tc.desc:
268  */
269 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0600, Function | MediumTest | Level1)
270 {
271     std::string src = BASE_PATH + APP_PATH + "test";
272     std::string dest = "";
273 
274     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
275     OPTIONS options;
276     auto ret = Zip(src, dest, options, false, zlibCallbackInfo);
277     EXPECT_FALSE(ret);
278     std::cout << dest << std::endl;
279     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
280 }
281 
282 /**
283  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0700
284  * @tc.name: Checkzip_0700
285  * @tc.desc:
286  */
287 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0700, Function | MediumTest | Level1)
288 {
289     std::string src = "";
290     std::string dest = BASE_PATH + APP_PATH;
291     FilePath destFile(dest);
292 
293     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
294     OPTIONS options;
295     auto ret = Zip(src, dest, options, false, zlibCallbackInfo);
296     EXPECT_FALSE(ret);
297     destFile.CheckDestDirTail();
298     FilePath newDestFile(destFile.CheckDestDirTail());
299     std::cout << newDestFile.Value() << std::endl;
300     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
301 }
302 
303 /**
304  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0800
305  * @tc.name: Checkzip_0800
306  * @tc.desc:
307  */
308 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0800, Function | MediumTest | Level1)
309 {
310     std::string src = "";
311     std::string dest = BASE_PATH + APP_PATH;
312 
313     OPTIONS options;
314     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
315     auto ret = Unzip(src, dest, options, zlibCallbackInfo);
316     EXPECT_FALSE(ret);
317     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
318 }
319 
320 /**
321  * @tc.number: APPEXECFWK_LIBZIP_GetOriginalSize_0100
322  * @tc.name: GetOriginalSize_0100
323  * @tc.desc:
324  */
325 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_GetOriginalSize_0100, Function | MediumTest | Level1)
326 {
327     std::string src = "";
328     int64_t originalSize = 0;
329 
330     auto ret = GetOriginalSize(src, originalSize);
331     EXPECT_EQ(ret, ERR_ZLIB_SRC_FILE_DISABLED);
332     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
333 }
334 
335 /**
336  * @tc.number: APPEXECFWK_LIBZIP_GetOriginalSize_0200
337  * @tc.name: GetOriginalSize_0200
338  * @tc.desc:
339  */
340 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_GetOriginalSize_0200, Function | MediumTest | Level1)
341 {
342     std::string src = BASE_PATH + APP_PATH + "result/8file.zip";
343     int64_t originalSize = 0;
344 
345     auto ret = GetOriginalSize(src, originalSize);
346     EXPECT_EQ(ret, ERR_ZLIB_SRC_FILE_DISABLED);
347     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
348 }
349 
350 /**
351  * @tc.number: APPEXECFWK_LIBZIP_GetOriginalSize_0300
352  * @tc.name: GetOriginalSize_0300
353  * @tc.desc:
354  */
355 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_GetOriginalSize_0300, Function | MediumTest | Level1)
356 {
357     std::string src = TEST_ZIP_DMAGED;
358     int64_t originalSize = 0;
359 
360     auto ret = GetOriginalSize(src, originalSize);
361     EXPECT_EQ(ret, ERR_ZLIB_SRC_FILE_FORMAT_ERROR);
362     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
363 }
364 
365 /**
366  * @tc.number: APPEXECFWK_LIBZIP_GetOriginalSize_0400
367  * @tc.name: GetOriginalSize_0400
368  * @tc.desc:
369  */
370 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_GetOriginalSize_0400, Function | MediumTest | Level1)
371 {
372     std::string src = TEST_ZIP_OK;
373     int64_t originalSize = 0;
374 
375     auto ret = GetOriginalSize(src, originalSize);
376     EXPECT_EQ(ret, ERR_OK);
377     EXPECT_GT(originalSize, 0);
378     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
379 }
380 }  // namespace LIBZIP
381 }  // namespace AppExecFwk
382 }  // namespace OHOS
383