From 80c4c1712a723ffa9ea1e171d2578c110c2e8049 Mon Sep 17 00:00:00 2001 From: huaqingsimeng <1004904143@qq.com> Date: Wed, 29 Mar 2023 08:50:27 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D=E6=8F=90?= =?UTF-8?q?=E4=BE=9Bjs=E6=8E=A5=E5=8F=A3=E8=83=BD=E5=8A=9B-TDD=E7=94=A8?= =?UTF-8?q?=E4=BE=8Bmock=E6=96=87=E4=BB=B6=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huaqingsimeng --- .../b_session_backup_mock.cpp | 15 ++++- .../b_session_restore_mock.cpp | 32 ++++------ .../backup_kit_inner/service_proxy_mock.cpp | 8 +-- .../include/mock_bundle_manager.h | 4 ++ .../src/mock_bundle_manager.cpp | 10 ++++ .../mock/module_external/bms_adapter_mock.cpp | 52 ++++++++++++++++ .../inner_receiver_impl_mock.cpp | 34 +++++++++++ .../mock/module_external/sms_adapter_mock.cpp | 27 +++++++++ tests/mock/module_ipc/service_mock.cpp | 10 +++- tests/mock/module_ipc/service_stub_mock.cpp | 43 ++++---------- .../module_ipc/svc_session_manager_mock.cpp | 59 ++++++++++++++++--- .../module_sched/sched_scheduler_mock.cpp | 6 ++ 12 files changed, 229 insertions(+), 71 deletions(-) create mode 100644 tests/mock/module_external/bms_adapter_mock.cpp create mode 100644 tests/mock/module_external/inner_receiver_impl_mock.cpp create mode 100644 tests/mock/module_external/sms_adapter_mock.cpp diff --git a/tests/mock/backup_kit_inner/b_session_backup_mock.cpp b/tests/mock/backup_kit_inner/b_session_backup_mock.cpp index a5b65ccb4..f3a8f5193 100644 --- a/tests/mock/backup_kit_inner/b_session_backup_mock.cpp +++ b/tests/mock/backup_kit_inner/b_session_backup_mock.cpp @@ -34,9 +34,7 @@ static BSessionBackup::Callbacks callbacks_ = {}; BSessionBackup::~BSessionBackup() {} -unique_ptr BSessionBackup::Init(UniqueFd remoteCap, - vector bundlesToBackup, - Callbacks callbacks) +unique_ptr BSessionBackup::Init(Callbacks callbacks) { try { callbacks_ = callbacks; @@ -83,4 +81,15 @@ ErrCode BSessionBackup::Start() callbacks_.onBackupServiceDied(); return BError(BError::Codes::OK); } + +ErrCode BSessionBackup::AppendBundles(vector bundlesToBackup) +{ + Start(); + return BError(BError::Codes::OK); +} + +ErrCode BSessionBackup::Finish() +{ + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/mock/backup_kit_inner/b_session_restore_mock.cpp b/tests/mock/backup_kit_inner/b_session_restore_mock.cpp index 3cfad611f..49d4126bc 100644 --- a/tests/mock/backup_kit_inner/b_session_restore_mock.cpp +++ b/tests/mock/backup_kit_inner/b_session_restore_mock.cpp @@ -35,11 +35,10 @@ static vector bundlesToRestore_ = {}; BSessionRestore::~BSessionRestore() {} -unique_ptr BSessionRestore::Init(vector bundlesToRestore, Callbacks callbacks) +unique_ptr BSessionRestore::Init(Callbacks callbacks) { try { callbacks_ = move(callbacks); - bundlesToRestore_ = move(bundlesToRestore); auto restore = make_unique(); return restore; } catch (const exception &e) { @@ -48,22 +47,6 @@ unique_ptr BSessionRestore::Init(vector bundlesToRe return nullptr; } -UniqueFd BSessionRestore::GetLocalCapabilities() -{ - string bundleName = "test"; - auto iter = find_if(bundlesToRestore_.begin(), bundlesToRestore_.end(), [&bundleName](auto &obj) { - const auto &bName = obj; - return bName == bundleName; - }); - if (iter != bundlesToRestore_.end()) { - return UniqueFd(-1); - } - TestManager tm("BSessionRestoreMock_GetFd_0100"); - string filePath = tm.GetRootDirCurTest().append("tmp"); - UniqueFd fd(open(filePath.data(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR)); - return fd; -} - ErrCode BSessionRestore::PublishFile(BFileInfo fileInfo) { return BError(BError::Codes::OK); @@ -97,7 +80,18 @@ ErrCode BSessionRestore::Start() return BError(BError::Codes::OK); } -ErrCode BSessionRestore::GetExtFileName(string &bundleName, string &fileName) +ErrCode BSessionRestore::GetFileHandle(const string &bundleName, const string &fileName) +{ + return BError(BError::Codes::OK); +} + +ErrCode BSessionRestore::AppendBundles(UniqueFd remoteCap, vector bundlesToRestore) +{ + Start(); + return BError(BError::Codes::OK); +} + +ErrCode BSessionRestore::Finish() { return BError(BError::Codes::OK); } diff --git a/tests/mock/backup_kit_inner/service_proxy_mock.cpp b/tests/mock/backup_kit_inner/service_proxy_mock.cpp index 7ce08cae2..6844d16bc 100644 --- a/tests/mock/backup_kit_inner/service_proxy_mock.cpp +++ b/tests/mock/backup_kit_inner/service_proxy_mock.cpp @@ -31,7 +31,7 @@ namespace OHOS::FileManagement::Backup { using namespace std; -int32_t ServiceProxy::InitRestoreSession(sptr remote, const vector &bundleNames) +int32_t ServiceProxy::InitRestoreSession(sptr remote) { if (!GetMockInitBackupOrRestoreSession()) { return 1; @@ -39,9 +39,7 @@ int32_t ServiceProxy::InitRestoreSession(sptr remote, const vec return 0; } -int32_t ServiceProxy::InitBackupSession(sptr remote, - UniqueFd fd, - const vector &bundleNames) +int32_t ServiceProxy::InitBackupSession(sptr remote) { if (!GetMockInitBackupOrRestoreSession()) { return 1; @@ -77,7 +75,7 @@ ErrCode ServiceProxy::AppDone(ErrCode errCode) return BError(BError::Codes::OK); } -ErrCode ServiceProxy::GetExtFileName(string &bundleName, string &fileName) +ErrCode ServiceProxy::GetFileHandle(const string &bundleName, const string &fileName) { return BError(BError::Codes::OK); } diff --git a/tests/mock/bundle_manager/include/mock_bundle_manager.h b/tests/mock/bundle_manager/include/mock_bundle_manager.h index b50908b42..bbd1638f4 100644 --- a/tests/mock/bundle_manager/include/mock_bundle_manager.h +++ b/tests/mock/bundle_manager/include/mock_bundle_manager.h @@ -56,6 +56,10 @@ public: int32_t flags, BundleInfo &bundleInfo, int32_t userId = Constants::UNSPECIFIED_USERID) override; + + virtual bool GetApplicationInfos(const ApplicationFlag flag, + int userId, + std::vector &appInfos) override; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/tests/mock/bundle_manager/src/mock_bundle_manager.cpp b/tests/mock/bundle_manager/src/mock_bundle_manager.cpp index 4b397ee2a..ee5cca248 100644 --- a/tests/mock/bundle_manager/src/mock_bundle_manager.cpp +++ b/tests/mock/bundle_manager/src/mock_bundle_manager.cpp @@ -70,6 +70,16 @@ bool BundleMgrStub::GetBundleInfo(const string &bundleName, int32_t flags, Bundl return true; } +bool BundleMgrStub::GetApplicationInfos(const ApplicationFlag flag, int userId, vector &appInfos) +{ + GTEST_LOG_(INFO) << "GetApplicationInfos is ok"; + ApplicationInfo appInfo {}; + appInfo.bundleName = "com.example.app2backup"; + appInfo.versionName = "1.0.0"; + appInfos.push_back(appInfo); + return true; +} + BundleMgrClient::BundleMgrClient() {} BundleMgrClient::~BundleMgrClient() {} diff --git a/tests/mock/module_external/bms_adapter_mock.cpp b/tests/mock/module_external/bms_adapter_mock.cpp new file mode 100644 index 000000000..edf7f3612 --- /dev/null +++ b/tests/mock/module_external/bms_adapter_mock.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 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 "module_external/bms_adapter.h" + +#include +#include +#include + +#include "b_json/b_json_entity_extension_config.h" +#include "bundle_mgr_client.h" +#include "module_external/inner_receiver_impl.h" +#include "module_ipc/service.h" +#include "module_ipc/svc_session_manager.h" +#include "module_sched/sched_scheduler.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +vector BundleMgrAdapter::GetBundleInfos() +{ + vector bundleInfos; + bundleInfos.emplace_back( + BJsonEntityCaps::BundleInfo {"com.example.app2backup", {}, {}, 0, true, "com.example.app2backup"}); + return bundleInfos; +} + +vector BundleMgrAdapter::GetBundleInfos(const vector &bundleNames) +{ + vector bundleInfos; + bundleInfos.emplace_back( + BJsonEntityCaps::BundleInfo {"com.example.app2backup", {}, {}, 0, true, "com.example.app2backup"}); + return bundleInfos; +} + +ErrCode BundleMgrAdapter::Install(wptr statusReceiver, const string &bundleFilePath) +{ + return 0; +} +} // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/module_external/inner_receiver_impl_mock.cpp b/tests/mock/module_external/inner_receiver_impl_mock.cpp new file mode 100644 index 000000000..4d1d6a349 --- /dev/null +++ b/tests/mock/module_external/inner_receiver_impl_mock.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 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 "module_external/inner_receiver_impl.h" + +#include +#include + +#include "module_ipc/service.h" +#include "module_ipc/svc_session_manager.h" +#include "module_sched/sched_scheduler.h" + +namespace OHOS::FileManagement::Backup { +void InnerReceiverImpl::OnStatusNotify(const int progress) {} + +void InnerReceiverImpl::OnFinished(const int32_t resultCode, const std::string &resultMsg) {} + +std::string InnerReceiverImpl::GetBundleName() +{ + return bundleName_; +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/mock/module_external/sms_adapter_mock.cpp b/tests/mock/module_external/sms_adapter_mock.cpp new file mode 100644 index 000000000..1ef1c8f20 --- /dev/null +++ b/tests/mock/module_external/sms_adapter_mock.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 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 "module_external/sms_adapter.h" + +#include +#include + +namespace OHOS::FileManagement::Backup { +StorageManager::BundleStats StorageMgrAdapter::GetBundleStats(const std::string &bundleName) +{ + StorageManager::BundleStats bundleStats; + return bundleStats; +} +} // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/module_ipc/service_mock.cpp b/tests/mock/module_ipc/service_mock.cpp index f3f124e8b..1af44b43d 100644 --- a/tests/mock/module_ipc/service_mock.cpp +++ b/tests/mock/module_ipc/service_mock.cpp @@ -42,12 +42,12 @@ string Service::VerifyCallerAndGetCallerName() return ""; } -ErrCode Service::InitRestoreSession(sptr remote, const vector &bundleNames) +ErrCode Service::InitRestoreSession(sptr remote) { return BError(BError::Codes::OK); } -ErrCode Service::InitBackupSession(sptr remote, UniqueFd fd, const vector &bundleNames) +ErrCode Service::InitBackupSession(sptr remote) { return BError(BError::Codes::OK); } @@ -92,7 +92,7 @@ ErrCode Service::LaunchBackupExtension(const BundleName &bundleName) return BError(BError::Codes::OK); } -ErrCode Service::GetExtFileName(string &bundleName, string &fileName) +ErrCode Service::GetFileHandle(const string &bundleName, const string &fileName) { return BError(BError::Codes::OK); } @@ -118,4 +118,8 @@ void Service::ClearSessionAndSchedInfo(const string &bundleName) {} void Service::VerifyCaller() {} void Service::VerifyCaller(IServiceReverse::Scenario scenario) {} + +void Service::OnAllBundlesFinished(ErrCode errCode) {} + +void Service::OnStartSched() {} } // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/module_ipc/service_stub_mock.cpp b/tests/mock/module_ipc/service_stub_mock.cpp index aa843d04c..8581b2cd4 100644 --- a/tests/mock/module_ipc/service_stub_mock.cpp +++ b/tests/mock/module_ipc/service_stub_mock.cpp @@ -32,7 +32,7 @@ ServiceStub::ServiceStub() opToInterfaceMap_[SERVICE_CMD_APP_FILE_READY] = &ServiceStub::CmdAppFileReady; opToInterfaceMap_[SERVICE_CMD_APP_DONE] = &ServiceStub::CmdAppDone; opToInterfaceMap_[SERVICE_CMD_START] = &ServiceStub::CmdStart; - opToInterfaceMap_[SERVICE_CMD_GET_EXT_FILE_NAME] = &ServiceStub::CmdGetExtFileName; + opToInterfaceMap_[SERVICE_CMD_GET_FILE_NAME] = &ServiceStub::CmdGetFileHandle; opToInterfaceMap_[SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION] = &ServiceStub::CmdAppendBundlesRestoreSession; opToInterfaceMap_[SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION] = &ServiceStub::CmdAppendBundlesBackupSession; opToInterfaceMap_[SERVICE_CMD_FINISH] = &ServiceStub::CmdFinish; @@ -58,9 +58,7 @@ int32_t ServiceStub::CmdInitRestoreSession(MessageParcel &data, MessageParcel &r auto remote = data.ReadRemoteObject(); auto iremote = iface_cast(remote); - std::vector bundleNames; - data.ReadStringVector(&bundleNames); - int32_t res = InitRestoreSession(iremote, bundleNames); + int32_t res = InitRestoreSession(iremote); reply.WriteInt32(res); return BError(BError::Codes::OK); } @@ -70,12 +68,7 @@ int32_t ServiceStub::CmdInitBackupSession(MessageParcel &data, MessageParcel &re auto remote = data.ReadRemoteObject(); auto iremote = iface_cast(remote); - UniqueFd fd(data.ReadFileDescriptor()); - - std::vector bundleNames; - data.ReadStringVector(&bundleNames); - - int res = InitBackupSession(iremote, move(fd), bundleNames); + int res = InitBackupSession(iremote); reply.WriteInt32(res); return BError(BError::Codes::OK); } @@ -121,57 +114,41 @@ int32_t ServiceStub::CmdAppDone(MessageParcel &data, MessageParcel &reply) return BError(BError::Codes::OK); } -int32_t ServiceStub::CmdGetExtFileName(MessageParcel &data, MessageParcel &reply) +int32_t ServiceStub::CmdGetFileHandle(MessageParcel &data, MessageParcel &reply) { string bundleName; data.ReadString(bundleName); string fileName; data.ReadString(fileName); - return GetExtFileName(bundleName, fileName); + return GetFileHandle(bundleName, fileName); } int32_t ServiceStub::CmdAppendBundlesRestoreSession(MessageParcel &data, MessageParcel &reply) { UniqueFd fd(data.ReadFileDescriptor()); - if (fd < 0) { - return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd"); - } std::vector bundleNames; - if (!data.ReadStringVector(&bundleNames)) { - return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames"); - } + data.ReadStringVector(&bundleNames); int res = AppendBundlesRestoreSession(move(fd), bundleNames); - if (!reply.WriteInt32(res)) { - string str = "Failed to send the result " + to_string(res); - return BError(BError::Codes::SA_INVAL_ARG, str.data()).GetCode(); - } + reply.WriteInt32(res); return BError(BError::Codes::OK); } int32_t ServiceStub::CmdAppendBundlesBackupSession(MessageParcel &data, MessageParcel &reply) { std::vector bundleNames; - if (!data.ReadStringVector(&bundleNames)) { - return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames"); - } + data.ReadStringVector(&bundleNames); int res = AppendBundlesBackupSession(bundleNames); - if (!reply.WriteInt32(res)) { - string str = "Failed to send the result " + to_string(res); - return BError(BError::Codes::SA_INVAL_ARG, str.data()).GetCode(); - } + reply.WriteInt32(res); return BError(BError::Codes::OK); } int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply) { int res = Finish(); - if (!reply.WriteInt32(res)) { - string str = "Failed to send the result " + to_string(res); - return BError(BError::Codes::SA_INVAL_ARG, str.data()).GetCode(); - } + reply.WriteInt32(res); return BError(BError::Codes::OK); } } // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/module_ipc/svc_session_manager_mock.cpp b/tests/mock/module_ipc/svc_session_manager_mock.cpp index 4e6f460f0..79eb5933f 100644 --- a/tests/mock/module_ipc/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_mock.cpp @@ -33,6 +33,7 @@ namespace { constexpr int SCHED_NUM = 1; constexpr int FILE_NUM = 2; static int32_t g_nFileReadyNum = 0; +static int32_t g_nAllBundlesFinished = 0; } // namespace void SvcSessionManager::VerifyCallerAndScenario(uint32_t clientToken, IServiceReverse::Scenario scenario) const @@ -69,11 +70,6 @@ IServiceReverse::Scenario SvcSessionManager::GetScenario() return impl_.scenario; } -void SvcSessionManager::GetBundleExtNames(map &backupExtNameMap) -{ - GTEST_LOG_(INFO) << "GetBundleExtNames"; -} - bool SvcSessionManager::OnBunleFileReady(const string &bundleName, const string &fileName) { GTEST_LOG_(INFO) << "OnBunleFileReady"; @@ -114,9 +110,10 @@ wptr SvcSessionManager::GetExtConnection(const BundleName & return wptr(it->second.backUpConnection); } -void SvcSessionManager::InitExtConn(std::map &backupExtNameMap) +sptr SvcSessionManager::GetBackupExtAbility(const string &bundleName) { - GTEST_LOG_(INFO) << "InitExtConn"; + GTEST_LOG_(INFO) << "GetBackupExtAbility"; + return nullptr; } void SvcSessionManager::DumpInfo(const int fd, const std::vector &args) @@ -194,8 +191,54 @@ void SvcSessionManager::SetServiceSchedAction(const string &bundleName, BConstan string SvcSessionManager::GetBackupExtName(const string &bundleName) { GTEST_LOG_(INFO) << "GetBackupExtName " << bundleName; - return ""; + return "com.example.app2backup"; +} + +void SvcSessionManager::AppendBundles(const vector &bundleNames) +{ + GTEST_LOG_(INFO) << "AppendBundles"; + BackupExtInfo info {}; + info.backupExtName = "com.example.app2backup"; + impl_.backupExtNameMap.insert(make_pair("com.example.app2backup", info)); } +void SvcSessionManager::Start() {} + void SvcSessionManager::Finish() {} + +bool SvcSessionManager::IsOnAllBundlesFinished() +{ + GTEST_LOG_(INFO) << "IsOnAllBundlesFinished"; + g_nAllBundlesFinished++; + if (g_nAllBundlesFinished % FILE_NUM == SCHED_NUM) { + GTEST_LOG_(INFO) << "IsOnAllBundlesFinished is true"; + return true; + } + GTEST_LOG_(INFO) << "IsOnAllBundlesFinished is false"; + return false; +} + +bool SvcSessionManager::IsOnOnStartSched() +{ + return true; +} + +void SvcSessionManager::SetInstallState(const string &bundleName, const string &state) {} + +string SvcSessionManager::GetInstallState(const string &bundleName) +{ + return "OK"; +} + +void SvcSessionManager::SetNeedToInstall(const std::string &bundleName, bool needToInstall) {} + +bool SvcSessionManager::GetNeedToInstall(const std::string &bundleName) +{ + return false; +} + +bool SvcSessionManager::NeedToUnloadService() +{ + return false; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/mock/module_sched/sched_scheduler_mock.cpp b/tests/mock/module_sched/sched_scheduler_mock.cpp index 88c947a75..b8fcaef54 100644 --- a/tests/mock/module_sched/sched_scheduler_mock.cpp +++ b/tests/mock/module_sched/sched_scheduler_mock.cpp @@ -29,4 +29,10 @@ void SchedScheduler::Sched(string bundleName) {} void SchedScheduler::ExecutingQueueTasks(const string &bundleName) {} void SchedScheduler::RemoveExtConn(const string &bundleName) {} + +void SchedScheduler::InstallingState(const string &bundleName) {} + +void SchedScheduler::UnloadServiceTimer() {} + +void SchedScheduler::InstallSuccess(const std::string &bundleName, const int32_t resultCode) {} }; // namespace OHOS::FileManagement::Backup \ No newline at end of file -- Gitee