1 /*
2 * Copyright (c) 2021 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 <fstream>
17 #include <gtest/gtest.h>
18 #include <unistd.h>
19
20 #include "db_common.h"
21 #include "distributeddb_data_generate_unit_test.h"
22 #include "distributeddb_tools_unit_test.h"
23 #include "kvdb_manager.h"
24 #include "platform_specific.h"
25 #include "process_system_api_adapter_impl.h"
26 #include "runtime_context.h"
27
28 using namespace testing::ext;
29 using namespace DistributedDB;
30 using namespace DistributedDBUnitTest;
31 using namespace std;
32
33 namespace {
34 enum {
35 SCHEMA_TYPE1 = 1,
36 SCHEMA_TYPE2
37 };
38 static int g_conflictCount = 0;
39 // define some variables to init a KvStoreDelegateManager object.
40 KvStoreDelegateManager g_mgr(APP_ID, USER_ID);
41 string g_testDir;
42 KvStoreConfig g_config;
43
44 // define the g_kvDelegateCallback, used to get some information when open a kv store.
45 DBStatus g_kvDelegateStatus = INVALID_ARGS;
46 KvStoreDelegate *g_kvDelegatePtr = nullptr;
47 // the type of g_kvDelegateCallback is function<void(DBStatus, KvStoreDelegate*)>
48 auto g_kvDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreDelegateCallback, placeholders::_1,
49 placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(g_kvDelegatePtr));
50 KvStoreNbDelegate *g_kvNbDelegatePtr = nullptr;
51 auto g_kvNbDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback,
52 placeholders::_1, placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(g_kvNbDelegatePtr));
53 #ifndef OMIT_JSON
54 const int PASSWD_LEN = 10;
55 const int PASSWD_VAL = 45;
GenerateValidSchemaString(std::string & string,int num=SCHEMA_TYPE1)56 void GenerateValidSchemaString(std::string &string, int num = SCHEMA_TYPE1)
57 {
58 switch (num) {
59 case SCHEMA_TYPE1:
60 string = "{\"SCHEMA_VERSION\":\"1.0\","
61 "\"SCHEMA_MODE\":\"STRICT\","
62 "\"SCHEMA_DEFINE\":{"
63 "\"field_name1\":\"BOOL\","
64 "\"field_name2\":{"
65 "\"field_name3\":\"INTEGER, NOT NULL\","
66 "\"field_name4\":\"LONG, DEFAULT 100\","
67 "\"field_name5\":\"DOUBLE, NOT NULL, DEFAULT 3.14\","
68 "\"field_name6\":\"STRING, NOT NULL, DEFAULT '3.1415'\","
69 "\"field_name7\":[],"
70 "\"field_name8\":{}"
71 "}"
72 "},"
73 "\"SCHEMA_INDEXES\":[\"$.field_name1\", \"$.field_name2.field_name6\"]}";
74 break;
75 case SCHEMA_TYPE2:
76 string = "{\"SCHEMA_VERSION\":\"1.0\","
77 "\"SCHEMA_MODE\":\"STRICT\","
78 "\"SCHEMA_DEFINE\":{"
79 "\"field_name1\":\"LONG, DEFAULT 100\","
80 "\"field_name2\":{"
81 "\"field_name3\":\"INTEGER, NOT NULL\","
82 "\"field_name4\":\"LONG, DEFAULT 100\","
83 "\"field_name5\":\"DOUBLE, NOT NULL, DEFAULT 3.14\","
84 "\"field_name6\":\"STRING, NOT NULL, DEFAULT '3.1415'\""
85 "}"
86 "},"
87 "\"SCHEMA_INDEXES\":[\"$.field_name1\", \"$.field_name2.field_name6\"]}";
88 break;
89 default:
90 return;
91 }
92 }
93
GenerateInvalidSchemaString(std::string & string)94 void GenerateInvalidSchemaString(std::string &string)
95 {
96 string = "123";
97 }
98
GenerateEmptySchemaString(std::string & string)99 void GenerateEmptySchemaString(std::string &string)
100 {
101 string.clear();
102 }
103
WriteValidDataIntoKvStore()104 int WriteValidDataIntoKvStore()
105 {
106 return OK;
107 }
108
OpenOpenedKvstoreWithSchema(const std::string & storeId,bool isEncrypt)109 void OpenOpenedKvstoreWithSchema(const std::string &storeId, bool isEncrypt)
110 {
111 /**
112 * @tc.steps: step1. create a new db(non-memory, encrypt), with schema;
113 * @tc.expected: step1. Returns a non-null kvstore and error code is OK.
114 */
115 KvStoreNbDelegate::Option option = {true, false, isEncrypt};
116 if (isEncrypt) {
117 CipherPassword passwd;
118 vector<uint8_t> passwdBuffer(PASSWD_LEN, PASSWD_VAL);
119 passwd.SetValue(passwdBuffer.data(), passwdBuffer.size());
120 option.passwd = passwd;
121 }
122 GenerateValidSchemaString(option.schema);
123 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
124 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
125 EXPECT_TRUE(g_kvDelegateStatus == OK);
126 KvStoreNbDelegate *kvNbDelegatePtr1 = g_kvNbDelegatePtr;
127 /**
128 * @tc.steps: step2. open an opened db, with same schema;
129 * @tc.expected: step2. Returns a non-null kvstore and error code is OK.
130 */
131 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
132 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
133 EXPECT_TRUE(g_kvDelegateStatus == OK);
134 KvStoreNbDelegate *kvNbDelegatePtr2 = g_kvNbDelegatePtr;
135 /**
136 * @tc.steps: step3. open an opened db, with valid but different schema;
137 * @tc.expected: step3. Returns a null kvstore and error code is SCHEMA_MISMATCH.
138 */
139 GenerateValidSchemaString(option.schema, SCHEMA_TYPE2);
140 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
141 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
142 EXPECT_TRUE(g_kvDelegateStatus == SCHEMA_MISMATCH);
143
144 /**
145 * @tc.steps: step4. open an opened db, with invalid schema;
146 * @tc.expected: step4. Returns a null kvstore and error code is INVALID_SCHEMA.
147 */
148 GenerateInvalidSchemaString(option.schema);
149 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
150 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
151 EXPECT_TRUE(g_kvDelegateStatus == INVALID_SCHEMA);
152
153 /**
154 * @tc.steps: step5. open an opened db, with empty schema;
155 * @tc.expected: step5. Returns a null kvstore and error code is INVALID_SCHEMA.
156 */
157 std::string emptySchema;
158 option.schema = emptySchema;
159 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
160 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
161 EXPECT_TRUE(g_kvDelegateStatus == SCHEMA_MISMATCH);
162
163 EXPECT_EQ(g_mgr.CloseKvStore(kvNbDelegatePtr1), OK);
164 EXPECT_EQ(g_mgr.CloseKvStore(kvNbDelegatePtr2), OK);
165 g_kvNbDelegatePtr = nullptr;
166 EXPECT_TRUE(g_mgr.DeleteKvStore(storeId) == OK);
167 }
168
OpenClosedSchemaKvStore(const std::string & storeId,bool isEncrypt,const std::string & inSchema)169 void OpenClosedSchemaKvStore(const std::string &storeId, bool isEncrypt, const std::string &inSchema)
170 {
171 /**
172 * @tc.steps: step1. create a new db(non-memory), with input schema;
173 * @tc.expected: step1. Returns a non-null kvstore and error code is OK.
174 */
175 KvStoreNbDelegate::Option option = {true, false, isEncrypt};
176 option.schema = inSchema;
177 if (isEncrypt) {
178 CipherPassword passwd;
179 vector<uint8_t> passwdBuffer(PASSWD_LEN, PASSWD_VAL);
180 passwd.SetValue(passwdBuffer.data(), passwdBuffer.size());
181 option.passwd = passwd;
182 }
183 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
184 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
185 EXPECT_TRUE(g_kvDelegateStatus == OK);
186 EXPECT_TRUE(WriteValidDataIntoKvStore() == OK);
187 /**
188 * @tc.steps: step2. close the created kvstore;
189 * @tc.expected: step2. Return OK.
190 */
191 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
192 g_kvNbDelegatePtr = nullptr;
193
194 /**
195 * @tc.steps: step3. reopen the kvstore with same schema;
196 * @tc.expected: step3. Return OK.
197 */
198 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
199 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
200 EXPECT_TRUE(g_kvDelegateStatus == OK);
201 EXPECT_TRUE(WriteValidDataIntoKvStore() == OK);
202 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
203 g_kvNbDelegatePtr = nullptr;
204
205 /**
206 * @tc.steps: step4. reopen the kvstore with valid schema, but the schema is not equal to inSchema;
207 * @tc.expected: step4. Return a null kvstore and retCode is SCHEMA_MISMATCH.
208 */
209 GenerateValidSchemaString(option.schema, SCHEMA_TYPE2);
210 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
211 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
212 EXPECT_TRUE(g_kvDelegateStatus == SCHEMA_MISMATCH);
213 /**
214 * @tc.steps: step5. reopen the kvstore with invalid schema;
215 * @tc.expected: step5. Return a null kvstore and retCode is INVALID_SCHEMA.
216 */
217 GenerateInvalidSchemaString(option.schema);
218 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
219 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
220 EXPECT_TRUE(g_kvDelegateStatus == INVALID_SCHEMA);
221 /**
222 * @tc.steps: step6. reopen the kvstore with empty schema;
223 * @tc.expected: step6. Return a read-only kvstore and retCode is READ_ONLY.
224 */
225 GenerateEmptySchemaString(option.schema);
226 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
227 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
228 EXPECT_TRUE(g_kvDelegateStatus == OK);
229 // here should return READ_ONLY
230 EXPECT_TRUE(WriteValidDataIntoKvStore() == OK);
231 KvStoreNbDelegate *kvNbDelegatePtr1 = g_kvNbDelegatePtr;
232
233 // Open another kvstore with empty schema
234 GenerateEmptySchemaString(option.schema);
235 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
236 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
237 EXPECT_TRUE(g_kvDelegateStatus == OK);
238 KvStoreNbDelegate *kvNbDelegatePtr2 = g_kvNbDelegatePtr;
239 // here should return READ_ONLY
240 EXPECT_TRUE(WriteValidDataIntoKvStore() == OK);
241
242 // Open another kvstore with origin schema
243 option.schema = inSchema;
244 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
245 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
246 EXPECT_TRUE(g_kvDelegateStatus == SCHEMA_MISMATCH);
247
248 EXPECT_EQ(g_mgr.CloseKvStore(kvNbDelegatePtr1), OK);
249 EXPECT_EQ(g_mgr.CloseKvStore(kvNbDelegatePtr2), OK);
250 EXPECT_TRUE(g_mgr.DeleteKvStore(storeId) == OK);
251 }
252
OpenClosedNormalKvStore(const std::string & storeId,bool isEncrypt)253 void OpenClosedNormalKvStore(const std::string &storeId, bool isEncrypt)
254 {
255 /**
256 * @tc.steps: step1. create a new db(non-memory), without schema;
257 * @tc.expected: step1. Returns a non-null kvstore and error code is OK.
258 */
259 KvStoreNbDelegate::Option option = {true, false, isEncrypt};
260 if (isEncrypt) {
261 CipherPassword passwd;
262 vector<uint8_t> passwdBuffer(PASSWD_LEN, PASSWD_VAL);
263 passwd.SetValue(passwdBuffer.data(), passwdBuffer.size());
264 option.passwd = passwd;
265 }
266 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
267 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
268 EXPECT_TRUE(g_kvDelegateStatus == OK);
269 EXPECT_TRUE(WriteValidDataIntoKvStore() == OK);
270 /**
271 * @tc.steps: step2. close the created kvstore;
272 * @tc.expected: step2. Return OK.
273 */
274 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
275 g_kvNbDelegatePtr = nullptr;
276
277 /**
278 * @tc.steps: step3. reopen the kvstore with empty schema;
279 * @tc.expected: step3. Return a kvstore and retCode is OK.
280 */
281 GenerateEmptySchemaString(option.schema);
282 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
283 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
284 EXPECT_TRUE(g_kvDelegateStatus == OK);
285 EXPECT_TRUE(WriteValidDataIntoKvStore() == OK);
286 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
287 g_kvNbDelegatePtr = nullptr;
288 /**
289 * @tc.steps: step4. reopen the kvstore with valid schema;
290 * @tc.expected: step4. Return OK.
291 */
292 GenerateValidSchemaString(option.schema);
293 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
294 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
295 EXPECT_TRUE(g_kvDelegateStatus == OK);
296 EXPECT_TRUE(WriteValidDataIntoKvStore() == OK);
297 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
298 g_kvNbDelegatePtr = nullptr;
299
300 /**
301 * @tc.steps: step5. reopen the kvstore with invalid schema;
302 * @tc.expected: step5. Return a null kvstore and retCode is SCHEMA_MISMATCH.
303 */
304 GenerateInvalidSchemaString(option.schema);
305 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
306 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
307 EXPECT_TRUE(g_kvDelegateStatus == INVALID_SCHEMA);
308 EXPECT_TRUE(g_mgr.DeleteKvStore(storeId) == OK);
309 }
310 #endif
311 }
312
313 class DistributedDBInterfacesDatabaseTest : public testing::Test {
314 public:
315 static void SetUpTestCase(void);
316 static void TearDownTestCase(void);
317 void SetUp();
318 void TearDown();
319 };
320
SetUpTestCase(void)321 void DistributedDBInterfacesDatabaseTest::SetUpTestCase(void)
322 {
323 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
324 g_config.dataDir = g_testDir;
325 g_mgr.SetKvStoreConfig(g_config);
326 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
327 }
328
TearDownTestCase(void)329 void DistributedDBInterfacesDatabaseTest::TearDownTestCase(void)
330 {
331 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
332 }
333
SetUp(void)334 void DistributedDBInterfacesDatabaseTest::SetUp(void)
335 {
336 DistributedDBToolsUnitTest::PrintTestCaseInfo();
337 g_kvDelegateStatus = INVALID_ARGS;
338 g_kvDelegatePtr = nullptr;
339 }
340
TearDown(void)341 void DistributedDBInterfacesDatabaseTest::TearDown(void)
342 {
343 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
344 LOGE("rm test db files error!");
345 }
346 }
347
348 /**
349 * @tc.name: GetKvStore001
350 * @tc.desc: Get kv store through different parameters.
351 * @tc.type: FUNC
352 * @tc.require: AR000CQDV4 AR000CQS3P
353 * @tc.author: huangnaigu
354 */
355 HWTEST_F(DistributedDBInterfacesDatabaseTest, GetKvStore001, TestSize.Level1)
356 {
357 /**
358 * @tc.steps: step1. Obtain the kvStore through the GetKvStore interface of the delegate manager
359 * using the parameter the normal storId, createIfNecessary(true) and isLocal(true).
360 * @tc.steps: step2. Close the kvStore through the CloseKvStore interface of the delegate manager.
361 * @tc.expected: step1. Returns a non-null kvstore.
362 * @tc.expected: step2. Returns OK.
363 */
364 KvStoreDelegate::Option option = {true, true, false};
365 g_mgr.GetKvStore("distributed_db_test1", option, g_kvDelegateCallback);
366 ASSERT_TRUE(g_kvDelegatePtr != nullptr);
367 EXPECT_TRUE(g_kvDelegateStatus == OK);
368 EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
369
370 /**
371 * @tc.steps: step3. Obtain the kvStore through the GetKvStore interface of the delegate manager
372 * using the parameter the normal storId, createIfNecessary(true) and isLocal(false).
373 * @tc.steps: step4. Close the kvStore through the CloseKvStore interface of the delegate manager.
374 * @tc.expected: step3. Returns a non-null kvstore.
375 * @tc.expected: step4. Returns OK.
376 */
377 option = {true, false, false};
378 g_mgr.GetKvStore("distributed_db_test2", option, g_kvDelegateCallback);
379 ASSERT_TRUE(g_kvDelegatePtr != nullptr);
380 EXPECT_TRUE(g_kvDelegateStatus == OK);
381 EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
382
383 /**
384 * @tc.steps: step5. Obtain the kvStore through the GetKvStore interface of the delegate manager
385 * using the parameter the normal storId, createIfNecessary(false) and isLocal(true).
386 * @tc.expected: step5. Returns a non-null kvstore and error code is ERROR.
387 */
388 option = {false, true, false};
389 g_mgr.GetKvStore("distributed_db_test3", option, g_kvDelegateCallback);
390 ASSERT_TRUE(g_kvDelegatePtr == nullptr);
391 EXPECT_NE(g_kvDelegateStatus, OK);
392
393 /**
394 * @tc.steps: step6. Obtain the kvStore through the GetKvStore interface of the delegate manager
395 * using the parameter the normal storId, createIfNecessary(false) and isLocal(false).
396 * @tc.expected: step6. Returns a non-null kvstore and error code is ERROR.
397 */
398 option = {false, false, false};
399 g_mgr.GetKvStore("distributed_db_test4", option, g_kvDelegateCallback);
400 ASSERT_TRUE(g_kvDelegatePtr == nullptr);
401 EXPECT_NE(g_kvDelegateStatus, OK);
402
403 /**
404 * @tc.steps: step7. Obtain the kvStore through the GetKvStore interface of the delegate manager
405 * which is initialized with the empty appid.
406 * @tc.expected: step7. Returns a non-null kvstore and error code is INVALID_ARGS.
407 */
408 KvStoreDelegateManager invalidMgrFirst("", USER_ID);
409 invalidMgrFirst.SetKvStoreConfig(g_config);
410 option = {true, true, false};
411 invalidMgrFirst.GetKvStore("distributed_db_test5", option, g_kvDelegateCallback);
412 ASSERT_TRUE(g_kvDelegatePtr == nullptr);
413 EXPECT_TRUE(g_kvDelegateStatus == INVALID_ARGS);
414
415 /**
416 * @tc.steps: step8. Obtain the kvStore through the GetKvStore interface of the delegate manager
417 * which is initialized with the empty userid.
418 * @tc.expected: step8. Returns a non-null kvstore and error code is INVALID_ARGS.
419 */
420 KvStoreDelegateManager invalidMgrSecond(APP_ID, "");
421 invalidMgrSecond.SetKvStoreConfig(g_config);
422 invalidMgrSecond.GetKvStore("distributed_db_test6", option, g_kvDelegateCallback);
423 ASSERT_TRUE(g_kvDelegatePtr == nullptr);
424 EXPECT_TRUE(g_kvDelegateStatus == INVALID_ARGS);
425
426 /**
427 * @tc.steps: step9. Obtain the kvStore through the GetKvStore interface of the delegate manager
428 * using the parameter the empty storId, createIfNecessary(true) and isLocal(true).
429 * @tc.expected: step9. Returns a non-null kvstore and error code is INVALID_ARGS.
430 */
431 g_mgr.GetKvStore("", option, g_kvDelegateCallback);
432 ASSERT_TRUE(g_kvDelegatePtr == nullptr);
433 EXPECT_TRUE(g_kvDelegateStatus == INVALID_ARGS);
434
435 /**
436 * @tc.steps: step10. Obtain the kvStore through the GetKvStore interface of the delegate manager
437 * using the parameter the invalid storId, createIfNecessary(true) and isLocal(true).
438 * @tc.expected: step10. Returns a non-null kvstore and error code is INVALID_ARGS.
439 */
440 g_mgr.GetKvStore("$@.test", option, g_kvDelegateCallback);
441 ASSERT_TRUE(g_kvDelegatePtr == nullptr);
442 EXPECT_TRUE(g_kvDelegateStatus == INVALID_ARGS);
443
444 /**
445 * @tc.steps: step11. Obtain the kvStore through the GetKvStore interface of the delegate manager
446 * using the parameter: all alphabet string storId, createIfNecessary(true) and isLocal(true).
447 * @tc.expected: step11. Returns a non-null kvstore and error code is OK.
448 */
449 g_mgr.GetKvStore("TEST", option, g_kvDelegateCallback);
450 ASSERT_TRUE(g_kvDelegatePtr != nullptr);
451 EXPECT_TRUE(g_kvDelegateStatus == OK);
452 EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
453
454 /**
455 * @tc.steps: step12. Obtain the kvStore through the GetKvStore interface of the delegate manager
456 * using the parameter: digital string storId, createIfNecessary(true) and isLocal(true).
457 * @tc.expected: step12. Returns a non-null kvstore and error code is OK.
458 */
459 g_mgr.GetKvStore("123", option, g_kvDelegateCallback);
460 ASSERT_TRUE(g_kvDelegatePtr != nullptr);
461 EXPECT_TRUE(g_kvDelegateStatus == OK);
462 EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
463
464 /**
465 * @tc.steps: step13. Obtain the kvStore through the GetKvStore interface of the delegate manager
466 * using the parmater: digital and alphabet combined string storId, createIfNecessary(true) and isLocal(true).
467 * @tc.expected: step13. Returns a non-null kvstore and error code is OK.
468 */
469 g_mgr.GetKvStore("TEST_test_123", option, g_kvDelegateCallback);
470 ASSERT_TRUE(g_kvDelegatePtr != nullptr);
471 EXPECT_TRUE(g_kvDelegateStatus == OK);
472 EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
473 }
474
475 /**
476 * @tc.name: GetKvStore002
477 * @tc.desc: Get kv store through different parameters for the same storeID.
478 * @tc.type: FUNC
479 * @tc.require: AR000CQDV5 AR000CQS3P
480 * @tc.author: huangnaigu
481 */
482 HWTEST_F(DistributedDBInterfacesDatabaseTest, GetKvStore002, TestSize.Level1)
483 {
484 /**
485 * @tc.steps: step1. Obtain the kvStore through the GetKvStore interface of the delegate manager
486 * using the parameter createIfNecessary(true) and isLocal(true).
487 * @tc.expected: step1. Returns a non-null kvstore and error code is OK.
488 */
489 CipherPassword passwd;
490 KvStoreDelegate::Option option = {true, true, false};
491 g_mgr.GetKvStore("distributed_getkvstore_002", option, g_kvDelegateCallback);
492 ASSERT_TRUE(g_kvDelegatePtr != nullptr);
493 EXPECT_TRUE(g_kvDelegateStatus == OK);
494 EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
495
496 /**
497 * @tc.steps: step2. Re-Obtain the kvStore through the GetKvStore interface of the delegate manager
498 * using the parameter createIfNecessary(true) and isLocal(false).
499 * @tc.expected: step2. Returns a non-null kvstore and error code is OK.
500 */
501 option.localOnly = false;
502 g_mgr.GetKvStore("distributed_getkvstore_002", option, g_kvDelegateCallback);
503 ASSERT_TRUE(g_kvDelegatePtr != nullptr);
504 EXPECT_TRUE(g_kvDelegateStatus == OK);
505 EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
506
507 /**
508 * @tc.steps: step3. Re-Obtain the kvStore through the GetKvStore interface of the delegate manager
509 * using the parameter createIfNecessary(false) and isLocal(true).
510 * @tc.expected: step3. Returns a non-null kvstore and error code is OK.
511 */
512 option = {false, true, false};
513 g_mgr.GetKvStore("distributed_getkvstore_002", option, g_kvDelegateCallback);
514 ASSERT_TRUE(g_kvDelegatePtr != nullptr);
515 EXPECT_TRUE(g_kvDelegateStatus == OK);
516 EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
517
518 /**
519 * @tc.steps: step4. Re-Obtain the kvStore through the GetKvStore interface of the delegate manager
520 * using the parameter createIfNecessary(false) and isLocal(false).
521 * @tc.expected: step4. Returns a non-null kvstore and error code is OK.
522 */
523 option = {false, false, false};
524 g_mgr.GetKvStore("distributed_getkvstore_002", option, g_kvDelegateCallback);
525 ASSERT_TRUE(g_kvDelegatePtr != nullptr);
526 EXPECT_TRUE(g_kvDelegateStatus == OK);
527 EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
528
529 /**
530 * @tc.steps: step5. Re-Obtain the kvStore through the GetKvStore interface of the delegate manager
531 * using the parameter createIfNecessary(false) and isLocal(false).
532 * @tc.expected: step5. Returns a non-null kvstore and error code is OK.
533 */
534 option = {true, true, false};
535 g_mgr.GetKvStore("distributed_getkvstore_002", option, g_kvDelegateCallback);
536 ASSERT_TRUE(g_kvDelegatePtr != nullptr);
537 EXPECT_TRUE(g_kvDelegateStatus == OK);
538 string retStoreId = g_kvDelegatePtr->GetStoreId();
539 EXPECT_TRUE(retStoreId.compare("distributed_getkvstore_002") == 0);
540 EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
541 }
542
543 /**
544 * @tc.name: GetKvStore003
545 * @tc.desc: Get kv store through different SecurityOption, abnormal or normal.
546 * @tc.type: FUNC
547 * @tc.require: AR000EV1G2
548 * @tc.author: liuwenkai
549 */
550 HWTEST_F(DistributedDBInterfacesDatabaseTest, GetKvStore003, TestSize.Level1)
551 {
552 /**
553 * @tc.steps: step1. Obtain the kvStore through the GetKvStore interface of the delegate manager
554 * using the parameter secOption(abnormal).
555 * @tc.expected: step1. Returns a null kvstore and error code is not OK.
556 */
557 std::shared_ptr<IProcessSystemApiAdapter> g_adapter = std::make_shared<ProcessSystemApiAdapterImpl>();
558 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
559 KvStoreNbDelegate::Option option = {true, false, false};
560 int abnormalNum = -100;
561 option.secOption.securityLabel = abnormalNum;
562 option.secOption.securityFlag = abnormalNum;
563 g_mgr.GetKvStore("distributed_getkvstore_003", option, g_kvNbDelegateCallback);
564 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
565 EXPECT_TRUE(g_kvDelegateStatus != OK);
566
567 /**
568 * @tc.steps: step2. Obtain the kvStore through the GetKvStore interface of the delegate manager
569 * using the parameter secOption(normal).
570 * @tc.expected: step2. Returns a non-null kvstore and error code is OK.
571 */
572 option.secOption.securityLabel = S3;
573 option.secOption.securityFlag = 0;
574 g_mgr.GetKvStore("distributed_getkvstore_003", option, g_kvNbDelegateCallback);
575 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
576 EXPECT_TRUE(g_kvDelegateStatus == OK);
577 KvStoreNbDelegate *kvNbDelegatePtr1 = g_kvNbDelegatePtr;
578
579 /**
580 * @tc.steps: step3. Obtain the kvStore through the GetKvStore interface of the delegate manager
581 * using the parameter secOption(normal but not same as last).
582 * @tc.expected: step3. Returns a null kvstore and error code is not OK.
583 */
584 option.secOption.securityLabel = S3;
585 option.secOption.securityFlag = 1;
586 g_mgr.GetKvStore("distributed_getkvstore_003", option, g_kvNbDelegateCallback);
587 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
588 EXPECT_TRUE(g_kvDelegateStatus != OK);
589
590 /**
591 * @tc.steps: step4. Obtain the kvStore through the GetKvStore interface of the delegate manager
592 * using the parameter secOption(normal and same as last).
593 * @tc.expected: step4. Returns a non-null kvstore and error code is OK.
594 */
595 option.secOption.securityLabel = S3;
596 option.secOption.securityFlag = 0;
597 g_mgr.GetKvStore("distributed_getkvstore_003", option, g_kvNbDelegateCallback);
598 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
599 EXPECT_TRUE(g_kvDelegateStatus == OK);
600
601 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
602 EXPECT_EQ(g_mgr.CloseKvStore(kvNbDelegatePtr1), OK);
603 g_kvNbDelegatePtr = nullptr;
604 EXPECT_TRUE(g_mgr.DeleteKvStore("distributed_getkvstore_003") == OK);
605 }
606
NotifierCallback(const KvStoreNbConflictData & data)607 static void NotifierCallback(const KvStoreNbConflictData &data)
608 {
609 LOGE("Trigger conflict callback!");
610 g_conflictCount++;
611 }
612
613 /**
614 * @tc.name: GetKvStore004
615 * @tc.desc: Get kv store parameters with Observer and Notifier, then trigger callback.
616 * @tc.type: FUNC
617 * @tc.require: AR000EV1G2
618 * @tc.author: liuwenkai
619 */
620 HWTEST_F(DistributedDBInterfacesDatabaseTest, GetKvStore004, TestSize.Level1)
621 {
622 /**
623 * @tc.steps: step1. Obtain the kvStore through the GetKvStore interface of the delegate manager
624 * using the parameter observer, notifier, key.
625 * @tc.expected: step1. Returns a non-null kvstore and error code is OK.
626 */
627 KvStoreNbDelegate::Option option = {true, false, false};
628 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
629 ASSERT_NE(observer, nullptr);
630 Key key;
631 Value value1;
632 Value value2;
633 key.push_back(1);
634 value1.push_back(1);
635 value2.push_back(2);
636 option.conflictType = CONFLICT_NATIVE_ALL;
637 option.notifier = NotifierCallback;
638 option.key = key;
639 option.observer = observer;
640 option.mode = OBSERVER_CHANGES_NATIVE;
641 g_conflictCount = 0;
642 int sleepTime = 100;
643 g_mgr.GetKvStore("distributed_getkvstore_004", option, g_kvNbDelegateCallback);
644 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
645 EXPECT_TRUE(g_kvDelegateStatus == OK);
646
647 /**
648 * @tc.steps: step2. Put(k1,v1) to db and check the observer info.
649 * @tc.expected: step2. Put successfully and trigger notifier callback.
650 */
651 EXPECT_TRUE(g_kvNbDelegatePtr->Put(key, value1) == OK);
652 std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
653 LOGI("observer count:%lu", observer->GetCallCount());
654 EXPECT_TRUE(observer->GetCallCount() == 1);
655
656 /**
657 * @tc.steps: step3. put(k1,v2) to db and check the observer info.
658 * @tc.expected: step3. put successfully and trigger conflict callback.
659 */
660 EXPECT_TRUE(g_kvNbDelegatePtr->Put(key, value2) == OK);
661 std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
662 LOGI("observer count:%lu", observer->GetCallCount());
663 EXPECT_TRUE(observer->GetCallCount() == 2);
664 LOGI("call conflictNotifier count:%d, 1 means trigger success.", g_conflictCount);
665 EXPECT_EQ(g_conflictCount, 1);
666
667 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
668 g_kvNbDelegatePtr = nullptr;
669 delete observer;
670 observer = nullptr;
671 EXPECT_TRUE(g_mgr.DeleteKvStore("distributed_getkvstore_004") == OK);
672 }
673
674 /**
675 * @tc.name: CloseKvStore001
676 * @tc.desc: Test the CloseKvStore Interface and check whether the database file can be closed.
677 * @tc.type: FUNC
678 * @tc.require: AR000CQDV6 AR000CQS3P
679 * @tc.author: huangnaigu
680 */
681 HWTEST_F(DistributedDBInterfacesDatabaseTest, CloseKvStore001, TestSize.Level1)
682 {
683 /**
684 * @tc.steps: step1. Obtain the kvStore of the non-existed database through the GetKvStore interface of
685 * the delegate manager using the parameter createdIfNecessary(true)
686 * @tc.steps: step2. Close the valid kvStore.
687 * @tc.expected: step1. Returns a non-null kvstore and error code is OK.
688 * @tc.expected: step2. Returns OK.
689 */
690 CipherPassword passwd;
691 KvStoreDelegate::Option option = {true, true, false};
692 g_mgr.GetKvStore("CloseKvStore_001", option, g_kvDelegateCallback);
693 ASSERT_TRUE(g_kvDelegatePtr != nullptr);
694 EXPECT_TRUE(g_kvDelegateStatus == OK);
695 EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
696 g_kvDelegatePtr = nullptr;
697
698 /**
699 * @tc.steps: step3. Obtain the kvStore of the existed database through the GetKvStore interface of
700 * the delegate manager using the parameter createIfNecessary(false)
701 * @tc.steps: step4. Close the valid kvStore.
702 * @tc.expected: step3. Returns a non-null kvstore and error code is OK.
703 * @tc.expected: step4. Returns OK.
704 */
705 option = {false, true, false};
706 g_mgr.GetKvStore("CloseKvStore_001", option, g_kvDelegateCallback);
707 ASSERT_TRUE(g_kvDelegatePtr != nullptr);
708 EXPECT_TRUE(g_kvDelegateStatus == OK);
709 EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
710 g_kvDelegatePtr = nullptr;
711
712 /**
713 * @tc.steps: step5. Close the invalid kvStore which is nullptr.
714 * @tc.expected: step5. Returns INVALID_ARGS.
715 */
716 KvStoreDelegate *storeDelegate = nullptr;
717 EXPECT_EQ(g_mgr.CloseKvStore(storeDelegate), INVALID_ARGS);
718 }
719
720 /**
721 * @tc.name: DeleteKvStore001
722 * @tc.desc: Test the DeleteKvStore Interface and check whether the database files can be removed.
723 * @tc.type: FUNC
724 * @tc.require: AR000C2F0C AR000CQDV7
725 * @tc.author: huangnaigu
726 */
727 HWTEST_F(DistributedDBInterfacesDatabaseTest, DeleteKvStore001, TestSize.Level1)
728 {
729 /**
730 * @tc.steps: step1. Obtain the kvStore through the GetKvStore interface of
731 * the delegate manager using the parameter createIfNecessary(true)
732 * @tc.steps: step2. Close the kvStore.
733 * @tc.expected: step1. Returns a non-null kvstore and error code is OK.
734 * @tc.expected: step2. Returns OK.
735 */
736 CipherPassword passwd;
737 KvStoreDelegate::Option option = {true, true, false};
738 const std::string storeId("DeleteKvStore_001");
739 g_mgr.GetKvStore(storeId, option, g_kvDelegateCallback);
740 std::string origIdentifierName = USER_ID + "-" + APP_ID + "-" + storeId;
741 std::string hashIdentifierName = DBCommon::TransferHashString(origIdentifierName);
742 std::string identifierName = DBCommon::TransferStringToHex(hashIdentifierName);
743 ASSERT_TRUE(g_kvDelegatePtr != nullptr);
744 EXPECT_TRUE(g_kvDelegateStatus == OK);
745 EXPECT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
746 g_kvDelegatePtr = nullptr;
747
748 /**
749 * @tc.steps: step3. Check the database file
750 * @tc.expected: step3. the database file are existed.
751 */
752 string dbFileName = g_testDir + "/" + identifierName + "/local/local.db";
753 ifstream dbFile(dbFileName);
754 EXPECT_TRUE(dbFile);
755 dbFile.close();
756 string walFileName = g_testDir + "/" + identifierName + "/local/local.db-wal";
757 fstream walFile(walFileName, fstream::out);
758 EXPECT_TRUE(walFile.is_open());
759 walFile.close();
760 string shmFileName = g_testDir + "/" + identifierName + "/local/local.db-shm";
761 fstream shmFile(shmFileName, fstream::out);
762 EXPECT_TRUE(shmFile.is_open());
763 shmFile.close();
764
765 std::string dataBaseDir = g_testDir + "/" + identifierName;
766 EXPECT_GE(access(dataBaseDir.c_str(), F_OK), 0);
767
768 /**
769 * @tc.steps: step4. Delete the kvStore through the DeleteKvStore interface of
770 * the delegate manager
771 * @tc.steps: step5. Check the database files and the storage paths.
772 * @tc.expected: step4. Returns OK.
773 * @tc.expected: step5. The database files and the storage paths are not existed.
774 */
775 EXPECT_TRUE(g_mgr.DeleteKvStore(storeId) == OK);
776 ifstream dbFileAfter(dbFileName);
777 ifstream walFileAfter(walFileName);
778 ifstream shmFileAfter(shmFileName);
779 EXPECT_FALSE(dbFileAfter);
780 EXPECT_FALSE(walFileAfter);
781 EXPECT_FALSE(shmFileAfter);
782 ASSERT_EQ(OS::CheckPathExistence(dataBaseDir), false);
783 std::string storeIdOnlyIdentifier = DBCommon::TransferHashString(storeId);
784 std::string storeIdOnlyIdentifierName = DBCommon::TransferStringToHex(storeIdOnlyIdentifier);
785 std::string storeIdOnlyIdDataBaseDir = g_testDir + "/" + storeIdOnlyIdentifierName;
786 ASSERT_EQ(OS::CheckPathExistence(storeIdOnlyIdDataBaseDir), false);
787
788 /**
789 * @tc.steps: step6. Re-Delete the kvStore through the DeleteKvStore interface of
790 * the delegate manager
791 * @tc.expected: step6. Returns NOT_FOUND.
792 */
793 EXPECT_TRUE(g_mgr.DeleteKvStore(storeId) == NOT_FOUND);
794 }
795
796 /**
797 * @tc.name: RepeatCloseKvStore001
798 * @tc.desc: Close the kv store repeatedly and check the database.
799 * @tc.type: FUNC
800 * @tc.require: AR000C2F0C AR000CQDV7
801 * @tc.author: wangbingquan
802 */
803 HWTEST_F(DistributedDBInterfacesDatabaseTest, RepeatCloseKvStore001, TestSize.Level2)
804 {
805 /**
806 * @tc.steps: step1. Obtain the kvStore through the GetKvStore interface of
807 * the delegate manager using the parameter createIfNecessary(true)
808 * @tc.expected: step1. Returns a non-null kvstore and error code is OK.
809 */
810 CipherPassword passwd;
811 KvStoreNbDelegate::Option option = {true, false, false};
812 g_mgr.GetKvStore("RepeatCloseKvStore_001", option, g_kvNbDelegateCallback);
813 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
814 EXPECT_TRUE(g_kvDelegateStatus == OK);
815 static const size_t totalSize = 50;
816
817 /**
818 * @tc.steps: step2. Put into the database some data.
819 * @tc.expected: step2. Put returns OK.
820 */
821 std::vector<Key> keys;
822 for (size_t i = 0; i < totalSize; i++) {
823 Entry entry;
824 DistributedDBToolsUnitTest::GetRandomKeyValue(entry.key, static_cast<uint32_t>(i + 1));
825 DistributedDBToolsUnitTest::GetRandomKeyValue(entry.value);
826 EXPECT_EQ(g_kvNbDelegatePtr->Put(entry.key, entry.value), OK);
827 keys.push_back(entry.key);
828 }
829
830 /**
831 * @tc.steps: step3. Delete the data from the database, and close the database, reopen the database and
832 * get the data.
833 * @tc.expected: step3. Delete returns OK, Close returns OK and Get returns NOT_FOUND.
834 */
835 for (size_t i = 0; i < keys.size(); i++) {
836 Value value;
837 EXPECT_EQ(g_kvNbDelegatePtr->Delete(keys[i]), OK);
838 EXPECT_EQ(g_kvNbDelegatePtr->Get(keys[i], value), NOT_FOUND);
839 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
840 g_mgr.GetKvStore("RepeatCloseKvStore_001", option, g_kvNbDelegateCallback);
841 EXPECT_EQ(g_kvNbDelegatePtr->Get(keys[i], value), NOT_FOUND);
842 }
843 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
844 /**
845 * @tc.steps: step4. Delete the kvstore created before.
846 * @tc.expected: step4. Delete returns OK.
847 */
848 EXPECT_EQ(g_mgr.DeleteKvStore("RepeatCloseKvStore_001"), OK);
849 }
850 #ifndef OMIT_JSON
851 /**
852 * @tc.name: CreatKvStoreWithSchema001
853 * @tc.desc: Create non-memory KvStore with schema, check if create success.
854 * @tc.type: FUNC
855 * @tc.require: AR000DR9K2
856 * @tc.author: weifeng
857 */
858 HWTEST_F(DistributedDBInterfacesDatabaseTest, CreatKvStoreWithSchema001, TestSize.Level1)
859 {
860 /**
861 * @tc.steps: step1. create a new db(non-memory, non-encrypt), with valid schema;
862 * @tc.expected: step1. Returns a non-null kvstore and error code is OK.
863 */
864 KvStoreNbDelegate::Option option = {true, false, false};
865 GenerateValidSchemaString(option.schema);
866 g_mgr.GetKvStore("CreatKvStoreWithSchema_001", option, g_kvNbDelegateCallback);
867 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
868 EXPECT_TRUE(g_kvDelegateStatus == OK);
869 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
870 g_kvNbDelegatePtr = nullptr;
871 EXPECT_TRUE(g_mgr.DeleteKvStore("CreatKvStoreWithSchema_001") == OK);
872 /**
873 * @tc.steps: step2. create a new db(non-memory, non-encrypt), with invalid schema;
874 * @tc.expected: step2. Returns null kvstore and error code is INVALID_SCHEMA.
875 */
876 option = {true, false, false};
877 GenerateInvalidSchemaString(option.schema);
878 g_mgr.GetKvStore("CreatKvStoreWithSchema_001_invalid_schema", option, g_kvNbDelegateCallback);
879 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
880 EXPECT_TRUE(g_kvDelegateStatus == INVALID_SCHEMA);
881 EXPECT_TRUE(g_mgr.DeleteKvStore("CreatKvStoreWithSchema_001_invalid_schema") == NOT_FOUND);
882 #ifndef OMIT_ENCRYPT
883 /**
884 * @tc.steps: step3. create a new db(non-memory, encrypt), with valid schema;
885 * @tc.expected: step3. Returns a non-null kvstore and error code is OK.
886 */
887 CipherPassword passwd;
888 vector<uint8_t> passwdBuffer(10, 45);
889 passwd.SetValue(passwdBuffer.data(), passwdBuffer.size());
890 option = {true, false, true};
891 GenerateValidSchemaString(option.schema);
892 option.passwd = passwd;
893 g_mgr.GetKvStore("CreatKvStoreWithSchema_001_002", option, g_kvNbDelegateCallback);
894 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
895 EXPECT_TRUE(g_kvDelegateStatus == OK);
896 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
897 g_kvNbDelegatePtr = nullptr;
898 EXPECT_TRUE(g_mgr.DeleteKvStore("CreatKvStoreWithSchema_001_002") == OK);
899
900 /**
901 * @tc.steps: step4. create a new db(non-memory, non-encrypt), with invalid schema;
902 * @tc.expected: step2. Returns null kvstore and error code is INVALID_SCHEMA.
903 */
904 option = {true, false, false};
905 GenerateInvalidSchemaString(option.schema);
906 g_mgr.GetKvStore("CreatKvStoreWithSchema_002_invalid_schema", option, g_kvNbDelegateCallback);
907 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
908 EXPECT_TRUE(g_kvDelegateStatus == INVALID_SCHEMA);
909 EXPECT_TRUE(g_mgr.DeleteKvStore("CreatKvStoreWithSchema_002_invalid_schema") == NOT_FOUND);
910 #endif
911 }
912
913 /**
914 * @tc.name: CreatKvStoreWithSchema002
915 * @tc.desc: Create memory KvStore with schema, check if create success.
916 * @tc.type: FUNC
917 * @tc.require: AR000DR9K2
918 * @tc.author: weifeng
919 */
920 HWTEST_F(DistributedDBInterfacesDatabaseTest, CreatKvStoreWithSchema002, TestSize.Level1)
921 {
922 /**
923 * @tc.steps: step1. create a new db(memory, non-encrypt), with valid schema;
924 * @tc.expected: step1. Returns a null kvstore and error code is NOT_SUPPORT.
925 */
926 KvStoreNbDelegate::Option option = {true, true, false};
927 GenerateValidSchemaString(option.schema);
928 g_mgr.GetKvStore("CreatKvStoreWithSchema_002", option, g_kvNbDelegateCallback);
929 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
930 EXPECT_TRUE(g_kvDelegateStatus == NOT_SUPPORT);
931
932 /**
933 * @tc.steps: step2. create a new db(memory, non-encrypt), with invalid schema;
934 * @tc.expected: step2. Returns null kvstore and error code is NOT_SUPPORT.
935 */
936 option = {true, true, false};
937 GenerateInvalidSchemaString(option.schema);
938 g_mgr.GetKvStore("CreatKvStoreWithSchema_002_invalid", option, g_kvNbDelegateCallback);
939 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
940 EXPECT_TRUE(g_kvDelegateStatus == NOT_SUPPORT);
941 }
942 #ifndef OMIT_ENCRYPT
943 /**
944 * @tc.name: OpenKvStoreWithSchema001
945 * @tc.desc: open an opened kvstore(non-memory, no-schema) with schema, check if open success.
946 * @tc.type: FUNC
947 * @tc.require: AR000DR9K2
948 * @tc.author: weifeng
949 */
950 HWTEST_F(DistributedDBInterfacesDatabaseTest, OpenKvStoreWithSchema001, TestSize.Level1)
951 {
952 /**
953 * @tc.steps: step1. create a new db(non-memory, non-encrypt), without schema;
954 * @tc.expected: step1. Returns a non-null kvstore and error code is OK.
955 */
956 KvStoreNbDelegate::Option option = {true, false, false};
957 g_mgr.GetKvStore("OpenKvStoreWithSchema_001", option, g_kvNbDelegateCallback);
958 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
959 EXPECT_TRUE(g_kvDelegateStatus == OK);
960 KvStoreNbDelegate *kvNbDelegatePtr = g_kvNbDelegatePtr;
961 /**
962 * @tc.steps: step2. open the db with valid schema;
963 * @tc.expected: step2. Returns a null kvstore and error code is SCHEMA_MISMATCH.
964 */
965 GenerateValidSchemaString(option.schema);
966 g_mgr.GetKvStore("OpenKvStoreWithSchema_001", option, g_kvNbDelegateCallback);
967 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
968 EXPECT_TRUE(g_kvDelegateStatus == SCHEMA_MISMATCH);
969
970 /**
971 * @tc.steps: step3. open the db with invalid schema;
972 * @tc.expected: step3. Returns a null kvstore and error code is SCHEMA_MISMATCH.
973 */
974 GenerateInvalidSchemaString(option.schema);
975 g_mgr.GetKvStore("OpenKvStoreWithSchema_001", option, g_kvNbDelegateCallback);
976 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
977 EXPECT_TRUE(g_kvDelegateStatus == INVALID_SCHEMA);
978
979 EXPECT_EQ(g_mgr.CloseKvStore(kvNbDelegatePtr), OK);
980 g_kvNbDelegatePtr = nullptr;
981 EXPECT_TRUE(g_mgr.DeleteKvStore("OpenKvStoreWithSchema_001") == OK);
982 /**
983 * @tc.steps: step4. create a new db(non-memory, encrypt), without schema;
984 * @tc.expected: step4. Returns a non-null kvstore and error code is OK.
985 */
986 option = {true, false, true};
987 CipherPassword passwd;
988 vector<uint8_t> passwdBuffer(10, 45);
989 passwd.SetValue(passwdBuffer.data(), passwdBuffer.size());
990 option.passwd = passwd;
991 g_mgr.GetKvStore("OpenKvStoreWithSchema_001_encrypt", option, g_kvNbDelegateCallback);
992 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
993 EXPECT_TRUE(g_kvDelegateStatus == OK);
994 kvNbDelegatePtr = g_kvNbDelegatePtr;
995 /**
996 * @tc.steps: step5. open the db with valid schema;
997 * @tc.expected: step5. Returns a null kvstore and error code is SCHEMA_MISMATCH.
998 */
999 GenerateValidSchemaString(option.schema);
1000 g_mgr.GetKvStore("OpenKvStoreWithSchema_001_encrypt", option, g_kvNbDelegateCallback);
1001 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
1002 EXPECT_TRUE(g_kvDelegateStatus == SCHEMA_MISMATCH);
1003
1004 /**
1005 * @tc.steps: step6. open the db with invalid schema;
1006 * @tc.expected: step6. Returns a null kvstore and error code is SCHEMA_MISMATCH.
1007 */
1008 GenerateInvalidSchemaString(option.schema);
1009 g_mgr.GetKvStore("OpenKvStoreWithSchema_001_encrypt", option, g_kvNbDelegateCallback);
1010 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
1011 EXPECT_TRUE(g_kvDelegateStatus == INVALID_SCHEMA);
1012
1013 EXPECT_EQ(g_mgr.CloseKvStore(kvNbDelegatePtr), OK);
1014 g_kvNbDelegatePtr = nullptr;
1015 EXPECT_TRUE(g_mgr.DeleteKvStore("OpenKvStoreWithSchema_001_encrypt") == OK);
1016 }
1017 #endif
1018 /**
1019 * @tc.name: OpenKvStoreWithSchema002
1020 * @tc.desc: open an opened kvstore(non-memory, schema) with schema, check if open success.
1021 * @tc.type: FUNC
1022 * @tc.require: AR000DR9K2
1023 * @tc.author: weifeng
1024 */
1025 HWTEST_F(DistributedDBInterfacesDatabaseTest, OpenKvStoreWithSchema002, TestSize.Level1)
1026 {
1027 #ifndef OMIT_ENCRYPT
1028 /**
1029 * @tc.steps: step1. open an opened kvstore(non-memory, non-encrypt), with different schemas;
1030 */
1031 OpenOpenedKvstoreWithSchema("OpenKvStoreWithSchema_002", true);
1032 #endif
1033 /**
1034 * @tc.steps: step2. open an opened kvstore(non-memory, encrypt), with different schemas;
1035 */
1036 OpenOpenedKvstoreWithSchema("OpenKvStoreWithSchema_002_encrypt", false);
1037 }
1038
1039 /**
1040 * @tc.name: OpenKvStoreWithSchema003
1041 * @tc.desc: open an opened kvstore(memory) with different schemas, check if open success.
1042 * @tc.type: FUNC
1043 * @tc.require: AR000DR9K2
1044 * @tc.author: weifeng
1045 */
1046 HWTEST_F(DistributedDBInterfacesDatabaseTest, OpenKvStoreWithSchema003, TestSize.Level1)
1047 {
1048 /**
1049 * @tc.steps: step1. create a new db(memory, non-encrypt), without schema;
1050 * @tc.expected: step1. Returns a non-null kvstore and error code is OK.
1051 */
1052 KvStoreNbDelegate::Option option = {true, true, false};
1053 g_mgr.GetKvStore("OpenKvStoreWithSchema_003", option, g_kvNbDelegateCallback);
1054 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1055 EXPECT_TRUE(g_kvDelegateStatus == OK);
1056 KvStoreNbDelegate *kvNbDelegatePtr1 = g_kvNbDelegatePtr;
1057 /**
1058 * @tc.steps: step2. open a new db(memory, non-encrypt), without schema;
1059 * @tc.expected: step2. Returns a non-null kvstore and error code is OK.
1060 */
1061 g_mgr.GetKvStore("OpenKvStoreWithSchema_003", option, g_kvNbDelegateCallback);
1062 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1063 EXPECT_TRUE(g_kvDelegateStatus == OK);
1064 KvStoreNbDelegate *kvNbDelegatePtr2 = g_kvNbDelegatePtr;
1065 /**
1066 * @tc.steps: step3. open a new db(memory, non-encrypt), with valid schema;
1067 * @tc.expected: step3. Returns a null kvstore and error code is NOT_SUPPORT.
1068 */
1069 GenerateValidSchemaString(option.schema);
1070 g_mgr.GetKvStore("OpenKvStoreWithSchema_003", option, g_kvNbDelegateCallback);
1071 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
1072 EXPECT_TRUE(g_kvDelegateStatus == NOT_SUPPORT);
1073
1074 /**
1075 * @tc.steps: step4. open a new db(memory, non-encrypt), with invalid schema;
1076 * @tc.expected: step4. Returns a null kvstore and error code is NOT_SUPPORT.
1077 */
1078 GenerateInvalidSchemaString(option.schema);
1079 g_mgr.GetKvStore("OpenKvStoreWithSchema_003", option, g_kvNbDelegateCallback);
1080 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
1081 EXPECT_TRUE(g_kvDelegateStatus == NOT_SUPPORT);
1082 EXPECT_EQ(g_mgr.CloseKvStore(kvNbDelegatePtr1), OK);
1083 EXPECT_EQ(g_mgr.CloseKvStore(kvNbDelegatePtr2), OK);
1084 }
1085
1086 /**
1087 * @tc.name: OpenKvStoreWithSchema004
1088 * @tc.desc: open a totally closed schema-kvstore(non-memory) with different schemas, check if open success.
1089 * @tc.type: FUNC
1090 * @tc.require: AR000DR9K2
1091 * @tc.author: weifeng
1092 */
1093 HWTEST_F(DistributedDBInterfacesDatabaseTest, OpenKvStoreWithSchema004, TestSize.Level1)
1094 {
1095 /**
1096 * @tc.steps: step1. open a new db(non-memory, non-encrypt), with different schemas;
1097 * @tc.expected: step1. Returns a null kvstore and error code is NOT_SUPPORT.
1098 */
1099 std::string schema;
1100 GenerateValidSchemaString(schema);
1101 OpenClosedSchemaKvStore("OpenKvStoreWithSchema_004", false, schema);
1102 #ifndef OMIT_ENCRYPT
1103 /**
1104 * @tc.steps: step2. open a new db(non-memory, encrypt), with different schemas;
1105 * @tc.expected: step2. Returns a null kvstore and error code is NOT_SUPPORT.
1106 */
1107 OpenClosedSchemaKvStore("OpenKvStoreWithSchema_004_encrypt", true, schema);
1108 #endif
1109 }
1110
1111 /**
1112 * @tc.name: OpenKvStoreWithSchema005
1113 * @tc.desc: open a totally closed non-schema-kvstore(non-memory) with different schemas, check if open success.
1114 * @tc.type: FUNC
1115 * @tc.require: AR000DR9K2
1116 * @tc.author: weifeng
1117 */
1118 HWTEST_F(DistributedDBInterfacesDatabaseTest, OpenKvStoreWithSchema005, TestSize.Level1)
1119 {
1120 /**
1121 * @tc.steps: step1. open a new db(non-memory, non-encrypt, non-schema), with different schemas;
1122 * @tc.expected: step1. Returns a different result.
1123 */
1124 OpenClosedNormalKvStore("OpenKvStoreWithSchema_005", false);
1125 #ifndef OMIT_ENCRYPT
1126 /**
1127 * @tc.steps: step2. open a new db(non-memory, encrypt, non-schema), with different schemas;
1128 * @tc.expected: step2. Returns a different result.
1129 */
1130 OpenClosedNormalKvStore("OpenKvStoreWithSchema_005", true);
1131 #endif
1132 }
1133
1134 /**
1135 * @tc.name: OpenKvStoreWithSchema006
1136 * @tc.desc: open a memory non-schema-kvstore with different schemas, check if open success.
1137 * @tc.type: FUNC
1138 * @tc.require: AR000DR9K2
1139 * @tc.author: weifeng
1140 */
1141 HWTEST_F(DistributedDBInterfacesDatabaseTest, OpenKvStoreWithSchema006, TestSize.Level1)
1142 {
1143 /**
1144 * @tc.steps: step1. open a new db(memory, non-encrypt, non-schema), without schema;
1145 * @tc.expected: step1. Returns OK.
1146 */
1147 KvStoreNbDelegate::Option option = {true, true, false};
1148 g_mgr.GetKvStore("OpenKvStoreWithSchema_006", option, g_kvNbDelegateCallback);
1149 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1150 EXPECT_TRUE(g_kvDelegateStatus == OK);
1151 /**
1152 * @tc.steps: step2. close the kvstore;
1153 * @tc.expected: step2. Returns OK.
1154 */
1155 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1156 g_kvNbDelegatePtr = nullptr;
1157
1158 /**
1159 * @tc.steps: step3. reopen the kvstore without schema;
1160 * @tc.expected: step3. Returns OK.
1161 */
1162 g_mgr.GetKvStore("OpenKvStoreWithSchema_006", option, g_kvNbDelegateCallback);
1163 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1164 EXPECT_TRUE(g_kvDelegateStatus == OK);
1165 EXPECT_TRUE(WriteValidDataIntoKvStore() == OK);
1166 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1167 g_kvNbDelegatePtr = nullptr;
1168
1169 /**
1170 * @tc.steps: step4. reopen the kvstore with valid schema;
1171 * @tc.expected: step4. Returns OK.
1172 */
1173 GenerateValidSchemaString(option.schema);
1174 g_mgr.GetKvStore("OpenKvStoreWithSchema_006", option, g_kvNbDelegateCallback);
1175 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
1176 EXPECT_TRUE(g_kvDelegateStatus == NOT_SUPPORT);
1177
1178 /**
1179 * @tc.steps: step4. reopen the kvstore with invalid schema;
1180 * @tc.expected: step4. Returns OK.
1181 */
1182 GenerateInvalidSchemaString(option.schema);
1183 g_mgr.GetKvStore("OpenKvStoreWithSchema_006", option, g_kvNbDelegateCallback);
1184 ASSERT_TRUE(g_kvNbDelegatePtr == nullptr);
1185 EXPECT_TRUE(g_kvDelegateStatus == NOT_SUPPORT);
1186 }
1187 #endif
1188 /**
1189 * @tc.name: OpenKvStoreWithStoreOnly001
1190 * @tc.desc: open the kv store with the option that createDirByStoreIdOnly is true.
1191 * @tc.type: FUNC
1192 * @tc.require: AR000DR9K2
1193 * @tc.author: wangbingquan
1194 */
1195 HWTEST_F(DistributedDBInterfacesDatabaseTest, OpenKvStoreWithStoreOnly001, TestSize.Level1)
1196 {
1197 /**
1198 * @tc.steps: step1. open the kv store with the option that createDirByStoreIdOnly is true.
1199 * @tc.expected: step1. Returns OK.
1200 */
1201 KvStoreNbDelegate::Option option;
1202 option.createDirByStoreIdOnly = true;
1203 g_mgr.GetKvStore("StoreOnly001", option, g_kvNbDelegateCallback);
1204 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1205 EXPECT_TRUE(g_kvDelegateStatus == OK);
1206 auto kvStorePtr = g_kvNbDelegatePtr;
1207 /**
1208 * @tc.steps: step2. open the same store with the option that createDirByStoreIdOnly is false.
1209 * @tc.expected: step2. Returns NOT OK.
1210 */
1211 option.createDirByStoreIdOnly = false;
1212 g_kvNbDelegatePtr = nullptr;
1213 g_mgr.GetKvStore("StoreOnly001", option, g_kvNbDelegateCallback);
1214 EXPECT_EQ(g_kvDelegateStatus, INVALID_ARGS);
1215 /**
1216 * @tc.steps: step3. close the kvstore and delete the kv store;
1217 * @tc.expected: step3. Returns OK.
1218 */
1219 EXPECT_EQ(g_mgr.CloseKvStore(kvStorePtr), OK);
1220 kvStorePtr = nullptr;
1221 EXPECT_EQ(g_mgr.DeleteKvStore("StoreOnly001"), OK);
1222 }
1223
1224 /**
1225 * @tc.name: GetDBWhileOpened001
1226 * @tc.desc: open the kv store with the option that createDirByStoreIdOnly is true.
1227 * @tc.type: FUNC
1228 * @tc.require: AR000E8S2V
1229 * @tc.author: wangbingquan
1230 */
1231 HWTEST_F(DistributedDBInterfacesDatabaseTest, GetDBWhileOpened001, TestSize.Level1)
1232 {
1233 /**
1234 * @tc.steps: step1. Get the connection.
1235 * @tc.expected: step1. Returns OK.
1236 */
1237 KvDBProperties property;
1238 std::string storeId = "openTest";
1239 std::string origId = USER_ID + "-" + APP_ID + "-" + storeId;
1240 std::string identifier = DBCommon::TransferHashString(origId);
1241 std::string hexDir = DBCommon::TransferStringToHex(identifier);
1242 property.SetStringProp(KvDBProperties::IDENTIFIER_DATA, identifier);
1243 property.SetStringProp(KvDBProperties::IDENTIFIER_DIR, hexDir);
1244 property.SetStringProp(KvDBProperties::DATA_DIR, g_testDir);
1245 property.SetBoolProp(KvDBProperties::CREATE_IF_NECESSARY, true);
1246 property.SetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::SINGLE_VER_TYPE);
1247 property.SetBoolProp(KvDBProperties::MEMORY_MODE, false);
1248 property.SetBoolProp(KvDBProperties::ENCRYPTED_MODE, false);
1249 property.SetBoolProp(KvDBProperties::CREATE_DIR_BY_STORE_ID_ONLY, true);
1250 property.SetStringProp(KvDBProperties::APP_ID, APP_ID);
1251 property.SetStringProp(KvDBProperties::USER_ID, USER_ID);
1252 property.SetStringProp(KvDBProperties::APP_ID, storeId);
1253
1254 int errCode = E_OK;
1255 auto connection1 = KvDBManager::GetDatabaseConnection(property, errCode, false);
1256 EXPECT_EQ(errCode, E_OK);
1257 /**
1258 * @tc.steps: step2. Get the connection with the para: isNeedIfOpened is false.
1259 * @tc.expected: step2. Returns -E_ALREADY_OPENED.
1260 */
1261 auto connection2 = KvDBManager::GetDatabaseConnection(property, errCode, false);
1262 EXPECT_EQ(errCode, -E_ALREADY_OPENED);
1263 EXPECT_EQ(connection2, nullptr);
1264
1265 /**
1266 * @tc.steps: step3. Get the connection with the para: isNeedIfOpened is true.
1267 * @tc.expected: step3. Returns E_OK.
1268 */
1269 auto connection3 = KvDBManager::GetDatabaseConnection(property, errCode, true);
1270 EXPECT_EQ(errCode, OK);
1271 EXPECT_NE(connection3, nullptr);
1272
1273 KvDBManager::ReleaseDatabaseConnection(connection1);
1274 KvDBManager::ReleaseDatabaseConnection(connection3);
1275 EXPECT_EQ(g_mgr.DeleteKvStore(storeId), OK);
1276 }
1277 namespace {
OpenCloseDatabase(const std::string & storeId)1278 void OpenCloseDatabase(const std::string &storeId)
1279 {
1280 KvStoreNbDelegate::Option option;
1281 DBStatus status;
1282 KvStoreNbDelegate *delegate = nullptr;
1283 auto nbDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback,
1284 placeholders::_1, placeholders::_2, std::ref(status), std::ref(delegate));
1285 int totalNum = 0;
1286 for (size_t i = 0; i < 100; i++) { // cycle 100 times.
1287 g_mgr.GetKvStore(storeId, option, nbDelegateCallback);
1288 if (delegate != nullptr) {
1289 totalNum++;
1290 }
1291 g_mgr.CloseKvStore(delegate);
1292 delegate = nullptr;
1293 std::this_thread::sleep_for(std::chrono::milliseconds(1));
1294 }
1295 LOGD("Succeed %d times", totalNum);
1296 }
1297 }
1298
1299 /**
1300 * @tc.name: FreqOpenCloseDel001
1301 * @tc.desc: Open/close/delete the kv store concurrently.
1302 * @tc.type: FUNC
1303 * @tc.require: AR000DR9K2
1304 * @tc.author: wangbingquan
1305 */
1306 HWTEST_F(DistributedDBInterfacesDatabaseTest, FreqOpenCloseDel001, TestSize.Level2)
1307 {
1308 std::string storeId = "FrqOpenCloseDelete001";
1309 std::thread t1(OpenCloseDatabase, storeId);
__anone8636dd80402() 1310 std::thread t2([&]() {
1311 for (int i = 0; i < 10000; i++) {
1312 g_mgr.DeleteKvStore(storeId);
1313 }
1314 });
1315 t1.join();
1316 t2.join();
1317 }
1318
1319 /**
1320 * @tc.name: FreqOpenClose001
1321 * @tc.desc: Open and close the kv store concurrently.
1322 * @tc.type: FUNC
1323 * @tc.require: AR000DR9K2
1324 * @tc.author: wangbingquan
1325 */
1326 HWTEST_F(DistributedDBInterfacesDatabaseTest, FreqOpenClose001, TestSize.Level2)
1327 {
1328 std::string storeId = "FrqOpenClose001";
1329 std::thread t1(OpenCloseDatabase, storeId);
1330 std::thread t2(OpenCloseDatabase, storeId);
1331 std::thread t3(OpenCloseDatabase, storeId);
1332 std::thread t4(OpenCloseDatabase, storeId);
1333 t1.join();
1334 t2.join();
1335 t3.join();
1336 t4.join();
1337 EXPECT_EQ(g_mgr.DeleteKvStore(storeId), OK);
1338 }
1339
1340 /**
1341 * @tc.name: CheckKvStoreDir001
1342 * @tc.desc: Delete the kv store with the option that createDirByStoreIdOnly is true.
1343 * @tc.type: FUNC
1344 * @tc.require: AR000CQDV7
1345 * @tc.author: wangbingquan
1346 */
1347 HWTEST_F(DistributedDBInterfacesDatabaseTest, CheckKvStoreDir001, TestSize.Level1)
1348 {
1349 /**
1350 * @tc.steps: step1. open the kv store with the option that createDirByStoreIdOnly is true.
1351 * @tc.expected: step1. Returns OK.
1352 */
1353 KvStoreNbDelegate::Option option;
1354 option.createDirByStoreIdOnly = true;
1355 const std::string storeId("StoreOnly002");
1356 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
1357 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1358 EXPECT_TRUE(g_kvDelegateStatus == OK);
1359 std::string testSubDir;
1360 EXPECT_EQ(KvStoreDelegateManager::GetDatabaseDir(storeId, testSubDir), OK);
1361 std::string dataBaseDir = g_testDir + "/" + testSubDir;
1362 EXPECT_GE(access(dataBaseDir.c_str(), F_OK), 0);
1363
1364 /**
1365 * @tc.steps: step2. delete the kv store, and check the directory.
1366 * @tc.expected: step2. the directory is removed.
1367 */
1368 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
1369 g_kvNbDelegatePtr = nullptr;
1370 EXPECT_EQ(g_mgr.DeleteKvStore(storeId), OK);
1371 LOGI("[%s]", dataBaseDir.c_str());
1372 ASSERT_EQ(OS::CheckPathExistence(dataBaseDir), false);
1373 }
1374
1375 /**
1376 * @tc.name: CompressionRate1
1377 * @tc.desc: Open the kv store with invalid compressionRate and open successfully.
1378 * @tc.type: FUNC
1379 * @tc.require: AR000G3QTT
1380 * @tc.author: lidongwei
1381 */
1382 HWTEST_F(DistributedDBInterfacesDatabaseTest, CompressionRate1, TestSize.Level1)
1383 {
1384 /**
1385 * @tc.steps: step1. Open the kv store with the option that comressionRate is invalid.
1386 * @tc.expected: step1. Open kv store successfully. Returns OK.
1387 */
1388 KvStoreNbDelegate::Option option;
1389 option.isNeedCompressOnSync = true;
1390 option.compressionRate = 0; // 0 is invalid.
1391 const std::string storeId("CompressionRate1");
1392 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
1393 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1394 EXPECT_TRUE(g_kvDelegateStatus == OK);
1395
1396 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
1397 g_kvNbDelegatePtr = nullptr;
1398 EXPECT_EQ(g_mgr.DeleteKvStore(storeId), OK);
1399 }
1400
1401 /**
1402 * @tc.name: CompressionRate2
1403 * @tc.desc: Open the kv store with again with different compression option.
1404 * @tc.type: FUNC
1405 * @tc.require:
1406 * @tc.author: lianhuix
1407 */
1408 HWTEST_F(DistributedDBInterfacesDatabaseTest, CompressionRate2, TestSize.Level1)
1409 {
1410 /**
1411 * @tc.steps: step1. Open the kv store with the option that comressionRate is invalid.
1412 * @tc.expected: step1. Open kv store successfully. Returns OK.
1413 */
1414 KvStoreNbDelegate::Option option;
1415 option.isNeedCompressOnSync = true;
1416 option.compressionRate = 70; // 70 compression rate.
1417 const std::string storeId("CompressionRate1");
1418 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
1419 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1420 EXPECT_TRUE(g_kvDelegateStatus == OK);
1421
1422 /**
1423 * @tc.steps: step2. Open again with different compression option
1424 * @tc.expected: step2. Open kv store failed. Returns INVALID_ARGS.
1425 */
1426 DBStatus status;
1427 KvStoreNbDelegate *delegate = nullptr;
1428 auto callback = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback,
1429 placeholders::_1, placeholders::_2, std::ref(status), std::ref(delegate));
1430
1431 option.compressionRate = 80; // 80 compression rate.
1432 g_mgr.GetKvStore(storeId, option, callback);
1433 ASSERT_TRUE(delegate == nullptr);
1434 EXPECT_TRUE(status == INVALID_ARGS);
1435
1436 option.isNeedCompressOnSync = false;
1437 option.compressionRate = 70; // 70 compression rate.
1438 g_mgr.GetKvStore(storeId, option, callback);
1439 ASSERT_TRUE(delegate == nullptr);
1440 EXPECT_TRUE(status == INVALID_ARGS);
1441
1442 /**
1443 * @tc.steps: step3. Close kv store
1444 * @tc.expected: step3. OK.
1445 */
1446 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
1447 g_kvNbDelegatePtr = nullptr;
1448 EXPECT_EQ(g_mgr.DeleteKvStore(storeId), OK);
1449 }
1450
1451 HWTEST_F(DistributedDBInterfacesDatabaseTest, DataInterceptor1, TestSize.Level1)
1452 {
1453 /**
1454 * @tc.steps: step1. Open the kv store with the option that comressionRate is invalid.
1455 * @tc.expected: step1. Open kv store successfully. Returns OK.
1456 */
1457 KvStoreNbDelegate::Option option;
1458 const std::string storeId("DataInterceptor1");
1459 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
1460 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1461 EXPECT_TRUE(g_kvDelegateStatus == OK);
1462
1463 g_kvNbDelegatePtr->SetPushDataInterceptor(
__anone8636dd80502(InterceptedData &data, const std::string &sourceID, const std::string &targetID) 1464 [](InterceptedData &data, const std::string &sourceID, const std::string &targetID) {
1465 int errCode = OK;
1466 auto entries = data.GetEntries();
1467 for (size_t i = 0; i < entries.size(); i++) {
1468 if (entries[i].key.empty() || entries[i].key.at(0) != 'A') {
1469 continue;
1470 }
1471 auto newKey = entries[i].key;
1472 newKey[0] = 'B';
1473 errCode = data.ModifyKey(i, newKey);
1474 if (errCode != OK) {
1475 break;
1476 }
1477 }
1478 return errCode;
1479 }
1480 );
1481
1482 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
1483 g_kvNbDelegatePtr = nullptr;
1484 EXPECT_EQ(g_mgr.DeleteKvStore(storeId), OK);
1485 }