1 /*
2 * Copyright (c) 2025 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 "stopvibratorenhanced_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstring>
21 #include <string>
22
23 #include "securec.h"
24 #include "vibrator_agent.h"
25
26 namespace OHOS {
27 namespace {
28 constexpr size_t U32_AT_SIZE = 28;
29 constexpr size_t LEN = 20;
30 } // namespace
31
32 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)33 size_t GetObject(const uint8_t *data, size_t size, T &object)
34 {
35 size_t objectSize = sizeof(object);
36 if (objectSize > size) {
37 return 0;
38 }
39 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
40 if (ret != EOK) {
41 return 0;
42 }
43 return objectSize;
44 }
45
StopVibratorEnhancedFuzzTest(const uint8_t * data,size_t size)46 bool StopVibratorEnhancedFuzzTest(const uint8_t *data, size_t size)
47 {
48 if (data == nullptr || size < U32_AT_SIZE) {
49 return false;
50 }
51 VibratorIdentifier identifier;
52 size_t startPos = 0;
53 char mode[LEN + 1];
54 startPos += GetObject<int32_t>(data + startPos, size - startPos, identifier.deviceId);
55 startPos += GetObject<int32_t>(data + startPos, size - startPos, identifier.vibratorId);
56 for (size_t i = 0; i < LEN; ++i) {
57 startPos += GetObject<char>(data + startPos, size - startPos, mode[i]);
58 }
59
60 int32_t ret = OHOS::Sensors::StopVibratorEnhanced(identifier, mode);
61 int32_t ret2 = strcmp(mode, "time") != 0 && strcmp(mode, "preset");
62 if ((ret2 != 0 && ret == 0) || (ret2 == 0 && ret != 0)) {
63 return false;
64 }
65 return true;
66 }
67 } // namespace OHOS
68
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)69 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
70 {
71 OHOS::StopVibratorEnhancedFuzzTest(data, size);
72 return 0;
73 }