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 "task_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "constants.h"
22 #include "distributed_hardware_errno.h"
23 #include "distributed_hardware_log.h"
24 #include "task_board.h"
25 #include "task_executor.h"
26 #include "task_factory.h"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 namespace {
31 const uint32_t DH_TYPE_SIZE = 10;
32 const DHType dhTypeFuzz[DH_TYPE_SIZE] = {
33 DHType::CAMERA, DHType::AUDIO, DHType::SCREEN, DHType::VIRMODEM_AUDIO,
34 DHType::INPUT, DHType::A2D, DHType::GPS, DHType::HFP
35 };
36
37 const uint32_t TASK_TYPE_SIZE = 5;
38 const TaskType taskTypeFuzz[TASK_TYPE_SIZE] = {
39 TaskType::UNKNOWN, TaskType::ENABLE, TaskType::DISABLE, TaskType::ON_LINE, TaskType::OFF_LINE
40 };
41 }
42
TaskFuzzTest(const uint8_t * data,size_t size)43 void TaskFuzzTest(const uint8_t* data, size_t size)
44 {
45 if ((data == nullptr) || (size == 0)) {
46 return;
47 }
48
49 std::string networkId(reinterpret_cast<const char*>(data), size);
50 std::string uuid(reinterpret_cast<const char*>(data), size);
51 std::string dhId(reinterpret_cast<const char*>(data), size);
52 DHType dhType = dhTypeFuzz[data[0] % DH_TYPE_SIZE];
53
54 TaskParam taskParam = {
55 .networkId = networkId,
56 .uuid = uuid,
57 .dhId = dhId,
58 .dhType = dhType
59 };
60 TaskType taskType = taskTypeFuzz[data[0] % TASK_TYPE_SIZE];
61
62 auto task = TaskFactory::GetInstance().CreateTask(taskType, taskParam, nullptr);
63 if (task != nullptr) {
64 TaskBoard::GetInstance().RemoveTask(task->GetId());
65 }
66 }
67 }
68 }
69
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
72 {
73 /* Run your code on data */
74 OHOS::DistributedHardware::TaskFuzzTest(data, size);
75 return 0;
76 }
77
78