1 /*
2  * Copyright (c) 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 "vibratorplayhapticpattern_fuzzer.h"
17 #include "hdf_base.h"
18 #include "v1_2/vibrator_interface_proxy.h"
19 #include <hdf_log.h>
20 #include <securec.h>
21 
22 using namespace OHOS::HDI::Vibrator::V1_2;
23 
24 namespace {
25     struct AllParameters {
26         int32_t g_pkgTime;
27         int32_t g_pkgEventNum;
28         enum OHOS::HDI::Vibrator::V1_2::EVENT_TYPE g_eventType;
29         int32_t g_eventDuration;
30         int32_t g_eventTime;
31         int32_t g_eventIntensity;
32         int32_t g_eventFrequency;
33         int32_t g_eventIndex;
34         int32_t g_eventPointNum;
35         int32_t g_pointTime;
36         int32_t g_pointIntensity;
37         int32_t g_pointFrequency;
38     };
39 }
40 
41 namespace OHOS {
VibratorPlayHapticPatternTest(const uint8_t * data,size_t size)42     bool VibratorPlayHapticPatternTest(const uint8_t* data, size_t size)
43     {
44         struct AllParameters params;
45         if (data == nullptr) {
46             return false;
47         }
48         OHOS::HDI::Vibrator::V1_2::HapticPaket pkg;
49 
50         if (size < sizeof(pkg)) {
51             return false;
52         }
53 
54         if (memcpy_s(reinterpret_cast<void *>(&params), sizeof(params), data, sizeof(params)) != 0) {
55             HDF_LOGE("%{public}s: memcpy_s failed", __func__);
56             return false;
57         }
58 
59         sptr<OHOS::HDI::Vibrator::V1_2::IVibratorInterface> g_vibratorInterface =
60             OHOS::HDI::Vibrator::V1_2::IVibratorInterface::Get();
61 
62         pkg.time = params.g_pkgTime;
63         pkg.eventNum = params.g_pkgEventNum;
64 
65         OHOS::HDI::Vibrator::V1_2::HapticEvent hapticEvent;
66         hapticEvent.type = params.g_eventType;
67         hapticEvent.duration = params.g_eventDuration;
68         hapticEvent.time = params.g_eventTime;
69         hapticEvent.intensity = params.g_eventIntensity;
70         hapticEvent.frequency = params.g_eventFrequency;
71         hapticEvent.index = params.g_eventIndex;
72         hapticEvent.pointNum = params.g_eventPointNum;
73 
74         OHOS::HDI::Vibrator::V1_2::CurvePoint curvePoint;
75         curvePoint.time = params.g_pointTime;
76         curvePoint.intensity = params.g_pointIntensity;
77         curvePoint.frequency = params.g_pointFrequency;
78         hapticEvent.points.push_back(std::move(curvePoint));
79 
80         pkg.events.push_back(std::move(hapticEvent));
81         int32_t ret = !g_vibratorInterface->PlayHapticPattern(pkg);
82         if (ret != HDF_SUCCESS) {
83             HDF_LOGE("%{public}s: GetConfig failed, ret is [%{public}x]\n", __func__, ret);
84             return false;
85         }
86 
87         return true;
88     }
89 }
90 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
92 {
93     OHOS::VibratorPlayHapticPatternTest(data, size);
94     return 0;
95 }
96 
97