diff --git a/services/storage_daemon/crypto/test/recover_manager_test/BUILD.gn b/services/storage_daemon/crypto/test/recover_manager_test/BUILD.gn index 350a9b406391ef4a49a469e85d61b15821403ec5..16c696434e096770a68809f93d740a6d12c219ac 100644 --- a/services/storage_daemon/crypto/test/recover_manager_test/BUILD.gn +++ b/services/storage_daemon/crypto/test/recover_manager_test/BUILD.gn @@ -45,16 +45,13 @@ ohos_unittest("RecoveryManagerTest") { ] deps = [ - "//third_party/googletest:gmock_main", - "//third_party/googletest:gtest_main", - ] - - deps += [ "${storage_daemon_path}/crypto:libsdcrypto", "${storage_daemon_path}/libfscrypt:libfscryptutils", ] external_deps = [ + "googletest:gmock_main", + "googletest:gtest_main", "huks:libhukssdk", "openssl:libcrypto_shared", ] diff --git a/services/storage_manager/ipc/test/BUILD.gn b/services/storage_manager/ipc/test/BUILD.gn index 4fe5a1a13a09e7c6874829bf8c09222b1136348b..681c4d420f66a778d585736e2c583104e65d7ded 100644 --- a/services/storage_manager/ipc/test/BUILD.gn +++ b/services/storage_manager/ipc/test/BUILD.gn @@ -50,11 +50,6 @@ ohos_unittest("storage_manager_proxy_test") { "LOG_DOMAIN = 0xD004300", ] - deps = [ - "//third_party/googletest:gmock_main", - "//third_party/googletest:gtest_main", - ] - external_deps = [ "ability_base:base", "ability_base:want", @@ -62,6 +57,8 @@ ohos_unittest("storage_manager_proxy_test") { "app_file_service:fileshare_native", "c_utils:utils", "common_event_service:cesfwk_innerkits", + "googletest:gmock_main", + "googletest:gtest_main", "hilog:libhilog", "hitrace:hitrace_meter", "ipc:ipc_single", @@ -114,11 +111,6 @@ ohos_unittest("storage_manager_stub_test") { "${storage_manager_path}/utils/src/storage_utils.cpp", ] - deps = [ - "//third_party/googletest:gmock_main", - "//third_party/googletest:gtest_main", - ] - external_deps = [ "ability_base:base", "ability_base:want", @@ -128,6 +120,8 @@ ohos_unittest("storage_manager_stub_test") { "access_token:libtoken_setproc", "app_file_service:fileshare_native", "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", "hilog:libhilog", "hitrace:hitrace_meter", "huks:libhukssdk", @@ -174,9 +168,61 @@ ohos_unittest("storage_manager_stub_nonpermission_test") { "${storage_manager_path}/utils/src/storage_utils.cpp", ] - deps = [ - "//third_party/googletest:gmock_main", - "//third_party/googletest:gtest_main", + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "access_token:libaccesstoken_sdk", + "app_file_service:fileshare_native", + "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "hitrace:hitrace_meter", + "huks:libhukssdk", + "init:libbegetutil", + "ipc:ipc_single", + "ipc:libdbinder", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +ohos_unittest("storage_manager_stub_mockipc_test") { + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + cfi = true + cfi_cross_dso = true + debug = false + blocklist = "${storage_service_path}/cfi_blocklist.txt" + } + module_out_path = "storage_service/storage_manager" + + defines = [ + "STORAGE_LOG_TAG = \"StorageManager\"", + "LOG_DOMAIN = 0xD004300", + ] + + include_dirs = [ + "${storage_manager_path}/client/test", + "${storage_interface_path}/innerkits/storage_manager/native", + "${storage_manager_path}/include", + "${storage_manager_path}/ipc/test/", + "${storage_service_common_path}/include", + ] + + sources = [ + "${storage_manager_path}/innerkits_impl/src/bundle_stats.cpp", + "${storage_manager_path}/innerkits_impl/src/disk.cpp", + "${storage_manager_path}/innerkits_impl/src/storage_manager_proxy.cpp", + "${storage_manager_path}/innerkits_impl/src/storage_stats.cpp", + "${storage_manager_path}/innerkits_impl/src/volume_core.cpp", + "${storage_manager_path}/innerkits_impl/src/volume_external.cpp", + "${storage_manager_path}/ipc/src/storage_manager_stub.cpp", + "${storage_manager_path}/ipc/test/ipc_skeleton_mock.cpp", + "${storage_manager_path}/ipc/test/storage_manager_stub_mockipc_test.cpp", + "${storage_manager_path}/utils/src/storage_utils.cpp", ] external_deps = [ @@ -186,6 +232,8 @@ ohos_unittest("storage_manager_stub_nonpermission_test") { "access_token:libaccesstoken_sdk", "app_file_service:fileshare_native", "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", "hilog:libhilog", "hitrace:hitrace_meter", "huks:libhukssdk", @@ -201,6 +249,7 @@ group("storage_manager_ipc_test") { testonly = true deps = [ ":storage_manager_proxy_test", + ":storage_manager_stub_mockipc_test", ":storage_manager_stub_nonpermission_test", ":storage_manager_stub_test", ] diff --git a/services/storage_manager/ipc/test/ipc_skeleton_mock.cpp b/services/storage_manager/ipc/test/ipc_skeleton_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5079bf4081705885aec5d8c7435b086632f346e4 --- /dev/null +++ b/services/storage_manager/ipc/test/ipc_skeleton_mock.cpp @@ -0,0 +1,36 @@ +/* + * 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 "ipc_skeleton_mock.h" + +using namespace std; +using namespace OHOS; +using namespace OHOS::StorageManager; + +IPCSkeleton &IPCSkeleton::GetInstance() +{ + static IPCSkeleton skeleton; + return skeleton; +} + +pid_t IPCSkeleton::GetCallingUid() +{ + return IIPCSkeletonMoc::ipcSkeletonMoc->GetCallingUid(); +} + +uint32_t IPCSkeleton::GetCallingTokenID() +{ + return IIPCSkeletonMoc::ipcSkeletonMoc->GetCallingTokenID(); +} diff --git a/services/storage_manager/ipc/test/ipc_skeleton_mock.h b/services/storage_manager/ipc/test/ipc_skeleton_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..0a41225359ade77cea9a5718f3598c7f642f1a00 --- /dev/null +++ b/services/storage_manager/ipc/test/ipc_skeleton_mock.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MOCK_STORAGE_MANAGER_IPC_SKELETON_H +#define MOCK_STORAGE_MANAGER_IPC_SKELETON_H + +#include + +#include "ipc_skeleton.h" + +namespace OHOS { +namespace StorageManager { +class IIPCSkeletonMoc { +public: + virtual ~IIPCSkeletonMoc() = default; +public: + virtual pid_t GetCallingUid() = 0; + virtual uint32_t GetCallingTokenID() = 0; +public: + static inline std::shared_ptr ipcSkeletonMoc = nullptr; +}; + +class IPCSkeletonMoc : public IIPCSkeletonMoc { +public: + MOCK_METHOD0(GetCallingUid, pid_t()); + MOCK_METHOD0(GetCallingTokenID, uint32_t()); +}; +} +} +#endif \ No newline at end of file diff --git a/services/storage_manager/ipc/test/storage_manager_stub_mockipc_test.cpp b/services/storage_manager/ipc/test/storage_manager_stub_mockipc_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ab27ae80cfe499b2e08a8bd0ccec66472a577094 --- /dev/null +++ b/services/storage_manager/ipc/test/storage_manager_stub_mockipc_test.cpp @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "gtest/gtest.h" +#include "gmock/gmock.h" + +#include "ipc_skeleton_mock.h" +#include "storage_manager_ipc_interface_code.h" +#include "storage_manager_proxy.h" +#include "ipc/storage_manager_stub.h" +#include "storage_manager_stub_mock.h" + +#include "storage_service_errno.h" +#include "storage_service_log.h" + +namespace OHOS { +namespace StorageManager { +using namespace std; +using namespace testing::ext; +constexpr pid_t BACKUP_SA_UID = 1089; +constexpr pid_t DFS_UID = 1009; + +class StorageManagerStubTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp() {}; + void TearDown() {}; + static inline shared_ptr ipcSkeletonMoc_ = nullptr; +}; + +void StorageManagerStubTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase Start"; + ipcSkeletonMoc_ = make_shared(); + IPCSkeletonMoc::ipcSkeletonMoc = ipcSkeletonMoc_; +} + +void StorageManagerStubTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase Start"; + IPCSkeletonMoc::ipcSkeletonMoc = nullptr; + ipcSkeletonMoc_ = nullptr; +} + +/** + * @tc.name: Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001 + * @tc.desc: Verify the OnRemoteRequest function. + * @tc.type: FUNC + * @tc.require: IB3B3O + */ +HWTEST_F(StorageManagerStubTest, Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001 start"; + + StorageManagerStubMock mock; + + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + bool bRet = data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor()); + EXPECT_TRUE(bRet) << "write token error"; + int32_t code = static_cast(StorageManagerInterfaceCode::GET_BUNDLE_STATS_INCREASE); + + EXPECT_CALL(*ipcSkeletonMoc_, GetCallingUid()).WillOnce(testing::Return(BACKUP_SA_UID)); + EXPECT_CALL(mock, GetBundleStatsForIncrease(testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(E_OK)); + int32_t ret = mock.OnRemoteRequest(code, data, reply, option); + EXPECT_EQ(ret, E_OK); + GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001 end"; +} + +/** + * @tc.name: Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002 + * @tc.desc: Verify the OnRemoteRequest function. + * @tc.type: FUNC + * @tc.require: IB3B3O + */ +HWTEST_F(StorageManagerStubTest, Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002 start"; + + StorageManagerStubMock mock; + + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + bool bRet = data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor()); + EXPECT_TRUE(bRet) << "write token error"; + int32_t code = static_cast(StorageManagerInterfaceCode::GET_USER_STATS_BY_TYPE); + + EXPECT_CALL(*ipcSkeletonMoc_, GetCallingUid()).WillOnce(testing::Return(BACKUP_SA_UID)); + EXPECT_CALL(mock, GetUserStorageStatsByType(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(E_OK)); + int32_t ret = mock.OnRemoteRequest(code, data, reply, option); + EXPECT_EQ(ret, E_OK); + GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002 end"; +} + +/** + * @tc.name: Storage_Manager_StorageManagerStubTest_OnRemoteRequest_003 + * @tc.desc: Verify the OnRemoteRequest function. + * @tc.type: FUNC + * @tc.require: IB3B3O + */ +HWTEST_F(StorageManagerStubTest, Storage_Manager_StorageManagerStubTest_OnRemoteRequest_003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_003 start"; + + StorageManagerStubMock mock; + + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + bool bRet = data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor()); + EXPECT_TRUE(bRet) << "write token error"; + int32_t code = static_cast(StorageManagerInterfaceCode::UPDATE_MEM_PARA); + + EXPECT_CALL(*ipcSkeletonMoc_, GetCallingUid()).WillOnce(testing::Return(BACKUP_SA_UID)); + EXPECT_CALL(mock, UpdateMemoryPara(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); + int32_t ret = mock.OnRemoteRequest(code, data, reply, option); + EXPECT_EQ(ret, E_OK); + GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_003 end"; +} + +/** + * @tc.name: Storage_Manager_StorageManagerStubTest_OnRemoteRequest_004 + * @tc.desc: Verify the OnRemoteRequest function. + * @tc.type: FUNC + * @tc.require: IB3B3O + */ +HWTEST_F(StorageManagerStubTest, Storage_Manager_StorageManagerStubTest_OnRemoteRequest_004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_004 start"; + + StorageManagerStubMock mock; + + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + bool bRet = data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor()); + EXPECT_TRUE(bRet) << "write token error"; + int32_t code = static_cast(StorageManagerInterfaceCode::MOUNT_DFS_DOCS); + + EXPECT_CALL(*ipcSkeletonMoc_, GetCallingUid()).WillOnce(testing::Return(DFS_UID)); + EXPECT_CALL(mock, MountDfsDocs(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(E_OK)); + int32_t ret = mock.OnRemoteRequest(code, data, reply, option); + EXPECT_EQ(ret, E_OK); + GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_004 end"; +} + +/** + * @tc.name: Storage_Manager_StorageManagerStubTest_OnRemoteRequest_005 + * @tc.desc: Verify the OnRemoteRequest function. + * @tc.type: FUNC + * @tc.require: IB3B3O + */ +HWTEST_F(StorageManagerStubTest, Storage_Manager_StorageManagerStubTest_OnRemoteRequest_005, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_005 start"; + + StorageManagerStubMock mock; + + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + bool bRet = data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor()); + EXPECT_TRUE(bRet) << "write token error"; + int32_t code = static_cast(StorageManagerInterfaceCode::UMOUNT_DFS_DOCS); + + EXPECT_CALL(*ipcSkeletonMoc_, GetCallingUid()).WillOnce(testing::Return(DFS_UID)); + EXPECT_CALL(mock, UMountDfsDocs(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(E_OK)); + int32_t ret = mock.OnRemoteRequest(code, data, reply, option); + EXPECT_EQ(ret, E_OK); + GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_005 end"; +} +} // STORAGE_MANAGER +} // OHOS diff --git a/services/storage_manager/ipc/test/storage_manager_stub_noper_test.cpp b/services/storage_manager/ipc/test/storage_manager_stub_noper_test.cpp index b888c1ad5cfe1f1c2e3ed52aef5a95fd80e782f9..ee9eeeb299ae4839436b4e025b55780b5e88c129 100644 --- a/services/storage_manager/ipc/test/storage_manager_stub_noper_test.cpp +++ b/services/storage_manager/ipc/test/storage_manager_stub_noper_test.cpp @@ -82,6 +82,8 @@ namespace { static_cast(StorageManagerInterfaceCode::GET_FILE_ENCRYPT_STATUS), static_cast(StorageManagerInterfaceCode::CREATE_RECOVER_KEY), static_cast(StorageManagerInterfaceCode::SET_RECOVER_KEY), + static_cast(StorageManagerInterfaceCode::NOTIFY_MTP_MOUNT), + static_cast(StorageManagerInterfaceCode::NOTIFY_MTP_UNMOUNT), }; } // namespace diff --git a/services/storage_manager/ipc/test/storage_manager_stub_test.cpp b/services/storage_manager/ipc/test/storage_manager_stub_test.cpp index 2412a135e7ea6127c5b50c71108df8841de983b5..a8e6e2d74120659f70b9216a5e353c0282aae157 100644 --- a/services/storage_manager/ipc/test/storage_manager_stub_test.cpp +++ b/services/storage_manager/ipc/test/storage_manager_stub_test.cpp @@ -36,6 +36,7 @@ namespace { static_cast(StorageManagerInterfaceCode::REMOVE_USER), static_cast(StorageManagerInterfaceCode::PREPARE_START_USER), static_cast(StorageManagerInterfaceCode::STOP_USER), + static_cast(StorageManagerInterfaceCode::COMPLETE_ADD_USER), static_cast(StorageManagerInterfaceCode::NOTIFY_VOLUME_CREATED), static_cast(StorageManagerInterfaceCode::NOTIFY_VOLUME_MOUNTED), static_cast(StorageManagerInterfaceCode::NOTIFY_VOLUME_STATE_CHANGED), @@ -73,6 +74,9 @@ namespace { static_cast(StorageManagerInterfaceCode::SET_BUNDLE_QUOTA), static_cast(StorageManagerInterfaceCode::GENERATE_APP_KEY), static_cast(StorageManagerInterfaceCode::DELETE_APP_KEY), + static_cast(StorageManagerInterfaceCode::GET_FILE_ENCRYPT_STATUS), + static_cast(StorageManagerInterfaceCode::CREATE_RECOVER_KEY), + static_cast(StorageManagerInterfaceCode::SET_RECOVER_KEY), static_cast(StorageManagerInterfaceCode::NOTIFY_MTP_MOUNT), static_cast(StorageManagerInterfaceCode::NOTIFY_MTP_UNMOUNT), }; @@ -167,6 +171,7 @@ HWTEST_F(StorageManagerStubTest, Storage_Manager_StorageManagerStubTest_OnRemote EXPECT_CALL(mock, RemoveUser(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); EXPECT_CALL(mock, PrepareStartUser(testing::_)).WillOnce(testing::Return(E_OK)); EXPECT_CALL(mock, StopUser(testing::_)).WillOnce(testing::Return(E_OK)); + EXPECT_CALL(mock, CompleteAddUser(testing::_)).WillOnce(testing::Return(E_OK)); EXPECT_CALL(mock, NotifyVolumeCreated(testing::_)).WillOnce(testing::Return(E_OK)); EXPECT_CALL(mock, NotifyVolumeMounted(testing::_, testing::_, testing::_, testing::_, testing::_)) .WillOnce(testing::Return(E_OK)); @@ -207,6 +212,9 @@ HWTEST_F(StorageManagerStubTest, Storage_Manager_StorageManagerStubTest_OnRemote EXPECT_CALL(mock, DeleteAppkey(testing::_)).WillOnce(testing::Return(E_OK)); EXPECT_CALL(mock, UpdateUseAuthWithRecoveryKey(testing::_, testing::_, testing::_, testing::_, testing::_)) .WillOnce(testing::Return(E_OK)); + EXPECT_CALL(mock, GetFileEncryptStatus(testing::_, testing::_, testing::_)).WillOnce(testing::Return(E_OK)); + EXPECT_CALL(mock, CreateRecoverKey(testing::_, testing::_, testing::_, testing::_)).WillOnce(testing::Return(E_OK)); + EXPECT_CALL(mock, SetRecoverKey(testing::_)).WillOnce(testing::Return(E_OK)); EXPECT_CALL(mock, NotifyMtpMounted(testing::_, testing::_, testing::_, testing::_)).WillOnce(testing::Return(E_OK)); EXPECT_CALL(mock, NotifyMtpUnmounted(testing::_, testing::_)).WillOnce(testing::Return(E_OK));