1 /*
2 * Copyright (c) 2023 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 <string>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include "common.h"
22 #include "relational_store.h"
23 #include "relational_store_error_code.h"
24
25 using namespace testing::ext;
26 using namespace OHOS::NativeRdb;
27
28 class RdbNativeStoreTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
InitRdbConfig()34 static void InitRdbConfig()
35 {
36 config_.dataBaseDir = RDB_TEST_PATH;
37 config_.storeName = "rdb_store_test.db";
38 config_.bundleName = "";
39 config_.moduleName = "";
40 config_.securityLevel = OH_Rdb_SecurityLevel::S1;
41 config_.isEncrypt = false;
42 config_.selfSize = sizeof(OH_Rdb_Config);
43 }
44 static OH_Rdb_Config config_;
45 };
46
47 OH_Rdb_Store *storeTestRdbStore_;
48 OH_Rdb_Config RdbNativeStoreTest::config_ = {0};
SetUpTestCase(void)49 void RdbNativeStoreTest::SetUpTestCase(void)
50 {
51 InitRdbConfig();
52 mkdir(config_.dataBaseDir, 0770);
53 int errCode = 0;
54 storeTestRdbStore_ = OH_Rdb_GetOrOpen(&config_, &errCode);
55 EXPECT_NE(storeTestRdbStore_, NULL);
56 }
57
TearDownTestCase(void)58 void RdbNativeStoreTest::TearDownTestCase(void)
59 {
60 int errCode = OH_Rdb_CloseStore(storeTestRdbStore_);
61 EXPECT_EQ(errCode, 0);
62 errCode = OH_Rdb_DeleteStore(&config_);
63 EXPECT_EQ(errCode, 0);
64 }
65
SetUp(void)66 void RdbNativeStoreTest::SetUp(void)
67 {
68 char createTableSql[] = "CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, data1 TEXT, data2 INTEGER, "
69 "data3 FLOAT, data4 BLOB, data5 TEXT);";
70 int errCode = OH_Rdb_Execute(storeTestRdbStore_, createTableSql);
71 EXPECT_EQ(errCode, 0);
72 }
73
TearDown(void)74 void RdbNativeStoreTest::TearDown(void)
75 {
76 char dropTableSql[] = "DROP TABLE IF EXISTS test";
77 int errCode = OH_Rdb_Execute(storeTestRdbStore_, dropTableSql);
78 EXPECT_EQ(errCode, 0);
79 }
80
81 /**
82 * @tc.name: RDB_Native_store_test_001
83 * @tc.desc: Normal testCase of store for Insert、Update、Query.
84 * @tc.type: FUNC
85 */
86 HWTEST_F(RdbNativeStoreTest, RDB_Native_store_test_001, TestSize.Level1)
87 {
88 int errCode = 0;
89 OH_VBucket* valueBucket = OH_Rdb_CreateValuesBucket();
90 valueBucket->putInt64(valueBucket, "id", 1);
91 valueBucket->putText(valueBucket, "data1", "zhangSan");
92 valueBucket->putInt64(valueBucket, "data2", 12800);
93 valueBucket->putReal(valueBucket, "data3", 100.1);
94 uint8_t arr[] = {1, 2, 3, 4, 5};
95 int len = sizeof(arr) / sizeof(arr[0]);
96 valueBucket->putBlob(valueBucket, "data4", arr, len);
97 valueBucket->putText(valueBucket, "data5", "ABCDEFG");
98 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
99 EXPECT_EQ(errCode, 1);
100
101 valueBucket->clear(valueBucket);
102 valueBucket->putText(valueBucket, "data1", "liSi");
103 valueBucket->putInt64(valueBucket, "data2", 13800);
104 valueBucket->putReal(valueBucket, "data3", 200.1);
105 valueBucket->putNull(valueBucket, "data5");
106
107 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
108 OH_VObject *valueObject = OH_Rdb_CreateValueObject();
109 const char *data1Value = "zhangSan";
110 valueObject->putText(valueObject, data1Value);
111 predicates->equalTo(predicates, "data1", valueObject);
112 errCode = OH_Rdb_Update(storeTestRdbStore_, valueBucket, predicates);
113 EXPECT_EQ(errCode, 1);
114
115 predicates->clear(predicates);
116 OH_Cursor *cursor = OH_Rdb_Query(storeTestRdbStore_, predicates, NULL, 0);
117 EXPECT_NE(cursor, NULL);
118
119 int rowCount = 0;
120 cursor->getRowCount(cursor, &rowCount);
121 EXPECT_EQ(rowCount, 1);
122
123 errCode = cursor->goToNextRow(cursor);
124 EXPECT_EQ(errCode, 0);
125
126 size_t size = 0;
127 cursor->getSize(cursor, 1, &size);
128 char data1Value_1[size + 1];
129 cursor->getText(cursor, 1, data1Value_1, size + 1);
130 EXPECT_EQ(strcmp(data1Value_1, "liSi"), 0);
131
132 int64_t data2Value;
133 cursor->getInt64(cursor, 2, &data2Value);
134 EXPECT_EQ(data2Value, 13800);
135
136 double data3Value;
137 cursor->getReal(cursor, 3, &data3Value);
138 EXPECT_EQ(data3Value, 200.1);
139
140 cursor->getSize(cursor, 4, &size);
141 unsigned char data4Value[size];
142 cursor->getBlob(cursor, 4, data4Value, size);
143 EXPECT_EQ(data4Value[0], 1);
144 EXPECT_EQ(data4Value[1], 2);
145
146 bool isNull = false;
147 cursor->isNull(cursor, 5, &isNull);
148 EXPECT_EQ(isNull, true);
149
150 valueObject->destroy(valueObject);
151 valueBucket->destroy(valueBucket);
152 predicates->destroy(predicates);
153 cursor->destroy(cursor);
154 }
155
156 /**
157 * @tc.name: RDB_Native_store_test_002
158 * @tc.desc: Normal testCase of store for Delete、ExecuteQuery.
159 * @tc.type: FUNC
160 */
161 HWTEST_F(RdbNativeStoreTest, RDB_Native_store_test_002, TestSize.Level1)
162 {
163 int errCode = 0;
164 OH_VBucket* valueBucket = OH_Rdb_CreateValuesBucket();
165 valueBucket->putInt64(valueBucket, "id", 1);
166 valueBucket->putText(valueBucket, "data1", "zhangSan");
167 valueBucket->putInt64(valueBucket, "data2", 12800);
168 valueBucket->putReal(valueBucket, "data3", 100.1);
169 uint8_t arr[] = {1, 2, 3, 4, 5};
170 int len = sizeof(arr) / sizeof(arr[0]);
171 valueBucket->putBlob(valueBucket, "data4", arr, len);
172 valueBucket->putText(valueBucket, "data5", "ABCDEFG");
173 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
174 EXPECT_EQ(errCode, 1);
175
176 valueBucket->clear(valueBucket);
177 valueBucket->putInt64(valueBucket, "id", 2);
178 valueBucket->putText(valueBucket, "data1", "liSi");
179 valueBucket->putInt64(valueBucket, "data2", 13800);
180 valueBucket->putReal(valueBucket, "data3", 200.1);
181 valueBucket->putText(valueBucket, "data5", "ABCDEFGH");
182 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
183 EXPECT_EQ(errCode, 2);
184
185 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
186 OH_VObject *valueObject = OH_Rdb_CreateValueObject();
187 const char *data1Value = "zhangSan";
188 valueObject->putText(valueObject, data1Value);
189 predicates->equalTo(predicates, "data1", valueObject);
190 errCode = OH_Rdb_Delete(storeTestRdbStore_, predicates);
191 EXPECT_EQ(errCode, 1);
192
193 char querySql[] = "SELECT * FROM test";
194 OH_Cursor *cursor = OH_Rdb_ExecuteQuery(storeTestRdbStore_, querySql);
195
196 int rowCount = 0;
197 cursor->getRowCount(cursor, &rowCount);
198 EXPECT_EQ(rowCount, 1);
199
200 errCode = cursor->goToNextRow(cursor);
201 EXPECT_EQ(errCode, 0);
202
203 size_t size = 0;
204 cursor->getSize(cursor, 1, &size);
205 char data1Value_1[size + 1];
206 cursor->getText(cursor, 1, data1Value_1, size + 1);
207 EXPECT_EQ(strcmp(data1Value_1, "liSi"), 0);
208
209 int64_t data2Value;
210 cursor->getInt64(cursor, 2, &data2Value);
211 EXPECT_EQ(data2Value, 13800);
212
213 double data3Value;
214 cursor->getReal(cursor, 3, &data3Value);
215 EXPECT_EQ(data3Value, 200.1);
216
217 bool isNull = false;
218 cursor->isNull(cursor, 4, &isNull);
219 EXPECT_EQ(isNull, true);
220
221 cursor->getSize(cursor, 5, &size);
222 char data5Value[size + 1];
223 cursor->getText(cursor, 5, data5Value, size + 1);
224 EXPECT_EQ(strcmp(data5Value, "ABCDEFGH"), 0);
225
226 valueObject->destroy(valueObject);
227 valueBucket->destroy(valueBucket);
228 predicates->destroy(predicates);
229 cursor->destroy(cursor);
230 }
231
232 /**
233 * @tc.name: RDB_Native_store_test_003
234 * @tc.desc: Normal testCase of store for Transaction、Commit.
235 * @tc.type: FUNC
236 */
237 HWTEST_F(RdbNativeStoreTest, RDB_Native_store_test_003, TestSize.Level1)
238 {
239 OH_Rdb_BeginTransaction(storeTestRdbStore_);
240
241 int errCode = 0;
242 OH_VBucket* valueBucket = OH_Rdb_CreateValuesBucket();
243 valueBucket->putInt64(valueBucket, "id", 1);
244 valueBucket->putText(valueBucket, "data1", "zhangSan");
245 valueBucket->putInt64(valueBucket, "data2", 12800);
246 valueBucket->putReal(valueBucket, "data3", 100.1);
247 uint8_t arr[] = {1, 2, 3, 4, 5};
248 int len = sizeof(arr) / sizeof(arr[0]);
249 valueBucket->putBlob(valueBucket, "data4", arr, len);
250 valueBucket->putText(valueBucket, "data5", "ABCDEFG");
251 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
252 EXPECT_EQ(errCode, 1);
253
254 valueBucket->clear(valueBucket);
255 valueBucket->putInt64(valueBucket, "id", 2);
256 valueBucket->putText(valueBucket, "data1", "liSi");
257 valueBucket->putInt64(valueBucket, "data2", 13800);
258 valueBucket->putReal(valueBucket, "data3", 200.1);
259 valueBucket->putText(valueBucket, "data5", "ABCDEFGH");
260 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
261 EXPECT_EQ(errCode, 2);
262
263 OH_Rdb_Commit(storeTestRdbStore_);
264
265 char querySql[] = "SELECT * FROM test";
266 OH_Cursor *cursor = OH_Rdb_ExecuteQuery(storeTestRdbStore_, querySql);
267
268 int rowCount = 0;
269 cursor->getRowCount(cursor, &rowCount);
270 EXPECT_EQ(rowCount, 2);
271
272 valueBucket->destroy(valueBucket);
273 cursor->destroy(cursor);
274 }
275
276 /**
277 * @tc.name: RDB_Native_store_test_004
278 * @tc.desc: Normal testCase of store for Transaction、RollBack.
279 * @tc.type: FUNC
280 */
281 HWTEST_F(RdbNativeStoreTest, RDB_Native_store_test_004, TestSize.Level1)
282 {
283 OH_Rdb_BeginTransaction(storeTestRdbStore_);
284
285 int errCode = 0;
286 OH_VBucket* valueBucket = OH_Rdb_CreateValuesBucket();
287 valueBucket->putInt64(valueBucket, "id", 1);
288 valueBucket->putText(valueBucket, "data1", "zhangSan");
289 valueBucket->putInt64(valueBucket, "data2", 12800);
290 valueBucket->putReal(valueBucket, "data3", 100.1);
291 uint8_t arr[] = {1, 2, 3, 4, 5};
292 int len = sizeof(arr) / sizeof(arr[0]);
293 valueBucket->putBlob(valueBucket, "data4", arr, len);
294 valueBucket->putText(valueBucket, "data5", "ABCDEFG");
295 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
296 EXPECT_EQ(errCode, 1);
297
298 valueBucket->clear(valueBucket);
299 valueBucket->putInt64(valueBucket, "id", 2);
300 valueBucket->putText(valueBucket, "data1", "liSi");
301 valueBucket->putInt64(valueBucket, "data2", 13800);
302 valueBucket->putReal(valueBucket, "data3", 200.1);
303 valueBucket->putText(valueBucket, "data5", "ABCDEFGH");
304 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
305 EXPECT_EQ(errCode, 2);
306
307 OH_Rdb_RollBack(storeTestRdbStore_);
308
309 char querySql[] = "SELECT * FROM test";
310 OH_Cursor *cursor = OH_Rdb_ExecuteQuery(storeTestRdbStore_, querySql);
311
312 int rowCount = 0;
313 cursor->getRowCount(cursor, &rowCount);
314 EXPECT_EQ(rowCount, 0);
315
316 valueBucket->destroy(valueBucket);
317 cursor->destroy(cursor);
318 }
319
320 /**
321 * @tc.name: RDB_Native_store_test_005
322 * @tc.desc: Normal testCase of store for Backup、Restore.
323 * @tc.type: FUNC
324 */
325 HWTEST_F(RdbNativeStoreTest, RDB_Native_store_test_005, TestSize.Level1)
326 {
327 int errCode = 0;
328 OH_VBucket* valueBucket = OH_Rdb_CreateValuesBucket();
329 valueBucket->putText(valueBucket, "data1", "zhangSan");
330 valueBucket->putInt64(valueBucket, "data2", 12800);
331 valueBucket->putReal(valueBucket, "data3", 100.1);
332 uint8_t arr[] = {1, 2, 3, 4, 5};
333 int len = sizeof(arr) / sizeof(arr[0]);
334 valueBucket->putBlob(valueBucket, "data4", arr, len);
335 valueBucket->putText(valueBucket, "data5", "ABCDEFG");
336 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
337 EXPECT_EQ(errCode, 1);
338
339 char querySql[] = "SELECT * FROM test";
340 OH_Cursor *cursor = OH_Rdb_ExecuteQuery(storeTestRdbStore_, querySql);
341
342 int rowCount = 0;
343 cursor->getRowCount(cursor, &rowCount);
344 EXPECT_EQ(rowCount, 1);
345 cursor->destroy(cursor);
346
347 std::string backupPath1 = RDB_TEST_PATH + std::string("a.db");
348 errCode = OH_Rdb_Backup(storeTestRdbStore_, backupPath1.c_str());
349 EXPECT_EQ(errCode, 0);
350
351 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
352 EXPECT_EQ(errCode, 2);
353 std::string backupPath2 = RDB_TEST_PATH + std::string("b.db");
354 errCode = OH_Rdb_Backup(storeTestRdbStore_, backupPath2.c_str());
355 EXPECT_EQ(errCode, 0);
356
357 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
358 EXPECT_EQ(errCode, 3);
359 std::string backupPath3 = RDB_TEST_PATH + std::string("c.db");
360 errCode = OH_Rdb_Backup(storeTestRdbStore_, backupPath3.c_str());
361 EXPECT_EQ(errCode, 0);
362
363 // Continuous backup
364 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
365 EXPECT_EQ(errCode, 4);
366 errCode = OH_Rdb_Backup(storeTestRdbStore_, backupPath3.c_str());
367 EXPECT_EQ(errCode, 0);
368
369 errCode = OH_Rdb_Restore(storeTestRdbStore_, backupPath1.c_str());
370 EXPECT_EQ(errCode, 0);
371 cursor = OH_Rdb_ExecuteQuery(storeTestRdbStore_, querySql);
372 cursor->getRowCount(cursor, &rowCount);
373 EXPECT_EQ(rowCount, 1);
374 cursor->destroy(cursor);
375
376 errCode = OH_Rdb_Restore(storeTestRdbStore_, backupPath2.c_str());
377 EXPECT_EQ(errCode, 0);
378 cursor = OH_Rdb_ExecuteQuery(storeTestRdbStore_, querySql);
379 cursor->getRowCount(cursor, &rowCount);
380 EXPECT_EQ(rowCount, 2);
381 cursor->destroy(cursor);
382
383 errCode = OH_Rdb_Restore(storeTestRdbStore_, backupPath3.c_str());
384 EXPECT_EQ(errCode, 0);
385 cursor = OH_Rdb_ExecuteQuery(storeTestRdbStore_, querySql);
386 cursor->getRowCount(cursor, &rowCount);
387 EXPECT_EQ(rowCount, 4);
388 cursor->destroy(cursor);
389
390 // Continuous restore
391 errCode = OH_Rdb_Restore(storeTestRdbStore_, backupPath3.c_str());
392 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_FILE_PATH);
393 cursor = OH_Rdb_ExecuteQuery(storeTestRdbStore_, querySql);
394 cursor->getRowCount(cursor, &rowCount);
395 EXPECT_EQ(rowCount, 4);
396
397 valueBucket->destroy(valueBucket);
398 cursor->destroy(cursor);
399 }
400
401 /**
402 * @tc.name: RDB_Native_store_test_006
403 * @tc.desc: Normal testCase of store for Backup、Restore.
404 * @tc.type: FUNC
405 */
406 HWTEST_F(RdbNativeStoreTest, RDB_Native_store_test_006, TestSize.Level1)
407 {
408 int errCode = 0;
409 OH_VBucket* valueBucket = OH_Rdb_CreateValuesBucket();
410 valueBucket->putText(valueBucket, "data1", "zhangSan");
411 valueBucket->putInt64(valueBucket, "data2", 12800);
412 valueBucket->putReal(valueBucket, "data3", 100.1);
413 uint8_t arr[] = {1, 2, 3, 4, 5};
414 int len = sizeof(arr) / sizeof(arr[0]);
415 valueBucket->putBlob(valueBucket, "data4", arr, len);
416 valueBucket->putText(valueBucket, "data5", "ABCDEFG");
417 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
418 EXPECT_EQ(errCode, 1);
419
420 char querySql[] = "SELECT * FROM test";
421 OH_Cursor *cursor = OH_Rdb_ExecuteQuery(storeTestRdbStore_, querySql);
422
423 int rowCount = 0;
424 cursor->getRowCount(cursor, &rowCount);
425 EXPECT_EQ(rowCount, 1);
426 cursor->destroy(cursor);
427
428 std::string backupPath = "backup.db";
429 errCode = OH_Rdb_Backup(storeTestRdbStore_, backupPath.c_str());
430 EXPECT_EQ(errCode, 0);
431 errCode = OH_Rdb_Restore(storeTestRdbStore_, backupPath.c_str());
432 EXPECT_EQ(errCode, 0);
433 cursor = OH_Rdb_ExecuteQuery(storeTestRdbStore_, querySql);
434 cursor->getRowCount(cursor, &rowCount);
435 EXPECT_EQ(rowCount, 1);
436 cursor->destroy(cursor);
437
438 std::string restorePath = "error.db";
439 errCode = OH_Rdb_Restore(storeTestRdbStore_, restorePath.c_str());
440 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_FILE_PATH);
441
442 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
443 EXPECT_EQ(errCode, 2);
444 backupPath = " ";
445 errCode = OH_Rdb_Backup(storeTestRdbStore_, backupPath.c_str());
446 EXPECT_EQ(errCode, 0);
447 errCode = OH_Rdb_Restore(storeTestRdbStore_, backupPath.c_str());
448 EXPECT_EQ(errCode, 0);
449 cursor = OH_Rdb_ExecuteQuery(storeTestRdbStore_, querySql);
450 cursor->getRowCount(cursor, &rowCount);
451 EXPECT_EQ(rowCount, 2);
452 cursor->destroy(cursor);
453
454 backupPath = "";
455 errCode = OH_Rdb_Backup(storeTestRdbStore_, backupPath.c_str());
456 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_FILE_PATH);
457
458 backupPath = RDB_TEST_PATH + std::string("/backup/backup.db");
459 errCode = OH_Rdb_Backup(storeTestRdbStore_, backupPath.c_str());
460 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_FILE_PATH);
461
462 backupPath = RDB_TEST_PATH;
463 errCode = OH_Rdb_Backup(storeTestRdbStore_, backupPath.c_str());
464 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_FILE_PATH);
465
466 restorePath = RDB_TEST_PATH;
467 errCode = OH_Rdb_Restore(storeTestRdbStore_, restorePath.c_str());
468 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_FILE_PATH);
469
470 valueBucket->destroy(valueBucket);
471 }
472
473 /**
474 * @tc.name: RDB_Native_store_test_007
475 * @tc.desc: Normal testCase of store for GetVersion、SetVersion.
476 * @tc.type: FUNC
477 */
478 HWTEST_F(RdbNativeStoreTest, RDB_Native_store_test_007, TestSize.Level1)
479 {
480 int errCode = 0;
481 int version = 0;
482 int setVersion = 3;
483 errCode = OH_Rdb_GetVersion(storeTestRdbStore_, &version);
484 EXPECT_EQ(errCode, 0);
485 EXPECT_EQ(version, 0);
486
487 errCode = OH_Rdb_SetVersion(storeTestRdbStore_, setVersion);
488 errCode = OH_Rdb_GetVersion(storeTestRdbStore_, &version);
489 EXPECT_EQ(errCode, 0);
490 EXPECT_EQ(version, 3);
491 }
492
493 /**
494 * @tc.name: RDB_Native_store_test_008
495 * @tc.desc: Normal testCase of store for Insert with wrong table name or table is NULL.
496 * @tc.type: FUNC
497 */
498 HWTEST_F(RdbNativeStoreTest, RDB_Native_store_test_008, TestSize.Level1)
499 {
500 int errCode = 0;
501 OH_VBucket* valueBucket = OH_Rdb_CreateValuesBucket();
502 valueBucket->putInt64(valueBucket, "id", 1);
503 valueBucket->putText(valueBucket, "data1", "zhangSan");
504 valueBucket->putInt64(valueBucket, "data2", 12800);
505 valueBucket->putReal(valueBucket, "data3", 100.1);
506 uint8_t arr[] = {1, 2, 3, 4, 5};
507 int len = sizeof(arr) / sizeof(arr[0]);
508 valueBucket->putBlob(valueBucket, "data4", arr, len);
509 valueBucket->putText(valueBucket, "data5", "ABCDEFG");
510 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
511 EXPECT_EQ(errCode, 1);
512
513 valueBucket->clear(valueBucket);
514 valueBucket->putInt64(valueBucket, "id", 2);
515 valueBucket->putText(valueBucket, "data1", "liSi");
516 valueBucket->putInt64(valueBucket, "data2", 13800);
517 valueBucket->putReal(valueBucket, "data3", 200.1);
518 valueBucket->putText(valueBucket, "data5", "ABCDEFGH");
519 errCode = OH_Rdb_Insert(storeTestRdbStore_, "wrong", valueBucket);
520 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_ERR);
521
522 valueBucket->clear(valueBucket);
523 valueBucket->putInt64(valueBucket, "id", 3);
524 valueBucket->putText(valueBucket, "data1", "wangWu");
525 valueBucket->putInt64(valueBucket, "data2", 14800);
526 valueBucket->putReal(valueBucket, "data3", 300.1);
527 valueBucket->putText(valueBucket, "data5", "ABCDEFGHI");
528 char *table = NULL;
529 errCode = OH_Rdb_Insert(storeTestRdbStore_, table, valueBucket);
530 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
531
532 char querySql[] = "SELECT * FROM test";
533 OH_Cursor *cursor = OH_Rdb_ExecuteQuery(storeTestRdbStore_, querySql);
534
535 int rowCount = 0;
536 cursor->getRowCount(cursor, &rowCount);
537 EXPECT_EQ(rowCount, 1);
538
539 valueBucket->destroy(valueBucket);
540 cursor->destroy(cursor);
541 }
542
543 /**
544 * @tc.name: RDB_Native_store_test_009
545 * @tc.desc: Normal testCase of store for Update with wrong table or table is NULL.
546 * @tc.type: FUNC
547 */
548 HWTEST_F(RdbNativeStoreTest, RDB_Native_store_test_009, TestSize.Level1)
549 {
550 int errCode = 0;
551 OH_VBucket* valueBucket = OH_Rdb_CreateValuesBucket();
552 valueBucket->putInt64(valueBucket, "id", 1);
553 valueBucket->putText(valueBucket, "data1", "zhangSan");
554 valueBucket->putInt64(valueBucket, "data2", 12800);
555 valueBucket->putReal(valueBucket, "data3", 100.1);
556 uint8_t arr[] = {1, 2, 3, 4, 5};
557 int len = sizeof(arr) / sizeof(arr[0]);
558 valueBucket->putBlob(valueBucket, "data4", arr, len);
559 valueBucket->putText(valueBucket, "data5", "ABCDEFG");
560 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
561 EXPECT_EQ(errCode, 1);
562
563 valueBucket->clear(valueBucket);
564 valueBucket->putText(valueBucket, "data1", "liSi");
565 valueBucket->putInt64(valueBucket, "data2", 13800);
566 valueBucket->putReal(valueBucket, "data3", 200.1);
567 valueBucket->putNull(valueBucket, "data5");
568
569 OH_Predicates *predicates = OH_Rdb_CreatePredicates("wrong");
570 OH_VObject *valueObject = OH_Rdb_CreateValueObject();
571 const char *data1Value = "zhangSan";
572 valueObject->putText(valueObject, data1Value);
573 predicates->equalTo(predicates, "data1", valueObject);
574 errCode = OH_Rdb_Update(storeTestRdbStore_, valueBucket, predicates);
575 EXPECT_EQ(errCode, -1);
576
577 char *table = NULL;
578 OH_Predicates *predicates1 = OH_Rdb_CreatePredicates(table);
579 EXPECT_EQ(predicates1, NULL);
580 errCode = OH_Rdb_Update(storeTestRdbStore_, valueBucket, predicates1);
581 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
582
583 OH_Predicates *predicates2 = OH_Rdb_CreatePredicates("test");
584 OH_Cursor *cursor = OH_Rdb_Query(storeTestRdbStore_, predicates2, NULL, 0);
585 EXPECT_NE(cursor, NULL);
586
587 int rowCount = 0;
588 cursor->getRowCount(cursor, &rowCount);
589 EXPECT_EQ(rowCount, 1);
590
591 errCode = cursor->goToNextRow(cursor);
592 EXPECT_EQ(errCode, 0);
593
594 size_t size = 0;
595 cursor->getSize(cursor, 1, &size);
596 char data1Value_1[size + 1];
597 cursor->getText(cursor, 1, data1Value_1, size + 1);
598 EXPECT_EQ(strcmp(data1Value_1, "zhangSan"), 0);
599
600 int64_t data2Value;
601 cursor->getInt64(cursor, 2, &data2Value);
602 EXPECT_EQ(data2Value, 12800);
603
604 double data3Value;
605 cursor->getReal(cursor, 3, &data3Value);
606 EXPECT_EQ(data3Value, 100.1);
607
608 cursor->getSize(cursor, 4, &size);
609 unsigned char data4Value[size];
610 cursor->getBlob(cursor, 4, data4Value, size);
611 EXPECT_EQ(data4Value[0], 1);
612 EXPECT_EQ(data4Value[1], 2);
613
614 cursor->getSize(cursor, 5, &size);
615 char data5Value[size + 1];
616 cursor->getText(cursor, 5, data5Value, size + 1);
617 EXPECT_EQ(strcmp(data5Value, "ABCDEFG"), 0);
618
619 valueObject->destroy(valueObject);
620 predicates->destroy(predicates);
621 predicates2->destroy(predicates2);
622 valueBucket->destroy(valueBucket);
623 cursor->destroy(cursor);
624 }
625
626 /**
627 * @tc.name: RDB_Native_store_test_010
628 * @tc.desc: Normal testCase of store for querysql is NULL.
629 * @tc.type: FUNC
630 */
631 HWTEST_F(RdbNativeStoreTest, RDB_Native_store_test_010, TestSize.Level1)
632 {
633 int errCode = 0;
634 OH_VBucket *valueBucket = OH_Rdb_CreateValuesBucket();
635 valueBucket->putInt64(valueBucket, "id", 1);
636 valueBucket->putText(valueBucket, "data1", "zhangSan");
637 valueBucket->putInt64(valueBucket, "data2", 12800);
638 valueBucket->putReal(valueBucket, "data3", 100.1);
639 uint8_t arr[] = { 1, 2, 3, 4, 5 };
640 int len = sizeof(arr) / sizeof(arr[0]);
641 valueBucket->putBlob(valueBucket, "data4", arr, len);
642 valueBucket->putText(valueBucket, "data5", "ABCDEFG");
643 errCode = OH_Rdb_Insert(storeTestRdbStore_, "test", valueBucket);
644 EXPECT_EQ(errCode, 1);
645
646 char *querySql = NULL;
647 OH_Cursor *cursor = OH_Rdb_ExecuteQuery(storeTestRdbStore_, querySql);
648 EXPECT_EQ(cursor, NULL);
649
650 valueBucket->destroy(valueBucket);
651 }
652
653 /**
654 * @tc.name: RDB_Native_store_test_011
655 * @tc.desc: Normal testCase of RelationalValuesBucket for anomalous branch.
656 * @tc.type: FUNC
657 */
658 HWTEST_F(RdbNativeStoreTest, RDB_Native_store_test_011, TestSize.Level1)
659 {
660 OH_VBucket *valueBucket = OH_Rdb_CreateValuesBucket();
661 uint8_t arr[] = { 1, 2, 3, 4, 5 };
662 uint32_t len = sizeof(arr) / sizeof(arr[0]);
663 int errCode = valueBucket->putBlob(nullptr, "data4", arr, len);
664 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
665 errCode = valueBucket->putBlob(valueBucket, nullptr, arr, len);
666 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
667 errCode = valueBucket->putBlob(valueBucket, "data4", nullptr, len);
668 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_OK);
669
670 errCode = valueBucket->clear(nullptr);
671 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
672 errCode = valueBucket->destroy(nullptr);
673 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
674 valueBucket->destroy(valueBucket);
675 }
676
677 /**
678 * @tc.name: RDB_Native_store_test_012
679 * @tc.desc: Normal testCase of store for anomalous branch.
680 * @tc.type: FUNC
681 */
682 HWTEST_F(RdbNativeStoreTest, RDB_Native_store_test_012, TestSize.Level1)
683 {
684 int errCode = 0;
685 OH_Rdb_Config config;
686 config.dataBaseDir = RDB_TEST_PATH;
687 config.storeName = "rdb_store_error.db";
688 config.bundleName = nullptr;
689 config.moduleName = "";
690 config.securityLevel = OH_Rdb_SecurityLevel::S1;
691 config.isEncrypt = false;
692 config.selfSize = 0;
693
694 auto store = OH_Rdb_GetOrOpen(nullptr, &errCode);
695 EXPECT_EQ(store, nullptr);
696 store = OH_Rdb_GetOrOpen(&config, nullptr);
697 EXPECT_EQ(store, nullptr);
698 store = OH_Rdb_GetOrOpen(&config, &errCode);
699 EXPECT_EQ(store, nullptr);
700
701 config.selfSize = sizeof(OH_Rdb_Config);
702 store = OH_Rdb_GetOrOpen(&config, &errCode);
703 EXPECT_NE(store, nullptr);
704
705 char createTableSql[] = "CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, data1 TEXT, data2 INTEGER, "
706 "data3 FLOAT, data4 BLOB, data5 TEXT);";
707 errCode = OH_Rdb_Execute(nullptr, createTableSql);
708 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
709 errCode = OH_Rdb_Execute(store, nullptr);
710 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
711
712 OH_VBucket *valueBucket = OH_Rdb_CreateValuesBucket();
713 errCode = OH_Rdb_Insert(nullptr, "test", valueBucket);
714 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
715 errCode = OH_Rdb_Insert(store, nullptr, valueBucket);
716 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
717 errCode = OH_Rdb_Insert(store, "test", nullptr);
718 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
719
720 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
721 errCode = OH_Rdb_Update(nullptr, valueBucket, predicates);
722 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
723 errCode = OH_Rdb_Update(store, nullptr, predicates);
724 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
725 errCode = OH_Rdb_Update(store, valueBucket, nullptr);
726 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
727
728 errCode = OH_Rdb_Delete(nullptr, predicates);
729 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
730 errCode = OH_Rdb_Delete(store, nullptr);
731 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
732
733 auto cursor = OH_Rdb_Query(nullptr, predicates, NULL, 0);
734 EXPECT_EQ(cursor, nullptr);
735 cursor = OH_Rdb_Query(store, nullptr, NULL, 0);
736 EXPECT_EQ(cursor, nullptr);
737
738 char querySql[] = "SELECT * FROM test";
739 cursor = OH_Rdb_ExecuteQuery(nullptr, querySql);
740 EXPECT_EQ(cursor, nullptr);
741 cursor = OH_Rdb_ExecuteQuery(store, nullptr);
742 EXPECT_EQ(cursor, nullptr);
743
744 errCode = OH_Rdb_BeginTransaction(nullptr);
745 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
746 errCode = OH_Rdb_RollBack(nullptr);
747 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
748 errCode = OH_Rdb_Commit(nullptr);
749 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
750
751 char backupDir[] = "backup.db";
752 errCode = OH_Rdb_Backup(nullptr, backupDir);
753 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
754 errCode = OH_Rdb_Backup(store, nullptr);
755 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
756
757 errCode = OH_Rdb_Restore(nullptr, backupDir);
758 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
759 errCode = OH_Rdb_Restore(store, nullptr);
760 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
761
762 int version = 1;
763 errCode = OH_Rdb_SetVersion(nullptr, version);
764 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
765 errCode = OH_Rdb_GetVersion(nullptr, &version);
766 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
767 errCode = OH_Rdb_GetVersion(store, nullptr);
768 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
769
770 errCode = OH_Rdb_CloseStore(nullptr);
771 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
772 errCode = OH_Rdb_DeleteStore(nullptr);
773 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
774 config.dataBaseDir = nullptr;
775 errCode = OH_Rdb_DeleteStore(&config);
776 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
777 config.dataBaseDir = RDB_TEST_PATH;
778 config.storeName = nullptr;
779 errCode = OH_Rdb_DeleteStore(&config);
780 EXPECT_EQ(errCode, OH_Rdb_ErrCode::RDB_E_INVALID_ARGS);
781
782 config.storeName = "rdb_store_error.db";
783 OH_Rdb_CloseStore(store);
784 OH_Rdb_DeleteStore(&config);
785 }