From b91558a32faae45af7d3a3166f398a9df8e9f179 Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Fri, 29 Mar 2024 09:56:26 +0800 Subject: [PATCH] add afs fuzz test Signed-off-by: lvyuanyuan Change-Id: Ie4a9ad3a13a50c88439bb6b0bc66c916d11a6460 --- test/fuzztest/BUILD.gn | 1 + .../svcrestoredepsmanager_fuzzer/BUILD.gn | 55 +++++++++ .../svcrestoredepsmanager_fuzzer/corpus/init | 16 +++ .../svcrestoredepsmanager_fuzzer/project.xml | 25 ++++ .../svcrestoredepsmanager_fuzzer.cpp | 113 ++++++++++++++++++ .../svcrestoredepsmanager_fuzzer.h | 21 ++++ 6 files changed, 231 insertions(+) create mode 100644 test/fuzztest/svcrestoredepsmanager_fuzzer/BUILD.gn create mode 100644 test/fuzztest/svcrestoredepsmanager_fuzzer/corpus/init create mode 100644 test/fuzztest/svcrestoredepsmanager_fuzzer/project.xml create mode 100644 test/fuzztest/svcrestoredepsmanager_fuzzer/svcrestoredepsmanager_fuzzer.cpp create mode 100644 test/fuzztest/svcrestoredepsmanager_fuzzer/svcrestoredepsmanager_fuzzer.h diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 12a10c0b2..02cd04307 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -21,5 +21,6 @@ group("fuzztest") { "fileshare_fuzzer:FileShareFuzzTest", "remotefileshare_fuzzer:RemoteFileShareFuzzTest", "servicereverse_fuzzer:ServiceReverseFuzzTest", + "svcrestoredepsmanager_fuzzer:SvcRestoreDepsManagerFuzzTest", ] } diff --git a/test/fuzztest/svcrestoredepsmanager_fuzzer/BUILD.gn b/test/fuzztest/svcrestoredepsmanager_fuzzer/BUILD.gn new file mode 100644 index 000000000..f66fc160e --- /dev/null +++ b/test/fuzztest/svcrestoredepsmanager_fuzzer/BUILD.gn @@ -0,0 +1,55 @@ +# 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("SvcRestoreDepsManagerFuzzTest") { + module_out_path = "app_file_service/app_file_service" + fuzz_config_file = + "${app_file_service_path}/test/fuzztest/svcrestoredepsmanager_fuzzer" + include_dirs = [ "${path_backup}/services/backup_sa/include" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ + "${path_backup}/services/backup_sa/src/module_ipc/svc_restore_deps_manager.cpp", + "svcrestoredepsmanager_fuzzer.cpp", + ] + + deps = [ + "${path_backup}/interfaces/inner_api/native/backup_kit_inner:backup_kit_inner", + "${path_backup}/utils:backup_utils", + "${path_jsoncpp}:jsoncpp", + ] + + external_deps = [ "hilog:libhilog" ] + + defines = [ + "LOG_TAG=\"app_file_service\"", + "LOG_DOMAIN=0xD200000", + ] + + use_exceptions = true +} + +############################################################################### + diff --git a/test/fuzztest/svcrestoredepsmanager_fuzzer/corpus/init b/test/fuzztest/svcrestoredepsmanager_fuzzer/corpus/init new file mode 100644 index 000000000..6198079a2 --- /dev/null +++ b/test/fuzztest/svcrestoredepsmanager_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/svcrestoredepsmanager_fuzzer/project.xml b/test/fuzztest/svcrestoredepsmanager_fuzzer/project.xml new file mode 100644 index 000000000..7133b2b92 --- /dev/null +++ b/test/fuzztest/svcrestoredepsmanager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/svcrestoredepsmanager_fuzzer/svcrestoredepsmanager_fuzzer.cpp b/test/fuzztest/svcrestoredepsmanager_fuzzer/svcrestoredepsmanager_fuzzer.cpp new file mode 100644 index 000000000..94a43f774 --- /dev/null +++ b/test/fuzztest/svcrestoredepsmanager_fuzzer/svcrestoredepsmanager_fuzzer.cpp @@ -0,0 +1,113 @@ +/* + * 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 "svcrestoredepsmanager_fuzzer.h" + +#include +#include +#include +#include "securec.h" + +#include "module_ipc/svc_restore_deps_manager.h" + +using namespace OHOS::FileManagement::Backup; +using namespace std; + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 3; +constexpr int SPLITE_SIZE = 5; +constexpr size_t REDUNDENT_SIZE = 2; +constexpr size_t DISTINCT_SIZE = 2; +void GetInfo(const uint8_t *data, size_t size, vector &info) +{ + size_t base = size; + if (size >= U32_AT_SIZE) { + base = (size + 1) / REDUNDENT_SIZE; + } + + for (size_t i = 0; i < size; i++) { + BJsonEntityCaps::BundleInfo bundleInfo; + int len = size / SPLITE_SIZE; + int pos = 0; + bundleInfo.name = string(reinterpret_cast(data + pos), len) + to_string(i % base); + pos += len; + bundleInfo.versionName = string(reinterpret_cast(data + pos), len) + to_string(i); + pos += len; + bundleInfo.extensionName = string(reinterpret_cast(data + pos), len) + to_string(i); + pos += len; + + if (i % DISTINCT_SIZE == 0) { + bundleInfo.restoreDeps = string(reinterpret_cast(data + pos), len) + to_string(i); + } else { + bundleInfo.restoreDeps = string(reinterpret_cast(data + pos), len) + "," + to_string(i); + } + + pos += len; + bundleInfo.supportScene = string(reinterpret_cast(data + pos), len) + to_string(i); + + if (size >= sizeof(bool)) { + bundleInfo.allToBackup = *(reinterpret_cast(data)); + } + + if (size >= sizeof(uint32_t)) { + bundleInfo.versionCode = *(reinterpret_cast(data)); + } + + if (size >= sizeof(int64_t)) { + bundleInfo.spaceOccupied = *(reinterpret_cast(data)); + } + info.push_back(bundleInfo); + } +} + +bool SvcRestoreDepsManagerFuzzTest(const uint8_t *data, size_t size) +{ + vector bundleInfos; + RestoreTypeEnum restoreType = *(reinterpret_cast(data)); + SvcRestoreDepsManager::GetInstance().GetRestoreBundleNames(bundleInfos, restoreType); + GetInfo(data, size, bundleInfos); + + BJsonEntityCaps::BundleInfo bundleInfo; + bundleInfo.name = string(reinterpret_cast(data), size); + bundleInfos.push_back(bundleInfo); + + SvcRestoreDepsManager::GetInstance().GetRestoreBundleNames(bundleInfos, restoreType); + SvcRestoreDepsManager::GetInstance().GetRestoreBundleMap(); + + size_t pos = size >> 1; + string bundleName(string(reinterpret_cast(data), pos)); + string fileName(string(reinterpret_cast(data + pos), size - pos)); + SvcRestoreDepsManager::GetInstance().AddRestoredBundles(bundleName); + SvcRestoreDepsManager::GetInstance().GetAllBundles(); + + SvcRestoreDepsManager::GetInstance().UpdateToRestoreBundleMap(bundleName, fileName); + SvcRestoreDepsManager::GetInstance().UpdateToRestoreBundleMap(fileName, bundleName); + SvcRestoreDepsManager::GetInstance().IsAllBundlesRestored(); + + 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::SvcRestoreDepsManagerFuzzTest(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/svcrestoredepsmanager_fuzzer/svcrestoredepsmanager_fuzzer.h b/test/fuzztest/svcrestoredepsmanager_fuzzer/svcrestoredepsmanager_fuzzer.h new file mode 100644 index 000000000..97c290d84 --- /dev/null +++ b/test/fuzztest/svcrestoredepsmanager_fuzzer/svcrestoredepsmanager_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 SVCRESTOREDEPSMANAGER_FUZZER_H +#define SVCRESTOREDEPSMANAGER_FUZZER_H + +#define FUZZ_PROJECT_NAME "svcrestoredepsmanager_fuzzer" + +#endif \ No newline at end of file -- Gitee