1 /*
2  * Copyright (c) 2022-2024 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 "distributed_input_transport_base_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <functional>
21 #include <iostream>
22 #include <thread>
23 #include <unistd.h>
24 
25 #include <refbase.h>
26 
27 #include "constants_dinput.h"
28 #include "distributed_input_transport_base.h"
29 
30 namespace OHOS {
31 namespace DistributedHardware {
StartSessionFuzzTest(const uint8_t * data,size_t size)32 void StartSessionFuzzTest(const uint8_t *data, size_t size)
33 {
34     if ((data == nullptr) || (size == 0)) {
35         return;
36     }
37 
38     std::string remoteDevId(reinterpret_cast<const char*>(data), size);
39 
40     const uint32_t sleepTimeUs = 100 * 1000;
41     usleep(sleepTimeUs);
42     DistributedInput::DistributedInputTransportBase::GetInstance().StartSession(remoteDevId);
43     DistributedInput::DistributedInputTransportBase::GetInstance().StopSession(remoteDevId);
44 }
45 
OnBytesReceivedFuzzTest(const uint8_t * data,size_t size)46 void OnBytesReceivedFuzzTest(const uint8_t *data, size_t size)
47 {
48     if ((data == nullptr) || (size < sizeof(int32_t))) {
49         return;
50     }
51 
52     int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
53     const char *msg = reinterpret_cast<const char *>(data);
54     uint32_t dataLen = static_cast<const uint32_t>(size);
55     DistributedInput::DistributedInputTransportBase::GetInstance().OnBytesReceived(sessionId, msg, dataLen);
56 }
57 } // namespace DistributedHardware
58 } // namespace OHOS
59 
60 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
62 {
63     /* Run your code on data */
64     OHOS::DistributedHardware::StartSessionFuzzTest(data, size);
65     OHOS::DistributedHardware::OnBytesReceivedFuzzTest(data, size);
66     return 0;
67 }