From 309a9c4bd8703a6245cf26e71bfb5e62388c133b Mon Sep 17 00:00:00 2001 From: gecheng Date: Sat, 2 Aug 2025 18:55:55 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gecheng --- .../service/test/kvdb_general_store_test.cpp | 38 ++++++++++++-- .../service/test/object_manager_mock_test.cpp | 51 +++++++++++++++++++ 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index 18b02267a..90d9c757e 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -533,7 +533,7 @@ HWTEST_F(KVDBGeneralStoreTest, GetDBSyncCompleteCB, TestSize.Level0) * @tc.desc: Test the scenario where the QueryUsers return false in the CloudSync function. * @tc.type: FUNC * @tc.require: -* @tc.author: SQL +* @tc.author: */ HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) { @@ -559,7 +559,7 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) * @tc.desc: CloudSync test the functionality of different branches. * @tc.type: FUNC * @tc.require: -* @tc.author: SQL +* @tc.author: */ HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) { @@ -588,6 +588,36 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) EXPECT_EQ(ret, DBStatus::OK); } +/** +* @tc.name: CloudSync003 +* @tc.desc: Test the scenario where the QueryUsers return true in the CloudSync function. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, CloudSync003, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + store->SetEqualIdentifier(bundleName, storeName); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + std::vector devices = { "device1", "device2" }; + auto asyncs = [](const GenDetails &result) {}; + store->storeInfo_.user = 0; + auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; + store->SetEqualIdentifier(bundleName, storeName); + std::string prepareTraceId; + std::vector users = {0, 1}; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) + .Times(1) + .WillOnce(DoAll( + SetArgReferee<0>(users), + Return(true))); + auto ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); + EXPECT_EQ(ret, DBStatus::OK); +} + /** * @tc.name: GetIdentifierParams * @tc.desc: GetIdentifierParams test. @@ -613,7 +643,7 @@ HWTEST_F(KVDBGeneralStoreTest, GetIdentifierParams, TestSize.Level0) * @tc.desc: Sync test the functionality of 3 < syncMode < 7 branches. * @tc.type: FUNC * @tc.require: -* @tc.author: SQL +* @tc.author: */ HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) { @@ -646,7 +676,7 @@ HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) * @tc.desc: Sync test the functionality of different branches. * @tc.type: FUNC * @tc.require: -* @tc.author: SQL +* @tc.author: */ HWTEST_F(KVDBGeneralStoreTest, Sync003, TestSize.Level0) { diff --git a/services/distributeddataservice/service/test/object_manager_mock_test.cpp b/services/distributeddataservice/service/test/object_manager_mock_test.cpp index 7c395f68c..5f48b0594 100644 --- a/services/distributeddataservice/service/test/object_manager_mock_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_mock_test.cpp @@ -377,5 +377,56 @@ HWTEST_F(ObjectManagerMockTest, UnRegisterAssetsLister001, TestSize.Level1) ret = manager.UnRegisterAssetsLister(); EXPECT_EQ(ret, true); } + +/** +* @tc.name: InitUserMeta001 +* @tc.desc: Test the scenario where the QueryUsers return false in the GetCurrentUser function. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectManagerMockTest, InitUserMeta001, TestSize.Level1) +{ + EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) + .WillOnce(testing::Return(false)); + auto &manager = ObjectStoreManager::GetInstance(); + std::vector users; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(users), Return(false))); + auto status = manager.InitUserMeta(); + ASSERT_EQ(status, DistributedObject::OBJECT_INNER_ERROR); +} + +/** +* @tc.name: InitUserMeta002 +* @tc.desc: Test the scenario where the QueryUsers users empty in the GetCurrentUser function. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectManagerMockTest, InitUserMeta002, TestSize.Level1) +{ + EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) + .WillOnce(testing::Return(false)); + auto &manager = ObjectStoreManager::GetInstance(); + std::vector users; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) + .Times(1) + .WillOnce( + DoAll(SetArgReferee<0>(users), Invoke([](std::vector &users) { users.clear(); }), Return(true))); + auto status = manager.InitUserMeta(); + ASSERT_EQ(status, DistributedObject::OBJECT_INNER_ERROR); +} + +/** +* @tc.name: InitUserMeta003 +* @tc.desc: Test the scenario where the QueryUsers return true in the GetCurrentUser function. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectManagerMockTest, InitUserMeta003, TestSize.Level1) +{ + EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) + .WillOnce(testing::Return(false)); + auto &manager = ObjectStoreManager::GetInstance(); + std::vector users = { 0, 1 }; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(users), Return(true))); + auto status = manager.InitUserMeta(); + ASSERT_EQ(status, DistributedObject::OBJECT_INNER_ERROR); +} }; // namespace DistributedDataTest } // namespace OHOS::Test \ No newline at end of file -- Gitee From 5d61aa6a86fc2badcb7e1cff56d3633c5d287d9b Mon Sep 17 00:00:00 2001 From: gecheng Date: Mon, 4 Aug 2025 10:35:02 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0object=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gecheng --- .../service/test/object_manager_mock_test.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/distributeddataservice/service/test/object_manager_mock_test.cpp b/services/distributeddataservice/service/test/object_manager_mock_test.cpp index 5f48b0594..faf7ca79c 100644 --- a/services/distributeddataservice/service/test/object_manager_mock_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_mock_test.cpp @@ -422,6 +422,8 @@ HWTEST_F(ObjectManagerMockTest, InitUserMeta003, TestSize.Level1) { EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) .WillOnce(testing::Return(false)); + DeviceInfo devInfo = { .uuid = "666" }; + EXPECT_CALL(*devMgrAdapterMock, GetLocalDevice()).WillOnce(Return(devInfo)); auto &manager = ObjectStoreManager::GetInstance(); std::vector users = { 0, 1 }; EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(users), Return(true))); -- Gitee From c0af0c66e5b6560846df05ffc1b07843c5a45e45 Mon Sep 17 00:00:00 2001 From: gecheng Date: Mon, 4 Aug 2025 11:49:52 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0object=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gecheng --- .../service/test/kvdb_general_store_test.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index 90d9c757e..54f07ee2a 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -406,7 +406,7 @@ HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) ZLOGI("SyncTest start"); mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + auto store = std::make_shared(metaData_); ASSERT_NE(store, nullptr); uint32_t syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; @@ -537,7 +537,7 @@ HWTEST_F(KVDBGeneralStoreTest, GetDBSyncCompleteCB, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) { - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + auto store = std::make_shared(metaData_); ASSERT_NE(store, nullptr); store->SetEqualIdentifier(bundleName, storeName); KvStoreNbDelegateMock mockDelegate; @@ -563,7 +563,7 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) { - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + auto store = std::make_shared(metaData_); ASSERT_NE(store, nullptr); store->SetEqualIdentifier(bundleName, storeName); KvStoreNbDelegateMock mockDelegate; @@ -597,7 +597,7 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, CloudSync003, TestSize.Level0) { - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + auto store = std::make_shared(metaData_); ASSERT_NE(store, nullptr); store->SetEqualIdentifier(bundleName, storeName); KvStoreNbDelegateMock mockDelegate; @@ -649,7 +649,7 @@ HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) { mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + auto store = std::make_shared(metaData_); ASSERT_NE(store, nullptr); uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; @@ -682,7 +682,7 @@ HWTEST_F(KVDBGeneralStoreTest, Sync003, TestSize.Level0) { mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + auto store = std::make_shared(metaData_); ASSERT_NE(store, nullptr); uint32_t syncMode = GeneralStore::SyncMode::NEARBY_END; uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; @@ -965,7 +965,7 @@ HWTEST_F(KVDBGeneralStoreTest, Sync001, TestSize.Level0) { mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + auto store = std::make_shared(metaData_); ASSERT_NE(store, nullptr); uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; -- Gitee From c01fcc807379264189bec8dbf33d7e45bb7be013 Mon Sep 17 00:00:00 2001 From: gecheng Date: Mon, 4 Aug 2025 12:38:10 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0object=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gecheng --- .../service/test/kvdb_general_store_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index 54f07ee2a..b84dec473 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -965,7 +965,7 @@ HWTEST_F(KVDBGeneralStoreTest, Sync001, TestSize.Level0) { mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = std::make_shared(metaData_); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; -- Gitee From 46759b11b6c11b64541dc6648f8fd927e231db29 Mon Sep 17 00:00:00 2001 From: gecheng Date: Mon, 4 Aug 2025 14:53:13 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0object=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gecheng --- .../service/test/kvdb_general_store_test.cpp | 883 +++++++++++++++++- 1 file changed, 877 insertions(+), 6 deletions(-) diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index b84dec473..ff7388eae 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -406,7 +406,7 @@ HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) ZLOGI("SyncTest start"); mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = std::make_shared(metaData_); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); uint32_t syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; @@ -419,6 +419,7 @@ HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) EXPECT_NE(ret.first, GeneralError::E_OK); auto status = store->Close(); EXPECT_EQ(status, GeneralError::E_OK); + delete store; } /** @@ -537,7 +538,7 @@ HWTEST_F(KVDBGeneralStoreTest, GetDBSyncCompleteCB, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) { - auto store = std::make_shared(metaData_); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); store->SetEqualIdentifier(bundleName, storeName); KvStoreNbDelegateMock mockDelegate; @@ -552,6 +553,8 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) 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); + store->delegate_ = nullptr; + delete store; } /** @@ -563,7 +566,7 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) { - auto store = std::make_shared(metaData_); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); store->SetEqualIdentifier(bundleName, storeName); KvStoreNbDelegateMock mockDelegate; @@ -586,6 +589,8 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_CLOUD_FORCE_PUSH; ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); EXPECT_EQ(ret, DBStatus::OK); + store->delegate_ = nullptr; + delete store; } /** @@ -597,7 +602,7 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, CloudSync003, TestSize.Level0) { - auto store = std::make_shared(metaData_); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); store->SetEqualIdentifier(bundleName, storeName); KvStoreNbDelegateMock mockDelegate; @@ -616,6 +621,8 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync003, TestSize.Level0) Return(true))); auto ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); EXPECT_EQ(ret, DBStatus::OK); + store->delegate_ = nullptr; + delete store; } /** @@ -649,7 +656,7 @@ HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) { mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = std::make_shared(metaData_); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; @@ -669,6 +676,8 @@ HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) store->SetConfig(storeConfig); ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); EXPECT_EQ(ret.first, GeneralError::E_OK); + store->delegate_ = nullptr; + delete store; } /** @@ -682,7 +691,7 @@ HWTEST_F(KVDBGeneralStoreTest, Sync003, TestSize.Level0) { mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = std::make_shared(metaData_); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); uint32_t syncMode = GeneralStore::SyncMode::NEARBY_END; uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; @@ -719,6 +728,8 @@ HWTEST_F(KVDBGeneralStoreTest, Sync003, TestSize.Level0) syncParam.mode = mixMode; ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); + store->delegate_ = nullptr; + delete store; } /** @@ -1217,5 +1228,865 @@ HWTEST_F(KVDBGeneralStoreTest, ConstructorTest, TestSize.Level0) EXPECT_EQ(store6->delegate_, nullptr); delete store6; } + +/** +* @tc.name: GetDBPasswordTest_001 +* @tc.desc: get password with all exception branch +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_001, TestSize.Level0) +{ + metaData_.isEncrypt = false; + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + + metaData_.isEncrypt = true; + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + SecretKeyMetaData secretKey; + auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + std::vector randomKey = Random(KEY_LENGTH); + ASSERT_FALSE(randomKey.empty()); + secretKey.sKey = randomKey; + result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); +} + +/** +* @tc.name: GetDBPasswordTest_002 +* @tc.desc: GetDBPassword from meta. +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_002, TestSize.Level0) +{ + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), metaData_, true)); + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_TRUE(dbPassword.GetSize() == 0); +} + +/** +* @tc.name: GetDBPasswordTest_003 +* @tc.desc: GetDBPassword from encrypt meta. +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_003, TestSize.Level0) +{ + ZLOGI("GetDBPasswordTest_002 start"); + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + metaData_.isEncrypt = true; + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); + + auto errCode = CryptoManager::GetInstance().GenerateRootKey(); + EXPECT_EQ(errCode, CryptoManager::ErrCode::SUCCESS); + + std::vector randomKey = Random(KEY_LENGTH); + ASSERT_FALSE(randomKey.empty()); + SecretKeyMetaData secretKey; + secretKey.storeType = metaData_.storeType; + CryptoManager::CryptoParams encryptParams = { .area = metaData_.area, .userId = metaData_.user }; + secretKey.sKey = CryptoManager::GetInstance().Encrypt(randomKey, encryptParams); + ASSERT_FALSE(secretKey.sKey.empty()); + ASSERT_FALSE(encryptParams.nonce.empty()); + secretKey.area = metaData_.area; + secretKey.nonce = encryptParams.nonce; + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true)); + + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_TRUE(dbPassword.GetSize() != 0); + randomKey.assign(randomKey.size(), 0); +} + +/** +* @tc.name: GetDBPasswordTest_004 +* @tc.desc: get password with exception branch +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_004, TestSize.Level0) +{ + metaData_.bundleName = INVALID_APPID; + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + + SecretKeyMetaData secretKey; + auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + auto key = Random(KEY_LENGTH); + ASSERT_FALSE(key.empty()); + secretKey.sKey = key; + result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + MetaDataManager::GetInstance().DelMeta(metaData_.GetSecretKey(), true); +} + +/** +* @tc.name: GetDBSecurityTest +* @tc.desc: GetDBSecurity +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBSecurityTest, TestSize.Level0) +{ + auto dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::INVALID_LABEL); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::NO_LABEL); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S0); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S0); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S1); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S1); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S2); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S2); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S3); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S3); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::SECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S4); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S4); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + auto action = static_cast(SecurityLevel::S4 + 1); + dbSecurity = KVDBGeneralStore::GetDBSecurity(action); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); +} + +/** +* @tc.name: GetDBOptionTest +* @tc.desc: GetDBOption from meta and dbPassword +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBOptionTest, TestSize.Level0) +{ + metaData_.isEncrypt = true; + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); + EXPECT_EQ(dbOption.syncDualTupleMode, true); + EXPECT_EQ(dbOption.createIfNecessary, false); + EXPECT_EQ(dbOption.isMemoryDb, false); + EXPECT_EQ(dbOption.isEncryptedDb, metaData_.isEncrypt); + EXPECT_EQ(dbOption.isNeedCompressOnSync, metaData_.isNeedCompress); + EXPECT_EQ(dbOption.schema, metaData_.schema); + EXPECT_EQ(dbOption.passwd, dbPassword); + EXPECT_EQ(dbOption.cipher, DistributedDB::CipherType::AES_256_GCM); + EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); + EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); + EXPECT_EQ(dbOption.secOption, KVDBGeneralStore::GetDBSecurity(metaData_.securityLevel)); +} + +/** +* @tc.name: CloseTest +* @tc.desc: Close kvdb general store and test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, CloseTest, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + auto ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_OK); + + KvStoreNbDelegateMock mockDelegate; + mockDelegate.taskCountMock_ = 1; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_BUSY); +} + +/** +* @tc.name: Close +* @tc.desc: RdbGeneralStore Close test +* @tc.type: FUNC +* @tc.require: +* @tc.author: shaoyuanzhao +*/ +HWTEST_F(KVDBGeneralStoreTest, BusyClose, TestSize.Level0) +{ + auto store = std::make_shared(metaData_); + ASSERT_NE(store, nullptr); + std::thread thread([store]() { + std::unique_lockrwMutex_)> lock(store->rwMutex_); + std::this_thread::sleep_for(std::chrono::seconds(1)); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + auto ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_BUSY); + thread.join(); + ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_OK); +} + +/** +* @tc.name: SyncTest +* @tc.desc: Sync. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) +{ + ZLOGI("SyncTest start"); + mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + uint32_t syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; + uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; + auto mixMode = GeneralStore::MixMode(syncMode, highMode); + std::string kvQuery = ""; + DistributedKv::KVDBQuery query(kvQuery); + SyncParam syncParam{}; + syncParam.mode = mixMode; + auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_NE(ret.first, GeneralError::E_OK); + auto status = store->Close(); + EXPECT_EQ(status, GeneralError::E_OK); + delete store; +} + +/** +* @tc.name: BindTest +* @tc.desc: Bind test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, BindTest, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + DistributedData::Database database; + std::map bindInfos; + GeneralStore::CloudConfig config; + EXPECT_EQ(bindInfos.empty(), true); + auto ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + + std::shared_ptr db; + std::shared_ptr loader; + GeneralStore::BindInfo bindInfo1(db, loader); + uint32_t key = 1; + bindInfos[key] = bindInfo1; + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + std::shared_ptr dbs = std::make_shared(); + std::shared_ptr loaders = std::make_shared(); + GeneralStore::BindInfo bindInfo2(dbs, loaders); + bindInfos[key] = bindInfo2; + EXPECT_EQ(store->IsBound(key), false); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); + + store->bindInfos_.clear(); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + EXPECT_EQ(store->IsBound(key), false); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + + EXPECT_EQ(store->IsBound(key), true); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); +} + +/** +* @tc.name: BindWithDifferentUsersTest +* @tc.desc: Bind test the functionality of different users. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, BindWithDifferentUsersTest, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + ASSERT_NE(store, nullptr); + DistributedData::Database database; + std::map bindInfos; + GeneralStore::CloudConfig config; + + std::shared_ptr dbs = std::make_shared(); + std::shared_ptr loaders = std::make_shared(); + GeneralStore::BindInfo bindInfo(dbs, loaders); + uint32_t key0 = 100; + uint32_t key1 = 101; + bindInfos[key0] = bindInfo; + bindInfos[key1] = bindInfo; + EXPECT_EQ(store->IsBound(key0), false); + EXPECT_EQ(store->IsBound(key1), false); + auto ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + EXPECT_EQ(store->IsBound(key0), true); + EXPECT_EQ(store->IsBound(key1), true); + + uint32_t key2 = 102; + bindInfos[key2] = bindInfo; + EXPECT_EQ(store->IsBound(key2), false); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + EXPECT_EQ(store->IsBound(key2), true); +} + +/** +* @tc.name: GetDBSyncCompleteCB +* @tc.desc: GetDBSyncCompleteCB test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBSyncCompleteCB, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + GeneralStore::DetailAsync async; + EXPECT_EQ(async, nullptr); + KVDBGeneralStore::DBSyncCallback ret = store->GetDBSyncCompleteCB(async); + EXPECT_NE(ret, nullptr); + auto asyncs = [](const GenDetails &result) {}; + EXPECT_NE(asyncs, nullptr); + ret = store->GetDBSyncCompleteCB(asyncs); + EXPECT_NE(ret, nullptr); +} + +/** +* @tc.name: CloudSync001 +* @tc.desc: Test the scenario where the QueryUsers return false in the CloudSync function. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + store->SetEqualIdentifier(bundleName, storeName); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + std::vector devices = { "device1", "device2" }; + auto asyncs = [](const GenDetails &result) {}; + store->storeInfo_.user = 0; + auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; + store->SetEqualIdentifier(bundleName, storeName); + std::string prepareTraceId; + std::vector users; + 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); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: CloudSync002 +* @tc.desc: CloudSync test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + store->SetEqualIdentifier(bundleName, storeName); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + std::vector devices = { "device1", "device2" }; + auto asyncs = [](const GenDetails &result) {}; + store->storeInfo_.user = 0; + auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; + store->SetEqualIdentifier(bundleName, storeName); + std::string prepareTraceId; + std::vector users; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) + .Times(1) + .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); + + store->storeInfo_.user = 1; + cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_CLOUD_FORCE_PUSH; + ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); + EXPECT_EQ(ret, DBStatus::OK); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: CloudSync003 +* @tc.desc: Test the scenario where the QueryUsers return true in the CloudSync function. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, CloudSync003, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + store->SetEqualIdentifier(bundleName, storeName); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + std::vector devices = { "device1", "device2" }; + auto asyncs = [](const GenDetails &result) {}; + store->storeInfo_.user = 0; + auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; + store->SetEqualIdentifier(bundleName, storeName); + std::string prepareTraceId; + std::vector users = {0, 1}; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) + .Times(1) + .WillOnce(DoAll( + SetArgReferee<0>(users), + Return(true))); + auto ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); + EXPECT_EQ(ret, DBStatus::OK); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: GetIdentifierParams +* @tc.desc: GetIdentifierParams test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: wangbin +*/ +HWTEST_F(KVDBGeneralStoreTest, GetIdentifierParams, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + std::vector sameAccountDevs{}; + 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); + EXPECT_EQ(DMAdapter::GetInstance().GetAuthType(devId), 0); // NO_ACCOUNT + } + EXPECT_EQ(sameAccountDevs.empty(), false); +} + +/** +* @tc.name: Sync002 +* @tc.desc: Sync test the functionality of 3 < syncMode < 7 branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) +{ + mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; + uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; + auto mixMode = GeneralStore::MixMode(syncMode, highMode); + std::string kvQuery = ""; + DistributedKv::KVDBQuery query(kvQuery); + SyncParam syncParam{}; + syncParam.mode = mixMode; + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + 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; + storeConfig.enableCloud_ = true; + store->SetConfig(storeConfig); + ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_OK); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: Sync003 +* @tc.desc: Sync test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, Sync003, TestSize.Level0) +{ + mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + uint32_t syncMode = GeneralStore::SyncMode::NEARBY_END; + uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; + auto mixMode = GeneralStore::MixMode(syncMode, highMode); + std::string kvQuery = ""; + DistributedKv::KVDBQuery query(kvQuery); + SyncParam syncParam{}; + syncParam.mode = mixMode; + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); + std::vector devices = { "device1", "device2" }; + syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_OK); + + syncMode = GeneralStore::SyncMode::NEARBY_UNSUBSCRIBE_REMOTE; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_OK); + + syncMode = GeneralStore::SyncMode::NEARBY_PULL_PUSH; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_OK); + + syncMode = GeneralStore::SyncMode::MODE_BUTT; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: Clean +* @tc.desc: Clean test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Clean, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + std::vector devices = { "device1", "device2" }; + std::string tableName = "tableName"; + auto ret = store->Clean(devices, -1, tableName); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + ret = store->Clean(devices, 6, tableName); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + ret = store->Clean(devices, GeneralStore::CleanMode::NEARBY_DATA, tableName); + EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); + + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + ret = store->Clean(devices, GeneralStore::CleanMode::CLOUD_INFO, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); + + ret = store->Clean(devices, GeneralStore::CleanMode::CLOUD_DATA, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); + + ret = store->Clean({}, GeneralStore::CleanMode::NEARBY_DATA, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); + ret = store->Clean(devices, GeneralStore::CleanMode::NEARBY_DATA, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); + + ret = store->Clean(devices, GeneralStore::CleanMode::LOCAL_DATA, tableName); + EXPECT_EQ(ret, GeneralError::E_ERROR); + + ret = store->Clean(devices, GeneralStore::CleanMode::CLEAN_WATER, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); +} + +/** +* @tc.name: Watch +* @tc.desc: Watch and Unwatch test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Watch, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + DistributedDataTest::MockGeneralWatcher watcher; + auto ret = store->Watch(GeneralWatcher::Origin::ORIGIN_CLOUD, watcher); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_CLOUD, watcher); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + + ret = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(ret, GeneralError::E_OK); + ret = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + + ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(ret, GeneralError::E_OK); + ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); +} + +/** +* @tc.name: Release +* @tc.desc: Release and AddRef test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Release, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + auto ret = store->Release(); + EXPECT_EQ(ret, 0); + store = new (std::nothrow) KVDBGeneralStore(metaData_); + store->ref_ = 0; + ret = store->Release(); + EXPECT_EQ(ret, 0); + store->ref_ = 2; + ret = store->Release(); + EXPECT_EQ(ret, 1); + + ret = store->AddRef(); + EXPECT_EQ(ret, 2); + store->ref_ = 0; + ret = store->AddRef(); + EXPECT_EQ(ret, 0); +} + +/** +* @tc.name: ConvertStatus +* @tc.desc: ConvertStatus test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, ConvertStatus, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + auto ret = store->ConvertStatus(DBStatus::OK); + EXPECT_EQ(ret, GeneralError::E_OK); + ret = store->ConvertStatus(DBStatus::CLOUD_NETWORK_ERROR); + EXPECT_EQ(ret, GeneralError::E_NETWORK_ERROR); + ret = store->ConvertStatus(DBStatus::CLOUD_LOCK_ERROR); + EXPECT_EQ(ret, GeneralError::E_LOCKED_BY_OTHERS); + ret = store->ConvertStatus(DBStatus::CLOUD_FULL_RECORDS); + EXPECT_EQ(ret, GeneralError::E_RECODE_LIMIT_EXCEEDED); + ret = store->ConvertStatus(DBStatus::CLOUD_ASSET_SPACE_INSUFFICIENT); + EXPECT_EQ(ret, GeneralError::E_NO_SPACE_FOR_ASSET); + ret = store->ConvertStatus(DBStatus::CLOUD_SYNC_TASK_MERGED); + EXPECT_EQ(ret, GeneralError::E_SYNC_TASK_MERGED); + ret = store->ConvertStatus(DBStatus::DB_ERROR); + EXPECT_EQ(ret, GeneralError::E_DB_ERROR); +} + +/** +* @tc.name: OnChange +* @tc.desc: OnChange test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, OnChange, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + DistributedDataTest::MockGeneralWatcher watcher; + DistributedDataTest::MockKvStoreChangedData data; + DistributedDB::ChangedData changedData; + store->observer_.OnChange(data); + store->observer_.OnChange(DistributedDB::Origin::ORIGIN_CLOUD, "originalId", std::move(changedData)); + auto result = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(result, GeneralError::E_OK); + store->observer_.OnChange(data); + store->observer_.OnChange(DistributedDB::Origin::ORIGIN_CLOUD, "originalId", std::move(changedData)); + result = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(result, GeneralError::E_OK); +} + +/** +* @tc.name: Delete +* @tc.desc: Delete test the function. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Delete, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::DEVICE_COLLABORATION); + store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::DEVICE_COLLABORATION); + DistributedData::VBuckets values; + auto ret = store->Insert("table", std::move(values)); + EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); + + DistributedData::Values args; + store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); + store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); + ret = store->Delete("table", "sql", std::move(args)); + EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); +} + +/** +* @tc.name: Query001 +* @tc.desc: KVDBGeneralStoreTest Query function test +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Query001, TestSize.Level1) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + std::string table = "table"; + std::string sql = "sql"; + auto [errCode, result] = store->Query(table, sql, {}); + EXPECT_EQ(errCode, GeneralError::E_NOT_SUPPORT); + EXPECT_EQ(result, nullptr); +} + +/** +* @tc.name: Query002 +* @tc.desc: KVDBGeneralStoreTest Query function test +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Query002, TestSize.Level1) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + std::string table = "table"; + MockQuery query; + auto [errCode, result] = store->Query(table, query); + EXPECT_EQ(errCode, GeneralError::E_NOT_SUPPORT); + EXPECT_EQ(result, nullptr); +} + +/** +* @tc.name: GetDBOption +* @tc.desc: GetDBOption from meta and dbPassword +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBOption001, TestSize.Level0) +{ + metaData_.isEncrypt = false; + metaData_.appId = "distributeddata"; + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); + EXPECT_FALSE(MetaDataManager::GetInstance().LoadMeta(metaData_.GetKeyLocal(), metaData_, true)); + EXPECT_EQ(metaData_.appId, Bootstrap::GetInstance().GetProcessLabel()); + EXPECT_EQ(dbOption.createIfNecessary, false); + EXPECT_EQ(dbOption.isMemoryDb, false); + EXPECT_EQ(dbOption.isEncryptedDb, metaData_.isEncrypt); + EXPECT_EQ(dbOption.isNeedCompressOnSync, metaData_.isNeedCompress); + EXPECT_EQ(dbOption.schema, metaData_.schema); + EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); + EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); + EXPECT_EQ(dbOption.secOption, KVDBGeneralStore::GetDBSecurity(metaData_.securityLevel)); +} + +/** +* @tc.name: Sync +* @tc.desc: Sync test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(KVDBGeneralStoreTest, Sync001, TestSize.Level0) +{ + mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; + uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; + auto mixMode = GeneralStore::MixMode(syncMode, highMode); + std::string kvQuery = "querytest"; + DistributedKv::KVDBQuery query(kvQuery); + SyncParam syncParam{}; + syncParam.mode = mixMode; + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_NOT_SUPPORT); + GeneralStore::StoreConfig storeConfig; + storeConfig.enableCloud_ = false; + store->SetConfig(storeConfig); + ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_NOT_SUPPORT); + + std::vector devices = { "device1", "device2" }; + syncMode = GeneralStore::SyncMode::CLOUD_END; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); + + syncMode = GeneralStore::SyncMode::NEARBY_PULL; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: SetEqualIdentifier +* @tc.desc: SetEqualIdentifier test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +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" }; + AppId appId01 = { "ohos.kvdbservice.test01" }; + StoreId storeId01 = { "meta_test_storeid" }; + std::string account = "account"; + store->SetEqualIdentifier(appId01, storeId01, account); + EXPECT_EQ(uuids.empty(), false); + EXPECT_EQ(sameAccountDevs.empty(), false); + delete store; +} } // namespace DistributedDataTest } // namespace OHOS::Test \ No newline at end of file -- Gitee From fa39d2d35d596ceac48c5532a7b616d1384905da Mon Sep 17 00:00:00 2001 From: gecheng Date: Mon, 4 Aug 2025 14:54:31 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9kv=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gecheng --- .../service/test/kvdb_general_store_test.cpp | 860 ------------------ 1 file changed, 860 deletions(-) diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index ff7388eae..c34db72a6 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -1228,865 +1228,5 @@ HWTEST_F(KVDBGeneralStoreTest, ConstructorTest, TestSize.Level0) EXPECT_EQ(store6->delegate_, nullptr); delete store6; } - -/** -* @tc.name: GetDBPasswordTest_001 -* @tc.desc: get password with all exception branch -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_001, TestSize.Level0) -{ - metaData_.isEncrypt = false; - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - - metaData_.isEncrypt = true; - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - SecretKeyMetaData secretKey; - auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - std::vector randomKey = Random(KEY_LENGTH); - ASSERT_FALSE(randomKey.empty()); - secretKey.sKey = randomKey; - result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); -} - -/** -* @tc.name: GetDBPasswordTest_002 -* @tc.desc: GetDBPassword from meta. -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_002, TestSize.Level0) -{ - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), metaData_, true)); - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_TRUE(dbPassword.GetSize() == 0); -} - -/** -* @tc.name: GetDBPasswordTest_003 -* @tc.desc: GetDBPassword from encrypt meta. -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_003, TestSize.Level0) -{ - ZLOGI("GetDBPasswordTest_002 start"); - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - metaData_.isEncrypt = true; - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); - - auto errCode = CryptoManager::GetInstance().GenerateRootKey(); - EXPECT_EQ(errCode, CryptoManager::ErrCode::SUCCESS); - - std::vector randomKey = Random(KEY_LENGTH); - ASSERT_FALSE(randomKey.empty()); - SecretKeyMetaData secretKey; - secretKey.storeType = metaData_.storeType; - CryptoManager::CryptoParams encryptParams = { .area = metaData_.area, .userId = metaData_.user }; - secretKey.sKey = CryptoManager::GetInstance().Encrypt(randomKey, encryptParams); - ASSERT_FALSE(secretKey.sKey.empty()); - ASSERT_FALSE(encryptParams.nonce.empty()); - secretKey.area = metaData_.area; - secretKey.nonce = encryptParams.nonce; - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true)); - - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_TRUE(dbPassword.GetSize() != 0); - randomKey.assign(randomKey.size(), 0); -} - -/** -* @tc.name: GetDBPasswordTest_004 -* @tc.desc: get password with exception branch -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_004, TestSize.Level0) -{ - metaData_.bundleName = INVALID_APPID; - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - - SecretKeyMetaData secretKey; - auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - auto key = Random(KEY_LENGTH); - ASSERT_FALSE(key.empty()); - secretKey.sKey = key; - result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - MetaDataManager::GetInstance().DelMeta(metaData_.GetSecretKey(), true); -} - -/** -* @tc.name: GetDBSecurityTest -* @tc.desc: GetDBSecurity -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBSecurityTest, TestSize.Level0) -{ - auto dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::INVALID_LABEL); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::NO_LABEL); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S0); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S0); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S1); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S1); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S2); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S2); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S3); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S3); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::SECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S4); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S4); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - auto action = static_cast(SecurityLevel::S4 + 1); - dbSecurity = KVDBGeneralStore::GetDBSecurity(action); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); -} - -/** -* @tc.name: GetDBOptionTest -* @tc.desc: GetDBOption from meta and dbPassword -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBOptionTest, TestSize.Level0) -{ - metaData_.isEncrypt = true; - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); - EXPECT_EQ(dbOption.syncDualTupleMode, true); - EXPECT_EQ(dbOption.createIfNecessary, false); - EXPECT_EQ(dbOption.isMemoryDb, false); - EXPECT_EQ(dbOption.isEncryptedDb, metaData_.isEncrypt); - EXPECT_EQ(dbOption.isNeedCompressOnSync, metaData_.isNeedCompress); - EXPECT_EQ(dbOption.schema, metaData_.schema); - EXPECT_EQ(dbOption.passwd, dbPassword); - EXPECT_EQ(dbOption.cipher, DistributedDB::CipherType::AES_256_GCM); - EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); - EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); - EXPECT_EQ(dbOption.secOption, KVDBGeneralStore::GetDBSecurity(metaData_.securityLevel)); -} - -/** -* @tc.name: CloseTest -* @tc.desc: Close kvdb general store and test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, CloseTest, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - auto ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_OK); - - KvStoreNbDelegateMock mockDelegate; - mockDelegate.taskCountMock_ = 1; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_BUSY); -} - -/** -* @tc.name: Close -* @tc.desc: RdbGeneralStore Close test -* @tc.type: FUNC -* @tc.require: -* @tc.author: shaoyuanzhao -*/ -HWTEST_F(KVDBGeneralStoreTest, BusyClose, TestSize.Level0) -{ - auto store = std::make_shared(metaData_); - ASSERT_NE(store, nullptr); - std::thread thread([store]() { - std::unique_lockrwMutex_)> lock(store->rwMutex_); - std::this_thread::sleep_for(std::chrono::seconds(1)); - }); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - auto ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_BUSY); - thread.join(); - ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_OK); -} - -/** -* @tc.name: SyncTest -* @tc.desc: Sync. -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) -{ - ZLOGI("SyncTest start"); - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), - (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - uint32_t syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; - uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; - auto mixMode = GeneralStore::MixMode(syncMode, highMode); - std::string kvQuery = ""; - DistributedKv::KVDBQuery query(kvQuery); - SyncParam syncParam{}; - syncParam.mode = mixMode; - auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_NE(ret.first, GeneralError::E_OK); - auto status = store->Close(); - EXPECT_EQ(status, GeneralError::E_OK); - delete store; -} - -/** -* @tc.name: BindTest -* @tc.desc: Bind test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, BindTest, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - DistributedData::Database database; - std::map bindInfos; - GeneralStore::CloudConfig config; - EXPECT_EQ(bindInfos.empty(), true); - auto ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - - std::shared_ptr db; - std::shared_ptr loader; - GeneralStore::BindInfo bindInfo1(db, loader); - uint32_t key = 1; - bindInfos[key] = bindInfo1; - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - std::shared_ptr dbs = std::make_shared(); - std::shared_ptr loaders = std::make_shared(); - GeneralStore::BindInfo bindInfo2(dbs, loaders); - bindInfos[key] = bindInfo2; - EXPECT_EQ(store->IsBound(key), false); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); - - store->bindInfos_.clear(); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - EXPECT_EQ(store->IsBound(key), false); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - - EXPECT_EQ(store->IsBound(key), true); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); -} - -/** -* @tc.name: BindWithDifferentUsersTest -* @tc.desc: Bind test the functionality of different users. -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, BindWithDifferentUsersTest, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - ASSERT_NE(store, nullptr); - DistributedData::Database database; - std::map bindInfos; - GeneralStore::CloudConfig config; - - std::shared_ptr dbs = std::make_shared(); - std::shared_ptr loaders = std::make_shared(); - GeneralStore::BindInfo bindInfo(dbs, loaders); - uint32_t key0 = 100; - uint32_t key1 = 101; - bindInfos[key0] = bindInfo; - bindInfos[key1] = bindInfo; - EXPECT_EQ(store->IsBound(key0), false); - EXPECT_EQ(store->IsBound(key1), false); - auto ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - EXPECT_EQ(store->IsBound(key0), true); - EXPECT_EQ(store->IsBound(key1), true); - - uint32_t key2 = 102; - bindInfos[key2] = bindInfo; - EXPECT_EQ(store->IsBound(key2), false); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - EXPECT_EQ(store->IsBound(key2), true); -} - -/** -* @tc.name: GetDBSyncCompleteCB -* @tc.desc: GetDBSyncCompleteCB test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBSyncCompleteCB, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - GeneralStore::DetailAsync async; - EXPECT_EQ(async, nullptr); - KVDBGeneralStore::DBSyncCallback ret = store->GetDBSyncCompleteCB(async); - EXPECT_NE(ret, nullptr); - auto asyncs = [](const GenDetails &result) {}; - EXPECT_NE(asyncs, nullptr); - ret = store->GetDBSyncCompleteCB(asyncs); - EXPECT_NE(ret, nullptr); -} - -/** -* @tc.name: CloudSync001 -* @tc.desc: Test the scenario where the QueryUsers return false in the CloudSync function. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - std::vector devices = { "device1", "device2" }; - auto asyncs = [](const GenDetails &result) {}; - store->storeInfo_.user = 0; - auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; - store->SetEqualIdentifier(bundleName, storeName); - std::string prepareTraceId; - std::vector users; - 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); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: CloudSync002 -* @tc.desc: CloudSync test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - std::vector devices = { "device1", "device2" }; - auto asyncs = [](const GenDetails &result) {}; - store->storeInfo_.user = 0; - auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; - store->SetEqualIdentifier(bundleName, storeName); - std::string prepareTraceId; - std::vector users; - EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) - .Times(1) - .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); - - store->storeInfo_.user = 1; - cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_CLOUD_FORCE_PUSH; - ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); - EXPECT_EQ(ret, DBStatus::OK); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: CloudSync003 -* @tc.desc: Test the scenario where the QueryUsers return true in the CloudSync function. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, CloudSync003, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - std::vector devices = { "device1", "device2" }; - auto asyncs = [](const GenDetails &result) {}; - store->storeInfo_.user = 0; - auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; - store->SetEqualIdentifier(bundleName, storeName); - std::string prepareTraceId; - std::vector users = {0, 1}; - EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) - .Times(1) - .WillOnce(DoAll( - SetArgReferee<0>(users), - Return(true))); - auto ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); - EXPECT_EQ(ret, DBStatus::OK); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: GetIdentifierParams -* @tc.desc: GetIdentifierParams test. -* @tc.type: FUNC -* @tc.require: -* @tc.author: wangbin -*/ -HWTEST_F(KVDBGeneralStoreTest, GetIdentifierParams, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - std::vector sameAccountDevs{}; - 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); - EXPECT_EQ(DMAdapter::GetInstance().GetAuthType(devId), 0); // NO_ACCOUNT - } - EXPECT_EQ(sameAccountDevs.empty(), false); -} - -/** -* @tc.name: Sync002 -* @tc.desc: Sync test the functionality of 3 < syncMode < 7 branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) -{ - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), - (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; - uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; - auto mixMode = GeneralStore::MixMode(syncMode, highMode); - std::string kvQuery = ""; - DistributedKv::KVDBQuery query(kvQuery); - SyncParam syncParam{}; - syncParam.mode = mixMode; - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - 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; - storeConfig.enableCloud_ = true; - store->SetConfig(storeConfig); - ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_OK); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: Sync003 -* @tc.desc: Sync test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, Sync003, TestSize.Level0) -{ - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), - (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - uint32_t syncMode = GeneralStore::SyncMode::NEARBY_END; - uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; - auto mixMode = GeneralStore::MixMode(syncMode, highMode); - std::string kvQuery = ""; - DistributedKv::KVDBQuery query(kvQuery); - SyncParam syncParam{}; - syncParam.mode = mixMode; - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); - std::vector devices = { "device1", "device2" }; - syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_OK); - - syncMode = GeneralStore::SyncMode::NEARBY_UNSUBSCRIBE_REMOTE; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_OK); - - syncMode = GeneralStore::SyncMode::NEARBY_PULL_PUSH; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_OK); - - syncMode = GeneralStore::SyncMode::MODE_BUTT; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: Clean -* @tc.desc: Clean test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Clean, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - std::vector devices = { "device1", "device2" }; - std::string tableName = "tableName"; - auto ret = store->Clean(devices, -1, tableName); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - ret = store->Clean(devices, 6, tableName); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - ret = store->Clean(devices, GeneralStore::CleanMode::NEARBY_DATA, tableName); - EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); - - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - ret = store->Clean(devices, GeneralStore::CleanMode::CLOUD_INFO, tableName); - EXPECT_EQ(ret, GeneralError::E_OK); - - ret = store->Clean(devices, GeneralStore::CleanMode::CLOUD_DATA, tableName); - EXPECT_EQ(ret, GeneralError::E_OK); - - ret = store->Clean({}, GeneralStore::CleanMode::NEARBY_DATA, tableName); - EXPECT_EQ(ret, GeneralError::E_OK); - ret = store->Clean(devices, GeneralStore::CleanMode::NEARBY_DATA, tableName); - EXPECT_EQ(ret, GeneralError::E_OK); - - ret = store->Clean(devices, GeneralStore::CleanMode::LOCAL_DATA, tableName); - EXPECT_EQ(ret, GeneralError::E_ERROR); - - ret = store->Clean(devices, GeneralStore::CleanMode::CLEAN_WATER, tableName); - EXPECT_EQ(ret, GeneralError::E_OK); -} - -/** -* @tc.name: Watch -* @tc.desc: Watch and Unwatch test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Watch, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - DistributedDataTest::MockGeneralWatcher watcher; - auto ret = store->Watch(GeneralWatcher::Origin::ORIGIN_CLOUD, watcher); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_CLOUD, watcher); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - - ret = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(ret, GeneralError::E_OK); - ret = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - - ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(ret, GeneralError::E_OK); - ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); -} - -/** -* @tc.name: Release -* @tc.desc: Release and AddRef test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Release, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - auto ret = store->Release(); - EXPECT_EQ(ret, 0); - store = new (std::nothrow) KVDBGeneralStore(metaData_); - store->ref_ = 0; - ret = store->Release(); - EXPECT_EQ(ret, 0); - store->ref_ = 2; - ret = store->Release(); - EXPECT_EQ(ret, 1); - - ret = store->AddRef(); - EXPECT_EQ(ret, 2); - store->ref_ = 0; - ret = store->AddRef(); - EXPECT_EQ(ret, 0); -} - -/** -* @tc.name: ConvertStatus -* @tc.desc: ConvertStatus test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, ConvertStatus, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - auto ret = store->ConvertStatus(DBStatus::OK); - EXPECT_EQ(ret, GeneralError::E_OK); - ret = store->ConvertStatus(DBStatus::CLOUD_NETWORK_ERROR); - EXPECT_EQ(ret, GeneralError::E_NETWORK_ERROR); - ret = store->ConvertStatus(DBStatus::CLOUD_LOCK_ERROR); - EXPECT_EQ(ret, GeneralError::E_LOCKED_BY_OTHERS); - ret = store->ConvertStatus(DBStatus::CLOUD_FULL_RECORDS); - EXPECT_EQ(ret, GeneralError::E_RECODE_LIMIT_EXCEEDED); - ret = store->ConvertStatus(DBStatus::CLOUD_ASSET_SPACE_INSUFFICIENT); - EXPECT_EQ(ret, GeneralError::E_NO_SPACE_FOR_ASSET); - ret = store->ConvertStatus(DBStatus::CLOUD_SYNC_TASK_MERGED); - EXPECT_EQ(ret, GeneralError::E_SYNC_TASK_MERGED); - ret = store->ConvertStatus(DBStatus::DB_ERROR); - EXPECT_EQ(ret, GeneralError::E_DB_ERROR); -} - -/** -* @tc.name: OnChange -* @tc.desc: OnChange test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, OnChange, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - DistributedDataTest::MockGeneralWatcher watcher; - DistributedDataTest::MockKvStoreChangedData data; - DistributedDB::ChangedData changedData; - store->observer_.OnChange(data); - store->observer_.OnChange(DistributedDB::Origin::ORIGIN_CLOUD, "originalId", std::move(changedData)); - auto result = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(result, GeneralError::E_OK); - store->observer_.OnChange(data); - store->observer_.OnChange(DistributedDB::Origin::ORIGIN_CLOUD, "originalId", std::move(changedData)); - result = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(result, GeneralError::E_OK); -} - -/** -* @tc.name: Delete -* @tc.desc: Delete test the function. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Delete, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::DEVICE_COLLABORATION); - store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::DEVICE_COLLABORATION); - DistributedData::VBuckets values; - auto ret = store->Insert("table", std::move(values)); - EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); - - DistributedData::Values args; - store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); - store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); - ret = store->Delete("table", "sql", std::move(args)); - EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); -} - -/** -* @tc.name: Query001 -* @tc.desc: KVDBGeneralStoreTest Query function test -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Query001, TestSize.Level1) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - std::string table = "table"; - std::string sql = "sql"; - auto [errCode, result] = store->Query(table, sql, {}); - EXPECT_EQ(errCode, GeneralError::E_NOT_SUPPORT); - EXPECT_EQ(result, nullptr); -} - -/** -* @tc.name: Query002 -* @tc.desc: KVDBGeneralStoreTest Query function test -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Query002, TestSize.Level1) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - std::string table = "table"; - MockQuery query; - auto [errCode, result] = store->Query(table, query); - EXPECT_EQ(errCode, GeneralError::E_NOT_SUPPORT); - EXPECT_EQ(result, nullptr); -} - -/** -* @tc.name: GetDBOption -* @tc.desc: GetDBOption from meta and dbPassword -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBOption001, TestSize.Level0) -{ - metaData_.isEncrypt = false; - metaData_.appId = "distributeddata"; - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); - EXPECT_FALSE(MetaDataManager::GetInstance().LoadMeta(metaData_.GetKeyLocal(), metaData_, true)); - EXPECT_EQ(metaData_.appId, Bootstrap::GetInstance().GetProcessLabel()); - EXPECT_EQ(dbOption.createIfNecessary, false); - EXPECT_EQ(dbOption.isMemoryDb, false); - EXPECT_EQ(dbOption.isEncryptedDb, metaData_.isEncrypt); - EXPECT_EQ(dbOption.isNeedCompressOnSync, metaData_.isNeedCompress); - EXPECT_EQ(dbOption.schema, metaData_.schema); - EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); - EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); - EXPECT_EQ(dbOption.secOption, KVDBGeneralStore::GetDBSecurity(metaData_.securityLevel)); -} - -/** -* @tc.name: Sync -* @tc.desc: Sync test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(KVDBGeneralStoreTest, Sync001, TestSize.Level0) -{ - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), - (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; - uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; - auto mixMode = GeneralStore::MixMode(syncMode, highMode); - std::string kvQuery = "querytest"; - DistributedKv::KVDBQuery query(kvQuery); - SyncParam syncParam{}; - syncParam.mode = mixMode; - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_NOT_SUPPORT); - GeneralStore::StoreConfig storeConfig; - storeConfig.enableCloud_ = false; - store->SetConfig(storeConfig); - ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_NOT_SUPPORT); - - std::vector devices = { "device1", "device2" }; - syncMode = GeneralStore::SyncMode::CLOUD_END; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); - - syncMode = GeneralStore::SyncMode::NEARBY_PULL; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: SetEqualIdentifier -* @tc.desc: SetEqualIdentifier test. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -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" }; - AppId appId01 = { "ohos.kvdbservice.test01" }; - StoreId storeId01 = { "meta_test_storeid" }; - std::string account = "account"; - store->SetEqualIdentifier(appId01, storeId01, account); - EXPECT_EQ(uuids.empty(), false); - EXPECT_EQ(sameAccountDevs.empty(), false); - delete store; -} } // namespace DistributedDataTest } // namespace OHOS::Test \ No newline at end of file -- Gitee From 8326f6e18263d75f99d2380496b0fdf003d8801a Mon Sep 17 00:00:00 2001 From: gecheng Date: Mon, 4 Aug 2025 15:34:57 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9kv=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gecheng --- .../service/test/kvdb_general_store_test.cpp | 900 ++++++++++++++++++ 1 file changed, 900 insertions(+) diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index c34db72a6..01f1906d8 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -1228,5 +1228,905 @@ HWTEST_F(KVDBGeneralStoreTest, ConstructorTest, TestSize.Level0) EXPECT_EQ(store6->delegate_, nullptr); delete store6; } + +/** +* @tc.name: GetDBPasswordTest_001 +* @tc.desc: get password with all exception branch +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_001, TestSize.Level0) +{ + metaData_.isEncrypt = false; + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + + metaData_.isEncrypt = true; + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + SecretKeyMetaData secretKey; + auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + std::vector randomKey = Random(KEY_LENGTH); + ASSERT_FALSE(randomKey.empty()); + secretKey.sKey = randomKey; + result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); +} + +/** +* @tc.name: GetDBPasswordTest_002 +* @tc.desc: GetDBPassword from meta. +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_002, TestSize.Level0) +{ + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), metaData_, true)); + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_TRUE(dbPassword.GetSize() == 0); +} + +/** +* @tc.name: GetDBPasswordTest_003 +* @tc.desc: GetDBPassword from encrypt meta. +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_003, TestSize.Level0) +{ + ZLOGI("GetDBPasswordTest_002 start"); + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + metaData_.isEncrypt = true; + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); + + auto errCode = CryptoManager::GetInstance().GenerateRootKey(); + EXPECT_EQ(errCode, CryptoManager::ErrCode::SUCCESS); + + std::vector randomKey = Random(KEY_LENGTH); + ASSERT_FALSE(randomKey.empty()); + SecretKeyMetaData secretKey; + secretKey.storeType = metaData_.storeType; + CryptoManager::CryptoParams encryptParams = { .area = metaData_.area, .userId = metaData_.user }; + secretKey.sKey = CryptoManager::GetInstance().Encrypt(randomKey, encryptParams); + ASSERT_FALSE(secretKey.sKey.empty()); + ASSERT_FALSE(encryptParams.nonce.empty()); + secretKey.area = metaData_.area; + secretKey.nonce = encryptParams.nonce; + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true)); + + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_TRUE(dbPassword.GetSize() != 0); + randomKey.assign(randomKey.size(), 0); +} + +/** +* @tc.name: GetDBPasswordTest_004 +* @tc.desc: get password with exception branch +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_004, TestSize.Level0) +{ + metaData_.bundleName = INVALID_APPID; + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + + SecretKeyMetaData secretKey; + auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + auto key = Random(KEY_LENGTH); + ASSERT_FALSE(key.empty()); + secretKey.sKey = key; + result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + MetaDataManager::GetInstance().DelMeta(metaData_.GetSecretKey(), true); +} + +/** +* @tc.name: GetDBSecurityTest +* @tc.desc: GetDBSecurity +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBSecurityTest, TestSize.Level0) +{ + auto dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::INVALID_LABEL); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::NO_LABEL); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S0); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S0); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S1); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S1); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S2); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S2); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S3); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S3); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::SECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S4); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S4); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + auto action = static_cast(SecurityLevel::S4 + 1); + dbSecurity = KVDBGeneralStore::GetDBSecurity(action); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); +} + +/** +* @tc.name: GetDBOptionTest +* @tc.desc: GetDBOption from meta and dbPassword +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBOptionTest, TestSize.Level0) +{ + metaData_.isEncrypt = true; + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); + EXPECT_EQ(dbOption.syncDualTupleMode, true); + EXPECT_EQ(dbOption.createIfNecessary, false); + EXPECT_EQ(dbOption.isMemoryDb, false); + EXPECT_EQ(dbOption.isEncryptedDb, metaData_.isEncrypt); + EXPECT_EQ(dbOption.isNeedCompressOnSync, metaData_.isNeedCompress); + EXPECT_EQ(dbOption.schema, metaData_.schema); + EXPECT_EQ(dbOption.passwd, dbPassword); + EXPECT_EQ(dbOption.cipher, DistributedDB::CipherType::AES_256_GCM); + EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); + EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); + EXPECT_EQ(dbOption.secOption, KVDBGeneralStore::GetDBSecurity(metaData_.securityLevel)); +} + +/** +* @tc.name: CloseTest +* @tc.desc: Close kvdb general store and test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, CloseTest, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + auto ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_OK); + + KvStoreNbDelegateMock mockDelegate; + mockDelegate.taskCountMock_ = 1; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_BUSY); +} + +/** +* @tc.name: Close +* @tc.desc: RdbGeneralStore Close test +* @tc.type: FUNC +* @tc.require: +* @tc.author: shaoyuanzhao +*/ +HWTEST_F(KVDBGeneralStoreTest, BusyClose, TestSize.Level0) +{ + auto store = std::make_shared(metaData_); + ASSERT_NE(store, nullptr); + std::thread thread([store]() { + std::unique_lockrwMutex_)> lock(store->rwMutex_); + std::this_thread::sleep_for(std::chrono::seconds(1)); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + auto ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_BUSY); + thread.join(); + ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_OK); +} + +/** +* @tc.name: SyncTest +* @tc.desc: Sync. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) +{ + ZLOGI("SyncTest start"); + mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + uint32_t syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; + uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; + auto mixMode = GeneralStore::MixMode(syncMode, highMode); + std::string kvQuery = ""; + DistributedKv::KVDBQuery query(kvQuery); + SyncParam syncParam{}; + syncParam.mode = mixMode; + auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_NE(ret.first, GeneralError::E_OK); + auto status = store->Close(); + EXPECT_EQ(status, GeneralError::E_OK); + delete store; +} + +/** +* @tc.name: BindTest +* @tc.desc: Bind test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, BindTest, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + DistributedData::Database database; + std::map bindInfos; + GeneralStore::CloudConfig config; + EXPECT_EQ(bindInfos.empty(), true); + auto ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + + std::shared_ptr db; + std::shared_ptr loader; + GeneralStore::BindInfo bindInfo1(db, loader); + uint32_t key = 1; + bindInfos[key] = bindInfo1; + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + std::shared_ptr dbs = std::make_shared(); + std::shared_ptr loaders = std::make_shared(); + GeneralStore::BindInfo bindInfo2(dbs, loaders); + bindInfos[key] = bindInfo2; + EXPECT_EQ(store->IsBound(key), false); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); + + store->bindInfos_.clear(); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + EXPECT_EQ(store->IsBound(key), false); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + + EXPECT_EQ(store->IsBound(key), true); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); +} + +/** +* @tc.name: BindWithDifferentUsersTest +* @tc.desc: Bind test the functionality of different users. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, BindWithDifferentUsersTest, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + ASSERT_NE(store, nullptr); + DistributedData::Database database; + std::map bindInfos; + GeneralStore::CloudConfig config; + + std::shared_ptr dbs = std::make_shared(); + std::shared_ptr loaders = std::make_shared(); + GeneralStore::BindInfo bindInfo(dbs, loaders); + uint32_t key0 = 100; + uint32_t key1 = 101; + bindInfos[key0] = bindInfo; + bindInfos[key1] = bindInfo; + EXPECT_EQ(store->IsBound(key0), false); + EXPECT_EQ(store->IsBound(key1), false); + auto ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + EXPECT_EQ(store->IsBound(key0), true); + EXPECT_EQ(store->IsBound(key1), true); + + uint32_t key2 = 102; + bindInfos[key2] = bindInfo; + EXPECT_EQ(store->IsBound(key2), false); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + EXPECT_EQ(store->IsBound(key2), true); +} + +/** +* @tc.name: GetDBSyncCompleteCB +* @tc.desc: GetDBSyncCompleteCB test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBSyncCompleteCB, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + GeneralStore::DetailAsync async; + EXPECT_EQ(async, nullptr); + KVDBGeneralStore::DBSyncCallback ret = store->GetDBSyncCompleteCB(async); + EXPECT_NE(ret, nullptr); + auto asyncs = [](const GenDetails &result) {}; + EXPECT_NE(asyncs, nullptr); + ret = store->GetDBSyncCompleteCB(asyncs); + EXPECT_NE(ret, nullptr); +} + +/** +* @tc.name: CloudSync001 +* @tc.desc: Test the scenario where the QueryUsers return false in the CloudSync function. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + store->SetEqualIdentifier(bundleName, storeName); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + std::vector devices = { "device1", "device2" }; + auto asyncs = [](const GenDetails &result) {}; + store->storeInfo_.user = 0; + auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; + store->SetEqualIdentifier(bundleName, storeName); + std::string prepareTraceId; + std::vector users; + 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); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: CloudSync002 +* @tc.desc: CloudSync test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + store->SetEqualIdentifier(bundleName, storeName); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + std::vector devices = { "device1", "device2" }; + auto asyncs = [](const GenDetails &result) {}; + store->storeInfo_.user = 0; + auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; + store->SetEqualIdentifier(bundleName, storeName); + std::string prepareTraceId; + std::vector users; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) + .Times(1) + .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); + + store->storeInfo_.user = 1; + cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_CLOUD_FORCE_PUSH; + ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); + EXPECT_EQ(ret, DBStatus::OK); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: CloudSync003 +* @tc.desc: Test the scenario where the QueryUsers return true in the CloudSync function. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, CloudSync003, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + store->SetEqualIdentifier(bundleName, storeName); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + std::vector devices = { "device1", "device2" }; + auto asyncs = [](const GenDetails &result) {}; + store->storeInfo_.user = 0; + auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; + store->SetEqualIdentifier(bundleName, storeName); + std::string prepareTraceId; + std::vector users = {0, 1}; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) + .Times(1) + .WillOnce(DoAll( + SetArgReferee<0>(users), + Return(true))); + auto ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); + EXPECT_EQ(ret, DBStatus::OK); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: GetIdentifierParams +* @tc.desc: GetIdentifierParams test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: wangbin +*/ +HWTEST_F(KVDBGeneralStoreTest, GetIdentifierParams, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + std::vector sameAccountDevs{}; + 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); + EXPECT_EQ(DMAdapter::GetInstance().GetAuthType(devId), 0); // NO_ACCOUNT + } + EXPECT_EQ(sameAccountDevs.empty(), false); +} + +/** +* @tc.name: Sync002 +* @tc.desc: Sync test the functionality of 3 < syncMode < 7 branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) +{ + mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; + uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; + auto mixMode = GeneralStore::MixMode(syncMode, highMode); + std::string kvQuery = ""; + DistributedKv::KVDBQuery query(kvQuery); + SyncParam syncParam{}; + syncParam.mode = mixMode; + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + 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; + storeConfig.enableCloud_ = true; + store->SetConfig(storeConfig); + ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_OK); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: Sync003 +* @tc.desc: Sync test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, Sync003, TestSize.Level0) +{ + mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + uint32_t syncMode = GeneralStore::SyncMode::NEARBY_END; + uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; + auto mixMode = GeneralStore::MixMode(syncMode, highMode); + std::string kvQuery = ""; + DistributedKv::KVDBQuery query(kvQuery); + SyncParam syncParam{}; + syncParam.mode = mixMode; + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); + std::vector devices = { "device1", "device2" }; + syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_OK); + + syncMode = GeneralStore::SyncMode::NEARBY_UNSUBSCRIBE_REMOTE; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_OK); + + syncMode = GeneralStore::SyncMode::NEARBY_PULL_PUSH; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_OK); + + syncMode = GeneralStore::SyncMode::MODE_BUTT; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: Clean +* @tc.desc: Clean test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Clean, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + std::vector devices = { "device1", "device2" }; + std::string tableName = "tableName"; + auto ret = store->Clean(devices, -1, tableName); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + ret = store->Clean(devices, 6, tableName); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + ret = store->Clean(devices, GeneralStore::CleanMode::NEARBY_DATA, tableName); + EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); + + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + ret = store->Clean(devices, GeneralStore::CleanMode::CLOUD_INFO, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); + + ret = store->Clean(devices, GeneralStore::CleanMode::CLOUD_DATA, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); + + ret = store->Clean({}, GeneralStore::CleanMode::NEARBY_DATA, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); + ret = store->Clean(devices, GeneralStore::CleanMode::NEARBY_DATA, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); + + ret = store->Clean(devices, GeneralStore::CleanMode::LOCAL_DATA, tableName); + EXPECT_EQ(ret, GeneralError::E_ERROR); + + ret = store->Clean(devices, GeneralStore::CleanMode::CLEAN_WATER, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); +} + +/** +* @tc.name: Watch +* @tc.desc: Watch and Unwatch test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Watch, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + DistributedDataTest::MockGeneralWatcher watcher; + auto ret = store->Watch(GeneralWatcher::Origin::ORIGIN_CLOUD, watcher); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_CLOUD, watcher); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + + ret = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(ret, GeneralError::E_OK); + ret = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + + ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(ret, GeneralError::E_OK); + ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); +} + +/** +* @tc.name: Release +* @tc.desc: Release and AddRef test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Release, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + auto ret = store->Release(); + EXPECT_EQ(ret, 0); + store = new (std::nothrow) KVDBGeneralStore(metaData_); + store->ref_ = 0; + ret = store->Release(); + EXPECT_EQ(ret, 0); + store->ref_ = 2; + ret = store->Release(); + EXPECT_EQ(ret, 1); + + ret = store->AddRef(); + EXPECT_EQ(ret, 2); + store->ref_ = 0; + ret = store->AddRef(); + EXPECT_EQ(ret, 0); +} + +/** +* @tc.name: ConvertStatus +* @tc.desc: ConvertStatus test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, ConvertStatus, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + auto ret = store->ConvertStatus(DBStatus::OK); + EXPECT_EQ(ret, GeneralError::E_OK); + ret = store->ConvertStatus(DBStatus::CLOUD_NETWORK_ERROR); + EXPECT_EQ(ret, GeneralError::E_NETWORK_ERROR); + ret = store->ConvertStatus(DBStatus::CLOUD_LOCK_ERROR); + EXPECT_EQ(ret, GeneralError::E_LOCKED_BY_OTHERS); + ret = store->ConvertStatus(DBStatus::CLOUD_FULL_RECORDS); + EXPECT_EQ(ret, GeneralError::E_RECODE_LIMIT_EXCEEDED); + ret = store->ConvertStatus(DBStatus::CLOUD_ASSET_SPACE_INSUFFICIENT); + EXPECT_EQ(ret, GeneralError::E_NO_SPACE_FOR_ASSET); + ret = store->ConvertStatus(DBStatus::CLOUD_SYNC_TASK_MERGED); + EXPECT_EQ(ret, GeneralError::E_SYNC_TASK_MERGED); + ret = store->ConvertStatus(DBStatus::DB_ERROR); + EXPECT_EQ(ret, GeneralError::E_DB_ERROR); +} + +/** +* @tc.name: OnChange +* @tc.desc: OnChange test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, OnChange, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + DistributedDataTest::MockGeneralWatcher watcher; + DistributedDataTest::MockKvStoreChangedData data; + DistributedDB::ChangedData changedData; + store->observer_.OnChange(data); + store->observer_.OnChange(DistributedDB::Origin::ORIGIN_CLOUD, "originalId", std::move(changedData)); + auto result = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(result, GeneralError::E_OK); + store->observer_.OnChange(data); + store->observer_.OnChange(DistributedDB::Origin::ORIGIN_CLOUD, "originalId", std::move(changedData)); + result = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(result, GeneralError::E_OK); +} + +/** +* @tc.name: Delete +* @tc.desc: Delete test the function. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Delete, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::DEVICE_COLLABORATION); + store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::DEVICE_COLLABORATION); + DistributedData::VBuckets values; + auto ret = store->Insert("table", std::move(values)); + EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); + + DistributedData::Values args; + store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); + store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); + ret = store->Delete("table", "sql", std::move(args)); + EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); +} + +/** +* @tc.name: Query001 +* @tc.desc: KVDBGeneralStoreTest Query function test +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Query001, TestSize.Level1) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + std::string table = "table"; + std::string sql = "sql"; + auto [errCode, result] = store->Query(table, sql, {}); + EXPECT_EQ(errCode, GeneralError::E_NOT_SUPPORT); + EXPECT_EQ(result, nullptr); +} + +/** +* @tc.name: Query002 +* @tc.desc: KVDBGeneralStoreTest Query function test +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Query002, TestSize.Level1) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + std::string table = "table"; + MockQuery query; + auto [errCode, result] = store->Query(table, query); + EXPECT_EQ(errCode, GeneralError::E_NOT_SUPPORT); + EXPECT_EQ(result, nullptr); +} + +/** +* @tc.name: GetDBOption +* @tc.desc: GetDBOption from meta and dbPassword +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBOption001, TestSize.Level0) +{ + metaData_.isEncrypt = false; + metaData_.appId = "distributeddata"; + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); + EXPECT_FALSE(MetaDataManager::GetInstance().LoadMeta(metaData_.GetKeyLocal(), metaData_, true)); + EXPECT_EQ(metaData_.appId, Bootstrap::GetInstance().GetProcessLabel()); + EXPECT_EQ(dbOption.createIfNecessary, false); + EXPECT_EQ(dbOption.isMemoryDb, false); + EXPECT_EQ(dbOption.isEncryptedDb, metaData_.isEncrypt); + EXPECT_EQ(dbOption.isNeedCompressOnSync, metaData_.isNeedCompress); + EXPECT_EQ(dbOption.schema, metaData_.schema); + EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); + EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); + EXPECT_EQ(dbOption.secOption, KVDBGeneralStore::GetDBSecurity(metaData_.securityLevel)); +} + +/** +* @tc.name: Sync +* @tc.desc: Sync test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(KVDBGeneralStoreTest, Sync001, TestSize.Level0) +{ + mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; + uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; + auto mixMode = GeneralStore::MixMode(syncMode, highMode); + std::string kvQuery = "querytest"; + DistributedKv::KVDBQuery query(kvQuery); + SyncParam syncParam{}; + syncParam.mode = mixMode; + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_NOT_SUPPORT); + GeneralStore::StoreConfig storeConfig; + storeConfig.enableCloud_ = false; + store->SetConfig(storeConfig); + ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_NOT_SUPPORT); + + std::vector devices = { "device1", "device2" }; + syncMode = GeneralStore::SyncMode::CLOUD_END; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); + + syncMode = GeneralStore::SyncMode::NEARBY_PULL; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: SetEqualIdentifier +* @tc.desc: SetEqualIdentifier test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +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" }; + AppId appId01 = { "ohos.kvdbservice.test01" }; + StoreId storeId01 = { "meta_test_storeid" }; + std::string account = "account"; + store->SetEqualIdentifier(appId01, storeId01, account); + EXPECT_EQ(uuids.empty(), false); + EXPECT_EQ(sameAccountDevs.empty(), false); + delete store; +} + +/** +* @tc.name: GetIdentifierParams +* @tc.desc: GetIdentifierParams test. +* @tc.type: FUNC +* @tc.require: +*/ +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" }; + store->GetIdentifierParams(sameAccountDevs, uuids, 1); // + for (const auto &devId : uuids) { + EXPECT_EQ(DMAdapter::GetInstance().IsOHOSType(devId), false); + EXPECT_EQ(DMAdapter::GetInstance().GetAuthType(devId), 0); // + } + EXPECT_EQ(sameAccountDevs.empty(), false); + delete store; +} + +/** +* @tc.name: ConvertStatus +* @tc.desc: ConvertStatus test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, ConvertStatus001, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + auto ret = store->ConvertStatus(DBStatus::NOT_SUPPORT); + EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); + DBStatus unknownStatus = static_cast(0xDEAD); + ret = store->ConvertStatus(unknownStatus); + EXPECT_EQ(ret, GeneralError::E_ERROR); + delete store; +} } // namespace DistributedDataTest } // namespace OHOS::Test \ No newline at end of file -- Gitee From b97c81306ee055803d74b7dc9b9a72ea126a4280 Mon Sep 17 00:00:00 2001 From: gecheng Date: Mon, 4 Aug 2025 15:38:55 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9kv=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gecheng --- .../service/test/kvdb_general_store_test.cpp | 900 ------------------ 1 file changed, 900 deletions(-) diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index 01f1906d8..c34db72a6 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -1228,905 +1228,5 @@ HWTEST_F(KVDBGeneralStoreTest, ConstructorTest, TestSize.Level0) EXPECT_EQ(store6->delegate_, nullptr); delete store6; } - -/** -* @tc.name: GetDBPasswordTest_001 -* @tc.desc: get password with all exception branch -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_001, TestSize.Level0) -{ - metaData_.isEncrypt = false; - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - - metaData_.isEncrypt = true; - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - SecretKeyMetaData secretKey; - auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - std::vector randomKey = Random(KEY_LENGTH); - ASSERT_FALSE(randomKey.empty()); - secretKey.sKey = randomKey; - result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); -} - -/** -* @tc.name: GetDBPasswordTest_002 -* @tc.desc: GetDBPassword from meta. -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_002, TestSize.Level0) -{ - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), metaData_, true)); - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_TRUE(dbPassword.GetSize() == 0); -} - -/** -* @tc.name: GetDBPasswordTest_003 -* @tc.desc: GetDBPassword from encrypt meta. -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_003, TestSize.Level0) -{ - ZLOGI("GetDBPasswordTest_002 start"); - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - metaData_.isEncrypt = true; - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); - - auto errCode = CryptoManager::GetInstance().GenerateRootKey(); - EXPECT_EQ(errCode, CryptoManager::ErrCode::SUCCESS); - - std::vector randomKey = Random(KEY_LENGTH); - ASSERT_FALSE(randomKey.empty()); - SecretKeyMetaData secretKey; - secretKey.storeType = metaData_.storeType; - CryptoManager::CryptoParams encryptParams = { .area = metaData_.area, .userId = metaData_.user }; - secretKey.sKey = CryptoManager::GetInstance().Encrypt(randomKey, encryptParams); - ASSERT_FALSE(secretKey.sKey.empty()); - ASSERT_FALSE(encryptParams.nonce.empty()); - secretKey.area = metaData_.area; - secretKey.nonce = encryptParams.nonce; - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true)); - - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_TRUE(dbPassword.GetSize() != 0); - randomKey.assign(randomKey.size(), 0); -} - -/** -* @tc.name: GetDBPasswordTest_004 -* @tc.desc: get password with exception branch -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_004, TestSize.Level0) -{ - metaData_.bundleName = INVALID_APPID; - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - - SecretKeyMetaData secretKey; - auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - auto key = Random(KEY_LENGTH); - ASSERT_FALSE(key.empty()); - secretKey.sKey = key; - result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - MetaDataManager::GetInstance().DelMeta(metaData_.GetSecretKey(), true); -} - -/** -* @tc.name: GetDBSecurityTest -* @tc.desc: GetDBSecurity -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBSecurityTest, TestSize.Level0) -{ - auto dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::INVALID_LABEL); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::NO_LABEL); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S0); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S0); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S1); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S1); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S2); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S2); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S3); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S3); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::SECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S4); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S4); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - auto action = static_cast(SecurityLevel::S4 + 1); - dbSecurity = KVDBGeneralStore::GetDBSecurity(action); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); -} - -/** -* @tc.name: GetDBOptionTest -* @tc.desc: GetDBOption from meta and dbPassword -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBOptionTest, TestSize.Level0) -{ - metaData_.isEncrypt = true; - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); - EXPECT_EQ(dbOption.syncDualTupleMode, true); - EXPECT_EQ(dbOption.createIfNecessary, false); - EXPECT_EQ(dbOption.isMemoryDb, false); - EXPECT_EQ(dbOption.isEncryptedDb, metaData_.isEncrypt); - EXPECT_EQ(dbOption.isNeedCompressOnSync, metaData_.isNeedCompress); - EXPECT_EQ(dbOption.schema, metaData_.schema); - EXPECT_EQ(dbOption.passwd, dbPassword); - EXPECT_EQ(dbOption.cipher, DistributedDB::CipherType::AES_256_GCM); - EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); - EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); - EXPECT_EQ(dbOption.secOption, KVDBGeneralStore::GetDBSecurity(metaData_.securityLevel)); -} - -/** -* @tc.name: CloseTest -* @tc.desc: Close kvdb general store and test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, CloseTest, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - auto ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_OK); - - KvStoreNbDelegateMock mockDelegate; - mockDelegate.taskCountMock_ = 1; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_BUSY); -} - -/** -* @tc.name: Close -* @tc.desc: RdbGeneralStore Close test -* @tc.type: FUNC -* @tc.require: -* @tc.author: shaoyuanzhao -*/ -HWTEST_F(KVDBGeneralStoreTest, BusyClose, TestSize.Level0) -{ - auto store = std::make_shared(metaData_); - ASSERT_NE(store, nullptr); - std::thread thread([store]() { - std::unique_lockrwMutex_)> lock(store->rwMutex_); - std::this_thread::sleep_for(std::chrono::seconds(1)); - }); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - auto ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_BUSY); - thread.join(); - ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_OK); -} - -/** -* @tc.name: SyncTest -* @tc.desc: Sync. -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) -{ - ZLOGI("SyncTest start"); - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), - (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - uint32_t syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; - uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; - auto mixMode = GeneralStore::MixMode(syncMode, highMode); - std::string kvQuery = ""; - DistributedKv::KVDBQuery query(kvQuery); - SyncParam syncParam{}; - syncParam.mode = mixMode; - auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_NE(ret.first, GeneralError::E_OK); - auto status = store->Close(); - EXPECT_EQ(status, GeneralError::E_OK); - delete store; -} - -/** -* @tc.name: BindTest -* @tc.desc: Bind test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, BindTest, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - DistributedData::Database database; - std::map bindInfos; - GeneralStore::CloudConfig config; - EXPECT_EQ(bindInfos.empty(), true); - auto ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - - std::shared_ptr db; - std::shared_ptr loader; - GeneralStore::BindInfo bindInfo1(db, loader); - uint32_t key = 1; - bindInfos[key] = bindInfo1; - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - std::shared_ptr dbs = std::make_shared(); - std::shared_ptr loaders = std::make_shared(); - GeneralStore::BindInfo bindInfo2(dbs, loaders); - bindInfos[key] = bindInfo2; - EXPECT_EQ(store->IsBound(key), false); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); - - store->bindInfos_.clear(); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - EXPECT_EQ(store->IsBound(key), false); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - - EXPECT_EQ(store->IsBound(key), true); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); -} - -/** -* @tc.name: BindWithDifferentUsersTest -* @tc.desc: Bind test the functionality of different users. -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, BindWithDifferentUsersTest, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - ASSERT_NE(store, nullptr); - DistributedData::Database database; - std::map bindInfos; - GeneralStore::CloudConfig config; - - std::shared_ptr dbs = std::make_shared(); - std::shared_ptr loaders = std::make_shared(); - GeneralStore::BindInfo bindInfo(dbs, loaders); - uint32_t key0 = 100; - uint32_t key1 = 101; - bindInfos[key0] = bindInfo; - bindInfos[key1] = bindInfo; - EXPECT_EQ(store->IsBound(key0), false); - EXPECT_EQ(store->IsBound(key1), false); - auto ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - EXPECT_EQ(store->IsBound(key0), true); - EXPECT_EQ(store->IsBound(key1), true); - - uint32_t key2 = 102; - bindInfos[key2] = bindInfo; - EXPECT_EQ(store->IsBound(key2), false); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - EXPECT_EQ(store->IsBound(key2), true); -} - -/** -* @tc.name: GetDBSyncCompleteCB -* @tc.desc: GetDBSyncCompleteCB test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBSyncCompleteCB, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - GeneralStore::DetailAsync async; - EXPECT_EQ(async, nullptr); - KVDBGeneralStore::DBSyncCallback ret = store->GetDBSyncCompleteCB(async); - EXPECT_NE(ret, nullptr); - auto asyncs = [](const GenDetails &result) {}; - EXPECT_NE(asyncs, nullptr); - ret = store->GetDBSyncCompleteCB(asyncs); - EXPECT_NE(ret, nullptr); -} - -/** -* @tc.name: CloudSync001 -* @tc.desc: Test the scenario where the QueryUsers return false in the CloudSync function. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - std::vector devices = { "device1", "device2" }; - auto asyncs = [](const GenDetails &result) {}; - store->storeInfo_.user = 0; - auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; - store->SetEqualIdentifier(bundleName, storeName); - std::string prepareTraceId; - std::vector users; - 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); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: CloudSync002 -* @tc.desc: CloudSync test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - std::vector devices = { "device1", "device2" }; - auto asyncs = [](const GenDetails &result) {}; - store->storeInfo_.user = 0; - auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; - store->SetEqualIdentifier(bundleName, storeName); - std::string prepareTraceId; - std::vector users; - EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) - .Times(1) - .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); - - store->storeInfo_.user = 1; - cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_CLOUD_FORCE_PUSH; - ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); - EXPECT_EQ(ret, DBStatus::OK); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: CloudSync003 -* @tc.desc: Test the scenario where the QueryUsers return true in the CloudSync function. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, CloudSync003, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - std::vector devices = { "device1", "device2" }; - auto asyncs = [](const GenDetails &result) {}; - store->storeInfo_.user = 0; - auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; - store->SetEqualIdentifier(bundleName, storeName); - std::string prepareTraceId; - std::vector users = {0, 1}; - EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) - .Times(1) - .WillOnce(DoAll( - SetArgReferee<0>(users), - Return(true))); - auto ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); - EXPECT_EQ(ret, DBStatus::OK); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: GetIdentifierParams -* @tc.desc: GetIdentifierParams test. -* @tc.type: FUNC -* @tc.require: -* @tc.author: wangbin -*/ -HWTEST_F(KVDBGeneralStoreTest, GetIdentifierParams, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - std::vector sameAccountDevs{}; - 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); - EXPECT_EQ(DMAdapter::GetInstance().GetAuthType(devId), 0); // NO_ACCOUNT - } - EXPECT_EQ(sameAccountDevs.empty(), false); -} - -/** -* @tc.name: Sync002 -* @tc.desc: Sync test the functionality of 3 < syncMode < 7 branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) -{ - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), - (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; - uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; - auto mixMode = GeneralStore::MixMode(syncMode, highMode); - std::string kvQuery = ""; - DistributedKv::KVDBQuery query(kvQuery); - SyncParam syncParam{}; - syncParam.mode = mixMode; - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - 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; - storeConfig.enableCloud_ = true; - store->SetConfig(storeConfig); - ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_OK); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: Sync003 -* @tc.desc: Sync test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, Sync003, TestSize.Level0) -{ - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), - (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - uint32_t syncMode = GeneralStore::SyncMode::NEARBY_END; - uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; - auto mixMode = GeneralStore::MixMode(syncMode, highMode); - std::string kvQuery = ""; - DistributedKv::KVDBQuery query(kvQuery); - SyncParam syncParam{}; - syncParam.mode = mixMode; - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); - std::vector devices = { "device1", "device2" }; - syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_OK); - - syncMode = GeneralStore::SyncMode::NEARBY_UNSUBSCRIBE_REMOTE; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_OK); - - syncMode = GeneralStore::SyncMode::NEARBY_PULL_PUSH; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_OK); - - syncMode = GeneralStore::SyncMode::MODE_BUTT; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: Clean -* @tc.desc: Clean test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Clean, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - std::vector devices = { "device1", "device2" }; - std::string tableName = "tableName"; - auto ret = store->Clean(devices, -1, tableName); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - ret = store->Clean(devices, 6, tableName); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - ret = store->Clean(devices, GeneralStore::CleanMode::NEARBY_DATA, tableName); - EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); - - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - ret = store->Clean(devices, GeneralStore::CleanMode::CLOUD_INFO, tableName); - EXPECT_EQ(ret, GeneralError::E_OK); - - ret = store->Clean(devices, GeneralStore::CleanMode::CLOUD_DATA, tableName); - EXPECT_EQ(ret, GeneralError::E_OK); - - ret = store->Clean({}, GeneralStore::CleanMode::NEARBY_DATA, tableName); - EXPECT_EQ(ret, GeneralError::E_OK); - ret = store->Clean(devices, GeneralStore::CleanMode::NEARBY_DATA, tableName); - EXPECT_EQ(ret, GeneralError::E_OK); - - ret = store->Clean(devices, GeneralStore::CleanMode::LOCAL_DATA, tableName); - EXPECT_EQ(ret, GeneralError::E_ERROR); - - ret = store->Clean(devices, GeneralStore::CleanMode::CLEAN_WATER, tableName); - EXPECT_EQ(ret, GeneralError::E_OK); -} - -/** -* @tc.name: Watch -* @tc.desc: Watch and Unwatch test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Watch, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - DistributedDataTest::MockGeneralWatcher watcher; - auto ret = store->Watch(GeneralWatcher::Origin::ORIGIN_CLOUD, watcher); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_CLOUD, watcher); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - - ret = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(ret, GeneralError::E_OK); - ret = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - - ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(ret, GeneralError::E_OK); - ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); -} - -/** -* @tc.name: Release -* @tc.desc: Release and AddRef test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Release, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - auto ret = store->Release(); - EXPECT_EQ(ret, 0); - store = new (std::nothrow) KVDBGeneralStore(metaData_); - store->ref_ = 0; - ret = store->Release(); - EXPECT_EQ(ret, 0); - store->ref_ = 2; - ret = store->Release(); - EXPECT_EQ(ret, 1); - - ret = store->AddRef(); - EXPECT_EQ(ret, 2); - store->ref_ = 0; - ret = store->AddRef(); - EXPECT_EQ(ret, 0); -} - -/** -* @tc.name: ConvertStatus -* @tc.desc: ConvertStatus test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, ConvertStatus, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - auto ret = store->ConvertStatus(DBStatus::OK); - EXPECT_EQ(ret, GeneralError::E_OK); - ret = store->ConvertStatus(DBStatus::CLOUD_NETWORK_ERROR); - EXPECT_EQ(ret, GeneralError::E_NETWORK_ERROR); - ret = store->ConvertStatus(DBStatus::CLOUD_LOCK_ERROR); - EXPECT_EQ(ret, GeneralError::E_LOCKED_BY_OTHERS); - ret = store->ConvertStatus(DBStatus::CLOUD_FULL_RECORDS); - EXPECT_EQ(ret, GeneralError::E_RECODE_LIMIT_EXCEEDED); - ret = store->ConvertStatus(DBStatus::CLOUD_ASSET_SPACE_INSUFFICIENT); - EXPECT_EQ(ret, GeneralError::E_NO_SPACE_FOR_ASSET); - ret = store->ConvertStatus(DBStatus::CLOUD_SYNC_TASK_MERGED); - EXPECT_EQ(ret, GeneralError::E_SYNC_TASK_MERGED); - ret = store->ConvertStatus(DBStatus::DB_ERROR); - EXPECT_EQ(ret, GeneralError::E_DB_ERROR); -} - -/** -* @tc.name: OnChange -* @tc.desc: OnChange test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, OnChange, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - DistributedDataTest::MockGeneralWatcher watcher; - DistributedDataTest::MockKvStoreChangedData data; - DistributedDB::ChangedData changedData; - store->observer_.OnChange(data); - store->observer_.OnChange(DistributedDB::Origin::ORIGIN_CLOUD, "originalId", std::move(changedData)); - auto result = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(result, GeneralError::E_OK); - store->observer_.OnChange(data); - store->observer_.OnChange(DistributedDB::Origin::ORIGIN_CLOUD, "originalId", std::move(changedData)); - result = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(result, GeneralError::E_OK); -} - -/** -* @tc.name: Delete -* @tc.desc: Delete test the function. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Delete, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::DEVICE_COLLABORATION); - store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::DEVICE_COLLABORATION); - DistributedData::VBuckets values; - auto ret = store->Insert("table", std::move(values)); - EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); - - DistributedData::Values args; - store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); - store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); - ret = store->Delete("table", "sql", std::move(args)); - EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); -} - -/** -* @tc.name: Query001 -* @tc.desc: KVDBGeneralStoreTest Query function test -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Query001, TestSize.Level1) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - std::string table = "table"; - std::string sql = "sql"; - auto [errCode, result] = store->Query(table, sql, {}); - EXPECT_EQ(errCode, GeneralError::E_NOT_SUPPORT); - EXPECT_EQ(result, nullptr); -} - -/** -* @tc.name: Query002 -* @tc.desc: KVDBGeneralStoreTest Query function test -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Query002, TestSize.Level1) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - std::string table = "table"; - MockQuery query; - auto [errCode, result] = store->Query(table, query); - EXPECT_EQ(errCode, GeneralError::E_NOT_SUPPORT); - EXPECT_EQ(result, nullptr); -} - -/** -* @tc.name: GetDBOption -* @tc.desc: GetDBOption from meta and dbPassword -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBOption001, TestSize.Level0) -{ - metaData_.isEncrypt = false; - metaData_.appId = "distributeddata"; - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); - EXPECT_FALSE(MetaDataManager::GetInstance().LoadMeta(metaData_.GetKeyLocal(), metaData_, true)); - EXPECT_EQ(metaData_.appId, Bootstrap::GetInstance().GetProcessLabel()); - EXPECT_EQ(dbOption.createIfNecessary, false); - EXPECT_EQ(dbOption.isMemoryDb, false); - EXPECT_EQ(dbOption.isEncryptedDb, metaData_.isEncrypt); - EXPECT_EQ(dbOption.isNeedCompressOnSync, metaData_.isNeedCompress); - EXPECT_EQ(dbOption.schema, metaData_.schema); - EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); - EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); - EXPECT_EQ(dbOption.secOption, KVDBGeneralStore::GetDBSecurity(metaData_.securityLevel)); -} - -/** -* @tc.name: Sync -* @tc.desc: Sync test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(KVDBGeneralStoreTest, Sync001, TestSize.Level0) -{ - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), - (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; - uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; - auto mixMode = GeneralStore::MixMode(syncMode, highMode); - std::string kvQuery = "querytest"; - DistributedKv::KVDBQuery query(kvQuery); - SyncParam syncParam{}; - syncParam.mode = mixMode; - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_NOT_SUPPORT); - GeneralStore::StoreConfig storeConfig; - storeConfig.enableCloud_ = false; - store->SetConfig(storeConfig); - ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_NOT_SUPPORT); - - std::vector devices = { "device1", "device2" }; - syncMode = GeneralStore::SyncMode::CLOUD_END; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); - - syncMode = GeneralStore::SyncMode::NEARBY_PULL; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: SetEqualIdentifier -* @tc.desc: SetEqualIdentifier test. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -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" }; - AppId appId01 = { "ohos.kvdbservice.test01" }; - StoreId storeId01 = { "meta_test_storeid" }; - std::string account = "account"; - store->SetEqualIdentifier(appId01, storeId01, account); - EXPECT_EQ(uuids.empty(), false); - EXPECT_EQ(sameAccountDevs.empty(), false); - delete store; -} - -/** -* @tc.name: GetIdentifierParams -* @tc.desc: GetIdentifierParams test. -* @tc.type: FUNC -* @tc.require: -*/ -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" }; - store->GetIdentifierParams(sameAccountDevs, uuids, 1); // - for (const auto &devId : uuids) { - EXPECT_EQ(DMAdapter::GetInstance().IsOHOSType(devId), false); - EXPECT_EQ(DMAdapter::GetInstance().GetAuthType(devId), 0); // - } - EXPECT_EQ(sameAccountDevs.empty(), false); - delete store; -} - -/** -* @tc.name: ConvertStatus -* @tc.desc: ConvertStatus test. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, ConvertStatus001, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - auto ret = store->ConvertStatus(DBStatus::NOT_SUPPORT); - EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); - DBStatus unknownStatus = static_cast(0xDEAD); - ret = store->ConvertStatus(unknownStatus); - EXPECT_EQ(ret, GeneralError::E_ERROR); - delete store; -} } // namespace DistributedDataTest } // namespace OHOS::Test \ No newline at end of file -- Gitee From 7647d51b7f98a198ffff5e25bf4aa9e34fd6ec3e Mon Sep 17 00:00:00 2001 From: gecheng Date: Mon, 4 Aug 2025 15:41:51 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9kv=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gecheng --- .../service/test/kvdb_general_store_test.cpp | 1473 +++++++++++++++++ 1 file changed, 1473 insertions(+) diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index c34db72a6..ed4704b81 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -1228,5 +1228,1478 @@ HWTEST_F(KVDBGeneralStoreTest, ConstructorTest, TestSize.Level0) EXPECT_EQ(store6->delegate_, nullptr); delete store6; } + +/** +* @tc.name: GetDBPasswordTest_001 +* @tc.desc: get password with all exception branch +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_001, TestSize.Level0) +{ + metaData_.isEncrypt = false; + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + + metaData_.isEncrypt = true; + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + SecretKeyMetaData secretKey; + auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + std::vector randomKey = Random(KEY_LENGTH); + ASSERT_FALSE(randomKey.empty()); + secretKey.sKey = randomKey; + result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); +} + +/** +* @tc.name: GetDBPasswordTest_002 +* @tc.desc: GetDBPassword from meta. +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_002, TestSize.Level0) +{ + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), metaData_, true)); + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_TRUE(dbPassword.GetSize() == 0); +} + +/** +* @tc.name: GetDBPasswordTest_003 +* @tc.desc: GetDBPassword from encrypt meta. +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_003, TestSize.Level0) +{ + ZLOGI("GetDBPasswordTest_002 start"); + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + metaData_.isEncrypt = true; + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); + + auto errCode = CryptoManager::GetInstance().GenerateRootKey(); + EXPECT_EQ(errCode, CryptoManager::ErrCode::SUCCESS); + + std::vector randomKey = Random(KEY_LENGTH); + ASSERT_FALSE(randomKey.empty()); + SecretKeyMetaData secretKey; + secretKey.storeType = metaData_.storeType; + CryptoManager::CryptoParams encryptParams = { .area = metaData_.area, .userId = metaData_.user }; + secretKey.sKey = CryptoManager::GetInstance().Encrypt(randomKey, encryptParams); + ASSERT_FALSE(secretKey.sKey.empty()); + ASSERT_FALSE(encryptParams.nonce.empty()); + secretKey.area = metaData_.area; + secretKey.nonce = encryptParams.nonce; + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true)); + + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_TRUE(dbPassword.GetSize() != 0); + randomKey.assign(randomKey.size(), 0); +} + +/** +* @tc.name: GetDBPasswordTest_004 +* @tc.desc: get password with exception branch +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_004, TestSize.Level0) +{ + metaData_.bundleName = INVALID_APPID; + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + + SecretKeyMetaData secretKey; + auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + auto key = Random(KEY_LENGTH); + ASSERT_FALSE(key.empty()); + secretKey.sKey = key; + result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + MetaDataManager::GetInstance().DelMeta(metaData_.GetSecretKey(), true); +} + +/** +* @tc.name: GetDBSecurityTest +* @tc.desc: GetDBSecurity +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBSecurityTest, TestSize.Level0) +{ + auto dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::INVALID_LABEL); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::NO_LABEL); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S0); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S0); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S1); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S1); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S2); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S2); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S3); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S3); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::SECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S4); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S4); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + auto action = static_cast(SecurityLevel::S4 + 1); + dbSecurity = KVDBGeneralStore::GetDBSecurity(action); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); +} + +/** +* @tc.name: GetDBOptionTest +* @tc.desc: GetDBOption from meta and dbPassword +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBOptionTest, TestSize.Level0) +{ + metaData_.isEncrypt = true; + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); + EXPECT_EQ(dbOption.syncDualTupleMode, true); + EXPECT_EQ(dbOption.createIfNecessary, false); + EXPECT_EQ(dbOption.isMemoryDb, false); + EXPECT_EQ(dbOption.isEncryptedDb, metaData_.isEncrypt); + EXPECT_EQ(dbOption.isNeedCompressOnSync, metaData_.isNeedCompress); + EXPECT_EQ(dbOption.schema, metaData_.schema); + EXPECT_EQ(dbOption.passwd, dbPassword); + EXPECT_EQ(dbOption.cipher, DistributedDB::CipherType::AES_256_GCM); + EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); + EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); + EXPECT_EQ(dbOption.secOption, KVDBGeneralStore::GetDBSecurity(metaData_.securityLevel)); +} + +/** +* @tc.name: CloseTest +* @tc.desc: Close kvdb general store and test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, CloseTest, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + auto ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_OK); + + KvStoreNbDelegateMock mockDelegate; + mockDelegate.taskCountMock_ = 1; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_BUSY); +} + +/** +* @tc.name: Close +* @tc.desc: RdbGeneralStore Close test +* @tc.type: FUNC +* @tc.require: +* @tc.author: shaoyuanzhao +*/ +HWTEST_F(KVDBGeneralStoreTest, BusyClose, TestSize.Level0) +{ + auto store = std::make_shared(metaData_); + ASSERT_NE(store, nullptr); + std::thread thread([store]() { + std::unique_lockrwMutex_)> lock(store->rwMutex_); + std::this_thread::sleep_for(std::chrono::seconds(1)); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + auto ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_BUSY); + thread.join(); + ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_OK); +} + +/** +* @tc.name: SyncTest +* @tc.desc: Sync. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) +{ + ZLOGI("SyncTest start"); + mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + uint32_t syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; + uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; + auto mixMode = GeneralStore::MixMode(syncMode, highMode); + std::string kvQuery = ""; + DistributedKv::KVDBQuery query(kvQuery); + SyncParam syncParam{}; + syncParam.mode = mixMode; + auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_NE(ret.first, GeneralError::E_OK); + auto status = store->Close(); + EXPECT_EQ(status, GeneralError::E_OK); + delete store; +} + +/** +* @tc.name: BindTest +* @tc.desc: Bind test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, BindTest, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + DistributedData::Database database; + std::map bindInfos; + GeneralStore::CloudConfig config; + EXPECT_EQ(bindInfos.empty(), true); + auto ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + + std::shared_ptr db; + std::shared_ptr loader; + GeneralStore::BindInfo bindInfo1(db, loader); + uint32_t key = 1; + bindInfos[key] = bindInfo1; + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + std::shared_ptr dbs = std::make_shared(); + std::shared_ptr loaders = std::make_shared(); + GeneralStore::BindInfo bindInfo2(dbs, loaders); + bindInfos[key] = bindInfo2; + EXPECT_EQ(store->IsBound(key), false); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); + + store->bindInfos_.clear(); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + EXPECT_EQ(store->IsBound(key), false); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + + EXPECT_EQ(store->IsBound(key), true); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); +} + +/** +* @tc.name: BindWithDifferentUsersTest +* @tc.desc: Bind test the functionality of different users. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, BindWithDifferentUsersTest, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + ASSERT_NE(store, nullptr); + DistributedData::Database database; + std::map bindInfos; + GeneralStore::CloudConfig config; + + std::shared_ptr dbs = std::make_shared(); + std::shared_ptr loaders = std::make_shared(); + GeneralStore::BindInfo bindInfo(dbs, loaders); + uint32_t key0 = 100; + uint32_t key1 = 101; + bindInfos[key0] = bindInfo; + bindInfos[key1] = bindInfo; + EXPECT_EQ(store->IsBound(key0), false); + EXPECT_EQ(store->IsBound(key1), false); + auto ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + EXPECT_EQ(store->IsBound(key0), true); + EXPECT_EQ(store->IsBound(key1), true); + + uint32_t key2 = 102; + bindInfos[key2] = bindInfo; + EXPECT_EQ(store->IsBound(key2), false); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + EXPECT_EQ(store->IsBound(key2), true); +} + +/** +* @tc.name: GetDBSyncCompleteCB +* @tc.desc: GetDBSyncCompleteCB test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBSyncCompleteCB, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + GeneralStore::DetailAsync async; + EXPECT_EQ(async, nullptr); + KVDBGeneralStore::DBSyncCallback ret = store->GetDBSyncCompleteCB(async); + EXPECT_NE(ret, nullptr); + auto asyncs = [](const GenDetails &result) {}; + EXPECT_NE(asyncs, nullptr); + ret = store->GetDBSyncCompleteCB(asyncs); + EXPECT_NE(ret, nullptr); +} + +/** +* @tc.name: CloudSync001 +* @tc.desc: Test the scenario where the QueryUsers return false in the CloudSync function. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + store->SetEqualIdentifier(bundleName, storeName); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + std::vector devices = { "device1", "device2" }; + auto asyncs = [](const GenDetails &result) {}; + store->storeInfo_.user = 0; + auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; + store->SetEqualIdentifier(bundleName, storeName); + std::string prepareTraceId; + std::vector users; + 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); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: CloudSync002 +* @tc.desc: CloudSync test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + store->SetEqualIdentifier(bundleName, storeName); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + std::vector devices = { "device1", "device2" }; + auto asyncs = [](const GenDetails &result) {}; + store->storeInfo_.user = 0; + auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; + store->SetEqualIdentifier(bundleName, storeName); + std::string prepareTraceId; + std::vector users; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) + .Times(1) + .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); + + store->storeInfo_.user = 1; + cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_CLOUD_FORCE_PUSH; + ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); + EXPECT_EQ(ret, DBStatus::OK); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: CloudSync003 +* @tc.desc: Test the scenario where the QueryUsers return true in the CloudSync function. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, CloudSync003, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + store->SetEqualIdentifier(bundleName, storeName); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + std::vector devices = { "device1", "device2" }; + auto asyncs = [](const GenDetails &result) {}; + store->storeInfo_.user = 0; + auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; + store->SetEqualIdentifier(bundleName, storeName); + std::string prepareTraceId; + std::vector users = {0, 1}; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) + .Times(1) + .WillOnce(DoAll( + SetArgReferee<0>(users), + Return(true))); + auto ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); + EXPECT_EQ(ret, DBStatus::OK); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: GetIdentifierParams +* @tc.desc: GetIdentifierParams test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: wangbin +*/ +HWTEST_F(KVDBGeneralStoreTest, GetIdentifierParams, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + std::vector sameAccountDevs{}; + 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); + EXPECT_EQ(DMAdapter::GetInstance().GetAuthType(devId), 0); // NO_ACCOUNT + } + EXPECT_EQ(sameAccountDevs.empty(), false); +} + +/** +* @tc.name: Sync002 +* @tc.desc: Sync test the functionality of 3 < syncMode < 7 branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) +{ + mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; + uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; + auto mixMode = GeneralStore::MixMode(syncMode, highMode); + std::string kvQuery = ""; + DistributedKv::KVDBQuery query(kvQuery); + SyncParam syncParam{}; + syncParam.mode = mixMode; + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + 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; + storeConfig.enableCloud_ = true; + store->SetConfig(storeConfig); + ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_OK); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: Sync003 +* @tc.desc: Sync test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, Sync003, TestSize.Level0) +{ + mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + uint32_t syncMode = GeneralStore::SyncMode::NEARBY_END; + uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; + auto mixMode = GeneralStore::MixMode(syncMode, highMode); + std::string kvQuery = ""; + DistributedKv::KVDBQuery query(kvQuery); + SyncParam syncParam{}; + syncParam.mode = mixMode; + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); + std::vector devices = { "device1", "device2" }; + syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_OK); + + syncMode = GeneralStore::SyncMode::NEARBY_UNSUBSCRIBE_REMOTE; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_OK); + + syncMode = GeneralStore::SyncMode::NEARBY_PULL_PUSH; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_OK); + + syncMode = GeneralStore::SyncMode::MODE_BUTT; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: Clean +* @tc.desc: Clean test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Clean, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + std::vector devices = { "device1", "device2" }; + std::string tableName = "tableName"; + auto ret = store->Clean(devices, -1, tableName); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + ret = store->Clean(devices, 6, tableName); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + ret = store->Clean(devices, GeneralStore::CleanMode::NEARBY_DATA, tableName); + EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); + + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + ret = store->Clean(devices, GeneralStore::CleanMode::CLOUD_INFO, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); + + ret = store->Clean(devices, GeneralStore::CleanMode::CLOUD_DATA, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); + + ret = store->Clean({}, GeneralStore::CleanMode::NEARBY_DATA, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); + ret = store->Clean(devices, GeneralStore::CleanMode::NEARBY_DATA, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); + + ret = store->Clean(devices, GeneralStore::CleanMode::LOCAL_DATA, tableName); + EXPECT_EQ(ret, GeneralError::E_ERROR); + + ret = store->Clean(devices, GeneralStore::CleanMode::CLEAN_WATER, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); +} + +/** +* @tc.name: Watch +* @tc.desc: Watch and Unwatch test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Watch, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + DistributedDataTest::MockGeneralWatcher watcher; + auto ret = store->Watch(GeneralWatcher::Origin::ORIGIN_CLOUD, watcher); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_CLOUD, watcher); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + + ret = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(ret, GeneralError::E_OK); + ret = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + + ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(ret, GeneralError::E_OK); + ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); +} + +/** +* @tc.name: Release +* @tc.desc: Release and AddRef test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Release, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + auto ret = store->Release(); + EXPECT_EQ(ret, 0); + store = new (std::nothrow) KVDBGeneralStore(metaData_); + store->ref_ = 0; + ret = store->Release(); + EXPECT_EQ(ret, 0); + store->ref_ = 2; + ret = store->Release(); + EXPECT_EQ(ret, 1); + + ret = store->AddRef(); + EXPECT_EQ(ret, 2); + store->ref_ = 0; + ret = store->AddRef(); + EXPECT_EQ(ret, 0); +} + +/** +* @tc.name: ConvertStatus +* @tc.desc: ConvertStatus test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, ConvertStatus, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + auto ret = store->ConvertStatus(DBStatus::OK); + EXPECT_EQ(ret, GeneralError::E_OK); + ret = store->ConvertStatus(DBStatus::CLOUD_NETWORK_ERROR); + EXPECT_EQ(ret, GeneralError::E_NETWORK_ERROR); + ret = store->ConvertStatus(DBStatus::CLOUD_LOCK_ERROR); + EXPECT_EQ(ret, GeneralError::E_LOCKED_BY_OTHERS); + ret = store->ConvertStatus(DBStatus::CLOUD_FULL_RECORDS); + EXPECT_EQ(ret, GeneralError::E_RECODE_LIMIT_EXCEEDED); + ret = store->ConvertStatus(DBStatus::CLOUD_ASSET_SPACE_INSUFFICIENT); + EXPECT_EQ(ret, GeneralError::E_NO_SPACE_FOR_ASSET); + ret = store->ConvertStatus(DBStatus::CLOUD_SYNC_TASK_MERGED); + EXPECT_EQ(ret, GeneralError::E_SYNC_TASK_MERGED); + ret = store->ConvertStatus(DBStatus::DB_ERROR); + EXPECT_EQ(ret, GeneralError::E_DB_ERROR); +} + +/** +* @tc.name: OnChange +* @tc.desc: OnChange test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, OnChange, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + DistributedDataTest::MockGeneralWatcher watcher; + DistributedDataTest::MockKvStoreChangedData data; + DistributedDB::ChangedData changedData; + store->observer_.OnChange(data); + store->observer_.OnChange(DistributedDB::Origin::ORIGIN_CLOUD, "originalId", std::move(changedData)); + auto result = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(result, GeneralError::E_OK); + store->observer_.OnChange(data); + store->observer_.OnChange(DistributedDB::Origin::ORIGIN_CLOUD, "originalId", std::move(changedData)); + result = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); + EXPECT_EQ(result, GeneralError::E_OK); +} + +/** +* @tc.name: Delete +* @tc.desc: Delete test the function. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Delete, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::DEVICE_COLLABORATION); + store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::DEVICE_COLLABORATION); + DistributedData::VBuckets values; + auto ret = store->Insert("table", std::move(values)); + EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); + + DistributedData::Values args; + store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); + store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); + ret = store->Delete("table", "sql", std::move(args)); + EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); +} + +/** +* @tc.name: Query001 +* @tc.desc: KVDBGeneralStoreTest Query function test +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Query001, TestSize.Level1) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + std::string table = "table"; + std::string sql = "sql"; + auto [errCode, result] = store->Query(table, sql, {}); + EXPECT_EQ(errCode, GeneralError::E_NOT_SUPPORT); + EXPECT_EQ(result, nullptr); +} + +/** +* @tc.name: Query002 +* @tc.desc: KVDBGeneralStoreTest Query function test +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, Query002, TestSize.Level1) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + std::string table = "table"; + MockQuery query; + auto [errCode, result] = store->Query(table, query); + EXPECT_EQ(errCode, GeneralError::E_NOT_SUPPORT); + EXPECT_EQ(result, nullptr); +} + +/** +* @tc.name: GetDBOption +* @tc.desc: GetDBOption from meta and dbPassword +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBOption001, TestSize.Level0) +{ + metaData_.isEncrypt = false; + metaData_.appId = "distributeddata"; + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); + EXPECT_FALSE(MetaDataManager::GetInstance().LoadMeta(metaData_.GetKeyLocal(), metaData_, true)); + EXPECT_EQ(metaData_.appId, Bootstrap::GetInstance().GetProcessLabel()); + EXPECT_EQ(dbOption.createIfNecessary, false); + EXPECT_EQ(dbOption.isMemoryDb, false); + EXPECT_EQ(dbOption.isEncryptedDb, metaData_.isEncrypt); + EXPECT_EQ(dbOption.isNeedCompressOnSync, metaData_.isNeedCompress); + EXPECT_EQ(dbOption.schema, metaData_.schema); + EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); + EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); + EXPECT_EQ(dbOption.secOption, KVDBGeneralStore::GetDBSecurity(metaData_.securityLevel)); +} + +/** +* @tc.name: Sync +* @tc.desc: Sync test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(KVDBGeneralStoreTest, Sync001, TestSize.Level0) +{ + mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; + uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; + auto mixMode = GeneralStore::MixMode(syncMode, highMode); + std::string kvQuery = "querytest"; + DistributedKv::KVDBQuery query(kvQuery); + SyncParam syncParam{}; + syncParam.mode = mixMode; + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_NOT_SUPPORT); + GeneralStore::StoreConfig storeConfig; + storeConfig.enableCloud_ = false; + store->SetConfig(storeConfig); + ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_NOT_SUPPORT); + + std::vector devices = { "device1", "device2" }; + syncMode = GeneralStore::SyncMode::CLOUD_END; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); + + syncMode = GeneralStore::SyncMode::NEARBY_PULL; + mixMode = GeneralStore::MixMode(syncMode, highMode); + syncParam.mode = mixMode; + ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); + EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: SetEqualIdentifier +* @tc.desc: SetEqualIdentifier test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +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" }; + AppId appId01 = { "ohos.kvdbservice.test01" }; + StoreId storeId01 = { "meta_test_storeid" }; + std::string account = "account"; + store->SetEqualIdentifier(appId01, storeId01, account); + EXPECT_EQ(uuids.empty(), false); + EXPECT_EQ(sameAccountDevs.empty(), false); + delete store; +} + +/** +* @tc.name: GetIdentifierParams +* @tc.desc: GetIdentifierParams test. +* @tc.type: FUNC +* @tc.require: +*/ +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" }; + store->GetIdentifierParams(sameAccountDevs, uuids, 1); // + for (const auto &devId : uuids) { + EXPECT_EQ(DMAdapter::GetInstance().IsOHOSType(devId), false); + EXPECT_EQ(DMAdapter::GetInstance().GetAuthType(devId), 0); // + } + EXPECT_EQ(sameAccountDevs.empty(), false); + delete store; +} + +/** +* @tc.name: ConvertStatus +* @tc.desc: ConvertStatus test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, ConvertStatus001, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + auto ret = store->ConvertStatus(DBStatus::NOT_SUPPORT); + EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); + DBStatus unknownStatus = static_cast(0xDEAD); + ret = store->ConvertStatus(unknownStatus); + EXPECT_EQ(ret, GeneralError::E_ERROR); + delete store; +} + +/** +* @tc.name: GetDBProcessCB +* @tc.desc: GetDBProcessCB test. +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBProcessCB, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + bool asyncCalled = false; + auto async = [&asyncCalled](const DistributedData::GenDetails &details) { + asyncCalled = true; + EXPECT_TRUE(details.empty()); + }; + + std::map processes; + auto callback = store->GetDBProcessCB(async); + callback(processes); + EXPECT_TRUE(asyncCalled); + delete store; +} + +/** +* @tc.name: GetDBProcessCB +* @tc.desc: GetDBProcessCB test. +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBProcessCB001, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + GeneralStore::DetailAsync async; + EXPECT_EQ(async, nullptr); + auto process = store->GetDBProcessCB(async); + EXPECT_NE(process, nullptr); + auto asyncs = [](const GenDetails &result) {}; + EXPECT_NE(asyncs, nullptr); + process = store->GetDBProcessCB(asyncs); + EXPECT_NE(process, nullptr); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: GetDBProcessCB +* @tc.desc: GetDBProcessCB test. +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBProcessCB002, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + std::map processes = { { "test_id", {} } }; + GeneralStore::DetailAsync async; + EXPECT_EQ(async, nullptr); + auto callback = store->GetDBProcessCB(async); + callback(processes); + delete store; +} + +/** +* @tc.name: Delete +* @tc.desc: Delete test the function. +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(KVDBGeneralStoreTest, SetDBPushDataInterceptor, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::MULTI_VERSION); + store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::MULTI_VERSION); + EXPECT_NE(metaData_.storeType, DistributedKv::KvStoreType::MULTI_VERSION); + + store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); + store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); + EXPECT_EQ(metaData_.storeType, DistributedKv::KvStoreType::SINGLE_VERSION); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: CloseTest +* @tc.desc: Close kvdb general store +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(KVDBGeneralStoreTest, CloseTest001, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + EXPECT_EQ(store->delegate_, nullptr); + auto ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_OK); + + bool isForce = false; + ret = store->Close(isForce); + EXPECT_EQ(ret, DBStatus::OK); + delete store; +} + +/** +* @tc.name: ConstructorTest +* @tc.desc: Test KVDBGeneralStore constructor with different scenarios +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(KVDBGeneralStoreTest, ConstructorTest, TestSize.Level0) +{ + // Test manager initialization with same appId + metaData_.appId = Bootstrap::GetInstance().GetProcessLabel(); + auto store1 = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store1, nullptr); + delete store1; + + // Test manager initialization with different appId + metaData_.appId = "different_app_id"; + auto store2 = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store2, nullptr); + delete store2; + + // Test GetKvStore failure path + metaData_.storeId = "invalid_store"; + auto store3 = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store3, nullptr); + EXPECT_EQ(store3->delegate_, nullptr); + delete store3; + + // Test observer registration + metaData_.storeId = storeName; + metaData_.storeType = DistributedKv::KvStoreType::SINGLE_VERSION; + auto store4 = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store4, nullptr); + delete store4; + + // Test remote push notification setup + metaData_.storeType = DistributedKv::KvStoreType::DEVICE_COLLABORATION; + auto store5 = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store5, nullptr); + delete store5; + + // Test auto-sync configuration + metaData_.isAutoSync = true; + auto store6 = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store6, nullptr); + EXPECT_EQ(store6->delegate_, nullptr); + delete store6; +} +/** +* @tc.name: GetDBPasswordTest_001 +* @tc.desc: get password with all exception branch +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_001, TestSize.Level0) +{ + metaData_.isEncrypt = false; + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + + metaData_.isEncrypt = true; + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + SecretKeyMetaData secretKey; + auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + std::vector randomKey = Random(KEY_LENGTH); + ASSERT_FALSE(randomKey.empty()); + secretKey.sKey = randomKey; + result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); +} + +/** +* @tc.name: GetDBPasswordTest_002 +* @tc.desc: GetDBPassword from meta. +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_002, TestSize.Level0) +{ + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), metaData_, true)); + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_TRUE(dbPassword.GetSize() == 0); +} + +/** +* @tc.name: GetDBPasswordTest_003 +* @tc.desc: GetDBPassword from encrypt meta. +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_003, TestSize.Level0) +{ + ZLOGI("GetDBPasswordTest_002 start"); + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + metaData_.isEncrypt = true; + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); + + auto errCode = CryptoManager::GetInstance().GenerateRootKey(); + EXPECT_EQ(errCode, CryptoManager::ErrCode::SUCCESS); + + std::vector randomKey = Random(KEY_LENGTH); + ASSERT_FALSE(randomKey.empty()); + SecretKeyMetaData secretKey; + secretKey.storeType = metaData_.storeType; + CryptoManager::CryptoParams encryptParams = { .area = metaData_.area, .userId = metaData_.user }; + secretKey.sKey = CryptoManager::GetInstance().Encrypt(randomKey, encryptParams); + ASSERT_FALSE(secretKey.sKey.empty()); + ASSERT_FALSE(encryptParams.nonce.empty()); + secretKey.area = metaData_.area; + secretKey.nonce = encryptParams.nonce; + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true)); + + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_TRUE(dbPassword.GetSize() != 0); + randomKey.assign(randomKey.size(), 0); +} + +/** +* @tc.name: GetDBPasswordTest_004 +* @tc.desc: get password with exception branch +* @tc.type: FUNC +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_004, TestSize.Level0) +{ + metaData_.bundleName = INVALID_APPID; + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + + SecretKeyMetaData secretKey; + auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + + auto key = Random(KEY_LENGTH); + ASSERT_FALSE(key.empty()); + secretKey.sKey = key; + result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); + ASSERT_TRUE(result); + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_EQ(dbPassword.GetSize(), 0); + MetaDataManager::GetInstance().DelMeta(metaData_.GetSecretKey(), true); +} + +/** +* @tc.name: GetDBSecurityTest +* @tc.desc: GetDBSecurity +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBSecurityTest, TestSize.Level0) +{ + auto dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::INVALID_LABEL); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::NO_LABEL); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S0); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S0); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S1); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S1); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S2); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S2); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S3); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S3); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::SECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S4); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S4); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + auto action = static_cast(SecurityLevel::S4 + 1); + dbSecurity = KVDBGeneralStore::GetDBSecurity(action); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); +} + +/** +* @tc.name: GetDBOptionTest +* @tc.desc: GetDBOption from meta and dbPassword +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBOptionTest, TestSize.Level0) +{ + metaData_.isEncrypt = true; + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); + EXPECT_EQ(dbOption.syncDualTupleMode, true); + EXPECT_EQ(dbOption.createIfNecessary, false); + EXPECT_EQ(dbOption.isMemoryDb, false); + EXPECT_EQ(dbOption.isEncryptedDb, metaData_.isEncrypt); + EXPECT_EQ(dbOption.isNeedCompressOnSync, metaData_.isNeedCompress); + EXPECT_EQ(dbOption.schema, metaData_.schema); + EXPECT_EQ(dbOption.passwd, dbPassword); + EXPECT_EQ(dbOption.cipher, DistributedDB::CipherType::AES_256_GCM); + EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); + EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); + EXPECT_EQ(dbOption.secOption, KVDBGeneralStore::GetDBSecurity(metaData_.securityLevel)); +} + +/** +* @tc.name: CloseTest +* @tc.desc: Close kvdb general store and test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, CloseTest, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + auto ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_OK); + + KvStoreNbDelegateMock mockDelegate; + mockDelegate.taskCountMock_ = 1; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_BUSY); +} + +/** +* @tc.name: Close +* @tc.desc: RdbGeneralStore Close test +* @tc.type: FUNC +* @tc.require: +* @tc.author: shaoyuanzhao +*/ +HWTEST_F(KVDBGeneralStoreTest, BusyClose, TestSize.Level0) +{ + auto store = std::make_shared(metaData_); + ASSERT_NE(store, nullptr); + std::thread thread([store]() { + std::unique_lockrwMutex_)> lock(store->rwMutex_); + std::this_thread::sleep_for(std::chrono::seconds(1)); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + auto ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_BUSY); + thread.join(); + ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_OK); +} + +/** +* @tc.name: SyncTest +* @tc.desc: Sync. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) +{ + ZLOGI("SyncTest start"); + mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + uint32_t syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; + uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; + auto mixMode = GeneralStore::MixMode(syncMode, highMode); + std::string kvQuery = ""; + DistributedKv::KVDBQuery query(kvQuery); + SyncParam syncParam{}; + syncParam.mode = mixMode; + auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); + EXPECT_NE(ret.first, GeneralError::E_OK); + auto status = store->Close(); + EXPECT_EQ(status, GeneralError::E_OK); + delete store; +} + +/** +* @tc.name: BindTest +* @tc.desc: Bind test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, BindTest, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + DistributedData::Database database; + std::map bindInfos; + GeneralStore::CloudConfig config; + EXPECT_EQ(bindInfos.empty(), true); + auto ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + + std::shared_ptr db; + std::shared_ptr loader; + GeneralStore::BindInfo bindInfo1(db, loader); + uint32_t key = 1; + bindInfos[key] = bindInfo1; + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + std::shared_ptr dbs = std::make_shared(); + std::shared_ptr loaders = std::make_shared(); + GeneralStore::BindInfo bindInfo2(dbs, loaders); + bindInfos[key] = bindInfo2; + EXPECT_EQ(store->IsBound(key), false); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); + + store->bindInfos_.clear(); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + EXPECT_EQ(store->IsBound(key), false); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + + EXPECT_EQ(store->IsBound(key), true); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); +} + +/** +* @tc.name: BindWithDifferentUsersTest +* @tc.desc: Bind test the functionality of different users. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, BindWithDifferentUsersTest, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + EXPECT_NE(store->delegate_, nullptr); + ASSERT_NE(store, nullptr); + DistributedData::Database database; + std::map bindInfos; + GeneralStore::CloudConfig config; + + std::shared_ptr dbs = std::make_shared(); + std::shared_ptr loaders = std::make_shared(); + GeneralStore::BindInfo bindInfo(dbs, loaders); + uint32_t key0 = 100; + uint32_t key1 = 101; + bindInfos[key0] = bindInfo; + bindInfos[key1] = bindInfo; + EXPECT_EQ(store->IsBound(key0), false); + EXPECT_EQ(store->IsBound(key1), false); + auto ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + EXPECT_EQ(store->IsBound(key0), true); + EXPECT_EQ(store->IsBound(key1), true); + + uint32_t key2 = 102; + bindInfos[key2] = bindInfo; + EXPECT_EQ(store->IsBound(key2), false); + ret = store->Bind(database, bindInfos, config); + EXPECT_EQ(ret, GeneralError::E_OK); + EXPECT_EQ(store->IsBound(key2), true); +} + +/** +* @tc.name: GetDBSyncCompleteCB +* @tc.desc: GetDBSyncCompleteCB test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBSyncCompleteCB, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + GeneralStore::DetailAsync async; + EXPECT_EQ(async, nullptr); + KVDBGeneralStore::DBSyncCallback ret = store->GetDBSyncCompleteCB(async); + EXPECT_NE(ret, nullptr); + auto asyncs = [](const GenDetails &result) {}; + EXPECT_NE(asyncs, nullptr); + ret = store->GetDBSyncCompleteCB(asyncs); + EXPECT_NE(ret, nullptr); +} + +/** +* @tc.name: CloudSync001 +* @tc.desc: Test the scenario where the QueryUsers return false in the CloudSync function. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + store->SetEqualIdentifier(bundleName, storeName); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + std::vector devices = { "device1", "device2" }; + auto asyncs = [](const GenDetails &result) {}; + store->storeInfo_.user = 0; + auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; + store->SetEqualIdentifier(bundleName, storeName); + std::string prepareTraceId; + std::vector users; + 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); + store->delegate_ = nullptr; + delete store; +} + +/** +* @tc.name: CloudSync002 +* @tc.desc: CloudSync test the functionality of different branches. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) +{ + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + store->SetEqualIdentifier(bundleName, storeName); + KvStoreNbDelegateMock mockDelegate; + store->delegate_ = &mockDelegate; + std::vector devices = { "device1", "device2" }; + auto asyncs = [](const GenDetails &result) {}; + store->storeInfo_.user = 0; + auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; + store->SetEqualIdentifier(bundleName, storeName); + std::string prepareTraceId; + std::vector users; + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) + .Times(1) + .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); + + store->storeInfo_.user = 1; + cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_CLOUD_FORCE_PUSH; + ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); + EXPECT_EQ(ret, DBStatus::OK); + store->delegate_ = nullptr; + delete store; +} } // namespace DistributedDataTest } // namespace OHOS::Test \ No newline at end of file -- Gitee From 4f26f39d9bfcc9abe873714133c2f62f082aeee3 Mon Sep 17 00:00:00 2001 From: gecheng Date: Mon, 4 Aug 2025 15:42:21 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9kv=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gecheng --- .../service/test/kvdb_general_store_test.cpp | 1473 ----------------- 1 file changed, 1473 deletions(-) diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index ed4704b81..c34db72a6 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -1228,1478 +1228,5 @@ HWTEST_F(KVDBGeneralStoreTest, ConstructorTest, TestSize.Level0) EXPECT_EQ(store6->delegate_, nullptr); delete store6; } - -/** -* @tc.name: GetDBPasswordTest_001 -* @tc.desc: get password with all exception branch -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_001, TestSize.Level0) -{ - metaData_.isEncrypt = false; - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - - metaData_.isEncrypt = true; - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - SecretKeyMetaData secretKey; - auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - std::vector randomKey = Random(KEY_LENGTH); - ASSERT_FALSE(randomKey.empty()); - secretKey.sKey = randomKey; - result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); -} - -/** -* @tc.name: GetDBPasswordTest_002 -* @tc.desc: GetDBPassword from meta. -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_002, TestSize.Level0) -{ - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), metaData_, true)); - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_TRUE(dbPassword.GetSize() == 0); -} - -/** -* @tc.name: GetDBPasswordTest_003 -* @tc.desc: GetDBPassword from encrypt meta. -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_003, TestSize.Level0) -{ - ZLOGI("GetDBPasswordTest_002 start"); - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - metaData_.isEncrypt = true; - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); - - auto errCode = CryptoManager::GetInstance().GenerateRootKey(); - EXPECT_EQ(errCode, CryptoManager::ErrCode::SUCCESS); - - std::vector randomKey = Random(KEY_LENGTH); - ASSERT_FALSE(randomKey.empty()); - SecretKeyMetaData secretKey; - secretKey.storeType = metaData_.storeType; - CryptoManager::CryptoParams encryptParams = { .area = metaData_.area, .userId = metaData_.user }; - secretKey.sKey = CryptoManager::GetInstance().Encrypt(randomKey, encryptParams); - ASSERT_FALSE(secretKey.sKey.empty()); - ASSERT_FALSE(encryptParams.nonce.empty()); - secretKey.area = metaData_.area; - secretKey.nonce = encryptParams.nonce; - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true)); - - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_TRUE(dbPassword.GetSize() != 0); - randomKey.assign(randomKey.size(), 0); -} - -/** -* @tc.name: GetDBPasswordTest_004 -* @tc.desc: get password with exception branch -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_004, TestSize.Level0) -{ - metaData_.bundleName = INVALID_APPID; - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - - SecretKeyMetaData secretKey; - auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - auto key = Random(KEY_LENGTH); - ASSERT_FALSE(key.empty()); - secretKey.sKey = key; - result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - MetaDataManager::GetInstance().DelMeta(metaData_.GetSecretKey(), true); -} - -/** -* @tc.name: GetDBSecurityTest -* @tc.desc: GetDBSecurity -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBSecurityTest, TestSize.Level0) -{ - auto dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::INVALID_LABEL); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::NO_LABEL); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S0); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S0); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S1); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S1); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S2); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S2); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S3); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S3); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::SECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S4); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S4); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - auto action = static_cast(SecurityLevel::S4 + 1); - dbSecurity = KVDBGeneralStore::GetDBSecurity(action); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); -} - -/** -* @tc.name: GetDBOptionTest -* @tc.desc: GetDBOption from meta and dbPassword -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBOptionTest, TestSize.Level0) -{ - metaData_.isEncrypt = true; - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); - EXPECT_EQ(dbOption.syncDualTupleMode, true); - EXPECT_EQ(dbOption.createIfNecessary, false); - EXPECT_EQ(dbOption.isMemoryDb, false); - EXPECT_EQ(dbOption.isEncryptedDb, metaData_.isEncrypt); - EXPECT_EQ(dbOption.isNeedCompressOnSync, metaData_.isNeedCompress); - EXPECT_EQ(dbOption.schema, metaData_.schema); - EXPECT_EQ(dbOption.passwd, dbPassword); - EXPECT_EQ(dbOption.cipher, DistributedDB::CipherType::AES_256_GCM); - EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); - EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); - EXPECT_EQ(dbOption.secOption, KVDBGeneralStore::GetDBSecurity(metaData_.securityLevel)); -} - -/** -* @tc.name: CloseTest -* @tc.desc: Close kvdb general store and test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, CloseTest, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - auto ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_OK); - - KvStoreNbDelegateMock mockDelegate; - mockDelegate.taskCountMock_ = 1; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_BUSY); -} - -/** -* @tc.name: Close -* @tc.desc: RdbGeneralStore Close test -* @tc.type: FUNC -* @tc.require: -* @tc.author: shaoyuanzhao -*/ -HWTEST_F(KVDBGeneralStoreTest, BusyClose, TestSize.Level0) -{ - auto store = std::make_shared(metaData_); - ASSERT_NE(store, nullptr); - std::thread thread([store]() { - std::unique_lockrwMutex_)> lock(store->rwMutex_); - std::this_thread::sleep_for(std::chrono::seconds(1)); - }); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - auto ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_BUSY); - thread.join(); - ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_OK); -} - -/** -* @tc.name: SyncTest -* @tc.desc: Sync. -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) -{ - ZLOGI("SyncTest start"); - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), - (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - uint32_t syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; - uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; - auto mixMode = GeneralStore::MixMode(syncMode, highMode); - std::string kvQuery = ""; - DistributedKv::KVDBQuery query(kvQuery); - SyncParam syncParam{}; - syncParam.mode = mixMode; - auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_NE(ret.first, GeneralError::E_OK); - auto status = store->Close(); - EXPECT_EQ(status, GeneralError::E_OK); - delete store; -} - -/** -* @tc.name: BindTest -* @tc.desc: Bind test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, BindTest, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - DistributedData::Database database; - std::map bindInfos; - GeneralStore::CloudConfig config; - EXPECT_EQ(bindInfos.empty(), true); - auto ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - - std::shared_ptr db; - std::shared_ptr loader; - GeneralStore::BindInfo bindInfo1(db, loader); - uint32_t key = 1; - bindInfos[key] = bindInfo1; - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - std::shared_ptr dbs = std::make_shared(); - std::shared_ptr loaders = std::make_shared(); - GeneralStore::BindInfo bindInfo2(dbs, loaders); - bindInfos[key] = bindInfo2; - EXPECT_EQ(store->IsBound(key), false); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); - - store->bindInfos_.clear(); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - EXPECT_EQ(store->IsBound(key), false); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - - EXPECT_EQ(store->IsBound(key), true); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); -} - -/** -* @tc.name: BindWithDifferentUsersTest -* @tc.desc: Bind test the functionality of different users. -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, BindWithDifferentUsersTest, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - ASSERT_NE(store, nullptr); - DistributedData::Database database; - std::map bindInfos; - GeneralStore::CloudConfig config; - - std::shared_ptr dbs = std::make_shared(); - std::shared_ptr loaders = std::make_shared(); - GeneralStore::BindInfo bindInfo(dbs, loaders); - uint32_t key0 = 100; - uint32_t key1 = 101; - bindInfos[key0] = bindInfo; - bindInfos[key1] = bindInfo; - EXPECT_EQ(store->IsBound(key0), false); - EXPECT_EQ(store->IsBound(key1), false); - auto ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - EXPECT_EQ(store->IsBound(key0), true); - EXPECT_EQ(store->IsBound(key1), true); - - uint32_t key2 = 102; - bindInfos[key2] = bindInfo; - EXPECT_EQ(store->IsBound(key2), false); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - EXPECT_EQ(store->IsBound(key2), true); -} - -/** -* @tc.name: GetDBSyncCompleteCB -* @tc.desc: GetDBSyncCompleteCB test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBSyncCompleteCB, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - GeneralStore::DetailAsync async; - EXPECT_EQ(async, nullptr); - KVDBGeneralStore::DBSyncCallback ret = store->GetDBSyncCompleteCB(async); - EXPECT_NE(ret, nullptr); - auto asyncs = [](const GenDetails &result) {}; - EXPECT_NE(asyncs, nullptr); - ret = store->GetDBSyncCompleteCB(asyncs); - EXPECT_NE(ret, nullptr); -} - -/** -* @tc.name: CloudSync001 -* @tc.desc: Test the scenario where the QueryUsers return false in the CloudSync function. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - std::vector devices = { "device1", "device2" }; - auto asyncs = [](const GenDetails &result) {}; - store->storeInfo_.user = 0; - auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; - store->SetEqualIdentifier(bundleName, storeName); - std::string prepareTraceId; - std::vector users; - 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); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: CloudSync002 -* @tc.desc: CloudSync test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - std::vector devices = { "device1", "device2" }; - auto asyncs = [](const GenDetails &result) {}; - store->storeInfo_.user = 0; - auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; - store->SetEqualIdentifier(bundleName, storeName); - std::string prepareTraceId; - std::vector users; - EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) - .Times(1) - .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); - - store->storeInfo_.user = 1; - cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_CLOUD_FORCE_PUSH; - ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); - EXPECT_EQ(ret, DBStatus::OK); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: CloudSync003 -* @tc.desc: Test the scenario where the QueryUsers return true in the CloudSync function. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, CloudSync003, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - std::vector devices = { "device1", "device2" }; - auto asyncs = [](const GenDetails &result) {}; - store->storeInfo_.user = 0; - auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; - store->SetEqualIdentifier(bundleName, storeName); - std::string prepareTraceId; - std::vector users = {0, 1}; - EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) - .Times(1) - .WillOnce(DoAll( - SetArgReferee<0>(users), - Return(true))); - auto ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); - EXPECT_EQ(ret, DBStatus::OK); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: GetIdentifierParams -* @tc.desc: GetIdentifierParams test. -* @tc.type: FUNC -* @tc.require: -* @tc.author: wangbin -*/ -HWTEST_F(KVDBGeneralStoreTest, GetIdentifierParams, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - std::vector sameAccountDevs{}; - 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); - EXPECT_EQ(DMAdapter::GetInstance().GetAuthType(devId), 0); // NO_ACCOUNT - } - EXPECT_EQ(sameAccountDevs.empty(), false); -} - -/** -* @tc.name: Sync002 -* @tc.desc: Sync test the functionality of 3 < syncMode < 7 branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) -{ - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), - (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; - uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; - auto mixMode = GeneralStore::MixMode(syncMode, highMode); - std::string kvQuery = ""; - DistributedKv::KVDBQuery query(kvQuery); - SyncParam syncParam{}; - syncParam.mode = mixMode; - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - 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; - storeConfig.enableCloud_ = true; - store->SetConfig(storeConfig); - ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_OK); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: Sync003 -* @tc.desc: Sync test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, Sync003, TestSize.Level0) -{ - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), - (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - uint32_t syncMode = GeneralStore::SyncMode::NEARBY_END; - uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; - auto mixMode = GeneralStore::MixMode(syncMode, highMode); - std::string kvQuery = ""; - DistributedKv::KVDBQuery query(kvQuery); - SyncParam syncParam{}; - syncParam.mode = mixMode; - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); - std::vector devices = { "device1", "device2" }; - syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_OK); - - syncMode = GeneralStore::SyncMode::NEARBY_UNSUBSCRIBE_REMOTE; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_OK); - - syncMode = GeneralStore::SyncMode::NEARBY_PULL_PUSH; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_OK); - - syncMode = GeneralStore::SyncMode::MODE_BUTT; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: Clean -* @tc.desc: Clean test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Clean, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - std::vector devices = { "device1", "device2" }; - std::string tableName = "tableName"; - auto ret = store->Clean(devices, -1, tableName); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - ret = store->Clean(devices, 6, tableName); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - ret = store->Clean(devices, GeneralStore::CleanMode::NEARBY_DATA, tableName); - EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); - - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - ret = store->Clean(devices, GeneralStore::CleanMode::CLOUD_INFO, tableName); - EXPECT_EQ(ret, GeneralError::E_OK); - - ret = store->Clean(devices, GeneralStore::CleanMode::CLOUD_DATA, tableName); - EXPECT_EQ(ret, GeneralError::E_OK); - - ret = store->Clean({}, GeneralStore::CleanMode::NEARBY_DATA, tableName); - EXPECT_EQ(ret, GeneralError::E_OK); - ret = store->Clean(devices, GeneralStore::CleanMode::NEARBY_DATA, tableName); - EXPECT_EQ(ret, GeneralError::E_OK); - - ret = store->Clean(devices, GeneralStore::CleanMode::LOCAL_DATA, tableName); - EXPECT_EQ(ret, GeneralError::E_ERROR); - - ret = store->Clean(devices, GeneralStore::CleanMode::CLEAN_WATER, tableName); - EXPECT_EQ(ret, GeneralError::E_OK); -} - -/** -* @tc.name: Watch -* @tc.desc: Watch and Unwatch test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Watch, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - DistributedDataTest::MockGeneralWatcher watcher; - auto ret = store->Watch(GeneralWatcher::Origin::ORIGIN_CLOUD, watcher); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_CLOUD, watcher); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - - ret = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(ret, GeneralError::E_OK); - ret = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - - ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(ret, GeneralError::E_OK); - ret = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); -} - -/** -* @tc.name: Release -* @tc.desc: Release and AddRef test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Release, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - auto ret = store->Release(); - EXPECT_EQ(ret, 0); - store = new (std::nothrow) KVDBGeneralStore(metaData_); - store->ref_ = 0; - ret = store->Release(); - EXPECT_EQ(ret, 0); - store->ref_ = 2; - ret = store->Release(); - EXPECT_EQ(ret, 1); - - ret = store->AddRef(); - EXPECT_EQ(ret, 2); - store->ref_ = 0; - ret = store->AddRef(); - EXPECT_EQ(ret, 0); -} - -/** -* @tc.name: ConvertStatus -* @tc.desc: ConvertStatus test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, ConvertStatus, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - auto ret = store->ConvertStatus(DBStatus::OK); - EXPECT_EQ(ret, GeneralError::E_OK); - ret = store->ConvertStatus(DBStatus::CLOUD_NETWORK_ERROR); - EXPECT_EQ(ret, GeneralError::E_NETWORK_ERROR); - ret = store->ConvertStatus(DBStatus::CLOUD_LOCK_ERROR); - EXPECT_EQ(ret, GeneralError::E_LOCKED_BY_OTHERS); - ret = store->ConvertStatus(DBStatus::CLOUD_FULL_RECORDS); - EXPECT_EQ(ret, GeneralError::E_RECODE_LIMIT_EXCEEDED); - ret = store->ConvertStatus(DBStatus::CLOUD_ASSET_SPACE_INSUFFICIENT); - EXPECT_EQ(ret, GeneralError::E_NO_SPACE_FOR_ASSET); - ret = store->ConvertStatus(DBStatus::CLOUD_SYNC_TASK_MERGED); - EXPECT_EQ(ret, GeneralError::E_SYNC_TASK_MERGED); - ret = store->ConvertStatus(DBStatus::DB_ERROR); - EXPECT_EQ(ret, GeneralError::E_DB_ERROR); -} - -/** -* @tc.name: OnChange -* @tc.desc: OnChange test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, OnChange, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - DistributedDataTest::MockGeneralWatcher watcher; - DistributedDataTest::MockKvStoreChangedData data; - DistributedDB::ChangedData changedData; - store->observer_.OnChange(data); - store->observer_.OnChange(DistributedDB::Origin::ORIGIN_CLOUD, "originalId", std::move(changedData)); - auto result = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(result, GeneralError::E_OK); - store->observer_.OnChange(data); - store->observer_.OnChange(DistributedDB::Origin::ORIGIN_CLOUD, "originalId", std::move(changedData)); - result = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher); - EXPECT_EQ(result, GeneralError::E_OK); -} - -/** -* @tc.name: Delete -* @tc.desc: Delete test the function. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Delete, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::DEVICE_COLLABORATION); - store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::DEVICE_COLLABORATION); - DistributedData::VBuckets values; - auto ret = store->Insert("table", std::move(values)); - EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); - - DistributedData::Values args; - store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); - store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); - ret = store->Delete("table", "sql", std::move(args)); - EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); -} - -/** -* @tc.name: Query001 -* @tc.desc: KVDBGeneralStoreTest Query function test -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Query001, TestSize.Level1) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - std::string table = "table"; - std::string sql = "sql"; - auto [errCode, result] = store->Query(table, sql, {}); - EXPECT_EQ(errCode, GeneralError::E_NOT_SUPPORT); - EXPECT_EQ(result, nullptr); -} - -/** -* @tc.name: Query002 -* @tc.desc: KVDBGeneralStoreTest Query function test -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, Query002, TestSize.Level1) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - std::string table = "table"; - MockQuery query; - auto [errCode, result] = store->Query(table, query); - EXPECT_EQ(errCode, GeneralError::E_NOT_SUPPORT); - EXPECT_EQ(result, nullptr); -} - -/** -* @tc.name: GetDBOption -* @tc.desc: GetDBOption from meta and dbPassword -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBOption001, TestSize.Level0) -{ - metaData_.isEncrypt = false; - metaData_.appId = "distributeddata"; - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); - EXPECT_FALSE(MetaDataManager::GetInstance().LoadMeta(metaData_.GetKeyLocal(), metaData_, true)); - EXPECT_EQ(metaData_.appId, Bootstrap::GetInstance().GetProcessLabel()); - EXPECT_EQ(dbOption.createIfNecessary, false); - EXPECT_EQ(dbOption.isMemoryDb, false); - EXPECT_EQ(dbOption.isEncryptedDb, metaData_.isEncrypt); - EXPECT_EQ(dbOption.isNeedCompressOnSync, metaData_.isNeedCompress); - EXPECT_EQ(dbOption.schema, metaData_.schema); - EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); - EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); - EXPECT_EQ(dbOption.secOption, KVDBGeneralStore::GetDBSecurity(metaData_.securityLevel)); -} - -/** -* @tc.name: Sync -* @tc.desc: Sync test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(KVDBGeneralStoreTest, Sync001, TestSize.Level0) -{ - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), - (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - uint32_t syncMode = GeneralStore::SyncMode::CLOUD_BEGIN; - uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; - auto mixMode = GeneralStore::MixMode(syncMode, highMode); - std::string kvQuery = "querytest"; - DistributedKv::KVDBQuery query(kvQuery); - SyncParam syncParam{}; - syncParam.mode = mixMode; - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_NOT_SUPPORT); - GeneralStore::StoreConfig storeConfig; - storeConfig.enableCloud_ = false; - store->SetConfig(storeConfig); - ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_NOT_SUPPORT); - - std::vector devices = { "device1", "device2" }; - syncMode = GeneralStore::SyncMode::CLOUD_END; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); - - syncMode = GeneralStore::SyncMode::NEARBY_PULL; - mixMode = GeneralStore::MixMode(syncMode, highMode); - syncParam.mode = mixMode; - ret = store->Sync(devices, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret.first, GeneralError::E_INVALID_ARGS); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: SetEqualIdentifier -* @tc.desc: SetEqualIdentifier test. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -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" }; - AppId appId01 = { "ohos.kvdbservice.test01" }; - StoreId storeId01 = { "meta_test_storeid" }; - std::string account = "account"; - store->SetEqualIdentifier(appId01, storeId01, account); - EXPECT_EQ(uuids.empty(), false); - EXPECT_EQ(sameAccountDevs.empty(), false); - delete store; -} - -/** -* @tc.name: GetIdentifierParams -* @tc.desc: GetIdentifierParams test. -* @tc.type: FUNC -* @tc.require: -*/ -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" }; - store->GetIdentifierParams(sameAccountDevs, uuids, 1); // - for (const auto &devId : uuids) { - EXPECT_EQ(DMAdapter::GetInstance().IsOHOSType(devId), false); - EXPECT_EQ(DMAdapter::GetInstance().GetAuthType(devId), 0); // - } - EXPECT_EQ(sameAccountDevs.empty(), false); - delete store; -} - -/** -* @tc.name: ConvertStatus -* @tc.desc: ConvertStatus test. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, ConvertStatus001, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - auto ret = store->ConvertStatus(DBStatus::NOT_SUPPORT); - EXPECT_EQ(ret, GeneralError::E_NOT_SUPPORT); - DBStatus unknownStatus = static_cast(0xDEAD); - ret = store->ConvertStatus(unknownStatus); - EXPECT_EQ(ret, GeneralError::E_ERROR); - delete store; -} - -/** -* @tc.name: GetDBProcessCB -* @tc.desc: GetDBProcessCB test. -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBProcessCB, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - bool asyncCalled = false; - auto async = [&asyncCalled](const DistributedData::GenDetails &details) { - asyncCalled = true; - EXPECT_TRUE(details.empty()); - }; - - std::map processes; - auto callback = store->GetDBProcessCB(async); - callback(processes); - EXPECT_TRUE(asyncCalled); - delete store; -} - -/** -* @tc.name: GetDBProcessCB -* @tc.desc: GetDBProcessCB test. -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBProcessCB001, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - GeneralStore::DetailAsync async; - EXPECT_EQ(async, nullptr); - auto process = store->GetDBProcessCB(async); - EXPECT_NE(process, nullptr); - auto asyncs = [](const GenDetails &result) {}; - EXPECT_NE(asyncs, nullptr); - process = store->GetDBProcessCB(asyncs); - EXPECT_NE(process, nullptr); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: GetDBProcessCB -* @tc.desc: GetDBProcessCB test. -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBProcessCB002, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - std::map processes = { { "test_id", {} } }; - GeneralStore::DetailAsync async; - EXPECT_EQ(async, nullptr); - auto callback = store->GetDBProcessCB(async); - callback(processes); - delete store; -} - -/** -* @tc.name: Delete -* @tc.desc: Delete test the function. -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(KVDBGeneralStoreTest, SetDBPushDataInterceptor, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::MULTI_VERSION); - store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::MULTI_VERSION); - EXPECT_NE(metaData_.storeType, DistributedKv::KvStoreType::MULTI_VERSION); - - store->SetDBPushDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); - store->SetDBReceiveDataInterceptor(DistributedKv::KvStoreType::SINGLE_VERSION); - EXPECT_EQ(metaData_.storeType, DistributedKv::KvStoreType::SINGLE_VERSION); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: CloseTest -* @tc.desc: Close kvdb general store -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(KVDBGeneralStoreTest, CloseTest001, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - EXPECT_EQ(store->delegate_, nullptr); - auto ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_OK); - - bool isForce = false; - ret = store->Close(isForce); - EXPECT_EQ(ret, DBStatus::OK); - delete store; -} - -/** -* @tc.name: ConstructorTest -* @tc.desc: Test KVDBGeneralStore constructor with different scenarios -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(KVDBGeneralStoreTest, ConstructorTest, TestSize.Level0) -{ - // Test manager initialization with same appId - metaData_.appId = Bootstrap::GetInstance().GetProcessLabel(); - auto store1 = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store1, nullptr); - delete store1; - - // Test manager initialization with different appId - metaData_.appId = "different_app_id"; - auto store2 = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store2, nullptr); - delete store2; - - // Test GetKvStore failure path - metaData_.storeId = "invalid_store"; - auto store3 = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store3, nullptr); - EXPECT_EQ(store3->delegate_, nullptr); - delete store3; - - // Test observer registration - metaData_.storeId = storeName; - metaData_.storeType = DistributedKv::KvStoreType::SINGLE_VERSION; - auto store4 = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store4, nullptr); - delete store4; - - // Test remote push notification setup - metaData_.storeType = DistributedKv::KvStoreType::DEVICE_COLLABORATION; - auto store5 = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store5, nullptr); - delete store5; - - // Test auto-sync configuration - metaData_.isAutoSync = true; - auto store6 = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store6, nullptr); - EXPECT_EQ(store6->delegate_, nullptr); - delete store6; -} -/** -* @tc.name: GetDBPasswordTest_001 -* @tc.desc: get password with all exception branch -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_001, TestSize.Level0) -{ - metaData_.isEncrypt = false; - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - - metaData_.isEncrypt = true; - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - SecretKeyMetaData secretKey; - auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - std::vector randomKey = Random(KEY_LENGTH); - ASSERT_FALSE(randomKey.empty()); - secretKey.sKey = randomKey; - result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); -} - -/** -* @tc.name: GetDBPasswordTest_002 -* @tc.desc: GetDBPassword from meta. -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_002, TestSize.Level0) -{ - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), metaData_, true)); - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_TRUE(dbPassword.GetSize() == 0); -} - -/** -* @tc.name: GetDBPasswordTest_003 -* @tc.desc: GetDBPassword from encrypt meta. -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_003, TestSize.Level0) -{ - ZLOGI("GetDBPasswordTest_002 start"); - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - metaData_.isEncrypt = true; - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); - - auto errCode = CryptoManager::GetInstance().GenerateRootKey(); - EXPECT_EQ(errCode, CryptoManager::ErrCode::SUCCESS); - - std::vector randomKey = Random(KEY_LENGTH); - ASSERT_FALSE(randomKey.empty()); - SecretKeyMetaData secretKey; - secretKey.storeType = metaData_.storeType; - CryptoManager::CryptoParams encryptParams = { .area = metaData_.area, .userId = metaData_.user }; - secretKey.sKey = CryptoManager::GetInstance().Encrypt(randomKey, encryptParams); - ASSERT_FALSE(secretKey.sKey.empty()); - ASSERT_FALSE(encryptParams.nonce.empty()); - secretKey.area = metaData_.area; - secretKey.nonce = encryptParams.nonce; - EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true)); - - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_TRUE(dbPassword.GetSize() != 0); - randomKey.assign(randomKey.size(), 0); -} - -/** -* @tc.name: GetDBPasswordTest_004 -* @tc.desc: get password with exception branch -* @tc.type: FUNC -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_004, TestSize.Level0) -{ - metaData_.bundleName = INVALID_APPID; - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); - - SecretKeyMetaData secretKey; - auto result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - - auto key = Random(KEY_LENGTH); - ASSERT_FALSE(key.empty()); - secretKey.sKey = key; - result = MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true); - ASSERT_TRUE(result); - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - ASSERT_EQ(dbPassword.GetSize(), 0); - MetaDataManager::GetInstance().DelMeta(metaData_.GetSecretKey(), true); -} - -/** -* @tc.name: GetDBSecurityTest -* @tc.desc: GetDBSecurity -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBSecurityTest, TestSize.Level0) -{ - auto dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::INVALID_LABEL); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::NO_LABEL); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S0); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S0); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S1); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S1); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S2); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S2); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S3); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S3); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::SECE); - - dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S4); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S4); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); - - auto action = static_cast(SecurityLevel::S4 + 1); - dbSecurity = KVDBGeneralStore::GetDBSecurity(action); - EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); - EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); -} - -/** -* @tc.name: GetDBOptionTest -* @tc.desc: GetDBOption from meta and dbPassword -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBOptionTest, TestSize.Level0) -{ - metaData_.isEncrypt = true; - auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); - auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); - EXPECT_EQ(dbOption.syncDualTupleMode, true); - EXPECT_EQ(dbOption.createIfNecessary, false); - EXPECT_EQ(dbOption.isMemoryDb, false); - EXPECT_EQ(dbOption.isEncryptedDb, metaData_.isEncrypt); - EXPECT_EQ(dbOption.isNeedCompressOnSync, metaData_.isNeedCompress); - EXPECT_EQ(dbOption.schema, metaData_.schema); - EXPECT_EQ(dbOption.passwd, dbPassword); - EXPECT_EQ(dbOption.cipher, DistributedDB::CipherType::AES_256_GCM); - EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); - EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); - EXPECT_EQ(dbOption.secOption, KVDBGeneralStore::GetDBSecurity(metaData_.securityLevel)); -} - -/** -* @tc.name: CloseTest -* @tc.desc: Close kvdb general store and test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, CloseTest, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - auto ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_OK); - - KvStoreNbDelegateMock mockDelegate; - mockDelegate.taskCountMock_ = 1; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_BUSY); -} - -/** -* @tc.name: Close -* @tc.desc: RdbGeneralStore Close test -* @tc.type: FUNC -* @tc.require: -* @tc.author: shaoyuanzhao -*/ -HWTEST_F(KVDBGeneralStoreTest, BusyClose, TestSize.Level0) -{ - auto store = std::make_shared(metaData_); - ASSERT_NE(store, nullptr); - std::thread thread([store]() { - std::unique_lockrwMutex_)> lock(store->rwMutex_); - std::this_thread::sleep_for(std::chrono::seconds(1)); - }); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - auto ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_BUSY); - thread.join(); - ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_OK); -} - -/** -* @tc.name: SyncTest -* @tc.desc: Sync. -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) -{ - ZLOGI("SyncTest start"); - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), - (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - uint32_t syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; - uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; - auto mixMode = GeneralStore::MixMode(syncMode, highMode); - std::string kvQuery = ""; - DistributedKv::KVDBQuery query(kvQuery); - SyncParam syncParam{}; - syncParam.mode = mixMode; - auto ret = store->Sync({}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_NE(ret.first, GeneralError::E_OK); - auto status = store->Close(); - EXPECT_EQ(status, GeneralError::E_OK); - delete store; -} - -/** -* @tc.name: BindTest -* @tc.desc: Bind test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, BindTest, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - DistributedData::Database database; - std::map bindInfos; - GeneralStore::CloudConfig config; - EXPECT_EQ(bindInfos.empty(), true); - auto ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - - std::shared_ptr db; - std::shared_ptr loader; - GeneralStore::BindInfo bindInfo1(db, loader); - uint32_t key = 1; - bindInfos[key] = bindInfo1; - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - std::shared_ptr dbs = std::make_shared(); - std::shared_ptr loaders = std::make_shared(); - GeneralStore::BindInfo bindInfo2(dbs, loaders); - bindInfos[key] = bindInfo2; - EXPECT_EQ(store->IsBound(key), false); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); - - store->bindInfos_.clear(); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - EXPECT_EQ(store->IsBound(key), false); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - - EXPECT_EQ(store->IsBound(key), true); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); -} - -/** -* @tc.name: BindWithDifferentUsersTest -* @tc.desc: Bind test the functionality of different users. -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, BindWithDifferentUsersTest, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - EXPECT_NE(store->delegate_, nullptr); - ASSERT_NE(store, nullptr); - DistributedData::Database database; - std::map bindInfos; - GeneralStore::CloudConfig config; - - std::shared_ptr dbs = std::make_shared(); - std::shared_ptr loaders = std::make_shared(); - GeneralStore::BindInfo bindInfo(dbs, loaders); - uint32_t key0 = 100; - uint32_t key1 = 101; - bindInfos[key0] = bindInfo; - bindInfos[key1] = bindInfo; - EXPECT_EQ(store->IsBound(key0), false); - EXPECT_EQ(store->IsBound(key1), false); - auto ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - EXPECT_EQ(store->IsBound(key0), true); - EXPECT_EQ(store->IsBound(key1), true); - - uint32_t key2 = 102; - bindInfos[key2] = bindInfo; - EXPECT_EQ(store->IsBound(key2), false); - ret = store->Bind(database, bindInfos, config); - EXPECT_EQ(ret, GeneralError::E_OK); - EXPECT_EQ(store->IsBound(key2), true); -} - -/** -* @tc.name: GetDBSyncCompleteCB -* @tc.desc: GetDBSyncCompleteCB test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(KVDBGeneralStoreTest, GetDBSyncCompleteCB, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - GeneralStore::DetailAsync async; - EXPECT_EQ(async, nullptr); - KVDBGeneralStore::DBSyncCallback ret = store->GetDBSyncCompleteCB(async); - EXPECT_NE(ret, nullptr); - auto asyncs = [](const GenDetails &result) {}; - EXPECT_NE(asyncs, nullptr); - ret = store->GetDBSyncCompleteCB(asyncs); - EXPECT_NE(ret, nullptr); -} - -/** -* @tc.name: CloudSync001 -* @tc.desc: Test the scenario where the QueryUsers return false in the CloudSync function. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - std::vector devices = { "device1", "device2" }; - auto asyncs = [](const GenDetails &result) {}; - store->storeInfo_.user = 0; - auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; - store->SetEqualIdentifier(bundleName, storeName); - std::string prepareTraceId; - std::vector users; - 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); - store->delegate_ = nullptr; - delete store; -} - -/** -* @tc.name: CloudSync002 -* @tc.desc: CloudSync test the functionality of different branches. -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); - KvStoreNbDelegateMock mockDelegate; - store->delegate_ = &mockDelegate; - std::vector devices = { "device1", "device2" }; - auto asyncs = [](const GenDetails &result) {}; - store->storeInfo_.user = 0; - auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; - store->SetEqualIdentifier(bundleName, storeName); - std::string prepareTraceId; - std::vector users; - EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) - .Times(1) - .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); - - store->storeInfo_.user = 1; - cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_CLOUD_FORCE_PUSH; - ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); - EXPECT_EQ(ret, DBStatus::OK); - store->delegate_ = nullptr; - delete store; -} } // namespace DistributedDataTest } // namespace OHOS::Test \ No newline at end of file -- Gitee