1 /*
2 * Copyright (c) 2024 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 "eventcenter_fuzzer.h"
17 #include "eventcenter/pasteboard_event.h"
18 #include "eventcenter/event_center.h"
19 #include <memory>
20
21 namespace OHOS {
22 using namespace OHOS::MiscServices;
23
24 template<class T>
TypeCast(const uint8_t * data,int * pos=nullptr)25 T TypeCast(const uint8_t *data, int *pos = nullptr)
26 {
27 if (pos) {
28 *pos += sizeof(T);
29 }
30 return *(reinterpret_cast<const T*>(data));
31 }
32
FuzzPbEventGetNetworkId(const uint8_t * rawData,size_t size)33 bool FuzzPbEventGetNetworkId(const uint8_t *rawData, size_t size)
34 {
35 if (rawData == nullptr || size < sizeof(int32_t)) {
36 return true;
37 }
38 int32_t evtId = TypeCast<int32_t>(rawData);
39 PasteboardEvent pbEvt(evtId, "pasteboard_fuzz_test");
40 pbEvt.GetNetworkId();
41 pbEvt.GetEventId();
42 return true;
43 }
44
FuzzEventCenterSubscribe(const uint8_t * rawData,size_t size)45 bool FuzzEventCenterSubscribe(const uint8_t *rawData, size_t size)
46 {
47 if (rawData == nullptr || size < sizeof(int32_t)) {
48 return true;
49 }
50 int32_t evtId = TypeCast<int32_t>(rawData);
51 EventCenter::GetInstance().Subscribe(evtId, [](const Event &evt) {
52 });
53 EventCenter::GetInstance().Unsubscribe(evtId);
54 return true;
55 }
56
FuzzEventCenterPostEvent(const uint8_t * rawData,size_t size)57 bool FuzzEventCenterPostEvent(const uint8_t *rawData, size_t size)
58 {
59 if (rawData == nullptr || size < sizeof(int32_t)) {
60 return true;
61 }
62 int32_t evtId = TypeCast<int32_t>(rawData);
63 EventCenter::GetInstance().PostEvent(nullptr);
64 EventCenter::GetInstance().PostEvent(std::make_unique<Event>(evtId));
65 return true;
66 }
67
FuzzEventCenterDefer(const uint8_t * rawData,size_t size)68 bool FuzzEventCenterDefer(const uint8_t *rawData, size_t size)
69 {
70 if (rawData == nullptr || size < sizeof(int32_t)) {
71 return true;
72 }
73 int32_t evtId = TypeCast<int32_t>(rawData);
74 EventCenter::Defer Defer([](const Event &evt) {
75 }, evtId);
76 return true;
77 }
78
79 } // namespace OHOS
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
82 {
83 /* Run your code on data */
84 OHOS::FuzzPbEventGetNetworkId(data, size);
85 OHOS::FuzzEventCenterSubscribe(data, size);
86 OHOS::FuzzEventCenterPostEvent(data, size);
87 OHOS::FuzzEventCenterDefer(data, size);
88 return 0;
89 }
90