• 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 #define private public
17 #define protected public
18 #include "common_event_subscribe_info.h"
19 #undef private
20 #undef protected
21 
22 #include "subscribecommonevent_fuzzer.h"
23 #include "securec.h"
24 #include "common_event_manager.h"
25 
26 namespace OHOS {
27 namespace EventFwk {
28 class TestSubscriber : public CommonEventSubscriber {
29 public:
TestSubscriber(const CommonEventSubscribeInfo & sp)30     explicit TestSubscriber(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
31     {}
32 
~TestSubscriber()33     ~TestSubscriber()
34     {}
35 
OnReceiveEvent(const CommonEventData & data)36     void OnReceiveEvent(const CommonEventData &data) override
37     {}
38 };
39 }  // namespace EventFwk
40 
41 constexpr size_t U32_AT_SIZE = 4;
42 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)43 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
44 {
45     std::string stringData(data);
46 
47     EventFwk::MatchingSkills matchingSkills;
48     Parcel parcel;
49     matchingSkills.AddEvent(stringData);
50     matchingSkills.AddEntity(stringData);
51     matchingSkills.AddScheme(stringData);
52     // set CommonEventSubscribeInfo and test CommonEventSubscribeInfo class function
53     uint8_t mode = *data % U32_AT_SIZE;
54     EventFwk::CommonEventSubscribeInfo::ThreadMode threadMode =
55         EventFwk::CommonEventSubscribeInfo::ThreadMode(mode);
56     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
57     int32_t priority = U32_AT(reinterpret_cast<const uint8_t*>(data));
58     subscribeInfo.ReadFromParcel(parcel);
59     subscribeInfo.Unmarshalling(parcel);
60     subscribeInfo.SetPriority(priority);
61     subscribeInfo.SetPermission(stringData);
62     subscribeInfo.SetDeviceId(stringData);
63     subscribeInfo.SetThreadMode(threadMode);
64     subscribeInfo.GetPriority();
65     subscribeInfo.SetUserId(priority);
66     subscribeInfo.GetUserId();
67     subscribeInfo.GetPermission();
68     subscribeInfo.GetDeviceId();
69 
70     std::shared_ptr<EventFwk::TestSubscriber> subscriber =
71         std::make_shared<EventFwk::TestSubscriber>(subscribeInfo);
72     if (subscriber != nullptr) {
73         subscriber->IsOrderedCommonEvent();
74         subscriber->IsStickyCommonEvent();
75     }
76     return EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
77 }
78 }  // namespace OHOS
79 
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
82 {
83     /* Run your code on data */
84     if (data == nullptr) {
85         return 0;
86     }
87 
88     if (size < OHOS::U32_AT_SIZE) {
89         return 0;
90     }
91 
92     char* ch = (char *)malloc(size + 1);
93     if (ch == nullptr) {
94         return 0;
95     }
96 
97     (void)memset_s(ch, size + 1, 0x00, size + 1);
98     if (memcpy_s(ch, size, data, size) != EOK) {
99         free(ch);
100         ch = nullptr;
101         return 0;
102     }
103 
104     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
105     free(ch);
106     ch = nullptr;
107     return 0;
108 }
109