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
18 #include "distributeddb_tools_unit_test.h"
19 #include "native_sqlite.h"
20 #include "platform_specific.h"
21 #include "sqlite_cloud_kv_executor_utils.h"
22 #include "sqlite_import.h"
23 #include "sqlite_log_table_manager.h"
24 #include "sqlite_local_storage_executor.h"
25 #include "sqlite_utils.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_dbDir;
35 sqlite3 *g_db = nullptr;
36
37 const int MAX_BLOB_READ_SIZE = 64 * 1024 * 1024; // 64M limit
38 const int MAX_TEXT_READ_SIZE = 5 * 1024 * 1024; // 5M limit
39 }
40
41 class DistributedDBSqliteUtilsTest : public testing::Test {
42 public:
43 static void SetUpTestCase(void);
44 static void TearDownTestCase(void);
45 void SetUp();
46 void TearDown();
47 };
48
SetUpTestCase(void)49 void DistributedDBSqliteUtilsTest::SetUpTestCase(void)
50 {
51 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
52 LOGI("The test db is:%s", g_testDir.c_str());
53 g_dbDir = g_testDir + "/";
54 }
55
TearDownTestCase(void)56 void DistributedDBSqliteUtilsTest::TearDownTestCase(void)
57 {
58 }
59
SetUp()60 void DistributedDBSqliteUtilsTest::SetUp()
61 {
62 DistributedDBToolsUnitTest::PrintTestCaseInfo();
63
64 g_db = NativeSqlite::CreateDataBase(g_dbDir + "test.db");
65 const std::string createTableSql = "CREATE TABLE IF NOT EXISTS data (key BLOB PRIMARY KEY, value BLOB);";
66 EXPECT_EQ(sqlite3_exec(g_db, createTableSql.c_str(), nullptr, nullptr, nullptr), SQLITE_OK);
67 }
68
TearDown()69 void DistributedDBSqliteUtilsTest::TearDown()
70 {
71 sqlite3_close_v2(g_db);
72 g_db = nullptr;
73 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
74 LOGE("rm test db files error.");
75 }
76 }
77
78 /**
79 * @tc.name: SQLiteLocalStorageExecutorTest001
80 * @tc.desc: Test SQLiteLocalStorageExecutor construction
81 * @tc.type: FUNC
82 * @tc.require:
83 * @tc.author: tiansimiao
84 */
85 HWTEST_F(DistributedDBSqliteUtilsTest, SQLiteLocalStorageExecutorTest001, TestSize.Level0)
86 {
87 SQLiteLocalStorageExecutor executor(g_db, true, false);
88 EXPECT_EQ(sqlite3_close(g_db), SQLITE_OK);
89 EXPECT_EQ(sqlite3_open("test.db", &g_db), SQLITE_OK);
90 }
91
92 /**
93 * @tc.name: SQLiteLocalStorageExecutorTest002
94 * @tc.desc: Test SQLiteLocalStorageExecutor Put and Get
95 * @tc.type: FUNC
96 * @tc.require:
97 * @tc.author: tiansimiao
98 */
99 HWTEST_F(DistributedDBSqliteUtilsTest, SQLiteLocalStorageExecutorTest002, TestSize.Level0)
100 {
101 SQLiteLocalStorageExecutor executor(g_db, true, false);
102 Key key = { 0x01 };
103 Value value = { 0x02 };
104 EXPECT_EQ(executor.Put(key, value), E_OK);
105 Value outValue;
106 EXPECT_EQ(executor.Get(key, outValue), E_OK);
107 EXPECT_EQ(value, outValue);
108 Key unknownKey = { 0x03 };
109 EXPECT_EQ(executor.Get(unknownKey, outValue), -E_NOT_FOUND);
110 }
111
112 /**
113 * @tc.name: SQLiteLocalStorageExecutorTest003
114 * @tc.desc: Test SQLiteLocalStorageExecutor PutBatch
115 * @tc.type: FUNC
116 * @tc.require:
117 * @tc.author: tiansimiao
118 */
119 HWTEST_F(DistributedDBSqliteUtilsTest, SQLiteLocalStorageExecutorTest003, TestSize.Level0)
120 {
121 SQLiteLocalStorageExecutor executor(g_db, true, false);
122 std::vector<Entry> entries;
123 Key key1 = { 0x01 };
124 Value value1 = { 0x02 };
125 Entry entry1;
126 entry1.key = key1;
127 entry1.value = value1;
128 Key key2 = { 0x03 };
129 Value value2 = { 0x04 };
130 Entry entry2;
131 entry2.key = key2;
132 entry2.value = value2;
133 entries.push_back(entry1);
134 entries.push_back(entry2);
135 EXPECT_EQ(executor.PutBatch(entries), E_OK);
136 Value outValue;
137 EXPECT_EQ(executor.Get(key1, outValue), E_OK);
138 EXPECT_EQ(outValue, value1);
139 EXPECT_EQ(executor.Get(key2, outValue), E_OK);
140 EXPECT_EQ(outValue, value2);
141 std::vector<Entry> emptyEntries;
142 EXPECT_EQ(executor.PutBatch(emptyEntries), -E_INVALID_ARGS);
143 Key invalidKey;
144 Entry invalidEntry;
145 invalidEntry.key = invalidKey;
146 invalidEntry.value = value2;
147 entries.push_back(invalidEntry);
148 EXPECT_EQ(executor.PutBatch(entries), -E_INVALID_ARGS);
149 }
150
151 /**
152 * @tc.name: SQLiteLocalStorageExecutorTest004
153 * @tc.desc: Test SQLiteLocalStorageExecutor Delete and GetEntries
154 * @tc.type: FUNC
155 * @tc.require:
156 * @tc.author: tiansimiao
157 */
158 HWTEST_F(DistributedDBSqliteUtilsTest, SQLiteLocalStorageExecutorTest004, TestSize.Level0)
159 {
160 SQLiteLocalStorageExecutor executor(g_db, true, false);
161 Key key = { 0x01 };
162 Key key1 = { 0x01, 0x02 };
163 Key key2 = { 0x01, 0x03 };
164 Value value;
165 executor.Put(key1, value);
166 executor.Put(key2, value);
167 std::vector<Entry> entries;
168 EXPECT_EQ(executor.Delete(key1), E_OK);
169 EXPECT_EQ(executor.GetEntries(key, entries), E_OK);
170 EXPECT_EQ(entries.size(), 1);
171 }
172
173 /**
174 * @tc.name: SQLiteLocalStorageExecutorTest005
175 * @tc.desc: Test SQLiteLocalStorageExecutor Transaction
176 * @tc.type: FUNC
177 * @tc.require:
178 * @tc.author: tiansimiao
179 */
180 HWTEST_F(DistributedDBSqliteUtilsTest, SQLiteLocalStorageExecutorTest005, TestSize.Level0)
181 {
182 SQLiteLocalStorageExecutor executor(g_db, true, false);
183 Key key = { 0x01 };
184 Value value = { 0x02 };
185 EXPECT_EQ(executor.Put(key, value), E_OK);
186 EXPECT_EQ(executor.Clear(), E_OK);
187 Value outValue;
188 EXPECT_EQ(executor.Get(key, outValue), -E_NOT_FOUND);
189 EXPECT_EQ(executor.StartTransaction(), E_OK);
190 EXPECT_EQ(executor.Put(key, value), E_OK);
191 EXPECT_EQ(executor.RollBack(), E_OK);
192 EXPECT_EQ(executor.Get(key, outValue), -E_NOT_FOUND);
193 }
194
195 /**
196 * @tc.name: GetBlobTest001
197 * @tc.desc: Get blob size over limit
198 * @tc.type: FUNC
199 * @tc.require:
200 * @tc.author: lianhuix
201 */
202 HWTEST_F(DistributedDBSqliteUtilsTest, GetBlobTest001, TestSize.Level1)
203 {
204 NativeSqlite::ExecSql(g_db, "CREATE TABLE IF NOT EXISTS t1 (a INT, b BLOB);");
205
206 vector<uint8_t> blob;
207 blob.resize(MAX_BLOB_READ_SIZE + 2); // 2: over limit
208 std::fill(blob.begin(), blob.end(), static_cast<uint8_t>('a'));
209
__anon5474a5f40202(sqlite3_stmt *stmt) 210 NativeSqlite::ExecSql(g_db, "INSERT INTO t1 VALUES(?, ?)", [&blob](sqlite3_stmt *stmt) {
211 (void)SQLiteUtils::BindInt64ToStatement(stmt, 1, 1);
212 (void)SQLiteUtils::BindBlobToStatement(stmt, 2, blob); // 2: bind index
213 return E_OK;
214 }, nullptr);
215
__anon5474a5f40302(sqlite3_stmt *stmt) 216 NativeSqlite::ExecSql(g_db, "SELECT b FROM t1", nullptr, [](sqlite3_stmt *stmt) {
217 Value val;
218 EXPECT_EQ(SQLiteUtils::GetColumnBlobValue(stmt, 0, val), E_OK);
219 EXPECT_EQ(static_cast<int>(val.size()), MAX_BLOB_READ_SIZE + 1);
220 return E_OK;
221 });
222 }
223
224 /**
225 * @tc.name: GetBlobTest002
226 * @tc.desc: Get blob size equal limit
227 * @tc.type: FUNC
228 * @tc.require:
229 * @tc.author: lianhuix
230 */
231 HWTEST_F(DistributedDBSqliteUtilsTest, GetBlobTest002, TestSize.Level1)
232 {
233 NativeSqlite::ExecSql(g_db, "CREATE TABLE IF NOT EXISTS t1 (a INT, b BLOB);");
234
235 vector<uint8_t> blob;
236 blob.resize(MAX_BLOB_READ_SIZE);
237 std::fill(blob.begin(), blob.end(), static_cast<uint8_t>('a'));
238
__anon5474a5f40402(sqlite3_stmt *stmt) 239 NativeSqlite::ExecSql(g_db, "INSERT INTO t1 VALUES(?, ?)", [&blob](sqlite3_stmt *stmt) {
240 (void)SQLiteUtils::BindInt64ToStatement(stmt, 1, 1);
241 (void)SQLiteUtils::BindBlobToStatement(stmt, 2, blob); // 2: bind index
242 return E_OK;
243 }, nullptr);
244
__anon5474a5f40502(sqlite3_stmt *stmt) 245 NativeSqlite::ExecSql(g_db, "SELECT b FROM t1", nullptr, [&blob](sqlite3_stmt *stmt) {
246 Value val;
247 EXPECT_EQ(SQLiteUtils::GetColumnBlobValue(stmt, 0, val), E_OK);
248 EXPECT_EQ(val, blob);
249 return E_OK;
250 });
251 }
252
253 /**
254 * @tc.name: GetBlobTest003
255 * @tc.desc: Get blob size equal zero
256 * @tc.type: FUNC
257 * @tc.require:
258 * @tc.author: lianhuix
259 */
260 HWTEST_F(DistributedDBSqliteUtilsTest, GetBlobTest003, TestSize.Level1)
261 {
262 NativeSqlite::ExecSql(g_db, "CREATE TABLE IF NOT EXISTS t1 (a INT, b BLOB);");
263
264 vector<uint8_t> blob;
265 blob.resize(0);
266
__anon5474a5f40602(sqlite3_stmt *stmt) 267 NativeSqlite::ExecSql(g_db, "INSERT INTO t1 VALUES(?, ?)", [&blob](sqlite3_stmt *stmt) {
268 (void)SQLiteUtils::BindInt64ToStatement(stmt, 1, 1);
269 (void)SQLiteUtils::BindBlobToStatement(stmt, 2, blob); // 2: bind index
270 return E_OK;
271 }, nullptr);
272
__anon5474a5f40702(sqlite3_stmt *stmt) 273 NativeSqlite::ExecSql(g_db, "SELECT b FROM t1", nullptr, [&blob](sqlite3_stmt *stmt) {
274 Value val;
275 EXPECT_EQ(SQLiteUtils::GetColumnBlobValue(stmt, 0, val), E_OK);
276 EXPECT_EQ(val, blob);
277 return E_OK;
278 });
279 }
280
281 /**
282 * @tc.name: GetTextTest001
283 * @tc.desc: Get text size over limit
284 * @tc.type: FUNC
285 * @tc.require:
286 * @tc.author: lianhuix
287 */
288 HWTEST_F(DistributedDBSqliteUtilsTest, GetTextTest001, TestSize.Level1)
289 {
290 NativeSqlite::ExecSql(g_db, "CREATE TABLE IF NOT EXISTS t1 (a INT, b TEXT);");
291
292 std::string text;
293 text.resize(MAX_TEXT_READ_SIZE + 2); // 2: over limit
294 std::fill(text.begin(), text.end(), 'a');
295
__anon5474a5f40802(sqlite3_stmt *stmt) 296 NativeSqlite::ExecSql(g_db, "INSERT INTO t1 VALUES(?, ?)", [&text](sqlite3_stmt *stmt) {
297 (void)SQLiteUtils::BindInt64ToStatement(stmt, 1, 1);
298 (void)SQLiteUtils::BindTextToStatement(stmt, 2, text); // 2: bind index
299 return E_OK;
300 }, nullptr);
301
__anon5474a5f40902(sqlite3_stmt *stmt) 302 NativeSqlite::ExecSql(g_db, "SELECT b FROM t1", nullptr, [](sqlite3_stmt *stmt) {
303 std::string val;
304 EXPECT_EQ(SQLiteUtils::GetColumnTextValue(stmt, 0, val), E_OK);
305 EXPECT_EQ(static_cast<int>(val.size()), MAX_TEXT_READ_SIZE + 1);
306 return E_OK;
307 });
308 }
309
310 /**
311 * @tc.name: GetTextTest002
312 * @tc.desc: Get text size equal limit
313 * @tc.type: FUNC
314 * @tc.require:
315 * @tc.author: lianhuix
316 */
317 HWTEST_F(DistributedDBSqliteUtilsTest, GetTextTest002, TestSize.Level1)
318 {
319 NativeSqlite::ExecSql(g_db, "CREATE TABLE IF NOT EXISTS t1 (a INT, b TEXT);");
320
321 std::string text;
322 text.resize(MAX_TEXT_READ_SIZE);
323 std::fill(text.begin(), text.end(), 'a');
324
__anon5474a5f40a02(sqlite3_stmt *stmt) 325 NativeSqlite::ExecSql(g_db, "INSERT INTO t1 VALUES(?, ?)", [&text](sqlite3_stmt *stmt) {
326 (void)SQLiteUtils::BindInt64ToStatement(stmt, 1, 1);
327 (void)SQLiteUtils::BindTextToStatement(stmt, 2, text); // 2: bind index
328 return E_OK;
329 }, nullptr);
330
__anon5474a5f40b02(sqlite3_stmt *stmt) 331 NativeSqlite::ExecSql(g_db, "SELECT b FROM t1", nullptr, [&text](sqlite3_stmt *stmt) {
332 std::string val;
333 EXPECT_EQ(SQLiteUtils::GetColumnTextValue(stmt, 0, val), E_OK);
334 EXPECT_EQ(val, text);
335 return E_OK;
336 });
337 }
338
339 /**
340 * @tc.name: GetTextTest003
341 * @tc.desc: Get text size equal zero
342 * @tc.type: FUNC
343 * @tc.require:
344 * @tc.author: lianhuix
345 */
346 HWTEST_F(DistributedDBSqliteUtilsTest, GetTextTest003, TestSize.Level1)
347 {
348 NativeSqlite::ExecSql(g_db, "CREATE TABLE IF NOT EXISTS t1 (a INT, b TEXT);");
349
350 std::string text;
351 text.resize(0);
352
__anon5474a5f40c02(sqlite3_stmt *stmt) 353 NativeSqlite::ExecSql(g_db, "INSERT INTO t1 VALUES(?, ?)", [&text](sqlite3_stmt *stmt) {
354 (void)SQLiteUtils::BindInt64ToStatement(stmt, 1, 1);
355 (void)SQLiteUtils::BindTextToStatement(stmt, 2, text); // 2: bind index
356 return E_OK;
357 }, nullptr);
358
__anon5474a5f40d02(sqlite3_stmt *stmt) 359 NativeSqlite::ExecSql(g_db, "SELECT b FROM t1", nullptr, [&text](sqlite3_stmt *stmt) {
360 std::string val;
361 EXPECT_EQ(SQLiteUtils::GetColumnTextValue(stmt, 0, val), E_OK);
362 EXPECT_EQ(val, text);
363 return E_OK;
364 });
365 }
366
367 /**
368 * @tc.name: KVLogUpgrade001
369 * @tc.desc: Get blob size over limit
370 * @tc.type: FUNC
371 * @tc.require:
372 * @tc.author: zhangqiquan
373 */
374 HWTEST_F(DistributedDBSqliteUtilsTest, KVLogUpgrade001, TestSize.Level0)
375 {
376 const std::string tableName = "naturalbase_kv_aux_sync_data_log";
377 const std::string primaryKey = "PRIMARY KEY(userid, hash_key)";
378 std::string createTableSql = "CREATE TABLE IF NOT EXISTS " + tableName + "(" \
379 "userid TEXT NOT NULL," + \
380 "hash_key BLOB NOT NULL," + \
381 "cloud_gid TEXT," + \
382 "version TEXT," + \
383 primaryKey + ");";
384 int errCode = SQLiteUtils::ExecuteRawSQL(g_db, createTableSql);
385 ASSERT_EQ(errCode, E_OK);
386 EXPECT_EQ(SqliteLogTableManager::CreateKvSyncLogTable(g_db), E_OK);
387 }
388
389 /**
390 * @tc.name: BindErr
391 * @tc.desc: Test bind err
392 * @tc.type: FUNC
393 * @tc.require:
394 * @tc.author: lijun
395 */
396 HWTEST_F(DistributedDBSqliteUtilsTest, BindErr, TestSize.Level1)
397 {
398 NativeSqlite::ExecSql(g_db, "CREATE TABLE IF NOT EXISTS t1 (a INT, b TEXT);");
399
400 std::string text;
401 text.resize(0);
__anon5474a5f40e02(sqlite3_stmt *stmt) 402 NativeSqlite::ExecSql(g_db, "INSERT INTO t1 VALUES(?, ?)", [&text](sqlite3_stmt *stmt) {
403 (void)SQLiteUtils::BindInt64ToStatement(stmt, 1, 1);
404 (void)SQLiteUtils::BindTextToStatement(stmt, 2, "text"); // 2: bind index
405 EXPECT_NE(SQLiteUtils::BindTextToStatement(stmt, 5, text), E_OK);
406 EXPECT_EQ(SQLiteUtils::BindTextToStatement(nullptr, 2, text), -E_INVALID_ARGS);
407 EXPECT_NE(SQLiteUtils::BindInt64ToStatement(nullptr, -6, 0), E_OK);
408 EXPECT_NE(SQLiteUtils::BindBlobToStatement(stmt, 7, {}), E_OK);
409 EXPECT_NE(SQLiteUtils::BindPrefixKey(stmt, 8, {}), E_OK);
410 EXPECT_NE(SQLiteUtils::BindPrefixKey(stmt, 2, {}), E_OK);
411 return E_OK;
412 }, nullptr);
413
__anon5474a5f40f02(sqlite3_stmt *stmt) 414 NativeSqlite::ExecSql(g_db, "SELECT b FROM t1", nullptr, [&text](sqlite3_stmt *stmt) {
415 std::string val;
416 EXPECT_EQ(SQLiteUtils::GetColumnTextValue(nullptr, 0, val), -E_INVALID_ARGS);
417 EXPECT_EQ(SQLiteUtils::GetColumnTextValue(stmt, 0, val), E_OK);
418 EXPECT_EQ(val, text);
419 return E_OK;
420 });
421 }
422
423 /**
424 * @tc.name: AbnormalBranchErr
425 * @tc.desc: Test abnormal branch error
426 * @tc.type: FUNC
427 * @tc.require:
428 * @tc.author: lijun
429 */
430 HWTEST_F(DistributedDBSqliteUtilsTest, AbnormalBranchErr, TestSize.Level1)
431 {
432 CipherPassword passwd;
433 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::SetKey(nullptr, CipherType::DEFAULT, passwd, true,
434 DBConstant::DEFAULT_ITER_TIMES));
435 EXPECT_EQ(-1, SQLiteUtils::AttachNewDatabaseInner(g_db, CipherType::DEFAULT, {}, "path", "asname ? "));
436 EXPECT_EQ(-E_SQLITE_CANT_OPEN, SQLiteUtils::CreateMetaDatabase("/axxbxxc/d sdsda"));
437
438 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::CheckIntegrity(nullptr, ""));
439 TableInfo table;
440 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::AnalysisSchema(nullptr, "", table, true));
441 EXPECT_EQ(-E_NOT_SUPPORT, SQLiteUtils::AnalysisSchema(g_db, "###", table, true));
442 EXPECT_EQ(-E_NOT_FOUND, SQLiteUtils::AnalysisSchema(g_db, "t2", table, true));
443 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::AnalysisSchemaFieldDefine(nullptr, "ttt", table));
444
445 int version;
446 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::GetVersion(nullptr, version));
447 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::SetUserVer(nullptr, version));
448 std::string mode;
449 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::GetJournalMode(nullptr, mode));
450
451 EXPECT_EQ(-SQLITE_IOERR, SQLiteUtils::MapSQLiteErrno(SQLITE_IOERR));
452 int oldErr = errno;
453 errno = EKEYREVOKED;
454 EXPECT_EQ(-E_EKEYREVOKED, SQLiteUtils::MapSQLiteErrno(SQLITE_IOERR));
455 EXPECT_EQ(-E_EKEYREVOKED, SQLiteUtils::MapSQLiteErrno(SQLITE_ERROR));
456 errno = oldErr;
457 EXPECT_EQ(-E_SQLITE_CANT_OPEN, SQLiteUtils::MapSQLiteErrno(SQLITE_CANTOPEN));
458 std::string strSchema;
459 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::SaveSchema(nullptr, strSchema));
460 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::GetSchema(nullptr, strSchema));
461
462 IndexName name;
463 IndexInfo info;
464 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::IncreaseIndex(nullptr, name, info, SchemaType::JSON, 1));
465 EXPECT_EQ(-E_NOT_PERMIT, SQLiteUtils::IncreaseIndex(g_db, name, info, SchemaType::JSON, 1));
466 EXPECT_EQ(-E_NOT_PERMIT, SQLiteUtils::ChangeIndex(g_db, name, info, SchemaType::JSON, 1));
467 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::ChangeIndex(nullptr, name, info, SchemaType::JSON, 1));
468
469 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::RegisterJsonFunctions(nullptr));
470 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::CloneIndexes(nullptr, strSchema, strSchema));
471 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::GetRelationalSchema(nullptr, strSchema));
472 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::GetLogTableVersion(nullptr, strSchema));
473 EXPECT_EQ(-E_INVALID_DB, SQLiteUtils::RegisterFlatBufferFunction(nullptr, strSchema));
474 EXPECT_EQ(-E_INTERNAL_ERROR, SQLiteUtils::RegisterFlatBufferFunction(g_db, strSchema));
475 EXPECT_EQ(-E_INVALID_ARGS, SQLiteUtils::ExpandedSql(nullptr, strSchema));
476 SQLiteUtils::ExecuteCheckPoint(nullptr);
477
478 bool isEmpty;
479 EXPECT_EQ(-E_INVALID_ARGS, SQLiteUtils::CheckTableEmpty(nullptr, strSchema, isEmpty));
480 EXPECT_EQ(-E_INVALID_ARGS, SQLiteUtils::SetPersistWalMode(nullptr));
481 EXPECT_EQ(-1, SQLiteUtils::GetLastRowId(nullptr));
482 std::string tableName;
483 EXPECT_EQ(-1, SQLiteUtils::CheckTableExists(nullptr, tableName, isEmpty));
484 }
485
486 /**
487 * @tc.name: AbnormalBranchErrAfterClose
488 * @tc.desc: Test abnormal branch error after close db
489 * @tc.type: FUNC
490 * @tc.require:
491 * @tc.author: lijun
492 */
493 HWTEST_F(DistributedDBSqliteUtilsTest, AbnormalBranchErrAfterClose, TestSize.Level1)
494 {
495 int version;
496 std::string mode;
497 std::string strSchema;
498 std::string tableName;
499 bool isEmpty;
500
501 sqlite3_close_v2(g_db);
502 // After close db
503 EXPECT_EQ(-SQLITE_MISUSE, SQLiteUtils::GetVersion(g_db, version));
504 EXPECT_EQ(-SQLITE_MISUSE, SQLiteUtils::GetJournalMode(g_db, mode));
505 EXPECT_EQ(-SQLITE_MISUSE, SQLiteUtils::SaveSchema(g_db, strSchema));
506 EXPECT_EQ(-SQLITE_MISUSE, SQLiteUtils::GetSchema(g_db, strSchema));
507 EXPECT_EQ(-SQLITE_MISUSE, SQLiteUtils::GetRelationalSchema(g_db, strSchema));
508 EXPECT_EQ(-SQLITE_MISUSE, SQLiteUtils::GetLogTableVersion(g_db, strSchema));
509 EXPECT_EQ(-SQLITE_MISUSE, SQLiteUtils::CheckTableExists(g_db, tableName, isEmpty));
510 }
511
512 /**
513 * @tc.name: AbnormalSqliteCloudKvExecutorUtilsTest001
514 * @tc.desc: Test sqlite cloud kv executor utils abnormal
515 * @tc.type: FUNC
516 * @tc.require:
517 * @tc.author: caihaoting
518 */
519 HWTEST_F(DistributedDBSqliteUtilsTest, AbnormalSqliteCloudKvExecutorUtilsTest001, TestSize.Level1)
520 {
521 SqliteCloudKvExecutorUtils cloudKvObj;
522 sqlite3 *db = nullptr;
523 CloudSyncData data;
524 CloudUploadRecorder recorder;
525
526 uint64_t flag = SQLITE_OPEN_URI | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
527 std::string fileUrl = g_testDir + "/test.db";
528 EXPECT_EQ(sqlite3_open_v2(fileUrl.c_str(), &db, flag, nullptr), SQLITE_OK);
529 ASSERT_NE(db, nullptr);
530
531 data.isCloudVersionRecord = false;
532 int ret = cloudKvObj.FillCloudLog({db, true}, OpType::INSERT, data, "", recorder);
533 EXPECT_EQ(ret, -1); // -1 for table not exist
534
535 std::vector<VBucket> dataVector;
536 ret = cloudKvObj.GetWaitCompensatedSyncDataPk(db, true, dataVector);
537 EXPECT_NE(ret, E_OK);
538 std::vector<VBucket> dataUser;
539 ret = cloudKvObj.GetWaitCompensatedSyncDataUserId(db, true, dataUser);
540 EXPECT_NE(ret, E_OK);
541 ret = cloudKvObj.GetWaitCompensatedSyncData(db, true, dataVector, dataUser);
542 EXPECT_NE(ret, E_OK);
543 std::vector<std::string> gid;
544 std::string user("USER");
545 QuerySyncObject query;
546 ret = cloudKvObj.QueryCloudGid(db, true, user, query, gid);
547 EXPECT_NE(ret, E_OK);
548 EXPECT_EQ(sqlite3_close_v2(db), E_OK);
549 }
550
551 /**
552 * @tc.name: CheckIntegrityTest001
553 * @tc.desc: Test CheckIntegrity function
554 * @tc.type: FUNC
555 * @tc.require:
556 * @tc.author: tiansimiao
557 */
558 HWTEST_F(DistributedDBSqliteUtilsTest, CheckIntegrityTest001, TestSize.Level0)
559 {
560 CipherPassword passwd;
561 CipherType invalidType = static_cast<CipherType>(999);
562 EXPECT_EQ(SQLiteUtils::CheckIntegrity(g_dbDir, invalidType, passwd), -E_INVALID_ARGS);
563 }
564
565 /**
566 * @tc.name: GetVersionTest001
567 * @tc.desc: Test GetVersion function
568 * @tc.type: FUNC
569 * @tc.require:
570 * @tc.author: tiansimiao
571 */
572 HWTEST_F(DistributedDBSqliteUtilsTest, GetVersionTest001, TestSize.Level0)
573 {
574 DistributedDB::OpenDbProperties properties;
575 properties.uri = "";
576 int version = 0;
577 EXPECT_EQ(SQLiteUtils::GetVersion(properties, version), -E_INVALID_ARGS);
578 }
579
580 /**
581 * @tc.name: SetUserVerTest001
582 * @tc.desc: Test SetUserVer function
583 * @tc.type: FUNC
584 * @tc.require:
585 * @tc.author: tiansimiao
586 */
587 HWTEST_F(DistributedDBSqliteUtilsTest, SetUserVerTest001, TestSize.Level0)
588 {
589 DistributedDB::OpenDbProperties properties;
590 properties.uri = "";
591 int version = 0;
592 EXPECT_EQ(SQLiteUtils::SetUserVer(properties, version), -E_INVALID_ARGS);
593 }