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 #define private public
17 #define protected public
18 #include "common_event_subscribe_info.h"
19 #undef private
20 #undef protected
21 
22 #include "subscribecommonevent_fuzzer.h"
23 #include "common_event_manager.h"
24 #include "fuzz_common_base.h"
25 
26 namespace OHOS {
27 namespace EventFwk {
28 class TestSubscriber : public CommonEventSubscriber {
29 public:
TestSubscriber(const CommonEventSubscribeInfo & sp)30     explicit TestSubscriber(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
31     {}
32 
~TestSubscriber()33     ~TestSubscriber()
34     {}
35 
OnReceiveEvent(const CommonEventData & data)36     void OnReceiveEvent(const CommonEventData &data) override
37     {}
38 };
39 }  // namespace EventFwk
40 
41 constexpr size_t U32_AT_SIZE = 4;
42 
DoSomethingInterestingWithMyAPI(FuzzData fuzzData)43 bool DoSomethingInterestingWithMyAPI(FuzzData fuzzData)
44 {
45     std::string stringData = fuzzData.GenerateRandomString();
46 
47     EventFwk::MatchingSkills matchingSkills;
48     Parcel parcel;
49     matchingSkills.AddEvent(stringData);
50     matchingSkills.AddEntity(stringData);
51     matchingSkills.AddScheme(stringData);
52     // set CommonEventSubscribeInfo and test CommonEventSubscribeInfo class function
53     uint8_t mode = fuzzData.GetData<uint8_t>();
54     EventFwk::CommonEventSubscribeInfo::ThreadMode threadMode =
55         EventFwk::CommonEventSubscribeInfo::ThreadMode(mode);
56     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
57     int32_t priority = fuzzData.GenerateRandomInt32();
58     subscribeInfo.ReadFromParcel(parcel);
59     subscribeInfo.Unmarshalling(parcel);
60     subscribeInfo.SetPriority(priority);
61     subscribeInfo.SetPermission(stringData);
62     subscribeInfo.SetDeviceId(stringData);
63     subscribeInfo.SetThreadMode(threadMode);
64     subscribeInfo.SetPublisherBundleName(fuzzData.GenerateRandomString());
65     subscribeInfo.GetPriority();
66     subscribeInfo.SetUserId(priority);
67     subscribeInfo.GetUserId();
68     subscribeInfo.GetPermission();
69     subscribeInfo.GetDeviceId();
70     subscribeInfo.GetMatchingSkills();
71     subscribeInfo.Marshalling(parcel);
72 
73     std::shared_ptr<EventFwk::TestSubscriber> subscriber =
74         std::make_shared<EventFwk::TestSubscriber>(subscribeInfo);
75     if (subscriber != nullptr) {
76         subscriber->IsOrderedCommonEvent();
77         subscriber->IsStickyCommonEvent();
78     }
79     return EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
80 }
81 }  // namespace OHOS
82 
83 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)84 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
85 {
86     /* Run your code on data */
87     if (data == nullptr) {
88         return 0;
89     }
90 
91     if (size < OHOS::U32_AT_SIZE) {
92         return 0;
93     }
94     OHOS::FuzzData fuzzData(data, size);
95     OHOS::DoSomethingInterestingWithMyAPI(fuzzData);
96     return 0;
97 }
98