• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <cstddef>
17 #include <cstdint>
18 #include <memory>
19 #include <string>
20 #include <unistd.h>
21 #include <unordered_map>
22 #include <fuzzer/FuzzedDataProvider.h>
23 
24 #include "dm_comm_tool_fuzzer.h"
25 #include "dm_comm_tool.h"
26 
27 
28 namespace OHOS {
29 namespace DistributedHardware {
30 
31 namespace {
32     constexpr uint32_t DATA_LEN = 10;
33 }
34 
35 std::shared_ptr<DMCommTool> dmCommToolPtr_ = std::make_shared<DMCommTool>();
36 
DmCommToolFuzzTest(const uint8_t * data,size_t size)37 void DmCommToolFuzzTest(const uint8_t* data, size_t size)
38 {
39     if ((data == nullptr) || (size < sizeof(int32_t))) {
40         return;
41     }
42     FuzzedDataProvider fdp(data, size);
43     int32_t socketId = fdp.ConsumeIntegral<int32_t>();
44     std::string rmtNetworkId(reinterpret_cast<const char*>(data), size);
45     dmCommToolPtr_->Init();
46     dmCommToolPtr_->GetInstance();
47     dmCommToolPtr_->UnInit();
48     std::vector<uint32_t> foregroundUserIds;
49     foregroundUserIds.push_back(DATA_LEN);
50     std::vector<uint32_t> backgroundUserIds;
51     dmCommToolPtr_->SendUserIds(rmtNetworkId, foregroundUserIds, backgroundUserIds);
52     dmCommToolPtr_->RspLocalFrontOrBackUserIds(rmtNetworkId, foregroundUserIds, backgroundUserIds, socketId);
53     rmtNetworkId = "";
54     dmCommToolPtr_->SendUserIds(rmtNetworkId, foregroundUserIds, backgroundUserIds);
55     std::string remoteNetworkId(reinterpret_cast<const char*>(data), size);
56     std::shared_ptr<CommMsg> commMsg = std::make_shared<CommMsg>();
57     std::shared_ptr<InnerCommMsg> innerCommMsg = std::make_shared<InnerCommMsg>(remoteNetworkId, commMsg, socketId);
58     dmCommToolPtr_->ProcessReceiveUserIdsEvent(innerCommMsg);
59     dmCommToolPtr_->ProcessResponseUserIdsEvent(innerCommMsg);
60     dmCommToolPtr_->GetDMTransportPtr();
61     dmCommToolPtr_->GetEventHandler();
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::DmCommToolFuzzTest(data, size);
71     return 0;
72 }
73