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 a3d48fbd84f94a886e2ed43931e7ed34f734e5b0..c10ad103c495481b45bf6406f91849965f60a5c5 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); diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.cpp index 9e03dfef46e2fb339f1d8dd694be6a2f4f4c759e..1b0509eedd20ec1fc93cdb52c263a6183fa0da67 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_mock_sync_module_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp index cc920a9ed95f1d15f6ad245e05fca5398640072c..5af8e14fadd1ee6c6cc519c6ba32d193487bcf6c 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/distributeddb_relational_ver_p2p_sync_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp index ab5c039a285fc8c5b0b493e52cc52706f8e71078..2b45522f0444436f955d3c4a75a63fe980175a60 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/mock_sync_task_context.h b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/mock_sync_task_context.h index b5a41503cb10b54a7c614f0cb45fb99594ea1358..5b650bc635468bd6f1266cf53af2feda5f18987e 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_relational_ver_sync_db_interface.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp index d38a326bff022649b405e6b2c8b929fff13046cb..cd9a0fa63b1f2bfeb03a0f17ce383e06545d68eb 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); 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 810ede8db5fb268030c77a09e34f9998960f3f37..c6590ad8e5b2a1267fbb3bc1c7d1d8505c52b843 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