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 "parse_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <iostream>
21
22 #include "bundle_parser.h"
23
24 using namespace OHOS::AppExecFwk;
25 namespace OHOS {
26 namespace {
27 const int FILE_RETURN_SUCCESS = 0;
28 int retCode = 0;
29 }
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)30 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
31 {
32 if ((size <= 4) || (data == nullptr)) {
33 return false;
34 }
35
36 auto pFile = fopen("myHap.hap", "wb");
37 if (pFile == nullptr) {
38 std::cout<< "fopen hap error!";
39 return false;
40 }
41
42 retCode = fputs(reinterpret_cast<const char*>(data), pFile);
43 if (retCode != FILE_RETURN_SUCCESS) {
44 fclose(pFile);
45 return false;
46 }
47 retCode = fclose(pFile);
48 if (retCode != FILE_RETURN_SUCCESS) {
49 return false;
50 }
51 pFile = nullptr;
52 InnerBundleInfo info;
53 BundleParser bundleParser;
54 auto ret = bundleParser.Parse("myHap.hap", info);
55 if (ret != ERR_OK) {
56 return false;
57 }
58 return true;
59 }
60 }
61
62 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)63 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
64 {
65 /* Run your code on data */
66 OHOS::DoSomethingInterestingWithMyAPI(data, size);
67 return 0;
68 }