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 #define private public
17 #define protected public
18 #include "notification.h"
19 #undef private
20 #undef protected
21 #include "readfromparcel_fuzzer.h"
22 
23 namespace OHOS {
24     namespace {
25         constexpr uint8_t ENABLE = 2;
26         constexpr uint8_t SOURCE_TYPE = 3;
27         constexpr uint8_t SLOT_VISIBLENESS_TYPE_NUM = 4;
28         constexpr uint8_t SLOT_TYPE_NUM = 5;
29     }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)30     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
31     {
32         std::string stringData(data);
33         sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
34         if (request != nullptr) {
35             request->SetClassification(stringData);
36         }
37         Notification::Notification notification(request);
38         Parcel parcel;
39         notification.MarshallingString(parcel);
40         notification.MarshallingInt32(parcel);
41         notification.MarshallingInt64(parcel);
42         notification.MarshallingParcelable(parcel);
43         notification.Marshalling(parcel);
44         notification.ReadFromParcelBool(parcel);
45         notification.ReadFromParcelString(parcel);
46         notification.ReadFromParcelInt32(parcel);
47         notification.ReadFromParcelInt64(parcel);
48         notification.ReadFromParcelParcelable(parcel);
49         notification.Unmarshalling(parcel);
50         bool enabled = *data % ENABLE;
51         notification.SetEnableSound(enabled);
52         notification.SetEnableLight(enabled);
53         notification.SetEnableVibration(enabled);
54         int32_t color = static_cast<int32_t>(GetU32Data(data));
55         notification.SetLedLightColor(color);
56         uint8_t visibleness = *data % SLOT_VISIBLENESS_TYPE_NUM;
57         Notification::NotificationConstant::VisiblenessType visiblenessType =
58             Notification::NotificationConstant::VisiblenessType(visibleness);
59         notification.SetLockScreenVisbleness(visiblenessType);
60         int64_t time = 2;
61         notification.SetPostTime(time);
62         std::vector<int64_t> style;
63         style.emplace_back(time);
64         notification.SetVibrationStyle(style);
65         int32_t remindType = static_cast<int32_t>(*data % SLOT_TYPE_NUM);
66         Notification::NotificationConstant::RemindType remind =
67             Notification::NotificationConstant::RemindType(remindType);
68         notification.SetRemindType(remind);
69         notification.SetRemoveAllowed(enabled);
70         int32_t source = static_cast<int32_t>(*data % SOURCE_TYPE);
71         Notification::NotificationConstant::SourceType sourceType =
72             Notification::NotificationConstant::SourceType(source);
73         notification.SetSourceType(sourceType);
74         notification.Dump();
75         return notification.ReadFromParcel(parcel);
76     }
77 }
78 
79 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)80 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
81 {
82     /* Run your code on data */
83     char *ch = ParseData(data, size);
84     if (ch != nullptr && size >= GetU32Size()) {
85         OHOS::DoSomethingInterestingWithMyAPI(ch, size);
86         free(ch);
87         ch = nullptr;
88     }
89     return 0;
90 }
91