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_stub.h"
17 #include "common_event_data.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 MessageParcel dataParcel;
29 MessageParcel reply;
30 MessageOption option;
31 EventFwk::CommonEventStub commonEventStub;
32 // test PublishCommonEvent function
33 AAFwk::Want want;
34 EventFwk::CommonEventData commonEventData;
35 EventFwk::CommonEventData eventData(want, code, stringData);
36 commonEventData.SetWant(want);
37 commonEventData.SetCode(code);
38 commonEventData.SetData(stringData);
39 commonEventData.GetWant();
40 commonEventData.GetCode();
41 commonEventData.GetData();
42 Parcel p;
43 commonEventData.Marshalling(p);
44 commonEventData.Unmarshalling(p);
45 // make commonEventPublishInfo info
46 EventFwk::CommonEventPublishInfo commonEventPublishInfo;
47 std::vector<std::string> permissions;
48 permissions.emplace_back(stringData);
49 commonEventPublishInfo.SetSubscriberPermissions(permissions);
50 sptr<IRemoteObject> commonEventListener = nullptr;
51 commonEventStub.PublishCommonEvent(commonEventData, commonEventPublishInfo, commonEventListener, code, code, code);
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);
58 // test UnsubscribeCommonEvent function
59 commonEventStub.UnsubscribeCommonEvent(commonEventListener);
60 // test UnsubscribeCommonEventSync function
61 commonEventStub.UnsubscribeCommonEventSync(commonEventListener);
62 // test GetStickyCommonEvent function
63 commonEventStub.GetStickyCommonEvent(stringData, commonEventData);
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);
69 // test FinishReceiver function
70 commonEventStub.FinishReceiver(commonEventListener, code, stringData, enabled);
71 // test Freeze function
72 commonEventStub.Freeze(code);
73 // test Unfreeze function
74 commonEventStub.Unfreeze(code);
75 commonEventStub.OnRemoteRequest(code, dataParcel, reply, option);
76 // test UnfreezeAll function
77 return commonEventStub.UnfreezeAll();
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