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