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 "publishnotification_fuzzer.h"
17 
18 #include "notification_helper.h"
19 
20 namespace OHOS {
21     namespace {
22         constexpr uint8_t ENABLE = 2;
23         constexpr uint8_t SLOT_TYPE_NUM = 5;
24         constexpr uint8_t FLAG_STATUS = 3;
25     }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)26     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
27     {
28         std::string stringData(data);
29         Notification::NotificationRequest request;
30         request.SetAlertOneTime(*data % ENABLE);
31 
32         int32_t style = static_cast<int32_t>(GetU32Data(data));
33         Notification::NotificationRequest::BadgeStyle badgeStyle =
34             Notification::NotificationRequest::BadgeStyle(style);
35         request.SetBadgeIconStyle(badgeStyle);
36         request.SetBadgeNumber(style);
37         request.SetClassification(stringData);
38 
39         uint32_t color = GetU32Data(data);
40         request.SetColor(color);
41         request.SetColorEnabled(*data % ENABLE);
42 
43         std::shared_ptr<Notification::NotificationNormalContent> contentType =
44             std::make_shared<Notification::NotificationNormalContent>();
45         contentType->SetText(stringData);
46         contentType->SetTitle(stringData);
47         contentType->SetAdditionalText(stringData);
48         std::shared_ptr<Notification::NotificationContent> content =
49             std::make_shared<Notification::NotificationContent>(contentType);
50         request.SetContent(content);
51         request.SetCountdownTimer(*data % ENABLE);
52         request.SetCreatorBundleName(stringData);
53         request.SetDeliveryTime(style);
54 
55         std::shared_ptr<Notification::NotificationFlags> notificationFlages =
56             std::make_shared<Notification::NotificationFlags>();
57         int32_t soundEnabled = static_cast<int32_t>(*data % FLAG_STATUS);
58         Notification::NotificationConstant::FlagStatus sound =
59             Notification::NotificationConstant::FlagStatus(soundEnabled);
60         notificationFlages->SetSoundEnabled(sound);
61         notificationFlages->SetVibrationEnabled(sound);
62         request.SetFlags(notificationFlages);
63 
64         Notification::NotificationRequest::GroupAlertType groupAlertType =
65             Notification::NotificationRequest::GroupAlertType(color);
66         request.SetGroupAlertType(groupAlertType);
67 
68         request.SetGroupName(stringData);
69         request.SetGroupOverview(*data % ENABLE);
70         request.SetLabel(stringData);
71         request.SetNotificationId(style);
72         request.SetOwnerBundleName(stringData);
73 
74         uint8_t types = *data % SLOT_TYPE_NUM;
75         Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(types);
76         request.SetSlotType(slotType);
77 
78         Notification::NotificationHelper::PublishNotification(request);
79         // test GetActiveNotifications function
80         sptr<Notification::NotificationRequest> requester = new Notification::NotificationRequest(request);
81         std::vector<sptr<Notification::NotificationRequest>> requested;
82         requested.emplace_back(requester);
83         Notification::NotificationHelper::GetActiveNotifications(requested);
84         // test PublishNotification function
85         return Notification::NotificationHelper::PublishNotification(stringData, request) == ERR_OK;
86     }
87 }
88 
89 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)90 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
91 {
92     /* Run your code on data */
93     char *ch = ParseData(data, size);
94     if (ch != nullptr && size >= GetU32Size()) {
95         OHOS::DoSomethingInterestingWithMyAPI(ch, size);
96         free(ch);
97         ch = nullptr;
98     }
99     return 0;
100 }
101