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