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_two_fuzzer.h"
25 #include "dm_comm_tool.h"
26
27
28 namespace OHOS {
29 namespace DistributedHardware {
30
31 std::shared_ptr<DMCommTool> dmCommToolPtr_ = std::make_shared<DMCommTool>();
32
GenerateUserIds(FuzzedDataProvider & fdp,std::vector<uint32_t> & outIds)33 void GenerateUserIds(FuzzedDataProvider& fdp, std::vector<uint32_t>& outIds)
34 {
35 outIds.clear();
36
37 auto count = fdp.ConsumeIntegralInRange<size_t>(0, 10);
38
39 while(count-- > 0 && fdp.remaining_bytes() >= sizeof(uint32_t)) {
40 outIds.push_back(fdp.ConsumeIntegral<uint32_t>());
41 }
42 }
43
DmCommToolTwoFuzzTest(const uint8_t * data,size_t size)44 void DmCommToolTwoFuzzTest(const uint8_t* data, size_t size)
45 {
46 if ((data == nullptr) || (size < sizeof(int32_t))) {
47 return;
48 }
49 FuzzedDataProvider fdp(data, size);
50
51 int32_t commCode = fdp.ConsumeIntegral<int32_t>();
52 std::string commMsgStr = fdp.ConsumeRandomLengthString();
53 auto commMsgPtr = std::make_shared<CommMsg>(commCode, commMsgStr);
54
55 std::string networkId = fdp.ConsumeRandomLengthString();
56 int32_t socketId = fdp.ConsumeIntegral<int32_t>();
57 auto innerCommMsg = std::make_shared<InnerCommMsg>(networkId, commMsgPtr, socketId);
58
59 std::string accountId = fdp.ConsumeRandomLengthString();
60 int32_t userId = fdp.ConsumeIntegral<int32_t>();
61
62 dmCommToolPtr_->ProcessReceiveUserIdsEvent(innerCommMsg);
63 dmCommToolPtr_->SendLogoutAccountInfo(networkId, accountId, userId);
64 dmCommToolPtr_->ProcessReceiveLogoutEvent(innerCommMsg);
65 dmCommToolPtr_->ProcessReceiveCommonEvent(innerCommMsg);
66
67 std::vector<uint32_t> foregroundIds;
68 std::vector<uint32_t> backgroundIds;
69
70 GenerateUserIds(fdp, foregroundIds);
71 GenerateUserIds(fdp, backgroundIds);
72
73 std::string msgStr = fdp.ConsumeRandomLengthString();
74 int32_t stopUserId = fdp.ConsumeIntegral<int32_t>();
75
76 dmCommToolPtr_->SendUserIds(networkId, foregroundIds, backgroundIds);
77 dmCommToolPtr_->RspLocalFrontOrBackUserIds(networkId, foregroundIds, backgroundIds, socketId);
78
79 dmCommToolPtr_->CreateUserStopMessage(stopUserId, msgStr);
80 dmCommToolPtr_->SendMsg(networkId, fdp.ConsumeIntegral<int32_t>(), msgStr);
81 dmCommToolPtr_->SendUserStop(networkId, stopUserId);
82
83 int32_t parsedUserId = fdp.ConsumeIntegral<int32_t>();
84 dmCommToolPtr_->ParseUserStopMessage(msgStr, parsedUserId);
85
86 dmCommToolPtr_->ProcessReceiveUserStopEvent(innerCommMsg);
87 dmCommToolPtr_->RspUserStop(networkId, socketId, stopUserId);
88 }
89 }
90 }
91
92 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
94 {
95 /* Run your code on data */
96 OHOS::DistributedHardware::DmCommToolTwoFuzzTest(data, size);
97 return 0;
98 }
99