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