• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CheckSynchronous function
52         result->CheckSynchronous();
53         // test IsStickyCommonEvent function
54         return result->IsStickyCommonEvent();
55     } else {
56         return false;
57     }
58 }
59 }
60 
61 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
63 {
64     /* Run your code on data */
65     if (data == nullptr) {
66         return 0;
67     }
68 
69     if (size < OHOS::U32_AT_SIZE) {
70         return 0;
71     }
72 
73     char* ch = (char *)malloc(size + 1);
74     if (ch == nullptr) {
75         return 0;
76     }
77 
78     (void)memset_s(ch, size + 1, 0x00, size + 1);
79     if (memcpy_s(ch, size, data, size) != EOK) {
80         free(ch);
81         ch = nullptr;
82         return 0;
83     }
84 
85     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
86     free(ch);
87     ch = nullptr;
88     return 0;
89 }
90