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 "async_common_event_result.h"
17 #include "asynccommoneventresult_fuzzer.h"
18 #include "securec.h"
19
20 namespace OHOS {
21 namespace {
22 constexpr size_t U32_AT_SIZE = 4;
23 constexpr uint8_t ENABLE = 2;
24 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)25 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
26 {
27 std::string stringData(data);
28 int32_t code = U32_AT(reinterpret_cast<const uint8_t*>(data));
29 bool enabled = *data % ENABLE;
30 std::shared_ptr<EventFwk::AsyncCommonEventResult> result = std::make_shared<EventFwk::AsyncCommonEventResult>(
31 code, stringData, enabled, enabled, nullptr);
32 if (result != nullptr) {
33 // test SetCode function
34 result->SetCode(code);
35 // test GetCode function
36 result->GetCode();
37 // test SetData function
38 result->SetData(stringData);
39 // test GetData function
40 result->GetData();
41 // test SetCodeAndData function
42 result->SetCodeAndData(code, stringData);
43 // test AbortCommonEvent function
44 result->AbortCommonEvent();
45 // test ClearAbortCommonEvent function
46 result->ClearAbortCommonEvent();
47 // test GetAbortCommonEvent function
48 result->GetAbortCommonEvent();
49 // test IsOrderedCommonEvent function
50 result->IsOrderedCommonEvent();
51 // test IsStickyCommonEvent function
52 return result->IsStickyCommonEvent();
53 } else {
54 return false;
55 }
56 }
57 }
58
59 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)60 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
61 {
62 /* Run your code on data */
63 if (data == nullptr) {
64 return 0;
65 }
66
67 if (size < OHOS::U32_AT_SIZE) {
68 return 0;
69 }
70
71 char* ch = (char *)malloc(size + 1);
72 if (ch == nullptr) {
73 return 0;
74 }
75
76 (void)memset_s(ch, size + 1, 0x00, size + 1);
77 if (memcpy_s(ch, size, data, size) != EOK) {
78 free(ch);
79 ch = nullptr;
80 return 0;
81 }
82
83 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
84 free(ch);
85 ch = nullptr;
86 return 0;
87 }
88