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