• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <fuzzer/FuzzedDataProvider.h>
20 
21 namespace OHOS {
22     namespace {
23         constexpr uint8_t SLOT_TYPE_NUM = 5;
24         constexpr uint8_t FLAG_STATUS = 3;
25     }
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)26     bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider* fdp)
27     {
28         std::string stringData = fdp->ConsumeRandomLengthString();
29         Notification::NotificationRequest request;
30         request.SetAlertOneTime(fdp->ConsumeBool());
31 
32         int32_t style = fdp->ConsumeIntegral<int32_t>();
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 = fdp->ConsumeIntegral<uint32_t>();
40         request.SetColor(color);
41         request.SetColorEnabled(fdp->ConsumeBool());
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(fdp->ConsumeBool());
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>(fdp->ConsumeIntegral<uint8_t>() % 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(fdp->ConsumeBool());
70         request.SetLabel(stringData);
71         request.SetNotificationId(style);
72         request.SetOwnerBundleName(stringData);
73 
74         uint8_t types = fdp->ConsumeIntegral<uint8_t>() % 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>(fdp->ConsumeIntegral<uint8_t>() % 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 = fdp->ConsumeBool();
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     FuzzedDataProvider fdp(data, size);
99     OHOS::DoSomethingInterestingWithMyAPI(&fdp);
100     return 0;
101 }
102