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 #define private public
17 #define protected public
18 #include "notification_user_input.h"
19 #undef private
20 #undef protected
21 #include "notificationuserinput_fuzzer.h"
22 #include <fuzzer/FuzzedDataProvider.h>
23
24 namespace OHOS {
25 namespace {
26 constexpr uint8_t ENABLE = 2;
27 constexpr uint8_t INPUT_EDIT_TYPE = 3;
28 }
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)29 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider* fdp)
30 {
31 std::string stringData = fdp->ConsumeRandomLengthString();
32 Notification::NotificationUserInput notificationUserInput(stringData);
33 AAFwk::Want want;
34 uint8_t sources = fdp->ConsumeIntegral<uint8_t>() % ENABLE;
35 Notification::NotificationConstant::InputsSource source =
36 Notification::NotificationConstant::InputsSource(sources);
37 notificationUserInput.SetInputsSource(want, source);
38 notificationUserInput.GetInputsSource(want);
39 std::shared_ptr<Notification::NotificationUserInput> input =
40 std::make_shared<Notification::NotificationUserInput>();
41 std::vector<std::shared_ptr<Notification::NotificationUserInput>> userInputs;
42 userInputs.emplace_back(input);
43 AAFwk::WantParams additional;
44 notificationUserInput.AddInputsToWant(userInputs, want, additional);
45 notificationUserInput.GetInputsFromWant(want);
46 notificationUserInput.Create(stringData);
47 notificationUserInput.GetInputKey();
48 notificationUserInput.AddAdditionalData(additional);
49 notificationUserInput.GetAdditionalData();
50 uint8_t inputEditTypes = fdp->ConsumeIntegral<uint8_t>() % INPUT_EDIT_TYPE;
51 Notification::NotificationConstant::InputEditType inputEditType =
52 Notification::NotificationConstant::InputEditType(inputEditTypes);
53 notificationUserInput.SetEditType(inputEditType);
54 notificationUserInput.GetEditType();
55 std::vector<std::string> options;
56 options.emplace_back(stringData);
57 notificationUserInput.SetOptions(options);
58 notificationUserInput.GetOptions();
59 bool doPermit = fdp->ConsumeBool();
60 notificationUserInput.SetPermitMimeTypes(stringData, doPermit);
61 notificationUserInput.GetPermitMimeTypes();
62 notificationUserInput.IsMimeTypeOnly();
63 notificationUserInput.SetTag(stringData);
64 notificationUserInput.GetTag();
65 notificationUserInput.SetPermitFreeFormInput(doPermit);
66 notificationUserInput.IsPermitFreeFormInput();
67 notificationUserInput.Dump();
68 Parcel parcel;
69 notificationUserInput.ReadFromParcel(parcel);
70 return notificationUserInput.Marshalling(parcel);
71 }
72 }
73
74 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)75 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
76 {
77 /* Run your code on data */
78 FuzzedDataProvider fdp(data, size);
79 OHOS::DoSomethingInterestingWithMyAPI(&fdp);
80 return 0;
81 }
82