1 /*
2 * Copyright (c) 2025 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 #include "publishcommonevent_fuzzer.h"
16
17 #include "common_event_data.h"
18 #include "common_event_manager_service.h"
19 #include "refbase.h"
20 #include "fuzz_common_base.h"
21 #include <fuzzer/FuzzedDataProvider.h>
22 #include <string>
23 #include <vector>
24
25 using namespace OHOS::EventFwk;
26
27 namespace OHOS {
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)28 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp)
29 {
30
31 sptr<CommonEventManagerService> service = CommonEventManagerService::GetInstance();
32 service->Init();
33
34 AAFwk::Want want;
35 want.SetAction(fdp->ConsumeRandomLengthString());
36 CommonEventData commonEventData;
37 commonEventData.SetWant(want);
38 commonEventData.SetCode(fdp->ConsumeIntegral<int32_t>());
39 commonEventData.SetData(fdp->ConsumeRandomLengthString());
40 CommonEventPublishInfo commonEventPublishInfo;
41 if (fdp->ConsumeBool()) {
42 std::vector<std::string> permissions;
43 permissions.emplace_back(fdp->ConsumeRandomLengthString());
44 commonEventPublishInfo.SetSubscriberPermissions(permissions);
45 }
46 commonEventPublishInfo.SetOrdered(fdp->ConsumeBool());
47 sptr<IRemoteObject> commonEventListener = nullptr;
48
49 int32_t funcResult = -1;
50 service->PublishCommonEvent(commonEventData, commonEventPublishInfo, commonEventListener,
51 fdp->ConsumeIntegralInRange<int32_t>(-3, 1000), funcResult);
52 service->PublishCommonEvent(commonEventData, commonEventPublishInfo,
53 fdp->ConsumeIntegralInRange<int32_t>(-3, 1000), funcResult);
54
55 bool funcResult1 = false;
56 service->PublishCommonEvent(commonEventData, commonEventPublishInfo, commonEventListener,
57 fdp->ConsumeIntegral<uint32_t>(), fdp->ConsumeIntegral<int32_t>(),
58 fdp->ConsumeIntegralInRange<int32_t>(-3, 1000), funcResult1);
59 service->PublishCommonEvent(commonEventData, commonEventPublishInfo,
60 fdp->ConsumeIntegral<uint32_t>(), fdp->ConsumeIntegral<int32_t>(),
61 fdp->ConsumeIntegralInRange<int32_t>(-3, 1000), funcResult1);
62 usleep(10000);
63 return true;
64 }
65 }
66
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
69 {
70 /* Run your code on data */
71 FuzzedDataProvider fdp(data, size);
72 std::vector<std::string> permissions;
73 if (fdp.ConsumeBool()) {
74 permissions.push_back("ohos.permission.COMMONEVEVT_STICKY");
75 }
76 MockRandomToken(&fdp, permissions);
77
78 OHOS::DoSomethingInterestingWithMyAPI(&fdp);
79 return 0;
80 }
81