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 "device_name_manager_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 #include <memory>
22 #include <string>
23 #include <unistd.h>
24 #include <unordered_map>
25
26 #include "device_name_manager.h"
27 #include "dm_constants.h"
28 #include "dm_datashare_common_event.h"
29
30 namespace OHOS {
31 namespace DistributedHardware {
32
33 namespace {
34 constexpr int32_t INT32_SIZE = 5;
35 }
36
37 std::shared_ptr<DeviceNameManager> deviceNameMgr_ = std::make_shared<DeviceNameManager>();
DeviceNameManagerFuzzTest(const uint8_t * data,size_t size)38 void DeviceNameManagerFuzzTest(const uint8_t* data, size_t size)
39 {
40 if ((data == nullptr) || (size < (sizeof(int32_t) * INT32_SIZE))) {
41 return;
42 }
43 FuzzedDataProvider fdp(data, size);
44 deviceNameMgr_->InitDeviceNameWhenSoftBusReady();
45 int32_t curUserId = fdp.ConsumeIntegral<int32_t>();
46 deviceNameMgr_->InitDeviceNameWhenUserSwitch(curUserId, curUserId);
47 deviceNameMgr_->InitDeviceNameWhenLogout();
48 deviceNameMgr_->InitDeviceNameWhenLogin();
49 deviceNameMgr_->InitDeviceNameWhenNickChange();
50 deviceNameMgr_->InitDeviceNameWhenNameChange(curUserId);
51 int32_t preUserId = fdp.ConsumeIntegral<int32_t>();
52 deviceNameMgr_->RegisterDeviceNameChangeMonitor(curUserId, preUserId);
53 deviceNameMgr_->UnRegisterDeviceNameChangeMonitor(curUserId);
54 int32_t userId = fdp.ConsumeIntegral<int32_t>();
55 deviceNameMgr_->InitDeviceName(userId);
56
57 deviceNameMgr_->InitDeviceName(-1);
58 std::string prefixName(reinterpret_cast<const char*>(data), size);
59 std::string subffixName(reinterpret_cast<const char*>(data), size);
60 deviceNameMgr_->InitDeviceNameToSoftBus(prefixName, subffixName);
61
62 int32_t maxNamelength = fdp.ConsumeIntegral<int32_t>();
63 std::string displayName(reinterpret_cast<const char*>(data), size);
64 deviceNameMgr_->GetLocalDisplayDeviceName(maxNamelength, displayName);
65 deviceNameMgr_->ModifyUserDefinedName(displayName);
66 deviceNameMgr_->RestoreLocalDeviceName();
67 std::string nickName(reinterpret_cast<const char*>(data), size);
68 std::string deviceName(reinterpret_cast<const char*>(data), size);
69 deviceNameMgr_->InitDisplayDeviceNameToSettingsData(nickName, deviceName, userId);
70 deviceNameMgr_->GetUserDefinedDeviceName(userId, deviceName);
71 std::string str(reinterpret_cast<const char*>(data), size);
72 int32_t maxNumBytes = fdp.ConsumeIntegral<int32_t>();
73 deviceNameMgr_->SubstrByBytes(str, maxNumBytes);
74 deviceNameMgr_->GetSystemLanguage();
75 deviceNameMgr_->GetLocalMarketName();
76 deviceNameMgr_->SetUserDefinedDeviceName(deviceName, userId);
77 deviceNameMgr_->GetDisplayDeviceName(userId, deviceName);
78 std::string state(reinterpret_cast<const char*>(data), size);
79 deviceNameMgr_->SetDisplayDeviceNameState(state, userId);
80 }
81
DeviceNameManagerFirstFuzzTest(const uint8_t * data,size_t size)82 void DeviceNameManagerFirstFuzzTest(const uint8_t* data, size_t size)
83 {
84 if ((data == nullptr) || (size < sizeof(int32_t))) {
85 return;
86 }
87 FuzzedDataProvider fdp(data, size);
88 std::string deviceName(reinterpret_cast<const char*>(data), size);
89 int32_t userId = fdp.ConsumeIntegral<int32_t>();
90 deviceNameMgr_->SetDisplayDeviceName(deviceName, userId);
91 deviceNameMgr_->SetDisplayDeviceName("", userId);
92 deviceNameMgr_->GetDeviceName(deviceName);
93 deviceNameMgr_->SetDeviceName(deviceName);
94 deviceNameMgr_->SetDeviceName("");
95 deviceNameMgr_->GetRemoteObj();
96 std::string tableName(reinterpret_cast<const char*>(data), size);
97 std::string key(reinterpret_cast<const char*>(data), size);
98 std::string value(reinterpret_cast<const char*>(data), size);
99 deviceNameMgr_->GetValue(tableName, userId, key, value);
100 deviceNameMgr_->SetValue(tableName, userId, key, value);
101 std::string proxyUri(reinterpret_cast<const char*>(data), size);
102 deviceNameMgr_->CreateDataShareHelper(proxyUri);
103 deviceNameMgr_->GetProxyUriStr(tableName, userId);
104 deviceNameMgr_->MakeUri(proxyUri, key);
105 deviceNameMgr_->MakeUri("", key);
106 std::shared_ptr<DataShare::DataShareHelper> helper = nullptr;
107 deviceNameMgr_->ReleaseDataShareHelper(helper);
108 }
109
DeviceNameManagerSecondFuzzTest(const uint8_t * data,size_t size)110 void DeviceNameManagerSecondFuzzTest(const uint8_t* data, size_t size)
111 {
112 if ((data == nullptr) || (size < sizeof(int32_t))) {
113 return;
114 }
115 FuzzedDataProvider fdp(data, size);
116 std::shared_ptr<DeviceNameManager> deviceNameMgr = std::make_shared<DeviceNameManager>();
117 deviceNameMgr->UnInit();
118 deviceNameMgr->GetUserDefinedDeviceName();
119 deviceNameMgr_->GetSystemRegion();
120 DmDataShareCommonEventManager eventManager;
121 eventManager.eventValidFlag_ = true;
122 eventManager.UnsubscribeDataShareCommonEvent();
123 }
124 } // namespace DistributedHardware
125 } // namespace OHOS
126
127 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)128 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
129 {
130 /* Run your code on data */
131 OHOS::DistributedHardware::DeviceNameManagerFuzzTest(data, size);
132 OHOS::DistributedHardware::DeviceNameManagerFirstFuzzTest(data, size);
133 OHOS::DistributedHardware::DeviceNameManagerSecondFuzzTest(data, size);
134 return 0;
135 }
136