• 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 "res_fuzzer.h"
17 
18 #include <string>
19 
20 #include "cJSON.h"
21 #include "json_util.h"
22 #include "bridge/common/dom/dom_type.h"
23 
24 const uint32_t u16m = 65535;
25 
26 namespace OHOS {
27 using namespace OHOS::Ace;
28 using namespace std;
29 void vfi(std::string& s);
30 string jack = "jack";
31 string tom = "tom";
32 string kal = "kal";
33 string kel = "kel";
34 string kol = "kol";
35 constexpr int32_t vol1 = 0;
36 constexpr int32_t vol2 = 1;
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)37 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
38 {
39     bool result = false;
40     auto r = JsonUtil::Create(true);
41     auto r1 = JsonUtil::Create(false);
42     double point = 2.0;
43     int64_t i64 = 0;
44     size_t li = 0;
45     auto ri = size % u16m;
46     std::string s (reinterpret_cast<const char*>(data), ri);
47     vfi(s);
48     JsonUtil::ParseJsonString(s);
49     JsonUtil::ParseJsonString(s.c_str());
50     JsonUtil::CreateArray(true);
51     JsonUtil::CreateArray(false);
52     r->Put(jack.c_str(), vol1);
53     r->Replace(jack.c_str(), false);
54     r->Replace(jack.c_str(), vol2);
55     r->GetString(jack);
56     r->GetUInt(jack);
57     r->GetDouble(jack);
58     r->Replace(jack.c_str(), tom.c_str());
59     r->Replace(kel.c_str(), point);
60     r->Put(kal.c_str(), i64);
61     r->Put(kal.c_str(), li);
62     r->Delete(jack.c_str());
63     r->GetObject(kal);
64     r->GetBool(kal);
65     auto r2 = Ace::JsonValue(cJSON_CreateObject());
66     return result;
67 }
68 
vfi(std::string & s)69 void vfi(std::string& s)
70 {
71     s = "{ key:123 }";
72 }
73 }
74 
75 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
77 {
78     /* Run your code on data */
79     OHOS::DoSomethingInterestingWithMyAPI(data, size);
80     return 0;
81 }