From 8d53c584713426207a248feeda56652dd0c6be7f Mon Sep 17 00:00:00 2001 From: wangpggg Date: Tue, 2 Apr 2024 16:02:01 +0800 Subject: [PATCH] add fuzz to impove cover Signed-off-by: wangpeng --- test/fuzztest/BUILD.gn | 1 + .../fileaccessextconnection_fuzzer/BUILD.gn | 51 ++++++++++++++ .../corpus/init | 16 +++++ .../fileaccessextconnection_fuzzer.cpp | 70 +++++++++++++++++++ .../fileaccessextconnection_fuzzer.h | 21 ++++++ .../project.xml | 25 +++++++ 6 files changed, 184 insertions(+) create mode 100644 test/fuzztest/fileaccessextconnection_fuzzer/BUILD.gn create mode 100644 test/fuzztest/fileaccessextconnection_fuzzer/corpus/init create mode 100644 test/fuzztest/fileaccessextconnection_fuzzer/fileaccessextconnection_fuzzer.cpp create mode 100644 test/fuzztest/fileaccessextconnection_fuzzer/fileaccessextconnection_fuzzer.h create mode 100644 test/fuzztest/fileaccessextconnection_fuzzer/project.xml diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 5b5753df..8e55019d 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -28,6 +28,7 @@ group("user_file_service_fuzz_test") { "externalfileaccessopenfile_fuzzer:ExternalFileAccessOpenFileFuzzTest", "externalfileaccessrename_fuzzer:ExternalFileAccessRenameFuzzTest", "externalfileaccessscanfile_fuzzer:ExternalFileAccessScanFileFuzzTest", + "fileaccessextconnection_fuzzer:FileAccessExtConnectionFuzzTest", "fileaccessextstub_fuzzer:FileAccessExtStubFuzzTest", "fileaccessserviceproxy_fuzzer:FileAccessServiceProxyFuzzTest", "fileaccessservicestub_fuzzer:FileAccessServiceStubFuzzTest", diff --git a/test/fuzztest/fileaccessextconnection_fuzzer/BUILD.gn b/test/fuzztest/fileaccessextconnection_fuzzer/BUILD.gn new file mode 100644 index 00000000..4dabd0bd --- /dev/null +++ b/test/fuzztest/fileaccessextconnection_fuzzer/BUILD.gn @@ -0,0 +1,51 @@ +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni") +ohos_fuzztest("FileAccessExtConnectionFuzzTest") { + module_out_path = "user_file_service/user_file_service" + fuzz_config_file = + "${user_file_service_path}/test/fuzztest/fileaccessextconnection_fuzzer" + include_dirs = [ + "${ability_runtime_path}/interfaces/inner_api/ability_manager/include", + "${user_file_service_path}/interfaces/inner_api/file_access/include", + "${user_file_service_path}/interfaces/kits/js/src/common", + "${user_file_service_path}/utils", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "${user_file_service_path}/interfaces/inner_api/file_access/src/file_access_ext_connection.cpp", + "${user_file_service_path}/interfaces/inner_api/file_access/src/file_access_ext_proxy.cpp", + "${user_file_service_path}/test/fuzztest/fileaccessextconnection_fuzzer/fileaccessextconnection_fuzzer.cpp", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "c_utils:utils", + "hilog:libhilog", + "hitrace:hitrace_meter", + "ipc:ipc_core", + ] + + defines = [ "private=public" ] +} diff --git a/test/fuzztest/fileaccessextconnection_fuzzer/corpus/init b/test/fuzztest/fileaccessextconnection_fuzzer/corpus/init new file mode 100644 index 00000000..6198079a --- /dev/null +++ b/test/fuzztest/fileaccessextconnection_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/fileaccessextconnection_fuzzer/fileaccessextconnection_fuzzer.cpp b/test/fuzztest/fileaccessextconnection_fuzzer/fileaccessextconnection_fuzzer.cpp new file mode 100644 index 00000000..d7a29ad0 --- /dev/null +++ b/test/fuzztest/fileaccessextconnection_fuzzer/fileaccessextconnection_fuzzer.cpp @@ -0,0 +1,70 @@ +/* + * 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 "fileaccessextconnection_fuzzer.h" + +#include +#include + +#include "hilog_wrapper.h" +#include "file_access_ext_connection.h" + +namespace OHOS { +using namespace std; +using namespace FileAccessFwk; + +bool OnAbilityConnectDoneFuzzTest(shared_ptr conn) +{ + AppExecFwk::ElementName element; + sptr remoteObject = nullptr; + conn->OnAbilityConnectDone(element, remoteObject, 0); + return true; +} + +bool OnAbilityDisconnectDoneFuzzTest(shared_ptr conn) +{ + AppExecFwk::ElementName element; + conn->OnAbilityDisconnectDone(element, 0); + return true; +} + +bool IsExtAbilityConnectedFuzzTest(shared_ptr conn) +{ + conn->IsExtAbilityConnected(); + return true; +} + +bool GetFileExtProxyFuzzTest(shared_ptr conn) +{ + conn->GetFileExtProxy(); + return true; +} + +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + auto conn = std::make_shared(); + if (conn == nullptr) { + return 0; + } + + OHOS::OnAbilityConnectDoneFuzzTest(conn); + OHOS::OnAbilityDisconnectDoneFuzzTest(conn); + OHOS::IsExtAbilityConnectedFuzzTest(conn); + OHOS::GetFileExtProxyFuzzTest(conn); + + return 0; +} diff --git a/test/fuzztest/fileaccessextconnection_fuzzer/fileaccessextconnection_fuzzer.h b/test/fuzztest/fileaccessextconnection_fuzzer/fileaccessextconnection_fuzzer.h new file mode 100644 index 00000000..1cea84cb --- /dev/null +++ b/test/fuzztest/fileaccessextconnection_fuzzer/fileaccessextconnection_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 USER_ACCESS_COMMON_UTILS_FUZZER_H +#define USER_ACCESS_COMMON_UTILS_FUZZER_H + +#define FUZZ_PROJECT_NAME "fileaccessextconnection_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/fileaccessextconnection_fuzzer/project.xml b/test/fuzztest/fileaccessextconnection_fuzzer/project.xml new file mode 100644 index 00000000..d6679ccc --- /dev/null +++ b/test/fuzztest/fileaccessextconnection_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + -- Gitee