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_->ProcessReceiveCommonEvent(innerCommMsg);
61 dmCommToolPtr_->ProcessResponseCommonEvent(innerCommMsg);
62 dmCommToolPtr_->ProcessReceiveUninstAppEvent(innerCommMsg);
63 dmCommToolPtr_->ProcessReceiveUnBindAppEvent(innerCommMsg);
64 dmCommToolPtr_->ProcessReceiveRspAppUninstallEvent(innerCommMsg);
65 dmCommToolPtr_->ProcessReceiveRspAppUnbindEvent(innerCommMsg);
66 dmCommToolPtr_->GetDMTransportPtr();
67 dmCommToolPtr_->GetEventHandler();
68 }
69
DmCommToolFirstFuzzTest(const uint8_t * data,size_t size)70 void DmCommToolFirstFuzzTest(const uint8_t* data, size_t size)
71 {
72 if ((data == nullptr) || (size < sizeof(int32_t))) {
73 return;
74 }
75 FuzzedDataProvider fdp(data, size);
76 DMCommTool::DMCommToolEventHandler dmCommToolEventHandler(
77 AppExecFwk::EventRunner::Create(FUZZ_PROJECT_NAME), dmCommToolPtr_);
78
79 int32_t socketId = fdp.ConsumeIntegral<int32_t>();
80 std::string rmtNetworkId = fdp.ConsumeRandomLengthString();
81 int32_t code = fdp.ConsumeIntegral<int32_t>();
82 std::string msg = fdp.ConsumeRandomLengthString();
83 std::shared_ptr<CommMsg> commMsg = std::make_shared<CommMsg>(code, msg);
84 std::shared_ptr<InnerCommMsg> innrCommMsg = std::make_shared<InnerCommMsg>(rmtNetworkId, commMsg, socketId);
85 UserIdsMsg userIdsMsg;
86 userIdsMsg.foregroundUserIds.push_back(DATA_LEN);
87 dmCommToolEventHandler.ParseUserIdsMsg(innrCommMsg, userIdsMsg);
88 AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(commMsg->code, innrCommMsg);
89 dmCommToolEventHandler.ProcessEvent(event);
90 dmCommToolPtr_->ProcessResponseUserStopEvent(innrCommMsg);
91 std::string commonEventType;
92 EventCallback eventCallback;
93 dmCommToolPtr_->StartCommonEvent(commonEventType, eventCallback);
94 dmCommToolPtr_->Init();
95 int32_t userId = fdp.ConsumeIntegral<int32_t>();
96 int32_t tokenId = fdp.ConsumeIntegral<int32_t>();
97 dmCommToolPtr_->SendUninstAppObj(userId, tokenId, rmtNetworkId);
98 std::string emptyNetworkId = "";
99 dmCommToolPtr_->SendUninstAppObj(userId, tokenId, emptyNetworkId);
100 dmCommToolPtr_->RspAppUninstall(rmtNetworkId, socketId);
101 dmCommToolPtr_->RspAppUnbind(rmtNetworkId, socketId);
102 std::string udid = fdp.ConsumeRandomLengthString();
103 dmCommToolPtr_->SendUnBindAppObj(userId, tokenId, msg, rmtNetworkId, udid);
104 dmCommToolPtr_->SendUnBindAppObj(userId, tokenId, msg, emptyNetworkId, udid);
105 dmCommToolPtr_->StopSocket(rmtNetworkId);
106 }
107 }
108 }
109
110 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)111 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
112 {
113 /* Run your code on data */
114 OHOS::DistributedHardware::DmCommToolFuzzTest(data, size);
115 OHOS::DistributedHardware::DmCommToolFirstFuzzTest(data, size);
116 return 0;
117 }
118