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 "common_event_stub.h"
17 #include "common_event_data.h"
18 #include "commoneventstub_fuzzer.h"
19 #include "securec.h"
20
21 namespace OHOS {
22 namespace {
23 constexpr size_t U32_AT_SIZE = 4;
24 constexpr uint8_t ENABLE = 2;
25 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)26 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
27 {
28 std::string stringData(data);
29 int32_t code = U32_AT(reinterpret_cast<const uint8_t*>(data));
30 bool enabled = *data % ENABLE;
31 MessageParcel dataParcel;
32 MessageParcel reply;
33 MessageOption option;
34 EventFwk::CommonEventStub commonEventStub;
35 // test PublishCommonEvent function
36 AAFwk::Want want;
37 EventFwk::CommonEventData commonEventData;
38 EventFwk::CommonEventData eventData(want, code, stringData);
39 commonEventData.SetWant(want);
40 commonEventData.SetCode(code);
41 commonEventData.SetData(stringData);
42 // make commonEventPublishInfo info
43 EventFwk::CommonEventPublishInfo commonEventPublishInfo;
44 std::vector<std::string> permissions;
45 permissions.emplace_back(stringData);
46 commonEventPublishInfo.SetSubscriberPermissions(permissions);
47 sptr<IRemoteObject> commonEventListener = nullptr;
48 commonEventStub.PublishCommonEvent(commonEventData, commonEventPublishInfo, commonEventListener, code, code, code);
49 // test SubscribeCommonEvent function
50 EventFwk::MatchingSkills matchingSkills;
51 matchingSkills.AddEvent(stringData);
52 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
53 subscribeInfo.SetPriority(code);
54 commonEventStub.SubscribeCommonEvent(subscribeInfo, commonEventListener);
55 // test UnsubscribeCommonEvent function
56 commonEventStub.UnsubscribeCommonEvent(commonEventListener);
57 // test GetStickyCommonEvent function
58 commonEventStub.GetStickyCommonEvent(stringData, commonEventData);
59 // test DumpState function
60 uint8_t dumpType = *data;
61 std::vector<std::string> state;
62 state.emplace_back(stringData);
63 commonEventStub.DumpState(dumpType, stringData, code, state);
64 // test FinishReceiver function
65 commonEventStub.FinishReceiver(commonEventListener, code, stringData, enabled);
66 // test Freeze function
67 commonEventStub.Freeze(code);
68 // test Unfreeze function
69 commonEventStub.Unfreeze(code);
70 commonEventStub.OnRemoteRequest(code, dataParcel, reply, option);
71 // test UnfreezeAll function
72 return commonEventStub.UnfreezeAll();
73 }
74 }
75
76 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
78 {
79 /* Run your code on data */
80 if (data == nullptr) {
81 return 0;
82 }
83
84 if (size < OHOS::U32_AT_SIZE) {
85 return 0;
86 }
87
88 char* ch = (char *)malloc(size + 1);
89 if (ch == nullptr) {
90 return 0;
91 }
92
93 (void)memset_s(ch, size + 1, 0x00, size + 1);
94 if (memcpy_s(ch, size, data, size) != EOK) {
95 free(ch);
96 ch = nullptr;
97 return 0;
98 }
99
100 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
101 free(ch);
102 ch = nullptr;
103 return 0;
104 }
105