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