From 5a896fb58903ad399a5e590e9ae577594e7c2a29 Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Tue, 20 Sep 2022 21:46:02 +0800 Subject: [PATCH 01/19] update sha algo feature Signed-off-by: zwtmichael --- .../storage/src/sqlite/sqlite_utils.cpp | 162 ++++++++++++++---- .../storage/src/sqlite/sqlite_utils.h | 12 +- ...ributeddb_relational_encrypted_db_test.cpp | 14 ++ ...tributeddb_storage_data_operation_test.cpp | 143 ++++++++++++++++ .../distributeddb_storage_encrypt_test.cpp | 2 +- ...ributeddb_relational_ver_p2p_sync_test.cpp | 61 ++++++- ...distributeddb_single_ver_p2p_sync_test.cpp | 144 ++++++++++++++++ 7 files changed, 501 insertions(+), 37 deletions(-) diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp index 3a39540e50c..15694733fed 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp @@ -57,6 +57,11 @@ namespace { const std::string JSON_EXTRACT_BY_PATH_TEST_CREATED = "SELECT json_extract_by_path('{\"field\":0}', '$.field', 0);"; const std::string DEFAULT_ATTACH_CIPHER = "PRAGMA cipher_default_attach_cipher="; const std::string DEFAULT_ATTACH_KDF_ITER = "PRAGMA cipher_default_attach_kdf_iter=5000"; + const std::string SHA1_ALGO_SQL = "PRAGMA codec_hmac_algo=SHA1;"; + const std::string SHA256_ALGO_SQL = "PRAGMA codec_hmac_algo=SHA256;"; + const std::string SHA256_ALGO_REKEY_SQL = "PRAGMA codec_rekey_hmac_algo=SHA256;"; + const std::string SHA1_ALGO_ATTACH_SQL = "PRAGMA cipher_default_attach_hmac_algo=SHA1;"; + const std::string SHA256_ALGO_ATTACH_SQL = "PRAGMA cipher_default_attach_hmac_algo=SHA256;"; const std::string EXPORT_BACKUP_SQL = "SELECT export_database('backup');"; const std::string CIPHER_CONFIG_SQL = "PRAGMA codec_cipher="; const std::string KDF_ITER_CONFIG_SQL = "PRAGMA codec_kdf_iter="; @@ -141,7 +146,6 @@ int SQLiteUtils::CreateDataBase(const OpenDbProperties &properties, sqlite3 *&db if (setWal) { sqls.push_back(WAL_MODE_SQL); } - std::string fileUrl = DBConstant::SQLITE_URL_PRE + properties.uri; int errCode = sqlite3_open_v2(fileUrl.c_str(), &dbTemp, flag, nullptr); if (errCode != SQLITE_OK) { @@ -150,7 +154,7 @@ int SQLiteUtils::CreateDataBase(const OpenDbProperties &properties, sqlite3 *&db goto END; } - errCode = SetDataBaseProperty(dbTemp, properties, sqls); + errCode = SetDataBaseProperty(dbTemp, properties, setWal, sqls); if (errCode != SQLITE_OK) { LOGE("[SQLite] SetDataBaseProperty failed: %d", errCode); goto END; @@ -432,33 +436,28 @@ int SQLiteUtils::ExecuteRawSQL(sqlite3 *db, const std::string &sql) return SQLiteUtils::MapSQLiteErrno(errCode); } -int SQLiteUtils::SetKey(sqlite3 *db, CipherType type, const CipherPassword &passwd, bool isMemDb, uint32_t iterTimes) +int SQLiteUtils::SetKey(sqlite3 *db, CipherType type, const CipherPassword &passwd, bool setWal, uint32_t iterTimes) { if (db == nullptr) { return -E_INVALID_DB; } - // in memory mode no need cipher - if (isMemDb) { - return E_OK; - } - if (passwd.GetSize() != 0) { -#ifndef OMIT_ENCRYPT - int errCode = sqlite3_key(db, static_cast(passwd.GetData()), static_cast(passwd.GetSize())); - if (errCode != SQLITE_OK) { - LOGE("[SQLiteUtils][Setkey] config key failed:(%d)", errCode); - return SQLiteUtils::MapSQLiteErrno(errCode); + int errCode = SetKeyInner(db, type, passwd, iterTimes); + if (errCode != E_OK) { + LOGE("[SQLiteUtils][Setkey] set keyInner failed:%d", errCode); + return errCode; } - - errCode = SQLiteUtils::SetCipherSettings(db, type, iterTimes); + errCode = SQLiteUtils::ExecuteRawSQL(db, SHA256_ALGO_SQL); if (errCode != E_OK) { - LOGE("[SQLiteUtils][Setkey] set cipher settings failed:%d", errCode); + LOGE("[SQLiteUtils][Setkey] set sha algo failed:%d", errCode); + return errCode; + } + errCode = SQLiteUtils::ExecuteRawSQL(db, SHA256_ALGO_REKEY_SQL); + if (errCode != E_OK) { + LOGE("[SQLiteUtils][Setkey] set rekey sha algo failed:%d", errCode); return errCode; } -#else - return -E_NOT_SUPPORT; -#endif } // verify key @@ -471,7 +470,11 @@ int SQLiteUtils::SetKey(sqlite3 *db, CipherType type, const CipherPassword &pass if (errCode == -E_BUSY) { return errCode; } - return -E_INVALID_PASSWD_OR_CORRUPTED_DB; + errCode = UpdateCipherShaAlgo(db, setWal, type, passwd, iterTimes); + if (errCode != E_OK) { + LOGE("[SQLiteUtils][Setkey] upgrade cipher sha algo failed:%d", errCode); + return errCode; + } } return E_OK; } @@ -509,6 +512,34 @@ int SQLiteUtils::GetColumnTextValue(sqlite3_stmt *statement, int index, std::str int SQLiteUtils::AttachNewDatabase(sqlite3 *db, CipherType type, const CipherPassword &password, const std::string &attachDbAbsPath, const std::string &attachAsName) +{ + int errCode = SQLiteUtils::ExecuteRawSQL(db, SHA256_ALGO_ATTACH_SQL); + if (errCode != E_OK) { + LOGE("[SQLiteUtils][AttachNewDatabase] set attach sha256 algo failed:%d", errCode); + return errCode; + } + errCode = AttachNewDatabaseInner(db, type, password, attachDbAbsPath, attachAsName); + if (errCode == -E_INVALID_PASSWD_OR_CORRUPTED_DB) { + errCode = SQLiteUtils::ExecuteRawSQL(db, SHA1_ALGO_ATTACH_SQL); + if (errCode != E_OK) { + LOGE("[SQLiteUtils][AttachNewDatabase] set attach sha1 algo failed:%d", errCode); + return errCode; + } + errCode = AttachNewDatabaseInner(db, type, password, attachDbAbsPath, attachAsName); + if (errCode != E_OK) { + LOGE("[SQLiteUtils][AttachNewDatabase] attach db failed:%d", errCode); + return errCode; + } + errCode = SQLiteUtils::ExecuteRawSQL(db, SHA256_ALGO_ATTACH_SQL); + if (errCode != E_OK) { + LOGE("[SQLiteUtils][AttachNewDatabase] set attach sha256 algo failed:%d", errCode); + } + } + return errCode; +} + +int SQLiteUtils::AttachNewDatabaseInner(sqlite3 *db, CipherType type, const CipherPassword &password, + const std::string &attachDbAbsPath, const std::string &attachAsName) { // example: "ATTACH '../new.db' AS backup KEY XXXX;" std::string attachSql = "ATTACH ? AS " + attachAsName + " KEY ?;"; // Internal interface not need verify alias name @@ -922,12 +953,14 @@ int SQLiteUtils::GetVersion(const OpenDbProperties &properties, int &version) LOGE("Open database failed: %d, sys:%d", errCode, errno); goto END; } - - errCode = SQLiteUtils::SetKey(dbTemp, properties.cipherType, properties.passwd, properties.isMemDb, - properties.iterTimes); - if (errCode != E_OK) { - LOGE("Set key failed: %d", errCode); - goto END; + // in memory mode no need cipher + if (!properties.isMemDb) { + errCode = SQLiteUtils::SetKey(dbTemp, properties.cipherType, properties.passwd, false, + properties.iterTimes); + if (errCode != E_OK) { + LOGE("Set key failed: %d", errCode); + goto END; + } } errCode = GetVersion(dbTemp, version); @@ -1983,7 +2016,7 @@ int SQLiteUtils::ExplainPlan(sqlite3 *db, const std::string &execSql, bool isQue return errCode; } -int SQLiteUtils::SetDataBaseProperty(sqlite3 *db, const OpenDbProperties &properties, +int SQLiteUtils::SetDataBaseProperty(sqlite3 *db, const OpenDbProperties &properties, bool setWal, const std::vector &sqls) { // Set the default busy handler to retry automatically before returning SQLITE_BUSY. @@ -1991,12 +2024,13 @@ int SQLiteUtils::SetDataBaseProperty(sqlite3 *db, const OpenDbProperties &proper if (errCode != E_OK) { return errCode; } - - errCode = SQLiteUtils::SetKey(db, properties.cipherType, properties.passwd, properties.isMemDb, - properties.iterTimes); - if (errCode != E_OK) { - LOGD("SQLiteUtils::SetKey fail!!![%d]", errCode); - return errCode; + if (!properties.isMemDb) { + errCode = SQLiteUtils::SetKey(db, properties.cipherType, properties.passwd, setWal, + properties.iterTimes); + if (errCode != E_OK) { + LOGD("SQLiteUtils::SetKey fail!!![%d]", errCode); + return errCode; + } } for (const auto &sql : sqls) { @@ -2184,4 +2218,66 @@ void SQLiteUtils::GetSelectCols(sqlite3_stmt *stmt, std::vector &co colNames.emplace_back(name == nullptr ? std::string() : std::string(name)); } } + +int SQLiteUtils::SetKeyInner(sqlite3 *db, CipherType type, const CipherPassword &passwd, uint32_t iterTimes) +{ +#ifndef OMIT_ENCRYPT + int errCode = sqlite3_key(db, static_cast(passwd.GetData()), static_cast(passwd.GetSize())); + if (errCode != SQLITE_OK) { + LOGE("[SQLiteUtils][SetKeyInner] config key failed:(%d)", errCode); + return SQLiteUtils::MapSQLiteErrno(errCode); + } + + errCode = SQLiteUtils::SetCipherSettings(db, type, iterTimes); + if (errCode != E_OK) { + LOGE("[SQLiteUtils][SetKeyInner] set cipher settings failed:%d", errCode); + } + return errCode; +#else + return -E_NOT_SUPPORT; +#endif +} + +int SQLiteUtils::UpdateCipherShaAlgo(sqlite3 *db, bool setWal, CipherType type, const CipherPassword &passwd, uint32_t iterTimes) +{ + if (passwd.GetSize() != 0) { + int errCode = SetKeyInner(db, type, passwd, iterTimes); + if (errCode != E_OK) { + return errCode; + } + // set sha1 algo for old version + errCode = SQLiteUtils::ExecuteRawSQL(db, SHA1_ALGO_SQL); + if (errCode != E_OK) { + LOGE("[SQLiteUtils][UpdateCipherShaAlgo] set sha algo failed:%d", errCode); + return errCode; + } + // try to get user version + errCode = SQLiteUtils::ExecuteRawSQL(db, USER_VERSION_SQL); + if (errCode != E_OK) { + LOGE("[SQLiteUtils][UpdateCipherShaAlgo] verify version failed:%d", errCode); + if (errno == EKEYREVOKED) { + return -E_EKEYREVOKED; + } + if (errCode == -E_BUSY) { + return errCode; + } + return -E_INVALID_PASSWD_OR_CORRUPTED_DB; + } + // try to update sha algo by rekey operation + errCode = SQLiteUtils::ExecuteRawSQL(db, SHA256_ALGO_REKEY_SQL); + if (errCode != E_OK) { + LOGE("[SQLiteUtils][UpdateCipherShaAlgo] set rekey sha algo failed:%d", errCode); + return errCode; + } + if (setWal) { + errCode = SQLiteUtils::ExecuteRawSQL(db, WAL_MODE_SQL); + if (errCode != E_OK) { + LOGE("[SQLite][UpdateCipherShaAlgo] execute wal sql failed: %d", errCode); + return errCode; + } + } + return Rekey(db, passwd); + } + return -E_INVALID_PASSWD_OR_CORRUPTED_DB; +} } // namespace DistributedDB \ No newline at end of file diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.h index f35765b3bc7..3b59e575fed 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.h @@ -101,7 +101,7 @@ public: static int ExecuteRawSQL(sqlite3 *db, const std::string &sql); - static int SetKey(sqlite3 *db, CipherType type, const CipherPassword &passwd, bool isMemDb, + static int SetKey(sqlite3 *db, CipherType type, const CipherPassword &passwd, bool setWal, uint32_t iterTimes = DBConstant::DEFAULT_ITER_TIMES); static int GetColumnBlobValue(sqlite3_stmt *statement, int index, std::vector &value); @@ -156,6 +156,9 @@ public: static int AttachNewDatabase(sqlite3 *db, CipherType type, const CipherPassword &password, const std::string &attachDbAbsPath, const std::string &attachAsName = "backup"); + static int AttachNewDatabaseInner(sqlite3 *db, CipherType type, const CipherPassword &password, + const std::string &attachDbAbsPath, const std::string &attachAsName); + static int CreateMetaDatabase(const std::string &metaDbPath); static int CheckIntegrity(sqlite3 *db, const std::string &sql); @@ -200,6 +203,8 @@ public: static void GetSelectCols(sqlite3_stmt *stmt, std::vector &colNames); + static int SetKeyInner(sqlite3 *db, CipherType type, const CipherPassword &passwd, uint32_t iterTimes); + private: static int CreateDataBase(const OpenDbProperties &properties, sqlite3 *&dbTemp, bool setWal); @@ -221,7 +226,7 @@ private: static void GetSysTime(sqlite3_context *ctx, int argc, sqlite3_value **argv); - static int SetDataBaseProperty(sqlite3 *db, const OpenDbProperties &properties, + static int SetDataBaseProperty(sqlite3 *db, const OpenDbProperties &properties, bool setWal, const std::vector &sqls); static int RegisterFunction(sqlite3 *db, const std::string &funcName, int nArg, void *uData, TransactFunc &func); @@ -236,6 +241,9 @@ private: static void SqliteLogCallback(void *data, int err, const char *msg); + static int UpdateCipherShaAlgo(sqlite3 *db, bool setWal, CipherType type, const CipherPassword &passwd, + uint32_t iterTimes); + static std::mutex logMutex_; static std::string lastErrorMsg_; }; diff --git a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_encrypted_db_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_encrypted_db_test.cpp index c8630e358b6..9f20c2eeb6b 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_encrypted_db_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_encrypted_db_test.cpp @@ -151,6 +151,8 @@ HWTEST_F(DistributedDBRelationalEncryptedDbTest, OpenEncryptedDBWithoutPasswdInC string sql = "PRAGMA key='" + CORRECT_KEY + "';" "PRAGMA codec_kdf_iter=" + std::to_string(DEFAULT_ITER) + ";" + "PRAGMA codec_hmac_algo=SHA256;" + "PRAGMA codec_rekey_hmac_algo=SHA256;" "PRAGMA journal_mode=WAL;" "CREATE TABLE " + g_tableName + "(key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, value INTEGER);"; ExecSqlAndAssertOK(db, sql); @@ -183,6 +185,8 @@ HWTEST_F(DistributedDBRelationalEncryptedDbTest, OpenEncryptedDBWithoutPasswdInS string sql = "PRAGMA key='" + CORRECT_KEY + "';" "PRAGMA codec_kdf_iter=" + std::to_string(DEFAULT_ITER) + ";" + "PRAGMA codec_hmac_algo=SHA256;" + "PRAGMA codec_rekey_hmac_algo=SHA256;" "PRAGMA journal_mode=WAL;" "CREATE TABLE " + g_tableName + "(key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, value INTEGER);"; ExecSqlAndAssertOK(db, sql); @@ -215,6 +219,8 @@ HWTEST_F(DistributedDBRelationalEncryptedDbTest, OpenEncryptedDBWithPasswdInSpli string sql = "PRAGMA key='" + CORRECT_KEY + "';" "PRAGMA codec_kdf_iter=" + std::to_string(DEFAULT_ITER) + ";" + "PRAGMA codec_hmac_algo=SHA256;" + "PRAGMA codec_rekey_hmac_algo=SHA256;" "PRAGMA journal_mode=WAL;" "CREATE TABLE " + g_tableName + "(key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, value INTEGER);"; ExecSqlAndAssertOK(db, sql); @@ -271,6 +277,8 @@ HWTEST_F(DistributedDBRelationalEncryptedDbTest, OpenEncryptedDBWithInvalidParam string sql = "PRAGMA key='" + CORRECT_KEY + "';" "PRAGMA codec_kdf_iter=" + std::to_string(DEFAULT_ITER) + ";" + "PRAGMA codec_hmac_algo=SHA256;" + "PRAGMA codec_rekey_hmac_algo=SHA256;" "PRAGMA journal_mode=WAL;" "CREATE TABLE " + g_tableName + "(key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, value INTEGER);"; ExecSqlAndAssertOK(db, sql); @@ -313,6 +321,8 @@ HWTEST_F(DistributedDBRelationalEncryptedDbTest, OpenEncryptedDBWithCustomizedIt string sql = "PRAGMA key='" + CORRECT_KEY + "';" "PRAGMA codec_kdf_iter=" + std::to_string(CUSTOMIZED_ITER) + ";" + "PRAGMA codec_hmac_algo=SHA256;" + "PRAGMA codec_rekey_hmac_algo=SHA256;" "PRAGMA journal_mode=WAL;" "CREATE TABLE " + g_tableName + "(key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, value INTEGER);"; ExecSqlAndAssertOK(db, sql); @@ -369,6 +379,8 @@ HWTEST_F(DistributedDBRelationalEncryptedDbTest, RekeyAfterOpenStore_001, TestSi string sql = "PRAGMA key='" + CORRECT_KEY + "';" "PRAGMA codec_kdf_iter=" + std::to_string(CUSTOMIZED_ITER) + ";" + "PRAGMA codec_hmac_algo=SHA256;" + "PRAGMA codec_rekey_hmac_algo=SHA256;" "PRAGMA journal_mode=WAL;" "CREATE TABLE " + g_tableName + "(key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, value INTEGER);"; ExecSqlAndAssertOK(db, sql); @@ -503,6 +515,8 @@ HWTEST_F(DistributedDBRelationalEncryptedDbTest, RemoteQueryForEncryptedDb_001, string sql = "PRAGMA key='" + CORRECT_KEY + "';" "PRAGMA codec_kdf_iter=" + std::to_string(DEFAULT_ITER) + ";" + "PRAGMA codec_hmac_algo=SHA256;" + "PRAGMA codec_rekey_hmac_algo=SHA256;" "PRAGMA journal_mode=WAL;" "CREATE TABLE " + g_tableName + "(key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, value INTEGER);"; ExecSqlAndAssertOK(db, sql); diff --git a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_storage_data_operation_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_storage_data_operation_test.cpp index 832cc32666f..e6c4363754c 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_storage_data_operation_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_storage_data_operation_test.cpp @@ -31,6 +31,16 @@ using namespace std; namespace { string g_testDir; SQLiteLocalKvDBConnection *g_connection = nullptr; + CipherPassword g_passwd; + CipherPassword g_passwd2; + std::vector g_passwdVect = {'p', 's', 'd', '1'}; + std::vector g_passwdVect2 = {'p', 's', 'd', '2'}; + const std::string SHA256_ALGO_SQL = "PRAGMA codec_hmac_algo=SHA256"; + const std::string SHA1_ALGO_SQL = "PRAGMA codec_hmac_algo=SHA1"; + const std::string USER_VERSION_SQL = "PRAGMA user_version;"; + const std::string SET_USER_VERSION_SQL = "PRAGMA user_version=100;"; + const std::string SHA1_ALGO_ATTACH_SQL = "PRAGMA cipher_default_attach_hmac_algo=SHA1;"; + const std::string SHA256_ALGO_ATTACH_SQL = "PRAGMA cipher_default_attach_hmac_algo=SHA256;"; } class DistributedDBStorageDataOperationTest : public testing::Test { @@ -44,6 +54,8 @@ public: void DistributedDBStorageDataOperationTest::SetUpTestCase(void) { DistributedDBToolsUnitTest::TestDirInit(g_testDir); + g_passwd.SetValue(g_passwdVect.data(), g_passwdVect.size()); + g_passwd2.SetValue(g_passwdVect2.data(), g_passwdVect2.size()); } void DistributedDBStorageDataOperationTest::TearDownTestCase(void) @@ -639,4 +651,135 @@ HWTEST_F(DistributedDBStorageDataOperationTest, CutValueIntoBlock002, TestSize.L CheckSplitData(value2, 0ul, valueDic, savedValue); CheckRecoverData(savedValue, valueDic, value2); EXPECT_EQ(valueDic.size(), 0ul); +} + +/** + * @tc.name: ShaAlgoEncryptTest001 + * @tc.desc: Test sqlite sha algo + * @tc.type: FUNC + * @tc.require: AR000HI2JS + * @tc.author: zhuwentao + */ +HWTEST_F(DistributedDBStorageDataOperationTest, ShaAlgoEncryptTest001, TestSize.Level1) +{ + sqlite3 *db = nullptr; + /** + * @tc.steps: step1. use sha256 to open db + * * @tc.expected: step1. interface return ok + */ + uint64_t flag = SQLITE_OPEN_URI | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; + std::string fileUrl = g_testDir + "/ShaAlgoEncryptTest001.db"; + EXPECT_EQ(sqlite3_open_v2(fileUrl.c_str(), &db, flag, nullptr), SQLITE_OK); + EXPECT_EQ(SQLiteUtils::SetKeyInner(db, CipherType::AES_256_GCM, g_passwd, DBConstant::DEFAULT_ITER_TIMES), E_OK); + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, SHA256_ALGO_SQL), E_OK); + ASSERT_TRUE(SQLiteUtils::ExecuteRawSQL(db, SET_USER_VERSION_SQL) == E_OK); + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, USER_VERSION_SQL), E_OK); + /** + * @tc.steps: step2. close db + * * @tc.expected: step2. interface return ok + */ + sqlite3_close_v2(db); + db = nullptr; + /** + * @tc.steps: step1. use sha1 to open db + * * @tc.expected: step1. interface return not ok + */ + EXPECT_EQ(sqlite3_open_v2(fileUrl.c_str(), &db, flag, nullptr), SQLITE_OK); + EXPECT_EQ(SQLiteUtils::SetKeyInner(db, CipherType::AES_256_GCM, g_passwd, DBConstant::DEFAULT_ITER_TIMES), E_OK); + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, SHA1_ALGO_SQL), E_OK); + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, USER_VERSION_SQL), -E_INVALID_PASSWD_OR_CORRUPTED_DB); + sqlite3_close_v2(db); +} + +/** + * @tc.name: ShaAlgoEncryptTest002 + * @tc.desc: Test sqlite sha algo in attach mode + * @tc.type: FUNC + * @tc.require: AR000HI2JS + * @tc.author: zhuwentao + */ +HWTEST_F(DistributedDBStorageDataOperationTest, ShaAlgoEncryptTest002, TestSize.Level1) +{ + sqlite3 *db = nullptr; + /** + * @tc.steps: step1. use sha256 to open db + * * @tc.expected: step1. interface return ok + */ + uint64_t flag = SQLITE_OPEN_URI | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; + std::string fileUrl = g_testDir + "/ShaAlgoEncryptTest002_attach.db"; + EXPECT_EQ(sqlite3_open_v2(fileUrl.c_str(), &db, flag, nullptr), SQLITE_OK); + EXPECT_EQ(SQLiteUtils::SetKeyInner(db, CipherType::AES_256_GCM, g_passwd, DBConstant::DEFAULT_ITER_TIMES), E_OK); + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, SHA256_ALGO_SQL), E_OK); + ASSERT_TRUE(SQLiteUtils::ExecuteRawSQL(db, SET_USER_VERSION_SQL) == E_OK); + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, USER_VERSION_SQL), E_OK); + /** + * @tc.steps: step2. close db + * * @tc.expected: step2. interface return ok + */ + sqlite3_close_v2(db); + db = nullptr; + /** + * @tc.steps: step3. open new db and attach old db + * * @tc.expected: step3. interface return ok + */ + std::string fileUrl2 = g_testDir + "/ShaAlgoEncryptTest002.db"; + EXPECT_EQ(sqlite3_open_v2(fileUrl2.c_str(), &db, flag, nullptr), SQLITE_OK); + EXPECT_EQ(SQLiteUtils::SetKeyInner(db, CipherType::AES_256_GCM, g_passwd2, DBConstant::DEFAULT_ITER_TIMES), E_OK); + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, SHA256_ALGO_SQL), E_OK); + ASSERT_TRUE(SQLiteUtils::ExecuteRawSQL(db, SET_USER_VERSION_SQL) == E_OK); + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, USER_VERSION_SQL), E_OK); + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, SHA256_ALGO_ATTACH_SQL), E_OK); + std::string attachName = "EncryptTest002"; + EXPECT_EQ(SQLiteUtils::AttachNewDatabaseInner(db, CipherType::AES_256_GCM, g_passwd, fileUrl, attachName), E_OK); + /** + * @tc.steps: step4. close db + * * @tc.expected: step4. interface return ok + */ + sqlite3_close_v2(db); +} + +/** + * @tc.name: ShaAlgoEncryptTest003 + * @tc.desc: Test sqlite sha algo in attach mode + * @tc.type: FUNC + * @tc.require: AR000HI2JS + * @tc.author: zhuwentao + */ +HWTEST_F(DistributedDBStorageDataOperationTest, ShaAlgoEncryptTest003, TestSize.Level1) +{ + sqlite3 *db = nullptr; + /** + * @tc.steps: step1. use sha256 to open db + * * @tc.expected: step1. interface return ok + */ + uint64_t flag = SQLITE_OPEN_URI | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; + std::string fileUrl = g_testDir + "/ShaAlgoEncryptTest003_attach.db"; + EXPECT_EQ(sqlite3_open_v2(fileUrl.c_str(), &db, flag, nullptr), SQLITE_OK); + EXPECT_EQ(SQLiteUtils::SetKeyInner(db, CipherType::AES_256_GCM, g_passwd, DBConstant::DEFAULT_ITER_TIMES), E_OK); + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, SHA1_ALGO_SQL), E_OK); + ASSERT_TRUE(SQLiteUtils::ExecuteRawSQL(db, SET_USER_VERSION_SQL) == E_OK); + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, USER_VERSION_SQL), E_OK); + /** + * @tc.steps: step2. close db + * * @tc.expected: step2. interface return ok + */ + sqlite3_close_v2(db); + db = nullptr; + /** + * @tc.steps: step3. open new db and attach old db + * * @tc.expected: step3. interface return ok + */ + std::string fileUrl2 = g_testDir + "/ShaAlgoEncryptTest003.db"; + EXPECT_EQ(sqlite3_open_v2(fileUrl2.c_str(), &db, flag, nullptr), SQLITE_OK); + EXPECT_EQ(SQLiteUtils::SetKeyInner(db, CipherType::AES_256_GCM, g_passwd2, DBConstant::DEFAULT_ITER_TIMES), E_OK); + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, SHA256_ALGO_SQL), E_OK); + ASSERT_TRUE(SQLiteUtils::ExecuteRawSQL(db, SET_USER_VERSION_SQL) == E_OK); + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, USER_VERSION_SQL), E_OK); + std::string attachName = "EncryptTest003"; + EXPECT_EQ(SQLiteUtils::AttachNewDatabase(db, CipherType::AES_256_GCM, g_passwd, fileUrl, attachName), E_OK); + /** + * @tc.steps: step4. close db + * * @tc.expected: step4. interface return ok + */ + sqlite3_close_v2(db); } \ No newline at end of file diff --git a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_storage_encrypt_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_storage_encrypt_test.cpp index ca927e8b6c1..e1733785e15 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_storage_encrypt_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_storage_encrypt_test.cpp @@ -1348,7 +1348,7 @@ HWTEST_F(DistributedDBStorageEncryptTest, EncryptTest025, TestSize.Level1) * @tc.expected: step6. Return SQLITE_OK. */ Value valueGet; - GetValue(KEY_1, valueGet); + EXPECT_EQ(GetValue(KEY_1, valueGet), SQLITE_OK); EXPECT_EQ(valueGet, VALUE_1); /** diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp index d2b6ecea96b..890d2013895 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp @@ -55,6 +55,9 @@ namespace { const char DEFAULT_CHAR = 'D'; const std::string DEFAULT_TEXT = "This is a text"; const std::vector DEFAULT_BLOB(ONE_HUNDERED, DEFAULT_CHAR); + const std::string SHA1_ALGO_SQL = "PRAGMA codec_hmac_algo=SHA1"; + const std::string SHA256_ALGO_SQL = "PRAGMA codec_hmac_algo=SHA256"; + const std::string SHA256_ALGO_REKEY_SQL = "PRAGMA codec_rekey_hmac_algo=SHA256"; RelationalStoreManager g_mgr(APP_ID, USER_ID); std::string g_testDir; @@ -95,7 +98,7 @@ namespace { ASSERT_TRUE(g_rdbDelegatePtr != nullptr); } - int GetDB(sqlite3 *&db) + int GetDB(sqlite3 *&db, bool isSha256Algo = true) { int flag = SQLITE_OPEN_URI | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; const auto &dbPath = g_dbDir; @@ -108,6 +111,12 @@ namespace { "PRAGMA key='" + (g_isAfterRekey ? REKEY_KEY : CORRECT_KEY) + "';" "PRAGMA codec_kdf_iter=" + std::to_string(DEFAULT_ITER) + ";"; EXPECT_EQ(sqlite3_exec(db, sql.c_str(), nullptr, nullptr, nullptr), SQLITE_OK); + if (isSha256Algo) { + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, SHA256_ALGO_SQL), E_OK); + } else { + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, SHA1_ALGO_SQL), E_OK); + } + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, SHA256_ALGO_REKEY_SQL), E_OK); #endif EXPECT_EQ(SQLiteUtils::RegisterCalcHash(db), E_OK); EXPECT_EQ(SQLiteUtils::RegisterGetSysTime(db), E_OK); @@ -1932,4 +1941,54 @@ HWTEST_F(DistributedDBRelationalVerP2PSyncTest, OrderbyWriteTimeSync001, TestSiz EXPECT_EQ(g_rdbDelegatePtr->Sync({DEVICE_B}, DistributedDB::SYNC_MODE_PUSH_ONLY, query, nullptr, false), NOT_SUPPORT); } + +/** +* @tc.name: EncryptedAlgoUpgrade001 + * @tc.desc: Test upgrade encrypted db can sync normally + * @tc.type: FUNC + * @tc.require: AR000HI2JS + * @tc.author: zhuwentao +*/ +HWTEST_F(DistributedDBRelationalVerP2PSyncTest, EncryptedAlgoUpgrade001, TestSize.Level0) +{ + if (g_rdbDelegatePtr != nullptr) { + LOGD("CloseStore Start"); + ASSERT_EQ(g_mgr.CloseStore(g_rdbDelegatePtr), OK); + g_rdbDelegatePtr = nullptr; + } + EXPECT_EQ(OS::RemoveFile(g_dbDir), E_OK); + /** + * @tc.steps: step1. open old db use sha1 algo and insert some data + * @tc.expected: step1. interface return ok + */ + sqlite3 *db = nullptr; + EXPECT_EQ(GetDB(db, false), SQLITE_OK); + const std::string user_version_sql = "PRAGMA user_version=101;"; + EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db, user_version_sql), E_OK); + sqlite3_close(db); + + /** + * @tc.steps: step2. open db by OpenStore + * @tc.expected: step2. interface return ok + */ + OpenStore(); + /** + * @tc.steps: step3. sync with push + * @tc.expected: step3. interface return ok + */ + std::map dataMap; + PrepareEnvironment(dataMap, {g_deviceB}); + BlockSync(SyncMode::SYNC_MODE_PUSH_ONLY, OK, {DEVICE_B}); + CheckVirtualData(dataMap); + dataMap.clear(); + + /** + * @tc.steps: step4. sync with pull + * @tc.expected: step4. interface return ok + */ + + Query query = Query::Select(g_tableName); + g_deviceB->GenericVirtualDevice::Sync(DistributedDB::SYNC_MODE_PULL_ONLY, query, true); + CheckVirtualData(dataMap); +} #endif \ No newline at end of file diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp index 21ac5c0a518..cb1b9de8de0 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp @@ -18,6 +18,7 @@ #include #include "db_constant.h" +#include "db_common.h" #include "distributeddb_data_generate_unit_test.h" #include "distributeddb_tools_unit_test.h" #include "kv_store_nb_delegate.h" @@ -40,6 +41,16 @@ namespace { const std::string DEVICE_A = "real_device"; const std::string DEVICE_B = "deviceB"; const std::string DEVICE_C = "deviceC"; + const std::string CREATE_SYNC_TABLE_SQL = + "CREATE TABLE IF NOT EXISTS sync_data(" \ + "key BLOB NOT NULL," \ + "value BLOB," \ + "timestamp INT NOT NULL," \ + "flag INT NOT NULL," \ + "device BLOB," \ + "ori_device BLOB," \ + "hash_key BLOB PRIMARY KEY NOT NULL," \ + "w_timestamp INT);"; KvStoreDelegateManager g_mgr(APP_ID, USER_ID); KvStoreConfig g_config; @@ -53,6 +64,65 @@ namespace { // the type of g_kvDelegateCallback is function auto g_kvDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback, placeholders::_1, placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(g_kvDelegatePtr)); + + void PullSyncTest() + { + DBStatus status = OK; + std::vector devices; + devices.push_back(g_deviceB->GetDeviceId()); + + Key key = {'1'}; + Key key2 = {'2'}; + Value value = {'1'}; + g_deviceB->PutData(key, value, 0, 0); + g_deviceB->PutData(key2, value, 1, 0); + + std::map result; + status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PULL_ONLY, result); + ASSERT_TRUE(status == OK); + + ASSERT_TRUE(result.size() == devices.size()); + for (const auto &pair : result) { + LOGD("dev %s, status %d", pair.first.c_str(), pair.second); + EXPECT_TRUE(pair.second == OK); + } + Value value3; + EXPECT_EQ(g_kvDelegatePtr->Get(key, value3), OK); + EXPECT_EQ(value3, value); + EXPECT_EQ(g_kvDelegatePtr->Get(key2, value3), OK); + EXPECT_EQ(value3, value); + } + + void CrudTest() + { + vector entries; + int totalSize = 10; + for (int i = 0; i < totalSize; i++) { + Entry entry; + entry.key.push_back(i); + entry.value.push_back('2'); + entries.push_back(entry); + } + EXPECT_TRUE(g_kvDelegatePtr->PutBatch(entries) == OK); + for (const auto &entry : entries) { + Value resultvalue; + EXPECT_TRUE(g_kvDelegatePtr->Get(entry.key, resultvalue) == OK); + EXPECT_TRUE(resultvalue == entry.value); + } + for (int i = 0; i < totalSize / 2; i++) { + g_kvDelegatePtr->Delete(entries[i].key); + Value resultvalue; + EXPECT_TRUE(g_kvDelegatePtr->Get(entries[i].key, resultvalue) == NOT_FOUND); + } + for (int i = totalSize / 2; i < totalSize; i++) { + Value value = entries[i].value; + value.push_back('x'); + EXPECT_TRUE(g_kvDelegatePtr->Put(entries[i].key, value) == OK); + Value resultvalue; + EXPECT_TRUE(g_kvDelegatePtr->Get(entries[i].key, resultvalue) == OK); + EXPECT_TRUE(resultvalue == value); + } + } } class DistributedDBSingleVerP2PSyncTest : public testing::Test { @@ -2530,3 +2600,77 @@ HWTEST_F(DistributedDBSingleVerP2PSyncTest, OrderbyWriteTimeSync001, TestSize.Le Query query = Query::Select().PrefixKey({'k'}).OrderByWriteTime(true); EXPECT_EQ(g_kvDelegatePtr->Sync(devices, DistributedDB::SYNC_MODE_PUSH_ONLY, nullptr, query, true), NOT_SUPPORT); } + +/** + * @tc.name: EncryptedAlgoUpgrade001 + * @tc.desc: Test upgrade encrypted db can sync normally + * @tc.type: FUNC + * @tc.require: AR000HI2JS + * @tc.author: zhuwentao + */ +HWTEST_F(DistributedDBSingleVerP2PSyncTest, EncryptedAlgoUpgrade001, TestSize.Level3) +{ + /** + * @tc.steps: step1. clear db + * * @tc.expected: step1. interface return ok + */ + if (g_kvDelegatePtr != nullptr) { + ASSERT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK); + g_kvDelegatePtr = nullptr; + DBStatus status = g_mgr.DeleteKvStore(STORE_ID); + LOGD("delete kv store status %d", status); + ASSERT_TRUE(status == OK); + } + + CipherPassword passwd; + std::vector passwdVect = {'p', 's', 'd', '1'}; + passwd.SetValue(passwdVect.data(), passwdVect.size()); + /** + * @tc.steps: step2. open old db by sql + * * @tc.expected: step2. interface return ok + */ + std::string identifier = DBCommon::GenerateIdentifierId(STORE_ID, APP_ID, USER_ID); + std::string hashDir = DBCommon::TransferHashString(identifier); + std::string hexHashDir = DBCommon::TransferStringToHex(hashDir); + std::string dbPath = g_testDir + "/" + hexHashDir + "/single_ver"; + ASSERT_TRUE(DBCommon::CreateDirectory(g_testDir + "/" + hexHashDir) == OK); + ASSERT_TRUE(DBCommon::CreateDirectory(dbPath) == OK); + std::vector dbDir {DBConstant::MAINDB_DIR, DBConstant::METADB_DIR, DBConstant::CACHEDB_DIR}; + for (const auto &item : dbDir) { + ASSERT_TRUE(DBCommon::CreateDirectory(dbPath + "/" + item) == OK); + } + uint64_t flag = SQLITE_OPEN_URI | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; + sqlite3 *db; + std::string fileUrl = dbPath + "/" + DBConstant::MAINDB_DIR + "/" + DBConstant::SINGLE_VER_DATA_STORE + ".db"; + ASSERT_TRUE(sqlite3_open_v2(fileUrl.c_str(), &db, flag, nullptr) == SQLITE_OK); + SQLiteUtils::SetKeyInner(db, CipherType::AES_256_GCM, passwd, DBConstant::DEFAULT_ITER_TIMES); + /** + * @tc.steps: step3. create table and close + * * @tc.expected: step3. interface return ok + */ + ASSERT_TRUE(SQLiteUtils::ExecuteRawSQL(db, CREATE_SYNC_TABLE_SQL) == E_OK); + sqlite3_close_v2(db); + db = nullptr; + LOGI("create old db success"); + /** + * @tc.steps: step4. get kvstore + * * @tc.expected: step4. interface return ok + */ + KvStoreNbDelegate::Option option; + option.isEncryptedDb = true; + option.cipher = CipherType::AES_256_GCM; + option.passwd = passwd; + g_mgr.GetKvStore(STORE_ID, option, g_kvDelegateCallback); + ASSERT_TRUE(g_kvDelegateStatus == OK); + ASSERT_TRUE(g_kvDelegatePtr != nullptr); + /** + * @tc.steps: step5. sync ok + * * @tc.expected: step5. interface return ok + */ + PullSyncTest(); + /** + * @tc.steps: step5. crud ok + * * @tc.expected: step5. interface return ok + */ + CrudTest(); +} -- Gitee From 2bbebba5cd0c15ae2eed743615413eaeed7a17f2 Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Mon, 10 Oct 2022 22:14:04 +0800 Subject: [PATCH 02/19] add define Signed-off-by: zwtmichael --- .../storage/src/sqlite/sqlite_utils.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp index e074aadb778..961e2a09abe 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp @@ -453,6 +453,7 @@ int SQLiteUtils::SetKey(sqlite3 *db, CipherType type, const CipherPassword &pass } if (passwd.GetSize() != 0) { +#ifndef OMIT_ENCRYPT int errCode = SetKeyInner(db, type, passwd, iterTimes); if (errCode != E_OK) { LOGE("[SQLiteUtils][Setkey] set keyInner failed:%d", errCode); @@ -468,6 +469,9 @@ int SQLiteUtils::SetKey(sqlite3 *db, CipherType type, const CipherPassword &pass LOGE("[SQLiteUtils][Setkey] set rekey sha algo failed:%d", errCode); return errCode; } +#else + return -E_NOT_SUPPORT; +#endif } // verify key @@ -480,13 +484,14 @@ int SQLiteUtils::SetKey(sqlite3 *db, CipherType type, const CipherPassword &pass if (errCode == -E_BUSY) { return errCode; } +#ifndef OMIT_ENCRYPT errCode = UpdateCipherShaAlgo(db, setWal, type, passwd, iterTimes); if (errCode != E_OK) { LOGE("[SQLiteUtils][Setkey] upgrade cipher sha algo failed:%d", errCode); - return errCode; } +#endif } - return E_OK; + return errCode; } int SQLiteUtils::GetColumnBlobValue(sqlite3_stmt *statement, int index, std::vector &value) @@ -523,12 +528,15 @@ int SQLiteUtils::GetColumnTextValue(sqlite3_stmt *statement, int index, std::str int SQLiteUtils::AttachNewDatabase(sqlite3 *db, CipherType type, const CipherPassword &password, const std::string &attachDbAbsPath, const std::string &attachAsName) { +#ifndef OMIT_ENCRYPT int errCode = SQLiteUtils::ExecuteRawSQL(db, SHA256_ALGO_ATTACH_SQL); if (errCode != E_OK) { LOGE("[SQLiteUtils][AttachNewDatabase] set attach sha256 algo failed:%d", errCode); return errCode; } +#endif errCode = AttachNewDatabaseInner(db, type, password, attachDbAbsPath, attachAsName); +#ifndef OMIT_ENCRYPT if (errCode == -E_INVALID_PASSWD_OR_CORRUPTED_DB) { errCode = SQLiteUtils::ExecuteRawSQL(db, SHA1_ALGO_ATTACH_SQL); if (errCode != E_OK) { @@ -545,6 +553,7 @@ int SQLiteUtils::AttachNewDatabase(sqlite3 *db, CipherType type, const CipherPas LOGE("[SQLiteUtils][AttachNewDatabase] set attach sha256 algo failed:%d", errCode); } } +#endif return errCode; } -- Gitee From 1011d7adb68b12ca4d4931d0f9d4f243fc6d7ca4 Mon Sep 17 00:00:00 2001 From: zqq Date: Tue, 11 Oct 2022 14:59:56 +0800 Subject: [PATCH 03/19] 1.begin notify before get machine lock 2.send notify packet before get machine lock Signed-off-by: zqq --- .../src/single_ver_sync_state_machine.cpp | 8 +-- .../syncer/src/sync_state_machine.cpp | 19 +++---- ...buteddb_single_ver_p2p_sync_check_test.cpp | 53 +++++++++++++++++++ 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp index 1ad51ce9f52..3c19347ed3a 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp @@ -645,16 +645,16 @@ int SingleVerSyncStateMachine::HandleDataRequestRecv(const Message *inMsg) performance->StepTimeRecordStart(PT_TEST_RECORDS::RECORD_DATA_REQUEST_RECV_TO_SEND_ACK); } DecRefCountOfFeedDogTimer(SyncDirectionFlag::RECEIVE); + + // RequestRecv will save data, it may cost a long time. + // So we need to send save data notify to keep remote alive. + bool isNeedStop = StartSaveDataNotify(inMsg->GetSessionId(), inMsg->GetSequenceId(), inMsg->GetMessageId()); { std::lock_guard lockWatchDog(stateMachineLock_); if (IsNeedResetWatchdog(inMsg)) { (void)ResetWatchDog(); } } - - // RequestRecv will save data, it may cost a long time. - // So we need to send save data notify to keep remote alive. - bool isNeedStop = StartSaveDataNotify(inMsg->GetSessionId(), inMsg->GetSequenceId(), inMsg->GetMessageId()); WaterMark pullEndWaterkark = 0; errCode = dataSync_->DataRequestRecv(context_, inMsg, pullEndWaterkark); if (performance != nullptr) { diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp index a8d1a649d74..52e8da2e2bf 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp @@ -375,17 +375,18 @@ void SyncStateMachine::DecRefCountOfFeedDogTimer(SyncDirectionFlag flag) void SyncStateMachine::DoSaveDataNotify(uint32_t sessionId, uint32_t sequenceId, uint32_t inMsgId) { + // we send notify packet at first, because it will cost a lot of time to get machine lock { - std::lock_guard lock(stateMachineLock_); - (void)ResetWatchDog(); - } - std::lock_guard innerLock(saveDataNotifyLock_); - if (saveDataNotifyCount_ >= MAX_DATA_NOTIFY_COUNT) { - StopSaveDataNotifyNoLock(); - return; + std::lock_guard innerLock(saveDataNotifyLock_); + if (saveDataNotifyCount_ >= MAX_DATA_NOTIFY_COUNT) { + StopSaveDataNotifyNoLock(); + return; + } + SendNotifyPacket(sessionId, sequenceId, inMsgId); + saveDataNotifyCount_++; } - SendNotifyPacket(sessionId, sequenceId, inMsgId); - saveDataNotifyCount_++; + std::lock_guard lock(stateMachineLock_); + (void)ResetWatchDog(); } void SyncStateMachine::DoFeedDogForSync(SyncDirectionFlag flag) diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp index 44a5353779d..e5ef87fbea8 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp @@ -40,6 +40,7 @@ namespace { const int SLEEP_MILLISECONDS = 500; const int TEN_SECONDS = 10; const int THREE_HUNDRED = 300; + const int WAIT_10_SECONDS = 10000; const int WAIT_30_SECONDS = 30000; const int WAIT_40_SECONDS = 40000; @@ -1372,4 +1373,56 @@ HWTEST_F(DistributedDBSingleVerP2PSyncCheckTest, GetDataNotify001, TestSize.Leve EXPECT_EQ(virtualRes.size(), devices.size()); EXPECT_EQ(virtualRes[DEVICE_A], static_cast(SyncOperation::OP_FINISHED_ALL)); g_deviceB->DelayGetSyncData(0); +} + +/** + * @tc.name: GetDataNotify002 + * @tc.desc: Test GetDataNotify function, two device sync each other at same time + * @tc.type: FUNC + * @tc.require: AR000D4876 + * @tc.author: zhangqiquan + */ +HWTEST_F(DistributedDBSingleVerP2PSyncCheckTest, GetDataNotify002, TestSize.Level3) +{ + ASSERT_NE(g_kvDelegatePtr, nullptr); + DBStatus status = OK; + std::vector devices; + devices.push_back(g_deviceB->GetDeviceId()); + const std::string DEVICE_A = "real_device"; + + /** + * @tc.steps: step1. deviceA sync first to finish time sync and ability sync + */ + std::map result; + status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PUSH_ONLY, result, true); + EXPECT_EQ(status, OK); + EXPECT_EQ(result.size(), devices.size()); + EXPECT_EQ(result[DEVICE_B], OK); + /** + * @tc.steps: step2. deviceB set get data delay 10s + */ + g_deviceB->DelayGetSyncData(WAIT_10_SECONDS); + + /** + * @tc.steps: step3. deviceB call sync and wait + */ + std::thread asyncThread([]() { + std::map virtualRes; + Query query = Query::Select(); + g_deviceB->Sync(SYNC_MODE_PUSH_ONLY, query, [&virtualRes](std::map resMap) { + virtualRes = std::move(resMap); + }, true); + }); + + /** + * @tc.steps: step4. deviceA call sync and wait + * @tc.expected: step4. sync should return OK. because notify timer trigger (10s - 1s)/2s => 4times + */ + std::this_thread::sleep_for(std::chrono::seconds(1)); + status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PUSH_ONLY, result, true); + EXPECT_EQ(status, OK); + EXPECT_EQ(result.size(), devices.size()); + EXPECT_EQ(result[DEVICE_B], OK); + asyncThread.join(); + std::this_thread::sleep_for(std::chrono::seconds(TEN_SECONDS)); } \ No newline at end of file -- Gitee From c84ab3e749f3ad63dc27cfec0add73dca88c8801 Mon Sep 17 00:00:00 2001 From: zqq Date: Tue, 11 Oct 2022 15:39:59 +0800 Subject: [PATCH 04/19] trigger timeout async Signed-off-by: zqq --- .../syncer/src/sync_task_context.cpp | 23 +++++++++++++++---- ...buteddb_single_ver_p2p_sync_check_test.cpp | 2 ++ .../common/syncer/virtual_communicator.cpp | 7 +++++- .../common/syncer/virtual_communicator.h | 4 ++++ .../virtual_communicator_aggregator.cpp | 10 +++++++- .../syncer/virtual_communicator_aggregator.h | 4 +++- 6 files changed, 42 insertions(+), 8 deletions(-) diff --git a/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp b/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp index 01481034db3..905e894dca0 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp @@ -536,15 +536,28 @@ void SyncTaskContext::CommErrHandlerFuncInner(int errCode, uint32_t sessionId) int SyncTaskContext::TimeOut(TimerId id) { - int errCode = E_OK; if (!timeOutCallback_) { - return errCode; + return E_OK; } - if (IncUsedCount() == E_OK) { - errCode = timeOutCallback_(id); + int errCode = IncUsedCount(); + if (errCode != E_OK) { + LOGW("[SyncTaskContext][TimeOut] IncUsedCount failed! errCode=", errCode); + // if return is not E_OK, the timer will be removed + // we removed timer when context call StopTimer + return E_OK; + } + IncObjRef(this); + errCode = RuntimeContext::GetInstance()->ScheduleTask([this, id]() { + timeOutCallback_(id); + SafeExit(); + DecObjRef(this); + }); + if (errCode != E_OK) { + LOGW("[SyncTaskContext][Timeout] Trigger Timeout Async Failed! TimerId=" PRIu64 " errCode=%d", id, errCode); SafeExit(); + DecObjRef(this); } - return errCode; + return E_OK; } void SyncTaskContext::CopyTargetData(const ISyncTarget *target, const TaskParam &taskParam) diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp index e5ef87fbea8..6540b15349f 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp @@ -43,6 +43,7 @@ namespace { const int WAIT_10_SECONDS = 10000; const int WAIT_30_SECONDS = 30000; const int WAIT_40_SECONDS = 40000; + const int TIMEOUT_6_SECONDS = 6000; KvStoreDelegateManager g_mgr(APP_ID, USER_ID); KvStoreConfig g_config; @@ -1334,6 +1335,7 @@ HWTEST_F(DistributedDBSingleVerP2PSyncCheckTest, GetDataNotify001, TestSize.Leve * @tc.steps: step1. deviceB set get data delay 40s */ g_deviceB->DelayGetSyncData(WAIT_40_SECONDS); + g_communicatorAggregator->SetTimeout(DEVICE_A, TIMEOUT_6_SECONDS); /** * @tc.steps: step2. deviceA call sync and wait diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.cpp index 0f8bfe3d7c4..e78ab129ede 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.cpp @@ -124,7 +124,7 @@ uint32_t VirtualCommunicator::GetCommunicatorMtuSize(const std::string &target) uint32_t VirtualCommunicator::GetTimeout() const { - return 5 * 1000; // 5 * 1000ms + return timeout_; } uint32_t VirtualCommunicator::GetTimeout(const std::string &target) const @@ -132,6 +132,11 @@ uint32_t VirtualCommunicator::GetTimeout(const std::string &target) const return GetTimeout(); } +void VirtualCommunicator::SetTimeout(uint32_t timeout) +{ + timeout_ = timeout; +} + int VirtualCommunicator::GetLocalIdentity(std::string &outTarget) const { outTarget = deviceId_; diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.h index 7cde89e4981..f202d1dad60 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.h @@ -49,6 +49,8 @@ public: uint32_t GetTimeout() const override; uint32_t GetTimeout(const std::string &target) const override; + void SetTimeout(uint32_t timeout); + int GetLocalIdentity(std::string &outTarget) const override; int SendMessage(const std::string &dstTarget, const Message *inMsg, const SendConfig &config) override; @@ -97,6 +99,8 @@ private: std::mutex onAggregatorLock_; VirtualCommunicatorAggregator *communicatorAggregator_; + + uint32_t timeout_ = 5 * 1000; // 5 * 1000ms }; } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp index 21313365810..7e40b97c0bd 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp @@ -143,7 +143,7 @@ ICommunicator *VirtualCommunicatorAggregator::AllocCommunicator(const std::strin } { std::lock_guard lock(communicatorsLock_); - communicators_.insert(std::pair(deviceId, communicator)); + communicators_.insert(std::pair(deviceId, communicator)); } OnlineDevice(deviceId); return communicator; @@ -250,4 +250,12 @@ void VirtualCommunicatorAggregator::SetCurrentUserId(const std::string &userId) { userId_ = userId; } + +void VirtualCommunicatorAggregator::SetTimeout(const std::string &deviceId, uint32_t timeout) +{ + std::lock_guard lock(communicatorsLock_); + if (communicators_.find(deviceId) != communicators_.end()) { + communicators_[deviceId]->SetTimeout(timeout); + } +} } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.h index 22db8b586c7..c1848a2cac9 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.h @@ -70,6 +70,8 @@ public: void SetCurrentUserId(const std::string &userId); + void SetTimeout(const std::string &deviceId, uint32_t timeout); + ~VirtualCommunicatorAggregator() {}; VirtualCommunicatorAggregator() {}; @@ -77,7 +79,7 @@ private: void CallSendEnd(int errCode, const OnSendEnd &onEnd); mutable std::mutex communicatorsLock_; - std::map communicators_; + std::map communicators_; std::string remoteDeviceId_ = "real_device"; std::mutex blockLock_; std::condition_variable conditionVar_; -- Gitee From 28e498730418f67e0ef3197436b23c32f7e3a4bd Mon Sep 17 00:00:00 2001 From: lianhuix Date: Wed, 12 Oct 2022 16:20:29 +0800 Subject: [PATCH 05/19] Fix mem leak Signed-off-by: lianhuix --- .../distributeddb/syncer/src/sync_engine.cpp | 4 ++++ ...istributeddb_relational_encrypted_db_test.cpp | 16 +++++++++++++--- .../common/syncer/virtual_communicator.cpp | 6 ++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp b/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp index d4685183809..962a6132868 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp @@ -508,6 +508,7 @@ int SyncEngine::MessageReciveCallbackInner(const std::string &targetDev, Message DecExecTaskCount(); return -E_BUSY; } + RefObject::DecObjRef(executor); int msgSize = 0; if (!IsSkipCalculateLen(inMsg)) { msgSize = GetMsgSize(inMsg); @@ -1039,6 +1040,7 @@ int SyncEngine::RemoteQuery(const std::string &device, const RemoteCondition &co { RemoteExecutor *executor = GetAndIncRemoteExector(); if (!isActive_ || executor == nullptr) { + RefObject::DecObjRef(executor); return -E_BUSY; // db is closing just return } int errCode = executor->RemoteQuery(device, condition, timeout, connectionId, result); @@ -1050,6 +1052,7 @@ void SyncEngine::NotifyConnectionClosed(uint64_t connectionId) { RemoteExecutor *executor = GetAndIncRemoteExector(); if (!isActive_ || executor == nullptr) { + RefObject::DecObjRef(executor); return; // db is closing just return } executor->NotifyConnectionClosed(connectionId); @@ -1060,6 +1063,7 @@ void SyncEngine::NotifyUserChange() { RemoteExecutor *executor = GetAndIncRemoteExector(); if (!isActive_ || executor == nullptr) { + RefObject::DecObjRef(executor); return; // db is closing just return } executor->NotifyUserChange(); diff --git a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_encrypted_db_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_encrypted_db_test.cpp index 9f20c2eeb6b..8ce15738002 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_encrypted_db_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_encrypted_db_test.cpp @@ -86,6 +86,16 @@ void ExecSqlAndAssertOK(sqlite3 *db, const std::string &sql) { ASSERT_EQ(sqlite3_exec(db, sql.c_str(), nullptr, nullptr, nullptr), SQLITE_OK); } + +void FreeEntires(std::vector &entries) +{ + for (auto *&it : entries) { + if (it != nullptr) { + delete it; + it = nullptr; + } + } +} } class DistributedDBRelationalEncryptedDbTest : public testing::Test { @@ -254,7 +264,7 @@ HWTEST_F(DistributedDBRelationalEncryptedDbTest, OpenEncryptedDBWithPasswdInSpli std::vector entries; EXPECT_EQ(store->GetSyncData(query, SyncTimeRange {}, DataSizeSpecInfo {}, token, entries), E_OK); EXPECT_EQ(entries.size(), 3u); - + FreeEntires(entries); sqlite3_close(db); RefObject::DecObjRef(g_store); } @@ -356,7 +366,7 @@ HWTEST_F(DistributedDBRelationalEncryptedDbTest, OpenEncryptedDBWithCustomizedIt std::vector entries; EXPECT_EQ(store->GetSyncData(query, SyncTimeRange {}, DataSizeSpecInfo {}, token, entries), E_OK); EXPECT_EQ(entries.size(), 3u); - + FreeEntires(entries); sqlite3_close(db); RefObject::DecObjRef(g_store); } @@ -432,7 +442,7 @@ HWTEST_F(DistributedDBRelationalEncryptedDbTest, RekeyAfterOpenStore_001, TestSi store = GetRelationalStore(); ASSERT_NE(store, nullptr); EXPECT_EQ(store->GetSyncData(query, SyncTimeRange {}, DataSizeSpecInfo {}, token, entries), E_OK); - + FreeEntires(entries); sqlite3_close(db); RefObject::DecObjRef(g_store); } diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.cpp index e78ab129ede..d6a625f42b5 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.cpp @@ -207,10 +207,8 @@ int VirtualCommunicator::TranslateMsg(const Message *inMsg, Message *&outMsg) } outMsg = ProtocolProto::ToMessage(buffer, errCode); - if (errCode != E_OK) { - delete buffer; - buffer = nullptr; - } + delete buffer; + buffer = nullptr; return errCode; } } // namespace DistributedDB \ No newline at end of file -- Gitee From 3cc59e5f4250a6d36f8b8dfa7b3f557ee4ec05a5 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Tue, 11 Oct 2022 15:19:48 +0800 Subject: [PATCH 06/19] Fix subscribe return code Fix some codecheck Signed-off-by: lianhuix --- .../libs/distributeddb/common/src/flatbuffer_schema.cpp | 6 +++--- .../communicator/src/communicator_aggregator.cpp | 2 ++ .../storage/src/generic_single_ver_kv_entry.cpp | 4 ++-- .../distributeddb/storage/src/generic_single_ver_kv_entry.h | 4 ++-- .../storage/src/relational_sync_able_storage.cpp | 2 +- .../storage/src/sqlite/sqlite_single_ver_natural_store.cpp | 2 +- .../sqlite/sqlite_single_ver_storage_executor_subscribe.cpp | 2 +- frameworks/libs/distributeddb/syncer/include/isyncer.h | 3 ++- 8 files changed, 14 insertions(+), 11 deletions(-) diff --git a/frameworks/libs/distributeddb/common/src/flatbuffer_schema.cpp b/frameworks/libs/distributeddb/common/src/flatbuffer_schema.cpp index 7f4ba262ad7..31e6de63066 100644 --- a/frameworks/libs/distributeddb/common/src/flatbuffer_schema.cpp +++ b/frameworks/libs/distributeddb/common/src/flatbuffer_schema.cpp @@ -763,12 +763,12 @@ int CompareFieldCount(bool isRoot, uint32_t selfCount, uint32_t otherCount) { if (isRoot) { if (otherCount < selfCount) { - LOGW("[FBSchema][CompareRoot] RootFieldSize: %" PRu32 " vs %" PRu32, selfCount, otherCount); + LOGW("[FBSchema][CompareRoot] RootFieldSize: %" PRIu32 " vs %" PRIu32, selfCount, otherCount); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } } else { if (selfCount != otherCount) { - LOGW("[FBSchema][CompareRoot] StructFieldSize: %" PRu32 " vs %" PRu32, selfCount, otherCount); + LOGW("[FBSchema][CompareRoot] StructFieldSize: %" PRIu32 " vs %" PRIu32, selfCount, otherCount); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } } @@ -821,7 +821,7 @@ int CompareFieldInfo(const reflection::Field &selfField, const reflection::Field auto selfElementType = selfType->element(); auto otherElementType = otherType->element(); if (selfElementType != otherElementType) { - LOGE("[FBSchema][CompareField] ElementType diff:%" PRu32 " vs %" PRu32, selfElementType, otherElementType); + LOGE("[FBSchema][CompareField] ElementType diff:%" PRIu32 " vs %" PRIu32, selfElementType, otherElementType); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } } diff --git a/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp b/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp index c3b3df30e90..7c9151c9fc2 100644 --- a/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp +++ b/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp @@ -15,6 +15,8 @@ #include "communicator_aggregator.h" +#include + #include "hash.h" #include "communicator.h" #include "communicator_linker.h" diff --git a/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp b/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp index a8be5246de7..48ec008934e 100644 --- a/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp +++ b/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp @@ -243,14 +243,14 @@ int GenericSingleVerKvEntry::AdaptToVersion(OperType operType, uint32_t targetVe return errCode; } -int GenericSingleVerKvEntry::AdaptToVersion(OperType operType, uint32_t targetVersion, uint64_t &datalen) +int GenericSingleVerKvEntry::AdaptToVersion(OperType operType, uint32_t targetVersion, uint64_t &dataLen) { if (targetVersion < SOFTWARE_VERSION_EARLIEST || targetVersion > SOFTWARE_VERSION_CURRENT) { return -E_VERSION_NOT_SUPPORT; } if (operType == OperType::CAL_LEN) { - return CalLenByVersion(targetVersion, datalen); + return CalLenByVersion(targetVersion, dataLen); } else { LOGE("Unknown upgrade serialize oper!"); return -E_UPGRADE_FAILED; diff --git a/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h b/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h index 8f6a430455c..f4fcd95e86e 100644 --- a/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h +++ b/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h @@ -89,8 +89,8 @@ private: DESERIALIZE, CAL_LEN, }; - int AdaptToVersion(OperType operType, uint32_t targetVersion, Parcel &parcel, uint64_t &datalen); - int AdaptToVersion(OperType operType, uint32_t targetVersion, uint64_t &datalen); + int AdaptToVersion(OperType operType, uint32_t targetVersion, Parcel &parcel, uint64_t &dataLen); + int AdaptToVersion(OperType operType, uint32_t targetVersion, uint64_t &dataLen); int SerializeDataByVersion(uint32_t targetVersion, Parcel &parcel) const; int SerializeDataByFirstVersion(Parcel &parcel) const; diff --git a/frameworks/libs/distributeddb/storage/src/relational_sync_able_storage.cpp b/frameworks/libs/distributeddb/storage/src/relational_sync_able_storage.cpp index 336669362eb..51e753f2e5f 100644 --- a/frameworks/libs/distributeddb/storage/src/relational_sync_able_storage.cpp +++ b/frameworks/libs/distributeddb/storage/src/relational_sync_able_storage.cpp @@ -660,7 +660,7 @@ int RelationalSyncAbleStorage::CheckAndInitQueryCondition(QueryObject &query) co query.SetSchema(schema); int errCode = E_OK; - auto *handle = GetHandle(false, errCode); + auto *handle = GetHandle(true, errCode); if (handle == nullptr) { return errCode; } diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp index 2e01e97021d..00b3b388f43 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp @@ -2152,7 +2152,7 @@ int SQLiteSingleVerNaturalStore::CheckAndInitQueryCondition(QueryObject &query) query.SetSchema(localSchema); int errCode = E_OK; - SQLiteSingleVerStorageExecutor *handle = GetHandle(false, errCode); + SQLiteSingleVerStorageExecutor *handle = GetHandle(true, errCode); if (handle == nullptr) { return errCode; } diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_subscribe.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_subscribe.cpp index 3ab1bd85d32..061d9b89ffa 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_subscribe.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_subscribe.cpp @@ -150,7 +150,7 @@ int SQLiteSingleVerStorageExecutor::AddSubscribeTrigger(QueryObject &query, cons { if (executorState_ == ExecutorState::CACHEDB || executorState_ == ExecutorState::CACHE_ATTACH_MAIN) { LOGE("Not support add subscribe in cache db."); - return -E_NOT_SUPPORT; + return -E_EKEYREVOKED; } int errCode = E_OK; SqliteQueryHelper helper = query.GetQueryHelper(errCode); diff --git a/frameworks/libs/distributeddb/syncer/include/isyncer.h b/frameworks/libs/distributeddb/syncer/include/isyncer.h index 400ad99fdec..15ea8ae4c76 100644 --- a/frameworks/libs/distributeddb/syncer/include/isyncer.h +++ b/frameworks/libs/distributeddb/syncer/include/isyncer.h @@ -18,8 +18,9 @@ #define I_SYNCER_H #include -#include #include +#include +#include #include "distributeddb/result_set.h" #include "isync_interface.h" -- Gitee From f87f9ea61c8985c50482cbf72c31ec4a4ea4c12a Mon Sep 17 00:00:00 2001 From: wbq_sky Date: Sun, 9 Oct 2022 14:40:40 +0800 Subject: [PATCH 07/19] fix the review issues. Signed-off-by: wbq_sky Change-Id: If11c691b64212f9cfd26460a2bcc4d12dd3e748c --- frameworks/libs/distributeddb/common/include/db_errno.h | 2 -- .../storage/src/sqlite/sqlite_storage_engine.h | 8 ++++---- .../libs/distributeddb/storage/src/storage_engine.cpp | 8 ++++---- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/frameworks/libs/distributeddb/common/include/db_errno.h b/frameworks/libs/distributeddb/common/include/db_errno.h index 6df071c3d5b..13a1098cc42 100644 --- a/frameworks/libs/distributeddb/common/include/db_errno.h +++ b/frameworks/libs/distributeddb/common/include/db_errno.h @@ -16,8 +16,6 @@ #ifndef DISTRIBUTEDDB_ERRNO_H #define DISTRIBUTEDDB_ERRNO_H -#include - namespace DistributedDB { constexpr int E_OK = 0; constexpr int E_BASE = 1000; // different from the other errno. diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.h index 068448da680..bed31646634 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.h @@ -13,14 +13,14 @@ * limitations under the License. */ -#ifndef SQLITE_KVDB_HANDLE_POOL_H -#define SQLITE_KVDB_HANDLE_POOL_H +#ifndef SQLITE_STORAGE_ENGINE_H +#define SQLITE_STORAGE_ENGINE_H #include #include "macro_utils.h" -#include "storage_engine.h" #include "sqlite_utils.h" +#include "storage_engine.h" namespace DistributedDB { class SQLiteStorageEngine : public StorageEngine { @@ -71,4 +71,4 @@ protected: OpenDbProperties option_; }; } // namespace DistributedDB -#endif // SQLITE_DB_HANDLE_H +#endif // SQLITE_STORAGE_ENGINE_H diff --git a/frameworks/libs/distributeddb/storage/src/storage_engine.cpp b/frameworks/libs/distributeddb/storage/src/storage_engine.cpp index 73727a8d9b4..5b316648017 100644 --- a/frameworks/libs/distributeddb/storage/src/storage_engine.cpp +++ b/frameworks/libs/distributeddb/storage/src/storage_engine.cpp @@ -55,7 +55,7 @@ int StorageEngine::Init() if (engineAttr_.minReadNum == 0 && engineAttr_.minWriteNum == 0) { errCode = CreateNewExecutor(true, handle); if (errCode != E_OK) { - goto ERROR; + goto END; } if (handle != nullptr) { @@ -68,7 +68,7 @@ int StorageEngine::Init() handle = nullptr; errCode = CreateNewExecutor(true, handle); if (errCode != E_OK) { - goto ERROR; + goto END; } AddStorageExecutor(handle); } @@ -77,13 +77,13 @@ int StorageEngine::Init() handle = nullptr; errCode = CreateNewExecutor(false, handle); if (errCode != E_OK) { - goto ERROR; + goto END; } AddStorageExecutor(handle); } isInitialized_ = true; -ERROR: +END: if (errCode != E_OK) { // Assumed file system has classification function, can only get one write handle if (errCode == -E_EKEYREVOKED && !writeIdleList_.empty()) { -- Gitee From a5875653396da4a95f57d445ba672cf90c66a425 Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Thu, 13 Oct 2022 15:55:13 +0800 Subject: [PATCH 08/19] fix store destructor problem Signed-off-by: zwtmichael --- .../syncer/src/single_ver_kv_syncer.cpp | 16 ++-- .../distributeddb_mock_sync_module_test.cpp | 78 +++++++++++++++++++ 2 files changed, 86 insertions(+), 8 deletions(-) diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_kv_syncer.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_kv_syncer.cpp index c9abb08a4d7..8164e1ccb0b 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_kv_syncer.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_kv_syncer.cpp @@ -83,19 +83,19 @@ void SingleVerKVSyncer::LocalDataChanged(int notifyEvent) return; } triggerSyncTask_ = false; + std::vector devices; + GetOnlineDevices(devices); + if (devices.empty()) { + LOGI("[Syncer] LocalDataChanged no online devices, Label=%s", label_.c_str()); + triggerSyncTask_ = true; + return; + } RefObject::IncObjRef(syncEngine_); // To avoid many task were produced and waiting in the queue. For example, put value in a loop. // It will consume thread pool resources, so other task will delay until these task finish. // In extreme situation, 10 thread run the localDataChanged task and 1 task waiting in queue. - int errCode = RuntimeContext::GetInstance()->ScheduleTask([this] { + int errCode = RuntimeContext::GetInstance()->ScheduleTask([this, devices] { triggerSyncTask_ = true; - std::vector devices; - GetOnlineDevices(devices); - if (devices.empty()) { - LOGI("[Syncer] LocalDataChanged no online devices, Label=%s", label_.c_str()); - RefObject::DecObjRef(syncEngine_); - return; - } if (!TryFullSync(devices)) { TriggerSubQuerySync(devices); } 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 c702113a5ba..ab5f8419987 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 @@ -45,6 +45,50 @@ using namespace testing; using namespace DistributedDB; using namespace DistributedDBUnitTest; +class TestKvDb { +public: + ~TestKvDb() + { + LOGI("~TestKvDb"); + } + void Initialize(ISyncInterface *syncInterface) + { + syncer_.Initialize(syncInterface, true); + syncer_.EnableAutoSync(true); + } + void LocalChange() + { + syncer_.LocalDataChanged(SQLITE_GENERAL_NS_PUT_EVENT); + } + void Close() + { + syncer_.Close(true); + } +private: + SyncerProxy syncer_; +}; + +class TestInterface: public TestKvDb, public VirtualSingleVerSyncDBInterface { +public: + TestInterface() {} + ~TestInterface() + { + TestKvDb::Close(); + } + void Initialize() + { + TestKvDb::Initialize(this); + } + void TestLocalChange() + { + TestKvDb::LocalChange(); + } + void TestSetIdentifier(std::vector &identifier) + { + VirtualSingleVerSyncDBInterface::SetIdentifier(identifier); + } +}; + namespace { const uint32_t MESSAGE_COUNT = 10u; const uint32_t EXECUTE_COUNT = 2u; @@ -767,6 +811,40 @@ HWTEST_F(DistributedDBMockSyncModuleTest, SyncLifeTest001, TestSize.Level3) delete syncDBInterface; } +/** + * @tc.name: SyncLifeTest003 + * @tc.desc: Test syncer localdatachange when store is destructor + * @tc.type: FUNC + * @tc.require: AR000CCPOM + * @tc.author: zhangqiquan + */ +HWTEST_F(DistributedDBMockSyncModuleTest, SyncLifeTest003, TestSize.Level3) +{ + VirtualCommunicatorAggregator *virtualCommunicatorAggregator = new VirtualCommunicatorAggregator(); + RuntimeContext::GetInstance()->SetCommunicatorAggregator(virtualCommunicatorAggregator); + TestInterface *syncDBInterface = new TestInterface(); + const std::string DEVICE_B = "deviceB"; + std::string userId = "userId_0"; + std::string storeId = "storeId_0"; + std::string appId = "appId_0"; + std::string identifier = KvStoreDelegateManager::GetKvStoreIdentifier(userId, appId, storeId); + std::vector identifierVec(identifier.begin(), identifier.end()); + syncDBInterface->TestSetIdentifier(identifierVec); + syncDBInterface->Initialize(); + virtualCommunicatorAggregator->OnlineDevice(DEVICE_B); + std::thread WriteThread([&syncDBInterface] { + syncDBInterface->TestLocalChange(); + }); + std::thread deleteThread([&syncDBInterface] { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + delete syncDBInterface; + }); + deleteThread.join(); + WriteThread.join(); + std::this_thread::sleep_for(std::chrono::seconds(5)); + RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr); +} + /** * @tc.name: MessageScheduleTest001 * @tc.desc: Test MessageSchedule stop timer when no message. -- Gitee From 730ca72afa3d8d057b76643a3cde30cb63241de2 Mon Sep 17 00:00:00 2001 From: zqq Date: Fri, 14 Oct 2022 16:43:35 +0800 Subject: [PATCH 09/19] sync code Signed-off-by: zqq --- .../syncer/src/isync_state_machine.h | 3 ++ .../src/single_ver_sync_state_machine.cpp | 7 +++ .../src/single_ver_sync_state_machine.h | 2 + .../distributeddb/syncer/src/sync_engine.cpp | 17 ++++--- .../distributeddb/syncer/src/sync_engine.h | 2 + .../syncer/src/sync_state_machine.cpp | 5 ++ .../syncer/src/sync_state_machine.h | 3 ++ .../syncer/src/sync_task_context.cpp | 1 + .../distributeddb/syncer/src/time_sync.cpp | 23 ++++++++- .../libs/distributeddb/syncer/src/time_sync.h | 5 ++ ...distributeddb_single_ver_p2p_sync_test.cpp | 50 +++++++++++++++++++ 11 files changed, 110 insertions(+), 8 deletions(-) diff --git a/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h index a867eee72dd..90f3bc6009c 100644 --- a/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h @@ -63,6 +63,9 @@ public: // check if need trigger query auto sync and get query from inMsg virtual bool IsNeedTriggerQueryAutoSync(Message *inMsg, QuerySyncObject &query) = 0; + // Notify machine is closing, should release some lock + virtual void NotifyClosing() = 0; + // start a timer to ResetWatchDog when get data and send notify ack if need virtual void StartFeedDogForGetData(uint32_t sessionId) = 0; diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp index 3c19347ed3a..7982df70638 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp @@ -1252,4 +1252,11 @@ void SingleVerSyncStateMachine::InnerErrorAbort(uint32_t sessionId) SyncStep(); } } + +void SingleVerSyncStateMachine::NotifyClosing() +{ + if (timeSync_ != nullptr) { + timeSync_->Close(); + } +} } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h index 18654f9caa4..593568ba595 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h @@ -97,6 +97,8 @@ public: uint64_t &outValue); void InnerErrorAbort(uint32_t sessionId) override; + + void NotifyClosing() override; protected: // Step the SingleVerSyncStateMachine void SyncStep() override; diff --git a/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp b/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp index 962a6132868..109f5a3c1f1 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp @@ -113,12 +113,6 @@ int SyncEngine::Close() UnRegCommunicatorsCallback(); StopAutoSubscribeTimer(); - std::unique_lock closeLock(execTaskCountLock_); - bool isTimeout = execTaskCv_.wait_for(closeLock, std::chrono::milliseconds(DBConstant::MIN_TIMEOUT), - [this]() { return execTaskCount_ == 0; }); - if (!isTimeout) { - LOGD("SyncEngine Close with executing task!"); - } // Clear SyncContexts { std::unique_lock lock(contextMapLock_); @@ -133,6 +127,7 @@ int SyncEngine::Close() syncTaskContextMap_.clear(); } + WaitingExecTaskExist(); ReleaseCommunicators(); std::lock_guard msgLock(queueLock_); while (!msgQueue_.empty()) { @@ -1127,4 +1122,14 @@ void SyncEngine::AbortMachineIfNeed(uint32_t syncId) RefObject::DecObjRef(abortContext); } } + +void SyncEngine::WaitingExecTaskExist() +{ + std::unique_lock closeLock(execTaskCountLock_); + bool isTimeout = execTaskCv_.wait_for(closeLock, std::chrono::milliseconds(DBConstant::MIN_TIMEOUT), + [this]() { return execTaskCount_ == 0; }); + if (!isTimeout) { + LOGD("SyncEngine Close with executing task!"); + } +} } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/syncer/src/sync_engine.h b/frameworks/libs/distributeddb/syncer/src/sync_engine.h index a4fd7466477..df2a59cc0c2 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_engine.h +++ b/frameworks/libs/distributeddb/syncer/src/sync_engine.h @@ -206,6 +206,8 @@ private: int GetLocalDeviceId(std::string &deviceId); + void WaitingExecTaskExist(); + ICommunicator *communicator_; DeviceManager *deviceManager_; std::function onRemoteDataChanged_; diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp index 52e8da2e2bf..94af49a5a5e 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp @@ -409,6 +409,11 @@ void SyncStateMachine::InnerErrorAbort(uint32_t sessionId) (void) sessionId; } +void SyncStateMachine::NotifyClosing() +{ + // do nothing +} + void SyncStateMachine::StartFeedDogForGetData(uint32_t sessionId) { std::lock_guard lockGuard(getDataNotifyLock_); diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h index 4b8cdccbd1a..51a6c545f56 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h @@ -77,6 +77,9 @@ public: // start a timer to ResetWatchDog when get data and stop send notify ack if need void StopFeedDogForGetData() override; + + // Notify machine is closing, should release some lock + void NotifyClosing() override; protected: // SyncOperation is timeout, step to timeout state diff --git a/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp b/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp index 905e894dca0..dcbf754068a 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp @@ -604,6 +604,7 @@ void SyncTaskContext::CopyTargetData(const ISyncTarget *target, const TaskParam void SyncTaskContext::KillWait() { StopTimer(); + stateMachine_->NotifyClosing(); UnlockObj(); stateMachine_->AbortImmediately(); LockObj(); diff --git a/frameworks/libs/distributeddb/syncer/src/time_sync.cpp b/frameworks/libs/distributeddb/syncer/src/time_sync.cpp index 9d7f57bbbcf..26cc3b9fd40 100644 --- a/frameworks/libs/distributeddb/syncer/src/time_sync.cpp +++ b/frameworks/libs/distributeddb/syncer/src/time_sync.cpp @@ -117,7 +117,8 @@ TimeSync::TimeSync() isAckReceived_(false), timeChangedListener_(nullptr), timeDriverLockCount_(0), - isOnline_(true) + isOnline_(true), + closed_(false) { } @@ -504,7 +505,7 @@ int TimeSync::GetTimeOffset(TimeOffset &outOffset, uint32_t timeout, uint32_t se TimeHelper::GetSysCurrentTime(), errCode, timeout); std::unique_lock lock(cvLock_); if (errCode != E_OK || !conditionVar_.wait_for(lock, std::chrono::milliseconds(timeout), - [this](){ return this->isAckReceived_ == true; })) { + [this](){ return this->isAckReceived_ || this->closed_; })) { LOGD("TimeSync::GetTimeOffset, retryTime_ = %d", retryTime_); retryTime_++; if (retryTime_ < MAX_RETRY_TIME) { @@ -516,6 +517,9 @@ int TimeSync::GetTimeOffset(TimeOffset &outOffset, uint32_t timeout, uint32_t se return -E_TIMEOUT; } } + if (IsClosed()) { + return -E_BUSY; + } retryTime_ = 0; metadata_->GetTimeOffset(deviceId_, outOffset); return E_OK; @@ -560,4 +564,19 @@ void TimeSync::ResetTimer() LOGW("[TimeSync] Reset TimeSync timer failed err :%d", errCode); } } + +void TimeSync::Close() +{ + { + std::lock_guard lock(cvLock_); + closed_ = true; + } + conditionVar_.notify_all(); +} + +bool TimeSync::IsClosed() +{ + std::lock_guard lock(cvLock_); + return closed_ ; +} } // namespace DistributedDB \ No newline at end of file diff --git a/frameworks/libs/distributeddb/syncer/src/time_sync.h b/frameworks/libs/distributeddb/syncer/src/time_sync.h index 5aaa8a115c3..0b79152cac6 100644 --- a/frameworks/libs/distributeddb/syncer/src/time_sync.h +++ b/frameworks/libs/distributeddb/syncer/src/time_sync.h @@ -87,6 +87,8 @@ public: void SetOnline(bool isOnline); + void Close(); + // Used in send msg, as execution is asynchronous, should use this function to handle result. static void CommErrHandlerFunc(int errCode, TimeSync *timeSync); @@ -107,6 +109,8 @@ private: void ResetTimer(); + bool IsClosed(); + ICommunicator *communicateHandle_; std::shared_ptr metadata_; std::unique_ptr timeHelper_; @@ -123,6 +127,7 @@ private: std::mutex timeDriverLock_; int timeDriverLockCount_; bool isOnline_; + bool closed_; static std::mutex timeSyncSetLock_; static std::set timeSyncSet_; }; diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp index cb1b9de8de0..2c95d024a38 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp @@ -2581,6 +2581,56 @@ HWTEST_F(DistributedDBSingleVerP2PSyncTest, CloseSync001, TestSize.Level3) EXPECT_EQ(g_deviceB->GetData(key, actualValue), -E_NOT_FOUND); } +/** + * @tc.name: CloseSync002 + * @tc.desc: Test 1 delegate close when in time sync + * @tc.type: FUNC + * @tc.require: AR000CCPOM + * @tc.author: zhangqiquan + */ +HWTEST_F(DistributedDBSingleVerP2PSyncTest, CloseSync002, TestSize.Level3) +{ + /** + * @tc.steps: step1. invalid time sync packet from A + */ + g_communicatorAggregator->RegOnDispatch([](const std::string &target, DistributedDB::Message *msg) { + ASSERT_NE(msg, nullptr); + if (target == DEVICE_B && msg->GetMessageId() == TIME_SYNC_MESSAGE && msg->GetMessageType() == TYPE_REQUEST) { + msg->SetMessageId(INVALID_MESSAGE_ID); + LOGD("Message is invalid"); + } + }); + Timestamp currentTime; + (void)OS::GetCurrentSysTimeInMicrosecond(currentTime); + g_deviceB->PutData({'k'}, {'v'}, currentTime, 0); + + /** + * @tc.steps: step2. B PUSH to A and A close after 1s + * @tc.expected: step2. A closing time cost letter than 4s + */ + std::thread closingThread([]() { + std::this_thread::sleep_for(std::chrono::seconds(1)); + LOGD("Begin Close"); + Timestamp beginTime; + (void)OS::GetCurrentSysTimeInMicrosecond(beginTime); + ASSERT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK); + Timestamp endTime; + (void)OS::GetCurrentSysTimeInMicrosecond(endTime); + EXPECT_LE(static_cast(endTime - beginTime), 4 * 1000 * 1000); // waiting 4 * 1000 * 1000 us + LOGD("End Close"); + }); + EXPECT_EQ(g_deviceB->Sync(DistributedDB::SYNC_MODE_PUSH_ONLY, true), E_OK); + closingThread.join(); + + /** + * @tc.steps: step3. remove db + * @tc.expected: step3. remove ok + */ + g_kvDelegatePtr = nullptr; + DBStatus status = g_mgr.DeleteKvStore(STORE_ID); + LOGD("delete kv store status %d", status); + ASSERT_TRUE(status == OK); +} /** * @tc.name: OrderbyWriteTimeSync001 -- Gitee From 450b5b60da680725f4b4fa3f8cf13b3107bd6cf7 Mon Sep 17 00:00:00 2001 From: zqq Date: Fri, 14 Oct 2022 16:48:45 +0800 Subject: [PATCH 10/19] add ut Signed-off-by: zqq --- .../distributeddb_mock_sync_module_test.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) 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 ab5f8419987..d2317c41e7a 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 @@ -420,6 +420,32 @@ HWTEST_F(DistributedDBMockSyncModuleTest, StateMachineCheck011, TestSize.Level1) EXPECT_EQ(syncTaskContext.IsCommNormal(), false); } +/** + * @tc.name: StateMachineCheck012 + * @tc.desc: Verify Ability LastNotify AckReceive callback. + * @tc.type: FUNC + * @tc.require: AR000DR9K4 + * @tc.author: zhangqiquan + */ +HWTEST_F(DistributedDBMockSyncModuleTest, StateMachineCheck012, TestSize.Level1) +{ + MockSingleVerStateMachine stateMachine; + MockSyncTaskContext syncTaskContext; + MockCommunicator communicator; + VirtualSingleVerSyncDBInterface dbSyncInterface; + Init(stateMachine, syncTaskContext, communicator, dbSyncInterface); + EXPECT_CALL(stateMachine, SwitchStateAndStep(_)).WillOnce(Return()); + DistributedDB::Message msg(ABILITY_SYNC_MESSAGE); + 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_EQ(stateMachine.ReceiveMessageCallback(&msg), E_OK); + EXPECT_EQ(syncTaskContext.GetTaskErrCode(), -E_BUSY); +} + /** * @tc.name: StateMachineCheck013 * @tc.desc: test kill syncTaskContext. -- Gitee From b30b52a3d33cf2be8e77685ee3f3c56cc3de4fc6 Mon Sep 17 00:00:00 2001 From: zqq Date: Thu, 13 Oct 2022 10:32:12 +0800 Subject: [PATCH 11/19] avoid use too much thread Signed-off-by: zqq --- .../libs/distributeddb/syncer/src/sync_state_machine.cpp | 9 +++++++++ .../libs/distributeddb/syncer/src/sync_state_machine.h | 1 + 2 files changed, 10 insertions(+) diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp index 94af49a5a5e..02e0600bad1 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp @@ -31,6 +31,7 @@ SyncStateMachine::SyncStateMachine() currentSyncProctolVersion_(SINGLE_VER_SYNC_PROCTOL_V3), saveDataNotifyTimerId_(0), saveDataNotifyCount_(0), + waitingResetLockBySaveData_(false), getDataNotifyTimerId_(0), getDataNotifyCount_(0) { @@ -384,8 +385,16 @@ void SyncStateMachine::DoSaveDataNotify(uint32_t sessionId, uint32_t sequenceId, } SendNotifyPacket(sessionId, sequenceId, inMsgId); saveDataNotifyCount_++; + if (waitingResetLockBySaveData_) { + return; + } + waitingResetLockBySaveData_ = true; } std::lock_guard lock(stateMachineLock_); + { + std::lock_guard innerLock(saveDataNotifyLock_); + waitingResetLockBySaveData_ = false; + } (void)ResetWatchDog(); } diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h index 51a6c545f56..c61796b0097 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h @@ -170,6 +170,7 @@ protected: std::mutex saveDataNotifyLock_; TimerId saveDataNotifyTimerId_; uint8_t saveDataNotifyCount_; + bool waitingResetLockBySaveData_; std::mutex getDataNotifyLock_; TimerId getDataNotifyTimerId_; -- Gitee From 6a5540eaf7f80cd3500fd710045cf400c83eb35e Mon Sep 17 00:00:00 2001 From: zqq Date: Thu, 13 Oct 2022 16:27:52 +0800 Subject: [PATCH 12/19] avoid notify close too early Signed-off-by: zqq --- .../src/single_ver_sync_state_machine.cpp | 19 ++++++++++++------- .../syncer/src/sync_state_machine.cpp | 10 +++++++++- .../syncer/src/sync_state_machine.h | 1 + ...buteddb_single_ver_p2p_sync_check_test.cpp | 7 +++---- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp index 7982df70638..8beb7e76154 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp @@ -660,19 +660,24 @@ int SingleVerSyncStateMachine::HandleDataRequestRecv(const Message *inMsg) if (performance != nullptr) { performance->StepTimeRecordEnd(PT_TEST_RECORDS::RECORD_DATA_REQUEST_RECV_TO_SEND_ACK); } - if (isNeedStop) { - StopSaveDataNotify(); - } // only higher than 102 version receive this errCode here. // while both RequestSessionId is not equal,but get this errCode;slwr would seem to handle first secquencid. // so while receive the same secquencid after abiitysync it wouldn't handle. if (errCode == -E_NEED_ABILITY_SYNC) { + if (isNeedStop) { + StopSaveDataNotify(); + } return errCode; } - std::lock_guard lock(stateMachineLock_); - DataRecvErrCodeHandle(inMsg->GetSessionId(), errCode); - if (pullEndWaterkark > 0) { - AddPullResponseTarget(inMsg, pullEndWaterkark); + { + std::lock_guard lock(stateMachineLock_); + DataRecvErrCodeHandle(inMsg->GetSessionId(), errCode); + if (pullEndWaterkark > 0) { + AddPullResponseTarget(inMsg, pullEndWaterkark); + } + } + if (isNeedStop) { + StopSaveDataNotify(); } return E_OK; } diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp index 02e0600bad1..e308b7f2a3d 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp @@ -32,6 +32,7 @@ SyncStateMachine::SyncStateMachine() saveDataNotifyTimerId_(0), saveDataNotifyCount_(0), waitingResetLockBySaveData_(false), + saveDataNotifyRefCount_(false), getDataNotifyTimerId_(0), getDataNotifyCount_(0) { @@ -224,10 +225,11 @@ void SyncStateMachine::StopWatchDog() bool SyncStateMachine::StartSaveDataNotify(uint32_t sessionId, uint32_t sequenceId, uint32_t inMsgId) { std::lock_guard lockGuard(saveDataNotifyLock_); + saveDataNotifyRefCount_++; if (saveDataNotifyTimerId_ > 0) { saveDataNotifyCount_ = 0; LOGW("[SyncStateMachine][SaveDataNotify] timer has been started!"); - return false; + return true; } // Incref to make sure context still alive before timer stopped. @@ -250,6 +252,7 @@ bool SyncStateMachine::StartSaveDataNotify(uint32_t sessionId, uint32_t sequence saveDataNotifyTimerId_); if (errCode != E_OK) { LOGW("[SyncStateMachine][SaveDataNotify] start timer failed err %d !", errCode); + saveDataNotifyRefCount_--; return false; } return true; @@ -258,6 +261,10 @@ bool SyncStateMachine::StartSaveDataNotify(uint32_t sessionId, uint32_t sequence void SyncStateMachine::StopSaveDataNotify() { std::lock_guard lockGuard(saveDataNotifyLock_); + saveDataNotifyRefCount_--; + if (saveDataNotifyRefCount_ > 0) { + return; + } StopSaveDataNotifyNoLock(); } @@ -270,6 +277,7 @@ void SyncStateMachine::StopSaveDataNotifyNoLock() RuntimeContext::GetInstance()->RemoveTimer(saveDataNotifyTimerId_); saveDataNotifyTimerId_ = 0; saveDataNotifyCount_ = 0; + saveDataNotifyRefCount_ = 0; } bool SyncStateMachine::StartFeedDogForSync(uint32_t time, SyncDirectionFlag flag) diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h index c61796b0097..a2df43c567d 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h @@ -171,6 +171,7 @@ protected: TimerId saveDataNotifyTimerId_; uint8_t saveDataNotifyCount_; bool waitingResetLockBySaveData_; + uint32_t saveDataNotifyRefCount_; std::mutex getDataNotifyLock_; TimerId getDataNotifyTimerId_; diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp index 6540b15349f..35a3af9eb72 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp @@ -40,7 +40,6 @@ namespace { const int SLEEP_MILLISECONDS = 500; const int TEN_SECONDS = 10; const int THREE_HUNDRED = 300; - const int WAIT_10_SECONDS = 10000; const int WAIT_30_SECONDS = 30000; const int WAIT_40_SECONDS = 40000; const int TIMEOUT_6_SECONDS = 6000; @@ -1401,9 +1400,9 @@ HWTEST_F(DistributedDBSingleVerP2PSyncCheckTest, GetDataNotify002, TestSize.Leve EXPECT_EQ(result.size(), devices.size()); EXPECT_EQ(result[DEVICE_B], OK); /** - * @tc.steps: step2. deviceB set get data delay 10s + * @tc.steps: step2. deviceB set get data delay 30s */ - g_deviceB->DelayGetSyncData(WAIT_10_SECONDS); + g_deviceB->DelayGetSyncData(WAIT_30_SECONDS); /** * @tc.steps: step3. deviceB call sync and wait @@ -1418,7 +1417,7 @@ HWTEST_F(DistributedDBSingleVerP2PSyncCheckTest, GetDataNotify002, TestSize.Leve /** * @tc.steps: step4. deviceA call sync and wait - * @tc.expected: step4. sync should return OK. because notify timer trigger (10s - 1s)/2s => 4times + * @tc.expected: step4. sync should return OK. because notify timer trigger (30s - 1s)/2s => 15times */ std::this_thread::sleep_for(std::chrono::seconds(1)); status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PUSH_ONLY, result, true); -- Gitee From 847db0c2bc2ae35165a7c62e56ed1a64c55a3ad6 Mon Sep 17 00:00:00 2001 From: saikit Date: Sat, 15 Oct 2022 14:25:32 +0800 Subject: [PATCH 13/19] add option check for open rdb Signed-off-by: saikit --- .../storage/src/relational_store_instance.cpp | 6 ++++++ ...stributeddb_relational_multi_user_test.cpp | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/frameworks/libs/distributeddb/storage/src/relational_store_instance.cpp b/frameworks/libs/distributeddb/storage/src/relational_store_instance.cpp index fd269ecebe1..9ebcd1d5b41 100644 --- a/frameworks/libs/distributeddb/storage/src/relational_store_instance.cpp +++ b/frameworks/libs/distributeddb/storage/src/relational_store_instance.cpp @@ -185,6 +185,12 @@ int CheckCompatibility(const RelationalDBProperties &prop, const RelationalDBPro LOGE("Failed to check cipher args."); return -E_INVALID_PASSWD_OR_CORRUPTED_DB; } + + if (prop.GetBoolProp(DBProperties::SYNC_DUAL_TUPLE_MODE, false) != + existedProp.GetBoolProp(DBProperties::SYNC_DUAL_TUPLE_MODE, false)) { + LOGE("Failed to check dual tuple sync mode for rdb"); + return -E_MODE_MISMATCH; + } return E_OK; } } diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp index c15b38ba390..45323a5ec3d 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp @@ -887,4 +887,25 @@ HWTEST_F(DistributedDBRelationalMultiUserTest, RdbMultiUser010, TestSize.Level1) Query query = Query::Select(g_tableName); EXPECT_EQ(g_rdbDelegatePtr1->Sync({DEVICE_B}, SYNC_MODE_PUSH_ONLY, query, nullptr, true), OK); CloseStore(); +} + +/** + * @tc.name: multi user 011 + * @tc.desc: test use different option to open store for rdb + * @tc.type: FUNC + * @tc.require: AR000GK58G + * @tc.author: zhangshijie + */ +HWTEST_F(DistributedDBRelationalMultiUserTest, RdbMultiUser011, TestSize.Level1) +{ + for (int i = 0; i < 2; i++) { + bool syncDualTupleMode = i / 2; + OpenStore1(syncDualTupleMode); + RelationalStoreDelegate::Option option = { g_observer }; + option.syncDualTupleMode = !syncDualTupleMode; + RelationalStoreDelegate *rdbDeletegatePtr = nullptr; + EXPECT_EQ(g_mgr1.OpenStore(g_storePath1, STORE_ID_1, option, rdbDeletegatePtr), MODE_MISMATCH); + EXPECT_EQ(rdbDeletegatePtr, nullptr); + CloseStore(); + } } \ No newline at end of file -- Gitee From aa40f5ffa598078a6c0d90fc8141a6916a872c57 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Mon, 17 Oct 2022 18:49:39 +0800 Subject: [PATCH 14/19] Fix UT issues Signed-off-by: lianhuix --- .../interfaces/distributeddb_interfaces_relational_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp index c9284cb0f15..bf589278326 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp @@ -316,7 +316,6 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest005, TestSize ASSERT_NE(db, nullptr); EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK); EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK); - EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); /** * @tc.steps:step2. Open store @@ -344,6 +343,7 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest005, TestSize */ status = g_mgr.CloseStore(delegate); EXPECT_EQ(status, OK); + EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); } /** -- Gitee From 38f79699b30c6f7cce02e318cc01a5ca181d0dcf Mon Sep 17 00:00:00 2001 From: zqq Date: Thu, 20 Oct 2022 10:04:04 +0800 Subject: [PATCH 15/19] avoid save data notify ref count overflowing Signed-off-by: zqq --- .../syncer/src/sync_state_machine.cpp | 10 +++++----- .../distributeddb/syncer/src/sync_state_machine.h | 2 +- .../syncer/distributeddb_mock_sync_module_test.cpp | 14 ++++++++++++++ .../common/syncer/mock_single_ver_state_machine.h | 5 +++++ 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp index e308b7f2a3d..666332660ca 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp @@ -32,7 +32,7 @@ SyncStateMachine::SyncStateMachine() saveDataNotifyTimerId_(0), saveDataNotifyCount_(0), waitingResetLockBySaveData_(false), - saveDataNotifyRefCount_(false), + saveDataNotifyRefCount_(0), getDataNotifyTimerId_(0), getDataNotifyCount_(0) { @@ -261,10 +261,6 @@ bool SyncStateMachine::StartSaveDataNotify(uint32_t sessionId, uint32_t sequence void SyncStateMachine::StopSaveDataNotify() { std::lock_guard lockGuard(saveDataNotifyLock_); - saveDataNotifyRefCount_--; - if (saveDataNotifyRefCount_ > 0) { - return; - } StopSaveDataNotifyNoLock(); } @@ -274,6 +270,10 @@ void SyncStateMachine::StopSaveDataNotifyNoLock() LOGI("[SyncStateMachine][SaveDataNotify] timer is not started!"); return; } + saveDataNotifyRefCount_--; + if (saveDataNotifyRefCount_ > 0) { + return; + } RuntimeContext::GetInstance()->RemoveTimer(saveDataNotifyTimerId_); saveDataNotifyTimerId_ = 0; saveDataNotifyCount_ = 0; diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h index a2df43c567d..477dc13d24a 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h @@ -171,7 +171,7 @@ protected: TimerId saveDataNotifyTimerId_; uint8_t saveDataNotifyCount_; bool waitingResetLockBySaveData_; - uint32_t saveDataNotifyRefCount_; + int32_t saveDataNotifyRefCount_; std::mutex getDataNotifyLock_; TimerId getDataNotifyTimerId_; 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 d2317c41e7a..7d6c97c9aa4 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 @@ -479,6 +479,20 @@ HWTEST_F(DistributedDBMockSyncModuleTest, StateMachineCheck013, TestSize.Level1) tokenPtr = nullptr; } +/** + * @tc.name: StateMachineCheck014 + * @tc.desc: test machine stop save notify without start. + * @tc.type: FUNC + * @tc.require: AR000CCPOM + * @tc.author: zhangqiquan + */ +HWTEST_F(DistributedDBMockSyncModuleTest, StateMachineCheck014, TestSize.Level1) +{ + MockSingleVerStateMachine stateMachine; + stateMachine.CallStopSaveDataNotify(); + EXPECT_EQ(stateMachine.GetSaveDataNotifyRefCount(), 0); +} + /** * @tc.name: DataSyncCheck001 * @tc.desc: Test dataSync recv error ack. diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_state_machine.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_state_machine.h index e48aac1f101..9b7fbb37030 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_state_machine.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_state_machine.h @@ -67,6 +67,11 @@ public: SingleVerSyncStateMachine::ResponsePullError(errCode, ignoreInnerErr); } + int32_t GetSaveDataNotifyRefCount() + { + return saveDataNotifyRefCount_; + } + MOCK_METHOD1(SwitchStateAndStep, void(uint8_t)); MOCK_METHOD0(PrepareNextSyncTask, int(void)); -- Gitee From fdb07e586ca406eb92666c6ff27f86d63560fc3a Mon Sep 17 00:00:00 2001 From: zqq Date: Thu, 20 Oct 2022 14:20:52 +0800 Subject: [PATCH 16/19] add sync interface ref count avoid it release before context Signed-off-by: zqq --- frameworks/libs/distributeddb/syncer/src/sync_engine.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp b/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp index 109f5a3c1f1..221dcbca9c5 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp @@ -629,9 +629,16 @@ ISyncTaskContext *SyncEngine::GetSyncTaskContext(const std::string &deviceId, in syncTaskContextMap_.insert(std::pair(deviceId, context)); // IncRef for SyncEngine to make sure SyncEngine is valid when context access RefObject::IncObjRef(this); - context->OnLastRef([this, deviceId]() { + auto storage = syncInterface_; + if (storage != nullptr) { + storage->IncRefCount(); + } + context->OnLastRef([this, deviceId, storage]() { LOGD("[SyncEngine] SyncTaskContext for id %s finalized", STR_MASK(deviceId)); RefObject::DecObjRef(this); + if (storage != nullptr) { + storage->DecRefCount(); + } }); context->RegOnSyncTask(std::bind(&SyncEngine::ExecSyncTask, this, context)); return context; -- Gitee From 3880226604dfa30eb08b623f996a51cfae3d8b1a Mon Sep 17 00:00:00 2001 From: zqq Date: Thu, 20 Oct 2022 15:57:27 +0800 Subject: [PATCH 17/19] modify ut and check ref count Signed-off-by: zqq --- .../distributeddb_mock_sync_module_test.cpp | 13 +++++++-- .../common/syncer/mock_kv_sync_interface.h | 29 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 frameworks/libs/distributeddb/test/unittest/common/syncer/mock_kv_sync_interface.h 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 7d6c97c9aa4..c770d209730 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 @@ -25,6 +25,7 @@ #include "message.h" #include "mock_auto_launch.h" #include "mock_communicator.h" +#include "mock_kv_sync_interface.h" #include "mock_meta_data.h" #include "mock_remote_executor.h" #include "mock_single_ver_data_sync.h" @@ -919,9 +920,14 @@ HWTEST_F(DistributedDBMockSyncModuleTest, MessageScheduleTest001, TestSize.Level HWTEST_F(DistributedDBMockSyncModuleTest, SyncEngineTest001, TestSize.Level1) { std::unique_ptr enginePtr = std::make_unique(); - EXPECT_CALL(*enginePtr, CreateSyncTaskContext()).WillRepeatedly(Return(nullptr)); + EXPECT_CALL(*enginePtr, CreateSyncTaskContext()) + .WillRepeatedly(Return(new (std::nothrow) SingleVerKvSyncTaskContext())); VirtualCommunicatorAggregator *virtualCommunicatorAggregator = new VirtualCommunicatorAggregator(); - VirtualSingleVerSyncDBInterface syncDBInterface; + MockKvSyncInterface syncDBInterface; + EXPECT_CALL(syncDBInterface, IncRefCount()).WillOnce(Return()); + EXPECT_CALL(syncDBInterface, DecRefCount()).WillRepeatedly(Return()); + std::vector identifier(COMM_LABEL_LENGTH, 1u); + syncDBInterface.SetIdentifier(identifier); std::shared_ptr metaData = std::make_shared(); ASSERT_NE(virtualCommunicatorAggregator, nullptr); RuntimeContext::GetInstance()->SetCommunicatorAggregator(virtualCommunicatorAggregator); @@ -935,10 +941,13 @@ HWTEST_F(DistributedDBMockSyncModuleTest, SyncEngineTest001, TestSize.Level1) } for (int count = 0; count < 100; count++) { // loop 100 times auto *message = new(std::nothrow) DistributedDB::Message(); + message->SetMessageId(LOCAL_DATA_CHANGED); + message->SetErrorNo(E_FEEDBACK_UNKNOWN_MESSAGE); communicator->CallbackOnMessage("src", message); } }); std::thread thread2([&]() { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); enginePtr->Close(); }); thread1.join(); 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 new file mode 100644 index 00000000000..1aacb5351f7 --- /dev/null +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_kv_sync_interface.h @@ -0,0 +1,29 @@ +/* + * 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_KV_SYNC_INTERFACE_H +#define MOCK_KV_SYNC_INTERFACE_H + +#include +#include "virtual_single_ver_sync_db_Interface.h" + +namespace DistributedDB { +class MockKvSyncInterface : public VirtualSingleVerSyncDBInterface { +public: + MOCK_METHOD0(IncRefCount, void(void)); + MOCK_METHOD0(DecRefCount, void(void)); +}; +} // namespace DistributedDB +#endif // #define MOCK_KV_SYNC_INTERFACE_H \ No newline at end of file -- Gitee From e2c795df8d841d4983747cf7cf3d74836b646121 Mon Sep 17 00:00:00 2001 From: saikit Date: Fri, 14 Oct 2022 16:54:59 +0800 Subject: [PATCH 18/19] fix code review issue Signed-off-by: saikit --- .../distributeddb/common/include/data_value.h | 1 - .../common/include/performance_analysis.h | 6 ++++-- .../common/include/platform_specific.h | 4 ++-- .../distributeddb/common/src/auto_launch.cpp | 2 +- .../distributeddb/common/src/data_value.cpp | 9 --------- .../distributeddb/common/src/json_object.cpp | 2 +- .../common/src/performance_analysis.cpp | 20 ++++++++++--------- .../common/src/platform_specific.cpp | 18 ++++++++++------- .../common/src/runtime_context_impl.cpp | 13 ++++++++---- .../storage/include/iconnection.h | 2 +- .../storage/include/storage_engine_manager.h | 2 +- .../storage/src/data_transformer.cpp | 7 +++++-- .../storage/src/default_factory.h | 2 +- .../storage/src/ikvdb_commit_storage.h | 2 +- .../storage/src/kvdb_manager.cpp | 7 ++++++- .../distributeddb/storage/src/kvdb_utils.cpp | 1 - .../distributeddb/storage/src/kvdb_utils.h | 2 +- ...multi_ver_natural_store_commit_storage.cpp | 2 +- .../multi_ver_natural_store_commit_storage.h | 2 +- .../multiver/multi_ver_storage_executor.cpp | 2 +- .../storage/src/operation/database_oper.cpp | 2 +- .../storage/src/package_file.cpp | 3 +-- .../src/relational_store_connection.cpp | 1 - .../storage/src/relational_store_instance.cpp | 3 --- .../storage/src/result_entries_window.cpp | 9 ++++----- ...e_ver_natural_store_commit_notify_data.cpp | 1 - .../storage/src/sqlite/sqlite_local_kvdb.cpp | 8 +------- .../sqlite_single_ver_natural_store.cpp | 6 +----- .../sqlite_single_ver_storage_executor.cpp | 4 ++-- .../sqlite_single_ver_storage_executor.h | 2 +- ...lite_single_ver_storage_executor_cache.cpp | 2 +- .../src/sqlite/sqlite_storage_engine.cpp | 6 +++--- .../storage/src/storage_engine.cpp | 1 - .../storage/src/storage_executor.h | 2 +- .../storage/src/sync_able_kvdb_connection.cpp | 2 +- .../syncer/src/isync_task_context.h | 2 +- .../common/distributeddb_common_test.cpp | 15 ++++++++++++-- ...stributeddb_relational_multi_user_test.cpp | 4 ++++ 38 files changed, 93 insertions(+), 86 deletions(-) diff --git a/frameworks/libs/distributeddb/common/include/data_value.h b/frameworks/libs/distributeddb/common/include/data_value.h index 27085aa7468..25a0ae4b714 100644 --- a/frameworks/libs/distributeddb/common/include/data_value.h +++ b/frameworks/libs/distributeddb/common/include/data_value.h @@ -75,7 +75,6 @@ public: int SetText(const uint8_t *val, uint32_t length); int GetText(std::string &outVal) const; void ResetValue(); - int GetBlobLength(uint32_t &length) const; std::string ToString() const; private: diff --git a/frameworks/libs/distributeddb/common/include/performance_analysis.h b/frameworks/libs/distributeddb/common/include/performance_analysis.h index 1b0da052c1d..51a84dfe921 100644 --- a/frameworks/libs/distributeddb/common/include/performance_analysis.h +++ b/frameworks/libs/distributeddb/common/include/performance_analysis.h @@ -74,6 +74,8 @@ public: static PerformanceAnalysis *GetInstance(int stepNum = 20); + void Initialization(); + void TimeRecordStart(); void TimeRecordEnd(); @@ -88,7 +90,7 @@ public: void ClosePerformanceAnalysis(); - void SetFileNumber(const std::string &FileID); + void SetFileName(const std::string &fileName); private: @@ -117,7 +119,7 @@ private: bool isOpen_; std::ofstream outFile; int fileNumber_; - std::string fileID_; + std::string fileName_; }; } // namespace DistributedDB #endif diff --git a/frameworks/libs/distributeddb/common/include/platform_specific.h b/frameworks/libs/distributeddb/common/include/platform_specific.h index 10d91822f80..6573bcf4402 100644 --- a/frameworks/libs/distributeddb/common/include/platform_specific.h +++ b/frameworks/libs/distributeddb/common/include/platform_specific.h @@ -34,8 +34,8 @@ enum FileType { struct FileAttr { std::string fileName; - FileType fileType; - uint64_t fileLen; + FileType fileType = FileType::FILE; + uint64_t fileLen = 0; }; // Shield the representation method of file handles on different platforms diff --git a/frameworks/libs/distributeddb/common/src/auto_launch.cpp b/frameworks/libs/distributeddb/common/src/auto_launch.cpp index 388840b1256..5e599fde4c1 100644 --- a/frameworks/libs/distributeddb/common/src/auto_launch.cpp +++ b/frameworks/libs/distributeddb/common/src/auto_launch.cpp @@ -435,7 +435,6 @@ int AutoLaunch::DisableKvStoreAutoLaunch(const std::string &normalIdentifier, co void AutoLaunch::GetAutoLaunchSyncDevices(const std::string &identifier, std::vector &devices) const { devices.clear(); - devices.shrink_to_fit(); std::lock_guard autoLock(dataLock_); if (autoLaunchItemMap_.count(identifier) == 0) { LOGD("[AutoLaunch] GetSyncDevices identifier is not exist!"); @@ -444,6 +443,7 @@ void AutoLaunch::GetAutoLaunchSyncDevices(const std::string &identifier, std::ve for (const auto &device : onlineDevices_) { devices.push_back(device); } + devices.shrink_to_fit(); } void AutoLaunch::CloseNotifier(const AutoLaunchItem &autoLaunchItem) diff --git a/frameworks/libs/distributeddb/common/src/data_value.cpp b/frameworks/libs/distributeddb/common/src/data_value.cpp index 8c8fd72916c..d5f7ae772e7 100644 --- a/frameworks/libs/distributeddb/common/src/data_value.cpp +++ b/frameworks/libs/distributeddb/common/src/data_value.cpp @@ -319,15 +319,6 @@ StorageType DataValue::GetType() const return type_; } -int DataValue::GetBlobLength(uint32_t &length) const -{ - if (type_ != StorageType::STORAGE_TYPE_BLOB && type_ != StorageType::STORAGE_TYPE_TEXT) { - return -E_NOT_SUPPORT; - } - length = value_.blobPtr->GetSize(); - return E_OK; -} - void DataValue::ResetValue() { switch (type_) { diff --git a/frameworks/libs/distributeddb/common/src/json_object.cpp b/frameworks/libs/distributeddb/common/src/json_object.cpp index 860f0b3c611..0b4a9a4ef34 100644 --- a/frameworks/libs/distributeddb/common/src/json_object.cpp +++ b/frameworks/libs/distributeddb/common/src/json_object.cpp @@ -327,7 +327,7 @@ int JsonObject::GetSubFieldPath(const FieldPath &inPath, std::set &ou std::vector subFields = valueNode.getMemberNames(); for (const auto &eachSubField : subFields) { FieldPath eachSubPath = inPath; - eachSubPath.push_back(eachSubField); + eachSubPath.emplace_back(eachSubField); outSubPath.insert(eachSubPath); } return E_OK; diff --git a/frameworks/libs/distributeddb/common/src/performance_analysis.cpp b/frameworks/libs/distributeddb/common/src/performance_analysis.cpp index 94207bf3067..75e11259c98 100644 --- a/frameworks/libs/distributeddb/common/src/performance_analysis.cpp +++ b/frameworks/libs/distributeddb/common/src/performance_analysis.cpp @@ -30,16 +30,20 @@ const std::string PerformanceAnalysis::DEFAULT_FILE_NAME = "default00"; PerformanceAnalysis *PerformanceAnalysis::GetInstance(int stepNum) { static PerformanceAnalysis inst(stepNum); + inst.Initialization(); return &inst; } PerformanceAnalysis::PerformanceAnalysis(uint32_t inStepNum) : isOpen_(false) { - if (inStepNum == 0) { - stepNum_ = 0; - } stepNum_ = inStepNum; + fileNumber_ = 0; + fileName_ = std::string(DEFAULT_FILE_NAME) + std::to_string(fileNumber_); +} + +void PerformanceAnalysis::Initialization() +{ counts_.resize(stepNum_); timeRecordData_.timeInfo.resize(stepNum_); stepTimeRecordInfo_.resize(stepNum_); @@ -51,8 +55,6 @@ PerformanceAnalysis::PerformanceAnalysis(uint32_t inStepNum) for (auto iter = counts_.begin(); iter != counts_.end(); ++iter) { *iter = 0; } - fileNumber_ = 0; - fileID_ = std::string(DEFAULT_FILE_NAME) + std::to_string(fileNumber_); } PerformanceAnalysis::~PerformanceAnalysis() {}; @@ -190,7 +192,7 @@ std::string PerformanceAnalysis::GetStatistics() void PerformanceAnalysis::OutStatistics() { - std::string addrStatistics = STATISTICAL_DATA_FILE_NAME_HEADER + fileID_ + CSV_FILE_EXTENSION; + std::string addrStatistics = STATISTICAL_DATA_FILE_NAME_HEADER + fileName_ + CSV_FILE_EXTENSION; outFile.open(addrStatistics, std::ios_base::app); if (!outFile.is_open()) { return; @@ -220,11 +222,11 @@ void PerformanceAnalysis::Clear() iter.min = ULLONG_MAX; iter.average = 0; } - fileID_ = std::string(DEFAULT_FILE_NAME) + std::to_string(fileNumber_); + fileName_ = std::string(DEFAULT_FILE_NAME) + std::to_string(fileNumber_); } -void PerformanceAnalysis::SetFileNumber(const std::string &FileID) +void PerformanceAnalysis::SetFileName(const std::string &fileName) { - fileID_ = FileID; + fileName_ = fileName; } } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/common/src/platform_specific.cpp b/frameworks/libs/distributeddb/common/src/platform_specific.cpp index 16df2d75745..93577c9e66b 100644 --- a/frameworks/libs/distributeddb/common/src/platform_specific.cpp +++ b/frameworks/libs/distributeddb/common/src/platform_specific.cpp @@ -308,6 +308,11 @@ int OpenFile(const std::string &fileName, FileHandle &handle) int CloseFile(FileHandle &handle) { + if (handle.handle == -1) { + LOGI("[CloseFile] file handle is invalid!"); + return E_OK; + } + if (close(handle.handle) != 0) { LOGE("close file failed, errno:%d", errno); return -E_SYSTEM_API_FAIL; @@ -328,12 +333,7 @@ int FileLock(const FileHandle &handle, bool isBlock) int FileUnlock(FileHandle &handle) { - if (handle.handle == -1) { - LOGI("[FileUnlock] file handle is invalid!"); - return E_OK; - } - - return CloseFile(handle); + return E_OK;; } #else namespace { @@ -584,6 +584,10 @@ int OpenFile(const std::string &fileName, FileHandle &handle) int CloseFile(FileHandle &handle) { + if (handle.handle == -1) { + LOGI("[CloseFile] file handle is invalid!"); + return E_OK; + } if (close(handle.handle) != 0) { LOGE("close file failed, errno:%d", errno); return -E_SYSTEM_API_FAIL; @@ -631,7 +635,7 @@ int FileUnlock(FileHandle &handle) LOGE("Unlock file failed. errno:%d", errno); return -E_SYSTEM_API_FAIL; } - return CloseFile(handle); + return E_OK; } #endif } // namespace OS diff --git a/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp b/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp index ed2510b2207..0fba976541a 100644 --- a/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp +++ b/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp @@ -538,11 +538,16 @@ int RuntimeContextImpl::GetSecurityOption(const std::string &filePath, SecurityO bool RuntimeContextImpl::CheckDeviceSecurityAbility(const std::string &devId, const SecurityOption &option) const { - std::lock_guard autoLock(systemApiAdapterLock_); - if (systemApiAdapter_ == nullptr) { - return true; + std::shared_ptr tempSystemApiAdapter = nullptr; + { + std::lock_guard autoLock(systemApiAdapterLock_); + if (systemApiAdapter_ == nullptr) { + return true; + } + tempSystemApiAdapter = systemApiAdapter_; } - return systemApiAdapter_->CheckDeviceSecurityAbility(devId, option); + + return tempSystemApiAdapter->CheckDeviceSecurityAbility(devId, option); } int RuntimeContextImpl::SetProcessSystemApiAdapter(const std::shared_ptr &adapter) diff --git a/frameworks/libs/distributeddb/storage/include/iconnection.h b/frameworks/libs/distributeddb/storage/include/iconnection.h index dbe63aac573..c5ac3d96a2f 100644 --- a/frameworks/libs/distributeddb/storage/include/iconnection.h +++ b/frameworks/libs/distributeddb/storage/include/iconnection.h @@ -37,4 +37,4 @@ protected: }; } // namespace DistributedDB -#endif // I_KV_DB_CONNECTION_H +#endif // I_CONNECTION_H diff --git a/frameworks/libs/distributeddb/storage/include/storage_engine_manager.h b/frameworks/libs/distributeddb/storage/include/storage_engine_manager.h index 717cc0ed436..5c036ac30c1 100644 --- a/frameworks/libs/distributeddb/storage/include/storage_engine_manager.h +++ b/frameworks/libs/distributeddb/storage/include/storage_engine_manager.h @@ -41,7 +41,7 @@ private: StorageEngineManager(); ~StorageEngineManager(); - // Get a StorageEngineManager instance, Singleton mode + // Get a StorageEngineManager instance, Singleton mode static StorageEngineManager *GetInstance(); int RegisterLockStatusListener(); diff --git a/frameworks/libs/distributeddb/storage/src/data_transformer.cpp b/frameworks/libs/distributeddb/storage/src/data_transformer.cpp index 0eac2a73844..264609c8ca5 100644 --- a/frameworks/libs/distributeddb/storage/src/data_transformer.cpp +++ b/frameworks/libs/distributeddb/storage/src/data_transformer.cpp @@ -114,11 +114,14 @@ uint32_t DataTransformer::CalDataValueLength(const DataValue &dataValue) uint32_t length = 0; switch (dataValue.GetType()) { case StorageType::STORAGE_TYPE_BLOB: - case StorageType::STORAGE_TYPE_TEXT: - (void)dataValue.GetBlobLength(length); + case StorageType::STORAGE_TYPE_TEXT: { + Blob blob; + (void)dataValue.GetBlob(blob); + length = blob.GetSize(); length = Parcel::GetEightByteAlign(length); length += Parcel::GetUInt32Len(); // record data length break; + } default: break; } diff --git a/frameworks/libs/distributeddb/storage/src/default_factory.h b/frameworks/libs/distributeddb/storage/src/default_factory.h index 975a00679db..b86dcf0162f 100644 --- a/frameworks/libs/distributeddb/storage/src/default_factory.h +++ b/frameworks/libs/distributeddb/storage/src/default_factory.h @@ -39,7 +39,7 @@ public: IKvDBCommitStorage *CreateMultiVerCommitStorage(int &errCode) override; #endif private: - // Create the a local kv db + // Create a local kv db IKvDB *CreateLocalKvDB(int &errCode); #ifndef OMIT_MULTI_VER diff --git a/frameworks/libs/distributeddb/storage/src/ikvdb_commit_storage.h b/frameworks/libs/distributeddb/storage/src/ikvdb_commit_storage.h index c6e7aeef883..cef320763be 100644 --- a/frameworks/libs/distributeddb/storage/src/ikvdb_commit_storage.h +++ b/frameworks/libs/distributeddb/storage/src/ikvdb_commit_storage.h @@ -57,7 +57,7 @@ public: virtual int ImportDatabase(const Property &property, const std::string &dir, const CipherPassword &passwd) = 0; virtual int StartVacuum() = 0; virtual int CancelVacuum() = 0; - virtual int FinishlVacuum() = 0; + virtual int FinishVacuum() = 0; virtual int GetAllCommitsInTree(std::list &commits) const = 0; }; } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/storage/src/kvdb_manager.cpp b/frameworks/libs/distributeddb/storage/src/kvdb_manager.cpp index 0c4ceb20c09..4c7c46867f0 100644 --- a/frameworks/libs/distributeddb/storage/src/kvdb_manager.cpp +++ b/frameworks/libs/distributeddb/storage/src/kvdb_manager.cpp @@ -242,6 +242,11 @@ int KvDBManager::UnlockDB(const KvDBProperties &kvDBProp) if (errCode != E_OK) { return errCode; } + errCode = OS::CloseFile(locks_[identifierDir]); + LOGI("DB closed! errCode = [%d]", errCode); + if (errCode != E_OK) { + return errCode; + } locks_.erase(identifierDir); return E_OK; } @@ -446,7 +451,7 @@ int KvDBManager::CalculateKvStoreSize(const KvDBProperties &properties, uint64_t totalSize = totalSize + dbSize; } // This represent Db file size(Unit is byte), It is small than max size(max uint64_t represent 2^64B) - if (totalSize != 0ull) { + if (totalSize != 0ULL) { size = totalSize; return E_OK; } diff --git a/frameworks/libs/distributeddb/storage/src/kvdb_utils.cpp b/frameworks/libs/distributeddb/storage/src/kvdb_utils.cpp index 0cedc59e6c0..b947542a22b 100644 --- a/frameworks/libs/distributeddb/storage/src/kvdb_utils.cpp +++ b/frameworks/libs/distributeddb/storage/src/kvdb_utils.cpp @@ -28,7 +28,6 @@ void KvDBUtils::GetStoreDirectory(std::string &directory, const std::string &ide directory += "/"; } directory += identifierName; - return; } int KvDBUtils::RemoveKvDB(const std::string &dirAll, const std::string &dirStoreOnly, const std::string &dbName) diff --git a/frameworks/libs/distributeddb/storage/src/kvdb_utils.h b/frameworks/libs/distributeddb/storage/src/kvdb_utils.h index b55ac8dac22..795a99a6aa5 100644 --- a/frameworks/libs/distributeddb/storage/src/kvdb_utils.h +++ b/frameworks/libs/distributeddb/storage/src/kvdb_utils.h @@ -35,4 +35,4 @@ public: static int RemoveKvDB(const std::string &dirAll, const std::string &dirStoreOnly, const std::string &dbName); }; } // namespace DistributedDB -#endif // LOCAL_KVDB_H \ No newline at end of file +#endif // KVDB_UTILLS_H \ No newline at end of file diff --git a/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.cpp b/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.cpp index 02133d50847..c32c7aa5541 100644 --- a/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.cpp +++ b/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.cpp @@ -237,7 +237,7 @@ int MultiVerNaturalStoreCommitStorage::CancelVacuum() return commitStorageDBConnection_->RollBack(); } -int MultiVerNaturalStoreCommitStorage::FinishlVacuum() +int MultiVerNaturalStoreCommitStorage::FinishVacuum() { if (commitStorageDBConnection_ == nullptr) { LOGE("commitStorage Connection not existed!"); diff --git a/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.h b/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.h index 3a310de4f40..791a1607dec 100644 --- a/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.h +++ b/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.h @@ -74,7 +74,7 @@ public: int CancelVacuum() override; - int FinishlVacuum() override; + int FinishVacuum() override; int GetAllCommitsInTree(std::list &commits) const override; diff --git a/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_storage_executor.cpp b/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_storage_executor.cpp index 14b82f68e8b..46fc2a09a3b 100644 --- a/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_storage_executor.cpp +++ b/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_storage_executor.cpp @@ -826,7 +826,7 @@ int MultiVerStorageExecutor::CommitAllDbTransaction() } // start commit history transaction - errCode = commitStorage_->FinishlVacuum(); + errCode = commitStorage_->FinishVacuum(); if (errCode != E_OK) { LOGE("Finish commitStorage transaction failed:%d", errCode); goto END; diff --git a/frameworks/libs/distributeddb/storage/src/operation/database_oper.cpp b/frameworks/libs/distributeddb/storage/src/operation/database_oper.cpp index 5a12f0a1340..9f877cdced4 100644 --- a/frameworks/libs/distributeddb/storage/src/operation/database_oper.cpp +++ b/frameworks/libs/distributeddb/storage/src/operation/database_oper.cpp @@ -366,7 +366,7 @@ int DatabaseOper::RemoveFile(const std::string &fileName) return E_OK; } - if (OS::RemoveFile(fileName.c_str()) != E_OK) { + if (OS::RemoveFile(fileName) != E_OK) { LOGE("Remove file failed:%d", errno); return -E_REMOVE_FILE; } diff --git a/frameworks/libs/distributeddb/storage/src/package_file.cpp b/frameworks/libs/distributeddb/storage/src/package_file.cpp index fb5b6a7e6c7..fe8b33e14d0 100644 --- a/frameworks/libs/distributeddb/storage/src/package_file.cpp +++ b/frameworks/libs/distributeddb/storage/src/package_file.cpp @@ -60,10 +60,9 @@ static void Clear(ofstream &target, string targetFile) if (target.is_open()) { target.close(); } - if (OS::RemoveFile(targetFile.c_str()) != E_OK) { + if (OS::RemoveFile(targetFile) != E_OK) { LOGE("Remove file failed."); } - return; } static int GetChecksum(const string &file, vector &result) diff --git a/frameworks/libs/distributeddb/storage/src/relational_store_connection.cpp b/frameworks/libs/distributeddb/storage/src/relational_store_connection.cpp index 76b7fffcddf..a3c4bb09691 100644 --- a/frameworks/libs/distributeddb/storage/src/relational_store_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/relational_store_connection.cpp @@ -15,7 +15,6 @@ #ifdef RELATIONAL_STORE #include "relational_store_connection.h" #include "db_errno.h" -#include "sqlite_single_ver_relational_storage_executor.h" namespace DistributedDB { RelationalStoreConnection::RelationalStoreConnection() : isExclusive_(false) diff --git a/frameworks/libs/distributeddb/storage/src/relational_store_instance.cpp b/frameworks/libs/distributeddb/storage/src/relational_store_instance.cpp index 9ebcd1d5b41..363b308908d 100644 --- a/frameworks/libs/distributeddb/storage/src/relational_store_instance.cpp +++ b/frameworks/libs/distributeddb/storage/src/relational_store_instance.cpp @@ -15,9 +15,6 @@ #ifdef RELATIONAL_STORE #include "relational_store_instance.h" -#include -#include - #include "db_common.h" #include "db_errno.h" #include "sqlite_relational_store.h" diff --git a/frameworks/libs/distributeddb/storage/src/result_entries_window.cpp b/frameworks/libs/distributeddb/storage/src/result_entries_window.cpp index 5709380850f..3433156cf55 100644 --- a/frameworks/libs/distributeddb/storage/src/result_entries_window.cpp +++ b/frameworks/libs/distributeddb/storage/src/result_entries_window.cpp @@ -68,10 +68,10 @@ int ResultEntriesWindow::GetCurrentPosition() const bool ResultEntriesWindow::MoveToPosition(int position) { - if ((rawCursor_ == nullptr && buffer_.size() == 0) || (position < 0 || position >= totalCount_)) { + if ((rawCursor_ == nullptr && buffer_.empty()) || (position < 0 || position >= totalCount_)) { return false; } - if (buffer_.size() == 0) { + if (buffer_.empty()) { if (SetCursor(0, position) != E_OK) { return false; } @@ -107,13 +107,13 @@ bool ResultEntriesWindow::MoveToPosition(int position) int ResultEntriesWindow::GetEntry(Entry &entry) const { - if (rawCursor_ == nullptr && buffer_.size() == 0) { + if (rawCursor_ == nullptr && buffer_.empty()) { return -E_NOT_INIT; } if (totalCount_ == 0) { return -E_NOT_FOUND; } - if (buffer_.size() == 0) { + if (buffer_.empty()) { int errCode = LoadData(0, currentPosition_); if (errCode != E_OK) { return errCode; @@ -132,7 +132,6 @@ void ResultEntriesWindow::ResetWindow() } begin_ = 0; currentPosition_ = 0; - return; } int ResultEntriesWindow::SetCursor(int begin, int target) diff --git a/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_commit_notify_data.cpp b/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_commit_notify_data.cpp index d4148e27001..db3c984fa49 100644 --- a/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_commit_notify_data.cpp +++ b/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_commit_notify_data.cpp @@ -45,7 +45,6 @@ const std::list SingleVerNaturalStoreCommitNotifyData::GetCom void SingleVerNaturalStoreCommitNotifyData::SetFilterKey(const Key &key) { keyFilter_ = key; - return; } bool SingleVerNaturalStoreCommitNotifyData::IsChangedDataEmpty() const diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_local_kvdb.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_local_kvdb.cpp index c7816daa92a..83036994d93 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_local_kvdb.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_local_kvdb.cpp @@ -167,11 +167,6 @@ int SQLiteLocalKvDB::RunExportLogic(CipherType type, const CipherPassword &passw } errCode = SQLiteUtils::ExportDatabase(db, type, passwd, newDbName); - if (errCode != E_OK) { - goto END; - } - -END: (void)sqlite3_close_v2(db); db = nullptr; return errCode; @@ -393,9 +388,8 @@ int SQLiteLocalKvDB::CheckVersionAndUpgradeIfNeed(const OpenDbProperties &openPr errCode = SQLiteUtils::SetUserVer(openProp, LOCAL_STORE_VERSION_CURRENT); if (errCode != E_OK) { LOGE("[SqlLocalDb][CheckUpgrade] SetUserVer fail, errCode=%d.", errCode); - return errCode; } - return E_OK; + return errCode; } DEFINE_OBJECT_TAG_FACILITIES(SQLiteLocalKvDB) diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp index 00b3b388f43..5366e00b25c 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp @@ -1725,11 +1725,7 @@ int SQLiteSingleVerNaturalStore::RemoveKvDB(const KvDBProperties &properties) if (errCode != E_OK) { return errCode; } - errCode = DBCommon::RemoveAllFilesOfDirectory(storeOnlyDir, true); - if (errCode != E_OK) { - return errCode; - } - return errCode; + return DBCommon::RemoveAllFilesOfDirectory(storeOnlyDir, true); } int SQLiteSingleVerNaturalStore::GetKvDBSize(const KvDBProperties &properties, uint64_t &size) const diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.cpp index 63d10262046..3501c1f4ba8 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.cpp @@ -1103,7 +1103,7 @@ END: } void SQLiteSingleVerStorageExecutor::PutIntoCommittedData(const DataItem &itemPut, const DataItem &itemGet, - const DataOperStatus &status, const Key &hashKey, SingleVerNaturalStoreCommitNotifyData *committedData) + const DataOperStatus &status, SingleVerNaturalStoreCommitNotifyData *committedData) { if (committedData == nullptr) { return; @@ -1363,7 +1363,7 @@ int SQLiteSingleVerStorageExecutor::SaveSyncDataItem(DataItem &dataItem, const D std::string origDev = GetOriginDevName(dataItem, notify.getData.origDev); errCode = SaveSyncDataToDatabase(dataItem, notify.hashKey, origDev, deviceInfo.deviceName, isUpdate); if (errCode == E_OK) { - PutIntoCommittedData(dataItem, notify.getData, notify.dataStatus, notify.hashKey, committedData); + PutIntoCommittedData(dataItem, notify.getData, notify.dataStatus, committedData); maxStamp = std::max(dataItem.timestamp, maxStamp); } else { LOGE("Save sync data to db failed:%d", errCode); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h index 92eafb275e5..64ebe43fca0 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h @@ -263,7 +263,7 @@ private: }; void PutIntoCommittedData(const DataItem &itemPut, const DataItem &itemGet, const DataOperStatus &status, - const Key &hashKey, SingleVerNaturalStoreCommitNotifyData *committedData); + SingleVerNaturalStoreCommitNotifyData *committedData); static int BindSavedSyncData(sqlite3_stmt *statement, const DataItem &dataItem, const Key &hashKey, const SyncDataDevices &devices, bool isUpdate); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_cache.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_cache.cpp index f236a4fdb81..8df1cfc29ab 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_cache.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_cache.cpp @@ -773,7 +773,7 @@ int SQLiteSingleVerStorageExecutor::PutIntoConflictAndCommitForMigrateCache(Data return ResetForMigrateCacheData(); } - PutIntoCommittedData(dataItem, notify.getData, notify.dataStatus, notify.hashKey, notify.committedData); + PutIntoCommittedData(dataItem, notify.getData, notify.dataStatus, notify.committedData); return ResetForMigrateCacheData(); } diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.cpp index b7ed3b1fc43..4d004150816 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.cpp @@ -120,7 +120,7 @@ EngineState SQLiteStorageEngine::GetEngineState() const void SQLiteStorageEngine::SetEngineState(EngineState state) { LOGD("[SQLiteStorageEngine::SetEngineState] Engine State : [%d]", state); - engineState_ = state; // Current usage logically can guarante no concurrency + engineState_ = state; // Current usage logically can guarantee no concurrency } const OpenDbProperties &SQLiteStorageEngine::GetOpenOption() const @@ -171,7 +171,7 @@ int SQLiteStorageEngine::CheckEngineOption(const KvDBProperties &kvDBProp) const securityOpt.securityFlag = kvDBProp.GetSecFlag(); } - int conflictReslovePolicy = kvDBProp.GetIntProp(KvDBProperties::CONFLICT_RESOLVE_POLICY, DEFAULT_LAST_WIN); + int conflictResolvePolicy = kvDBProp.GetIntProp(KvDBProperties::CONFLICT_RESOLVE_POLICY, DEFAULT_LAST_WIN); bool createDirByStoreIdOnly = kvDBProp.GetBoolProp(KvDBProperties::CREATE_DIR_BY_STORE_ID_ONLY, false); if (kvDBProp.GetSchemaConstRef().IsSchemaValid() == option_.schema.empty()) { @@ -194,7 +194,7 @@ int SQLiteStorageEngine::CheckEngineOption(const KvDBProperties &kvDBProp) const if (isMemDb == false && option_.createDirByStoreIdOnly == createDirByStoreIdOnly && option_.securityOpt == securityOpt && - option_.conflictReslovePolicy == conflictReslovePolicy) { + option_.conflictReslovePolicy == conflictResolvePolicy) { return E_OK; } return -E_INVALID_ARGS; diff --git a/frameworks/libs/distributeddb/storage/src/storage_engine.cpp b/frameworks/libs/distributeddb/storage/src/storage_engine.cpp index 5b316648017..afc52c46952 100644 --- a/frameworks/libs/distributeddb/storage/src/storage_engine.cpp +++ b/frameworks/libs/distributeddb/storage/src/storage_engine.cpp @@ -329,7 +329,6 @@ void StorageEngine::SetNotifiedCallback(const std::function lock(notifyMutex_); commitNotifyFunc_ = callback; - return; } void StorageEngine::SetConnectionFlag(bool isExisted) diff --git a/frameworks/libs/distributeddb/storage/src/storage_executor.h b/frameworks/libs/distributeddb/storage/src/storage_executor.h index 9645cef7a61..224ee2b6843 100644 --- a/frameworks/libs/distributeddb/storage/src/storage_executor.h +++ b/frameworks/libs/distributeddb/storage/src/storage_executor.h @@ -23,7 +23,7 @@ enum EngineState { INVALID = -1, // default value, representative database is not generated CACHEDB, ATTACHING, // main db and cache db attach together - MIGRATING, // begine to Migrate data + MIGRATING, // began to Migrate data MAINDB, ENGINE_BUSY, // In order to change handle during the migration process, it is temporarily unavailable }; diff --git a/frameworks/libs/distributeddb/storage/src/sync_able_kvdb_connection.cpp b/frameworks/libs/distributeddb/storage/src/sync_able_kvdb_connection.cpp index ca10da6dba3..d55ccfb2a41 100644 --- a/frameworks/libs/distributeddb/storage/src/sync_able_kvdb_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/sync_able_kvdb_connection.cpp @@ -65,7 +65,7 @@ void SyncAbleKvDBConnection::InitPragmaFunc() {PRAGMA_PERFORMANCE_ANALYSIS_CLOSE, [](void *parameter, int &errCode) { PerformanceAnalysis::GetInstance()->ClosePerformanceAnalysis(); }}, {PRAGMA_PERFORMANCE_ANALYSIS_SET_REPORTFILENAME, [](void *parameter, int &errCode) { - PerformanceAnalysis::GetInstance()->SetFileNumber(*(static_cast(parameter))); }}, + PerformanceAnalysis::GetInstance()->SetFileName(*(static_cast(parameter))); }}, {PRAGMA_GET_QUEUED_SYNC_SIZE, [this](void *parameter, int &errCode) { errCode = GetQueuedSyncSize(static_cast(parameter)); }}, {PRAGMA_SET_QUEUED_SYNC_LIMIT, [this](void *parameter, int &errCode) { diff --git a/frameworks/libs/distributeddb/syncer/src/isync_task_context.h b/frameworks/libs/distributeddb/syncer/src/isync_task_context.h index 3c4146e429e..d14caa4ae98 100644 --- a/frameworks/libs/distributeddb/syncer/src/isync_task_context.h +++ b/frameworks/libs/distributeddb/syncer/src/isync_task_context.h @@ -149,7 +149,7 @@ public: virtual uint32_t GetRemoteSoftwareVersion() const = 0; // Get the remount software version id, when called GetRemoteSoftwareVersion this id will be increase. - // Used to check if the version num is is overdue + // Used to check if the version num is overdue virtual uint64_t GetRemoteSoftwareVersionId() const = 0; // Judge if the communicator is normal diff --git a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_common_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_common_test.cpp index 00b4da37533..873eea992b3 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_common_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_common_test.cpp @@ -121,7 +121,9 @@ HWTEST_F(DistributedDBCommonTest, SameProcessReLockFile, TestSize.Level1) // unlock EXPECT_EQ(OS::FileUnlock(fd), E_OK); - EXPECT_EQ(OS::FileUnlock(fd2), E_OK); // unlock success will close fd + EXPECT_EQ(OS::CloseFile(fd), E_OK); + EXPECT_EQ(OS::FileUnlock(fd2), E_OK); + EXPECT_EQ(OS::CloseFile(fd2), E_OK); } /** @@ -138,7 +140,9 @@ HWTEST_F(DistributedDBCommonTest, SameProcessReUnLockFile, TestSize.Level1) OS::FileHandle fd; EXPECT_EQ(OS::OpenFile(g_testDir + "/normalmode", fd), E_OK); EXPECT_EQ(OS::FileUnlock(fd), E_OK); - EXPECT_EQ(OS::FileUnlock(fd), E_OK); // unlock success will close fd + EXPECT_EQ(OS::CloseFile(fd), E_OK); + EXPECT_EQ(OS::FileUnlock(fd), E_OK); + EXPECT_EQ(OS::CloseFile(fd), E_OK); EXPECT_EQ(OS::FileLock(fd, true), -E_SYSTEM_API_FAIL); EXPECT_EQ(OS::FileLock(fd, true), -E_SYSTEM_API_FAIL); @@ -152,7 +156,9 @@ HWTEST_F(DistributedDBCommonTest, SameProcessReUnLockFile, TestSize.Level1) EXPECT_EQ(OS::FileLock(fd, false), E_OK); EXPECT_EQ(OS::FileUnlock(fd), E_OK); + EXPECT_EQ(OS::CloseFile(fd), E_OK); EXPECT_EQ(OS::FileUnlock(fd), E_OK); + EXPECT_EQ(OS::CloseFile(fd), E_OK); } /** @@ -263,6 +269,7 @@ HWTEST_F(DistributedDBCommonTest, DiffProcessLockFileBlocked, TestSize.Level1) EXPECT_EQ(OS::FileLock(ChildFd, true), E_OK); createStepFlag(1); EXPECT_EQ(OS::FileUnlock(ChildFd), E_OK); + EXPECT_EQ(OS::CloseFile(ChildFd), E_OK); LOGI("child process finish!"); exit(0); } else { @@ -273,6 +280,7 @@ HWTEST_F(DistributedDBCommonTest, DiffProcessLockFileBlocked, TestSize.Level1) } ASSERT_FALSE(waitForStep(1, 10)); EXPECT_EQ(OS::FileUnlock(fd), E_OK); + EXPECT_EQ(OS::CloseFile(fd), E_OK); ASSERT_TRUE(waitForStep(1, 10)); } } @@ -319,6 +327,7 @@ HWTEST_F(DistributedDBCommonTest, DiffProcessGetDBBlocked, TestSize.Level1) // Prevent the child process from not being completed, the main process ends to clean up resources EXPECT_TRUE(waitForStep(2, 1000)); EXPECT_EQ(OS::FileUnlock(fd), E_OK); + EXPECT_EQ(OS::CloseFile(fd), E_OK); } /** @@ -365,6 +374,7 @@ HWTEST_F(DistributedDBCommonTest, DiffProcessDeleteDBBlocked, TestSize.Level1) // Prevent the child process from not being completed, the main process ends to clean up resources EXPECT_TRUE(waitForStep(2, 1000)); EXPECT_EQ(OS::FileUnlock(fd), E_OK); + EXPECT_EQ(OS::CloseFile(fd), E_OK); g_mgr.CloseKvStore(g_kvNbDelegatePtr); } @@ -409,6 +419,7 @@ HWTEST_F(DistributedDBCommonTest, DiffProcessGetDBBlocked001, TestSize.Level1) ASSERT_TRUE(waitForStep(1, 100)); EXPECT_EQ(OS::FileUnlock(fd), E_OK); + EXPECT_EQ(OS::CloseFile(fd), E_OK); ASSERT_TRUE(waitForStep(2, 100)); } diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp index 45323a5ec3d..cdaf305c83b 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp @@ -126,6 +126,7 @@ namespace { dev->SetLocalFieldInfo(fieldInfoList); dev->SetTableInfo(tableInfo); } + EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); } void PrepareData(const std::string &tableName, const std::string &dbPath) @@ -133,6 +134,7 @@ namespace { sqlite3 *db = nullptr; EXPECT_EQ(GetDB(db, dbPath), SQLITE_OK); EXPECT_EQ(InsertValue(db, tableName), SQLITE_OK); + EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); } void OpenStore1(bool syncDualTupleMode = true) @@ -394,6 +396,7 @@ namespace { } EXPECT_EQ(rowCount, 1); sqlite3_finalize(statement); + EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); } } @@ -823,6 +826,7 @@ HWTEST_F(DistributedDBRelationalMultiUserTest, RdbMultiUser008, TestSize.Level1) */ OpenStore1(true); OpenStore2(true); + PrepareEnvironment(g_tableName, g_storePath1, g_rdbDelegatePtr1); /** * @tc.steps: step3. user1 call remote query -- Gitee From 41f5453fa9acd88b7a36c909b67305456cdb888e Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Mon, 24 Oct 2022 11:27:16 +0800 Subject: [PATCH 19/19] fix codecheck Signed-off-by: zwtmichael --- .../syncer/src/single_ver_data_sync.cpp | 6 +++--- .../syncer/src/single_ver_data_sync.h | 2 +- .../syncer/src/single_ver_data_sync_utils.cpp | 6 +++--- .../syncer/src/single_ver_data_sync_utils.h | 3 ++- .../syncer/src/single_ver_sync_engine.cpp | 4 ++-- .../src/single_ver_sync_state_machine.cpp | 4 ++-- .../src/single_ver_sync_state_machine.h | 2 +- .../syncer/src/subscribe_manager.cpp | 2 +- .../syncer/src/subscribe_manager.h | 2 +- .../distributeddb/syncer/src/sync_engine.cpp | 10 +++++----- .../distributeddb/syncer/src/sync_engine.h | 2 +- .../distributeddb/syncer/src/time_sync.cpp | 2 +- .../libs/distributeddb/syncer/src/time_sync.h | 2 +- frameworks/libs/distributeddb/test/BUILD.gn | 2 +- ...nterfaces_schema_database_upgrade_test.cpp | 2 +- .../distributeddb_multi_ver_p2p_sync_test.cpp | 6 +++--- ...ributeddb_relational_ver_p2p_sync_test.cpp | 20 +++++++++---------- ...ributeddb_single_ver_msg_schedule_test.cpp | 8 ++++---- ...buteddb_single_ver_p2p_query_sync_test.cpp | 4 ++++ ...db_single_ver_p2p_subscribe_sync_test.cpp} | 12 +++++------ ...buteddb_single_ver_p2p_sync_check_test.cpp | 3 +-- ...distributeddb_single_ver_p2p_sync_test.cpp | 14 +++++++------ .../unittest/common/syncer/mock_auto_launch.h | 2 +- ...rtual_relational_ver_sync_db_interface.cpp | 6 +++--- ...virtual_relational_ver_sync_db_interface.h | 2 +- .../virtual_single_ver_sync_db_Interface.cpp | 2 +- 26 files changed, 68 insertions(+), 62 deletions(-) rename frameworks/libs/distributeddb/test/unittest/common/syncer/{distributeddb_single_ver_p2p_subsribe_sync_test.cpp => distributeddb_single_ver_p2p_subscribe_sync_test.cpp} (98%) diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp index 291518d1f41..b4fd0d634de 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp @@ -325,7 +325,7 @@ int SingleVerDataSync::GetUnsyncData(SingleVerSyncTaskContext *context, std::vec SyncType curType = (context->IsQuerySync()) ? SyncType::QUERY_SYNC_TYPE : SyncType::MANUAL_FULL_SYNC_TYPE; GetLocalWaterMark(curType, context->GetQuerySyncId(), context, startMark); WaterMark endMark = MAX_TIMESTAMP; - if ((endMark == 0) || (startMark > endMark)) { + if ((startMark > endMark)) { return E_OK; } ContinueToken token = nullptr; @@ -833,8 +833,8 @@ int SingleVerDataSync::PullResponseStart(SingleVerSyncTaskContext *context) return errCode; } -void SingleVerDataSync::UpdateQueryPeerWaterMark(SyncType syncType, const std::string &queryId, SyncTimeRange &dataTime, - const SingleVerSyncTaskContext *context, UpdateWaterMark isUpdateWaterMark) +void SingleVerDataSync::UpdateQueryPeerWaterMark(SyncType syncType, const std::string &queryId, + const SyncTimeRange &dataTime, const SingleVerSyncTaskContext *context, UpdateWaterMark isUpdateWaterMark) { WaterMark tmpPeerWatermark = dataTime.endTime; WaterMark tmpPeerDeletedWatermark = dataTime.deleteEndTime; diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.h b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.h index 4e9343aece3..4b212023154 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.h +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.h @@ -180,7 +180,7 @@ protected: int SendDataPacket(SyncType syncType, const DataRequestPacket *packet, SingleVerSyncTaskContext *context); - void UpdateQueryPeerWaterMark(SyncType syncType, const std::string &queryId, SyncTimeRange &dataTime, + void UpdateQueryPeerWaterMark(SyncType syncType, const std::string &queryId, const SyncTimeRange &dataTime, const SingleVerSyncTaskContext *context, UpdateWaterMark isUpdateWaterMark); void UpdatePeerWaterMark(SyncType syncType, const std::string &queryId, const SingleVerSyncTaskContext *context, diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.cpp index 2827ef3758a..51e2b47b604 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.cpp @@ -257,7 +257,7 @@ void SingleVerDataSyncUtils::FillControlRequestPacket(ControlRequestPacket *pack uint32_t version = std::min(context->GetRemoteSoftwareVersion(), SOFTWARE_VERSION_CURRENT); uint32_t flag = 0; if (context->GetMode() == SyncModeType::SUBSCRIBE_QUERY && context->IsAutoSubscribe()) { - flag = flag | SubscribeRequest::IS_AUTO_SUBSCRIBE; + flag = SubscribeRequest::IS_AUTO_SUBSCRIBE; } packet->SetPacketHead(E_OK, version, GetControlCmdType(context->GetMode()), flag); packet->SetQuery(context->GetQuery()); @@ -298,7 +298,7 @@ bool SingleVerDataSyncUtils::IsNeedTriggerQueryAutoSync(Message *inMsg, QuerySyn uint32_t controlCmdType = packet->GetcontrolCmdType(); if (controlCmdType == ControlCmdType::SUBSCRIBE_QUERY_CMD && inMsg->GetMessageType() == TYPE_REQUEST) { const SubscribeRequest *subPacket = inMsg->GetObject(); - if (packet == nullptr) { + if (subPacket == nullptr) { return false; } query = subPacket->GetQuery(); @@ -398,7 +398,7 @@ SyncTimeRange SingleVerDataSyncUtils::GetQuerySyncDataTimeRange(const std::vecto return dataTimeRange; } -SyncTimeRange SingleVerDataSyncUtils::ReviseLocalMark(SyncType syncType, SyncTimeRange &dataTimeRange, +SyncTimeRange SingleVerDataSyncUtils::ReviseLocalMark(SyncType syncType, const SyncTimeRange &dataTimeRange, UpdateWaterMark updateMark) { SyncTimeRange tmpDataTime = dataTimeRange; diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.h b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.h index 09ff9780c55..e42472a6075 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.h +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.h @@ -79,7 +79,8 @@ public: static SyncTimeRange GetQuerySyncDataTimeRange(const std::vector &inData, WaterMark localMark, WaterMark deleteLocalMark, UpdateWaterMark &isUpdate); - static SyncTimeRange ReviseLocalMark(SyncType syncType, SyncTimeRange &dataTimeRange, UpdateWaterMark updateMark); + static SyncTimeRange ReviseLocalMark(SyncType syncType, const SyncTimeRange &dataTimeRange, + UpdateWaterMark updateMark); static SyncTimeRange GetRecvDataTimeRange(SyncType syncType, const std::vector &data, UpdateWaterMark &isUpdate); diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_engine.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_engine.cpp index 3f85cb9edbb..13988c72c7d 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_engine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_engine.cpp @@ -103,8 +103,8 @@ int SingleVerSyncEngine::SubscribeTimeOut(TimerId id) LOGI("no need to trigger auto subscribe"); return E_OK; } - for (auto &item : allSyncQueries) { - for (auto &query : item.second) { + for (const auto &item : allSyncQueries) { + for (const auto &query : item.second) { InternalSyncParma param; GetSubscribeSyncParam(item.first, query, param); queryAutoSyncCallback_(param); diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp index 8beb7e76154..27de0c7a7dc 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp @@ -298,7 +298,7 @@ void SingleVerSyncStateMachine::CommErrAbort(uint32_t sessionId) { std::lock_guard lock(stateMachineLock_); uint32_t requestSessionId = context_->GetRequestSessionId(); - if ((sessionId != 0) && ((sessionId != requestSessionId) || (requestSessionId == 0))) { + if ((sessionId != 0) && ((requestSessionId == 0) || (sessionId != requestSessionId))) { return; } context_->SetCommNormal(false); @@ -1067,7 +1067,7 @@ bool SingleVerSyncStateMachine::IsNeedResetWatchdog(const Message *inMsg) const return false; } -Event SingleVerSyncStateMachine::TransforTimeOutErrCodeToEvent() +Event SingleVerSyncStateMachine::TransforTimeOutErrCodeToEvent() const { if (syncContext_->IsSyncTaskNeedRetry() && (syncContext_->GetRetryTime() < syncContext_->GetSyncRetryTimes())) { return Event::WAIT_TIME_OUT_EVENT; diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h index 593568ba595..59df3bccfcb 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h @@ -201,7 +201,7 @@ private: bool IsNeedResetWatchdog(const Message *inMsg) const; - Event TransforTimeOutErrCodeToEvent(); + Event TransforTimeOutErrCodeToEvent() const; bool AbilityMsgSessionIdCheck(const Message *inMsg); diff --git a/frameworks/libs/distributeddb/syncer/src/subscribe_manager.cpp b/frameworks/libs/distributeddb/syncer/src/subscribe_manager.cpp index f0facc99739..c1de6980657 100644 --- a/frameworks/libs/distributeddb/syncer/src/subscribe_manager.cpp +++ b/frameworks/libs/distributeddb/syncer/src/subscribe_manager.cpp @@ -97,7 +97,7 @@ void SubscribeManager::DeleteRemoteSubscribeQuery(const std::string &device, con } void SubscribeManager::PutLocalUnFiniedSubQueries(const std::string &device, - std::vector &subscribeQueries) + const std::vector &subscribeQueries) { LOGI("[SubscribeManager] put local unfinished subscribe queries, nums=%zu", subscribeQueries.size()); std::unique_lock lockGuard(localSubscribeMapLock_); diff --git a/frameworks/libs/distributeddb/syncer/src/subscribe_manager.h b/frameworks/libs/distributeddb/syncer/src/subscribe_manager.h index 42ca070c54b..87a38556c2e 100644 --- a/frameworks/libs/distributeddb/syncer/src/subscribe_manager.h +++ b/frameworks/libs/distributeddb/syncer/src/subscribe_manager.h @@ -63,7 +63,7 @@ public: void DeleteRemoteSubscribeQuery(const std::string &device, const QuerySyncObject &query); // put subscribe queries into unfinished map when remote db online - void PutLocalUnFiniedSubQueries(const std::string &device, std::vector &subscribeQueries); + void PutLocalUnFiniedSubQueries(const std::string &device, const std::vector &subscribeQueries); // get all device unFinished subscribe queries which triggered by auto subscribe and need retry subscribe void GetAllUnFinishSubQueries(std::map> &allSyncQueries) const; diff --git a/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp b/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp index 221dcbca9c5..8dae7991b4e 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp @@ -765,7 +765,7 @@ void SyncEngine::SetEqualIdentifier() equalIdentifier[item.second].push_back(item.first); } } - for (auto &item : equalIdentifier) { + for (const auto &item : equalIdentifier) { SetEqualIdentifier(item.first, item.second); } } @@ -779,7 +779,7 @@ void SyncEngine::SetEqualIdentifierMap(const std::string &identifier, const std: } iter++; } - for (auto &device : targets) { + for (const auto &device : targets) { equalIdentifierMap_[device] = identifier; } } @@ -835,7 +835,7 @@ void SyncEngine::GetRemoteSubscribeQueries(const std::string &device, std::vecto subManager_->GetRemoteSubscribeQueries(device, subscribeQueries); } -void SyncEngine::PutUnfiniedSubQueries(const std::string &device, std::vector &subscribeQueries) +void SyncEngine::PutUnfiniedSubQueries(const std::string &device, const std::vector &subscribeQueries) { subManager_->PutLocalUnFiniedSubQueries(device, subscribeQueries); } @@ -992,7 +992,7 @@ bool SyncEngine::IsEngineActive() const void SyncEngine::SchemaChange() { std::lock_guard lock(contextMapLock_); - for (auto &entry : syncTaskContextMap_) { + for (const auto &entry : syncTaskContextMap_) { auto context = entry.second; if (context->IsKilled()) { continue; @@ -1111,7 +1111,7 @@ void SyncEngine::AbortMachineIfNeed(uint32_t syncId) ISyncTaskContext *abortContext = nullptr; { std::lock_guard lock(contextMapLock_); - for (auto &entry : syncTaskContextMap_) { + for (const auto &entry : syncTaskContextMap_) { auto context = entry.second; if (context->IsKilled()) { continue; diff --git a/frameworks/libs/distributeddb/syncer/src/sync_engine.h b/frameworks/libs/distributeddb/syncer/src/sync_engine.h index a7f8fad203a..3c529f6a87e 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_engine.h +++ b/frameworks/libs/distributeddb/syncer/src/sync_engine.h @@ -93,7 +93,7 @@ public: void GetRemoteSubscribeQueries(const std::string &device, std::vector &subscribeQueries); - void PutUnfiniedSubQueries(const std::string &device, std::vector &subscribeQueries); + void PutUnfiniedSubQueries(const std::string &device, const std::vector &subscribeQueries); void GetAllUnFinishSubQueries(std::map> &allSyncQueries); diff --git a/frameworks/libs/distributeddb/syncer/src/time_sync.cpp b/frameworks/libs/distributeddb/syncer/src/time_sync.cpp index 26cc3b9fd40..bcc8f28eb75 100644 --- a/frameworks/libs/distributeddb/syncer/src/time_sync.cpp +++ b/frameworks/libs/distributeddb/syncer/src/time_sync.cpp @@ -150,7 +150,7 @@ int TimeSync::RegisterTransformFunc() return MessageTransform::RegTransformFunction(TIME_SYNC_MESSAGE, func); } -int TimeSync::Initialize(ICommunicator *communicator, std::shared_ptr &metadata, +int TimeSync::Initialize(ICommunicator *communicator, const std::shared_ptr &metadata, const ISyncInterface *storage, const DeviceID &deviceId) { if ((communicator == nullptr) || (storage == nullptr) || (metadata == nullptr)) { diff --git a/frameworks/libs/distributeddb/syncer/src/time_sync.h b/frameworks/libs/distributeddb/syncer/src/time_sync.h index 0b79152cac6..263faac1677 100644 --- a/frameworks/libs/distributeddb/syncer/src/time_sync.h +++ b/frameworks/libs/distributeddb/syncer/src/time_sync.h @@ -71,7 +71,7 @@ public: static int DeSerialization(const uint8_t *buffer, uint32_t length, Message *inMsg); // register to communicator - int Initialize(ICommunicator *communicator, std::shared_ptr &metadata, + int Initialize(ICommunicator *communicator, const std::shared_ptr &metadata, const ISyncInterface *storage, const DeviceID &deviceId); int SyncStart(const CommErrHandler &handler = nullptr, uint32_t sessionId = 0); // send timesync request diff --git a/frameworks/libs/distributeddb/test/BUILD.gn b/frameworks/libs/distributeddb/test/BUILD.gn index d65fed70523..5d8a48d35e6 100644 --- a/frameworks/libs/distributeddb/test/BUILD.gn +++ b/frameworks/libs/distributeddb/test/BUILD.gn @@ -658,7 +658,7 @@ distributeddb_unittest("DistributedDBCommunicatorProxyTest") { } distributeddb_unittest("DistributedDBSingleVerP2PSubscribeSyncTest") { - sources = [ "unittest/common/syncer/distributeddb_single_ver_p2p_subsribe_sync_test.cpp" ] + sources = [ "unittest/common/syncer/distributeddb_single_ver_p2p_subscribe_sync_test.cpp" ] } distributeddb_unittest("DistributedDBMockSyncModuleTest") { diff --git a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_schema_database_upgrade_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_schema_database_upgrade_test.cpp index c0dd063026e..9587a2c46f6 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_schema_database_upgrade_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_schema_database_upgrade_test.cpp @@ -96,7 +96,7 @@ namespace { } return oriSchemaStr; } - bool SchemaChecker(const std::string schema) + bool SchemaChecker(const std::string &schema) { SchemaObject schemaObj; return (schemaObj.ParseFromSchemaString(schema) == E_OK); diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_multi_ver_p2p_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_multi_ver_p2p_sync_test.cpp index 70d3ffbcd2a..f4cce093ad7 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_multi_ver_p2p_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_multi_ver_p2p_sync_test.cpp @@ -770,7 +770,7 @@ static bool IsCommitHistorySyncRequestPacketEqual(const CommitHistorySyncRequest std::map commitMapB; inPacketA.GetCommitMap(commitMapA); inPacketB.GetCommitMap(commitMapB); - for (auto &entry : commitMapA) { + for (const auto &entry : commitMapA) { if (commitMapB.count(entry.first) == 0) { return false; } @@ -778,7 +778,7 @@ static bool IsCommitHistorySyncRequestPacketEqual(const CommitHistorySyncRequest return false; } } - for (auto &entry : commitMapB) { + for (const auto &entry : commitMapB) { if (commitMapA.count(entry.first) == 0) { return false; } @@ -897,7 +897,7 @@ static bool IsCommitHistorySyncAckPacketEqual(const CommitHistorySyncAckPacket & return false; } int count = 0; - for (auto &entry : commitVecA) { + for (const auto &entry : commitVecA) { if (!IsMultiVerCommitEqual(entry, commitVecB[count++])) { return false; } diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp index 9c49cac7395..cbc305229bf 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp @@ -145,7 +145,7 @@ namespace { return sqlite3_exec(db, sql.c_str(), nullptr, nullptr, nullptr); } - int CreateTable(sqlite3 *db, std::vector &fieldInfoList, const std::string &tableName) + int CreateTable(sqlite3 *db, const std::vector &fieldInfoList, const std::string &tableName) { std::string sql = "CREATE TABLE " + tableName + "("; int index = 0; @@ -398,7 +398,7 @@ namespace { } void GetSyncDataStep(std::map &dataMap, sqlite3_stmt *statement, - std::vector fieldInfoList) + std::vector &fieldInfoList) { int columnCount = sqlite3_column_count(statement); ASSERT_EQ(static_cast(columnCount), fieldInfoList.size()); @@ -410,7 +410,7 @@ namespace { } void GetSyncData(sqlite3 *db, std::map &dataMap, const std::string &tableName, - std::vector fieldInfoList) + std::vector &fieldInfoList) { sqlite3_stmt *statement = nullptr; EXPECT_EQ(PrepareSelect(db, statement, GetDeviceTableName(tableName)), SQLITE_OK); @@ -434,7 +434,7 @@ namespace { } void PrepareBasicTable(const std::string &tableName, std::vector &fieldInfoList, - std::vector &remoteDeviceVec, bool createDistributedTable = true) + const std::vector &remoteDeviceVec, bool createDistributedTable = true) { sqlite3 *db = nullptr; EXPECT_EQ(GetDB(db), SQLITE_OK); @@ -466,7 +466,7 @@ namespace { } void PrepareVirtualEnvironment(std::map &dataMap, const std::string &tableName, - std::vector &fieldInfoList, std::vector remoteDeviceVec, + std::vector &fieldInfoList, const std::vector remoteDeviceVec, bool createDistributedTable = true) { PrepareBasicTable(tableName, fieldInfoList, remoteDeviceVec, createDistributedTable); @@ -480,13 +480,13 @@ namespace { } void PrepareVirtualEnvironment(std::map &dataMap, - std::vector remoteDeviceVec, bool createDistributedTable = true) + const std::vector remoteDeviceVec, bool createDistributedTable = true) { PrepareVirtualEnvironment(dataMap, g_tableName, g_fieldInfoList, remoteDeviceVec, createDistributedTable); } - void CheckData(std::map &targetMap, const std::string &tableName, - std::vector fieldInfoList) + void CheckData(const std::map &targetMap, const std::string &tableName, + std::vector &fieldInfoList) { std::map dataMap; sqlite3 *db = nullptr; @@ -500,7 +500,7 @@ namespace { } } - void CheckData(std::map &targetMap) + void CheckData(const std::map &targetMap) { CheckData(targetMap, g_tableName, g_fieldInfoList); } @@ -576,7 +576,7 @@ namespace { void PrepareEnvironment(std::map &dataMap, std::vector &localFieldInfoList, std::vector &remoteFieldInfoList, - std::vector remoteDeviceVec) + const std::vector remoteDeviceVec) { PrepareEnvironment(dataMap, g_tableName, localFieldInfoList, remoteFieldInfoList, remoteDeviceVec); } diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_msg_schedule_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_msg_schedule_test.cpp index 9df55d1bb65..f1778d10a06 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_msg_schedule_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_msg_schedule_test.cpp @@ -432,8 +432,8 @@ HWTEST_F(DistributedDBSingleVerMsgScheduleTest, MsgSchedule007, TestSize.Level0) msgSchedule.ScheduleInfoHandle(isNeedHandle, false, msg); delete msg; } - Message *msg = msgSchedule.MoveNextMsg(context, isNeedHandle, isNeedContinue); - ASSERT_TRUE(msg == nullptr); + Message *msg2 = msgSchedule.MoveNextMsg(context, isNeedHandle, isNeedContinue); + ASSERT_TRUE(msg2 == nullptr); /** * @tc.steps: step2. put msg seq1_packet1, seq2_packet2 * @tc.expected: get nullptr @@ -448,9 +448,9 @@ HWTEST_F(DistributedDBSingleVerMsgScheduleTest, MsgSchedule007, TestSize.Level0) isNeedHandle = true; isNeedContinue = true; for (uint32_t i = 1; i <= 3; i++) { - Message *msg = msgSchedule.MoveNextMsg(context, isNeedHandle, isNeedContinue); + Message *msg3 = msgSchedule.MoveNextMsg(context, isNeedHandle, isNeedContinue); EXPECT_EQ(isNeedContinue, true); - ASSERT_TRUE(msg == nullptr); + ASSERT_TRUE(msg3 == nullptr); } RefObject::KillAndDecObjRef(context); context = nullptr; diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_query_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_query_sync_test.cpp index cdf06110bd4..112dc2f2270 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_query_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_query_sync_test.cpp @@ -344,9 +344,11 @@ HWTEST_F(DistributedDBSingleVerP2PQuerySyncTest, NormalSync003, TestSize.Level1) Value value = {'1'}; const int dataSize = 10; status = g_kvDelegatePtr->Put(key, value); + ASSERT_TRUE(status == OK); Key key2 = {'2'}; Value value2 = {'2'}; status = g_kvDelegatePtr->Put(key2, value2); + ASSERT_TRUE(status == OK); /** * @tc.steps: step2. deviceB put {b0, v0} - {b9, v9}, {c, v} @@ -1448,6 +1450,7 @@ HWTEST_F(DistributedDBSingleVerP2PQuerySyncTest, AllPredicateQuerySync001, TestS key.push_back(i); key2.push_back(i); status = g_schemaKvDelegatePtr->Put(key, value); + ASSERT_TRUE(status == OK); status = g_schemaKvDelegatePtr->Put(key2, value2); ASSERT_TRUE(status == OK); key.pop_back(); @@ -1545,6 +1548,7 @@ HWTEST_F(DistributedDBSingleVerP2PQuerySyncTest, AllPredicateQuerySync003, TestS key.push_back(i); key2.push_back(i); status = g_schemaKvDelegatePtr->Put(key, value); + ASSERT_TRUE(status == OK); status = g_schemaKvDelegatePtr->Put(key2, value2); ASSERT_TRUE(status == OK); key.pop_back(); diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_subsribe_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_subscribe_sync_test.cpp similarity index 98% rename from frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_subsribe_sync_test.cpp rename to frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_subscribe_sync_test.cpp index a7ce0829ad3..fcf58d5200f 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_subsribe_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_subscribe_sync_test.cpp @@ -511,9 +511,9 @@ HWTEST_F(DistributedDBSingleVerP2PSubscribeSyncTest, subscribeManager003, TestSi */ LOGI("============step 3============"); for (int i = 0; i < 8; i++) { - QuerySyncObject querySyncObj(Query::Select().PrefixKey({'a', static_cast('a' + i)})); - ASSERT_TRUE(subManager.ReserveLocalSubscribeQuery(device + std::to_string(i), querySyncObj) == E_OK); - ASSERT_TRUE(subManager.ActiveLocalSubscribeQuery(device + std::to_string(i), querySyncObj) == E_OK); + QuerySyncObject querySyncObj2(Query::Select().PrefixKey({'a', static_cast('a' + i)})); + ASSERT_TRUE(subManager.ReserveLocalSubscribeQuery(device + std::to_string(i), querySyncObj2) == E_OK); + ASSERT_TRUE(subManager.ActiveLocalSubscribeQuery(device + std::to_string(i), querySyncObj2) == E_OK); } QuerySyncObject querySyncObj1(Query::Select().PrefixKey({'a', static_cast('a' + 8)})); ASSERT_TRUE(subManager.ReserveLocalSubscribeQuery(device + std::to_string(8), querySyncObj1) != E_OK); @@ -562,9 +562,9 @@ HWTEST_F(DistributedDBSingleVerP2PSubscribeSyncTest, subscribeManager004, TestSi */ LOGI("============step 3============"); for (int i = 0; i < 8; i++) { - QuerySyncObject querySyncObj(Query::Select().PrefixKey({'a', static_cast('a' + i)})); - ASSERT_TRUE(subManager.ReserveRemoteSubscribeQuery(device + std::to_string(i), querySyncObj) == E_OK); - ASSERT_TRUE(subManager.ActiveRemoteSubscribeQuery(device + std::to_string(i), querySyncObj) == E_OK); + QuerySyncObject querySyncObj2(Query::Select().PrefixKey({'a', static_cast('a' + i)})); + ASSERT_TRUE(subManager.ReserveRemoteSubscribeQuery(device + std::to_string(i), querySyncObj2) == E_OK); + ASSERT_TRUE(subManager.ActiveRemoteSubscribeQuery(device + std::to_string(i), querySyncObj2) == E_OK); } QuerySyncObject querySyncObj1(Query::Select().PrefixKey({'a', static_cast('a' + 8)})); ASSERT_TRUE(subManager.ReserveRemoteSubscribeQuery(device + std::to_string(8), querySyncObj1) != E_OK); diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp index 35a3af9eb72..5f468630015 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp @@ -593,7 +593,7 @@ void SyncWithQuery(vector &devices, const Query &query, const DBSta SyncWithQuery(devices, query, DistributedDB::SYNC_MODE_PUSH_ONLY, targetStatus); } -void SyncWithDeviceOffline(vector &devices, Key &key, Query &query) +void SyncWithDeviceOffline(vector &devices, Key &key, const Query &query) { Value value = {'2'}; ASSERT_TRUE(g_kvDelegatePtr->Put(key, value) == OK); @@ -660,7 +660,6 @@ HWTEST_F(DistributedDBSingleVerP2PSyncCheckTest, AckSessionCheck001, TestSize.Le * @tc.steps: step1. deviceB sync to deviceA just for timeSync and abilitySync * @tc.expected: step1. should return OK. */ - std::map result; ASSERT_TRUE(g_deviceB->Sync(SYNC_MODE_PUSH_ONLY, true) == OK); /** diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp index c828bda01c9..71bc92db6fd 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp @@ -2302,11 +2302,12 @@ HWTEST_F(DistributedDBSingleVerP2PSyncTest, SaveDataNotify001, TestSize.Level3) */ g_deviceB->SetSaveDataDelayTime(WAIT_30_SECONDS); status = g_kvDelegatePtr->Put(key, value); - + ASSERT_TRUE(status == OK); /** * @tc.steps: step3. deviceA call sync and wait * @tc.expected: step3. sync should return OK. onComplete should be called, deviceB sync success. */ + result.clear(); status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PUSH_ONLY, result); ASSERT_TRUE(status == OK); ASSERT_TRUE(result.size() == devices.size()); @@ -2317,11 +2318,12 @@ HWTEST_F(DistributedDBSingleVerP2PSyncTest, SaveDataNotify001, TestSize.Level3) */ g_deviceB->SetSaveDataDelayTime(WAIT_36_SECONDS); status = g_kvDelegatePtr->Put(key, value); - + ASSERT_TRUE(status == OK); /** * @tc.steps: step5. deviceA call sync and wait * @tc.expected: step5. sync should return OK. onComplete should be called, deviceB sync TIME_OUT. */ + result.clear(); status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PUSH_ONLY, result); ASSERT_TRUE(status == OK); ASSERT_TRUE(result.size() == devices.size()); @@ -2412,7 +2414,7 @@ HWTEST_F(DistributedDBSingleVerP2PSyncTest, SametimeSync002, TestSize.Level3) } }); std::map result; - auto callback = [&result](std::map map) { + auto callback = [&result](std::map &map) { result = map; }; Query query = Query::Select().PrefixKey({'k', '1'}); @@ -2431,7 +2433,7 @@ HWTEST_F(DistributedDBSingleVerP2PSyncTest, SametimeSync002, TestSize.Level3) }); std::thread subThread([&devices] { std::map result; - auto callback = [&result](std::map map) { + auto callback = [&result](std::map &map) { result = map; }; Query query = Query::Select().PrefixKey({'k', '2'}); @@ -2447,7 +2449,7 @@ HWTEST_F(DistributedDBSingleVerP2PSyncTest, SametimeSync002, TestSize.Level3) std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::map virtualResult; g_deviceB->Sync(DistributedDB::SYNC_MODE_PULL_ONLY, query, - [&virtualResult](std::map map) { + [&virtualResult](std::map &map) { virtualResult = map; }, true); EXPECT_TRUE(status == OK); @@ -2472,7 +2474,7 @@ HWTEST_F(DistributedDBSingleVerP2PSyncTest, DatabaseOnlineCallback001, TestSize. */ std::string targetDev = "DEVICE_X"; bool isCheckOk = false; - auto databaseStatusNotifyCallback = [targetDev, &isCheckOk] (std::string userId, + auto databaseStatusNotifyCallback = [targetDev, &isCheckOk] (std::string userId,2304 std::string appId, std::string storeId, const std::string deviceId, bool onlineStatus) -> void { if (userId == USER_ID && appId == APP_ID && storeId == STORE_ID && deviceId == targetDev && onlineStatus == true) { diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_auto_launch.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_auto_launch.h index af89a85aea4..6236d010a93 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_auto_launch.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_auto_launch.h @@ -22,7 +22,7 @@ namespace DistributedDB { class MockAutoLaunch : public AutoLaunch { public: - void SetAutoLaunchItem(const std::string &identify, const std::string &userId, AutoLaunchItem &item) + void SetAutoLaunchItem(const std::string &identify, const std::string &userId, const AutoLaunchItem &item) { std::lock_guard autoLock(extLock_); extItemMap_[identify][userId] = item; diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp index afda4562fed..5ec215d783f 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp @@ -25,7 +25,7 @@ namespace { int GetEntriesFromItems(std::vector &entries, const std::vector &dataItems) { int errCode = E_OK; - for (auto &item : dataItems) { + for (const auto &item : dataItems) { auto entry = new (std::nothrow) GenericSingleVerKvEntry(); if (entry == nullptr) { LOGE("Create entry failed."); @@ -274,7 +274,7 @@ int VirtualRelationalVerSyncDBInterface::DeleteMetaDataByPrefixKey(const Key &ke int VirtualRelationalVerSyncDBInterface::GetAllMetaKeys(std::vector &keys) const { - for (auto &iter : metadata_) { + for (const auto &iter : metadata_) { keys.push_back(iter.first); } LOGD("GetAllMetaKeys size %zu", keys.size()); @@ -364,7 +364,7 @@ void VirtualRelationalVerSyncDBInterface::SetPermitCreateDistributedTable(bool p permitCreateDistributedTable_ = permitCreateDistributedTable; } -void ObjectData::PutDataValue(const std::string &fieldName, const DataValue &value) +void ObjectData::PutDataValue(const std::string &fieldName, const DataValue &value) const { fieldData[fieldName] = value; } diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.h index e42cba6be4f..a7ca8f479b2 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.h @@ -24,7 +24,7 @@ namespace DistributedDB { struct ObjectData { public: - void PutDataValue(const std::string &fieldName, const DataValue &value); + void PutDataValue(const std::string &fieldName, const DataValue &value) const; int GetDataValue(const std::string &fieldName, DataValue &value) const; private: mutable std::map fieldData; diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp index b826d4845cb..259868e4872 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp @@ -31,7 +31,7 @@ namespace { int GetEntriesFromItems(std::vector &entries, const std::vector &dataItems) { int errCode = E_OK; - for (auto &item : dataItems) { + for (const auto &item : dataItems) { auto entry = new (std::nothrow) GenericSingleVerKvEntry(); if (entry == nullptr) { LOGE("Create entry failed."); -- Gitee