diff --git a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_engine.cpp b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_engine.cpp index a27883b79eea86834525cb092dd15ad327dd1f2c..23e9b9fd597b7419dc3bda12eecc4dd505350a26 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_engine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_engine.cpp @@ -54,7 +54,7 @@ void SingleVerSyncEngine::EnableClearRemoteStaleData(bool enable) std::unique_lock lock(contextMapLock_); for (auto &iter : syncTaskContextMap_) { auto context = static_cast(iter.second); - if (context != nullptr) { // LCOV_EXCL_BR_LINE + if (context != nullptr) { context->EnableClearRemoteStaleData(enable); } } @@ -101,13 +101,13 @@ void SingleVerSyncEngine::StopAutoSubscribeTimer() int SingleVerSyncEngine::SubscribeTimeOut(TimerId id) { - if (!queryAutoSyncCallback_) { // LCOV_EXCL_BR_LINE + if (!queryAutoSyncCallback_) { return E_OK; } std::lock_guard lockGuard(timerLock_); std::map> allSyncQueries; GetAllUnFinishSubQueries(allSyncQueries); - if (allSyncQueries.empty()) { // LCOV_EXCL_BR_LINE + if (allSyncQueries.empty()) { return E_OK; } for (const auto &item : allSyncQueries) { diff --git a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_engine.h b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_engine.h index 785588e155e73ae28ded6f5fc992d88d1de7ddc2..164722feefa072d40b0ef61350fb5ba9ac7d1f3b 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_engine.h +++ b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_engine.h @@ -20,7 +20,7 @@ #include "sync_engine.h" namespace DistributedDB { -class SingleVerSyncEngine final : public SyncEngine { +class SingleVerSyncEngine : public SyncEngine { public: SingleVerSyncEngine() : needClearRemoteStaleData_(false) {}; diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp index b5b3f75ec020c332d96ef90cbb9c9aeb5473bd7d..3bf4b9d2bbe10d94580dc83dd1faf5d9fcce6c91 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp @@ -31,9 +31,11 @@ #include "mock_single_ver_data_sync.h" #include "mock_single_ver_kv_syncer.h" #include "mock_single_ver_state_machine.h" +#include "mock_single_ver_sync_engine.h" #include "mock_sync_engine.h" #include "mock_sync_task_context.h" #include "mock_time_sync.h" +#include "mock_unknow_sync_interface.h" #include "remote_executor_packet.h" #include "single_ver_data_sync_utils.h" #include "single_ver_kv_syncer.h" @@ -1519,6 +1521,36 @@ HWTEST_F(DistributedDBMockSyncModuleTest, SyncEngineTest006, TestSize.Level0) enginePtr->Close(); } +/** + * @tc.name: SyncEngineTest007 + * @tc.desc: Test SyncEngine handles default case by returning null context. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, SyncEngineTest007, TestSize.Level0) +{ + std::unique_ptr enginePtr = std::make_unique(); + EXPECT_CALL(*enginePtr, CreateSyncTaskContext(_)) + .WillRepeatedly(Return(nullptr)); + + auto virtualCommunicatorAggregator = new VirtualCommunicatorAggregator(); + ASSERT_NE(virtualCommunicatorAggregator, nullptr); + std::vector identifier(COMM_LABEL_LENGTH, 1u); + MockUnknowSyncInterface unknowSyncInterface; + unknowSyncInterface.SetIdentifier(identifier); + std::shared_ptr metaData = std::make_shared(); + metaData->Initialize(&unknowSyncInterface); + RuntimeContext::GetInstance()->SetCommunicatorAggregator(virtualCommunicatorAggregator); + ISyncEngine::InitCallbackParam param = { nullptr, nullptr, nullptr }; + enginePtr->Initialize(&unknowSyncInterface, metaData, param); + + auto *context = enginePtr->CreateSyncTaskContext(unknowSyncInterface); + EXPECT_EQ(context, nullptr); + enginePtr->Close(); + RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr); +} + /** * @tc.name: remote query packet 001 * @tc.desc: Test RemoteExecutorRequestPacket Serialization And DeSerialization @@ -2523,4 +2555,452 @@ HWTEST_F(DistributedDBMockSyncModuleTest, GetSavingTaskCountTest001, TestSize.Le EXPECT_TRUE(syncTaskContext->IsSavingTask(DBConstant::MIN_TIMEOUT)); RefObject::KillAndDecObjRef(syncTaskContext); } + +/** + * @tc.name: EnableClearRemoteStaleDataTest001 + * @tc.desc: Test EnableClearRemoteStaleData when syncTaskContextMap_ is not empty and context != nullptr. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, EnableClearRemoteStaleDataTest001, TestSize.Level0) +{ + auto enginePtr = std::make_unique(); + ASSERT_NE(enginePtr, nullptr); + EXPECT_CALL(*enginePtr, CreateSyncTaskContext(_)) + .WillOnce(Return(new (std::nothrow) SingleVerKvSyncTaskContext())); + + MockKvSyncInterface syncInterface; + std::vector identifier(COMM_LABEL_LENGTH, 1u); + syncInterface.SetIdentifier(identifier); + std::shared_ptr metaData = std::make_shared(); + metaData->Initialize(&syncInterface); + ISyncEngine::InitCallbackParam param = { nullptr, nullptr, nullptr }; + enginePtr->Initialize(&syncInterface, metaData, param); + + auto *context = enginePtr->CreateSyncTaskContext(syncInterface); + ASSERT_NE(context, nullptr); + + DeviceSyncTarget devTarget("dev", "user"); + std::map syncTaskContextMap = {{devTarget, context}}; + enginePtr->SetSyncTaskContextMap(syncTaskContextMap); + enginePtr->EnableClearRemoteStaleData(true); + enginePtr->Close(); +} + +/** + * @tc.name: EnableClearRemoteStaleDataTest002 + * @tc.desc: Test EnableClearRemoteStaleData when syncTaskContextMap_ is not empty and context == nullptr. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, EnableClearRemoteStaleDataTest002, TestSize.Level0) +{ + auto enginePtr = std::make_unique(); + ASSERT_NE(enginePtr, nullptr); + EXPECT_CALL(*enginePtr, CreateSyncTaskContext(_)) + .WillOnce(Return(nullptr)); + + MockKvSyncInterface syncInterface; + std::vector identifier(COMM_LABEL_LENGTH, 1u); + syncInterface.SetIdentifier(identifier); + std::shared_ptr metaData = std::make_shared(); + metaData->Initialize(&syncInterface); + ISyncEngine::InitCallbackParam param = { nullptr, nullptr, nullptr }; + enginePtr->Initialize(&syncInterface, metaData, param); + + auto *context = enginePtr->CreateSyncTaskContext(syncInterface); + ASSERT_EQ(context, nullptr); + + DeviceSyncTarget devTarget("dev", "user"); + std::map syncTaskContextMap = {{devTarget, context}}; + enginePtr->SetSyncTaskContextMap(syncTaskContextMap); + enginePtr->EnableClearRemoteStaleData(true); + enginePtr->Close(); +} + +/** + * @tc.name: StartAutoSubscribeTimer + * @tc.desc: Test StartAutoSubscribeTimer when subscribeTimerId_ > 0. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, StartAutoSubscribeTimer, TestSize.Level0) +{ + auto enginePtr = std::make_unique(); + ASSERT_NE(enginePtr, nullptr); + MockKvSyncInterface syncInterface; + EXPECT_CALL(syncInterface, IsSupportSubscribe()).WillRepeatedly(Return(E_OK)); + + std::vector identifier(COMM_LABEL_LENGTH, 1u); + syncInterface.SetIdentifier(identifier); + std::shared_ptr metaData = std::make_shared(); + metaData->Initialize(&syncInterface); + ISyncEngine::InitCallbackParam param = { nullptr, nullptr, nullptr }; + enginePtr->Initialize(&syncInterface, metaData, param); + + ISyncTaskContext *context = enginePtr->CreateSyncTaskContext(syncInterface); + DeviceSyncTarget devTarget("dev", "user"); + std::map syncTaskContextMap = {{devTarget, context}}; + enginePtr->SetSyncTaskContextMap(syncTaskContextMap); + EXPECT_EQ(enginePtr->StartAutoSubscribeTimer(syncInterface), E_OK); + EXPECT_EQ(enginePtr->StartAutoSubscribeTimer(syncInterface), -E_INTERNAL_ERROR); + enginePtr->Close(); +} + +/** + * @tc.name: SubscribeTimeOutCallbackIsNull + * @tc.desc: Test SubscribeTimeOut when Callback is Null. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, SubscribeTimeOutCallbackIsNull, TestSize.Level0) +{ + auto enginePtr = std::make_unique(); + ISyncEngine::InitCallbackParam callbackParam = { nullptr, nullptr, nullptr }; + enginePtr->SetQueryAutoSyncCallbackNull(false, callbackParam); + TimerId id = 0; + EXPECT_EQ(enginePtr->SubscribeTimeOut(id), E_OK); +} + +/** + * @tc.name: SubscribeTimeOutCallbackIsNull + * @tc.desc: Test SubscribeTimeOut when Callback is Null. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, SubscribeTimeOutAllQueriesEmpty, TestSize.Level0) +{ + auto enginePtr = std::make_unique(); + ISyncEngine::InitCallbackParam callbackParam = { nullptr, nullptr, nullptr }; + enginePtr->SetQueryAutoSyncCallbackNull(true, callbackParam); + enginePtr->InitSubscribeManager(); + TimerId id = 0; + std::map> emptyQueries; + enginePtr->GetAllUnFinishSubQueries(emptyQueries); + + ASSERT_TRUE(emptyQueries.empty()); + EXPECT_EQ(enginePtr->SubscribeTimeOut(id), E_OK); +} + +/** + * @tc.name: SubscribeTimeOutCallbackIsNull + * @tc.desc: Test SubscribeTimeOut when Callback is Null. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, SubscribeTimeOutNormal, TestSize.Level0) +{ + auto enginePtr = std::make_unique(); + ISyncEngine::InitCallbackParam callbackParam = { nullptr, nullptr, nullptr }; + + enginePtr->SetQueryAutoSyncCallbackNull(true, callbackParam); + enginePtr->InitSubscribeManager(); + + SubscribeManager subManager; + std::string device = "device_A"; + QuerySyncObject querySyncObj(Query::Select().PrefixKey({'a', static_cast('a')})); + ASSERT_TRUE(subManager.ReserveLocalSubscribeQuery(device, querySyncObj) == E_OK); + ASSERT_TRUE(subManager.ActiveLocalSubscribeQuery(device, querySyncObj) == E_OK); + + std::vector subscribeQueries; + subManager.GetLocalSubscribeQueries(device, subscribeQueries); + EXPECT_EQ(subscribeQueries.size(), 1); + + enginePtr->PutUnfinishedSubQueries(device, subscribeQueries); + TimerId id = 0; + std::map> Queries; + enginePtr->GetAllUnFinishSubQueries(Queries); + ASSERT_FALSE(Queries.empty()); + EXPECT_EQ(enginePtr->SubscribeTimeOut(id), E_OK); +} + +/** + * @tc.name: ControlCmdAckRecvSubManagerIsNull + * @tc.desc: Test ControlCmdAckRecv when SubManager is Null. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, ControlCmdAckRecvSubManagerIsNull, TestSize.Level0) +{ + MockSingleVerDataSync mockDataSync; + MockSyncTaskContext mockSyncTaskContext; + + std::shared_ptr subManager = nullptr; + mockSyncTaskContext.SetSubscribeManager(subManager); + EXPECT_TRUE(mockSyncTaskContext.GetSubscribeManager() == nullptr); + + int ret = mockDataSync.ControlCmdAckRecv(&mockSyncTaskContext, nullptr); + EXPECT_EQ(ret, -E_INVALID_ARGS); +} + +/** + * @tc.name: ControlCmdAckRecvSubPacketIsNull + * @tc.desc: Test ControlCmdAck when packet is Null. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, ControlCmdAckRecvPacketIsNull, TestSize.Level0) +{ + MockSingleVerDataSync mockDataSync; + MockSyncTaskContext mockSyncTaskContext; + auto subManager = std::make_shared(); + DistributedDB::Message *message = new (std::nothrow) DistributedDB::Message(); + mockSyncTaskContext.SetSubscribeManager(subManager); + + ASSERT_NE(message, nullptr); + EXPECT_EQ(message->GetObject(), nullptr); + EXPECT_NE(mockSyncTaskContext.GetSubscribeManager(), nullptr); + int ret = mockDataSync.ControlCmdAckRecv(&mockSyncTaskContext, message); + EXPECT_EQ(ret, -E_INVALID_ARGS); + delete message; + message = nullptr; +} + +/** + * @tc.name: SendControlAckSendError + * @tc.desc: Test SendControlAck when send() return Error. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, SendControlAckSendError, TestSize.Level0) +{ + MockSingleVerDataSync mockDataSync; + MockSyncTaskContext mockSyncTaskContext; + VirtualRelationalVerSyncDBInterface storage; + auto communicator = new (std::nothrow) MockCommunicator(); + ASSERT_NE(communicator, nullptr); + EXPECT_CALL(*communicator, SendMessage(_,_,_,_)).WillOnce(Return(-E_NOT_INIT)); + + auto mockMetadata = std::make_shared(); + std::shared_ptr metadata = std::static_pointer_cast(mockMetadata); + mockDataSync.Initialize(&storage, communicator, metadata, "deviceId"); + + DistributedDB::Message *msg = new (std::nothrow) DistributedDB::Message(TYPE_NOTIFY); + ASSERT_NE(msg, nullptr); + msg->SetMessageType(TYPE_NOTIFY); + AbilitySyncAckPacket packet; + packet.SetProtocolVersion(ABILITY_SYNC_VERSION_V1); + packet.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); + packet.SetAckCode(-E_BUSY); + msg->SetCopiedObject(packet); + + const SubscribeRequest *object = msg->GetObject(); + uint32_t controlCmdType = object->GetcontrolCmdType(); + + int ret = mockDataSync.CallSendControlAck(&mockSyncTaskContext, msg, -E_NOT_REGISTER, controlCmdType); + EXPECT_EQ(ret, -E_NOT_INIT); + + delete communicator; + delete msg; +} + +/** + * @tc.name: TestControlCmdRequestRecvPre + * @tc.desc: Test when packet is null or context->GetRemoteSoftwareVersion() <= SOFTWARE_VERSION_BASE. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, TestControlCmdRequestRecvPre, TestSize.Level0) +{ + /** + * @tc.step: 1. init. + */ + MockSingleVerDataSync mockDataSync; + MockSyncTaskContext mockSyncTaskContext; + DistributedDB::Message *message = new (std::nothrow) DistributedDB::Message(); + ASSERT_NE(message, nullptr); + /** + * @tc.case: 1. when packet is null. + */ + EXPECT_EQ(message->GetObject(), nullptr); + EXPECT_EQ(mockDataSync.CallControlCmdRequestRecvPre(&mockSyncTaskContext, message), -E_INVALID_ARGS); + /** + * @tc.case: 2. RemoteSoftwareVersion() <= SOFTWARE_VERSION_BASE. + */ + mockSyncTaskContext.SetRemoteSoftwareVersion(SOFTWARE_VERSION_BASE); + message->SetMessageType(TYPE_NOTIFY); + auto packet = new (std::nothrow) SubscribeRequest; + //set Sendcode = 100 + packet->SetPacketHead(100, SOFTWARE_VERSION_CURRENT, INVALID_CONTROL_CMD, 1); + message->SetCopiedObject(*packet); + EXPECT_NE(message->GetObject(), nullptr); + auto communicator = new (std::nothrow) MockCommunicator(); + ASSERT_NE(communicator, nullptr); + mockDataSync.SetCommunicatorHandle(communicator); + VirtualRelationalVerSyncDBInterface storage; + auto mockMetadata = std::make_shared(); + std::shared_ptr metadata = std::static_pointer_cast(mockMetadata); + mockDataSync.Initialize(&storage, communicator, metadata, "deviceId"); + + int ret1 = mockDataSync.CallControlCmdRequestRecvPre(&mockSyncTaskContext, message); + int ret2 = mockDataSync.CallDoAbilitySyncIfNeed(&mockSyncTaskContext, message, true); + EXPECT_EQ(ret1, ret2); + /** + * @tc.case: 3. controlCmdType >= ControlCmdType::INVALID_CONTROL_CMD. + */ + EXPECT_CALL(*communicator, SendMessage(_,_,_,_)).WillOnce(Return(-E_NOT_INIT)); + mockSyncTaskContext.SetRemoteSoftwareVersion(SOFTWARE_VERSION_BASE + 1); + EXPECT_EQ(mockDataSync.CallControlCmdRequestRecvPre(&mockSyncTaskContext, message), -E_WAIT_NEXT_MESSAGE); + + mockDataSync.SetCommunicatorHandle(nullptr); + RefObject::KillAndDecObjRef(communicator); + delete message; + delete packet; +} + +/** + * @tc.name: TestSubscribeRequestRecvPre + * @tc.desc: Test when controlCmdType != ControlCmdType::SUBSCRIBE_QUERY_CMD. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, TestSubscribeRequestRecvPre, TestSize.Level0) +{ + /** + * @tc.step: 1. init. + */ + MockSingleVerDataSync mockDataSync; + MockSyncTaskContext mockSyncTaskContext; + DistributedDB::Message *message = new (std::nothrow) DistributedDB::Message(); + auto packet = new (std::nothrow) SubscribeRequest; + EXPECT_NE(message, nullptr); + EXPECT_NE(packet, nullptr); + //set Sendcode = 100 + packet->SetPacketHead(100, SOFTWARE_VERSION_CURRENT, INVALID_CONTROL_CMD, 1); + message->SetCopiedObject(*packet); + + EXPECT_EQ(mockDataSync.CallSubscribeRequestRecvPre(&mockSyncTaskContext, packet, message), E_OK); + mockDataSync.SetCommunicatorHandle(nullptr); + delete message; + delete packet; +} + +/** + * @tc.name: TestSubscribeRequestRecv + * @tc.desc: Test when packet is null or submanage is null. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, TestSubscribeRequestRecv, TestSize.Level0) +{ + MockSingleVerDataSync mockDataSync; + MockSyncTaskContext mockSyncTaskContext; + DistributedDB::Message *message = new (std::nothrow) DistributedDB::Message(); + /** + * @tc.case: 1. when packet is null. + */ + ASSERT_NE(message, nullptr); + EXPECT_EQ(message->GetObject(), nullptr); + EXPECT_EQ(mockDataSync.CallSubscribeRequestRecv(&mockSyncTaskContext, message), -E_INVALID_ARGS); + /** + * @tc.case: 2. subscribeManager == nullptr. + */ + auto packet = new (std::nothrow) SubscribeRequest; + packet->SetPacketHead(100, SOFTWARE_VERSION_CURRENT, INVALID_CONTROL_CMD, 1); + message->SetCopiedObject(*packet); + VirtualRelationalVerSyncDBInterface storage; + auto communicator = new (std::nothrow) MockCommunicator(); + auto mockMetadata = std::make_shared(); + std::shared_ptr metadata = std::static_pointer_cast(mockMetadata); + mockDataSync.Initialize(&storage, communicator, metadata, "deviceId"); + EXPECT_CALL(*communicator, SendMessage(_,_,_,_)).WillRepeatedly(Return(-E_NOT_INIT)); + EXPECT_EQ(mockDataSync.CallSubscribeRequestRecv(&mockSyncTaskContext, message), -E_INVALID_ARGS); + /** + * @tc.case: 2. storage_->AddSubscribe is not E_OK. + */ + auto subManager = std::make_shared(); + mockSyncTaskContext.SetSubscribeManager(subManager); + EXPECT_NE(mockSyncTaskContext.GetSubscribeManager(), nullptr); + EXPECT_EQ(mockDataSync.CallSubscribeRequestRecv(&mockSyncTaskContext, message), -E_NOT_SUPPORT); + RefObject::KillAndDecObjRef(communicator); + delete message; + delete packet; +} + +/** + * @tc.name: UnsubscribeRequestRecvPacketIsNull + * @tc.desc: Test when packet is null. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, UnsubscribeRequestRecvPacketIsNull, TestSize.Level0) +{ + MockSingleVerDataSync mockDataSync; + MockSyncTaskContext mockSyncTaskContext; + DistributedDB::Message *message = new (std::nothrow) DistributedDB::Message(TYPE_NOTIFY); + EXPECT_NE(message, nullptr); + EXPECT_EQ(message->GetObject(), nullptr); + int ret = mockDataSync.CallUnsubscribeRequestRecv(&mockSyncTaskContext, message); + EXPECT_EQ(ret, -E_INVALID_ARGS); + delete message; + message = nullptr; +} + +/** + * @tc.name: UnsubscribeRequestRecvManagerIsNull + * @tc.desc: Test when manager is null. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, UnsubscribeRequestRecvManagerIsNull, TestSize.Level1) +{ + MockSyncTaskContext *mockSyncTaskContext = new (std::nothrow) MockSyncTaskContext(); + EXPECT_NE(mockSyncTaskContext, nullptr); + MockSingleVerDataSync mockDataSync; + VirtualRelationalVerSyncDBInterface storage; + auto communicator = new (std::nothrow) MockCommunicator(); + EXPECT_NE(communicator, nullptr); + EXPECT_CALL(*communicator, SendMessage(_,_,_,_)).WillOnce(Return(-E_NOT_INIT)); + auto mockMetadata = std::make_shared(); + std::shared_ptr metadata = std::static_pointer_cast(mockMetadata); + mockDataSync.Initialize(&storage, communicator, metadata, "deviceId"); + + DistributedDB::Message *msg = new (std::nothrow) DistributedDB::Message(TYPE_NOTIFY); + msg->SetMessageType(TYPE_NOTIFY); + AbilitySyncAckPacket packet; + packet.SetProtocolVersion(ABILITY_SYNC_VERSION_V1); + packet.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); + packet.SetAckCode(-E_BUSY); + msg->SetCopiedObject(packet); + + EXPECT_NE(msg->GetObject(), nullptr); + EXPECT_EQ(mockSyncTaskContext->GetSubscribeManager(), nullptr); + int ret = mockDataSync.CallUnsubscribeRequestRecv(mockSyncTaskContext, msg); + EXPECT_EQ(ret, -E_INVALID_ARGS); + delete mockSyncTaskContext; + delete msg; + delete communicator; + msg = nullptr; + metadata = nullptr; +} + +/** + * @tc.name: QuerySyncCheckContextIsNull + * @tc.desc: Test when context is null. + * @tc.type: FUNC + * @tc.require: + * @tc.author: xiefengzhu + */ +HWTEST_F(DistributedDBMockSyncModuleTest, QuerySyncCheckContextIsNull, TestSize.Level1) +{ + MockSingleVerDataSync mockDataSync; + MockSyncTaskContext *mockSyncTaskContext = nullptr; + EXPECT_EQ(mockSyncTaskContext, nullptr); + int ret = mockDataSync.CallQuerySyncCheck(mockSyncTaskContext); + EXPECT_EQ(ret, -E_INVALID_ARGS); } +} \ No newline at end of file diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_kv_sync_interface.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_kv_sync_interface.h index 7d9c3fa01e90be20d01f27f56c09f3a0d6482942..9010b7a2aad3640da8c6e003cb42d7bfa700c562 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_kv_sync_interface.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_kv_sync_interface.h @@ -24,6 +24,7 @@ class MockKvSyncInterface : public VirtualSingleVerSyncDBInterface { public: MOCK_METHOD0(IncRefCount, void(void)); MOCK_METHOD0(DecRefCount, void(void)); + MOCK_CONST_METHOD0(IsSupportSubscribe, int()); MOCK_CONST_METHOD1(GetDBInfo, void(DBInfo &)); }; } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_data_sync.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_data_sync.h index dc3e0d2f9f900cf9bf2b804a1adf0e3918883111..641fb003b1da01b40bb609456fd528f326934a58 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_data_sync.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_data_sync.h @@ -59,6 +59,38 @@ public: { return SingleVerDataSync::DealRemoveDeviceDataByAck(context, ackWaterMark, reserved); } + + int CallControlCmdRequestRecvPre(SingleVerSyncTaskContext *context, const Message *message) + { + return SingleVerDataSync::ControlCmdRequestRecvPre(context, message); + } + + int CallSubscribeRequestRecv(SingleVerSyncTaskContext *context, const Message *message) + { + return SingleVerDataSync::SubscribeRequestRecv(context, message); + } + + int CallUnsubscribeRequestRecv(SingleVerSyncTaskContext *context, const Message *message) + { + return SingleVerDataSync::UnsubscribeRequestRecv(context, message); + } + + int CallQuerySyncCheck(SingleVerSyncTaskContext *context) + { + return SingleVerDataSync::QuerySyncCheck(context); + } + + int CallSendControlAck(SingleVerSyncTaskContext *context, const Message *message, int32_t recvCode, + uint32_t controlCmdType) + { + return SingleVerDataSync::SendControlAck(context, message, recvCode, controlCmdType); + } + + int CallSubscribeRequestRecvPre(SingleVerSyncTaskContext *context, const SubscribeRequest *packet, + const Message *message) + { + return SingleVerDataSync::SubscribeRequestRecvPre(context, packet, message); + } }; } // namespace DistributedDB #endif // #define MOCK_SINGLE_VER_DATA_SYNC_H \ No newline at end of file diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_kv_syncer.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_kv_syncer.h index 69b775ba9598485863d561180c17dc7410ba8961..81ed46239bd5bd3297ae2cd75baa05c82daa8da4 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_kv_syncer.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_kv_syncer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2025 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 @@ -36,6 +36,13 @@ public: syncEngine_ = engine; } + ISyncEngine * GetSyncEngine() + { + return syncEngine_; + + + } + void CallQueryAutoSync(const InternalSyncParma ¶m) { SingleVerKVSyncer::QueryAutoSync(param); diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_sync_engine.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_sync_engine.h new file mode 100644 index 0000000000000000000000000000000000000000..c6647a989f8f8e3c3f33aaed93301f72dc3ed3db --- /dev/null +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_sync_engine.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MOCK_SINGLE_VER_SYNC_ENGINE_H +#define MOCK_SINGLE_VER_SYNC_ENGINE_H + +#include +#include "sync_engine.h" +#include "single_ver_sync_engine.h" + +namespace DistributedDB { +class MockSingleVerSyncEngine : public SingleVerSyncEngine { +public: + MOCK_METHOD1(CreateSyncTaskContext, ISyncTaskContext *(const ISyncInterface &syncInterface)); + + void InitSubscribeManager() + { + subManager_ = std::make_shared(); + } + + ISyncTaskContext * CallGetSyncTaskContext(const DeviceSyncTarget &target, int &errCode) + { + return SyncEngine::GetSyncTaskContext(target, errCode); + } + + void SetSyncTaskContextMap(const std::map &syncTaskContextMap) + { + std::unique_lock lock(contextMapLock_); + syncTaskContextMap_ = syncTaskContextMap; + } + + void SetQueryAutoSyncCallbackNull(bool isNeed, const InitCallbackParam &callbackParam) + { + if(!isNeed) { + this->queryAutoSyncCallback_ = nullptr; + } else { + this->queryAutoSyncCallback_ = callbackParam.queryAutoSyncCallback; + } + return; + } +}; +} // namespace DistributedDB +#endif // #define MOCK_SYNC_ENGINE_H \ No newline at end of file diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_unknow_sync_interface.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_unknow_sync_interface.h new file mode 100644 index 0000000000000000000000000000000000000000..98d9b14b6cf2530fa44e2a54600360bf64721048 --- /dev/null +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_unknow_sync_interface.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MOCK_UNKONW_SYNC_INTERFACE_H +#define MOCK_UNKONW_SYNC_INTERFACE_H + +#include +#include "virtual_single_ver_sync_db_Interface.h" + +namespace DistributedDB { +class MockUnknowSyncInterface : public VirtualSingleVerSyncDBInterface { +public: + MockUnknowSyncInterface() = default; + ~MockUnknowSyncInterface() = default; + int GetInterfaceType() const override { + return ISyncInterface::SYNC_MVD; + } +}; +} +#endif // \ No newline at end of file