• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "parsesyscap_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 
26 namespace OHOS {
27     namespace {
28         const int FILE_RETURN_SUCCESS = 0;
29         int retCode = 0;
30     }
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)31     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
32     {
33         if ((size <= 4) || (data == nullptr)) {
34             return false;
35         }
36 
37         auto pFile = fopen("rpcid.sc", "wb");
38         if (pFile == nullptr) {
39             std::cout<< "fopen hap error!";
40             return false;
41         }
42 
43         std::string info (reinterpret_cast<const char*>(data), size);
44         retCode = fputs(info.c_str(), pFile);
45         if (retCode != FILE_RETURN_SUCCESS) {
46             fclose(pFile);
47             return false;
48         }
49         retCode = fclose(pFile);
50         if (retCode != FILE_RETURN_SUCCESS) {
51             return false;
52         }
53         pFile = nullptr;
54         std::vector<std::string> sysCaps;
55         BundleParser bundleParser;
56         auto ret = bundleParser.ParseSysCap("rpcid.sc", sysCaps);
57         if (ret != ERR_OK) {
58             return false;
59         }
60         return true;
61     }
62 }
63 
64 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)65 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
66 {
67     /* Run your code on data */
68     OHOS::DoSomethingInterestingWithMyAPI(data, size);
69     return 0;
70 }