1 /*
2 * Copyright (c) 2022 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 "openpandafileorzip_fuzzer.h"
17 #include "libpandafile/file.h"
18 #include "libziparchive/zip_archive.h"
19
20 namespace OHOS {
OpenPandaFileOrZipFuzzTest(const uint8_t * data,size_t size)21 void OpenPandaFileOrZipFuzzTest(const uint8_t *data, size_t size)
22 {
23 const char *filename1 = panda::panda_file::ARCHIVE_FILENAME;
24 const char *filename2 = "classes1.abc";
25 // Create uncompressed zip file
26 const char *uncompress_zip_filename = "__OpenPandaFileOrZipFuzzTest_uncompress.zip";
27 int ret1 = panda::CreateOrAddFileIntoZip(uncompress_zip_filename, filename1, data, size, APPEND_STATUS_CREATE,
28 Z_NO_COMPRESSION);
29 int ret2 = panda::CreateOrAddFileIntoZip(uncompress_zip_filename, filename2, data, size, APPEND_STATUS_ADDINZIP,
30 Z_NO_COMPRESSION);
31 if (ret1 != 0 || ret2 != 0) {
32 (void)remove(uncompress_zip_filename);
33 return;
34 }
35 // Create compressed zip file
36 const char *compressed_zip_filename = "__OpenPandaFileOrZipFuzzTest_compressed.zip";
37 ret1 = panda::CreateOrAddFileIntoZip(uncompress_zip_filename, filename1, data, size, APPEND_STATUS_CREATE,
38 Z_BEST_COMPRESSION);
39 ret2 = panda::CreateOrAddFileIntoZip(uncompress_zip_filename, filename2, data, size, APPEND_STATUS_ADDINZIP,
40 Z_BEST_COMPRESSION);
41 if (ret1 != 0 || ret2 != 0) {
42 (void)remove(compressed_zip_filename);
43 return;
44 }
45
46 {
47 panda::panda_file::OpenPandaFileOrZip(uncompress_zip_filename);
48 panda::panda_file::OpenPandaFileOrZip(compressed_zip_filename);
49 }
50 (void)remove(uncompress_zip_filename);
51 (void)remove(compressed_zip_filename);
52 }
53 } // namespace OHOS
54
55 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)56 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
57 {
58 /* Run your code on data */
59 OHOS::OpenPandaFileOrZipFuzzTest(data, size);
60 return 0;
61 }
62