• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "accessmanager_fuzzer.h"
17 
18 #include <algorithm>
19 #include <chrono>
20 #include <cstddef>
21 #include <cstdint>
22 #include <securec.h>
23 #include <string>
24 #include <unistd.h>
25 
26 #include "access_manager.h"
27 #include "distributed_hardware_errno.h"
28 #include "distributed_hardware_manager_factory.h"
29 
30 namespace OHOS {
31 namespace DistributedHardware {
32 namespace {
33     constexpr uint32_t SLEEP_TIME_US = 10 * 1000;
34 }
35 
AccessManagerFuzzTest(const uint8_t * data,size_t size)36 void AccessManagerFuzzTest(const uint8_t* data, size_t size)
37 {
38     if ((data == nullptr) || (size <= sizeof(DmDeviceInfo))) {
39         return;
40     }
41 
42     AccessManager::GetInstance()->Init();
43     DmDeviceInfo deviceInfo;
44     int32_t ret = memcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, (reinterpret_cast<const char *>(data)), size);
45     if (ret != EOK) {
46         return;
47     }
48     AccessManager::GetInstance()->OnDeviceReady(deviceInfo);
49 
50     usleep(SLEEP_TIME_US);
51 }
52 }
53 }
54 
55 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)56 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
57 {
58     /* Run your code on data */
59     OHOS::DistributedHardware::AccessManagerFuzzTest(data, size);
60     return 0;
61 }
62 
63