1 /*
2 * Copyright (c) 2023 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 "common_event_manager_service.h"
17 #include "common_event_data.h"
18 #include "commoneventmanagerservice_fuzzer.h"
19 #include "refbase.h"
20 #include "fuzz_common_base.h"
21 #include <fuzzer/FuzzedDataProvider.h>
22 #include <string>
23 #include <vector>
24
25 namespace OHOS {
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)26 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp)
27 {
28 std::string stringData = fdp->ConsumeRandomLengthString();
29 int32_t code = fdp->ConsumeIntegral<int32_t>();
30 bool enabled = fdp->ConsumeBool();
31 EventFwk::CommonEventManagerService::GetInstance();
32 sptr<EventFwk::CommonEventManagerService> service =
33 sptr<EventFwk::CommonEventManagerService>(new EventFwk::CommonEventManagerService());
34 service->Init();
35 AAFwk::Want want;
36 EventFwk::CommonEventData commonEventData;
37 commonEventData.SetWant(want);
38 commonEventData.SetCode(code);
39 commonEventData.SetData(stringData);
40
41 Parcel p;
42 commonEventData.Marshalling(p);
43 commonEventData.Unmarshalling(p);
44 EventFwk::CommonEventPublishInfo commonEventPublishInfo;
45 std::vector<std::string> permissions;
46 permissions.emplace_back(stringData);
47 commonEventPublishInfo.SetSubscriberPermissions(permissions);
48 sptr<IRemoteObject> commonEventListener = nullptr;
49 int32_t funcResult = -1;
50 service->PublishCommonEvent(commonEventData, commonEventPublishInfo, commonEventListener, code, funcResult);
51 bool funcResult1 = false;
52 service->PublishCommonEvent(
53 commonEventData, commonEventPublishInfo, commonEventListener, code, code, code, funcResult1);
54 EventFwk::MatchingSkills matchingSkills;
55 matchingSkills.AddEvent(stringData);
56 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
57 subscribeInfo.SetPriority(code);
58 service->SubscribeCommonEvent(subscribeInfo, commonEventListener, 0, funcResult);
59 service->UnsubscribeCommonEvent(commonEventListener, funcResult);
60 service->UnsubscribeCommonEventSync(commonEventListener, funcResult);
61 service->GetStickyCommonEvent(stringData, commonEventData, funcResult1);
62 uint8_t dumpType = fdp->ConsumeIntegral<uint8_t>();
63 std::vector<std::string> state;
64 state.emplace_back(stringData);
65 service->DumpState(dumpType, stringData, code, state, funcResult1);
66 service->FinishReceiver(commonEventListener, code, stringData, enabled, funcResult1);
67 service->Freeze(code, funcResult1);
68 service->Unfreeze(code, funcResult1);
69 service->UnfreezeAll(funcResult1);
70 return true;
71 }
72 }
73
74 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)75 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
76 {
77 /* Run your code on data */
78 FuzzedDataProvider fdp(data, size);
79 std::vector<std::string> permissions;
80 NativeTokenGet(permissions);
81 OHOS::DoSomethingInterestingWithMyAPI(&fdp);
82 return 0;
83 }
84