• 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 "publishnotification_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 
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     FuzzedDataProvider fdp(data, size);
94     OHOS::DoSomethingInterestingWithMyAPI(&fdp);
95     return 0;
96 }
97