• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
135  * @tc.number: APPEXECFWK_LIBZIP_unzip_0100_8file_parallel
136  * @tc.name: unzip_0100_8file_parallel
137  * @tc.desc:
138  */
139 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_unzip_0100_8file_parallel, Function | MediumTest | Level1)
140 {
141     std::string src = BASE_PATH + APP_PATH + "result/8file.zip";
142     std::string dest = BASE_PATH + APP_PATH + "unzip/01";
143 
144     OPTIONS options;
145     options.parallel = PARALLEL_STRATEGY_PARALLEL_DECOMPRESSION;
146     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
147     auto ret = Unzip(src, dest, options, zlibCallbackInfo);
148     EXPECT_FALSE(ret);
149     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
150 }
151 
152 /**
153  * @tc.number: APPEXECFWK_LIBZIP_unzip_single_0200_1file
154  * @tc.name: unzip_single_0200_1file
155  * @tc.desc:
156  */
157 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_unzip_single_0200_1file, Function | MediumTest | Level1)
158 {
159     std::string src = BASE_PATH + APP_PATH + "result/1file.zip";
160     std::string dest = BASE_PATH + APP_PATH + "unzip/02";
161 
162     OPTIONS options;
163     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
164     auto ret = Unzip(src, dest, options, zlibCallbackInfo);
165     EXPECT_FALSE(ret);
166     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
167 }
168 
169 /**
170  * @tc.number: APPEXECFWK_LIBZIP_unzip_single_0200_1file_parallel
171  * @tc.name: unzip_single_0200_1file_parallel
172  * @tc.desc:
173  */
174 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_unzip_single_0200_1file_parallel, Function | MediumTest | Level1)
175 {
176     std::string src = BASE_PATH + APP_PATH + "result/1file.zip";
177     std::string dest = BASE_PATH + APP_PATH + "unzip/02";
178 
179     OPTIONS options;
180     options.parallel = PARALLEL_STRATEGY_PARALLEL_DECOMPRESSION;
181     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
182     auto ret = Unzip(src, dest, options, zlibCallbackInfo);
183     EXPECT_FALSE(ret);
184     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
185 }
186 
187 /**
188  * @tc.number: APPEXECFWK_LIBZIP_zip_0100_zip1file
189  * @tc.name: zip_0100_zip1file
190  * @tc.desc:
191  */
192 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_unzip_0100_zip1file, Function | MediumTest | Level1)
193 {
194     std::string src = BASE_PATH + APP_PATH + "result/zip1file.zip";
195     std::string dest = BASE_PATH + APP_PATH + "unzip/zip1file";
196 
197     OPTIONS options;
198     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
199     auto ret = Unzip(src, dest, options, zlibCallbackInfo);
200     EXPECT_FALSE(ret);
201     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
202 }
203 
204 /**
205  * @tc.number: APPEXECFWK_LIBZIP_zip_0100_zip1file_parallel
206  * @tc.name: zip_0100_zip1file_parallel
207  * @tc.desc:
208  */
209 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_unzip_0100_zip1file_parallel, Function | MediumTest | Level1)
210 {
211     std::string src = BASE_PATH + APP_PATH + "result/zip1file.zip";
212     std::string dest = BASE_PATH + APP_PATH + "unzip/zip1file";
213 
214     OPTIONS options;
215     options.parallel = PARALLEL_STRATEGY_PARALLEL_DECOMPRESSION;
216     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
217     auto ret = Unzip(src, dest, options, zlibCallbackInfo);
218     EXPECT_FALSE(ret);
219     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
220 }
221 
222 
223 /**
224  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0100
225  * @tc.name: Checkzip_0100
226  * @tc.desc:
227  */
228 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0100, Function | MediumTest | Level1)
229 {
230     std::string src = BASE_PATH + APP_PATH + "test";
231     std::string dest = BASE_PATH + APP_PATH + "check";
232     FilePath destFile(dest);
233 
234     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
235     OPTIONS options;
236     auto ret = Zip(src, dest, options, false, zlibCallbackInfo);
237     destFile.CheckDestDirTail();
238     FilePath newDestFile(destFile.CheckDestDirTail());
239     std::cout << newDestFile.Value() << std::endl;
240     EXPECT_TRUE(ret);
241     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
242 }
243 
244 /**
245  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0200
246  * @tc.name: Checkzip_0200
247  * @tc.desc:
248  */
249 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0200, Function | MediumTest | Level1)
250 {
251     std::string src = BASE_PATH + APP_PATH + "test";
252     std::string dest = BASE_PATH + APP_PATH + "error/check.zip";
253 
254     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
255     OPTIONS options;
256     auto ret = Zip(src, dest, options, false, zlibCallbackInfo);
257     EXPECT_TRUE(ret);
258     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
259 }
260 
261 /**
262  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0300
263  * @tc.name: Checkzip_0300
264  * @tc.desc:
265  */
266 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0300, Function | MediumTest | Level1)
267 {
268     std::string src = BASE_PATH + APP_PATH + "error";
269     std::string src1 = BASE_PATH + APP_PATH + "#%#@$%";
270     std::string src2 = BASE_PATH + APP_PATH + "error/error1";
271     std::string dest = BASE_PATH + APP_PATH + "/check.zip";
272 
273     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
274     OPTIONS options;
275     auto ret1 = Zip(src, dest, options, false, zlibCallbackInfo);
276     auto ret2 = Zip(src1, dest, options, false, zlibCallbackInfo);
277     auto ret3 = Zip(src2, dest, options, false, zlibCallbackInfo);
278     EXPECT_TRUE(ret1);
279     EXPECT_TRUE(ret2);
280     EXPECT_TRUE(ret3);
281     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
282 }
283 
284 /**
285  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0400
286  * @tc.name: Checkzip_0400
287  * @tc.desc:
288  */
289 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0400, Function | MediumTest | Level1)
290 {
291     std::string src1 = BASE_PATH + APP_PATH + "error.txt";
292     std::string dest = BASE_PATH + APP_PATH + "check.zip";
293     FilePath srcFile1(src1);
294 
295     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
296     OPTIONS options;
297     auto ret = Zip(src1, dest, options, false, zlibCallbackInfo);
298     EXPECT_TRUE(ret);
299     std::cout << "srcFile1  DirName: " << srcFile1.DirName().Value() << std::endl;
300     std::cout << "srcFile1  Value:   " << srcFile1.Value() << std::endl;
301     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
302 }
303 
304 /**
305  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0500
306  * @tc.name: Checkzip_0500
307  * @tc.desc:
308  */
309 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0500, Function | MediumTest | Level1)
310 {
311     std::string src = BASE_PATH + APP_PATH + "error";
312     std::string dest = BASE_PATH + APP_PATH + "error1";
313 
314     OPTIONS options;
315     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
316     auto ret = Unzip(src, dest, options, zlibCallbackInfo);
317     EXPECT_FALSE(ret);
318     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
319 }
320 
321 /**
322  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0500_parallel
323  * @tc.name: Checkzip_0500_parallel
324  * @tc.desc:
325  */
326 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0500_parallel, Function | MediumTest | Level1)
327 {
328     std::string src = BASE_PATH + APP_PATH + "error";
329     std::string dest = BASE_PATH + APP_PATH + "error1";
330 
331     OPTIONS options;
332     options.parallel = PARALLEL_STRATEGY_PARALLEL_DECOMPRESSION;
333     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
334     auto ret = Unzip(src, dest, options, zlibCallbackInfo);
335     EXPECT_FALSE(ret);
336     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
337 }
338 
339 
340 /**
341  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0600
342  * @tc.name: Checkzip_0600
343  * @tc.desc:
344  */
345 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0600, Function | MediumTest | Level1)
346 {
347     std::string src = BASE_PATH + APP_PATH + "test";
348     std::string dest = "";
349 
350     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
351     OPTIONS options;
352     auto ret = Zip(src, dest, options, false, zlibCallbackInfo);
353     EXPECT_FALSE(ret);
354     std::cout << dest << std::endl;
355     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
356 }
357 
358 /**
359  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0700
360  * @tc.name: Checkzip_0700
361  * @tc.desc:
362  */
363 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0700, Function | MediumTest | Level1)
364 {
365     std::string src = "";
366     std::string dest = BASE_PATH + APP_PATH;
367     FilePath destFile(dest);
368 
369     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
370     OPTIONS options;
371     auto ret = Zip(src, dest, options, false, zlibCallbackInfo);
372     EXPECT_FALSE(ret);
373     destFile.CheckDestDirTail();
374     FilePath newDestFile(destFile.CheckDestDirTail());
375     std::cout << newDestFile.Value() << std::endl;
376     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
377 }
378 
379 /**
380  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0800
381  * @tc.name: Checkzip_0800
382  * @tc.desc:
383  */
384 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0800, Function | MediumTest | Level1)
385 {
386     std::string src = "";
387     std::string dest = BASE_PATH + APP_PATH;
388 
389     OPTIONS options;
390     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
391     auto ret = Unzip(src, dest, options, zlibCallbackInfo);
392     EXPECT_FALSE(ret);
393     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
394 }
395 
396 /**
397  * @tc.number: APPEXECFWK_LIBZIP_Checkzip_0800_parallel
398  * @tc.name: Checkzip_0800_parallel
399  * @tc.desc:
400  */
401 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_Checkzip_0800_parallel, Function | MediumTest | Level1)
402 {
403     std::string src = "";
404     std::string dest = BASE_PATH + APP_PATH;
405 
406     OPTIONS options;
407     options.parallel = PARALLEL_STRATEGY_PARALLEL_DECOMPRESSION;
408     std::shared_ptr<ZlibCallbackInfo> zlibCallbackInfo = std::make_shared<ZlibCallbackInfo>();
409     auto ret = Unzip(src, dest, options, zlibCallbackInfo);
410     EXPECT_FALSE(ret);
411     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
412 }
413 
414 /**
415  * @tc.number: APPEXECFWK_LIBZIP_GetOriginalSize_0100
416  * @tc.name: GetOriginalSize_0100
417  * @tc.desc:
418  */
419 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_GetOriginalSize_0100, Function | MediumTest | Level1)
420 {
421     std::string src = "";
422     int64_t originalSize = 0;
423 
424     auto ret = GetOriginalSize(src, originalSize);
425     EXPECT_EQ(ret, ERR_ZLIB_SRC_FILE_DISABLED);
426     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
427 }
428 
429 /**
430  * @tc.number: APPEXECFWK_LIBZIP_GetOriginalSize_0200
431  * @tc.name: GetOriginalSize_0200
432  * @tc.desc:
433  */
434 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_GetOriginalSize_0200, Function | MediumTest | Level1)
435 {
436     std::string src = BASE_PATH + APP_PATH + "result/8file.zip";
437     int64_t originalSize = 0;
438 
439     auto ret = GetOriginalSize(src, originalSize);
440     EXPECT_EQ(ret, ERR_ZLIB_SRC_FILE_DISABLED);
441     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
442 }
443 
444 /**
445  * @tc.number: APPEXECFWK_LIBZIP_GetOriginalSize_0300
446  * @tc.name: GetOriginalSize_0300
447  * @tc.desc:
448  */
449 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_GetOriginalSize_0300, Function | MediumTest | Level1)
450 {
451     std::string src = TEST_ZIP_DMAGED;
452     int64_t originalSize = 0;
453 
454     auto ret = GetOriginalSize(src, originalSize);
455     EXPECT_EQ(ret, ERR_ZLIB_SRC_FILE_FORMAT_ERROR);
456     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
457 }
458 
459 /**
460  * @tc.number: APPEXECFWK_LIBZIP_GetOriginalSize_0400
461  * @tc.name: GetOriginalSize_0400
462  * @tc.desc:
463  */
464 HWTEST_F(ZipTest, APPEXECFWK_LIBZIP_GetOriginalSize_0400, Function | MediumTest | Level1)
465 {
466     std::string src = TEST_ZIP_OK;
467     int64_t originalSize = 0;
468 
469     auto ret = GetOriginalSize(src, originalSize);
470     EXPECT_EQ(ret, ERR_OK);
471     EXPECT_GT(originalSize, 0);
472     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
473 }
474 }  // namespace LIBZIP
475 }  // namespace AppExecFwk
476 }  // namespace OHOS
477