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 #include <fuzzer/FuzzedDataProvider.h>
21
22 namespace OHOS {
23 namespace EventFwk {
24 class TestSubscriber : public CommonEventSubscriber {
25 public:
TestSubscriber(const CommonEventSubscribeInfo & sp)26 explicit TestSubscriber(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
27 {}
28
~TestSubscriber()29 ~TestSubscriber()
30 {}
31
OnReceiveEvent(const CommonEventData & data)32 void OnReceiveEvent(const CommonEventData &data) override
33 {}
34 };
35 } // namespace EventFwk
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)36 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp)
37 {
38 uint8_t dumpType = fdp->ConsumeIntegral<uint8_t>();
39 int32_t code = fdp->ConsumeIntegral<int32_t>();
40 std::string stringData = fdp->ConsumeRandomLengthString();
41 std::vector<std::string> state;
42 state.emplace_back(stringData);
43 EventFwk::CommonEvent commonEvent;
44 // test PublishCommonEvent function
45 AAFwk::Want want;
46 EventFwk::CommonEventData commonEventData(want);
47 commonEventData.SetCode(code);
48 commonEventData.SetData(stringData);
49 // make commonEventPublishInfo info
50 EventFwk::CommonEventPublishInfo commonEventPublishInfo;
51 std::vector<std::string> permissions;
52 permissions.emplace_back(stringData);
53 commonEventPublishInfo.SetSubscriberPermissions(permissions);
54 commonEventPublishInfo.IsSticky();
55 commonEventPublishInfo.GetSubscriberPermissions();
56 commonEventPublishInfo.SetOrdered(fdp->ConsumeBool());
57 commonEventPublishInfo.SetBundleName(stringData);
58 commonEventPublishInfo.GetBundleName();
59 // make CommonEventSubscriber info
60 EventFwk::MatchingSkills matchingSkills;
61 matchingSkills.AddEvent(stringData);
62
63 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
64 subscribeInfo.SetDeviceId(stringData);
65 std::shared_ptr<EventFwk::TestSubscriber> subscriber =
66 std::make_shared<EventFwk::TestSubscriber>(subscribeInfo);
67 subscriber->SetCode(code);
68 subscriber->GetCode();
69 subscriber->SetData(stringData);
70 subscriber->GetData();
71 subscriber->SetCodeAndData(code, stringData);
72 subscriber->AbortCommonEvent();
73 subscriber->ClearAbortCommonEvent();
74 subscriber->GetAbortCommonEvent();
75 subscriber->GoAsyncCommonEvent();
76 subscriber->GetSubscribeInfo();
77 subscriber->IsOrderedCommonEvent();
78 subscriber->IsStickyCommonEvent();
79 commonEvent.PublishCommonEvent(commonEventData, commonEventPublishInfo, subscriber);
80 // test PublishCommonEvent and four paramter
81 commonEvent.PublishCommonEvent(commonEventData, commonEventPublishInfo, subscriber, code, code);
82 commonEvent.PublishCommonEventAsUser(commonEventData, commonEventPublishInfo, nullptr, code);
83 commonEvent.PublishCommonEventAsUser(commonEventData, commonEventPublishInfo, nullptr, code, code, code);
84 // test DumpState function
85 return commonEvent.DumpState(dumpType, stringData, code, state);
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 std::vector<std::string> permissions;
95 NativeTokenGet(permissions);
96 OHOS::DoSomethingInterestingWithMyAPI(&fdp);
97 return 0;
98 }
99