• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 constexpr int8_t FUZZ_DATA_LEN = 3;
22 
23 namespace OHOS {
24 constexpr size_t U32_AT_SIZE = 4;
DoSomethingInterestingWithMyAPI(const char * data,size_t size)25 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
26 {
27     AAFwk::Want want;
28     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_TEST_ACTION1);
29     EventFwk::CommonEventData commonEventData;
30     commonEventData.SetWant(want);
31     int32_t code = U32_AT(reinterpret_cast<const uint8_t*>(data));
32     commonEventData.SetCode(code);
33     std::string stringData(data);
34     commonEventData.SetData(stringData);
35 
36     EventFwk::CommonEventPublishInfo commonEventPublishInfo;
37     std::vector<std::string> permissions;
38     permissions.emplace_back(stringData);
39     commonEventPublishInfo.SetSubscriberPermissions(permissions);
40 
41     std::shared_ptr<EventFwk::CommonEventSubscriber> subscriber = nullptr;
42 
43     // test pubilshCommonEvent function and two paramter
44     EventFwk::CommonEventManager::PublishCommonEvent(commonEventData, commonEventPublishInfo);
45 
46     if (size < FUZZ_DATA_LEN) {
47         return EventFwk::CommonEventManager::PublishCommonEvent(
48             commonEventData, commonEventPublishInfo, subscriber);
49     } else {
50         int32_t uid = U32_AT(reinterpret_cast<const uint8_t*>(data));
51         return EventFwk::CommonEventManager::PublishCommonEvent(
52             commonEventData, commonEventPublishInfo, subscriber, uid, code);
53     }
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     if (data == nullptr) {
62         return 0;
63     }
64 
65     if (size < OHOS::U32_AT_SIZE) {
66         return 0;
67     }
68 
69     char* ch = (char *)malloc(size + 1);
70     if (ch == nullptr) {
71         return 0;
72     }
73 
74     (void)memset_s(ch, size + 1, 0x00, size + 1);
75     if (memcpy_s(ch, size, data, size) != EOK) {
76         free(ch);
77         ch = nullptr;
78         return 0;
79     }
80 
81     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
82     free(ch);
83     ch = nullptr;
84     return 0;
85 }
86