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 #include "publishcommonevent_fuzzer.h"
17 #include "securec.h"
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "fuzz_common_base.h"
21 #include <fuzzer/FuzzedDataProvider.h>
22 #include <string>
23 #include <vector>
24
25 namespace OHOS {
26
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)27 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp)
28 {
29 AAFwk::Want want;
30 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_TEST_ACTION1);
31 EventFwk::CommonEventData commonEventData;
32 commonEventData.SetWant(want);
33 int32_t code = fdp->ConsumeIntegral<int32_t>();
34 commonEventData.SetCode(code);
35 std::string stringData = fdp->ConsumeRandomLengthString();
36 commonEventData.SetData(stringData);
37
38 EventFwk::CommonEventPublishInfo commonEventPublishInfo;
39 std::vector<std::string> permissions;
40 permissions.emplace_back(stringData);
41 commonEventPublishInfo.SetSubscriberPermissions(permissions);
42
43 std::shared_ptr<EventFwk::CommonEventSubscriber> subscriber = nullptr;
44
45 // test pubilshCommonEvent function and two paramter
46 EventFwk::CommonEventManager::PublishCommonEvent(commonEventData, commonEventPublishInfo);
47 EventFwk::CommonEventManager::NewPublishCommonEvent(commonEventData, commonEventPublishInfo);
48
49 EventFwk::CommonEventManager::PublishCommonEvent(commonEventData, commonEventPublishInfo, subscriber);
50 int32_t uid = fdp->ConsumeIntegral<int32_t>();
51 EventFwk::CommonEventManager::PublishCommonEvent(
52 commonEventData, commonEventPublishInfo, subscriber, uid, code);
53 return true;
54 }
55 }
56
57 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)58 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
59 {
60 /* Run your code on data */
61 FuzzedDataProvider fdp(data, size);
62 std::vector<std::string> permissions;
63 NativeTokenGet(permissions);
64 OHOS::DoSomethingInterestingWithMyAPI(&fdp);
65 return 0;
66 }
67