• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <condition_variable>
16 #include <gtest/gtest.h>
17 #include <vector>
18 
19 #include "block_data.h"
20 #include "dev_manager.h"
21 #include "distributed_kv_data_manager.h"
22 #include "store_manager.h"
23 #include "sys/stat.h"
24 #include "types.h"
25 using namespace testing::ext;
26 using namespace OHOS::DistributedKv;
27 class SingleStoreImplTest : public testing::Test {
28 public:
29     class TestObserver : public KvStoreObserver {
30     public:
TestObserver()31         TestObserver()
32         {
33             data_ = std::make_shared<OHOS::BlockData<bool>>(5, false);
34         }
OnChange(const ChangeNotification & notification)35         void OnChange(const ChangeNotification &notification) override
36         {
37             insert_ = notification.GetInsertEntries();
38             update_ = notification.GetUpdateEntries();
39             delete_ = notification.GetDeleteEntries();
40             deviceId_ = notification.GetDeviceId();
41             bool value = true;
42             data_->SetValue(value);
43         }
44         std::vector<Entry> insert_;
45         std::vector<Entry> update_;
46         std::vector<Entry> delete_;
47         std::string deviceId_;
48 
49         std::shared_ptr<OHOS::BlockData<bool>> data_;
50     };
51 
52     static void SetUpTestCase(void);
53     static void TearDownTestCase(void);
54     void SetUp();
55     void TearDown();
56 
57     std::shared_ptr<SingleKvStore> CreateKVStore(std::string storeIdTest, KvStoreType type, bool encrypt, bool backup);
58     std::shared_ptr<SingleKvStore> kvStore_;
59 };
60 
SetUpTestCase(void)61 void SingleStoreImplTest::SetUpTestCase(void)
62 {
63     std::string baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
64     mkdir(baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
65 }
66 
TearDownTestCase(void)67 void SingleStoreImplTest::TearDownTestCase(void)
68 {
69     std::string baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
70     StoreManager::GetInstance().Delete({ "SingleStoreImplTest" }, { "SingleKVStore" }, baseDir);
71 
72     (void)remove("/data/service/el1/public/database/SingleStoreImplTest/key");
73     (void)remove("/data/service/el1/public/database/SingleStoreImplTest/kvdb");
74     (void)remove("/data/service/el1/public/database/SingleStoreImplTest");
75 }
76 
SetUp(void)77 void SingleStoreImplTest::SetUp(void)
78 {
79     kvStore_ = CreateKVStore("SingleKVStore", SINGLE_VERSION, false, true);
80     if (kvStore_ == nullptr) {
81         kvStore_ = CreateKVStore("SingleKVStore", SINGLE_VERSION, false, true);
82     }
83     ASSERT_NE(kvStore_, nullptr);
84 }
85 
TearDown(void)86 void SingleStoreImplTest::TearDown(void)
87 {
88     AppId appId = { "SingleStoreImplTest" };
89     StoreId storeId = { "SingleKVStore" };
90     kvStore_ = nullptr;
91     auto status = StoreManager::GetInstance().CloseKVStore(appId, storeId);
92     ASSERT_EQ(status, SUCCESS);
93     auto baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
94     status = StoreManager::GetInstance().Delete(appId, storeId, baseDir);
95     ASSERT_EQ(status, SUCCESS);
96 }
97 
CreateKVStore(std::string storeIdTest,KvStoreType type,bool encrypt,bool backup)98 std::shared_ptr<SingleKvStore> SingleStoreImplTest::CreateKVStore(std::string storeIdTest, KvStoreType type,
99     bool encrypt, bool backup)
100 {
101     Options options;
102     options.kvStoreType = type;
103     options.securityLevel = S1;
104     options.encrypt = encrypt;
105     options.area = EL1;
106     options.backup = backup;
107     options.baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
108 
109     AppId appId = { "SingleStoreImplTest" };
110     StoreId storeId = { storeIdTest };
111     Status status = StoreManager::GetInstance().Delete(appId, storeId, options.baseDir);
112     return StoreManager::GetInstance().GetKVStore(appId, storeId, options, status);
113 }
114 
115 /**
116  * @tc.name: GetStoreId
117  * @tc.desc: get the store id of the kv store
118  * @tc.type: FUNC
119  * @tc.require: I4XVQQ
120  * @tc.author: Sven Wang
121  */
122 HWTEST_F(SingleStoreImplTest, GetStoreId, TestSize.Level0)
123 {
124     ASSERT_NE(kvStore_, nullptr);
125     auto storeId = kvStore_->GetStoreId();
126     ASSERT_EQ(storeId.storeId, "SingleKVStore");
127 }
128 
129 /**
130  * @tc.name: Put
131  * @tc.desc: put key-value data to the kv store
132  * @tc.type: FUNC
133  * @tc.require: I4XVQQ
134  * @tc.author: Sven Wang
135  */
136 HWTEST_F(SingleStoreImplTest, Put, TestSize.Level0)
137 {
138     ASSERT_NE(kvStore_, nullptr);
139     auto status = kvStore_->Put({ "Put Test" }, { "Put Value" });
140     ASSERT_EQ(status, SUCCESS);
141     status = kvStore_->Put({ "   Put Test" }, { "Put2 Value" });
142     ASSERT_EQ(status, SUCCESS);
143     Value value;
144     status = kvStore_->Get({ "Put Test" }, value);
145     ASSERT_EQ(status, SUCCESS);
146     ASSERT_EQ(value.ToString(), "Put2 Value");
147 }
148 
149 /**
150  * @tc.name: Put_Invalid_Key
151  * @tc.desc: put invalid key-value data to the device kv store and single kv store
152  * @tc.type: FUNC
153  * @tc.require: I4XVQQ
154  * @tc.author: wu fengshan
155  */
156 HWTEST_F(SingleStoreImplTest, Put_Invalid_Key, TestSize.Level0)
157 {
158     std::shared_ptr<SingleKvStore> kvStore;
159     AppId appId = { "SingleStoreImplTest" };
160     StoreId storeId = { "DeviceKVStore" };
161     kvStore = CreateKVStore(storeId.storeId, DEVICE_COLLABORATION, false, true);
162     ASSERT_NE(kvStore, nullptr);
163 
164     size_t MAX_DEV_KEY_LEN = 897;
165     std::string str(MAX_DEV_KEY_LEN, 'a');
166     Blob key(str);
167     Blob value("test_value");
168     Status status = kvStore->Put(key, value);
169     EXPECT_EQ(status, INVALID_ARGUMENT);
170 
171     Blob key1("");
172     Blob value1("test_value1");
173     status = kvStore->Put(key1, value1);
174     EXPECT_EQ(status, INVALID_ARGUMENT);
175 
176     kvStore = nullptr;
177     status = StoreManager::GetInstance().CloseKVStore(appId, storeId);
178     ASSERT_EQ(status, SUCCESS);
179     std::string baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
180     status = StoreManager::GetInstance().Delete(appId, storeId, baseDir);
181     ASSERT_EQ(status, SUCCESS);
182 
183     size_t MAX_SINGLE_KEY_LEN = 1025;
184     std::string str1(MAX_SINGLE_KEY_LEN, 'b');
185     Blob key2(str1);
186     Blob value2("test_value2");
187     status = kvStore_->Put(key2, value2);
188     EXPECT_EQ(status, INVALID_ARGUMENT);
189 
190     status = kvStore_->Put(key1, value1);
191     EXPECT_EQ(status, INVALID_ARGUMENT);
192 }
193 
194 /**
195  * @tc.name: PutBatch
196  * @tc.desc: put some key-value data to the kv store
197  * @tc.type: FUNC
198  * @tc.require: I4XVQQ
199  * @tc.author: Sven Wang
200  */
201 HWTEST_F(SingleStoreImplTest, PutBatch, TestSize.Level0)
202 {
203     ASSERT_NE(kvStore_, nullptr);
204     std::vector<Entry> entries;
205     for (int i = 0; i < 10; ++i) {
206         Entry entry;
207         entry.key = std::to_string(i).append("_k");
208         entry.value = std::to_string(i).append("_v");
209         entries.push_back(entry);
210     }
211     auto status = kvStore_->PutBatch(entries);
212     ASSERT_EQ(status, SUCCESS);
213 }
214 
215 /**
216  * @tc.name: Delete
217  * @tc.desc: delete the value of the key
218  * @tc.type: FUNC
219  * @tc.require: I4XVQQ
220  * @tc.author: Sven Wang
221  */
222 HWTEST_F(SingleStoreImplTest, Delete, TestSize.Level0)
223 {
224     ASSERT_NE(kvStore_, nullptr);
225     auto status = kvStore_->Put({ "Put Test" }, { "Put Value" });
226     ASSERT_EQ(status, SUCCESS);
227     Value value;
228     status = kvStore_->Get({ "Put Test" }, value);
229     ASSERT_EQ(status, SUCCESS);
230     ASSERT_EQ(std::string("Put Value"), value.ToString());
231     status = kvStore_->Delete({ "Put Test" });
232     ASSERT_EQ(status, SUCCESS);
233     value = {};
234     status = kvStore_->Get({ "Put Test" }, value);
235     ASSERT_EQ(status, KEY_NOT_FOUND);
236     ASSERT_EQ(std::string(""), value.ToString());
237 }
238 
239 /**
240  * @tc.name: DeleteBatch
241  * @tc.desc: delete the values of the keys
242  * @tc.type: FUNC
243  * @tc.require: I4XVQQ
244  * @tc.author: Sven Wang
245  */
246 HWTEST_F(SingleStoreImplTest, DeleteBatch, TestSize.Level0)
247 {
248     ASSERT_NE(kvStore_, nullptr);
249     std::vector<Entry> entries;
250     for (int i = 0; i < 10; ++i) {
251         Entry entry;
252         entry.key = std::to_string(i).append("_k");
253         entry.value = std::to_string(i).append("_v");
254         entries.push_back(entry);
255     }
256     auto status = kvStore_->PutBatch(entries);
257     ASSERT_EQ(status, SUCCESS);
258     std::vector<Key> keys;
259     for (int i = 0; i < 10; ++i) {
260         Key key = std::to_string(i).append("_k");
261         keys.push_back(key);
262     }
263     status = kvStore_->DeleteBatch(keys);
264     ASSERT_EQ(status, SUCCESS);
265     for (int i = 0; i < 10; ++i) {
266         Value value;
267         status = kvStore_->Get(keys[i], value);
268         ASSERT_EQ(status, KEY_NOT_FOUND);
269         ASSERT_EQ(value.ToString(), std::string(""));
270     }
271 }
272 
273 /**
274  * @tc.name: Transaction
275  * @tc.desc: do transaction
276  * @tc.type: FUNC
277  * @tc.require: I4XVQQ
278  * @tc.author: Sven Wang
279  */
280 HWTEST_F(SingleStoreImplTest, Transaction, TestSize.Level0)
281 {
282     ASSERT_NE(kvStore_, nullptr);
283     auto status = kvStore_->StartTransaction();
284     ASSERT_EQ(status, SUCCESS);
285     status = kvStore_->Commit();
286     ASSERT_EQ(status, SUCCESS);
287 
288     status = kvStore_->StartTransaction();
289     ASSERT_EQ(status, SUCCESS);
290     status = kvStore_->Rollback();
291     ASSERT_EQ(status, SUCCESS);
292 }
293 
294 /**
295  * @tc.name: SubscribeKvStore
296  * @tc.desc: subscribe local
297  * @tc.type: FUNC
298  * @tc.require: I4XVQQ
299  * @tc.author: Sven Wang
300  */
301 HWTEST_F(SingleStoreImplTest, SubscribeKvStore, TestSize.Level0)
302 {
303     ASSERT_NE(kvStore_, nullptr);
304     auto observer = std::make_shared<TestObserver>();
305     auto status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_LOCAL, observer);
306     ASSERT_EQ(status, SUCCESS);
307     status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_REMOTE, observer);
308     ASSERT_EQ(status, SUCCESS);
309     status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_LOCAL, observer);
310     ASSERT_EQ(status, STORE_ALREADY_SUBSCRIBE);
311     status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_REMOTE, observer);
312     ASSERT_EQ(status, STORE_ALREADY_SUBSCRIBE);
313     status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_ALL, observer);
314     ASSERT_EQ(status, STORE_ALREADY_SUBSCRIBE);
315     status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_ALL, observer);
316     ASSERT_EQ(status, STORE_ALREADY_SUBSCRIBE);
317     bool invalidValue = false;
318     observer->data_->Clear(invalidValue);
319     status = kvStore_->Put({ "Put Test" }, { "Put Value" });
320     ASSERT_EQ(status, SUCCESS);
321     ASSERT_TRUE(observer->data_->GetValue());
322     ASSERT_EQ(observer->insert_.size(), 1);
323     ASSERT_EQ(observer->update_.size(), 0);
324     ASSERT_EQ(observer->delete_.size(), 0);
325     observer->data_->Clear(invalidValue);
326     status = kvStore_->Put({ "Put Test" }, { "Put Value1" });
327     ASSERT_EQ(status, SUCCESS);
328     ASSERT_TRUE(observer->data_->GetValue());
329     ASSERT_EQ(observer->insert_.size(), 0);
330     ASSERT_EQ(observer->update_.size(), 1);
331     ASSERT_EQ(observer->delete_.size(), 0);
332     observer->data_->Clear(invalidValue);
333     status = kvStore_->Delete({ "Put Test" });
334     ASSERT_EQ(status, SUCCESS);
335     ASSERT_TRUE(observer->data_->GetValue());
336     ASSERT_EQ(observer->insert_.size(), 0);
337     ASSERT_EQ(observer->update_.size(), 0);
338     ASSERT_EQ(observer->delete_.size(), 1);
339 }
340 
341 /**
342  * @tc.name: SubscribeKvStore002
343  * @tc.desc: subscribe local
344  * @tc.type: FUNC
345  * @tc.require: I4XVQQ
346  * @tc.author: Hollokin
347  */
348 HWTEST_F(SingleStoreImplTest, SubscribeKvStore002, TestSize.Level0)
349 {
350     ASSERT_NE(kvStore_, nullptr);
351     std::shared_ptr<TestObserver> subscribedObserver;
352     std::shared_ptr<TestObserver> unSubscribedObserver;
353     for (int i = 0; i < 15; ++i) {
354         auto observer = std::make_shared<TestObserver>();
355         auto status1 = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_LOCAL, observer);
356         auto status2 = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_REMOTE, observer);
357         if (i < 8) {
358             ASSERT_EQ(status1, SUCCESS);
359             ASSERT_EQ(status2, SUCCESS);
360             subscribedObserver = observer;
361         } else {
362             ASSERT_EQ(status1, OVER_MAX_SUBSCRIBE_LIMITS);
363             ASSERT_EQ(status2, OVER_MAX_SUBSCRIBE_LIMITS);
364             unSubscribedObserver = observer;
365         }
366     }
367 
368     auto status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_LOCAL, subscribedObserver);
369     ASSERT_EQ(status, STORE_ALREADY_SUBSCRIBE);
370 
371     status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_LOCAL, {});
372     ASSERT_EQ(status, INVALID_ARGUMENT);
373 
374     status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_REMOTE, subscribedObserver);
375     ASSERT_EQ(status, STORE_ALREADY_SUBSCRIBE);
376 
377     status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_ALL, subscribedObserver);
378     ASSERT_EQ(status, STORE_ALREADY_SUBSCRIBE);
379 
380     status = kvStore_->UnSubscribeKvStore(SUBSCRIBE_TYPE_LOCAL, subscribedObserver);
381     ASSERT_EQ(status, SUCCESS);
382     status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_LOCAL, subscribedObserver);
383     ASSERT_EQ(status, SUCCESS);
384 
385     status = kvStore_->UnSubscribeKvStore(SUBSCRIBE_TYPE_ALL, subscribedObserver);
386     ASSERT_EQ(status, SUCCESS);
387     status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_LOCAL, unSubscribedObserver);
388     ASSERT_EQ(status, SUCCESS);
389     subscribedObserver = unSubscribedObserver;
390     status = kvStore_->UnSubscribeKvStore(SUBSCRIBE_TYPE_LOCAL, subscribedObserver);
391     ASSERT_EQ(status, SUCCESS);
392     auto observer = std::make_shared<TestObserver>();
393     status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_LOCAL, observer);
394     ASSERT_EQ(status, SUCCESS);
395     status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_ALL, observer);
396     ASSERT_EQ(status, SUCCESS);
397     observer = std::make_shared<TestObserver>();
398     status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_ALL, observer);
399     ASSERT_EQ(status, OVER_MAX_SUBSCRIBE_LIMITS);
400 }
401 
402 /**
403  * @tc.name: UnsubscribeKvStore
404  * @tc.desc: unsubscribe
405  * @tc.type: FUNC
406  * @tc.require: I4XVQQ
407  * @tc.author: Sven Wang
408  */
409 HWTEST_F(SingleStoreImplTest, UnsubscribeKvStore, TestSize.Level0)
410 {
411     ASSERT_NE(kvStore_, nullptr);
412     auto observer = std::make_shared<TestObserver>();
413     auto status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_ALL, observer);
414     ASSERT_EQ(status, SUCCESS);
415     status = kvStore_->UnSubscribeKvStore(SUBSCRIBE_TYPE_REMOTE, observer);
416     ASSERT_EQ(status, SUCCESS);
417     status = kvStore_->UnSubscribeKvStore(SUBSCRIBE_TYPE_REMOTE, observer);
418     ASSERT_EQ(status, STORE_NOT_SUBSCRIBE);
419     status = kvStore_->UnSubscribeKvStore(SUBSCRIBE_TYPE_LOCAL, observer);
420     ASSERT_EQ(status, SUCCESS);
421     status = kvStore_->UnSubscribeKvStore(SUBSCRIBE_TYPE_LOCAL, observer);
422     ASSERT_EQ(status, STORE_NOT_SUBSCRIBE);
423     status = kvStore_->UnSubscribeKvStore(SUBSCRIBE_TYPE_ALL, observer);
424     ASSERT_EQ(status, STORE_NOT_SUBSCRIBE);
425     status = kvStore_->SubscribeKvStore(SUBSCRIBE_TYPE_LOCAL, observer);
426     ASSERT_EQ(status, SUCCESS);
427     status = kvStore_->UnSubscribeKvStore(SUBSCRIBE_TYPE_ALL, observer);
428     ASSERT_EQ(status, SUCCESS);
429 }
430 
431 /**
432  * @tc.name: GetEntries
433  * @tc.desc: get entries by prefix
434  * @tc.type: FUNC
435  * @tc.require: I4XVQQ
436  * @tc.author: Sven Wang
437  */
438 HWTEST_F(SingleStoreImplTest, GetEntries_Prefix, TestSize.Level0)
439 {
440     ASSERT_NE(kvStore_, nullptr);
441     std::vector<Entry> input;
442     for (int i = 0; i < 10; ++i) {
443         Entry entry;
444         entry.key = std::to_string(i).append("_k");
445         entry.value = std::to_string(i).append("_v");
446         input.push_back(entry);
447     }
448     auto status = kvStore_->PutBatch(input);
449     ASSERT_EQ(status, SUCCESS);
450     std::vector<Entry> output;
451     status = kvStore_->GetEntries({ "" }, output);
452     ASSERT_EQ(status, SUCCESS);
453     std::sort(output.begin(), output.end(),
__anon2048b17a0102(const Entry &entry, const Entry &sentry) 454         [](const Entry &entry, const Entry &sentry) { return entry.key.Data() < sentry.key.Data(); });
455     for (int i = 0; i < 10; ++i) {
456         ASSERT_TRUE(input[i].key == output[i].key);
457         ASSERT_TRUE(input[i].value == output[i].value);
458     }
459 }
460 
461 /**
462  * @tc.name: GetEntries_Less_Prefix
463  * @tc.desc: get entries by prefix and the key size less than sizeof(uint32_t)
464  * @tc.type: FUNC
465  * @tc.require: I4XVQQ
466  * @tc.author: wu fengshan
467  */
468 HWTEST_F(SingleStoreImplTest, GetEntries_Less_Prefix, TestSize.Level0)
469 {
470     std::shared_ptr<SingleKvStore> kvStore;
471     AppId appId = { "SingleStoreImplTest" };
472     StoreId storeId = { "DeviceKVStore" };
473     kvStore = CreateKVStore(storeId.storeId, DEVICE_COLLABORATION, false, true);
474     ASSERT_NE(kvStore, nullptr);
475 
476     std::vector<Entry> input;
477     for (int i = 0; i < 10; ++i) {
478         Entry entry;
479         entry.key = std::to_string(i).append("_k");
480         entry.value = std::to_string(i).append("_v");
481         input.push_back(entry);
482     }
483     auto status = kvStore->PutBatch(input);
484     ASSERT_EQ(status, SUCCESS);
485     std::vector<Entry> output;
486     status = kvStore->GetEntries({"1"}, output);
487     ASSERT_NE(output.empty(), true);
488     ASSERT_EQ(status, SUCCESS);
489 
490     kvStore = nullptr;
491     status = StoreManager::GetInstance().CloseKVStore(appId, storeId);
492     ASSERT_EQ(status, SUCCESS);
493     std::string baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
494     status = StoreManager::GetInstance().Delete(appId, storeId, baseDir);
495     ASSERT_EQ(status, SUCCESS);
496 
497     status = kvStore_->PutBatch(input);
498     ASSERT_EQ(status, SUCCESS);
499     std::vector<Entry> output1;
500     status = kvStore_->GetEntries({"1"}, output1);
501     ASSERT_NE(output1.empty(), true);
502     ASSERT_EQ(status, SUCCESS);
503 }
504 
505 /**
506  * @tc.name: GetEntries_Greater_Prefix
507  * @tc.desc: get entries by prefix and the key size is greater than  sizeof(uint32_t)
508  * @tc.type: FUNC
509  * @tc.require: I4XVQQ
510  * @tc.author: wu fengshan
511  */
512 HWTEST_F(SingleStoreImplTest, GetEntries_Greater_Prefix, TestSize.Level0)
513 {
514     std::shared_ptr<SingleKvStore> kvStore;
515     AppId appId = { "SingleStoreImplTest" };
516     StoreId storeId = { "DeviceKVStore" };
517     kvStore = CreateKVStore(storeId.storeId, DEVICE_COLLABORATION, false, true);
518     ASSERT_NE(kvStore, nullptr);
519 
520     size_t KEY_LEN = sizeof(uint32_t);
521     std::vector<Entry> input;
522     for (int i = 1; i < 10; ++i) {
523         Entry entry;
524         std::string str(KEY_LEN, i + '0');
525         entry.key = str;
526         entry.value = std::to_string(i).append("_v");
527         input.push_back(entry);
528     }
529     auto status = kvStore->PutBatch(input);
530     ASSERT_EQ(status, SUCCESS);
531     std::vector<Entry> output;
532     std::string str1(KEY_LEN, '1');
533     status = kvStore->GetEntries(str1, output);
534     ASSERT_NE(output.empty(), true);
535     ASSERT_EQ(status, SUCCESS);
536 
537     kvStore = nullptr;
538     status = StoreManager::GetInstance().CloseKVStore(appId, storeId);
539     ASSERT_EQ(status, SUCCESS);
540     std::string baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
541     status = StoreManager::GetInstance().Delete(appId, storeId, baseDir);
542     ASSERT_EQ(status, SUCCESS);
543 
544     status = kvStore_->PutBatch(input);
545     ASSERT_EQ(status, SUCCESS);
546     std::vector<Entry> output1;
547     status = kvStore_->GetEntries(str1, output1);
548     ASSERT_NE(output1.empty(), true);
549     ASSERT_EQ(status, SUCCESS);
550 }
551 
552 /**
553  * @tc.name: GetEntries
554  * @tc.desc: get entries by query
555  * @tc.type: FUNC
556  * @tc.require: I4XVQQ
557  * @tc.author: Sven Wang
558  */
559 HWTEST_F(SingleStoreImplTest, GetEntries_DataQuery, TestSize.Level0)
560 {
561     ASSERT_NE(kvStore_, nullptr);
562     std::vector<Entry> input;
563     for (int i = 0; i < 10; ++i) {
564         Entry entry;
565         entry.key = std::to_string(i).append("_k");
566         entry.value = std::to_string(i).append("_v");
567         input.push_back(entry);
568     }
569     auto status = kvStore_->PutBatch(input);
570     ASSERT_EQ(status, SUCCESS);
571     DataQuery query;
572     query.InKeys({ "0_k", "1_k" });
573     std::vector<Entry> output;
574     status = kvStore_->GetEntries(query, output);
575     ASSERT_EQ(status, SUCCESS);
576     std::sort(output.begin(), output.end(),
__anon2048b17a0202(const Entry &entry, const Entry &sentry) 577         [](const Entry &entry, const Entry &sentry) { return entry.key.Data() < sentry.key.Data(); });
578     ASSERT_LE(output.size(), 2);
579     for (size_t i = 0; i < output.size(); ++i) {
580         ASSERT_TRUE(input[i].key == output[i].key);
581         ASSERT_TRUE(input[i].value == output[i].value);
582     }
583 }
584 
585 /**
586  * @tc.name: GetResultSet
587  * @tc.desc: get result set by prefix
588  * @tc.type: FUNC
589  * @tc.require: I4XVQQ
590  * @tc.author: Sven Wang
591  */
592 HWTEST_F(SingleStoreImplTest, GetResultSet_Prefix, TestSize.Level0)
593 {
594     ASSERT_NE(kvStore_, nullptr);
595     std::vector<Entry> input;
__anon2048b17a0302(const Key &entry, const Key &sentry) 596     auto cmp = [](const Key &entry, const Key &sentry) { return entry.Data() < sentry.Data(); };
597     std::map<Key, Value, decltype(cmp)> dictionary(cmp);
598     for (int i = 0; i < 10; ++i) {
599         Entry entry;
600         entry.key = std::to_string(i).append("_k");
601         entry.value = std::to_string(i).append("_v");
602         dictionary[entry.key] = entry.value;
603         input.push_back(entry);
604     }
605     auto status = kvStore_->PutBatch(input);
606     ASSERT_EQ(status, SUCCESS);
607     std::shared_ptr<KvStoreResultSet> output;
608     status = kvStore_->GetResultSet({ "" }, output);
609     ASSERT_EQ(status, SUCCESS);
610     ASSERT_NE(output, nullptr);
611     ASSERT_EQ(output->GetCount(), 10);
612     int count = 0;
613     while (output->MoveToNext()) {
614         count++;
615         Entry entry;
616         output->GetEntry(entry);
617         ASSERT_EQ(entry.value.Data(), dictionary[entry.key].Data());
618     }
619     ASSERT_EQ(count, output->GetCount());
620 }
621 
622 /**
623  * @tc.name: GetResultSet
624  * @tc.desc: get result set by query
625  * @tc.type: FUNC
626  * @tc.require: I4XVQQ
627  * @tc.author: Sven Wang
628  */
629 HWTEST_F(SingleStoreImplTest, GetResultSet_Query, TestSize.Level0)
630 {
631     ASSERT_NE(kvStore_, nullptr);
632     std::vector<Entry> input;
__anon2048b17a0402(const Key &entry, const Key &sentry) 633     auto cmp = [](const Key &entry, const Key &sentry) { return entry.Data() < sentry.Data(); };
634     std::map<Key, Value, decltype(cmp)> dictionary(cmp);
635     for (int i = 0; i < 10; ++i) {
636         Entry entry;
637         entry.key = std::to_string(i).append("_k");
638         entry.value = std::to_string(i).append("_v");
639         dictionary[entry.key] = entry.value;
640         input.push_back(entry);
641     }
642     auto status = kvStore_->PutBatch(input);
643     ASSERT_EQ(status, SUCCESS);
644     DataQuery query;
645     query.InKeys({ "0_k", "1_k" });
646     std::shared_ptr<KvStoreResultSet> output;
647     status = kvStore_->GetResultSet(query, output);
648     ASSERT_EQ(status, SUCCESS);
649     ASSERT_NE(output, nullptr);
650     ASSERT_LE(output->GetCount(), 2);
651     int count = 0;
652     while (output->MoveToNext()) {
653         count++;
654         Entry entry;
655         output->GetEntry(entry);
656         ASSERT_EQ(entry.value.Data(), dictionary[entry.key].Data());
657     }
658     ASSERT_EQ(count, output->GetCount());
659 }
660 
661 /**
662  * @tc.name: CloseResultSet
663  * @tc.desc: close the result set
664  * @tc.type: FUNC
665  * @tc.require: I4XVQQ
666  * @tc.author: Sven Wang
667  */
668 HWTEST_F(SingleStoreImplTest, CloseResultSet, TestSize.Level0)
669 {
670     ASSERT_NE(kvStore_, nullptr);
671     std::vector<Entry> input;
__anon2048b17a0502(const Key &entry, const Key &sentry) 672     auto cmp = [](const Key &entry, const Key &sentry) { return entry.Data() < sentry.Data(); };
673     std::map<Key, Value, decltype(cmp)> dictionary(cmp);
674     for (int i = 0; i < 10; ++i) {
675         Entry entry;
676         entry.key = std::to_string(i).append("_k");
677         entry.value = std::to_string(i).append("_v");
678         dictionary[entry.key] = entry.value;
679         input.push_back(entry);
680     }
681     auto status = kvStore_->PutBatch(input);
682     ASSERT_EQ(status, SUCCESS);
683     DataQuery query;
684     query.InKeys({ "0_k", "1_k" });
685     std::shared_ptr<KvStoreResultSet> output;
686     status = kvStore_->GetResultSet(query, output);
687     ASSERT_EQ(status, SUCCESS);
688     ASSERT_NE(output, nullptr);
689     ASSERT_LE(output->GetCount(), 2);
690     auto outputTmp = output;
691     status = kvStore_->CloseResultSet(output);
692     ASSERT_EQ(status, SUCCESS);
693     ASSERT_EQ(output, nullptr);
694     ASSERT_EQ(outputTmp->GetCount(), KvStoreResultSet::INVALID_COUNT);
695     ASSERT_EQ(outputTmp->GetPosition(), KvStoreResultSet::INVALID_POSITION);
696     ASSERT_EQ(outputTmp->MoveToFirst(), false);
697     ASSERT_EQ(outputTmp->MoveToLast(), false);
698     ASSERT_EQ(outputTmp->MoveToNext(), false);
699     ASSERT_EQ(outputTmp->MoveToPrevious(), false);
700     ASSERT_EQ(outputTmp->Move(1), false);
701     ASSERT_EQ(outputTmp->MoveToPosition(1), false);
702     ASSERT_EQ(outputTmp->IsFirst(), false);
703     ASSERT_EQ(outputTmp->IsLast(), false);
704     ASSERT_EQ(outputTmp->IsBeforeFirst(), false);
705     ASSERT_EQ(outputTmp->IsAfterLast(), false);
706     Entry entry;
707     ASSERT_EQ(outputTmp->GetEntry(entry), ALREADY_CLOSED);
708 }
709 
710 /**
711  * @tc.name: Move_Offset
712  * @tc.desc: Move the ResultSet Relative Distance
713  * @tc.type: FUNC
714  * @tc.require: I4XVQQ
715  * @tc.author: wu fengshan
716  */
717 HWTEST_F(SingleStoreImplTest, Move_Offset, TestSize.Level0)
718 {
719     std::vector<Entry> input;
720     for (int i = 0; i < 10; ++i) {
721         Entry entry;
722         entry.key = std::to_string(i).append("_k");
723         entry.value = std::to_string(i).append("_v");
724         input.push_back(entry);
725     }
726     auto status = kvStore_->PutBatch(input);
727     ASSERT_EQ(status, SUCCESS);
728 
729     Key prefix = "2";
730     std::shared_ptr<KvStoreResultSet> output;
731     status = kvStore_->GetResultSet(prefix, output);
732     ASSERT_EQ(status, SUCCESS);
733     ASSERT_NE(output, nullptr);
734 
735     auto outputTmp = output;
736     ASSERT_EQ(outputTmp->Move(1), true);
737     status = kvStore_->CloseResultSet(output);
738     ASSERT_EQ(status, SUCCESS);
739     ASSERT_EQ(output, nullptr);
740 
741     std::shared_ptr<SingleKvStore> kvStore;
742     AppId appId = { "SingleStoreImplTest" };
743     StoreId storeId = { "DeviceKVStore" };
744     kvStore = CreateKVStore(storeId.storeId, DEVICE_COLLABORATION, false, true);
745     ASSERT_NE(kvStore, nullptr);
746 
747     status = kvStore->PutBatch(input);
748     ASSERT_EQ(status, SUCCESS);
749     std::shared_ptr<KvStoreResultSet> output1;
750     status = kvStore->GetResultSet(prefix, output1);
751     ASSERT_EQ(status, SUCCESS);
752     ASSERT_NE(output1, nullptr);
753     auto outputTmp1 = output1;
754     ASSERT_EQ(outputTmp1->Move(1), true);
755     status = kvStore->CloseResultSet(output1);
756     ASSERT_EQ(status, SUCCESS);
757     ASSERT_EQ(output1, nullptr);
758 
759     kvStore = nullptr;
760     status = StoreManager::GetInstance().CloseKVStore(appId, storeId);
761     ASSERT_EQ(status, SUCCESS);
762     std::string baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
763     status = StoreManager::GetInstance().Delete(appId, storeId, baseDir);
764     ASSERT_EQ(status, SUCCESS);
765 }
766 
767 /**
768  * @tc.name: GetCount
769  * @tc.desc: close the result set
770  * @tc.type: FUNC
771  * @tc.require: I4XVQQ
772  * @tc.author: Sven Wang
773  */
774 HWTEST_F(SingleStoreImplTest, GetCount, TestSize.Level0)
775 {
776     ASSERT_NE(kvStore_, nullptr);
777     std::vector<Entry> input;
__anon2048b17a0602(const Key &entry, const Key &sentry) 778     auto cmp = [](const Key &entry, const Key &sentry) { return entry.Data() < sentry.Data(); };
779     std::map<Key, Value, decltype(cmp)> dictionary(cmp);
780     for (int i = 0; i < 10; ++i) {
781         Entry entry;
782         entry.key = std::to_string(i).append("_k");
783         entry.value = std::to_string(i).append("_v");
784         dictionary[entry.key] = entry.value;
785         input.push_back(entry);
786     }
787     auto status = kvStore_->PutBatch(input);
788     ASSERT_EQ(status, SUCCESS);
789     DataQuery query;
790     query.InKeys({ "0_k", "1_k" });
791     int count = 0;
792     status = kvStore_->GetCount(query, count);
793     ASSERT_EQ(status, SUCCESS);
794     ASSERT_EQ(count, 2);
795     query.Reset();
796     status = kvStore_->GetCount(query, count);
797     ASSERT_EQ(status, SUCCESS);
798     ASSERT_EQ(count, 10);
799 }
800 
801 /**
802  * @tc.name: RemoveDeviceData
803  * @tc.desc: remove local device data
804  * @tc.type: FUNC
805  * @tc.require: I4XVQQ
806  * @tc.author: Sven Wang
807  */
808 HWTEST_F(SingleStoreImplTest, RemoveDeviceData, TestSize.Level0)
809 {
810     auto store = CreateKVStore("DeviceKVStore", DEVICE_COLLABORATION, false, true);
811     ASSERT_NE(store, nullptr);
812     std::vector<Entry> input;
__anon2048b17a0702(const Key &entry, const Key &sentry) 813     auto cmp = [](const Key &entry, const Key &sentry) { return entry.Data() < sentry.Data(); };
814     std::map<Key, Value, decltype(cmp)> dictionary(cmp);
815     for (int i = 0; i < 10; ++i) {
816         Entry entry;
817         entry.key = std::to_string(i).append("_k");
818         entry.value = std::to_string(i).append("_v");
819         dictionary[entry.key] = entry.value;
820         input.push_back(entry);
821     }
822     auto status = store->PutBatch(input);
823     ASSERT_EQ(status, SUCCESS);
824     int count = 0;
825     status = store->GetCount({}, count);
826     ASSERT_EQ(status, SUCCESS);
827     ASSERT_EQ(count, 10);
828     status = store->RemoveDeviceData(DevManager::GetInstance().GetLocalDevice().networkId);
829     ASSERT_EQ(status, SUCCESS);
830     status = store->GetCount({}, count);
831     ASSERT_EQ(status, SUCCESS);
832     ASSERT_EQ(count, 10);
833     std::string baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
834     status = StoreManager::GetInstance().Delete({ "SingleStoreImplTest" }, { "DeviceKVStore" }, baseDir);
835     ASSERT_EQ(status, SUCCESS);
836 }
837 
838 /**
839  * @tc.name: GetSecurityLevel
840  * @tc.desc: get security level
841  * @tc.type: FUNC
842  * @tc.require: I4XVQQ
843  * @tc.author: Sven Wang
844  */
845 HWTEST_F(SingleStoreImplTest, GetSecurityLevel, TestSize.Level0)
846 {
847     ASSERT_NE(kvStore_, nullptr);
848     SecurityLevel securityLevel = NO_LABEL;
849     auto status = kvStore_->GetSecurityLevel(securityLevel);
850     ASSERT_EQ(status, SUCCESS);
851     ASSERT_EQ(securityLevel, S1);
852 }
853 
854 /**
855  * @tc.name: RegisterSyncCallback
856  * @tc.desc: register the data sync callback
857  * @tc.type: FUNC
858  * @tc.require: I4XVQQ
859  * @tc.author: Sven Wang
860  */
861 HWTEST_F(SingleStoreImplTest, RegisterSyncCallback, TestSize.Level0)
862 {
863     ASSERT_NE(kvStore_, nullptr);
864     class TestSyncCallback : public KvStoreSyncCallback {
865     public:
SyncCompleted(const map<std::string,Status> & results)866         void SyncCompleted(const map<std::string, Status> &results) override
867         {
868         }
869     };
870     auto callback = std::make_shared<TestSyncCallback>();
871     auto status = kvStore_->RegisterSyncCallback(callback);
872     ASSERT_EQ(status, SUCCESS);
873 }
874 
875 /**
876  * @tc.name: UnRegisterSyncCallback
877  * @tc.desc: unregister the data sync callback
878  * @tc.type: FUNC
879  * @tc.require: I4XVQQ
880  * @tc.author: Sven Wang
881  */
882 HWTEST_F(SingleStoreImplTest, UnRegisterSyncCallback, TestSize.Level0)
883 {
884     ASSERT_NE(kvStore_, nullptr);
885     class TestSyncCallback : public KvStoreSyncCallback {
886     public:
SyncCompleted(const map<std::string,Status> & results)887         void SyncCompleted(const map<std::string, Status> &results) override
888         {
889         }
890     };
891     auto callback = std::make_shared<TestSyncCallback>();
892     auto status = kvStore_->RegisterSyncCallback(callback);
893     ASSERT_EQ(status, SUCCESS);
894     status = kvStore_->UnRegisterSyncCallback();
895     ASSERT_EQ(status, SUCCESS);
896 }
897 
898 /**
899 * @tc.name: disableBackup
900 * @tc.desc: Disable backup
901 * @tc.type: FUNC
902 * @tc.require:
903 * @tc.author: Wang Kai
904 */
905 HWTEST_F(SingleStoreImplTest, disableBackup, TestSize.Level0)
906 {
907     AppId appId = { "SingleStoreImplTest" };
908     StoreId storeId = { "SingleKVStoreNoBackup" };
909     std::shared_ptr<SingleKvStore> kvStoreNoBackup;
910     kvStoreNoBackup = CreateKVStore(storeId, SINGLE_VERSION, true, false);
911     ASSERT_NE(kvStoreNoBackup, nullptr);
912     auto baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
913     auto status = StoreManager::GetInstance().CloseKVStore(appId, storeId);
914     ASSERT_EQ(status, SUCCESS);
915     status = StoreManager::GetInstance().Delete(appId, storeId, baseDir);
916     ASSERT_EQ(status, SUCCESS);
917 }
918 
919 /**
920  * @tc.name: PutOverMaxValue
921  * @tc.desc: put key-value data to the kv store and the value size  over the limits
922  * @tc.type: FUNC
923  * @tc.require: I605H3
924  * @tc.author: Wang Kai
925  */
926 HWTEST_F(SingleStoreImplTest, PutOverMaxValue, TestSize.Level0)
927 {
928     ASSERT_NE(kvStore_, nullptr);
929     std::string value;
930     int maxsize = 1024 * 1024;
931     for (int i = 0; i <= maxsize; i++) {
932         value += "test";
933     }
934     Value valuePut(value);
935     auto status = kvStore_->Put({ "Put Test" }, valuePut);
936     ASSERT_EQ(status, INVALID_ARGUMENT);
937 }
938 /**
939  * @tc.name: DeleteOverMaxKey
940  * @tc.desc: delete the values of the keys and the key size  over the limits
941  * @tc.type: FUNC
942  * @tc.require: I605H3
943  * @tc.author: Wang Kai
944  */
945 HWTEST_F(SingleStoreImplTest, DeleteOverMaxKey, TestSize.Level0)
946 {
947     ASSERT_NE(kvStore_, nullptr);
948     std::string str;
949     int maxsize = 1024;
950     for (int i = 0; i <= maxsize; i++) {
951         str += "key";
952     }
953     Key key(str);
954     auto status = kvStore_->Put(key, "Put Test");
955     ASSERT_EQ(status, INVALID_ARGUMENT);
956     Value value;
957     status = kvStore_->Get(key, value);
958     ASSERT_EQ(status, INVALID_ARGUMENT);
959     status = kvStore_->Delete(key);
960     ASSERT_EQ(status, INVALID_ARGUMENT);
961 }
962 
963 /**
964  * @tc.name: GetEntriesOverMaxKey
965  * @tc.desc: get entries the by prefix and the prefix size  over the limits
966  * @tc.type: FUNC
967  * @tc.require: I605H3
968  * @tc.author: Wang Kai
969  */
970 HWTEST_F(SingleStoreImplTest, GetEntriesOverMaxPrefix, TestSize.Level0)
971 {
972     ASSERT_NE(kvStore_, nullptr);
973     std::string str;
974     int maxsize = 1024;
975     for (int i = 0; i <= maxsize; i++) {
976         str += "key";
977     }
978     const Key prefix(str);
979     std::vector<Entry> output;
980     auto status = kvStore_->GetEntries(prefix, output);
981     ASSERT_EQ(status, INVALID_ARGUMENT);
982 }
983 
984 /**
985  * @tc.name: GetResultSetOverMaxPrefix
986  * @tc.desc: get result set the by prefix and the prefix size  over the limits
987  * @tc.type: FUNC
988  * @tc.require: I605H3
989  * @tc.author: Wang Kai
990  */
991 HWTEST_F(SingleStoreImplTest, GetResultSetOverMaxPrefix, TestSize.Level0)
992 {
993     ASSERT_NE(kvStore_, nullptr);
994     std::string str;
995     int maxsize = 1024;
996     for (int i = 0; i <= maxsize; i++) {
997         str += "key";
998     }
999     const Key prefix(str);
1000     std::shared_ptr<KvStoreResultSet> output;
1001     auto status = kvStore_->GetResultSet(prefix, output);
1002     ASSERT_EQ(status, INVALID_ARGUMENT);
1003 }
1004 
1005 /**
1006  * @tc.name: RemoveNullDeviceData
1007  * @tc.desc: remove local device data and the device is null
1008  * @tc.type: FUNC
1009  * @tc.require: I605H3
1010  * @tc.author: Wang Kai
1011  */
1012 HWTEST_F(SingleStoreImplTest, RemoveNullDeviceData, TestSize.Level0)
1013 {
1014     auto store = CreateKVStore("DeviceKVStore", DEVICE_COLLABORATION, false, true);
1015     ASSERT_NE(store, nullptr);
1016     std::vector<Entry> input;
__anon2048b17a0802(const Key &entry, const Key &sentry) 1017     auto cmp = [](const Key &entry, const Key &sentry) {
1018         return entry.Data() < sentry.Data();
1019     };
1020     std::map<Key, Value, decltype(cmp)> dictionary(cmp);
1021     for (int i = 0; i < 10; ++i) {
1022         Entry entry;
1023         entry.key = std::to_string(i).append("_k");
1024         entry.value = std::to_string(i).append("_v");
1025         dictionary[entry.key] = entry.value;
1026         input.push_back(entry);
1027     }
1028     auto status = store->PutBatch(input);
1029     ASSERT_EQ(status, SUCCESS);
1030     int count = 0;
1031     status = store->GetCount({}, count);
1032     ASSERT_EQ(status, SUCCESS);
1033     ASSERT_EQ(count, 10);
1034     const string device = { "" };
1035     status = store->RemoveDeviceData(device);
1036     ASSERT_EQ(status, SUCCESS);
1037 }
1038 
1039 /**
1040  * @tc.name: CloseKVStoreWithInvalidAppId
1041  * @tc.desc: close the kv store with invalid appid
1042  * @tc.type: FUNC
1043  * @tc.require:
1044  * @tc.author: Yang Qing
1045  */
1046 HWTEST_F(SingleStoreImplTest, CloseKVStoreWithInvalidAppId, TestSize.Level0)
1047 {
1048     AppId appId = { "" };
1049     StoreId storeId = { "SingleKVStore" };
1050     Status status = StoreManager::GetInstance().CloseKVStore(appId, storeId);
1051     ASSERT_EQ(status, INVALID_ARGUMENT);
1052 }
1053 
1054 /**
1055  * @tc.name: CloseKVStoreWithInvalidStoreId
1056  * @tc.desc: close the kv store with invalid store id
1057  * @tc.type: FUNC
1058  * @tc.require:
1059  * @tc.author: Yang Qing
1060  */
1061 HWTEST_F(SingleStoreImplTest, CloseKVStoreWithInvalidStoreId, TestSize.Level0)
1062 {
1063     AppId appId = { "SingleStoreImplTest" };
1064     StoreId storeId = { "" };
1065     Status status = StoreManager::GetInstance().CloseKVStore(appId, storeId);
1066     ASSERT_EQ(status, INVALID_ARGUMENT);
1067 }
1068 
1069 /**
1070  * @tc.name: CloseAllKVStore
1071  * @tc.desc: close all kv store
1072  * @tc.type: FUNC
1073  * @tc.require:
1074  * @tc.author: Yang Qing
1075  */
1076 HWTEST_F(SingleStoreImplTest, CloseAllKVStore, TestSize.Level0)
1077 {
1078     AppId appId = { "SingleStoreImplTestCloseAll" };
1079     std::vector<std::shared_ptr<SingleKvStore>> kvStores;
1080     for (int i = 0; i < 5; i++) {
1081         std::shared_ptr<SingleKvStore> kvStore;
1082         Options options;
1083         options.kvStoreType = SINGLE_VERSION;
1084         options.securityLevel = S1;
1085         options.area = EL1;
1086         options.baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
1087         std::string sId = "SingleStoreImplTestCloseAll" + std::to_string(i);
1088         StoreId storeId = { sId };
1089         Status status;
1090         kvStore = StoreManager::GetInstance().GetKVStore(appId, storeId, options, status);
1091         ASSERT_NE(kvStore, nullptr);
1092         kvStores.push_back(kvStore);
1093         ASSERT_EQ(status, SUCCESS);
1094         kvStore = nullptr;
1095     }
1096     Status status = StoreManager::GetInstance().CloseAllKVStore(appId);
1097     ASSERT_EQ(status, SUCCESS);
1098 }
1099 
1100 /**
1101  * @tc.name: CloseAllKVStoreWithInvalidAppId
1102  * @tc.desc: close the kv store with invalid appid
1103  * @tc.type: FUNC
1104  * @tc.require:
1105  * @tc.author: Yang Qing
1106  */
1107 HWTEST_F(SingleStoreImplTest, CloseAllKVStoreWithInvalidAppId, TestSize.Level0)
1108 {
1109     AppId appId = { "" };
1110     Status status = StoreManager::GetInstance().CloseAllKVStore(appId);
1111     ASSERT_EQ(status, INVALID_ARGUMENT);
1112 }
1113 
1114 /**
1115  * @tc.name: DeleteWithInvalidAppId
1116  * @tc.desc: delete the kv store with invalid appid
1117  * @tc.type: FUNC
1118  * @tc.require:
1119  * @tc.author: Yang Qing
1120  */
1121 HWTEST_F(SingleStoreImplTest, DeleteWithInvalidAppId, TestSize.Level0)
1122 {
1123     std::string baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
1124     AppId appId = { "" };
1125     StoreId storeId = { "SingleKVStore" };
1126     Status status = StoreManager::GetInstance().Delete(appId, storeId, baseDir);
1127     ASSERT_EQ(status, INVALID_ARGUMENT);
1128 }
1129 
1130 /**
1131  * @tc.name: DeleteWithInvalidStoreId
1132  * @tc.desc: delete the kv store with invalid storeid
1133  * @tc.type: FUNC
1134  * @tc.require:
1135  * @tc.author: Yang Qing
1136  */
1137 HWTEST_F(SingleStoreImplTest, DeleteWithInvalidStoreId, TestSize.Level0)
1138 {
1139     std::string baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
1140     AppId appId = { "SingleStoreImplTest" };
1141     StoreId storeId = { "" };
1142     Status status = StoreManager::GetInstance().Delete(appId, storeId, baseDir);
1143     ASSERT_EQ(status, INVALID_ARGUMENT);
1144 }
1145 
1146 /**
1147  * @tc.name: GetKVStoreWithPersistentFalse
1148  * @tc.desc: delete the kv store with the persistent is false
1149  * @tc.type: FUNC
1150  * @tc.require:
1151  * @tc.author: Wang Kai
1152  */
1153 HWTEST_F(SingleStoreImplTest, GetKVStoreWithPersistentFalse, TestSize.Level0)
1154 {
1155     AppId appId = { "SingleStoreImplTest" };
1156     StoreId storeId = { "SingleKVStorePersistentFalse" };
1157     std::shared_ptr<SingleKvStore> kvStore;
1158     Options options;
1159     options.kvStoreType = SINGLE_VERSION;
1160     options.securityLevel = S1;
1161     options.area = EL1;
1162     options.persistent = false;
1163     options.baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
1164     Status status;
1165     kvStore = StoreManager::GetInstance().GetKVStore(appId, storeId, options, status);
1166     ASSERT_EQ(kvStore, nullptr);
1167 }
1168 
1169 /**
1170  * @tc.name: GetKVStoreWithInvalidType
1171  * @tc.desc: delete the kv store with the KvStoreType is InvalidType
1172  * @tc.type: FUNC
1173  * @tc.require:
1174  * @tc.author: Wang Kai
1175  */
1176 HWTEST_F(SingleStoreImplTest, GetKVStoreWithInvalidType, TestSize.Level0)
1177 {
1178     AppId appId = { "SingleStoreImplTest" };
1179     StoreId storeId = { "SingleKVStoreInvalidType" };
1180     std::shared_ptr<SingleKvStore> kvStore;
1181     Options options;
1182     options.kvStoreType = INVALID_TYPE;
1183     options.securityLevel = S1;
1184     options.area = EL1;
1185     options.baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
1186     Status status;
1187     kvStore = StoreManager::GetInstance().GetKVStore(appId, storeId, options, status);
1188     ASSERT_EQ(kvStore, nullptr);
1189 }
1190 
1191 /**
1192  * @tc.name: GetKVStoreWithCreateIfMissingFalse
1193  * @tc.desc: delete the kv store with the createIfMissing is false
1194  * @tc.type: FUNC
1195  * @tc.require:
1196  * @tc.author: Wang Kai
1197  */
1198 HWTEST_F(SingleStoreImplTest, GetKVStoreWithCreateIfMissingFalse, TestSize.Level0)
1199 {
1200     AppId appId = { "SingleStoreImplTest" };
1201     StoreId storeId = { "SingleKVStoreCreateIfMissingFalse" };
1202     std::shared_ptr<SingleKvStore> kvStore;
1203     Options options;
1204     options.kvStoreType = SINGLE_VERSION;
1205     options.securityLevel = S1;
1206     options.area = EL1;
1207     options.createIfMissing = false;
1208     options.baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
1209     Status status;
1210     kvStore = StoreManager::GetInstance().GetKVStore(appId, storeId, options, status);
1211     ASSERT_EQ(kvStore, nullptr);
1212 }
1213 
1214 /**
1215  * @tc.name: GetKVStoreWithAutoSync
1216  * @tc.desc: delete the kv store with the autoSync is false
1217  * @tc.type: FUNC
1218  * @tc.require:
1219  * @tc.author: Wang Kai
1220  */
1221 HWTEST_F(SingleStoreImplTest, GetKVStoreWithAutoSync, TestSize.Level0)
1222 {
1223     AppId appId = { "SingleStoreImplTest" };
1224     StoreId storeId = { "SingleKVStoreAutoSync" };
1225     std::shared_ptr<SingleKvStore> kvStore;
1226     Options options;
1227     options.kvStoreType = SINGLE_VERSION;
1228     options.securityLevel = S1;
1229     options.area = EL1;
1230     options.autoSync = false;
1231     options.baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
1232     Status status;
1233     kvStore = StoreManager::GetInstance().GetKVStore(appId, storeId, options, status);
1234     ASSERT_NE(kvStore, nullptr);
1235     status = StoreManager::GetInstance().CloseKVStore(appId, storeId);
1236     ASSERT_EQ(status, SUCCESS);
1237 }
1238 
1239 /**
1240  * @tc.name: GetKVStoreWithAreaEL2
1241  * @tc.desc: delete the kv store with the area is EL2
1242  * @tc.type: FUNC
1243  * @tc.require:
1244  * @tc.author: Wang Kai
1245  */
1246 HWTEST_F(SingleStoreImplTest, GetKVStoreWithAreaEL2, TestSize.Level0)
1247 {
1248     std::string baseDir = "/data/service/el2/100/SingleStoreImplTest";
1249     mkdir(baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
1250 
1251     AppId appId = { "SingleStoreImplTest" };
1252     StoreId storeId = { "SingleKVStoreAreaEL2" };
1253     std::shared_ptr<SingleKvStore> kvStore;
1254     Options options;
1255     options.kvStoreType = SINGLE_VERSION;
1256     options.securityLevel = S2;
1257     options.area = EL2;
1258     options.baseDir = "/data/service/el2/100/SingleStoreImplTest";
1259     Status status;
1260     kvStore = StoreManager::GetInstance().GetKVStore(appId, storeId, options, status);
1261     ASSERT_NE(kvStore, nullptr);
1262     status = StoreManager::GetInstance().CloseKVStore(appId, storeId);
1263     ASSERT_EQ(status, SUCCESS);
1264 }
1265 
1266 /**
1267  * @tc.name: GetKVStoreWithRebuildTrue
1268  * @tc.desc: delete the kv store with the rebuild is true
1269  * @tc.type: FUNC
1270  * @tc.require:
1271  * @tc.author: Wang Kai
1272  */
1273 HWTEST_F(SingleStoreImplTest, GetKVStoreWithRebuildTrue, TestSize.Level0)
1274 {
1275     AppId appId = { "SingleStoreImplTest" };
1276     StoreId storeId = { "SingleKVStoreRebuildFalse" };
1277     std::shared_ptr<SingleKvStore> kvStore;
1278     Options options;
1279     options.kvStoreType = SINGLE_VERSION;
1280     options.securityLevel = S1;
1281     options.area = EL1;
1282     options.rebuild = true;
1283     options.baseDir = "/data/service/el1/public/database/SingleStoreImplTest";
1284     Status status;
1285     kvStore = StoreManager::GetInstance().GetKVStore(appId, storeId, options, status);
1286     ASSERT_NE(kvStore, nullptr);
1287     status = StoreManager::GetInstance().CloseKVStore(appId, storeId);
1288     ASSERT_EQ(status, SUCCESS);
1289 }