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 "commonevent_fuzzer.h"
17 #include "common_event.h"
18 #include "common_event_subscriber.h"
19 #include "fuzz_common_base.h"
20
21 namespace OHOS {
22 namespace EventFwk {
23 class TestSubscriber : public CommonEventSubscriber {
24 public:
TestSubscriber(const CommonEventSubscribeInfo & sp)25 explicit TestSubscriber(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
26 {}
27
~TestSubscriber()28 ~TestSubscriber()
29 {}
30
OnReceiveEvent(const CommonEventData & data)31 void OnReceiveEvent(const CommonEventData &data) override
32 {}
33 };
34 } // namespace EventFwk
35 namespace {
36 constexpr size_t U32_AT_SIZE = 4;
37 }
DoSomethingInterestingWithMyAPI(FuzzData fuzzData)38 bool DoSomethingInterestingWithMyAPI(FuzzData fuzzData)
39 {
40 uint8_t dumpType = fuzzData.GetData<uint8_t>();
41 int32_t code = fuzzData.GetData<int32_t>();
42 std::string stringData = fuzzData.GenerateRandomString();
43 std::vector<std::string> state;
44 state.emplace_back(stringData);
45 EventFwk::CommonEvent commonEvent;
46 // test PublishCommonEvent function
47 AAFwk::Want want;
48 EventFwk::CommonEventData commonEventData(want);
49 commonEventData.SetCode(code);
50 commonEventData.SetData(stringData);
51 // make commonEventPublishInfo info
52 EventFwk::CommonEventPublishInfo commonEventPublishInfo;
53 std::vector<std::string> permissions;
54 permissions.emplace_back(stringData);
55 commonEventPublishInfo.SetSubscriberPermissions(permissions);
56 commonEventPublishInfo.IsSticky();
57 commonEventPublishInfo.GetSubscriberPermissions();
58 commonEventPublishInfo.SetOrdered(fuzzData.GenerateRandomBool());
59 commonEventPublishInfo.SetBundleName(stringData);
60 commonEventPublishInfo.GetBundleName();
61 // make CommonEventSubscriber info
62 EventFwk::MatchingSkills matchingSkills;
63 matchingSkills.AddEvent(stringData);
64
65 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
66 subscribeInfo.SetDeviceId(stringData);
67 std::shared_ptr<EventFwk::TestSubscriber> subscriber =
68 std::make_shared<EventFwk::TestSubscriber>(subscribeInfo);
69 subscriber->SetCode(code);
70 subscriber->GetCode();
71 subscriber->SetData(stringData);
72 subscriber->GetData();
73 subscriber->SetCodeAndData(code, stringData);
74 subscriber->AbortCommonEvent();
75 subscriber->ClearAbortCommonEvent();
76 subscriber->GetAbortCommonEvent();
77 subscriber->GoAsyncCommonEvent();
78 subscriber->GetSubscribeInfo();
79 subscriber->IsOrderedCommonEvent();
80 subscriber->IsStickyCommonEvent();
81 commonEvent.PublishCommonEvent(commonEventData, commonEventPublishInfo, subscriber);
82 // test PublishCommonEvent and four paramter
83 commonEvent.PublishCommonEvent(commonEventData, commonEventPublishInfo, subscriber, code, code);
84 commonEvent.PublishCommonEventAsUser(commonEventData, commonEventPublishInfo, nullptr, code);
85 commonEvent.PublishCommonEventAsUser(commonEventData, commonEventPublishInfo, nullptr, code, code, code);
86 // test DumpState function
87 return commonEvent.DumpState(dumpType, stringData, code, state);
88 }
89 }
90
91 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)92 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
93 {
94 /* Run your code on data */
95 if (data == nullptr) {
96 return 0;
97 }
98
99 if (size < OHOS::U32_AT_SIZE) {
100 return 0;
101 }
102 std::vector<std::string> permissions;
103 NativeTokenGet(permissions);
104 OHOS::FuzzData fuzzData(data, size);
105 OHOS::DoSomethingInterestingWithMyAPI(fuzzData);
106 return 0;
107 }
108