• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 
20 #include <algorithm>
21 #include <chrono>
22 #include <cstddef>
23 #include <cstdint>
24 #include <string>
25 #include "ipc_client_manager.h"
26 #include "ipc_set_useroperation_req.h"
27 #include "ipc_rsp.h"
28 #include "ipc_def.h"
29 #include "device_manager_notify.h"
30 #include "dm_device_info.h"
31 #include "ipc_client_stub.h"
32 #include "ipc_register_listener_req.h"
33 #include "ipc_remote_broker.h"
34 #include "iremote_object.h"
35 #include "iservice_registry.h"
36 #include "dm_constants.h"
37 #include "system_ability_definition.h"
38 #include "device_manager_notify_fuzzer.h"
39 
40 namespace OHOS {
41 namespace DistributedHardware {
DeviceManagerNotifyUnRegisterFuzzTest(const uint8_t * data,size_t size)42 void DeviceManagerNotifyUnRegisterFuzzTest(const uint8_t* data, size_t size)
43 {
44     if ((data == nullptr) || (size == 0)) {
45         return;
46     }
47 
48     std::string pkgName(reinterpret_cast<const char*>(data), size);
49     std::string deviceId(reinterpret_cast<const char*>(data), size);
50     uint16_t subscribeId = 33;
51     int32_t publishId = 123;
52 
53     DeviceManagerNotify::GetInstance().UnRegisterDeathRecipientCallback(pkgName);
54     DeviceManagerNotify::GetInstance().UnRegisterDeviceStateCallback(pkgName);
55     DeviceManagerNotify::GetInstance().UnRegisterPackageCallback(pkgName);
56     DeviceManagerNotify::GetInstance().UnRegisterDeviceManagerFaCallback(pkgName);
57     DeviceManagerNotify::GetInstance().UnRegisterDiscoveryCallback(pkgName, subscribeId);
58     DeviceManagerNotify::GetInstance().UnRegisterPublishCallback(pkgName, publishId);
59     DeviceManagerNotify::GetInstance().UnRegisterAuthenticateCallback(pkgName, deviceId);
60     DeviceManagerNotify::GetInstance().OnUiCall(pkgName, deviceId);
61 }
62 
DeviceManagerNotifyDeviceStatusFuzzTest(const uint8_t * data,size_t size)63 void DeviceManagerNotifyDeviceStatusFuzzTest(const uint8_t* data, size_t size)
64 {
65     if ((data == nullptr) || (size == 0)) {
66         return;
67     }
68 
69     std::string pkgName(reinterpret_cast<const char*>(data), size);
70     std::string deviceId(reinterpret_cast<const char*>(data), size);
71     std::string token(reinterpret_cast<const char*>(data), size);
72     uint16_t subscribeId = 12;
73     int32_t publishId = 111;
74     int32_t failedReason = 231;
75     uint32_t status = 3;
76     uint32_t reason = 14;
77     DmDeviceInfo deviceInfo;
78     DmDeviceBasicInfo deviceBasicInfo;
79 
80     DeviceManagerNotify::GetInstance().OnDeviceOnline(pkgName, deviceInfo);
81     DeviceManagerNotify::GetInstance().OnDeviceOnline(pkgName, deviceBasicInfo);
82     DeviceManagerNotify::GetInstance().OnDeviceOffline(pkgName, deviceInfo);
83     DeviceManagerNotify::GetInstance().OnDeviceOffline(pkgName, deviceBasicInfo);
84     DeviceManagerNotify::GetInstance().OnDeviceChanged(pkgName, deviceInfo);
85     DeviceManagerNotify::GetInstance().OnDeviceReady(pkgName, deviceInfo);
86     DeviceManagerNotify::GetInstance().OnDeviceReady(pkgName, deviceBasicInfo);
87     DeviceManagerNotify::GetInstance().OnDeviceFound(pkgName, subscribeId, deviceInfo);
88     DeviceManagerNotify::GetInstance().OnDeviceFound(pkgName, subscribeId, deviceBasicInfo);
89     DeviceManagerNotify::GetInstance().OnDiscoveryFailed(pkgName, subscribeId, failedReason);
90     DeviceManagerNotify::GetInstance().OnDiscoverySuccess(pkgName, subscribeId);
91     DeviceManagerNotify::GetInstance().OnPublishResult(pkgName, publishId, failedReason);
92     DeviceManagerNotify::GetInstance().OnAuthResult(pkgName, deviceId, token, status, reason);
93 }
94 }
95 }
96 
97 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)98 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
99 {
100     /* Run your code on data */
101     OHOS::DistributedHardware::DeviceManagerNotifyUnRegisterFuzzTest(data, size);
102     OHOS::DistributedHardware::DeviceManagerNotifyDeviceStatusFuzzTest(data, size);
103 
104     return 0;
105 }
106