• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "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 
28 namespace OHOS {
29 namespace DistributedHardware {
30 class PinHolderCallbackFuzzTest : public PinHolderCallback {
31 public:
~PinHolderCallbackFuzzTest()32     virtual ~PinHolderCallbackFuzzTest() {}
33 
OnPinHolderCreate(const std::string & deviceId,DmPinType pinType,const std::string & payload)34     void OnPinHolderCreate(const std::string &deviceId, DmPinType pinType, const std::string &payload) override {}
OnPinHolderDestroy(DmPinType pinType,const std::string & payload)35     void OnPinHolderDestroy(DmPinType pinType, const std::string &payload) override {}
OnCreateResult(int32_t result)36     void OnCreateResult(int32_t result) override {}
OnDestroyResult(int32_t result)37     void OnDestroyResult(int32_t result) override {}
38 };
39 
PinHolderFuzzTest(const uint8_t * data,size_t size)40 void PinHolderFuzzTest(const uint8_t* data, size_t size)
41 {
42     if ((data == nullptr) || (size < sizeof(uint16_t))) {
43         return;
44     }
45 
46     std::string pkgName(reinterpret_cast<const char*>(data), size);
47     std::string payload(reinterpret_cast<const char*>(data), size);
48     PeerTargetId peerTargetId;
49     peerTargetId.deviceId = pkgName;
50     peerTargetId.brMac = pkgName;
51     peerTargetId.bleMac = pkgName;
52     peerTargetId.wifiIp = pkgName;
53     peerTargetId.wifiPort = *(reinterpret_cast<const uint16_t*>(data));
54     uint16_t tmp = *(reinterpret_cast<const uint16_t*>(data));
55     DmPinType pinType = static_cast<DmPinType>(tmp);
56 
57     std::shared_ptr<PinHolderCallback> pinHolderCallback = std::make_shared<PinHolderCallbackFuzzTest>();
58 
59     DeviceManager::GetInstance().RegisterPinHolderCallback(pkgName, pinHolderCallback);
60     DeviceManager::GetInstance().CreatePinHolder(pkgName, peerTargetId, pinType, payload);
61     DeviceManager::GetInstance().DestroyPinHolder(pkgName, peerTargetId, pinType, payload);
62 }
63 }
64 }
65 
66 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
68 {
69     /* Run your code on data */
70     OHOS::DistributedHardware::PinHolderFuzzTest(data, size);
71 
72     return 0;
73 }
74