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 <fuzzer/FuzzedDataProvider.h>
17 #include <cstddef>
18 #include <cstdint>
19 #include <string>
20 #include <unistd.h>
21 #include <unordered_map>
22
23 #include "dp_inited_callback.h"
24 #include "dp_init_callback_fuzzer.h"
25 #include "dm_constants.h"
26 #include "json_object.h"
27
28
29 namespace OHOS {
30 namespace DistributedHardware {
31
32 namespace {
33 constexpr uint32_t DATA_LEN = 10;
34 }
DpInitedCallbackFuzzTest(const uint8_t * data,size_t size)35 void DpInitedCallbackFuzzTest(const uint8_t* data, size_t size)
36 {
37 if ((data == nullptr) || (size < sizeof(int32_t))) {
38 return;
39 }
40 DpInitedCallback dpInitedCallback;
41 dpInitedCallback.PutAllTrustedDevices();
42 }
43
DpInitedCallbackFirstFuzzTest(const uint8_t * data,size_t size)44 void DpInitedCallbackFirstFuzzTest(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 DpInitedCallback dpInitedCallback;
51 std::unordered_map<std::string, DmAuthForm> authFormMap;
52 DistributedDeviceProfile::TrustedDeviceInfo trustedDeviceInfo;
53 std::string extraDataStr = fdp.ConsumeRandomLengthString(50);
54 DmDeviceInfo deviceInfo;
55 deviceInfo.extraData = extraDataStr;
56 dpInitedCallback.ConvertToTrustedDeviceInfo(authFormMap, deviceInfo, trustedDeviceInfo);
57 deviceInfo.extraData = "";
58 dpInitedCallback.ConvertToTrustedDeviceInfo(authFormMap, deviceInfo, trustedDeviceInfo);
59
60 deviceInfo.extraData = "extraInfo";
61 dpInitedCallback.ConvertToTrustedDeviceInfo(authFormMap, deviceInfo, trustedDeviceInfo);
62
63 JsonObject extraJson;
64 extraJson[PARAM_KEY_OS_VERSION] = 1;
65 deviceInfo.extraData = extraJson.Dump();
66 dpInitedCallback.ConvertToTrustedDeviceInfo(authFormMap, deviceInfo, trustedDeviceInfo);
67
68 extraJson[PARAM_KEY_OS_VERSION] = "os_version";
69 extraJson[PARAM_KEY_OS_TYPE] = "os_type";
70 deviceInfo.extraData = extraJson.Dump();
71 dpInitedCallback.ConvertToTrustedDeviceInfo(authFormMap, deviceInfo, trustedDeviceInfo);
72
73 extraJson[PARAM_KEY_OS_TYPE] = DATA_LEN;
74 deviceInfo.extraData = extraJson.Dump();
75 dpInitedCallback.ConvertToTrustedDeviceInfo(authFormMap, deviceInfo, trustedDeviceInfo);
76 }
77 }
78 }
79
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
82 {
83 /* Run your code on data */
84 OHOS::DistributedHardware::DpInitedCallbackFuzzTest(data, size);
85 OHOS::DistributedHardware::DpInitedCallbackFirstFuzzTest(data, size);
86 return 0;
87 }
88