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 "typesutil_fuzzer.h"
17 
18 #include <cstdint>
19 #include <variant>
20 #include <vector>
21 
22 #include "change_notification.h"
23 #include "itypes_util.h"
24 #include "kv_types_util.h"
25 #include "types.h"
26 
27 using namespace OHOS::DistributedKv;
28 namespace OHOS {
ClientDevFuzz(const std::string & strBase)29 void ClientDevFuzz(const std::string &strBase)
30 {
31     DeviceInfo clientDev;
32     clientDev.deviceId = strBase;
33     clientDev.deviceName = strBase;
34     clientDev.deviceType = strBase;
35     MessageParcel parcel;
36     ITypesUtil::Marshal(parcel, clientDev);
37     DeviceInfo serverDev;
38     ITypesUtil::Unmarshal(parcel, serverDev);
39 }
40 
EntryFuzz(const std::string & strBase)41 void EntryFuzz(const std::string &strBase)
42 {
43     Entry entryIn;
44     entryIn.key = strBase;
45     entryIn.value = strBase;
46     MessageParcel parcel;
47     ITypesUtil::Marshal(parcel, entryIn);
48     Entry entryOut;
49     ITypesUtil::Unmarshal(parcel, entryOut);
50 }
51 
BlobFuzz(const std::string & strBase)52 void BlobFuzz(const std::string &strBase)
53 {
54     Blob blobIn = strBase;
55     MessageParcel parcel;
56     ITypesUtil::Marshal(parcel, blobIn);
57     Blob blobOut;
58     ITypesUtil::Unmarshal(parcel, blobOut);
59 }
60 
VecFuzz(const std::vector<uint8_t> & vec)61 void VecFuzz(const std::vector<uint8_t> &vec)
62 {
63     std::vector<uint8_t> vecIn(vec);
64     MessageParcel parcel;
65     ITypesUtil::Marshal(parcel, vecIn);
66     std::vector<uint8_t> vecOut;
67     ITypesUtil::Unmarshal(parcel, vecOut);
68 }
69 
OptionsFuzz(const std::string & strBase)70 void OptionsFuzz(const std::string &strBase)
71 {
72     Options optionsIn = {
73         .createIfMissing = true,
74         .encrypt = false,
75         .autoSync = true,
76         .kvStoreType = KvStoreType::SINGLE_VERSION
77     };
78     optionsIn.area = EL1;
79     optionsIn.baseDir = strBase;
80     MessageParcel parcel;
81     ITypesUtil::Marshal(parcel, optionsIn);
82     Options optionsOut;
83     ITypesUtil::Unmarshal(parcel, optionsOut);
84 }
85 
SyncPolicyFuzz(uint32_t base)86 void SyncPolicyFuzz(uint32_t base)
87 {
88     SyncPolicy syncPolicyIn { base, base };
89     MessageParcel parcel;
90     ITypesUtil::Marshal(parcel, syncPolicyIn);
91     SyncPolicy syncPolicyOut;
92     ITypesUtil::Unmarshal(parcel, syncPolicyOut);
93 }
94 
ChangeNotificationFuzz(const std::string & strBase,bool boolBase)95 void ChangeNotificationFuzz(const std::string &strBase, bool boolBase)
96 {
97     Entry insert;
98     Entry update;
99     Entry del;
100     insert.key = strBase;
101     update.key = strBase;
102     del.key = strBase;
103     insert.value = strBase;
104     update.value = strBase;
105     del.value = strBase;
106     std::vector<Entry> inserts;
107     std::vector<Entry> updates;
108     std::vector<Entry> deleteds;
109     inserts.push_back(insert);
110     updates.push_back(update);
111     deleteds.push_back(del);
112 
113     ChangeNotification changeIn(std::move(inserts), std::move(updates), std::move(deleteds), strBase, boolBase);
114     MessageParcel parcel;
115     ITypesUtil::Marshal(parcel, changeIn);
116     std::vector<Entry> empty;
117     ChangeNotification changeOut(std::move(empty), {}, {}, "", !boolBase);
118     ITypesUtil::Unmarshal(parcel, changeOut);
119 }
120 
IntFuzz(size_t valBase)121 void IntFuzz(size_t valBase)
122 {
123     MessageParcel parcel;
124     int32_t int32In = static_cast<int32_t>(valBase);
125     ITypesUtil::Marshal(parcel, int32In);
126     int32_t int32Out;
127     ITypesUtil::Unmarshal(parcel, int32Out);
128 
129     uint32_t uint32In = static_cast<uint32_t>(valBase);
130     ITypesUtil::Marshal(parcel, uint32In);
131     uint32_t uint32Out;
132     ITypesUtil::Unmarshal(parcel, uint32Out);
133 
134     uint64_t uint64In = static_cast<uint64_t>(valBase);
135     ITypesUtil::Marshal(parcel, uint64In);
136     uint64_t uint64Out;
137     ITypesUtil::Unmarshal(parcel, uint64Out);
138 }
139 
StringFuzz(const std::string & strBase)140 void StringFuzz(const std::string &strBase)
141 {
142     MessageParcel parcel;
143     std::string strIn = strBase;
144     ITypesUtil::Marshal(parcel, strIn);
145     std::string strOut;
146     ITypesUtil::Unmarshal(parcel, strOut);
147 }
148 
GetTotalSizeFuzz(const std::string & strBase,uint32_t size)149 void GetTotalSizeFuzz(const std::string &strBase, uint32_t size)
150 {
151     Entry entry;
152     entry.key = strBase;
153     entry.value = strBase;
154     std::vector<Entry> VecEntryIn(size, entry);
155     std::vector<Key> VecKeyIn(size, Key { strBase });
156     ITypesUtil::GetTotalSize(VecEntryIn);
157     ITypesUtil::GetTotalSize(VecKeyIn);
158 }
159 } // namespace OHOS
160 
161 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)162 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
163 {
164     /* Run your code on data */
165     bool fuzzBool = ((size % 2) == 0);
166     uint32_t fuzzUInt32 = static_cast<uint32_t>(size);
167     std::string fuzzStr(reinterpret_cast<const char *>(data), size);
168     std::vector<uint8_t> fuzzVec(fuzzStr.begin(), fuzzStr.end());
169 
170     OHOS::ClientDevFuzz(fuzzStr);
171     OHOS::EntryFuzz(fuzzStr);
172     OHOS::BlobFuzz(fuzzStr);
173     OHOS::VecFuzz(fuzzVec);
174     OHOS::OptionsFuzz(fuzzStr);
175     OHOS::SyncPolicyFuzz(fuzzUInt32);
176     OHOS::ChangeNotificationFuzz(fuzzStr, fuzzBool);
177     OHOS::IntFuzz(size);
178     OHOS::StringFuzz(fuzzStr);
179     OHOS::GetTotalSizeFuzz(fuzzStr, fuzzUInt32);
180     return 0;
181 }
182