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