179a732c7Sopenharmony_ci/* 279a732c7Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd. 379a732c7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 479a732c7Sopenharmony_ci * you may not use this file except in compliance with the License. 579a732c7Sopenharmony_ci * You may obtain a copy of the License at 679a732c7Sopenharmony_ci * 779a732c7Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 879a732c7Sopenharmony_ci * 979a732c7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1079a732c7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1179a732c7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1279a732c7Sopenharmony_ci * See the License for the specific language governing permissions and 1379a732c7Sopenharmony_ci * limitations under the License. 1479a732c7Sopenharmony_ci */ 1579a732c7Sopenharmony_ci 1679a732c7Sopenharmony_ci#include "pinholder_fuzzer.h" 1779a732c7Sopenharmony_ci 1879a732c7Sopenharmony_ci#include "device_manager.h" 1979a732c7Sopenharmony_ci#include "device_manager_callback.h" 2079a732c7Sopenharmony_ci#include "dm_ability_manager.h" 2179a732c7Sopenharmony_ci#include "dm_constants.h" 2279a732c7Sopenharmony_ci#include "dm_log.h" 2379a732c7Sopenharmony_ci#include "pin_holder.h" 2479a732c7Sopenharmony_ci#include "nlohmann/json.hpp" 2579a732c7Sopenharmony_ci#include "parameter.h" 2679a732c7Sopenharmony_ci#include "device_manager_service_listener.h" 2779a732c7Sopenharmony_ci 2879a732c7Sopenharmony_cinamespace OHOS { 2979a732c7Sopenharmony_cinamespace DistributedHardware { 3079a732c7Sopenharmony_ciclass PinHolderCallbackFuzzTest : public PinHolderCallback { 3179a732c7Sopenharmony_cipublic: 3279a732c7Sopenharmony_ci virtual ~PinHolderCallbackFuzzTest() {} 3379a732c7Sopenharmony_ci 3479a732c7Sopenharmony_ci void OnPinHolderCreate(const std::string &deviceId, DmPinType pinType, const std::string &payload) override {} 3579a732c7Sopenharmony_ci void OnPinHolderDestroy(DmPinType pinType, const std::string &payload) override {} 3679a732c7Sopenharmony_ci void OnCreateResult(int32_t result) override {} 3779a732c7Sopenharmony_ci void OnDestroyResult(int32_t result) override {} 3879a732c7Sopenharmony_ci void OnPinHolderEvent(DmPinHolderEvent event, int32_t result, const std::string &content) override {} 3979a732c7Sopenharmony_ci}; 4079a732c7Sopenharmony_ci 4179a732c7Sopenharmony_civoid PinHolderFuzzTest(const uint8_t* data, size_t size) 4279a732c7Sopenharmony_ci{ 4379a732c7Sopenharmony_ci if ((data == nullptr) || (size < sizeof(uint16_t))) { 4479a732c7Sopenharmony_ci return; 4579a732c7Sopenharmony_ci } 4679a732c7Sopenharmony_ci 4779a732c7Sopenharmony_ci std::string pkgName(reinterpret_cast<const char*>(data), size); 4879a732c7Sopenharmony_ci std::string payload(reinterpret_cast<const char*>(data), size); 4979a732c7Sopenharmony_ci PeerTargetId peerTargetId; 5079a732c7Sopenharmony_ci peerTargetId.deviceId = pkgName; 5179a732c7Sopenharmony_ci peerTargetId.brMac = pkgName; 5279a732c7Sopenharmony_ci peerTargetId.bleMac = pkgName; 5379a732c7Sopenharmony_ci peerTargetId.wifiIp = pkgName; 5479a732c7Sopenharmony_ci peerTargetId.wifiPort = *(reinterpret_cast<const uint16_t*>(data)); 5579a732c7Sopenharmony_ci uint16_t tmp = *(reinterpret_cast<const uint16_t*>(data)); 5679a732c7Sopenharmony_ci DmPinType pinType = static_cast<DmPinType>(tmp); 5779a732c7Sopenharmony_ci 5879a732c7Sopenharmony_ci std::shared_ptr<PinHolderCallback> pinHolderCallback = std::make_shared<PinHolderCallbackFuzzTest>(); 5979a732c7Sopenharmony_ci 6079a732c7Sopenharmony_ci DeviceManager::GetInstance().RegisterPinHolderCallback(pkgName, pinHolderCallback); 6179a732c7Sopenharmony_ci DeviceManager::GetInstance().CreatePinHolder(pkgName, peerTargetId, pinType, payload); 6279a732c7Sopenharmony_ci DeviceManager::GetInstance().DestroyPinHolder(pkgName, peerTargetId, pinType, payload); 6379a732c7Sopenharmony_ci} 6479a732c7Sopenharmony_ci} 6579a732c7Sopenharmony_ci} 6679a732c7Sopenharmony_ci 6779a732c7Sopenharmony_ci/* Fuzzer entry point */ 6879a732c7Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 6979a732c7Sopenharmony_ci{ 7079a732c7Sopenharmony_ci /* Run your code on data */ 7179a732c7Sopenharmony_ci OHOS::DistributedHardware::PinHolderFuzzTest(data, size); 7279a732c7Sopenharmony_ci 7379a732c7Sopenharmony_ci return 0; 7479a732c7Sopenharmony_ci} 75