• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef OMIT_ENCRYPT
16 #ifdef RELATIONAL_STORE
17 #include <gtest/gtest.h>
18 
19 #include "data_transformer.h"
20 #include "db_common.h"
21 #include "db_constant.h"
22 #include "db_errno.h"
23 #include "db_types.h"
24 #include "distributeddb_data_generate_unit_test.h"
25 #include "distributeddb_tools_unit_test.h"
26 #include "generic_single_ver_kv_entry.h"
27 #include "log_print.h"
28 #include "relational_store_delegate.h"
29 #include "relational_store_instance.h"
30 #include "relational_store_manager.h"
31 #include "relational_store_sqlite_ext.h"
32 #include "relational_sync_able_storage.h"
33 #include "sqlite_relational_store.h"
34 #include "sqlite_utils.h"
35 #include "virtual_communicator_aggregator.h"
36 
37 using namespace testing::ext;
38 using namespace DistributedDB;
39 using namespace DistributedDBUnitTest;
40 using namespace std;
41 
42 namespace {
43 string g_testDir;
44 string g_storePath;
45 string g_storeID = "dftStoreID";
46 string g_tableName = "data";
47 const string CORRECT_KEY = "a correct key";
48 CipherPassword g_correctPasswd;
49 const string REKEY_KEY = "a key after rekey";
50 CipherPassword g_rekeyPasswd;
51 const string INCORRECT_KEY = "a incorrect key";
52 CipherPassword g_incorrectPasswd;
53 const int DEFAULT_ITER = 5000;
54 const int CUSTOMIZED_ITER = 10000;
55 
56 DistributedDB::RelationalStoreManager g_mgr(APP_ID, USER_ID);
57 RelationalStoreDelegate *g_delegate = nullptr;
58 IRelationalStore *g_store = nullptr;
59 
InitStoreProp(const std::string & storePath,const std::string & appId,const std::string & userId,RelationalDBProperties & properties)60 void InitStoreProp(const std::string &storePath, const std::string &appId, const std::string &userId,
61     RelationalDBProperties &properties)
62 {
63     properties.SetStringProp(RelationalDBProperties::DATA_DIR, storePath);
64     properties.SetStringProp(RelationalDBProperties::APP_ID, appId);
65     properties.SetStringProp(RelationalDBProperties::USER_ID, userId);
66     properties.SetStringProp(RelationalDBProperties::STORE_ID, g_storeID);
67     std::string identifier = userId + "-" + appId + "-" + g_storeID;
68     std::string hashIdentifier = DBCommon::TransferHashString(identifier);
69     properties.SetStringProp(RelationalDBProperties::IDENTIFIER_DATA, hashIdentifier);
70 }
71 
GetRelationalStore()72 const RelationalSyncAbleStorage *GetRelationalStore()
73 {
74     RelationalDBProperties properties;
75     InitStoreProp(g_storePath, APP_ID, USER_ID, properties);
76     int errCode = E_OK;
77     g_store = RelationalStoreInstance::GetDataBase(properties, errCode);
78     if (g_store == nullptr) {
79         LOGE("Get db failed:%d", errCode);
80         return nullptr;
81     }
82     return static_cast<SQLiteRelationalStore *>(g_store)->GetStorageEngine();
83 }
84 
ExecSqlAndAssertOK(sqlite3 * db,const std::string & sql)85 void ExecSqlAndAssertOK(sqlite3 *db, const std::string &sql)
86 {
87     ASSERT_EQ(sqlite3_exec(db, sql.c_str(), nullptr, nullptr, nullptr), SQLITE_OK);
88 }
89 }
90 
91 class DistributedDBRelationalEncryptedDbTest : public testing::Test {
92 public:
93     static void SetUpTestCase(void);
94     static void TearDownTestCase(void);
95     void SetUp();
96     void TearDown();
97 };
98 
SetUpTestCase(void)99 void DistributedDBRelationalEncryptedDbTest::SetUpTestCase(void)
100 {
101     DistributedDBToolsUnitTest::TestDirInit(g_testDir);
102     g_storePath = g_testDir + "/getDataTest.db";
103     LOGI("The test db is:%s", g_testDir.c_str());
104 
105     auto communicator = new (std::nothrow) VirtualCommunicatorAggregator();
106     ASSERT_TRUE(communicator != nullptr);
107     RuntimeContext::GetInstance()->SetCommunicatorAggregator(communicator);
108 
109     g_correctPasswd.SetValue(reinterpret_cast<const uint8_t *>(CORRECT_KEY.data()), CORRECT_KEY.size());
110     g_rekeyPasswd.SetValue(reinterpret_cast<const uint8_t *>(REKEY_KEY.data()), REKEY_KEY.size());
111     g_incorrectPasswd.SetValue(reinterpret_cast<const uint8_t *>(INCORRECT_KEY.data()), INCORRECT_KEY.size());
112 }
113 
TearDownTestCase(void)114 void DistributedDBRelationalEncryptedDbTest::TearDownTestCase(void)
115 {
116     RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
117 }
118 
SetUp(void)119 void DistributedDBRelationalEncryptedDbTest::SetUp(void)
120 {
121     DistributedDBToolsUnitTest::PrintTestCaseInfo();
122 }
123 
TearDown(void)124 void DistributedDBRelationalEncryptedDbTest::TearDown(void)
125 {
126     if (g_delegate != nullptr) {
127         EXPECT_EQ(g_mgr.CloseStore(g_delegate), DBStatus::OK);
128         g_delegate = nullptr;
129     }
130     if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
131         LOGE("rm test db files error.");
132     }
133     return;
134 }
135 
136 /**
137  * @tc.name: OpenEncryptedDBWithoutPasswd_001
138  * @tc.desc: Open encrypted db without password in collaboration mode.
139  * @tc.type: FUNC
140  * @tc.require: AR000H68LL
141  * @tc.author: lidongwei
142  */
143 HWTEST_F(DistributedDBRelationalEncryptedDbTest, OpenEncryptedDBWithoutPasswdInCollMode_001, TestSize.Level1)
144 {
145     /**
146      * @tc.steps: step1. Create an encrypted db.
147      * @tc.expected: Succeed.
148      */
149     sqlite3 *db = nullptr;
150     ASSERT_EQ(sqlite3_open(g_storePath.c_str(), &db), SQLITE_OK);
151     string sql =
152         "PRAGMA key='" + CORRECT_KEY + "';"
153         "PRAGMA codec_kdf_iter=" + std::to_string(DEFAULT_ITER) + ";"
154         "PRAGMA codec_hmac_algo=SHA256;"
155         "PRAGMA codec_rekey_hmac_algo=SHA256;"
156         "PRAGMA journal_mode=WAL;"
157         "CREATE TABLE " + g_tableName + "(key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, value INTEGER);";
158     ExecSqlAndAssertOK(db, sql);
159 
160     /**
161      * @tc.steps: step2. Open store.
162      * @tc.expected: Failed, return INVALID_PASSWD_OR_CORRUPTED_DB.
163      */
164     RelationalStoreDelegate::Option option { nullptr };
165     EXPECT_EQ(g_mgr.OpenStore(g_storePath, g_storeID, option, g_delegate), DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB);
166     EXPECT_EQ(g_delegate, nullptr);
167     sqlite3_close(db);
168 }
169 
170 /**
171  * @tc.name: OpenEncryptedDBWithoutPasswdInSplitMode_001
172  * @tc.desc: Open encrypted db without password in split mode.
173  * @tc.type: FUNC
174  * @tc.require: AR000H68LL
175  * @tc.author: lidongwei
176  */
177 HWTEST_F(DistributedDBRelationalEncryptedDbTest, OpenEncryptedDBWithoutPasswdInSplitMode_001, TestSize.Level1)
178 {
179     /**
180      * @tc.steps: step1. Create an encrypted db.
181      * @tc.expected: Succeed.
182      */
183     sqlite3 *db = nullptr;
184     ASSERT_EQ(sqlite3_open(g_storePath.c_str(), &db), SQLITE_OK);
185     string sql =
186         "PRAGMA key='" + CORRECT_KEY + "';"
187         "PRAGMA codec_kdf_iter=" + std::to_string(DEFAULT_ITER) + ";"
188         "PRAGMA codec_hmac_algo=SHA256;"
189         "PRAGMA codec_rekey_hmac_algo=SHA256;"
190         "PRAGMA journal_mode=WAL;"
191         "CREATE TABLE " + g_tableName + "(key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, value INTEGER);";
192     ExecSqlAndAssertOK(db, sql);
193 
194     /**
195      * @tc.steps: step2. Open store.
196      * @tc.expected: Failed, return INVALID_PASSWD_OR_CORRUPTED_DB.
197      */
198     RelationalStoreDelegate::Option option { nullptr };
199     EXPECT_EQ(g_mgr.OpenStore(g_storePath, g_storeID, option, g_delegate), DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB);
200     EXPECT_EQ(g_delegate, nullptr);
201     sqlite3_close(db);
202 }
203 
204 /**
205  * @tc.name: OpenEncryptedDBWithPasswdInSplitMode_001
206  * @tc.desc: Open encrypted db with password in split mode.
207  * @tc.type: FUNC
208  * @tc.require: AR000H68LL
209  * @tc.author: lidongwei
210  */
211 HWTEST_F(DistributedDBRelationalEncryptedDbTest, OpenEncryptedDBWithPasswdInSplitMode_001, TestSize.Level1)
212 {
213     /**
214      * @tc.steps: step1. Create an encrypted db.
215      * @tc.expected: Succeed.
216      */
217     sqlite3 *db = nullptr;
218     ASSERT_EQ(sqlite3_open(g_storePath.c_str(), &db), SQLITE_OK);
219     string sql =
220         "PRAGMA key='" + CORRECT_KEY + "';"
221         "PRAGMA codec_kdf_iter=" + std::to_string(DEFAULT_ITER) + ";"
222         "PRAGMA codec_hmac_algo=SHA256;"
223         "PRAGMA codec_rekey_hmac_algo=SHA256;"
224         "PRAGMA journal_mode=WAL;"
225         "CREATE TABLE " + g_tableName + "(key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, value INTEGER);";
226     ExecSqlAndAssertOK(db, sql);
227 
228     /**
229      * @tc.steps: step2. Open store.
230      * @tc.expected: Succeed, return OK.
231      */
232     RelationalStoreDelegate::Option option {
233         nullptr, false, true, CipherType::DEFAULT, g_correctPasswd, DEFAULT_ITER };
234     EXPECT_EQ(g_mgr.OpenStore(g_storePath, g_storeID, option, g_delegate), DBStatus::OK);
235     ASSERT_NE(g_delegate, nullptr);
236     ASSERT_EQ(g_delegate->CreateDistributedTable(g_tableName), DBStatus::OK);
237 
238     /**
239      * @tc.steps: step3. Insert several data.
240      * @tc.expected: Succeed, return OK.
241      */
242     ExecSqlAndAssertOK(db, "INSERT OR REPLACE INTO " + g_tableName + " VALUES(1, 1);");
243     ExecSqlAndAssertOK(db, "INSERT OR REPLACE INTO " + g_tableName + " VALUES(2, 2);");
244     ExecSqlAndAssertOK(db, "INSERT OR REPLACE INTO " + g_tableName + " VALUES(3, 3);");
245 
246     /**
247      * @tc.steps: step4. Get sync data.
248      * @tc.expected: Succeed, get 3 data.
249      */
250     auto store = GetRelationalStore();
251     ASSERT_NE(store, nullptr);
252     ContinueToken token = nullptr;
253     QueryObject query(Query::Select(g_tableName));
254     std::vector<SingleVerKvEntry *> entries;
255     EXPECT_EQ(store->GetSyncData(query, SyncTimeRange {}, DataSizeSpecInfo {}, token, entries), E_OK);
256     EXPECT_EQ(entries.size(), 3u);
257 
258     sqlite3_close(db);
259     RefObject::DecObjRef(g_store);
260 }
261 
262 /**
263  * @tc.name: OpenEncryptedDBWithInvalidParameters_001
264  * @tc.desc: Open encrypted db with invalid parameters in split mode.
265  * @tc.type: FUNC
266  * @tc.require: AR000H68LL
267  * @tc.author: lidongwei
268  */
269 HWTEST_F(DistributedDBRelationalEncryptedDbTest, OpenEncryptedDBWithInvalidParameters_001, TestSize.Level1)
270 {
271     /**
272      * @tc.steps: step1. Create an encrypted db.
273      * @tc.expected: Succeed.
274      */
275     sqlite3 *db = nullptr;
276     ASSERT_EQ(sqlite3_open(g_storePath.c_str(), &db), SQLITE_OK);
277     string sql =
278         "PRAGMA key='" + CORRECT_KEY + "';"
279         "PRAGMA codec_kdf_iter=" + std::to_string(DEFAULT_ITER) + ";"
280         "PRAGMA codec_hmac_algo=SHA256;"
281         "PRAGMA codec_rekey_hmac_algo=SHA256;"
282         "PRAGMA journal_mode=WAL;"
283         "CREATE TABLE " + g_tableName + "(key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, value INTEGER);";
284     ExecSqlAndAssertOK(db, sql);
285 
286     /**
287      * @tc.steps: step2. Open store with invalid password.
288      * @tc.expected: Failed, return INVALID_ARGS.
289      */
290     RelationalStoreDelegate::Option option1 {
291         nullptr, false, true, CipherType::DEFAULT, CipherPassword {}, DEFAULT_ITER };
292     EXPECT_EQ(g_mgr.OpenStore(g_storePath, g_storeID, option1, g_delegate), DBStatus::INVALID_ARGS);
293     ASSERT_EQ(g_delegate, nullptr);
294 
295     /**
296      * @tc.steps: step3. Open store with invalid password.
297      * @tc.expected: Failed, return INVALID_ARGS.
298      */
299     RelationalStoreDelegate::Option option2 {
300         nullptr, false, true, CipherType::DEFAULT, g_correctPasswd, 0u };
301     EXPECT_EQ(g_mgr.OpenStore(g_storePath, g_storeID, option2, g_delegate), DBStatus::INVALID_ARGS);
302     ASSERT_EQ(g_delegate, nullptr);
303     sqlite3_close(db);
304 }
305 
306 /**
307  * @tc.name: OpenEncryptedDBWithCustomizedIterTimes_001
308  * @tc.desc: Open encrypted db with customized iterate times.
309  * @tc.type: FUNC
310  * @tc.require: AR000H68LL
311  * @tc.author: lidongwei
312  */
313 HWTEST_F(DistributedDBRelationalEncryptedDbTest, OpenEncryptedDBWithCustomizedIterTimes_001, TestSize.Level1)
314 {
315     /**
316      * @tc.steps: step1. Create an encrypted db.
317      * @tc.expected: Succeed.
318      */
319     sqlite3 *db = nullptr;
320     ASSERT_EQ(sqlite3_open(g_storePath.c_str(), &db), SQLITE_OK);
321     string sql =
322         "PRAGMA key='" + CORRECT_KEY + "';"
323         "PRAGMA codec_kdf_iter=" + std::to_string(CUSTOMIZED_ITER) + ";"
324         "PRAGMA codec_hmac_algo=SHA256;"
325         "PRAGMA codec_rekey_hmac_algo=SHA256;"
326         "PRAGMA journal_mode=WAL;"
327         "CREATE TABLE " + g_tableName + "(key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, value INTEGER);";
328     ExecSqlAndAssertOK(db, sql);
329 
330     /**
331      * @tc.steps: step2. Open store.
332      * @tc.expected: Succeed, return OK.
333      */
334     RelationalStoreDelegate::Option option1 {
335         nullptr, false, true, CipherType::DEFAULT, g_correctPasswd, CUSTOMIZED_ITER };
336     EXPECT_EQ(g_mgr.OpenStore(g_storePath, g_storeID, option1, g_delegate), DBStatus::OK);
337     ASSERT_NE(g_delegate, nullptr);
338     ASSERT_EQ(g_delegate->CreateDistributedTable(g_tableName), DBStatus::OK);
339 
340     /**
341      * @tc.steps: step3. Insert several data.
342      * @tc.expected: Succeed, return OK.
343      */
344     ExecSqlAndAssertOK(db, "INSERT OR REPLACE INTO " + g_tableName + " VALUES(1, 1);");
345     ExecSqlAndAssertOK(db, "INSERT OR REPLACE INTO " + g_tableName + " VALUES(2, 2);");
346     ExecSqlAndAssertOK(db, "INSERT OR REPLACE INTO " + g_tableName + " VALUES(3, 3);");
347 
348     /**
349      * @tc.steps: step4. Get sync data.
350      * @tc.expected: Succeed, get 3 data.
351      */
352     auto store = GetRelationalStore();
353     ASSERT_NE(store, nullptr);
354     ContinueToken token = nullptr;
355     QueryObject query(Query::Select(g_tableName));
356     std::vector<SingleVerKvEntry *> entries;
357     EXPECT_EQ(store->GetSyncData(query, SyncTimeRange {}, DataSizeSpecInfo {}, token, entries), E_OK);
358     EXPECT_EQ(entries.size(), 3u);
359 
360     sqlite3_close(db);
361     RefObject::DecObjRef(g_store);
362 }
363 
364 /**
365  * @tc.name: RekeyAfterOpenStore_001
366  * @tc.desc: Rekey after open store.
367  * @tc.type: FUNC
368  * @tc.require: AR000H68LL
369  * @tc.author: lidongwei
370  */
371 HWTEST_F(DistributedDBRelationalEncryptedDbTest, RekeyAfterOpenStore_001, TestSize.Level1)
372 {
373     /**
374      * @tc.steps: step1. Create an encrypted db.
375      * @tc.expected: Succeed.
376      */
377     sqlite3 *db = nullptr;
378     ASSERT_EQ(sqlite3_open(g_storePath.c_str(), &db), SQLITE_OK);
379     string sql =
380         "PRAGMA key='" + CORRECT_KEY + "';"
381         "PRAGMA codec_kdf_iter=" + std::to_string(CUSTOMIZED_ITER) + ";"
382         "PRAGMA codec_hmac_algo=SHA256;"
383         "PRAGMA codec_rekey_hmac_algo=SHA256;"
384         "PRAGMA journal_mode=WAL;"
385         "CREATE TABLE " + g_tableName + "(key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, value INTEGER);";
386     ExecSqlAndAssertOK(db, sql);
387 
388     /**
389      * @tc.steps: step2. Open store.
390      * @tc.expected: Succeed, return OK.
391      */
392     RelationalStoreDelegate::Option option1 {
393         nullptr, false, true, CipherType::DEFAULT, g_correctPasswd, CUSTOMIZED_ITER };
394     EXPECT_EQ(g_mgr.OpenStore(g_storePath, g_storeID, option1, g_delegate), DBStatus::OK);
395     ASSERT_NE(g_delegate, nullptr);
396     ASSERT_EQ(g_delegate->CreateDistributedTable(g_tableName), DBStatus::OK);
397 
__anond147210d0202null398     std::thread t1([db] {
399         std::string sql = "PARGMA rekey=" + REKEY_KEY;
400         EXPECT_EQ(sqlite3_rekey(db, REKEY_KEY.data(), REKEY_KEY.size()), SQLITE_OK);
401     });
402     t1.join();
403 
404     /**
405      * @tc.steps: step3. Get sync data.
406      * @tc.expected: Failed.
407      */
408     auto store = GetRelationalStore();
409     ASSERT_NE(store, nullptr);
410     ContinueToken token = nullptr;
411     QueryObject query(Query::Select(g_tableName));
412     std::vector<SingleVerKvEntry *> entries;
413     EXPECT_NE(store->GetSyncData(query, SyncTimeRange {}, DataSizeSpecInfo {}, token, entries), E_OK);
414 
415     RefObject::DecObjRef(g_store);
416     g_mgr.CloseStore(g_delegate);
417     g_delegate = nullptr;
418 
419     /**
420      * @tc.steps: step4. Open store with new key.
421      * @tc.expected: Succeed.
422      */
423     option1.passwd = g_rekeyPasswd;
424     EXPECT_EQ(g_mgr.OpenStore(g_storePath, g_storeID, option1, g_delegate), DBStatus::OK);
425     ASSERT_NE(g_delegate, nullptr);
426     ASSERT_EQ(g_delegate->CreateDistributedTable(g_tableName), DBStatus::OK);
427 
428     /**
429      * @tc.steps: step5. Get sync data.
430      * @tc.expected: Succeed.
431      */
432     store = GetRelationalStore();
433     ASSERT_NE(store, nullptr);
434     EXPECT_EQ(store->GetSyncData(query, SyncTimeRange {}, DataSizeSpecInfo {}, token, entries), E_OK);
435 
436     sqlite3_close(db);
437     RefObject::DecObjRef(g_store);
438 }
439 
440 /**
441  * @tc.name: OpenEncryptedDBWithDifferentPasswd_001
442  * @tc.desc: Open encrypted db with different password in split mode.
443  * @tc.type: FUNC
444  * @tc.require: AR000H68LL
445  * @tc.author: lidongwei
446  */
447 HWTEST_F(DistributedDBRelationalEncryptedDbTest, OpenEncryptedDBWithDifferentPasswd_001, TestSize.Level1)
448 {
449     /**
450      * @tc.steps: step1. Create an encrypted db.
451      * @tc.expected: Succeed.
452      */
453     sqlite3 *db = nullptr;
454     ASSERT_EQ(sqlite3_open(g_storePath.c_str(), &db), SQLITE_OK);
455     string sql =
456         "PRAGMA key='" + CORRECT_KEY + "';"
457         "PRAGMA codec_kdf_iter=" + std::to_string(DEFAULT_ITER) + ";"
458         "PRAGMA journal_mode=WAL;"
459         "CREATE TABLE " + g_tableName + "(key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, value INTEGER);";
460     ExecSqlAndAssertOK(db, sql);
461 
462     /**
463      * @tc.steps: step2. Open store.
464      * @tc.expected: Succeed, return OK.
465      */
466     RelationalStoreDelegate::Option option {
467         nullptr, false, true, CipherType::DEFAULT, g_correctPasswd, DEFAULT_ITER };
468     EXPECT_EQ(g_mgr.OpenStore(g_storePath, g_storeID, option, g_delegate), DBStatus::OK);
469     ASSERT_NE(g_delegate, nullptr);
470 
471     /**
472      * @tc.steps: step3. Open store with different key or iter times.
473      * @tc.expected: Failed, return INVALID_PASSWD_OR_CORRUPTED_DB.
474      */
475     RelationalStoreDelegate *delegate = nullptr;
476     option = { nullptr, false, true, CipherType::DEFAULT, g_incorrectPasswd, DEFAULT_ITER };
477     EXPECT_EQ(g_mgr.OpenStore(g_storePath, g_storeID, option, delegate), DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB);
478     EXPECT_EQ(delegate, nullptr);
479 
480     option = { nullptr, false, true, CipherType::DEFAULT, g_incorrectPasswd, CUSTOMIZED_ITER };
481     EXPECT_EQ(g_mgr.OpenStore(g_storePath, g_storeID, option, delegate), DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB);
482     EXPECT_EQ(delegate, nullptr);
483 
484     option = { nullptr, false, false, CipherType::DEFAULT, g_correctPasswd, DEFAULT_ITER };
485     EXPECT_EQ(g_mgr.OpenStore(g_storePath, g_storeID, option, delegate), DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB);
486     EXPECT_EQ(delegate, nullptr);
487 
488     /**
489      * @tc.steps: step4. Open store with different cipher.
490      * @tc.expected: Succeed, return OK.
491      */
492     option = { nullptr, false, true, CipherType::AES_256_GCM, g_correctPasswd, DEFAULT_ITER };
493     EXPECT_EQ(g_mgr.OpenStore(g_storePath, g_storeID, option, delegate), DBStatus::OK);
494     EXPECT_NE(delegate, nullptr);
495 
496     EXPECT_EQ(g_mgr.CloseStore(delegate), DBStatus::OK);
497     sqlite3_close(db);
498 }
499 
500 /**
501  * @tc.name: RemoteQueryForEncryptedDb_001
502  * @tc.desc: Exec remote query on encrypted db.
503  * @tc.type: FUNC
504  * @tc.require: AR000H68LL
505  * @tc.author: lidongwei
506  */
507 HWTEST_F(DistributedDBRelationalEncryptedDbTest, RemoteQueryForEncryptedDb_001, TestSize.Level1)
508 {
509     /**
510      * @tc.steps: step1. Create an encrypted db.
511      * @tc.expected: Succeed.
512      */
513     sqlite3 *db = nullptr;
514     ASSERT_EQ(sqlite3_open(g_storePath.c_str(), &db), SQLITE_OK);
515     string sql =
516         "PRAGMA key='" + CORRECT_KEY + "';"
517         "PRAGMA codec_kdf_iter=" + std::to_string(DEFAULT_ITER) + ";"
518         "PRAGMA codec_hmac_algo=SHA256;"
519         "PRAGMA codec_rekey_hmac_algo=SHA256;"
520         "PRAGMA journal_mode=WAL;"
521         "CREATE TABLE " + g_tableName + "(key INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, value INTEGER);";
522     ExecSqlAndAssertOK(db, sql);
523 
524     /**
525      * @tc.steps: step2. Open store and create distributed table.
526      * @tc.expected: Succeed, return OK.
527      */
528     RelationalStoreDelegate::Option option {
529         nullptr, false, true, CipherType::DEFAULT, g_correctPasswd, DEFAULT_ITER };
530     EXPECT_EQ(g_mgr.OpenStore(g_storePath, g_storeID, option, g_delegate), DBStatus::OK);
531     ASSERT_NE(g_delegate, nullptr);
532     ASSERT_EQ(g_delegate->CreateDistributedTable(g_tableName), DBStatus::OK);
533 
534     /**
535      * @tc.steps: step3. Remote query.
536      * @tc.expected: Return not INVALID_PASSWD_OR_CORRUPTED_DB.
537      */
538     int invalidTime = 1000; // 1000 for test.
539     std::shared_ptr<ResultSet> result = nullptr;
540     EXPECT_NE(g_delegate->RemoteQuery("deviceA", RemoteCondition {}, invalidTime, result),
541         DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB);
542 
543     /**
544      * @tc.steps: step4. Rekey.
545      */
__anond147210d0302null546     std::thread t1([db] {
547         std::string sql = "PARGMA rekey=" + REKEY_KEY;
548         EXPECT_EQ(sqlite3_rekey(db, REKEY_KEY.data(), REKEY_KEY.size()), SQLITE_OK);
549     });
550     t1.join();
551 
552     /**
553      * @tc.steps: step5. Remote query.
554      * @tc.expected: Return INVALID_PASSWD_OR_CORRUPTED_DB.
555      */
556     EXPECT_EQ(g_delegate->RemoteQuery("deviceA", RemoteCondition {}, invalidTime, result),
557         DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB);
558     sqlite3_close(db);
559 }
560 #endif  // RELATIONAL_STORE
561 #endif  // OMIT_ENCRYPT
562