1 /*
2 * Copyright (c) 2025 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 #include "finishreceiver_fuzzer.h"
16
17 #include "common_event_data.h"
18 #include "common_event_listener.h"
19 #include "common_event_manager_service.h"
20 #include "common_event_subscriber.h"
21 #include "fuzz_common_base.h"
22 #include "refbase.h"
23 #include <fuzzer/FuzzedDataProvider.h>
24 #include <string>
25
26 using namespace OHOS::EventFwk;
27
28 namespace OHOS {
29
30 class SubscriberTest : public CommonEventSubscriber {
31 public:
SubscriberTest(const CommonEventSubscribeInfo & sp)32 explicit SubscriberTest(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
33 {}
34
~SubscriberTest()35 ~SubscriberTest()
36 {}
37
OnReceiveEvent(const CommonEventData & data)38 virtual void OnReceiveEvent(const CommonEventData &data)
39 {}
40 };
41
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)42 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp)
43 {
44 sptr<CommonEventManagerService> service = CommonEventManagerService::GetInstance();
45 service->Init();
46
47 MatchingSkills matchingSkills;
48 matchingSkills.AddEvent(fdp->ConsumeRandomLengthString());
49 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
50 int32_t userId = fdp->ConsumeIntegralInRange<int32_t>(-3, 1000);
51 if (fdp->ConsumeBool()) {
52 subscribeInfo.SetUserId(userId);
53 }
54 subscribeInfo.SetPriority(fdp->ConsumeIntegral<int32_t>());
55 std::shared_ptr<CommonEventSubscriber> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
56 sptr<IRemoteObject> commonEventListener = new (std::nothrow) CommonEventListener(subscriber);
57
58 bool funcResult1 = false;
59 service->FinishReceiver(commonEventListener, fdp->ConsumeIntegral<int32_t>(),
60 fdp->ConsumeRandomLengthString(), fdp->ConsumeBool(), funcResult1);
61 return true;
62 }
63 }
64
65 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)66 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
67 {
68 /* Run your code on data */
69 FuzzedDataProvider fdp(data, size);
70 std::vector<std::string> permissions;
71 MockRandomToken(&fdp, permissions);
72 OHOS::DoSomethingInterestingWithMyAPI(&fdp);
73 return 0;
74 }
75