From 52240dd6b165fd7a16785cd121feef0dc29c081e Mon Sep 17 00:00:00 2001 From: zqq Date: Tue, 15 Feb 2022 10:05:02 +0800 Subject: [PATCH 1/4] add test case Signed-off-by: zqq --- .../distributeddb/syncer/src/ability_sync.cpp | 5 +- ...ributeddb_relational_ver_p2p_sync_test.cpp | 47 +++++++++++++++++++ ...rtual_relational_ver_sync_db_interface.cpp | 5 +- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.cpp index 9e03dfef4..1b0509eed 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.cpp @@ -417,6 +417,7 @@ int AbilitySync::AckRecv(const Message *message, ISyncTaskContext *context) errCode = metadata_->SetDbCreateTime(deviceId_, packet->GetDbCreateTime(), true); if (errCode != E_OK) { LOGE("[AbilitySync][AckRecv] set db create time failed,errCode=%d", errCode); + context->SetTaskErrCode(errCode); return errCode; } } @@ -1033,9 +1034,7 @@ int AbilitySync::AckMsgCheck(const Message *message, ISyncTaskContext *context) int ackCode = packet->GetAckCode(); if (ackCode != E_OK) { LOGE("[AbilitySync][AckMsgCheck] received a errCode %d", ackCode); - if (ackCode == -E_SECURITY_OPTION_CHECK_ERROR) { - context->SetTaskErrCode(-E_SECURITY_OPTION_CHECK_ERROR); - } + context->SetTaskErrCode(ackCode); return ackCode; } return E_OK; diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp index ab5c039a2..2b45522f0 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp @@ -979,6 +979,53 @@ HWTEST_F(DistributedDBRelationalVerP2PSyncTest, AbilitySync002, TestSize.Level1) BlockSync(SyncMode::SYNC_MODE_PUSH_ONLY, SCHEMA_MISMATCH, {DEVICE_B}); } +/** +* @tc.name: Ability Sync 003 +* @tc.desc: Test ability sync failed when has different schema. +* @tc.type: FUNC +* @tc.require: AR000GK58N +* @tc.author: zhangqiquan +*/ +HWTEST_F(DistributedDBRelationalVerP2PSyncTest, AbilitySync003, TestSize.Level1) +{ + /** + * @tc.steps: step1. set local and remote schema is (BOOL, INTEGER, REAL, TEXT, BLOB) + */ + std::map dataMap; + std::vector schema; + std::vector localStorageType = g_storageType; + GetFieldInfo(schema, localStorageType); + + /** + * @tc.steps: step2. create table and insert data + */ + PrepareEnvironment(dataMap, schema, schema, {g_deviceB}); + + /** + * @tc.steps: step3. change local table to (BOOL, INTEGER, REAL, TEXT, BLOB) + * @tc.expected: sync fail + */ + g_communicatorAggregator->RegOnDispatch([](const std::string &target, Message *inMsg) { + if (target != "real_device") { + return; + } + if (inMsg->GetMessageType() != TYPE_NOTIFY || inMsg->GetMessageId() != ABILITY_SYNC_MESSAGE) { + return; + } + sqlite3 *db = nullptr; + EXPECT_EQ(GetDB(db), SQLITE_OK); + ASSERT_NE(db, nullptr); + std::string alterSql = "ALTER TABLE " + g_tableName + " ADD COLUMN NEW_COLUMN TEXT DEFAULT 'DEFAULT_TEXT'"; + EXPECT_EQ(sqlite3_exec(db, alterSql.c_str(), nullptr, nullptr, nullptr), SQLITE_OK); + + EXPECT_EQ(g_rdbDelegatePtr->CreateDistributedTable(g_tableName), OK); + }); + + BlockSync(SyncMode::SYNC_MODE_PUSH_ONLY, OK, {DEVICE_B}); + + g_communicatorAggregator->RegOnDispatch(nullptr); +} + /** * @tc.name: WaterMark 001 * @tc.desc: Test sync success after erase waterMark. diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp index d38a326bf..cd9a0fa63 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp @@ -89,8 +89,11 @@ int VirtualRelationalVerSyncDBInterface::PutSyncDataWithQuery(const QueryObject for (const auto &optRowDataWithLog : optTableDataWithLog.dataList) { VirtualRowData virtualRowData; virtualRowData.logInfo = optRowDataWithLog.logInfo; - int index = 0; + size_t index = 0; for (const auto &optItem : optRowDataWithLog.optionalData) { + if (index >= localFieldInfo_.size()) { + break; + } DataValue dataValue = std::move(optItem); LOGD("type:%d", optItem.GetType()); virtualRowData.objectData.PutDataValue(localFieldInfo_[index].GetFieldName(), dataValue); -- Gitee From 2725450cf377000165ef43f3da8f5de6cd124465 Mon Sep 17 00:00:00 2001 From: zqq Date: Tue, 15 Feb 2022 14:22:21 +0800 Subject: [PATCH 2/4] fix bug sync return error errCode Signed-off-by: zqq --- .../distributeddb_mock_sync_module_test.cpp | 59 +++++++++++++++++++ .../common/syncer/mock_sync_task_context.h | 2 + .../virtual_single_ver_sync_db_Interface.cpp | 8 +++ .../virtual_single_ver_sync_db_Interface.h | 3 + 4 files changed, 72 insertions(+) diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp index cc920a9ed..5af8e14fa 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp @@ -272,4 +272,63 @@ HWTEST_F(DistributedDBMockSyncModuleTest, SyncDataSync002, TestSize.Level1) EXPECT_CALL(dataSync, RemoveDeviceDataIfNeed(_)).WillRepeatedly(Return(-E_BUSY)); EXPECT_EQ(dataSync.CallPullRequestStart(&syncTaskContext), -E_BUSY); EXPECT_EQ(syncTaskContext.GetTaskErrCode(), -E_BUSY); +} + +/** + * @tc.name: AbilitySync001 + * @tc.desc: Test abilitySync abort when recv error. + * @tc.type: FUNC + * @tc.require: AR000CCPOM + * @tc.author: zhangqiquan + */ +HWTEST_F(DistributedDBMockSyncModuleTest, AbilitySync001, TestSize.Level1) +{ + MockSyncTaskContext syncTaskContext; + AbilitySync abilitySync; + + DistributedDB::Message *message = new(std::nothrow) DistributedDB::Message(); + ASSERT_TRUE(message != nullptr); + AbilitySyncAckPacket packet; + packet.SetAckCode(-E_BUSY); + message->SetCopiedObject(&packet); + EXPECT_EQ(abilitySync.AckRecv(message, &syncTaskContext), -E_BUSY); + delete message; + EXPECT_EQ(syncTaskContext.GetTaskErrCode(), -E_BUSY); +} + +/** + * @tc.name: AbilitySync002 + * @tc.desc: Test abilitySync abort when save meta failed. + * @tc.type: FUNC + * @tc.require: AR000CCPOM + * @tc.author: zhangqiquan + */ +HWTEST_F(DistributedDBMockSyncModuleTest, AbilitySync002, TestSize.Level1) +{ + MockSyncTaskContext syncTaskContext; + AbilitySync abilitySync; + MockCommunicator comunicator; + VirtualSingleVerSyncDBInterface syncDBInterface; + std::shared_ptr metaData = std::make_shared(); + metaData->Initialize(&syncDBInterface); + abilitySync.Initialize(&comunicator, &syncDBInterface, metaData, "deviceId"); + + /** + * @tc.steps: step1. set AbilitySyncAckPacket ackCode is E_OK for pass the ack check + */ + DistributedDB::Message *message = new(std::nothrow) DistributedDB::Message(); + ASSERT_TRUE(message != nullptr); + AbilitySyncAckPacket packet; + packet.SetAckCode(E_OK); + packet.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); + message->SetCopiedObject(&packet); + /** + * @tc.steps: step1. set syncDBInterface busy for save data return -E_BUSY + */ + syncDBInterface.SetBusy(true); + SyncStrategy mockStrategy = {true, false, false}; + EXPECT_CALL(syncTaskContext, GetSyncStrategy(_)).WillOnce(Return(mockStrategy)); + EXPECT_EQ(abilitySync.AckRecv(message, &syncTaskContext), -E_BUSY); + delete message; + EXPECT_EQ(syncTaskContext.GetTaskErrCode(), -E_BUSY); } \ No newline at end of file diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/mock_sync_task_context.h b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/mock_sync_task_context.h index b5a41503c..5b650bc63 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/mock_sync_task_context.h +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/mock_sync_task_context.h @@ -37,6 +37,8 @@ public: MOCK_METHOD0(Clear, void(void)); MOCK_CONST_METHOD0(GetRequestSessionId, uint32_t(void)); + + MOCK_CONST_METHOD1(GetSyncStrategy, SyncStrategy(QuerySyncObject &)); }; } // namespace DistributedDB #endif // #define MOCK_SINGLE_VER_STATE_MACHINE_H \ No newline at end of file diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp index 810ede8db..c6590ad8e 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp @@ -88,6 +88,9 @@ int VirtualSingleVerSyncDBInterface::GetMetaData(const Key &key, Value &value) c int VirtualSingleVerSyncDBInterface::PutMetaData(const Key &key, const Value &value) { + if (busy_) { + return -E_BUSY; + } metadata_[key] = value; return E_OK; } @@ -408,4 +411,9 @@ int VirtualSingleVerSyncDBInterface::RemoveSubscribe(const std::vector &subscribeIds) override; + + void SetBusy(bool busy); private: int GetSyncData(TimeStamp begin, TimeStamp end, uint32_t blockSize, std::vector& dataItems, ContinueToken& continueStmtToken) const; @@ -134,6 +136,7 @@ private: KvDBProperties properties_; uint64_t saveDataDelayTime_ = 0; SecurityOption secOption_; + bool busy_ = false; }; } // namespace DistributedDB -- Gitee From 32cd56a751236f3331a603d9002b1f4b0269171c Mon Sep 17 00:00:00 2001 From: lianhuix Date: Tue, 15 Feb 2022 15:25:28 +0800 Subject: [PATCH 3/4] Check schema changed with analysis Signed-off-by: lianhuix --- ...qlite_single_ver_relational_storage_executor.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp index a3d48fbd8..c10ad103c 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp @@ -1189,9 +1189,18 @@ int SQLiteSingleVerRelationalStorageExecutor::CheckQueryObjectLegal(const TableI return errCode; } - errCode = SQLiteUtils::CheckSchemaChanged(stmt, table, DBConstant::RELATIONAL_LOG_TABLE_FIELD_NUM); + TableInfo newTable; + SQLiteUtils::AnalysisSchema(dbHandle_, table.GetTableName(), newTable); if (errCode != E_OK) { - LOGE("Check schema failed, schema was changed. %d", errCode); + LOGE("Check new schema failed. %d", errCode); + } else { + errCode = table.CompareWithTable(newTable); + if (errCode != -E_RELATIONAL_TABLE_EQUAL && errCode != -E_RELATIONAL_TABLE_COMPATIBLE) { + LOGE("Check schema failed, schema was changed. %d", errCode); + errCode = -E_DISTRIBUTED_SCHEMA_CHANGED; + } else { + errCode = E_OK; + } } SQLiteUtils::ResetStatement(stmt, true, errCode); -- Gitee From 4efafccd751e3860423b5647f58eb4d044500c65 Mon Sep 17 00:00:00 2001 From: zqq Date: Tue, 15 Feb 2022 16:30:43 +0800 Subject: [PATCH 4/4] fix test case error Signed-off-by: zqq --- .../common/syncer/distributeddb_mock_sync_module_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp index 5af8e14fa..f1ad561d2 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp @@ -290,7 +290,7 @@ HWTEST_F(DistributedDBMockSyncModuleTest, AbilitySync001, TestSize.Level1) ASSERT_TRUE(message != nullptr); AbilitySyncAckPacket packet; packet.SetAckCode(-E_BUSY); - message->SetCopiedObject(&packet); + message->SetCopiedObject(packet); EXPECT_EQ(abilitySync.AckRecv(message, &syncTaskContext), -E_BUSY); delete message; EXPECT_EQ(syncTaskContext.GetTaskErrCode(), -E_BUSY); @@ -321,7 +321,7 @@ HWTEST_F(DistributedDBMockSyncModuleTest, AbilitySync002, TestSize.Level1) AbilitySyncAckPacket packet; packet.SetAckCode(E_OK); packet.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); - message->SetCopiedObject(&packet); + message->SetCopiedObject(packet); /** * @tc.steps: step1. set syncDBInterface busy for save data return -E_BUSY */ -- Gitee