1 /*
2 * Copyright (c) 2023 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_manager_service.h"
17 #include "common_event_data.h"
18 #include "commoneventmanagerservice_fuzzer.h"
19 #include "refbase.h"
20 #include "fuzz_common_base.h"
21 #include <string>
22 #include <vector>
23
24 namespace OHOS {
25 namespace {
26 constexpr size_t U32_AT_SIZE = 3;
27 }
DoSomethingInterestingWithMyAPI(FuzzData fuzzData)28 bool DoSomethingInterestingWithMyAPI(FuzzData fuzzData)
29 {
30 std::string stringData = fuzzData.GenerateRandomString();
31 int32_t code = fuzzData.GetData<int32_t>();
32 bool enabled = fuzzData.GenerateRandomBool();
33 MessageParcel dataParcel;
34 MessageParcel reply;
35 MessageOption option;
36 EventFwk::CommonEventManagerService::GetInstance();
37 sptr<EventFwk::CommonEventManagerService> service =
38 sptr<EventFwk::CommonEventManagerService>(new EventFwk::CommonEventManagerService());
39 service->Init();
40 AAFwk::Want want;
41 EventFwk::CommonEventData commonEventData;
42 commonEventData.SetWant(want);
43 commonEventData.SetCode(code);
44 commonEventData.SetData(stringData);
45
46 Parcel p;
47 commonEventData.Marshalling(p);
48 commonEventData.Unmarshalling(p);
49 EventFwk::CommonEventPublishInfo commonEventPublishInfo;
50 std::vector<std::string> permissions;
51 permissions.emplace_back(stringData);
52 commonEventPublishInfo.SetSubscriberPermissions(permissions);
53 sptr<IRemoteObject> commonEventListener = nullptr;
54 service->PublishCommonEvent(commonEventData, commonEventPublishInfo, commonEventListener, code);
55 service->PublishCommonEvent(
56 commonEventData, commonEventPublishInfo, commonEventListener, code, code, code);
57 EventFwk::MatchingSkills matchingSkills;
58 matchingSkills.AddEvent(stringData);
59 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
60 subscribeInfo.SetPriority(code);
61 service->SubscribeCommonEvent(subscribeInfo, commonEventListener);
62 service->UnsubscribeCommonEvent(commonEventListener);
63 service->GetStickyCommonEvent(stringData, commonEventData);
64 uint8_t dumpType = fuzzData.GetData<uint8_t>();
65 std::vector<std::string> state;
66 state.emplace_back(stringData);
67 service->DumpState(dumpType, stringData, code, state);
68 service->FinishReceiver(commonEventListener, code, stringData, enabled);
69 service->Freeze(code);
70 service->Unfreeze(code);
71 service->UnfreezeAll();
72 return true;
73 }
74 }
75
76 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
78 {
79 /* Run your code on data */
80 if (data == nullptr) {
81 return 0;
82 }
83
84 if (size < OHOS::U32_AT_SIZE) {
85 return 0;
86 }
87 std::vector<std::string> permissions;
88 NativeTokenGet(permissions);
89 OHOS::FuzzData fuzzData(data, size);
90 OHOS::DoSomethingInterestingWithMyAPI(fuzzData);
91 return 0;
92 }
93