From 8555af2cd9e2c7ba8a4992b75cb1d7db7248fb22 Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Tue, 2 Apr 2024 17:49:52 +0800 Subject: [PATCH] add afs fuzz test Signed-off-by: lvyuanyuan Change-Id: I8ba37c0f1a3ac9a8d7246368fa5e630659144bf6 --- test/fuzztest/BUILD.gn | 1 + .../svcextensionproxy_fuzzer/BUILD.gn | 61 +++++++++++++++ .../svcextensionproxy_fuzzer/corpus/init | 16 ++++ .../svcextensionproxy_fuzzer/project.xml | 25 ++++++ .../svcextensionproxy_fuzzer.cpp | 78 +++++++++++++++++++ .../svcextensionproxy_fuzzer.h | 21 +++++ 6 files changed, 202 insertions(+) create mode 100644 test/fuzztest/svcextensionproxy_fuzzer/BUILD.gn create mode 100644 test/fuzztest/svcextensionproxy_fuzzer/corpus/init create mode 100644 test/fuzztest/svcextensionproxy_fuzzer/project.xml create mode 100644 test/fuzztest/svcextensionproxy_fuzzer/svcextensionproxy_fuzzer.cpp create mode 100644 test/fuzztest/svcextensionproxy_fuzzer/svcextensionproxy_fuzzer.h diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 49bed1ad8..80190ea05 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -22,6 +22,7 @@ group("fuzztest") { "fileshare_fuzzer:FileShareFuzzTest", "remotefileshare_fuzzer:RemoteFileShareFuzzTest", "servicereverse_fuzzer:ServiceReverseFuzzTest", + "svcextensionproxy_fuzzer:SvcExtensionProxyFuzzTest", "svcrestoredepsmanager_fuzzer:SvcRestoreDepsManagerFuzzTest", ] } diff --git a/test/fuzztest/svcextensionproxy_fuzzer/BUILD.gn b/test/fuzztest/svcextensionproxy_fuzzer/BUILD.gn new file mode 100644 index 000000000..196111fda --- /dev/null +++ b/test/fuzztest/svcextensionproxy_fuzzer/BUILD.gn @@ -0,0 +1,61 @@ +# Copyright (c) 2021-2024 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/app_file_service/app_file_service.gni") +import("//foundation/filemanagement/app_file_service/backup.gni") + +##############################fuzztest########################################## +ohos_fuzztest("SvcExtensionProxyFuzzTest") { + module_out_path = "app_file_service/app_file_service" + fuzz_config_file = + "${app_file_service_path}/test/fuzztest/svcextensionproxy_fuzzer" + include_dirs = [ + "${path_backup}/interfaces/inner_api/native/backup_kit_inner/impl", + "${path_backup}/services/backup_sa/include", + "${path_backup}/tests/unittests/backup_api/backup_impl/include", + "${path_backup_mock}/module_ipc/app_gallery_dispose_proxy_mock.cpp", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ + "$path_backup_mock/module_ipc/svc_extension_proxy_mock.cpp", + "svcextensionproxy_fuzzer.cpp", + ] + + deps = [ + "${path_backup}/utils:backup_utils", + "${path_backup}/tests/utils:backup_test_utils", + "//third_party/googletest:gmock_main", + ] + + external_deps = [ + "c_utils:utils", + "ipc:ipc_core", + ] + + defines = [ + "LOG_TAG=\"app_file_service\"", + "LOG_DOMAIN=0xD200000", + ] +} + +############################################################################### + diff --git a/test/fuzztest/svcextensionproxy_fuzzer/corpus/init b/test/fuzztest/svcextensionproxy_fuzzer/corpus/init new file mode 100644 index 000000000..6198079a2 --- /dev/null +++ b/test/fuzztest/svcextensionproxy_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2024 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/test/fuzztest/svcextensionproxy_fuzzer/project.xml b/test/fuzztest/svcextensionproxy_fuzzer/project.xml new file mode 100644 index 000000000..7133b2b92 --- /dev/null +++ b/test/fuzztest/svcextensionproxy_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/svcextensionproxy_fuzzer/svcextensionproxy_fuzzer.cpp b/test/fuzztest/svcextensionproxy_fuzzer/svcextensionproxy_fuzzer.cpp new file mode 100644 index 000000000..83af9cdfb --- /dev/null +++ b/test/fuzztest/svcextensionproxy_fuzzer/svcextensionproxy_fuzzer.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2024 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 "svcextensionproxy_fuzzer.h" + +#include +#include +#include + +#include "ext_extension_mock.h" +#include "module_ipc/svc_extension_proxy.h" +#include "unique_fd.h" + +using namespace OHOS::FileManagement::Backup; +using namespace std; + +namespace OHOS { +bool SvcExtensionProxyFuzzTest(const uint8_t *data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int))) { + return false; + } + + sptr mock_ = sptr(new BackupExtExtensionMock()); + sptr proxy_ = sptr(new SvcExtensionProxy(mock_)); + std::string fileName((const char *)data, size); + + proxy_->GetFileHandle(fileName); + proxy_->HandleClear(); + proxy_->HandleBackup(); + proxy_->PublishFile(fileName); + proxy_->HandleRestore(); + proxy_->GetIncrementalFileHandle(fileName); + proxy_->PublishIncrementalFile(fileName); + + int incrementalId = -1; + int manifestId = -1; + + if (size >= sizeof(int)) { + incrementalId = *(reinterpret_cast(data)); + manifestId = incrementalId; + } + + if (size >= sizeof(int) + sizeof(int)) { + manifestId = *(reinterpret_cast(data + sizeof(int))); + } + + UniqueFd incrementalFd(incrementalId); + UniqueFd manifestFd(manifestId); + proxy_->HandleIncrementalBackup(move(incrementalFd), move(manifestFd)); + proxy_->GetIncrementalBackupFileHandle(); + return true; +} +}// namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + OHOS::SvcExtensionProxyFuzzTest(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/svcextensionproxy_fuzzer/svcextensionproxy_fuzzer.h b/test/fuzztest/svcextensionproxy_fuzzer/svcextensionproxy_fuzzer.h new file mode 100644 index 000000000..614681418 --- /dev/null +++ b/test/fuzztest/svcextensionproxy_fuzzer/svcextensionproxy_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 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 SVCEXTENSIONPROXY_FUZZER_H +#define SVCEXTENSIONPROXY_FUZZER_H + +#define FUZZ_PROJECT_NAME "svcextensionproxy_fuzzer" + +#endif \ No newline at end of file -- Gitee