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 commonEventData.GetWant();
43 commonEventData.GetCode();
44 commonEventData.GetData();
45 Parcel p;
46 commonEventData.Marshalling(p);
47 commonEventData.Unmarshalling(p);
48 // make commonEventPublishInfo info
49 EventFwk::CommonEventPublishInfo commonEventPublishInfo;
50 std::vector<std::string> permissions;
51 permissions.emplace_back(stringData);
52 commonEventPublishInfo.SetSubscriberPermissions(permissions);
53 sptr<IRemoteObject> commonEventListener = nullptr;
54 commonEventStub.PublishCommonEvent(commonEventData, commonEventPublishInfo, commonEventListener, code, code, code);
55 // test SubscribeCommonEvent function
56 EventFwk::MatchingSkills matchingSkills;
57 matchingSkills.AddEvent(stringData);
58 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
59 subscribeInfo.SetPriority(code);
60 commonEventStub.SubscribeCommonEvent(subscribeInfo, commonEventListener);
61 // test UnsubscribeCommonEvent function
62 commonEventStub.UnsubscribeCommonEvent(commonEventListener);
63 // test GetStickyCommonEvent function
64 commonEventStub.GetStickyCommonEvent(stringData, commonEventData);
65 // test DumpState function
66 uint8_t dumpType = *data;
67 std::vector<std::string> state;
68 state.emplace_back(stringData);
69 commonEventStub.DumpState(dumpType, stringData, code, state);
70 // test FinishReceiver function
71 commonEventStub.FinishReceiver(commonEventListener, code, stringData, enabled);
72 // test Freeze function
73 commonEventStub.Freeze(code);
74 // test Unfreeze function
75 commonEventStub.Unfreeze(code);
76 commonEventStub.OnRemoteRequest(code, dataParcel, reply, option);
77 // test UnfreezeAll function
78 return commonEventStub.UnfreezeAll();
79 }
80 }
81
82 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85 /* Run your code on data */
86 if (data == nullptr) {
87 return 0;
88 }
89
90 if (size < OHOS::U32_AT_SIZE) {
91 return 0;
92 }
93
94 char* ch = (char *)malloc(size + 1);
95 if (ch == nullptr) {
96 return 0;
97 }
98
99 (void)memset_s(ch, size + 1, 0x00, size + 1);
100 if (memcpy_s(ch, size, data, size) != EOK) {
101 free(ch);
102 ch = nullptr;
103 return 0;
104 }
105
106 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
107 free(ch);
108 ch = nullptr;
109 return 0;
110 }
111