• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #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 
22 using namespace testing::ext;
23 using namespace DistributedDB;
24 using namespace DistributedDBUnitTest;
25 using namespace std;
26 
27 namespace {
28     string g_testDir;
29     string g_databaseName;
30     string g_identifier;
31     KvDBProperties g_property;
32 
33     SQLiteSingleVerNaturalStore *g_store = nullptr;
34     SQLiteSingleVerNaturalStoreConnection *g_connection = nullptr;
35     SQLiteSingleVerStorageExecutor *g_handle = nullptr;
36     SQLiteSingleVerStorageExecutor *g_nullHandle = nullptr;
37 }
38 
39 class DistributedDBStorageSQLiteSingleVerNaturalExecutorTest : public testing::Test {
40 public:
41     static void SetUpTestCase(void);
42     static void TearDownTestCase(void);
43     void SetUp();
44     void TearDown();
45 };
46 
SetUpTestCase(void)47 void DistributedDBStorageSQLiteSingleVerNaturalExecutorTest::SetUpTestCase(void)
48 {
49     DistributedDBToolsUnitTest::TestDirInit(g_testDir);
50     LOGD("DistributedDBStorageSQLiteSingleVerNaturalExecutorTest dir is %s", g_testDir.c_str());
51     std::string oriIdentifier = APP_ID + "-" + USER_ID + "-" + "TestGeneralNBExecutor";
52     std::string identifier = DBCommon::TransferHashString(oriIdentifier);
53     g_identifier = DBCommon::TransferStringToHex(identifier);
54 
55     g_databaseName = "/" + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR + "/" + DBConstant::MAINDB_DIR + "/" +
56         DBConstant::SINGLE_VER_DATA_STORE + ".db";
57     g_property.SetStringProp(KvDBProperties::DATA_DIR, g_testDir);
58     g_property.SetStringProp(KvDBProperties::STORE_ID, "TestGeneralNBExecutor");
59     g_property.SetStringProp(KvDBProperties::IDENTIFIER_DIR, g_identifier);
60     g_property.SetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::SINGLE_VER_TYPE);
61 }
62 
TearDownTestCase(void)63 void DistributedDBStorageSQLiteSingleVerNaturalExecutorTest::TearDownTestCase(void)
64 {
65     DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir + "/" + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR);
66 }
67 
SetUp(void)68 void DistributedDBStorageSQLiteSingleVerNaturalExecutorTest::SetUp(void)
69 {
70     DistributedDBToolsUnitTest::PrintTestCaseInfo();
71     DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir + "/" + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR);
72     g_store = new (std::nothrow) SQLiteSingleVerNaturalStore;
73     ASSERT_NE(g_store, nullptr);
74     ASSERT_EQ(g_store->Open(g_property), E_OK);
75 
76     int erroCode = E_OK;
77     g_connection = static_cast<SQLiteSingleVerNaturalStoreConnection *>(g_store->GetDBConnection(erroCode));
78     ASSERT_NE(g_connection, nullptr);
79     g_store->DecObjRef(g_store);
80     EXPECT_EQ(erroCode, E_OK);
81 
82     g_handle = static_cast<SQLiteSingleVerStorageExecutor *>(
83         g_store->GetHandle(true, erroCode, OperatePerm::NORMAL_PERM));
84     ASSERT_EQ(erroCode, E_OK);
85     ASSERT_NE(g_handle, nullptr);
86 
87     g_nullHandle = new (nothrow) SQLiteSingleVerStorageExecutor(nullptr, false, false);
88     ASSERT_NE(g_nullHandle, nullptr);
89 }
90 
TearDown(void)91 void DistributedDBStorageSQLiteSingleVerNaturalExecutorTest::TearDown(void)
92 {
93     if (g_nullHandle != nullptr) {
94         delete g_nullHandle;
95         g_nullHandle = nullptr;
96     }
97     if (g_store != nullptr) {
98         g_store->ReleaseHandle(g_handle);
99     }
100     if (g_connection != nullptr) {
101         g_connection->Close();
102         g_connection = nullptr;
103     }
104     g_store = nullptr;
105     g_handle = nullptr;
106 }
107 
108 /**
109   * @tc.name: Destructor001
110   * @tc.desc: Test the destructor of g_handle when the transaction is opened
111   * @tc.type: FUNC
112   * @tc.require:
113   * @tc.author: bty
114   */
115 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, Destructor001, TestSize.Level1)
116 {
117     g_handle->StartTransaction(TransactType::DEFERRED);
118 }
119 
120 /**
121   * @tc.name: InvalidParam001
122   * @tc.desc: Get Kv Data with Invalid condition
123   * @tc.type: FUNC
124   * @tc.require:
125   * @tc.author: bty
126   */
127 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam001, TestSize.Level1)
128 {
129     /**
130      * @tc.steps: step1. The Data type is invalid
131      * @tc.expected: step1. Expect -E_INVALID_ARGS
132      */
133     Timestamp timestamp = 0;
134     Key key;
135     Value value;
136     int type = static_cast<int>(SingleVerDataType::SYNC_TYPE);
137     EXPECT_EQ(g_nullHandle->GetKvData(SingleVerDataType(type + 1), key, value, timestamp), -E_INVALID_ARGS);
138 
139     /**
140      * @tc.steps: step2. The db is null
141      * @tc.expected: step2. Expect -E_INVALID_DB
142      */
143     EXPECT_EQ(g_nullHandle->GetKvData(SingleVerDataType(type), key, value, timestamp), -E_INVALID_DB);
144 
145     /**
146      * @tc.steps: step3. The key is empty
147      * @tc.expected: step3. Expect -E_INVALID_ARGS
148      */
149     EXPECT_EQ(g_handle->GetKvData(SingleVerDataType(type), key, value, timestamp), -E_INVALID_ARGS);
150 }
151 
152 /**
153   * @tc.name: InvalidParam002
154   * @tc.desc: Put Kv Data with Invalid condition
155   * @tc.type: FUNC
156   * @tc.require:
157   * @tc.author: bty
158   */
159 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam002, TestSize.Level1)
160 {
161     /**
162      * @tc.steps: step1. The Data type is invalid
163      * @tc.expected: step1. Expect -E_INVALID_ARGS
164      */
165     Value value;
166     EXPECT_EQ(g_nullHandle->PutKvData(SingleVerDataType::SYNC_TYPE, KEY_1, value, 0, nullptr), -E_INVALID_ARGS);
167 
168     /**
169      * @tc.steps: step2. The db is null
170      * @tc.expected: step2. Expect -E_INVALID_DB
171      */
172     EXPECT_EQ(g_nullHandle->PutKvData(SingleVerDataType::META_TYPE, KEY_1, value, 0, nullptr), -E_INVALID_DB);
173 
174     /**
175      * @tc.steps: step3. The key is null
176      * @tc.expected: step3. Expect -E_INVALID_ARGS
177      */
178     Key key;
179     EXPECT_EQ(g_handle->PutKvData(SingleVerDataType::META_TYPE, key, value, 0, nullptr), -E_INVALID_ARGS);
180 }
181 
182 /**
183   * @tc.name: InvalidParam004
184   * @tc.desc: Get count with Invalid condition
185   * @tc.type: FUNC
186   * @tc.require:
187   * @tc.author: bty
188   */
189 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam004, TestSize.Level1)
190 {
191     /**
192      * @tc.steps: step1. The db is null
193      * @tc.expected: step1. Expect -E_INVALID_DB
194      */
195     Query query = Query::Select().OrderBy("abc", false);
196     QueryObject object(query);
197     int count;
198     EXPECT_EQ(g_nullHandle->GetCount(object, count), -E_INVALID_DB);
199 
200     /**
201      * @tc.steps: step2. The query condition is invalid
202      * @tc.expected: step2. Expect -E_NOT_SUPPORT
203      */
204     EXPECT_EQ(g_handle->GetCount(object, count), -E_NOT_SUPPORT);
205 }
206 
207 /**
208   * @tc.name: InvalidParam005
209   * @tc.desc: Test timestamp with Invalid condition
210   * @tc.type: FUNC
211   * @tc.require:
212   * @tc.author: bty
213   */
214 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam005, TestSize.Level1)
215 {
216     /**
217      * @tc.steps: step1. The db is null
218      * @tc.expected: step1. Expect return 0
219      */
220     Timestamp timestamp = 0;
221     g_nullHandle->InitCurrentMaxStamp(timestamp);
222     EXPECT_EQ(timestamp, 0u);
223 
224     /**
225      * @tc.steps: step2. Get timestamp when The db is null
226      * @tc.expected: step2. Expect -E_INVALID_DB
227      */
228     std::vector<DataItem> dataItems;
229     Timestamp begin = 0;
230     Timestamp end = INT64_MAX;
231     DataSizeSpecInfo info;
232     EXPECT_EQ(g_nullHandle->GetSyncDataByTimestamp(dataItems, sizeof("time"), begin, end, info), -E_INVALID_DB);
233     EXPECT_EQ(g_nullHandle->GetDeletedSyncDataByTimestamp(dataItems, sizeof("time"), begin, end, info), -E_INVALID_DB);
234 }
235 
236 /**
237   * @tc.name: InvalidParam006
238   * @tc.desc: Open and get resultSet with Invalid condition
239   * @tc.type: FUNC
240   * @tc.require:
241   * @tc.author: bty
242   */
243 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam006, TestSize.Level1)
244 {
245     /**
246      * @tc.steps: step1. The db is null
247      * @tc.expected: step1. Expect -E_INVALID_DB
248      */
249     int count;
250     EXPECT_EQ(g_nullHandle->OpenResultSet(KEY_1, count), -E_INVALID_DB);
251     vector<int64_t> cache;
252     Key key;
253     EXPECT_EQ(g_nullHandle->OpenResultSetForCacheRowIdMode(KEY_1, cache, 0, count), -E_INVALID_DB);
254     Query query = Query::Select();
255     QueryObject object(query);
256     EXPECT_EQ(g_nullHandle->OpenResultSetForCacheRowIdMode(object, cache, 0, count), -E_INVALID_DB);
257 
258     /**
259      * @tc.steps: step2. Then get
260      * @tc.expected: step2. Expect -E_RESULT_SET_STATUS_INVALID
261      */
262     Value value;
263     EXPECT_EQ(g_nullHandle->GetNextEntryFromResultSet(key, value, false), -E_RESULT_SET_STATUS_INVALID);
264 
265     /**
266      * @tc.steps: step3. The db is valid,open
267      * @tc.expected: step3. Expect E_OK
268      */
269     EXPECT_EQ(g_handle->OpenResultSetForCacheRowIdMode(object, cache, 0, count), E_OK);
270 
271     /**
272      * @tc.steps: step4. Then get
273      * @tc.expected: step4. Expect -E_RESULT_SET_STATUS_INVALID
274      */
275     Entry entry;
276     EXPECT_EQ(g_handle->GetEntryByRowId(0, entry), -E_UNEXPECTED_DATA);
277 }
278 
279 /**
280   * @tc.name: InvalidParam007
281   * @tc.desc: Reload resultSet with Invalid condition
282   * @tc.type: FUNC
283   * @tc.require:
284   * @tc.author: bty
285   */
286 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam007, TestSize.Level1)
287 {
288     /**
289      * @tc.steps: step1. Reload,but the db is null
290      * @tc.expected: step1. Expect -E_INVALID_ARGS
291      */
292     Key key;
293     EXPECT_EQ(g_nullHandle->ReloadResultSet(key), -E_INVALID_ARGS);
294 
295     /**
296      * @tc.steps: step2. Reload,but the key is empty
297      * @tc.expected: step2. Expect -E_INVALID_ARGS
298      */
299     vector<int64_t> cache;
300     EXPECT_EQ(g_handle->ReloadResultSet(key), -E_INVALID_ARGS);
301     EXPECT_EQ(g_nullHandle->ReloadResultSetForCacheRowIdMode(key, cache, 0, 0), -E_INVALID_ARGS);
302 
303     /**
304      * @tc.steps: step3. Reload by object,but the db is null
305      * @tc.expected: step3. Expect -E_INVALID_QUERY_FORMAT
306      */
307     Query query = Query::Select();
308     QueryObject object(query);
309     EXPECT_EQ(g_nullHandle->ReloadResultSet(object), -E_INVALID_QUERY_FORMAT);
310     EXPECT_EQ(g_nullHandle->ReloadResultSetForCacheRowIdMode(object, cache, 0, 0), -E_INVALID_QUERY_FORMAT);
311 
312     /**
313      * @tc.steps: step4. Reload with the valid db
314      * @tc.expected: step4. Expect E_OK
315      */
316     EXPECT_EQ(g_handle->ReloadResultSetForCacheRowIdMode(object, cache, 0, 0), E_OK);
317 }
318 
319 /**
320   * @tc.name: InvalidParam008
321   * @tc.desc: Test transaction with Invalid condition
322   * @tc.type: FUNC
323   * @tc.require:
324   * @tc.author: bty
325   */
326 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam008, TestSize.Level1)
327 {
328     EXPECT_EQ(g_nullHandle->StartTransaction(TransactType::DEFERRED), -E_INVALID_DB);
329     EXPECT_EQ(g_nullHandle->Commit(), -E_INVALID_DB);
330     EXPECT_EQ(g_nullHandle->Rollback(), -E_INVALID_DB);
331 
332     EXPECT_EQ(g_handle->StartTransaction(TransactType::DEFERRED), E_OK);
333     EXPECT_EQ(g_handle->Reset(), E_OK);
334 }
335 
336 /**
337   * @tc.name: InvalidParam009
338   * @tc.desc: Get identifier with Invalid condition
339   * @tc.type: FUNC
340   * @tc.require:
341   * @tc.author: bty
342   */
343 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam009, TestSize.Level1)
344 {
345     /**
346      * @tc.steps: step1. The parameter is null
347      * @tc.expected: step1. Expect -E_INVALID_ARGS
348      */
349     EXPECT_EQ(g_nullHandle->GetDeviceIdentifier(nullptr), -E_INVALID_ARGS);
350 
351     /**
352      * @tc.steps: step2. The db is null
353      * @tc.expected: step2. Expect -E_INVALID_DB
354      */
355     PragmaEntryDeviceIdentifier identifier;
356     EXPECT_EQ(g_nullHandle->GetDeviceIdentifier(&identifier), -E_INVALID_DB);
357 
358     /**
359      * @tc.steps: step3. The identifier is empty
360      * @tc.expected: step3. Expect -E_INVALID_ARGS
361      */
362     EXPECT_EQ(g_handle->GetDeviceIdentifier(&identifier), -E_INVALID_ARGS);
363 }
364 
365 /**
366   * @tc.name: InvalidParam010
367   * @tc.desc: Get meta key with Invalid condition
368   * @tc.type: FUNC
369   * @tc.require:
370   * @tc.author: bty
371   */
372 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam010, TestSize.Level1)
373 {
374     vector<Key> keys;
375     EXPECT_EQ(g_nullHandle->GetAllMetaKeys(keys), -E_INVALID_DB);
376 
377     string devName;
378     vector<Entry> entries;
379     EXPECT_EQ(g_nullHandle->GetAllSyncedEntries(devName, entries), -E_INVALID_DB);
380 }
381 
382 /**
383   * @tc.name: InvalidParam011
384   * @tc.desc:
385   * @tc.type: FUNC
386   * @tc.require:
387   * @tc.author: bty
388   */
389 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidParam011, TestSize.Level1)
390 {
391     /**
392      * @tc.steps: step1. put local kv data
393      * @tc.expected: step1. Expect E_OK
394      */
395     Key key = KEY_1;
396     Value value;
397     Timestamp timestamp = 0;
398     EXPECT_EQ(g_handle->PutKvData(SingleVerDataType::LOCAL_TYPE, key, value, timestamp, nullptr), E_OK);
399 
400     /**
401      * @tc.steps: step2. Get sqlite3 handle,then create executor for state CACHE_ATTACH_MAIN
402      * @tc.expected: step2. Expect not null
403      */
404     sqlite3 *sqlHandle = nullptr;
405     std::string dbPath = g_testDir + g_databaseName;
406     OpenDbProperties property = {dbPath, false, false};
407     EXPECT_EQ(SQLiteUtils::OpenDatabase(property, sqlHandle), E_OK);
408     EXPECT_NE(sqlHandle, nullptr);
409     auto executor = new (std::nothrow) SQLiteSingleVerStorageExecutor(
410         sqlHandle, false, false, ExecutorState::CACHE_ATTACH_MAIN);
411     EXPECT_NE(executor, nullptr);
412 
413     /**
414      * @tc.steps: step3. The singleVerNaturalStoreCommitNotifyData is null,delete
415      * @tc.expected: step3. Expect -1
416      */
417     EXPECT_EQ(executor->DeleteLocalKvData(key, nullptr, value, timestamp), -1); // -1 is covert from sqlite error code
418     sqlite3_close_v2(sqlHandle);
419     delete executor;
420     sqlHandle = nullptr;
421 }
422 
423 /**
424   * @tc.name: InvalidSync001
425   * @tc.desc: Save sync data with Invalid condition
426   * @tc.type: FUNC
427   * @tc.require:
428   * @tc.author: bty
429   */
430 HWTEST_F(DistributedDBStorageSQLiteSingleVerNaturalExecutorTest, InvalidSync001, TestSize.Level1)
431 {
432     /**
433      * @tc.steps: step1. The saveSyncStatements_ is not prepare
434      * @tc.expected: step1. Expect -E_INVALID_ARGS
435      */
436     DataItem item;
437     DeviceInfo info;
438     Timestamp time = 0;
439     EXPECT_EQ(g_handle->SaveSyncDataItem(item, info, time, nullptr, false), -E_INVALID_ARGS);
440 
441     /**
442      * @tc.steps: step2. Try to prepare when the db is null
443      * @tc.expected: step2. Expect -E_INVALID_DB
444      */
445     EXPECT_EQ(g_nullHandle->PrepareForSavingData(SingleVerDataType::LOCAL_TYPE), -E_INVALID_DB);
446 
447     /**
448      * @tc.steps: step3. The data item key is empty
449      * @tc.expected: step3. Expect -E_INVALID_ARGS
450      */
451     EXPECT_EQ(g_handle->PrepareForSavingData(SingleVerDataType::SYNC_TYPE), E_OK);
452     EXPECT_EQ(g_handle->SaveSyncDataItem(item, info, time, nullptr, false), -E_INVALID_ARGS);
453 
454     /**
455      * @tc.steps: step4. The committedData is null
456      * @tc.expected: step4. Expect return E_OK
457      */
458     item.key = KEY_1;
459     EXPECT_EQ(g_handle->SaveSyncDataItem(item, info, time, nullptr, false), E_OK);
460 
461     /**
462      * @tc.steps: step5. Into EraseSyncData
463      * @tc.expected: step5. Expect return E_OK
464      */
465     SingleVerNaturalStoreCommitNotifyData data;
466     item.writeTimestamp = 1;
467     item.flag = DataItem::REMOTE_DEVICE_DATA_MISS_QUERY;
468     EXPECT_EQ(g_handle->SaveSyncDataItem(item, info, time, &data, true), E_OK);
469 }