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 #include <cstdint>
16 #include <gtest/gtest.h>
17
18 #include "db_constant.h"
19 #include "db_common.h"
20 #include "distributeddb_storage_single_ver_natural_store_testcase.h"
21 #include "kvdb_pragma.h"
22 #include "storage_engine_manager.h"
23
24 using namespace testing::ext;
25 using namespace DistributedDB;
26 using namespace DistributedDBUnitTest;
27 using namespace std;
28
29 namespace {
30 string g_testDir;
31 string g_databaseName;
32 string g_identifier;
33 KvDBProperties g_property;
34
35 SQLiteSingleVerNaturalStore *g_store = nullptr;
36 SQLiteSingleVerNaturalStoreConnection *g_connection = nullptr;
37 SQLiteSingleVerStorageExecutor *g_handle = nullptr;
38 SQLiteSingleVerStorageExecutor *g_nullHandle = nullptr;
39
40 const char * const ADD_SYNC = "ALTER TABLE sync_data ADD column version INT";
41 const char * const ADD_LOCAL = "ALTER TABLE local_data ADD column flag INT";
42 const char * const INSERT_SQL = "INSERT INTO sync_data VALUES('a', 'b', 1, 2, '', '', 'efdef', 100 , 1);";
43 const int SQL_STATE_ERR = -1;
44 }
45
46 class DistributedDBStorageSQLiteSingleVerNaturalExecutorTest : public testing::Test {
47 public:
48 static void SetUpTestCase(void);
49 static void TearDownTestCase(void);
50 void SetUp();
51 void TearDown();
52 };
53
SetUpTestCase(void)54 void DistributedDBStorageSQLiteSingleVerNaturalExecutorTest::SetUpTestCase(void)
55 {
56 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
57 LOGI("DistributedDBStorageSQLiteSingleVerNaturalExecutorTest dir is %s", g_testDir.c_str());
58 std::string oriIdentifier = APP_ID + "-" + USER_ID + "-" + "TestGeneralNBExecutor";
59 std::string identifier = DBCommon::TransferHashString(oriIdentifier);
60 g_identifier = DBCommon::TransferStringToHex(identifier);
61
62 g_databaseName = "/" + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR + "/" + DBConstant::MAINDB_DIR + "/" +
63 DBConstant::SINGLE_VER_DATA_STORE + DBConstant::SQLITE_DB_EXTENSION;
64 g_property.SetStringProp(KvDBProperties::DATA_DIR, g_testDir);
65 g_property.SetStringProp(KvDBProperties::STORE_ID, "TestGeneralNBExecutor");
66 g_property.SetStringProp(KvDBProperties::IDENTIFIER_DIR, g_identifier);
67 g_property.SetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::SINGLE_VER_TYPE);
68 }
69
TearDownTestCase(void)70 void DistributedDBStorageSQLiteSingleVerNaturalExecutorTest::TearDownTestCase(void)
71 {
72 DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir + "/" + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR);
73 }
74
SetUp(void)75 void DistributedDBStorageSQLiteSingleVerNaturalExecutorTest::SetUp(void)
76 {
77 DistributedDBToolsUnitTest::PrintTestCaseInfo();
78 DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir + "/" + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR);
79 g_store = new (std::nothrow) SQLiteSingleVerNaturalStore;
80 ASSERT_NE(g_store, nullptr);
81 ASSERT_EQ(g_store->Open(g_property), E_OK);
82
83 int erroCode = E_OK;
84 g_connection = static_cast<SQLiteSingleVerNaturalStoreConnection *>(g_store->GetDBConnection(erroCode));
85 ASSERT_NE(g_connection, nullptr);
86 g_store->DecObjRef(g_store);
87 EXPECT_EQ(erroCode, E_OK);
88
89 g_handle = static_cast<SQLiteSingleVerStorageExecutor *>(
90 g_store->GetHandle(true, erroCode, OperatePerm::NORMAL_PERM));
91 ASSERT_EQ(erroCode, E_OK);
92 ASSERT_NE(g_handle, nullptr);
93
94 g_nullHandle = new (nothrow) SQLiteSingleVerStorageExecutor(nullptr, false, false);
95 ASSERT_NE(g_nullHandle, nullptr);
96 }
97
TearDown(void)98 void DistributedDBStorageSQLiteSingleVerNaturalExecutorTest::TearDown(void)
99 {
100 if (g_nullHandle != nullptr) {
101 delete g_nullHandle;
102 g_nullHandle = nullptr;
103 }
104 if (g_store != nullptr) {
105 g_store->ReleaseHandle(g_handle);
106 }
107 if (g_connection != nullptr) {
108 g_connection->Close();
109 g_connection = nullptr;
110 }
111 g_store = nullptr;
112 g_handle = nullptr;
113 }
114
115 /**
116 * @tc.name: InvalidParam001
117 * @tc.desc: Get Kv Data with Invalid condition
118 * @tc.type: FUNC
119 * @tc.require:
120 * @tc.author: bty
121 */
122 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam001, TestSize.Level1)
123 {
124 /**
125 * @tc.steps: step1. The Data type is invalid
126 * @tc.expected: step1. Expect -E_INVALID_ARGS
127 */
128 Timestamp timestamp = 0;
129 Key key;
130 Value value;
131 int type = static_cast<int>(SingleVerDataType::SYNC_TYPE);
132 EXPECT_EQ(g_nullHandle->GetKvData(SingleVerDataType(type + 1), key, value, timestamp), -E_INVALID_ARGS);
133
134 /**
135 * @tc.steps: step2. The db is null
136 * @tc.expected: step2. Expect -E_INVALID_DB
137 */
138 EXPECT_EQ(g_nullHandle->GetKvData(SingleVerDataType(type), key, value, timestamp), -E_INVALID_DB);
139
140 /**
141 * @tc.steps: step3. The key is empty
142 * @tc.expected: step3. Expect -E_INVALID_ARGS
143 */
144 EXPECT_EQ(g_handle->GetKvData(SingleVerDataType(type), key, value, timestamp), -E_INVALID_ARGS);
145 }
146
147 /**
148 * @tc.name: InvalidParam002
149 * @tc.desc: Put Kv Data with Invalid condition
150 * @tc.type: FUNC
151 * @tc.require:
152 * @tc.author: bty
153 */
154 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam002, TestSize.Level1)
155 {
156 /**
157 * @tc.steps: step1. The Data type is invalid
158 * @tc.expected: step1. Expect -E_INVALID_ARGS
159 */
160 Value value;
161 EXPECT_EQ(g_nullHandle->PutKvData(SingleVerDataType::SYNC_TYPE, KEY_1, value, 0, nullptr), -E_INVALID_ARGS);
162
163 /**
164 * @tc.steps: step2. The db is null
165 * @tc.expected: step2. Expect -E_INVALID_DB
166 */
167 EXPECT_EQ(g_nullHandle->PutKvData(SingleVerDataType::META_TYPE, KEY_1, value, 0, nullptr), -E_INVALID_DB);
168
169 /**
170 * @tc.steps: step3. The key is null
171 * @tc.expected: step3. Expect -E_INVALID_ARGS
172 */
173 Key key;
174 EXPECT_EQ(g_handle->PutKvData(SingleVerDataType::META_TYPE, key, value, 0, nullptr), -E_INVALID_ARGS);
175 }
176
177 /**
178 * @tc.name: InvalidParam003
179 * @tc.desc: Get entries with Invalid condition
180 * @tc.type: FUNC
181 * @tc.require:
182 * @tc.author: bty
183 */
184 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam003, TestSize.Level1)
185 {
186 /**
187 * @tc.steps: step1. The Data type is invalid
188 * @tc.expected: step1. Expect -E_INVALID_ARGS
189 */
190 vector<Entry> entries;
191 EXPECT_EQ(g_nullHandle->GetEntries(false, SingleVerDataType::META_TYPE, KEY_1, entries), -E_INVALID_ARGS);
192
193 /**
194 * @tc.steps: step2. The db is null
195 * @tc.expected: step2.. Expect -E_INVALID_DB
196 */
197 EXPECT_EQ(g_nullHandle->GetEntries(false, SingleVerDataType::LOCAL_TYPE, KEY_1, entries), -E_INVALID_DB);
198
199 /**
200 * @tc.steps: step3. This key does not exist
201 * @tc.expected: step3. Expect -E_NOT_FOUND
202 */
203 Key key;
204 EXPECT_EQ(g_handle->GetEntries(false, SingleVerDataType::LOCAL_TYPE, KEY_1, entries), -E_NOT_FOUND);
205 }
206
207 /**
208 * @tc.name: InvalidParam004
209 * @tc.desc: Get count with Invalid condition
210 * @tc.type: FUNC
211 * @tc.require:
212 * @tc.author: bty
213 */
214 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam004, TestSize.Level1)
215 {
216 /**
217 * @tc.steps: step1. The db is null
218 * @tc.expected: step1. Expect -E_INVALID_DB
219 */
220 Query query = Query::Select().OrderBy("abc", false);
221 QueryObject object(query);
222 int count;
223 EXPECT_EQ(g_nullHandle->GetCount(object, count), -E_INVALID_DB);
224
225 /**
226 * @tc.steps: step2. The query condition is invalid
227 * @tc.expected: step2. Expect -E_NOT_SUPPORT
228 */
229 EXPECT_EQ(g_handle->GetCount(object, count), -E_NOT_SUPPORT);
230 }
231
232 /**
233 * @tc.name: InvalidParam005
234 * @tc.desc: Test timestamp with Invalid condition
235 * @tc.type: FUNC
236 * @tc.require:
237 * @tc.author: bty
238 */
239 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam005, TestSize.Level1)
240 {
241 /**
242 * @tc.steps: step1. The db is null
243 * @tc.expected: step1. Expect return 0
244 */
245 Timestamp timestamp = 0;
246 g_nullHandle->InitCurrentMaxStamp(timestamp);
247 EXPECT_EQ(timestamp, 0u);
248
249 /**
250 * @tc.steps: step2. Get timestamp when The db is null
251 * @tc.expected: step2. Expect -E_INVALID_DB
252 */
253 std::vector<DataItem> dataItems;
254 Timestamp begin = 0;
255 Timestamp end = INT64_MAX;
256 DataSizeSpecInfo info;
257 EXPECT_EQ(g_nullHandle->GetSyncDataByTimestamp(dataItems, sizeof("time"), begin, end, info), -E_INVALID_DB);
258 EXPECT_EQ(g_nullHandle->GetDeletedSyncDataByTimestamp(dataItems, sizeof("time"), begin, end, info), -E_INVALID_DB);
259 }
260
261 /**
262 * @tc.name: InvalidParam006
263 * @tc.desc: Open and get resultSet with Invalid condition
264 * @tc.type: FUNC
265 * @tc.require:
266 * @tc.author: bty
267 */
268 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam006, TestSize.Level1)
269 {
270 /**
271 * @tc.steps: step1. The db is null
272 * @tc.expected: step1. Expect -E_INVALID_DB
273 */
274 int count;
275 EXPECT_EQ(g_nullHandle->OpenResultSet(KEY_1, count), -E_INVALID_DB);
276 vector<int64_t> cache;
277 Key key;
278 EXPECT_EQ(g_nullHandle->OpenResultSetForCacheRowIdMode(KEY_1, cache, 0, count), -E_INVALID_DB);
279 Query query = Query::Select();
280 QueryObject object(query);
281 EXPECT_EQ(g_nullHandle->OpenResultSet(object, count), -E_INVALID_DB);
282 EXPECT_EQ(g_nullHandle->OpenResultSetForCacheRowIdMode(object, cache, 0, count), -E_INVALID_DB);
283
284 /**
285 * @tc.steps: step2. Then get
286 * @tc.expected: step2. Expect -E_RESULT_SET_STATUS_INVALID
287 */
288 Value value;
289 EXPECT_EQ(g_nullHandle->GetNextEntryFromResultSet(key, value, false), -E_RESULT_SET_STATUS_INVALID);
290
291 /**
292 * @tc.steps: step3. The db is valid,open
293 * @tc.expected: step3. Expect E_OK
294 */
295 EXPECT_EQ(g_handle->OpenResultSetForCacheRowIdMode(object, cache, 0, count), E_OK);
296
297 /**
298 * @tc.steps: step4. Then get
299 * @tc.expected: step4. Expect -E_RESULT_SET_STATUS_INVALID
300 */
301 Entry entry;
302 EXPECT_EQ(g_handle->GetEntryByRowId(0, entry), -E_UNEXPECTED_DATA);
303 }
304
305 /**
306 * @tc.name: InvalidParam007
307 * @tc.desc: Reload resultSet with Invalid condition
308 * @tc.type: FUNC
309 * @tc.require:
310 * @tc.author: bty
311 */
312 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam007, TestSize.Level1)
313 {
314 /**
315 * @tc.steps: step1. Reload,but the db is null
316 * @tc.expected: step1. Expect -E_INVALID_ARGS
317 */
318 Key key;
319 EXPECT_EQ(g_nullHandle->ReloadResultSet(key), -E_INVALID_ARGS);
320
321 /**
322 * @tc.steps: step2. Reload,but the key is empty
323 * @tc.expected: step2. Expect -E_INVALID_ARGS
324 */
325 vector<int64_t> cache;
326 EXPECT_EQ(g_handle->ReloadResultSet(key), -E_INVALID_ARGS);
327 EXPECT_EQ(g_nullHandle->ReloadResultSetForCacheRowIdMode(key, cache, 0, 0), -E_INVALID_ARGS);
328
329 /**
330 * @tc.steps: step3. Reload by object,but the db is null
331 * @tc.expected: step3. Expect -E_INVALID_QUERY_FORMAT
332 */
333 Query query = Query::Select();
334 QueryObject object(query);
335 EXPECT_EQ(g_nullHandle->ReloadResultSet(object), -E_INVALID_QUERY_FORMAT);
336 EXPECT_EQ(g_nullHandle->ReloadResultSetForCacheRowIdMode(object, cache, 0, 0), -E_INVALID_QUERY_FORMAT);
337
338 /**
339 * @tc.steps: step4. Reload with the valid db
340 * @tc.expected: step4. Expect E_OK
341 */
342 EXPECT_EQ(g_handle->ReloadResultSetForCacheRowIdMode(object, cache, 0, 0), E_OK);
343 }
344
345 /**
346 * @tc.name: InvalidParam008
347 * @tc.desc: Test transaction with Invalid condition
348 * @tc.type: FUNC
349 * @tc.require:
350 * @tc.author: bty
351 */
352 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam008, TestSize.Level1)
353 {
354 EXPECT_EQ(g_nullHandle->StartTransaction(TransactType::DEFERRED), -E_INVALID_DB);
355 EXPECT_EQ(g_nullHandle->Commit(), -E_INVALID_DB);
356 EXPECT_EQ(g_nullHandle->Rollback(), -E_INVALID_DB);
357
358 EXPECT_EQ(g_handle->StartTransaction(TransactType::DEFERRED), E_OK);
359 EXPECT_EQ(g_handle->Reset(), E_OK);
360 }
361
362 /**
363 * @tc.name: InvalidParam009
364 * @tc.desc: Get identifier with Invalid condition
365 * @tc.type: FUNC
366 * @tc.require:
367 * @tc.author: bty
368 */
369 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam009, TestSize.Level1)
370 {
371 /**
372 * @tc.steps: step1. The parameter is null
373 * @tc.expected: step1. Expect -E_INVALID_ARGS
374 */
375 EXPECT_EQ(g_nullHandle->GetDeviceIdentifier(nullptr), -E_INVALID_ARGS);
376
377 /**
378 * @tc.steps: step2. The db is null
379 * @tc.expected: step2. Expect -E_INVALID_DB
380 */
381 PragmaEntryDeviceIdentifier identifier;
382 EXPECT_EQ(g_nullHandle->GetDeviceIdentifier(&identifier), -E_INVALID_DB);
383
384 /**
385 * @tc.steps: step3. The identifier is empty
386 * @tc.expected: step3. Expect -E_INVALID_ARGS
387 */
388 EXPECT_EQ(g_handle->GetDeviceIdentifier(&identifier), -E_INVALID_ARGS);
389 }
390
391 /**
392 * @tc.name: InvalidParam010
393 * @tc.desc: Fail to call function with Invalid condition
394 * @tc.type: FUNC
395 * @tc.require:
396 * @tc.author: bty
397 */
398 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam010, TestSize.Level1)
399 {
400 vector<Key> keys;
401 EXPECT_EQ(g_nullHandle->GetAllMetaKeys(keys), -E_INVALID_DB);
402 string devName;
403 vector<Entry> entries;
404 EXPECT_EQ(g_nullHandle->GetAllSyncedEntries(devName, entries), -E_INVALID_DB);
405 EXPECT_EQ(g_nullHandle->ForceCheckPoint(), -E_INVALID_DB);
406 EXPECT_EQ(g_nullHandle->CheckIntegrity(), -E_INVALID_DB);
407 }
408
409 /**
410 * @tc.name: InvalidParam011
411 * @tc.desc: Change executor state to operate data
412 * @tc.type: FUNC
413 * @tc.require:
414 * @tc.author: bty
415 */
416 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam011, TestSize.Level1)
417 {
418 /**
419 * @tc.steps: step1. put local kv data
420 * @tc.expected: step1. Expect E_OK
421 */
422 Key key = KEY_1;
423 Value value;
424 Timestamp timestamp = 0;
425 EXPECT_EQ(g_handle->PutKvData(SingleVerDataType::LOCAL_TYPE, key, value, timestamp, nullptr), E_OK);
426
427 /**
428 * @tc.steps: step2. Get sqlite3 handle,then create executor for state CACHE_ATTACH_MAIN
429 * @tc.expected: step2. Expect not null
430 */
431 sqlite3 *sqlHandle = nullptr;
432 std::string dbPath = g_testDir + g_databaseName;
433 OpenDbProperties property = {dbPath, false, false};
434 EXPECT_EQ(SQLiteUtils::OpenDatabase(property, sqlHandle), E_OK);
435 ASSERT_NE(sqlHandle, nullptr);
436
437 auto executor = std::make_unique<SQLiteSingleVerStorageExecutor>(
438 sqlHandle, false, false, ExecutorState::CACHE_ATTACH_MAIN);
439 ASSERT_NE(executor, nullptr);
440
441 /**
442 * @tc.steps: step3. The singleVerNaturalStoreCommitNotifyData is null,delete
443 * @tc.expected: step3. Expect SQL_STATE_ERR
444 */
445 EXPECT_EQ(executor->DeleteLocalKvData(key, nullptr, value, timestamp), SQL_STATE_ERR);
446
447 /**
448 * @tc.steps: step4. Update sync_data table and insert a sync data
449 * @tc.expected: step4. Expect E_OK
450 */
451 ASSERT_TRUE(SQLiteUtils::ExecuteRawSQL(sqlHandle, ADD_SYNC) == E_OK);
452 ASSERT_TRUE(SQLiteUtils::ExecuteRawSQL(sqlHandle, INSERT_SQL) == E_OK);
453 std::vector<DataItem> vec;
454 uint64_t version = 0u;
455 EXPECT_EQ(executor->GetMinVersionCacheData(vec, version), E_OK);
456 EXPECT_EQ(executor->GetMaxVersionInCacheDb(version), E_OK);
457 std::string hashDev = DBCommon::TransferHashString("device1");
458 EXPECT_EQ(executor->RemoveDeviceDataInCacheMode(hashDev, true, 0u), E_OK);
459 sqlite3_close_v2(sqlHandle);
460 sqlHandle = nullptr;
461 }
462
463 /**
464 * @tc.name: InvalidSync001
465 * @tc.desc: Save sync data with Invalid condition
466 * @tc.type: FUNC
467 * @tc.require:
468 * @tc.author: bty
469 */
470 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidSync001, TestSize.Level1)
471 {
472 /**
473 * @tc.steps: step1. The saveSyncStatements_ is not prepare
474 * @tc.expected: step1. Expect -E_INVALID_ARGS
475 */
476 DataItem item;
477 DeviceInfo info;
478 Timestamp time = 0;
479 EXPECT_EQ(g_handle->SaveSyncDataItem(item, info, time, nullptr, false), -E_INVALID_ARGS);
480
481 /**
482 * @tc.steps: step2. Try to prepare when the db is null
483 * @tc.expected: step2. Expect -E_INVALID_DB
484 */
485 EXPECT_EQ(g_nullHandle->PrepareForSavingData(SingleVerDataType::LOCAL_TYPE), -E_INVALID_DB);
486
487 /**
488 * @tc.steps: step3. The data item key is empty
489 * @tc.expected: step3. Expect -E_INVALID_ARGS
490 */
491 EXPECT_EQ(g_handle->PrepareForSavingData(SingleVerDataType::SYNC_TYPE), E_OK);
492 EXPECT_EQ(g_handle->SaveSyncDataItem(item, info, time, nullptr, false), -E_INVALID_ARGS);
493
494 /**
495 * @tc.steps: step4. The committedData is null
496 * @tc.expected: step4. Expect return E_OK
497 */
498 item.key = KEY_1;
499 EXPECT_EQ(g_handle->SaveSyncDataItem(item, info, time, nullptr, false), E_OK);
500
501 /**
502 * @tc.steps: step5. Into EraseSyncData
503 * @tc.expected: step5. Expect return E_OK
504 */
505 SingleVerNaturalStoreCommitNotifyData data;
506 item.writeTimestamp = 1;
507 item.flag = DataItem::REMOTE_DEVICE_DATA_MISS_QUERY;
508 EXPECT_EQ(g_handle->SaveSyncDataItem(item, info, time, &data, true), E_OK);
509 }
510
511 /**
512 * @tc.name: ConnectionTest001
513 * @tc.desc: Failed to get the keys
514 * @tc.type: FUNC
515 * @tc.require:
516 * @tc.author: bty
517 */
518 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, ConnectionTest001, TestSize.Level1)
519 {
520 /**
521 * @tc.steps: step1. the dataType is error
522 * @tc.expected: step1. Expect -E_INVALID_ARGS
523 */
524 IOption option;
525 option.dataType = IOption::SYNC_DATA + 1;
526 vector<Key> keys;
527 EXPECT_EQ(g_connection->GetKeys(option, KEY_1, keys), -E_INVALID_ARGS);
528
529 /**
530 * @tc.steps: step2. Get keys in cacheDB state
531 * @tc.expected: step2. Expect -E_EKEYREVOKED
532 */
533 int errCode = E_OK;
534 SQLiteSingleVerStorageEngine *storageEngine =
535 static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
536 ASSERT_EQ(errCode, E_OK);
537 ASSERT_NE(storageEngine, nullptr);
538 storageEngine->SetEngineState(EngineState::CACHEDB);
539 option.dataType = IOption::LOCAL_DATA;
540 EXPECT_EQ(g_connection->GetKeys(option, KEY_1, keys), -E_EKEYREVOKED);
541 storageEngine->Release();
542
543 /**
544 * @tc.steps: step3. Get keys in null db connection
545 * @tc.expected: step3. Expect -E_NOT_INIT
546 */
547 std::unique_ptr<SQLiteSingleVerNaturalStoreConnection> emptyConn =
548 std::make_unique<SQLiteSingleVerNaturalStoreConnection>(nullptr);
549 ASSERT_NE(emptyConn, nullptr);
550 EXPECT_EQ(emptyConn->GetKeys(option, KEY_1, keys), -E_NOT_INIT);
551 }
552
553 /**
554 * @tc.name: ConnectionTest002
555 * @tc.desc: Push and delete on empty connect
556 * @tc.type: FUNC
557 * @tc.require:
558 * @tc.author: bty
559 */
560 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, ConnectionTest002, TestSize.Level1)
561 {
562 std::unique_ptr<SQLiteSingleVerNaturalStoreConnection> emptyConn =
563 std::make_unique<SQLiteSingleVerNaturalStoreConnection>(nullptr);
564 IOption option = {IOption::LOCAL_DATA};
565 std::vector<Entry> entries;
566 EXPECT_EQ(emptyConn->PutBatch(option, entries), -E_INVALID_DB);
567 std::vector<Key> keys;
568 EXPECT_EQ(emptyConn->DeleteBatch(option, keys), -E_INVALID_DB);
569 option.dataType = IOption::SYNC_DATA;
570 EXPECT_EQ(emptyConn->PutBatch(option, entries), -E_INVALID_DB);
571 EXPECT_EQ(emptyConn->DeleteBatch(option, keys), -E_INVALID_DB);
572 option.dataType = IOption::SYNC_DATA + 1;
573 EXPECT_EQ(emptyConn->PutBatch(option, entries), -E_NOT_SUPPORT);
574 }
575
576 /**
577 * @tc.name: ConnectionTest003
578 * @tc.desc: Failed to Put and Delete
579 * @tc.type: FUNC
580 * @tc.require:
581 * @tc.author: bty
582 */
583 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, ConnectionTest003, TestSize.Level1)
584 {
585 /**
586 * @tc.steps: step1. Only change the storageEngine to cacheDB
587 * @tc.expected: step1. Expect SQL_STATE_ERR
588 */
589 int errCode = E_OK;
590 SQLiteSingleVerStorageEngine *storageEngine =
591 static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
592 ASSERT_EQ(errCode, E_OK);
593 ASSERT_NE(storageEngine, nullptr);
594 storageEngine->SetEngineState(EngineState::CACHEDB);
595 IOption option = {IOption::SYNC_DATA};
596 std::vector<Entry> entries;
597 entries.push_back(ENTRY_1);
598 g_store->ReleaseHandle(g_handle);
599 EXPECT_EQ(g_connection->PutBatch(option, entries), SQL_STATE_ERR);
600 std::vector<Key> keys;
601 keys.push_back(KEY_1);
602 EXPECT_EQ(g_connection->DeleteBatch(option, keys), SQL_STATE_ERR);
603
604 /**
605 * @tc.steps: step2.Change to LOCAL_DATA option
606 * @tc.expected: step2. Expect SQL_STATE_ERR
607 */
608 option.dataType = IOption::LOCAL_DATA;
609 EXPECT_EQ(g_connection->PutBatch(option, entries), SQL_STATE_ERR);
610 EXPECT_EQ(g_connection->DeleteBatch(option, keys), SQL_STATE_ERR);
611
612 /**
613 * @tc.steps: step3. Table sync_data adds a column to make the num of cols equal to the cacheDB
614 * @tc.expected: step3. Expect E_OK
615 */
616 sqlite3 *db;
617 ASSERT_TRUE(sqlite3_open_v2((g_testDir + g_databaseName).c_str(),
618 &db, SQLITE_OPEN_URI | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr) == SQLITE_OK);
619 ASSERT_TRUE(SQLiteUtils::ExecuteRawSQL(db, ADD_SYNC) == E_OK);
620 sqlite3_close_v2(db);
621 option.dataType = IOption::SYNC_DATA;
622 EXPECT_EQ(g_connection->PutBatch(option, entries), E_OK);
623 storageEngine->Release();
624 }
625
626 /**
627 * @tc.name: ConnectionTest004
628 * @tc.desc: Failed to GetResultSet
629 * @tc.type: FUNC
630 * @tc.require:
631 * @tc.author: bty
632 */
633 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, ConnectionTest004, TestSize.Level1)
634 {
635 /**
636 * @tc.steps: step1. the db is null
637 * @tc.expected: step1. Expect -E_INVALID_DB
638 */
639 g_store->ReleaseHandle(g_handle);
640 IOption option;
641 option.dataType = IOption::SYNC_DATA;
642 IKvDBResultSet *set = nullptr;
643 Query query = Query::Select();
644 std::unique_ptr<SQLiteSingleVerNaturalStoreConnection> emptyConn =
645 std::make_unique<SQLiteSingleVerNaturalStoreConnection>(nullptr);
646 EXPECT_EQ(emptyConn->GetResultSet(option, KEY_1, set), -E_INVALID_DB);
647
648 /**
649 * @tc.steps: step2. get in transaction
650 * @tc.expected: step2. Expect -E_BUSY
651 */
652 g_connection->StartTransaction();
653 EXPECT_EQ(g_connection->GetResultSet(option, query, set), -E_BUSY);
654 g_connection->RollBack();
655
656 /**
657 * @tc.steps: step3. change the storageEngine to cacheDB
658 * @tc.expected: step3. Expect -E_EKEYREVOKED
659 */
660 int errCode = E_OK;
661 SQLiteSingleVerStorageEngine *storageEngine =
662 static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
663 ASSERT_EQ(errCode, E_OK);
664 ASSERT_NE(storageEngine, nullptr);
665 storageEngine->SetEngineState(EngineState::CACHEDB);
666 EXPECT_EQ(g_connection->GetResultSet(option, query, set), -E_EKEYREVOKED);
667 EXPECT_EQ(g_connection->GetResultSet(option, KEY_1, set), -E_EKEYREVOKED);
668 storageEngine->Release();
669 }
670
671 /**
672 * @tc.name: ConnectionTest005
673 * @tc.desc: Failed to Get entries,value and count
674 * @tc.type: FUNC
675 * @tc.require:
676 * @tc.author: bty
677 */
678 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, ConnectionTest005, TestSize.Level1)
679 {
680 /**
681 * @tc.steps: step1. the db is null
682 * @tc.expected: step1. Expect -E_INVALID_DB, Get return -E_NOT_INIT
683 */
684 g_store->ReleaseHandle(g_handle);
685 IOption option;
686 option.dataType = IOption::SYNC_DATA;
687 Query query = Query::Select();
688 std::unique_ptr<SQLiteSingleVerNaturalStoreConnection> emptyConn =
689 std::make_unique<SQLiteSingleVerNaturalStoreConnection>(nullptr);
690 ASSERT_NE(emptyConn, nullptr);
691 int count;
692 EXPECT_EQ(emptyConn->GetCount(option, query, count), -E_INVALID_DB);
693 std::vector<Entry> entries;
694 EXPECT_EQ(emptyConn->GetEntries(option, query, entries), -E_INVALID_DB);
695 Value value;
696 EXPECT_EQ(emptyConn->Get(option, KEY_1, value), -E_NOT_INIT);
697
698 /**
699 * @tc.steps: step2. the dataType is not SYNC_DATA
700 * @tc.expected: step2. Expect -E_NOT_SUPPORT
701 */
702 option.dataType = IOption::SYNC_DATA + 1;
703 EXPECT_EQ(emptyConn->GetCount(option, query, count), -E_NOT_SUPPORT);
704 EXPECT_EQ(emptyConn->GetEntries(option, query, entries), -E_NOT_SUPPORT);
705 EXPECT_EQ(emptyConn->Get(option, KEY_1, value), -E_NOT_SUPPORT);
706
707 /**
708 * @tc.steps: step3. get in transaction
709 * @tc.expected: step3. Expect GetEntries -E_NOT_FOUND, GetCount E_OK
710 */
711 option.dataType = IOption::SYNC_DATA;
712 g_connection->StartTransaction();
713 EXPECT_EQ(g_connection->GetEntries(option, query, entries), -E_NOT_FOUND);
714 EXPECT_EQ(g_connection->GetCount(option, query, count), E_OK);
715 g_connection->RollBack();
716
717 /**
718 * @tc.steps: step4. change the storageEngine to cacheDB
719 * @tc.expected: step4. Expect -E_EKEYREVOKED
720 */
721 int errCode = E_OK;
722 SQLiteSingleVerStorageEngine *storageEngine =
723 static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
724 ASSERT_EQ(errCode, E_OK);
725 ASSERT_NE(storageEngine, nullptr);
726 storageEngine->SetEngineState(EngineState::CACHEDB);
727 EXPECT_EQ(g_connection->GetCount(option, query, count), -E_EKEYREVOKED);
728 EXPECT_EQ(g_connection->GetEntries(option, query, entries), -E_EKEYREVOKED);
729 EXPECT_EQ(g_connection->Get(option, KEY_1, value), -E_EKEYREVOKED);
730 storageEngine->Release();
731 }
732
733 /**
734 * @tc.name: PragmaTest001
735 * @tc.desc: Calling Pragma incorrectly
736 * @tc.type: FUNC
737 * @tc.require:
738 * @tc.author: bty
739 */
740 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, PragmaTest001, TestSize.Level1)
741 {
742 /**
743 * @tc.steps: step1. the parameter is null
744 * @tc.expected: step1. Expect -E_INVALID_ARGS
745 */
746 EXPECT_EQ(g_connection->Pragma(PRAGMA_RESULT_SET_CACHE_MAX_SIZE, nullptr), -E_INVALID_ARGS);
747 EXPECT_EQ(g_connection->Pragma(PRAGMA_RESULT_SET_CACHE_MODE, nullptr), -E_INVALID_ARGS);
748 EXPECT_EQ(g_connection->Pragma(PRAGMA_SET_AUTO_LIFE_CYCLE, nullptr), -E_INVALID_ARGS);
749 EXPECT_EQ(g_connection->Pragma(PRAGMA_UNPUBLISH_SYNC, nullptr), -E_INVALID_ARGS);
750 EXPECT_EQ(g_connection->Pragma(PRAGMA_PUBLISH_LOCAL, nullptr), -E_INVALID_ARGS);
751 EXPECT_EQ(g_connection->Pragma(PRAGMA_GET_DEVICE_IDENTIFIER_OF_ENTRY, nullptr), -E_INVALID_ARGS);
752 EXPECT_EQ(g_connection->Pragma(PRAGMA_SET_MAX_LOG_LIMIT, nullptr), -E_INVALID_ARGS);
753 EXPECT_EQ(g_connection->Pragma(PRAGMA_GET_IDENTIFIER_OF_DEVICE, nullptr), -E_INVALID_ARGS);
754
755 /**
756 * @tc.steps: step2. the option is invalid
757 * @tc.expected: step2. Expect -E_INVALID_ARGS
758 */
759 std::unique_ptr<SQLiteSingleVerNaturalStoreConnection> emptyConn =
760 std::make_unique<SQLiteSingleVerNaturalStoreConnection>(nullptr);
761 ASSERT_NE(emptyConn, nullptr);
762 SecurityOption option = {S3, SECE};
763 EXPECT_EQ(emptyConn->Pragma(PRAGMA_TRIGGER_TO_MIGRATE_DATA, &option), -E_INVALID_CONNECTION);
764
765 /**
766 * @tc.steps: step3. the size is invalid
767 * @tc.expected: step3. Expect -E_INVALID_ARGS
768 */
769 int size = 0;
770 EXPECT_EQ(emptyConn->Pragma(PRAGMA_RESULT_SET_CACHE_MAX_SIZE, &size), -E_INVALID_ARGS);
771 size = 1;
772 EXPECT_EQ(emptyConn->Pragma(PRAGMA_RESULT_SET_CACHE_MAX_SIZE, &size), E_OK);
773
774 /**
775 * @tc.steps: step4. the mode is invalid
776 * @tc.expected: step4. Expect -E_INVALID_ARGS
777 */
778 ResultSetCacheMode mode = ResultSetCacheMode(2); // 2 is invalid mode
779 EXPECT_EQ(emptyConn->Pragma(PRAGMA_RESULT_SET_CACHE_MODE, &mode), -E_INVALID_ARGS);
780
781 /**
782 * @tc.steps: step5. the db is null
783 * @tc.expected: step5. Expect -E_INVALID_DB
784 */
785 int time = 6000; // 6000 is random
786 EXPECT_EQ(emptyConn->Pragma(PRAGMA_SET_AUTO_LIFE_CYCLE, &time), -E_INVALID_DB);
787 }
788
789 /**
790 * @tc.name: PragmaTest002
791 * @tc.desc: Incorrect publishing and unPublishing
792 * @tc.type: FUNC
793 * @tc.require:
794 * @tc.author: bty
795 */
796 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, PragmaTest002, TestSize.Level1)
797 {
798 /**
799 * @tc.steps: step1. the db is null
800 * @tc.expected: step1. Expect -E_INVALID_DB
801 */
802 std::unique_ptr<SQLiteSingleVerNaturalStoreConnection> emptyConn =
803 std::make_unique<SQLiteSingleVerNaturalStoreConnection>(nullptr);
804 PragmaPublishInfo info;
805 EXPECT_EQ(emptyConn->Pragma(PRAGMA_PUBLISH_LOCAL, &info), -E_INVALID_DB);
806 EXPECT_EQ(emptyConn->Pragma(PRAGMA_UNPUBLISH_SYNC, &info), -E_INVALID_DB);
807
808 /**
809 * @tc.steps: step2. publish in transaction
810 * @tc.expected: step2. Expect -E_NOT_SUPPORT
811 */
812 g_store->ReleaseHandle(g_handle);
813 g_connection->StartTransaction();
814 EXPECT_EQ(g_connection->Pragma(PRAGMA_PUBLISH_LOCAL, &info), -E_NOT_SUPPORT);
815 EXPECT_EQ(g_connection->Pragma(PRAGMA_UNPUBLISH_SYNC, &info), -E_NOT_SUPPORT);
816 g_connection->RollBack();
817
818 /**
819 * @tc.steps: step3. publish in cacheDB
820 * @tc.expected: step3. Expect -E_EKEYREVOKED
821 */
822 int errCode = E_OK;
823 SQLiteSingleVerStorageEngine *storageEngine =
824 static_cast<SQLiteSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
825 ASSERT_EQ(errCode, E_OK);
826 ASSERT_NE(storageEngine, nullptr);
827 storageEngine->SetEngineState(EngineState::CACHEDB);
828 EXPECT_EQ(g_connection->Pragma(PRAGMA_PUBLISH_LOCAL, &info), -E_EKEYREVOKED);
829 EXPECT_EQ(g_connection->Pragma(PRAGMA_UNPUBLISH_SYNC, &info), -E_EKEYREVOKED);
830 g_connection->StartTransaction();
831 g_connection->Commit();
832 storageEngine->Release();
833 }
834
835 /**
836 * @tc.name: PragmaTest003
837 * @tc.desc: Failed to call function with empty connection
838 * @tc.type: FUNC
839 * @tc.require:
840 * @tc.author: bty
841 */
842 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, PragmaTest003, TestSize.Level1)
843 {
844 auto emptyConn = std::make_unique<SQLiteSingleVerNaturalStoreConnection>(nullptr);
845 PragmaEntryDeviceIdentifier identifier = {.key = KEY_1};
846 EXPECT_EQ(emptyConn->Pragma(PRAGMA_GET_DEVICE_IDENTIFIER_OF_ENTRY, &identifier), -E_NOT_INIT);
847 EXPECT_EQ(emptyConn->Pragma(PRAGMA_EXEC_CHECKPOINT, nullptr), -E_NOT_INIT);
848 EXPECT_EQ(emptyConn->CheckIntegrity(), -E_NOT_INIT);
849
850 int limit = 0;
851 EXPECT_EQ(emptyConn->Pragma(PRAGMA_SET_MAX_LOG_LIMIT, &limit), -E_INVALID_DB);
852 EXPECT_EQ(emptyConn->Pragma(PRAGMA_RM_DEVICE_DATA, nullptr), -E_INVALID_DB);
853 CipherPassword pw;
854 EXPECT_EQ(emptyConn->Import("/a.b", pw), -E_INVALID_DB);
855 EXPECT_EQ(emptyConn->Export("/a.b", pw), -E_INVALID_DB);
856 DatabaseLifeCycleNotifier notifier;
857 EXPECT_EQ(emptyConn->RegisterLifeCycleCallback(notifier), -E_INVALID_DB);
858
859 EXPECT_EQ(emptyConn->SetConflictNotifier(0, nullptr), -E_INVALID_ARGS);
__anoncbaf7c060202(const KvDBCommitNotifyData &data) 860 KvDBConflictAction func = [&](const KvDBCommitNotifyData &data) {};
861 EXPECT_EQ(emptyConn->SetConflictNotifier(0, func), -E_INVALID_DB);
862 IKvDBSnapshot *shot;
863 EXPECT_EQ(emptyConn->GetSnapshot(shot), -E_NOT_SUPPORT);
864 }
865
866 /**
867 * @tc.name: ExecutorCache001
868 * @tc.desc: Fail to operate data
869 * @tc.type: FUNC
870 * @tc.require:
871 * @tc.author: bty
872 */
873 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, ExecutorCache001, TestSize.Level1)
874 {
875 g_handle->SetAttachMetaMode(true);
876 std::set<std::string> devices;
877 EXPECT_EQ(g_handle->GetExistsDevicesFromMeta(devices), SQL_STATE_ERR);
878 EXPECT_EQ(g_handle->DeleteMetaDataByPrefixKey(KEY_1), SQL_STATE_ERR);
879 std::vector<Key> keys;
880 EXPECT_EQ(g_handle->DeleteMetaData(keys), SQL_STATE_ERR);
881 EXPECT_EQ(g_handle->PrepareForSavingCacheData(SingleVerDataType::LOCAL_TYPE), SQL_STATE_ERR);
882 std::string hashDev = DBCommon::TransferHashString("device1");
883 EXPECT_EQ(g_handle->RemoveDeviceDataInCacheMode(hashDev, true, 0u), SQL_STATE_ERR);
884 Timestamp timestamp;
885 EXPECT_EQ(g_handle->GetMaxTimestampDuringMigrating(timestamp), -E_NOT_INIT);
886 EXPECT_EQ(g_handle->ResetForSavingCacheData(SingleVerDataType::LOCAL_TYPE), E_OK);
887 }
888
889 /**
890 * @tc.name: ExecutorCache002
891 * @tc.desc: Fail to call func in each ExecutorState
892 * @tc.type: FUNC
893 * @tc.require:
894 * @tc.author: bty
895 */
896 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, ExecutorCache002, TestSize.Level1)
897 {
898 /**
899 * @tc.steps: step1. In MAINDB
900 * @tc.expected: step1. Expect not E_OK
901 */
902 NotifyMigrateSyncData syncData;
903 DataItem dataItem;
904 std::vector<DataItem> items;
905 items.push_back(dataItem);
906 EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), -E_INVALID_DB);
907 uint64_t version;
908 EXPECT_EQ(g_handle->GetMinVersionCacheData(items, version), -E_INVALID_ARGS);
909 EXPECT_EQ(g_handle->GetMaxVersionInCacheDb(version), -E_INVALID_ARGS);
910 EXPECT_EQ(g_handle->MigrateLocalData(), -E_INVALID_ARGS);
911
912 /**
913 * @tc.steps: step2. Change executor to CACHE_ATTACH_MAIN
914 * @tc.expected: step2. Expect SQL_STATE_ERR
915 */
916 sqlite3 *sqlHandle = nullptr;
917 EXPECT_EQ(SQLiteUtils::OpenDatabase({g_testDir + g_databaseName, false, false}, sqlHandle), E_OK);
918 ASSERT_NE(sqlHandle, nullptr);
919 auto executor = std::make_unique<SQLiteSingleVerStorageExecutor>(
920 sqlHandle, false, false, ExecutorState::CACHE_ATTACH_MAIN);
921 ASSERT_NE(executor, nullptr);
922 EXPECT_EQ(executor->MigrateSyncDataByVersion(0u, syncData, items), SQL_STATE_ERR);
923 EXPECT_EQ(executor->GetMinVersionCacheData(items, version), SQL_STATE_ERR);
924 EXPECT_EQ(executor->GetMaxVersionInCacheDb(version), SQL_STATE_ERR);
925 EXPECT_EQ(executor->MigrateLocalData(), SQL_STATE_ERR);
926 executor->SetAttachMetaMode(true);
927 EXPECT_EQ(executor->MigrateSyncDataByVersion(0u, syncData, items), SQL_STATE_ERR);
928
929 /**
930 * @tc.steps: step3. Change executor to MAIN_ATTACH_CACHE
931 */
932 auto executor2 = std::make_unique<SQLiteSingleVerStorageExecutor>(
933 sqlHandle, false, false, ExecutorState::MAIN_ATTACH_CACHE);
934 ASSERT_NE(executor2, nullptr);
935 items.clear();
936 EXPECT_EQ(executor2->MigrateSyncDataByVersion(0u, syncData, items), -E_INVALID_ARGS);
937 items.push_back(dataItem);
938 EXPECT_EQ(executor2->MigrateSyncDataByVersion(0u, syncData, items), SQL_STATE_ERR);
939 EXPECT_EQ(executor2->GetMinVersionCacheData(items, version), SQL_STATE_ERR);
940 EXPECT_EQ(executor2->GetMaxVersionInCacheDb(version), SQL_STATE_ERR);
941 EXPECT_EQ(executor2->MigrateLocalData(), SQL_STATE_ERR);
942 sqlite3_close_v2(sqlHandle);
943 sqlHandle = nullptr;
944 }
945
946 /**
947 * @tc.name: ExecutorCache003
948 * @tc.desc: Test different condition to attach db
949 * @tc.type: FUNC
950 * @tc.require:
951 * @tc.author: bty
952 */
953 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, ExecutorCache003, TestSize.Level1)
954 {
955 /**
956 * @tc.steps: step1. Copy empty db, then attach
957 */
958 string cacheDir = g_testDir + "/" + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR +
959 "/" + DBConstant::CACHEDB_DIR + "/" + DBConstant::SINGLE_VER_CACHE_STORE + DBConstant::SQLITE_DB_EXTENSION;
960 EXPECT_EQ(DBCommon::CopyFile(g_testDir + g_databaseName, cacheDir), E_OK);
961 CipherPassword password;
962 EXPECT_EQ(g_nullHandle->AttachMainDbAndCacheDb(
963 CipherType::DEFAULT, password, cacheDir, EngineState::INVALID), -E_INVALID_ARGS);
964 EXPECT_EQ(g_nullHandle->AttachMainDbAndCacheDb(
965 CipherType::DEFAULT, password, cacheDir, EngineState::CACHEDB), -E_INVALID_DB);
966 EXPECT_EQ(g_nullHandle->AttachMainDbAndCacheDb(
967 CipherType::DEFAULT, password, cacheDir, EngineState::ATTACHING), E_OK);
968 EXPECT_EQ(g_handle->AttachMainDbAndCacheDb(
969 CipherType::DEFAULT, password, cacheDir, EngineState::MAINDB), E_OK);
970
971 /**
972 * @tc.steps: step2. Try migrate data after attaching cache
973 * @tc.expected: step2. Expect SQL_STATE_ERR
974 */
975 NotifyMigrateSyncData syncData;
976 DataItem dataItem;
977 std::vector<DataItem> items;
978 items.push_back(dataItem);
979 EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), SQL_STATE_ERR);
980 }
981
982 /**
983 * @tc.name: ExecutorCache004
984 * @tc.desc: Test migrate after attaching
985 * @tc.type: FUNC
986 * @tc.require:
987 * @tc.author: bty
988 */
989 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, ExecutorCache004, TestSize.Level1)
990 {
991 /**
992 * @tc.steps: step1. Copy normal db, attach cache
993 * @tc.expected: step1. Expect E_OK
994 */
995 string cacheDir = g_testDir + "/" + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR +
996 "/" + DBConstant::CACHEDB_DIR + "/" + DBConstant::SINGLE_VER_CACHE_STORE + DBConstant::SQLITE_DB_EXTENSION;
997 EXPECT_EQ(g_handle->ForceCheckPoint(), E_OK);
998 EXPECT_EQ(DBCommon::CopyFile(g_testDir + g_databaseName, cacheDir), E_OK);
999 CipherPassword password;
1000 EXPECT_EQ(g_handle->AttachMainDbAndCacheDb(
1001 CipherType::DEFAULT, password, cacheDir, EngineState::MAINDB), E_OK);
1002
1003 /**
1004 * @tc.steps: step2. Migrate sync data but param incomplete
1005 */
1006 NotifyMigrateSyncData syncData;
1007 DataItem dataItem;
1008 std::vector<DataItem> items;
1009 items.push_back(dataItem);
1010 EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), -E_INVALID_ARGS);
1011 Timestamp timestamp;
1012 EXPECT_EQ(g_handle->GetMaxTimestampDuringMigrating(timestamp), E_OK);
1013 items.front().neglect = true;
1014 EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), SQL_STATE_ERR);
1015 items.front().neglect = false;
1016 items.front().flag = DataItem::REMOVE_DEVICE_DATA_FLAG;
1017 EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), -E_INVALID_ARGS);
1018 items.front().key = {'r', 'e', 'm', 'o', 'v', 'e'};
1019 EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), SQL_STATE_ERR);
1020 items.front().flag = DataItem::REMOVE_DEVICE_DATA_NOTIFY_FLAG;
1021 EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), SQL_STATE_ERR);
1022 items.front().flag = DataItem::REMOTE_DEVICE_DATA_MISS_QUERY;
1023 EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), -E_INVALID_DB);
1024 string selectSync = "SELECT * FROM sync_data";
1025 Value value;
1026 value.assign(selectSync.begin(), selectSync.end());
1027 items.front().value = value;
1028 items.front().flag = DataItem::REMOVE_DEVICE_DATA_NOTIFY_FLAG;
1029 EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), SQL_STATE_ERR);
1030 EXPECT_EQ(g_handle->MigrateLocalData(), E_OK);
1031
1032 /**
1033 * @tc.steps: step3. Attach maindb
1034 */
1035 EXPECT_EQ(g_handle->AttachMainDbAndCacheDb(
1036 CipherType::DEFAULT, password, cacheDir, EngineState::CACHEDB), E_OK);
1037 EXPECT_EQ(g_handle->MigrateLocalData(), E_OK);
1038 EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), -E_BUSY);
1039 }
1040
1041 /**
1042 * @tc.name: ExecutorCache005
1043 * @tc.desc: Alter table then save data in cache mode
1044 * @tc.type: FUNC
1045 * @tc.require:
1046 * @tc.author: bty
1047 */
1048 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, ExecutorCache005, TestSize.Level1)
1049 {
1050 g_store->ReleaseHandle(g_handle);
1051 sqlite3 *db = nullptr;
1052 ASSERT_TRUE(sqlite3_open_v2((g_testDir + g_databaseName).c_str(),
1053 &db, SQLITE_OPEN_URI | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr) == SQLITE_OK);
1054 ASSERT_TRUE(SQLiteUtils::ExecuteRawSQL(db, ADD_LOCAL) == E_OK);
1055 ASSERT_TRUE(SQLiteUtils::ExecuteRawSQL(db, ADD_SYNC) == E_OK);
1056 auto executor = std::make_unique<SQLiteSingleVerStorageExecutor>(db, false, false);
1057 ASSERT_NE(executor, nullptr);
1058 LocalDataItem item;
1059 EXPECT_EQ(executor->PutLocalDataToCacheDB(item), -E_INVALID_ARGS);
1060 item.hashKey = KEY_1;
1061 EXPECT_EQ(executor->PutLocalDataToCacheDB(item), -E_INVALID_ARGS);
1062 item.flag = DataItem::DELETE_FLAG;
1063 EXPECT_EQ(executor->PutLocalDataToCacheDB(item), E_OK);
1064 Query query = Query::Select();
1065 QueryObject object(query);
1066 DataItem dataItem;
1067 dataItem.flag = DataItem::REMOTE_DEVICE_DATA_MISS_QUERY;
1068 DeviceInfo info;
1069 Timestamp maxTime;
1070 EXPECT_EQ(executor->SaveSyncDataItemInCacheMode(dataItem, info, maxTime, 0, object), -E_INVALID_ARGS);
1071 dataItem.key = KEY_1;
1072 EXPECT_EQ(executor->SaveSyncDataItemInCacheMode(dataItem, info, maxTime, 0, object), E_OK);
1073 dataItem.flag = DataItem::DELETE_FLAG;
1074 EXPECT_EQ(executor->SaveSyncDataItemInCacheMode(dataItem, info, maxTime, 0, object), E_OK);
1075 sqlite3_close_v2(db);
1076 }