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