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 MessageParcel dataParcel;
32 MessageParcel reply;
33 MessageOption option;
34 EventFwk::CommonEventManagerService::GetInstance();
35 sptr<EventFwk::CommonEventManagerService> service =
36 sptr<EventFwk::CommonEventManagerService>(new EventFwk::CommonEventManagerService());
37 service->Init();
38 AAFwk::Want want;
39 EventFwk::CommonEventData commonEventData;
40 commonEventData.SetWant(want);
41 commonEventData.SetCode(code);
42 commonEventData.SetData(stringData);
43
44 Parcel p;
45 commonEventData.Marshalling(p);
46 commonEventData.Unmarshalling(p);
47 EventFwk::CommonEventPublishInfo commonEventPublishInfo;
48 std::vector<std::string> permissions;
49 permissions.emplace_back(stringData);
50 commonEventPublishInfo.SetSubscriberPermissions(permissions);
51 sptr<IRemoteObject> commonEventListener = nullptr;
52 service->PublishCommonEvent(commonEventData, commonEventPublishInfo, commonEventListener, code);
53 service->PublishCommonEvent(
54 commonEventData, commonEventPublishInfo, commonEventListener, code, code, code);
55 EventFwk::MatchingSkills matchingSkills;
56 matchingSkills.AddEvent(stringData);
57 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
58 subscribeInfo.SetPriority(code);
59 service->SubscribeCommonEvent(subscribeInfo, commonEventListener);
60 service->UnsubscribeCommonEvent(commonEventListener);
61 service->UnsubscribeCommonEventSync(commonEventListener);
62 service->GetStickyCommonEvent(stringData, commonEventData);
63 uint8_t dumpType = fdp->ConsumeIntegral<uint8_t>();
64 std::vector<std::string> state;
65 state.emplace_back(stringData);
66 service->DumpState(dumpType, stringData, code, state);
67 service->FinishReceiver(commonEventListener, code, stringData, enabled);
68 service->Freeze(code);
69 service->Unfreeze(code);
70 service->UnfreezeAll();
71 return true;
72 }
73 }
74
75 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
77 {
78 /* Run your code on data */
79 FuzzedDataProvider fdp(data, size);
80 std::vector<std::string> permissions;
81 NativeTokenGet(permissions);
82 OHOS::DoSomethingInterestingWithMyAPI(&fdp);
83 return 0;
84 }
85