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:
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:
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:
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:
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:
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(std::string(DBConstant::SYSTEM_TABLE_PREFIX) + "_tmp", tableSyncType),
440 OK);
441
442 EXPECT_EQ(delegate->CreateDistributedTable("Handle-J@^.", tableSyncType), INVALID_ARGS);
443 EXPECT_EQ(delegate->CreateDistributedTable("sync_data",
444 static_cast<TableSyncType>(DistributedDB::DEVICE_COOPERATION - 1)), INVALID_ARGS);
445 EXPECT_EQ(delegate->CreateDistributedTable("sync_data",
446 static_cast<TableSyncType>(DistributedDB::CLOUD_COOPERATION + 1)), INVALID_ARGS);
447
448 EXPECT_EQ(RelationalTestUtils::ExecSql(db, INVALID_TABLE_FIELD_SQL), SQLITE_OK);
449 EXPECT_EQ(delegate->CreateDistributedTable("t1", tableSyncType), NOT_SUPPORT);
450
451 /**
452 * @tc.steps:step4. Create distributed table temp table or not exist table
453 * @tc.expected: step4. Create distributed table failed.
454 */
455 EXPECT_EQ(delegate->CreateDistributedTable("child", tableSyncType), NOT_FOUND);
456 std::string tempTableSql = "CREATE TEMP TABLE child(x, y, z)";
457 EXPECT_EQ(RelationalTestUtils::ExecSql(db, tempTableSql), SQLITE_OK);
458 EXPECT_EQ(delegate->CreateDistributedTable("child", tableSyncType), NOT_FOUND);
459
460 /**
461 * @tc.steps:step5. Close store
462 * @tc.expected: step5. Return OK.
463 */
464 status = g_mgr.CloseStore(delegate);
465 EXPECT_EQ(status, OK);
466 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
467 }
468
469 /**
470 * @tc.name: RelationalStoreTest005
471 * @tc.desc: Test create distributed table with invalid table name or invalid table sync type
472 * @tc.type: FUNC
473 * @tc.require:
474 * @tc.author: lianhuix
475 */
476 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest005, TestSize.Level1)
477 {
478 CreateDistributedTableInvalidArgsTest(DistributedDB::DEVICE_COOPERATION);
479 }
480
481 /**
482 * @tc.name: RelationalStoreTest005
483 * @tc.desc: Test create distributed table with invalid table name or invalid table sync type
484 * @tc.type: FUNC
485 * @tc.require:
486 * @tc.author:
487 */
488 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest005_1, TestSize.Level1)
489 {
490 CreateDistributedTableInvalidArgsTest(DistributedDB::CLOUD_COOPERATION);
491 }
492
CreateDistributedTableNonPrimaryKeyTest(TableSyncType tableSyncType)493 void CreateDistributedTableNonPrimaryKeyTest(TableSyncType tableSyncType)
494 {
495 /**
496 * @tc.steps:step1. Prepare db file
497 * @tc.expected: step1. Return OK.
498 */
499 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
500 ASSERT_NE(db, nullptr);
501 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
502 if (tableSyncType == DistributedDB::DEVICE_COOPERATION) {
503 EXPECT_EQ(RelationalTestUtils::ExecSql(db, CREATE_TABLE_SQL_NO_PRIMARY_KEY), SQLITE_OK);
504 } else {
505 EXPECT_EQ(RelationalTestUtils::ExecSql(db, CREATE_TABLE_SQL_NO_PRIMARY_KEY_NO_UNIQUE), SQLITE_OK);
506 }
507 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
508
509 /**
510 * @tc.steps:step2. Open store
511 * @tc.expected: step2. return OK
512 */
513 RelationalStoreDelegate *delegate = nullptr;
514 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
515 EXPECT_EQ(status, OK);
516 ASSERT_NE(delegate, nullptr);
517
518 /**
519 * @tc.steps:step3. Create distributed table with valid table name
520 * @tc.expected: step3. Create distributed table success.
521 */
522 EXPECT_EQ(delegate->CreateDistributedTable("sync_data", tableSyncType), OK);
523
524 /**
525 * @tc.steps:step4. Close store
526 * @tc.expected: step4. Return OK.
527 */
528 status = g_mgr.CloseStore(delegate);
529 EXPECT_EQ(status, OK);
530 delegate = nullptr;
531
532 status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
533 EXPECT_EQ(status, OK);
534 ASSERT_NE(delegate, nullptr);
535
536 status = g_mgr.CloseStore(delegate);
537 EXPECT_EQ(status, OK);
538 }
539
540 /**
541 * @tc.name: RelationalStoreTest006
542 * @tc.desc: Test create distributed table with non primary key schema
543 * @tc.type: FUNC
544 * @tc.require:
545 * @tc.author: lianhuix
546 */
547 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest006, TestSize.Level1)
548 {
549 CreateDistributedTableNonPrimaryKeyTest(DistributedDB::DEVICE_COOPERATION);
550 }
551
552 /**
553 * @tc.name: RelationalStoreTest006
554 * @tc.desc: Test create distributed table with non primary key schema
555 * @tc.type: FUNC
556 * @tc.require:
557 * @tc.author:
558 */
559 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest006_1, TestSize.Level1)
560 {
561 CreateDistributedTableNonPrimaryKeyTest(DistributedDB::CLOUD_COOPERATION);
562 }
563
CreateDistributedTableInvalidFieldTest(TableSyncType tableSyncType)564 void CreateDistributedTableInvalidFieldTest(TableSyncType tableSyncType)
565 {
566 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
567 ASSERT_NE(db, nullptr);
568 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
569 EXPECT_EQ(RelationalTestUtils::ExecSql(db, UNSUPPORTED_FIELD_TABLE_SQL), SQLITE_OK);
570 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
571
572 RelationalStoreDelegate *delegate = nullptr;
573 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
574 EXPECT_EQ(status, OK);
575 ASSERT_NE(delegate, nullptr);
576
577 EXPECT_EQ(delegate->CreateDistributedTable("test", tableSyncType), NOT_SUPPORT);
578 status = g_mgr.CloseStore(delegate);
579 EXPECT_EQ(status, OK);
580 }
581
582 /**
583 * @tc.name: RelationalStoreTest007
584 * @tc.desc: Test create distributed table with table has invalid field name
585 * @tc.type: FUNC
586 * @tc.require:
587 * @tc.author: lianhuix
588 */
589 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest007, TestSize.Level1)
590 {
591 CreateDistributedTableInvalidFieldTest(DistributedDB::DEVICE_COOPERATION);
592 }
593
594 /**
595 * @tc.name: RelationalStoreTest007
596 * @tc.desc: Test create distributed table with table has invalid field name
597 * @tc.type: FUNC
598 * @tc.require:
599 * @tc.author: zhangshijie
600 */
601 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest007_1, TestSize.Level1)
602 {
603 CreateDistributedTableInvalidFieldTest(DistributedDB::CLOUD_COOPERATION);
604 }
605
CreateDistributedTableCompositePKTest(TableSyncType tableSyncType,int expectCode)606 void CreateDistributedTableCompositePKTest(TableSyncType tableSyncType, int expectCode)
607 {
608 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
609 ASSERT_NE(db, nullptr);
610 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
611 EXPECT_EQ(RelationalTestUtils::ExecSql(db, COMPOSITE_PRIMARY_KEY_TABLE_SQL), SQLITE_OK);
612 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
613
614 RelationalStoreDelegate *delegate = nullptr;
615 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
616 EXPECT_EQ(status, OK);
617 ASSERT_NE(delegate, nullptr);
618
619 EXPECT_EQ(delegate->CreateDistributedTable("workers", tableSyncType), expectCode);
620 status = g_mgr.CloseStore(delegate);
621 EXPECT_EQ(status, OK);
622 }
623
624 /**
625 * @tc.name: RelationalStoreTest008
626 * @tc.desc: Test create distributed table with table has composite primary keys for DEVICE_COOPERATION
627 * @tc.type: FUNC
628 * @tc.require:
629 * @tc.author: lianhuix
630 */
631 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest008, TestSize.Level1)
632 {
633 CreateDistributedTableCompositePKTest(DistributedDB::DEVICE_COOPERATION, NOT_SUPPORT);
634 }
635
636 /**
637 * @tc.name: RelationalStoreTest008
638 * @tc.desc: Test create distributed table with table has composite primary keys for CLOUD_COOPERATION
639 * @tc.type: FUNC
640 * @tc.require:
641 * @tc.author:
642 */
643 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest008_1, TestSize.Level1)
644 {
645 CreateDistributedTableCompositePKTest(DistributedDB::CLOUD_COOPERATION, OK);
646 }
647
CreateDistributedTableWithHistoryDataTest(TableSyncType tableSyncType)648 void CreateDistributedTableWithHistoryDataTest(TableSyncType tableSyncType)
649 {
650 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
651 ASSERT_NE(db, nullptr);
652 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
653 EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
654 EXPECT_EQ(RelationalTestUtils::ExecSql(db, INSERT_SYNC_DATA_SQL), SQLITE_OK);
655 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
656
657 RelationalStoreDelegate *delegate = nullptr;
658 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
659 EXPECT_EQ(status, OK);
660 ASSERT_NE(delegate, nullptr);
661
662 EXPECT_EQ(delegate->CreateDistributedTable("sync_data"), OK);
663 status = g_mgr.CloseStore(delegate);
664 EXPECT_EQ(status, OK);
665 }
666
667 /**
668 * @tc.name: RelationalStoreTest009
669 * @tc.desc: Test create distributed table with table has history data for DEVICE_COOPERATION
670 * @tc.type: FUNC
671 * @tc.require:
672 * @tc.author: lianhuix
673 */
674 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest009, TestSize.Level1)
675 {
676 CreateDistributedTableWithHistoryDataTest(DistributedDB::DEVICE_COOPERATION);
677 }
678
679 /**
680 * @tc.name: RelationalStoreTest009
681 * @tc.desc: Test create distributed table with table has history data for CLOUD_COOPERATION
682 * @tc.type: FUNC
683 * @tc.require:
684 * @tc.author:
685 */
686 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest009_1, TestSize.Level1)
687 {
688 CreateDistributedTableWithHistoryDataTest(DistributedDB::CLOUD_COOPERATION);
689 }
690
TableModifyTest(const std::string & modifySql,TableSyncType tableSyncType,DBStatus expect)691 void TableModifyTest(const std::string &modifySql, TableSyncType tableSyncType, DBStatus expect)
692 {
693 /**
694 * @tc.steps:step1. Prepare db file
695 * @tc.expected: step1. Return OK.
696 */
697 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
698 ASSERT_NE(db, nullptr);
699 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
700 if (tableSyncType == DistributedDB::DEVICE_COOPERATION) {
701 EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
702 } else {
703 EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
704 }
705
706 RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_A");
707 RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_B");
708 RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_C");
709
710 /**
711 * @tc.steps:step2. Open store
712 * @tc.expected: step2. return OK
713 */
714 RelationalStoreDelegate *delegate = nullptr;
715 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
716 EXPECT_EQ(status, OK);
717 ASSERT_NE(delegate, nullptr);
718
719 /**
720 * @tc.steps:step3. Create distributed table
721 * @tc.expected: step3. Create distributed table OK.
722 */
723 EXPECT_EQ(delegate->CreateDistributedTable("sync_data", tableSyncType), OK);
724
725 /**
726 * @tc.steps:step4. Upgrade table with modifySql
727 * @tc.expected: step4. return OK
728 */
729 EXPECT_EQ(RelationalTestUtils::ExecSql(db, modifySql), SQLITE_OK);
730
731 /**
732 * @tc.steps:step5. Create distributed table again
733 * @tc.expected: step5. Create distributed table return expect.
734 */
735 EXPECT_EQ(delegate->CreateDistributedTable("sync_data", tableSyncType), expect);
736
737 /**
738 * @tc.steps:step6. Close store
739 * @tc.expected: step6 Return OK.
740 */
741 status = g_mgr.CloseStore(delegate);
742 EXPECT_EQ(status, OK);
743 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
744 }
745
746 /**
747 * @tc.name: RelationalStoreTest010
748 * @tc.desc: Test opening libraries with the same name in different paths.
749 * @tc.type: FUNC
750 * @tc.require:
751 * @tc.author:
752 */
753 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest010, TestSize.Level0)
754 {
755 /**
756 * @tc.steps:step1. Prepare db file
757 * @tc.expected: step1. Return OK.
758 */
759 std::string dbPath1 = g_dbDir + "path1/";
760 ASSERT_EQ(OS::MakeDBDirectory(dbPath1), E_OK);
761 sqlite3 *db1 = RelationalTestUtils::CreateDataBase(dbPath1 + STORE_ID + DB_SUFFIX);
762 ASSERT_NE(db1, nullptr);
763 EXPECT_EQ(RelationalTestUtils::ExecSql(db1, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
764 EXPECT_EQ(RelationalTestUtils::ExecSql(db1, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
765 EXPECT_EQ(sqlite3_close_v2(db1), SQLITE_OK);
766 std::string dbPath2 = g_dbDir + "path2/";
767 ASSERT_EQ(OS::MakeDBDirectory(dbPath2), E_OK);
768 sqlite3 *db2 = RelationalTestUtils::CreateDataBase(dbPath2 + STORE_ID + DB_SUFFIX);
769 ASSERT_NE(db2, nullptr);
770 EXPECT_EQ(RelationalTestUtils::ExecSql(db2, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
771 EXPECT_EQ(RelationalTestUtils::ExecSql(db2, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
772 EXPECT_EQ(sqlite3_close_v2(db2), SQLITE_OK);
773 /**
774 * @tc.steps:step2. open db
775 * @tc.expected: step2. Return OK.
776 */
777 RelationalStoreDelegate *delegate1 = nullptr;
778 DBStatus status1 = g_mgr.OpenStore(dbPath1 + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate1);
779 EXPECT_EQ(status1, OK);
780 ASSERT_NE(delegate1, nullptr);
781 RelationalStoreDelegate *delegate2 = nullptr;
782 DBStatus status2 = g_mgr.OpenStore(dbPath2 + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate2);
783 EXPECT_EQ(status2, OK);
784 ASSERT_NE(delegate2, nullptr);
785 /**
786 * @tc.steps:step3. create table
787 * @tc.expected: step3. Return OK.
788 */
789 SqlCondition sqlCondition1;
790 std::vector<VBucket> records1;
791 sqlCondition1.sql = "create table if not exists table1(id int);";
792 EXPECT_EQ(delegate1->ExecuteSql(sqlCondition1, records1), OK);
793 SqlCondition sqlCondition2;
794 std::vector<VBucket> records2;
795 sqlCondition2.sql = "create table if not exists table2(id int);";
796 EXPECT_EQ(delegate2->ExecuteSql(sqlCondition2, records2), OK);
797 /**
798 * @tc.steps:step4. check table
799 * @tc.expected: step4. Return OK.
800 */
801 db1 = RelationalTestUtils::CreateDataBase(dbPath1 + STORE_ID + DB_SUFFIX);
802 std::string sql1 = "select count(*) from sqlite_master where type = 'table' and name = 'table1'";
803 int tableCount1 = 0;
804 EXPECT_EQ(SQLiteUtils::GetCountBySql(db1, sql1, tableCount1), E_OK);
805 EXPECT_EQ(tableCount1, 1);
806 EXPECT_EQ(sqlite3_close_v2(db1), SQLITE_OK);
807 db2 = RelationalTestUtils::CreateDataBase(dbPath2 + STORE_ID + DB_SUFFIX);
808 std::string sql2 = "select count(*) from sqlite_master where type = 'table' and name = 'table2'";
809 int tableCount2 = 0;
810 EXPECT_EQ(SQLiteUtils::GetCountBySql(db2, sql2, tableCount2), E_OK);
811 EXPECT_EQ(tableCount2, 1);
812 EXPECT_EQ(sqlite3_close_v2(db2), SQLITE_OK);
813 status1 = g_mgr.CloseStore(delegate1);
814 EXPECT_EQ(status1, OK);
815 status2 = g_mgr.CloseStore(delegate2);
816 EXPECT_EQ(status2, OK);
817 }
818
819 /**
820 * @tc.name: RelationalTableModifyTest001
821 * @tc.desc: Test modify distributed table with compatible upgrade
822 * @tc.type: FUNC
823 * @tc.require:
824 * @tc.author: lianhuix
825 */
826 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest001, TestSize.Level1)
827 {
828 TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field INTEGER NOT NULL DEFAULT 123;",
829 DistributedDB::DEVICE_COOPERATION, OK);
830 }
831
832 /**
833 * @tc.name: RelationalTableModifyTest002
834 * @tc.desc: Test modify distributed table with incompatible upgrade
835 * @tc.type: FUNC
836 * @tc.require:
837 * @tc.author: lianhuix
838 */
839 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest002, TestSize.Level1)
840 {
841 TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field INTEGER NOT NULL;",
842 DistributedDB::DEVICE_COOPERATION, SCHEMA_MISMATCH);
843 }
844
845 /**
846 * @tc.name: RelationalTableModifyTest003
847 * @tc.desc: Test modify distributed table with incompatible upgrade
848 * @tc.type: FUNC
849 * @tc.require:
850 * @tc.author: lianhuix
851 */
852 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest003, TestSize.Level1)
853 {
854 TableModifyTest("ALTER TABLE sync_data DROP COLUMN w_timestamp;",
855 DistributedDB::DEVICE_COOPERATION, SCHEMA_MISMATCH);
856 }
857
858 /**
859 * @tc.name: RelationalTableModifyTest001
860 * @tc.desc: Test modify distributed table with compatible upgrade
861 * @tc.type: FUNC
862 * @tc.require:
863 * @tc.author: zhangshijie
864 */
865 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest001_1, TestSize.Level1)
866 {
867 TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field INTEGER NOT NULL DEFAULT 123;",
868 DistributedDB::CLOUD_COOPERATION, OK);
869 }
870
871 /**
872 * @tc.name: RelationalTableModifyTest002
873 * @tc.desc: Test modify distributed table with incompatible upgrade
874 * @tc.type: FUNC
875 * @tc.require:
876 * @tc.author: zhangshijie
877 */
878 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest002_1, TestSize.Level1)
879 {
880 TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field INTEGER NOT NULL;",
881 DistributedDB::CLOUD_COOPERATION, SCHEMA_MISMATCH);
882 }
883
884 /**
885 * @tc.name: RelationalTableModifyTest003
886 * @tc.desc: Test modify distributed table with incompatible upgrade
887 * @tc.type: FUNC
888 * @tc.require:
889 * @tc.author: zhangshijie
890 */
891 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest003_1, TestSize.Level1)
892 {
893 TableModifyTest("ALTER TABLE sync_data DROP COLUMN w_timestamp;",
894 DistributedDB::CLOUD_COOPERATION, SCHEMA_MISMATCH);
895 }
896
UpgradeDistributedTableTest(TableSyncType tableSyncType)897 void UpgradeDistributedTableTest(TableSyncType tableSyncType)
898 {
899 /**
900 * @tc.steps:step1. Prepare db file
901 * @tc.expected: step1. Return OK.
902 */
903 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
904 ASSERT_NE(db, nullptr);
905 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
906 if (tableSyncType == DistributedDB::DEVICE_COOPERATION) {
907 EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
908 } else {
909 EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
910 }
911
912 RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_A");
913 RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_B");
914 RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_C");
915
916 /**
917 * @tc.steps:step2. Open store
918 * @tc.expected: step2. return OK
919 */
920 RelationalStoreDelegate *delegate = nullptr;
921 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
922 EXPECT_EQ(status, OK);
923 ASSERT_NE(delegate, nullptr);
924
925 /**
926 * @tc.steps:step3. Create distributed table
927 * @tc.expected: step3. Create distributed table OK.
928 */
929 EXPECT_EQ(delegate->CreateDistributedTable("sync_data", tableSyncType), OK);
930
931 /**
932 * @tc.steps:step4. Upgrade table
933 * @tc.expected: step4. return OK
934 */
935 std::string modifySql = "ALTER TABLE sync_data ADD COLUMN add_field INTEGER;";
936 EXPECT_EQ(RelationalTestUtils::ExecSql(db, modifySql), SQLITE_OK);
937 std::string indexSql = "CREATE INDEX add_index ON sync_data (add_field);";
938 EXPECT_EQ(RelationalTestUtils::ExecSql(db, indexSql), SQLITE_OK);
939 std::string deleteIndexSql = "DROP INDEX IF EXISTS key_index";
940 EXPECT_EQ(RelationalTestUtils::ExecSql(db, deleteIndexSql), SQLITE_OK);
941 EXPECT_EQ(RelationalTestUtils::ExecSql(db, INSERT_SYNC_DATA_SQL), SQLITE_OK);
942
943 /**
944 * @tc.steps:step5. Create distributed table again
945 * @tc.expected: step5. Create distributed table return expect.
946 */
947 EXPECT_EQ(delegate->CreateDistributedTable("sync_data", tableSyncType), OK);
948
949 /**
950 * @tc.steps:step6. Close store
951 * @tc.expected: step6 Return OK.
952 */
953 status = g_mgr.CloseStore(delegate);
954 EXPECT_EQ(status, OK);
955 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
956 }
957
958 /**
959 * @tc.name: RelationalTableModifyTest004
960 * @tc.desc: Test upgrade distributed table with device table exists
961 * @tc.type: FUNC
962 * @tc.require:
963 * @tc.author: lianhuix
964 */
965 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest004, TestSize.Level1)
966 {
967 UpgradeDistributedTableTest(DistributedDB::DEVICE_COOPERATION);
968 }
969
970 /**
971 * @tc.name: RelationalTableModifyTest004
972 * @tc.desc: Test upgrade distributed table with device table exists
973 * @tc.type: FUNC
974 * @tc.require:
975 * @tc.author: zhangshijie
976 */
977 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest004_1, TestSize.Level1)
978 {
979 UpgradeDistributedTableTest(DistributedDB::CLOUD_COOPERATION);
980 }
981
982 /**
983 * @tc.name: RelationalTableModifyTest005
984 * @tc.desc: Test modify distributed table with compatible upgrade
985 * @tc.type: FUNC
986 * @tc.require:
987 * @tc.author: lianhuix
988 */
989 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest005, TestSize.Level1)
990 {
991 TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field STRING NOT NULL DEFAULT 'asdf';",
992 DistributedDB::DEVICE_COOPERATION, OK);
993 }
994
995 /**
996 * @tc.name: RelationalTableModifyTest005
997 * @tc.desc: Test modify distributed table with compatible upgrade
998 * @tc.type: FUNC
999 * @tc.require:
1000 * @tc.author: zhangshijie
1001 */
1002 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest005_1, TestSize.Level1)
1003 {
1004 TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field STRING NOT NULL DEFAULT 'asdf';",
1005 DistributedDB::CLOUD_COOPERATION, OK);
1006 }
1007
CheckTable(TableSyncType tableSyncType,RelationalStoreDelegate * delegate,sqlite3 * db)1008 void CheckTable(TableSyncType tableSyncType, RelationalStoreDelegate *delegate, sqlite3 *db)
1009 {
1010 /**
1011 * @tc.steps:step4. Create distributed table with a table with "UNIQUE"
1012 * @tc.expected: step4. return OK or NOT_SUPPORT.
1013 */
1014 std::string tableName4 = "t4";
1015 std::string createSql = "create table " + tableName4 + "(id int UNIQUE);";
1016 createSql += "create table t4_1(id int primary key, value text UNIQUE, name int);";
1017 createSql += "create table t4_2(id int primary key, value text UNIQUE , name int);";
1018 createSql += "create table t4_3(id int primary key, value text UNIQUE );";
1019 createSql += "create table t4_4(id int primary key, value text UNIQUE , name int);";
1020 createSql += "create table t4_5(id int unique);";
1021 createSql += "create table t4_6(id int primary key, value text UniqUe, name int);";
1022 createSql += "create table t4_7(id int primary key, uniquekey text , name int);";
1023 createSql += "create table t4_8(id int , name text, UNIQUE(id, name));";
1024 createSql += "create table t4_9(id int , name text,UNIQUE(id, name));";
1025 EXPECT_EQ(RelationalTestUtils::ExecSql(db, createSql), SQLITE_OK);
1026 DBStatus expectCode;
1027 if (tableSyncType == DEVICE_COOPERATION) {
1028 expectCode = OK;
1029 } else {
1030 expectCode = NOT_SUPPORT;
1031 }
1032 EXPECT_EQ(delegate->CreateDistributedTable(tableName4, tableSyncType), expectCode);
1033 EXPECT_EQ(delegate->CreateDistributedTable("t4_1", tableSyncType), expectCode);
1034 EXPECT_EQ(delegate->CreateDistributedTable("t4_2", tableSyncType), expectCode);
1035 EXPECT_EQ(delegate->CreateDistributedTable("t4_3", tableSyncType), expectCode);
1036 EXPECT_EQ(delegate->CreateDistributedTable("t4_4", tableSyncType), expectCode);
1037 EXPECT_EQ(delegate->CreateDistributedTable("t4_5", tableSyncType), expectCode);
1038 EXPECT_EQ(delegate->CreateDistributedTable("t4_6", tableSyncType), expectCode);
1039 EXPECT_EQ(delegate->CreateDistributedTable("t4_7", tableSyncType), OK);
1040 EXPECT_EQ(delegate->CreateDistributedTable("t4_8", tableSyncType), expectCode);
1041 EXPECT_EQ(delegate->CreateDistributedTable("t4_9", tableSyncType), expectCode);
1042 }
1043
TableConstraintsCheck(TableSyncType tableSyncType,RelationalStoreDelegate * delegate,sqlite3 * db)1044 void TableConstraintsCheck(TableSyncType tableSyncType, RelationalStoreDelegate *delegate, sqlite3 *db)
1045 {
1046 /**
1047 * @tc.steps:step1. Create distributed table with a table with "CHECK" key word
1048 * @tc.expected: step1. return NOT_SUPPORT or OK.
1049 */
1050 std::string tableName1 = "t1";
1051 std::string createSql = "create table " + tableName1 + "(id int CHECK(id > 5));";
1052 EXPECT_EQ(RelationalTestUtils::ExecSql(db, createSql), SQLITE_OK);
1053 int expectCode = OK;
1054 if (tableSyncType == DistributedDB::CLOUD_COOPERATION) {
1055 expectCode = NOT_SUPPORT;
1056 }
1057 EXPECT_EQ(delegate->CreateDistributedTable(tableName1, tableSyncType), expectCode);
1058
1059 /**
1060 * @tc.steps:step2. Create distributed table with a table with foreign key
1061 * @tc.expected: step2. return OK.
1062 */
1063 std::string tableName2 = "t2";
1064 createSql = "create table " + tableName2 + "(name text, t1_id int, FOREIGN KEY (t1_id) REFERENCES " + tableName1 +
1065 " (id));";
1066 EXPECT_EQ(RelationalTestUtils::ExecSql(db, createSql), SQLITE_OK);
1067 EXPECT_EQ(delegate->CreateDistributedTable(tableName2, tableSyncType), OK);
1068
1069 CheckTable(tableSyncType, delegate, db);
1070
1071 /**
1072 * @tc.steps:step3. Create distributed table with a table with "WITHOUT ROWID"
1073 * @tc.expected: step3. return NOT_SUPPORT.
1074 */
1075 std::string tableName3 = "t3";
1076 createSql = "create table " + tableName3 + "(id int primary key) WITHOUT ROWID;";
1077 EXPECT_EQ(RelationalTestUtils::ExecSql(db, createSql), SQLITE_OK);
1078 EXPECT_EQ(delegate->CreateDistributedTable(tableName3, tableSyncType), NOT_SUPPORT);
1079
1080 /**
1081 * @tc.steps:step5. Create distributed table with a table with primary key which is real
1082 * @tc.expected: step5. return OK or NOT_SUPPORT.
1083 */
1084 std::string tableName5 = "t5";
1085 createSql = "create table " + tableName5 + "(id REAL primary key);";
1086 EXPECT_EQ(RelationalTestUtils::ExecSql(db, createSql), SQLITE_OK);
1087 if (tableSyncType == DEVICE_COOPERATION) {
1088 expectCode = OK;
1089 } else {
1090 expectCode = NOT_SUPPORT;
1091 }
1092 EXPECT_EQ(delegate->CreateDistributedTable(tableName5, tableSyncType), expectCode);
1093
1094 /**
1095 * @tc.steps:step6. Create distributed table with a table with primary key which is ASSET
1096 * @tc.expected: step6. return OK or NOT_SUPPORT.
1097 */
1098 std::string tableName6 = "t6";
1099 createSql = "create table " + tableName6 + "(id ASSET primary key);";
1100 EXPECT_EQ(RelationalTestUtils::ExecSql(db, createSql), SQLITE_OK);
1101 if (tableSyncType == DEVICE_COOPERATION) {
1102 expectCode = OK;
1103 } else {
1104 expectCode = NOT_SUPPORT;
1105 }
1106 EXPECT_EQ(delegate->CreateDistributedTable(tableName6, tableSyncType), expectCode);
1107
1108 /**
1109 * @tc.steps:step7. Create distributed table with a table with primary key which is ASSETS
1110 * @tc.expected: step7. return OK or NOT_SUPPORT.
1111 */
1112 std::string tableName7 = "t7";
1113 createSql = "create table " + tableName7 + "(id assets primary key);";
1114 EXPECT_EQ(RelationalTestUtils::ExecSql(db, createSql), SQLITE_OK);
1115 if (tableSyncType == DEVICE_COOPERATION) {
1116 expectCode = OK;
1117 } else {
1118 expectCode = NOT_SUPPORT;
1119 }
1120 EXPECT_EQ(delegate->CreateDistributedTable(tableName7, tableSyncType), expectCode);
1121 }
1122
TableConstraintsTest(TableSyncType tableSyncType)1123 void TableConstraintsTest(TableSyncType tableSyncType)
1124 {
1125 /**
1126 * @tc.steps:step1. Prepare db file
1127 * @tc.expected: step1. Return OK.
1128 */
1129 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1130 ASSERT_NE(db, nullptr);
1131 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1132
1133 /**
1134 * @tc.steps:step2. Open store
1135 * @tc.expected: step2. return OK
1136 */
1137 RelationalStoreDelegate *delegate = nullptr;
1138 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1139 EXPECT_EQ(status, OK);
1140 ASSERT_NE(delegate, nullptr);
1141
1142 /**
1143 * @tc.steps:step3. check constraints
1144 */
1145 TableConstraintsCheck(tableSyncType, delegate, db);
1146
1147 /**
1148 * @tc.steps:step4. Close store
1149 * @tc.expected: step4 Return OK.
1150 */
1151 EXPECT_EQ(g_mgr.CloseStore(delegate), OK);
1152 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1153 }
1154
1155 /**
1156 * @tc.name: TableConstraintsTest001
1157 * @tc.desc: Test table constraints when create distributed table with DistributedDB::DEVICE_COOPERATION
1158 * @tc.type: FUNC
1159 * @tc.require:
1160 * @tc.author: zhangshijie
1161 */
1162 HWTEST_F(DistributedDBInterfacesRelationalTest, TableConstraintsTest001, TestSize.Level1)
1163 {
1164 TableConstraintsTest(DistributedDB::DEVICE_COOPERATION);
1165 }
1166
1167 /**
1168 * @tc.name: TableConstraintsTest002
1169 * @tc.desc: Test table constraints when create distributed table with DistributedDB::CLOUD_COOPERATION
1170 * @tc.type: FUNC
1171 * @tc.require:
1172 * @tc.author: zhangshijie
1173 */
1174 HWTEST_F(DistributedDBInterfacesRelationalTest, TableConstraintsTest002, TestSize.Level1)
1175 {
1176 TableConstraintsTest(DistributedDB::CLOUD_COOPERATION);
1177 }
1178
1179 /**
1180 * @tc.name: RelationalRemoveDeviceDataTest001
1181 * @tc.desc: Test remove device data
1182 * @tc.type: FUNC
1183 * @tc.require:
1184 * @tc.author: lianhuix
1185 */
1186 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalRemoveDeviceDataTest001, TestSize.Level1)
1187 {
1188 /**
1189 * @tc.steps:step1. Prepare db file
1190 * @tc.expected: step1. Return OK.
1191 */
1192 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1193 ASSERT_NE(db, nullptr);
1194 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1195 EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
1196 RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_A");
1197 RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_B");
1198 RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_C");
1199
1200 /**
1201 * @tc.steps:step2. Open store
1202 * @tc.expected: step2. return OK
1203 */
1204 RelationalStoreDelegate *delegate = nullptr;
1205 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1206 EXPECT_EQ(status, OK);
1207 ASSERT_NE(delegate, nullptr);
1208
1209 /**
1210 * @tc.steps:step3. Remove device data
1211 * @tc.expected: step3. ok
1212 */
1213 EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_A"), DISTRIBUTED_SCHEMA_NOT_FOUND);
1214 EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_D"), DISTRIBUTED_SCHEMA_NOT_FOUND);
1215 EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_A", "sync_data"), DISTRIBUTED_SCHEMA_NOT_FOUND);
1216 EXPECT_EQ(delegate->CreateDistributedTable("sync_data"), OK);
1217 EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_A"), OK);
1218 EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_B"), OK);
1219 EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_C", "sync_data"), OK);
1220 EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_D"), OK);
1221 EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_A", "sync_data_A"), DISTRIBUTED_SCHEMA_NOT_FOUND);
1222
1223 /**
1224 * @tc.steps:step4. Remove device data with invalid args
1225 * @tc.expected: step4. invalid
1226 */
1227 EXPECT_EQ(delegate->RemoveDeviceData(""), INVALID_ARGS);
1228 EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_A", "Handle-J@^."), INVALID_ARGS);
1229
1230 /**
1231 * @tc.steps:step5. Close store
1232 * @tc.expected: step5 Return OK.
1233 */
1234 status = g_mgr.CloseStore(delegate);
1235 EXPECT_EQ(status, OK);
1236 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1237 }
1238
1239 struct TableT1 {
1240 int a;
1241 std::string b;
1242 int rowid;
1243 int flag;
1244 int timestamp;
1245
operator ()__anon61815a6f0111::TableT11246 VirtualRowData operator() () const
1247 {
1248 VirtualRowData rowData;
1249 DataValue dA;
1250 dA = static_cast<int64_t>(a);
1251 rowData.objectData.PutDataValue("a", dA);
1252 DataValue dB;
1253 dB.SetText(b);
1254 rowData.objectData.PutDataValue("b", dB);
1255 rowData.logInfo.dataKey = rowid;
1256 rowData.logInfo.device = DEVICE_B;
1257 rowData.logInfo.originDev = DEVICE_B;
1258 rowData.logInfo.timestamp = timestamp;
1259 rowData.logInfo.wTimestamp = timestamp;
1260 rowData.logInfo.flag = flag;
1261 Key key;
1262 DBCommon::StringToVector(std::to_string(rowid), key);
1263 std::vector<uint8_t> hashKey;
1264 DBCommon::CalcValueHash(key, hashKey);
1265 rowData.logInfo.hashKey = hashKey;
1266 return rowData;
1267 }
1268 };
1269
AddDeviceSchema(RelationalVirtualDevice * device,sqlite3 * db,const std::string & name)1270 void AddDeviceSchema(RelationalVirtualDevice *device, sqlite3 *db, const std::string &name)
1271 {
1272 TableInfo table;
1273 SQLiteUtils::AnalysisSchema(db, name, table);
1274 device->SetLocalFieldInfo(table.GetFieldInfos());
1275 device->SetTableInfo(table);
1276 }
1277
AddErrorTrigger(sqlite3 * db,const std::string & name)1278 void AddErrorTrigger(sqlite3 *db, const std::string &name)
1279 {
1280 ASSERT_NE(db, nullptr);
1281 ASSERT_NE(name, "");
1282 std::string sql = "CREATE TRIGGER IF NOT EXISTS "
1283 "naturalbase_rdb_aux_" + name + "_log_ON_DELETE BEFORE DELETE \n"
1284 "ON naturalbase_rdb_aux_" + name + "_log \n"
1285 "BEGIN \n"
1286 "\t INSERT INTO naturalbase_rdb_aux_" + name + "_log VALUES(no_exist_func(), '', '', '', '', '', ''); \n"
1287 "END;";
1288 char *errMsg = nullptr;
1289 EXPECT_EQ(sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &errMsg), SQLITE_OK);
1290 if (errMsg != nullptr) {
1291 LOGE("sql error %s", errMsg);
1292 LOGE("sql %s", sql.c_str());
1293 }
1294 }
1295
1296 /**
1297 * @tc.name: RelationalRemoveDeviceDataTest002
1298 * @tc.desc: Test remove device data and syn
1299 * @tc.type: FUNC
1300 * @tc.require:
1301 * @tc.author: lianhuix
1302 */
1303 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalRemoveDeviceDataTest002, TestSize.Level1)
1304 {
1305 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1306 ASSERT_NE(db, nullptr);
1307 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1308 EXPECT_EQ(RelationalTestUtils::ExecSql(db, SIMPLE_CREATE_TABLE_SQL), SQLITE_OK);
1309 AddDeviceSchema(g_deviceB, db, "t1");
1310
1311 RelationalStoreDelegate *delegate = nullptr;
1312 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1313 EXPECT_EQ(status, OK);
1314 ASSERT_NE(delegate, nullptr);
1315
1316 EXPECT_EQ(delegate->CreateDistributedTable("t1"), OK);
1317
1318 g_deviceB->PutDeviceData("t1", std::vector<TableT1>{
1319 {1, "111", 1, 2, 1}, // test data
1320 {2, "222", 2, 2, 2}, // test data
1321 {3, "333", 3, 2, 3}, // test data
1322 {4, "444", 4, 2, 4} // test data
1323 });
1324 std::vector<std::string> devices = {DEVICE_B};
1325 Query query = Query::Select("t1").NotEqualTo("a", 0);
1326 status = delegate->Sync(devices, SyncMode::SYNC_MODE_PULL_ONLY, query,
__anon61815a6f0302(const std::map<std::string, std::vector<TableStatus>> &devicesMap) 1327 [&devices](const std::map<std::string, std::vector<TableStatus>> &devicesMap) {
1328 EXPECT_EQ(devicesMap.size(), devices.size());
1329 EXPECT_EQ(devicesMap.at(DEVICE_B)[0].status, OK);
1330 }, true);
1331 EXPECT_EQ(status, OK);
1332
1333 EXPECT_EQ(delegate->RemoveDeviceData(DEVICE_B), OK);
1334
1335 int logCnt = -1;
1336 std::string checkLogSql = "SELECT count(*) FROM naturalbase_rdb_aux_t1_log";
__anon61815a6f0402(sqlite3_stmt *stmt) 1337 RelationalTestUtils::ExecSql(db, checkLogSql, nullptr, [&logCnt](sqlite3_stmt *stmt) {
1338 logCnt = sqlite3_column_int(stmt, 0);
1339 return E_OK;
1340 });
1341 EXPECT_EQ(logCnt, 0);
1342
1343 int dataCnt = -1;
1344 std::string deviceTable = g_mgr.GetDistributedTableName(DEVICE_B, "t1");
1345 std::string checkDataSql = "SELECT count(*) FROM " + deviceTable;
__anon61815a6f0502(sqlite3_stmt *stmt) 1346 RelationalTestUtils::ExecSql(db, checkDataSql, nullptr, [&dataCnt](sqlite3_stmt *stmt) {
1347 dataCnt = sqlite3_column_int(stmt, 0);
1348 return E_OK;
1349 });
1350 EXPECT_EQ(logCnt, 0);
1351
1352 status = g_mgr.CloseStore(delegate);
1353 EXPECT_EQ(status, OK);
1354 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1355 }
1356
TestRemoveDeviceDataWithCallback(bool removeAll)1357 void TestRemoveDeviceDataWithCallback(bool removeAll)
1358 {
1359 /**
1360 * @tc.steps:step1. Prepare db and data
1361 * @tc.expected: step1. Return OK.
1362 */
1363 RuntimeConfig::SetTranslateToDeviceIdCallback([](const std::string &oriDevId, const StoreInfo &info) {
1364 return oriDevId + "_" + info.appId;
1365 });
1366 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1367 ASSERT_NE(db, nullptr);
1368 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1369 EXPECT_EQ(RelationalTestUtils::ExecSql(db, SIMPLE_CREATE_TABLE_SQL), SQLITE_OK);
1370 AddDeviceSchema(g_deviceB, db, "t1");
1371 RelationalStoreDelegate *delegate = nullptr;
1372 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1373 EXPECT_EQ(status, OK);
1374 ASSERT_NE(delegate, nullptr);
1375 EXPECT_EQ(delegate->CreateDistributedTable("t1"), OK);
1376 g_deviceB->PutDeviceData("t1", std::vector<TableT1> {
1377 {1, "111", 1, 0, 1} // test data
1378 });
1379 std::vector<std::string> devices = {DEVICE_B};
1380 Query query = Query::Select("t1").EqualTo("a", 1);
1381 status = delegate->Sync(devices, SyncMode::SYNC_MODE_PULL_ONLY, query,
1382 [&devices](const std::map<std::string, std::vector<TableStatus>> &devicesMap) {
1383 ASSERT_EQ(devicesMap.size(), devices.size());
1384 EXPECT_EQ(devicesMap.at(DEVICE_B)[0].status, OK);
1385 }, true);
1386 EXPECT_EQ(status, OK);
1387 /**
1388 * @tc.steps:step2. remove device data and check table
1389 * @tc.expected: step2. dev table not exist and log not exist device b.
1390 */
1391 std::this_thread::sleep_for(std::chrono::seconds(1));
1392 if (removeAll) {
1393 EXPECT_EQ(delegate->RemoveDeviceData(), OK);
1394 } else {
1395 EXPECT_EQ(delegate->RemoveDeviceData(DEVICE_B + "_" + APP_ID), OK);
1396 }
1397 int logCnt = -1;
1398 std::string checkLogSql = "SELECT count(*) FROM naturalbase_rdb_aux_t1_log";
1399 RelationalTestUtils::ExecSql(db, checkLogSql, nullptr, [&logCnt](sqlite3_stmt *stmt) {
1400 logCnt = sqlite3_column_int(stmt, 0);
1401 return E_OK;
1402 });
1403 EXPECT_EQ(logCnt, 0);
1404 std::string deviceTable = RelationalStoreManager::GetDistributedTableName(DEVICE_B + "_" + APP_ID, "t1");
1405 std::string checkDataSql = "SELECT count(*) FROM " + deviceTable;
1406 EXPECT_NE(RelationalTestUtils::ExecSql(db, checkDataSql, nullptr, nullptr), SQLITE_OK);
1407 /**
1408 * @tc.steps:step3. close db
1409 * @tc.expected: step3. Return OK.
1410 */
1411 status = g_mgr.CloseStore(delegate);
1412 EXPECT_EQ(status, OK);
1413 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1414 RuntimeConfig::SetTranslateToDeviceIdCallback(nullptr);
1415 }
1416
1417 /**
1418 * @tc.name: RelationalRemoveDeviceDataTest003
1419 * @tc.desc: Test remove all device data and sync again
1420 * @tc.type: FUNC
1421 * @tc.require:
1422 * @tc.author: zhangqiquan
1423 */
1424 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalRemoveDeviceDataTest003, TestSize.Level1)
1425 {
1426 TestRemoveDeviceDataWithCallback(true);
1427 }
1428
1429 /**
1430 * @tc.name: RelationalRemoveDeviceDataTest004
1431 * @tc.desc: Test remove one device data and sync again
1432 * @tc.type: FUNC
1433 * @tc.require:
1434 * @tc.author: zhangqiquan
1435 */
1436 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalRemoveDeviceDataTest004, TestSize.Level1)
1437 {
1438 TestRemoveDeviceDataWithCallback(false);
1439 }
1440
1441 /**
1442 * @tc.name: RelationalRemoveDeviceDataTest005
1443 * @tc.desc: Test remove device data with invalid param
1444 * @tc.type: FUNC
1445 * @tc.require:
1446 * @tc.author: zhangqiquan
1447 */
1448 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalRemoveDeviceDataTest005, TestSize.Level1)
1449 {
1450 /**
1451 * @tc.steps:step1. Prepare db file
1452 * @tc.expected: step1. Return OK.
1453 */
1454 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1455 ASSERT_NE(db, nullptr);
1456 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1457 EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
1458 RelationalTestUtils::CreateDeviceTable(db, "sync_data", "DEVICE_A");
1459 AddDeviceSchema(g_deviceB, db, "sync_data");
1460 /**
1461 * @tc.steps:step2. Open store
1462 * @tc.expected: step2. return OK
1463 */
1464 RelationalStoreDelegate *delegate = nullptr;
1465 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1466 EXPECT_EQ(status, OK);
1467 ASSERT_NE(delegate, nullptr);
1468 int count = 0;
__anon61815a6f0902(const std::string &oriDevId, const StoreInfo &info) 1469 RuntimeConfig::SetTranslateToDeviceIdCallback([&count](const std::string &oriDevId, const StoreInfo &info) {
1470 count++;
1471 return oriDevId + "_" + info.appId;
1472 });
1473 /**
1474 * @tc.steps:step3. Remove not exist device data
1475 * @tc.expected: step3. ok
1476 */
1477 EXPECT_EQ(delegate->CreateDistributedTable("sync_data"), OK);
1478 EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_C", "sync_data"), OK);
1479 EXPECT_EQ(count, 0);
1480 /**
1481 * @tc.steps:step4. Close store
1482 * @tc.expected: step4 Return OK.
1483 */
1484 status = g_mgr.CloseStore(delegate);
1485 EXPECT_EQ(status, OK);
1486 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1487 RuntimeConfig::SetTranslateToDeviceIdCallback(nullptr);
1488 }
1489
1490 /**
1491 * @tc.name: RelationalRemoveDeviceDataTest006
1492 * @tc.desc: Test remove device data with busy
1493 * @tc.type: FUNC
1494 * @tc.require:
1495 * @tc.author: zhangqiquan
1496 */
1497 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalRemoveDeviceDataTest006, TestSize.Level1)
1498 {
1499 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1500 ASSERT_NE(db, nullptr);
1501 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1502 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "CREATE TABLE IF NOT EXISTS t2(a INT, b TEXT)"), SQLITE_OK);
1503 AddDeviceSchema(g_deviceB, db, "t2");
1504
1505 RelationalStoreDelegate *delegate = nullptr;
1506 auto observer = new (std::nothrow) RelationalStoreObserverUnitTest();
1507 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID,
1508 { .observer = observer }, delegate);
1509 EXPECT_EQ(status, OK);
1510 ASSERT_NE(delegate, nullptr);
1511
1512 EXPECT_EQ(delegate->CreateDistributedTable("t2"), OK);
1513
1514 g_deviceB->PutDeviceData("t2", std::vector<TableT1>{
1515 {1, "111", 1, 2, 1}, // test data
1516 });
1517 std::vector<std::string> devices = {DEVICE_B};
1518 Query query = Query::Select("t2");
1519 status = delegate->Sync(devices, SyncMode::SYNC_MODE_PULL_ONLY, query, nullptr, true);
1520 EXPECT_EQ(status, OK);
1521
1522 auto beforeCallCount = observer->GetCallCount();
1523 AddErrorTrigger(db, "t2");
1524 EXPECT_EQ(delegate->RemoveDeviceData(DEVICE_B), DB_ERROR);
1525
1526 status = delegate->Sync(devices, SyncMode::SYNC_MODE_PULL_ONLY, query, nullptr, true);
1527 EXPECT_EQ(status, OK);
1528 auto afterCallCount = observer->GetCallCount();
1529 EXPECT_NE(beforeCallCount, afterCallCount);
1530
1531 status = g_mgr.CloseStore(delegate);
1532 EXPECT_EQ(status, OK);
1533 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1534 delete observer;
1535 }
1536
1537 /**
1538 * @tc.name: RelationalOpenStorePathCheckTest001
1539 * @tc.desc: Test open store with same label but different path.
1540 * @tc.type: FUNC
1541 * @tc.require:
1542 * @tc.author: lianhuix
1543 */
1544 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalOpenStorePathCheckTest001, TestSize.Level1)
1545 {
1546 std::string dir1 = g_dbDir + "dbDir1";
1547 EXPECT_EQ(OS::MakeDBDirectory(dir1), E_OK);
1548 sqlite3 *db1 = RelationalTestUtils::CreateDataBase(dir1 + STORE_ID + DB_SUFFIX);
1549 ASSERT_NE(db1, nullptr);
1550 EXPECT_EQ(RelationalTestUtils::ExecSql(db1, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1551 EXPECT_EQ(RelationalTestUtils::ExecSql(db1, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
1552 EXPECT_EQ(sqlite3_close_v2(db1), SQLITE_OK);
1553
1554 std::string dir2 = g_dbDir + "dbDir2";
1555 EXPECT_EQ(OS::MakeDBDirectory(dir2), E_OK);
1556 sqlite3 *db2 = RelationalTestUtils::CreateDataBase(dir2 + STORE_ID + DB_SUFFIX);
1557 ASSERT_NE(db2, nullptr);
1558 EXPECT_EQ(RelationalTestUtils::ExecSql(db2, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1559 EXPECT_EQ(RelationalTestUtils::ExecSql(db2, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
1560 EXPECT_EQ(sqlite3_close_v2(db2), SQLITE_OK);
1561
1562 DBStatus status = OK;
1563 RelationalStoreDelegate *delegate1 = nullptr;
1564 status = g_mgr.OpenStore(dir1 + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate1);
1565 EXPECT_EQ(status, OK);
1566 ASSERT_NE(delegate1, nullptr);
1567
1568 RelationalStoreDelegate *delegate2 = nullptr;
1569 status = g_mgr.OpenStore(dir2 + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate2);
1570 EXPECT_EQ(status, OK);
1571 ASSERT_NE(delegate2, nullptr);
1572
1573 status = g_mgr.CloseStore(delegate1);
1574 EXPECT_EQ(status, OK);
1575
1576 status = g_mgr.CloseStore(delegate2);
1577 EXPECT_EQ(status, OK);
1578 }
1579
1580 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalOpenStorePressureTest001, TestSize.Level1)
1581 {
1582 /**
1583 * @tc.steps:step1. Prepare db file
1584 * @tc.expected: step1. Return OK.
1585 */
1586 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1587 ASSERT_NE(db, nullptr);
1588 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1589 EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
1590 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1591
1592 DBStatus status = OK;
1593 for (int i = 0; i < 1000; i++) {
1594 RelationalStoreDelegate *delegate = nullptr;
1595 status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1596 EXPECT_EQ(status, OK);
1597 ASSERT_NE(delegate, nullptr);
1598
1599 status = g_mgr.CloseStore(delegate);
1600 EXPECT_EQ(status, OK);
1601 delegate = nullptr;
1602 }
1603 }
1604
1605 HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalOpenStorePressureTest002, TestSize.Level1)
1606 {
1607 /**
1608 * @tc.steps:step1. Prepare db file
1609 * @tc.expected: step1. Return OK.
1610 */
1611 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1612 ASSERT_NE(db, nullptr);
1613 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1614 EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK);
1615 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1616
1617 std::queue<RelationalStoreDelegate *> delegateQueue;
1618 std::mutex queueLock;
1619 std::random_device rd;
1620 default_random_engine e(rd());
1621 uniform_int_distribution<unsigned> u(0, 9);
1622
__anon61815a6f0a02() 1623 std::thread openStoreThread([&, this]() {
1624 for (int i = 0; i < 1000; i++) {
1625 LOGD("++++> open store delegate: %d", i);
1626 RelationalStoreDelegate *delegate = nullptr;
1627 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1628 EXPECT_EQ(status, OK);
1629 ASSERT_NE(delegate, nullptr);
1630 {
1631 std::lock_guard<std::mutex> lock(queueLock);
1632 delegateQueue.push(delegate);
1633 }
1634 LOGD("++++< open store delegate: %d", i);
1635 }
1636 });
1637
1638 int cnt = 0;
1639 while (cnt < 1000) {
1640 RelationalStoreDelegate *delegate = nullptr;
1641 {
1642 std::lock_guard<std::mutex> lock(queueLock);
1643 if (delegateQueue.empty()) {
1644 std::this_thread::sleep_for(std::chrono::microseconds(100));
1645 continue;
1646 }
1647 delegate = delegateQueue.front();
1648 delegateQueue.pop();
1649 }
1650 LOGD("++++> close store delegate: %d", cnt);
1651 DBStatus status = g_mgr.CloseStore(delegate);
1652 LOGD("++++< close store delegate: %d", cnt);
1653 EXPECT_EQ(status, OK);
1654 delegate = nullptr;
1655 cnt++;
1656 std::this_thread::sleep_for(std::chrono::microseconds(100 * u(e)));
1657 }
1658 openStoreThread.join();
1659 }
1660
1661 namespace {
ProcessSync(RelationalStoreDelegate * delegate)1662 void ProcessSync(RelationalStoreDelegate *delegate)
1663 {
1664 std::vector<std::string> devices = {DEVICE_B};
1665 Query query = Query::Select("create").EqualTo("create", 1);
1666 DBStatus status = delegate->Sync(devices, SyncMode::SYNC_MODE_PUSH_ONLY, query,
1667 [&devices](const std::map<std::string, std::vector<TableStatus>> &devicesMap) {
1668 EXPECT_EQ(devicesMap.size(), devices.size());
1669 EXPECT_EQ(devicesMap.at(DEVICE_B)[0].status, OK);
1670 }, true);
1671 EXPECT_EQ(status, OK);
1672
1673 std::vector<VirtualRowData> data;
1674 g_deviceB->GetAllSyncData("create", data);
1675 EXPECT_EQ(data.size(), 1u);
1676
1677 VirtualRowData virtualRowData;
1678 DataValue d1;
1679 d1 = static_cast<int64_t>(2); // 2: test data
1680 virtualRowData.objectData.PutDataValue("create", d1);
1681 DataValue d2;
1682 d2.SetText("hello");
1683 virtualRowData.objectData.PutDataValue("ddd", d2);
1684 DataValue d3;
1685 d3.SetText("hello");
1686 virtualRowData.objectData.PutDataValue("eee", d3);
1687 virtualRowData.logInfo.timestamp = 1;
1688 g_deviceB->PutData("create", {virtualRowData});
1689 status = delegate->Sync(devices, SyncMode::SYNC_MODE_PULL_ONLY, query,
1690 [&devices](const std::map<std::string, std::vector<TableStatus>> &devicesMap) {
1691 EXPECT_EQ(devicesMap.size(), devices.size());
1692 EXPECT_EQ(devicesMap.at(DEVICE_B)[0].status, OK);
1693 }, true);
1694 EXPECT_EQ(status, OK);
1695 }
1696 }
1697
1698 /**
1699 * @tc.name: SqliteKeyWord001
1700 * @tc.desc: Test relational table with sqlite key world.
1701 * @tc.type: FUNC
1702 * @tc.require:
1703 * @tc.author: lianhuix
1704 */
1705 HWTEST_F(DistributedDBInterfacesRelationalTest, SqliteKeyWordTest001, TestSize.Level1)
1706 {
1707 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1708 ASSERT_NE(db, nullptr);
1709 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1710
1711 std::string tableSql = "CREATE TABLE IF NOT EXISTS 'create' ('create' INTEGER PRIMARY KEY, b 'CREATE', " \
1712 "c TEXT DEFAULT 'DEFAULT', UNIQUE(b, c))";
1713 EXPECT_EQ(RelationalTestUtils::ExecSql(db, tableSql), SQLITE_OK);
1714 std::string indexSql = "CREATE INDEX IF NOT EXISTS 'index' on 'create' (b)";
1715 EXPECT_EQ(RelationalTestUtils::ExecSql(db, indexSql), SQLITE_OK);
1716
1717 PrepareVirtualDeviceEnv("create", g_dbDir + STORE_ID + DB_SUFFIX, {g_deviceB});
1718
1719 DBStatus status = OK;
1720 RelationalStoreDelegate *delegate = nullptr;
1721 status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1722 EXPECT_EQ(status, OK);
1723 ASSERT_NE(delegate, nullptr);
1724
1725 status = delegate->CreateDistributedTable("create");
1726 EXPECT_EQ(status, OK);
1727
1728 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "INSERT INTO 'create' values(1, 'bbb', 'ccc')"), SQLITE_OK);
1729
1730 std::string insertSql = "INSERT INTO 'create' values(1001, 'create', 'text')";
1731 EXPECT_EQ(RelationalTestUtils::ExecSql(db, insertSql), SQLITE_OK);
1732
1733 std::string updateSql = "UPDATE 'create' SET 'create' = 1002 WHERE b = 'create'";
1734 EXPECT_EQ(RelationalTestUtils::ExecSql(db, updateSql), SQLITE_OK);
1735
1736 std::string deleteSql = "DELETE FROM 'create' WHERE 'create' = 1002";
1737 EXPECT_EQ(RelationalTestUtils::ExecSql(db, deleteSql), SQLITE_OK);
1738
1739 ProcessSync(delegate);
1740
1741 std::string alterSql = "ALTER TABLE 'create' ADD COLUMN 'table' 'insert' DEFAULT NULL";
1742 EXPECT_EQ(RelationalTestUtils::ExecSql(db, alterSql), SQLITE_OK);
1743 status = delegate->CreateDistributedTable("create");
1744 EXPECT_EQ(status, OK);
1745
1746 status = delegate->RemoveDeviceData("DEVICES_B", "create");
1747 EXPECT_EQ(status, OK);
1748
1749 status = g_mgr.CloseStore(delegate);
1750 EXPECT_EQ(status, OK);
1751 delegate = nullptr;
1752 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1753 }
1754
1755 /**
1756 * @tc.name: GetDistributedTableName001
1757 * @tc.desc: Test get distributed table name
1758 * @tc.type: FUNC
1759 * @tc.require:
1760 * @tc.author: zhangqiquan
1761 */
1762 HWTEST_F(DistributedDBInterfacesRelationalTest, GetDistributedTableName001, TestSize.Level1)
1763 {
1764 const std::string deviceName = "DEVICES_A";
1765 const std::string tableName = "TABLE";
1766 const std::string hashDev = DBCommon::TransferStringToHex(DBCommon::TransferHashString(deviceName));
1767
1768 std::string devTableName = RelationalStoreManager::GetDistributedTableName(deviceName, tableName);
1769 EXPECT_EQ(devTableName, DBConstant::RELATIONAL_PREFIX + tableName + "_" + hashDev);
__anon61815a6f0e02(const std::string &oriDevId, const StoreInfo &info) 1770 RuntimeConfig::SetTranslateToDeviceIdCallback([](const std::string &oriDevId, const StoreInfo &info) {
1771 EXPECT_EQ(info.appId, "");
1772 return oriDevId;
1773 });
1774 devTableName = RelationalStoreManager::GetDistributedTableName(deviceName, tableName);
1775 EXPECT_EQ(devTableName, DBConstant::RELATIONAL_PREFIX + tableName + "_" + deviceName);
1776 devTableName = RelationalStoreManager::GetDistributedTableName("", tableName);
1777 EXPECT_EQ(devTableName, DBConstant::RELATIONAL_PREFIX + tableName + "_");
1778 RuntimeConfig::SetTranslateToDeviceIdCallback(nullptr);
1779 }
1780
1781 /**
1782 * @tc.name: CloudRelationalStoreTest001
1783 * @tc.desc: Test create distributed table in cloud table sync type
1784 * @tc.type: FUNC
1785 * @tc.require:
1786 * @tc.author: zhangshjie
1787 */
1788 HWTEST_F(DistributedDBInterfacesRelationalTest, CreateDistributedTableTest001, TestSize.Level0)
1789 {
1790 /**
1791 * @tc.steps:step1. Prepare db file
1792 * @tc.expected: step1. Return OK.
1793 */
1794 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1795 ASSERT_NE(db, nullptr);
1796 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1797 EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
1798 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1799
1800 /**
1801 * @tc.steps:step2. open relational store, create distributed table with CLOUD_COOPERATION
1802 * @tc.expected: step2. Return OK.
1803 */
1804 RelationalStoreDelegate *delegate = nullptr;
1805 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1806 EXPECT_EQ(status, OK);
1807 ASSERT_NE(delegate, nullptr);
1808
1809 status = delegate->CreateDistributedTable("sync_data", DistributedDB::CLOUD_COOPERATION);
1810 EXPECT_EQ(status, OK);
1811
1812 /**
1813 * @tc.steps:step3. open relational store, create distributed table with CLOUD_COOPERATION again
1814 * @tc.expected: step3. Return OK.
1815 */
1816 status = delegate->CreateDistributedTable("sync_data", DistributedDB::CLOUD_COOPERATION);
1817 EXPECT_EQ(status, OK);
1818
1819 status = g_mgr.CloseStore(delegate);
1820 EXPECT_EQ(status, OK);
1821 }
1822
1823 /**
1824 * @tc.name: CloudRelationalStoreTest002
1825 * @tc.desc: Test create distributed table in diff table sync type for the same table
1826 * @tc.type: FUNC
1827 * @tc.require:
1828 * @tc.author: zhangshjie
1829 */
1830 HWTEST_F(DistributedDBInterfacesRelationalTest, CreateDistributedTableTest002, TestSize.Level1)
1831 {
1832 /**
1833 * @tc.steps:step1. Prepare db file
1834 * @tc.expected: step1. Return OK.
1835 */
1836 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1837 ASSERT_NE(db, nullptr);
1838 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1839 EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
1840 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1841
1842 /**
1843 * @tc.steps:step2. open relational store, create distributed table with DEVICE_COOPERATION
1844 * @tc.expected: step2. Return OK.
1845 */
1846 RelationalStoreDelegate *delegate = nullptr;
1847 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1848 EXPECT_EQ(status, OK);
1849 ASSERT_NE(delegate, nullptr);
1850
1851 status = delegate->CreateDistributedTable("sync_data", DistributedDB::DEVICE_COOPERATION);
1852 EXPECT_EQ(status, OK);
1853
1854 /**
1855 * @tc.steps:step3. create distributed table with CLOUD_COOPERATION again
1856 * @tc.expected: step3. Return TYPE_MISMATCH.
1857 */
1858 status = delegate->CreateDistributedTable("sync_data", DistributedDB::CLOUD_COOPERATION);
1859 EXPECT_EQ(status, TYPE_MISMATCH);
1860
1861 status = g_mgr.CloseStore(delegate);
1862 EXPECT_EQ(status, OK);
1863 delegate = nullptr;
1864
1865 /**
1866 * @tc.steps:step4. drop table sync_data and create again
1867 * @tc.expected: step4. Return OK.
1868 */
1869 db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1870 ASSERT_NE(db, nullptr);
1871 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1872 const std::string dropSql = "drop table sync_data;";
1873 EXPECT_EQ(RelationalTestUtils::ExecSql(db, dropSql), SQLITE_OK);
1874 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1875
1876 /**
1877 * @tc.steps:step5. open relational store, create distributed table with CLOUD_COOPERATION
1878 * @tc.expected: step5. Return OK.
1879 */
1880 status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1881 EXPECT_EQ(status, OK);
1882 ASSERT_NE(delegate, nullptr);
1883
1884 db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1885 ASSERT_NE(db, nullptr);
1886 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1887 EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
1888 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1889
1890 EXPECT_EQ(delegate->CreateDistributedTable("sync_data", DistributedDB::CLOUD_COOPERATION), OK);
1891
1892 /**
1893 * @tc.steps:step6. create distributed table with DEVICE_COOPERATION again
1894 * @tc.expected: step6. Return TYPE_MISMATCH.
1895 */
1896 status = delegate->CreateDistributedTable("sync_data", DistributedDB::DEVICE_COOPERATION);
1897 EXPECT_EQ(status, TYPE_MISMATCH);
1898
1899 status = g_mgr.CloseStore(delegate);
1900 EXPECT_EQ(status, OK);
1901 delegate = nullptr;
1902 }
1903
1904 /**
1905 * @tc.name: CreateDistributedTableTest003
1906 * @tc.desc: Test create distributed table after rename table
1907 * @tc.type: FUNC
1908 * @tc.require:
1909 * @tc.author: zhangqiquan
1910 */
1911 HWTEST_F(DistributedDBInterfacesRelationalTest, CreateDistributedTableTest003, TestSize.Level1)
1912 {
1913 /**
1914 * @tc.steps:step1. Prepare db file
1915 */
1916 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1917 ASSERT_NE(db, nullptr);
1918 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1919 EXPECT_EQ(RelationalTestUtils::ExecSql(db, SIMPLE_CREATE_TABLE_SQL), SQLITE_OK);
1920 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1921 /**
1922 * @tc.steps:step2. open relational store, create distributed table with default mode
1923 */
1924 RelationalStoreDelegate *delegate = nullptr;
1925 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1926 EXPECT_EQ(status, OK);
1927 ASSERT_NE(delegate, nullptr);
1928
1929 status = delegate->CreateDistributedTable("t1");
1930 EXPECT_EQ(status, OK);
1931 /**
1932 * @tc.steps:step3. rename table
1933 */
1934 db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1935 ASSERT_NE(db, nullptr);
1936 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "alter table t1 rename to t2;"), SQLITE_OK);
1937 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1938 /**
1939 * @tc.steps:step4. reopen delegate
1940 */
1941 status = g_mgr.CloseStore(delegate);
1942 EXPECT_EQ(status, OK);
1943 delegate = nullptr;
1944 status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
1945 EXPECT_EQ(status, OK);
1946 ASSERT_NE(delegate, nullptr);
1947 /**
1948 * @tc.steps:step5. create distributed table and insert data ok
1949 */
1950 status = delegate->CreateDistributedTable("t2");
1951 EXPECT_EQ(status, OK);
1952
1953 db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1954 ASSERT_NE(db, nullptr);
1955 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "insert into t2 values(1, '2');"), SQLITE_OK);
1956 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1957 /**
1958 * @tc.steps:step6. close store
1959 */
1960 status = g_mgr.CloseStore(delegate);
1961 EXPECT_EQ(status, OK);
1962 delegate = nullptr;
1963 }
1964
1965 /**
1966 * @tc.name: CreateDistributedTableTest004
1967 * @tc.desc: Test create distributed table will violate the constraint when table contains "_rowid_" column
1968 * @tc.type: FUNC
1969 * @tc.require:
1970 * @tc.author: zhangshijie
1971 */
1972 HWTEST_F(DistributedDBInterfacesRelationalTest, CreateDistributedTableTest004, TestSize.Level0)
1973 {
1974 /**
1975 * @tc.steps:step1. Prepare db and table
1976 */
1977 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
1978 ASSERT_NE(db, nullptr);
1979 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
1980 std::string t1 = "t1";
1981 std::string sql = "create table " + t1 + "(key text, _rowid_ int);";
1982 EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK);
1983 std::string t2 = "t2";
1984 sql = "create table " + t2 + "(rowid int);";
1985 EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK);
1986 std::string t3 = "t3";
1987 sql = "create table " + t3 + "(oid int);";
1988 EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK);
1989 std::string t4 = "t4";
1990 sql = "create table " + t4 + "(rowid int, oid int, _rowid_ text);";
1991 EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK);
1992 std::string t5 = "t5";
1993 sql = "create table " + t5 + "(_RoWiD_ int);";
1994 EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK);
1995 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
1996
1997 /**
1998 * @tc.steps:step2. open relational store, create distributed table
1999 */
2000 RelationalStoreDelegate *delegate = nullptr;
2001 EXPECT_EQ(g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate), OK);
2002 ASSERT_NE(delegate, nullptr);
2003 EXPECT_EQ(delegate->CreateDistributedTable(t1), NOT_SUPPORT);
2004 EXPECT_EQ(delegate->CreateDistributedTable(t1, DistributedDB::CLOUD_COOPERATION), NOT_SUPPORT);
2005 EXPECT_EQ(delegate->CreateDistributedTable(t2), OK);
2006 EXPECT_EQ(delegate->CreateDistributedTable(t3), OK);
2007 EXPECT_EQ(delegate->CreateDistributedTable(t4), NOT_SUPPORT);
2008 EXPECT_EQ(delegate->CreateDistributedTable(t5), NOT_SUPPORT);
2009
2010 EXPECT_EQ(g_mgr.CloseStore(delegate), OK);
2011 delegate = nullptr;
2012 }
2013
2014 /**
2015 * @tc.name: CreateDistributedTableTest006
2016 * @tc.desc: Test create distributed table and execute transaction concurrently
2017 * @tc.type: FUNC
2018 * @tc.require:
2019 * @tc.author: liaoyonghuang
2020 */
2021 HWTEST_F(DistributedDBInterfacesRelationalTest, CreateDistributedTableTest006, TestSize.Level0)
2022 {
2023 /**
2024 * @tc.steps:step1. Prepare db file
2025 * @tc.expected: step1. Return OK.
2026 */
2027 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
2028 ASSERT_NE(db, nullptr);
2029 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
2030 EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
2031 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
2032 /**
2033 * @tc.steps:step2. open relational store
2034 * @tc.expected: step2. Return OK.
2035 */
2036 RelationalStoreDelegate *delegate = nullptr;
2037 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
2038 EXPECT_EQ(status, OK);
2039 ASSERT_NE(delegate, nullptr);
2040 /**
2041 * @tc.steps:step3. create distributed table with CLOUD_COOPERATION after between a transaction
2042 * @tc.expected: step3. Return OK.
2043 */
2044 DistributedDB::SqlCondition sqlCondition;
2045 std::vector<VBucket> records = {};
2046 sqlCondition.sql = "BEGIN;";
2047 EXPECT_EQ(delegate->ExecuteSql(sqlCondition, records), E_OK);
2048 status = delegate->CreateDistributedTable("sync_data", DistributedDB::CLOUD_COOPERATION);
2049 EXPECT_EQ(status, OK);
2050 sqlCondition.sql = "COMMIT;";
2051 EXPECT_EQ(delegate->ExecuteSql(sqlCondition, records), E_OK);
2052 status = g_mgr.CloseStore(delegate);
2053 EXPECT_EQ(status, OK);
2054 }
2055
2056 /**
2057 * @tc.name: CreateDistributedTableTest007
2058 * @tc.desc: Test create distributed table after insert data
2059 * @tc.type: FUNC
2060 * @tc.require:
2061 * @tc.author: liaoyonghuang
2062 */
2063 HWTEST_F(DistributedDBInterfacesRelationalTest, CreateDistributedTableTest007, TestSize.Level0)
2064 {
2065 /**
2066 * @tc.steps:step1. Prepare db and table
2067 * @tc.expected: step1. Return OK.
2068 */
2069 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
2070 ASSERT_NE(db, nullptr);
2071 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
2072 std::string t1 = "t1";
2073 std::string sql = "create table " + t1 + "(id integer);";
2074 EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK);
2075 int64_t dataCount = 10;
2076 for (int64_t i = 0; i < dataCount; i++) {
2077 sql = "insert into " + t1 + " values(" + std::to_string(i) + ")";
2078 EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK);
2079 }
2080 /**
2081 * @tc.steps:step2. open relational store, create distributed table
2082 * @tc.expected: step2. Return OK.
2083 */
2084 RelationalStoreDelegate *delegate = nullptr;
2085 EXPECT_EQ(g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate), OK);
2086 ASSERT_NE(delegate, nullptr);
2087 EXPECT_EQ(delegate->CreateDistributedTable(t1), OK);
2088 /**
2089 * @tc.steps:step3. check log table
2090 * @tc.expected: step3. Return OK.
2091 */
2092 sql = "select flag, extend_field, cursor, status from " + DBCommon::GetLogTableName(t1) + " order by data_key;";
2093 sqlite3_stmt *stmt = nullptr;
2094 EXPECT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK);
2095 int64_t actualCount = 0;
2096 while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) {
2097 actualCount++;
2098 int64_t actualFlag = sqlite3_column_int64(stmt, 0); // index 0 is flag
2099 std::string actualExtendVal;
2100 EXPECT_EQ(SQLiteUtils::GetColumnTextValue(stmt, 1, actualExtendVal), E_OK); // index 1 is extend_field
2101 int64_t actualCursor = sqlite3_column_int64(stmt, 2); // index 2 is cursor
2102 int64_t actualStatus = sqlite3_column_int64(stmt, 3); // index 3 is status
2103 EXPECT_EQ(actualFlag, static_cast<int64_t>(LogInfoFlag::FLAG_LOCAL) |
2104 static_cast<int64_t>(LogInfoFlag::FLAG_DEVICE_CLOUD_INCONSISTENCY));
2105 EXPECT_EQ(actualExtendVal, "");
2106 EXPECT_EQ(actualCursor, actualCount);
2107 EXPECT_EQ(actualStatus, 0);
2108 }
2109 EXPECT_EQ(actualCount, dataCount);
2110 int errCode = E_OK;
2111 SQLiteUtils::ResetStatement(stmt, true, errCode);
2112 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
2113 DBStatus status = g_mgr.CloseStore(delegate);
2114 EXPECT_EQ(status, OK);
2115 }
2116
2117 /**
2118 * @tc.name: StoreId001
2119 * @tc.desc: Test storeId support with dot
2120 * @tc.type: FUNC
2121 * @tc.require:
2122 * @tc.author: zqq
2123 */
2124 HWTEST_F(DistributedDBInterfacesRelationalTest, StoreId001, TestSize.Level0)
2125 {
2126 EXPECT_NE(RelationalStoreManager::GetRelationalStoreIdentifier(USER_ID, APP_ID, STORE_ID_1, false), "");
2127 EXPECT_NE(RelationalStoreManager::GetRelationalStoreIdentifier(USER_ID, APP_ID, STORE_ID_1, true), "");
2128 EXPECT_NE(RelationalStoreManager::GetRelationalStoreIdentifier(USER_ID, APP_ID, STORE_ID_1, true), "");
2129 const std::string storeIdWithDot = STORE_ID_1 + ".";
2130 EXPECT_NE(RelationalStoreManager::GetRelationalStoreIdentifier(USER_ID, APP_ID, storeIdWithDot, true), "");
2131 EXPECT_NE(RelationalStoreManager::GetRelationalStoreIdentifier(USER_ID, USER_ID, APP_ID, STORE_ID_1, false), "");
2132 EXPECT_NE(RelationalStoreManager::GetRelationalStoreIdentifier(USER_ID, USER_ID, APP_ID, STORE_ID_1, true), "");
2133 EXPECT_NE(RelationalStoreManager::GetRelationalStoreIdentifier(USER_ID, USER_ID, APP_ID, storeIdWithDot, true), "");
2134 }
2135
2136 /**
2137 * @tc.name: GetDbHandleConcurrentlyTest001
2138 * @tc.desc: Test get db handle concurrently
2139 * @tc.type: FUNC
2140 * @tc.require:
2141 * @tc.author: liaoyonghuang
2142 */
2143 HWTEST_F(DistributedDBInterfacesRelationalTest, GetDbHandleConcurrentlyTest001, TestSize.Level1)
2144 {
2145 /**
2146 * @tc.steps:step1. Prepare db file
2147 * @tc.expected: step1. Return OK.
2148 */
2149 sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
2150 ASSERT_NE(db, nullptr);
2151 EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
2152 EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_NO_UNIQUE), SQLITE_OK);
2153 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
2154 /**
2155 * @tc.steps:step2. open relational store
2156 * @tc.expected: step2. Return OK.
2157 */
2158 RelationalStoreDelegate *delegate = nullptr;
2159 DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
2160 EXPECT_EQ(status, OK);
2161 ASSERT_NE(delegate, nullptr);
2162 /**
2163 * @tc.steps:step3. open relational store
2164 * @tc.expected: step3. Return OK.
2165 */
2166 std::string tableName = "sync_data";
2167 int executeTime = 100;
__anon61815a6f0f02() 2168 std::thread thread1([&, this]() {
2169 for (int i = 0; i < executeTime; i++) {
2170 DistributedDB::SqlCondition sqlCondition;
2171 std::vector<VBucket> records = {};
2172 sqlCondition.sql = "BEGIN IMMEDIATE TRANSACTION;";
2173 EXPECT_EQ(delegate->ExecuteSql(sqlCondition, records), E_OK);
2174 sqlCondition.sql = "DROP TABLE IF EXISTS " + DBCommon::GetLogTableName(tableName);
2175 EXPECT_EQ(delegate->ExecuteSql(sqlCondition, records), E_OK);
2176 sqlCondition.sql = "COMMIT;";
2177 EXPECT_EQ(delegate->ExecuteSql(sqlCondition, records), E_OK);
2178 }
2179 });
__anon61815a6f1002() 2180 std::thread thread2([&, this]() {
2181 for (int i = 0; i < executeTime; i++) {
2182 status = delegate->CreateDistributedTable(tableName, DistributedDB::CLOUD_COOPERATION);
2183 EXPECT_EQ(status, OK);
2184 }
2185 });
2186 thread1.join();
2187 thread2.join();
2188 status = g_mgr.CloseStore(delegate);
2189 EXPECT_EQ(status, OK);
2190 }
2191 }
2192