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 <string>
21 #include <vector>
22 
23 constexpr int8_t FUZZ_DATA_LEN = 4;
24 
25 namespace OHOS {
26 constexpr size_t U32_AT_SIZE = 4;
27 
DoSomethingInterestingWithMyAPI(FuzzData fuzzData)28 bool DoSomethingInterestingWithMyAPI(FuzzData fuzzData)
29 {
30     AAFwk::Want want;
31     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_TEST_ACTION1);
32     EventFwk::CommonEventData commonEventData;
33     commonEventData.SetWant(want);
34     int32_t code = fuzzData.GenerateRandomInt32();
35     commonEventData.SetCode(code);
36     std::string stringData = fuzzData.GenerateRandomString();
37     commonEventData.SetData(stringData);
38 
39     EventFwk::CommonEventPublishInfo commonEventPublishInfo;
40     std::vector<std::string> permissions;
41     permissions.emplace_back(stringData);
42     commonEventPublishInfo.SetSubscriberPermissions(permissions);
43 
44     std::shared_ptr<EventFwk::CommonEventSubscriber> subscriber = nullptr;
45 
46     int32_t userId = code + 100;
47 
48     // test PublishCommonEventAsUser and two paramter
49     EventFwk::CommonEventManager::PublishCommonEventAsUser(commonEventData, userId);
50 
51     if (fuzzData.GetSize() < FUZZ_DATA_LEN) {
52         EventFwk::CommonEventManager::PublishCommonEventAsUser(
53             commonEventData, commonEventPublishInfo, subscriber, userId);
54         EventFwk::CommonEventManager::NewPublishCommonEventAsUser(
55             commonEventData, commonEventPublishInfo, userId);
56         EventFwk::CommonEventManager::NewPublishCommonEventAsUser(
57             commonEventData, commonEventPublishInfo, subscriber, userId);
58     } else {
59         int32_t uid = fuzzData.GenerateRandomInt32();
60         EventFwk::CommonEventManager::PublishCommonEventAsUser(
61             commonEventData, commonEventPublishInfo, subscriber, uid, code, userId);
62     }
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     if (data == nullptr) {
72         return 0;
73     }
74 
75     if (size < OHOS::U32_AT_SIZE) {
76         return 0;
77     }
78     std::vector<std::string> permissions;
79     NativeTokenGet(permissions);
80     OHOS::FuzzData fuzzData(data, size);
81     OHOS::DoSomethingInterestingWithMyAPI(fuzzData);
82     return 0;
83 }
84