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 "settemplate_fuzzer.h"
17 #define private public
18 #define protected public
19 #include "notification_request.h"
20 #undef private
21 #undef protected
22 
23 namespace OHOS {
24     namespace {
25         constexpr uint8_t ENABLE = 2;
26         constexpr uint8_t VISIBLENESS_TYPE_NUM = 4;
27     }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)28     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
29     {
30         std::string stringData(data);
31         int32_t notificationId = static_cast<int32_t>(GetU32Data(data));
32         Notification::NotificationRequest request(notificationId);
33         bool enabled = *data % ENABLE;
34         request.SetTapDismissed(enabled);
35         uint8_t type = *data % VISIBLENESS_TYPE_NUM;
36         Notification::NotificationConstant::VisiblenessType VisiblenessType =
37             Notification::NotificationConstant::VisiblenessType(type);
38         request.SetVisibleness(VisiblenessType);
39         request.GetVisibleness();
40         request.GetBadgeIconStyle();
41         request.SetShortcutId(stringData);
42         request.GetShortcutId();
43         request.SetFloatingIcon(enabled);
44         request.IsFloatingIcon();
45         request.SetProgressBar(notificationId, notificationId, enabled);
46         request.GetProgressMax();
47         request.GetProgressValue();
48         request.IsProgressIndeterminate();
49         std::vector<std::string> text;
50         text.emplace_back(stringData);
51         request.SetNotificationUserInputHistory(text);
52         request.GetNotificationUserInputHistory();
53         request.GetNotificationHashCode();
54         request.GetOwnerBundleName();
55         request.GetCreatorBundleName();
56         request.GetCreatorPid();
57         request.SetCreatorUid(notificationId);
58         request.GetCreatorUid();
59         request.SetOwnerUid(notificationId);
60         request.GetOwnerUid();
61         request.GetLabel();
62         request.SetDistributed(enabled);
63         request.SetDevicesSupportDisplay(text);
64         request.SetDevicesSupportOperate(text);
65         request.GetNotificationDistributedOptions();
66         request.SetCreatorUserId(notificationId);
67         request.GetCreatorUserId();
68         request.SetOwnerUserId(notificationId);
69         request.GetOwnerUserId();
70         request.GetNowSysTime();
71         std::shared_ptr<Notification::NotificationTemplate> templ =
72             std::make_shared<Notification::NotificationTemplate>();
73         if (templ != nullptr) {
74             templ->SetTemplateName(stringData);
75         }
76         request.SetTemplate(templ);
77         request.GetTemplate();
78         request.GetFlags();
79         request.SetReceiverUserId(notificationId);
80         return request.GetReceiverUserId();
81     }
82 }
83 
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
86 {
87     /* Run your code on data */
88     char *ch = ParseData(data, size);
89     if (ch != nullptr && size >= GetU32Size()) {
90         OHOS::DoSomethingInterestingWithMyAPI(ch, size);
91         free(ch);
92         ch = nullptr;
93     }
94     return 0;
95 }
96