1 /*
2 * Copyright (c) 2022-2024 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 <fuzzer/FuzzedDataProvider.h>
21
22 #include "device_manager_impl.h"
23 #include "device_manager.h"
24 #include "device_manager_callback.h"
25 #include "device_publish_fuzzer.h"
26
27 namespace OHOS {
28 namespace DistributedHardware {
29 namespace {
30 constexpr uint32_t SLEEP_TIME_US = 10 * 1000;
31 }
32
33 class DevicePublishCallbackTest : public PublishCallback {
34 public:
DevicePublishCallbackTest()35 DevicePublishCallbackTest() : PublishCallback() {}
~DevicePublishCallbackTest()36 virtual ~DevicePublishCallbackTest() {}
OnPublishResult(int32_t publishId,int32_t failedReason)37 void OnPublishResult(int32_t publishId, int32_t failedReason) override {}
38 };
39
DevicePublishFuzzTest(const uint8_t * data,size_t size)40 void DevicePublishFuzzTest(const uint8_t* data, size_t size)
41 {
42 if ((data == nullptr) || (size < sizeof(int32_t))) {
43 return;
44 }
45 std::string bundleName(reinterpret_cast<const char*>(data), size);
46 FuzzedDataProvider fdp(data, size);
47 DmPublishInfo publishInfo;
48 publishInfo.publishId = fdp.ConsumeIntegral<int32_t>();
49 publishInfo.mode = fdp.PickValueInArray<DmDiscoverMode>({
50 DM_DISCOVER_MODE_PASSIVE,
51 DM_DISCOVER_MODE_ACTIVE
52 });
53 publishInfo.freq = fdp.PickValueInArray<DmExchangeFreq>({
54 DM_LOW,
55 DM_MID,
56 DM_HIGH,
57 DM_SUPER_HIGH
58 });
59 int32_t publishId = fdp.ConsumeIntegral<int32_t>();
60 std::shared_ptr<PublishCallback> callback = std::make_shared<DevicePublishCallbackTest>();
61 DeviceManager::GetInstance().PublishDeviceDiscovery(bundleName, publishInfo, callback);
62 usleep(SLEEP_TIME_US);
63 DeviceManager::GetInstance().UnPublishDeviceDiscovery(bundleName, publishId);
64 }
65 }
66 }
67
68 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)69 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
70 {
71 /* Run your code on data */
72 OHOS::DistributedHardware::DevicePublishFuzzTest(data, size);
73 return 0;
74 }
75