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