1/*
2 * Copyright (c) 2023-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 "pinholder_fuzzer.h"
17
18#include "device_manager.h"
19#include "device_manager_callback.h"
20#include "dm_ability_manager.h"
21#include "dm_constants.h"
22#include "dm_log.h"
23#include "pin_holder.h"
24#include "nlohmann/json.hpp"
25#include "parameter.h"
26#include "device_manager_service_listener.h"
27
28namespace OHOS {
29namespace DistributedHardware {
30class PinHolderCallbackFuzzTest : public PinHolderCallback {
31public:
32    virtual ~PinHolderCallbackFuzzTest() {}
33
34    void OnPinHolderCreate(const std::string &deviceId, DmPinType pinType, const std::string &payload) override {}
35    void OnPinHolderDestroy(DmPinType pinType, const std::string &payload) override {}
36    void OnCreateResult(int32_t result) override {}
37    void OnDestroyResult(int32_t result) override {}
38    void OnPinHolderEvent(DmPinHolderEvent event, int32_t result, const std::string &content) override {}
39};
40
41void PinHolderFuzzTest(const uint8_t* data, size_t size)
42{
43    if ((data == nullptr) || (size < sizeof(uint16_t))) {
44        return;
45    }
46
47    std::string pkgName(reinterpret_cast<const char*>(data), size);
48    std::string payload(reinterpret_cast<const char*>(data), size);
49    PeerTargetId peerTargetId;
50    peerTargetId.deviceId = pkgName;
51    peerTargetId.brMac = pkgName;
52    peerTargetId.bleMac = pkgName;
53    peerTargetId.wifiIp = pkgName;
54    peerTargetId.wifiPort = *(reinterpret_cast<const uint16_t*>(data));
55    uint16_t tmp = *(reinterpret_cast<const uint16_t*>(data));
56    DmPinType pinType = static_cast<DmPinType>(tmp);
57
58    std::shared_ptr<PinHolderCallback> pinHolderCallback = std::make_shared<PinHolderCallbackFuzzTest>();
59
60    DeviceManager::GetInstance().RegisterPinHolderCallback(pkgName, pinHolderCallback);
61    DeviceManager::GetInstance().CreatePinHolder(pkgName, peerTargetId, pinType, payload);
62    DeviceManager::GetInstance().DestroyPinHolder(pkgName, peerTargetId, pinType, payload);
63}
64}
65}
66
67/* Fuzzer entry point */
68extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
69{
70    /* Run your code on data */
71    OHOS::DistributedHardware::PinHolderFuzzTest(data, size);
72
73    return 0;
74}
75