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 "fuzz_common_base.h"
19
20 namespace OHOS {
21 namespace {
22 constexpr size_t U32_AT_SIZE = 4;
23 }
DoSomethingInterestingWithMyAPI(FuzzData fuzzData)24 bool DoSomethingInterestingWithMyAPI(FuzzData fuzzData)
25 {
26 std::string stringData = fuzzData.GenerateRandomString();
27 int32_t code = fuzzData.GetData<int32_t>();
28 bool enabled = fuzzData.GenerateRandomBool();
29 std::shared_ptr<EventFwk::AsyncCommonEventResult> result = std::make_shared<EventFwk::AsyncCommonEventResult>(
30 code, stringData, enabled, enabled, nullptr);
31 if (result != nullptr) {
32 // test SetCode function
33 result->SetCode(code);
34 // test GetCode function
35 result->GetCode();
36 // test SetData function
37 result->SetData(stringData);
38 // test GetData function
39 result->GetData();
40 // test SetCodeAndData function
41 result->SetCodeAndData(code, stringData);
42 // test AbortCommonEvent function
43 result->AbortCommonEvent();
44 // test ClearAbortCommonEvent function
45 result->ClearAbortCommonEvent();
46 // test GetAbortCommonEvent function
47 result->GetAbortCommonEvent();
48 // test IsOrderedCommonEvent function
49 result->IsOrderedCommonEvent();
50 // test CheckSynchronous function
51 result->CheckSynchronous();
52 // test IsStickyCommonEvent function
53 return result->IsStickyCommonEvent();
54 }
55 return false;
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 if (size < OHOS::U32_AT_SIZE) {
67 return 0;
68 }
69 OHOS::FuzzData fuzzData(data, size);
70 OHOS::DoSomethingInterestingWithMyAPI(fuzzData);
71 return 0;
72 }
73