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_client_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <functional>
21 #include <iostream>
22 #include <thread>
23 
24 #include <refbase.h>
25 
26 #include "constants_dinput.h"
27 #include "distributed_input_handler.h"
28 #include "distributed_input_kit.h"
29 #include "distributed_input_sink_handler.h"
30 #include "distributed_input_source_handler.h"
31 #include "i_distributed_sink_input.h"
32 #include "i_distributed_source_input.h"
33 
34 namespace OHOS {
35 namespace DistributedHardware {
36 class TestRegisterInputCallback : public OHOS::DistributedHardware::RegisterCallback {
37     public:
38         TestRegisterInputCallback() = default;
39         virtual ~TestRegisterInputCallback() = default;
OnRegisterResult(const std::string & devId,const std::string & dhId,int32_t status,const std::string & data)40         int32_t OnRegisterResult(const std::string &devId, const std::string &dhId, int32_t status,
41             const std::string &data) override
42         {
43             return 0;
44         }
45     };
46 
47 class TestUnregisterInputCallback : public OHOS::DistributedHardware::UnregisterCallback {
48 public:
49     TestUnregisterInputCallback() = default;
50     virtual ~TestUnregisterInputCallback() = default;
OnUnregisterResult(const std::string & devId,const std::string & dhId,int32_t status,const std::string & data)51     int32_t OnUnregisterResult(const std::string &devId, const std::string &dhId, int32_t status,
52         const std::string &data) override
53     {
54         return 0;
55     }
56 };
57 
RegisterDistributedHardwareFuzzTest(const uint8_t * data,size_t size)58 void RegisterDistributedHardwareFuzzTest(const uint8_t *data, size_t size)
59 {
60     if ((data == nullptr) || (size == 0)) {
61         return;
62     }
63 
64     std::string rDevId(reinterpret_cast<const char*>(data), size);
65     std::string rDhId(reinterpret_cast<const char*>(data), size);
66     std::string sinkVersion(reinterpret_cast<const char*>(data), size);
67     std::string sinkAttrs(reinterpret_cast<const char*>(data), size);
68     EnableParam enableParam;
69     enableParam.sinkVersion = sinkVersion;
70     enableParam.sinkAttrs = sinkAttrs;
71 
72     std::shared_ptr<TestRegisterInputCallback> registerCb = std::make_shared<TestRegisterInputCallback>();
73     std::shared_ptr<TestUnregisterInputCallback> unregisterCb = std::make_shared<TestUnregisterInputCallback>();
74 
75     DistributedInput::DistributedInputSourceHandler::GetInstance().RegisterDistributedHardware(
76         rDevId, rDhId, enableParam, registerCb);
77     DistributedInput::DistributedInputSourceHandler::GetInstance().UnregisterDistributedHardware(
78         rDevId, rDhId, unregisterCb);
79 }
80 } // namespace DistributedHardware
81 } // namespace OHOS
82 
83 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)84 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
85 {
86     /* Run your code on data */
87     OHOS::DistributedHardware::RegisterDistributedHardwareFuzzTest(data, size);
88     return 0;
89 }