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 "dumpstate_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 using namespace OHOS::EventFwk;
26
27 namespace OHOS {
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)28 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp)
29 {
30 sptr<CommonEventManagerService> service = CommonEventManagerService::GetInstance();
31 service->Init();
32
33 AAFwk::Want want;
34 want.SetAction(fdp->ConsumeRandomLengthString());
35 CommonEventData commonEventData;
36 commonEventData.SetWant(want);
37 commonEventData.SetCode(fdp->ConsumeIntegral<int32_t>());
38 commonEventData.SetData(fdp->ConsumeRandomLengthString());
39 CommonEventPublishInfo commonEventPublishInfo;
40 if (fdp->ConsumeBool()) {
41 std::vector<std::string> permissions;
42 permissions.emplace_back(fdp->ConsumeRandomLengthString());
43 commonEventPublishInfo.SetSubscriberPermissions(permissions);
44 }
45 commonEventPublishInfo.SetOrdered(fdp->ConsumeBool());
46
47 int32_t funcResult = -1;
48 service->PublishCommonEvent(commonEventData, commonEventPublishInfo,
49 fdp->ConsumeIntegralInRange<int32_t>(-3, 1000), funcResult);
50
51 uint8_t dumpType = fdp->ConsumeIntegral<uint8_t>();
52 std::vector<std::string> state;
53 bool funcResult1 = false;
54
55 service->DumpState(dumpType, fdp->ConsumeRandomLengthString(), fdp->ConsumeIntegralInRange<int32_t>(-3, 1000),
56 state, funcResult1);
57 usleep(10000);
58 return true;
59 }
60 }
61
62 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)63 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
64 {
65 /* Run your code on data */
66 FuzzedDataProvider fdp(data, size);
67 std::vector<std::string> permissions;
68 MockRandomToken(&fdp, permissions);
69 OHOS::DoSomethingInterestingWithMyAPI(&fdp);
70 return 0;
71 }
72