1 /*
2  * Copyright (c) 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 
16 #include "getglobalfileinfo_fuzzer.h"
17 #include "libpandafile/file.h"
18 #include "libziparchive/zip_archive.h"
19 
20 namespace OHOS {
GetGlobalFileInfoFuzzTest(const uint8_t* data, size_t size)21     void GetGlobalFileInfoFuzzTest(const uint8_t* data, size_t size)
22     {
23         {
24             // handle is nullptr
25             panda::ZipArchiveHandle handle = nullptr;
26             panda::GlobalStat gi = panda::GlobalStat();
27             if (panda::GetGlobalFileInfo(handle, &gi) != 0) {
28                 return;
29             }
30         }
31 
32         {
33             // handle is not nullptr
34             // Create zip file
35             const char* filename = "classes1.abc";
36             const char* zipname = "__OpenPandaFileFuzzTest.zip";
37             int ret =
38                 panda::CreateOrAddFileIntoZip(zipname, filename, data, size, APPEND_STATUS_CREATE,
39                                               Z_BEST_COMPRESSION);
40             if (ret != 0) {
41                 return;
42             }
43             // Quick Check
44             panda::ZipArchiveHandle handle = nullptr;
45             if (panda::OpenArchive(handle, zipname) != 0) {
46                 return;
47             }
48             panda::GlobalStat gi = panda::GlobalStat();
49             if (panda::GetGlobalFileInfo(handle, &gi) != 0) {
50                 return;
51             }
52             panda::CloseArchive(handle);
53             (void)remove(zipname);
54         }
55     }
56 }
57 
58 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)59 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
60 {
61     /* Run your code on data */
62     OHOS::GetGlobalFileInfoFuzzTest(data, size);
63     return 0;
64 }