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 <condition_variable>
17 #include <gtest/gtest.h>
18 #include <thread>
19
20 #include "db_constant.h"
21 #include "db_common.h"
22 #include "distributeddb_data_generate_unit_test.h"
23 #include "distributeddb_tools_unit_test.h"
24 #include "kv_store_nb_delegate.h"
25 #include "kv_virtual_device.h"
26 #include "platform_specific.h"
27
28 using namespace testing::ext;
29 using namespace DistributedDB;
30 using namespace DistributedDBUnitTest;
31 using namespace std;
32
33 namespace {
34 string g_testDir;
35 const string STORE_ID = "kv_stroe_permission_sync_test";
36 const int WAIT_TIME = 1000;
37 const std::string DEVICE_A = "real_device";
38 const std::string DEVICE_B = "deviceB";
39 const std::string DEVICE_C = "deviceC";
40
41 KvStoreDelegateManager g_mgr(APP_ID, USER_ID);
42 KvStoreConfig g_config;
43 DistributedDBToolsUnitTest g_tool;
44 DBStatus g_kvDelegateStatus = INVALID_ARGS;
45 KvStoreNbDelegate* g_kvDelegatePtr = nullptr;
46 VirtualCommunicatorAggregator* g_communicatorAggregator = nullptr;
47 KvVirtualDevice *g_deviceB = nullptr;
48 KvVirtualDevice *g_deviceC = nullptr;
49
50 // the type of g_kvDelegateCallback is function<void(DBStatus, KvStoreDelegate*)>
51 auto g_kvDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback,
52 placeholders::_1, placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(g_kvDelegatePtr));
53 }
54
55 class DistributedDBSingleVerP2PPermissionSyncTest : public testing::Test {
56 public:
57 static void SetUpTestCase(void);
58 static void TearDownTestCase(void);
59 void SetUp();
60 void TearDown();
61 };
62
SetUpTestCase(void)63 void DistributedDBSingleVerP2PPermissionSyncTest::SetUpTestCase(void)
64 {
65 /**
66 * @tc.setup: Init datadir and Virtual Communicator.
67 */
68 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
69 g_config.dataDir = g_testDir;
70 g_mgr.SetKvStoreConfig(g_config);
71
72 string dir = g_testDir + "/single_ver";
73 DIR* dirTmp = opendir(dir.c_str());
74 if (dirTmp == nullptr) {
75 OS::MakeDBDirectory(dir);
76 } else {
77 closedir(dirTmp);
78 }
79
80 g_communicatorAggregator = new (std::nothrow) VirtualCommunicatorAggregator();
81 ASSERT_TRUE(g_communicatorAggregator != nullptr);
82 RuntimeContext::GetInstance()->SetCommunicatorAggregator(g_communicatorAggregator);
83 }
84
TearDownTestCase(void)85 void DistributedDBSingleVerP2PPermissionSyncTest::TearDownTestCase(void)
86 {
87 /**
88 * @tc.teardown: Release virtual Communicator and clear data dir.
89 */
90 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
91 LOGE("rm test db files error!");
92 }
93 RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
94 }
95
SetUp(void)96 void DistributedDBSingleVerP2PPermissionSyncTest::SetUp(void)
97 {
98 DistributedDBToolsUnitTest::PrintTestCaseInfo();
99 /**
100 * @tc.setup: create virtual device B and C, and get a KvStoreNbDelegate as deviceA
101 */
102 KvStoreNbDelegate::Option option;
103 g_mgr.GetKvStore(STORE_ID, option, g_kvDelegateCallback);
104 ASSERT_TRUE(g_kvDelegateStatus == OK);
105 ASSERT_TRUE(g_kvDelegatePtr != nullptr);
106 g_deviceB = new (std::nothrow) KvVirtualDevice(DEVICE_B);
107 ASSERT_TRUE(g_deviceB != nullptr);
108 VirtualSingleVerSyncDBInterface *syncInterfaceB = new (std::nothrow) VirtualSingleVerSyncDBInterface();
109 ASSERT_TRUE(syncInterfaceB != nullptr);
110 ASSERT_EQ(g_deviceB->Initialize(g_communicatorAggregator, syncInterfaceB), E_OK);
111
112 g_deviceC = new (std::nothrow) KvVirtualDevice(DEVICE_C);
113 ASSERT_TRUE(g_deviceC != nullptr);
114 VirtualSingleVerSyncDBInterface *syncInterfaceC = new (std::nothrow) VirtualSingleVerSyncDBInterface();
115 ASSERT_TRUE(syncInterfaceC != nullptr);
116 ASSERT_EQ(g_deviceC->Initialize(g_communicatorAggregator, syncInterfaceC), E_OK);
117
118 auto permissionCheckCallback = [] (const std::string &userId, const std::string &appId, const std::string &storeId,
119 const std::string &deviceId, uint8_t flag) -> bool {
120 return true;
121 };
122 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(permissionCheckCallback), OK);
123 }
124
TearDown(void)125 void DistributedDBSingleVerP2PPermissionSyncTest::TearDown(void)
126 {
127 /**
128 * @tc.teardown: Release device A, B, C
129 */
130 if (g_kvDelegatePtr != nullptr) {
131 ASSERT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
132 g_kvDelegatePtr = nullptr;
133 DBStatus status = g_mgr.DeleteKvStore(STORE_ID);
134 LOGD("delete kv store status %d", status);
135 ASSERT_TRUE(status == OK);
136 }
137 if (g_deviceB != nullptr) {
138 delete g_deviceB;
139 g_deviceB = nullptr;
140 }
141 if (g_deviceC != nullptr) {
142 delete g_deviceC;
143 g_deviceC = nullptr;
144 }
145 PermissionCheckCallbackV2 nullCallback;
146 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(nullCallback), OK);
147 }
148
149 /**
150 * @tc.name: PermissionCheck001
151 * @tc.desc: deviceA PermissionCheck not pass test, SYNC_MODE_PUSH_ONLY
152 * @tc.type: FUNC
153 * @tc.require:
154 * @tc.author: wangchuanqing
155 */
156 HWTEST_F(DistributedDBSingleVerP2PPermissionSyncTest, PermissionCheck001, TestSize.Level3)
157 {
158 /**
159 * @tc.steps: step1. SetPermissionCheckCallback
160 * @tc.expected: step1. return OK.
161 */
162 auto permissionCheckCallback = [] (const std::string &userId, const std::string &appId, const std::string &storeId,
__anonbebe21540302(const std::string &userId, const std::string &appId, const std::string &storeId, const std::string &deviceId, uint8_t flag) 163 const std::string &deviceId, uint8_t flag) -> bool {
164 if (flag & CHECK_FLAG_SEND) {
165 LOGD("in RunPermissionCheck callback func, check not pass, flag:%d", flag);
166 return false;
167 } else {
168 LOGD("in RunPermissionCheck callback func, check pass, flag:%d", flag);
169 return true;
170 }
171 };
172 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(permissionCheckCallback), OK);
173 DBStatus status = OK;
174 std::vector<std::string> devices;
175 devices.push_back(g_deviceB->GetDeviceId());
176 devices.push_back(g_deviceC->GetDeviceId());
177
178 /**
179 * @tc.steps: step2. deviceA put {k1, v1}
180 */
181 Key key = {'1'};
182 Value value = {'1'};
183 status = g_kvDelegatePtr->Put(key, value);
184 ASSERT_TRUE(status == OK);
185
186 /**
187 * @tc.steps: step3. deviceA call sync and wait
188 * @tc.expected: step3. sync should return OK.
189 */
190 std::map<std::string, DBStatus> result;
191 status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PUSH_ONLY, result);
192 ASSERT_TRUE(status == OK);
193
194 /**
195 * @tc.expected: step3. onComplete should be called,
196 * status == PERMISSION_CHECK_FORBID_SYNC, deviceB and deviceC do not have {k1, v1}
197 */
198 ASSERT_TRUE(result.size() == devices.size());
199 for (const auto &pair : result) {
200 LOGD("dev %s, status %d", pair.first.c_str(), pair.second);
201 EXPECT_TRUE(pair.second == PERMISSION_CHECK_FORBID_SYNC);
202 }
203 VirtualDataItem item;
204 g_deviceB->GetData(key, item);
205 EXPECT_TRUE(item.value.empty());
206 g_deviceC->GetData(key, item);
207 EXPECT_TRUE(item.value.empty());
208 PermissionCheckCallbackV2 nullCallback;
209 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(nullCallback), OK);
210 }
211
212 /**
213 * @tc.name: PermissionCheck002
214 * @tc.desc: deviceA PermissionCheck not pass test, SYNC_MODE_PULL_ONLY
215 * @tc.type: FUNC
216 * @tc.require:
217 * @tc.author: wangchuanqing
218 */
219 HWTEST_F(DistributedDBSingleVerP2PPermissionSyncTest, PermissionCheck002, TestSize.Level3)
220 {
221 /**
222 * @tc.steps: step1. SetPermissionCheckCallback
223 * @tc.expected: step1. return OK.
224 */
225 auto permissionCheckCallback = [] (const std::string &userId, const std::string &appId, const std::string &storeId,
__anonbebe21540402(const std::string &userId, const std::string &appId, const std::string &storeId, const std::string &deviceId, uint8_t flag) 226 const std::string &deviceId, uint8_t flag) -> bool {
227 if (flag & CHECK_FLAG_RECEIVE) {
228 LOGD("in RunPermissionCheck callback func, check not pass, flag:%d", flag);
229 return false;
230 } else {
231 LOGD("in RunPermissionCheck callback func, check pass, flag:%d", flag);
232 return true;
233 }
234 };
235
236 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(permissionCheckCallback), OK);
237
238 std::vector<std::string> devices;
239 devices.push_back(g_deviceB->GetDeviceId());
240 devices.push_back(g_deviceC->GetDeviceId());
241
242 /**
243 * @tc.steps: step2. deviceB put {k1, v1}
244 */
245 Key key = {'1'};
246 Value value = {'1'};
247 g_deviceB->PutData(key, value, 0, 0);
248
249 /**
250 * @tc.steps: step2. deviceB put {k2, v2}
251 */
252 Key key2 = {'2'};
253 Value value2 = {'2'};
254 g_deviceC->PutData(key2, value2, 0, 0);
255
256 /**
257 * @tc.steps: step3. deviceA call pull sync
258 * @tc.expected: step3. sync should return OK.
259 */
260 std::map<std::string, DBStatus> result;
261 DBStatus status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PULL_ONLY, result);
262 ASSERT_TRUE(status == OK);
263
264 /**
265 * @tc.expected: step3. onComplete should be called,
266 * status == PERMISSION_CHECK_FORBID_SYNC, DeviceA do not have {k1, VALUE_1}, {K2. VALUE_2}
267 */
268 ASSERT_TRUE(result.size() == devices.size());
269 for (const auto &pair : result) {
270 LOGD("dev %s, status %d", pair.first.c_str(), pair.second);
271 EXPECT_TRUE(pair.second == PERMISSION_CHECK_FORBID_SYNC);
272 }
273 Value value3;
274 EXPECT_EQ(g_kvDelegatePtr->Get(key, value3), NOT_FOUND);
275 EXPECT_EQ(g_kvDelegatePtr->Get(key2, value3), NOT_FOUND);
276 PermissionCheckCallbackV2 nullCallback;
277 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(nullCallback), OK);
278 }
279
280 /**
281 * @tc.name: PermissionCheck003
282 * @tc.desc: deviceA PermissionCheck not pass test, SYNC_MODE_PUSH_PULL
283 * @tc.type: FUNC
284 * @tc.require:
285 * @tc.author: wangchuanqing
286 */
287 HWTEST_F(DistributedDBSingleVerP2PPermissionSyncTest, PermissionCheck003, TestSize.Level3)
288 {
289 /**
290 * @tc.steps: step1. SetPermissionCheckCallback
291 * @tc.expected: step1. return OK.
292 */
293 auto permissionCheckCallback = [] (const std::string &userId, const std::string &appId, const std::string &storeId,
__anonbebe21540502(const std::string &userId, const std::string &appId, const std::string &storeId, const std::string &deviceId, uint8_t flag) 294 const std::string &deviceId, uint8_t flag) -> bool {
295 if (flag & (CHECK_FLAG_SEND | CHECK_FLAG_RECEIVE)) {
296 LOGD("in RunPermissionCheck callback func, check not pass, flag:%d", flag);
297 return false;
298 } else {
299 LOGD("in RunPermissionCheck callback func, check pass, flag:%d", flag);
300 return true;
301 }
302 };
303 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(permissionCheckCallback), OK);
304
305 std::vector<std::string> devices;
306 devices.push_back(g_deviceB->GetDeviceId());
307 devices.push_back(g_deviceC->GetDeviceId());
308
309 /**
310 * @tc.steps: step2. deviceA put {k1, v1}
311 */
312 Key key1 = {'1'};
313 Value value1 = {'1'};
314 DBStatus status = g_kvDelegatePtr->Put(key1, value1);
315 EXPECT_TRUE(status == OK);
316
317 /**
318 * @tc.steps: step2. deviceB put {k2, v2}
319 */
320 Key key2 = {'2'};
321 Value value2 = {'2'};
322 g_deviceB->PutData(key2, value2, 0, 0);
323
324 /**
325 * @tc.steps: step2. deviceB put {k3, v3}
326 */
327 Key key3 = {'3'};
328 Value value3 = {'3'};
329 g_deviceC->PutData(key3, value3, 0, 0);
330
331 /**
332 * @tc.steps: step3. deviceA call push_pull sync
333 * @tc.expected: step3. sync should return OK.
334 * onComplete should be called, status == PERMISSION_CHECK_FORBID_SYNC
335 */
336 std::map<std::string, DBStatus> result;
337 status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PUSH_PULL, result);
338 ASSERT_TRUE(status == OK);
339
340 ASSERT_TRUE(result.size() == devices.size());
341 for (const auto &pair : result) {
342 LOGD("dev %s, status %d", pair.first.c_str(), pair.second);
343 EXPECT_TRUE(pair.second == PERMISSION_CHECK_FORBID_SYNC);
344 }
345
346 /**
347 * @tc.expected: step3. DeviceA only have {k1. v1}
348 * DeviceB only have {k2. v2}, DeviceC only have {k3. v3}
349 */
350 Value value4;
351 EXPECT_TRUE(g_kvDelegatePtr->Get(key2, value4) == NOT_FOUND);
352 EXPECT_TRUE(g_kvDelegatePtr->Get(key3, value4) == NOT_FOUND);
353
354 VirtualDataItem item1;
355 g_deviceB->GetData(key1, item1);
356 EXPECT_TRUE(item1.value.empty());
357 g_deviceB->GetData(key3, item1);
358 EXPECT_TRUE(item1.value.empty());
359
360 VirtualDataItem item2;
361 g_deviceC->GetData(key1, item2);
362 EXPECT_TRUE(item1.value.empty());
363 g_deviceC->GetData(key2, item2);
364 EXPECT_TRUE(item2.value.empty());
365 PermissionCheckCallbackV2 nullCallback;
366 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(nullCallback), OK);
367 }
368
369 /**
370 * @tc.name: PermissionCheck004
371 * @tc.desc: deviceB and deviceC PermissionCheck not pass test, SYNC_MODE_PUSH_ONLY
372 * @tc.type: FUNC
373 * @tc.require:
374 * @tc.author: wangchuanqing
375 */
376 HWTEST_F(DistributedDBSingleVerP2PPermissionSyncTest, PermissionCheck004, TestSize.Level3)
377 {
378 /**
379 * @tc.steps: step1. SetPermissionCheckCallback
380 * @tc.expected: step1. return OK.
381 */
382 auto permissionCheckCallback = [] (const std::string &userId, const std::string &appId, const std::string &storeId,
__anonbebe21540602(const std::string &userId, const std::string &appId, const std::string &storeId, const std::string &deviceId, uint8_t flag) 383 const std::string &deviceId, uint8_t flag) -> bool {
384 if (flag & CHECK_FLAG_RECEIVE) {
385 LOGD("in RunPermissionCheck callback func, check not pass, flag:%d", flag);
386 return false;
387 } else {
388 LOGD("in RunPermissionCheck callback func, check pass, flag:%d", flag);
389 return true;
390 }
391 };
392 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(permissionCheckCallback), OK);
393 DBStatus status = OK;
394 std::vector<std::string> devices;
395 devices.push_back(g_deviceB->GetDeviceId());
396 devices.push_back(g_deviceC->GetDeviceId());
397
398 /**
399 * @tc.steps: step2. deviceA put {k1, v1}
400 */
401 Key key = {'1'};
402 Value value = {'1'};
403 status = g_kvDelegatePtr->Put(key, value);
404 ASSERT_TRUE(status == OK);
405
406 /**
407 * @tc.steps: step3. deviceA call sync and wait
408 * @tc.expected: step3. sync should return OK.
409 */
410 std::map<std::string, DBStatus> result;
411 status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PUSH_ONLY, result);
412 ASSERT_TRUE(status == OK);
413
414 /**
415 * @tc.expected: step3. onComplete should be called,
416 * status == PERMISSION_CHECK_FORBID_SYNC, deviceB and deviceC do not have {k1, v1}
417 */
418 ASSERT_TRUE(result.size() == devices.size());
419 for (const auto &pair : result) {
420 LOGD("dev %s, status %d", pair.first.c_str(), pair.second);
421 EXPECT_TRUE(pair.second == PERMISSION_CHECK_FORBID_SYNC);
422 }
423 VirtualDataItem item;
424 g_deviceB->GetData(key, item);
425 EXPECT_TRUE(item.value.empty());
426 g_deviceC->GetData(key, item);
427 EXPECT_TRUE(item.value.empty());
428 PermissionCheckCallbackV2 nullCallback;
429 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(nullCallback), OK);
430 }
431
432 /**
433 * @tc.name: PermissionCheck005
434 * @tc.desc: deviceB and deviceC PermissionCheck not pass test, SYNC_MODE_PULL_ONLY
435 * @tc.type: FUNC
436 * @tc.require:
437 * @tc.author: wangchuanqing
438 */
439 HWTEST_F(DistributedDBSingleVerP2PPermissionSyncTest, PermissionCheck005, TestSize.Level3)
440 {
441 /**
442 * @tc.steps: step1. SetPermissionCheckCallback
443 * @tc.expected: step1. return OK.
444 */
445 auto permissionCheckCallback = [] (const std::string &userId, const std::string &appId, const std::string &storeId,
__anonbebe21540702(const std::string &userId, const std::string &appId, const std::string &storeId, const std::string &deviceId, uint8_t flag) 446 const std::string &deviceId, uint8_t flag) -> bool {
447 if (flag & CHECK_FLAG_SEND) {
448 LOGD("in RunPermissionCheck callback func, check not pass, flag:%d", flag);
449 return false;
450 } else {
451 LOGD("in RunPermissionCheck callback func, check pass, flag:%d", flag);
452 return true;
453 }
454 };
455 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(permissionCheckCallback), OK);
456
457 std::vector<std::string> devices;
458 devices.push_back(g_deviceB->GetDeviceId());
459 devices.push_back(g_deviceC->GetDeviceId());
460
461 /**
462 * @tc.steps: step2. deviceB put {k1, v1}
463 */
464 Key key = {'1'};
465 Value value = {'1'};
466 g_deviceB->PutData(key, value, 0, 0);
467
468 /**
469 * @tc.steps: step2. deviceB put {k2, v2}
470 */
471 Key key2 = {'2'};
472 Value value2 = {'2'};
473 g_deviceC->PutData(key2, value2, 0, 0);
474
475 /**
476 * @tc.steps: step3. deviceA call pull sync
477 * @tc.expected: step3. sync should return OK.
478 */
479 std::map<std::string, DBStatus> result;
480 DBStatus status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PULL_ONLY, result);
481 ASSERT_TRUE(status == OK);
482
483 /**
484 * @tc.expected: step3. onComplete should be called,
485 * status == PERMISSION_CHECK_FORBID_SYNC, DeviceA do not have {k1, VALUE_1}, {K2. VALUE_2}
486 */
487 ASSERT_TRUE(result.size() == devices.size());
488 for (const auto &pair : result) {
489 LOGD("dev %s, status %d", pair.first.c_str(), pair.second);
490 EXPECT_TRUE(pair.second == PERMISSION_CHECK_FORBID_SYNC);
491 }
492 Value value3;
493 EXPECT_EQ(g_kvDelegatePtr->Get(key, value3), NOT_FOUND);
494 EXPECT_EQ(g_kvDelegatePtr->Get(key2, value3), NOT_FOUND);
495 PermissionCheckCallbackV2 nullCallback;
496 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(nullCallback), OK);
497 }
498
499 /**
500 * @tc.name: PermissionCheck006
501 * @tc.desc: deviceA PermissionCheck deviceB not pass, deviceC pass
502 * @tc.type: FUNC
503 * @tc.require:
504 * @tc.author: wangchuanqing
505 */
506 HWTEST_F(DistributedDBSingleVerP2PPermissionSyncTest, PermissionCheck006, TestSize.Level3)
507 {
508 /**
509 * @tc.steps: step1. SetPermissionCheckCallback
510 * @tc.expected: step1. return OK.
511 */
512 auto permissionCheckCallback = [] (const std::string &userId, const std::string &appId, const std::string &storeId,
__anonbebe21540802(const std::string &userId, const std::string &appId, const std::string &storeId, const std::string &deviceId, uint8_t flag) 513 const std::string &deviceId, uint8_t flag) -> bool {
514 if (deviceId == g_deviceB->GetDeviceId()) {
515 LOGD("in RunPermissionCheck callback func, check not pass, flag:%d", flag);
516 return false;
517 } else {
518 LOGD("in RunPermissionCheck callback func, check pass, flag:%d", flag);
519 return true;
520 }
521 };
522 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(permissionCheckCallback), OK);
523 DBStatus status = OK;
524 std::vector<std::string> devices;
525 devices.push_back(g_deviceB->GetDeviceId());
526 devices.push_back(g_deviceC->GetDeviceId());
527
528 /**
529 * @tc.steps: step2. deviceA put {k1, v1}
530 */
531 Key key = {'1'};
532 Value value = {'1'};
533 status = g_kvDelegatePtr->Put(key, value);
534 ASSERT_TRUE(status == OK);
535
536 /**
537 * @tc.steps: step3. deviceA call sync and wait
538 * @tc.expected: step3. sync should return OK.
539 */
540 std::map<std::string, DBStatus> result;
541 status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PUSH_ONLY, result);
542 ASSERT_TRUE(status == OK);
543
544 /**
545 * @tc.expected: step3. onComplete should be called,
546 * status == PERMISSION_CHECK_FORBID_SYNC, deviceB and deviceC do not have {k1, v1}
547 */
548 ASSERT_TRUE(result.size() == devices.size());
549 for (const auto &pair : result) {
550 LOGD("dev %s, status %d", pair.first.c_str(), pair.second);
551 if (g_deviceB->GetDeviceId() == pair.first) {
552 EXPECT_TRUE(pair.second == PERMISSION_CHECK_FORBID_SYNC);
553 } else {
554 EXPECT_TRUE(pair.second == OK);
555 }
556 }
557 VirtualDataItem item;
558 g_deviceB->GetData(key, item);
559 EXPECT_TRUE(item.value.empty());
560 g_deviceC->GetData(key, item);
561 EXPECT_TRUE(item.value == value);
562 PermissionCheckCallbackV2 nullCallback;
563 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(nullCallback), OK);
564 }
565
566 /**
567 * @tc.name: PermissionCheck007
568 * @tc.desc: deviceA PermissionCheck, deviceB not pass, deviceC pass in SYNC_MODE_AUTO_PUSH
569 * @tc.type: FUNC
570 * @tc.require:
571 * @tc.author: zhuwentao
572 */
573 HWTEST_F(DistributedDBSingleVerP2PPermissionSyncTest, PermissionCheck007, TestSize.Level3)
574 {
575 /**
576 * @tc.steps: step1. SetPermissionCheckCallback
577 * @tc.expected: step1. return OK.
578 */
579 auto permissionCheckCallback = [] (const std::string &userId, const std::string &appId, const std::string &storeId,
__anonbebe21540902(const std::string &userId, const std::string &appId, const std::string &storeId, const std::string &deviceId, uint8_t flag) 580 const std::string &deviceId, uint8_t flag) -> bool {
581 if (deviceId == g_deviceC->GetDeviceId() &&
582 (flag & (CHECK_FLAG_RECEIVE | CHECK_FLAG_AUTOSYNC))) {
583 LOGD("in RunPermissionCheck callback func, check not pass, flag:%d", flag);
584 return false;
585 } else {
586 LOGD("in RunPermissionCheck callback func, check pass, flag:%d", flag);
587 return true;
588 }
589 };
590 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(permissionCheckCallback), OK);
591 DBStatus status = OK;
592 std::vector<std::string> devices;
593 devices.push_back(g_deviceB->GetDeviceId());
594 devices.push_back(g_deviceC->GetDeviceId());
595 /**
596 * @tc.steps: step2. deviceA set auto sync
597 */
598 bool autoSync = true;
599 PragmaData data = static_cast<PragmaData>(&autoSync);
600 status = g_kvDelegatePtr->Pragma(AUTO_SYNC, data);
601 ASSERT_EQ(status, OK);
602
603 /**
604 * @tc.steps: step3. deviceA put {k1, v1}, and sleep 1s
605 */
606 Key key = {'1'};
607 Value value = {'1'};
608 status = g_kvDelegatePtr->Put(key, value);
609 ASSERT_TRUE(status == OK);
610 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
611
612 /**
613 * @tc.steps: step3. check value in device B and not in device C.
614 */
615 VirtualDataItem item;
616 g_deviceC->GetData(key, item);
617 EXPECT_TRUE(item.value.empty());
618 g_deviceB->GetData(key, item);
619 EXPECT_TRUE(item.value == value);
620 PermissionCheckCallbackV2 nullCallback;
621 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(nullCallback), OK);
622 }
623
624 /**
625 * @tc.name: PermissionCheck008
626 * @tc.desc: deviceA PermissionCheck, deviceB not pass, deviceC pass in SYNC_MODE_AUTO_PULL
627 * @tc.type: FUNC
628 * @tc.require:
629 * @tc.author: zhangqiquan
630 */
631 HWTEST_F(DistributedDBSingleVerP2PPermissionSyncTest, PermissionCheck008, TestSize.Level3)
632 {
633 /**
634 * @tc.steps: step1. SetPermissionCheckCallback
635 * @tc.expected: step1. return OK.
636 */
637 auto permissionCheckCallback = [] (const std::string &userId, const std::string &appId, const std::string &storeId,
__anonbebe21540a02(const std::string &userId, const std::string &appId, const std::string &storeId, const std::string &deviceId, uint8_t flag) 638 const std::string &deviceId, uint8_t flag) -> bool {
639 if (deviceId == g_deviceC->GetDeviceId() &&
640 (flag & CHECK_FLAG_SPONSOR)) {
641 LOGD("in RunPermissionCheck callback func, check not pass, flag:%d", flag);
642 return false;
643 } else {
644 LOGD("in RunPermissionCheck callback func, check pass, flag:%d", flag);
645 return true;
646 }
647 };
648 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(permissionCheckCallback), OK);
649 std::vector<std::string> devices;
650 devices.push_back(g_deviceB->GetDeviceId());
651 devices.push_back(g_deviceC->GetDeviceId());
652
653 /**
654 * @tc.steps: step2. deviceB put {k1, v1}
655 */
656 Key key = {'1'};
657 Value value = {'1'};
658 g_deviceB->PutData(key, value, 0, 0);
659
660 /**
661 * @tc.steps: step2. device put {k2, v2}
662 */
663 Key key2 = {'2'};
664 Value value2 = {'2'};
665 g_deviceC->PutData(key2, value2, 0, 0);
666
667 /**
668 * @tc.steps: step3. deviceA call push sync
669 * @tc.expected: step3. sync should return OK.
670 */
671 std::map<std::string, DBStatus> result;
672 DBStatus status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PULL_ONLY, result);
673 ASSERT_TRUE(status == OK);
674 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
675
676 /**
677 * @tc.expected: step4. onComplete should be called,
678 * status == PERMISSION_CHECK_FORBID_SYNC, deviceB and deviceC do not have {k1, v1}
679 */
680 ASSERT_TRUE(result.size() == devices.size());
681 for (const auto &pair : result) {
682 LOGD("dev %s, status %d", pair.first.c_str(), pair.second);
683 if (g_deviceC->GetDeviceId() == pair.first) {
684 EXPECT_TRUE(pair.second == PERMISSION_CHECK_FORBID_SYNC);
685 } else {
686 EXPECT_TRUE(pair.second == OK);
687 }
688 }
689 /**
690 * @tc.steps: step5. check value in device A
691 */
692 Value value4;
693 EXPECT_TRUE(g_kvDelegatePtr->Get(key, value4) == OK);
694 EXPECT_TRUE(g_kvDelegatePtr->Get(key2, value4) == NOT_FOUND);
695 PermissionCheckCallbackV2 nullCallback;
696 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(nullCallback), OK);
697 }
698
699 /**
700 * @tc.name: PermissionCheck009
701 * @tc.desc: different runpermissioncheck call return different value
702 * @tc.type: FUNC
703 * @tc.require:
704 * @tc.author: zhuwentao
705 */
706 HWTEST_F(DistributedDBSingleVerP2PPermissionSyncTest, PermissionCheck009, TestSize.Level3)
707 {
708 /**
709 * @tc.steps: step1. SetPermissionCheckCallback
710 * @tc.expected: step1. return OK.
711 */
712 int count = 1;
713 auto permissionCheckCallback = [&count] (const std::string &userId, const std::string &appId,
__anonbebe21540b02(const std::string &userId, const std::string &appId, const std::string &storeId, const std::string &deviceId, uint8_t flag) 714 const std::string &storeId, const std::string &deviceId, uint8_t flag) -> bool {
715 (void)userId;
716 (void)appId;
717 (void)storeId;
718 (void)deviceId;
719 if (flag & CHECK_FLAG_SEND) {
720 bool result = count % 2;
721 LOGD("in RunPermissionCheck callback, check result:%d, flag:%d", result, flag);
722 count++;
723 return result;
724 }
725 LOGD("in RunPermissionCheck callback, check pass, flag:%d", flag);
726 return true;
727 };
728 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(permissionCheckCallback), OK);
729 std::vector<std::string> devices = {g_deviceB->GetDeviceId()};
730 /**
731 * @tc.steps: step2. deviceA call sync three times and not wait
732 * @tc.expected: step2. sync should return OK.
733 */
734 std::map<std::string, DBStatus> result;
735 ASSERT_TRUE(g_kvDelegatePtr->Sync(devices, SYNC_MODE_PUSH_ONLY,
__anonbebe21540c02(const std::map<std::string, DBStatus>& statusMap) 736 [&result](const std::map<std::string, DBStatus>& statusMap) {
737 result = statusMap;
738 }, false) == OK);
739 std::map<std::string, DBStatus> result2;
740 ASSERT_TRUE(g_kvDelegatePtr->Sync(devices, SYNC_MODE_PUSH_ONLY,
__anonbebe21540d02(const std::map<std::string, DBStatus>& statusMap) 741 [&result2](const std::map<std::string, DBStatus>& statusMap) {
742 result2 = statusMap;
743 }, false) == OK);
744 std::map<std::string, DBStatus> result3;
745 ASSERT_TRUE(g_kvDelegatePtr->Sync(devices, SYNC_MODE_PUSH_ONLY,
__anonbebe21540e02(const std::map<std::string, DBStatus>& statusMap) 746 [&result3](const std::map<std::string, DBStatus>& statusMap) {
747 result3 = statusMap;
748 }, false) == OK);
749 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
750 /**
751 * @tc.expected: step3. onComplete should be called,
752 * status is : OK, PERMISSION_CHECK_FORBID_SYNC, OK
753 */
754 ASSERT_TRUE(result.size() == devices.size());
755 for (const auto &pair : result) {
756 EXPECT_TRUE(pair.second == OK);
757 }
758 ASSERT_TRUE(result2.size() == devices.size());
759 for (const auto &pair : result2) {
760 EXPECT_TRUE(pair.second == PERMISSION_CHECK_FORBID_SYNC);
761 }
762 ASSERT_TRUE(result3.size() == devices.size());
763 for (const auto &pair : result3) {
764 EXPECT_TRUE(pair.second == OK);
765 }
766 PermissionCheckCallbackV2 nullCallback;
767 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(nullCallback), OK);
768 }
769
770 /**
771 * @tc.name: PermissionCheck010
772 * @tc.desc: permission check cost lot of time and return false
773 * @tc.type: FUNC
774 * @tc.require:
775 * @tc.author: zhangqiquan
776 */
777 HWTEST_F(DistributedDBSingleVerP2PPermissionSyncTest, PermissionCheck010, TestSize.Level3)
778 {
779 /**
780 * @tc.steps: step1. SetPermissionCheckCallback
781 * @tc.expected: step1. return OK.
782 */
783 int count = 0;
784 auto permissionCheckCallback = [&count] (const std::string &userId, const std::string &appId,
__anonbebe21540f02(const std::string &userId, const std::string &appId, const std::string &storeId, const std::string &deviceId, uint8_t flag) 785 const std::string &storeId, const std::string &deviceId, uint8_t flag) -> bool {
786 std::this_thread::sleep_for(std::chrono::seconds(1));
787 count++;
788 LOGD("check permission %d", count);
789 return count > 1;
790 };
791 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(permissionCheckCallback), OK);
792 /**
793 * @tc.steps: step2. put (k1, v1)
794 * @tc.expected: step2. return OK.
795 */
796 Key k1 = {'k', '1'};
797 Value v1 = {'v', '1'};
798 EXPECT_EQ(g_kvDelegatePtr->Put(k1, v1), OK);
799 /**
800 * @tc.steps: step3. sync to DEVICE_B twice
801 * @tc.expected: step3. return OK.
802 */
803 std::vector<std::string> devices;
804 devices.push_back(DEVICE_B);
805 EXPECT_TRUE(g_kvDelegatePtr->Sync(devices, SYNC_MODE_PUSH_ONLY, nullptr, false) == OK);
806 EXPECT_TRUE(g_kvDelegatePtr->Sync(devices, SYNC_MODE_PUSH_ONLY, nullptr, true) == OK);
807 /**
808 * @tc.steps: step4. (k1, v1) exist in DeviceB
809 * @tc.expected: step4. get return OK.
810 */
811 VirtualDataItem actualValue;
812 EXPECT_EQ(g_deviceB->GetData(k1, actualValue), E_OK);
813 EXPECT_EQ(v1, actualValue.value);
814 PermissionCheckCallbackV2 nullCallback = nullptr;
815 EXPECT_EQ(g_mgr.SetPermissionCheckCallback(nullCallback), OK);
816 }