• 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 "publishcommoneventasuser_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 = 4;
22 
23 namespace OHOS {
24 constexpr size_t U32_AT_SIZE = 4;
25 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)26 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
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 = U32_AT(reinterpret_cast<const uint8_t*>(data));
33     commonEventData.SetCode(code);
34     std::string stringData(data);
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     if (size < FUZZ_DATA_LEN) {
50         return EventFwk::CommonEventManager::PublishCommonEventAsUser(
51             commonEventData, commonEventPublishInfo, subscriber, userId);
52     } else {
53         int32_t uid = U32_AT(reinterpret_cast<const uint8_t*>(data));
54         return EventFwk::CommonEventManager::PublishCommonEventAsUser(
55             commonEventData, commonEventPublishInfo, subscriber, uid, code, userId);
56     }
57 }
58 }
59 
60 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
62 {
63     /* Run your code on data */
64     if (data == nullptr) {
65         return 0;
66     }
67 
68     if (size < OHOS::U32_AT_SIZE) {
69         return 0;
70     }
71 
72     char* ch = (char *)malloc(size + 1);
73     if (ch == nullptr) {
74         return 0;
75     }
76 
77     (void)memset_s(ch, size + 1, 0x00, size + 1);
78     if (memcpy_s(ch, size, data, size) != EOK) {
79         free(ch);
80         ch = nullptr;
81         return 0;
82     }
83 
84     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
85     free(ch);
86     ch = nullptr;
87     return 0;
88 }
89