1 /*
2 * Copyright (c) 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 "dhcommtool_fuzzer.h"
17
18 #include <algorithm>
19 #include <chrono>
20 #include <cstddef>
21 #include <cstdint>
22 #include <string>
23 #include <unistd.h>
24
25 #include "constants.h"
26 #include "dh_transport.h"
27 #include "dh_comm_tool.h"
28 #include "socket.h"
29 #include "softbus_bus_center.h"
30
31 namespace OHOS {
32 namespace DistributedHardware {
33 namespace {
34 const int32_t SOCKETID = 1;
35 }
DhTransportTriggerReqFullDHCapsFuzzTest(const uint8_t * data,size_t size)36 void DhTransportTriggerReqFullDHCapsFuzzTest(const uint8_t* data, size_t size)
37 {
38 if ((data == nullptr) || (size == 0)) {
39 return;
40 }
41 std::string remoteNetworkId(reinterpret_cast<const char*>(data), size);
42 std::shared_ptr<DHCommTool> dhCommTool = std::make_shared<DHCommTool>();
43 dhCommTool->dhTransportPtr_ = std::make_shared<DHTransport>(dhCommTool);
44 dhCommTool->dhTransportPtr_->remoteDevSocketIds_[remoteNetworkId] = SOCKETID;
45 dhCommTool->TriggerReqFullDHCaps(remoteNetworkId);
46 dhCommTool->dhTransportPtr_->remoteDevSocketIds_.clear();
47 dhCommTool->dhTransportPtr_ = nullptr;
48 }
49
DhTransportGetAndSendLocalFullCapsFuzzTest(const uint8_t * data,size_t size)50 void DhTransportGetAndSendLocalFullCapsFuzzTest(const uint8_t* data, size_t size)
51 {
52 if ((data == nullptr) || (size == 0)) {
53 return;
54 }
55 std::string remoteNetworkId(reinterpret_cast<const char*>(data), size);
56 std::shared_ptr<DHCommTool> dhCommTool = std::make_shared<DHCommTool>();
57 dhCommTool->dhTransportPtr_ = std::make_shared<DHTransport>(dhCommTool);
58 dhCommTool->GetAndSendLocalFullCaps(remoteNetworkId);
59 dhCommTool->dhTransportPtr_ = nullptr;
60 }
61
DhTransportParseAndSaveRemoteDHCapsFuzzTest(const uint8_t * data,size_t size)62 void DhTransportParseAndSaveRemoteDHCapsFuzzTest(const uint8_t* data, size_t size)
63 {
64 if ((data == nullptr) || (size == 0)) {
65 return;
66 }
67 std::string remoteCaps(reinterpret_cast<const char*>(data), size);
68 std::shared_ptr<DHCommTool> dhCommTool = std::make_shared<DHCommTool>();
69 dhCommTool->ParseAndSaveRemoteDHCaps(remoteCaps);
70 }
71 }
72 }
73
74 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)75 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
76 {
77 /* Run your code on data */
78
79 OHOS::DistributedHardware::DhTransportTriggerReqFullDHCapsFuzzTest(data, size);
80 OHOS::DistributedHardware::DhTransportGetAndSendLocalFullCapsFuzzTest(data, size);
81 OHOS::DistributedHardware::DhTransportParseAndSaveRemoteDHCapsFuzzTest(data, size);
82 return 0;
83 }
84
85