• 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     double point = 2.0;
42     int64_t i64 = 0;
43     size_t li = 0;
44     auto ri = size % u16m;
45     std::string s (reinterpret_cast<const char*>(data), ri);
46     vfi(s);
47     JsonUtil::ParseJsonString(s);
48     JsonUtil::ParseJsonString(s.c_str());
49     r->Put(jack.c_str(), vol1);
50     r->Replace(jack.c_str(), false);
51     r->Replace(jack.c_str(), vol2);
52     r->GetString(jack);
53     r->GetUInt(jack);
54     r->GetDouble(jack);
55     r->Replace(jack.c_str(), tom.c_str());
56     r->Replace(kel.c_str(), point);
57     r->Put(kal.c_str(), i64);
58     r->Put(kal.c_str(), li);
59     r->Delete(jack.c_str());
60     r->GetObject(kal);
61     r->GetBool(kal);
62     r->Replace(kal.c_str(), false);
63     r->Delete(kal.c_str());
64     return result;
65 }
66 
vfi(std::string & s)67 void vfi(std::string& s)
68 {
69     s = "{ key:123 }";
70 }
71 }
72 
73 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)74 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
75 {
76     /* Run your code on data */
77     OHOS::DoSomethingInterestingWithMyAPI(data, size);
78     return 0;
79 }