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 
21 namespace OHOS {
22 namespace {
23     constexpr size_t U32_AT_SIZE = 4;
24 }
DoSomethingInterestingWithMyAPI(FuzzData fuzzData)25 bool DoSomethingInterestingWithMyAPI(FuzzData fuzzData)
26 {
27     std::string stringData = fuzzData.GenerateRandomString();
28     int32_t code = fuzzData.GetData<int32_t>();
29     bool enabled = fuzzData.GenerateRandomBool();
30     MessageParcel dataParcel;
31     MessageParcel reply;
32     MessageOption option;
33     EventFwk::CommonEventStub commonEventStub;
34     // test PublishCommonEvent function
35     AAFwk::Want want;
36     EventFwk::CommonEventData commonEventData;
37     EventFwk::CommonEventData eventData(want, code, stringData);
38     commonEventData.SetWant(want);
39     commonEventData.SetCode(code);
40     commonEventData.SetData(stringData);
41     commonEventData.GetWant();
42     commonEventData.GetCode();
43     commonEventData.GetData();
44     Parcel p;
45     commonEventData.Marshalling(p);
46     commonEventData.Unmarshalling(p);
47     // make commonEventPublishInfo info
48     EventFwk::CommonEventPublishInfo commonEventPublishInfo;
49     std::vector<std::string> permissions;
50     permissions.emplace_back(stringData);
51     commonEventPublishInfo.SetSubscriberPermissions(permissions);
52     sptr<IRemoteObject> commonEventListener = nullptr;
53     commonEventStub.PublishCommonEvent(commonEventData, commonEventPublishInfo, commonEventListener, code, code, code);
54     // test SubscribeCommonEvent function
55     EventFwk::MatchingSkills matchingSkills;
56     matchingSkills.AddEvent(stringData);
57     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
58     subscribeInfo.SetPriority(code);
59     commonEventStub.SubscribeCommonEvent(subscribeInfo, commonEventListener);
60     // test UnsubscribeCommonEvent function
61     commonEventStub.UnsubscribeCommonEvent(commonEventListener);
62     // test GetStickyCommonEvent function
63     commonEventStub.GetStickyCommonEvent(stringData, commonEventData);
64     // test DumpState function
65     uint8_t dumpType = fuzzData.GetData<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     if (data == nullptr) {
86         return 0;
87     }
88 
89     if (size < OHOS::U32_AT_SIZE) {
90         return 0;
91     }
92 
93     OHOS::FuzzData fuzzData(data, size);
94     OHOS::DoSomethingInterestingWithMyAPI(fuzzData);
95     return 0;
96 }
97