diff --git a/services/distributeddataservice/adapter/communicator/test/fuzztest/softbusadapter_fuzzer/BUILD.gn b/services/distributeddataservice/adapter/communicator/test/fuzztest/softbusadapter_fuzzer/BUILD.gn index 54392bffff00f494340fe1a7013c090819c3d76a..b3c1e72797f90adad5c737f907a0601ae34be7a2 100644 --- a/services/distributeddataservice/adapter/communicator/test/fuzztest/softbusadapter_fuzzer/BUILD.gn +++ b/services/distributeddataservice/adapter/communicator/test/fuzztest/softbusadapter_fuzzer/BUILD.gn @@ -35,7 +35,17 @@ ohos_fuzztest("SoftBusAdapterFuzzTest") { "-fno-omit-frame-pointer", ] - sources = [ "softbusadapter_fuzzer.cpp" ] + sources = [ + "${data_service_path}/adapter/communicator/src/communicator_context.cpp", + "${data_service_path}/adapter/communicator/src/device_manager_adapter.cpp", + "${data_service_path}/adapter/communicator/src/softbus_client.cpp", + "softbusadapter_fuzzer.cpp", + ] + + deps = [ + "${data_service_path}/adapter/utils:distributeddata_utils", + "${data_service_path}/framework:distributeddatasvcfwk", + ] external_deps = [ "access_token:libaccesstoken_sdk", diff --git a/services/distributeddataservice/adapter/screenlock/test/screen_lock_test.cpp b/services/distributeddataservice/adapter/screenlock/test/screen_lock_test.cpp index 9374568e57385ac1ca1fa5a348bfa796271fd8a5..5384155bcb5dc94b87ca1164aa6286e4ddb4fe07 100644 --- a/services/distributeddataservice/adapter/screenlock/test/screen_lock_test.cpp +++ b/services/distributeddataservice/adapter/screenlock/test/screen_lock_test.cpp @@ -53,6 +53,8 @@ public: void TearDown() { } +protected: + static constexpr int32_t MAX_RETRY_TIME = 300; }; /** @@ -125,4 +127,57 @@ HWTEST_F(ScreenLockTest, SubscribeScreenEvent001, TestSize.Level0) screenLock->UnsubscribeScreenEvent(); screenLock->executors_ = nullptr; } + +/** +* @tc.name: SubscribeScreenEvent002 +* @tc.desc: subscribe ScreenEvent +* @tc.type: FUNC +* @tc.author: +*/ +HWTEST_F(ScreenLockTest, SubscribeScreenEvent002, TestSize.Level0) +{ + auto screenLock = std::make_shared(); + EXPECT_EQ(screenLock->executors_, nullptr); + EXPECT_EQ(screenLock->eventSubscriber_, nullptr); + screenLock->SubscribeScreenEvent(); + EXPECT_NE(screenLock->eventSubscriber_, nullptr); + screenLock->UnsubscribeScreenEvent(); +} + +/** +* @tc.name: GetTask001 +* @tc.desc: subscribe ScreenEvent +* @tc.type: FUNC +* @tc.author: +*/ +HWTEST_F(ScreenLockTest, GetTask001, TestSize.Level0) +{ + auto screenLock = std::make_shared(); + EXPECT_EQ(screenLock->executors_, nullptr); + EXPECT_EQ(screenLock->eventSubscriber_, nullptr); + auto Task = screenLock->GetTask(0); + Task(); + auto executor = std::make_shared(12, 5); + screenLock->BindExecutor(executor); + ASSERT_NE(screenLock->executors_, nullptr); + Task = screenLock->GetTask(MAX_RETRY_TIME - 1); + Task(); +} + +/** +* @tc.name: GetTask002 +* @tc.desc: subscribe ScreenEvent +* @tc.type: FUNC +* @tc.author: +*/ +HWTEST_F(ScreenLockTest, GetTask002, TestSize.Level0) +{ + auto screenLock = std::make_shared(); + EXPECT_EQ(screenLock->executors_, nullptr); + EXPECT_EQ(screenLock->eventSubscriber_, nullptr); + screenLock->SubscribeScreenEvent(); + EXPECT_NE(screenLock->eventSubscriber_, nullptr); + auto Task = screenLock->GetTask(0); + Task(); +} } // namespace \ No newline at end of file diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.h b/services/distributeddataservice/service/kvdb/kvdb_general_store.h index 80e030f05502be847d9d51d4c89f70fc2ca9d1f3..b5849b0182705bfbd3ac8e54ceed7cfc52feefc5 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.h +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.h @@ -97,8 +97,8 @@ private: DBProcessCB GetDBProcessCB(DetailAsync async); DBStatus CloudSync(const Devices &devices, DistributedDB::SyncMode cloudSyncMode, DetailAsync async, int64_t wait, const std::string &prepareTraceId); - void GetIdentifierParams( - std::vector &devices, const std::vector &uuids, int32_t authType); + void GetIdentifierParams(std::vector &devices, + const std::vector &uuids, int32_t authType); void Report(const std::string &faultType, int32_t errCode, const std::string &appendix); class ObserverProxy : public DistributedDB::KvStoreObserver { public: diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index c497a1288eb9c3d409f49d361888856b94368bbb..5bce7eda4fb46886a5e4551eca23119f1459e490 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -104,9 +104,9 @@ void KVDBServiceImpl::Init() StoreMetaMapping meta(storeInfo); meta.deviceId = DMAdapter::GetInstance().GetLocalDevice().uuid; if (!MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta, true)) { + ZLOGE("meta empty, bundleName:%{public}s, storeId:%{public}s, user = %{public}s", meta.bundleName.c_str(), + meta.GetStoreAlias().c_str(), meta.user.c_str()); if (meta.user == "0") { - ZLOGE("meta empty, bundleName:%{public}s, storeId:%{public}s, user = %{public}s", - meta.bundleName.c_str(), meta.GetStoreAlias().c_str(), meta.user.c_str()); return; } meta.user = "0"; diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index c34db72a617fcc3173e4615191b60aef0798a614..3566bb00790b96cd89b6509dd990ffe276c6cc99 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -59,10 +59,9 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - protected: - static constexpr const char *bundleName = "test_distributeddata"; - static constexpr const char *storeName = "test_service_meta"; + static constexpr const char *BUNDLE_NAME = "test_distributeddata"; + static constexpr const char *STORE_NAME = "test_service_meta"; void InitMetaData(); static std::vector Random(uint32_t len); @@ -76,15 +75,15 @@ static const uint32_t KEY_LENGTH = 32; void KVDBGeneralStoreTest::InitMetaData() { - metaData_.bundleName = bundleName; - metaData_.appId = bundleName; + metaData_.bundleName = BUNDLE_NAME; + metaData_.appId = BUNDLE_NAME; metaData_.user = "0"; metaData_.area = OHOS::DistributedKv::EL1; metaData_.instanceId = 0; metaData_.isAutoSync = true; metaData_.storeType = DistributedKv::KvStoreType::SINGLE_VERSION; - metaData_.storeId = storeName; - metaData_.dataDir = "/data/service/el1/public/database/" + std::string(bundleName) + "/kvdb"; + metaData_.storeId = STORE_NAME; + metaData_.dataDir = "/data/service/el1/public/database/" + std::string(BUNDLE_NAME) + "/kvdb"; metaData_.securityLevel = SecurityLevel::S2; } @@ -404,7 +403,7 @@ HWTEST_F(KVDBGeneralStoreTest, BusyClose, TestSize.Level0) HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) { ZLOGI("SyncTest start"); - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + mkdir(("/data/service/el1/public/database/" + std::string(BUNDLE_NAME)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); @@ -540,14 +539,14 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) { auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); + store->SetEqualIdentifier(BUNDLE_NAME, STORE_NAME); 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); + store->SetEqualIdentifier(BUNDLE_NAME, STORE_NAME); std::string prepareTraceId; std::vector users; EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(users), Return(false))); @@ -568,20 +567,22 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) { auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); + store->SetEqualIdentifier(BUNDLE_NAME, STORE_NAME); 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); + store->SetEqualIdentifier(BUNDLE_NAME, STORE_NAME); 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))); + .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); @@ -604,14 +605,14 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync003, TestSize.Level0) { auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); + store->SetEqualIdentifier(BUNDLE_NAME, STORE_NAME); 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); + store->SetEqualIdentifier(BUNDLE_NAME, STORE_NAME); std::string prepareTraceId; std::vector users = {0, 1}; EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) @@ -654,7 +655,7 @@ HWTEST_F(KVDBGeneralStoreTest, GetIdentifierParams, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) { - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + mkdir(("/data/service/el1/public/database/" + std::string(BUNDLE_NAME)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); @@ -689,7 +690,7 @@ HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, Sync003, TestSize.Level0) { - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + mkdir(("/data/service/el1/public/database/" + std::string(BUNDLE_NAME)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); @@ -974,7 +975,7 @@ HWTEST_F(KVDBGeneralStoreTest, GetDBOption001, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, Sync001, TestSize.Level0) { - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + mkdir(("/data/service/el1/public/database/" + std::string(BUNDLE_NAME)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); @@ -1209,7 +1210,7 @@ HWTEST_F(KVDBGeneralStoreTest, ConstructorTest, TestSize.Level0) delete store3; // Test observer registration - metaData_.storeId = storeName; + metaData_.storeId = STORE_NAME; metaData_.storeType = DistributedKv::KvStoreType::SINGLE_VERSION; auto store4 = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store4, nullptr); diff --git a/services/distributeddataservice/service/test/kvdb_service_impl_test.cpp b/services/distributeddataservice/service/test/kvdb_service_impl_test.cpp index 039577976be7a8734d7a87fad25c39288a4dbbfc..1d0d7d625033cbec3f486044c71eda09b7c9fc87 100644 --- a/services/distributeddataservice/service/test/kvdb_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_service_impl_test.cpp @@ -325,6 +325,31 @@ HWTEST_F(KvdbServiceImplTest, OnInitialize001, TestSize.Level0) EXPECT_EQ(result, 1); // CODE_SYNC } +/** +* @tc.name: OnInitialize002 +* @tc.desc: OnInitialize function test. +* @tc.type: FUNC +* @tc.author: my +*/ +HWTEST_F(KvdbServiceImplTest, OnInitialize002, TestSize.Level0) +{ + auto result = kvdbServiceImpl_->OnInitialize(); + EXPECT_EQ(result, Status::SUCCESS); + DistributedData::StoreInfo storeInfo; + storeInfo.bundleName = "bundleName"; + storeInfo.storeName = "storeName"; + storeInfo.user = 100; + + EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(false)) + .WillRepeatedly(testing::Return(true)); + + auto event = std::make_unique(CloudEvent::CLOUD_SYNC, storeInfo); + EXPECT_NE(event, nullptr); + result = EventCenter::GetInstance().PostEvent(move(event)); + EXPECT_EQ(result, 1); // CODE_SYNC +} + /** * @tc.name: GetStoreIdsTest001 * @tc.desc: GetStoreIds diff --git a/services/distributeddataservice/service/test/rdb_cloud_test1.cpp b/services/distributeddataservice/service/test/rdb_cloud_test1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2683faa96b00f3080e8b244716e3aa905b5c735a --- /dev/null +++ b/services/distributeddataservice/service/test/rdb_cloud_test1.cpp @@ -0,0 +1,423 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define LOG_TAG "RdbCloudTest" + +#include "rdb_cloud.h" + +#include "cloud_db_mock.h" +#include "cursor_mock.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "log_print.h" + +using namespace testing::ext; +using namespace testing; +using namespace OHOS::DistributedData; +using namespace OHOS::DistributedRdb; +using DBVBucket = DistributedDB::VBucket; +using DBStatus = DistributedDB::DBStatus; +using DBAsset = DistributedDB::Asset; +using DBAssets = DistributedDB::Assets; +using AssetOpType = DistributedDB::AssetOpType; +using AssetStatus = DistributedDB::AssetStatus; +DBAsset assetValue1 = { .version = 1, + .name = "texture_diffuse", + .assetId = "123", + .subpath = "textures/environment", + .uri = "http://asset.com/textures/123.jpg", + .modifyTime = "2025-04-05T12:30:00Z", + .createTime = "2025-04-05T10:15:00Z", + .size = "1024", + .hash = "sha256-abc123", + .flag = static_cast(AssetOpType::INSERT), + .status = static_cast(AssetStatus::NORMAL), + .timestamp = std::time(nullptr) }; + +DBAsset assetValue2 = { .version = 2, + .name = "texture_diffuse", + .assetId = "456", + .subpath = "textures/environment", + .uri = "http://asset.com/textures/456.jpg", + .modifyTime = "2025-06-19T12:30:00Z", + .createTime = "2025-04-05T10:15:00Z", + .size = "1024", + .hash = "sha256-abc456", + .flag = static_cast(AssetOpType::UPDATE), + .status = static_cast(AssetStatus::NORMAL), + .timestamp = std::time(nullptr) }; + +DBAssets assetsValue = { assetValue1, assetValue2 }; +std::vector g_DBVBuckets = { { { "#gid", { "0000000" } }, { "#flag", { true } }, + { "#value", { int64_t(100) } }, { "#float", { double(100) } }, { "#_type", { int64_t(1) } }, + { "#_error", { "E_ERROR" } }, { "#_error", { "INVALID" } }, { "#_query", { Bytes({ 1, 2, 3, 4 }) } }, + { "#asset", assetValue1 }, { "#assets", assetsValue } } }; +DBVBucket g_DBVBucket = { { "#value", { int64_t(100) } } }; + +namespace OHOS::Test { +namespace DistributedRDBTest { +class RdbCloudTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(){}; + void TearDown(){}; + static inline std::shared_ptr mockCloudDB = nullptr; + static constexpr int32_t COUNT = 2; +}; + +void RdbCloudTest::SetUpTestCase() +{ + mockCloudDB = std::make_shared(); +} + +void RdbCloudTest::TearDownTestCase() +{ + mockCloudDB = nullptr; +} + +/** +* @tc.name: RdbCloudTest001 +* @tc.desc: RdbCloud BatchInsert BatchUpdate BatchDelete test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(RdbCloudTest, RdbCloudTest001, TestSize.Level1) +{ + BindAssets bindAssets; + Bytes bytes; + RdbCloud rdbCloud(std::make_shared(), bindAssets); + std::string tableName = "testTable"; + + std::vector dataInsert = g_DBVBuckets; + std::vector extendInsert = g_DBVBuckets; + auto result = rdbCloud.BatchInsert(tableName, std::move(dataInsert), extendInsert); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); + + std::vector dataUpdate = g_DBVBuckets; + std::vector extendUpdate = g_DBVBuckets; + result = rdbCloud.BatchUpdate(tableName, std::move(dataUpdate), extendUpdate); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); + + std::vector extendDelete = g_DBVBuckets; + result = rdbCloud.BatchDelete(tableName, extendDelete); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); +} + +/** +* @tc.name: RdbCloudTest002 +* @tc.desc: RdbCloud Query PreSharing HeartBeat Close test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(RdbCloudTest, RdbCloudTest002, TestSize.Level1) +{ + BindAssets bindAssets; + RdbCloud rdbCloud(std::make_shared(), bindAssets); + std::string tableName = "testTable"; + rdbCloud.Lock(); + std::string traceId = "id"; + rdbCloud.SetPrepareTraceId(traceId); + std::vector dataInsert = g_DBVBuckets; + std::vector extendInsert = g_DBVBuckets; + auto result = rdbCloud.BatchInsert(tableName, std::move(dataInsert), extendInsert); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); + DBVBucket extends = { { "#gid", { "00000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, + { "#float", { double(100) } }, { "#_type", { int64_t(1) } }, { "#_query", { Bytes({ 1, 2, 3, 4 }) } } }; + result = rdbCloud.Query(tableName, extends, g_DBVBuckets); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); + std::vector vBuckets = { { { "#gid", { "00000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, + { "#float", { double(100) } }, { "#_type", { int64_t(1) } }, { "#_query", { Bytes({ 1, 2, 3, 4 }) } } } }; + rdbCloud.PreSharing(tableName, vBuckets); + result = rdbCloud.HeartBeat(); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); + rdbCloud.UnLock(); + rdbCloud.GetEmptyCursor(tableName); + result = rdbCloud.Close(); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); +} + +/** +* @tc.name: RdbCloudTest003 +* @tc.desc: RdbCloud Query error test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(RdbCloudTest, RdbCloudTest003, TestSize.Level1) +{ + BindAssets bindAssets = nullptr; + RdbCloud rdbCloud(std::make_shared(), bindAssets); + std::string tableName = "testTable"; + DBVBucket extends = { { "#gid", { "0000000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, + { "#float", { double(100) } }, { "#_type", { int64_t(1) } } }; + std::vector data = { { { "#gid", { "0000000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, + { "#float", { double(100) } } } }; + auto result = rdbCloud.Query(tableName, extends, data); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); + + extends = { { "#gid", { "0000000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, + { "#float", { double(100) } }, { "#_query", { Bytes({ 1, 2, 3, 4 }) } } }; + result = rdbCloud.Query(tableName, extends, data); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); + + extends = { { "#gid", { "0000000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, + { "#float", { double(100) } }, { "#_type", { int64_t(0) } } }; + result = rdbCloud.Query(tableName, extends, data); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); +} + +/** +* @tc.name: RdbCloudTest004 +* @tc.desc: RdbCloud UnLockCloudDB LockCloudDB InnerUnLock test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(RdbCloudTest, RdbCloudTest004, TestSize.Level1) +{ + BindAssets bindAssets; + RdbCloud rdbCloud(std::make_shared(), bindAssets); + + auto err = rdbCloud.UnLockCloudDB(OHOS::DistributedRdb::RdbCloud::FLAG::SYSTEM_ABILITY); + EXPECT_EQ(err, GeneralError::E_NOT_SUPPORT); + + auto result = rdbCloud.LockCloudDB(OHOS::DistributedRdb::RdbCloud::FLAG::SYSTEM_ABILITY); + EXPECT_EQ(result.first, GeneralError::E_NOT_SUPPORT); + + rdbCloud.flag_ = 1; + err = rdbCloud.InnerUnLock(static_cast(0)); + EXPECT_EQ(err, GeneralError::E_OK); +} + +/** +* @tc.name: ConvertStatus +* @tc.desc: RdbCloud ConvertStatus function test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(RdbCloudTest, ConvertStatus, TestSize.Level1) +{ + BindAssets bindAssets; + RdbCloud rdbCloud(std::make_shared(), bindAssets); + auto result = rdbCloud.ConvertStatus(GeneralError::E_OK); + EXPECT_EQ(result, DBStatus::OK); + result = rdbCloud.ConvertStatus(GeneralError::E_NETWORK_ERROR); + EXPECT_EQ(result, DBStatus::CLOUD_NETWORK_ERROR); + result = rdbCloud.ConvertStatus(GeneralError::E_LOCKED_BY_OTHERS); + EXPECT_EQ(result, DBStatus::CLOUD_LOCK_ERROR); + result = rdbCloud.ConvertStatus(GeneralError::E_RECODE_LIMIT_EXCEEDED); + EXPECT_EQ(result, DBStatus::CLOUD_FULL_RECORDS); + result = rdbCloud.ConvertStatus(GeneralError::E_NO_SPACE_FOR_ASSET); + EXPECT_EQ(result, DBStatus::CLOUD_ASSET_SPACE_INSUFFICIENT); + result = rdbCloud.ConvertStatus(GeneralError::E_RECORD_EXIST_CONFLICT); + EXPECT_EQ(result, DBStatus::CLOUD_RECORD_EXIST_CONFLICT); + result = rdbCloud.ConvertStatus(GeneralError::E_VERSION_CONFLICT); + EXPECT_EQ(result, DBStatus::CLOUD_VERSION_CONFLICT); + result = rdbCloud.ConvertStatus(GeneralError::E_RECORD_NOT_FOUND); + EXPECT_EQ(result, DBStatus::CLOUD_RECORD_NOT_FOUND); + result = rdbCloud.ConvertStatus(GeneralError::E_RECORD_ALREADY_EXISTED); + EXPECT_EQ(result, DBStatus::CLOUD_RECORD_ALREADY_EXISTED); + result = rdbCloud.ConvertStatus(GeneralError::E_FILE_NOT_EXIST); + EXPECT_EQ(result, DBStatus::LOCAL_ASSET_NOT_FOUND); + result = rdbCloud.ConvertStatus(GeneralError::E_TIME_OUT); + EXPECT_EQ(result, DBStatus::TIME_OUT); + result = rdbCloud.ConvertStatus(GeneralError::E_CLOUD_DISABLED); + EXPECT_EQ(result, DBStatus::CLOUD_DISABLED); +} + +/** +* @tc.name: ConvertQuery +* @tc.desc: RdbCloud ConvertQuery function test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(RdbCloudTest, ConvertQuery, TestSize.Level1) +{ + RdbCloud::DBQueryNodes nodes; + DistributedDB::QueryNode node = { DistributedDB::QueryNodeType::IN, "", { int64_t(1) } }; + nodes.push_back(node); + node = { DistributedDB::QueryNodeType::OR, "", { int64_t(1) } }; + nodes.push_back(node); + node = { DistributedDB::QueryNodeType::AND, "", { int64_t(1) } }; + nodes.push_back(node); + node = { DistributedDB::QueryNodeType::EQUAL_TO, "", { int64_t(1) } }; + nodes.push_back(node); + node = { DistributedDB::QueryNodeType::BEGIN_GROUP, "", { int64_t(1) } }; + nodes.push_back(node); + node = { DistributedDB::QueryNodeType::END_GROUP, "", { int64_t(1) } }; + nodes.push_back(node); + + auto result = RdbCloud::ConvertQuery(std::move(nodes)); + EXPECT_EQ(result.size(), 6); + + nodes.clear(); + node = { DistributedDB::QueryNodeType::ILLEGAL, "", { int64_t(1) } }; + nodes.push_back(node); + result = RdbCloud::ConvertQuery(std::move(nodes)); + EXPECT_EQ(result.size(), 0); +} + +/** +* @tc.name: SetPrepareTraceId001 +* @tc.desc: RdbCloud PostEvent test cloudDB_ is nullptr. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, SetPrepareTraceId001, TestSize.Level1) +{ + std::string traceId = "testId"; + EXPECT_CALL(*mockCloudDB, SetPrepareTraceId(_)).Times(0); + BindAssets bindAssets; + std::shared_ptr mockCloudDB = nullptr; + RdbCloud rdbCloud(mockCloudDB, bindAssets); + rdbCloud.SetPrepareTraceId(traceId); +} + +/** +* @tc.name: PostEvent001 +* @tc.desc: RdbCloud PostEvent test bindAssets is nullptr. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, PostEvent001, TestSize.Level1) +{ + BindAssets bindAssets; + RdbCloud rdbCloud(std::make_shared(), nullptr); + std::string traceId = "testId"; + rdbCloud.SetPrepareTraceId(traceId); + std::string tableName = "testTable"; + std::vector dataInsert = g_DBVBuckets; + std::vector extendInsert = g_DBVBuckets; + auto result = rdbCloud.BatchInsert(tableName, std::move(dataInsert), extendInsert); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); +} + +/** +* @tc.name: PostEvent002 +* @tc.desc: RdbCloud PostEvent test snapshots contains asset.uri. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, PostEvent002, TestSize.Level1) +{ + BindAssets snapshots = std::make_shared>>(); + std::shared_ptr snapshot; + snapshots->insert_or_assign(assetValue1.uri, snapshot); + RdbCloud rdbCloud(std::make_shared(), snapshots); + std::string tableName = "testTable"; + std::vector dataInsert = g_DBVBuckets; + std::vector extendInsert = g_DBVBuckets; + auto result = rdbCloud.BatchInsert(tableName, std::move(dataInsert), extendInsert); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); +} + +/** +* @tc.name: PostEvent003 +* @tc.desc: RdbCloud PostEvent test snapshots does not contains asset.uri. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, PostEvent003, TestSize.Level1) +{ + BindAssets snapshots = std::make_shared>>(); + std::shared_ptr snapshot; + std::string uri = "testuri"; + snapshots->insert_or_assign(uri, snapshot); + RdbCloud rdbCloud(std::make_shared(), snapshots); + std::string tableName = "testTable"; + std::vector dataInsert = g_DBVBuckets; + std::vector extendInsert = g_DBVBuckets; + auto result = rdbCloud.BatchInsert(tableName, std::move(dataInsert), extendInsert); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); +} + +/** +* @tc.name: PostEvent004 +* @tc.desc: RdbCloud PostEvent test the snapshot contained in the snapshots is nullpt. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, PostEvent004, TestSize.Level1) +{ + BindAssets snapshots = std::make_shared>>(); + std::shared_ptr snapshot = nullptr; + std::string uri = "testuri"; + snapshots->insert_or_assign(uri, snapshot); + RdbCloud rdbCloud(std::make_shared(), snapshots); + std::string tableName = "testTable"; + std::vector dataInsert = g_DBVBuckets; + std::vector extendInsert = g_DBVBuckets; + auto result = rdbCloud.BatchInsert(tableName, std::move(dataInsert), extendInsert); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); +} + +/** +* @tc.name: Query001 +* @tc.desc: RdbCloud Query test the cursor is nullptr and code is not E_OK. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, Query001, TestSize.Level1) +{ + std::shared_ptr cursor = nullptr; + std::string tableName = "testTable"; + EXPECT_CALL(*mockCloudDB, Query(tableName, _)).WillOnce(Return(std::make_pair(E_NETWORK_ERROR, cursor))); + BindAssets snapshots; + RdbCloud rdbCloud(mockCloudDB, snapshots); + auto result = rdbCloud.Query(tableName, g_DBVBucket, g_DBVBuckets); + EXPECT_EQ(result, DBStatus::CLOUD_NETWORK_ERROR); +} + +/** +* @tc.name: Query002 +* @tc.desc: RdbCloud Query test code is QUERY_END. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, Query002, TestSize.Level1) +{ + std::shared_ptr mockCursor = std::make_shared(); + std::string tableName = "testTable"; + EXPECT_CALL(*mockCloudDB, Query(tableName, _)).WillOnce(Return(std::make_pair(E_OK, mockCursor))); + EXPECT_CALL(*mockCursor, GetCount()).WillOnce(Return(COUNT)); + EXPECT_CALL(*mockCursor, GetEntry(_)).WillOnce(Return(E_OK)).WillOnce(Return(E_ERROR)); + EXPECT_CALL(*mockCursor, MoveToNext()).WillRepeatedly(Return(E_OK)); + EXPECT_CALL(*mockCursor, IsEnd()).WillOnce(Return(true)); + + BindAssets snapshots; + RdbCloud rdbCloud(mockCloudDB, snapshots); + auto result = rdbCloud.Query(tableName, g_DBVBucket, g_DBVBuckets); + EXPECT_EQ(result, DBStatus::QUERY_END); +} + +/** +* @tc.name: Query003 +* @tc.desc: RdbCloud Query test code is CLOUD_ERROR. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, Query003, TestSize.Level1) +{ + std::shared_ptr mockCursor = std::make_shared(); + std::string tableName = "testTable"; + EXPECT_CALL(*mockCloudDB, Query(tableName, _)).WillOnce(Return(std::make_pair(E_OK, mockCursor))); + EXPECT_CALL(*mockCursor, GetCount()).WillOnce(Return(COUNT)); + EXPECT_CALL(*mockCursor, GetEntry(_)).WillOnce(Return(E_OK)).WillOnce(Return(E_ERROR)); + EXPECT_CALL(*mockCursor, MoveToNext()).WillRepeatedly(Return(E_OK)); + EXPECT_CALL(*mockCursor, IsEnd()).WillOnce(Return(false)); + + BindAssets snapshots; + RdbCloud rdbCloud(mockCloudDB, snapshots); + auto result = rdbCloud.Query(tableName, g_DBVBucket, g_DBVBuckets); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); +} +} // namespace DistributedRDBTest +} // namespace OHOS::Test \ No newline at end of file