diff --git a/services/storage_daemon/mtpfs/test/BUILD.gn b/services/storage_daemon/mtpfs/test/BUILD.gn index 541f2b8c9773dae64ab915c5a5ba710025bfeec4..6edd307f1b3923162ac437b87417e19e8aa80d21 100644 --- a/services/storage_daemon/mtpfs/test/BUILD.gn +++ b/services/storage_daemon/mtpfs/test/BUILD.gn @@ -79,7 +79,201 @@ ohos_unittest("mtpfs_fuse_test") { install_enable = true } +ohos_unittest("mtpfs_libmtp_test") { + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + cfi = true + cfi_cross_dso = true + debug = false + } + module_out_path = "storage_service/storage_daemon" + + defines = [ + "STORAGE_LOG_TAG = \"StorageDaemon\"", + "private = public", + ] + + include_dirs = [ + "${storage_daemon_path}/mtpfs/include", + "${storage_daemon_path}/include/utils", + "${storage_service_common_path}/include", + ] + + cflags = [ + "-w", + "-DFUSE_USE_VERSION=31", + "-D_FILE_OFFSET_BITS=64", + "-std=c++11", + ] + + deps = [ "${storage_daemon_path}:storage_common_utils" ] + + external_deps = [ + "c_utils:utils", + "googletest:gtest_main", + "hilog:libhilog", + ] + + if (support_open_source_libmtp) { + sources = [ + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_fuse.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_libmtp.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_mtp_device.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_tmp_files_pool.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_type_dir.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_type_file.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_type_tmp_file.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_util.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/test/mtpfs_libmtp_test.cpp", + ] + external_deps += [ + "libfuse:libfuse", + "libmtp:libmtp", + "libusb:libusb", + "openssl:libcrypto_shared", + ] + } else { + sources = [ "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_main_virtual.cpp" ] + } + + subsystem_name = "filemanagement" + part_name = "storage_service" + install_enable = true +} + +ohos_unittest("mtpfs_mtp_device_test") { + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + cfi = true + cfi_cross_dso = true + debug = false + } + module_out_path = "storage_service/storage_daemon" + + defines = [ + "STORAGE_LOG_TAG = \"StorageDaemon\"", + "private = public", + ] + + include_dirs = [ + "${storage_daemon_path}/mtpfs/include", + "${storage_daemon_path}/include/utils", + "${storage_service_common_path}/include", + ] + + cflags = [ + "-w", + "-DFUSE_USE_VERSION=31", + "-D_FILE_OFFSET_BITS=64", + "-std=c++11", + ] + + deps = [ "${storage_daemon_path}:storage_common_utils" ] + + external_deps = [ + "c_utils:utils", + "googletest:gtest_main", + "hilog:libhilog", + ] + + if (support_open_source_libmtp) { + sources = [ + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_fuse.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_libmtp.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_mtp_device.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_tmp_files_pool.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_type_dir.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_type_file.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_type_tmp_file.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_util.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/test/mtpfs_mtp_device_test.cpp", + ] + external_deps += [ + "libfuse:libfuse", + "libmtp:libmtp", + "libusb:libusb", + "openssl:libcrypto_shared", + ] + } else { + sources = [ "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_main_virtual.cpp" ] + } + + subsystem_name = "filemanagement" + part_name = "storage_service" + install_enable = true +} + +ohos_unittest("mtpfs_tmp_files_pool_test") { + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + cfi = true + cfi_cross_dso = true + debug = false + } + module_out_path = "storage_service/storage_daemon" + + defines = [ + "STORAGE_LOG_TAG = \"StorageDaemon\"", + "private = public", + ] + + include_dirs = [ + "${storage_daemon_path}/mtpfs/include", + "${storage_daemon_path}/include/utils", + "${storage_service_common_path}/include", + ] + + cflags = [ + "-w", + "-DFUSE_USE_VERSION=31", + "-D_FILE_OFFSET_BITS=64", + "-std=c++11", + ] + + deps = [ "${storage_daemon_path}:storage_common_utils" ] + + external_deps = [ + "c_utils:utils", + "googletest:gtest_main", + "hilog:libhilog", + ] + + if (support_open_source_libmtp) { + sources = [ + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_fuse.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_libmtp.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_mtp_device.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_tmp_files_pool.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_type_dir.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_type_file.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_type_tmp_file.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_util.cpp", + "$ROOT_DIR/storage_daemon/mtpfs/test/mtpfs_tmp_files_pool_test.cpp", + ] + external_deps += [ + "libfuse:libfuse", + "libmtp:libmtp", + "libusb:libusb", + "openssl:libcrypto_shared", + ] + } else { + sources = [ "$ROOT_DIR/storage_daemon/mtpfs/src/mtpfs_main_virtual.cpp" ] + } + + subsystem_name = "filemanagement" + part_name = "storage_service" + install_enable = true +} + group("storage_daemon_mtpfs_test") { testonly = true - deps = [ ":mtpfs_fuse_test" ] + deps = [ + ":mtpfs_fuse_test", + ":mtpfs_libmtp_test", + ":mtpfs_mtp_device_test", + ":mtpfs_tmp_files_pool_test", + ] } diff --git a/services/storage_daemon/mtpfs/test/mtpfs_libmtp_test.cpp b/services/storage_daemon/mtpfs/test/mtpfs_libmtp_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41adea565cf2539e93b6928d3810c9a61f13ea08 --- /dev/null +++ b/services/storage_daemon/mtpfs/test/mtpfs_libmtp_test.cpp @@ -0,0 +1,49 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include +#include +#include +#include "mtpfs_libmtp.h" +#include +#include +#include +#include "mtpfs_util.h" +#include "storage_service_log.h" + +using namespace std; +using namespace testing::ext; +using namespace testing; + +class MtpfsLibmtpTest : public testing::Test { +public: + static void SetUpTestCase(void){}; + static void TearDownTestCase(void){}; + void SetUp(){}; + void TearDown(){}; +}; + +/** + * @tc.name: Mtpfs_LIBMTPFreeFilesAndFolders_001 + * @tc.desc: Verify the LIBMTPFreeFilesAndFolders function. + * @tc.type: FUNC + */ +HWTEST_F(MtpfsLibmtpTest, MtpfsLibmtpTest_LIBMTPFreeFilesAndFolders_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MtpfsLibmtpTest_LIBMTPFreeFilesAndFolders_001 start"; + LIBMTP_file_t** file = nullptr; + LIBMTPFreeFilesAndFolders(file); + EXPECT_EQ(file, nullptr); + GTEST_LOG_(INFO) << "MtpfsLibmtpTest_LIBMTPFreeFilesAndFolders_001 end"; +} \ No newline at end of file diff --git a/services/storage_daemon/mtpfs/test/mtpfs_mtp_device_test.cpp b/services/storage_daemon/mtpfs/test/mtpfs_mtp_device_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fc946fa896817e062ba88355874e97b7ee93e4b --- /dev/null +++ b/services/storage_daemon/mtpfs/test/mtpfs_mtp_device_test.cpp @@ -0,0 +1,166 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. + */ +#include +#include +#include +#include +#include +#include +#include +#include "mtpfs_mtp_device.h" +#include "mtpfs_libmtp.h" +#include "mtpfs_util.h" +#include "mtpfs_fuse.h" +#include "storage_service_log.h" + +LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device_Uncached(LIBMTP_raw_device_t *rawdevice) +{ + if (rawdevice == nullptr) { + return nullptr; + } +} + +namespace OHOS { +namespace StorageDaemon { +using namespace std; +using namespace testing::ext; +using namespace testing; + +class MtpfsDeviceTest : public testing::Test { +public: + static void SetUpTestCase(void){}; + static void TearDownTestCase(void){}; + void SetUp(){}; + void TearDown(){}; +}; + +/** + * @tc.name: Mtpfs_Connect_001 + * @tc.desc: Verify the Connect function. + * @tc.type: FUNC + */ +HWTEST_F(MtpfsDeviceTest, MtpfsDeviceTest_Connect_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MtpfsDeviceTest_Connect_001 start"; + LIBMTP_raw_device_t *dev = nullptr; + auto mtpfsdevice = std::make_shared(); + bool result = mtpfsdevice->Connect(dev); + EXPECT_EQ(result, false); + GTEST_LOG_(INFO) << "MtpfsDeviceTest_Connect_001 end"; +} + +/** + * @tc.name: Mtpfs_ConvertErrorCode_001 + * @tc.desc: Verify the ConvertErrorCode function. + * @tc.type: FUNC + */ +HWTEST_F(MtpfsDeviceTest, MtpfsDeviceTest_ConvertErrorCode_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MtpfsDeviceTest_ConvertErrorCode_001 start"; + auto mtpfsdevice = std::make_shared(); + LIBMTP_error_number_t err = LIBMTP_ERROR_NONE; + bool result = mtpfsdevice->ConvertErrorCode(err); + EXPECT_EQ(result, true); + + LIBMTP_error_number_t err1 = LIBMTP_ERROR_NO_DEVICE_ATTACHED; + bool result1 = mtpfsdevice->ConvertErrorCode(err1); + EXPECT_EQ(result1, false); + + LIBMTP_error_number_t err2 = LIBMTP_ERROR_CONNECTING; + bool result2 = mtpfsdevice->ConvertErrorCode(err2); + EXPECT_EQ(result2, false); + + LIBMTP_error_number_t err3 = LIBMTP_ERROR_MEMORY_ALLOCATION; + bool result3 = mtpfsdevice->ConvertErrorCode(err3); + EXPECT_EQ(result3, false); + + LIBMTP_error_number_t err4 = LIBMTP_ERROR_GENERAL; + bool result4 = mtpfsdevice->ConvertErrorCode(err4); + EXPECT_EQ(result4, false); + + LIBMTP_error_number_t err5 = LIBMTP_ERROR_USB_LAYER; + bool result5 = mtpfsdevice->ConvertErrorCode(err5); + EXPECT_EQ(result5, false); + + GTEST_LOG_(INFO) << "MtpfsDeviceTest_ConvertErrorCode_001 end"; +} + +/** + * @tc.name: Mtpfs_ConnectByDevNo_001 + * @tc.desc: Verify the Connect function. + * @tc.type: FUNC + */ +HWTEST_F(MtpfsDeviceTest, MtpfsDeviceTest_ConnectByDevNo_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MtpfsDeviceTest_ConnectByDevNo_001 start"; + int devNo = 3; + int rawDevicesCnt = 3; + LIBMTP_raw_device_t *rawDevices = nullptr; + auto mtpfsdevice = std::make_shared(); + bool result = mtpfsdevice->ConnectByDevNo(devNo); + EXPECT_EQ(result, false); + GTEST_LOG_(INFO) << "MtpfsDeviceTest_ConnectByDevNo_001 end"; +} + + +/** + * @tc.name: Mtpfs_ConnectByDevFile_001 + * @tc.desc: Verify the ConnectByDevFile function. + * @tc.type: FUNC + */ +HWTEST_F(MtpfsDeviceTest, MtpfsDeviceTest_ConnectByDevFile_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MtpfsDeviceTest_ConnectByDevFile_001 start"; + const std::string &devFile = "test"; + uint8_t bnum = 0; + uint8_t dnum = 0; + auto mtpfsdevice = std::make_shared(); + bool result = mtpfsdevice->ConnectByDevFile(devFile); + EXPECT_EQ(result, false); + GTEST_LOG_(INFO) << "MtpfsDeviceTest_ConnectByDevFile_001 end"; +} + +/** + * @tc.name: Mtpfs_Disconnect_001 + * @tc.desc: Verify the Disconnect function. + * @tc.type: FUNC + */ +HWTEST_F(MtpfsDeviceTest, MtpfsDeviceTest_Disconnect_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MtpfsDeviceTest_Disconnect_001 start"; + LIBMTP_mtpdevice_t *device_ = nullptr; + auto mtpfsdevice = std::make_shared(); + mtpfsdevice->Disconnect(); + EXPECT_EQ(device_, nullptr); + GTEST_LOG_(INFO) << "MtpfsDeviceTest_Disconnect_001 end"; +} + +/** + * @tc.name: Mtpfs_HandleDir_001 + * @tc.desc: Verify the HandleDir function. + * @tc.type: FUNC + */ +HWTEST_F(MtpfsDeviceTest, MtpfsDeviceTest_HandleDir_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MtpfsDeviceTest_HandleDir_001 start"; + LIBMTP_file_t *content = nullptr; + MtpFsTypeDir *dir = nullptr; + auto mtpfsdevice = std::make_shared(); + mtpfsdevice->HandleDir(content, dir); + EXPECT_EQ(content, nullptr); + GTEST_LOG_(INFO) << "MtpfsDeviceTest_HandleDir_001 end"; +} +} +} diff --git a/services/storage_daemon/mtpfs/test/mtpfs_tmp_files_pool_test.cpp b/services/storage_daemon/mtpfs/test/mtpfs_tmp_files_pool_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea621f66e8a15ea2ea8685ad2d3713fabe1f25a8 --- /dev/null +++ b/services/storage_daemon/mtpfs/test/mtpfs_tmp_files_pool_test.cpp @@ -0,0 +1,73 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "mtpfs_tmp_files_pool.h" +#include "mtpfs_util.h" +#include "storage_service_log.h" + +namespace OHOS { +namespace StorageDaemon { +using namespace std; +using namespace testing::ext; +using namespace testing; + +class MtpfsTmpFilesPoolTest : public testing::Test { +public: + static void SetUpTestCase(void){}; + static void TearDownTestCase(void){}; + void SetUp(){}; + void TearDown(){}; +}; + +/** + * @tc.name: Mtpfs_CreateTmpDir_001 + * @tc.desc: Verify the CreateTmpDir function. + * @tc.type: FUNC + */ +HWTEST_F(MtpfsTmpFilesPoolTest, MtpfsTmpFilesPoolTest_CreateTmpDir_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MtpfsTmpFilesPoolTest_CreateTmpDir_001 start"; + auto mtptmpfilespool = std::make_shared(); + mtptmpfilespool->tmpDir_ = ""; + bool result = mtptmpfilespool->CreateTmpDir(); + EXPECT_EQ(result, false); + GTEST_LOG_(INFO) << "MtpfsTmpFilesPoolTest_CreateTmpDir_001 end"; +} + +/** + * @tc.name: Mtpfs_RemoveTmpDir_001 + * @tc.desc: Verify the RemoveTmpDir function. + * @tc.type: FUNC + */ +HWTEST_F(MtpfsTmpFilesPoolTest, MtpfsTmpFilesPoolTest_RemoveTmpDir_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MtpfsTmpFilesPoolTest_RemoveTmpDir_001 start"; + auto mtptmpfilespool = std::make_shared(); + mtptmpfilespool->tmpDir_ = ""; + bool result = mtptmpfilespool->RemoveTmpDir(); + EXPECT_EQ(result, false); + GTEST_LOG_(INFO) << "MtpfsTmpFilesPoolTest_RemoveTmpDir_001 end"; +} +} +} \ No newline at end of file