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 <string>
19 #include <unistd.h>
20 #include <unordered_map>
21 #include <fuzzer/FuzzedDataProvider.h>
22
23 #include "dm_transport_msg.h"
24 #include "dm_transport_msg_fuzzer.h"
25 #include "dm_comm_tool.h"
26 #include "cJSON.h"
27
28
29 namespace OHOS {
30 namespace DistributedHardware {
31
32 namespace {
33 constexpr uint32_t FORUSERID = 10;
34 constexpr uint32_t BACKUSERID = 20;
35 constexpr uint32_t USERID = 30;
36 constexpr uint32_t USERFIRSTID = 40;
37 }
38
DmTransPortMsgFuzzTest(const uint8_t * data,size_t size)39 void DmTransPortMsgFuzzTest(const uint8_t* data, size_t size)
40 {
41 if ((data == nullptr) || (size < sizeof(int32_t))) {
42 return;
43 }
44 FuzzedDataProvider fdp(data, size);
45 int32_t code = fdp.ConsumeIntegral<int32_t>();
46 std::string msg(reinterpret_cast<const char*>(data), size);
47 std::string remoteUdid(reinterpret_cast<const char*>(data), size);
48 const char* jsonString = R"({
49 "MsgType": "0",
50 "userId": "12345",
51 "accountId": "a******3",
52 "peerUdids": ["u******1", "u******2"],
53 "peerUdid": "p******d",
54 "accountName": "t******t",
55 "syncUserIdFlag": 1,
56 "userIds": [
57 {"type": 1, "userId": 111},
58 {"type": 0, "userId": 222}
59 ]
60 })";
61 cJSON *jsonObject = nullptr;
62 std::vector<uint32_t> foregroundUserIds;
63 foregroundUserIds.push_back(FORUSERID);
64 std::vector<uint32_t> backgroundUserIds;
65 backgroundUserIds.push_back(BACKUSERID);
66 UserIdsMsg userIdsMsg(foregroundUserIds, backgroundUserIds);
67 CommMsg commMsg(code, msg);
68 std::vector<uint32_t> userIds;
69 userIds.push_back(USERID);
70 userIds.push_back(USERFIRSTID);
71 NotifyUserIds notifyUserIds(remoteUdid, userIds);
72 ToJson(jsonObject, userIdsMsg);
73 FromJson(jsonObject, userIdsMsg);
74 ToJson(jsonObject, commMsg);
75 FromJson(jsonObject, commMsg);
76 ToJson(jsonObject, notifyUserIds);
77 FromJson(jsonObject, notifyUserIds);
78 jsonObject = cJSON_Parse(jsonString);
79 ToJson(jsonObject, userIdsMsg);
80 FromJson(jsonObject, userIdsMsg);
81 ToJson(jsonObject, commMsg);
82 FromJson(jsonObject, commMsg);
83 GetCommMsgString(commMsg);
84 ToJson(jsonObject, notifyUserIds);
85 FromJson(jsonObject, notifyUserIds);
86 notifyUserIds.ToString();
87 }
88 }
89 }
90
91 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)92 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
93 {
94 /* Run your code on data */
95 OHOS::DistributedHardware::DmTransPortMsgFuzzTest(data, size);
96 return 0;
97 }
98