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