From 7740de3f21f0e3966b4c421f7efe48f7eed315c2 Mon Sep 17 00:00:00 2001 From: tianp Date: Sat, 26 Jul 2025 14:31:38 +0800 Subject: [PATCH 1/2] =?UTF-8?q?master=E5=90=88=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke Change-Id: I1d411ad2cccc1759e84f108d48e259c0e540250d --- interfaces/kits/js/BUILD.gn | 230 +++- .../src/common/ani_helper/ani_signature.cpp | 1 + .../js/src/common/ani_helper/ani_signature.h | 1 + .../js/src/mod_fs/ani/bind_function_class.cpp | 169 ++- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 584 +++------ .../mod_fs/properties/ani/fdatasync_ani.cpp | 44 + .../src/mod_fs/properties/ani/fdatasync_ani.h | 36 + .../properties/ani/fdopen_stream_ani.cpp | 63 + .../mod_fs/properties/ani/fdopen_stream_ani.h | 33 + .../src/mod_fs/properties/ani/fsync_ani.cpp | 41 + .../js/src/mod_fs/properties/ani/fsync_ani.h | 35 + .../mod_fs/properties/ani/listfile_ani.cpp | 4 +- .../src/mod_fs/properties/ani/lseek_ani.cpp | 63 + .../js/src/mod_fs/properties/ani/lseek_ani.h | 34 + .../src/mod_fs/properties/ani/symlink_ani.h | 2 +- .../js/src/mod_fs/properties/ani/utimes_ani.h | 2 +- .../js/src/mod_fs/properties/copy_core.cpp | 891 +++++++++++++ .../kits/js/src/mod_fs/properties/copy_core.h | 190 +++ .../dfs_listener/file_dfs_listener_stub.h | 2 +- .../src/mod_fs/properties/fdatasync_core.cpp | 44 + .../js/src/mod_fs/properties/fdatasync_core.h | 29 + .../mod_fs/properties/fdopen_stream_core.cpp | 49 + .../mod_fs/properties/fdopen_stream_core.h | 39 + .../js/src/mod_fs/properties/fsync_core.cpp | 42 + .../js/src/mod_fs/properties/fsync_core.h | 29 + .../js/src/mod_fs/properties/lseek_core.cpp | 53 + .../js/src/mod_fs/properties/lseek_core.h | 33 + .../src/mod_fs/properties/read_lines_core.cpp | 106 ++ .../js/src/mod_fs/properties/symlink_core.h | 1 - .../src/mod_hash/ani/bind_function_class.cpp | 3 +- .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 50 +- .../class_hashstream/ani/hashstream_ani.cpp | 123 ++ .../class_hashstream/ani/hashstream_ani.h | 37 + .../class_hashstream/hs_hashstream.cpp | 161 +++ .../mod_hash/class_hashstream/hs_hashstream.h | 46 + .../class_hashstream/hs_hashstream_entity.h | 37 + .../ani/ets/@ohos.file.securityLabel.ets | 16 +- interfaces/test/unittest/BUILD.gn | 2 + interfaces/test/unittest/js/BUILD.gn | 197 ++- .../mod_fs/class_file/fs_file_mock_test.cpp | 180 +-- .../js/mod_fs/class_file/fs_file_test.cpp | 168 +-- .../mod_fs/class_stat/fs_stat_mock_test.cpp | 40 +- .../class_stream/fs_stream_mock_test.cpp | 191 +++ .../js/mod_fs/class_stream/fs_stream_test.cpp | 791 ++++++++++++ .../js/mod_fs/class_stream/mock/c_mock.cpp | 97 ++ .../js/mod_fs/class_stream/mock/c_mock.h | 49 + .../unittest/js/mod_fs/common/fs_err_code.h | 34 + .../unittest/js/mod_fs/mock/eventfd_mock.cpp | 83 ++ .../unittest/js/mod_fs/mock/eventfd_mock.h | 53 + .../unittest/js/mod_fs/mock/inotify_mock.cpp | 125 ++ .../unittest/js/mod_fs/mock/inotify_mock.h | 57 + .../js/mod_fs/mock/mock_progress_listener.h | 29 + .../unittest/js/mod_fs/mock/poll_mock.cpp | 83 ++ .../test/unittest/js/mod_fs/mock/poll_mock.h | 53 + .../unittest/js/mod_fs/mock/unistd_mock.cpp | 125 ++ .../unittest/js/mod_fs/mock/unistd_mock.h | 57 + .../properties/access_core_mock_test.cpp | 39 +- .../js/mod_fs/properties/access_core_test.cpp | 12 +- .../mod_fs/properties/copy_core_mock_test.cpp | 552 ++++++++ .../js/mod_fs/properties/copy_core_test.cpp | 1150 +++++++++++++++++ .../properties/fdatasync_core_mock_test.cpp | 102 ++ .../properties/fdopen_stream_core_test.cpp | 117 ++ .../properties/fsync_core_mock_test.cpp | 98 ++ .../mod_fs/properties/listfile_core_test.cpp | 17 - .../js/mod_fs/properties/lseek_core_test.cpp | 115 ++ .../properties/mkdir_core_mock_test.cpp | 10 +- .../js/mod_fs/properties/mock/system_mock.cpp | 2 +- .../js/mod_fs/properties/mock/system_mock.h | 4 +- .../js/mod_fs/properties/mock/uv_fs_mock.cpp | 8 +- .../js/mod_fs/properties/mock/uv_fs_mock.h | 25 +- .../mod_fs/properties/open_core_mock_test.cpp | 77 +- .../js/mod_fs/properties/open_core_test.cpp | 88 +- .../mod_fs/properties/stat_core_mock_test.cpp | 29 +- .../js/mod_fs/properties/stat_core_test.cpp | 4 +- .../properties/trans_listener_mock_test.cpp | 237 ++++ .../mod_fs/properties/trans_listener_test.cpp | 577 +++++++++ .../properties/unlink_core_mock_test.cpp | 5 +- .../unittest/js/mod_hash/hash_core_test.cpp | 5 - .../securitylabel_core_test.cpp | 5 - interfaces/test/unittest/task_signal/BUILD.gn | 7 + 80 files changed, 8150 insertions(+), 872 deletions(-) create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/copy_core.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/copy_core.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/fsync_core.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/fsync_core.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/lseek_core.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/lseek_core.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp create mode 100644 interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp create mode 100644 interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.h create mode 100644 interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.cpp create mode 100644 interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.h create mode 100644 interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream_entity.h create mode 100644 interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h create mode 100644 interfaces/test/unittest/js/mod_fs/common/fs_err_code.h create mode 100644 interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.h create mode 100644 interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/mock/inotify_mock.h create mode 100644 interfaces/test/unittest/js/mod_fs/mock/mock_progress_listener.h create mode 100644 interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/mock/poll_mock.h create mode 100644 interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h create mode 100644 interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/copy_core_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/fdatasync_core_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/fdopen_stream_core_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/fsync_core_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/lseek_core_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/trans_listener_test.cpp diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index f9fd98aa6..22590dc37 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -628,14 +628,17 @@ group("build_kits_js") { group("ani_file_api") { deps = [ + ":ani_file_environment", ":ani_file_fs", ":ani_file_hash", ":ani_file_securitylabel", + ":ani_file_statvfs", ] } config("ani_config") { include_dirs = [ + "./include", "${file_api_path}/interfaces/kits/rust/include", "${utils_path}/common/include", "${utils_path}/filemgmt_libfs/include", @@ -661,13 +664,28 @@ config("ani_config") { ohos_shared_library("ani_file_fs") { public_configs = [ ":ani_config" ] include_dirs = [ + "include/ipc", "src/mod_fs/ani", + "src/mod_fs/class_atomicfile", + "src/mod_fs/class_atomicfile/ani", "src/mod_fs/class_file", "src/mod_fs/class_file/ani", + "src/mod_fs/class_randomaccessfile", + "src/mod_fs/class_randomaccessfile/ani", + "src/mod_fs/class_readeriterator", + "src/mod_fs/class_readeriterator/ani", "src/mod_fs/class_stat", "src/mod_fs/class_stat/ani", + "src/mod_fs/class_stream", + "src/mod_fs/class_stream/ani", + "src/mod_fs/class_tasksignal", + "src/mod_fs/class_tasksignal/ani", + "src/mod_fs/class_watcher", + "src/mod_fs/class_watcher/ani", "src/mod_fs/properties", "src/mod_fs/properties/ani", + "src/mod_fs/properties/copy_listener", + "src/mod_fs/properties/copy_listener/ani", ] sources = [ "src/common/ani_helper/ani_signature.cpp", @@ -675,42 +693,102 @@ ohos_shared_library("ani_file_fs") { "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", "src/mod_fs/ani/bind_function_class.cpp", + "src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp", + "src/mod_fs/class_atomicfile/fs_atomicfile.cpp", "src/mod_fs/class_file/ani/file_ani.cpp", "src/mod_fs/class_file/ani/file_wrapper.cpp", "src/mod_fs/class_file/file_instantiator.cpp", "src/mod_fs/class_file/fs_file.cpp", + "src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp", + "src/mod_fs/class_randomaccessfile/fs_randomaccessfile.cpp", + "src/mod_fs/class_readeriterator/ani/reader_iterator_ani.cpp", + "src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.cpp", + "src/mod_fs/class_readeriterator/fs_reader_iterator.cpp", + "src/mod_fs/class_stat/ani/lstat_ani.cpp", "src/mod_fs/class_stat/ani/stat_ani.cpp", "src/mod_fs/class_stat/ani/stat_wrapper.cpp", "src/mod_fs/class_stat/fs_stat.cpp", "src/mod_fs/class_stat/stat_instantiator.cpp", + "src/mod_fs/class_stream/ani/stream_ani.cpp", + "src/mod_fs/class_stream/ani/stream_wrapper.cpp", + "src/mod_fs/class_stream/fs_stream.cpp", + "src/mod_fs/class_stream/stream_instantiator.cpp", + "src/mod_fs/class_tasksignal/ani/task_signal_ani.cpp", + "src/mod_fs/class_tasksignal/ani/task_signal_listener_ani.cpp", + "src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp", + "src/mod_fs/class_tasksignal/fs_task_signal.cpp", + "src/mod_fs/class_watcher/ani/fs_watcher_ani.cpp", + "src/mod_fs/class_watcher/ani/fs_watcher_wrapper.cpp", + "src/mod_fs/class_watcher/ani/watch_event_listener.cpp", + "src/mod_fs/class_watcher/ani/watch_event_wrapper.cpp", + "src/mod_fs/class_watcher/fs_file_watcher.cpp", + "src/mod_fs/class_watcher/fs_watcher.cpp", + "src/mod_fs/class_watcher/watcher_data_cache.cpp", "src/mod_fs/fs_utils.cpp", "src/mod_fs/properties/access_core.cpp", "src/mod_fs/properties/ani/access_ani.cpp", "src/mod_fs/properties/ani/close_ani.cpp", + "src/mod_fs/properties/ani/copy_ani.cpp", + "src/mod_fs/properties/ani/copy_dir_ani.cpp", "src/mod_fs/properties/ani/copy_file_ani.cpp", + "src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp", + "src/mod_fs/properties/ani/create_stream_ani.cpp", + "src/mod_fs/properties/ani/dup_ani.cpp", + "src/mod_fs/properties/ani/fdatasync_ani.cpp", + "src/mod_fs/properties/ani/fdopen_stream_ani.cpp", + "src/mod_fs/properties/ani/fsync_ani.cpp", "src/mod_fs/properties/ani/listfile_ani.cpp", + "src/mod_fs/properties/ani/lseek_ani.cpp", "src/mod_fs/properties/ani/mkdir_ani.cpp", + "src/mod_fs/properties/ani/mkdtemp_ani.cpp", "src/mod_fs/properties/ani/move_ani.cpp", + "src/mod_fs/properties/ani/movedir_ani.cpp", "src/mod_fs/properties/ani/open_ani.cpp", "src/mod_fs/properties/ani/read_ani.cpp", + "src/mod_fs/properties/ani/read_lines_ani.cpp", "src/mod_fs/properties/ani/read_text_ani.cpp", + "src/mod_fs/properties/ani/rename_ani.cpp", "src/mod_fs/properties/ani/rmdir_ani.cpp", + "src/mod_fs/properties/ani/symlink_ani.cpp", "src/mod_fs/properties/ani/truncate_ani.cpp", "src/mod_fs/properties/ani/unlink_ani.cpp", + "src/mod_fs/properties/ani/utimes_ani.cpp", + "src/mod_fs/properties/ani/watcher_ani.cpp", "src/mod_fs/properties/ani/write_ani.cpp", + "src/mod_fs/properties/ani/xattr_ani.cpp", "src/mod_fs/properties/close_core.cpp", + "src/mod_fs/properties/copy_core.cpp", + "src/mod_fs/properties/copy_dir_core.cpp", "src/mod_fs/properties/copy_file_core.cpp", + "src/mod_fs/properties/copy_listener/ani/progress_listener_ani.cpp", + "src/mod_fs/properties/copy_listener/trans_listener_core.cpp", + "src/mod_fs/properties/create_randomaccessfile_core.cpp", + "src/mod_fs/properties/create_stream_core.cpp", + "src/mod_fs/properties/dup_core.cpp", + "src/mod_fs/properties/fdatasync_core.cpp", + "src/mod_fs/properties/fdopen_stream_core.cpp", + "src/mod_fs/properties/fsync_core.cpp", "src/mod_fs/properties/listfile_core.cpp", + "src/mod_fs/properties/lseek_core.cpp", + "src/mod_fs/properties/lstat_core.cpp", "src/mod_fs/properties/mkdir_core.cpp", + "src/mod_fs/properties/mkdtemp_core.cpp", "src/mod_fs/properties/move_core.cpp", + "src/mod_fs/properties/movedir_core.cpp", "src/mod_fs/properties/open_core.cpp", "src/mod_fs/properties/read_core.cpp", + "src/mod_fs/properties/read_lines_core.cpp", "src/mod_fs/properties/read_text_core.cpp", + "src/mod_fs/properties/rename_core.cpp", "src/mod_fs/properties/rmdir_core.cpp", "src/mod_fs/properties/stat_core.cpp", + "src/mod_fs/properties/symlink_core.cpp", "src/mod_fs/properties/truncate_core.cpp", "src/mod_fs/properties/unlink_core.cpp", + "src/mod_fs/properties/utimes_core.cpp", + "src/mod_fs/properties/watcher_core.cpp", "src/mod_fs/properties/write_core.cpp", + "src/mod_fs/properties/xattr_core.cpp", ] deps = [ ":ohos_file_fs_abc_etc", @@ -724,7 +802,7 @@ ohos_shared_library("ani_file_fs") { external_deps = [ "ability_runtime:ability_manager", "app_file_service:fileuri_native", - "bounds_checking_function:libsec_static", + "bounds_checking_function:libsec_shared", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", @@ -777,6 +855,8 @@ ohos_shared_library("ani_file_hash") { include_dirs = [ "src/mod_hash", "src/mod_hash/ani", + "src/mod_hash/class_hashstream", + "src/mod_hash/class_hashstream/ani", ] sources = [ "src/common/ani_helper/ani_signature.cpp", @@ -787,6 +867,8 @@ ohos_shared_library("ani_file_hash") { "src/mod_fs/fs_utils.cpp", "src/mod_hash/ani/bind_function_class.cpp", "src/mod_hash/ani/hash_ani.cpp", + "src/mod_hash/class_hashstream/ani/hashstream_ani.cpp", + "src/mod_hash/class_hashstream/hs_hashstream.cpp", "src/mod_hash/hash_core.cpp", ] @@ -797,10 +879,8 @@ ohos_shared_library("ani_file_hash") { "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] external_deps = [ - "bounds_checking_function:libsec_static", - "eventhandler:libeventhandler", + "bounds_checking_function:libsec_shared", "hilog:libhilog", - "ipc:ipc_core", "libuv:uv", "openssl:libcrypto_shared", "runtime_core:ani", @@ -862,10 +942,8 @@ ohos_shared_library("ani_file_securitylabel") { "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] external_deps = [ - "bounds_checking_function:libsec_static", - "eventhandler:libeventhandler", + "bounds_checking_function:libsec_shared", "hilog:libhilog", - "ipc:ipc_core", "libuv:uv", "runtime_core:ani", "runtime_core:ani_helpers", @@ -901,3 +979,141 @@ ohos_prebuilt_etc("ohos_file_securityLabel_abc_etc") { part_name = "file_api" deps = [ ":ohos_file_securityLabel_abc" ] } + +ohos_shared_library("ani_file_environment") { + public_configs = [ ":ani_config" ] + include_dirs = [ + "src/mod_environment/ani", + "src/mod_environment", + ] + sources = [ + "src/common/ani_helper/ani_signature.cpp", + "src/common/ani_helper/error_handler.cpp", + "src/common/ani_helper/type_converter.cpp", + "src/common/file_helper/fd_guard.cpp", + "src/mod_environment/ani/bind_function_class.cpp", + "src/mod_environment/ani/environment_ani.cpp", + "src/mod_environment/environment_core.cpp", + "src/mod_fs/fs_utils.cpp", + ] + + deps = [ + ":ohos_file_environment_abc_etc", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + ] + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "bounds_checking_function:libsec_shared", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "libuv:uv", + "openssl:libcrypto_shared", + "os_account:os_account_innerkits", + "runtime_core:ani", + "runtime_core:ani_helpers", + "runtime_core:libarkruntime", + ] + use_exceptions = true + + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + output_extension = "so" + subsystem_name = "filemanagement" + part_name = "file_api" +} + +generate_static_abc("ohos_file_environment_abc") { + base_url = "./src/mod_environment/ani/ets" + files = [ "./src/mod_environment/ani/ets/@ohos.file.environment.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/ohos_file_environment_abc.abc" +} + +ohos_prebuilt_etc("ohos_file_environment_abc_etc") { + source = "$target_out_dir/ohos_file_environment_abc.abc" + module_install_dir = "framework" + subsystem_name = "filemanagement" + part_name = "file_api" + deps = [ ":ohos_file_environment_abc" ] +} + +ohos_shared_library("ani_file_statvfs") { + public_configs = [ ":ani_config" ] + include_dirs = [ + "src/mod_statvfs/ani", + "src/mod_statvfs", + ] + sources = [ + "src/common/ani_helper/ani_signature.cpp", + "src/common/ani_helper/error_handler.cpp", + "src/common/ani_helper/type_converter.cpp", + "src/common/file_helper/fd_guard.cpp", + "src/mod_fs/fs_utils.cpp", + "src/mod_statvfs/ani/bind_function_class.cpp", + "src/mod_statvfs/ani/statvfs_ani.cpp", + "src/mod_statvfs/statvfs_core.cpp", + ] + + deps = [ + ":ohos_file_statvfs_abc_etc", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + ] + external_deps = [ + "bounds_checking_function:libsec_shared", + "hilog:libhilog", + "libuv:uv", + "runtime_core:ani", + "runtime_core:ani_helpers", + "runtime_core:libarkruntime", + ] + use_exceptions = true + cflags = [ + "-fdata-sections", + "-ffunction-sections", + "-Oz", + ] + cflags_cc = [ + "-fvisibility-inlines-hidden", + "-Oz", + ] + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + output_extension = "so" + subsystem_name = "filemanagement" + part_name = "file_api" +} + +generate_static_abc("ohos_file_statvfs_abc") { + base_url = "./src/mod_statvfs/ani/ets" + files = [ "./src/mod_statvfs/ani/ets/@ohos.file.statvfs.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/ohos_file_statvfs_abc.abc" +} + +ohos_prebuilt_etc("ohos_file_statvfs_abc_etc") { + source = "$target_out_dir/ohos_file_statvfs_abc.abc" + module_install_dir = "framework" + subsystem_name = "filemanagement" + part_name = "file_api" + deps = [ ":ohos_file_statvfs_abc" ] +} diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp index 1599dfa75..616c62f96 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp @@ -30,6 +30,7 @@ const Type BasicTypes::doubleType = Builder::BuildDouble(); // BasicTypesConverter::toLong const string BasicTypesConverter::toLong = "toLong"; const string BasicTypesConverter::toInt = "toInt"; +const string BasicTypesConverter::toDouble = "toDouble"; // BoxedTypes::Boolean const Type BoxedTypes::Boolean::classType = Builder::BuildClass("std.core.Boolean"); diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.h b/interfaces/kits/js/src/common/ani_helper/ani_signature.h index c422af267..7118623aa 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.h +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.h @@ -41,6 +41,7 @@ struct BasicTypes { struct BasicTypesConverter { static const string toLong; static const string toInt; + static const string toDouble; }; struct BaseType { diff --git a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index f8da76c82..f9ebd1051 100644 --- a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp @@ -13,31 +13,84 @@ * limitations under the License. */ -#include - #include +#include #include "access_ani.h" #include "ani_signature.h" +#include "atomicfile_ani.h" #include "bind_function.h" #include "close_ani.h" +#include "connectdfs_ani.h" +#include "copy_ani.h" +#include "copy_dir_ani.h" #include "copy_file_ani.h" +#include "create_randomaccessfile_ani.h" +#include "create_stream_ani.h" +#include "disconnectdfs_ani.h" +#include "dup_ani.h" +#include "fdatasync_ani.h" +#include "fdopen_stream_ani.h" #include "file_ani.h" +#include "filemgmt_libhilog.h" +#include "fs_watcher_ani.h" +#include "fsync_ani.h" #include "listfile_ani.h" +#include "lseek_ani.h" +#include "lstat_ani.h" #include "mkdir_ani.h" +#include "mkdtemp_ani.h" #include "move_ani.h" -#include "read_ani.h" -#include "rmdir_ani.h" +#include "movedir_ani.h" #include "open_ani.h" +#include "randomaccessfile_ani.h" +#include "read_ani.h" +#include "read_lines_ani.h" #include "read_text_ani.h" +#include "reader_iterator_ani.h" +#include "rename_ani.h" +#include "rmdir_ani.h" #include "stat_ani.h" +#include "stream_ani.h" +#include "symlink_ani.h" +#include "task_signal_ani.h" #include "truncate_ani.h" #include "unlink_ani.h" +#include "utimes_ani.h" +#include "watcher_ani.h" #include "write_ani.h" +#include "xattr_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; +static ani_status BindRafFileMethods(ani_env *env) +{ + auto classDesc = FS::RandomAccessFileInner::classDesc.c_str(); + + std::array methods = { + ani_native_function { + "setFilePointer0", nullptr, reinterpret_cast(RandomAccessFileAni::SetFilePointer) }, + ani_native_function { "close", nullptr, reinterpret_cast(RandomAccessFileAni::Close) }, + ani_native_function { "writeSync0", nullptr, reinterpret_cast(RandomAccessFileAni::WriteSync) }, + ani_native_function { "readSync0", nullptr, reinterpret_cast(RandomAccessFileAni::ReadSync) }, + }; + + return BindClass(env, classDesc, methods); +} + +static ani_status BindWatcherClassMethods(ani_env *env) +{ + auto classDesc = FS::WatcherInner::classDesc.c_str(); + + std::array methods = { + ani_native_function { "start", nullptr, reinterpret_cast(FsWatcherAni::Start) }, + ani_native_function { "stop", nullptr, reinterpret_cast(FsWatcherAni::Stop) }, + }; + + return BindClass(env, classDesc, methods); +} + static ani_status BindFileMethods(ani_env *env) { auto classDesc = FS::FileInner::classDesc.c_str(); @@ -52,6 +105,17 @@ static ani_status BindFileMethods(ani_env *env) return BindClass(env, classDesc, methods); } +static ani_status BindReaderIteratorMethods(ani_env *env) +{ + auto classDesc = FS::ReaderIteratorInner::classDesc.c_str(); + + std::array methods = { + ani_native_function { "next", nullptr, reinterpret_cast(ReaderIteratorAni::Next) }, + }; + + return BindClass(env, classDesc, methods); +} + static ani_status BindStatClassMethods(ani_env *env) { auto classDesc = FS::StatInner::classDesc.c_str(); @@ -69,6 +133,49 @@ static ani_status BindStatClassMethods(ani_env *env) return BindClass(env, classDesc, methods); } +static ani_status BindStreamMethods(ani_env *env) +{ + auto classDesc = FS::StreamInner::classDesc.c_str(); + + std::array methods = { + ani_native_function { "closeSync", nullptr, reinterpret_cast(StreamAni::Close) }, + ani_native_function { "flushSync", nullptr, reinterpret_cast(StreamAni::Flush) }, + ani_native_function { "readSync", nullptr, reinterpret_cast(StreamAni::Read) }, + ani_native_function { "writeSync", nullptr, reinterpret_cast(StreamAni::Write) }, + ani_native_function { "seek", nullptr, reinterpret_cast(StreamAni::Seek) }, + }; + + return BindClass(env, classDesc, methods); +} + +static ani_status BindTaskSignalClassMethods(ani_env *env) +{ + auto classDesc = FS::TaskSignal::classDesc.c_str(); + + std::array methods = { + ani_native_function { "cancel", nullptr, reinterpret_cast(TaskSignalAni::Cancel) }, + ani_native_function { "onCancelNative", nullptr, reinterpret_cast(TaskSignalAni::OnCancel) }, + }; + + return BindClass(env, classDesc, methods); +} + +static ani_status BindAtomicFileMethods(ani_env *env) +{ + auto classDesc = FS::AtomicFile::classDesc.c_str(); + auto ctorDesc = FS::AtomicFile::ctorDesc.c_str(); + auto ctorSig = FS::AtomicFile::ctorSig.c_str(); + + std::array methods = { + ani_native_function { "getPath", nullptr, reinterpret_cast(AtomicFileAni::GetPath) }, + ani_native_function { "getBaseFile", nullptr, reinterpret_cast(AtomicFileAni::GetBaseFile) }, + ani_native_function { "readFully", nullptr, reinterpret_cast(AtomicFileAni::ReadFully) }, + ani_native_function { "delete", nullptr, reinterpret_cast(AtomicFileAni::Delete) }, + ani_native_function { ctorDesc, ctorSig, reinterpret_cast(AtomicFileAni::Constructor) }, + }; + + return BindClass(env, classDesc, methods); +} const static string mkdirCtorSig0 = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); const static string mkdirCtorSig1 = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType, BasicTypes::booleanType }); @@ -79,19 +186,43 @@ static ani_status BindStaticMethods(ani_env *env) std::array methods = { ani_native_function { "closeSync", nullptr, reinterpret_cast(CloseAni::CloseSync) }, + ani_native_function { "connectDfs", nullptr, reinterpret_cast(ConnectDfsAni::ConnectDfsSync) }, + ani_native_function { "copyDirSync", nullptr, reinterpret_cast(CopyDirAni::CopyDirSync) }, ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, + ani_native_function { "copySync", nullptr, reinterpret_cast(CopyAni::CopySync) }, + ani_native_function { "createRandomAccessFileSync", nullptr, + reinterpret_cast(CreateRandomAccessFileAni::CreateRandomAccessFileSync) }, + ani_native_function { + "createStreamSync", nullptr, reinterpret_cast(CreateStreamAni::CreateStreamSync) }, + ani_native_function { "createWatcherSync", nullptr, reinterpret_cast(WatcherAni::CreateWatcherSync) }, + ani_native_function { "disConnectDfs", nullptr, reinterpret_cast(DisConnectDfsAni::DisConnectDfsSync) }, ani_native_function { "doAccessSync", nullptr, reinterpret_cast(AccessAni::AccessSync3) }, + ani_native_function { "dup", nullptr, reinterpret_cast(DupAni::Dup) }, + ani_native_function { "fdatasyncSync", nullptr, reinterpret_cast(FDataSyncAni::FDataSyncSync) }, + ani_native_function { + "fdopenStreamSync", nullptr, reinterpret_cast(FdopenStreamAni::FdopenStreamSync) }, + ani_native_function { "fsyncSync", nullptr, reinterpret_cast(FsyncAni::FsyncSync) }, + ani_native_function { "getxattrSync", nullptr, reinterpret_cast(XattrAni::GetXattrSync) }, ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, + ani_native_function { "lseekSync", nullptr, reinterpret_cast(LseekAni::LseekSync) }, + ani_native_function { "lstatSync", nullptr, reinterpret_cast(LstatAni::LstatSync) }, ani_native_function { "mkdirSync", mkdirCtorSig0.c_str(), reinterpret_cast(MkdirkAni::MkdirSync0) }, ani_native_function { "mkdirSync", mkdirCtorSig1.c_str(), reinterpret_cast(MkdirkAni::MkdirSync1) }, + ani_native_function { "mkdtempSync", nullptr, reinterpret_cast(MkdtempAni::MkdtempSync) }, + ani_native_function { "movedirSync", nullptr, reinterpret_cast(MoveDirAni::MoveDirSync) }, ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, ani_native_function { "openSync", nullptr, reinterpret_cast(OpenAni::OpenSync) }, + ani_native_function { "readlinesSync", nullptr, reinterpret_cast(ReadLinesAni::ReadLinesSync) }, ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, ani_native_function { "readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync) }, + ani_native_function { "renameSync", nullptr, reinterpret_cast(RenameAni::RenameSync) }, ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, + ani_native_function { "setxattrSync", nullptr, reinterpret_cast(XattrAni::SetXattrSync) }, ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, + ani_native_function { "symlinkSync", nullptr, reinterpret_cast(SymlinkAni::SymlinkSync) }, ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, + ani_native_function { "utimes", nullptr, reinterpret_cast(UtimesAni::Utimes) }, ani_native_function { "writeSync", nullptr, reinterpret_cast(WriteAni::WriteSync) }, }; return BindClass(env, classDesc, methods); @@ -110,11 +241,41 @@ static ani_status DoBindMethods(ani_env *env) return status; }; + if ((status = BindReaderIteratorMethods(env)) != ANI_OK) { + HILOGE("Cannot bind native methods for ReaderIterator Class"); + return status; + }; + if ((status = BindStatClassMethods(env)) != ANI_OK) { HILOGE("Cannot bind native methods for Stat Class!"); return status; }; + if ((status = BindRafFileMethods(env)) != ANI_OK) { + HILOGE("Cannot bind native methods for RafFile Class"); + return status; + }; + + if ((status = BindStreamMethods(env)) != ANI_OK) { + HILOGE("Cannot bind native methods for Stream Class!"); + return status; + }; + + if ((status = BindTaskSignalClassMethods(env)) != ANI_OK) { + HILOGE("Cannot bind native methods for TaskSignal Class!"); + return status; + }; + + if ((status = BindWatcherClassMethods(env)) != ANI_OK) { + HILOGE("Cannot bind native methods for Watcher Class"); + return status; + }; + + if ((status = BindAtomicFileMethods(env)) != ANI_OK) { + HILOGE("Cannot bind native methods for AtomicFile Class!"); + return status; + }; + return ANI_OK; } diff --git a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets index 5de05d13c..bf94fd628 100644 --- a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets +++ b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets @@ -13,7 +13,6 @@ * limitations under the License. */ import { BusinessError, AsyncCallback } from '@ohos.base'; -// import stream from '@ohos.util.stream'; const UNKNOWN_ERR: number = 13900042 const UNKNOWN_MSG: string = "Unknown error" @@ -47,8 +46,8 @@ function access(path: string, mode?: AccessModeType): Promise { promise.then((ret: NullishType): void => { let result = ret as boolean; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -62,8 +61,8 @@ function access(path: string, callback: AsyncCallback): void { e.code = 0; let result = ret as boolean; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, false); + }).catch((e: Error): void => { + callback(e as BusinessError, false); }); } @@ -75,8 +74,8 @@ function access(path: string, mode: AccessModeType, flag: AccessFlagType): Promi promise.then((ret: NullishType): void => { let result = ret as boolean; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }) } @@ -94,8 +93,8 @@ function close(file: number | File): Promise { let promise = taskpool.execute((file: number | File): undefined => FileIoImpl.closeSync(file), file); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -106,8 +105,8 @@ function close(file: number | File, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -121,8 +120,8 @@ function connectDfs(networkId: string, listeners: DfsListeners): Promise { FileIoImpl.connectDfs(networkId, listeners), networkId, listeners); promise.then((): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -132,8 +131,8 @@ function disconnectDfs(networkId: string): Promise { let promise = taskpool.execute((networkId: string): void => FileIoImpl.disConnectDfs(networkId), networkId); promise.then((): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -150,8 +149,8 @@ function getxattr(path: string, key: string): Promise { promise.then((ret: NullishType): void => { let result = ret as string; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -171,8 +170,8 @@ function copyDir(src: string, dest: string, mode?: number): Promise { FileIoImpl.copyDirSync(src, dest, mode), src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError>): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError>); }); }); } @@ -185,8 +184,8 @@ function copyDir(src: string, dest: string, callback: AsyncCallback(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -198,8 +197,8 @@ function copyDir(src: string, dest: string, mode: number, callback: AsyncCallbac e.code = 0; e.data = new Array(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -237,7 +236,7 @@ function fdatasync(fd: number): Promise { let promise = taskpool.execute((fd: number): undefined => FileIoImpl.fdatasyncSync(fd), fd); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { reject(e as BusinessError); }); }); @@ -249,8 +248,8 @@ function fdatasync(fd: number, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -273,7 +272,7 @@ function mkdir(path: string): Promise { let promise = taskpool.execute((path: string): undefined => mkdirSync1(path), path); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { reject(e as BusinessError); }); }); @@ -285,8 +284,8 @@ function mkdir(path: string, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -309,8 +308,8 @@ function mkdir(path: string, recursion: boolean, callback: AsyncCallback): let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -326,8 +325,8 @@ function moveDir(src: string, dest: string, mode?: number): Promise { }, src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError>): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError>); }); }) } @@ -341,8 +340,8 @@ function moveDir(src: string, dest: string, callback: AsyncCallback(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -355,8 +354,8 @@ function moveDir(src: string, dest: string, mode: number, callback: AsyncCallbac e.code = 0; e.data = new Array(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -393,8 +392,8 @@ function mkdtemp(prefix: string): Promise { promise.then((ret: NullishType): void => { let result = ret as string; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -408,8 +407,8 @@ function mkdtemp(prefix: string, callback: AsyncCallback): void { e.code = 0; let result = ret as string; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -424,8 +423,8 @@ function moveFile(src: string, dest: string, mode?: number): Promise { }, src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -438,8 +437,8 @@ function moveFile(src: string, dest: string, mode: number, callback: AsyncCallba let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -451,8 +450,8 @@ function moveFile(src: string, dest: string, callback: AsyncCallback): voi let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -468,8 +467,8 @@ function open(path: String, mode?: number): Promise { promise.then((ret: NullishType): void => { let file = ret as File; resolve(file); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -483,9 +482,9 @@ function open(path: String, mode: number, callback: AsyncCallback): e.code = 0; let file = ret as File; callback(e, file); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let f: File = new FileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -498,9 +497,9 @@ function open(path: String, callback: AsyncCallback): void { e.code = 0; let file = ret as File; callback(e, file); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let f: File = new FileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -516,8 +515,8 @@ function write(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions) promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -532,8 +531,8 @@ function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -546,8 +545,8 @@ function write(fd: number, buffer: string | ArrayBuffer, callback: AsyncCallback e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -563,8 +562,8 @@ function read(fd: number, buffer: ArrayBuffer, options?: ReadOptions): Promise { let result = ret as number; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -578,8 +577,8 @@ function read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -592,8 +591,8 @@ function read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: A e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -610,8 +609,8 @@ function readLines(filePath: string, options?: Options): Promise promise.then((ret: NullishType): void => { let it = ret as ReaderIterator; resolve(it); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -625,9 +624,9 @@ function readLines(filePath: string, callback: AsyncCallback): v e.code = 0; let it = ret as ReaderIterator; callback(e, it); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let r: ReaderIterator = new ReaderIteratorInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -640,9 +639,9 @@ function readLines(filePath: string, options: Options, callback: AsyncCallback { + }).catch((e: Error): void => { let r: ReaderIterator = new ReaderIteratorInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -656,8 +655,8 @@ function rmdir(path: string): Promise { let promise = taskpool.execute((path: string): void => FileIoImpl.rmdirSync(path), path); promise.then((ret: NullishType) => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -668,8 +667,8 @@ function rmdir(path: string, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -684,8 +683,8 @@ function truncate(file: string | number, len?: number): Promise { }, file, len); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }) } @@ -698,8 +697,8 @@ function truncate(file: string | number, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -711,8 +710,8 @@ function truncate(file: string | number, len: number, callback: AsyncCallback(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -726,8 +725,8 @@ function unlink(path: string): Promise { let promise = taskpool.execute((path: string): undefined => unlinkSync(path), path); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -738,8 +737,8 @@ function unlink(path: string, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -751,9 +750,9 @@ function readText(filePath: string, options?: ReadTextOptions): Promise promise.then((ret: NullishType): void => { let r = ret as string; resolve(r); - }).catch((e: BusinessError): void => { - reject(e); - }); + }).catch((e: Error): void => { + reject(e as BusinessError); + }); }); } @@ -766,8 +765,8 @@ function readText(filePath: string, callback: AsyncCallback): void { e.code = 0; let r = ret as string; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -780,8 +779,8 @@ function readText(filePath: string, options: ReadTextOptions, callback: AsyncCal e.code = 0; let r = ret as string; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -797,8 +796,8 @@ function listFile(path: string, options?: ListFileOptions): Promise { promise.then((ret: NullishType): void => { let r = ret as string[]; resolve(r); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -812,8 +811,8 @@ function listFile(path: string, callback: AsyncCallback): void { e.code = 0; let r = ret as string[]; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, []); + }).catch((e: Error): void => { + callback(e as BusinessError, []); }); } @@ -826,8 +825,8 @@ function listFile(path: string, options: ListFileOptions, callback: AsyncCallbac e.code = 0; let r = ret as string[]; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, []); + }).catch((e: Error): void => { + callback(e as BusinessError, []); }); } @@ -851,8 +850,8 @@ function stat(file: string | number): Promise { promise.then((ret: NullishType): void => { let r = ret as Stat; resolve(r); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -866,8 +865,8 @@ function stat(file: string | number, callback: AsyncCallback): void e.code = 0; let r = ret as Stat; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, new StatInner(0)); + }).catch((e: Error): void => { + callback(e as BusinessError, new StatInner(0)); }); } @@ -882,8 +881,8 @@ function fsync(fd: number): Promise { }, fd); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -896,8 +895,8 @@ function fsync(fd: number, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -913,8 +912,8 @@ function symlink(target: string, srcPath: string): Promise { }, target, srcPath); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -929,8 +928,8 @@ function rename(oldPath: string, newPath: string): Promise { }, oldPath, newPath); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -943,8 +942,8 @@ function rename(oldPath: string, newPath: string, callback: AsyncCallback) let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -964,8 +963,8 @@ function createRandomAccessFile(file: string | File, mode?: number, promise.then((ret: NullishType): void => { let raffile = ret as RandomAccessFileInner; resolve(raffile); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -978,8 +977,8 @@ function fdopenStream(fd: number, mode: string): Promise { promise.then((ret: NullishType): void => { let stream = ret as Stream; resolve(stream); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -994,7 +993,7 @@ function setxattr(path: string, key: string, value: string): Promise { FileIoImpl.setxattrSync(path, key, value), path, key, value); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { reject(e as BusinessError); }); }); @@ -1009,9 +1008,9 @@ function createRandomAccessFile(file: string | File, callback: AsyncCallback { + }).catch((e: Error): void => { let f: RandomAccessFile = new RandomAccessFileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -1025,9 +1024,9 @@ function createRandomAccessFile(file: string | File, mode: number, e.code = 0; let raffile = ret as RandomAccessFile; callback(e, raffile); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let f: RandomAccessFile = new RandomAccessFileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -1040,9 +1039,9 @@ function fdopenStream(fd: number, mode: string, callback: AsyncCallback) e.code = 0; let stream = ret as Stream; callback(e, stream); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let r: Stream = new StreamInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -1058,8 +1057,8 @@ function createStream(path: string, mode: string): Promise { promise.then((ret: NullishType): void => { let stream = ret as Stream; resolve(stream); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1073,9 +1072,9 @@ function createStream(path: string, mode: string, callback: AsyncCallback { + }).catch((e: Error): void => { let r: Stream = new StreamInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -1083,14 +1082,6 @@ function createStreamSync(path: string, mode: string): Stream { return FileIoImpl.createStreamSync(path, mode); } -// function createReadStream(path: string, options?: ReadStreamOptions): ReadStream { -// return new ReadStream(path, options) -// } - -// function createWriteStream(path: string, options?: WriteStreamOptions): WriteStream { -// return new WriteStream(path, options); -// } - function createWatcher(path: string, events: number, listener: WatchEventListener): Watcher { return FileIoImpl.createWatcherSync(path, events, listener); } @@ -1103,8 +1094,8 @@ function symlink(target: string, srcPath: string, callback: AsyncCallback) let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1130,8 +1121,8 @@ function lstat(path: string): Promise { let r = ret as Stat; resolve(r); } - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1151,8 +1142,8 @@ function lstat(path: string, callback: AsyncCallback): void { let r = ret as Stat; callback(e, r); } - }).catch((e: BusinessError): void => { - callback(e, new StatInner(0)); + }).catch((e: Error): void => { + callback(e as BusinessError, new StatInner(0)); }); } function copyFile(src: string | number, dest: string | number, mode?: number): Promise { @@ -1161,8 +1152,8 @@ function copyFile(src: string | number, dest: string | number, mode?: number): P FileIoImpl.copyFileSync(src, dest, mode), src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1173,8 +1164,8 @@ function copy(srcUri: string, destUri: string, options?: CopyOptions): Promise { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1186,8 +1177,8 @@ function copyFile(src: string | number, dest: string | number, mode: number, cal let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1198,8 +1189,8 @@ function copy(srcUri: string, destUri: string, options: CopyOptions, callback: A let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1210,8 +1201,8 @@ function copyFile(src: string | number, dest: string | number, callback: AsyncCa let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1222,8 +1213,8 @@ function copy(srcUri: string, destUri: string, callback: AsyncCallback): v let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1263,20 +1254,31 @@ export type ProgressListener = (progress: Progress) => void; export class TaskSignal { private nativeTaskSignal: long = 0; + private native onCancelNative(): void; + private onCancelResolve: (path: string) => void = (path: string): void => {}; private onCancelCallback(path: string): void { if (this.onCancelResolve) { this.onCancelResolve(path); } } + native cancel(): void; + onCancel(): Promise { - return new Promise((resolve: (path: string) => void, reject: (e: BusinessError) => void): void => { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { this.onCancelResolve = resolve; - this.onCancelNative(); + try { + this.onCancelNative(); + } catch (e: BusinessError) { + reject(e); + } catch (e: Error) { + reject(e as BusinessError); + } }); } + } export interface CopyOptions { @@ -1309,8 +1311,6 @@ export interface RandomAccessFile { read(buffer: ArrayBuffer, callback: AsyncCallback): void; read(buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void; readSync(buffer: ArrayBuffer, options?: ReadOptions): number; - // getReadStream(): ReadStream; - // getWriteStream(): WriteStream; } export class RandomAccessFileInner implements RandomAccessFile { @@ -1331,7 +1331,7 @@ export class RandomAccessFileInner implements RandomAccessFile { } native setFilePointer0(filePointer: number): void; - + native close(): void; writeSync(buffer: ArrayBuffer | string, options?: WriteOptions): number { @@ -1350,8 +1350,8 @@ export class RandomAccessFileInner implements RandomAccessFile { promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1365,8 +1365,8 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1379,8 +1379,8 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1400,8 +1400,8 @@ export class RandomAccessFileInner implements RandomAccessFile { promise.then((ret: NullishType): void => { let result = ret as number; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1415,8 +1415,8 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1429,13 +1429,11 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } - // native getReadStream(): ReadStream; - // native getWriteStream(): WriteStream; } export interface File { @@ -1474,8 +1472,8 @@ export class FileInner implements File { }, exclusive); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1488,8 +1486,8 @@ export class FileInner implements File { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1501,8 +1499,8 @@ export class FileInner implements File { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1590,7 +1588,7 @@ export class StatInner implements Stat { this.nativeStat = stat; } } - + native isBlockDevice(): boolean; native isCharacterDevice(): boolean; native isDirectory(): boolean; @@ -1632,8 +1630,8 @@ export class StreamInner implements Stream { let promise = taskpool.execute((): undefined => this.closeSync()); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1644,8 +1642,8 @@ export class StreamInner implements Stream { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1656,8 +1654,8 @@ export class StreamInner implements Stream { let promise = taskpool.execute((): undefined => this.flushSync()); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1668,8 +1666,8 @@ export class StreamInner implements Stream { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1683,8 +1681,8 @@ export class StreamInner implements Stream { promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1698,8 +1696,8 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1712,8 +1710,8 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1727,8 +1725,8 @@ export class StreamInner implements Stream { promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1742,8 +1740,8 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1756,163 +1754,15 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } - + native readSync(buffer: ArrayBuffer, options?: ReadOptions): number; native seek(offset: number, whence?: number): number; } -// export class ReadStream extends stream.Readable { -// path: string; -// bytesRead: number; -// private offset: number; -// private start?: number; -// private end?: number; -// private stream?: Stream; - -// constructor(path: string, options?: ReadStreamOptions) { -// super(); -// this.path = path; -// this.bytesRead = 0; -// this.start = options?.start; -// this.end = options?.end; -// this.stream = createStreamSync(this.path, 'r'); -// this.offset = this.start ?? 0; -// } - -// seek(offset: number, whence?: WhenceType): number { -// if (whence === undefined) { -// let off = this.stream?.seek(offset); -// if (off !== undefined) { -// this.offset = off -// } -// } else { -// let off = this.stream?.seek(offset, whence); -// if (off !== undefined) { -// this.offset = off -// } -// } -// return this.offset; -// } - -// close(): void { -// this.stream?.close(); -// } - -// doInitialize(callback: () => void): void { -// callback(); -// } - -// doRead(size: number): void { -// let readSize = size; -// let end = this.end -// if (end !== undefined) { -// if (this.offset > end) { -// this.push(null); -// return; -// } -// if (this.offset + readSize > end) { -// readSize = end - this.offset; -// } -// } -// let buffer = new ArrayBuffer(readSize); -// const off = this.offset; -// this.offset += readSize; -// this.stream?.read(buffer, { offset: off, length: readSize }) -// .then((readOut: number) => { -// if (readOut > 0) { -// this.bytesRead += readOut; -// this.push(new Uint8Array(buffer.slice(0, readOut))); -// } -// if (readOut !== readSize || readOut < size) { -// this.offset = this.offset - readSize + readOut; -// this.push(null); -// } -// }); -// } -// } - -// export class WriteStream extends stream.Writable { -// path: string; -// bytesWritten: number; -// private offset: number; -// private mode: string; -// private start?: number; -// private stream?: Stream; - -// constructor(path: string, options?: WriteStreamOptions) { -// super(); -// this.path = path; -// this.bytesWritten = 0; -// this.start = options?.start; -// this.mode = this.convertOpenMode(options?.mode); -// this.stream = createStreamSync(this.path, this.mode); -// this.offset = this.start ?? 0; -// } - -// seek(offset: number, whence?: WhenceType): number { -// if (whence === undefined) { -// let off = this.stream?.seek(offset); -// if (off !== undefined) { -// this.offset = off -// } -// } else { -// let off = this.stream?.seek(offset, whence); -// if (off !== undefined) { -// this.offset = off -// } -// } -// return this.offset; -// } - -// close(): void { -// this.stream?.close(); -// } - -// closeSync(): void { -// this.stream?.closeSync(); -// } - -// doInitialize(callback: () => void): void { -// callback(); -// } - -// doWrite(chunk: string | ArrayBuffer, encoding: string, callback: () => void): void { -// this.stream?.write(chunk, { offset: this.offset }) -// .then((writeIn: number) => { -// this.offset += writeIn; -// this.bytesWritten += writeIn; -// callback(); -// }) -// .finally(() => { -// this.stream?.flush(); -// }); -// } - -// convertOpenMode(mode?: number): string { -// let modeStr = 'w'; -// if (mode === undefined) { -// return modeStr; -// } -// if ((mode as number) & fileIo.OpenMode.WRITE_ONLY) { -// modeStr = 'w'; -// } -// if ((mode as number) & fileIo.OpenMode.READ_WRITE) { -// modeStr = 'w+'; -// } -// if (((mode as number) & fileIo.OpenMode.WRITE_ONLY) && ((mode as number) & fileIo.OpenMode.APPEND)) { -// modeStr = 'a'; -// } -// if (((mode as number) & fileIo.OpenMode.READ_WRITE) && ((mode as number) & fileIo.OpenMode.APPEND)) { -// modeStr = 'a+'; -// } -// return modeStr; -// } -// } - export class AtomicFile { static { loadLibrary("ani_file_fs"); @@ -1920,43 +1770,13 @@ export class AtomicFile { private nativePtr: long = 0; private native getPath(): string; - // private writeStream: WriteStream | null = null; native constructor(path: string); native getBaseFile(): File; - // native openRead(): ReadStream; - native readFully(): ArrayBuffer; - // native nativeStartWrite(): WriteStream; - // startWrite(): WriteStream { - // let ws = this.nativeStartWrite(); - // this.writeStream = ws; - // return ws; - // } - - // native nativeFinishWrite(): void; - // finishWrite(): void { - // if (!this.writeStream) { - // throw createBusinessError(UNKNOWN_ERR, UNKNOWN_MSG); - // } - // this.writeStream?.close(); - // this.nativeFinishWrite(); - // this.writeStream = null; - // }; - - // native nativeFailWrite(): void; - // failWrite(): void { - // if (!this.writeStream) { - // throw createBusinessError(UNKNOWN_ERR, UNKNOWN_MSG); - // } - // this.writeStream?.close(); - // this.nativeFailWrite(); - // this.writeStream = null; - // }; - native delete(): void; } @@ -2119,15 +1939,15 @@ export class FileIoImpl { static native createStreamSync(path: string, mode: string): fileIo.Stream; static native createWatcherSync(path: string, events: number, listener: WatchEventListener): fileIo.Watcher; - + static native fdopenStreamSync(fd: number, mode: string): fileIo.Stream; - + static native dup(fd: number): fileIo.File; static native listFileSync(path: string, options?: ListFileOptions): string[]; static native lstatSync(path: string): fileIo.Stat; - + static native lseekSync(fd: number, offset: number, whence?: fileIo.WhenceType): number; static native mkdirSync(path: string): void; @@ -2143,7 +1963,7 @@ export class FileIoImpl { static native openSync(path: String, mode?: number): fileIo.File; static native readlinesSync(filePath: string, options?: Options): fileIo.ReaderIterator; - + static native readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number; static native readTextSync(filePath: string, options?: ReadTextOptions): string; diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp new file mode 100644 index 000000000..7ce4a8bfc --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp @@ -0,0 +1,44 @@ +/* + * 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 "fdatasync_ani.h" + +#include + +#include "error_handler.h" +#include "fdatasync_core.h" +#include "file_utils.h" +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +void FDataSyncAni::FDataSyncSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd) +{ + auto ret = FDataSyncCore::DoFDataSync(static_cast(fd)); + if (!ret.IsSuccess()) { + HILOGE("Fdatasync failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return; + } +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.h new file mode 100644 index 000000000..ce0b83261 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.h @@ -0,0 +1,36 @@ +/* + * 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 INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_FDATASYNC_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_FDATASYNC_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class FDataSyncAni final { +public: + static void FDataSyncSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd); +}; + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_FDATASYNC_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.cpp new file mode 100644 index 000000000..3ade60f9d --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.cpp @@ -0,0 +1,63 @@ +/* + * 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 "fdopen_stream_ani.h" + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "fdopen_stream_core.h" +#include "stream_wrapper.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; + +ani_object FdopenStreamAni::FdopenStreamSync( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_string mode) +{ + auto [succMode, openMode] = TypeConverter::ToUTF8String(env, mode); + if (!succMode) { + HILOGE("Invalid mode"); + ErrorHandler::Throw(env, EINVAL); + return nullptr; + } + + FsResult ret = FdopenStreamCore::DoFdopenStream(static_cast(fd), openMode); + if (!ret.IsSuccess()) { + HILOGE("Fdopen stream failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + const FsStream *stream = ret.GetData().value(); + auto result = StreamWrapper::Wrap(env, move(stream)); + if (result == nullptr) { + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.h new file mode 100644 index 000000000..81f0fde31 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.h @@ -0,0 +1,33 @@ +/* + * 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 INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDOPEN_STREAM_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDOPEN_STREAM_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +class FdopenStreamAni final { +public: + static ani_object FdopenStreamSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_string mode); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDOPEN_STREAM_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.cpp new file mode 100644 index 000000000..f2680914f --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.cpp @@ -0,0 +1,41 @@ +/* + * 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 "fsync_ani.h" + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "fsync_core.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +using namespace OHOS::FileManagement::ModuleFileIO; + +void FsyncAni::FsyncSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd) +{ + auto ret = FsyncCore::DoFsync(static_cast(fd)); + if (!ret.IsSuccess()) { + HILOGE("DoFsync failed!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return; + } +} +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.h new file mode 100644 index 000000000..ad9d936fe --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.h @@ -0,0 +1,35 @@ +/* + * 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 INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_FSYNC_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_FSYNC_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class FsyncAni final { +public: + static void FsyncSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_FSYNC_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp index 6b65ec5ce..d3bbe159e 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp @@ -65,7 +65,7 @@ tuple ParseIntParam(ani_env *env, ani_object obj, string tag) } ani_int resultRefRes; if (ANI_OK != env->Object_CallMethodByName_Int( - static_cast(resultRef), "intValue", nullptr, &resultRefRes)) { + static_cast(resultRef), BasicTypesConverter::toInt.c_str(), nullptr, &resultRefRes)) { result = -1; return { false, result }; } @@ -87,7 +87,7 @@ tuple> ParseDoubleParam(ani_env *env, ani_object obj, str ani_double resultRefRes; if (ANI_OK != env->Object_CallMethodByName_Double( - static_cast(resultRef), "doubleValue", nullptr, &resultRefRes)) { + static_cast(resultRef), BasicTypesConverter::toDouble, nullptr, &resultRefRes)) { return { false, nullopt }; } double result = static_cast(resultRefRes); diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp new file mode 100644 index 000000000..3f4a4214a --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp @@ -0,0 +1,63 @@ +/* + * 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 "lseek_ani.h" + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "lseek_core.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +using namespace std; + +optional ParseSeekPos(const optional &whence) +{ + if (!whence.has_value()) { + return nullopt; + } + + return make_optional(static_cast(move(whence.value()))); +} + +ani_double LseekAni::LseekSync( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_double offset, ani_enum_item whence) +{ + auto [succWhence, whenceOp] = TypeConverter::EnumToInt32(env, whence); + if (!succWhence) { + HILOGE("Invalid whence"); + ErrorHandler::Throw(env, EINVAL); + return -1; + } + + auto pos = ParseSeekPos(whenceOp); + auto ret = LseekCore::DoLseek(static_cast(fd), static_cast(offset), pos); + if (!ret.IsSuccess()) { + HILOGE("DoLseek failed!"); + const FsError &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return -1; + } + + return ani_double(static_cast(ret.GetData().value())); +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.h new file mode 100644 index 000000000..5a7995d79 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.h @@ -0,0 +1,34 @@ +/* + * 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 INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_LSEEK_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_LSEEK_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +class LseekAni final { +public: + static ani_double LseekSync( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_double offset, ani_enum_item whence); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_LSEEK_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/symlink_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/symlink_ani.h index 3c8c16ca2..8681a8e12 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/symlink_ani.h +++ b/interfaces/kits/js/src/mod_fs/properties/ani/symlink_ani.h @@ -32,4 +32,4 @@ public: } // namespace FileManagement } // namespace OHOS -#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_SYMLINK_ANI_H \ No newline at end of file +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_SYMLINK_ANI_H diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/utimes_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/utimes_ani.h index b07dd421d..a180875b0 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/utimes_ani.h +++ b/interfaces/kits/js/src/mod_fs/properties/ani/utimes_ani.h @@ -33,4 +33,4 @@ public: } // namespace FileManagement } // namespace OHOS -#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_UTIMES_ANI_H \ No newline at end of file +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_UTIMES_ANI_H diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_core.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_core.cpp new file mode 100644 index 000000000..612326ba4 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/copy_core.cpp @@ -0,0 +1,891 @@ +/* + * 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 "copy_core.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "datashare_helper.h" +#include "file_uri.h" +#include "file_utils.h" +#include "filemgmt_libhilog.h" +#include "fs_utils.h" +#include "if_system_ability_manager.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#include "trans_listener_core.h" +#include "utils_log.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace AppFileService::ModuleFileUri; +namespace fs = std::filesystem; +const std::string FILE_PREFIX_NAME = "file://"; +const std::string NETWORK_PARA = "?networkid="; +const string PROCEDURE_COPY_NAME = "FileFSCopy"; +const std::string MEDIALIBRARY_DATA_URI = "datashare:///media"; +const std::string MEDIA = "media"; +const int SLEEP_TIME = 100000; +constexpr int DISMATCH = 0; +constexpr int MATCH = 1; +constexpr int BUF_SIZE = 1024; +constexpr size_t MAX_SIZE = 1024 * 1024 * 4; +constexpr std::chrono::milliseconds NOTIFY_PROGRESS_DELAY(300); +std::recursive_mutex CopyCore::mutex_; +std::map> CopyCore::callbackMap_; + +static int OpenSrcFile(const string &srcPth, std::shared_ptr infos, int32_t &srcFd) +{ + Uri uri(infos->srcUri); + if (uri.GetAuthority() == MEDIA) { + std::shared_ptr dataShareHelper = nullptr; + sptr remote = new (std::nothrow) IRemoteStub(); + if (!remote) { + HILOGE("Failed to get remote object"); + return ENOMEM; + } + dataShareHelper = DataShare::DataShareHelper::Creator(remote->AsObject(), MEDIALIBRARY_DATA_URI); + if (!dataShareHelper) { + HILOGE("Failed to connect to datashare"); + return E_PERMISSION; + } + srcFd = dataShareHelper->OpenFile(uri, FsUtils::GetModeFromFlags(O_RDONLY)); + if (srcFd < 0) { + HILOGE("Open media uri by data share fail. ret = %{public}d", srcFd); + return EPERM; + } + } else { + srcFd = open(srcPth.c_str(), O_RDONLY); + if (srcFd < 0) { + HILOGE("Error opening src file descriptor. errno = %{public}d", errno); + return errno; + } + } + return ERRNO_NOERR; +} + +static int SendFileCore(std::unique_ptr srcFdg, std::unique_ptr destFdg, + std::shared_ptr infos) +{ + std::unique_ptr sendFileReq = { new (nothrow) uv_fs_t, + FsUtils::FsReqCleanup }; + if (sendFileReq == nullptr) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + int64_t offset = 0; + struct stat srcStat {}; + if (fstat(srcFdg->GetFD(), &srcStat) < 0) { + HILOGE("Failed to get stat of file by fd: %{public}d ,errno = %{public}d", srcFdg->GetFD(), errno); + return errno; + } + int32_t ret = 0; + int64_t size = static_cast(srcStat.st_size); + while (size >= 0) { + ret = uv_fs_sendfile(nullptr, sendFileReq.get(), destFdg->GetFD(), srcFdg->GetFD(), offset, MAX_SIZE, nullptr); + if (ret < 0) { + HILOGE("Failed to sendfile by errno : %{public}d", errno); + return errno; + } + if (infos != nullptr && infos->taskSignal != nullptr) { + if (infos->taskSignal->CheckCancelIfNeed(infos->srcPath)) { + return ECANCELED; + } + } + offset += static_cast(ret); + size -= static_cast(ret); + if (ret == 0) { + break; + } + } + if (size != 0) { + HILOGE("The execution of the sendfile task was terminated, remaining file size %{public}" PRIu64, size); + return EIO; + } + return ERRNO_NOERR; +} + +bool CopyCore::IsValidUri(const std::string &uri) +{ + return uri.find(FILE_PREFIX_NAME) == 0; +} + +bool CopyCore::ValidOperand(std::string uriStr) +{ + return IsValidUri(uriStr); +} + +bool CopyCore::IsRemoteUri(const std::string &uri) +{ + // NETWORK_PARA + return uri.find(NETWORK_PARA) != uri.npos; +} + +bool CopyCore::IsDirectory(const std::string &path) +{ + struct stat buf {}; + int ret = stat(path.c_str(), &buf); + if (ret == -1) { + HILOGE("Stat failed, errno is %{public}d", errno); + return false; + } + return (buf.st_mode & S_IFMT) == S_IFDIR; +} + +bool CopyCore::IsFile(const std::string &path) +{ + struct stat buf {}; + int ret = stat(path.c_str(), &buf); + if (ret == -1) { + HILOGI("Stat failed, errno is %{public}d, ", errno); + return false; + } + return (buf.st_mode & S_IFMT) == S_IFREG; +} + +bool CopyCore::IsMediaUri(const std::string &uriPath) +{ + Uri uri(uriPath); + string bundleName = uri.GetAuthority(); + return bundleName == MEDIA; +} + +tuple CopyCore::GetFileSize(const std::string &path) +{ + struct stat buf {}; + int ret = stat(path.c_str(), &buf); + if (ret == -1) { + HILOGI("Stat failed."); + return { errno, 0 }; + } + return { ERRNO_NOERR, buf.st_size }; +} + +int CopyCore::CheckOrCreatePath(const std::string &destPath) +{ + std::error_code errCode; + if (!filesystem::exists(destPath, errCode) && errCode.value() == ERRNO_NOERR) { + HILOGI("DestPath not exist"); + auto file = open(destPath.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); + if (file < 0) { + HILOGE("Error opening file descriptor. errno = %{public}d", errno); + return errno; + } + close(file); + } else if (errCode.value() != 0) { + return errCode.value(); + } + return ERRNO_NOERR; +} + +int CopyCore::CopyFile(const string &src, const string &dest, std::shared_ptr infos) +{ + HILOGD("src = %{public}s, dest = %{public}s", GetAnonyString(src).c_str(), GetAnonyString(dest).c_str()); + + int32_t srcFd = -1; + int32_t ret = OpenSrcFile(src, infos, srcFd); + if (srcFd < 0) { + return ret; + } + + auto destFd = open(dest.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); + if (destFd < 0) { + HILOGE("Error opening dest file descriptor. errno = %{public}d", errno); + close(srcFd); + return errno; + } + + auto srcFdg = CreateUniquePtr(srcFd, true); + auto destFdg = CreateUniquePtr(destFd, true); + if (srcFdg == nullptr || destFdg == nullptr) { + HILOGE("Failed to request heap memory."); + close(srcFd); + close(destFd); + return ENOMEM; + } + return SendFileCore(move(srcFdg), move(destFdg), infos); +} + +int CopyCore::MakeDir(const string &path) +{ + filesystem::path destDir(path); + std::error_code errCode; + if (!filesystem::create_directory(destDir, errCode)) { + HILOGE("Failed to create directory, error code: %{public}d", errCode.value()); + return errCode.value(); + } + return ERRNO_NOERR; +} + +int CopyCore::CopySubDir(const string &srcPath, const string &destPath, std::shared_ptr infos) +{ + std::error_code errCode; + if (!filesystem::exists(destPath, errCode) && errCode.value() == ERRNO_NOERR) { + int res = MakeDir(destPath); + if (res != ERRNO_NOERR) { + HILOGE("Failed to mkdir"); + return res; + } + } else if (errCode.value() != ERRNO_NOERR) { + HILOGE("fs exists fail, errcode is %{public}d", errCode.value()); + return errCode.value(); + } + uint32_t watchEvents = IN_MODIFY; + if (infos->notifyFd >= 0) { + int newWd = inotify_add_watch(infos->notifyFd, destPath.c_str(), watchEvents); + if (newWd < 0) { + HILOGE("inotify_add_watch, newWd is unvaild, newWd = %{public}d", newWd); + return errno; + } + { + std::lock_guard lock(CopyCore::mutex_); + auto iter = CopyCore::callbackMap_.find(*infos); + auto receiveInfo = CreateSharedPtr(); + if (receiveInfo == nullptr) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + receiveInfo->path = destPath; + if (iter == CopyCore::callbackMap_.end() || iter->second == nullptr) { + HILOGE("Failed to find infos, srcPath = %{public}s, destPath = %{public}s", + GetAnonyString(infos->srcPath).c_str(), GetAnonyString(infos->destPath).c_str()); + return UNKNOWN_ERR; + } + iter->second->wds.push_back({ newWd, receiveInfo }); + } + } + return RecurCopyDir(srcPath, destPath, infos); +} + +static int FilterFunc(const struct dirent *filename) +{ + if (string_view(filename->d_name) == "." || string_view(filename->d_name) == "..") { + return DISMATCH; + } + return MATCH; +} + +struct NameList { + struct dirent **namelist = { nullptr }; + int direntNum = 0; +}; + +static void Deleter(struct NameList *arg) +{ + for (int i = 0; i < arg->direntNum; i++) { + free((arg->namelist)[i]); + (arg->namelist)[i] = nullptr; + } + free(arg->namelist); + arg->namelist = nullptr; + delete arg; + arg = nullptr; +} + +std::string CopyCore::GetRealPath(const std::string &path) +{ + fs::path tempPath(path); + fs::path realPath {}; + for (const auto &component : tempPath) { + if (component == ".") { + continue; + } else if (component == "..") { + realPath = realPath.parent_path(); + } else { + realPath /= component; + } + } + return realPath.string(); +} + +uint64_t CopyCore::GetDirSize(std::shared_ptr infos, std::string path) +{ + unique_ptr pNameList = { new (nothrow) struct NameList, Deleter }; + if (pNameList == nullptr) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + int num = scandir(path.c_str(), &(pNameList->namelist), FilterFunc, alphasort); + pNameList->direntNum = num; + + long int size = 0; + for (int i = 0; i < num; i++) { + string dest = path + '/' + string((pNameList->namelist[i])->d_name); + if ((pNameList->namelist[i])->d_type == DT_LNK) { + continue; + } + if ((pNameList->namelist[i])->d_type == DT_DIR) { + size += static_cast(GetDirSize(infos, dest)); + } else { + struct stat st {}; + if (stat(dest.c_str(), &st) == -1) { + return size; + } + size += st.st_size; + } + } + return size; +} + +int CopyCore::RecurCopyDir(const string &srcPath, const string &destPath, std::shared_ptr infos) +{ + unique_ptr pNameList = { new (nothrow) struct NameList, Deleter }; + if (pNameList == nullptr) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + int num = scandir(srcPath.c_str(), &(pNameList->namelist), FilterFunc, alphasort); + pNameList->direntNum = num; + + for (int i = 0; i < num; i++) { + string src = srcPath + '/' + string((pNameList->namelist[i])->d_name); + string dest = destPath + '/' + string((pNameList->namelist[i])->d_name); + if ((pNameList->namelist[i])->d_type == DT_LNK) { + continue; + } + int ret = ERRNO_NOERR; + if ((pNameList->namelist[i])->d_type == DT_DIR) { + ret = CopySubDir(src, dest, infos); + } else { + infos->filePaths.insert(dest); + ret = CopyFile(src, dest, infos); + } + if (ret != ERRNO_NOERR) { + return ret; + } + } + return ERRNO_NOERR; +} + +int CopyCore::CopyDirFunc(const string &src, const string &dest, std::shared_ptr infos) +{ + HILOGD("CopyDirFunc in, src = %{public}s, dest = %{public}s", GetAnonyString(src).c_str(), + GetAnonyString(dest).c_str()); + size_t found = dest.find(src); + if (found != std::string::npos && found == 0) { + return EINVAL; + } + fs::path srcPath = fs::u8path(src); + std::string dirName; + if (srcPath.has_parent_path()) { + dirName = srcPath.parent_path().filename(); + } + string destStr = dest + "/" + dirName; + return CopySubDir(src, destStr, infos); +} + +int CopyCore::ExecLocal(std::shared_ptr infos, std::shared_ptr callback) +{ + if (infos->isFile) { + if (infos->srcPath == infos->destPath) { + HILOGE("The src and dest is same"); + return EINVAL; + } + int ret = CheckOrCreatePath(infos->destPath); + if (ret != ERRNO_NOERR) { + HILOGE("Check or create fail, error code is %{public}d", ret); + return ret; + } + } + if (!infos->hasListener) { + return ExecCopy(infos); + } + auto ret = SubscribeLocalListener(infos, callback); + if (ret != ERRNO_NOERR) { + HILOGE("Failed to subscribe local listener, errno = %{public}d", ret); + return ret; + } + StartNotify(infos, callback); + return ExecCopy(infos); +} + +int CopyCore::SubscribeLocalListener(std::shared_ptr infos, std::shared_ptr callback) +{ + infos->notifyFd = inotify_init(); + if (infos->notifyFd < 0) { + HILOGE("Failed to init inotify, errno:%{public}d", errno); + return errno; + } + infos->eventFd = eventfd(0, EFD_CLOEXEC); + if (infos->eventFd < 0) { + HILOGE("Failed to init eventFd, errno:%{public}d", errno); + return errno; + } + callback->notifyFd = infos->notifyFd; + callback->eventFd = infos->eventFd; + int newWd = inotify_add_watch(infos->notifyFd, infos->destPath.c_str(), IN_MODIFY); + if (newWd < 0) { + auto errCode = errno; + HILOGE("Failed to add watch, errno = %{public}d, notifyFd: %{public}d, destPath: %{public}s", errno, + infos->notifyFd, infos->destPath.c_str()); + CloseNotifyFdLocked(infos, callback); + return errCode; + } + auto receiveInfo = CreateSharedPtr(); + if (receiveInfo == nullptr) { + HILOGE("Failed to request heap memory."); + inotify_rm_watch(infos->notifyFd, newWd); + CloseNotifyFdLocked(infos, callback); + return ENOMEM; + } + receiveInfo->path = infos->destPath; + callback->wds.push_back({ newWd, receiveInfo }); + if (!infos->isFile) { + callback->totalSize = GetDirSize(infos, infos->srcPath); + return ERRNO_NOERR; + } + auto [err, fileSize] = GetFileSize(infos->srcPath); + if (err == ERRNO_NOERR) { + callback->totalSize = fileSize; + } + return err; +} + +std::shared_ptr CopyCore::RegisterListener(const std::shared_ptr &infos) +{ + auto callback = CreateSharedPtr(infos->listener); + if (callback == nullptr) { + HILOGE("Failed to request heap memory."); + return nullptr; + } + std::lock_guard lock(mutex_); + auto iter = callbackMap_.find(*infos); + if (iter != callbackMap_.end()) { + HILOGE("CopyCore::RegisterListener, already registered."); + return nullptr; + } + callbackMap_.insert({ *infos, callback }); + return callback; +} + +void CopyCore::UnregisterListener(std::shared_ptr fileInfos) +{ + if (fileInfos == nullptr) { + HILOGE("fileInfos is nullptr"); + return; + } + std::lock_guard lock(mutex_); + auto iter = callbackMap_.find(*fileInfos); + if (iter == callbackMap_.end()) { + HILOGI("It is not be registered."); + return; + } + callbackMap_.erase(*fileInfos); +} + +void CopyCore::ReceiveComplete(std::shared_ptr entry) +{ + if (entry == nullptr) { + HILOGE("entry pointer is nullptr."); + return; + } + if (entry->callback == nullptr) { + HILOGE("entry callback pointer is nullptr."); + return; + } + auto processedSize = entry->progressSize; + if (processedSize < entry->callback->maxProgressSize) { + return; + } + entry->callback->maxProgressSize = processedSize; + auto listener = entry->callback->listener; + if (listener == nullptr) { + HILOGE("listener pointer is nullptr."); + return; + } + listener->InvokeListener(processedSize, entry->totalSize); +} + +FsUvEntry *CopyCore::GetUVEntry(std::shared_ptr infos) +{ + FsUvEntry *entry = nullptr; + { + std::lock_guard lock(mutex_); + auto iter = callbackMap_.find(*infos); + if (iter == callbackMap_.end()) { + HILOGE("Failed to find callback"); + return nullptr; + } + auto callback = iter->second; + entry = new (std::nothrow) FsUvEntry(iter->second, infos); + if (entry == nullptr) { + HILOGE("entry ptr is nullptr."); + return nullptr; + } + entry->progressSize = callback->progressSize; + entry->totalSize = callback->totalSize; + } + return entry; +} + +void CopyCore::OnFileReceive(std::shared_ptr infos) +{ + std::shared_ptr entry(GetUVEntry(infos)); + if (entry == nullptr) { + HILOGE("failed to get uv entry"); + return; + } + ReceiveComplete(entry); +} + +std::shared_ptr CopyCore::GetReceivedInfo(int wd, std::shared_ptr callback) +{ + auto it = find_if(callback->wds.begin(), callback->wds.end(), [wd](const auto &item) { return item.first == wd; }); + if (it != callback->wds.end()) { + return it->second; + } + return nullptr; +} + +bool CopyCore::CheckFileValid(const std::string &filePath, std::shared_ptr infos) +{ + return infos->filePaths.count(filePath) != 0; +} + +int CopyCore::UpdateProgressSize( + const std::string &filePath, std::shared_ptr receivedInfo, std::shared_ptr callback) +{ + auto [err, fileSize] = GetFileSize(filePath); + if (err != ERRNO_NOERR) { + HILOGE("GetFileSize failed, err: %{public}d.", err); + return err; + } + + auto size = fileSize; + auto iter = receivedInfo->fileList.find(filePath); + if (iter == receivedInfo->fileList.end()) { + receivedInfo->fileList.insert({ filePath, size }); + callback->progressSize += size; + } else { // file + if (size > iter->second) { + callback->progressSize += (size - iter->second); + iter->second = size; + } + } + return ERRNO_NOERR; +} + +std::shared_ptr CopyCore::GetRegisteredListener(std::shared_ptr infos) +{ + std::lock_guard lock(mutex_); + auto iter = callbackMap_.find(*infos); + if (iter == callbackMap_.end()) { + HILOGE("It is not registered."); + return nullptr; + } + return iter->second; +} + +void CopyCore::CloseNotifyFd(std::shared_ptr infos, std::shared_ptr callback) +{ + callback->closed = false; + infos->eventFd = -1; + infos->notifyFd = -1; + { + std::unique_lock lock(callback->cvLock); + callback->CloseFd(); + callback->cv.notify_one(); + } +} + +void CopyCore::CloseNotifyFdLocked(std::shared_ptr infos, std::shared_ptr callback) +{ + { + lock_guard lock(callback->readMutex); + callback->closed = true; + if (callback->reading) { + HILOGE("close while reading"); + return; + } + } + CloseNotifyFd(infos, callback); +} + +tuple CopyCore::HandleProgress( + inotify_event *event, std::shared_ptr infos, std::shared_ptr callback) +{ + if (callback == nullptr) { + return { true, EINVAL, false }; + } + auto receivedInfo = GetReceivedInfo(event->wd, callback); + if (receivedInfo == nullptr) { + return { true, EINVAL, false }; + } + std::string fileName = receivedInfo->path; + if (!infos->isFile) { // files under subdir + fileName += "/" + string(event->name); + if (!CheckFileValid(fileName, infos)) { + return { true, EINVAL, false }; + } + auto err = UpdateProgressSize(fileName, receivedInfo, callback); + if (err != ERRNO_NOERR) { + return { false, err, false }; + } + } else { + auto [err, fileSize] = GetFileSize(fileName); + if (err != ERRNO_NOERR) { + return { false, err, false }; + } + callback->progressSize = fileSize; + } + return { true, callback->errorCode, true }; +} + +void CopyCore::ReadNotifyEvent(std::shared_ptr infos) +{ + char buf[BUF_SIZE] = { 0 }; + struct inotify_event *event = nullptr; + int len = 0; + int64_t index = 0; + auto callback = GetRegisteredListener(infos); + while (infos->run && ((len = read(infos->notifyFd, &buf, sizeof(buf))) < 0) && (errno == EINTR)) { + } + while (infos->run && index < len) { + event = reinterpret_cast(buf + index); + auto [needContinue, errCode, needSend] = HandleProgress(event, infos, callback); + if (!needContinue) { + infos->exceptionCode = errCode; + return; + } + if (needContinue && !needSend) { + index += static_cast(sizeof(struct inotify_event) + event->len); + continue; + } + if (!callback || (callback->progressSize == callback->totalSize)) { + infos->run = false; + return; + } + auto currentTime = std::chrono::steady_clock::now(); + if (currentTime >= infos->notifyTime) { + OnFileReceive(infos); + infos->notifyTime = currentTime + NOTIFY_PROGRESS_DELAY; + } + index += static_cast(sizeof(struct inotify_event) + event->len); + } +} + +void CopyCore::ReadNotifyEventLocked(std::shared_ptr infos, std::shared_ptr callback) +{ + { + std::lock_guard lock(callback->readMutex); + if (callback->closed) { + HILOGE("read after close"); + return; + } + callback->reading = true; + } + ReadNotifyEvent(infos); + { + std::lock_guard lock(callback->readMutex); + callback->reading = false; + if (callback->closed) { + HILOGE("close after read"); + CloseNotifyFd(infos, callback); + return; + } + } +} + +void CopyCore::GetNotifyEvent(std::shared_ptr infos) +{ + auto callback = GetRegisteredListener(infos); + if (callback == nullptr) { + infos->exceptionCode = EINVAL; + return; + } + prctl(PR_SET_NAME, "NotifyThread"); + nfds_t nfds = 2; + struct pollfd fds[2]; + fds[0].events = 0; + fds[1].events = POLLIN; + fds[0].fd = infos->eventFd; + fds[1].fd = infos->notifyFd; + while (infos->run && infos->exceptionCode == ERRNO_NOERR && infos->eventFd != -1 && infos->notifyFd != -1) { + auto ret = poll(fds, nfds, -1); + if (ret > 0) { + if (static_cast(fds[0].revents) & POLLNVAL) { + infos->run = false; + return; + } + if (static_cast(fds[1].revents) & POLLIN) { + ReadNotifyEventLocked(infos, callback); + } + } else if (ret < 0 && errno == EINTR) { + continue; + } else { + infos->exceptionCode = errno; + return; + } + { + std::unique_lock lock(callback->cvLock); + callback->cv.wait_for( + lock, std::chrono::microseconds(SLEEP_TIME), [callback]() -> bool { return callback->notifyFd == -1; }); + } + } +} + +tuple> CopyCore::CreateFileInfos( + const std::string &srcUri, const std::string &destUri, const std::optional &options) +{ + auto infos = CreateSharedPtr(); + if (infos == nullptr) { + HILOGE("Failed to request heap memory."); + return { ENOMEM, nullptr }; + } + infos->srcUri = srcUri; + infos->destUri = destUri; + FileUri srcFileUri(infos->srcUri); + infos->srcPath = srcFileUri.GetRealPath(); + FileUri dstFileUri(infos->destUri); + infos->destPath = dstFileUri.GetPath(); + infos->srcPath = GetRealPath(infos->srcPath); + infos->destPath = GetRealPath(infos->destPath); + infos->isFile = IsMediaUri(infos->srcUri) || IsFile(infos->srcPath); + infos->notifyTime = std::chrono::steady_clock::now() + NOTIFY_PROGRESS_DELAY; + if (options.has_value()) { + auto listener = options.value().progressListener; + if (listener) { + infos->hasListener = true; + infos->listener = listener; + } + auto copySignal = options.value().copySignal; + if (copySignal) { + infos->taskSignal = copySignal->GetTaskSignal(); + } + } + + return { ERRNO_NOERR, infos }; +} + +void CopyCore::StartNotify(std::shared_ptr infos, std::shared_ptr callback) +{ + if (infos->hasListener && callback != nullptr) { + callback->notifyHandler = std::thread([infos] { GetNotifyEvent(infos); }); + } +} + +int CopyCore::ExecCopy(std::shared_ptr infos) +{ + if (infos->isFile && IsFile(infos->destPath)) { + // copyFile + return CopyFile(infos->srcPath.c_str(), infos->destPath.c_str(), infos); + } + if (!infos->isFile && IsDirectory(infos->destPath)) { + if (infos->srcPath.back() != '/') { + infos->srcPath += '/'; + } + if (infos->destPath.back() != '/') { + infos->destPath += '/'; + } + // copyDir + return CopyDirFunc(infos->srcPath.c_str(), infos->destPath.c_str(), infos); + } + return EINVAL; +} + +bool CopyCore::ValidParams(const string &src, const string &dest) +{ + auto succSrc = ValidOperand(src); + auto succDest = ValidOperand(dest); + if (!succSrc || !succDest) { + HILOGE("The first/second argument requires uri/uri"); + return false; + } + return true; +} + +void CopyCore::WaitNotifyFinished(std::shared_ptr callback) +{ + if (callback != nullptr) { + if (callback->notifyHandler.joinable()) { + callback->notifyHandler.join(); + } + } +} + +void CopyCore::CopyComplete(std::shared_ptr infos, std::shared_ptr callback) +{ + if (callback != nullptr && infos->hasListener) { + callback->progressSize = callback->totalSize; + OnFileReceive(infos); + } +} + +FsResult CopyCore::DoCopy(const string &src, const string &dest, std::optional &options) +{ + auto isValid = ValidParams(src, dest); + if (!isValid) { + return FsResult::Error(E_PARAMS); + } + + auto [errCode, infos] = CreateFileInfos(src, dest, options); + if (errCode != ERRNO_NOERR) { + return FsResult::Error(errCode); + } + + auto callback = RegisterListener(infos); + if (callback == nullptr) { + return FsResult::Error(EINVAL); + } + + if (IsRemoteUri(infos->srcUri)) { + if (infos->taskSignal != nullptr) { + infos->taskSignal->MarkRemoteTask(); + } + auto ret = TransListenerCore::CopyFileFromSoftBus(infos->srcUri, infos->destUri, infos, std::move(callback)); + UnregisterListener(infos); + if (ret != ERRNO_NOERR) { + return FsResult::Error(ret); + } else { + return FsResult::Success(); + } + } + auto result = CopyCore::ExecLocal(infos, callback); + CloseNotifyFdLocked(infos, callback); + infos->run = false; + WaitNotifyFinished(callback); + if (result != ERRNO_NOERR) { + infos->exceptionCode = result; + UnregisterListener(infos); + return FsResult::Error(infos->exceptionCode); + } + CopyComplete(infos, callback); + UnregisterListener(infos); + if (infos->exceptionCode != ERRNO_NOERR) { + return FsResult::Error(infos->exceptionCode); + } else { + return FsResult::Success(); + } +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_core.h b/interfaces/kits/js/src/mod_fs/properties/copy_core.h new file mode 100644 index 000000000..f6ba476fb --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/copy_core.h @@ -0,0 +1,190 @@ +/* + * 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 INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_COPY_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_COPY_CORE_H + +#include +#include +#include +#include +#include + +#include "bundle_mgr_client_impl.h" +#include "filemgmt_libfs.h" +#include "filemgmt_libhilog.h" +#include "fs_task_signal.h" +#include "i_progress_listener.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::AppExecFwk; +using namespace DistributedFS::ModuleTaskSignal; + +struct CopyOptions { + shared_ptr progressListener; + shared_ptr copySignal; +}; + +struct ReceiveInfo { + std::string path; // dir name + std::map fileList; // filename, proceededSize +}; + +struct FsCallbackObject { + std::shared_ptr listener = nullptr; + int32_t notifyFd = -1; + int32_t eventFd = -1; + std::vector>> wds; + uint64_t totalSize = 0; + uint64_t progressSize = 0; + uint64_t maxProgressSize = 0; + int32_t errorCode = 0; + std::thread notifyHandler; + std::mutex readMutex; + std::condition_variable cv; + std::mutex cvLock; + bool reading = false; + bool closed = false; + explicit FsCallbackObject(std::shared_ptr listener) : listener(listener) {} + + void CloseFd() + { + if (eventFd != -1) { + close(eventFd); + eventFd = -1; + } + if (notifyFd == -1) { + return; + } + for (auto item : wds) { + inotify_rm_watch(notifyFd, item.first); + } + close(notifyFd); + notifyFd = -1; + } + + ~FsCallbackObject() + { + CloseFd(); + } +}; + +struct FsFileInfos { + std::string srcUri; + std::string destUri; + std::string srcPath; + std::string destPath; + bool isFile = false; + std::chrono::steady_clock::time_point notifyTime; + int32_t notifyFd = -1; + int32_t eventFd = -1; + bool run = true; + bool hasListener = false; + std::shared_ptr listener = nullptr; + std::shared_ptr taskSignal = nullptr; + std::set filePaths; + int exceptionCode = ERRNO_NOERR; // notify copy thread or listener thread has exceptions. + bool operator==(const FsFileInfos &infos) const + { + return (srcUri == infos.srcUri && destUri == infos.destUri); + } + bool operator<(const FsFileInfos &infos) const + { + if (srcUri == infos.srcUri) { + return destUri < infos.destUri; + } + return srcUri < infos.srcUri; + } +}; + +struct FsUvEntry { + std::shared_ptr callback; + std::shared_ptr fileInfos; + uint64_t progressSize = 0; + uint64_t totalSize = 0; + FsUvEntry(const std::shared_ptr &cb, std::shared_ptr fileInfos) + : callback(cb), fileInfos(fileInfos){} + + explicit FsUvEntry(const std::shared_ptr &cb) : callback(cb) {} +}; + +class CopyCore final { +public: + static FsResult DoCopy(const string &src, const string &dest, std::optional &options); + +private: + // operator of params + static bool ValidOperand(std::string uriStr); + static int CheckOrCreatePath(const std::string &destPath); + static bool ValidParams(const string &src, const string &dest); + + // operator of local listener + static std::shared_ptr RegisterListener(const std::shared_ptr &infos); + static std::shared_ptr GetRegisteredListener(std::shared_ptr infos); + static void UnregisterListener(std::shared_ptr fileInfos); + static int ExecLocal(std::shared_ptr infos, std::shared_ptr callback); + static void CopyComplete(std::shared_ptr infos, std::shared_ptr callback); + static void WaitNotifyFinished(std::shared_ptr callback); + static void ReadNotifyEvent(std::shared_ptr infos); + static void ReadNotifyEventLocked(std::shared_ptr infos, std::shared_ptr callback); + static int SubscribeLocalListener(std::shared_ptr infos, std::shared_ptr callback); + static void OnFileReceive(std::shared_ptr infos); + static void GetNotifyEvent(std::shared_ptr infos); + static void StartNotify(std::shared_ptr infos, std::shared_ptr callback); + static FsUvEntry *GetUVEntry(std::shared_ptr infos); + static void ReceiveComplete(std::shared_ptr entry); + static void CloseNotifyFd(std::shared_ptr infos, std::shared_ptr callback); + static void CloseNotifyFdLocked(std::shared_ptr infos, std::shared_ptr callback); + + // operator of file + static int RecurCopyDir(const string &srcPath, const string &destPath, std::shared_ptr infos); + static tuple GetFileSize(const std::string &path); + static uint64_t GetDirSize(std::shared_ptr infos, std::string path); + static int CopyFile(const string &src, const string &dest, std::shared_ptr infos); + static int MakeDir(const string &path); + static int CopySubDir(const string &srcPath, const string &destPath, std::shared_ptr infos); + static int CopyDirFunc(const string &src, const string &dest, std::shared_ptr infos); + static tuple> CreateFileInfos( + const std::string &srcUri, const std::string &destUri, const std::optional &options); + static int ExecCopy(std::shared_ptr infos); + + // operator of file size + static int UpdateProgressSize(const std::string &filePath, std::shared_ptr receivedInfo, + std::shared_ptr callback); + static tuple HandleProgress( + inotify_event *event, std::shared_ptr infos, std::shared_ptr callback); + static std::shared_ptr GetReceivedInfo(int wd, std::shared_ptr callback); + static bool CheckFileValid(const std::string &filePath, std::shared_ptr infos); + + // operator of uri or path + static bool IsValidUri(const std::string &uri); + static bool IsRemoteUri(const std::string &uri); + static bool IsDirectory(const std::string &path); + static bool IsFile(const std::string &path); + static bool IsMediaUri(const std::string &uriPath); + static std::string ConvertUriToPath(const std::string &uri); + static std::string GetRealPath(const std::string &path); + + static std::recursive_mutex mutex_; + static std::map> callbackMap_; +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_COPY_CORE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.h b/interfaces/kits/js/src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.h index 3b5f782e6..8d5891fe0 100644 --- a/interfaces/kits/js/src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.h +++ b/interfaces/kits/js/src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.h @@ -43,4 +43,4 @@ private: } // namespace FileManagement } // namespace OHOS -#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_DFS_LISTENER_FILE_DFS_LISTENER_STUB_H \ No newline at end of file +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_DFS_LISTENER_FILE_DFS_LISTENER_STUB_H diff --git a/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp new file mode 100644 index 000000000..645cf65ae --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp @@ -0,0 +1,44 @@ +/* + * 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 "fdatasync_core.h" + +#include +#include +#include +#include + +#include "filemgmt_libhilog.h" + +namespace OHOS::FileManagement::ModuleFileIO { + +FsResult FDataSyncCore::DoFDataSync(const int32_t &fd) +{ + std::unique_ptr fDataSyncReq = { new uv_fs_t, FsUtils::FsReqCleanup }; + if (!fDataSyncReq) { + HILOGE("Failed to request heap memory."); + return FsResult::Error(ENOMEM); + } + + int ret = uv_fs_fdatasync(nullptr, fDataSyncReq.get(), fd, nullptr); + if (ret < 0) { + HILOGE("Failed to transfer data associated with file descriptor: %{public}d, ret:%{public}d", fd, ret); + return FsResult::Error(ret); + } + + return FsResult::Success(); +} + +} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h new file mode 100644 index 000000000..5e26c9140 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h @@ -0,0 +1,29 @@ +/* + * 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 INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDATASYNC_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDATASYNC_CORE_H + +#include "filemgmt_libfs.h" +#include "fs_utils.h" + +namespace OHOS::FileManagement::ModuleFileIO { +class FDataSyncCore final { +public: + static FsResult DoFDataSync(const int32_t &fd); +}; + +} // namespace OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDATASYNC_CORE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.cpp b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.cpp new file mode 100644 index 000000000..4337cd766 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.cpp @@ -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. + */ + +#include "fdopen_stream_core.h" + +#include + +#include "file_utils.h" +#include "filemgmt_libhilog.h" +#include "fs_utils.h" +#include "stream_instantiator.h" +#include "stream_entity.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +FsResult FdopenStreamCore::DoFdopenStream(const int &fd, const string &mode) +{ + if (fd < 0) { + HILOGE("Invalid fd"); + return FsResult::Error(EINVAL); + } + + FILE *file = fdopen(fd, mode.c_str()); + if (!file) { + HILOGE("Failed to fopen file by fd"); + return FsResult::Error(errno); + } + + return StreamInstantiator::InstantiateStream(move(file)); +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.h b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.h new file mode 100644 index 000000000..a319480fd --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.h @@ -0,0 +1,39 @@ +/* + * 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 INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDOPEN_STREAM_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDOPEN_STREAM_CORE_H + +#include "filemgmt_libfs.h" +#include "fs_stream.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { + +class FdopenStreamCore final { +public: + static FsResult DoFdopenStream(const int &fd, const string &mode); +}; + +struct AsyncFdopenStreamArg { + std::shared_ptr fp { nullptr }; +}; + +const std::string PROCEDURE_FDOPENSTREAM_NAME = "FileIOFdopenStream"; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDOPEN_STREAM_CORE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fsync_core.cpp b/interfaces/kits/js/src/mod_fs/properties/fsync_core.cpp new file mode 100644 index 000000000..d902ff1f3 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fsync_core.cpp @@ -0,0 +1,42 @@ +/* + * 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 "fsync_core.h" + +#include +#include +#include + +#include "filemgmt_libhilog.h" +#include "fs_utils.h" + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; + +FsResult FsyncCore::DoFsync(const int32_t &fd) +{ + std::unique_ptr fsyncReq = { new uv_fs_t, FsUtils::FsReqCleanup }; + if (!fsyncReq) { + HILOGE("Failed to request heap memory."); + return FsResult::Error(ENOMEM); + } + int ret = uv_fs_fsync(nullptr, fsyncReq.get(), fd, nullptr); + if (ret < 0) { + HILOGE("Failed to transfer data associated with file descriptor: %{public}d", fd); + return FsResult::Error(ret); + } + return FsResult::Success(); +} +} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fsync_core.h b/interfaces/kits/js/src/mod_fs/properties/fsync_core.h new file mode 100644 index 000000000..ce2b66835 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fsync_core.h @@ -0,0 +1,29 @@ +/* + * 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 INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FSYNC_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FSYNC_CORE_H + +#include "filemgmt_libfs.h" + +namespace OHOS::FileManagement::ModuleFileIO { + +class FsyncCore final { +public: + static FsResult DoFsync(const int32_t &fd); +}; +const std::string PROCEDURE_FSYNC_NAME = "FileIOFsync"; +} // namespace OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FSYNC_CORE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/lseek_core.cpp b/interfaces/kits/js/src/mod_fs/properties/lseek_core.cpp new file mode 100644 index 000000000..dfec873c9 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/lseek_core.cpp @@ -0,0 +1,53 @@ +/* + * 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 "lseek_core.h" + +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +FsResult LseekCore::DoLseek(const int32_t &fd, const int64_t &offset, + const optional &pos) +{ + if (fd < 0) { + HILOGE("Invalid fd from JS first argument"); + return FsResult::Error(EINVAL); + } + + SeekPos whence = SeekPos::START; + if (pos.has_value()) { + if (pos.value() < SeekPos::START || pos.value() > SeekPos::END) { + HILOGE("Invalid whence from JS third argument"); + return FsResult::Error(EINVAL); + } + whence = pos.value(); + } + + int64_t ret = ::Lseek(fd, offset, whence); + if (ret < 0) { + HILOGE("Failed to lseek, error:%{public}d", errno); + return FsResult::Error(errno); + } + + return FsResult::Success(ret); +} + +} // ModuleFileIO +} // FileManagement +} // OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/lseek_core.h b/interfaces/kits/js/src/mod_fs/properties/lseek_core.h new file mode 100644 index 000000000..dd399f652 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/lseek_core.h @@ -0,0 +1,33 @@ +/* + * 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 INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_LSEEK_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_LSEEK_CORE_H + +#include "filemgmt_libfs.h" +#include "rust_file.h" + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; + +class LseekCore final { +public: + static FsResult DoLseek(const int32_t &fd, const int64_t &offset, + const optional &pos = nullopt); +}; + +} // namespace OHOS::FileManagement::ModuleFileIO + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_LSEEK_CORE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp b/interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp new file mode 100644 index 000000000..1f9869151 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp @@ -0,0 +1,106 @@ +/* + * 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 "read_lines_core.h" + +#include + +#include "file_utils.h" +#include "filemgmt_libhilog.h" +#include "rust_file.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +static int CheckOptionArg(Options option) +{ + auto encoding = option.encoding; + if (encoding != "utf-8") { + return EINVAL; + } + + return ERRNO_NOERR; +} + +static int GetFileSize(const string &path, int64_t &offset) +{ + std::unique_ptr readLinesReq = { new uv_fs_t, FsUtils::FsReqCleanup }; + if (!readLinesReq) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + + int ret = uv_fs_stat(nullptr, readLinesReq.get(), path.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to get file stat by path"); + return ret; + } + + offset = static_cast(readLinesReq->statbuf.st_size); + return ERRNO_NOERR; +} + +static FsResult InstantiateReaderIterator(void *iterator, int64_t offset) +{ + if (iterator == nullptr) { + HILOGE("Invalid argument iterator"); + return FsResult::Error(EINVAL); + } + + auto readeriterator = FsReaderIterator::Constructor(); + if (!readeriterator.IsSuccess()) { + HILOGE("Failed to instantiate class ReaderIterator"); + return FsResult::Error(UNKNOWN_ERR); + } + + auto readerIteratorEntity = readeriterator.GetData().value()->GetReaderIteratorEntity(); + if (!readerIteratorEntity) { + HILOGE("Failed to get readerIteratorEntity"); + return FsResult::Error(UNKNOWN_ERR); + } + readerIteratorEntity->iterator = iterator; + readerIteratorEntity->offset = offset; + return FsResult::Success(readeriterator.GetData().value()); +} + +FsResult ReadLinesCore::DoReadLines(const string &path, optional option) +{ + if (option.has_value()) { + int ret = CheckOptionArg(option.value()); + if (ret) { + HILOGE("Invalid option.encoding parameter"); + return FsResult::Error(ret); + } + } + + auto iterator = ::ReaderIterator(path.c_str()); + if (iterator == nullptr) { + HILOGE("Failed to read lines of the file, error: %{public}d", errno); + return FsResult::Error(errno); + } + + int64_t offset = 0; + int ret = GetFileSize(path, offset); + if (ret != 0) { + HILOGE("Failed to get size of the file"); + return FsResult::Error(ret); + } + return InstantiateReaderIterator(iterator, offset); +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/symlink_core.h b/interfaces/kits/js/src/mod_fs/properties/symlink_core.h index 4eaef88d9..f426a1a16 100644 --- a/interfaces/kits/js/src/mod_fs/properties/symlink_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/symlink_core.h @@ -25,7 +25,6 @@ class SymlinkCore final { public: static FsResult DoSymlink(const string &target, const string &srcPath); }; - } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp index 5ab549948..d8fd4c556 100644 --- a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp @@ -17,6 +17,7 @@ #include "ani_signature.h" #include "bind_function.h" #include "hash_ani.h" +#include "hashstream_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; @@ -57,4 +58,4 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) *result = ANI_VERSION_1; return ANI_OK; -} \ No newline at end of file +} diff --git a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets index 7b825ee14..56e07f441 100644 --- a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets +++ b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets @@ -14,7 +14,6 @@ */ import { BusinessError, AsyncCallback } from '@ohos.base'; -// import stream from '@ohos.util.stream'; export default namespace hash { export function hash(path: string, algorithm: string): Promise { @@ -23,8 +22,8 @@ export default namespace hash { promise.then((ret: NullishType): void => { let res = ret as string; resolve(res); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -36,51 +35,10 @@ export default namespace hash { e.code = 0; let res = ret as string; callback(e, res); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } - - // export function createHash(algorithm: string): HashStream { - // return new HashStream(algorithm); - // } - - // export class HashStream extends stream.Transform { - // hs: hash.HashStream; - // hashBuf?: ArrayBuffer; - - // constructor(algorithm: string) { - // super(); - // this.hs = new hash.HashStream(algorithm); - // } - - // digest(): string { - // return this.hs.digest(); - // } - - // update(data: ArrayBuffer): void { - // this.hs.update(data); - // } - - // doTransform(chunk: string, encoding: string, callback: () => void): void { - // let charCodes: number[] = []; - // for (let i = 0; i < chunk.length; i++) { - // charCodes = [...charCodes, chunk.charCodeAt(i)]; - // } - // const buf = new Uint8Array(charCodes).buffer; - // this.hs.update((buf as ArrayBuffer)); - // this.push(chunk); - // callback(); - // } - - // doWrite(chunk: string | Uint8Array, encoding: string, callback: () => void): void { - // callback(); - // } - - // doFlush(callback: () => void): void { - // callback(); - // } - // } } class HashImpl { diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp new file mode 100644 index 000000000..cac067b37 --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp @@ -0,0 +1,123 @@ +/* + * 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 "hashstream_ani.h" + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "hs_hashstream.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +namespace fs = std::filesystem; +using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; + +HsHashStream *Unwrap(ani_env *env, ani_object object) +{ + ani_long nativePtr; + auto ret = env->Object_GetFieldByName_Long(object, "nativePtr", &nativePtr); + if (ret != ANI_OK) { + HILOGE("Unwrap hashstream err: %{public}d", ret); + return nullptr; + } + uintptr_t ptrValue = static_cast(nativePtr); + HsHashStream *hashStream = reinterpret_cast(ptrValue); + return hashStream; +} + +void HashStreamAni::Update(ani_env *env, [[maybe_unused]] ani_object object, ani_arraybuffer buffer) +{ + auto hashStream = Unwrap(env, object); + if (hashStream == nullptr) { + HILOGE("Cannot unwrap hashStream!"); + ErrorHandler::Throw(env, EINVAL); + return; + } + + auto [succ, arrayBuffer] = TypeConverter::ToArrayBuffer(env, buffer); + if (!succ) { + HILOGE("illegal array buffer"); + ErrorHandler::Throw(env, EINVAL); + return; + } + + auto ret = hashStream->Update(arrayBuffer); + if (!ret.IsSuccess()) { + HILOGE("Cannot Update!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return; + } +} + +ani_string HashStreamAni::Digest(ani_env *env, [[maybe_unused]] ani_object object) +{ + auto hashStream = Unwrap(env, object); + if (hashStream == nullptr) { + HILOGE("Cannot unwrap hashStream!"); + ErrorHandler::Throw(env, EINVAL); + return nullptr; + } + + auto ret = hashStream->Digest(); + if (!ret.IsSuccess()) { + HILOGE("Cannot Digest!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + const auto &res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Convert result to ani string failed"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; +} + +void HashStreamAni::Constructor(ani_env *env, ani_object obj, ani_string alg) +{ + auto [succ, algorithm] = TypeConverter::ToUTF8String(env, alg); + if (!succ) { + HILOGE("Invalid alg"); + ErrorHandler::Throw(env, EINVAL); + return; + } + + auto ret = HsHashStream::Constructor(algorithm); + if (!ret.IsSuccess()) { + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return; + } + + if (ANI_OK != + env->Object_SetFieldByName_Long(obj, "nativePtr", reinterpret_cast(ret.GetData().value()))) { + HILOGE("Failed to wrap entity for obj HashStream"); + ErrorHandler::Throw(env, EIO); + return; + } +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.h b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.h new file mode 100644 index 000000000..c5f98974e --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.h @@ -0,0 +1,37 @@ +/* + * 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 INTERFACES_KITS_JS_SRC_MOD_HASH_HASHSTREAM_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_HASH_HASHSTREAM_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class HashStreamAni final { +public: + static void Constructor(ani_env *env, ani_object obj, ani_string alg); + static ani_string Digest(ani_env *env, [[maybe_unused]] ani_object object); + static void Update(ani_env *env, [[maybe_unused]] ani_object object, ani_arraybuffer buffer); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_HASH_HASHSTREAM_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.cpp b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.cpp new file mode 100644 index 000000000..b03eb2b15 --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.cpp @@ -0,0 +1,161 @@ +/* + * 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 "hs_hashstream.h" + +#include +#include + +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +static HASH_ALGORITHM_TYPE GetHashAlgorithm(const string &alg) +{ + return (algorithmMaps.find(alg) != algorithmMaps.end()) ? algorithmMaps.at(alg) : HASH_ALGORITHM_TYPE_UNSUPPORTED; +} + +static string HashFinal(const unique_ptr &hashBuf, size_t hashLen) +{ + stringstream ss; + for (size_t i = 0; i < hashLen; ++i) { + const int hexPerByte = 2; + ss << std::uppercase << std::setfill('0') << std::setw(hexPerByte) << std::hex + << static_cast(hashBuf[i]); + } + + return ss.str(); +} + +tuple HsHashStream::GetHsEntity() +{ + if (!entity) { + return { false, nullptr }; + } + return { true, entity.get() }; +} + +FsResult HsHashStream::Update(ArrayBuffer &buffer) +{ + auto [succ, hsEntity] = GetHsEntity(); + if (!succ) { + HILOGE("Failed to get entity of HashStream"); + return FsResult::Error(EIO); + } + + switch (hsEntity->algType) { + case HASH_ALGORITHM_TYPE_MD5: + MD5_Update(&hsEntity->md5Ctx, buffer.buf, buffer.length); + break; + case HASH_ALGORITHM_TYPE_SHA1: + SHA1_Update(&hsEntity->shaCtx, buffer.buf, buffer.length); + break; + case HASH_ALGORITHM_TYPE_SHA256: + SHA256_Update(&hsEntity->sha256Ctx, buffer.buf, buffer.length); + break; + default: + break; + } + + return FsResult::Success(); +} + +FsResult HsHashStream::Digest() +{ + auto [succ, hsEntity] = GetHsEntity(); + if (!succ) { + HILOGE("Failed to get entity of HashStream"); + return FsResult::Error(EIO); + } + + string digestStr; + switch (hsEntity->algType) { + case HASH_ALGORITHM_TYPE_MD5: { + auto res = make_unique(MD5_DIGEST_LENGTH); + MD5_Final(res.get(), &hsEntity->md5Ctx); + digestStr = HashFinal(res, MD5_DIGEST_LENGTH); + break; + } + case HASH_ALGORITHM_TYPE_SHA1: { + auto res = make_unique(SHA_DIGEST_LENGTH); + SHA1_Final(res.get(), &hsEntity->shaCtx); + digestStr = HashFinal(res, SHA_DIGEST_LENGTH); + break; + } + case HASH_ALGORITHM_TYPE_SHA256: { + auto res = make_unique(SHA256_DIGEST_LENGTH); + SHA256_Final(res.get(), &hsEntity->sha256Ctx); + digestStr = HashFinal(res, SHA256_DIGEST_LENGTH); + break; + } + default: + break; + } + return FsResult::Success(digestStr); +} + +FsResult HsHashStream::Constructor(string alg) +{ + HASH_ALGORITHM_TYPE algType = GetHashAlgorithm(alg); + if (algType == HASH_ALGORITHM_TYPE_UNSUPPORTED) { + HILOGE("algType is not found."); + return FsResult::Error(EINVAL); + } + + HsHashStreamEntity *rawPtr = new (std::nothrow) HsHashStreamEntity(); + if (rawPtr == nullptr) { + HILOGE("Failed to request heap memory."); + return FsResult::Error(ENOMEM); + } + std::unique_ptr hsEntity(rawPtr); + hsEntity->algType = algType; + + switch (algType) { + case HASH_ALGORITHM_TYPE_MD5: { + MD5_CTX ctx; + MD5_Init(&ctx); + hsEntity->md5Ctx = ctx; + break; + } + case HASH_ALGORITHM_TYPE_SHA1: { + SHA_CTX ctx; + SHA1_Init(&ctx); + hsEntity->shaCtx = ctx; + break; + } + case HASH_ALGORITHM_TYPE_SHA256: { + SHA256_CTX ctx; + SHA256_Init(&ctx); + hsEntity->sha256Ctx = ctx; + break; + } + default: + break; + } + + HsHashStream *hsStreamPtr = new HsHashStream(move(hsEntity)); + if (hsStreamPtr == nullptr) { + HILOGE("Failed to create HsHashStream object on heap."); + return FsResult::Error(ENOMEM); + } + + return FsResult::Success(move(hsStreamPtr)); +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.h b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.h new file mode 100644 index 000000000..b39b86fb2 --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.h @@ -0,0 +1,46 @@ +/* + * 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 INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_CORE_H + +#include +#include "filemgmt_libfs.h" +#include "fs_utils.h" +#include "hs_hashstream_entity.h" + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; + +class HsHashStream { +public: + HsHashStream(const HsHashStream &) = delete; + HsHashStream &operator=(const HsHashStream &) = delete; + + tuple GetHsEntity(); + FsResult Update(ArrayBuffer &buffer); + FsResult Digest(); + + static FsResult Constructor(string alg); + ~HsHashStream() = default; + +private: + unique_ptr entity; + explicit HsHashStream(unique_ptr entity) : entity(move(entity)) {} +}; + +} // namespace OHOS::FileManagement::ModuleFileIO + +#endif // INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_CORE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream_entity.h b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream_entity.h new file mode 100644 index 000000000..de1700cad --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream_entity.h @@ -0,0 +1,37 @@ +/* + * 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 INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_ENTITY_H +#define INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_ENTITY_H + +#include +#include +#include + +#include "hash_core.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +struct HsHashStreamEntity { + MD5_CTX md5Ctx; + SHA_CTX shaCtx; + SHA256_CTX sha256Ctx; + HASH_ALGORITHM_TYPE algType = HASH_ALGORITHM_TYPE_UNSUPPORTED; +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_ENTITY_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets index 06e659dc2..4f81cef3d 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets +++ b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets @@ -23,8 +23,8 @@ namespace securityLabel { let promise = taskpool.execute((path: string, type: DataLevel): void => SecurityLabelImpl.setSecurityLabelSync(path, type), path, type); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -35,8 +35,8 @@ namespace securityLabel { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -50,8 +50,8 @@ namespace securityLabel { promise.then((ret: NullishType): void => { let r = ret as string; resolve(r); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -63,8 +63,8 @@ namespace securityLabel { e.code = 0; let r = ret as string; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index c6df1457c..9a3e2915e 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -19,10 +19,12 @@ group("file_api_unittest") { "class_atomicfile:class_atomicfile_test", "class_file:class_file_test", "filemgmt_libn_test:filemgmt_libn_test", + "js:ani_file_environment_test", "js:ani_file_fs_mock_test", "js:ani_file_fs_test", "js:ani_file_hash_test", "js:ani_file_securitylabel_test", + "js:ani_file_statvfs_test", "js:napi_file_fs_test", "remote_uri:remote_uri_test", "task_signal:task_signal_test", diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index dbef98d62..af69f1996 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -14,59 +14,46 @@ import("//build/test.gni") import("//foundation/filemanagement/file_api/file_api.gni") -ohos_unittest("ani_file_fs_mock_test") { +ohos_unittest("ani_file_environment_test") { branch_protector_ret = "pac_ret" testonly = true module_out_path = "file_api/file_api" - include_dirs = [ - "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", - "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stat", - "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", - "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", + "${src_path}/mod_environment", + "${src_path}/mod_environment/ani", + "${file_api_path}/interfaces/test/unittest/js/mod_environment/mock", ] sources = [ - "mod_fs/class_file/fs_file_mock_test.cpp", - "mod_fs/class_stat/fs_stat_mock_test.cpp", - "mod_fs/properties/access_core_mock_test.cpp", - "mod_fs/properties/copy_file_core_mock_test.cpp", - "mod_fs/properties/mkdir_core_mock_test.cpp", - "mod_fs/properties/mock/system_mock.cpp", - "mod_fs/properties/mock/uv_fs_mock.cpp", - "mod_fs/properties/move_core_mock_test.cpp", - "mod_fs/properties/open_core_mock_test.cpp", - "mod_fs/properties/read_core_mock_test.cpp", - "mod_fs/properties/stat_core_mock_test.cpp", - "mod_fs/properties/truncate_core_mock_test.cpp", - "mod_fs/properties/unlink_core_mock_test.cpp", - "mod_fs/properties/write_core_mock_test.cpp", + "mod_environment/environment_core_mock_test.cpp", + "mod_environment/mock/accesstoken_kit_mock.cpp", + "mod_environment/mock/ipc_skeleton_mock.cpp", + "mod_environment/mock/parameter_mock.cpp", ] deps = [ - "${file_api_path}/interfaces/kits/js:ani_file_fs", - "${file_api_path}/interfaces/kits/native:remote_uri_native", - "${file_api_path}/interfaces/kits/native:task_signal_native", - "${file_api_path}/interfaces/kits/rust:rust_file", + "${file_api_path}/interfaces/kits/js:ani_file_environment", "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] external_deps = [ - "ability_runtime:ability_manager", - "app_file_service:fileuri_native", - "c_utils:utils", + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", "libuv:uv", + "openssl:libcrypto_shared", + "os_account:os_account_innerkits", + "runtime_core:ani", + "runtime_core:libarkruntime", ] defines = [ "private=public" ] - - use_exceptions = true } ohos_unittest("ani_file_fs_test") { @@ -75,8 +62,6 @@ ohos_unittest("ani_file_fs_test") { module_out_path = "file_api/file_api" - resource_config_file = "../resource/ohos_test.xml" - include_dirs = [ "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", @@ -86,23 +71,42 @@ ohos_unittest("ani_file_fs_test") { "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stream", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_tasksignal", "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", + "${file_api_path}/interfaces/kits/js/src/mod_fs/properties/copy_listener", ] sources = [ + "mod_fs/class_atomicfile/fs_atomicfile_test.cpp", "mod_fs/class_file/fs_file_test.cpp", + "mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp", + "mod_fs/class_readeriterator/fs_reader_iterator_test.cpp", "mod_fs/class_stat/fs_stat_test.cpp", + "mod_fs/class_stream/fs_stream_test.cpp", + "mod_fs/class_tasksignal/fs_task_signal_test.cpp", "mod_fs/properties/access_core_test.cpp", "mod_fs/properties/close_core_test.cpp", + "mod_fs/properties/copy_core_test.cpp", + "mod_fs/properties/copy_dir_core_test.cpp", "mod_fs/properties/copy_file_core_test.cpp", + "mod_fs/properties/create_randomaccessfile_core_test.cpp", + "mod_fs/properties/create_stream_core_test.cpp", + "mod_fs/properties/dup_core_test.cpp", + "mod_fs/properties/fdopen_stream_core_test.cpp", "mod_fs/properties/listfile_core_test.cpp", + "mod_fs/properties/lseek_core_test.cpp", + "mod_fs/properties/lstat_core_test.cpp", "mod_fs/properties/move_core_test.cpp", + "mod_fs/properties/movedir_core_test.cpp", "mod_fs/properties/open_core_test.cpp", "mod_fs/properties/read_core_test.cpp", + "mod_fs/properties/read_lines_core_test.cpp", "mod_fs/properties/read_text_core_test.cpp", "mod_fs/properties/rmdir_core_test.cpp", "mod_fs/properties/stat_core_test.cpp", + "mod_fs/properties/trans_listener_test.cpp", "mod_fs/properties/truncate_core_test.cpp", + "mod_fs/properties/utimes_core_test.cpp", "mod_fs/properties/write_core_test.cpp", + "mod_fs/properties/xattr_core_test.cpp", ] deps = [ @@ -118,6 +122,8 @@ ohos_unittest("ani_file_fs_test") { "ability_runtime:ability_manager", "app_file_service:fileuri_native", "c_utils:utils", + "dfs_service:distributed_file_daemon_kit_inner", + "dfs_service:libdistributedfileutils", "googletest:gtest_main", "hilog:libhilog", "ipc:ipc_core", @@ -125,8 +131,98 @@ ohos_unittest("ani_file_fs_test") { ] defines = [ "private=public" ] +} - use_exceptions = true +ohos_unittest("ani_file_fs_mock_test") { + branch_protector_ret = "pac_ret" + testonly = true + + module_out_path = "file_api/file_api" + + include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_randomaccessfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_readeriterator", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stat", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stream", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_tasksignal", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_watcher", + "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", + "${file_api_path}/interfaces/kits/js/src/mod_fs/properties/copy_listener", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/class_stream/mock", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/common", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/mock", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", + ] + + sources = [ + "mod_fs/class_file/fs_file_mock_test.cpp", + "mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp", + "mod_fs/class_readeriterator/fs_reader_iterator_mock_test.cpp", + "mod_fs/class_stat/fs_stat_mock_test.cpp", + "mod_fs/class_stream/fs_stream_mock_test.cpp", + "mod_fs/class_stream/mock/c_mock.cpp", + "mod_fs/class_watcher/fs_file_watcher_mock_test.cpp", + "mod_fs/class_watcher/fs_watcher_mock_test.cpp", + "mod_fs/mock/eventfd_mock.cpp", + "mod_fs/mock/inotify_mock.cpp", + "mod_fs/mock/poll_mock.cpp", + "mod_fs/mock/unistd_mock.cpp", + "mod_fs/properties/access_core_mock_test.cpp", + "mod_fs/properties/copy_core_mock_test.cpp", + "mod_fs/properties/copy_file_core_mock_test.cpp", + "mod_fs/properties/create_randomaccessfile_core_mock_test.cpp", + "mod_fs/properties/dup_core_mock_test.cpp", + "mod_fs/properties/fdatasync_core_mock_test.cpp", + "mod_fs/properties/fsync_core_mock_test.cpp", + "mod_fs/properties/lstat_core_mock_test.cpp", + "mod_fs/properties/mkdir_core_mock_test.cpp", + "mod_fs/properties/mkdtemp_core_mock_test.cpp", + "mod_fs/properties/mock/system_mock.cpp", + "mod_fs/properties/mock/uv_fs_mock.cpp", + "mod_fs/properties/move_core_mock_test.cpp", + "mod_fs/properties/open_core_mock_test.cpp", + "mod_fs/properties/read_core_mock_test.cpp", + "mod_fs/properties/read_lines_core_mock_test.cpp", + "mod_fs/properties/rename_core_mock_test.cpp", + "mod_fs/properties/stat_core_mock_test.cpp", + "mod_fs/properties/symlink_core_mock_test.cpp", + "mod_fs/properties/trans_listener_mock_test.cpp", + "mod_fs/properties/unlink_core_mock_test.cpp", + "mod_fs/properties/utimes_core_mock_test.cpp", + "mod_fs/properties/watcher_core_mock_test.cpp", + "mod_fs/properties/xattr_core_mock_test.cpp", + ] + + deps = [ + "${file_api_path}/interfaces/kits/js:ani_file_fs", + "${file_api_path}/interfaces/kits/native:remote_uri_native", + "${file_api_path}/interfaces/kits/native:task_signal_native", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + ] + + external_deps = [ + "ability_runtime:ability_manager", + "app_file_service:fileuri_native", + "c_utils:utils", + "dfs_service:distributed_file_daemon_kit_inner", + "dfs_service:libdistributedfileutils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "libuv:uv", + ] + + libs = [ "dl" ] + + defines = [ + "ENABLE_DISTRIBUTED_FILE_MOCK", + "private=public", + ] } ohos_unittest("ani_file_hash_test") { @@ -154,8 +250,6 @@ ohos_unittest("ani_file_hash_test") { "hilog:libhilog", "libuv:uv", ] - - use_exceptions = true } ohos_unittest("ani_file_securitylabel_test") { @@ -165,7 +259,10 @@ ohos_unittest("ani_file_securitylabel_test") { sources = [ "mod_securitylabel/securitylabel_core_test.cpp" ] - include_dirs = [ "${file_api_path}/interfaces/kits/js/src/mod_securitylabel" ] + include_dirs = [ + "mock/libuv", + "${file_api_path}/interfaces/kits/js/src/mod_securitylabel", + ] deps = [ "${file_api_path}/interfaces/kits/js:ani_file_securitylabel", @@ -178,9 +275,35 @@ ohos_unittest("ani_file_securitylabel_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "libuv:uv", ] +} - use_exceptions = true +ohos_unittest("ani_file_statvfs_test") { + module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + sources = [ "mod_statvfs/statvfs_core_test.cpp" ] + + include_dirs = [ + "mock/libuv", + "${file_api_path}/interfaces/kits/js/src/mod_statvfs", + ] + + deps = [ + "${file_api_path}/interfaces/kits/js:ani_file_statvfs", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + ] + + external_deps = [ + "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "libuv:uv", + ] } ohos_unittest("napi_file_fs_test") { @@ -213,4 +336,4 @@ ohos_unittest("napi_file_fs_test") { defines = [ "private=public" ] use_exceptions = true -} \ No newline at end of file +} diff --git a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp index 0dcd0e963..90cc1f29f 100644 --- a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp @@ -75,12 +75,12 @@ void FsFileMockTest::TearDown(void) } /** -* @tc.name: FsFileMockTest_GetPath_001 -* @tc.desc: Test function of GetPath() interface for SUCCESS. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileMockTest_GetPath_001 + * @tc.desc: Test function of GetPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetPath_001"; @@ -100,12 +100,12 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_001, testing::ext::TestSize.Leve } /** -* @tc.name: FsFileMockTest_GetPath_002 -* @tc.desc: Test function of GetPath() interface for ERROR. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileMockTest_GetPath_002 + * @tc.desc: Test function of GetPath() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_002, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetPath_002"; @@ -119,12 +119,12 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_002, testing::ext::TestSize.Leve } /** -* @tc.name: FsFileMockTest_GetName_003 -* @tc.desc: Test function of GetName() interface for ERROR. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileMockTest_GetName_003 + * @tc.desc: Test function of GetName() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_003, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_003"; @@ -138,12 +138,12 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_003, testing::ext::TestSize.Leve } /** -* @tc.name: FsFileMockTest_GetName_004 -* @tc.desc: Test function of GetName() interface for ERROR. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileMockTest_GetName_004 + * @tc.desc: Test function of GetName() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_004, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_004"; @@ -157,12 +157,12 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_004, testing::ext::TestSize.Leve } /** -* @tc.name: FsFileMockTest_GetParent_005 -* @tc.desc: Test function of GetParent() interface for ERROR. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileMockTest_GetParent_005 + * @tc.desc: Test function of GetParent() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_005, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_005"; @@ -176,12 +176,12 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_005, testing::ext::TestSize.Le } /** -* @tc.name: FsFileMockTest_GetName_006 -* @tc.desc: Test function of GetName() interface for SUCCESS. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileMockTest_GetName_006 + * @tc.desc: Test function of GetName() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_006, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_006"; @@ -201,12 +201,12 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_006, testing::ext::TestSize.Leve } /** -* @tc.name: FsFileMockTest_GetParent_007 -* @tc.desc: Test function of GetParent() interface for SUCCESS. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileMockTest_GetParent_007 + * @tc.desc: Test function of GetParent() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_007, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_007"; @@ -226,12 +226,12 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_007, testing::ext::TestSize.Le } /** -* @tc.name: FsFileMockTest_GetName_008 -* @tc.desc: Test function of GetName() interface for FALSE. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileMockTest_GetName_008 + * @tc.desc: Test function of GetName() interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_008, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_008"; @@ -251,12 +251,12 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_008, testing::ext::TestSize.Leve } /** -* @tc.name: FsFileMockTest_GetParent_009 -* @tc.desc: Test function of GetParent() interface for FALSE. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileMockTest_GetParent_009 + * @tc.desc: Test function of GetParent() interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_009, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_009"; @@ -276,12 +276,12 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_009, testing::ext::TestSize.Le } /** -* @tc.name: FsFileMockTest_Lock_010 -* @tc.desc: Test function of Lock() interface for SUCCESS. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileMockTest_Lock_010 + * @tc.desc: Test function of Lock() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileMockTest, FsFileMockTest_Lock_010, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_Lock_010"; @@ -294,12 +294,12 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_Lock_010, testing::ext::TestSize.Level1) } /** -* @tc.name: FsFileMockTest_Lock_011 -* @tc.desc: Test function of Lock() interface for FALSE. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileMockTest_Lock_011 + * @tc.desc: Test function of Lock() interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileMockTest, FsFileMockTest_Lock_011, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_Lock_011"; @@ -312,12 +312,12 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_Lock_011, testing::ext::TestSize.Level1) } /** -* @tc.name: FsFileMockTest_TryLock_012 -* @tc.desc: Test function of TryLock() interface for SUCCESS. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileMockTest_TryLock_012 + * @tc.desc: Test function of TryLock() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileMockTest, FsFileMockTest_TryLock_012, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_TryLock_012"; @@ -330,12 +330,12 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_TryLock_012, testing::ext::TestSize.Leve } /** -* @tc.name: FsFileMockTest_TryLock_013 -* @tc.desc: Test function of TryLock() interface for FALSE. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileMockTest_TryLock_013 + * @tc.desc: Test function of TryLock() interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileMockTest, FsFileMockTest_TryLock_013, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_TryLock_013"; @@ -348,12 +348,12 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_TryLock_013, testing::ext::TestSize.Leve } /** -* @tc.name: FsFileMockTest_UnLock_014 -* @tc.desc: Test function of UnLock() interface for SUCCESS. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileMockTest_UnLock_014 + * @tc.desc: Test function of UnLock() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileMockTest, FsFileMockTest_UnLock_014, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_UnLock_014"; @@ -366,12 +366,12 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_UnLock_014, testing::ext::TestSize.Level } /** -* @tc.name: FsFileMockTest_UnLock_015 -* @tc.desc: Test function of UnLock() interface for FALSE. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileMockTest_UnLock_015 + * @tc.desc: Test function of UnLock() interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileMockTest, FsFileMockTest_UnLock_015, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_UnLock_015"; diff --git a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp index 242d472ff..e432f66f6 100644 --- a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp @@ -29,8 +29,8 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - std::unique_ptr fileEntity; - std::unique_ptr fsFile; + unique_ptr fileEntity; + unique_ptr fsFile; }; void FsFileTest::SetUpTestCase(void) @@ -47,13 +47,13 @@ void FsFileTest::SetUp(void) { GTEST_LOG_(INFO) << "SetUp"; - fileEntity = std::make_unique(); + fileEntity = make_unique(); const int fdValue = 3; const bool isClosed = false; - fileEntity->fd_ = std::make_unique(fdValue, isClosed); + fileEntity->fd_ = make_unique(fdValue, isClosed); fileEntity->path_ = "/data/test/file_test.txt"; fileEntity->uri_ = ""; - fsFile = std::make_unique(std::move(fileEntity)); + fsFile = make_unique(move(fileEntity)); } void FsFileTest::TearDown(void) @@ -62,12 +62,12 @@ void FsFileTest::TearDown(void) } /** -* @tc.name: FsFileTest_Constructor_001 -* @tc.desc: Test function of FsFile::Constructor() interface for SUCCESS. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileTest_Constructor_001 + * @tc.desc: Test function of FsFile::Constructor() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileTest, FsFileTest_Constructor_001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_Constructor_001"; @@ -79,12 +79,12 @@ HWTEST_F(FsFileTest, FsFileTest_Constructor_001, testing::ext::TestSize.Level1) } /** -* @tc.name: FsFileTest_GetFD_002 -* @tc.desc: Test function of GetFD() interface for SUCCESS. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileTest_GetFD_002 + * @tc.desc: Test function of GetFD() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileTest, FsFileTest_GetFD_002, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetFD_002"; @@ -96,17 +96,17 @@ HWTEST_F(FsFileTest, FsFileTest_GetFD_002, testing::ext::TestSize.Level1) } /** -* @tc.name: FsFileTest_GetFD_003 -* @tc.desc: Test function of GetFD() interface for ERROR. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileTest_GetFD_003 + * @tc.desc: Test function of GetFD() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileTest, FsFileTest_GetFD_003, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetFD_003"; - fsFile = std::make_unique(nullptr); + fsFile = make_unique(nullptr); auto result = fsFile->GetFD(); EXPECT_EQ(result.IsSuccess(), false); @@ -114,17 +114,17 @@ HWTEST_F(FsFileTest, FsFileTest_GetFD_003, testing::ext::TestSize.Level1) } /** -* @tc.name: FsFileTest_GetPath_004 -* @tc.desc: Test function of GetPath() interface for ERROR. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileTest_GetPath_004 + * @tc.desc: Test function of GetPath() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileTest, FsFileTest_GetPath_004, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetPath_004"; - fsFile = std::make_unique(nullptr); + fsFile = make_unique(nullptr); auto result = fsFile->GetPath(); EXPECT_EQ(result.IsSuccess(), false); @@ -132,17 +132,17 @@ HWTEST_F(FsFileTest, FsFileTest_GetPath_004, testing::ext::TestSize.Level1) } /** -* @tc.name: FsFileTest_GetName_005 -* @tc.desc: Test function of GetName() interface for ERROR. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileTest_GetName_005 + * @tc.desc: Test function of GetName() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileTest, FsFileTest_GetName_005, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetName_005"; - fsFile = std::make_unique(nullptr); + fsFile = make_unique(nullptr); auto result = fsFile->GetName(); EXPECT_EQ(result.IsSuccess(), false); @@ -150,17 +150,17 @@ HWTEST_F(FsFileTest, FsFileTest_GetName_005, testing::ext::TestSize.Level1) } /** -* @tc.name: FsFileTest_GetParent_006 -* @tc.desc: Test function of GetParent() interface for ERROR. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileTest_GetParent_006 + * @tc.desc: Test function of GetParent() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileTest, FsFileTest_GetParent_006, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetParent_006"; - fsFile = std::make_unique(nullptr); + fsFile = make_unique(nullptr); auto result = fsFile->GetParent(); EXPECT_EQ(result.IsSuccess(), false); @@ -168,17 +168,17 @@ HWTEST_F(FsFileTest, FsFileTest_GetParent_006, testing::ext::TestSize.Level1) } /** -* @tc.name: FsFileTest_Lock_007 -* @tc.desc: Test function of Lock() interface for ERROR. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileTest_Lock_007 + * @tc.desc: Test function of Lock() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileTest, FsFileTest_Lock_007, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_Lock_007"; - fsFile = std::make_unique(nullptr); + fsFile = make_unique(nullptr); auto result = fsFile->Lock(true); EXPECT_EQ(result.IsSuccess(), false); @@ -186,17 +186,17 @@ HWTEST_F(FsFileTest, FsFileTest_Lock_007, testing::ext::TestSize.Level1) } /** -* @tc.name: FsFileTest_TryLock_008 -* @tc.desc: Test function of TryLock() interface for ERROR. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileTest_TryLock_008 + * @tc.desc: Test function of TryLock() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileTest, FsFileTest_TryLock_008, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_TryLock_008"; - fsFile = std::make_unique(nullptr); + fsFile = make_unique(nullptr); auto result = fsFile->TryLock(true); EXPECT_EQ(result.IsSuccess(), false); @@ -204,17 +204,17 @@ HWTEST_F(FsFileTest, FsFileTest_TryLock_008, testing::ext::TestSize.Level1) } /** -* @tc.name: FsFileTest_UnLock_009 -* @tc.desc: Test function of UnLock() interface for ERROR. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileTest_UnLock_009 + * @tc.desc: Test function of UnLock() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileTest, FsFileTest_UnLock_009, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_UnLock_009"; - fsFile = std::make_unique(nullptr); + fsFile = make_unique(nullptr); auto result = fsFile->UnLock(); EXPECT_EQ(result.IsSuccess(), false); @@ -222,12 +222,12 @@ HWTEST_F(FsFileTest, FsFileTest_UnLock_009, testing::ext::TestSize.Level1) } /** -* @tc.name: FsFileTest_GetName_010 -* @tc.desc: Test function of GetName() interface for SUCCESS. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileTest_GetName_010 + * @tc.desc: Test function of GetName() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileTest, FsFileTest_GetName_010, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetName_010"; @@ -240,12 +240,12 @@ HWTEST_F(FsFileTest, FsFileTest_GetName_010, testing::ext::TestSize.Level1) } /** -* @tc.name: FsFileTest_GetParent_011 -* @tc.desc: Test function of GetParent() interface for SUCCESS. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileTest_GetParent_011 + * @tc.desc: Test function of GetParent() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileTest, FsFileTest_GetParent_011, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetParent_011"; @@ -258,12 +258,12 @@ HWTEST_F(FsFileTest, FsFileTest_GetParent_011, testing::ext::TestSize.Level1) } /** -* @tc.name: FsFileTest_GetPath_012 -* @tc.desc: Test function of GetPath() interface for SUCCESS. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: FsFileTest_GetPath_012 + * @tc.desc: Test function of GetPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(FsFileTest, FsFileTest_GetPath_012, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetPath_012"; diff --git a/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp index ea2e168d5..2de8331e0 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp @@ -13,13 +13,13 @@ * limitations under the License. */ -#include "fs_stat.h" -#include "../properties/mock/system_mock.h" +#include +#include + #include "fs_stat_entity.h" +#include "fs_stat.h" #include "securec.h" - -#include -#include +#include "system_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -38,7 +38,7 @@ public: void FsStatMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - sys = std::make_shared(); + sys = make_shared(); System::ins = sys; } @@ -70,17 +70,17 @@ HWTEST_F(FsStatMockTest, FsStatMockTest_GetLocation_001, testing::ext::TestSize. { GTEST_LOG_(INFO) << "FsStatMockTes-begin FsStatMockTest_GetLocation_001"; - std::unique_ptr statEntity; - std::unique_ptr fsStat; - statEntity = std::make_unique(); - statEntity->fileInfo_ = std::make_unique(); + unique_ptr statEntity; + unique_ptr fsStat; + statEntity = make_unique(); + statEntity->fileInfo_ = make_unique(); statEntity->fileInfo_->isPath = true; int length = 100; - string testPath = "/test/path"; - statEntity->fileInfo_->path = std::make_unique(length); + string testPath = "/test/stat_path"; + statEntity->fileInfo_->path = make_unique(length); strncpy_s(statEntity->fileInfo_->path.get(), length, testPath.c_str(), testPath.size()); statEntity->fileInfo_->path.get()[99] = '\0'; - fsStat = std::make_unique(std::move(statEntity)); + fsStat = make_unique(move(statEntity)); EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillOnce(Return(1)); EXPECT_EQ(fsStat->GetLocation(), 1); @@ -99,15 +99,15 @@ HWTEST_F(FsStatMockTest, FsStatMockTest_GetLocation_002, testing::ext::TestSize. { GTEST_LOG_(INFO) << "FsStatMockTes-begin FsStatMockTest_GetLocation_002"; - std::unique_ptr statEntity; - std::unique_ptr fsStat; - statEntity = std::make_unique(); - statEntity->fileInfo_ = std::make_unique(); + unique_ptr statEntity; + unique_ptr fsStat; + statEntity = make_unique(); + statEntity->fileInfo_ = make_unique(); statEntity->fileInfo_->isPath = false; - const int fdValue = 3; + const int fdValue = 3; //模拟fd为3 const bool isClosed = false; - statEntity->fileInfo_->fdg = std::make_unique(fdValue, isClosed); - fsStat = std::make_unique(std::move(statEntity)); + statEntity->fileInfo_->fdg = make_unique(fdValue, isClosed); + fsStat = make_unique(move(statEntity)); EXPECT_CALL(*sys, fgetxattr(_, _, _, _)).WillOnce(Return(1)); EXPECT_EQ(fsStat->GetLocation(), 1); diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp new file mode 100644 index 000000000..5641bd86b --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp @@ -0,0 +1,191 @@ +/* + * 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 "c_mock.h" +#include "create_stream_core.h" +#include "fs_utils.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { +using namespace std; + +static const string g_streamFilePath = "/data/test/FsStreamCoreTest.txt"; + +class FsStreamMockTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + CMock::EnableMock(); + int32_t fd = open(g_streamFilePath.c_str(), CREATE | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + CMock::DisableMock(); + rmdir(g_streamFilePath.c_str()); + }; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.name: FsStreamSeekTest_0001 + * @tc.desc: Test function of Seek() interface for fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamSeekTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto mock_ = CMock::GetMock(); + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + auto retSk = result->Seek(1); + EXPECT_FALSE(retSk.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamSeekTest_0001"; +} + +/** + * @tc.name: FsStreamSeekTest_0002 + * @tc.desc: Test function of Seek() interface for fail ftell. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamSeekTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto mock_ = CMock::GetMock(); + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(0)); + EXPECT_CALL(*mock_, ftell(testing::_)).WillOnce(testing::Return(-1)); + + auto retSk = result->Seek(1); + EXPECT_FALSE(retSk.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamSeekTest_0002"; +} + +/** + * @tc.name: FsStreamWriteTest_0001 + * @tc.desc: Test function of Write() interface for string fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamWriteTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto mock_ = CMock::GetMock(); + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + WriteOptions opt; + opt.offset = 5; + auto retWr = result->Write("FsStreamWriteTest_0001", opt); + EXPECT_FALSE(retWr.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamWriteTest_0001"; +} + +/** + * @tc.name: FsStreamWriteTest_0002 + * @tc.desc: Test function of Write() interface for ArrayBuffer fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamWriteTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto mock_ = CMock::GetMock(); + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + WriteOptions opt; + opt.offset = 5; + string buf = "FsStreamWriteTest_0002"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), buf.length()), opt); + EXPECT_FALSE(retWr.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamWriteTest_0002"; +} + +/** + * @tc.name: FsStreamReadTest_0001 + * @tc.desc: Test function of Read() interface for fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamMockTest, FsStreamReadTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamReadTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + + auto mock_ = CMock::GetMock(); + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + ReadOptions opt; + opt.offset = 5; + auto retRd = result->Read(arrayBuffer, opt); + EXPECT_FALSE(retRd.IsSuccess()); + + free(buffer); + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamReadTest_0001"; +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_test.cpp new file mode 100644 index 000000000..770069c81 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_test.cpp @@ -0,0 +1,791 @@ +/* + * 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 "create_stream_core.h" +#include "fs_utils.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +static const string streamFilePath = "/data/test/FsStreamCoreTest.txt"; + +class FsStreamTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + int32_t fd = open(streamFilePath.c_str(), CREATE | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + rmdir(streamFilePath.c_str()); + }; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.name: FsStreamCloseTest_0001 + * @tc.desc: Test function of Close() interface for close success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamCloseTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamCloseTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamCloseTest_0001"; +} + +/** + * @tc.name: FsStreamCloseTest_0002 + * @tc.desc: Test function of Close() interface for close fail. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamCloseTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamCloseTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + retCs = result->Close(); + EXPECT_FALSE(retCs.IsSuccess()); + auto err = retCs.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900005); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamCloseTest_0002"; +} + +/** + * @tc.name: FsStreamFlushTest_0001 + * @tc.desc: Test function of Flush() interface for null fp. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamFlushTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamFlushTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + auto retFl = result->Flush(); + EXPECT_FALSE(retFl.IsSuccess()); + auto err = retFl.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900005); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamFlushTest_0001"; +} + +/** + * @tc.name: FsStreamFlushTest_0002 + * @tc.desc: Test function of Flush() interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamFlushTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamFlushTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retFl = result->Flush(); + ASSERT_TRUE(retFl.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamFlushTest_0002"; +} + +/** + * @tc.name: FsStreamSeekTest_0001 + * @tc.desc: Test function of Seek() interface for null fp. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamSeekTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamSeekTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + auto retSk = result->Seek(0); + EXPECT_FALSE(retSk.IsSuccess()); + auto err = retSk.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamSeekTest_0001"; +} + +/** + * @tc.name: FsStreamSeekTest_0002 + * @tc.desc: Test function of Seek() interface for error whence < SEEK_SET (0). + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamSeekTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamSeekTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + optional opt = -1; + auto retSk = result->Seek(0, opt); + EXPECT_FALSE(retSk.IsSuccess()); + auto err = retSk.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamSeekTest_0002"; +} + +/** + * @tc.name: FsStreamSeekTest_0003 + * @tc.desc: Test function of Seek() interface for error whence > SEEK_END (2). + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamSeekTest_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamSeekTest_0003"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + optional opt = 3; + auto retSk = result->Seek(0, opt); + EXPECT_FALSE(retSk.IsSuccess()); + auto err = retSk.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamSeekTest_0003"; +} + +/** + * @tc.name: FsStreamSeekTest_0004 + * @tc.desc: Test function of Seek() interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamSeekTest_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamSeekTest_0004"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retSk = result->Seek(1); + ASSERT_TRUE(retSk.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamSeekTest_0004"; +} + +/** + * @tc.name: FsStreamWriteTest_0001 + * @tc.desc: Test function of Write() interface for string single argument fail null fp. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + auto retWr = result->Write("FsStreamWriteTest_0001"); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900005); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0001"; +} + +/** + * @tc.name: FsStreamWriteTest_0002 + * @tc.desc: Test function of Write() interface for ArrayBuffer single argument fail null fp. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + string buf = "FsStreamWriteTest_0001"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22)); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900005); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0002"; +} + +/** + * @tc.name: FsStreamWriteTest_0003 + * @tc.desc: Test function of Write() interface for string error offset. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0003"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.offset = -1; + + auto retWr = result->Write("FsStreamWriteTest_0003", opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0003"; +} + +/** + * @tc.name: FsStreamWriteTest_0004 + * @tc.desc: Test function of Write() interface for ArrayBuffer error offset. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0004"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.offset = -1; + + string buf = "FsStreamWriteTest_0004"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0004"; +} + +/** + * @tc.name: FsStreamWriteTest_0005 + * @tc.desc: Test function of Write() interface for string error encoding. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0005"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.encoding = "utf-16"; + + auto retWr = result->Write("FsStreamWriteTest_0005", opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0005"; +} + +/** + * @tc.name: FsStreamWriteTest_0006 + * @tc.desc: Test function of Write() interface for ArrayBuffer error encoding. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0006"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.encoding = "utf-16"; + + string buf = "FsStreamWriteTest_0006"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0004"; +} + +#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__) || defined(__LP64__) +/** + * @tc.name: FsStreamWriteTest_0007 + * @tc.desc: Test function of Write() interface for string > UINT_MAX. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0007"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + size_t largeLength = static_cast(UINT_MAX) + 1; + string largeString(largeLength, 'a'); + + auto retWr = result->Write(largeString); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0005"; +} + +/** + * @tc.name: FsStreamWriteTest_0008 + * @tc.desc: Test function of Write() interface for ArrayBuffer > UINT_MAX. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0008"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + size_t largeLength = static_cast(UINT_MAX) + 1; + string largeString(largeLength, 'a'); + + auto retWr = result->Write(ArrayBuffer(static_cast(largeString.data()), largeLength)); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0008"; +} +#endif + +/** + * @tc.name: FsStreamWriteTest_0009 + * @tc.desc: Test function of Write() interface for string length < 0. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0009"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.length = -1; + + auto retWr = result->Write("FsStreamWriteTest_0009", opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0009"; +} + +/** + * @tc.name: FsStreamWriteTest_0010 + * @tc.desc: Test function of Write() interface for ArrayBuffer length < 0. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0010"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.length = -1; + + string buf = "FsStreamWriteTest_0010"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0010"; +} + +/** + * @tc.name: FsStreamWriteTest_0013 + * @tc.desc: Test function of Write() interface for string no permission. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0013, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0013"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retWr = result->Write("FsStreamWriteTest_0013"); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900005); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0013"; +} + +/** + * @tc.name: FsStreamWriteTest_0014 + * @tc.desc: Test function of Write() interface for ArrayBuffer no permission. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0014, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0014"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + string buf = "FsStreamWriteTest_0014"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22)); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900005); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0014"; +} + +/** + * @tc.name: FsStreamWriteTest_0015 + * @tc.desc: Test function of Write() interface for string success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0015, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0015"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.offset = 0; + opt.encoding = "utf-8"; + opt.length = 5; + + auto retWr = result->Write("FsStreamWriteTest_0015", opt); + ASSERT_TRUE(retWr.IsSuccess()); + + size_t retLen = retWr.GetData().value(); + EXPECT_EQ(retLen, 5); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0015"; +} + +/** + * @tc.name: FsStreamWriteTest_0016 + * @tc.desc: Test function of Write() interface for ArrayBuffer success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0016, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0016"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.offset = 0; + opt.encoding = "utf-8"; + opt.length = 5; + + string buf = "FsStreamWriteTest_0016"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + ASSERT_TRUE(retWr.IsSuccess()); + + size_t retLen = retWr.GetData().value(); + EXPECT_EQ(retLen, 5); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0016"; +} + +/** + * @tc.name: FsStreamReadTest_0001 + * @tc.desc: Test function of Read() interface for single argument null fp. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamReadTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + auto retRd = result->Read(arrayBuffer); + EXPECT_FALSE(retRd.IsSuccess()); + EXPECT_EQ(retRd.GetError().GetErrNo(), 13900005); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamReadTest_0001"; +} + +/** + * @tc.name: FsStreamReadTest_0002 + * @tc.desc: Test function of Read() interface for error offset. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamReadTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + ReadOptions opt; + opt.offset = -1; + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + auto retRd = result->Read(arrayBuffer, opt); + EXPECT_FALSE(retRd.IsSuccess()); + EXPECT_EQ(retRd.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamReadTest_0002"; +} + +#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__) || defined(__LP64__) +/** + * @tc.name: FsStreamReadTest_0003 + * @tc.desc: Test function of Read() interface for bufLen > UINT_MAX. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamReadTest_0003"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + size_t largeLength = static_cast(UINT_MAX) + 1; + + void *buffer = std::malloc(largeLength); + ArrayBuffer arrayBuffer(buffer, largeLength); + auto retRd = result->Read(arrayBuffer); + EXPECT_FALSE(retRd.IsSuccess()); + EXPECT_EQ(retRd.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamReadTest_0003"; +} +#endif + +/** + * @tc.name: FsStreamReadTest_0004 + * @tc.desc: Test function of Read() interface for error length. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamReadTest_0004"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + ReadOptions opt; + opt.length = -1; + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + auto retRd = result->Read(arrayBuffer, opt); + EXPECT_FALSE(retRd.IsSuccess()); + EXPECT_EQ(retRd.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamReadTest_0004"; +} + +/** + * @tc.name: FsStreamReadTest_0005 + * @tc.desc: Test function of Read() interface for no permission. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamReadTest_0005"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + auto retRd = result->Read(arrayBuffer); + EXPECT_FALSE(retRd.IsSuccess()); + EXPECT_EQ(retRd.GetError().GetErrNo(), 13900005); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamReadTest_0005"; +} + +/** + * @tc.name: FsStreamReadTest_0006 + * @tc.desc: Test function of Read() interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamReadTest_0006"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + ReadOptions opt; + opt.offset = 0; + opt.length = 5; + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + + auto retRd = result->Read(arrayBuffer, opt); + ASSERT_TRUE(retRd.IsSuccess()); + + size_t retLen = retRd.GetData().value(); + EXPECT_EQ(retLen, 0); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamReadTest_0006"; +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp new file mode 100644 index 000000000..fa513e4fe --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp @@ -0,0 +1,97 @@ +/* + * 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 "c_mock.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { + +thread_local std::shared_ptr CMock::cMock = nullptr; +thread_local bool CMock::mockable = false; + +std::shared_ptr CMock::GetMock() +{ + if (cMock == nullptr) { + cMock = std::make_shared(); + } + return cMock; +} + +void CMock::EnableMock() +{ + mockable = true; +} + +void CMock::DisableMock() +{ + cMock = nullptr; + mockable = false; +} + +bool CMock::IsMockable() +{ + return mockable; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test + +#ifdef __cplusplus +extern "C" { +using namespace OHOS::FileManagement::ModuleFileIO::Test; + +int fseek(FILE *stream, long len, int offset) +{ + if (CMock::IsMockable()) { + return CMock::GetMock()->fseek(stream, len, offset); + } + + static int (*realFseek)(FILE *, long, int) = []() { + auto func = (int (*)(FILE *, long, int))dlsym(RTLD_NEXT, "fseek"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real fseek: " << dlerror(); + } + return func; + }(); + + if (!realFseek) { + return -1; + } + + return realFseek(stream, len, offset); +} + +long ftell(FILE *stream) +{ + if (CMock::IsMockable()) { + return CMock::GetMock()->ftell(stream); + } + + static long (*realFtell)(FILE *) = []() { + auto func = (long (*)(FILE *))dlsym(RTLD_NEXT, "ftell"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real ftell: " << dlerror(); + } + return func; + }(); + + if (!realFtell) { + return -1; + } + + return realFtell(stream); +} +} // extern "C" +#endif \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h new file mode 100644 index 000000000..9baf3240c --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h @@ -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. + */ + +#ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H + +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { + +class ICMock { +public: + virtual ~ICMock() = default; + virtual int fseek(FILE *, long, int) = 0; + virtual long ftell(FILE *) = 0; +}; + +class CMock : public ICMock { +public: + MOCK_METHOD(int, fseek, (FILE *, long, int), (override)); + MOCK_METHOD(long, ftell, (FILE *), (override)); + +public: + static std::shared_ptr GetMock(); + static void EnableMock(); + static void DisableMock(); + static bool IsMockable(); + +private: + static thread_local std::shared_ptr cMock; + static thread_local bool mockable; +}; + +} // namespace OHOS::FileManagement::ModuleFileIO::Test +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/common/fs_err_code.h b/interfaces/test/unittest/js/mod_fs/common/fs_err_code.h new file mode 100644 index 000000000..7e8a4cbbd --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/common/fs_err_code.h @@ -0,0 +1,34 @@ +/* + * 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 INTERFACES_TEST_UNITTEST_JS_MOD_FS_COMMON_FS_ERR_CODE_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_COMMON_FS_ERR_CODE_H + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +enum FsErrCode { + E_IO_CODE = 13900005, + E_INVAL_CODE = 13900020, +}; + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_COMMON_FS_ERR_CODE_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp b/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp new file mode 100644 index 000000000..1b140d042 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.cpp @@ -0,0 +1,83 @@ +/* + * 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 "eventfd_mock.h" + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +thread_local std::shared_ptr EventfdMock::eventfdMock = nullptr; +thread_local bool EventfdMock::mockable = false; + +std::shared_ptr EventfdMock::GetMock() +{ + if (eventfdMock == nullptr) { + eventfdMock = std::make_shared(); + } + return eventfdMock; +} + +void EventfdMock::EnableMock() +{ + mockable = true; +} + +void EventfdMock::DisableMock() +{ + eventfdMock = nullptr; + mockable = false; +} + +bool EventfdMock::IsMockable() +{ + return mockable; +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#ifdef __cplusplus +extern "C" { +using namespace OHOS::FileManagement::ModuleFileIO::Test; + +int eventfd(unsigned int count, int flags) +{ + if (EventfdMock::IsMockable()) { + return EventfdMock::GetMock()->eventfd(count, flags); + } + + static int (*realEventfd)(unsigned int, int) = []() { + auto func = (int (*)(unsigned int, int))dlsym(RTLD_NEXT, "eventfd"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real eventfd: " << dlerror(); + } + return func; + }(); + + if (!realEventfd) { + return -1; + } + + return realEventfd(count, flags); +} + +} // extern "C" +#endif \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.h b/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.h new file mode 100644 index 000000000..97dd8430f --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/eventfd_mock.h @@ -0,0 +1,53 @@ +/* + * 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 INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_EVENTFD_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_EVENTFD_MOCK_H + +#include +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +class IEventfdMock { +public: + virtual ~IEventfdMock() = default; + virtual int eventfd(unsigned int, int) = 0; +}; + +class EventfdMock : public IEventfdMock { +public: + MOCK_METHOD(int, eventfd, (unsigned int, int), (override)); + +public: + static std::shared_ptr GetMock(); + static void EnableMock(); + static void DisableMock(); + static bool IsMockable(); + +private: + static thread_local std::shared_ptr eventfdMock; + static thread_local bool mockable; +}; + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_EVENTFD_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp b/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp new file mode 100644 index 000000000..729d21a55 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.cpp @@ -0,0 +1,125 @@ +/* + * 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 "inotify_mock.h" + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +thread_local std::shared_ptr InotifyMock::inotifyMock = nullptr; +thread_local bool InotifyMock::mockable = false; + +std::shared_ptr InotifyMock::GetMock() +{ + if (inotifyMock == nullptr) { + inotifyMock = std::make_shared(); + } + return inotifyMock; +} + +void InotifyMock::EnableMock() +{ + mockable = true; +} + +void InotifyMock::DisableMock() +{ + inotifyMock = nullptr; + mockable = false; +} + +bool InotifyMock::IsMockable() +{ + return mockable; +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#ifdef __cplusplus +extern "C" { +using namespace OHOS::FileManagement::ModuleFileIO::Test; + +int inotify_init() +{ + if (InotifyMock::IsMockable()) { + return InotifyMock::GetMock()->inotify_init(); + } + + static int (*realInotifyInit)() = []() { + auto func = (int (*)())dlsym(RTLD_NEXT, "inotify_init"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real inotify_init: " << dlerror(); + } + return func; + }(); + + if (!realInotifyInit) { + return -1; + } + + return realInotifyInit(); +} + +int inotify_add_watch(int fd, const char *pathname, uint32_t mask) +{ + if (InotifyMock::IsMockable()) { + return InotifyMock::GetMock()->inotify_add_watch(fd, pathname, mask); + } + + static int (*realInotifyAddWatch)(int, const char *, uint32_t) = []() { + auto func = (int (*)(int, const char *, uint32_t))dlsym(RTLD_NEXT, "inotify_add_watch"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real inotify_add_watch: " << dlerror(); + } + return func; + }(); + + if (!realInotifyAddWatch) { + return -1; + } + + return realInotifyAddWatch(fd, pathname, mask); +} + +int inotify_rm_watch(int fd, int wd) +{ + if (InotifyMock::IsMockable()) { + return InotifyMock::GetMock()->inotify_rm_watch(fd, wd); + } + + static int (*realInotifyRmWatch)(int, int) = []() { + auto func = (int (*)(int, int))dlsym(RTLD_NEXT, "inotify_rm_watch"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real inotify_rm_watch: " << dlerror(); + } + return func; + }(); + + if (!realInotifyRmWatch) { + return -1; + } + + return realInotifyRmWatch(fd, wd); +} + +} // extern "C" +#endif \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.h b/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.h new file mode 100644 index 000000000..233403309 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/inotify_mock.h @@ -0,0 +1,57 @@ +/* + * 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 INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_INOTIFY_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_INOTIFY_MOCK_H + +#include +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +class IInotifyMock { +public: + virtual ~IInotifyMock() = default; + virtual int inotify_init() = 0; + virtual int inotify_add_watch(int, const char *, uint32_t) = 0; + virtual int inotify_rm_watch(int, int) = 0; +}; + +class InotifyMock : public IInotifyMock { +public: + MOCK_METHOD(int, inotify_init, (), (override)); + MOCK_METHOD(int, inotify_add_watch, (int, const char *, uint32_t), (override)); + MOCK_METHOD(int, inotify_rm_watch, (int, int), (override)); + +public: + static std::shared_ptr GetMock(); + static void EnableMock(); + static void DisableMock(); + static bool IsMockable(); + +private: + static thread_local std::shared_ptr inotifyMock; + static thread_local bool mockable; +}; + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_INOTIFY_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/mock_progress_listener.h b/interfaces/test/unittest/js/mod_fs/mock/mock_progress_listener.h new file mode 100644 index 000000000..e7851c98d --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/mock_progress_listener.h @@ -0,0 +1,29 @@ +/* + * 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 INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_MOCK_PROGRESS_LISTENER_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_MOCK_PROGRESS_LISTENER_H + +#include +#include "i_progress_listener.h" + +namespace OHOS::FileManagement::ModuleFileIO { +class MockProgressListener : public IProgressListener { +public: + MOCK_METHOD(void, InvokeListener, (uint64_t progressSize, uint64_t totalSize), (const, override)); +}; +} // namespace OHOS::FileManagement::ModuleFileIO + +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_MOCK_PROGRESS_LISTENER_H diff --git a/interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp b/interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp new file mode 100644 index 000000000..6e2095ab2 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/poll_mock.cpp @@ -0,0 +1,83 @@ +/* + * 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 "poll_mock.h" + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +thread_local std::shared_ptr PollMock::pollMock = nullptr; +thread_local bool PollMock::mockable = false; + +std::shared_ptr PollMock::GetMock() +{ + if (pollMock == nullptr) { + pollMock = std::make_shared(); + } + return pollMock; +} + +void PollMock::EnableMock() +{ + mockable = true; +} + +void PollMock::DisableMock() +{ + pollMock = nullptr; + mockable = false; +} + +bool PollMock::IsMockable() +{ + return mockable; +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#ifdef __cplusplus +extern "C" { +using namespace OHOS::FileManagement::ModuleFileIO::Test; + +int poll(struct pollfd *fds, nfds_t n, int timeout) +{ + if (PollMock::IsMockable()) { + return PollMock::GetMock()->poll(fds, n, timeout); + } + + static int (*realPoll)(struct pollfd * fds, nfds_t n, int timeout) = []() { + auto func = (int (*)(struct pollfd *, nfds_t, int))dlsym(RTLD_NEXT, "poll"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real poll: " << dlerror(); + } + return func; + }(); + + if (!realPoll) { + return -1; + } + + return realPoll(fds, n, timeout); +} + +} // extern "C" +#endif \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/poll_mock.h b/interfaces/test/unittest/js/mod_fs/mock/poll_mock.h new file mode 100644 index 000000000..da771e4ec --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/poll_mock.h @@ -0,0 +1,53 @@ +/* + * 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 INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_POLL_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_POLL_MOCK_H + +#include +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +class IPollMock { +public: + virtual ~IPollMock() = default; + virtual int poll(struct pollfd *, nfds_t, int) = 0; +}; + +class PollMock : public IPollMock { +public: + MOCK_METHOD(int, poll, (struct pollfd *, nfds_t, int), (override)); + +public: + static std::shared_ptr GetMock(); + static void EnableMock(); + static void DisableMock(); + static bool IsMockable(); + +private: + static thread_local std::shared_ptr pollMock; + static thread_local bool mockable; +}; + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_POLL_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp new file mode 100644 index 000000000..cda59d9bf --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.cpp @@ -0,0 +1,125 @@ +/* + * 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 "unistd_mock.h" + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +thread_local std::shared_ptr UnistdMock::unistdMock = nullptr; +thread_local bool UnistdMock::mockable = false; + +std::shared_ptr UnistdMock::GetMock() +{ + if (unistdMock == nullptr) { + unistdMock = std::make_shared(); + } + return unistdMock; +} + +void UnistdMock::EnableMock() +{ + mockable = true; +} + +void UnistdMock::DisableMock() +{ + unistdMock = nullptr; + mockable = false; +} + +bool UnistdMock::IsMockable() +{ + return mockable; +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#ifdef __cplusplus +extern "C" { +using namespace OHOS::FileManagement::ModuleFileIO::Test; + +int access(const char *filename, int amode) +{ + if (UnistdMock::IsMockable()) { + return UnistdMock::GetMock()->access(filename, amode); + } + + static int (*realAccess)(const char *filename, int amode) = []() { + auto func = (int (*)(const char *, int))dlsym(RTLD_NEXT, "access"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real access: " << dlerror(); + } + return func; + }(); + + if (!realAccess) { + return -1; + } + + return realAccess(filename, amode); +} + +int close(int fd) +{ + if (UnistdMock::IsMockable()) { + return UnistdMock::GetMock()->close(fd); + } + + static int (*realClose)(int fd) = []() { + auto func = (int (*)(int))dlsym(RTLD_NEXT, "close"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real close: " << dlerror(); + } + return func; + }(); + + if (!realClose) { + return -1; + } + + return realClose(fd); +} + +ssize_t read(int fd, void *buf, size_t count) +{ + if (UnistdMock::IsMockable()) { + return UnistdMock::GetMock()->read(fd, buf, count); + } + + static ssize_t (*realRead)(int fd, void *buf, size_t count) = []() { + auto func = (ssize_t(*)(int, void *, size_t))dlsym(RTLD_NEXT, "read"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real read: " << dlerror(); + } + return func; + }(); + + if (!realRead) { + return 0; + } + + return realRead(fd, buf, count); +} + +} // extern "C" +#endif \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h new file mode 100644 index 000000000..ec6f961e1 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/mock/unistd_mock.h @@ -0,0 +1,57 @@ +/* + * 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 INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_UNISTD_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_UNISTD_MOCK_H + +#include +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { + +class IUnistdMock { +public: + virtual ~IUnistdMock() = default; + virtual int access(const char *, int) = 0; + virtual int close(int) = 0; + virtual ssize_t read(int, void *, size_t) = 0; +}; + +class UnistdMock : public IUnistdMock { +public: + MOCK_METHOD(int, access, (const char *, int), (override)); + MOCK_METHOD(int, close, (int), (override)); + MOCK_METHOD(ssize_t, read, (int, void *, size_t), (override)); + +public: + static std::shared_ptr GetMock(); + static void EnableMock(); + static void DisableMock(); + static bool IsMockable(); + +private: + static thread_local std::shared_ptr unistdMock; + static thread_local bool mockable; +}; + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_MOCK_UNISTD_MOCK_H diff --git a/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp index 4d9355d9c..a95e4eea4 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp @@ -67,7 +67,7 @@ void AccessCoreMockTest::TearDown(void) */ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreMockTest_DoAccess_001"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-begin AccessCoreMockTest_DoAccess_001"; std::string path = "TEST"; std::optional mode; @@ -76,7 +76,7 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_001, testing::ext::Test auto res = AccessCore::DoAccess(path, mode); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_001"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-end AccessCoreMockTest_DoAccess_001"; } /** @@ -88,7 +88,7 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_001, testing::ext::Test */ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreMockTest_DoAccess_002"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-begin AccessCoreMockTest_DoAccess_002"; std::string path = "TEST"; std::optional mode; @@ -98,7 +98,7 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_002, testing::ext::Test auto res = AccessCore::DoAccess(path, mode); EXPECT_EQ(res.IsSuccess(), true); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_002"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-end AccessCoreMockTest_DoAccess_002"; } /** @@ -110,7 +110,7 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_002, testing::ext::Test */ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_003, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreMockTest_DoAccess_003"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-begin AccessCoreMockTest_DoAccess_003"; std::string path = "TEST"; AccessModeType mode = AccessModeType::EXIST; @@ -120,7 +120,7 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_003, testing::ext::Test auto res = AccessCore::DoAccess(path, mode, flag); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_003"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-end AccessCoreMockTest_DoAccess_003"; } /** @@ -132,7 +132,7 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_003, testing::ext::Test */ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_004, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreMockTest_DoAccess_004"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-begin AccessCoreMockTest_DoAccess_004"; std::string path = "TEST"; AccessModeType mode = AccessModeType::EXIST; @@ -143,30 +143,7 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_004, testing::ext::Test auto res = AccessCore::DoAccess(path, mode, flag); EXPECT_EQ(res.IsSuccess(), true); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_004"; -} - -/** - * @tc.name: AccessCoreMockTest_DoAccess_005 - * @tc.desc: Test function of AccessCore::DoAccess interface for success. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_005, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "AccessCoreMockTest-begin AccessCoreMockTest_DoAccess_005"; - - std::string path = DISTRIBUTED_FILE_PREFIX; - AccessModeType mode = AccessModeType::EXIST; - AccessFlag flag = LOCAL_FLAG; - - EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); - - auto res = AccessCore::DoAccess(path, mode, flag); - EXPECT_EQ(res.IsSuccess(), true); - - GTEST_LOG_(INFO) << "AccessCoreMockTest-end AccessCoreMockTest_DoAccess_005"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-end AccessCoreMockTest_DoAccess_004"; } } // OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp index b7966bd3c..6460d9a65 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp @@ -24,7 +24,6 @@ namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; using namespace std; - const mode_t DIR_PERMISSIONS = 0755; class AccessCoreTest : public testing::Test { @@ -61,8 +60,7 @@ void AccessCoreTest::TearDown(void) } // 递归创建多级目录的辅助函数 -bool CreateDirectoryRecursive(const std::string& path) -{ +bool CreateDirectoryRecursive(const std::string& path) { if (path.empty()) { return false; } @@ -74,8 +72,7 @@ bool CreateDirectoryRecursive(const std::string& path) pos++; } - while ((pos = path.find('/', pos)) != std::string::npos) - { + while ((pos = path.find('/', pos)) != std::string::npos) { dir = path.substr(0, pos++); if (dir.empty()) { continue; @@ -87,8 +84,7 @@ bool CreateDirectoryRecursive(const std::string& path) } } - if (mkdir(path.c_str(), DIR_PERMISSIONS) == -1 && errno != EEXIST) - { + if (mkdir(path.c_str(), DIR_PERMISSIONS) == -1 && errno != EEXIST) { return false; } return true; @@ -212,7 +208,7 @@ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_006, testing::ext::TestSize.Lev { GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_006"; - std::string path = "test"; + std::string path = "AccessCoreTest"; AccessModeType mode = AccessModeType::EXIST; AccessFlag flag = LOCAL_FLAG; diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp new file mode 100644 index 000000000..2b5266ae4 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp @@ -0,0 +1,552 @@ +/* + * 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 +#include +#include + +#include "copy_core.h" +#include "inotify_mock.h" +#include "mock_progress_listener.h" +#include "poll_mock.h" +#include "unistd_mock.h" +#include "uv_fs_mock.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CopyCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; + + static const string testDir; + static const string srcDir; + static const string destDir; + static const string srcFile; + static const string destFile; + +private: + // rwxr-xr-x + static constexpr mode_t permission0755 = 0755; + // rw-r--r-- + static constexpr mode_t permission0644 = 0644; +}; + +const string CopyCoreMockTest::testDir = "/data/test/CopyCoreMockTest"; +const string CopyCoreMockTest::srcDir = testDir + "/src"; +const string CopyCoreMockTest::destDir = testDir + "/dest"; +const string CopyCoreMockTest::srcFile = srcDir + "/src.txt"; +const string CopyCoreMockTest::destFile = destDir + "/dest.txt"; + +void CopyCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + mkdir(testDir.c_str(), permission0755); + mkdir(srcDir.c_str(), permission0755); + mkdir(destDir.c_str(), permission0755); + int32_t fd = open(srcFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + uvMock = std::make_shared(); + Uvfs::ins = uvMock; + InotifyMock::EnableMock(); + PollMock::EnableMock(); + UnistdMock::EnableMock(); +} + +void CopyCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + int ret = remove(srcFile.c_str()); + EXPECT_TRUE(ret == 0); + rmdir(srcDir.c_str()); + rmdir(destDir.c_str()); + rmdir(testDir.c_str()); + Uvfs::ins = nullptr; + uvMock = nullptr; + InotifyMock::DisableMock(); + PollMock::DisableMock(); + UnistdMock::DisableMock(); +} + +void CopyCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CopyCoreMockTest::TearDown(void) +{ + CopyCore::callbackMap_.clear(); + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: CopyCoreMockTest_CopyFile_001 + * @tc.desc: Test function of CopyCore::CopyFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_CopyFile_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_CopyFile_001"; + + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = CopyCoreMockTest::srcFile; + infos->destPath = CopyCoreMockTest::destFile; + auto unistdMock = UnistdMock::GetMock(); + + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*uvMock, uv_fs_sendfile(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + + auto res = CopyCore::CopyFile(srcFile, destFile, infos); + EXPECT_EQ(res, errno); + + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_CopyFile_001"; +} + +/** + * @tc.name: CopyCoreMockTest_DoCopy_001 + * @tc.desc: Test function of CopyCore::DoCopy interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_DoCopy_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_DoCopy_001"; + + string srcUri = "file://" + srcFile; + string destUri = "file://" + destFile; + optional options; + auto unistdMock = UnistdMock::GetMock(); + + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*uvMock, uv_fs_sendfile(_, _, _, _, _, _, _)).WillOnce(Return(0)); + + auto res = CopyCore::DoCopy(srcUri, destUri, options); + EXPECT_EQ(res.IsSuccess(), true); + EXPECT_TRUE(filesystem::exists(destFile)); + int ret = remove(destFile.c_str()); + EXPECT_TRUE(ret == 0); + + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_DoCopy_001"; +} + +/** + * @tc.name: CopyCoreMockTest_CopySubDir_001 + * @tc.desc: Test function of CopyCore::CopySubDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_CopySubDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_CopySubDir_001"; + + string subDir = srcDir + "/sub_dir"; + mkdir(subDir.c_str(), permission0755); + string subFile = subDir + "/sub_file.txt"; + int fd = open(subFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + + auto inotifyMock = InotifyMock::GetMock(); + string destSubDir = destDir + "/sub_dir"; + auto infos = make_shared(); + infos->notifyFd = 1; + auto unistdMock = UnistdMock::GetMock(); + + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)).WillOnce(testing::Return(0)); + auto res = CopyCore::CopySubDir(subDir, destSubDir, infos); + EXPECT_EQ(res, UNKNOWN_ERR); + + int ret = remove(subFile.c_str()); + EXPECT_TRUE(ret == 0); + rmdir(subDir.c_str()); + rmdir(destSubDir.c_str()); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_CopySubDir_001"; +} + +/** + * @tc.name: CopyCoreMockTest_CopySubDir_002 + * @tc.desc: Test CopyCore::CopySubDir when iter == CopyCore::callbackMap_.end() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_CopySubDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_CopySubDir_002"; + + string subDir = srcDir + "/sub_dir"; + mkdir(subDir.c_str(), permission0755); + string subFile = subDir + "/sub_file.txt"; + int fd = open(subFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + + auto inotifyMock = InotifyMock::GetMock(); + string destSubDir = destDir + "/sub_dir"; + auto infos = make_shared(); + infos->notifyFd = 1; + auto unistdMock = UnistdMock::GetMock(); + CopyCore::callbackMap_.clear(); + + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)).WillOnce(testing::Return(0)); + auto res = CopyCore::CopySubDir(subDir, destSubDir, infos); + EXPECT_EQ(res, UNKNOWN_ERR); + + int ret = remove(subFile.c_str()); + EXPECT_TRUE(ret == 0); + rmdir(subDir.c_str()); + rmdir(destSubDir.c_str()); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_CopySubDir_002"; +} + +/** + * @tc.name: CopyCoreMockTest_CopySubDir_003 + * @tc.desc: Test CopyCore::CopySubDir when iter->second == nullptr + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_CopySubDir_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_CopySubDir_003"; + + string subDir = srcDir + "/sub_dir"; + mkdir(subDir.c_str(), permission0755); + string subFile = subDir + "/sub_file.txt"; + int fd = open(subFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + GTEST_LOG_(INFO) << "Open test file failed! ret: " << fd << ", errno: " << errno; + EXPECT_TRUE(false); + } + close(fd); + + auto inotifyMock = InotifyMock::GetMock(); + string destSubDir = destDir + "/sub_dir"; + auto infos = make_shared(); + infos->notifyFd = 1; + auto unistdMock = UnistdMock::GetMock(); + CopyCore::callbackMap_[*infos] = nullptr; + + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)).WillOnce(testing::Return(0)); + auto res = CopyCore::CopySubDir(subDir, destSubDir, infos); + EXPECT_EQ(res, UNKNOWN_ERR); + + int ret = remove(subFile.c_str()); + EXPECT_TRUE(ret == 0); + rmdir(subDir.c_str()); + rmdir(destSubDir.c_str()); + CopyCore::callbackMap_.clear(); + + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_CopySubDir_003"; +} + +/** + * @tc.name: CopyCoreMockTest_ReceiveComplete_001 + * @tc.desc: Test CopyCore::ReceiveComplete in normal case + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_ReceiveComplete_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_ReceiveComplete_001"; + + auto mockListener = std::make_shared(); + auto callback = std::make_shared(mockListener); + callback->maxProgressSize = 50; + auto entry = std::make_shared(callback); + // 当前已经拷贝的字节数 + entry->progressSize = 100; + // 需要拷贝的字节数 + entry->totalSize = 200; + + EXPECT_CALL(*mockListener, InvokeListener(entry->progressSize, entry->totalSize)).Times(1); + CopyCore::ReceiveComplete(entry); + + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_ReceiveComplete_001"; +} + +/** + * @tc.name: CopyCoreMockTest_ReceiveComplete_002 + * @tc.desc: Test CopyCore::ReceiveComplete when processedSize < entry->callback->maxProgressSize + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_ReceiveComplete_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_ReceiveComplete_002"; + + auto mockListener = std::make_shared(); + auto callback = std::make_shared(mockListener); + callback->maxProgressSize = 100; + auto entry = std::make_shared(callback); + entry->progressSize = 50; // Mock valid progressSize + entry->totalSize = 200; // Mock valid totalSize, and progressSize < totalSize + + EXPECT_CALL(*mockListener, InvokeListener(testing::_, testing::_)).Times(0); + CopyCore::ReceiveComplete(entry); + + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_ReceiveComplete_002"; +} + +/** + * @tc.name: CopyCoreMockTest_ReadNotifyEvent_001 + * @tc.desc: Test function of CopyCoreMockTest::ReadNotifyEvent interface for SUCCESS when read valid event data. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 0 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_ReadNotifyEvent_001, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_ReadNotifyEvent_001"; + // Prepare test condition + auto infos = make_shared(); + infos->run = true; + auto callback = CopyCore::RegisterListener(infos); + int32_t len = static_cast(sizeof(struct inotify_event)); + // Set mock behaviors + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).Times(1).WillOnce(testing::Return(len)); + // Do testing + CopyCore::ReadNotifyEvent(infos); + // Verify results + EXPECT_NE(callback, nullptr); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_ReadNotifyEvent_001"; +} + +/** + * @tc.name: CopyCoreMockTest_ReadNotifyEvent_002 + * @tc.desc: Test function of CopyCore::ReadNotifyEvent interface for FAILURE when read returns -1. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_ReadNotifyEvent_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_ReadNotifyEvent_002"; + // Prepare test condition + auto infos = make_shared(); + infos->run = true; + auto callback = CopyCore::RegisterListener(infos); + // Set mock behaviors + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::SetErrnoAndReturn(EIO, -1)); + // Do testing + CopyCore::ReadNotifyEvent(infos); + // Verify results + EXPECT_NE(callback, nullptr); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + EXPECT_EQ(errno, EIO); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_ReadNotifyEvent_002"; +} + +/** + * @tc.name: CopyCoreMockTest_ReadNotifyEvent_003 + * @tc.desc: Test function of CopyCore::ReadNotifyEvent interface for SUCCESS when read returns 0 (EOF). + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_ReadNotifyEvent_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_ReadNotifyEvent_003"; + // Prepare test condition + auto infos = make_shared(); + infos->run = true; + auto callback = CopyCore::RegisterListener(infos); + // Set mock behaviors + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::SetErrnoAndReturn(0, 0)); + // Do testing + CopyCore::ReadNotifyEvent(infos); + // Verify results + EXPECT_NE(callback, nullptr); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + EXPECT_EQ(errno, 0); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_ReadNotifyEvent_003"; +} + +/** + * @tc.name: CopyCoreMockTest_ReadNotifyEventLocked_001 + * @tc.desc: Test ReadNotifyEventLocked when closed is false. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 0 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_ReadNotifyEventLocked_001, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_ReadNotifyEventLocked_001"; + // Prepare test condition + auto infos = make_shared(); + auto callback = std::make_shared(nullptr); + callback->closed = false; + // Set mock behaviors + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); + EXPECT_CALL(*unistdMock, close(testing::_)).Times(0); + // Do testing + CopyCore::ReadNotifyEventLocked(infos, callback); + // Verify results + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + EXPECT_FALSE(callback->reading); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_ReadNotifyEventLocked_001"; +} + +/** + * @tc.name: CopyCoreMockTest_ReadNotifyEventLocked_002 + * @tc.desc: Test ReadNotifyEventLocked when close after read. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_ReadNotifyEventLocked_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_ReadNotifyEventLocked_002"; + // Prepare test condition + auto infos = make_shared(); + auto callback = std::make_shared(nullptr); + callback->closed = false; + // Set mock behaviors + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce([callback](int fd, void *buf, size_t count) { + errno = EIO; + callback->closed = true; + return 0; + }); + EXPECT_CALL(*unistdMock, close(testing::_)).WillRepeatedly(testing::Return(0)); + // Do testing + CopyCore::ReadNotifyEventLocked(infos, callback); + // Verify results + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + EXPECT_FALSE(callback->closed); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_ReadNotifyEventLocked_002"; +} + +/** + * @tc.name: CopyCoreMockTest_GetNotifyId_001 + * @tc.desc: Test function of CopyCore::GetNotifyId interface for SUCCESS. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 0 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_GetNotifyId_001, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_GetNotifyId_001"; + // Prepare test condition + auto infos = make_shared(); + infos->run = true; + infos->exceptionCode = ERRNO_NOERR; + infos->eventFd = 1; + infos->notifyFd = 1; + + auto callback = CopyCore::RegisterListener(infos); + // Set mock behaviors + auto pollMock = PollMock::GetMock(); + EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce([infos](struct pollfd *fds, nfds_t n, int timeout) { + fds[1].revents = POLLIN; + // Ensure the loop will exit + infos->run = false; + return 1; + }); + // Do testing + CopyCore::GetNotifyEvent(infos); + // Verify results + testing::Mock::VerifyAndClearExpectations(pollMock.get()); + EXPECT_NE(callback, nullptr); + EXPECT_FALSE(infos->run); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_GetNotifyId_001"; +} + +/** + * @tc.name: CopyCoreMockTest_GetNotifyId_002 + * @tc.desc: Test function of CopyCore::GetNotifyId interface fails when callback is nullptr. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 0 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_GetNotifyId_002, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_GetNotifyId_002"; + // Prepare test condition + auto infos = make_shared(); + // Do testing + CopyCore::GetNotifyEvent(infos); + // Verify results + EXPECT_EQ(infos->exceptionCode, EINVAL); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_GetNotifyId_002"; +} + +/** + * @tc.name: CopyCoreMockTest_CreateFileInfos_001 + * @tc.desc: Test function of CopyCore::CreateFileInfos interface with listener and copySignal. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_CreateFileInfos_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_CreateFileInfos_001"; + + auto copySignal = std::make_shared(); + copySignal->taskSignal_ = std::make_shared(); + auto options = std::make_optional(); + options->progressListener = std::make_shared(); + options->copySignal = std::move(copySignal); + + auto [errCode, infos] = CopyCore::CreateFileInfos(srcFile, destFile, options); + EXPECT_EQ(errCode, ERRNO_NOERR); + EXPECT_NE(infos, nullptr); + if (infos) { + EXPECT_TRUE(infos->hasListener); + EXPECT_NE(infos->listener, nullptr); + EXPECT_NE(infos->taskSignal, nullptr); + } + + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_CreateFileInfos_001"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_core_test.cpp new file mode 100644 index 000000000..febb6cf12 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_core_test.cpp @@ -0,0 +1,1150 @@ +/* + * 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 "copy_core.h" + +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CopyCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + static const string testDir; + static const string copyCoreDir; + static const string srcDir; + static const string destDir; + static const string srcFile; + static const string destFile; + +private: + // rwxr-xr-x + static constexpr mode_t permission0755 = 0755; + // rw-r--r-- + static constexpr mode_t permission0644 = 0644; +}; + +const string CopyCoreTest::testDir = "/data/test"; +const string CopyCoreTest::copyCoreDir = testDir + "/CopyCoreTest"; +const string CopyCoreTest::srcDir = copyCoreDir + "/src"; +const string CopyCoreTest::destDir = copyCoreDir + "/dest"; +const string CopyCoreTest::srcFile = srcDir + "/src.txt"; +const string CopyCoreTest::destFile = destDir + "/dest.txt"; + +void CopyCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + mkdir(testDir.c_str(), permission0755); + mkdir(copyCoreDir.c_str(), permission0755); + mkdir(srcDir.c_str(), permission0755); + mkdir(destDir.c_str(), permission0755); + int32_t fd = open(srcFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); +} + +void CopyCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + int ret = remove(srcFile.c_str()); + EXPECT_TRUE(ret == 0); + rmdir(srcDir.c_str()); + rmdir(destDir.c_str()); + rmdir(testDir.c_str()); +} + +void CopyCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CopyCoreTest::TearDown(void) +{ + CopyCore::callbackMap_.clear(); + GTEST_LOG_(INFO) << "TearDown"; +} + +inline const int32_t EXPECTED_WD = 100; +inline const int32_t UNEXPECTED_WD = 200; + +/** + * @tc.name: CopyCoreTest_IsValidUri_001 + * @tc.desc: Test function of CopyCore::IsValidUri interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsValidUri_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsValidUri_001"; + + string validUri = "file://data/test/file.txt"; + auto res = CopyCore::IsValidUri(validUri); + EXPECT_EQ(res, true); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsValidUri_001"; +} + +/** + * @tc.name: CopyCoreTest_IsValidUri_002 + * @tc.desc: Test function of CopyCore::IsValidUri interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsValidUri_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsValidUri_002"; + + string invalidUri = "invalid://data/test/CopyCoreTest_IsValidUri_002/file.txt"; + auto res = CopyCore::IsValidUri(invalidUri); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsValidUri_002"; +} + +/** + * @tc.name: CopyCoreTest_IsRemoteUri_001 + * @tc.desc: Test function of CopyCore::IsRemoteUri interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsRemoteUri_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsRemoteUri_001"; + + string remoteUri = "file://data/test/file.txt?networkid=123"; + auto res = CopyCore::IsRemoteUri(remoteUri); + EXPECT_EQ(res, true); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsRemoteUri_001"; +} + +/** + * @tc.name: CopyCoreTest_IsRemoteUri_002 + * @tc.desc: Test function of CopyCore::IsRemoteUri interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsRemoteUri_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsRemoteUri_002"; + + string localUri = "file://data/test/file.txt"; + auto res = CopyCore::IsRemoteUri(localUri); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsRemoteUri_002"; +} + +/** + * @tc.name: CopyCoreTest_IsDirectory_001 + * @tc.desc: Test function of CopyCore::IsDirectory interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsDirectory_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsDirectory_001"; + + auto res = CopyCore::IsDirectory(srcDir); + EXPECT_EQ(res, true); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsDirectory_001"; +} + +/** + * @tc.name: CopyCoreTest_IsDirectory_002 + * @tc.desc: Test function of CopyCore::IsDirectory interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsDirectory_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsDirectory_002"; + + auto res = CopyCore::IsDirectory(srcFile); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsDirectory_002"; +} + +/** + * @tc.name: CopyCoreTest_IsFile_001 + * @tc.desc: Test function of CopyCore::IsFile interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsFile_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsFile_001"; + + auto res = CopyCore::IsFile(srcFile); + EXPECT_EQ(res, true); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsFile_001"; +} + +/** + * @tc.name: CopyCoreTest_IsFile_002 + * @tc.desc: Test function of CopyCore::IsFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsFile_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsFile_002"; + + auto res = CopyCore::IsFile(srcDir); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsFile_002"; +} + +/** + * @tc.name: CopyCoreTest_IsMediaUri_001 + * @tc.desc: Test function of CopyCore::IsMediaUri interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsMediaUri_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsMediaUri_001"; + + auto res = CopyCore::IsMediaUri(srcFile); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsMediaUri_001"; +} + +/** + * @tc.name: CopyCoreTest_GetFileSize_001 + * @tc.desc: Test function of CopyCore::GetFileSize interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetFileSize_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetFileSize_001"; + + auto [err, size] = CopyCore::GetFileSize(srcFile); + EXPECT_EQ(err, ERRNO_NOERR); + EXPECT_GE(size, 0); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetFileSize_001"; +} + +/** + * @tc.name: CopyCoreTest_GetFileSize_002 + * @tc.desc: Test function of CopyCore::GetFileSize interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetFileSize_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetFileSize_002"; + + string nonExistentFile = "/data/test/non_existent.txt"; + auto [err, size] = CopyCore::GetFileSize(nonExistentFile); + EXPECT_NE(err, ERRNO_NOERR); + EXPECT_EQ(size, 0); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetFileSize_002"; +} + +/** + * @tc.name: CopyCoreTest_CheckOrCreatePath_001 + * @tc.desc: Test function of CopyCore::CheckOrCreatePath interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CheckOrCreatePath_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CheckOrCreatePath_001"; + + auto res = CopyCore::CheckOrCreatePath(srcFile); + EXPECT_EQ(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CheckOrCreatePath_001"; +} + +/** + * @tc.name: CopyCoreTest_CheckOrCreatePath_002 + * @tc.desc: Test function of CopyCore::CheckOrCreatePath interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CheckOrCreatePath_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CheckOrCreatePath_002"; + + string newFile = destDir + "/new_file.txt"; + auto res = CopyCore::CheckOrCreatePath(newFile); + EXPECT_EQ(res, ERRNO_NOERR); + EXPECT_TRUE(CopyCore::IsFile(newFile)); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CheckOrCreatePath_002"; +} + +/** + * @tc.name: CopyCoreTest_MakeDir_001 + * @tc.desc: Test function of CopyCore::MakeDir interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_MakeDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_MakeDir_001"; + + string newDir = destDir + "/new_dir"; + auto res = CopyCore::MakeDir(newDir); + EXPECT_EQ(res, ERRNO_NOERR); + EXPECT_TRUE(CopyCore::IsDirectory(newDir)); + + rmdir(newDir.c_str()); + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_MakeDir_001"; +} + +/** + * @tc.name: CopyCoreTest_MakeDir_002 + * @tc.desc: Test function of CopyCore::MakeDir interface for TRUE. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_MakeDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_MakeDir_002"; + + auto res = CopyCore::MakeDir(srcDir); + EXPECT_EQ(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_MakeDir_002"; +} + +/** + * @tc.name: CopyCoreTest_MakeDir_003 + * @tc.desc: Test function of CopyCore::MakeDir interface for FALSE. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_MakeDir_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_MakeDir_003"; + + string invalidPath = "/invalid/path/dir"; + auto res = CopyCore::MakeDir(invalidPath); + EXPECT_NE(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_MakeDir_003"; +} + +/** + * @tc.name: CopyCoreTest_ValidParams_001 + * @tc.desc: Test function of CopyCore::ValidParams interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ValidParams_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ValidParams_001"; + + string srcFile = "invalid://data/test/src.txt"; + + auto res = CopyCore::ValidParams(srcFile, destFile); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ValidParams_001"; +} + +/** + * @tc.name: CopyCoreTest_ValidParams_002 + * @tc.desc: Test function of CopyCore::ValidParams interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ValidParams_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ValidParams_002"; + + string destFile = "invalid://data/test/dest.txt"; + + auto res = CopyCore::ValidParams(srcFile, destFile); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ValidParams_002"; +} + +/** + * @tc.name: CopyCoreTest_ValidParams_003 + * @tc.desc: Test function of CopyCore::ValidParams interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ValidParams_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ValidParams_003"; + + string src = "file://data/test/src.txt"; + string dest = "file://data/test/dest.txt"; + + auto res = CopyCore::ValidParams(src, dest); + EXPECT_EQ(res, true); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ValidParams_003"; +} + +/** + * @tc.name: CopyCoreTest_CreateFileInfos_001 + * @tc.desc: Test function of CopyCore::CreateFileInfos interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CreateFileInfos_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CreateFileInfos_001"; + + optional options = std::make_optional(); + + auto [errCode, infos] = CopyCore::CreateFileInfos(srcFile, destFile, options); + EXPECT_EQ(errCode, ERRNO_NOERR); + EXPECT_NE(infos, nullptr); + if (infos) { + EXPECT_FALSE(infos->hasListener); + EXPECT_EQ(infos->listener, nullptr); + EXPECT_EQ(infos->taskSignal, nullptr); + } + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CreateFileInfos_001"; +} + +/** + * @tc.name: CopyCoreTest_CopySubDir_001 + * @tc.desc: Test function of CopyCore::CopySubDir interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CopySubDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CopySubDir_001"; + + string subDir = srcDir + "/CopyCoreTest_CopySubDir_001_sub_dir"; + mkdir(subDir.c_str(), permission0755); + string subFile = subDir + "/sub_file.txt"; + int fd = open(subFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + + string destSubDir = destDir + "/sub_dir"; + auto infos = make_shared(); + auto res = CopyCore::CopySubDir(subDir, destSubDir, infos); + string destSubFile = destSubDir + "/sub_file.txt"; + EXPECT_EQ(res, ERRNO_NOERR); + EXPECT_TRUE(CopyCore::IsDirectory(destSubDir)); + EXPECT_TRUE(CopyCore::IsFile(destSubFile)); + + remove(subFile.c_str()); + rmdir(subDir.c_str()); + remove(destSubFile.c_str()); + rmdir(destSubDir.c_str()); + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CopySubDir_001"; +} + +/** + * @tc.name: CopyCoreTest_CopySubDir_002 + * @tc.desc: Test function of CopyCore::CopySubDir interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CopySubDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CopySubDir_002"; + + string subDir = srcDir + "/sub_dir"; + mkdir(subDir.c_str(), permission0755); + string subFile = subDir + "/sub_file.txt"; + int fd = open(subFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + + string destSubDir = destDir + "/sub_dir"; + auto infos = make_shared(); + infos->notifyFd = 1; + auto res = CopyCore::CopySubDir(subDir, destSubDir, infos); + EXPECT_EQ(res, errno); + + remove(subFile.c_str()); + rmdir(subDir.c_str()); + rmdir(destSubDir.c_str()); + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CopySubDir_002"; +} + +/** + * @tc.name: CopyCoreTest_RecurCopyDir_001 + * @tc.desc: Test function of CopyCore::RecurCopyDir interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_RecurCopyDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_RecurCopyDir_001"; + + string subDir = srcDir + "/sub_dir"; + mkdir(subDir.c_str(), permission0755); + string subFile = subDir + "/sub_file.txt"; + int fd = open(subFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + + string destSubDir = destDir + "/sub_dir"; + auto infos = make_shared(); + auto res = CopyCore::RecurCopyDir(srcDir, destDir, infos); + string destSubFile = destSubDir + "/sub_file.txt"; + EXPECT_EQ(res, ERRNO_NOERR); + EXPECT_TRUE(CopyCore::IsDirectory(destSubDir)); + EXPECT_TRUE(CopyCore::IsFile(destSubDir + "/sub_file.txt")); + + remove(subFile.c_str()); + rmdir(subDir.c_str()); + remove(destSubFile.c_str()); + rmdir(destSubDir.c_str()); + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_RecurCopyDir_001"; +} + +/** + * @tc.name: CopyCoreTest_CopyDirFunc_001 + * @tc.desc: Test function of CopyCore::CopyDirFunc interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CopyDirFunc_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CopyDirFunc_001"; + + string subDir = srcDir + "/sub_dir"; + mkdir(subDir.c_str(), permission0755); + string subFile = subDir + "/sub_file.txt"; + int fd = open(subFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + + string destSubDir = destDir + "/src/sub_dir"; + string destSubFile = destSubDir + "/sub_file.txt"; + string destSrcDir = destDir + "/src"; + auto infos = make_shared(); + auto res = CopyCore::CopyDirFunc(srcDir, destDir, infos); + EXPECT_EQ(res, ERRNO_NOERR); + EXPECT_EQ(CopyCore::IsDirectory(destSubDir), false); + EXPECT_EQ(CopyCore::IsFile(destSubDir + "/sub_file.txt"), false); + + remove(subFile.c_str()); + rmdir(subDir.c_str()); + remove(destSubFile.c_str()); + rmdir(destSubDir.c_str()); + rmdir(destSrcDir.c_str()); + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CopyDirFunc_001"; +} + +/** + * @tc.name: CopyCoreTest_ExecLocal_001 + * @tc.desc: Test function of CopyCore::ExecLocal interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ExecLocal_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ExecLocal_001"; + + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = srcFile; + infos->destPath = destFile; + auto callback = make_shared(nullptr); + + auto res = CopyCore::ExecLocal(infos, callback); + EXPECT_EQ(res, ERRNO_NOERR); + EXPECT_TRUE(CopyCore::IsFile(destFile)); + int ret = remove(destFile.c_str()); + EXPECT_TRUE(ret == 0); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ExecLocal_001"; +} + +/** + * @tc.name: CopyCoreTest_ExecLocal_002 + * @tc.desc: Test function of CopyCore::ExecLocal interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ExecLocal_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ExecLocal_002"; + + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = srcFile; + infos->destPath = srcFile; + auto callback = make_shared(nullptr); + + auto res = CopyCore::ExecLocal(infos, callback); + EXPECT_EQ(res, EINVAL); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ExecLocal_002"; +} + +/** + * @tc.name: CopyCoreTest_RegisterListener_001 + * @tc.desc: Test function of CopyCore::RegisterListener interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_RegisterListener_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_RegisterListener_001"; + + auto infos = make_shared(); + auto callback = CopyCore::RegisterListener(infos); + EXPECT_NE(callback, nullptr); + + { + std::lock_guard lock(CopyCore::mutex_); + auto iter = CopyCore::callbackMap_.find(*infos); + EXPECT_NE(iter, CopyCore::callbackMap_.end()); + } + + CopyCore::UnregisterListener(infos); + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_RegisterListener_001"; +} + +/** + * @tc.name: CopyCoreTest_RegisterListener_002 + * @tc.desc: Test function of CopyCore::RegisterListener interface for the case when the info already exists in + * callbackMap_. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_RegisterListener_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_RegisterListener_002"; + + auto infos = make_shared(); + auto firstCallback = CopyCore::RegisterListener(infos); + EXPECT_NE(firstCallback, nullptr); + auto secondCallback = CopyCore::RegisterListener(infos); + EXPECT_EQ(secondCallback, nullptr); + + { + std::lock_guard lock(CopyCore::mutex_); + auto iter = CopyCore::callbackMap_.find(*infos); + EXPECT_NE(iter, CopyCore::callbackMap_.end()); + } + + CopyCore::UnregisterListener(infos); + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_RegisterListener_002"; +} + +/** + * @tc.name: CopyCoreTest_UnregisterListener_001 + * @tc.desc: Test function of CopyCore::UnregisterListener interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_UnregisterListener_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_UnregisterListener_001"; + + auto infos = make_shared(); + auto callback = CopyCore::RegisterListener(infos); + EXPECT_NE(callback, nullptr); + + CopyCore::UnregisterListener(infos); + + { + std::lock_guard lock(CopyCore::mutex_); + auto iter = CopyCore::callbackMap_.find(*infos); + EXPECT_EQ(iter, CopyCore::callbackMap_.end()); + } + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_UnregisterListener_001"; +} + +/** + * @tc.name: CopyCoreTest_UnregisterListener_002 + * @tc.desc: Test function of CopyCore::UnregisterListener interface for the case when the info is not registered in + * callbackMap_. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_UnregisterListener_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_UnregisterListener_002"; + + auto infos = make_shared(); + + CopyCore::UnregisterListener(infos); + + { + std::lock_guard lock(CopyCore::mutex_); + auto iter = CopyCore::callbackMap_.find(*infos); + EXPECT_EQ(iter, CopyCore::callbackMap_.end()); + } + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_UnregisterListener_002"; +} + +/** + * @tc.name: CopyCoreTest_DoCopy_001 + * @tc.desc: Test function of CopyCore::DoCopy interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_DoCopy_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_DoCopy_001"; + + string src = "invalid:/" + srcFile; + string dest = "invalid:/" + destFile; + optional options; + + auto res = CopyCore::DoCopy(src, dest, options); + EXPECT_FALSE(res.IsSuccess()); + auto err = res.GetError(); + EXPECT_EQ(err.GetErrNo(), E_PARAMS); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_DoCopy_001"; +} + +/** + * @tc.name: CopyCoreTest_DoCopy_002 + * @tc.desc: Test function of CopyCore::DoCopy interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_DoCopy_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_DoCopy_002"; + + string src = "file:///data/test/src/src.txt"; + string dest = "file:///data/test/dest/dest.txt"; + optional options; + + auto res = CopyCore::DoCopy(src, dest, options); + EXPECT_TRUE(res.IsSuccess()); + int ret = remove(destFile.c_str()); + EXPECT_TRUE(ret == 0); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_DoCopy_002"; +} + +/** + * @tc.name: CopyCoreTest_GetDirSize_001 + * @tc.desc: Test function of CopyCore::GetDirSize interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetDirSize_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetDirSize_001"; + + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = srcFile; + infos->destPath = destFile; + + auto res = CopyCore::GetDirSize(infos, srcDir); + EXPECT_EQ(res, 0); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetDirSize_001"; +} + +/** + * @tc.name: CopyCoreTest_GetUVEntry_001 + * @tc.desc: Test function of CopyCore::GetUVEntry interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetUVEntry_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetUVEntry_001"; + + auto infos = make_shared(); + auto res = CopyCore::GetUVEntry(infos); + EXPECT_EQ(res, nullptr); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetUVEntry_001"; +} + +/** + * @tc.name: CopyCoreTest_CheckFileValid_001 + * @tc.desc: Test function of CopyCore::CheckFileValid interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CheckFileValid_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CheckFileValid_001"; + + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = srcFile; + infos->destPath = destFile; + + auto res = CopyCore::CheckFileValid(srcFile, infos); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CheckFileValid_001"; +} + +/** + * @tc.name: CopyCoreTest_UpdateProgressSize_001 + * @tc.desc: Test function of CopyCore::UpdateProgressSize interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_UpdateProgressSize_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_UpdateProgressSize_001"; + + auto receivedInfo = make_shared(); + auto callback = make_shared(nullptr); + + auto res = CopyCore::UpdateProgressSize(srcFile, receivedInfo, callback); + EXPECT_EQ(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_UpdateProgressSize_001"; +} + +/** + * @tc.name: CopyCoreTest_GetRegisteredListener_001 + * @tc.desc: Test function of CopyCore::GetRegisteredListener interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetRegisteredListener_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetRegisteredListener_001"; + + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = srcFile; + infos->destPath = destFile; + + auto res = CopyCore::GetRegisteredListener(infos); + EXPECT_EQ(res, nullptr); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetRegisteredListener_001"; +} + +/** + * @tc.name: CopyCoreTest_SubscribeLocalListener_001 + * @tc.desc: Test function of CopyCore::SubscribeLocalListener interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_SubscribeLocalListener_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_SubscribeLocalListener_001"; + + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = srcFile; + infos->destPath = destFile; + auto callback = make_shared(nullptr); + + auto res = CopyCore::SubscribeLocalListener(infos, callback); + EXPECT_EQ(res, errno); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_SubscribeLocalListener_001"; +} + +/** + * @tc.name: CopyCoreTest_GetRealPath_001 + * @tc.desc: Test function of CopyCore::GetRealPath interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetRealPath_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetRealPath_001"; + + string path = "./data/test/src/src.txt"; + + auto res = CopyCore::GetRealPath(path); + EXPECT_EQ(res, "data/test/src/src.txt"); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetRealPath_001"; +} + +/** + * @tc.name: CopyCoreTest_GetRealPath_002 + * @tc.desc: Test function of CopyCore::GetRealPath interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetRealPath_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetRealPath_002"; + + string path = "../data/test/src/src.txt"; + + auto res = CopyCore::GetRealPath(path); + EXPECT_EQ(res, "data/test/src/src.txt"); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetRealPath_002"; +} + +/** + * @tc.name: CopyCoreTest_ExecCopy_001 + * @tc.desc: Test function of CopyCore::ExecCopy interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ExecCopy_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ExecCopy_001"; + + auto infos = make_shared(); + infos->isFile = false; + infos->srcPath = "/data/test/src"; + infos->destPath = "/data/test/dest"; + + auto res = CopyCore::ExecCopy(infos); + EXPECT_EQ(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ExecCopy_001"; +} + +/** + * @tc.name: CopyCoreTest_CopyFile_001 + * @tc.desc: Test function of CopyCore::CopyFile interface for file copy FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CopyFile_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CopyFile_001"; + + string src = "datashare:///media/src_test.jpg"; + string dest = "datashare:///media/dest_test.jpg"; + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = src; + infos->destPath = dest; + + auto res = CopyCore::CopyFile(src, dest, infos); + EXPECT_EQ(res, errno); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CopyFile_001"; +} + +/** + * @tc.name: CopyCoreTest_HandleProgress_001 + * @tc.desc: Test function of CopyCore::HandleProgress interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_HandleProgress_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_HandleProgress_001"; + + auto infos = make_shared(); + infos->isFile = false; + infos->srcPath = "/data/test/src"; + infos->destPath = "/data/test/dest"; + + auto event = make_unique(); + event->wd = EXPECTED_WD; + event->mask = IN_MODIFY; + event->len = 0; + auto [continueProcess, errCode, needSend] = CopyCore::HandleProgress(event.get(), infos, nullptr); + + EXPECT_TRUE(continueProcess); + EXPECT_EQ(errCode, EINVAL); + EXPECT_FALSE(needSend); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_HandleProgress_001"; +} + +/** + * @tc.name: CopyCoreTest_HandleProgress_002 + * @tc.desc: Test function of CopyCore::HandleProgress interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_HandleProgress_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_HandleProgress_002"; + + auto infos = make_shared(); + infos->srcPath = srcDir; + infos->destPath = destDir; + infos->isFile = true; + + auto callback = make_shared(nullptr); + + auto receiveInfo = make_shared(); + receiveInfo->path = testDir; + callback->wds.push_back({ UNEXPECTED_WD, receiveInfo }); + + auto event = make_unique(); + event->wd = EXPECTED_WD; + event->mask = IN_MODIFY; + event->len = 0; + + auto [continueProcess, errCode, needSend] = CopyCore::HandleProgress(event.get(), infos, callback); + + EXPECT_TRUE(continueProcess); + EXPECT_EQ(errCode, EINVAL); + EXPECT_FALSE(needSend); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_HandleProgress_002"; +} + +/** + * @tc.name: CopyCoreTest_HandleProgress_003 + * @tc.desc: Test function of CopyCore::HandleProgress interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_HandleProgress_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_HandleProgress_003"; + + auto infos = make_shared(); + infos->srcPath = srcDir; + infos->destPath = destDir; + infos->isFile = true; + + auto callback = make_shared(nullptr); + + auto receiveInfo = make_shared(); + receiveInfo->path = srcFile; + callback->wds.push_back({ EXPECTED_WD, receiveInfo }); + + auto event = make_unique(); + event->wd = EXPECTED_WD; + event->mask = IN_MODIFY; + event->len = 0; + + auto [continueProcess, errCode, needSend] = CopyCore::HandleProgress(event.get(), infos, callback); + + EXPECT_TRUE(continueProcess); + EXPECT_EQ(errCode, ERRNO_NOERR); + EXPECT_TRUE(needSend); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_HandleProgress_003"; +} + +/** + * @tc.name: CopyCoreTest_OnFileReceive_001 + * @tc.desc: Test function of CopyCore::OnFileReceive interface fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_OnFileReceive_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_OnFileReceive_001"; + + auto infos = make_shared(); + CopyCore::OnFileReceive(infos); + EXPECT_TRUE(CopyCore::callbackMap_.empty()); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_OnFileReceive_001"; +} + +/** + * @tc.name: CopyCoreTest_OnFileReceive_002 + * @tc.desc: Test function of CopyCore::OnFileReceive interface when listener is nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_OnFileReceive_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_OnFileReceive_002"; + + auto infos = make_shared(); + auto callback = CopyCore::RegisterListener(infos); + CopyCore::OnFileReceive(infos); + EXPECT_NE(callback, nullptr); + if (callback) { + EXPECT_EQ(callback->listener, nullptr); + } + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_OnFileReceive_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test diff --git a/interfaces/test/unittest/js/mod_fs/properties/fdatasync_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/fdatasync_core_mock_test.cpp new file mode 100644 index 000000000..c6308de50 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/fdatasync_core_mock_test.cpp @@ -0,0 +1,102 @@ +/* + * 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 +#include +#include + +#include "fdatasync_core.h" +#include "mock/uv_fs_mock.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FDataSyncCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +void FDataSyncCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void FDataSyncCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void FDataSyncCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void FDataSyncCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FDataSyncCoreMockTest_DoFDataSync_001 + * @tc.desc: Test function of FDataSyncCore::DoFDataSync interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FDataSyncCoreMockTest, FDataSyncCoreMockTest_DoFDataSync_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-begin FDataSyncCoreMockTest_DoFDataSync_001"; + + int fd = 3; + + EXPECT_CALL(*uvMock, uv_fs_fdatasync(_, _, _, _)).WillOnce(Return(1)); + auto res = FDataSyncCore::DoFDataSync(fd); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-end FDataSyncCoreMockTest_DoFDataSync_001"; +} + +/** + * @tc.name: FDataSyncCoreMockTest_DoFDataSync_002 + * @tc.desc: Test function of FDataSyncCore::DoFDataSync interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FDataSyncCoreMockTest, FDataSyncCoreMockTest_DoFDataSync_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-begin FDataSyncCoreMockTest_DoFDataSync_002"; + + int fd = 3; + + EXPECT_CALL(*uvMock, uv_fs_fdatasync(_, _, _, _)).WillOnce(Return(-1)); + auto res = FDataSyncCore::DoFDataSync(fd); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-end FDataSyncCoreMockTest_DoFDataSync_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/fdopen_stream_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/fdopen_stream_core_test.cpp new file mode 100644 index 000000000..f3be0e56b --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/fdopen_stream_core_test.cpp @@ -0,0 +1,117 @@ +/* +* 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 "fdopen_stream_core.h" + +#define FDOPEN_STREAM_FILE_PATH "/data/test/FdopenStreamCoreTest.txt" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +class FdopenStreamCoreTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + int32_t fd = open(FDOPEN_STREAM_FILE_PATH, CREATE | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + rmdir(FDOPEN_STREAM_FILE_PATH); + }; + void SetUp() {}; + void TearDown() {}; +}; + +/** +* @tc.name: DoFdopenStreamTest_0001 +* @tc.desc: Test function of DoFdopenStream() interface for success. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +*/ +HWTEST_F(FdopenStreamCoreTest, DoFdopenStreamTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin DoFdopenStreamTest_0001"; + int32_t fd = open(FDOPEN_STREAM_FILE_PATH, O_RDWR); + if (fd <= 0) { + close(fd); + ASSERT_TRUE(false); + } + + auto ret = FdopenStreamCore::DoFdopenStream(fd, "r"); + ASSERT_TRUE(ret.IsSuccess()); + + auto stream = ret.GetData().value(); + ASSERT_NE(stream, nullptr); + auto retClose = stream->Close(); + EXPECT_TRUE(retClose.IsSuccess()); + + close(fd); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end DoFdopenStreamTest_0001"; +} + +/** +* @tc.name: DoFdopenStreamTest_0002 +* @tc.desc: Test function of DoFdopenStream() interface for fd < 0. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +*/ +HWTEST_F(FdopenStreamCoreTest, DoFdopenStreamTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin DoFdopenStreamTest_0002"; + auto ret = FdopenStreamCore::DoFdopenStream(-1, "r"); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end DoFdopenStreamTest_0002"; +} + +/** +* @tc.name: DoFdopenStreamTest_0003 +* @tc.desc: Test function of DoFdopenStream() interface for fail. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +*/ +HWTEST_F(FdopenStreamCoreTest, DoFdopenStreamTest_0003, testing::ext::TestSize.Level1) +{ + int32_t fd = open(FDOPEN_STREAM_FILE_PATH, O_RDWR); + if (fd <= 0) { + close(fd); + ASSERT_TRUE(false); + } + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin DoFdopenStreamTest_0003"; + auto ret = FdopenStreamCore::DoFdopenStream(fd, "sssssss"); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + close(fd); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end DoFdopenStreamTest_0003"; +} + +} +} +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/fsync_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/fsync_core_mock_test.cpp new file mode 100644 index 000000000..688a5bca0 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/fsync_core_mock_test.cpp @@ -0,0 +1,98 @@ +/* + * 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 "fsync_core.h" +#include "uv_fs_mock.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FsyncCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvfs = nullptr; +}; + +void FsyncCoreMockTest::SetUpTestCase(void) +{ + uvfs = std::make_shared(); + Uvfs::ins = uvfs; + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsyncCoreMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvfs = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FsyncCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void FsyncCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsyncCoreMockTest_DoFsync_001 + * @tc.desc: Test function of RenameCore::DoFsync interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsyncCoreMockTest, FsyncCoreMockTest_DoFsync_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsyncCoreMockTest-begin FsyncCoreMockTest_DoFsync_001"; + + EXPECT_CALL(*uvfs, uv_fs_fsync(_, _, _, _)).WillOnce(Return(-1)); + + auto res = FsyncCore::DoFsync(1); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsyncCoreMockTest-end FsyncCoreMockTest_DoFsync_001"; +} + +/** + * @tc.name: FsyncCoreMockTest_DoFsync_002 + * @tc.desc: Test function of RenameCore::DoFsync interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsyncCoreMockTest, FsyncCoreMockTest_DoFsync_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsyncCoreMockTest-begin FsyncCoreMockTest_DoFsync_002"; + + EXPECT_CALL(*uvfs, uv_fs_fsync(_, _, _, _)).WillOnce(Return(1)); + + auto res = FsyncCore::DoFsync(1); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsyncCoreMockTest-end FsyncCoreMockTest_DoFsync_002"; +} + + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/listfile_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/listfile_core_test.cpp index dbea771a4..64387d5b4 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/listfile_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/listfile_core_test.cpp @@ -90,7 +90,6 @@ public: * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0001, testing::ext::TestSize.Level1) { @@ -117,7 +116,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0001, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0002, testing::ext::TestSize.Level1) { @@ -144,7 +142,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0002, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0003, testing::ext::TestSize.Level1) { @@ -167,7 +164,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0003, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0004, testing::ext::TestSize.Level1) { @@ -195,7 +191,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0004, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0005, testing::ext::TestSize.Level1) { @@ -223,7 +218,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0005, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0006, testing::ext::TestSize.Level1) { @@ -251,7 +245,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0006, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0007, testing::ext::TestSize.Level1) { @@ -278,7 +271,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0007, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0008, testing::ext::TestSize.Level1) { @@ -305,7 +297,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0008, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0009, testing::ext::TestSize.Level1) { @@ -330,7 +321,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0009, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0010, testing::ext::TestSize.Level1) { @@ -355,7 +345,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0010, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0011, testing::ext::TestSize.Level1) { @@ -379,7 +368,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0011, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0012, testing::ext::TestSize.Level1) { @@ -399,7 +387,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0012, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0013, testing::ext::TestSize.Level1) { @@ -422,7 +409,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0013, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0014, testing::ext::TestSize.Level1) { @@ -442,7 +428,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0014, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0015, testing::ext::TestSize.Level1) { @@ -476,7 +461,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0015, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0016, testing::ext::TestSize.Level1) { @@ -503,7 +487,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0016, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0017, testing::ext::TestSize.Level1) { diff --git a/interfaces/test/unittest/js/mod_fs/properties/lseek_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/lseek_core_test.cpp new file mode 100644 index 000000000..f5bd78bb7 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/lseek_core_test.cpp @@ -0,0 +1,115 @@ +/* + * 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 "lseek_core.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class LseekCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void LseekCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void LseekCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void LseekCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void LseekCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: LseekCoreTest_DoLseek_001 + * @tc.desc: Test function of LseekCore::DoLseek interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(LseekCoreTest, LseekCoreTest_DoLseek_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LseekCoreTest-begin LseekCoreTest_DoLseek_001"; + + int32_t fd = -1; + int64_t offset = 0; + + auto res = LseekCore::DoLseek(fd, offset); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "LseekCoreTest-end LseekCoreTest_DoLseek_001"; +} + +/** + * @tc.name: LseekCoreTest_DoLseek_002 + * @tc.desc: Test function of LseekCore::DoLseek interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(LseekCoreTest, LseekCoreTest_DoLseek_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LseekCoreTest-begin LseekCoreTest_DoLseek_002"; + + int32_t fd = 1; + int64_t offset = 0; + optional pos = std::make_optional(static_cast(-1)); + + auto res = LseekCore::DoLseek(fd, offset, pos); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "LseekCoreTest-end LseekCoreTest_DoLseek_002"; +} + +/** + * @tc.name: LseekCoreTest_DoLseek_003 + * @tc.desc: Test function of LseekCore::DoLseek interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(LseekCoreTest, LseekCoreTest_DoLseek_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LseekCoreTest-begin LseekCoreTest_DoLseek_003"; + + int32_t fd = 1; + int64_t offset = 0; + optional pos = std::make_optional(SeekPos::CURRENT); + + auto res = LseekCore::DoLseek(fd, offset, pos); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "LseekCoreTest-end LseekCoreTest_DoLseek_003"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mkdir_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/mkdir_core_mock_test.cpp index c70674062..9c5bc7789 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mkdir_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/mkdir_core_mock_test.cpp @@ -15,11 +15,11 @@ #include #include -#include #include +#include #include "mkdir_core.h" -#include "mock/uv_fs_mock.h" +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -107,7 +107,7 @@ HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0002, testing::ext::TestSi /** * @tc.name: MkdirCoreMockTest_DoMkdir_0003 - * @tc.desc: Test function of DoMkdir() interface for FAILED. + * @tc.desc: Test function of DoMkdir() interface is FAILED for uv_fs_mkdir return 1. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 @@ -127,7 +127,7 @@ HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0003, testing::ext::TestSi /** * @tc.name: MkdirCoreMockTest_DoMkdir_0004 - * @tc.desc: Test function of DoMkdir() interface for FAILED. + * @tc.desc: Test function of DoMkdir() interface is FAILED for file exists. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 @@ -152,7 +152,7 @@ HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0004, testing::ext::TestSi /** * @tc.name: MkdirCoreMockTest_DoMkdir_0005 - * @tc.desc: Test function of DoMkdir() interface for FAILED. + * @tc.desc: Test function of DoMkdir() interface is FAILED for no such file or directory. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp index 5878a9304..189edf925 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp @@ -23,7 +23,7 @@ int setxattr(const char *path, const char *name, const void *value, size_t size, return System::ins->setxattr(path, name, value, size, flags); } -int getxattr(const char *path, const char *name, void *value, size_t size) +ssize_t getxattr(const char *path, const char *name, void *value, size_t size) { return System::ins->getxattr(path, name, value, size); } diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h index 3ff74be78..dad578b09 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h @@ -29,7 +29,7 @@ public: public: virtual ~System() = default; virtual int setxattr(const char *path, const char *name, const void *value, size_t size, int flags) = 0; - virtual int getxattr(const char *path, const char *name, void *value, size_t size) = 0; + virtual ssize_t getxattr(const char *path, const char *name, void *value, size_t size) = 0; virtual int fgetxattr(int filedes, const char *name, void *value, size_t size) = 0; virtual int flock(int fd, int operation) = 0; }; @@ -37,7 +37,7 @@ public: class SystemMock : public System { public: MOCK_METHOD5(setxattr, int(const char *path, const char *name, const void *value, size_t size, int flags)); - MOCK_METHOD4(getxattr, int(const char *path, const char *name, void *value, size_t size)); + MOCK_METHOD4(getxattr, ssize_t(const char *path, const char *name, void *value, size_t size)); MOCK_METHOD4(fgetxattr, int(int filedes, const char *name, void *value, size_t size)); MOCK_METHOD2(flock, int(int fd, int operation)); }; diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp index b656a1ad1..2a13ecf69 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp @@ -13,10 +13,8 @@ * limitations under the License. */ - #include "uv_fs_mock.h" - using namespace OHOS::FileManagement::ModuleFileIO; int uv_fs_read(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t off, @@ -55,9 +53,9 @@ int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) return Uvfs::ins->uv_fs_rmdir(loop, req, path, cb); } -int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, uv_fs_cb cb) +int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb) { - return Uvfs::ins->uv_fs_symlink(loop, req, path, new_path, flags, cb); + return Uvfs::ins->uv_fs_symlink(loop, req, path, newPath, flags, cb); } int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) @@ -134,4 +132,4 @@ int uv_fs_sendfile(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, i int uv_fs_lstat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) { return Uvfs::ins->uv_fs_lstat(loop, req, path, cb); -} \ No newline at end of file +} diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h index 1fa703a0f..0bcfa87a5 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef UV_FS_READ_MOCK_H -#define UV_FS_READ_MOCK_H +#ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H #include "uv.h" @@ -25,6 +25,7 @@ namespace OHOS::FileManagement::ModuleFileIO { class Uvfs { public: static inline std::shared_ptr ins = nullptr; + public: virtual ~Uvfs() = default; virtual int uv_fs_read(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, @@ -36,7 +37,7 @@ public: virtual int uv_fs_scandir(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) = 0; virtual int uv_fs_scandir_next(uv_fs_t *req, uv_dirent_t *ent) = 0; virtual int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char* path, uv_fs_cb cb) = 0; - virtual int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, + virtual int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb) = 0; virtual int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) = 0; virtual int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb) = 0; @@ -50,9 +51,9 @@ public: virtual int uv_fs_mkdtemp(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb) = 0; virtual int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; virtual int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) = 0; - virtual int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) = 0; - virtual int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file outFd, uv_file inFd, - int64_t off, size_t len, uv_fs_cb cb) = 0; + virtual int uv_fs_fsync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; + virtual int uv_fs_sendfile(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, size_t len, + uv_fs_cb cb) = 0; virtual int uv_fs_lstat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; }; @@ -67,7 +68,7 @@ public: MOCK_METHOD5(uv_fs_scandir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb)); MOCK_METHOD2(uv_fs_scandir_next, int(uv_fs_t *req, uv_dirent_t *ent)); MOCK_METHOD4(uv_fs_rmdir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); - MOCK_METHOD6(uv_fs_symlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, + MOCK_METHOD6(uv_fs_symlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb)); MOCK_METHOD6(uv_fs_open, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_ftruncate, int(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb)); @@ -81,11 +82,11 @@ public: MOCK_METHOD4(uv_fs_mkdtemp, int(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_unlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_rename, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb)); - MOCK_METHOD4(uv_fs_fsync, int(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb)); - MOCK_METHOD7(uv_fs_sendfile, int(uv_loop_t* loop, uv_fs_t* req, uv_file outFd, uv_file inFd, - int64_t off, size_t len, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_fsync, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); + MOCK_METHOD7(uv_fs_sendfile, int(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, + size_t len, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_lstat, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); }; -} // OHOS::FileManagement::ModuleFileIO -#endif \ No newline at end of file +} // namespace OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp index 27cd72731..abee054b1 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp @@ -13,14 +13,15 @@ * limitations under the License. */ -#include "open_core.h" -#include "mock/uv_fs_mock.h" - #include #include #include #include +#include "mock/uv_fs_mock.h" +#include "open_core.h" +#include "unistd_mock.h" + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; @@ -32,6 +33,8 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); + +private: static inline shared_ptr uvMock = nullptr; }; @@ -40,6 +43,7 @@ void OpenCoreMockTest::SetUpTestCase(void) GTEST_LOG_(INFO) << "SetUpTestCase"; uvMock = std::make_shared(); Uvfs::ins = uvMock; + UnistdMock::EnableMock(); } void OpenCoreMockTest::TearDownTestCase(void) @@ -47,6 +51,7 @@ void OpenCoreMockTest::TearDownTestCase(void) GTEST_LOG_(INFO) << "TearDownTestCase"; Uvfs::ins = nullptr; uvMock = nullptr; + UnistdMock::DisableMock(); } void OpenCoreMockTest::SetUp(void) @@ -60,19 +65,19 @@ void OpenCoreMockTest::TearDown(void) } /** -* @tc.name: OpenCoreMockTest_DoOpen_001 -* @tc.desc: Test function of OpenCore::DoOpen interface for SUCCESS. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: OpenCoreMockTest_DoOpen_001 + * @tc.desc: Test function of OpenCore::DoOpen interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_001"; - string path = "/test/open_test.txt"; + string path = "/test/OpenCoreMockTest_DoOpen_001"; int32_t mode = 0; - + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(0)); auto res = OpenCore::DoOpen(path, mode); EXPECT_EQ(res.IsSuccess(), true); @@ -81,59 +86,67 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_001, testing::ext::TestSize.L } /** -* @tc.name: OpenCoreMockTest_DoOpen_002 -* @tc.desc: Test function of OpenCore::DoOpen interface for SUCCESS. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: OpenCoreMockTest_DoOpen_002 + * @tc.desc: Test function of OpenCore::DoOpen interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_002, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_002"; - string path = "file://test/open_test.txt"; + string path = "file://test/OpenCoreMockTest_DoOpen_002"; int32_t mode = 0; + auto unistdMock = UnistdMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(0)); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).WillRepeatedly(testing::Return(0)); auto res = OpenCore::DoOpen(path, mode); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); EXPECT_EQ(res.IsSuccess(), true); GTEST_LOG_(INFO) << "OpenCoreMockTest-end OpenCoreMockTest_DoOpen_002"; } /** -* @tc.name: OpenCoreMockTest_DoOpen_003 -* @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: OpenCoreMockTest_DoOpen_003 + * @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_003, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_003"; - string path = "file://test/open_test.txt"; + string path = "file://test/OpenCoreMockTest_DoOpen_003"; int32_t mode = 0; + auto unistdMock = UnistdMock::GetMock(); EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*unistdMock, access(testing::_, testing::_)).WillRepeatedly(testing::Return(0)); auto res = OpenCore::DoOpen(path, mode); + testing::Mock::VerifyAndClearExpectations(&unistdMock); EXPECT_EQ(res.IsSuccess(), false); GTEST_LOG_(INFO) << "OpenCoreMockTest-end OpenCoreMockTest_DoOpen_003"; } /** -* @tc.name: OpenCoreMockTest_DoOpen_004 -* @tc.desc: Test function of OpenCore::DoOpen interface for ERROR. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: OpenCoreMockTest_DoOpen_004 + * @tc.desc: Test function of OpenCore::DoOpen interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_004, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_004"; - string path = "/test/open_test.txt"; + string path = "/test/OpenCoreMockTest_DoOpen_004"; int32_t mode = 0; EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); diff --git a/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp index fcfc9019b..a6cad138a 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp @@ -13,10 +13,10 @@ * limitations under the License. */ -#include - #include "open_core.h" +#include + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; @@ -51,12 +51,12 @@ void OpenCoreTest::TearDown(void) } /** -* @tc.name: OpenCoreTest_DoOpen_001 -* @tc.desc: Test function of OpenCore::DoOpen interface for ERROR. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: OpenCoreTest_DoOpen_001 + * @tc.desc: Test function of OpenCore::DoOpen interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_001"; @@ -71,12 +71,12 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_001, testing::ext::TestSize.Level1) } /** -* @tc.name: OpenCoreTest_DoOpen_002 -* @tc.desc: Test function of OpenCore::DoOpen interface for ERROR. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: OpenCoreTest_DoOpen_002 + * @tc.desc: Test function of OpenCore::DoOpen interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_002, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_002"; @@ -91,12 +91,12 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_002, testing::ext::TestSize.Level1) } /** -* @tc.name: OpenCoreTest_DoOpen_003 -* @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: OpenCoreTest_DoOpen_003 + * @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_003, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_003"; @@ -111,12 +111,12 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_003, testing::ext::TestSize.Level1) } /** -* @tc.name: OpenCoreTest_DoOpen_004 -* @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: OpenCoreTest_DoOpen_004 + * @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_004, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_004"; @@ -131,12 +131,12 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_004, testing::ext::TestSize.Level1) } /** -* @tc.name: OpenCoreTest_DoOpen_005 -* @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: OpenCoreTest_DoOpen_005 + * @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_005, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_005"; @@ -151,12 +151,12 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_005, testing::ext::TestSize.Level1) } /** -* @tc.name: OpenCoreTest_DoOpen_006 -* @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: OpenCoreTest_DoOpen_006 + * @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_006, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_006"; @@ -171,12 +171,12 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_006, testing::ext::TestSize.Level1) } /** -* @tc.name: OpenCoreTest_DoOpen_007 -* @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. -* @tc.size: MEDIUM -* @tc.type: FUNC -* @tc.level Level 1 -*/ + * @tc.name: OpenCoreTest_DoOpen_007 + * @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_007, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_007"; diff --git a/interfaces/test/unittest/js/mod_fs/properties/stat_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/stat_core_mock_test.cpp index 2fd8bd7f5..2a3133bdb 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/stat_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/stat_core_mock_test.cpp @@ -13,10 +13,10 @@ * limitations under the License. */ -#include "securec.h" #include "stat_core.h" #include "uv_fs_mock.h" +#include #include namespace OHOS::FileManagement::ModuleFileIO::Test { @@ -59,7 +59,7 @@ void StatCoreMockTest::TearDown(void) /** * @tc.name: StatCoreMockTest_DoStat_001 - * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. + * @tc.desc: Test function of StatCore::DoStat interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 @@ -82,7 +82,7 @@ HWTEST_F(StatCoreMockTest, StatCoreMockTest_DoStat_001, testing::ext::TestSize.L /** * @tc.name: StatCoreMockTest_DoStat_002 - * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. + * @tc.desc: Test function of StatCore::DoStat interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 @@ -105,7 +105,7 @@ HWTEST_F(StatCoreMockTest, StatCoreMockTest_DoStat_002, testing::ext::TestSize.L /** * @tc.name: StatCoreMockTest_DoStat_003 - * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. + * @tc.desc: Test function of StatCore::DoStat interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 @@ -127,30 +127,27 @@ HWTEST_F(StatCoreMockTest, StatCoreMockTest_DoStat_003, testing::ext::TestSize.L } /** - * @tc.name: StatCoreMockTest_DoStat_005 - * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. + * @tc.name: StatCoreMockTest_DoStat_004 + * @tc.desc: Test function of StatCore::DoStat interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(StatCoreMockTest, StatCoreMockTest_DoStat_005, testing::ext::TestSize.Level1) +HWTEST_F(StatCoreMockTest, StatCoreMockTest_DoStat_004, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "StatCoreMockTest-begin StatCoreMockTest_DoStat_005"; + GTEST_LOG_(INFO) << "StatCoreMockTest-begin StatCoreMockTest_DoStat_004"; FileInfo fileinfo; - string buffer = "Hello, World!"; - int pathLength = buffer.size() + 1; - fileinfo.path = std::make_unique(pathLength); - memcpy_s(fileinfo.path.get(), pathLength, buffer.c_str(), buffer.size()); - fileinfo.fdg = std::make_unique(-1); - fileinfo.isPath = true; + fileinfo.path = std::make_unique(1); + fileinfo.fdg = std::make_unique(1); + fileinfo.isPath = false; EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); auto res = StatCore::DoStat(fileinfo); - EXPECT_EQ(res.IsSuccess(), false); + EXPECT_EQ(res.IsSuccess(), true); - GTEST_LOG_(INFO) << "StatCoreMockTest-end StatCoreMockTest_DoStat_005"; + GTEST_LOG_(INFO) << "StatCoreMockTest-end StatCoreMockTest_DoStat_004"; } } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/stat_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/stat_core_test.cpp index 5ad93fbf9..f11d92625 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/stat_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/stat_core_test.cpp @@ -52,7 +52,7 @@ void StatCoreTest::TearDown(void) /** * @tc.name: StatCoreTest_DoStat_001 - * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. + * @tc.desc: Test function of StatCore::DoStat interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 @@ -71,7 +71,7 @@ HWTEST_F(StatCoreTest, StatCoreTest_DoStat_001, testing::ext::TestSize.Level1) /** * @tc.name: StatCoreTest_DoStat_002 - * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. + * @tc.desc: Test function of StatCore::DoStat interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 diff --git a/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp new file mode 100644 index 000000000..dbda35d77 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp @@ -0,0 +1,237 @@ +/* + * 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 "trans_listener_core.h" + +#include +#include +#include + +#include "copy_core.h" +#include "unistd_mock.h" + +using namespace OHOS; +using namespace OHOS::Storage::DistributedFile; + +class MockDistributedFileDaemonManager final : public DistributedFileDaemonManager { +public: + MOCK_METHOD(int32_t, PrepareSession, + (const std::string &srcUri, const std::string &dstUri, const std::string &srcDeviceId, + const sptr &listener, HmdfsInfo &info), + (override)); + + int32_t OpenP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override + { + return 0; + } + int32_t CloseP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override + { + return 0; + } + int32_t OpenP2PConnectionEx(const std::string &networkId, sptr remoteReverseObj) override + { + return 0; + } + int32_t CloseP2PConnectionEx(const std::string &networkId) override + { + return 0; + } + int32_t CancelCopyTask(const std::string &sessionName) override + { + return 0; + } + int32_t PushAsset( + int32_t userId, const sptr &assetObj, const sptr &sendCallback) override + { + return 0; + } + int32_t RegisterAssetCallback(const sptr &recvCallback) override + { + return 0; + } + int32_t UnRegisterAssetCallback(const sptr &recvCallback) override + { + return 0; + } + int32_t GetSize(const std::string &uri, bool isSrcUri, uint64_t &size) override + { + return 0; + } + int32_t IsDirectory(const std::string &uri, bool isSrcUri, bool &isDirectory) override + { + return 0; + } + int32_t Copy(const std::string &srcUri, const std::string &destUri, ProcessCallback processCallback) override + { + return 0; + } + int32_t Cancel(const std::string &srcUri, const std::string &destUri) override + { + return 0; + } + int32_t Cancel() override + { + return 0; + } + + MockDistributedFileDaemonManager() = default; + ~MockDistributedFileDaemonManager() = default; + +public: + static std::shared_ptr GetMock(); + static void DisableMock(); + +private: + static thread_local std::shared_ptr managerMock; +}; + +thread_local std::shared_ptr MockDistributedFileDaemonManager::managerMock = nullptr; + +std::shared_ptr MockDistributedFileDaemonManager::GetMock() +{ + if (managerMock == nullptr) { + managerMock = std::make_shared(); + } + return managerMock; +} + +void MockDistributedFileDaemonManager::DisableMock() +{ + managerMock = nullptr; +} + +class MockTaskSignalListener : public OHOS::DistributedFS::ModuleTaskSignal::TaskSignalListener { +public: + MOCK_METHOD(void, OnCancel, (), (override)); +}; + +#ifdef ENABLE_DISTRIBUTED_FILE_MOCK +DistributedFileDaemonManager &DistributedFileDaemonManager::GetInstance() +{ + return *MockDistributedFileDaemonManager::GetMock(); +} +#endif + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +string g_path = "/data/test/TransListenerCoreMockTest.txt"; +const string FILE_MANAGER_AUTHORITY = "docs"; +const string MEDIA_AUTHORITY = "media"; + +class IProgressListenerTest : public IProgressListener { +public: + void InvokeListener(uint64_t progressSize, uint64_t totalSize) const override {} +}; + +class TransListenerCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void TransListenerCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + int32_t fd = open(g_path.c_str(), O_CREAT | O_RDWR, 0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + UnistdMock::EnableMock(); +} + +void TransListenerCoreMockTest::TearDownTestCase(void) +{ + rmdir(g_path.c_str()); + MockDistributedFileDaemonManager::DisableMock(); + UnistdMock::DisableMock(); + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void TransListenerCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void TransListenerCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: TransListenerCoreMockTest_PrepareCopySession_001 + * @tc.desc: Test function of TransListenerCore::PrepareCopySession interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreMockTest, TransListenerCoreMockTest_PrepareCopySession_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreMockTest-begin TransListenerCoreMockTest_PrepareCopySession_001"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = FILE_MANAGER_AUTHORITY; + info.authority = MEDIA_AUTHORITY; + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + + string disSandboxPath = "disSandboxPath"; + auto unistdMock = UnistdMock::GetMock(); + auto managerMock = MockDistributedFileDaemonManager::GetMock(); + + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*managerMock, PrepareSession(testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(ERRNO_NOERR)); + auto result = TransListenerCore::PrepareCopySession(srcUri, "destUri", nullptr, info, disSandboxPath); + EXPECT_EQ(result, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "TransListenerCoreMockTest-end TransListenerCoreMockTest_PrepareCopySession_001"; +} + +/** + * @tc.name: TransListenerCoreMockTest_CopyFileFromSoftBus_001 + * @tc.desc: Test function of TransListenerCore::CopyFileFromSoftBus interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreMockTest, TransListenerCoreMockTest_CopyFileFromSoftBus_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreMockTest-begin TransListenerCoreMockTest_CopyFileFromSoftBus_001"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = FILE_MANAGER_AUTHORITY; + + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + std::shared_ptr transListener = std::make_shared(); + std::shared_ptr infos = std::make_shared(); + auto unistdMock = UnistdMock::GetMock(); + auto managerMock = MockDistributedFileDaemonManager::GetMock(); + + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*managerMock, PrepareSession(testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(ERRNO_NOERR)); + + auto res = transListener->CopyFileFromSoftBus(srcUri, "destUri", infos, nullptr); + EXPECT_EQ(res, EIO); + + GTEST_LOG_(INFO) << "TransListenerCoreMockTest-end TransListenerCoreMockTest_CopyFileFromSoftBus_001"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test diff --git a/interfaces/test/unittest/js/mod_fs/properties/trans_listener_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_test.cpp new file mode 100644 index 000000000..5e6dca235 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_test.cpp @@ -0,0 +1,577 @@ +/* + * 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 "trans_listener_core.h" + +#include +#include + +#include "copy_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +string g_path = "/data/test/TransListenerCoreTest.txt"; +const string FILE_MANAGER_AUTHORITY = "docs"; +const string MEDIA_AUTHORITY = "media"; + +class IProgressListenerTest : public IProgressListener { +public: + void InvokeListener(uint64_t progressSize, uint64_t totalSize) const override {} +}; + +class TransListenerCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void TransListenerCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + int32_t fd = open(g_path.c_str(), O_CREAT | O_RDWR, 0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); +} + +void TransListenerCoreTest::TearDownTestCase(void) +{ + rmdir(g_path.c_str()); + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void TransListenerCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void TransListenerCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: TransListenerCoreTest_CreateDfsCopyPath_001 + * @tc.desc: Test function of TransListenerCore::CreateDfsCopyPath interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CreateDfsCopyPath_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CreateDfsCopyPath_001"; + + string result = TransListenerCore::CreateDfsCopyPath(); + EXPECT_FALSE(result.empty()); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CreateDfsCopyPath_001"; +} + +/** + * @tc.name: TransListenerCoreTest_HandleCopyFailure_001 + * @tc.desc: Test function of TransListenerCore::HandleCopyFailure interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_HandleCopyFailure_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_HandleCopyFailure_001"; + + string path = "/data/test/TransListenerCoreTest_HandleCopyFailure_001.txt"; + Storage::DistributedFile::HmdfsInfo info; + info.authority = "abc"; + CopyEvent event; + event.errorCode = 0; + int result = TransListenerCore::HandleCopyFailure(event, info, path, ""); + EXPECT_EQ(result, EIO); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_HandleCopyFailure_001"; +} + +/** + * @tc.name: TransListenerCoreTest_HandleCopyFailure_002 + * @tc.desc: Test function of TransListenerCore::HandleCopyFailure interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_HandleCopyFailure_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_HandleCopyFailure_002"; + + string path = "/data/test/TransListenerCoreTest_HandleCopyFailure_002.txt"; + int32_t fd = open(path.c_str(), O_CREAT | O_RDWR, 0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + + Storage::DistributedFile::HmdfsInfo info; + info.authority = "abc"; + CopyEvent event; + event.errorCode = SOFTBUS_TRANS_FILE_EXISTED; + int result = TransListenerCore::HandleCopyFailure(event, info, path, ""); + EXPECT_EQ(result, EEXIST); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_HandleCopyFailure_002"; +} + +/** + * @tc.name: TransListenerCoreTest_HandleCopyFailure_003 + * @tc.desc: Test function of TransListenerCore::HandleCopyFailure interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_HandleCopyFailure_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_HandleCopyFailure_003"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = MEDIA_AUTHORITY; + CopyEvent event; + event.errorCode = DFS_CANCEL_SUCCESS; + int result = TransListenerCore::HandleCopyFailure(event, info, "", ""); + EXPECT_EQ(result, ECANCELED); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_HandleCopyFailure_003"; +} + +/** + * @tc.name: TransListenerCoreTest_WaitForCopyResult_001 + * @tc.desc: Test function of TransListenerCore::WaitForCopyResult interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_WaitForCopyResult_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_WaitForCopyResult_001"; + + std::shared_ptr transListener = std::make_shared(); + transListener->copyEvent_.copyResult = FAILED; + int result = TransListenerCore::WaitForCopyResult(transListener.get()); + EXPECT_EQ(result, 2); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_WaitForCopyResult_001"; +} + +/** + * @tc.name: TransListenerCoreTest_WaitForCopyResult_002 + * @tc.desc: Test function of TransListenerCore::WaitForCopyResult interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_WaitForCopyResult_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_WaitForCopyResult_002"; + + int result = TransListenerCore::WaitForCopyResult(nullptr); + EXPECT_TRUE(result); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_WaitForCopyResult_002"; +} + +/** + * @tc.name: TransListenerCoreTest_PrepareCopySession_001 + * @tc.desc: Test function of TransListenerCore::PrepareCopySession interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_PrepareCopySession_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_PrepareCopySession_001"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = MEDIA_AUTHORITY; + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + + string disSandboxPath = "disSandboxPath"; + + int result = TransListenerCore::PrepareCopySession(srcUri, "destUri", nullptr, info, disSandboxPath); + EXPECT_EQ(result, EIO); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_PrepareCopySession_001"; +} + +/** + * @tc.name: TransListenerCoreTest_PrepareCopySession_002 + * @tc.desc: Test function of TransListenerCore::PrepareCopySession interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_PrepareCopySession_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_PrepareCopySession_002"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = "abc"; + + string disSandboxPath = "disSandboxPath"; + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + + int result = TransListenerCore::PrepareCopySession(srcUri, "destUri", nullptr, info, disSandboxPath); + EXPECT_EQ(result, ENOENT); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_PrepareCopySession_002"; +} + +/** + * @tc.name: TransListenerCoreTest_PrepareCopySession_003 + * @tc.desc: Test function of TransListenerCore::PrepareCopySession interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_PrepareCopySession_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_PrepareCopySession_003"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = "abc"; + info.sandboxPath = "abc"; + + string disSandboxPath = "disSandboxPath"; + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + + int result = TransListenerCore::PrepareCopySession(srcUri, "destUri", nullptr, info, disSandboxPath); + EXPECT_EQ(result, ENOENT); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_PrepareCopySession_003"; +} + +/** + * @tc.name: TransListenerCoreTest_PrepareCopySession_004 + * @tc.desc: Test function of TransListenerCore::PrepareCopySession interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_PrepareCopySession_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_PrepareCopySession_004"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = "abc"; + info.sandboxPath = "/data/test/PrepareCopySession_004.txt"; + + string disSandboxPath = "disSandboxPath"; + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + + int result = TransListenerCore::PrepareCopySession(srcUri, "destUri", nullptr, info, disSandboxPath); + EXPECT_EQ(result, ENOENT); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_PrepareCopySession_004"; +} + +/** + * @tc.name: TransListenerCoreTest_PrepareCopySession_005 + * @tc.desc: Test function of TransListenerCore::PrepareCopySession interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_PrepareCopySession_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_PrepareCopySession_005"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = "abc"; + info.sandboxPath = "/data/test/PrepareCopySession_004.txt"; + int32_t fd = open(info.sandboxPath.c_str(), O_CREAT | O_RDWR, 0644); + close(fd); + + string disSandboxPath = "disSandboxPath"; + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + + int result = TransListenerCore::PrepareCopySession(srcUri, "destUri", nullptr, info, disSandboxPath); + EXPECT_EQ(result, ENOENT); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_PrepareCopySession_005"; +} + +/** + * @tc.name: TransListenerCoreTest_CopyToSandBox_001 + * @tc.desc: Test function of TransListenerCore::CopyToSandBox interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CopyToSandBox_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CopyToSandBox_001"; + + string disSandboxPath = "disSandboxPath"; + string sandboxPath = "sandboxPath"; + string currentId = "currentId"; + + int result = TransListenerCore::CopyToSandBox("srcUri", disSandboxPath, sandboxPath, currentId); + EXPECT_EQ(result, EIO); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CopyToSandBox_001"; +} + +/** + * @tc.name: TransListenerCoreTest_CopyToSandBox_002 + * @tc.desc: Test function of TransListenerCore::CopyToSandBox interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CopyToSandBox_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CopyToSandBox_002"; + + string disSandboxPath = g_path; + string sandboxPath = "/data/test"; + string currentId = "currentId"; + + int result = TransListenerCore::CopyToSandBox("srcUri", disSandboxPath, sandboxPath, currentId); + EXPECT_EQ(result, EIO); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CopyToSandBox_002"; +} + +/** + * @tc.name: TransListenerCoreTest_GetFileName_001 + * @tc.desc: Test function of TransListenerCore::GetFileName interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_GetFileName_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_GetFileName_001"; + + string path = "abc"; + + auto result = TransListenerCore::GetFileName(path); + EXPECT_EQ(result, ""); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_GetFileName_001"; +} + +/** + * @tc.name: TransListenerCoreTest_GetFileName_002 + * @tc.desc: Test function of TransListenerCore::GetFileName interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_GetFileName_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_GetFileName_002"; + + auto result = TransListenerCore::GetFileName(g_path); + EXPECT_EQ(result, "/TransListenerCoreTest.txt"); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_GetFileName_002"; +} + +/** + * @tc.name: TransListenerCoreTest_GetNetworkIdFromUri_001 + * @tc.desc: Test function of TransListenerCore::GetNetworkIdFromUri interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_GetNetworkIdFromUri_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_GetNetworkIdFromUri_001"; + + string uri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + + auto result = TransListenerCore::GetNetworkIdFromUri(uri); + EXPECT_EQ(result, "AD125AD1CF"); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_GetNetworkIdFromUri_001"; +} + +/** + * @tc.name: TransListenerCoreTest_CallbackComplete_001 + * @tc.desc: Test function of TransListenerCore::CallbackComplete interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CallbackComplete_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CallbackComplete_001"; + + TransListenerCore::CallbackComplete(nullptr); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CallbackComplete_001"; +} + +/** + * @tc.name: TransListenerCoreTest_CallbackComplete_002 + * @tc.desc: Test function of TransListenerCore::CallbackComplete interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CallbackComplete_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CallbackComplete_002"; + + auto entry = make_shared(make_shared(std::make_shared())); + entry->callback = nullptr; + TransListenerCore::CallbackComplete(entry); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CallbackComplete_002"; +} + +/** + * @tc.name: TransListenerCoreTest_CallbackComplete_003 + * @tc.desc: Test function of TransListenerCore::CallbackComplete interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CallbackComplete_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CallbackComplete_003"; + + auto entry = make_shared(make_shared(std::make_shared())); + entry->callback->listener = nullptr; + TransListenerCore::CallbackComplete(entry); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CallbackComplete_003"; +} + +/** + * @tc.name: TransListenerCoreTest_CallbackComplete_004 + * @tc.desc: Test function of TransListenerCore::CallbackComplete interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CallbackComplete_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CallbackComplete_004"; + + auto entry = make_shared(make_shared(std::make_shared())); + TransListenerCore::CallbackComplete(entry); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CallbackComplete_004"; +} + +/** + * @tc.name: TransListenerCoreTest_OnFileReceive_001 + * @tc.desc: Test function of TransListenerCore::OnFileReceive interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_OnFileReceive_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_OnFileReceive_001"; + + std::shared_ptr transListener = std::make_shared(); + transListener->callback_ = nullptr; + auto res = transListener->OnFileReceive(0, 0); + EXPECT_EQ(res, ENOMEM); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_OnFileReceive_001"; +} + +/** + * @tc.name: TransListenerCoreTest_OnFileReceive_002 + * @tc.desc: Test function of TransListenerCore::OnFileReceive interface for SUCC. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_OnFileReceive_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_OnFileReceive_002"; + + std::shared_ptr transListener = std::make_shared(); + transListener->callback_ = make_shared(std::make_shared()); + auto res = transListener->OnFileReceive(0, 0); + EXPECT_EQ(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_OnFileReceive_002"; +} + +/** + * @tc.name: TransListenerCoreTest_OnFinished_001 + * @tc.desc: Test function of TransListenerCore::OnFinished interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_OnFinished_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_OnFinished_001"; + + std::shared_ptr transListener = std::make_shared(); + auto res = transListener->OnFinished("sessionName"); + EXPECT_EQ(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_OnFinished_001"; +} + +/** + * @tc.name: TransListenerCoreTest_OnFailed_001 + * @tc.desc: Test function of TransListenerCore::OnFailed interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_OnFailed_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_OnFailed_001"; + + std::shared_ptr transListener = std::make_shared(); + auto res = transListener->OnFailed("sessionName", 0); + EXPECT_EQ(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_OnFailed_001"; +} + +/** + * @tc.name: TransListenerCoreTest_CopyFileFromSoftBus_001 + * @tc.desc: Test function of TransListenerCore::CopyFileFromSoftBus interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CopyFileFromSoftBus_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CopyFileFromSoftBus_001"; + + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + std::shared_ptr transListener = std::make_shared(); + std::shared_ptr infos = std::make_shared(); + transListener->copyEvent_.copyResult = FAILED; + + auto res = transListener->CopyFileFromSoftBus(srcUri, "destUri", infos, nullptr); + EXPECT_EQ(res, EIO); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CopyFileFromSoftBus_001"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp index fcd294b5f..160a4b63b 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp @@ -15,11 +15,12 @@ #include #include -#include + #include +#include -#include "mock/uv_fs_mock.h" #include "unlink_core.h" +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; diff --git a/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp b/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp index 069a3230e..0f343b77b 100644 --- a/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp +++ b/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp @@ -45,7 +45,6 @@ public: * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(HashCoreTest, DoHashTest_0001, testing::ext::TestSize.Level1) { @@ -66,7 +65,6 @@ HWTEST_F(HashCoreTest, DoHashTest_0001, testing::ext::TestSize.Level1) * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(HashCoreTest, DoHashTest_0002, testing::ext::TestSize.Level1) { @@ -83,7 +81,6 @@ HWTEST_F(HashCoreTest, DoHashTest_0002, testing::ext::TestSize.Level1) * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(HashCoreTest, DoHashTest_0003, testing::ext::TestSize.Level1) { @@ -100,7 +97,6 @@ HWTEST_F(HashCoreTest, DoHashTest_0003, testing::ext::TestSize.Level1) * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(HashCoreTest, DoHashTest_0004, testing::ext::TestSize.Level1) { @@ -117,7 +113,6 @@ HWTEST_F(HashCoreTest, DoHashTest_0004, testing::ext::TestSize.Level1) * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(HashCoreTest, DoHashTest_0005, testing::ext::TestSize.Level1) { diff --git a/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp b/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp index f320af037..e0447335e 100644 --- a/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp +++ b/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp @@ -48,7 +48,6 @@ public: * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0001, testing::ext::TestSize.Level1) { @@ -68,7 +67,6 @@ HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0001, testing::ext::TestSize. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0002, testing::ext::TestSize.Level1) { @@ -88,7 +86,6 @@ HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0002, testing::ext::TestSize. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0003, testing::ext::TestSize.Level1) { @@ -105,7 +102,6 @@ HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0003, testing::ext::TestSize. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(SecurityLabelCoreTest, DoGetSecurityLabel_0001, testing::ext::TestSize.Level1) { @@ -125,7 +121,6 @@ HWTEST_F(SecurityLabelCoreTest, DoGetSecurityLabel_0001, testing::ext::TestSize. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(SecurityLabelCoreTest, DoGetSecurityLabel_0002, testing::ext::TestSize.Level1) { diff --git a/interfaces/test/unittest/task_signal/BUILD.gn b/interfaces/test/unittest/task_signal/BUILD.gn index 2f9900498..af6bfda21 100644 --- a/interfaces/test/unittest/task_signal/BUILD.gn +++ b/interfaces/test/unittest/task_signal/BUILD.gn @@ -33,7 +33,14 @@ ohos_unittest("task_signal_test") { external_deps = [ "c_utils:utils", + "dfs_service:distributed_file_daemon_kit_inner", + "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", ] + + defines = [ + "ENABLE_DISTRIBUTED_FILE_MOCK", + "private=public", + ] } -- Gitee From 5fcf443c3af9c61797f8047c631a7f27a0d50975 Mon Sep 17 00:00:00 2001 From: liyuke Date: Mon, 28 Jul 2025 20:44:58 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke Change-Id: I0f0c5466af05ad8fcc38048a98f7a31aa7c9b380 --- .../kits/js/src/common/ani_helper/ani_signature.cpp | 2 +- .../kits/js/src/mod_fs/ani/bind_function_class.cpp | 4 ---- .../ani/randomaccessfile_ani.cpp | 12 ++++++------ .../class_tasksignal/ani/task_signal_wrapper.cpp | 4 ++-- .../mod_fs/class_watcher/ani/fs_watcher_wrapper.cpp | 2 +- .../js/src/mod_fs/properties/ani/listfile_ani.cpp | 2 +- 6 files changed, 11 insertions(+), 15 deletions(-) diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp index 616c62f96..00537d1be 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp @@ -164,7 +164,7 @@ const string FS::ReadStreamOptionsInner::ctorSig = Builder::BuildSignatureDescri const string FS::ReadStreamOptionsInner::ctorDesc = Builder::BuildConstructorName(); // FS::WriteStreamOptionsInner const Type FS::WriteStreamOptionsInner::classType = Builder::BuildClass("@ohos.file.fs.WriteStreamOptionsInner"); -const string FS::WriteStreamOptionsInner::classDesc = FS::ReadStreamOptionsInner::classType.Descriptor(); +const string FS::WriteStreamOptionsInner::classDesc = FS::WriteStreamOptionsInner::classType.Descriptor(); const string FS::WriteStreamOptionsInner::ctorSig = Builder::BuildSignatureDescriptor({}); const string FS::WriteStreamOptionsInner::ctorDesc = Builder::BuildConstructorName(); diff --git a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index f9ebd1051..d1b34627f 100644 --- a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp @@ -21,13 +21,11 @@ #include "atomicfile_ani.h" #include "bind_function.h" #include "close_ani.h" -#include "connectdfs_ani.h" #include "copy_ani.h" #include "copy_dir_ani.h" #include "copy_file_ani.h" #include "create_randomaccessfile_ani.h" #include "create_stream_ani.h" -#include "disconnectdfs_ani.h" #include "dup_ani.h" #include "fdatasync_ani.h" #include "fdopen_stream_ani.h" @@ -186,7 +184,6 @@ static ani_status BindStaticMethods(ani_env *env) std::array methods = { ani_native_function { "closeSync", nullptr, reinterpret_cast(CloseAni::CloseSync) }, - ani_native_function { "connectDfs", nullptr, reinterpret_cast(ConnectDfsAni::ConnectDfsSync) }, ani_native_function { "copyDirSync", nullptr, reinterpret_cast(CopyDirAni::CopyDirSync) }, ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, ani_native_function { "copySync", nullptr, reinterpret_cast(CopyAni::CopySync) }, @@ -195,7 +192,6 @@ static ani_status BindStaticMethods(ani_env *env) ani_native_function { "createStreamSync", nullptr, reinterpret_cast(CreateStreamAni::CreateStreamSync) }, ani_native_function { "createWatcherSync", nullptr, reinterpret_cast(WatcherAni::CreateWatcherSync) }, - ani_native_function { "disConnectDfs", nullptr, reinterpret_cast(DisConnectDfsAni::DisConnectDfsSync) }, ani_native_function { "doAccessSync", nullptr, reinterpret_cast(AccessAni::AccessSync3) }, ani_native_function { "dup", nullptr, reinterpret_cast(DupAni::Dup) }, ani_native_function { "fdatasyncSync", nullptr, reinterpret_cast(FDataSyncAni::FDataSyncSync) }, diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp index a220f84ba..953755eb2 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp @@ -285,15 +285,15 @@ static ani_string GetFilePath(ani_env *env, const int fd) static ani_object CreateReadStreamOptions(ani_env *env, int64_t start, int64_t end) { - static const char *className = FS::ReadStreamOptionsInner::classType; + static const char *className = FS::ReadStreamOptionsInner::classDesc.c_str(); ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { HILOGE("Cannot find class %{public}s", className); return nullptr; } ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, FS::ReadStreamOptionsInner::ctorDesc, - FS::ReadStreamOptionsInner::ctorSig, &ctor)) { + if (ANI_OK != env->Class_FindMethod(cls, FS::ReadStreamOptionsInner::ctorDesc.c_str(), + FS::ReadStreamOptionsInner::ctorSig.c_str(), &ctor)) { HILOGE("Cannot find constructor method for class %{public}s", className); return nullptr; } @@ -329,15 +329,15 @@ static ani_object CreateReadStreamOptions(ani_env *env, int64_t start, int64_t e static ani_object CreateWriteStreamOptions(ani_env *env, int64_t start, int flags) { - static const char *className = FS::WriteStreamOptionsInner::classType; + static const char *className = FS::WriteStreamOptionsInner::classDesc.c_str(); ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { HILOGE("Cannot find class %{public}s", className); return nullptr; } ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, FS::WriteStreamOptionsInner::ctorDesc, - FS::WriteStreamOptionsInner::ctorSig, &ctor)) { + if (ANI_OK != env->Class_FindMethod(cls, FS::WriteStreamOptionsInner::ctorDesc.c_str(), + FS::WriteStreamOptionsInner::ctorSig.c_str(), &ctor)) { HILOGE("Cannot find constructor method for class %{public}s", className); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp index cc3085418..79be64231 100644 --- a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp +++ b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp @@ -32,7 +32,7 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; FsTaskSignal *TaskSignalWrapper::Unwrap(ani_env *env, ani_object object) { ani_long nativePtr; - auto status = env->Object_GetFieldByName_Long(object, FS::TaskSignal::nativeTaskSignal, &nativePtr); + auto status = env->Object_GetFieldByName_Long(object, "nativeTaskSignal", &nativePtr); if (status != ANI_OK) { HILOGE("Unwrap taskSignal obj failed! status: %{public}d", status); return nullptr; @@ -56,7 +56,7 @@ bool TaskSignalWrapper::Wrap(ani_env *env, ani_object object, const FsTaskSignal ani_long ptr = static_cast(reinterpret_cast(signal)); - auto status = env->Object_SetFieldByName_Long(object, FS::TaskSignal::nativeTaskSignal, ptr); + auto status = env->Object_SetFieldByName_Long(object, "nativeTaskSignal", ptr); if (status != ANI_OK) { HILOGE("Wrap taskSignal obj failed! status: %{public}d", status); return false; diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/ani/fs_watcher_wrapper.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/ani/fs_watcher_wrapper.cpp index 51ea2e2d3..f769d533a 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/ani/fs_watcher_wrapper.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/ani/fs_watcher_wrapper.cpp @@ -29,7 +29,7 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; FsWatcher *FsWatcherWrapper::Unwrap(ani_env *env, ani_object object) { ani_long nativePtr; - auto ret = env->Object_GetFieldByName_Long(object, FS::WatcherInner::nativePtr, &nativePtr); + auto ret = env->Object_GetFieldByName_Long(object, "nativePtr", &nativePtr); if (ret != ANI_OK) { HILOGE("Unwrap fsWatcher err: %{public}d", ret); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp index d3bbe159e..16339ea59 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp @@ -87,7 +87,7 @@ tuple> ParseDoubleParam(ani_env *env, ani_object obj, str ani_double resultRefRes; if (ANI_OK != env->Object_CallMethodByName_Double( - static_cast(resultRef), BasicTypesConverter::toDouble, nullptr, &resultRefRes)) { + static_cast(resultRef), BasicTypesConverter::toDouble.c_str(), nullptr, &resultRefRes)) { return { false, nullopt }; } double result = static_cast(resultRefRes); -- Gitee