• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 #include <unistd.h>
18 #include <cstddef>
19 #include <cstdint>
20 #include <vector>
21 
22 #include "block_data.h"
23 #include "dev_manager.h"
24 #include "distributed_kv_data_manager.h"
25 #include "file_ex.h"
26 #include "types.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS::DistributedKv;
30 namespace OHOS::Test {
31 class SingleKvStoreAsyncGetTest : public testing::Test {
32 public:
33     static void SetUpTestCase(void);
34 
35     static void TearDownTestCase(void);
36 
37     void SetUp();
38 
39     void TearDown();
40 
41     static std::shared_ptr<SingleKvStore> singleKvStore;
42     static Status status_;
43 };
44 
45 std::shared_ptr<SingleKvStore> SingleKvStoreAsyncGetTest::singleKvStore = nullptr;
46 Status SingleKvStoreAsyncGetTest::status_ = Status::ERROR;
47 
SetUpTestCase(void)48 void SingleKvStoreAsyncGetTest::SetUpTestCase(void)
49 {
50     DistributedKvDataManager manager;
51     Options options = { .createIfMissing = true, .encrypt = false, .autoSync = true,
52                         .kvStoreType = KvStoreType::SINGLE_VERSION, .dataType = DataType::TYPE_DYNAMICAL };
53     options.area = EL1;
54     options.securityLevel = S1;
55     options.baseDir = std::string("/data/service/el1/public/database/asyncgettest");
56     AppId appId = { "asyncgettest" };
57     StoreId storeId = { "asyncgettest_store_0" };
58     mkdir(options.baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
59     status_ = manager.GetSingleKvStore(options, appId, storeId, singleKvStore);
60 }
61 
TearDownTestCase(void)62 void SingleKvStoreAsyncGetTest::TearDownTestCase(void)
63 {
64     (void)remove("/data/service/el1/public/database/asyncgettest/key");
65     (void)remove("/data/service/el1/public/database/asyncgettest/kvdb");
66     (void)remove("/data/service/el1/public/database/asyncgettest");
67 }
68 
SetUp(void)69 void SingleKvStoreAsyncGetTest::SetUp(void)
70 {}
71 
TearDown(void)72 void SingleKvStoreAsyncGetTest::TearDown(void)
73 {}
74 
75 /**
76 * @tc.name: CreateDefaultKvStore
77 * @tc.desc: get a single KvStore instance, default is dynamic.
78 * @tc.type: FUNC
79 * @tc.require:
80 * @tc.author: zuojiangjiang
81 */
82 HWTEST_F(SingleKvStoreAsyncGetTest, CreateDefaultKvStore, TestSize.Level1)
83 {
84     DistributedKvDataManager manager;
85     Options options = { .createIfMissing = true, .kvStoreType = KvStoreType::SINGLE_VERSION};
86     EXPECT_EQ(options.autoSync, false);
87     EXPECT_EQ(options.dataType, DataType::TYPE_DYNAMICAL);
88     options.area = EL1;
89     options.securityLevel = S1;
90     options.baseDir = std::string("/data/service/el1/public/database/asyncgettest");
91     AppId appId = { "asyncgettest" };
92     StoreId storeId = { "asyncgettest_store_1" };
93 
94     std::shared_ptr<SingleKvStore> store = nullptr;
95     auto status = manager.GetSingleKvStore(options, appId, storeId, store);
96     EXPECT_EQ(status, Status::SUCCESS);
97     EXPECT_NE(store, nullptr);
98     status = manager.CloseKvStore(appId, storeId);
99     EXPECT_EQ(status, Status::SUCCESS);
100     status = manager.DeleteKvStore(appId, storeId, options.baseDir);
101     EXPECT_EQ(status, Status::SUCCESS);
102 }
103 
104 /**
105 * @tc.name: CreateStaticKvStore
106 * @tc.desc: get a single KvStore instance, data type is STATICS.
107 * @tc.type: FUNC
108 * @tc.require:
109 * @tc.author: zuojiangjiang
110 */
111 HWTEST_F(SingleKvStoreAsyncGetTest, CreateStaticKvStore, TestSize.Level1)
112 {
113     DistributedKvDataManager manager;
114     Options options = { .createIfMissing = true, .autoSync = true, .dataType = DataType::TYPE_STATICS };
115     options.area = EL1;
116     options.securityLevel = S1;
117     options.baseDir = std::string("/data/service/el1/public/database/asyncgettest");
118     AppId appId = { "asyncgettest" };
119     StoreId storeId = { "asyncgettest_store" };
120 
121     std::shared_ptr<SingleKvStore> store = nullptr;
122     auto status = manager.GetSingleKvStore(options, appId, storeId, store);
123     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
124 }
125 
126 /**
127 * @tc.name: GetKvStoreWithDiffDataType
128 * @tc.desc: get a single KvStore instance 2 times, data type is different.
129 * @tc.type: FUNC
130 * @tc.require:
131 * @tc.author: zuojiangjiang
132 */
133 HWTEST_F(SingleKvStoreAsyncGetTest, GetKvStoreWithDiffDataType, TestSize.Level1)
134 {
135     DistributedKvDataManager manager;
136     Options options = { .createIfMissing = true, .dataType = DataType::TYPE_STATICS };
137     options.area = EL1;
138     options.securityLevel = S1;
139     options.baseDir = std::string("/data/service/el1/public/database/asyncgettest");
140     AppId appId = { "asyncgettest" };
141     StoreId storeId = { "asyncgettest_store_2" };
142 
143     std::shared_ptr<SingleKvStore> store = nullptr;
144     auto status = manager.GetSingleKvStore(options, appId, storeId, store);
145     EXPECT_EQ(status, Status::SUCCESS);
146     EXPECT_NE(store, nullptr);
147     status = manager.CloseKvStore(appId, storeId);
148     EXPECT_EQ(status, Status::SUCCESS);
149     options.dataType = DataType::TYPE_DYNAMICAL;
150     status = manager.GetSingleKvStore(options, appId, storeId, store);
151     EXPECT_EQ(status, Status::SUCCESS);
152     status = manager.DeleteKvStore(appId, storeId, options.baseDir);
153     EXPECT_EQ(status, Status::SUCCESS);
154 }
155 
156 /**
157 * @tc.name: AsyncGetValue
158 * @tc.desc: async get value, data type is TYPE_STATICS.
159 * @tc.type: FUNC
160 * @tc.require:
161 * @tc.author: zuojiangjiang
162 */
163 HWTEST_F(SingleKvStoreAsyncGetTest, AsyncGetValue, TestSize.Level1)
164 {
165     DistributedKvDataManager manager;
166     Options options = { .createIfMissing = true, .dataType = DataType::TYPE_STATICS };
167     options.area = EL1;
168     options.securityLevel = S1;
169     options.baseDir = std::string("/data/service/el1/public/database/asyncgettest");
170     AppId appId = { "asyncgettest" };
171     StoreId storeId = { "asyncgettest_store_3" };
172 
173     std::shared_ptr<SingleKvStore> store = nullptr;
174     auto status = manager.GetSingleKvStore(options, appId, storeId, store);
175     EXPECT_EQ(status, Status::SUCCESS);
176     EXPECT_NE(store, nullptr);
177     status = store->Put({ "test_key" }, { "test_value" });
178     EXPECT_EQ(status, Status::SUCCESS);
179     Value value;
180     status = store->Get({ "test_key" }, value);
181     EXPECT_EQ(status, Status::SUCCESS);
182     EXPECT_EQ(value.ToString(), "test_value");
183     auto blockData = std::make_shared<BlockData<bool>>(1, false);
__anonc80b0db20102(Status status, Value &&out) 184     std::function<void(Status, Value&&)> call = [blockData, value](Status status, Value &&out) {
185         EXPECT_EQ(status, Status::SUCCESS);
186         EXPECT_EQ(out.ToString(), value.ToString());
187         blockData->SetValue(true);
188     };
189     auto devInfo = DevManager::GetInstance().GetLocalDevice();
190     store->Get({ "test_key" }, devInfo.networkId, call);
191     EXPECT_EQ(blockData->GetValue(), true);
192     status = manager.CloseKvStore(appId, storeId);
193     EXPECT_EQ(status, Status::SUCCESS);
194     status = manager.DeleteKvStore(appId, storeId, options.baseDir);
195     EXPECT_EQ(status, Status::SUCCESS);
196 }
197 
198 /**
199 * @tc.name: AsyncGetValueWithInvalidNetworkId
200 * @tc.desc: async get value, networkId is invalid.
201 * @tc.type: FUNC
202 * @tc.require:
203 * @tc.author: zuojiangjiang
204 */
205 HWTEST_F(SingleKvStoreAsyncGetTest, AsyncGetValueWithInvalidNetworkId, TestSize.Level1)
206 {
207     EXPECT_NE(singleKvStore, nullptr);
208     auto status = singleKvStore->Put({ "test_key_0" }, { "test_value_0" });
209     EXPECT_EQ(status, Status::SUCCESS);
210     Value value;
211     status = singleKvStore->Get({ "test_key_0" }, value);
212     EXPECT_EQ(status, Status::SUCCESS);
213     EXPECT_EQ(value.ToString(), "test_value_0");
214     auto blockData = std::make_shared<BlockData<bool>>(1, false);
__anonc80b0db20202(Status status, Value &&value) 215     std::function<void(Status, Value&&)> call = [blockData](Status status, Value &&value) {
216         EXPECT_EQ(status, Status::SUCCESS);
217         blockData->SetValue(true);
218     };
219     singleKvStore->Get({ "test_key_0" }, "", call);
220     EXPECT_EQ(blockData->GetValue(), true);
221     blockData->Clear(false);
222     singleKvStore->Get({ "test_key_0" }, "networkId_test", call);
223     EXPECT_EQ(blockData->GetValue(), true);
224 }
225 
226 /**
227 * @tc.name: AsyncGetEntriesWithInvalidNetworkId
228 * @tc.desc: async get entries, networkId is invalid.
229 * @tc.type: FUNC
230 * @tc.require:
231 * @tc.author: zuojiangjiang
232 */
233 HWTEST_F(SingleKvStoreAsyncGetTest, AsyncGetEntriesWithInvalidNetworkId, TestSize.Level1)
234 {
235     EXPECT_NE(singleKvStore, nullptr);
236     std::vector<Entry> entries;
237     for (int i = 0; i < 10; ++i) {
238         Entry entry;
239         entry.key = "prefix_key_" + std::to_string(i);
240         entry.value = std::to_string(i).append("_v");
241         entries.push_back(entry);
242     }
243     auto status = singleKvStore->PutBatch(entries);
244     EXPECT_EQ(status, Status::SUCCESS);
245     std::vector<Entry> results;
246     singleKvStore->GetEntries({ "prefix_key_" }, results);
247     EXPECT_EQ(results.size(), 10);
248     auto blockData = std::make_shared<BlockData<bool>>(1, false);
__anonc80b0db20302(Status status, std::vector<Entry> &&value) 249     std::function<void(Status, std::vector<Entry>&&)> call = [blockData](Status status, std::vector<Entry> &&value) {
250         EXPECT_EQ(status, Status::SUCCESS);
251         blockData->SetValue(true);
252     };
253     singleKvStore->GetEntries({ "prefix_key_" }, "", call);
254     EXPECT_EQ(blockData->GetValue(), true);
255     blockData->Clear(false);
256     singleKvStore->GetEntries({ "prefix_key_" }, "networkId_test", call);
257     EXPECT_EQ(blockData->GetValue(), true);
258 }
259 
260 /**
261 * @tc.name: AsyncGetValueWithLocalNetworkId
262 * @tc.desc: async get value, networkId is local.
263 * @tc.type: FUNC
264 * @tc.require:
265 * @tc.author: zuojiangjiang
266 */
267 HWTEST_F(SingleKvStoreAsyncGetTest, AsyncGetValueWithLocalNetworkId, TestSize.Level1)
268 {
269     EXPECT_NE(singleKvStore, nullptr);
270     auto status = singleKvStore->Put({ "test_key_1" }, { "test_value_1" });
271     EXPECT_EQ(status, Status::SUCCESS);
272     Value result;
273     status = singleKvStore->Get({ "test_key_1" }, result);
274     EXPECT_EQ(status, Status::SUCCESS);
275     EXPECT_EQ(result.ToString(), "test_value_1");
276     auto blockData = std::make_shared<BlockData<bool>>(1, false);
__anonc80b0db20402(Status status, Value &&value) 277     std::function<void(Status, Value&&)> call = [blockData, result](Status status, Value &&value) {
278         EXPECT_EQ(status, Status::SUCCESS);
279         EXPECT_EQ(result.ToString(), value.ToString());
280         blockData->SetValue(true);
281     };
282     auto devInfo = DevManager::GetInstance().GetLocalDevice();
283     singleKvStore->Get("test_key_1", devInfo.networkId, call);
284     EXPECT_EQ(blockData->GetValue(), true);
285 }
286 
287 /**
288 * @tc.name: AsyncGetEntriesWithLocalNetworkId
289 * @tc.desc: async get entries, networkId is local.
290 * @tc.type: FUNC
291 * @tc.require:
292 * @tc.author: zuojiangjiang
293 */
294 HWTEST_F(SingleKvStoreAsyncGetTest, AsyncGetEntriesWithLocalNetworkId, TestSize.Level1)
295 {
296     EXPECT_NE(singleKvStore, nullptr);
297     int num = 5;
298     for (int i = 0; i < num; i++) {
299         singleKvStore->Put({ "prefix_of_" + std::to_string(i) }, { "test_value_2" });
300     }
301     std::vector<Entry> results;
302     singleKvStore->GetEntries({ "prefix_of_" }, results);
303     EXPECT_EQ(results.size(), num);
304     auto blockData = std::make_shared<BlockData<bool>>(1, false);
305     std::function<void(Status, std::vector<Entry>&&)> call =
__anonc80b0db20502(Status status, std::vector<Entry>&& values) 306         [blockData, results](Status status, std::vector<Entry>&& values) {
307             EXPECT_EQ(status, Status::SUCCESS);
308             EXPECT_EQ(results.size(), values.size());
309             EXPECT_EQ(values[0].value.ToString(), "test_value_2");
310             blockData->SetValue(true);
311     };
312     auto devInfo = DevManager::GetInstance().GetLocalDevice();
313     singleKvStore->GetEntries("prefix_of_", devInfo.networkId, call);
314     EXPECT_EQ(blockData->GetValue(), true);
315     auto ret = singleKvStore->GetDeviceEntries("test_device_1", results);
316     EXPECT_EQ(ret, Status::SUCCESS);
317 }
318 } // namespace OHOS::Test