From f060a25add60cbfbe050daa4773d35eba678bd37 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Mon, 28 Jul 2025 11:12:15 +0800 Subject: [PATCH 1/5] fix: Signed-off-by: weishaoxiong --- .../test/mock/distributed_file_daemon_manager_mock.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/test/mock/distributed_file_daemon_manager_mock.h b/services/distributeddataservice/service/test/mock/distributed_file_daemon_manager_mock.h index cd68204fd..34e4b8740 100644 --- a/services/distributeddataservice/service/test/mock/distributed_file_daemon_manager_mock.h +++ b/services/distributeddataservice/service/test/mock/distributed_file_daemon_manager_mock.h @@ -103,15 +103,15 @@ public: { return 0; } - int32_t GetDfsSwitchStatus(const std::string &networkId, int32_t &switchStatus) override + int32_t GetDfsSwitchStatus(const std::string &networkId, int32_t &switchStatus) { return 0; } - int32_t UpdateDfsSwitchStatus(int32_t switchStatus) override + int32_t UpdateDfsSwitchStatus(int32_t switchStatus) { return 0; } - int32_t GetConnectedDeviceList(std::vector &deviceList) override + int32_t GetConnectedDeviceList(std::vector &deviceList) { return 0; } -- Gitee From dc8791d653c3af848a9c2598f86bc204d172bc0c Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Tue, 29 Jul 2025 11:20:55 +0800 Subject: [PATCH 2/5] fix: Signed-off-by: weishaoxiong --- .../service/test/cloud_data_mock_test.cpp | 66 +++++++-------- .../test/data_share_service_impl_test.cpp | 82 +++++++++---------- .../service/test/kvdb_general_store_test.cpp | 61 ++++++++------ .../test/mock/account_delegate_mock.cpp | 2 +- .../service/test/mock/account_delegate_mock.h | 5 -- .../distributed_file_daemon_manager_mock.h | 6 +- .../service/test/object_manager_mock_test.cpp | 54 ++++++------ .../service/test/user_delegate_mock_test.cpp | 45 ++++++---- 8 files changed, 165 insertions(+), 156 deletions(-) diff --git a/services/distributeddataservice/service/test/cloud_data_mock_test.cpp b/services/distributeddataservice/service/test/cloud_data_mock_test.cpp index 00fd01710..0407eeeae 100644 --- a/services/distributeddataservice/service/test/cloud_data_mock_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_mock_test.cpp @@ -43,8 +43,8 @@ static constexpr const char *TEST_CLOUD_STORE = "test_cloud_store"; static constexpr const char *TEST_CLOUD_ID = "test_cloud_id"; static constexpr const char *TEST_CLOUD_DATABASE_ALIAS_1 = "test_cloud_database_alias_1"; static constexpr const char *TEST_CLOUD_DATABASE_ALIAS_2 = "test_cloud_database_alias_2"; -static constexpr const char *TEST_CLOUD_PATH = - "/data/app/el2/100/database/test_cloud_bundleName/entry/rdb/test_cloud_store"; +static constexpr const char *TEST_CLOUD_PATH = "/data/app/el2/100/database/test_cloud_bundleName/entry/rdb/" + "test_cloud_store"; class CloudDataMockTest : public testing::Test { public: static void SetUpTestCase(void); @@ -54,6 +54,7 @@ public: static SchemaMeta schemaMeta_; static std::shared_ptr cloudServiceImpl_; + protected: static void InitMetaData(); static void InitSchemaMeta(); @@ -62,6 +63,7 @@ protected: static StoreMetaData metaData_; static CloudInfo cloudInfo_; static NetworkDelegateMock delegate_; + static inline AccountDelegateMock *accountDelegateMock = nullptr; }; class CloudServerMock : public CloudServer { @@ -176,10 +178,14 @@ void CloudDataMockTest::InitCloudInfo() void CloudDataMockTest::SetUpTestCase(void) { + accountDelegateMock = new (std::nothrow) AccountDelegateMock(); + if (accountDelegateMock != nullptr) { + AccountDelegate::instance_ = nullptr; + AccountDelegate::RegisterAccountInstance(accountDelegateMock); + } MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - MetaDataManager::GetInstance().SetSyncer([](const auto &, auto) { - DeviceMatrix::GetInstance().OnChanged(DeviceMatrix::META_STORE_MASK); - }); + MetaDataManager::GetInstance().SetSyncer( + [](const auto &, auto) { DeviceMatrix::GetInstance().OnChanged(DeviceMatrix::META_STORE_MASK); }); auto cloudServerMock = new CloudServerMock(); CloudServer::RegisterCloudInstance(cloudServerMock); @@ -198,7 +204,13 @@ void CloudDataMockTest::SetUpTestCase(void) InitSchemaMeta(); } -void CloudDataMockTest::TearDownTestCase() {} +void CloudDataMockTest::TearDownTestCase() +{ + if (accountDelegateMock != nullptr) { + delete accountDelegateMock; + accountDelegateMock = nullptr; + } +} void CloudDataMockTest::SetUp() { @@ -234,17 +246,11 @@ HWTEST_F(CloudDataMockTest, GetSchema001, TestSize.Level1) ASSERT_TRUE(MetaDataManager::GetInstance().DelMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), true)); SchemaMeta schemaMeta; ASSERT_FALSE(MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), schemaMeta, true)); - std::vector users = {0, 1}; - EXPECT_CALL(AccountDelegateMock::Init(), QueryUsers(_)) - .Times(1) - .WillOnce(DoAll( - SetArgReferee<0>(users), - Return(true))); - EXPECT_CALL(AccountDelegateMock::Init(), IsVerified(_)) - .Times(1) - .WillOnce(DoAll(Return(true))); - DistributedData::StoreInfo storeInfo{ OHOS::IPCSkeleton::GetCallingTokenID(), TEST_CLOUD_BUNDLE, - TEST_CLOUD_STORE, 0 }; + std::vector users = { 0, 1 }; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(users), Return(true))); + EXPECT_CALL(*accountDelegateMock, IsVerified(_)).Times(1).WillOnce(DoAll(Return(true))); + DistributedData::StoreInfo storeInfo{ OHOS::IPCSkeleton::GetCallingTokenID(), TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, + 0 }; auto event = std::make_unique(CloudEvent::GET_SCHEMA, storeInfo); EventCenter::GetInstance().PostEvent(std::move(event)); auto ret = MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), schemaMeta, true); @@ -268,14 +274,12 @@ HWTEST_F(CloudDataMockTest, GetSchema002, TestSize.Level1) ASSERT_FALSE(MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), schemaMeta, true)); std::vector users; - EXPECT_CALL(AccountDelegateMock::Init(), QueryUsers(_)) + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) .Times(1) - .WillOnce(DoAll( - SetArgReferee<0>(users), - Invoke([](std::vector& users) { users.clear(); }), - Return(true))); - DistributedData::StoreInfo storeInfo{ OHOS::IPCSkeleton::GetCallingTokenID(), TEST_CLOUD_BUNDLE, - TEST_CLOUD_STORE, 0 }; + .WillOnce( + DoAll(SetArgReferee<0>(users), Invoke([](std::vector &users) { users.clear(); }), Return(true))); + DistributedData::StoreInfo storeInfo{ OHOS::IPCSkeleton::GetCallingTokenID(), TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, + 0 }; auto event = std::make_unique(CloudEvent::GET_SCHEMA, storeInfo); EventCenter::GetInstance().PostEvent(std::move(event)); auto ret = MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), schemaMeta, true); @@ -299,13 +303,9 @@ HWTEST_F(CloudDataMockTest, GetSchema003, TestSize.Level1) ASSERT_FALSE(MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), schemaMeta, true)); std::vector users; - EXPECT_CALL(AccountDelegateMock::Init(), QueryUsers(_)) - .Times(1) - .WillOnce(DoAll( - SetArgReferee<0>(users), - Return(false))); - DistributedData::StoreInfo storeInfo{ OHOS::IPCSkeleton::GetCallingTokenID(), TEST_CLOUD_BUNDLE, - TEST_CLOUD_STORE, 0 }; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(users), Return(false))); + DistributedData::StoreInfo storeInfo{ OHOS::IPCSkeleton::GetCallingTokenID(), TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, + 0 }; auto event = std::make_unique(CloudEvent::GET_SCHEMA, storeInfo); EventCenter::GetInstance().PostEvent(std::move(event)); auto ret = MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), schemaMeta, true); @@ -325,11 +325,11 @@ HWTEST_F(CloudDataMockTest, OnReadyTest_LoginAccount, TestSize.Level0) auto ret = cloudServiceImpl_->OnReady(device); EXPECT_EQ(ret, CloudData::CloudService::SUCCESS); - EXPECT_CALL(AccountDelegateMock::Init(), IsLoginAccount()).Times(1).WillOnce(testing::Return(false)); + EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).Times(1).WillOnce(testing::Return(false)); ret = cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); EXPECT_NE(ret, CloudData::CloudService::SUCCESS); - EXPECT_CALL(AccountDelegateMock::Init(), IsLoginAccount()).Times(1).WillOnce(testing::Return(true)); + EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).Times(1).WillOnce(testing::Return(true)); ret = cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); EXPECT_EQ(ret, CloudData::CloudService::SUCCESS); ZLOGI("CloudDataMockTest OnReadyTest_LoginAccount end"); diff --git a/services/distributeddataservice/service/test/data_share_service_impl_test.cpp b/services/distributeddataservice/service/test/data_share_service_impl_test.cpp index 9eecec988..5d9273bed 100644 --- a/services/distributeddataservice/service/test/data_share_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/data_share_service_impl_test.cpp @@ -48,49 +48,51 @@ public: static constexpr int64_t TEST_SUB_ID = 100; static constexpr uint32_t CUREEENT_USER_ID = 123; static constexpr uint32_t NATIVE_USER_ID = 0; - static void SetUpTestCase(void){}; - static void TearDownTestCase(void){}; + static void SetUpTestCase(void) + { + accountDelegateMock = new (std::nothrow) AccountDelegateMock(); + if (accountDelegateMock != nullptr) { + AccountDelegate::instance_ = nullptr; + AccountDelegate::RegisterAccountInstance(accountDelegateMock); + } + } + static void TearDownTestCase(void) + { + if (accountDelegateMock != nullptr) { + delete accountDelegateMock; + accountDelegateMock = nullptr; + } + } void SetSelfTokenInfo(int32_t user); void SetUp(); void TearDown(); + static inline AccountDelegateMock *accountDelegateMock = nullptr; }; void DataShareServiceImplTest::SetSelfTokenInfo(int32_t user) { - HapInfoParams info = { - .userID = user, + HapInfoParams info = { .userID = user, .bundleName = "ohos.datasharetest.demo", .instIndex = 0, - .appIDDesc = "ohos.datasharetest.demo" - }; - HapPolicyParams policy = { - .apl = APL_NORMAL, + .appIDDesc = "ohos.datasharetest.demo" }; + HapPolicyParams policy = { .apl = APL_NORMAL, .domain = "test.domain", - .permList = { - { - .permissionName = "ohos.permission.test", - .bundleName = "ohos.datasharetest.demo", - .grantMode = 1, - .availableLevel = APL_NORMAL, - .label = "label", - .labelId = 1, - .description = "ohos.datasharetest.demo", - .descriptionId = 1 - } - }, - .permStateList = { - { - .permissionName = "ohos.permission.test", - .isGeneral = true, - .resDeviceID = { "local" }, - .grantStatus = { PermissionState::PERMISSION_GRANTED }, - .grantFlags = { 1 } - } - } - }; + .permList = { { .permissionName = "ohos.permission.test", + .bundleName = "ohos.datasharetest.demo", + .grantMode = 1, + .availableLevel = APL_NORMAL, + .label = "label", + .labelId = 1, + .description = "ohos.datasharetest.demo", + .descriptionId = 1 } }, + .permStateList = { { .permissionName = "ohos.permission.test", + .isGeneral = true, + .resDeviceID = { "local" }, + .grantStatus = { PermissionState::PERMISSION_GRANTED }, + .grantFlags = { 1 } } } }; AccessTokenKit::AllocHapToken(info, policy); - auto testTokenId = Security::AccessToken::AccessTokenKit::GetHapTokenID( - info.userID, info.bundleName, info.instIndex); + auto testTokenId = + Security::AccessToken::AccessTokenKit::GetHapTokenID(info.userID, info.bundleName, info.instIndex); SetSelfTokenID(testTokenId); } void DataShareServiceImplTest::SetUp(void) @@ -333,13 +335,13 @@ HWTEST_F(DataShareServiceImplTest, SubscribePublishedData001, TestSize.Level1) auto tokenId = AccessTokenKit::GetHapTokenID(USER_TEST, "ohos.datasharetest.demo", 0); AccessTokenKit::DeleteToken(tokenId); - std::vector result = dataShareServiceImpl.SubscribePublishedData(uris, subscriberId, observer); + std::vector result = dataShareServiceImpl.SubscribePublishedData(uris, subscriberId, observer); EXPECT_NE(result.size(), uris.size()); for (auto const &operationResult : result) { EXPECT_EQ(operationResult.errCode_, 0); } - result = dataShareServiceImpl.UnsubscribePublishedData(uris, subscriberId); + result = dataShareServiceImpl.UnsubscribePublishedData(uris, subscriberId); EXPECT_NE(result.size(), uris.size()); for (auto const &operationResult : result) { EXPECT_EQ(operationResult.errCode_, 0); @@ -531,11 +533,8 @@ HWTEST_F(DataShareServiceImplTest, DataProviderConfig001, TestSize.Level1) { ZLOGI("DataShareServiceImplTest DataProviderConfig001 start"); SetSelfTokenInfo(NATIVE_USER_ID); - auto testTokenId = Security::AccessToken::AccessTokenKit::GetHapTokenID( - NATIVE_USER_ID, BUNDLE_NAME, 0); - EXPECT_CALL(AccountDelegateMock::Init(), QueryForegroundUserId(testing::_)) - .Times(1) - .WillOnce(testing::Return(false)); + auto testTokenId = Security::AccessToken::AccessTokenKit::GetHapTokenID(NATIVE_USER_ID, BUNDLE_NAME, 0); + EXPECT_CALL(*accountDelegateMock, QueryForegroundUserId(testing::_)).Times(1).WillOnce(testing::Return(false)); DataProviderConfig config(SLIENT_ACCESS_URI, testTokenId); EXPECT_EQ(config.providerInfo_.uri, SLIENT_ACCESS_URI); EXPECT_EQ(config.providerInfo_.currentUserId, NATIVE_USER_ID); @@ -557,9 +556,8 @@ HWTEST_F(DataShareServiceImplTest, UpdateLaunchInfo001, TestSize.Level1) DataShareServiceImpl dataShareServiceImpl; // cover branch of config is nullptr dataShareServiceImpl.UpdateLaunchInfo(); - - std::string prefix = StoreMetaData::GetPrefix( - { DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid }); + + std::string prefix = StoreMetaData::GetPrefix({ DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid }); std::vector storeMetaData; MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData, true); EXPECT_EQ(storeMetaData.size(), 0); diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index 11aa042e5..18b02267a 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -59,6 +59,7 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); + protected: static constexpr const char *bundleName = "test_distributeddata"; static constexpr const char *storeName = "test_service_meta"; @@ -67,6 +68,7 @@ protected: static std::vector Random(uint32_t len); static std::shared_ptr dbStoreMock_; StoreMetaData metaData_; + static inline AccountDelegateMock *accountDelegateMock = nullptr; }; std::shared_ptr KVDBGeneralStoreTest::dbStoreMock_ = std::make_shared(); @@ -97,9 +99,22 @@ std::vector KVDBGeneralStoreTest::Random(uint32_t len) return key; } -void KVDBGeneralStoreTest::SetUpTestCase(void) {} +void KVDBGeneralStoreTest::SetUpTestCase(void) +{ + accountDelegateMock = new (std::nothrow) AccountDelegateMock(); + if (accountDelegateMock != nullptr) { + AccountDelegate::instance_ = nullptr; + AccountDelegate::RegisterAccountInstance(accountDelegateMock); + } +} -void KVDBGeneralStoreTest::TearDownTestCase() {} +void KVDBGeneralStoreTest::TearDownTestCase() +{ + if (accountDelegateMock != nullptr) { + delete accountDelegateMock; + accountDelegateMock = nullptr; + } +} void KVDBGeneralStoreTest::SetUp() { @@ -128,8 +143,12 @@ public: class MockKvStoreChangedData : public DistributedDB::KvStoreChangedData { public: - MockKvStoreChangedData() {} - virtual ~MockKvStoreChangedData() {} + MockKvStoreChangedData() + { + } + virtual ~MockKvStoreChangedData() + { + } std::list entriesInserted = {}; std::list entriesUpdated = {}; std::list entriesDeleted = {}; @@ -530,11 +549,7 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) store->SetEqualIdentifier(bundleName, storeName); std::string prepareTraceId; std::vector users; - EXPECT_CALL(AccountDelegateMock::Init(), QueryUsers(_)) - .Times(1) - .WillOnce(DoAll( - SetArgReferee<0>(users), - Return(false))); + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(users), Return(false))); auto ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); EXPECT_EQ(ret, DBStatus::DB_ERROR); } @@ -560,12 +575,10 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) store->SetEqualIdentifier(bundleName, storeName); std::string prepareTraceId; std::vector users; - EXPECT_CALL(AccountDelegateMock::Init(), QueryUsers(_)) + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) .Times(1) - .WillOnce(DoAll( - SetArgReferee<0>(users), - Invoke([](std::vector& users) { users.clear(); }), - Return(true))); + .WillOnce( + DoAll(SetArgReferee<0>(users), Invoke([](std::vector &users) { users.clear(); }), Return(true))); auto ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); EXPECT_EQ(ret, DBStatus::DB_ERROR); @@ -586,7 +599,7 @@ HWTEST_F(KVDBGeneralStoreTest, GetIdentifierParams, TestSize.Level0) { auto store = new (std::nothrow) KVDBGeneralStore(metaData_); std::vector sameAccountDevs{}; - std::vector uuids{"uuidtest01", "uuidtest02", "uuidtest03"}; + std::vector uuids{ "uuidtest01", "uuidtest02", "uuidtest03" }; store->GetIdentifierParams(sameAccountDevs, uuids, 0); // NO_ACCOUNT for (const auto &devId : uuids) { EXPECT_EQ(DMAdapter::GetInstance().IsOHOSType(devId), false); @@ -617,11 +630,8 @@ HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) syncParam.mode = mixMode; KvStoreNbDelegateMock mockDelegate; store->delegate_ = &mockDelegate; - std::vector users1 = {0, 1}; - EXPECT_CALL(AccountDelegateMock::Init(), QueryUsers(_)) - .WillRepeatedly(DoAll( - SetArgReferee<0>(users1), - Return(true))); + std::vector users1 = { 0, 1 }; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillRepeatedly(DoAll(SetArgReferee<0>(users1), Return(true))); auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); EXPECT_EQ(ret.first, GeneralError::E_NOT_SUPPORT); GeneralStore::StoreConfig storeConfig; @@ -961,7 +971,6 @@ HWTEST_F(KVDBGeneralStoreTest, Sync001, TestSize.Level0) delete store; } - /** * @tc.name: SetEqualIdentifier * @tc.desc: SetEqualIdentifier test. @@ -973,8 +982,8 @@ HWTEST_F(KVDBGeneralStoreTest, SetEqualIdentifier, TestSize.Level0) { auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); - std::vector sameAccountDevs{"account01", "account02", "account03"}; - std::vector uuids{"uuidtest01", "uuidtest02", "uuidtest03"}; + std::vector sameAccountDevs{ "account01", "account02", "account03" }; + std::vector uuids{ "uuidtest01", "uuidtest02", "uuidtest03" }; AppId appId01 = { "ohos.kvdbservice.test01" }; StoreId storeId01 = { "meta_test_storeid" }; std::string account = "account"; @@ -994,8 +1003,8 @@ HWTEST_F(KVDBGeneralStoreTest, GetIdentifierParams001, TestSize.Level0) { auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); - std::vector sameAccountDevs{"account01", "account02", "account03"}; - std::vector uuids{"uuidtest01", "uuidtest02", "uuidtest03"}; + std::vector sameAccountDevs{ "account01", "account02", "account03" }; + std::vector uuids{ "uuidtest01", "uuidtest02", "uuidtest03" }; store->GetIdentifierParams(sameAccountDevs, uuids, 1); // for (const auto &devId : uuids) { EXPECT_EQ(DMAdapter::GetInstance().IsOHOSType(devId), false); @@ -1079,7 +1088,7 @@ HWTEST_F(KVDBGeneralStoreTest, GetDBProcessCB002, TestSize.Level0) { auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); - std::map processes = {{"test_id", {}}}; + std::map processes = { { "test_id", {} } }; GeneralStore::DetailAsync async; EXPECT_EQ(async, nullptr); auto callback = store->GetDBProcessCB(async); diff --git a/services/distributeddataservice/service/test/mock/account_delegate_mock.cpp b/services/distributeddataservice/service/test/mock/account_delegate_mock.cpp index 1f4b6d5ea..011d0d2f2 100644 --- a/services/distributeddataservice/service/test/mock/account_delegate_mock.cpp +++ b/services/distributeddataservice/service/test/mock/account_delegate_mock.cpp @@ -30,7 +30,7 @@ bool AccountDelegate::RegisterAccountInstance(AccountDelegate *instance) AccountDelegate *AccountDelegate::GetInstance() { - return &(AccountDelegateMock::Init()); + return instance_; } } // namespace DistributedData } // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/service/test/mock/account_delegate_mock.h b/services/distributeddataservice/service/test/mock/account_delegate_mock.h index 1207f6392..a2ca487c8 100644 --- a/services/distributeddataservice/service/test/mock/account_delegate_mock.h +++ b/services/distributeddataservice/service/test/mock/account_delegate_mock.h @@ -24,11 +24,6 @@ namespace DistributedData { class AccountDelegateMock : public AccountDelegate { public: virtual ~AccountDelegateMock() = default; - static AccountDelegateMock& Init() - { - static AccountDelegateMock instance; - return instance; - } MOCK_METHOD(int32_t, Subscribe, (std::shared_ptr)); MOCK_METHOD(int32_t, Unsubscribe, (std::shared_ptr)); MOCK_METHOD(std::string, GetCurrentAccountId, (), (const)); diff --git a/services/distributeddataservice/service/test/mock/distributed_file_daemon_manager_mock.h b/services/distributeddataservice/service/test/mock/distributed_file_daemon_manager_mock.h index 34e4b8740..cd68204fd 100644 --- a/services/distributeddataservice/service/test/mock/distributed_file_daemon_manager_mock.h +++ b/services/distributeddataservice/service/test/mock/distributed_file_daemon_manager_mock.h @@ -103,15 +103,15 @@ public: { return 0; } - int32_t GetDfsSwitchStatus(const std::string &networkId, int32_t &switchStatus) + int32_t GetDfsSwitchStatus(const std::string &networkId, int32_t &switchStatus) override { return 0; } - int32_t UpdateDfsSwitchStatus(int32_t switchStatus) + int32_t UpdateDfsSwitchStatus(int32_t switchStatus) override { return 0; } - int32_t GetConnectedDeviceList(std::vector &deviceList) + int32_t GetConnectedDeviceList(std::vector &deviceList) override { return 0; } diff --git a/services/distributeddataservice/service/test/object_manager_mock_test.cpp b/services/distributeddataservice/service/test/object_manager_mock_test.cpp index 8d901defb..50d4127ff 100644 --- a/services/distributeddataservice/service/test/object_manager_mock_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_mock_test.cpp @@ -20,10 +20,11 @@ #include "device_matrix_mock.h" #include "gtest/gtest.h" #include "mock/account_delegate_mock.h" -#include "mock/meta_data_manager_mock.h" #include "mock/distributed_file_daemon_manager_mock.h" +#include "mock/meta_data_manager_mock.h" #include "object_manager.h" + using namespace OHOS::DistributedObject; using namespace OHOS::DistributedData; using namespace OHOS::Storage::DistributedFile; @@ -48,6 +49,11 @@ public: BDeviceMatrix::deviceMatrix = deviceMatrixMock; fileDaemonMgrMock = std::make_shared(); BDistributedFileDaemonManager::fileDaemonManger_ = fileDaemonMgrMock; + accountDelegateMock = new (std::nothrow) AccountDelegateMock(); + if (accountDelegateMock != nullptr) { + AccountDelegate::instance_ = nullptr; + AccountDelegate::RegisterAccountInstance(accountDelegateMock); + } } static void TearDownTestCase(void) { @@ -61,6 +67,10 @@ public: BDeviceMatrix::deviceMatrix = nullptr; fileDaemonMgrMock = nullptr; BDistributedFileDaemonManager::fileDaemonManger_ = nullptr; + if (accountDelegateMock != nullptr) { + delete accountDelegateMock; + accountDelegateMock = nullptr; + } } static inline std::shared_ptr metaDataManagerMock = nullptr; @@ -68,8 +78,9 @@ public: static inline std::shared_ptr devMgrAdapterMock = nullptr; static inline std::shared_ptr deviceMatrixMock = nullptr; static inline std::shared_ptr fileDaemonMgrMock = nullptr; - void SetUp(){}; - void TearDown(){}; + static inline AccountDelegateMock *accountDelegateMock = nullptr; + void SetUp() {}; + void TearDown() {}; }; /** @@ -80,8 +91,7 @@ public: */ HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync001, TestSize.Level0) { - EXPECT_CALL(*fileDaemonMgrMock, RegisterAssetCallback(_)) - .WillOnce(testing::Return(0)); + EXPECT_CALL(*fileDaemonMgrMock, RegisterAssetCallback(_)).WillOnce(testing::Return(0)); auto &manager = ObjectStoreManager::GetInstance(); StoreMetaData meta; meta.deviceId = "test_device_id"; @@ -189,8 +199,7 @@ HWTEST_F(ObjectManagerMockTest, SyncOnStore001, TestSize.Level0) EXPECT_CALL(*devMgrAdapterMock, GetUuidByNetworkId(_)).WillRepeatedly(Return("mock_uuid")); EXPECT_CALL(*devMgrAdapterMock, ToUUID(testing::A &>())) .WillOnce(Return(std::vector{ "mock_uuid_1" })); - EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) - .WillOnce(testing::Return(false)); + EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)).WillOnce(testing::Return(false)); EXPECT_CALL(*metaDataManagerMock, Sync(_, _, _)).WillOnce(testing::Return(true)); auto result = manager.SyncOnStore(prefix, remoteDeviceList, func); EXPECT_EQ(result, OBJECT_SUCCESS); @@ -223,11 +232,7 @@ HWTEST_F(ObjectManagerMockTest, GetCurrentUser001, TestSize.Level1) { auto &manager = ObjectStoreManager::GetInstance(); std::vector users; - EXPECT_CALL(AccountDelegateMock::Init(), QueryUsers(_)) - .Times(1) - .WillOnce(DoAll( - SetArgReferee<0>(users), - Return(false))); + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(users), Return(false))); auto result = manager.GetCurrentUser(); EXPECT_EQ(result, ""); } @@ -243,12 +248,10 @@ HWTEST_F(ObjectManagerMockTest, GetCurrentUser002, TestSize.Level1) { auto &manager = ObjectStoreManager::GetInstance(); std::vector users; - EXPECT_CALL(AccountDelegateMock::Init(), QueryUsers(_)) + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) .Times(1) - .WillOnce(DoAll( - SetArgReferee<0>(users), - Invoke([](std::vector& users) { users.clear(); }), - Return(true))); + .WillOnce( + DoAll(SetArgReferee<0>(users), Invoke([](std::vector &users) { users.clear(); }), Return(true))); auto result = manager.GetCurrentUser(); EXPECT_EQ(result, ""); } @@ -263,12 +266,8 @@ HWTEST_F(ObjectManagerMockTest, GetCurrentUser002, TestSize.Level1) HWTEST_F(ObjectManagerMockTest, GetCurrentUser003, TestSize.Level1) { auto &manager = ObjectStoreManager::GetInstance(); - std::vector users = {0, 1}; - EXPECT_CALL(AccountDelegateMock::Init(), QueryUsers(_)) - .Times(1) - .WillOnce(DoAll( - SetArgReferee<0>(users), - Return(true))); + std::vector users = { 0, 1 }; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(users), Return(true))); auto result = manager.GetCurrentUser(); EXPECT_EQ(result, std::to_string(users[0])); } @@ -367,15 +366,12 @@ HWTEST_F(ObjectManagerMockTest, UnRegisterAssetsLister001, TestSize.Level1) manager.objectAssetsRecvListener_ = nullptr; auto ret = manager.UnRegisterAssetsLister(); EXPECT_EQ(ret, true); - EXPECT_CALL(*fileDaemonMgrMock, RegisterAssetCallback(_)) - .WillOnce(testing::Return(0)); + EXPECT_CALL(*fileDaemonMgrMock, RegisterAssetCallback(_)).WillOnce(testing::Return(0)); manager.RegisterAssetsLister(); - EXPECT_CALL(*fileDaemonMgrMock, UnRegisterAssetCallback(_)) - .WillOnce(testing::Return(-1)); + EXPECT_CALL(*fileDaemonMgrMock, UnRegisterAssetCallback(_)).WillOnce(testing::Return(-1)); ret = manager.UnRegisterAssetsLister(); EXPECT_EQ(ret, false); - EXPECT_CALL(*fileDaemonMgrMock, UnRegisterAssetCallback(_)) - .WillOnce(testing::Return(0)); + EXPECT_CALL(*fileDaemonMgrMock, UnRegisterAssetCallback(_)).WillOnce(testing::Return(0)); ret = manager.UnRegisterAssetsLister(); EXPECT_EQ(ret, true); } diff --git a/services/distributeddataservice/service/test/user_delegate_mock_test.cpp b/services/distributeddataservice/service/test/user_delegate_mock_test.cpp index 910e17ca9..5a40b23ab 100644 --- a/services/distributeddataservice/service/test/user_delegate_mock_test.cpp +++ b/services/distributeddataservice/service/test/user_delegate_mock_test.cpp @@ -13,12 +13,14 @@ * limitations under the License. */ -#include #include -#include "user_delegate.h" -#include "device_manager_adapter_mock.h" +#include + #include "account_delegate_mock.h" +#include "device_manager_adapter_mock.h" #include "metadata/meta_data_manager.h" +#include "user_delegate.h" + using namespace OHOS::DistributedData; using namespace testing::ext; @@ -52,21 +54,35 @@ class UserDelegateMockTest : public testing::Test { public: static void SetUpTestCase(void); static void TearDownTestCase(void); - void SetUp() {} - void TearDown() {} + void SetUp() + { + } + void TearDown() + { + } static inline shared_ptr devMgrAdapterMock = nullptr; + static inline AccountDelegateMock *accountDelegateMock = nullptr; }; void UserDelegateMockTest::SetUpTestCase(void) { devMgrAdapterMock = make_shared(); BDeviceManagerAdapter::deviceManagerAdapter = devMgrAdapterMock; + accountDelegateMock = new (std::nothrow) AccountDelegateMock(); + if (accountDelegateMock != nullptr) { + AccountDelegate::instance_ = nullptr; + AccountDelegate::RegisterAccountInstance(accountDelegateMock); + } } void UserDelegateMockTest::TearDownTestCase(void) { BDeviceManagerAdapter::deviceManagerAdapter = nullptr; devMgrAdapterMock = nullptr; + if (accountDelegateMock != nullptr) { + delete accountDelegateMock; + accountDelegateMock = nullptr; + } } /** @@ -91,16 +107,12 @@ HWTEST_F(UserDelegateMockTest, GetLocalUserStatus, TestSize.Level0) */ HWTEST_F(UserDelegateMockTest, InitLocalUserMeta, TestSize.Level0) { - EXPECT_CALL(AccountDelegateMock::Init(), QueryUsers(_)) - .Times(1) - .WillOnce(Return(false)); + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).Times(1).WillOnce(Return(false)); bool ret = UserDelegate::GetInstance().InitLocalUserMeta(); EXPECT_FALSE(ret); std::vector users; - EXPECT_CALL(AccountDelegateMock::Init(), QueryUsers(_)) - .Times(1) - .WillOnce(DoAll(SetArgReferee<0>(users), Return(true))); + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(users), Return(true))); ret = UserDelegate::GetInstance().InitLocalUserMeta(); UserDelegate::GetInstance().DeleteUsers("users"); EXPECT_FALSE(ret); @@ -147,7 +159,7 @@ HWTEST_F(UserDelegateMockTest, GetUsers, TestSize.Level0) */ HWTEST_F(UserDelegateMockTest, Init, TestSize.Level0) { - EXPECT_CALL(AccountDelegateMock::Init(), Subscribe(_)).WillRepeatedly(Return(0)); + EXPECT_CALL(*accountDelegateMock, Subscribe(_)).WillRepeatedly(Return(0)); DeviceInfo devInfo = { .uuid = "HJJ4FGAGAAGA45WF3663FAGA" }; EXPECT_CALL(*devMgrAdapterMock, GetLocalDevice()).WillRepeatedly(Return(devInfo)); std::shared_ptr poolPtr = std::make_shared(1, 0); @@ -156,12 +168,11 @@ HWTEST_F(UserDelegateMockTest, Init, TestSize.Level0) instance.executors_ = poolPtr; ASSERT_NE(instance.executors_, nullptr); - std::vector users = {0, 1}; - EXPECT_CALL(AccountDelegateMock::Init(), QueryUsers(_)) - .WillRepeatedly(DoAll(SetArgReferee<0>(users), Return(true))); + std::vector users = { 0, 1 }; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillRepeatedly(DoAll(SetArgReferee<0>(users), Return(true))); EXPECT_TRUE(instance.InitLocalUserMeta()); instance.Init(poolPtr); ASSERT_TRUE(instance.executors_ != nullptr); } -} -} \ No newline at end of file +} // namespace DistributedDataTest +} // namespace OHOS::Test \ No newline at end of file -- Gitee From c03f917cb0cbd400118793c0e62ce961ab797ef0 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Tue, 29 Jul 2025 11:34:28 +0800 Subject: [PATCH 3/5] fix: Signed-off-by: weishaoxiong --- .../service/test/cloud_data_mock_test.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/services/distributeddataservice/service/test/cloud_data_mock_test.cpp b/services/distributeddataservice/service/test/cloud_data_mock_test.cpp index 0407eeeae..3165ca73e 100644 --- a/services/distributeddataservice/service/test/cloud_data_mock_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_mock_test.cpp @@ -183,6 +183,7 @@ void CloudDataMockTest::SetUpTestCase(void) AccountDelegate::instance_ = nullptr; AccountDelegate::RegisterAccountInstance(accountDelegateMock); } + EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).Times(2).WillRepeatedly(Return(0)); MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); MetaDataManager::GetInstance().SetSyncer( [](const auto &, auto) { DeviceMatrix::GetInstance().OnChanged(DeviceMatrix::META_STORE_MASK); }); @@ -240,6 +241,7 @@ void CloudDataMockTest::TearDown() */ HWTEST_F(CloudDataMockTest, GetSchema001, TestSize.Level1) { + EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillOnce(Return(0)); auto cloudServerMock = std::make_shared(); auto user = AccountDelegate::GetInstance()->GetUserByToken(OHOS::IPCSkeleton::GetCallingTokenID()); auto [status, cloudInfo] = cloudServerMock->GetServerInfo(user, true); @@ -266,6 +268,7 @@ HWTEST_F(CloudDataMockTest, GetSchema001, TestSize.Level1) */ HWTEST_F(CloudDataMockTest, GetSchema002, TestSize.Level1) { + EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillOnce(Return(0)); auto cloudServerMock = std::make_shared(); auto user = AccountDelegate::GetInstance()->GetUserByToken(OHOS::IPCSkeleton::GetCallingTokenID()); auto [status, cloudInfo] = cloudServerMock->GetServerInfo(user, true); @@ -295,6 +298,7 @@ HWTEST_F(CloudDataMockTest, GetSchema002, TestSize.Level1) */ HWTEST_F(CloudDataMockTest, GetSchema003, TestSize.Level1) { + EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillOnce(Return(0)); auto cloudServerMock = std::make_shared(); auto user = AccountDelegate::GetInstance()->GetUserByToken(OHOS::IPCSkeleton::GetCallingTokenID()); auto [status, cloudInfo] = cloudServerMock->GetServerInfo(user, true); -- Gitee From bf8f022461e8e83b5cf27fda6c585f813ebaf285 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Tue, 29 Jul 2025 11:55:37 +0800 Subject: [PATCH 4/5] fix: Signed-off-by: weishaoxiong --- .../service/test/object_manager_mock_test.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributeddataservice/service/test/object_manager_mock_test.cpp b/services/distributeddataservice/service/test/object_manager_mock_test.cpp index 50d4127ff..fcc58a336 100644 --- a/services/distributeddataservice/service/test/object_manager_mock_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_mock_test.cpp @@ -175,6 +175,7 @@ HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync003, TestSize.Level0) */ HWTEST_F(ObjectManagerMockTest, SyncOnStore001, TestSize.Level0) { + EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).Times(2).WillRepeatedly(Return(0)); auto &manager = ObjectStoreManager::GetInstance(); std::function &results)> func; func = [](const std::map &results) { return results; }; -- Gitee From 79dda2530e83882a4adfdbe827d1414f3b8b2ca3 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Tue, 29 Jul 2025 20:19:30 +0800 Subject: [PATCH 5/5] fix: Signed-off-by: weishaoxiong --- .../distributeddataservice/service/test/cloud_data_mock_test.cpp | 1 + .../service/test/object_manager_mock_test.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/services/distributeddataservice/service/test/cloud_data_mock_test.cpp b/services/distributeddataservice/service/test/cloud_data_mock_test.cpp index 3165ca73e..0e02096dd 100644 --- a/services/distributeddataservice/service/test/cloud_data_mock_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_mock_test.cpp @@ -183,6 +183,7 @@ void CloudDataMockTest::SetUpTestCase(void) AccountDelegate::instance_ = nullptr; AccountDelegate::RegisterAccountInstance(accountDelegateMock); } + // 2 means that the GetUserByToken interface will be called twice EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).Times(2).WillRepeatedly(Return(0)); MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); MetaDataManager::GetInstance().SetSyncer( diff --git a/services/distributeddataservice/service/test/object_manager_mock_test.cpp b/services/distributeddataservice/service/test/object_manager_mock_test.cpp index fcc58a336..7c395f68c 100644 --- a/services/distributeddataservice/service/test/object_manager_mock_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_mock_test.cpp @@ -175,6 +175,7 @@ HWTEST_F(ObjectManagerMockTest, IsNeedMetaSync003, TestSize.Level0) */ HWTEST_F(ObjectManagerMockTest, SyncOnStore001, TestSize.Level0) { + // 2 means that the GetUserByToken interface will be called twice EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).Times(2).WillRepeatedly(Return(0)); auto &manager = ObjectStoreManager::GetInstance(); std::function &results)> func; -- Gitee