From f58fca76e17fdf64267e4c064504f016d3607ee9 Mon Sep 17 00:00:00 2001 From: zhaona45 Date: Fri, 15 Aug 2025 10:30:35 +0800 Subject: [PATCH] Add Image Exif Metadata Formatter fuzz test. Signed-off-by: zhaona45 --- .../innerkitsimpl/test/fuzztest/BUILD.gn | 1 + .../BUILD.gn | 49 ++++++++++ .../corpus/init | 16 ++++ .../image_exif_metadata_formatter_fuzzer.h | 19 ++++ .../project.xml | 25 +++++ .../image_exif_metadata_formatter_fuzzer.cpp | 94 +++++++++++++++++++ 6 files changed, 204 insertions(+) create mode 100644 frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/BUILD.gn create mode 100644 frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/corpus/init create mode 100644 frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/include/image_exif_metadata_formatter_fuzzer.h create mode 100644 frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/project.xml create mode 100644 frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/src/image_exif_metadata_formatter_fuzzer.cpp diff --git a/frameworks/innerkitsimpl/test/fuzztest/BUILD.gn b/frameworks/innerkitsimpl/test/fuzztest/BUILD.gn index 7c2c5bf23..5a21f6f4b 100644 --- a/frameworks/innerkitsimpl/test/fuzztest/BUILD.gn +++ b/frameworks/innerkitsimpl/test/fuzztest/BUILD.gn @@ -16,6 +16,7 @@ group("fuzztest") { deps = [ "imageaccessor_fuzzer:ImageAccessorFuzzTest", "imageaccessor2_fuzzer:ImageAccessor2FuzzTest", + "imageexifmetadataformatter_fuzzer:ImageExifMetadataFormatterFuzzTest", "imagefwkconvertastc2rgb_fuzzer:ImageFwkConvertAstc2RgbFuzzTest", "imagefwkconvertp010yuv2rgbext_fuzzer:ImageFwkConvertP010Yuv2RgbExtFuzzTest", "imagefwkconvertpixelfotmat_fuzzer:ImageFwkConvertPixelFormatFuzzTest", diff --git a/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/BUILD.gn b/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/BUILD.gn new file mode 100644 index 000000000..63d9eec56 --- /dev/null +++ b/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/BUILD.gn @@ -0,0 +1,49 @@ +# Copyright (c) 2025 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/test.gni") +import("//foundation/multimedia/image_framework/ide/image_decode_config.gni") + +ohos_fuzztest("ImageExifMetadataFormatterFuzzTest") { + module_out_path = "image_framework/image_framework" + fuzz_config_file = "$image_subsystem/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer" + resource_config_file = "$image_subsystem/test/resource/image/ohos_test.xml" + + include_dirs = [ + "./include", + "$image_subsystem/interfaces/innerkits/include", + "$image_subsystem/frameworks/innerkitsimpl/test/fuzztest/common/include", + ] + + sources = [ + "$image_subsystem/frameworks/innerkitsimpl/test/fuzztest/common/src/common_fuzztest_function.cpp", + "src/image_exif_metadata_formatter_fuzzer.cpp", + ] + + deps = [ + "$image_subsystem/interfaces/innerkits:image_native", + "$image_subsystem/plugins/common/libs/image/libextplugin:extplugin", + ] + + external_deps = [ + "c_utils:utils", + "graphic_2d:color_manager", + "hilog:libhilog", + "libexif:libexif", + ] + + cflags = [ + "-Dprotected=public", + "-Dprivate=public", + ] +} diff --git a/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/corpus/init b/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/corpus/init new file mode 100644 index 000000000..14cddafc0 --- /dev/null +++ b/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 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 diff --git a/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/include/image_exif_metadata_formatter_fuzzer.h b/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/include/image_exif_metadata_formatter_fuzzer.h new file mode 100644 index 000000000..91d31a5ae --- /dev/null +++ b/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/include/image_exif_metadata_formatter_fuzzer.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2025 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 FRAMEWORKS_INNERKITSIMPL_TEST_FUZZTEST_IMAGEFRAMEWORK_FUZZER_IMAGE_EXIF_METADATA_FORMATTER_FUZZER_H +#define FRAMEWORKS_INNERKITSIMPL_TEST_FUZZTEST_IMAGEFRAMEWORK_FUZZER_IMAGE_EXIF_METADATA_FORMATTER_FUZZER_H + +#define FUZZ_PROJECT_NAME "image_exif_metadata_formatter_fuzzer" +#endif diff --git a/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/project.xml b/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/project.xml new file mode 100644 index 000000000..138c61f16 --- /dev/null +++ b/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 52428800 + + 300 + + 4096 + + diff --git a/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/src/image_exif_metadata_formatter_fuzzer.cpp b/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/src/image_exif_metadata_formatter_fuzzer.cpp new file mode 100644 index 000000000..9d6fa8649 --- /dev/null +++ b/frameworks/innerkitsimpl/test/fuzztest/imageexifmetadataformatter_fuzzer/src/image_exif_metadata_formatter_fuzzer.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2025 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 +#include "image_exif_metadata_formatter_fuzzer.h" + +#include +#include +#include +#include +#include +#include "securec.h" + +#include "common_fuzztest_function.h" +#include "webp_exif_metadata_accessor.h" +#include "image_log.h" +#include "exif_metadata_formatter.h" + +namespace OHOS { +namespace Media { +FuzzedDataProvider* FDP; +using namespace std; + +namespace { + static constexpr size_t MAX_SIZE = 1000; +} + +void ExifMetadataFormatterFuzzTest() +{ + size_t maxStrSize = 20; + std::string value = FDP->ConsumeRandomLengthString(maxStrSize); + for (auto it = ExifMetadatFormatter::valueFormatConvertConfig.begin(); + it != ExifMetadatFormatter::valueFormatConvertConfig.end(); it++) { + auto func = (it->second).first; + func(value, (it->second).second); + } +} + +void WebpExifMetadataAccessorFuzzTest(const uint8_t *data, size_t size) +{ + if (data == nullptr) { + return; + } + if (size == 0 || size > MAX_SIZE) { + return; + } + std::unique_ptr copyData = std::make_unique(size); + if (!copyData) { + return; + } + if (EOK != memcpy_s(copyData.get(), size, data, size)) { + return; + } + std::shared_ptr srcStream = std::make_shared( + copyData.get(), size, BufferMetadataStream::MemoryMode::Dynamic); + if (!srcStream) { + return; + } + std::shared_ptr accessor = std::make_shared(srcStream); + if (!accessor) { + return; + } + std::vector mockVec = FDP->ConsumeBytes(size); + if (!mockVec.data()) { + return; + } + DataBuf dataBuf(mockVec.data(), mockVec.size()); + accessor->WriteBlob(dataBuf); +} +} // namespace Media +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + FuzzedDataProvider fdp(data, size); + OHOS::Media::FDP = &fdp; + + OHOS::Media::ExifMetadataFormatterFuzzTest(); + OHOS::Media::WebpExifMetadataAccessorFuzzTest(data, size); + return 0; +} -- Gitee