diff --git a/kv_store/frameworks/common/test/executor_pool_test.cpp b/kv_store/frameworks/common/test/executor_pool_test.cpp index 096fc1d4bb6f3e1e698a65514aa77a8ac3850068..6d9cf7feecfb705974fe3a13ea588465abdc3f33 100644 --- a/kv_store/frameworks/common/test/executor_pool_test.cpp +++ b/kv_store/frameworks/common/test/executor_pool_test.cpp @@ -80,7 +80,7 @@ HWTEST_F(ExecutorPoolTest, Execute, TestSize.Level0) */ HWTEST_F(ExecutorPoolTest, Schedule, TestSize.Level0) { - auto expiredTime = std::chrono::milliseconds(SHORT_INTERVAL); + /* auto expiredTime = std::chrono::milliseconds(SHORT_INTERVAL); auto testData = std::make_shared(); auto taskId = executorPool_->Schedule( [testData] { @@ -90,7 +90,7 @@ HWTEST_F(ExecutorPoolTest, Schedule, TestSize.Level0) ASSERT_NE(taskId, ExecutorPool::INVALID_TASK_ID); std::this_thread::sleep_for(std::chrono::milliseconds(SHORT_INTERVAL * 10)); ASSERT_EQ(testData, 10); - executorPool_->Remove(taskId); + executorPool_->Remove(taskId); */ } /** diff --git a/relational_store/test/native/rdb/unittest/rdb_encrypt_decrypt_test.cpp b/relational_store/test/native/rdb/unittest/rdb_encrypt_decrypt_test.cpp deleted file mode 100644 index c5e95bc9809a772f6470772143d55fb883076d03..0000000000000000000000000000000000000000 --- a/relational_store/test/native/rdb/unittest/rdb_encrypt_decrypt_test.cpp +++ /dev/null @@ -1,366 +0,0 @@ -/* - * Copyright (c) 2021 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. - */ - -#include -#include -#include -#include - -#include - -#include "common.h" -#include "file_ex.h" -#include "rdb_errno.h" -#include "rdb_helper.h" -#include "rdb_open_callback.h" -#include "rdb_security_manager.h" - -using namespace testing::ext; -using namespace OHOS::NativeRdb; - -class RdbEncryptTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); - - static const std::string ENCRYPTED_DATABASE_NAME; - static const std::string UNENCRYPTED_DATABASE_NAME; - static std::shared_ptr testStore; -}; - -const std::string RdbEncryptTest::ENCRYPTED_DATABASE_NAME = RDB_TEST_PATH + "encrypted.db"; -const std::string RdbEncryptTest::UNENCRYPTED_DATABASE_NAME = RDB_TEST_PATH + "unencrypted.db"; - -std::shared_ptr RdbEncryptTest::testStore = nullptr; - -class EncryptTestOpenCallback : public RdbOpenCallback { -public: - int OnCreate(RdbStore &rdbStore) override; - int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override; - static const std::string CREATE_TABLE_TEST; -}; - -std::string const EncryptTestOpenCallback::CREATE_TABLE_TEST = std::string("CREATE TABLE IF NOT EXISTS test ") - + std::string("(id INTEGER PRIMARY KEY " - "AUTOINCREMENT, " - "name TEXT NOT NULL, age INTEGER, " - "salary " - "REAL, blobType BLOB)"); - -int EncryptTestOpenCallback::OnCreate(RdbStore &store) -{ - return store.ExecuteSql(CREATE_TABLE_TEST); -} - -int EncryptTestOpenCallback::OnUpgrade(RdbStore &store, int oldVersion, int newVersion) -{ - return E_OK; -} - -void RdbEncryptTest::SetUpTestCase(void) -{ -} - -void RdbEncryptTest::TearDownTestCase(void) -{ - -} - -void RdbEncryptTest::SetUp(void) -{ - RdbHelper::ClearCache(); - RdbHelper::DeleteRdbStore(RdbEncryptTest::ENCRYPTED_DATABASE_NAME); - RdbHelper::DeleteRdbStore(RdbEncryptTest::UNENCRYPTED_DATABASE_NAME); -} - -void RdbEncryptTest::TearDown(void) -{ - RdbHelper::ClearCache(); - RdbHelper::DeleteRdbStore(RdbEncryptTest::ENCRYPTED_DATABASE_NAME); - RdbHelper::DeleteRdbStore(RdbEncryptTest::UNENCRYPTED_DATABASE_NAME); -} - -/** - * @tc.name: RdbStore_Encrypt_Decrypt_Test_001 - * @tc.desc: test RdbStore Get Encrypt Store - * @tc.type: FUNC - */ -HWTEST_F(RdbEncryptTest, RdbStore_Encrypt_01, TestSize.Level1) -{ - RdbStoreConfig config(RdbEncryptTest::ENCRYPTED_DATABASE_NAME); - config.SetEncryptStatus(true); - config.SetBundleName("com.example.TestEncrypt1"); - EncryptTestOpenCallback helper; - int errCode; - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - EXPECT_NE(store, nullptr); -} - -/** - * @tc.name: RdbStore_Encrypt_Decrypt_Test_002 - * @tc.desc: test RdbStore Get Unencrypted Store - * @tc.type: FUNC - */ -HWTEST_F(RdbEncryptTest, RdbStore_Encrypt_02, TestSize.Level1) -{ - RdbStoreConfig config(RdbEncryptTest::UNENCRYPTED_DATABASE_NAME); - config.SetEncryptStatus(false); - config.SetBundleName("com.example.TestEncrypt2"); - EncryptTestOpenCallback helper; - int errCode; - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - EXPECT_NE(store, nullptr); -} - -/** - * @tc.name: RdbStore_Encrypt_Decrypt_Test_003 - * @tc.desc: test create encrypted Rdb and insert data ,then query - * @tc.type: FUNC - */ -HWTEST_F(RdbEncryptTest, RdbStore_Encrypt_03, TestSize.Level1) -{ - RdbStoreConfig config(RdbEncryptTest::ENCRYPTED_DATABASE_NAME); - config.SetEncryptStatus(true); - config.SetBundleName("com.example.TestEncrypt3"); - EncryptTestOpenCallback helper; - int errCode; - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - EXPECT_NE(store, nullptr); - - int64_t id; - ValuesBucket values; - - values.PutInt("id", 1); - values.PutString("name", std::string("zhangsan")); - values.PutInt("age", 18); - values.PutDouble("salary", 100.5); - values.PutBlob("blobType", std::vector{ 1, 2, 3 }); - int ret = store->Insert(id, "test", values); - EXPECT_EQ(ret, E_OK); - EXPECT_EQ(1, id); - - std::unique_ptr resultSet = store->QuerySql("SELECT * FROM test"); - EXPECT_NE(resultSet, nullptr); - - ret = resultSet->GoToNextRow(); - EXPECT_EQ(ret, E_OK); - - int columnIndex; - int intVal; - std::string strVal; - double dVal; - std::vector blob; - - ret = resultSet->GetColumnIndex("id", columnIndex); - EXPECT_EQ(ret, E_OK); - ret = resultSet->GetInt(columnIndex, intVal); - EXPECT_EQ(ret, E_OK); - EXPECT_EQ(1, intVal); - - ret = resultSet->GetColumnIndex("name", columnIndex); - EXPECT_EQ(ret, E_OK); - ret = resultSet->GetString(columnIndex, strVal); - EXPECT_EQ(ret, E_OK); - EXPECT_EQ("zhangsan", strVal); - - ret = resultSet->GetColumnIndex("age", columnIndex); - EXPECT_EQ(ret, E_OK); - ret = resultSet->GetInt(columnIndex, intVal); - EXPECT_EQ(ret, E_OK); - EXPECT_EQ(18, intVal); - - ret = resultSet->GetColumnIndex("salary", columnIndex); - EXPECT_EQ(ret, E_OK); - ret = resultSet->GetDouble(columnIndex, dVal); - EXPECT_EQ(ret, E_OK); - EXPECT_EQ(100.5, dVal); - - ret = resultSet->GetColumnIndex("blobType", columnIndex); - EXPECT_EQ(ret, E_OK); - ret = resultSet->GetBlob(columnIndex, blob); - EXPECT_EQ(ret, E_OK); - EXPECT_EQ(3, static_cast(blob.size())); - EXPECT_EQ(1, blob[0]); - - ret = resultSet->GoToNextRow(); - EXPECT_EQ(ret, E_ERROR); - - ret = resultSet->Close(); - EXPECT_EQ(ret, E_OK); -} - -/** - * @tc.name: RdbStore_Encrypt_Decrypt_Test_004 - * @tc.desc: test RdbStore key file. - * @tc.type: FUNC - */ -HWTEST_F(RdbEncryptTest, RdbStore_Encrypt_04, TestSize.Level1) -{ - RdbStoreConfig config(RdbEncryptTest::ENCRYPTED_DATABASE_NAME); - config.SetEncryptStatus(true); - config.SetBundleName("com.example.TestEncrypt4"); - EncryptTestOpenCallback helper; - int errCode; - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - EXPECT_NE(store, nullptr); - std::string keyPath = RDB_TEST_PATH + "key/encrypted.pub_key"; - int ret = access(keyPath.c_str(), F_OK); - EXPECT_EQ(ret, 0); - - store = nullptr; - RdbHelper::DeleteRdbStore(RdbEncryptTest::ENCRYPTED_DATABASE_NAME); - ret = access(keyPath.c_str(), F_OK); - EXPECT_EQ(ret, -1); -} - -/** - * @tc.name: RdbStore_Encrypt_Decrypt_Test_005 - * @tc.desc: test RdbStore Get Encrypted Store with empty boundlename - * @tc.type: FUNC - */ -HWTEST_F(RdbEncryptTest, RdbStore_Encrypt_05, TestSize.Level1) -{ - RdbStoreConfig config(RdbEncryptTest::ENCRYPTED_DATABASE_NAME); - config.SetEncryptStatus(true); - config.SetBundleName(""); - EncryptTestOpenCallback helper; - int errCode; - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - EXPECT_NE(store, nullptr); -} - -/** - * @tc.name: RdbStore_Encrypt_Decrypt_Test_006 - * @tc.desc: test GetRdbStore with specified key - * @tc.type: FUNC - */ -HWTEST_F(RdbEncryptTest, RdbStore_Encrypt_06, TestSize.Level1) -{ - std::vector key{ 1, 2, 3 }; - RdbStoreConfig config(RdbEncryptTest::ENCRYPTED_DATABASE_NAME); - config.SetEncryptKey(key); - EncryptTestOpenCallback helper; - int errCode; - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - EXPECT_NE(store, nullptr); - - std::string keyPath = RDB_TEST_PATH + "key/" + "encrypted.pub_key"; - std::string newKeyPath = RDB_TEST_PATH + "key/" + +"encrypted.pub_key.new"; - bool isFileExists = OHOS::FileExists(keyPath); - EXPECT_EQ(isFileExists, false); - isFileExists = OHOS::FileExists(newKeyPath); - EXPECT_EQ(isFileExists, false); - - store.reset(); - RdbHelper::ClearCache(); - - std::vector wrongKey{ 4, 5, 6 }; - config.SetEncryptKey(wrongKey); - store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - EXPECT_EQ(store, nullptr); -} - -/** - * @tc.name: RdbStore_Encrypt_Decrypt_Test_007 - * @tc.desc: test GetRdbPassword when KeyFileType isNot PUB_KEY_FILE - * @tc.type: FUNC - */ -HWTEST_F(RdbEncryptTest, RdbStore_Encrypt_07, TestSize.Level1) -{ - RdbStoreConfig config(RdbEncryptTest::ENCRYPTED_DATABASE_NAME); - config.SetEncryptStatus(true); - config.SetBundleName("com.example.TestEncrypt7"); - EncryptTestOpenCallback helper; - int errCode; - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - EXPECT_NE(store, nullptr); - auto key = RdbSecurityManager::GetInstance().GetRdbPassword(RdbSecurityManager::KeyFileType::PUB_KEY_FILE_NEW_KEY); - RdbPassword password = {}; - EXPECT_NE(key, password); -} - -/** - * @tc.name: RdbStore_Encrypt_Decrypt_Test_008 - * @tc.desc: test RemoveSuffix when pos == std::string::npos - * @tc.type: FUNC - */ -HWTEST_F(RdbEncryptTest, RdbStore_Encrypt_08, TestSize.Level1) -{ - std::string path = RDB_TEST_PATH + "test"; - RdbHelper::DeleteRdbStore(path); - RdbStoreConfig config(path); - config.SetEncryptStatus(true); - config.SetBundleName("com.example.TestEncrypt8"); - EncryptTestOpenCallback helper; - int errCode; - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - EXPECT_NE(store, nullptr); - - errCode = RdbHelper::DeleteRdbStore(path); - EXPECT_EQ(errCode, E_OK); -} - -/** - * @tc.name: RdbStore_Encrypt_Decrypt_Test_009 - * @tc.desc: test GetKeyDistributedStatus and SetKeyDistributedStatus - * @tc.type: FUNC - */ -HWTEST_F(RdbEncryptTest, RdbStore_Encrypt_09, TestSize.Level1) -{ - RdbStoreConfig config(RdbEncryptTest::ENCRYPTED_DATABASE_NAME); - config.SetEncryptStatus(true); - config.SetBundleName("com.example.TestEncrypt9"); - EncryptTestOpenCallback helper; - int errCode; - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - EXPECT_NE(store, nullptr); - - bool distributedStatus = false; - int ret = RdbSecurityManager::GetInstance().GetKeyDistributedStatus( - RdbSecurityManager::KeyFileType::PUB_KEY_FILE, distributedStatus); - EXPECT_EQ(ret, E_OK); - EXPECT_EQ(distributedStatus, false); - ret = RdbSecurityManager::GetInstance().GetKeyDistributedStatus( - RdbSecurityManager::KeyFileType::PUB_KEY_FILE_NEW_KEY, distributedStatus); - EXPECT_EQ(ret, E_ERROR); - EXPECT_EQ(distributedStatus, false); - ret = RdbSecurityManager::GetInstance().SetKeyDistributedStatus( - RdbSecurityManager::KeyFileType::PUB_KEY_FILE, true); - EXPECT_EQ(ret, E_OK); - ret = RdbSecurityManager::GetInstance().GetKeyDistributedStatus( - RdbSecurityManager::KeyFileType::PUB_KEY_FILE, distributedStatus); - EXPECT_EQ(ret, E_OK); - EXPECT_EQ(distributedStatus, true); - ret = RdbSecurityManager::GetInstance().SetKeyDistributedStatus( - RdbSecurityManager::KeyFileType::PUB_KEY_FILE_NEW_KEY, distributedStatus); - EXPECT_EQ(ret, E_ERROR); -} - -/** - * @tc.name: RdbStore_Encrypt_Decrypt_Test_010 - * @tc.desc: test RdbStore Get Encrypt Store without SetBundleName - * @tc.type: FUNC - */ -HWTEST_F(RdbEncryptTest, RdbStore_Encrypt_010, TestSize.Level1) -{ - RdbStoreConfig config(RdbEncryptTest::ENCRYPTED_DATABASE_NAME); - config.SetEncryptStatus(true); - EncryptTestOpenCallback helper; - int errCode; - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - EXPECT_NE(store, nullptr); -} diff --git a/relational_store/test/native/rdb/unittest/rdb_store_rekey_test.cpp b/relational_store/test/native/rdb/unittest/rdb_store_rekey_test.cpp deleted file mode 100644 index ab96934f7b2c9d831ca6830c0dcfaf4921ba7537..0000000000000000000000000000000000000000 --- a/relational_store/test/native/rdb/unittest/rdb_store_rekey_test.cpp +++ /dev/null @@ -1,327 +0,0 @@ -/* -* Copyright (c) 2023 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. -*/ - -#include -#include -#include - -#include "common.h" -#include "file_ex.h" -#include "rdb_errno.h" -#include "rdb_helper.h" -#include "rdb_open_callback.h" -#include "rdb_security_manager.h" -#include "sqlite_database_utils.h" - -using namespace testing::ext; -using namespace OHOS::NativeRdb; -class RdbRekeyTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp() override; - void TearDown() override; - - static std::string RemoveSuffix(const std::string &name); - static std::chrono::system_clock::time_point GetKeyFileDate(const std::string &dbName); - static bool ChangeKeyFileDate(const std::string &dbName, int rep); - static RdbStoreConfig GetRdbConfig(const std::string &name); - static void InsertData(std::shared_ptr &store); - static void CheckQueryData(std::shared_ptr &store); - - static const std::string encryptedDatabaseName; - static const std::string encryptedDatabasePath; - static const std::string encryptedDatabaseKeyDir; - static const std::string encryptedDatabaseMockName; - static const std::string encryptedDatabaseMockPath; - static constexpr int HOURS_EXPIRED = (24 * 365) + 1; - static constexpr int HOURS_NOT_EXPIRED = (24 * 30); -}; - -const std::string RdbRekeyTest::encryptedDatabaseName = "encrypted.db"; -const std::string RdbRekeyTest::encryptedDatabasePath = RDB_TEST_PATH + encryptedDatabaseName; -const std::string RdbRekeyTest::encryptedDatabaseKeyDir = RDB_TEST_PATH + "key/"; -const std::string RdbRekeyTest::encryptedDatabaseMockName = "encrypted_mock.db"; -const std::string RdbRekeyTest::encryptedDatabaseMockPath = RDB_TEST_PATH + encryptedDatabaseMockName; - -class RekeyTestOpenCallback : public RdbOpenCallback { -public: - int OnCreate(RdbStore &store) override; - int OnUpgrade(RdbStore &store, int oldVersion, int newVersion) override; - static const std::string createTableTest; -}; - -std::string const RekeyTestOpenCallback::createTableTest = "CREATE TABLE IF NOT EXISTS test " - "(id INTEGER PRIMARY KEY " - "AUTOINCREMENT, " - "name TEXT NOT NULL, age INTEGER, " - "salary " - "REAL, blobType BLOB)"; - -int RekeyTestOpenCallback::OnCreate(RdbStore &store) -{ - return store.ExecuteSql(createTableTest); -} - -int RekeyTestOpenCallback::OnUpgrade(RdbStore &store, int oldVersion, int newVersion) -{ - return E_OK; -} - -void RdbRekeyTest::SetUpTestCase() {} - -void RdbRekeyTest::TearDownTestCase() {} - -void RdbRekeyTest::SetUp() -{ - RdbStoreConfig config = GetRdbConfig(encryptedDatabasePath); - RekeyTestOpenCallback helper; - int errCode; - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - EXPECT_NE(store, nullptr); - InsertData(store); - store.reset(); - RdbHelper::ClearCache(); -} - -void RdbRekeyTest::TearDown() -{ - RdbHelper::ClearCache(); - RdbHelper::DeleteRdbStore(RdbRekeyTest::encryptedDatabasePath); -} - -std::string RdbRekeyTest::RemoveSuffix(const string &name) -{ - std::string suffix(".db"); - auto pos = name.rfind(suffix); - if (pos == std::string::npos || pos < name.length() - suffix.length()) { - return name; - } - return { name, 0, pos }; -} - -std::chrono::system_clock::time_point RdbRekeyTest::GetKeyFileDate(const std::string &dbName) -{ - std::chrono::system_clock::time_point timePoint; - std::string name = RemoveSuffix(dbName); - auto keyPath = RDB_TEST_PATH + "key/" + name + ".pub_key"; - if (!OHOS::FileExists(keyPath)) { - return timePoint; - } - std::vector content; - auto loaded = OHOS::LoadBufferFromFile(keyPath, content); - if (!loaded) { - return timePoint; - } - auto iter = content.begin(); - iter++; - constexpr uint32_t dateFileLength = sizeof(time_t) / sizeof(uint8_t); - std::vector date; - date.assign(iter, iter + dateFileLength); - timePoint = std::chrono::system_clock::from_time_t(*reinterpret_cast(const_cast(&date[0]))); - return timePoint; -} - -bool RdbRekeyTest::ChangeKeyFileDate(const std::string &dbName, int rep) -{ - std::string name = RemoveSuffix(dbName); - auto keyPath = RDB_TEST_PATH + "key/" + name + ".pub_key"; - if (!OHOS::FileExists(keyPath)) { - return false; - } - std::vector content; - auto loaded = OHOS::LoadBufferFromFile(keyPath, content); - if (!loaded) { - return false; - } - auto time = - std::chrono::system_clock::to_time_t(std::chrono::system_clock::system_clock::now() - std::chrono::hours(rep)); - std::vector date(reinterpret_cast(&time), reinterpret_cast(&time) + sizeof(time)); - std::copy(date.begin(), date.end(), ++content.begin()); - - auto saved = OHOS::SaveBufferToFile(keyPath, content); - return saved; -} - -RdbStoreConfig RdbRekeyTest::GetRdbConfig(const std::string &name) -{ - RdbStoreConfig config(name); - config.SetEncryptStatus(true); - config.SetBundleName("com.example.test_rekey"); - return config; -} - -void RdbRekeyTest::InsertData(std::shared_ptr &store) -{ - int64_t id; - ValuesBucket values; - std::string name = "zhangsan"; - int age = 18; - double salary = 100.5; - std::vector blob{ 1, 2, 3 }; - values.PutString("name", name); - values.PutInt("age", age); - values.PutDouble("salary", salary); - values.PutBlob("blobType", blob); - int insertRet = store->Insert(id, "test", values); - EXPECT_EQ(insertRet, E_OK); -} - -void RdbRekeyTest::CheckQueryData(std::shared_ptr &store) -{ - std::unique_ptr resultSet = - store->QuerySql("SELECT * FROM test WHERE name = ?", std::vector{ "zhangsan" }); - EXPECT_NE(resultSet, nullptr); - int result = resultSet->GoToFirstRow(); - EXPECT_EQ(result, E_OK); - int columnIndex; - std::string strVal; - ColumnType columnType; - result = resultSet->GetColumnIndex("name", columnIndex); - EXPECT_EQ(result, E_OK); - result = resultSet->GetColumnType(columnIndex, columnType); - EXPECT_EQ(result, E_OK); - EXPECT_EQ(columnType, ColumnType::TYPE_STRING); - result = resultSet->GetString(columnIndex, strVal); - EXPECT_EQ(result, E_OK); - EXPECT_EQ("zhangsan", strVal); - - result = resultSet->Close(); - EXPECT_EQ(result, E_OK); -} - -/** -* @tc.name: Rdb_Rekey_Test_001 -* @tc.desc: test RdbStore rekey function -* @tc.type: FUNC -*/ -HWTEST_F(RdbRekeyTest, Rdb_Rekey_01, TestSize.Level1) -{ - std::string keyPath = encryptedDatabaseKeyDir + RemoveSuffix(encryptedDatabaseName) + ".pub_key"; - std::string newKeyPath = encryptedDatabaseKeyDir + RemoveSuffix(encryptedDatabaseName) + ".pub_key.new"; - - bool isFileExists = OHOS::FileExists(keyPath); - ASSERT_TRUE(isFileExists); - - bool isFileDateChanged = ChangeKeyFileDate(encryptedDatabaseName, RdbRekeyTest::HOURS_EXPIRED); - ASSERT_TRUE(isFileDateChanged); - - auto changedDate = GetKeyFileDate(encryptedDatabaseName); - ASSERT_TRUE(std::chrono::system_clock::now() - changedDate > std::chrono::hours(RdbRekeyTest::HOURS_EXPIRED)); - - RdbStoreConfig config = GetRdbConfig(RdbRekeyTest::encryptedDatabasePath); - RekeyTestOpenCallback helper; - int errCode; - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - ASSERT_NE(store, nullptr); - - isFileExists = OHOS::FileExists(keyPath); - ASSERT_TRUE(isFileExists); - isFileExists = OHOS::FileExists(newKeyPath); - ASSERT_FALSE(isFileExists); - - auto newDate = GetKeyFileDate(encryptedDatabaseName); - ASSERT_TRUE(std::chrono::system_clock::now() - newDate < std::chrono::seconds(2)); - CheckQueryData(store); -} - -/** -* @tc.name: Rdb_Rekey_Test_002 -* @tc.desc: test RdbStore with not outdated password -* @tc.type: FUNC -*/ -HWTEST_F(RdbRekeyTest, Rdb_Rekey_02, TestSize.Level1) -{ - std::string keyPath = encryptedDatabaseKeyDir + RemoveSuffix(encryptedDatabaseName) + ".pub_key"; - bool isFileExists = OHOS::FileExists(keyPath); - ASSERT_TRUE(isFileExists); - - bool isFileDateChanged = ChangeKeyFileDate(encryptedDatabaseName, RdbRekeyTest::HOURS_NOT_EXPIRED); - ASSERT_TRUE(isFileDateChanged); - - auto changedDate = GetKeyFileDate(encryptedDatabaseName); - ASSERT_TRUE(std::chrono::system_clock::now() - changedDate > std::chrono::hours(RdbRekeyTest::HOURS_NOT_EXPIRED)); - - RdbStoreConfig config = GetRdbConfig(RdbRekeyTest::encryptedDatabasePath); - RekeyTestOpenCallback helper; - int errCode; - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - ASSERT_NE(store, nullptr); - CheckQueryData(store); -} - -/** -* @tc.name: Rdb_Rekey_Test_003 -* @tc.desc: try to open store and execute RekeyRecover() without key and new key files. -* @tc.type: FUNC -*/ -HWTEST_F(RdbRekeyTest, Rdb_Rekey_03, TestSize.Level1) -{ - std::string keyPath = encryptedDatabaseKeyDir + RemoveSuffix(encryptedDatabaseName) + ".pub_key"; - std::string newKeyPath = encryptedDatabaseKeyDir + RemoveSuffix(encryptedDatabaseName) + ".pub_key.new"; - - bool isFileExists = OHOS::FileExists(keyPath); - ASSERT_TRUE(isFileExists); - - SqliteDatabaseUtils::DeleteFile(keyPath); - isFileExists = OHOS::FileExists(keyPath); - ASSERT_FALSE(isFileExists); - isFileExists = OHOS::FileExists(newKeyPath); - ASSERT_FALSE(isFileExists); - - RekeyTestOpenCallback helper; - int errCode; - RdbStoreConfig config = GetRdbConfig(encryptedDatabasePath); - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - ASSERT_EQ(store, nullptr); -} - -/** -* @tc.name: Rdb_Rekey_Test_004 -* @tc.desc: try to open store and modify create date to a future time. -* @tc.type: FUNC -*/ -HWTEST_F(RdbRekeyTest, Rdb_Rekey_04, TestSize.Level1) -{ - std::string keyPath = encryptedDatabaseKeyDir + RemoveSuffix(encryptedDatabaseName) + ".pub_key"; - std::string newKeyPath = encryptedDatabaseKeyDir + RemoveSuffix(encryptedDatabaseName) + ".pub_key.new"; - - bool isFileExists = OHOS::FileExists(keyPath); - ASSERT_TRUE(isFileExists); - - auto keyFileDate = GetKeyFileDate(encryptedDatabaseName); - - bool isFileDateChanged = ChangeKeyFileDate(encryptedDatabaseName, -RdbRekeyTest::HOURS_EXPIRED); - ASSERT_TRUE(isFileDateChanged); - - auto changedDate = GetKeyFileDate(encryptedDatabaseName); - ASSERT_GT(changedDate, keyFileDate); - - RdbStoreConfig config = GetRdbConfig(RdbRekeyTest::encryptedDatabasePath); - RekeyTestOpenCallback helper; - int errCode; - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - ASSERT_NE(store, nullptr); - - isFileExists = OHOS::FileExists(keyPath); - ASSERT_TRUE(isFileExists); - isFileExists = OHOS::FileExists(newKeyPath); - ASSERT_FALSE(isFileExists); - - keyFileDate = GetKeyFileDate(encryptedDatabaseName); - ASSERT_EQ(changedDate, keyFileDate); - - CheckQueryData(store); -} \ No newline at end of file