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 
16 #include <gtest/gtest.h>
17 #include <queue>
18 #include <random>
19 
20 #include "db_common.h"
21 #include "distributeddb_data_generate_unit_test.h"
22 #include "distributeddb_tools_unit_test.h"
23 #include "log_print.h"
24 #include "platform_specific.h"
25 #include "relational_store_manager.h"
26 #include "relational_store_sqlite_ext.h"
27 #include "relational_virtual_device.h"
28 #include "runtime_config.h"
29 #ifdef DB_DEBUG_ENV
30 #include "system_time.h"
31 #endif // DB_DEBUG_ENV
32 #include "virtual_relational_ver_sync_db_interface.h"
33 
34 using namespace testing::ext;
35 using namespace DistributedDB;
36 using namespace DistributedDBUnitTest;
37 using namespace std;
38 
39 namespace {
40 constexpr const char* DB_SUFFIX = ".db";
41 constexpr const char* STORE_ID = "Relational_Store_ID";
42 std::string g_testDir;
43 std::string g_dbDir;
44 DistributedDB::RelationalStoreManager g_mgr(APP_ID, USER_ID);
45 
46 const std::string DEVICE_A = "real_device";
47 const std::string DEVICE_B = "deviceB";
48 VirtualCommunicatorAggregator* g_communicatorAggregator = nullptr;
49 RelationalVirtualDevice *g_deviceB = nullptr;
50 
51 const std::string NORMAL_CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS sync_data(" \
52     "key         BLOB NOT NULL UNIQUE," \
53     "value       BLOB," \
54     "timestamp   INT  NOT NULL," \
55     "flag        INT  NOT NULL," \
56     "device      BLOB," \
57     "ori_device  BLOB," \
58     "hash_key    BLOB PRIMARY KEY NOT NULL," \
59     "w_timestamp INT," \
60     "UNIQUE(device, ori_device));" \
61     "CREATE INDEX key_index ON sync_data(key, flag);";
62 
63 const std::string NORMAL_CREATE_NO_UNIQUE = "CREATE TABLE IF NOT EXISTS sync_data(" \
64     "key         BLOB NOT NULL," \
65     "value       BLOB," \
66     "timestamp   INT  NOT NULL," \
67     "flag        INT  NOT NULL," \
68     "device      BLOB," \
69     "ori_device  BLOB," \
70     "hash_key    BLOB PRIMARY KEY NOT NULL," \
71     "w_timestamp INT);" \
72     "CREATE INDEX key_index ON sync_data(key, flag);";
73 
74 const std::string SIMPLE_CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS t1(a INT, b TEXT)";
75 
76 const std::string CREATE_TABLE_SQL_NO_PRIMARY_KEY = "CREATE TABLE IF NOT EXISTS sync_data(" \
77     "key         BLOB NOT NULL UNIQUE," \
78     "value       BLOB," \
79     "timestamp   INT  NOT NULL," \
80     "flag        INT  NOT NULL," \
81     "device      BLOB," \
82     "ori_device  BLOB," \
83     "hash_key    BLOB NOT NULL," \
84     "w_timestamp INT," \
85     "UNIQUE(device, ori_device));" \
86     "CREATE INDEX key_index ON sync_data (key, flag);";
87 
88 const std::string CREATE_TABLE_SQL_NO_PRIMARY_KEY_NO_UNIQUE = "CREATE TABLE IF NOT EXISTS sync_data(" \
89     "key         BLOB NOT NULL," \
90     "value       BLOB," \
91     "timestamp   INT  NOT NULL," \
92     "flag        INT  NOT NULL," \
93     "device      BLOB," \
94     "ori_device  BLOB," \
95     "hash_key    BLOB NOT NULL," \
96     "w_timestamp INT);" \
97     "CREATE INDEX key_index ON sync_data (key, flag);";
98 
99 const std::string UNSUPPORTED_FIELD_TABLE_SQL = "CREATE TABLE IF NOT EXISTS test('$.ID' INT, val BLOB);";
100 
101 const std::string COMPOSITE_PRIMARY_KEY_TABLE_SQL = R"(CREATE TABLE workers (
102         worker_id INTEGER,
103         last_name VARCHAR NOT NULL,
104         first_name VARCHAR,
105         join_date DATE,
106         PRIMARY KEY (last_name, first_name)
107     );)";
108 
109 const std::string INSERT_SYNC_DATA_SQL = "INSERT OR REPLACE INTO sync_data (key, timestamp, flag, hash_key) "
110     "VALUES('KEY', 123456789, 1, 'HASH_KEY');";
111 
112 const std::string INVALID_TABLE_FIELD_SQL = "create table if not exists t1 ('1 = 1; --' int primary key, b blob)";
113 
114 
PrepareVirtualDeviceEnv(const std::string & tableName,const std::string & dbPath,const std::vector<RelationalVirtualDevice * > & remoteDeviceVec)115 void PrepareVirtualDeviceEnv(const std::string &tableName, const std::string &dbPath,
116     const std::vector<RelationalVirtualDevice *> &remoteDeviceVec)
117 {
118     sqlite3 *db = RelationalTestUtils::CreateDataBase(dbPath);
119     ASSERT_NE(db, nullptr);
120     TableInfo tableInfo;
121     SQLiteUtils::AnalysisSchema(db, tableName, tableInfo);
122     for (const auto &dev : remoteDeviceVec) {
123         std::vector<FieldInfo> fieldInfoList = tableInfo.GetFieldInfos();
124         dev->SetLocalFieldInfo(fieldInfoList);
125         dev->SetTableInfo(tableInfo);
126     }
127     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
128 }
129 
130 class DistributedDBInterfacesRelationalTest : public testing::Test {
131 public:
132     static void SetUpTestCase(void);
133     static void TearDownTestCase(void);
134     void SetUp();
135     void TearDown();
136 };
137 
SetUpTestCase(void)138 void DistributedDBInterfacesRelationalTest::SetUpTestCase(void)
139 {
140     DistributedDBToolsUnitTest::TestDirInit(g_testDir);
141     LOGD("Test dir is %s", g_testDir.c_str());
142     g_dbDir = g_testDir + "/";
143     DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir);
144 
145     g_communicatorAggregator = new (std::nothrow) VirtualCommunicatorAggregator();
146     ASSERT_TRUE(g_communicatorAggregator != nullptr);
147     RuntimeContext::GetInstance()->SetCommunicatorAggregator(g_communicatorAggregator);
148 }
149 
TearDownTestCase(void)150 void DistributedDBInterfacesRelationalTest::TearDownTestCase(void)
151 {
152 }
153 
SetUp(void)154 void DistributedDBInterfacesRelationalTest::SetUp(void)
155 {
156     DistributedDBToolsUnitTest::PrintTestCaseInfo();
157 
158     g_deviceB = new (std::nothrow) RelationalVirtualDevice(DEVICE_B);
159     ASSERT_TRUE(g_deviceB != nullptr);
160     auto *syncInterfaceB = new (std::nothrow) VirtualRelationalVerSyncDBInterface();
161     ASSERT_TRUE(syncInterfaceB != nullptr);
162     ASSERT_EQ(g_deviceB->Initialize(g_communicatorAggregator, syncInterfaceB), E_OK);
163     auto permissionCheckCallback = [] (const std::string &userId, const std::string &appId, const std::string &storeId,
164         const std::string &deviceId, uint8_t flag) -> bool {
165         return true;
166     };
167     EXPECT_EQ(RuntimeConfig::SetPermissionCheckCallback(permissionCheckCallback), OK);
168 }
169 
TearDown(void)170 void DistributedDBInterfacesRelationalTest::TearDown(void)
171 {
172     DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir);
173     if (g_deviceB != nullptr) {
174         delete g_deviceB;
175         g_deviceB = nullptr;
176     }
177     PermissionCheckCallbackV2 nullCallback;
178     EXPECT_EQ(RuntimeConfig::SetPermissionCheckCallback(nullCallback), OK);
179     if (g_communicatorAggregator != nullptr) {
180         g_communicatorAggregator->RegOnDispatch(nullptr);
181     }
182 }
183 
NoramlCreateDistributedTableTest(TableSyncType tableSyncType)184 void NoramlCreateDistributedTableTest(TableSyncType tableSyncType)
185 {
186     /**
187      * @tc.steps:step1. Prepare db file
188      * @tc.expected: step1. Return OK.
189      */
190     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
191     ASSERT_NE(db, nullptr);
192     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
193     if (tableSyncType == DistributedDB::DEVICE_COOPERATION) {
194         EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
195     } else {
196         EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
197     }
198 
199     RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_A");
200     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
201 
202     /**
203      * @tc.steps:step2. open relational store, create distributed table, close store
204      * @tc.expected: step2. Return OK.
205      */
206     RelationalStoreDelegate *delegate = nullptr;
207     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
208     EXPECT_EQ(status, OK);
209     ASSERT_NE(delegate, nullptr);
210 
211     status = delegate->CreateDistributedTable("sync_data", tableSyncType);
212     EXPECT_EQ(status, OK);
213 
214     // test create same table again
215     status = delegate->CreateDistributedTable("sync_data", tableSyncType);
216     EXPECT_EQ(status, OK);
217 
218     status = g_mgr.CloseStore(delegate);
219     EXPECT_EQ(status, OK);
220 
221     /**
222      * @tc.steps:step3. drop sync_data table
223      * @tc.expected: step3. Return OK.
224      */
225     db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
226     ASSERT_NE(db, nullptr);
227     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "drop table sync_data;"), SQLITE_OK);
228     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
229 
230     /**
231      * @tc.steps:step4. open again, check auxiliary should be delete
232      * @tc.expected: step4. Return OK.
233      */
234     delegate = nullptr;
235     status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
236     EXPECT_EQ(status, OK);
237     ASSERT_NE(delegate, nullptr);
238     status = g_mgr.CloseStore(delegate);
239     EXPECT_EQ(status, OK);
240 }
241 
242 /**
243   * @tc.name: RelationalStoreTest001
244   * @tc.desc: Test open store and create distributed db with DEVICE_COOPERATION type
245   * @tc.type: FUNC
246   * @tc.require: AR000GK58F
247   * @tc.author: lianhuix
248   */
249 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest001, TestSize.Level1)
250 {
251     NoramlCreateDistributedTableTest(DistributedDB::DEVICE_COOPERATION);
252 }
253 
254 /**
255   * @tc.name: RelationalStoreTest001
256   * @tc.desc: Test open store and create distributed db with CLOUD_COOPERATION type
257   * @tc.type: FUNC
258   * @tc.require: AR000GK58F
259   * @tc.author: lianhuix
260   */
261 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest001_1, TestSize.Level1)
262 {
263     NoramlCreateDistributedTableTest(DistributedDB::CLOUD_COOPERATION);
264 }
265 
266 /**
267   * @tc.name: RelationalStoreTest002
268   * @tc.desc: Test open store with invalid path or store ID
269   * @tc.type: FUNC
270   * @tc.require: AR000GK58F
271   * @tc.author: lianhuix
272   */
273 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest002, TestSize.Level1)
274 {
275     /**
276      * @tc.steps:step1. Prepare db file
277      * @tc.expected: step1. Return OK.
278      */
279     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
280     ASSERT_NE(db, nullptr);
281     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
282     EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
283     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
284 
285     /**
286      * @tc.steps:step2. Test open store with invalid path or store ID
287      * @tc.expected: step2. open store failed.
288      */
289     RelationalStoreDelegate *delegate = nullptr;
290 
291     // test open store with path not exist
292     DBStatus status = g_mgr.OpenStore(g_dbDir + "tmp/" + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
293     EXPECT_NE(status, OK);
294     ASSERT_EQ(delegate, nullptr);
295 
296     // test open store with empty store_id
297     status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, {}, {}, delegate);
298     EXPECT_NE(status, OK);
299     ASSERT_EQ(delegate, nullptr);
300 
301     // test open store with path has invalid character
302     status = g_mgr.OpenStore(g_dbDir + "t&m$p/" + STORE_ID + DB_SUFFIX, {}, {}, delegate);
303     EXPECT_NE(status, OK);
304     ASSERT_EQ(delegate, nullptr);
305 
306     // test open store with store_id has invalid character
307     status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, "Relation@al_S$tore_ID", {}, delegate);
308     EXPECT_NE(status, OK);
309     ASSERT_EQ(delegate, nullptr);
310 
311     // test open store with store_id length over MAX_STORE_ID_LENGTH
312     status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX,
313         std::string(DBConstant::MAX_STORE_ID_LENGTH + 1, 'a'), {}, delegate);
314     EXPECT_NE(status, OK);
315     ASSERT_EQ(delegate, nullptr);
316 }
317 
318 /**
319   * @tc.name: RelationalStoreTest003
320   * @tc.desc: Test open store with journal_mode is not WAL
321   * @tc.type: FUNC
322   * @tc.require: AR000GK58F
323   * @tc.author: lianhuix
324   */
325 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest003, TestSize.Level1)
326 {
327     /**
328      * @tc.steps:step1. Prepare db file with string is not WAL
329      * @tc.expected: step1. Return OK.
330      */
331     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
332     ASSERT_NE(db, nullptr);
333     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=PERSIST;"), SQLITE_OK);
334     EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
335     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
336 
337     /**
338      * @tc.steps:step2. Test open store
339      * @tc.expected: step2. Open store failed.
340      */
341     RelationalStoreDelegate *delegate = nullptr;
342 
343     // test open store with journal mode is not WAL
344     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
345     EXPECT_NE(status, OK);
346     ASSERT_EQ(delegate, nullptr);
347 }
348 
CreateDistributedTableOverLimitTest(TableSyncType tableSyncTpe)349 void CreateDistributedTableOverLimitTest(TableSyncType tableSyncTpe)
350 {
351     /**
352      * @tc.steps:step1. Prepare db file with multiple tables
353      * @tc.expected: step1. Return OK.
354      */
355     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
356     ASSERT_NE(db, nullptr);
357     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
358     const int tableCount = DBConstant::MAX_DISTRIBUTED_TABLE_COUNT + 10; // 10: additional size for test abnormal scene
359     for (int i = 0; i < tableCount; i++) {
360         std::string sql = "CREATE TABLE TEST_" + std::to_string(i) + "(id INT PRIMARY KEY, value TEXT);";
361         EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK);
362     }
363     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
364 
365     RelationalStoreDelegate *delegate = nullptr;
366 
367     /**
368      * @tc.steps:step2. Open store and create multiple distributed table
369      * @tc.expected: step2. The tables in limited quantity were created successfully, the others failed.
370      */
371     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
372     EXPECT_EQ(status, OK);
373     ASSERT_NE(delegate, nullptr);
374 
375     for (int i = 0; i < tableCount; i++) {
376         if (i < DBConstant::MAX_DISTRIBUTED_TABLE_COUNT) {
377             EXPECT_EQ(delegate->CreateDistributedTable("TEST_" + std::to_string(i), tableSyncTpe), OK);
378         } else {
379             EXPECT_NE(delegate->CreateDistributedTable("TEST_" + std::to_string(i), tableSyncTpe), OK);
380         }
381     }
382 
383     /**
384      * @tc.steps:step3. Close store
385      * @tc.expected: step3. Return OK.
386      */
387     status = g_mgr.CloseStore(delegate);
388     EXPECT_EQ(status, OK);
389 }
390 
391 /**
392   * @tc.name: RelationalStoreTest004
393   * @tc.desc: Test create distributed table with over limit for DEVICE_COOPERATION type
394   * @tc.type: FUNC
395   * @tc.require: AR000GK58F
396   * @tc.author: lianhuix
397   */
398 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest004, TestSize.Level1)
399 {
400     CreateDistributedTableOverLimitTest(DistributedDB::DEVICE_COOPERATION);
401 }
402 
403 /**
404   * @tc.name: RelationalStoreTest004
405   * @tc.desc: Test create distributed table with over limit for CLOUD_COOPERATION type
406   * @tc.type: FUNC
407   * @tc.require:
408   * @tc.author: zhangshijie
409   */
410 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest004_1, TestSize.Level1)
411 {
412     CreateDistributedTableOverLimitTest(DistributedDB::CLOUD_COOPERATION);
413 }
414 
CreateDistributedTableInvalidArgsTest(TableSyncType tableSyncType)415 void CreateDistributedTableInvalidArgsTest(TableSyncType tableSyncType)
416 {
417     /**
418      * @tc.steps:step1. Prepare db file
419      * @tc.expected: step1. Return OK.
420      */
421     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
422     ASSERT_NE(db, nullptr);
423     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
424     EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
425 
426     /**
427      * @tc.steps:step2. Open store
428      * @tc.expected: step2. return OK
429      */
430     RelationalStoreDelegate *delegate = nullptr;
431     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
432     EXPECT_EQ(status, OK);
433     ASSERT_NE(delegate, nullptr);
434 
435     /**
436      * @tc.steps:step3. Create distributed table with invalid table name
437      * @tc.expected: step3. Create distributed table failed.
438      */
439     EXPECT_NE(delegate->CreateDistributedTable(DBConstant::SYSTEM_TABLE_PREFIX + "_tmp", tableSyncType), OK);
440 
441     EXPECT_EQ(delegate->CreateDistributedTable("Handle-J@^.", tableSyncType), INVALID_ARGS);
442     EXPECT_EQ(delegate->CreateDistributedTable("sync_data",
443         static_cast<TableSyncType>(DistributedDB::DEVICE_COOPERATION - 1)), INVALID_ARGS);
444     EXPECT_EQ(delegate->CreateDistributedTable("sync_data",
445         static_cast<TableSyncType>(DistributedDB::CLOUD_COOPERATION + 1)), INVALID_ARGS);
446 
447     EXPECT_EQ(RelationalTestUtils::ExecSql(db, INVALID_TABLE_FIELD_SQL), SQLITE_OK);
448     EXPECT_EQ(delegate->CreateDistributedTable("t1", tableSyncType), NOT_SUPPORT);
449 
450     /**
451      * @tc.steps:step4. Create distributed table temp table or not exist table
452      * @tc.expected: step4. Create distributed table failed.
453      */
454     EXPECT_EQ(delegate->CreateDistributedTable("child", tableSyncType), NOT_FOUND);
455     std::string tempTableSql = "CREATE TEMP TABLE child(x, y, z)";
456     EXPECT_EQ(RelationalTestUtils::ExecSql(db, tempTableSql), SQLITE_OK);
457     EXPECT_EQ(delegate->CreateDistributedTable("child", tableSyncType), NOT_FOUND);
458 
459     /**
460      * @tc.steps:step5. Close store
461      * @tc.expected: step5. Return OK.
462      */
463     status = g_mgr.CloseStore(delegate);
464     EXPECT_EQ(status, OK);
465     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
466 }
467 
468 /**
469   * @tc.name: RelationalStoreTest005
470   * @tc.desc: Test create distributed table with invalid table name or invalid table sync type
471   * @tc.type: FUNC
472   * @tc.require: AR000GK58F
473   * @tc.author: lianhuix
474   */
475 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest005, TestSize.Level1)
476 {
477     CreateDistributedTableInvalidArgsTest(DistributedDB::DEVICE_COOPERATION);
478 }
479 
480 /**
481   * @tc.name: RelationalStoreTest005
482   * @tc.desc: Test create distributed table with invalid table name or invalid table sync type
483   * @tc.type: FUNC
484   * @tc.require:
485   * @tc.author:
486   */
487 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest005_1, TestSize.Level1)
488 {
489     CreateDistributedTableInvalidArgsTest(DistributedDB::CLOUD_COOPERATION);
490 }
491 
CreateDistributedTableNonPrimaryKeyTest(TableSyncType tableSyncType)492 void CreateDistributedTableNonPrimaryKeyTest(TableSyncType tableSyncType)
493 {
494     /**
495      * @tc.steps:step1. Prepare db file
496      * @tc.expected: step1. Return OK.
497      */
498     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
499     ASSERT_NE(db, nullptr);
500     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
501     if (tableSyncType == DistributedDB::DEVICE_COOPERATION) {
502         EXPECT_EQ(RelationalTestUtils::ExecSql(db, CREATE_TABLE_SQL_NO_PRIMARY_KEY), SQLITE_OK);
503     } else {
504         EXPECT_EQ(RelationalTestUtils::ExecSql(db, CREATE_TABLE_SQL_NO_PRIMARY_KEY_NO_UNIQUE), SQLITE_OK);
505     }
506     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
507 
508     /**
509      * @tc.steps:step2. Open store
510      * @tc.expected: step2. return OK
511      */
512     RelationalStoreDelegate *delegate = nullptr;
513     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
514     EXPECT_EQ(status, OK);
515     ASSERT_NE(delegate, nullptr);
516 
517     /**
518      * @tc.steps:step3. Create distributed table with valid table name
519      * @tc.expected: step3. Create distributed table success.
520      */
521     EXPECT_EQ(delegate->CreateDistributedTable("sync_data", tableSyncType), OK);
522 
523     /**
524      * @tc.steps:step4. Close store
525      * @tc.expected: step4. Return OK.
526      */
527     status = g_mgr.CloseStore(delegate);
528     EXPECT_EQ(status, OK);
529     delegate = nullptr;
530 
531     status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
532     EXPECT_EQ(status, OK);
533     ASSERT_NE(delegate, nullptr);
534 
535     status = g_mgr.CloseStore(delegate);
536     EXPECT_EQ(status, OK);
537 }
538 
539 /**
540   * @tc.name: RelationalStoreTest006
541   * @tc.desc: Test create distributed table with non primary key schema
542   * @tc.type: FUNC
543   * @tc.require: AR000GK58F
544   * @tc.author: lianhuix
545   */
546 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest006, TestSize.Level1)
547 {
548     CreateDistributedTableNonPrimaryKeyTest(DistributedDB::DEVICE_COOPERATION);
549 }
550 
551 /**
552   * @tc.name: RelationalStoreTest006
553   * @tc.desc: Test create distributed table with non primary key schema
554   * @tc.type: FUNC
555   * @tc.require:
556   * @tc.author:
557   */
558 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest006_1, TestSize.Level1)
559 {
560     CreateDistributedTableNonPrimaryKeyTest(DistributedDB::CLOUD_COOPERATION);
561 }
562 
CreateDistributedTableInvalidFieldTest(TableSyncType tableSyncType)563 void CreateDistributedTableInvalidFieldTest(TableSyncType tableSyncType)
564 {
565     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
566     ASSERT_NE(db, nullptr);
567     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
568     EXPECT_EQ(RelationalTestUtils::ExecSql(db, UNSUPPORTED_FIELD_TABLE_SQL), SQLITE_OK);
569     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
570 
571     RelationalStoreDelegate *delegate = nullptr;
572     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
573     EXPECT_EQ(status, OK);
574     ASSERT_NE(delegate, nullptr);
575 
576     EXPECT_EQ(delegate->CreateDistributedTable("test", tableSyncType), NOT_SUPPORT);
577     status = g_mgr.CloseStore(delegate);
578     EXPECT_EQ(status, OK);
579 }
580 
581 /**
582   * @tc.name: RelationalStoreTest007
583   * @tc.desc: Test create distributed table with table has invalid field name
584   * @tc.type: FUNC
585   * @tc.require: AR000GK58F
586   * @tc.author: lianhuix
587   */
588 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest007, TestSize.Level1)
589 {
590     CreateDistributedTableInvalidFieldTest(DistributedDB::DEVICE_COOPERATION);
591 }
592 
593 /**
594   * @tc.name: RelationalStoreTest007
595   * @tc.desc: Test create distributed table with table has invalid field name
596   * @tc.type: FUNC
597   * @tc.require:
598   * @tc.author: zhangshijie
599   */
600 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest007_1, TestSize.Level1)
601 {
602     CreateDistributedTableInvalidFieldTest(DistributedDB::CLOUD_COOPERATION);
603 }
604 
CreateDistributedTableCompositePKTest(TableSyncType tableSyncType,int expectCode)605 void CreateDistributedTableCompositePKTest(TableSyncType tableSyncType, int expectCode)
606 {
607     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
608     ASSERT_NE(db, nullptr);
609     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
610     EXPECT_EQ(RelationalTestUtils::ExecSql(db, COMPOSITE_PRIMARY_KEY_TABLE_SQL), SQLITE_OK);
611     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
612 
613     RelationalStoreDelegate *delegate = nullptr;
614     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
615     EXPECT_EQ(status, OK);
616     ASSERT_NE(delegate, nullptr);
617 
618     EXPECT_EQ(delegate->CreateDistributedTable("workers", tableSyncType), expectCode);
619     status = g_mgr.CloseStore(delegate);
620     EXPECT_EQ(status, OK);
621 }
622 
623 /**
624   * @tc.name: RelationalStoreTest008
625   * @tc.desc: Test create distributed table with table has composite primary keys for DEVICE_COOPERATION
626   * @tc.type: FUNC
627   * @tc.require: AR000GK58F
628   * @tc.author: lianhuix
629   */
630 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest008, TestSize.Level1)
631 {
632     CreateDistributedTableCompositePKTest(DistributedDB::DEVICE_COOPERATION, NOT_SUPPORT);
633 }
634 
635 /**
636   * @tc.name: RelationalStoreTest008
637   * @tc.desc: Test create distributed table with table has composite primary keys for CLOUD_COOPERATION
638   * @tc.type: FUNC
639   * @tc.require:
640   * @tc.author:
641   */
642 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest008_1, TestSize.Level1)
643 {
644     CreateDistributedTableCompositePKTest(DistributedDB::CLOUD_COOPERATION, OK);
645 }
646 
CreateDistributedTableWithHistoryDataTest(TableSyncType tableSyncType)647 void CreateDistributedTableWithHistoryDataTest(TableSyncType tableSyncType)
648 {
649     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
650     ASSERT_NE(db, nullptr);
651     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
652     EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
653     EXPECT_EQ(RelationalTestUtils::ExecSql(db, INSERT_SYNC_DATA_SQL), SQLITE_OK);
654     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
655 
656     RelationalStoreDelegate *delegate = nullptr;
657     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
658     EXPECT_EQ(status, OK);
659     ASSERT_NE(delegate, nullptr);
660 
661     EXPECT_EQ(delegate->CreateDistributedTable("sync_data"), OK);
662     status = g_mgr.CloseStore(delegate);
663     EXPECT_EQ(status, OK);
664 }
665 
666 /**
667   * @tc.name: RelationalStoreTest009
668   * @tc.desc: Test create distributed table with table has history data for DEVICE_COOPERATION
669   * @tc.type: FUNC
670   * @tc.require: AR000GK58F
671   * @tc.author: lianhuix
672   */
673 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest009, TestSize.Level1)
674 {
675     CreateDistributedTableWithHistoryDataTest(DistributedDB::DEVICE_COOPERATION);
676 }
677 
678 /**
679   * @tc.name: RelationalStoreTest009
680   * @tc.desc: Test create distributed table with table has history data for CLOUD_COOPERATION
681   * @tc.type: FUNC
682   * @tc.require:
683   * @tc.author:
684   */
685 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest009_1, TestSize.Level1)
686 {
687     CreateDistributedTableWithHistoryDataTest(DistributedDB::CLOUD_COOPERATION);
688 }
689 
TableModifyTest(const std::string & modifySql,TableSyncType tableSyncType,DBStatus expect)690 void TableModifyTest(const std::string &modifySql, TableSyncType tableSyncType, DBStatus expect)
691 {
692     /**
693      * @tc.steps:step1. Prepare db file
694      * @tc.expected: step1. Return OK.
695      */
696     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
697     ASSERT_NE(db, nullptr);
698     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
699     if (tableSyncType == DistributedDB::DEVICE_COOPERATION) {
700         EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
701     } else {
702         EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
703     }
704 
705     RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_A");
706     RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_B");
707     RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_C");
708 
709     /**
710      * @tc.steps:step2. Open store
711      * @tc.expected: step2. return OK
712      */
713     RelationalStoreDelegate *delegate = nullptr;
714     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
715     EXPECT_EQ(status, OK);
716     ASSERT_NE(delegate, nullptr);
717 
718     /**
719      * @tc.steps:step3. Create distributed table
720      * @tc.expected: step3. Create distributed table OK.
721      */
722     EXPECT_EQ(delegate->CreateDistributedTable("sync_data", tableSyncType), OK);
723 
724     /**
725      * @tc.steps:step4. Upgrade table with modifySql
726      * @tc.expected: step4. return OK
727      */
728     EXPECT_EQ(RelationalTestUtils::ExecSql(db, modifySql), SQLITE_OK);
729 
730     /**
731      * @tc.steps:step5. Create distributed table again
732      * @tc.expected: step5. Create distributed table return expect.
733      */
734     EXPECT_EQ(delegate->CreateDistributedTable("sync_data", tableSyncType), expect);
735 
736     /**
737      * @tc.steps:step6. Close store
738      * @tc.expected: step6 Return OK.
739      */
740     status = g_mgr.CloseStore(delegate);
741     EXPECT_EQ(status, OK);
742     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
743 }
744 
745 /**
746   * @tc.name: RelationalTableModifyTest001
747   * @tc.desc: Test modify distributed table with compatible upgrade
748   * @tc.type: FUNC
749   * @tc.require: AR000GK58F
750   * @tc.author: lianhuix
751   */
752 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest001, TestSize.Level1)
753 {
754     TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field INTEGER NOT NULL DEFAULT 123;",
755         DistributedDB::DEVICE_COOPERATION, OK);
756 }
757 
758 /**
759   * @tc.name: RelationalTableModifyTest002
760   * @tc.desc: Test modify distributed table with incompatible upgrade
761   * @tc.type: FUNC
762   * @tc.require: AR000GK58F
763   * @tc.author: lianhuix
764   */
765 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest002, TestSize.Level1)
766 {
767     TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field INTEGER NOT NULL;",
768         DistributedDB::DEVICE_COOPERATION, SCHEMA_MISMATCH);
769 }
770 
771 /**
772   * @tc.name: RelationalTableModifyTest003
773   * @tc.desc: Test modify distributed table with incompatible upgrade
774   * @tc.type: FUNC
775   * @tc.require: AR000GK58F
776   * @tc.author: lianhuix
777   */
778 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest003, TestSize.Level1)
779 {
780     TableModifyTest("ALTER TABLE sync_data DROP COLUMN w_timestamp;",
781         DistributedDB::DEVICE_COOPERATION, SCHEMA_MISMATCH);
782 }
783 
784 /**
785   * @tc.name: RelationalTableModifyTest001
786   * @tc.desc: Test modify distributed table with compatible upgrade
787   * @tc.type: FUNC
788   * @tc.require:
789   * @tc.author: zhangshijie
790   */
791 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest001_1, TestSize.Level1)
792 {
793     TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field INTEGER NOT NULL DEFAULT 123;",
794         DistributedDB::CLOUD_COOPERATION, OK);
795 }
796 
797 /**
798   * @tc.name: RelationalTableModifyTest002
799   * @tc.desc: Test modify distributed table with incompatible upgrade
800   * @tc.type: FUNC
801   * @tc.require:
802   * @tc.author: zhangshijie
803   */
804 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest002_1, TestSize.Level1)
805 {
806     TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field INTEGER NOT NULL;",
807         DistributedDB::CLOUD_COOPERATION, SCHEMA_MISMATCH);
808 }
809 
810 /**
811   * @tc.name: RelationalTableModifyTest003
812   * @tc.desc: Test modify distributed table with incompatible upgrade
813   * @tc.type: FUNC
814   * @tc.require:
815   * @tc.author: zhangshijie
816   */
817 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest003_1, TestSize.Level1)
818 {
819     TableModifyTest("ALTER TABLE sync_data DROP COLUMN w_timestamp;",
820         DistributedDB::CLOUD_COOPERATION, SCHEMA_MISMATCH);
821 }
822 
UpgradeDistributedTableTest(TableSyncType tableSyncType)823 void UpgradeDistributedTableTest(TableSyncType tableSyncType)
824 {
825     /**
826      * @tc.steps:step1. Prepare db file
827      * @tc.expected: step1. Return OK.
828      */
829     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
830     ASSERT_NE(db, nullptr);
831     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
832     if (tableSyncType == DistributedDB::DEVICE_COOPERATION) {
833         EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
834     } else {
835         EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
836     }
837 
838     RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_A");
839     RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_B");
840     RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_C");
841 
842     /**
843      * @tc.steps:step2. Open store
844      * @tc.expected: step2. return OK
845      */
846     RelationalStoreDelegate *delegate = nullptr;
847     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
848     EXPECT_EQ(status, OK);
849     ASSERT_NE(delegate, nullptr);
850 
851     /**
852      * @tc.steps:step3. Create distributed table
853      * @tc.expected: step3. Create distributed table OK.
854      */
855     EXPECT_EQ(delegate->CreateDistributedTable("sync_data", tableSyncType), OK);
856 
857     /**
858      * @tc.steps:step4. Upgrade table
859      * @tc.expected: step4. return OK
860      */
861     std::string modifySql = "ALTER TABLE sync_data ADD COLUMN add_field INTEGER;";
862     EXPECT_EQ(RelationalTestUtils::ExecSql(db, modifySql), SQLITE_OK);
863     std::string indexSql = "CREATE INDEX add_index ON sync_data (add_field);";
864     EXPECT_EQ(RelationalTestUtils::ExecSql(db, indexSql), SQLITE_OK);
865     std::string deleteIndexSql = "DROP INDEX IF EXISTS key_index";
866     EXPECT_EQ(RelationalTestUtils::ExecSql(db, deleteIndexSql), SQLITE_OK);
867     EXPECT_EQ(RelationalTestUtils::ExecSql(db, INSERT_SYNC_DATA_SQL), SQLITE_OK);
868 
869     /**
870      * @tc.steps:step5. Create distributed table again
871      * @tc.expected: step5. Create distributed table return expect.
872      */
873     EXPECT_EQ(delegate->CreateDistributedTable("sync_data", tableSyncType), OK);
874 
875     /**
876      * @tc.steps:step6. Close store
877      * @tc.expected: step6 Return OK.
878      */
879     status = g_mgr.CloseStore(delegate);
880     EXPECT_EQ(status, OK);
881     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
882 }
883 
884 /**
885   * @tc.name: RelationalTableModifyTest004
886   * @tc.desc: Test upgrade distributed table with device table exists
887   * @tc.type: FUNC
888   * @tc.require: AR000GK58F
889   * @tc.author: lianhuix
890   */
891 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest004, TestSize.Level1)
892 {
893     UpgradeDistributedTableTest(DistributedDB::DEVICE_COOPERATION);
894 }
895 
896 /**
897   * @tc.name: RelationalTableModifyTest004
898   * @tc.desc: Test upgrade distributed table with device table exists
899   * @tc.type: FUNC
900   * @tc.require:
901   * @tc.author: zhangshijie
902   */
903 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest004_1, TestSize.Level1)
904 {
905     UpgradeDistributedTableTest(DistributedDB::CLOUD_COOPERATION);
906 }
907 
908 /**
909   * @tc.name: RelationalTableModifyTest005
910   * @tc.desc: Test modify distributed table with compatible upgrade
911   * @tc.type: FUNC
912   * @tc.require: AR000GK58F
913   * @tc.author: lianhuix
914   */
915 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest005, TestSize.Level1)
916 {
917     TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field STRING NOT NULL DEFAULT 'asdf';",
918         DistributedDB::DEVICE_COOPERATION, OK);
919 }
920 
921 /**
922   * @tc.name: RelationalTableModifyTest005
923   * @tc.desc: Test modify distributed table with compatible upgrade
924   * @tc.type: FUNC
925   * @tc.require:
926   * @tc.author: zhangshijie
927   */
928 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest005_1, TestSize.Level1)
929 {
930     TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field STRING NOT NULL DEFAULT 'asdf';",
931         DistributedDB::CLOUD_COOPERATION, OK);
932 }
933 
CheckTable(TableSyncType tableSyncType,RelationalStoreDelegate * delegate,sqlite3 * db)934 void CheckTable(TableSyncType tableSyncType, RelationalStoreDelegate *delegate, sqlite3 *db)
935 {
936     /**
937      * @tc.steps:step4. Create distributed table with a table with "UNIQUE"
938      * @tc.expected: step4. return OK or NOT_SUPPORT.
939      */
940     std::string tableName4 = "t4";
941     std::string createSql = "create table " + tableName4 + "(id int UNIQUE);";
942     createSql += "create table t4_1(id int primary key, value text UNIQUE, name int);";
943     createSql += "create table t4_2(id int primary key, value text UNIQUE , name int);";
944     createSql += "create table t4_3(id int primary key, value text UNIQUE );";
945     createSql += "create table t4_4(id int primary key, value text UNIQUE , name int);";
946     createSql += "create table t4_5(id int unique);";
947     createSql += "create table t4_6(id int primary key, value text UniqUe, name int);";
948     createSql += "create table t4_7(id int primary key, uniquekey text , name int);";
949     createSql += "create table t4_8(id int , name text, UNIQUE(id, name));";
950     createSql += "create table t4_9(id int , name text,UNIQUE(id, name));";
951     EXPECT_EQ(RelationalTestUtils::ExecSql(db, createSql), SQLITE_OK);
952     DBStatus expectCode;
953     if (tableSyncType == DEVICE_COOPERATION) {
954         expectCode = OK;
955     } else {
956         expectCode = NOT_SUPPORT;
957     }
958     EXPECT_EQ(delegate->CreateDistributedTable(tableName4, tableSyncType), expectCode);
959     EXPECT_EQ(delegate->CreateDistributedTable("t4_1", tableSyncType), expectCode);
960     EXPECT_EQ(delegate->CreateDistributedTable("t4_2", tableSyncType), expectCode);
961     EXPECT_EQ(delegate->CreateDistributedTable("t4_3", tableSyncType), expectCode);
962     EXPECT_EQ(delegate->CreateDistributedTable("t4_4", tableSyncType), expectCode);
963     EXPECT_EQ(delegate->CreateDistributedTable("t4_5", tableSyncType), expectCode);
964     EXPECT_EQ(delegate->CreateDistributedTable("t4_6", tableSyncType), expectCode);
965     EXPECT_EQ(delegate->CreateDistributedTable("t4_7", tableSyncType), OK);
966     EXPECT_EQ(delegate->CreateDistributedTable("t4_8", tableSyncType), expectCode);
967     EXPECT_EQ(delegate->CreateDistributedTable("t4_9", tableSyncType), expectCode);
968 }
969 
TableConstraintsCheck(TableSyncType tableSyncType,RelationalStoreDelegate * delegate,sqlite3 * db)970 void TableConstraintsCheck(TableSyncType tableSyncType, RelationalStoreDelegate *delegate, sqlite3 *db)
971 {
972     /**
973      * @tc.steps:step1. Create distributed table with a table with "CHECK" key word
974      * @tc.expected: step1. return NOT_SUPPORT or OK.
975      */
976     std::string tableName1 = "t1";
977     std::string createSql = "create table " + tableName1 + "(id int CHECK(id > 5));";
978     EXPECT_EQ(RelationalTestUtils::ExecSql(db, createSql), SQLITE_OK);
979     int expectCode = OK;
980     if (tableSyncType == DistributedDB::CLOUD_COOPERATION) {
981         expectCode = NOT_SUPPORT;
982     }
983     EXPECT_EQ(delegate->CreateDistributedTable(tableName1, tableSyncType), expectCode);
984 
985     /**
986      * @tc.steps:step2. Create distributed table with a table with foreign key
987      * @tc.expected: step2. return OK.
988      */
989     std::string tableName2 = "t2";
990     createSql = "create table " + tableName2 + "(name text, t1_id int, FOREIGN KEY (t1_id) REFERENCES " + tableName1 +
991                 " (id));";
992     EXPECT_EQ(RelationalTestUtils::ExecSql(db, createSql), SQLITE_OK);
993     EXPECT_EQ(delegate->CreateDistributedTable(tableName2, tableSyncType), OK);
994 
995     CheckTable(tableSyncType, delegate, db);
996 
997     /**
998      * @tc.steps:step3. Create distributed table with a table with "WITHOUT ROWID"
999      * @tc.expected: step3. return NOT_SUPPORT.
1000      */
1001     std::string tableName3 = "t3";
1002     createSql = "create table " + tableName3 + "(id int primary key) WITHOUT ROWID;";
1003     EXPECT_EQ(RelationalTestUtils::ExecSql(db, createSql), SQLITE_OK);
1004     EXPECT_EQ(delegate->CreateDistributedTable(tableName3, tableSyncType), NOT_SUPPORT);
1005 
1006     /**
1007      * @tc.steps:step5. Create distributed table with a table with primary key which is real
1008      * @tc.expected: step5. return OK or NOT_SUPPORT.
1009      */
1010     std::string tableName5 = "t5";
1011     createSql = "create table " + tableName5 + "(id REAL primary key);";
1012     EXPECT_EQ(RelationalTestUtils::ExecSql(db, createSql), SQLITE_OK);
1013     if (tableSyncType == DEVICE_COOPERATION) {
1014         expectCode = OK;
1015     } else {
1016         expectCode = NOT_SUPPORT;
1017     }
1018     EXPECT_EQ(delegate->CreateDistributedTable(tableName5, tableSyncType), expectCode);
1019 
1020     /**
1021      * @tc.steps:step6. Create distributed table with a table with primary key which is ASSET
1022      * @tc.expected: step6. return OK or NOT_SUPPORT.
1023      */
1024     std::string tableName6 = "t6";
1025     createSql = "create table " + tableName6 + "(id ASSET primary key);";
1026     EXPECT_EQ(RelationalTestUtils::ExecSql(db, createSql), SQLITE_OK);
1027     if (tableSyncType == DEVICE_COOPERATION) {
1028         expectCode = OK;
1029     } else {
1030         expectCode = NOT_SUPPORT;
1031     }
1032     EXPECT_EQ(delegate->CreateDistributedTable(tableName6, tableSyncType), expectCode);
1033 
1034     /**
1035      * @tc.steps:step7. Create distributed table with a table with primary key which is ASSETS
1036      * @tc.expected: step7. return OK or NOT_SUPPORT.
1037      */
1038     std::string tableName7 = "t7";
1039     createSql = "create table " + tableName7 + "(id assets primary key);";
1040     EXPECT_EQ(RelationalTestUtils::ExecSql(db, createSql), SQLITE_OK);
1041     if (tableSyncType == DEVICE_COOPERATION) {
1042         expectCode = OK;
1043     } else {
1044         expectCode = NOT_SUPPORT;
1045     }
1046     EXPECT_EQ(delegate->CreateDistributedTable(tableName7, tableSyncType), expectCode);
1047 }
1048 
TableConstraintsTest(TableSyncType tableSyncType)1049 void TableConstraintsTest(TableSyncType tableSyncType)
1050 {
1051     /**
1052      * @tc.steps:step1. Prepare db file
1053      * @tc.expected: step1. Return OK.
1054      */
1055     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1056     ASSERT_NE(db, nullptr);
1057     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1058 
1059     /**
1060      * @tc.steps:step2. Open store
1061      * @tc.expected: step2. return OK
1062      */
1063     RelationalStoreDelegate *delegate = nullptr;
1064     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1065     EXPECT_EQ(status, OK);
1066     ASSERT_NE(delegate, nullptr);
1067 
1068     /**
1069      * @tc.steps:step3. check constraints
1070      */
1071     TableConstraintsCheck(tableSyncType, delegate, db);
1072 
1073     /**
1074      * @tc.steps:step4. Close store
1075      * @tc.expected: step4 Return OK.
1076      */
1077     EXPECT_EQ(g_mgr.CloseStore(delegate), OK);
1078     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1079 }
1080 
1081 /**
1082   * @tc.name: TableConstraintsTest001
1083   * @tc.desc: Test table constraints when create distributed table with DistributedDB::DEVICE_COOPERATION
1084   * @tc.type: FUNC
1085   * @tc.require:
1086   * @tc.author: zhangshijie
1087   */
1088 HWTEST_F(DistributedDBInterfacesRelationalTest, TableConstraintsTest001, TestSize.Level1)
1089 {
1090     TableConstraintsTest(DistributedDB::DEVICE_COOPERATION);
1091 }
1092 
1093 /**
1094   * @tc.name: TableConstraintsTest002
1095   * @tc.desc: Test table constraints when create distributed table with DistributedDB::CLOUD_COOPERATION
1096   * @tc.type: FUNC
1097   * @tc.require:
1098   * @tc.author: zhangshijie
1099   */
1100 HWTEST_F(DistributedDBInterfacesRelationalTest, TableConstraintsTest002, TestSize.Level1)
1101 {
1102     TableConstraintsTest(DistributedDB::CLOUD_COOPERATION);
1103 }
1104 
1105 /**
1106   * @tc.name: RelationalRemoveDeviceDataTest001
1107   * @tc.desc: Test remove device data
1108   * @tc.type: FUNC
1109   * @tc.require: AR000GK58F
1110   * @tc.author: lianhuix
1111   */
1112 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalRemoveDeviceDataTest001, TestSize.Level1)
1113 {
1114     /**
1115      * @tc.steps:step1. Prepare db file
1116      * @tc.expected: step1. Return OK.
1117      */
1118     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1119     ASSERT_NE(db, nullptr);
1120     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1121     EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
1122     RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_A");
1123     RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_B");
1124     RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_C");
1125 
1126     /**
1127      * @tc.steps:step2. Open store
1128      * @tc.expected: step2. return OK
1129      */
1130     RelationalStoreDelegate *delegate = nullptr;
1131     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1132     EXPECT_EQ(status, OK);
1133     ASSERT_NE(delegate, nullptr);
1134 
1135     /**
1136      * @tc.steps:step3. Remove device data
1137      * @tc.expected: step3. ok
1138      */
1139     EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_A"), DISTRIBUTED_SCHEMA_NOT_FOUND);
1140     EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_D"), DISTRIBUTED_SCHEMA_NOT_FOUND);
1141     EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_A", "sync_data"), DISTRIBUTED_SCHEMA_NOT_FOUND);
1142     EXPECT_EQ(delegate->CreateDistributedTable("sync_data"), OK);
1143     EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_A"), OK);
1144     EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_B"), OK);
1145     EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_C", "sync_data"), OK);
1146     EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_D"), OK);
1147     EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_A", "sync_data_A"), DISTRIBUTED_SCHEMA_NOT_FOUND);
1148 
1149     /**
1150      * @tc.steps:step4. Remove device data with invalid args
1151      * @tc.expected: step4. invalid
1152      */
1153     EXPECT_EQ(delegate->RemoveDeviceData(""), INVALID_ARGS);
1154     EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_A", "Handle-J@^."), INVALID_ARGS);
1155 
1156     /**
1157      * @tc.steps:step5. Close store
1158      * @tc.expected: step5 Return OK.
1159      */
1160     status = g_mgr.CloseStore(delegate);
1161     EXPECT_EQ(status, OK);
1162     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1163 }
1164 
1165 struct TableT1 {
1166     int a;
1167     std::string b;
1168     int rowid;
1169     int flag;
1170     int timestamp;
1171 
operator ()__anonc71ddc450110::TableT11172     VirtualRowData operator() () const
1173     {
1174         VirtualRowData rowData;
1175         DataValue dA;
1176         dA = static_cast<int64_t>(a);
1177         rowData.objectData.PutDataValue("a", dA);
1178         DataValue dB;
1179         dB.SetText(b);
1180         rowData.objectData.PutDataValue("b", dB);
1181         rowData.logInfo.dataKey = rowid;
1182         rowData.logInfo.device = DEVICE_B;
1183         rowData.logInfo.originDev = DEVICE_B;
1184         rowData.logInfo.timestamp = timestamp;
1185         rowData.logInfo.wTimestamp = timestamp;
1186         rowData.logInfo.flag = flag;
1187         Key key;
1188         DBCommon::StringToVector(std::to_string(rowid), key);
1189         std::vector<uint8_t> hashKey;
1190         DBCommon::CalcValueHash(key, hashKey);
1191         rowData.logInfo.hashKey = hashKey;
1192         return rowData;
1193     }
1194 };
1195 
AddDeviceSchema(RelationalVirtualDevice * device,sqlite3 * db,const std::string & name)1196 void AddDeviceSchema(RelationalVirtualDevice *device, sqlite3 *db, const std::string &name)
1197 {
1198     TableInfo table;
1199     SQLiteUtils::AnalysisSchema(db, name, table);
1200     device->SetLocalFieldInfo(table.GetFieldInfos());
1201     device->SetTableInfo(table);
1202 }
1203 
AddErrorTrigger(sqlite3 * db,const std::string & name)1204 void AddErrorTrigger(sqlite3 *db, const std::string &name)
1205 {
1206     ASSERT_NE(db, nullptr);
1207     ASSERT_NE(name, "");
1208     std::string sql = "CREATE TRIGGER IF NOT EXISTS "
1209         "naturalbase_rdb_aux_" + name + "_log_ON_DELETE BEFORE DELETE \n"
1210         "ON naturalbase_rdb_aux_" + name + "_log \n"
1211         "BEGIN \n"
1212         "\t INSERT INTO naturalbase_rdb_aux_" + name + "_log VALUES(no_exist_func(), '', '', '', '', '', ''); \n"
1213         "END;";
1214     char *errMsg = nullptr;
1215     EXPECT_EQ(sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &errMsg), SQLITE_OK);
1216     if (errMsg != nullptr) {
1217         LOGE("sql error %s", errMsg);
1218         LOGE("sql %s", sql.c_str());
1219     }
1220 }
1221 
1222 /**
1223   * @tc.name: RelationalRemoveDeviceDataTest002
1224   * @tc.desc: Test remove device data and syn
1225   * @tc.type: FUNC
1226   * @tc.require: AR000GK58F
1227   * @tc.author: lianhuix
1228   */
1229 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalRemoveDeviceDataTest002, TestSize.Level1)
1230 {
1231     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1232     ASSERT_NE(db, nullptr);
1233     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1234     EXPECT_EQ(RelationalTestUtils::ExecSql(db, SIMPLE_CREATE_TABLE_SQL), SQLITE_OK);
1235     AddDeviceSchema(g_deviceB, db, "t1");
1236 
1237     RelationalStoreDelegate *delegate = nullptr;
1238     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1239     EXPECT_EQ(status, OK);
1240     ASSERT_NE(delegate, nullptr);
1241 
1242     EXPECT_EQ(delegate->CreateDistributedTable("t1"), OK);
1243 
1244     g_deviceB->PutDeviceData("t1", std::vector<TableT1>{
1245         {1, "111", 1, 2, 1}, // test data
1246         {2, "222", 2, 2, 2}, // test data
1247         {3, "333", 3, 2, 3}, // test data
1248         {4, "444", 4, 2, 4} // test data
1249     });
1250     std::vector<std::string> devices = {DEVICE_B};
1251     Query query = Query::Select("t1").NotEqualTo("a", 0);
1252     status = delegate->Sync(devices, SyncMode::SYNC_MODE_PULL_ONLY, query,
__anonc71ddc450302(const std::map<std::string, std::vector<TableStatus>> &devicesMap) 1253         [&devices](const std::map<std::string, std::vector<TableStatus>> &devicesMap) {
1254             EXPECT_EQ(devicesMap.size(), devices.size());
1255             EXPECT_EQ(devicesMap.at(DEVICE_B)[0].status, OK);
1256         }, true);
1257     EXPECT_EQ(status, OK);
1258 
1259     EXPECT_EQ(delegate->RemoveDeviceData(DEVICE_B), OK);
1260 
1261     int logCnt = -1;
1262     std::string checkLogSql = "SELECT count(*) FROM naturalbase_rdb_aux_t1_log";
__anonc71ddc450402(sqlite3_stmt *stmt) 1263     RelationalTestUtils::ExecSql(db, checkLogSql, nullptr, [&logCnt](sqlite3_stmt *stmt) {
1264         logCnt = sqlite3_column_int(stmt, 0);
1265         return E_OK;
1266     });
1267     EXPECT_EQ(logCnt, 0);
1268 
1269     int dataCnt = -1;
1270     std::string deviceTable = g_mgr.GetDistributedTableName(DEVICE_B, "t1");
1271     std::string checkDataSql = "SELECT count(*) FROM " + deviceTable;
__anonc71ddc450502(sqlite3_stmt *stmt) 1272     RelationalTestUtils::ExecSql(db, checkDataSql, nullptr, [&dataCnt](sqlite3_stmt *stmt) {
1273         dataCnt = sqlite3_column_int(stmt, 0);
1274         return E_OK;
1275     });
1276     EXPECT_EQ(logCnt, 0);
1277 
1278     status = g_mgr.CloseStore(delegate);
1279     EXPECT_EQ(status, OK);
1280     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1281 }
1282 
TestRemoveDeviceDataWithCallback(bool removeAll)1283 void TestRemoveDeviceDataWithCallback(bool removeAll)
1284 {
1285     /**
1286      * @tc.steps:step1. Prepare db and data
1287      * @tc.expected: step1. Return OK.
1288      */
1289     RuntimeConfig::SetTranslateToDeviceIdCallback([](const std::string &oriDevId, const StoreInfo &info) {
1290         return oriDevId + "_" + info.appId;
1291     });
1292     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1293     ASSERT_NE(db, nullptr);
1294     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1295     EXPECT_EQ(RelationalTestUtils::ExecSql(db, SIMPLE_CREATE_TABLE_SQL), SQLITE_OK);
1296     AddDeviceSchema(g_deviceB, db, "t1");
1297     RelationalStoreDelegate *delegate = nullptr;
1298     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1299     EXPECT_EQ(status, OK);
1300     ASSERT_NE(delegate, nullptr);
1301     EXPECT_EQ(delegate->CreateDistributedTable("t1"), OK);
1302     g_deviceB->PutDeviceData("t1", std::vector<TableT1> {
1303         {1, "111", 1, 0, 1} // test data
1304     });
1305     std::vector<std::string> devices = {DEVICE_B};
1306     Query query = Query::Select("t1").EqualTo("a", 1);
1307     status = delegate->Sync(devices, SyncMode::SYNC_MODE_PULL_ONLY, query,
1308         [&devices](const std::map<std::string, std::vector<TableStatus>> &devicesMap) {
1309             ASSERT_EQ(devicesMap.size(), devices.size());
1310             EXPECT_EQ(devicesMap.at(DEVICE_B)[0].status, OK);
1311         }, true);
1312     EXPECT_EQ(status, OK);
1313     /**
1314      * @tc.steps:step2. remove device data and check table
1315      * @tc.expected: step2. dev table not exist and log not exist device b.
1316      */
1317     std::this_thread::sleep_for(std::chrono::seconds(1));
1318     if (removeAll) {
1319         EXPECT_EQ(delegate->RemoveDeviceData(), OK);
1320     } else {
1321         EXPECT_EQ(delegate->RemoveDeviceData(DEVICE_B + "_" + APP_ID), OK);
1322     }
1323     int logCnt = -1;
1324     std::string checkLogSql = "SELECT count(*) FROM naturalbase_rdb_aux_t1_log";
1325     RelationalTestUtils::ExecSql(db, checkLogSql, nullptr, [&logCnt](sqlite3_stmt *stmt) {
1326         logCnt = sqlite3_column_int(stmt, 0);
1327         return E_OK;
1328     });
1329     EXPECT_EQ(logCnt, 0);
1330     std::string deviceTable = RelationalStoreManager::GetDistributedTableName(DEVICE_B + "_" + APP_ID, "t1");
1331     std::string checkDataSql = "SELECT count(*) FROM " + deviceTable;
1332     EXPECT_NE(RelationalTestUtils::ExecSql(db, checkDataSql, nullptr, nullptr), SQLITE_OK);
1333     /**
1334      * @tc.steps:step3. close db
1335      * @tc.expected: step3. Return OK.
1336      */
1337     status = g_mgr.CloseStore(delegate);
1338     EXPECT_EQ(status, OK);
1339     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1340     RuntimeConfig::SetTranslateToDeviceIdCallback(nullptr);
1341 }
1342 
1343 /**
1344   * @tc.name: RelationalRemoveDeviceDataTest003
1345   * @tc.desc: Test remove all device data and sync again
1346   * @tc.type: FUNC
1347   * @tc.require: AR000GK58F
1348   * @tc.author: zhangqiquan
1349   */
1350 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalRemoveDeviceDataTest003, TestSize.Level1)
1351 {
1352     TestRemoveDeviceDataWithCallback(true);
1353 }
1354 
1355 /**
1356   * @tc.name: RelationalRemoveDeviceDataTest004
1357   * @tc.desc: Test remove one device data and sync again
1358   * @tc.type: FUNC
1359   * @tc.require: AR000GK58F
1360   * @tc.author: zhangqiquan
1361   */
1362 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalRemoveDeviceDataTest004, TestSize.Level1)
1363 {
1364     TestRemoveDeviceDataWithCallback(false);
1365 }
1366 
1367 /**
1368   * @tc.name: RelationalRemoveDeviceDataTest005
1369   * @tc.desc: Test remove device data with invalid param
1370   * @tc.type: FUNC
1371   * @tc.require: AR000GK58F
1372   * @tc.author: zhangqiquan
1373   */
1374 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalRemoveDeviceDataTest005, TestSize.Level1)
1375 {
1376     /**
1377      * @tc.steps:step1. Prepare db file
1378      * @tc.expected: step1. Return OK.
1379      */
1380     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1381     ASSERT_NE(db, nullptr);
1382     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1383     EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
1384     RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_A");
1385     AddDeviceSchema(g_deviceB, db, "sync_data");
1386     /**
1387      * @tc.steps:step2. Open store
1388      * @tc.expected: step2. return OK
1389      */
1390     RelationalStoreDelegate *delegate = nullptr;
1391     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1392     EXPECT_EQ(status, OK);
1393     ASSERT_NE(delegate, nullptr);
1394     int count = 0;
__anonc71ddc450902(const std::string &oriDevId, const StoreInfo &info) 1395     RuntimeConfig::SetTranslateToDeviceIdCallback([&count](const std::string &oriDevId, const StoreInfo &info) {
1396         count++;
1397         return oriDevId + "_" + info.appId;
1398     });
1399     /**
1400      * @tc.steps:step3. Remove not exist device data
1401      * @tc.expected: step3. ok
1402      */
1403     EXPECT_EQ(delegate->CreateDistributedTable("sync_data"), OK);
1404     EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_C", "sync_data"), OK);
1405     EXPECT_EQ(count, 0);
1406     /**
1407      * @tc.steps:step4. Close store
1408      * @tc.expected: step4 Return OK.
1409      */
1410     status = g_mgr.CloseStore(delegate);
1411     EXPECT_EQ(status, OK);
1412     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1413     RuntimeConfig::SetTranslateToDeviceIdCallback(nullptr);
1414 }
1415 
1416 /**
1417   * @tc.name: RelationalRemoveDeviceDataTest006
1418   * @tc.desc: Test remove device data with busy
1419   * @tc.type: FUNC
1420   * @tc.require: AR000GK58F
1421   * @tc.author: zhangqiquan
1422   */
1423 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalRemoveDeviceDataTest006, TestSize.Level1)
1424 {
1425     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1426     ASSERT_NE(db, nullptr);
1427     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1428     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "CREATE TABLE IF NOT EXISTS t2(a INT, b TEXT)"), SQLITE_OK);
1429     AddDeviceSchema(g_deviceB, db, "t2");
1430 
1431     RelationalStoreDelegate *delegate = nullptr;
1432     auto observer = new (std::nothrow) RelationalStoreObserverUnitTest();
1433     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID,
1434         { .observer = observer }, delegate);
1435     EXPECT_EQ(status, OK);
1436     ASSERT_NE(delegate, nullptr);
1437 
1438     EXPECT_EQ(delegate->CreateDistributedTable("t2"), OK);
1439 
1440     g_deviceB->PutDeviceData("t2", std::vector<TableT1>{
1441         {1, "111", 1, 2, 1}, // test data
1442     });
1443     std::vector<std::string> devices = {DEVICE_B};
1444     Query query = Query::Select("t2");
1445     status = delegate->Sync(devices, SyncMode::SYNC_MODE_PULL_ONLY, query, nullptr, true);
1446     EXPECT_EQ(status, OK);
1447 
1448     auto beforeCallCount = observer->GetCallCount();
1449     AddErrorTrigger(db, "t2");
1450     EXPECT_EQ(delegate->RemoveDeviceData(DEVICE_B), DB_ERROR);
1451 
1452     status = delegate->Sync(devices, SyncMode::SYNC_MODE_PULL_ONLY, query, nullptr, true);
1453     EXPECT_EQ(status, OK);
1454     auto afterCallCount = observer->GetCallCount();
1455     EXPECT_NE(beforeCallCount, afterCallCount);
1456 
1457     status = g_mgr.CloseStore(delegate);
1458     EXPECT_EQ(status, OK);
1459     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1460     delete observer;
1461 }
1462 
1463 /**
1464   * @tc.name: RelationalOpenStorePathCheckTest001
1465   * @tc.desc: Test open store with same label but different path.
1466   * @tc.type: FUNC
1467   * @tc.require: AR000GK58F
1468   * @tc.author: lianhuix
1469   */
1470 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalOpenStorePathCheckTest001, TestSize.Level1)
1471 {
1472     std::string dir1 = g_dbDir + "dbDir1";
1473     EXPECT_EQ(OS::MakeDBDirectory(dir1), E_OK);
1474     sqlite3 *db1 = RelationalTestUtils::CreateDataBase(dir1 + STORE_ID + DB_SUFFIX);
1475     ASSERT_NE(db1, nullptr);
1476     EXPECT_EQ(RelationalTestUtils::ExecSql(db1, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1477     EXPECT_EQ(RelationalTestUtils::ExecSql(db1, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
1478     EXPECT_EQ(sqlite3_close_v2(db1), SQLITE_OK);
1479 
1480     std::string dir2 = g_dbDir + "dbDir2";
1481     EXPECT_EQ(OS::MakeDBDirectory(dir2), E_OK);
1482     sqlite3 *db2 = RelationalTestUtils::CreateDataBase(dir2 + STORE_ID + DB_SUFFIX);
1483     ASSERT_NE(db2, nullptr);
1484     EXPECT_EQ(RelationalTestUtils::ExecSql(db2, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1485     EXPECT_EQ(RelationalTestUtils::ExecSql(db2, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
1486     EXPECT_EQ(sqlite3_close_v2(db2), SQLITE_OK);
1487 
1488     DBStatus status = OK;
1489     RelationalStoreDelegate *delegate1 = nullptr;
1490     status = g_mgr.OpenStore(dir1 + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate1);
1491     EXPECT_EQ(status, OK);
1492     ASSERT_NE(delegate1, nullptr);
1493 
1494     RelationalStoreDelegate *delegate2 = nullptr;
1495     status = g_mgr.OpenStore(dir2 + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate2);
1496     EXPECT_EQ(status, INVALID_ARGS);
1497     ASSERT_EQ(delegate2, nullptr);
1498 
1499     status = g_mgr.CloseStore(delegate1);
1500     EXPECT_EQ(status, OK);
1501 
1502     status = g_mgr.CloseStore(delegate2);
1503     EXPECT_EQ(status, INVALID_ARGS);
1504 }
1505 
1506 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalOpenStorePressureTest001, TestSize.Level1)
1507 {
1508     /**
1509      * @tc.steps:step1. Prepare db file
1510      * @tc.expected: step1. Return OK.
1511      */
1512     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1513     ASSERT_NE(db, nullptr);
1514     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1515     EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
1516     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1517 
1518     DBStatus status = OK;
1519     for (int i = 0; i < 1000; i++) {
1520         RelationalStoreDelegate *delegate = nullptr;
1521         status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1522         EXPECT_EQ(status, OK);
1523         ASSERT_NE(delegate, nullptr);
1524 
1525         status = g_mgr.CloseStore(delegate);
1526         EXPECT_EQ(status, OK);
1527         delegate = nullptr;
1528     }
1529 }
1530 
1531 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalOpenStorePressureTest002, TestSize.Level1)
1532 {
1533     /**
1534      * @tc.steps:step1. Prepare db file
1535      * @tc.expected: step1. Return OK.
1536      */
1537     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1538     ASSERT_NE(db, nullptr);
1539     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1540     EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
1541     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1542 
1543     std::queue<RelationalStoreDelegate *> delegateQueue;
1544     std::mutex queueLock;
1545     std::random_device rd;
1546     default_random_engine e(rd());
1547     uniform_int_distribution<unsigned> u(0, 9);
1548 
__anonc71ddc450a02() 1549     std::thread openStoreThread([&, this]() {
1550         for (int i = 0; i < 1000; i++) {
1551             LOGD("++++> open store delegate: %d", i);
1552             RelationalStoreDelegate *delegate = nullptr;
1553             DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1554             EXPECT_EQ(status, OK);
1555             ASSERT_NE(delegate, nullptr);
1556             {
1557                 std::lock_guard<std::mutex> lock(queueLock);
1558                 delegateQueue.push(delegate);
1559             }
1560             LOGD("++++< open store delegate: %d", i);
1561         }
1562     });
1563 
1564     int cnt = 0;
1565     while (cnt < 1000) {
1566         RelationalStoreDelegate *delegate = nullptr;
1567         {
1568             std::lock_guard<std::mutex> lock(queueLock);
1569             if (delegateQueue.empty()) {
1570                 std::this_thread::sleep_for(std::chrono::microseconds(100));
1571                 continue;
1572             }
1573             delegate = delegateQueue.front();
1574             delegateQueue.pop();
1575         }
1576         LOGD("++++> close store delegate: %d", cnt);
1577         DBStatus status = g_mgr.CloseStore(delegate);
1578         LOGD("++++< close store delegate: %d", cnt);
1579         EXPECT_EQ(status, OK);
1580         delegate = nullptr;
1581         cnt++;
1582         std::this_thread::sleep_for(std::chrono::microseconds(100 * u(e)));
1583     }
1584     openStoreThread.join();
1585 }
1586 
1587 namespace {
ProcessSync(RelationalStoreDelegate * delegate)1588     void ProcessSync(RelationalStoreDelegate *delegate)
1589     {
1590         std::vector<std::string> devices = {DEVICE_B};
1591         Query query = Query::Select("create").EqualTo("create", 1);
1592         DBStatus status = delegate->Sync(devices, SyncMode::SYNC_MODE_PUSH_ONLY, query,
1593             [&devices](const std::map<std::string, std::vector<TableStatus>> &devicesMap) {
1594                 EXPECT_EQ(devicesMap.size(), devices.size());
1595                 EXPECT_EQ(devicesMap.at(DEVICE_B)[0].status, OK);
1596             }, true);
1597         EXPECT_EQ(status, OK);
1598 
1599         std::vector<VirtualRowData> data;
1600         g_deviceB->GetAllSyncData("create", data);
1601         EXPECT_EQ(data.size(), 1u);
1602 
1603         VirtualRowData virtualRowData;
1604         DataValue d1;
1605         d1 = static_cast<int64_t>(2); // 2: test data
1606         virtualRowData.objectData.PutDataValue("create", d1);
1607         DataValue d2;
1608         d2.SetText("hello");
1609         virtualRowData.objectData.PutDataValue("ddd", d2);
1610         DataValue d3;
1611         d3.SetText("hello");
1612         virtualRowData.objectData.PutDataValue("eee", d3);
1613         virtualRowData.logInfo.timestamp = 1;
1614         g_deviceB->PutData("create", {virtualRowData});
1615         status = delegate->Sync(devices, SyncMode::SYNC_MODE_PULL_ONLY, query,
1616             [&devices](const std::map<std::string, std::vector<TableStatus>> &devicesMap) {
1617                 EXPECT_EQ(devicesMap.size(), devices.size());
1618                 EXPECT_EQ(devicesMap.at(DEVICE_B)[0].status, OK);
1619             }, true);
1620         EXPECT_EQ(status, OK);
1621     }
1622 }
1623 
1624 /**
1625   * @tc.name: SqliteKeyWord001
1626   * @tc.desc: Test relational table with sqlite key world.
1627   * @tc.type: FUNC
1628   * @tc.require:
1629   * @tc.author: lianhuix
1630   */
1631 HWTEST_F(DistributedDBInterfacesRelationalTest, SqliteKeyWordTest001, TestSize.Level1)
1632 {
1633     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1634     ASSERT_NE(db, nullptr);
1635     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1636 
1637     std::string tableSql = "CREATE TABLE IF NOT EXISTS 'create' ('create' INTEGER PRIMARY KEY, b 'CREATE', " \
1638     "c TEXT DEFAULT 'DEFAULT', UNIQUE(b, c))";
1639     EXPECT_EQ(RelationalTestUtils::ExecSql(db, tableSql), SQLITE_OK);
1640     std::string indexSql = "CREATE INDEX IF NOT EXISTS 'index' on 'create' (b)";
1641     EXPECT_EQ(RelationalTestUtils::ExecSql(db, indexSql), SQLITE_OK);
1642 
1643     PrepareVirtualDeviceEnv("create", g_dbDir + STORE_ID + DB_SUFFIX, {g_deviceB});
1644 
1645     DBStatus status = OK;
1646     RelationalStoreDelegate *delegate = nullptr;
1647     status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1648     EXPECT_EQ(status, OK);
1649     ASSERT_NE(delegate, nullptr);
1650 
1651     status = delegate->CreateDistributedTable("create");
1652     EXPECT_EQ(status, OK);
1653 
1654     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "INSERT INTO 'create' values(1, 'bbb', 'ccc')"), SQLITE_OK);
1655 
1656     std::string insertSql = "INSERT INTO 'create' values(1001, 'create', 'text')";
1657     EXPECT_EQ(RelationalTestUtils::ExecSql(db, insertSql), SQLITE_OK);
1658 
1659     std::string updateSql = "UPDATE 'create' SET 'create' = 1002 WHERE b = 'create'";
1660     EXPECT_EQ(RelationalTestUtils::ExecSql(db, updateSql), SQLITE_OK);
1661 
1662     std::string deleteSql = "DELETE FROM 'create' WHERE 'create' = 1002";
1663     EXPECT_EQ(RelationalTestUtils::ExecSql(db, deleteSql), SQLITE_OK);
1664 
1665     ProcessSync(delegate);
1666 
1667     std::string alterSql = "ALTER TABLE 'create' ADD COLUMN 'table' 'insert' DEFAULT NULL";
1668     EXPECT_EQ(RelationalTestUtils::ExecSql(db, alterSql), SQLITE_OK);
1669     status = delegate->CreateDistributedTable("create");
1670     EXPECT_EQ(status, OK);
1671 
1672     status = delegate->RemoveDeviceData("DEVICES_B", "create");
1673     EXPECT_EQ(status, OK);
1674 
1675     status = g_mgr.CloseStore(delegate);
1676     EXPECT_EQ(status, OK);
1677     delegate = nullptr;
1678     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1679 }
1680 
1681 /**
1682   * @tc.name: GetDistributedTableName001
1683   * @tc.desc: Test get distributed table name
1684   * @tc.type: FUNC
1685   * @tc.require: AR000GK58F
1686   * @tc.author: zhangqiquan
1687   */
1688 HWTEST_F(DistributedDBInterfacesRelationalTest, GetDistributedTableName001, TestSize.Level1)
1689 {
1690     const std::string deviceName = "DEVICES_A";
1691     const std::string tableName = "TABLE";
1692     const std::string hashDev = DBCommon::TransferStringToHex(DBCommon::TransferHashString(deviceName));
1693 
1694     std::string devTableName = RelationalStoreManager::GetDistributedTableName(deviceName, tableName);
1695     EXPECT_EQ(devTableName, DBConstant::RELATIONAL_PREFIX + tableName + "_" + hashDev);
__anonc71ddc450e02(const std::string &oriDevId, const StoreInfo &info) 1696     RuntimeConfig::SetTranslateToDeviceIdCallback([](const std::string &oriDevId, const StoreInfo &info) {
1697         EXPECT_EQ(info.appId, "");
1698         return oriDevId;
1699     });
1700     devTableName = RelationalStoreManager::GetDistributedTableName(deviceName, tableName);
1701     EXPECT_EQ(devTableName, DBConstant::RELATIONAL_PREFIX + tableName + "_" + deviceName);
1702     devTableName = RelationalStoreManager::GetDistributedTableName("", tableName);
1703     EXPECT_EQ(devTableName, DBConstant::RELATIONAL_PREFIX + tableName + "_");
1704     RuntimeConfig::SetTranslateToDeviceIdCallback(nullptr);
1705 }
1706 
1707 /**
1708   * @tc.name: CloudRelationalStoreTest001
1709   * @tc.desc: Test create distributed table in cloud table sync type
1710   * @tc.type: FUNC
1711   * @tc.require:
1712   * @tc.author: zhangshjie
1713   */
1714 HWTEST_F(DistributedDBInterfacesRelationalTest, CreateDistributedTableTest001, TestSize.Level0)
1715 {
1716     /**
1717      * @tc.steps:step1. Prepare db file
1718      * @tc.expected: step1. Return OK.
1719      */
1720     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1721     ASSERT_NE(db, nullptr);
1722     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1723     EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
1724     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1725 
1726     /**
1727      * @tc.steps:step2. open relational store, create distributed table with CLOUD_COOPERATION
1728      * @tc.expected: step2. Return OK.
1729      */
1730     RelationalStoreDelegate *delegate = nullptr;
1731     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1732     EXPECT_EQ(status, OK);
1733     ASSERT_NE(delegate, nullptr);
1734 
1735     status = delegate->CreateDistributedTable("sync_data", DistributedDB::CLOUD_COOPERATION);
1736     EXPECT_EQ(status, OK);
1737 
1738     /**
1739      * @tc.steps:step3. open relational store, create distributed table with CLOUD_COOPERATION again
1740      * @tc.expected: step3. Return OK.
1741      */
1742     status = delegate->CreateDistributedTable("sync_data", DistributedDB::CLOUD_COOPERATION);
1743     EXPECT_EQ(status, OK);
1744 
1745     status = g_mgr.CloseStore(delegate);
1746     EXPECT_EQ(status, OK);
1747 }
1748 
1749 /**
1750   * @tc.name: CloudRelationalStoreTest002
1751   * @tc.desc: Test create distributed table in diff table sync type for the same table
1752   * @tc.type: FUNC
1753   * @tc.require:
1754   * @tc.author: zhangshjie
1755   */
1756 HWTEST_F(DistributedDBInterfacesRelationalTest, CreateDistributedTableTest002, TestSize.Level0)
1757 {
1758     /**
1759      * @tc.steps:step1. Prepare db file
1760      * @tc.expected: step1. Return OK.
1761      */
1762     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1763     ASSERT_NE(db, nullptr);
1764     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1765     EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
1766     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1767 
1768     /**
1769      * @tc.steps:step2. open relational store, create distributed table with DEVICE_COOPERATION
1770      * @tc.expected: step2. Return OK.
1771      */
1772     RelationalStoreDelegate *delegate = nullptr;
1773     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1774     EXPECT_EQ(status, OK);
1775     ASSERT_NE(delegate, nullptr);
1776 
1777     status = delegate->CreateDistributedTable("sync_data", DistributedDB::DEVICE_COOPERATION);
1778     EXPECT_EQ(status, OK);
1779 
1780     /**
1781      * @tc.steps:step3. create distributed table with CLOUD_COOPERATION again
1782      * @tc.expected: step3. Return TYPE_MISMATCH.
1783      */
1784     status = delegate->CreateDistributedTable("sync_data", DistributedDB::CLOUD_COOPERATION);
1785     EXPECT_EQ(status, TYPE_MISMATCH);
1786 
1787     status = g_mgr.CloseStore(delegate);
1788     EXPECT_EQ(status, OK);
1789     delegate = nullptr;
1790 
1791     /**
1792      * @tc.steps:step4. drop table sync_data and create again
1793      * @tc.expected: step4. Return OK.
1794      */
1795     db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1796     ASSERT_NE(db, nullptr);
1797     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1798     const std::string dropSql = "drop table sync_data;";
1799     EXPECT_EQ(RelationalTestUtils::ExecSql(db, dropSql), SQLITE_OK);
1800     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1801 
1802     /**
1803      * @tc.steps:step5. open relational store, create distributed table with CLOUD_COOPERATION
1804      * @tc.expected: step5. Return OK.
1805      */
1806     status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1807     EXPECT_EQ(status, OK);
1808     ASSERT_NE(delegate, nullptr);
1809 
1810     db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1811     ASSERT_NE(db, nullptr);
1812     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1813     EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
1814     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1815 
1816     EXPECT_EQ(delegate->CreateDistributedTable("sync_data", DistributedDB::CLOUD_COOPERATION), OK);
1817 
1818     /**
1819      * @tc.steps:step6. create distributed table with DEVICE_COOPERATION again
1820      * @tc.expected: step6. Return TYPE_MISMATCH.
1821      */
1822     status = delegate->CreateDistributedTable("sync_data", DistributedDB::DEVICE_COOPERATION);
1823     EXPECT_EQ(status, TYPE_MISMATCH);
1824 
1825     status = g_mgr.CloseStore(delegate);
1826     EXPECT_EQ(status, OK);
1827     delegate = nullptr;
1828 }
1829 
1830 /**
1831   * @tc.name: CreateDistributedTableTest003
1832   * @tc.desc: Test create distributed table after rename table
1833   * @tc.type: FUNC
1834   * @tc.require:
1835   * @tc.author: zhangqiquan
1836   */
1837 HWTEST_F(DistributedDBInterfacesRelationalTest, CreateDistributedTableTest003, TestSize.Level0)
1838 {
1839     /**
1840      * @tc.steps:step1. Prepare db file
1841      */
1842     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1843     ASSERT_NE(db, nullptr);
1844     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1845     EXPECT_EQ(RelationalTestUtils::ExecSql(db, SIMPLE_CREATE_TABLE_SQL), SQLITE_OK);
1846     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1847     /**
1848      * @tc.steps:step2. open relational store, create distributed table with default mode
1849      */
1850     RelationalStoreDelegate *delegate = nullptr;
1851     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1852     EXPECT_EQ(status, OK);
1853     ASSERT_NE(delegate, nullptr);
1854 
1855     status = delegate->CreateDistributedTable("t1");
1856     EXPECT_EQ(status, OK);
1857     /**
1858      * @tc.steps:step3. rename table
1859      */
1860     db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1861     ASSERT_NE(db, nullptr);
1862     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "alter table t1 rename to t2;"), SQLITE_OK);
1863     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1864     /**
1865      * @tc.steps:step4. reopen delegate
1866      */
1867     status = g_mgr.CloseStore(delegate);
1868     EXPECT_EQ(status, OK);
1869     delegate = nullptr;
1870     status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1871     EXPECT_EQ(status, OK);
1872     ASSERT_NE(delegate, nullptr);
1873     /**
1874      * @tc.steps:step5. create distributed table and insert data ok
1875      */
1876     status = delegate->CreateDistributedTable("t2");
1877     EXPECT_EQ(status, OK);
1878 
1879     db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1880     ASSERT_NE(db, nullptr);
1881     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "insert into t2 values(1, '2');"), SQLITE_OK);
1882     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1883     /**
1884      * @tc.steps:step6. close store
1885      */
1886     status = g_mgr.CloseStore(delegate);
1887     EXPECT_EQ(status, OK);
1888     delegate = nullptr;
1889 }
1890 
1891 /**
1892   * @tc.name: CreateDistributedTableTest004
1893   * @tc.desc: Test create distributed table will violate the constraint when table contains "_rowid_" column
1894   * @tc.type: FUNC
1895   * @tc.require:
1896   * @tc.author: zhangshijie
1897   */
1898 HWTEST_F(DistributedDBInterfacesRelationalTest, CreateDistributedTableTest004, TestSize.Level0)
1899 {
1900     /**
1901      * @tc.steps:step1. Prepare db and table
1902      */
1903     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1904     ASSERT_NE(db, nullptr);
1905     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1906     std::string t1 = "t1";
1907     std::string sql = "create table " + t1 + "(key text, _rowid_ int);";
1908     EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK);
1909     std::string t2 = "t2";
1910     sql = "create table " + t2 + "(rowid int);";
1911     EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK);
1912     std::string t3 = "t3";
1913     sql = "create table " + t3 + "(oid int);";
1914     EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK);
1915     std::string t4 = "t4";
1916     sql = "create table " + t4 + "(rowid int, oid int, _rowid_ text);";
1917     EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK);
1918     std::string t5 = "t5";
1919     sql = "create table " + t5 + "(_RoWiD_ int);";
1920     EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK);
1921     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1922 
1923     /**
1924      * @tc.steps:step2. open relational store, create distributed table
1925      */
1926     RelationalStoreDelegate *delegate = nullptr;
1927     EXPECT_EQ(g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate), OK);
1928     ASSERT_NE(delegate, nullptr);
1929     EXPECT_EQ(delegate->CreateDistributedTable(t1), NOT_SUPPORT);
1930     EXPECT_EQ(delegate->CreateDistributedTable(t1, DistributedDB::CLOUD_COOPERATION), NOT_SUPPORT);
1931     EXPECT_EQ(delegate->CreateDistributedTable(t2), OK);
1932     EXPECT_EQ(delegate->CreateDistributedTable(t3), OK);
1933     EXPECT_EQ(delegate->CreateDistributedTable(t4), NOT_SUPPORT);
1934     EXPECT_EQ(delegate->CreateDistributedTable(t5), NOT_SUPPORT);
1935 
1936     EXPECT_EQ(g_mgr.CloseStore(delegate), OK);
1937     delegate = nullptr;
1938 }
1939 
1940 /**
1941   * @tc.name: CloudRelationalStoreTest006
1942   * @tc.desc: Test create distributed table and execute transaction concurrently
1943   * @tc.type: FUNC
1944   * @tc.require:
1945   * @tc.author: liaoyonghuang
1946   */
1947 HWTEST_F(DistributedDBInterfacesRelationalTest, CreateDistributedTableTest006, TestSize.Level0)
1948 {
1949     /**
1950      * @tc.steps:step1. Prepare db file
1951      * @tc.expected: step1. Return OK.
1952      */
1953     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1954     ASSERT_NE(db, nullptr);
1955     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1956     EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
1957     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1958     /**
1959      * @tc.steps:step2. open relational store
1960      * @tc.expected: step2. Return OK.
1961      */
1962     RelationalStoreDelegate *delegate = nullptr;
1963     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1964     EXPECT_EQ(status, OK);
1965     ASSERT_NE(delegate, nullptr);
1966     /**
1967      * @tc.steps:step3. create distributed table with CLOUD_COOPERATION after between a transaction
1968      * @tc.expected: step3. Return OK.
1969      */
1970     DistributedDB::SqlCondition sqlCondition;
1971     std::vector<VBucket> records = {};
1972     sqlCondition.sql = "BEGIN;";
1973     EXPECT_EQ(delegate->ExecuteSql(sqlCondition, records), E_OK);
1974     status = delegate->CreateDistributedTable("sync_data", DistributedDB::CLOUD_COOPERATION);
1975     EXPECT_EQ(status, OK);
1976     sqlCondition.sql = "COMMIT;";
1977     EXPECT_EQ(delegate->ExecuteSql(sqlCondition, records), E_OK);
1978     status = g_mgr.CloseStore(delegate);
1979     EXPECT_EQ(status, OK);
1980 }
1981 }
1982