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 "commoneventsubscriber_fuzzer.h"
17 #include "common_event.h"
18 #include "common_event_subscriber.h"
19 #include "securec.h"
20
21 namespace OHOS {
22 namespace EventFwk {
23 class TestSubscriber : public CommonEventSubscriber {
24 public:
TestSubscriber(const CommonEventSubscribeInfo & sp)25 explicit TestSubscriber(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
26 {}
27
~TestSubscriber()28 ~TestSubscriber()
29 {}
30
OnReceiveEvent(const CommonEventData & data)31 void OnReceiveEvent(const CommonEventData &data) override
32 {}
33 };
34 } // namespace EventFwk
35 namespace {
36 constexpr size_t U32_AT_SIZE = 4;
37 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)38 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
39 {
40 uint8_t dumpType = *data;
41 int32_t code = U32_AT(reinterpret_cast<const uint8_t*>(data));
42 std::string stringData(data);
43 std::vector<std::string> state;
44 state.emplace_back(stringData);
45 EventFwk::CommonEvent commonEvent;
46 // test PublishCommonEvent function
47 AAFwk::Want want;
48 EventFwk::CommonEventData commonEventData(want);
49 commonEventData.SetCode(code);
50 commonEventData.SetData(stringData);
51 // make commonEventPublishInfo info
52 EventFwk::CommonEventPublishInfo commonEventPublishInfo;
53 std::vector<std::string> permissions;
54 permissions.emplace_back(stringData);
55 commonEventPublishInfo.SetSubscriberPermissions(permissions);
56 commonEventPublishInfo.IsSticky();
57 commonEventPublishInfo.GetSubscriberPermissions();
58 commonEventPublishInfo.SetBundleName(stringData);
59 commonEventPublishInfo.GetBundleName();
60 // make CommonEventSubscriber info
61 EventFwk::MatchingSkills matchingSkills;
62 matchingSkills.AddEvent(stringData);
63
64 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
65 subscribeInfo.SetDeviceId(stringData);
66 std::shared_ptr<EventFwk::TestSubscriber> subscriber =
67 std::make_shared<EventFwk::TestSubscriber>(subscribeInfo);
68 if (subscriber != nullptr) {
69 subscriber->SetCode(code);
70 subscriber->GetCode();
71 subscriber->SetData(stringData);
72 subscriber->GetData();
73 subscriber->SetCodeAndData(code, stringData);
74 subscriber->AbortCommonEvent();
75 subscriber->ClearAbortCommonEvent();
76 subscriber->GetAbortCommonEvent();
77 subscriber->GoAsyncCommonEvent();
78 subscriber->GetSubscribeInfo();
79 subscriber->IsOrderedCommonEvent();
80 subscriber->IsStickyCommonEvent();
81 }
82 commonEvent.PublishCommonEvent(commonEventData, commonEventPublishInfo, subscriber);
83 // test PublishCommonEvent and four paramter
84 commonEvent.PublishCommonEvent(commonEventData, commonEventPublishInfo, subscriber, code, code);
85 // test DumpState function
86 return commonEvent.DumpState(dumpType, stringData, code, state);
87 }
88 }
89
90 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
92 {
93 /* Run your code on data */
94 if (data == nullptr) {
95 return 0;
96 }
97
98 if (size < OHOS::U32_AT_SIZE) {
99 return 0;
100 }
101
102 char* ch = (char *)malloc(size + 1);
103 if (ch == nullptr) {
104 return 0;
105 }
106
107 (void)memset_s(ch, size + 1, 0x00, size + 1);
108 if (memcpy_s(ch, size, data, size) != EOK) {
109 free(ch);
110 ch = nullptr;
111 return 0;
112 }
113
114 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
115 free(ch);
116 ch = nullptr;
117 return 0;
118 }
119