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 #ifdef USE_RD_KERNEL
16 #include <cstdint>
17 #include <gtest/gtest.h>
18 
19 #include "db_constant.h"
20 #include "db_common.h"
21 #include "distributeddb_storage_single_ver_natural_store_testcase.h"
22 #include "rd_single_ver_natural_store.h"
23 #include "rd_single_ver_natural_store_connection.h"
24 #include "kvdb_pragma.h"
25 #include "storage_engine_manager.h"
26 
27 using namespace testing::ext;
28 using namespace DistributedDB;
29 using namespace DistributedDBUnitTest;
30 using namespace std;
31 
32 namespace {
33     string g_testDir;
34     string g_databaseName;
35     string g_identifier;
36     KvDBProperties g_property;
37 
38     RdSingleVerNaturalStore *g_store = nullptr;
39     RdSingleVerNaturalStoreConnection *g_connection = nullptr;
40     RdSingleVerStorageExecutor *g_handle = nullptr;
41     RdSingleVerStorageExecutor *g_nullHandle = nullptr;
42 }
43 
44 class DistributedDBStorageRdSingleVerNaturalExecutorTest : public testing::Test {
45 public:
46     static void SetUpTestCase(void);
47     static void TearDownTestCase(void);
48     void SetUp();
49     void TearDown();
50 };
51 
SetUpTestCase(void)52 void DistributedDBStorageRdSingleVerNaturalExecutorTest::SetUpTestCase(void)
53 {
54     DistributedDBToolsUnitTest::TestDirInit(g_testDir);
55     LOGI("DistributedDBStorageRdSingleVerNaturalExecutorTest dir is %s", g_testDir.c_str());
56     std::string oriIdentifier = APP_ID + "-" + USER_ID + "-" + "TestGeneralNBExecutor";
57     std::string identifier = DBCommon::TransferHashString(oriIdentifier);
58     g_identifier = DBCommon::TransferStringToHex(identifier);
59 
60     g_databaseName = "/" + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR + "/" + DBConstant::MAINDB_DIR + "/" +
61         DBConstant::SINGLE_VER_DATA_STORE + DBConstant::DB_EXTENSION;
62     g_property.SetStringProp(KvDBProperties::DATA_DIR, g_testDir);
63     g_property.SetStringProp(KvDBProperties::STORE_ID, "TestGeneralNBExecutor");
64     g_property.SetStringProp(KvDBProperties::IDENTIFIER_DIR, g_identifier);
65     g_property.SetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::SINGLE_VER_TYPE_RD_KERNAL);
66 }
67 
TearDownTestCase(void)68 void DistributedDBStorageRdSingleVerNaturalExecutorTest::TearDownTestCase(void)
69 {
70     DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir + "/" + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR);
71 }
72 
SetUp(void)73 void DistributedDBStorageRdSingleVerNaturalExecutorTest::SetUp(void)
74 {
75     DistributedDBToolsUnitTest::PrintTestCaseInfo();
76     DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir + "/" + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR);
77     g_store = new (std::nothrow) RdSingleVerNaturalStore;
78     ASSERT_NE(g_store, nullptr);
79     ASSERT_EQ(g_store->Open(g_property), E_OK);
80 
81     int erroCode = E_OK;
82     g_connection = static_cast<RdSingleVerNaturalStoreConnection *>(g_store->GetDBConnection(erroCode));
83     ASSERT_NE(g_connection, nullptr);
84     g_store->DecObjRef(g_store);
85     EXPECT_EQ(erroCode, E_OK);
86 
87     g_handle = static_cast<RdSingleVerStorageExecutor *>(
88         g_store->GetHandle(true, erroCode, OperatePerm::NORMAL_PERM));
89     ASSERT_EQ(erroCode, E_OK);
90     ASSERT_NE(g_handle, nullptr);
91 
92     g_nullHandle = new (nothrow) RdSingleVerStorageExecutor(nullptr, false);
93     ASSERT_NE(g_nullHandle, nullptr);
94 }
95 
TearDown(void)96 void DistributedDBStorageRdSingleVerNaturalExecutorTest::TearDown(void)
97 {
98     if (g_nullHandle != nullptr) {
99         delete g_nullHandle;
100         g_nullHandle = nullptr;
101     }
102     if (g_store != nullptr) {
103         g_store->ReleaseHandle(g_handle);
104     }
105     if (g_connection != nullptr) {
106         g_connection->Close();
107         g_connection = nullptr;
108     }
109     g_store = nullptr;
110     g_handle = nullptr;
111 }
112 
113 /**
114   * @tc.name: InvalidParam001
115   * @tc.desc: Get Kv Data with Invalid condition
116   * @tc.type: FUNC
117   * @tc.require:
118   * @tc.author: bty
119   */
120 HWTEST_F(DistributedDBStorageRdSingleVerNaturalExecutorTest, InvalidParam001, TestSize.Level1)
121 {
122     /**
123      * @tc.steps: step1. The Data type is invalid
124      * @tc.expected: step1. Expect -E_INVALID_ARGS
125      */
126     Timestamp timestamp = 0;
127     Key key;
128     Value value;
129     int type = static_cast<int>(SingleVerDataType::SYNC_TYPE);
130     EXPECT_EQ(g_nullHandle->GetKvData(SingleVerDataType(type + 1), key, value, timestamp), -E_INVALID_ARGS);
131 
132     /**
133      * @tc.steps: step2. The key is empty
134      * @tc.expected: step2. Expect -E_INVALID_ARGS
135      */
136     EXPECT_EQ(g_handle->GetKvData(SingleVerDataType(type), key, value, timestamp), -E_INVALID_ARGS);
137 
138     /**
139      * @tc.steps: step3. The db is null
140      * @tc.expected: step3. Expect -E_INVALID_DB
141      */
142     EXPECT_EQ(g_nullHandle->GetKvData(SingleVerDataType(type), KEY_1, value, timestamp), -E_INVALID_DB);
143 }
144 
145 /**
146   * @tc.name: InvalidParam002
147   * @tc.desc: Put Kv Data check
148   * @tc.type: FUNC
149   * @tc.require:
150   * @tc.author: bty
151   */
152 HWTEST_F(DistributedDBStorageRdSingleVerNaturalExecutorTest, InvalidParam002, TestSize.Level1)
153 {
154     /**
155      * @tc.steps: step1. rd unsupport put kv data
156      * @tc.expected: step1. Expect -E_NOT_SUPPORT
157      */
158     Value value;
159     EXPECT_EQ(g_nullHandle->PutKvData(SingleVerDataType::SYNC_TYPE, KEY_1, value, 0, nullptr), -E_NOT_SUPPORT);
160 
161     /**
162      * @tc.steps: step2. rd unsupport put kv data
163      * @tc.expected: step2. Expect -E_NOT_SUPPORT
164      */
165     EXPECT_EQ(g_nullHandle->PutKvData(SingleVerDataType::META_TYPE, KEY_1, value, 0, nullptr), -E_NOT_SUPPORT);
166 }
167 
168 /**
169   * @tc.name: InvalidParam005
170   * @tc.desc: Test timestamp with Invalid condition (rd not support timestamp)
171   * @tc.type: FUNC
172   * @tc.require:
173   * @tc.author: bty
174   */
175 HWTEST_F(DistributedDBStorageRdSingleVerNaturalExecutorTest, InvalidParam005, TestSize.Level1)
176 {
177     /**
178      * @tc.steps: step1. The db is null
179      * @tc.expected: step1. Expect return 0
180      */
181     Timestamp timestamp = 0;
182     g_nullHandle->InitCurrentMaxStamp(timestamp);
183     EXPECT_EQ(timestamp, 0u);
184 
185     /**
186      * @tc.steps: step2. Get timestamp when The db is null
187      * @tc.expected: step2. Expect -E_NOT_SUPPORT
188      */
189     std::vector<DataItem> dataItems;
190     Timestamp begin = 0;
191     Timestamp end = INT64_MAX;
192     DataSizeSpecInfo info;
193     EXPECT_EQ(g_nullHandle->GetSyncDataByTimestamp(dataItems, sizeof("time"), begin, end, info), -E_NOT_SUPPORT);
194     EXPECT_EQ(g_nullHandle->GetDeletedSyncDataByTimestamp(dataItems, sizeof("time"), begin, end, info),
195         -E_NOT_SUPPORT);
196 }
197 
198 /**
199   * @tc.name: InvalidParam008
200   * @tc.desc: Test transaction with Invalid condition (rd not support transcaction yet)
201   * @tc.type: FUNC
202   * @tc.require:
203   * @tc.author: bty
204   */
205 HWTEST_F(DistributedDBStorageRdSingleVerNaturalExecutorTest, InvalidParam008, TestSize.Level1)
206 {
207     EXPECT_EQ(g_nullHandle->StartTransaction(TransactType::DEFERRED), E_OK);    // -E_INVALID_DB
208     EXPECT_EQ(g_nullHandle->Commit(), E_OK);   // -E_INVALID_DB
209     EXPECT_EQ(g_nullHandle->Rollback(), E_OK); // -E_INVALID_DB
210 
211     EXPECT_EQ(g_handle->StartTransaction(TransactType::DEFERRED), E_OK);
212     EXPECT_EQ(g_handle->Reset(), -E_NOT_SUPPORT);
213 }
214 
215 /**
216   * @tc.name: InvalidParam009
217   * @tc.desc: Get identifier with Invalid condition (rd not support)
218   * @tc.type: FUNC
219   * @tc.require:
220   * @tc.author: bty
221   */
222 HWTEST_F(DistributedDBStorageRdSingleVerNaturalExecutorTest, InvalidParam009, TestSize.Level1)
223 {
224     /**
225      * @tc.steps: step1. The parameter is null
226      * @tc.expected: step1. Expect -E_INVALID_ARGS
227      */
228     EXPECT_EQ(g_nullHandle->GetDeviceIdentifier(nullptr), -E_NOT_SUPPORT);
229 
230     /**
231      * @tc.steps: step2. The db is null
232      * @tc.expected: step2. Expect -E_INVALID_DB
233      */
234     PragmaEntryDeviceIdentifier identifier;
235     EXPECT_EQ(g_nullHandle->GetDeviceIdentifier(&identifier), -E_NOT_SUPPORT);
236 
237     /**
238      * @tc.steps: step3. The identifier is empty
239      * @tc.expected: step3. Expect -E_INVALID_ARGS
240      */
241     EXPECT_EQ(g_handle->GetDeviceIdentifier(&identifier), -E_NOT_SUPPORT);
242 }
243 
244 /**
245   * @tc.name: InvalidParam010
246   * @tc.desc: Fail to call function with Invalid condition
247   * @tc.type: FUNC
248   * @tc.require:
249   * @tc.author: bty
250   */
251 HWTEST_F(DistributedDBStorageRdSingleVerNaturalExecutorTest, InvalidParam010, TestSize.Level1)
252 {
253     vector<Key> keys;
254     EXPECT_EQ(g_nullHandle->GetAllMetaKeys(keys), -E_NOT_SUPPORT);
255     string devName;
256     vector<Entry> entries;
257     EXPECT_EQ(g_nullHandle->GetAllSyncedEntries(devName, entries), -E_NOT_SUPPORT);
258     EXPECT_EQ(g_nullHandle->ForceCheckPoint(), -E_INVALID_DB);
259     EXPECT_EQ(g_nullHandle->CheckIntegrity(), -E_NOT_SUPPORT);
260 }
261 
262 /**
263   * @tc.name: ConnectionTest001
264   * @tc.desc: Failed to get the keys (rd not support yet)
265   * @tc.type: FUNC
266   * @tc.require:
267   * @tc.author: bty
268   */
269 HWTEST_F(DistributedDBStorageRdSingleVerNaturalExecutorTest, ConnectionTest001, TestSize.Level1)
270 {
271     /**
272      * @tc.steps: step1. the dataType is error
273      * @tc.expected: step1. Expect -E_INVALID_ARGS
274      */
275     IOption option;
276     option.dataType = IOption::SYNC_DATA + 1;
277     vector<Key> keys;
278     EXPECT_EQ(g_connection->GetKeys(option, KEY_1, keys), -E_NOT_SUPPORT);
279 
280     /**
281      * @tc.steps: step2. Get keys in cacheDB state
282      * @tc.expected: step2. Expect -E_EKEYREVOKED
283      */
284     int errCode = E_OK;
285     RdSingleVerStorageEngine *storageEngine =
286         static_cast<RdSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
287     ASSERT_EQ(errCode, E_OK);
288     ASSERT_NE(storageEngine, nullptr);
289     storageEngine->SetEngineState(EngineState::CACHEDB);
290     option.dataType = IOption::LOCAL_DATA;
291     EXPECT_EQ(g_connection->GetKeys(option, KEY_1, keys), -E_NOT_SUPPORT);
292     storageEngine->Release();
293 
294     /**
295      * @tc.steps: step3. Get keys in null db connection
296      * @tc.expected: step3. Expect -E_NOT_INIT
297      */
298     std::unique_ptr<RdSingleVerNaturalStoreConnection> emptyConn =
299         std::make_unique<RdSingleVerNaturalStoreConnection>(nullptr);
300     ASSERT_NE(emptyConn, nullptr);
301     EXPECT_EQ(emptyConn->GetKeys(option, KEY_1, keys), -E_NOT_SUPPORT);
302 }
303 
304 /**
305   * @tc.name: ConnectionTest002
306   * @tc.desc: Push and delete on empty connect
307   * @tc.type: FUNC
308   * @tc.require:
309   * @tc.author: bty
310   */
311 HWTEST_F(DistributedDBStorageRdSingleVerNaturalExecutorTest, ConnectionTest002, TestSize.Level1)
312 {
313     std::unique_ptr<RdSingleVerNaturalStoreConnection> emptyConn =
314         std::make_unique<RdSingleVerNaturalStoreConnection>(nullptr);
315     IOption option = {IOption::SYNC_DATA};
316     std::vector<Entry> entries;
317     EXPECT_EQ(emptyConn->PutBatch(option, entries), -E_INVALID_DB);
318     std::vector<Key> keys;
319     EXPECT_EQ(emptyConn->DeleteBatch(option, keys), -E_INVALID_DB);
320     option.dataType = IOption::SYNC_DATA;
321     EXPECT_EQ(emptyConn->PutBatch(option, entries), -E_INVALID_DB);
322     EXPECT_EQ(emptyConn->DeleteBatch(option, keys), -E_INVALID_DB);
323     option.dataType = IOption::SYNC_DATA + 1;
324     EXPECT_EQ(emptyConn->PutBatch(option, entries), -E_NOT_SUPPORT);
325 }
326 
327 /**
328   * @tc.name: PragmaTest001
329   * @tc.desc: Calling Pragma incorrectly
330   * @tc.type: FUNC
331   * @tc.require:
332   * @tc.author: bty
333   */
334 HWTEST_F(DistributedDBStorageRdSingleVerNaturalExecutorTest, PragmaTest001, TestSize.Level1)
335 {
336     /**
337      * @tc.steps: step1. the parameter is null
338      * @tc.expected: step1. Expect -E_INVALID_ARGS
339      */
340     // Rd Pragma only support check point for now
341     EXPECT_EQ(g_connection->Pragma(PRAGMA_RESULT_SET_CACHE_MAX_SIZE, nullptr), -E_NOT_SUPPORT);
342     EXPECT_EQ(g_connection->Pragma(PRAGMA_RESULT_SET_CACHE_MODE, nullptr), -E_NOT_SUPPORT);
343     EXPECT_EQ(g_connection->Pragma(PRAGMA_SET_AUTO_LIFE_CYCLE, nullptr), -E_NOT_SUPPORT);
344     EXPECT_EQ(g_connection->Pragma(PRAGMA_UNPUBLISH_SYNC, nullptr), -E_NOT_SUPPORT);
345     EXPECT_EQ(g_connection->Pragma(PRAGMA_PUBLISH_LOCAL, nullptr), -E_NOT_SUPPORT);
346     EXPECT_EQ(g_connection->Pragma(PRAGMA_GET_DEVICE_IDENTIFIER_OF_ENTRY, nullptr), -E_NOT_SUPPORT);
347     EXPECT_EQ(g_connection->Pragma(PRAGMA_SET_MAX_LOG_LIMIT, nullptr), -E_NOT_SUPPORT);
348     EXPECT_EQ(g_connection->Pragma(PRAGMA_GET_IDENTIFIER_OF_DEVICE, nullptr), -E_NOT_SUPPORT);
349 
350     /**
351      * @tc.steps: step2. the option is invalid
352      * @tc.expected: step2. Expect -E_INVALID_ARGS
353      */
354     std::unique_ptr<RdSingleVerNaturalStoreConnection> emptyConn =
355         std::make_unique<RdSingleVerNaturalStoreConnection>(nullptr);
356     ASSERT_NE(emptyConn, nullptr);
357     SecurityOption option = {S3, SECE};
358     EXPECT_EQ(emptyConn->Pragma(PRAGMA_TRIGGER_TO_MIGRATE_DATA, &option), -E_NOT_SUPPORT);
359 
360     /**
361      * @tc.steps: step3. the size is invalid
362      * @tc.expected: step3. Expect -E_INVALID_ARGS
363      */
364     int size = 0;
365     EXPECT_EQ(emptyConn->Pragma(PRAGMA_RESULT_SET_CACHE_MAX_SIZE, &size), -E_NOT_SUPPORT);
366     size = 1;
367     EXPECT_EQ(emptyConn->Pragma(PRAGMA_RESULT_SET_CACHE_MAX_SIZE, &size), -E_NOT_SUPPORT);
368 
369     /**
370      * @tc.steps: step4. the mode is invalid
371      * @tc.expected: step4. Expect -E_INVALID_ARGS
372      */
373     ResultSetCacheMode mode = ResultSetCacheMode(2); // 2 is invalid mode
374     EXPECT_EQ(emptyConn->Pragma(PRAGMA_RESULT_SET_CACHE_MODE, &mode), -E_NOT_SUPPORT);
375 
376     /**
377      * @tc.steps: step5. the db is null
378      * @tc.expected: step5. Expect -E_INVALID_DB
379      */
380     int time = 6000; // 6000 is random
381     EXPECT_EQ(emptyConn->Pragma(PRAGMA_SET_AUTO_LIFE_CYCLE, &time), -E_NOT_SUPPORT);
382 }
383 
384 /**
385   * @tc.name: PragmaTest002
386   * @tc.desc: Incorrect publishing and unPublishing
387   * @tc.type: FUNC
388   * @tc.require:
389   * @tc.author: bty
390   */
391 HWTEST_F(DistributedDBStorageRdSingleVerNaturalExecutorTest, PragmaTest002, TestSize.Level1)
392 {
393     /**
394      * @tc.steps: step1. the db is null
395      * @tc.expected: step1. Expect -E_INVALID_DB
396      */
397     std::unique_ptr<RdSingleVerNaturalStoreConnection> emptyConn =
398         std::make_unique<RdSingleVerNaturalStoreConnection>(nullptr);
399     PragmaPublishInfo info;
400     EXPECT_EQ(emptyConn->Pragma(PRAGMA_PUBLISH_LOCAL, &info), -E_NOT_SUPPORT);
401     EXPECT_EQ(emptyConn->Pragma(PRAGMA_UNPUBLISH_SYNC, &info), -E_NOT_SUPPORT);
402 
403     /**
404      * @tc.steps: step2. publish in transaction
405      * @tc.expected: step2. Expect -E_NOT_SUPPORT
406      */
407     g_store->ReleaseHandle(g_handle);
408     g_connection->StartTransaction();
409     EXPECT_EQ(g_connection->Pragma(PRAGMA_PUBLISH_LOCAL, &info), -E_NOT_SUPPORT);
410     EXPECT_EQ(g_connection->Pragma(PRAGMA_UNPUBLISH_SYNC, &info), -E_NOT_SUPPORT);
411     g_connection->RollBack();
412 
413     /**
414      * @tc.steps: step3. publish in cacheDB
415      * @tc.expected: step3. Expect -E_EKEYREVOKED
416      */
417     int errCode = E_OK;
418     RdSingleVerStorageEngine *storageEngine =
419         static_cast<RdSingleVerStorageEngine *>(StorageEngineManager::GetStorageEngine(g_property, errCode));
420     ASSERT_EQ(errCode, E_OK);
421     ASSERT_NE(storageEngine, nullptr);
422     storageEngine->SetEngineState(EngineState::CACHEDB);
423     EXPECT_EQ(g_connection->Pragma(PRAGMA_PUBLISH_LOCAL, &info), -E_NOT_SUPPORT);
424     EXPECT_EQ(g_connection->Pragma(PRAGMA_UNPUBLISH_SYNC, &info), -E_NOT_SUPPORT);
425     g_connection->StartTransaction();
426     g_connection->Commit();
427     storageEngine->Release();
428 }
429 
430 /**
431   * @tc.name: PragmaTest003
432   * @tc.desc: Failed to call function with empty connection
433   * @tc.type: FUNC
434   * @tc.require:
435   * @tc.author: bty
436   */
437 HWTEST_F(DistributedDBStorageRdSingleVerNaturalExecutorTest, PragmaTest003, TestSize.Level1)
438 {
439     auto emptyConn = std::make_unique<RdSingleVerNaturalStoreConnection>(nullptr);
440     PragmaEntryDeviceIdentifier identifier = {.key = KEY_1};
441     EXPECT_EQ(emptyConn->Pragma(PRAGMA_GET_DEVICE_IDENTIFIER_OF_ENTRY, &identifier), -E_NOT_SUPPORT);
442     EXPECT_EQ(emptyConn->Pragma(PRAGMA_EXEC_CHECKPOINT, nullptr), -E_NOT_INIT);
443     EXPECT_EQ(emptyConn->CheckIntegrity(), -E_NOT_INIT);
444 
445     int limit = 0;
446     EXPECT_EQ(emptyConn->Pragma(PRAGMA_SET_MAX_LOG_LIMIT, &limit), -E_NOT_SUPPORT);
447     EXPECT_EQ(emptyConn->Pragma(PRAGMA_RM_DEVICE_DATA, nullptr), -E_NOT_SUPPORT);
448     CipherPassword pw;
449     EXPECT_EQ(emptyConn->Import("/a.b", pw), -E_INVALID_DB);
450     EXPECT_EQ(emptyConn->Export("/a.b", pw), -E_INVALID_DB);
451     DatabaseLifeCycleNotifier notifier;
452     EXPECT_EQ(emptyConn->RegisterLifeCycleCallback(notifier), -E_NOT_SUPPORT);
453 
454     EXPECT_EQ(emptyConn->SetConflictNotifier(0, nullptr), -E_NOT_SUPPORT);
__anon81a096a30202(const KvDBCommitNotifyData &data) 455     KvDBConflictAction func = [&](const KvDBCommitNotifyData &data) {};
456     EXPECT_EQ(emptyConn->SetConflictNotifier(0, func), -E_NOT_SUPPORT);
457     IKvDBSnapshot *shot;
458     EXPECT_EQ(emptyConn->GetSnapshot(shot), -E_NOT_SUPPORT);
459 }
460 
461 /**
462   * @tc.name: ExecutorCache001
463   * @tc.desc: Fail to operate data
464   * @tc.type: FUNC
465   * @tc.require:
466   * @tc.author: bty
467   */
468 HWTEST_F(DistributedDBStorageRdSingleVerNaturalExecutorTest, ExecutorCache001, TestSize.Level1)
469 {
470     g_handle->SetAttachMetaMode(true);
471     std::set<std::string> devices;
472     EXPECT_EQ(g_handle->GetExistsDevicesFromMeta(devices), -E_NOT_SUPPORT);
473     EXPECT_EQ(g_handle->DeleteMetaDataByPrefixKey(KEY_1), -E_NOT_SUPPORT);
474     std::vector<Key> keys;
475     EXPECT_EQ(g_handle->DeleteMetaData(keys), -E_NOT_SUPPORT);
476     EXPECT_EQ(g_handle->PrepareForSavingCacheData(SingleVerDataType::LOCAL_TYPE_SQLITE), -E_NOT_SUPPORT);
477     std::string hashDev = DBCommon::TransferHashString("device1");
478     EXPECT_EQ(g_handle->RemoveDeviceDataInCacheMode(hashDev, true, 0u), -E_NOT_SUPPORT);
479     Timestamp timestamp;
480     EXPECT_EQ(g_handle->GetMaxTimestampDuringMigrating(timestamp), -E_NOT_SUPPORT);
481     EXPECT_EQ(g_handle->ResetForSavingCacheData(SingleVerDataType::LOCAL_TYPE_SQLITE), -E_NOT_SUPPORT);
482 }
483 
484 /**
485   * @tc.name: ExecutorCache003
486   * @tc.desc: Test different condition to attach db
487   * @tc.type: FUNC
488   * @tc.require:
489   * @tc.author: bty
490   */
491 HWTEST_F(DistributedDBStorageRdSingleVerNaturalExecutorTest, ExecutorCache003, TestSize.Level1)
492 {
493     /**
494      * @tc.steps: step1. Copy empty db, then attach
495      */
496     string cacheDir = g_testDir + "/" + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR +
497         "/" + DBConstant::CACHEDB_DIR + "/" + DBConstant::SINGLE_VER_CACHE_STORE + DBConstant::DB_EXTENSION;
498     EXPECT_EQ(DBCommon::CopyFile(g_testDir + g_databaseName, cacheDir), E_OK);
499     CipherPassword password;
500     EXPECT_EQ(g_nullHandle->AttachMainDbAndCacheDb(
501         CipherType::DEFAULT, password, cacheDir, EngineState::INVALID), -E_NOT_SUPPORT);
502     EXPECT_EQ(g_nullHandle->AttachMainDbAndCacheDb(
503         CipherType::DEFAULT, password, cacheDir, EngineState::CACHEDB), -E_NOT_SUPPORT);
504     EXPECT_EQ(g_nullHandle->AttachMainDbAndCacheDb(
505         CipherType::DEFAULT, password, cacheDir, EngineState::ATTACHING), -E_NOT_SUPPORT);
506     EXPECT_EQ(g_handle->AttachMainDbAndCacheDb(
507         CipherType::DEFAULT, password, cacheDir, EngineState::MAINDB), -E_NOT_SUPPORT);
508 
509     /**
510      * @tc.steps: step2. Try migrate data after attaching cache
511      * @tc.expected: step2. Expect SQL_STATE_ERR
512      */
513     NotifyMigrateSyncData syncData;
514     DataItem dataItem;
515     std::vector<DataItem> items;
516     items.push_back(dataItem);
517     EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), -E_NOT_SUPPORT);
518 }
519 
520 /**
521   * @tc.name: ExecutorCache004
522   * @tc.desc: Test migrate after attaching
523   * @tc.type: FUNC
524   * @tc.require:
525   * @tc.author: bty
526   */
527 HWTEST_F(DistributedDBStorageRdSingleVerNaturalExecutorTest, ExecutorCache004, TestSize.Level1)
528 {
529     /**
530      * @tc.steps: step1. Copy normal db, attach cache
531      * @tc.expected: step1. Expect E_OK
532      */
533     string cacheDir = g_testDir + "/" + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR +
534         "/" + DBConstant::CACHEDB_DIR + "/" + DBConstant::SINGLE_VER_CACHE_STORE + DBConstant::DB_EXTENSION;
535     EXPECT_EQ(g_handle->ForceCheckPoint(), E_OK);
536     EXPECT_EQ(DBCommon::CopyFile(g_testDir + g_databaseName, cacheDir), E_OK);
537     CipherPassword password;
538     EXPECT_EQ(g_handle->AttachMainDbAndCacheDb(
539         CipherType::DEFAULT, password, cacheDir, EngineState::MAINDB), -E_NOT_SUPPORT);
540 
541     /**
542      * @tc.steps: step2. Migrate sync data but param incomplete
543      */
544     NotifyMigrateSyncData syncData;
545     DataItem dataItem;
546     std::vector<DataItem> items;
547     items.push_back(dataItem);
548     EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), -E_NOT_SUPPORT);
549     Timestamp timestamp;
550     EXPECT_EQ(g_handle->GetMaxTimestampDuringMigrating(timestamp), -E_NOT_SUPPORT);
551     items.front().neglect = true;
552     EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), -E_NOT_SUPPORT);
553     items.front().neglect = false;
554     items.front().flag = DataItem::REMOVE_DEVICE_DATA_FLAG;
555     EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), -E_NOT_SUPPORT);
556     items.front().key = {'r', 'e', 'm', 'o', 'v', 'e'};
557     EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), -E_NOT_SUPPORT);
558     items.front().flag = DataItem::REMOVE_DEVICE_DATA_NOTIFY_FLAG;
559     EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), -E_NOT_SUPPORT);
560     items.front().flag = DataItem::REMOTE_DEVICE_DATA_MISS_QUERY;
561     EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), -E_NOT_SUPPORT);
562     string selectSync = "SELECT * FROM sync_data";
563     Value value;
564     value.assign(selectSync.begin(), selectSync.end());
565     items.front().value = value;
566     items.front().flag = DataItem::REMOVE_DEVICE_DATA_NOTIFY_FLAG;
567     EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), -E_NOT_SUPPORT);
568     EXPECT_EQ(g_handle->MigrateLocalData(), -E_NOT_SUPPORT);
569 
570     /**
571      * @tc.steps: step3. Attach maindb
572      */
573     EXPECT_EQ(g_handle->AttachMainDbAndCacheDb(
574         CipherType::DEFAULT, password, cacheDir, EngineState::CACHEDB), -E_NOT_SUPPORT);
575     EXPECT_EQ(g_handle->MigrateLocalData(), -E_NOT_SUPPORT);
576     EXPECT_EQ(g_handle->MigrateSyncDataByVersion(0u, syncData, items), -E_NOT_SUPPORT);
577 }
578 #endif // USE_RD_KERNEL