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 "geteffectinfo_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstring>
21 #include <string>
22
23 #include "securec.h"
24
25 #include "vibrator_agent.h"
26
27 namespace OHOS {
28 namespace {
29 constexpr size_t U32_AT_SIZE = 8;
30 } // namespace
31 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)32 size_t GetObject(const uint8_t *data, size_t size, T &object)
33 {
34 size_t objectSize = sizeof(object);
35 if (objectSize > size) {
36 return 0;
37 }
38 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
39 if (ret != EOK) {
40 return 0;
41 }
42 return objectSize;
43 }
44
GetEffectInfoFuzzTest(const uint8_t * data,size_t size)45 bool GetEffectInfoFuzzTest(const uint8_t *data, size_t size)
46 {
47 VibratorIdentifier identifier;
48 if (size < U32_AT_SIZE) {
49 identifier.deviceId = -1;
50 identifier.vibratorId = -1;
51 } else {
52 size_t startPos = 0;
53 startPos += GetObject<int32_t>(data + startPos, size - startPos, identifier.deviceId);
54 GetObject<int32_t>(data + startPos, size - startPos, identifier.vibratorId);
55 }
56 EffectInfo effectInfo;
57 std::string effectType = "haptic.clock.timer";
58 int32_t ret = OHOS::Sensors::GetEffectInfo(identifier, effectType, effectInfo);
59 if (ret == 0) {
60 return false;
61 }
62 return true;
63 }
64 } // namespace OHOS
65
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)66 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
67 {
68 OHOS::GetEffectInfoFuzzTest(data, size);
69 return 0;
70 }