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