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 "notificationuserinputannex_fuzzer.h"
22
23 namespace OHOS {
24 namespace {
25 constexpr uint8_t ENABLE = 2;
26 constexpr uint8_t INPUT_EDIT_TYPE = 3;
27 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)28 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
29 {
30 std::string stringData(data);
31 Notification::NotificationUserInput notificationUserInput(stringData);
32 AAFwk::Want want;
33 Notification::NotificationUserInput userInput(stringData);
34 std::map<std::string, std::shared_ptr<Uri>> results;
35 notificationUserInput.AddMimeInputToWant(userInput, want, results);
36 std::string inputKey(data);
37 std::string tag(data);
38 std::vector<std::string> options;
39 options.emplace_back(stringData);
40 bool permitFreeFormInput = *data % ENABLE;
41 std::set<std::string> permitMimeTypes;
42 std::shared_ptr<AAFwk::WantParams> additional;
43 uint8_t inputEditTypes = *data % INPUT_EDIT_TYPE;
44 Notification::NotificationConstant::InputEditType inputEditType =
45 Notification::NotificationConstant::InputEditType(inputEditTypes);
46 Notification::NotificationUserInput::Create(inputKey, tag, options, permitFreeFormInput,
47 permitMimeTypes, additional, inputEditType);
48 Notification::NotificationUserInput notificationUserInputannex
49 (inputKey, tag, options, permitFreeFormInput, permitMimeTypes, additional, inputEditType);
50 nlohmann::json jsonObject;
51 notificationUserInputannex.ToJson(jsonObject);
52 notificationUserInputannex.FromJson(jsonObject);
53 Parcel parcel;
54 return notificationUserInput.Marshalling(parcel);
55 }
56 }
57
58 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)59 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
60 {
61 /* Run your code on data */
62 char *ch = ParseData(data, size);
63 if (ch != nullptr && size >= GetU32Size()) {
64 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
65 free(ch);
66 ch = nullptr;
67 }
68 return 0;
69 }
70