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 "publishcontinuoustasknotification_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         // test PublishContinuousTaskNotification function
78         Notification::NotificationHelper::PublishContinuousTaskNotification(request);
79         // test GetDeviceRemindType function
80         int32_t remindType = static_cast<int32_t>(*data % SLOT_TYPE_NUM);
81         Notification::NotificationConstant::RemindType remind =
82             Notification::NotificationConstant::RemindType(remindType);
83         Notification::NotificationHelper::GetDeviceRemindType(remind);
84         // test CancelContinuousTaskNotification function
85         Notification::NotificationHelper::CancelContinuousTaskNotification(stringData, style);
86         // test IsSupportTemplate function
87         bool support = *data % ENABLE;
88         Notification::NotificationHelper::IsSupportTemplate(stringData, support);
89         // test IsAllowedNotify function
90         return Notification::NotificationHelper::IsAllowedNotify(style, support);
91     }
92 }
93 
94 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)95 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
96 {
97     /* Run your code on data */
98     char *ch = ParseData(data, size);
99     if (ch != nullptr && size >= GetU32Size()) {
100         OHOS::DoSomethingInterestingWithMyAPI(ch, size);
101         free(ch);
102         ch = nullptr;
103     }
104     return 0;
105 }
106