1 /*
2  * Copyright (c) 2021-2023 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_sink_transport_fuzzer.h"
17 
18 #include <functional>
19 #include <iostream>
20 #include <thread>
21 
22 #include <refbase.h>
23 
24 #include "constants_dinput.h"
25 #include "distributed_input_sink_transport.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
RespPrepareRemoteInputFuzzTest(const uint8_t * data,size_t size)29 void RespPrepareRemoteInputFuzzTest(const uint8_t *data, size_t size)
30 {
31     if ((data == nullptr) || (size < sizeof(int32_t))) {
32         return;
33     }
34 
35     int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
36     std::string smsg(reinterpret_cast<const char*>(data), size);
37 
38     DistributedInput::DistributedInputSinkTransport::GetInstance().RespPrepareRemoteInput(sessionId, smsg);
39     DistributedInput::DistributedInputSinkTransport::GetInstance().RespUnprepareRemoteInput(sessionId, smsg);
40 }
41 
RespStartRemoteInputFuzzTest(const uint8_t * data,size_t size)42 void RespStartRemoteInputFuzzTest(const uint8_t *data, size_t size)
43 {
44     if ((data == nullptr) || (size < sizeof(int32_t))) {
45         return;
46     }
47 
48     int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
49     std::string smsg(reinterpret_cast<const char*>(data), size);
50 
51     DistributedInput::DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(sessionId, smsg);
52     DistributedInput::DistributedInputSinkTransport::GetInstance().RespStopRemoteInput(sessionId, smsg);
53 }
54 } // namespace DistributedHardware
55 } // namespace OHOS
56 
57 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)58 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
59 {
60     /* Run your code on data */
61     OHOS::DistributedHardware::RespPrepareRemoteInputFuzzTest(data, size);
62     OHOS::DistributedHardware::RespStartRemoteInputFuzzTest(data, size);
63     return 0;
64 }