1686862fbSopenharmony_ci/*
2686862fbSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4686862fbSopenharmony_ci * you may not use this file except in compliance with the License.
5686862fbSopenharmony_ci * You may obtain a copy of the License at
6686862fbSopenharmony_ci *
7686862fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8686862fbSopenharmony_ci *
9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12686862fbSopenharmony_ci * See the License for the specific language governing permissions and
13686862fbSopenharmony_ci * limitations under the License.
14686862fbSopenharmony_ci */
15686862fbSopenharmony_ci
16686862fbSopenharmony_ci#include "distributedwant_fuzzer.h"
17686862fbSopenharmony_ci
18686862fbSopenharmony_ci#include <cstddef>
19686862fbSopenharmony_ci#include <cstdint>
20686862fbSopenharmony_ci#include <iostream>
21686862fbSopenharmony_ci
22686862fbSopenharmony_ci#include "bool_wrapper.h"
23686862fbSopenharmony_ci#include "distributed_want.h"
24686862fbSopenharmony_ci#include "securec.h"
25686862fbSopenharmony_ci
26686862fbSopenharmony_ciusing namespace OHOS::AAFwk;
27686862fbSopenharmony_ciusing namespace OHOS::DistributedSchedule;
28686862fbSopenharmony_ci
29686862fbSopenharmony_cinamespace OHOS {
30686862fbSopenharmony_cinamespace {
31686862fbSopenharmony_ciconstexpr size_t FOO_MAX_LEN = 1024;
32686862fbSopenharmony_ciconstexpr size_t U32_AT_SIZE = 4;
33686862fbSopenharmony_ciconstexpr int32_t POS_0 = 0;
34686862fbSopenharmony_ciconstexpr int32_t POS_1 = 1;
35686862fbSopenharmony_ciconstexpr int32_t POS_2 = 2;
36686862fbSopenharmony_ciconstexpr int32_t POS_3 = 3;
37686862fbSopenharmony_ciconstexpr int32_t OFFSET_24 = 24;
38686862fbSopenharmony_ciconstexpr int32_t OFFSET_16 = 16;
39686862fbSopenharmony_ciconstexpr int32_t OFFSET_8 = 8;
40686862fbSopenharmony_ci}
41686862fbSopenharmony_ciuint32_t GetU32Data(const char* ptr)
42686862fbSopenharmony_ci{
43686862fbSopenharmony_ci    // convert fuzz input data to an integer
44686862fbSopenharmony_ci    return (ptr[POS_0] << OFFSET_24) | (ptr[POS_1] << OFFSET_16) | (ptr[POS_2] << OFFSET_8) | ptr[POS_3];
45686862fbSopenharmony_ci}
46686862fbSopenharmony_ci
47686862fbSopenharmony_cibool DoSomethingInterestingWithMyAPI_DistributedWant_001(const char* data, size_t size)
48686862fbSopenharmony_ci{
49686862fbSopenharmony_ci    std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
50686862fbSopenharmony_ci    unsigned int flags = static_cast<unsigned int>(GetU32Data(data));
51686862fbSopenharmony_ci    want->SetFlags(flags);
52686862fbSopenharmony_ci    want->RemoveFlags(flags);
53686862fbSopenharmony_ci    want->AddFlags(flags);
54686862fbSopenharmony_ci    std::string entity(data, size);
55686862fbSopenharmony_ci    want->AddEntity(entity);
56686862fbSopenharmony_ci    want->HasEntity(entity);
57686862fbSopenharmony_ci    want->RemoveEntity(entity);
58686862fbSopenharmony_ci    std::string bundleName(data, size);
59686862fbSopenharmony_ci    want->SetBundle(bundleName);
60686862fbSopenharmony_ci    std::string deviceId(data, size);
61686862fbSopenharmony_ci    want->SetDeviceId(deviceId);
62686862fbSopenharmony_ci    want->SetElementName(bundleName, entity);
63686862fbSopenharmony_ci    want->SetElementName(deviceId, bundleName, entity);
64686862fbSopenharmony_ci    return true;
65686862fbSopenharmony_ci}
66686862fbSopenharmony_ci
67686862fbSopenharmony_cibool DoSomethingInterestingWithMyAPI_DistributedWant_002(const char* data, size_t size)
68686862fbSopenharmony_ci{
69686862fbSopenharmony_ci    std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
70686862fbSopenharmony_ci    std::string type(data, size);
71686862fbSopenharmony_ci    want->SetType(type);
72686862fbSopenharmony_ci    Uri uri(type);
73686862fbSopenharmony_ci    want->SetUri(uri);
74686862fbSopenharmony_ci    want->SetUriAndType(uri, type);
75686862fbSopenharmony_ci    want->FormatUri(uri);
76686862fbSopenharmony_ci    want->FormatUri(type);
77686862fbSopenharmony_ci    want->GetLowerCaseScheme(uri);
78686862fbSopenharmony_ci    return true;
79686862fbSopenharmony_ci}
80686862fbSopenharmony_ci
81686862fbSopenharmony_cibool DoSomethingInterestingWithMyAPI_DistributedWant_003(const char* data, size_t size)
82686862fbSopenharmony_ci{
83686862fbSopenharmony_ci    std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
84686862fbSopenharmony_ci    want->CountEntities();
85686862fbSopenharmony_ci    want->GetScheme();
86686862fbSopenharmony_ci    DistributedOperation operation;
87686862fbSopenharmony_ci    want->SetOperation(operation);
88686862fbSopenharmony_ci    std::string key(data, size);
89686862fbSopenharmony_ci    want->HasParameter(key);
90686862fbSopenharmony_ci    std::string content(data, size);
91686862fbSopenharmony_ci    std::string prop(data, size);
92686862fbSopenharmony_ci    std::string value(data, size);
93686862fbSopenharmony_ci    std::string str(data, size);
94686862fbSopenharmony_ci    nlohmann::json wantJson;
95686862fbSopenharmony_ci    want->ReadFromJson(wantJson);
96686862fbSopenharmony_ci    return true;
97686862fbSopenharmony_ci}
98686862fbSopenharmony_ci
99686862fbSopenharmony_cibool DoSomethingInterestingWithMyAPI_DistributedWant_004(const char* data, size_t size)
100686862fbSopenharmony_ci{
101686862fbSopenharmony_ci    std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
102686862fbSopenharmony_ci    std::string key(data, size);
103686862fbSopenharmony_ci    sptr<IRemoteObject> remoteObject;
104686862fbSopenharmony_ci    want->SetParam(key, remoteObject);
105686862fbSopenharmony_ci    std::vector<bool> boolValue;
106686862fbSopenharmony_ci    want->SetParam(key, boolValue);
107686862fbSopenharmony_ci    want->GetBoolArrayParam(key);
108686862fbSopenharmony_ci    byte byteValue = '\0';
109686862fbSopenharmony_ci    want->SetParam(key, byteValue);
110686862fbSopenharmony_ci    want->GetByteParam(key, byteValue);
111686862fbSopenharmony_ci    std::vector<byte> byteVector;
112686862fbSopenharmony_ci    want->SetParam(key, byteVector);
113686862fbSopenharmony_ci    want->GetByteArrayParam(key);
114686862fbSopenharmony_ci    zchar charValue = U'\0';
115686862fbSopenharmony_ci    want->SetParam(key, charValue);
116686862fbSopenharmony_ci    want->GetCharParam(key, charValue);
117686862fbSopenharmony_ci    want->GetParams();
118686862fbSopenharmony_ci    return true;
119686862fbSopenharmony_ci}
120686862fbSopenharmony_ci
121686862fbSopenharmony_cibool DoSomethingInterestingWithMyAPI_DistributedWant_005(const char* data, size_t size)
122686862fbSopenharmony_ci{
123686862fbSopenharmony_ci    std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
124686862fbSopenharmony_ci    std::string key(data, size);
125686862fbSopenharmony_ci    std::vector<zchar> charVector;
126686862fbSopenharmony_ci    want->SetParam(key, charVector);
127686862fbSopenharmony_ci    want->GetCharArrayParam(key);
128686862fbSopenharmony_ci    std::vector<int> intVector;
129686862fbSopenharmony_ci    want->SetParam(key, intVector);
130686862fbSopenharmony_ci    want->GetIntArrayParam(key);
131686862fbSopenharmony_ci    double doubleValue = 0.0;
132686862fbSopenharmony_ci    want->SetParam(key, doubleValue);
133686862fbSopenharmony_ci    want->GetDoubleParam(key, doubleValue);
134686862fbSopenharmony_ci    std::vector<double> doubleVector;
135686862fbSopenharmony_ci    want->SetParam(key, doubleVector);
136686862fbSopenharmony_ci    want->GetDoubleArrayParam(key);
137686862fbSopenharmony_ci    float floatValue = 0.0;
138686862fbSopenharmony_ci    want->SetParam(key, floatValue);
139686862fbSopenharmony_ci    want->GetFloatParam(key, floatValue);
140686862fbSopenharmony_ci    bool boolValue = true;
141686862fbSopenharmony_ci    want->SetParam(key, boolValue);
142686862fbSopenharmony_ci    want->GetBoolParam(key, boolValue);
143686862fbSopenharmony_ci    return true;
144686862fbSopenharmony_ci}
145686862fbSopenharmony_ci
146686862fbSopenharmony_cibool DoSomethingInterestingWithMyAPI_DistributedWant_006(const char* data, size_t size)
147686862fbSopenharmony_ci{
148686862fbSopenharmony_ci    std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
149686862fbSopenharmony_ci    std::string key(data, size);
150686862fbSopenharmony_ci    std::vector<float> floatVector;
151686862fbSopenharmony_ci    want->SetParam(key, floatVector);
152686862fbSopenharmony_ci    want->GetFloatArrayParam(key);
153686862fbSopenharmony_ci    long longValue = 0;
154686862fbSopenharmony_ci    want->SetParam(key, longValue);
155686862fbSopenharmony_ci    want->GetShortParam(key, longValue);
156686862fbSopenharmony_ci    std::vector<long> longVector;
157686862fbSopenharmony_ci    want->SetParam(key, longVector);
158686862fbSopenharmony_ci    want->GetLongArrayParam(key);
159686862fbSopenharmony_ci    short shortValue = 0;
160686862fbSopenharmony_ci    want->SetParam(key, shortValue);
161686862fbSopenharmony_ci    want->GetShortParam(key, shortValue);
162686862fbSopenharmony_ci    std::vector<short> shortVector;
163686862fbSopenharmony_ci    want->SetParam(key, shortVector);
164686862fbSopenharmony_ci    want->GetShortArrayParam(key);
165686862fbSopenharmony_ci    std::string stringValue(data, size);
166686862fbSopenharmony_ci    want->SetParam(key, stringValue);
167686862fbSopenharmony_ci    want->GetStringParam(key);
168686862fbSopenharmony_ci    std::vector<std::string> stringVector;
169686862fbSopenharmony_ci    want->SetParam(key, stringVector);
170686862fbSopenharmony_ci    want->GetStringArrayParam(key);
171686862fbSopenharmony_ci    want->RemoveParam(key);
172686862fbSopenharmony_ci
173686862fbSopenharmony_ci    bool boolValue = true;
174686862fbSopenharmony_ci    DistributedWantParams dWantParams;
175686862fbSopenharmony_ci    dWantParams.SetParam(key, Boolean::Box(boolValue));
176686862fbSopenharmony_ci    want->SetParams(dWantParams);
177686862fbSopenharmony_ci    want->ReplaceParams(dWantParams);
178686862fbSopenharmony_ci    DistributedWant dWant;
179686862fbSopenharmony_ci    want->ReplaceParams(dWant);
180686862fbSopenharmony_ci    want->ClearWant(&dWant);
181686862fbSopenharmony_ci    return true;
182686862fbSopenharmony_ci}
183686862fbSopenharmony_ci}
184686862fbSopenharmony_ci
185686862fbSopenharmony_ci/* Fuzzer entry point */
186686862fbSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
187686862fbSopenharmony_ci{
188686862fbSopenharmony_ci    /* Run your code on data */
189686862fbSopenharmony_ci    if (data == nullptr) {
190686862fbSopenharmony_ci        std::cout << "invalid data" << std::endl;
191686862fbSopenharmony_ci        return 0;
192686862fbSopenharmony_ci    }
193686862fbSopenharmony_ci
194686862fbSopenharmony_ci    /* Validate the length of size */
195686862fbSopenharmony_ci    if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
196686862fbSopenharmony_ci        return 0;
197686862fbSopenharmony_ci    }
198686862fbSopenharmony_ci
199686862fbSopenharmony_ci    char* ch = reinterpret_cast<char *>(malloc(size + 1));
200686862fbSopenharmony_ci    if (ch == nullptr) {
201686862fbSopenharmony_ci        std::cout << "malloc failed." << std::endl;
202686862fbSopenharmony_ci        return 0;
203686862fbSopenharmony_ci    }
204686862fbSopenharmony_ci
205686862fbSopenharmony_ci    (void)memset_s(ch, size + 1, 0x00, size + 1);
206686862fbSopenharmony_ci    if (memcpy_s(ch, size + 1, data, size) != EOK) {
207686862fbSopenharmony_ci        std::cout << "copy failed." << std::endl;
208686862fbSopenharmony_ci        free(ch);
209686862fbSopenharmony_ci        ch = nullptr;
210686862fbSopenharmony_ci        return 0;
211686862fbSopenharmony_ci    }
212686862fbSopenharmony_ci
213686862fbSopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_001(ch, size);
214686862fbSopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_002(ch, size);
215686862fbSopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_003(ch, size);
216686862fbSopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_004(ch, size);
217686862fbSopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_005(ch, size);
218686862fbSopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_006(ch, size);
219686862fbSopenharmony_ci    free(ch);
220686862fbSopenharmony_ci    ch = nullptr;
221686862fbSopenharmony_ci    return 0;
222686862fbSopenharmony_ci}
223