1 /*
2 * Copyright (c) 2024 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 "distributedwant_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <iostream>
21
22 #include "bool_wrapper.h"
23 #include "distributed_want.h"
24 #include "securec.h"
25
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::DistributedSchedule;
28
29 namespace OHOS {
30 namespace {
31 constexpr size_t FOO_MAX_LEN = 1024;
32 constexpr size_t U32_AT_SIZE = 4;
33 constexpr int32_t POS_0 = 0;
34 constexpr int32_t POS_1 = 1;
35 constexpr int32_t POS_2 = 2;
36 constexpr int32_t POS_3 = 3;
37 constexpr int32_t OFFSET_24 = 24;
38 constexpr int32_t OFFSET_16 = 16;
39 constexpr int32_t OFFSET_8 = 8;
40 }
GetU32Data(const char * ptr)41 uint32_t GetU32Data(const char* ptr)
42 {
43 // convert fuzz input data to an integer.
44 return (ptr[POS_0] << OFFSET_24) | (ptr[POS_1] << OFFSET_16) | (ptr[POS_2] << OFFSET_8) | ptr[POS_3];
45 }
46
DoSomethingInterestingWithMyApiDistributedWant001(const uint8_t * data,size_t size)47 bool DoSomethingInterestingWithMyApiDistributedWant001(const uint8_t* data, size_t size)
48 {
49 if (data == nullptr || size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
50 return false;
51 }
52
53 std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
54 unsigned int flags = static_cast<unsigned int>(GetU32Data(reinterpret_cast<const char*>(data)));
55 want->SetFlags(flags);
56 want->RemoveFlags(flags);
57 want->AddFlags(flags);
58 std::string entity(reinterpret_cast<const char*>(data), size);
59 want->AddEntity(entity);
60 want->HasEntity(entity);
61 want->RemoveEntity(entity);
62 std::string bundleName(reinterpret_cast<const char*>(data), size);
63 want->SetBundle(bundleName);
64 std::string deviceId(reinterpret_cast<const char*>(data), size);
65 want->SetDeviceId(deviceId);
66 want->SetElementName(bundleName, entity);
67 want->SetElementName(deviceId, bundleName, entity);
68 return true;
69 }
70
DoSomethingInterestingWithMyApiDistributedWant002(const uint8_t * data,size_t size)71 bool DoSomethingInterestingWithMyApiDistributedWant002(const uint8_t* data, size_t size)
72 {
73 if (data == nullptr || size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
74 return false;
75 }
76
77 std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
78 std::string type(reinterpret_cast<const char*>(data), size);
79 want->SetType(type);
80 Uri uri(type);
81 want->SetUri(uri);
82 want->SetUriAndType(uri, type);
83 want->FormatUri(uri);
84 want->FormatUri(type);
85 want->GetLowerCaseScheme(uri);
86 return true;
87 }
88
DoSomethingInterestingWithMyApiDistributedWant003(const uint8_t * data,size_t size)89 bool DoSomethingInterestingWithMyApiDistributedWant003(const uint8_t* data, size_t size)
90 {
91 if (data == nullptr || size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
92 return false;
93 }
94
95 std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
96 want->CountEntities();
97 want->GetScheme();
98 DistributedOperation operation;
99 want->SetOperation(operation);
100 std::string key(reinterpret_cast<const char*>(data), size);
101 want->HasParameter(key);
102 std::string content(reinterpret_cast<const char*>(data), size);
103 std::string prop(reinterpret_cast<const char*>(data), size);
104 std::string value(reinterpret_cast<const char*>(data), size);
105 std::string str(reinterpret_cast<const char*>(data), size);
106 nlohmann::json wantJson;
107 want->ReadFromJson(wantJson);
108 return true;
109 }
110
DoSomethingInterestingWithMyApiDistributedWant004(const uint8_t * data,size_t size)111 bool DoSomethingInterestingWithMyApiDistributedWant004(const uint8_t* data, size_t size)
112 {
113 if (data == nullptr || size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
114 return false;
115 }
116
117 std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
118 std::string key(reinterpret_cast<const char*>(data), size);
119 sptr<IRemoteObject> remoteObject;
120 want->SetParam(key, remoteObject);
121 std::vector<bool> boolValue;
122 want->SetParam(key, boolValue);
123 want->GetBoolArrayParam(key);
124 byte byteValue = '\0';
125 want->SetParam(key, byteValue);
126 want->GetByteParam(key, byteValue);
127 std::vector<byte> byteVector;
128 want->SetParam(key, byteVector);
129 want->GetByteArrayParam(key);
130 zchar charValue = U'\0';
131 want->SetParam(key, charValue);
132 want->GetCharParam(key, charValue);
133 want->GetParams();
134 return true;
135 }
136
DoSomethingInterestingWithMyApiDistributedWant005(const uint8_t * data,size_t size)137 bool DoSomethingInterestingWithMyApiDistributedWant005(const uint8_t* data, size_t size)
138 {
139 if (data == nullptr || size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
140 return false;
141 }
142
143 std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
144 std::string key(reinterpret_cast<const char*>(data), size);
145 std::vector<zchar> charVector;
146 want->SetParam(key, charVector);
147 want->GetCharArrayParam(key);
148 std::vector<int> intVector;
149 want->SetParam(key, intVector);
150 want->GetIntArrayParam(key);
151 double doubleValue = 0.0;
152 want->SetParam(key, doubleValue);
153 want->GetDoubleParam(key, doubleValue);
154 std::vector<double> doubleVector;
155 want->SetParam(key, doubleVector);
156 want->GetDoubleArrayParam(key);
157 float floatValue = 0.0;
158 want->SetParam(key, floatValue);
159 want->GetFloatParam(key, floatValue);
160 bool boolValue = true;
161 want->SetParam(key, boolValue);
162 want->GetBoolParam(key, boolValue);
163 return true;
164 }
165
DoSomethingInterestingWithMyApiDistributedWant006(const uint8_t * data,size_t size)166 bool DoSomethingInterestingWithMyApiDistributedWant006(const uint8_t* data, size_t size)
167 {
168 if (data == nullptr || size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
169 return false;
170 }
171
172 std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
173 std::string key(reinterpret_cast<const char*>(data), size);
174 std::vector<float> floatVector;
175 want->SetParam(key, floatVector);
176 want->GetFloatArrayParam(key);
177 long longValue = 0;
178 want->SetParam(key, longValue);
179 want->GetShortParam(key, longValue);
180 std::vector<long> longVector;
181 want->SetParam(key, longVector);
182 want->GetLongArrayParam(key);
183 short shortValue = 0;
184 want->SetParam(key, shortValue);
185 want->GetShortParam(key, shortValue);
186 std::vector<short> shortVector;
187 want->SetParam(key, shortVector);
188 want->GetShortArrayParam(key);
189 std::string stringValue(reinterpret_cast<const char*>(data), size);
190 want->SetParam(key, stringValue);
191 want->GetStringParam(key);
192 std::vector<std::string> stringVector;
193 want->SetParam(key, stringVector);
194 want->GetStringArrayParam(key);
195 want->RemoveParam(key);
196
197 bool boolValue = true;
198 DistributedWantParams dWantParams;
199 dWantParams.SetParam(key, Boolean::Box(boolValue));
200 want->SetParams(dWantParams);
201 want->ReplaceParams(dWantParams);
202 DistributedWant dWant;
203 want->ReplaceParams(dWant);
204 want->ClearWant(&dWant);
205 return true;
206 }
207 }
208
209 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)210 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
211 {
212 OHOS::DoSomethingInterestingWithMyApiDistributedWant001(data, size);
213 OHOS::DoSomethingInterestingWithMyApiDistributedWant002(data, size);
214 OHOS::DoSomethingInterestingWithMyApiDistributedWant003(data, size);
215 OHOS::DoSomethingInterestingWithMyApiDistributedWant004(data, size);
216 OHOS::DoSomethingInterestingWithMyApiDistributedWant005(data, size);
217 OHOS::DoSomethingInterestingWithMyApiDistributedWant006(data, size);
218 return 0;
219 }
220