diff --git a/interfaces/native/vibrator/test/fuzztest/BUILD.gn b/interfaces/native/vibrator/test/fuzztest/BUILD.gn index e62c755f0ba0ec98832a47e556a500ddcca05a9a..27311fe3d07a16e614a6a7080dfbcc18dc9ca0af 100755 --- a/interfaces/native/vibrator/test/fuzztest/BUILD.gn +++ b/interfaces/native/vibrator/test/fuzztest/BUILD.gn @@ -19,6 +19,8 @@ group("fuzztest") { testonly = true deps = [] deps += [ + "issupporteffect_fuzzer:fuzztest", + "playvibratorcustom_fuzzer:fuzztest", "setloopcount_fuzzer:fuzztest", "setusage_fuzzer:fuzztest", "startvibrator_fuzzer:fuzztest", diff --git a/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer/BUILD.gn b/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4c8994e5da8f7fb0e0edaec4a6d55394a29482a1 --- /dev/null +++ b/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer/BUILD.gn @@ -0,0 +1,49 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "sensors/vibrator" + +ohos_fuzztest("IsSupportEffectFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "//base/sensors/miscdevice/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer" + + include_dirs = [ + "//base/sensors/miscdevice/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer", + "//base/sensors/miscdevice/interfaces/native/vibrator/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "issupporteffect_fuzzer.cpp" ] + + deps = [ "//base/sensors/miscdevice/interfaces/native/vibrator:vibrator_interface_native" ] + external_deps = [ "c_utils:utils" ] +} + +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":IsSupportEffectFuzzTest", + ] +} diff --git a/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer/corpus/init b/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..c49c21aa8683c4d54af710059267afe15db14f96 --- /dev/null +++ b/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer/issupporteffect_fuzzer.cpp b/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer/issupporteffect_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1742ec32598125f68be245680bf71bf2bf5228a2 --- /dev/null +++ b/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer/issupporteffect_fuzzer.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "issupporteffect_fuzzer.h" + +#include "securec.h" + +#include "vibrator_agent.h" + +namespace OHOS { +namespace Sensors { +namespace { +constexpr size_t DATA_MIN_SIZE = 2; +constexpr char END_CHAR = '\0'; +constexpr size_t LEN = 10; +} // namespace + +template +size_t GetObject(const uint8_t *data, size_t size, T &object) +{ + size_t objectSize = sizeof(object); + if (objectSize > size) { + return 0; + } + errno_t ret = memcpy_s(&object, objectSize, data, objectSize); + if (ret != EOK) { + return 0; + } + return objectSize; +} + +bool IsSupportEffectFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + size_t startPos = 0; + char effectId[LEN + 1]; + effectId[LEN] = END_CHAR; + for (size_t i = 0; i < LEN; ++i) { + startPos += GetObject(data + startPos, size - startPos, effectId[i]); + } + bool state { false }; + GetObject(data + startPos, size - startPos, state); + int32_t ret = OHOS::Sensors::IsSupportEffect(effectId, &state); + if (ret != 0) { + return false; + } + return true; +} +} // Sensors +} // OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + OHOS::Sensors::IsSupportEffectFuzzTest(data, size); + return 0; +} \ No newline at end of file diff --git a/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer/issupporteffect_fuzzer.h b/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer/issupporteffect_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..57a2053dbfeb69f88ebae01eae65715bc73b619a --- /dev/null +++ b/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer/issupporteffect_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SENSOR_DISABLE_FUZZER_H +#define SENSOR_DISABLE_FUZZER_H + +#define FUZZ_PROJECT_NAME "issupporteffect_fuzzer" + +#endif + diff --git a/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer/project.xml b/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..20dc766db73742058c8300227a37ba63703fc683 --- /dev/null +++ b/interfaces/native/vibrator/test/fuzztest/issupporteffect_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer/BUILD.gn b/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..d18f8f9174f524ab985f17a7905a0d74434ecce0 --- /dev/null +++ b/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer/BUILD.gn @@ -0,0 +1,49 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "sensors/vibrator" + +ohos_fuzztest("PlayVibratorCustomFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "//base/sensors/miscdevice/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer" + + include_dirs = [ + "//base/sensors/miscdevice/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer", + "//base/sensors/miscdevice/interfaces/native/vibrator/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "playvibratorcustom_fuzzer.cpp" ] + + deps = [ "//base/sensors/miscdevice/interfaces/native/vibrator:vibrator_interface_native" ] + external_deps = [ "c_utils:utils" ] +} + +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":PlayVibratorCustomFuzzTest", + ] +} diff --git a/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer/corpus/init b/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..c49c21aa8683c4d54af710059267afe15db14f96 --- /dev/null +++ b/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer/playvibratorcustom_fuzzer.cpp b/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer/playvibratorcustom_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45e761240f989fb5b1ffbbab65b0c76acf5ae2cd --- /dev/null +++ b/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer/playvibratorcustom_fuzzer.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "playvibratorcustom_fuzzer.h" + +#include "securec.h" + +#include "vibrator_agent.h" + +namespace OHOS { +namespace Sensors { +namespace { +constexpr size_t DATA_MIN_SIZE = 2; +} // namespace + +template +size_t GetObject(const uint8_t *data, size_t size, T &object) +{ + size_t objectSize = sizeof(object); + if (objectSize > size) { + return 0; + } + errno_t ret = memcpy_s(&object, objectSize, data, objectSize); + if (ret != EOK) { + return 0; + } + return objectSize; +} + +bool PlayVibratorCustomFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return false; + } + size_t startPos = 0; + int32_t fd { 0 }; + startPos += GetObject(data + startPos, size - startPos, fd); + int64_t offset { 0 }; + startPos += GetObject(data + startPos, size - startPos, offset); + int64_t length { 0 }; + GetObject(data + startPos, size - startPos, length); + int32_t ret = OHOS::Sensors::PlayVibratorCustom(fd, offset, length); + if (ret != 0) { + return false; + } + return true; +} +} // Sensors +} // OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + OHOS::Sensors::PlayVibratorCustomFuzzTest(data, size); + return 0; +} \ No newline at end of file diff --git a/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer/playvibratorcustom_fuzzer.h b/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer/playvibratorcustom_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..57a2053dbfeb69f88ebae01eae65715bc73b619a --- /dev/null +++ b/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer/playvibratorcustom_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SENSOR_DISABLE_FUZZER_H +#define SENSOR_DISABLE_FUZZER_H + +#define FUZZ_PROJECT_NAME "issupporteffect_fuzzer" + +#endif + diff --git a/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer/project.xml b/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..20dc766db73742058c8300227a37ba63703fc683 --- /dev/null +++ b/interfaces/native/vibrator/test/fuzztest/playvibratorcustom_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + +