1 /*
2  * Copyright (c) 2024 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 #define LOG_TAG "ObjectManagerTest"
17 
18 #include "object_manager.h"
19 
20 #include <gtest/gtest.h>
21 #include <ipc_skeleton.h>
22 
23 #include "executor_pool.h"
24 #include "kv_store_nb_delegate_mock.h"
25 #include "object_types.h"
26 #include "snapshot/machine_status.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS::DistributedObject;
30 using AssetValue = OHOS::CommonType::AssetValue;
31 using RestoreStatus = OHOS::DistributedObject::ObjectStoreManager::RestoreStatus;
32 namespace OHOS::Test {
33 
34 class ObjectManagerTest : public testing::Test {
35 public:
36     void SetUp();
37     void TearDown();
38 
39 protected:
40     Asset asset_;
41     std::string uri_;
42     std::string appId_ = "objectManagerTest_appid_1";
43     std::string sessionId_ = "123";
44     std::vector<uint8_t> data_;
45     std::string deviceId_ = "7001005458323933328a258f413b3900";
46     uint64_t sequenceId_ = 10;
47     uint64_t sequenceId_2 = 20;
48     uint64_t sequenceId_3 = 30;
49     std::string userId_ = "100";
50     std::string bundleName_ = "com.examples.hmos.notepad";
51     OHOS::ObjectStore::AssetBindInfo assetBindInfo_;
52     pid_t pid_ = 10;
53     uint32_t tokenId_ = 100;
54     AssetValue assetValue_;
55 };
56 
SetUp()57 void ObjectManagerTest::SetUp()
58 {
59     uri_ = "file:://com.examples.hmos.notepad/data/storage/el2/distributedfiles/dir/asset1.jpg";
60     Asset asset{
61         .name = "test_name",
62         .uri = uri_,
63         .modifyTime = "modifyTime",
64         .size = "size",
65         .hash = "modifyTime_size",
66     };
67     asset_ = asset;
68 
69     AssetValue assetValue{
70         .id = "test_name",
71         .name = uri_,
72         .uri = uri_,
73         .createTime = "2024.07.23",
74         .modifyTime = "modifyTime",
75         .size = "size",
76         .hash = "modifyTime_size",
77         .path = "/data/storage/el2",
78     };
79     assetValue_ = assetValue;
80 
81     data_.push_back(10); // 10 is for testing
82     data_.push_back(20); // 20 is for testing
83     data_.push_back(30); // 30 is for testing
84 
85     OHOS::ObjectStore::AssetBindInfo AssetBindInfo{
86         .storeName = "store_test",
87         .tableName = "table_test",
88         .field = "attachment",
89         .assetName = "asset1.jpg",
90     };
91     assetBindInfo_ = AssetBindInfo;
92 }
93 
TearDown()94 void ObjectManagerTest::TearDown() {}
95 
96 /**
97 * @tc.name: DeleteNotifier001
98 * @tc.desc: DeleteNotifier test.
99 * @tc.type: FUNC
100 * @tc.require:
101 * @tc.author: wangbin
102 */
HWTEST_F(ObjectManagerTest, DeleteNotifier001, TestSize.Level0)103 HWTEST_F(ObjectManagerTest, DeleteNotifier001, TestSize.Level0)
104 {
105     auto syncManager = SequenceSyncManager::GetInstance();
106     auto result = syncManager->DeleteNotifier(sequenceId_, userId_);
107     ASSERT_EQ(result, SequenceSyncManager::ERR_SID_NOT_EXIST);
108 }
109 
110 /**
111 * @tc.name: Process001
112 * @tc.desc: Process test.
113 * @tc.type: FUNC
114 * @tc.require:
115 * @tc.author: wangbin
116 */
HWTEST_F(ObjectManagerTest, Process001, TestSize.Level0)117 HWTEST_F(ObjectManagerTest, Process001, TestSize.Level0)
118 {
119     auto syncManager = SequenceSyncManager::GetInstance();
120     std::map<std::string, DistributedDB::DBStatus> results;
121     results = {{ "test_cloud", DistributedDB::DBStatus::OK }};
122 
123     std::function<void(const std::map<std::string, int32_t> &results)> func;
124     func = [](const std::map<std::string, int32_t> &results) {
125         return results;
126     };
127     auto result = syncManager->Process(sequenceId_, results, userId_);
128     ASSERT_EQ(result, SequenceSyncManager::ERR_SID_NOT_EXIST);
129     syncManager->seqIdCallbackRelations_.emplace(sequenceId_, func);
130     result = syncManager->Process(sequenceId_, results, userId_);
131     ASSERT_EQ(result, SequenceSyncManager::SUCCESS_USER_HAS_FINISHED);
132 }
133 
134 /**
135 * @tc.name: DeleteNotifierNoLock001
136 * @tc.desc: DeleteNotifierNoLock test.
137 * @tc.type: FUNC
138 * @tc.require:
139 * @tc.author: wangbin
140 */
HWTEST_F(ObjectManagerTest, DeleteNotifierNoLock001, TestSize.Level0)141 HWTEST_F(ObjectManagerTest, DeleteNotifierNoLock001, TestSize.Level0)
142 {
143     auto syncManager = SequenceSyncManager::GetInstance();
144     std::function<void(const std::map<std::string, int32_t> &results)> func;
145     func = [](const std::map<std::string, int32_t> &results) {
146         return results;
147     };
148     syncManager->seqIdCallbackRelations_.emplace(sequenceId_, func);
149     std::vector<uint64_t> seqIds = {sequenceId_, sequenceId_2, sequenceId_3};
150     std::string userId = "user_1";
151     auto result = syncManager->DeleteNotifierNoLock(sequenceId_, userId_);
152     ASSERT_EQ(result, SequenceSyncManager::SUCCESS_USER_HAS_FINISHED);
153     syncManager->userIdSeqIdRelations_[userId] = seqIds;
154     result = syncManager->DeleteNotifierNoLock(sequenceId_, userId_);
155     ASSERT_EQ(result, SequenceSyncManager::SUCCESS_USER_IN_USE);
156 }
157 
158 /**
159 * @tc.name: Clear001
160 * @tc.desc: Clear test.
161 * @tc.type: FUNC
162 * @tc.require:
163 * @tc.author: wangbin
164 */
HWTEST_F(ObjectManagerTest, Clear001, TestSize.Level0)165 HWTEST_F(ObjectManagerTest, Clear001, TestSize.Level0)
166 {
167     auto manager = ObjectStoreManager::GetInstance();
168     auto result = manager->Clear();
169     ASSERT_EQ(result, OHOS::DistributedObject::OBJECT_STORE_NOT_FOUND);
170 }
171 
172 /**
173 * @tc.name: registerAndUnregisterRemoteCallback001
174 * @tc.desc: test RegisterRemoteCallback and UnregisterRemoteCallback.
175 * @tc.type: FUNC
176 * @tc.require:
177 * @tc.author: wangbin
178 */
HWTEST_F(ObjectManagerTest, registerAndUnregisterRemoteCallback001, TestSize.Level0)179 HWTEST_F(ObjectManagerTest, registerAndUnregisterRemoteCallback001, TestSize.Level0)
180 {
181     auto manager = ObjectStoreManager::GetInstance();
182     sptr<IRemoteObject> callback;
183     manager->RegisterRemoteCallback(bundleName_, sessionId_, pid_, tokenId_, callback);
184     ObjectStoreManager::CallbackInfo callbackInfo = manager->callbacks_.Find(tokenId_).second;
185     std::string prefix = bundleName_ + sessionId_;
186     ASSERT_NE(callbackInfo.observers_.find(prefix), callbackInfo.observers_.end());
187     manager->UnregisterRemoteCallback(bundleName_, pid_, tokenId_, sessionId_);
188     callbackInfo = manager->callbacks_.Find(tokenId_).second;
189     ASSERT_EQ(callbackInfo.observers_.find(prefix), callbackInfo.observers_.end());
190 }
191 
192 /**
193 * @tc.name: registerAndUnregisterRemoteCallback002
194 * @tc.desc: abnormal use cases.
195 * @tc.type: FUNC
196 * @tc.require:
197 * @tc.author: wangbin
198 */
HWTEST_F(ObjectManagerTest, registerAndUnregisterRemoteCallback002, TestSize.Level0)199 HWTEST_F(ObjectManagerTest, registerAndUnregisterRemoteCallback002, TestSize.Level0)
200 {
201     auto manager = ObjectStoreManager::GetInstance();
202     sptr<IRemoteObject> callback;
203     uint32_t tokenId = 101;
204     manager->RegisterRemoteCallback("", sessionId_, pid_, tokenId, callback);
205     manager->RegisterRemoteCallback(bundleName_, "", pid_, tokenId, callback);
206     manager->RegisterRemoteCallback("", "", pid_, tokenId, callback);
207     ASSERT_EQ(manager->callbacks_.Find(tokenId).first, false);
208     manager->UnregisterRemoteCallback("", pid_, tokenId, sessionId_);
209 }
210 
211 /**
212 * @tc.name: NotifyDataChanged001
213 * @tc.desc: NotifyDataChanged test.
214 * @tc.type: FUNC
215 * @tc.require:
216 * @tc.author: wangbin
217 */
HWTEST_F(ObjectManagerTest, NotifyDataChanged001, TestSize.Level0)218 HWTEST_F(ObjectManagerTest, NotifyDataChanged001, TestSize.Level0)
219 {
220     auto manager = ObjectStoreManager::GetInstance();
221     std::string bundleName1_ = "com.examples.ophm.notepad";
222     std::string objectKey = bundleName1_ + sessionId_;
223     std::map<std::string, std::map<std::string, std::vector<uint8_t>>> data;
224     std::map<std::string, std::vector<uint8_t>> data1;
225     std::vector<uint8_t> data1_;
226     data1_.push_back(RestoreStatus::DATA_READY);
227     data1_.push_back(RestoreStatus::ASSETS_READY);
228     data1_.push_back(RestoreStatus::ALL_READY);
229     data1 = {{ "objectKey", data1_ }};
230     data = {{ objectKey, data1 }};
231     std::shared_ptr<ExecutorPool> executors = std::make_shared<ExecutorPool>(5, 3); // executor pool
232     manager->SetThreadPool(executors);
233     ASSERT_EQ(manager->restoreStatus_.Find(objectKey).first, false);
234     manager->NotifyDataChanged(data, {});
235     ASSERT_EQ(manager->restoreStatus_.Find(objectKey).second, RestoreStatus::DATA_READY);
236 }
237 
238 /**
239 * @tc.name: NotifyAssetsReady001
240 * @tc.desc: NotifyAssetsReady test.
241 * @tc.type: FUNC
242 * @tc.require:
243 * @tc.author: wangbin
244 */
HWTEST_F(ObjectManagerTest, NotifyAssetsReady001, TestSize.Level0)245 HWTEST_F(ObjectManagerTest, NotifyAssetsReady001, TestSize.Level0)
246 {
247     auto manager = ObjectStoreManager::GetInstance();
248     std::string objectKey = bundleName_ + sessionId_;
249     std::string srcNetworkId = "1";
250     ASSERT_EQ(manager->restoreStatus_.Find(objectKey).first, false);
251     manager->NotifyAssetsReady(objectKey, srcNetworkId);
252     ASSERT_EQ(manager->restoreStatus_.Find(objectKey).second, RestoreStatus::ASSETS_READY);
253     manager->restoreStatus_.Clear();
254     manager->restoreStatus_.Insert(objectKey, RestoreStatus::DATA_READY);
255     manager->NotifyAssetsReady(objectKey, srcNetworkId);
256     ASSERT_EQ(manager->restoreStatus_.Find(objectKey).second, RestoreStatus::ALL_READY);
257 }
258 
259 /**
260  * @tc.name: NotifyAssetsReady002
261  * @tc.desc: NotifyAssetsReady test.
262  * @tc.type: FUNC
263  */
HWTEST_F(ObjectManagerTest, NotifyAssetsReady002, TestSize.Level0)264 HWTEST_F(ObjectManagerTest, NotifyAssetsReady002, TestSize.Level0)
265 {
266     auto manager = ObjectStoreManager::GetInstance();
267     std::string objectKey="com.example.myapplicaiton123456";
268     std::string srcNetworkId = "654321";
269 
270     manager->restoreStatus_.Clear();
271     manager->NotifyAssetsStart(objectKey, srcNetworkId);
272     auto [has0, value0] = manager->restoreStatus_.Find(objectKey);
273     EXPECT_TRUE(has0);
274     EXPECT_EQ(value0, RestoreStatus::NONE);
275 
276     manager->restoreStatus_.Clear();
277     manager->NotifyAssetsReady(objectKey, srcNetworkId);
278     auto [has1, value1] = manager->restoreStatus_.Find(objectKey);
279     EXPECT_TRUE(has1);
280     EXPECT_EQ(value1, RestoreStatus::ASSETS_READY);
281 
282     manager->restoreStatus_.Clear();
283     manager->restoreStatus_.Insert(objectKey, RestoreStatus::DATA_NOTIFIED);
284     manager->NotifyAssetsReady(objectKey, srcNetworkId);
285     auto [has2, value2] = manager->restoreStatus_.Find(objectKey);
286     EXPECT_TRUE(has2);
287     EXPECT_EQ(value2, RestoreStatus::ALL_READY);
288 
289     manager->restoreStatus_.Clear();
290     manager->restoreStatus_.Insert(objectKey, RestoreStatus::DATA_READY);
291     manager->NotifyAssetsReady(objectKey, srcNetworkId);
292     auto [has3, value3] = manager->restoreStatus_.Find(objectKey);
293     EXPECT_TRUE(has3);
294     EXPECT_EQ(value3, RestoreStatus::ALL_READY);
295     manager->restoreStatus_.Clear();
296 }
297 
298 /**
299 * @tc.name: NotifyChange001
300 * @tc.desc: NotifyChange test.
301 * @tc.type: FUNC
302 * @tc.require:
303 * @tc.author: wangbin
304 */
HWTEST_F(ObjectManagerTest, NotifyChange001, TestSize.Level0)305 HWTEST_F(ObjectManagerTest, NotifyChange001, TestSize.Level0)
306 {
307     auto manager = ObjectStoreManager::GetInstance();
308     std::map<std::string, std::vector<uint8_t>> data;
309     std::map<std::string, std::vector<uint8_t>> data1;
310     std::vector<uint8_t> data1_;
311     data1_.push_back(RestoreStatus::DATA_READY);
312     data_.push_back(RestoreStatus::ALL_READY);
313     data = {{ "test_cloud", data_ }};
314     data1 = {{ "p_###SAVEINFO###001", data1_ }};
315     manager->NotifyChange(data1);
316     manager->NotifyChange(data);
317 }
318 
319 /**
320  * @tc.name: NotifyChange002
321  * @tc.desc: NotifyChange test.
322  * @tc.type: FUNC
323  */
HWTEST_F(ObjectManagerTest, NotifyChange002, TestSize.Level0)324 HWTEST_F(ObjectManagerTest, NotifyChange002, TestSize.Level0)
325 {
326     auto manager = ObjectStoreManager::GetInstance();
327     std::shared_ptr<ExecutorPool> executor = std::make_shared<ExecutorPool>(1, 0);
328     manager->SetThreadPool(executor);
329     std::map<std::string, std::vector<uint8_t>> data{};
330     std::vector<uint8_t> value{0};
331     std::string bundleName = "com.example.myapplication";
332     std::string sessionId = "123456";
333     std::string source = "source";
334     std::string target = "target";
335     std::string timestamp = "1234567890";
336     ObjectStoreManager::SaveInfo saveInfo(bundleName, sessionId, source, target, timestamp);
337     std::string saveInfoStr = DistributedData::Serializable::Marshall(saveInfo);
338     auto saveInfoValue = std::vector<uint8_t>(saveInfoStr.begin(), saveInfoStr.end());
339     std::string prefix = saveInfo.ToPropertyPrefix();
340     std::string assetPrefix = prefix + "p_asset";
341     data.insert_or_assign(prefix + "p_###SAVEINFO###", saveInfoValue);
342     data.insert_or_assign(prefix + "p_data", value);
343     data.insert_or_assign(assetPrefix + ObjectStore::NAME_SUFFIX, value);
344     data.insert_or_assign(assetPrefix + ObjectStore::URI_SUFFIX, value);
345     data.insert_or_assign(assetPrefix + ObjectStore::PATH_SUFFIX, value);
346     data.insert_or_assign(assetPrefix + ObjectStore::CREATE_TIME_SUFFIX, value);
347     data.insert_or_assign(assetPrefix + ObjectStore::MODIFY_TIME_SUFFIX, value);
348     data.insert_or_assign(assetPrefix + ObjectStore::SIZE_SUFFIX, value);
349     data.insert_or_assign("testkey", value);
350     manager->NotifyChange(data);
351     EXPECT_TRUE(manager->restoreStatus_.Contains(bundleName+sessionId));
352     auto [has, taskId] = manager->objectTimer_.Find(bundleName+sessionId);
353     EXPECT_TRUE(has);
354     manager->restoreStatus_.Clear();
355     manager->executors_->Remove(taskId);
356     manager->objectTimer_.Clear();
357 }
358 
359 /**
360  * @tc.name: ComputeStatus001
361  * @tc.desc: ComputeStatus.test
362  * @tc.type: FUNC
363  */
HWTEST_F(ObjectManagerTest, ComputeStatus001, TestSize.Level0)364 HWTEST_F(ObjectManagerTest, ComputeStatus001, TestSize.Level0)
365 {
366     auto manager = ObjectStoreManager::GetInstance();
367     std::shared_ptr<ExecutorPool> executor = std::make_shared<ExecutorPool>(1, 0);
368     manager->SetThreadPool(executor);
369     std::string objectKey="com.example.myapplicaiton123456";
370     std::map<std::string, std::map<std::string, std::vector<uint8_t>>> data{};
371     manager->restoreStatus_.Clear();
372     manager->ComputeStatus(objectKey, {}, data);
373     auto [has0, value0] = manager->restoreStatus_.Find(objectKey);
374     EXPECT_TRUE(has0);
375     EXPECT_EQ(value0, RestoreStatus::DATA_READY);
376     auto [has1, taskId1] = manager->objectTimer_.Find(objectKey);
377     EXPECT_TRUE(has1);
378     manager->executors_->Remove(taskId1);
379     manager->objectTimer_.Clear();
380     manager->restoreStatus_.Clear();
381 
382     manager->restoreStatus_.Insert(objectKey, RestoreStatus::ASSETS_READY);
383     manager->ComputeStatus(objectKey, {}, data);
384     auto [has2, value2] = manager->restoreStatus_.Find(objectKey);
385     EXPECT_TRUE(has2);
386     EXPECT_EQ(value2, RestoreStatus::ALL_READY);
387     auto [has3, taskId3] = manager->objectTimer_.Find(objectKey);
388     EXPECT_FALSE(has3);
389     manager->restoreStatus_.Clear();
390 }
391 
392 /**
393 * @tc.name: Open001
394 * @tc.desc: Open test.
395 * @tc.type: FUNC
396 * @tc.require:
397 * @tc.author: wangbin
398 */
HWTEST_F(ObjectManagerTest, Open001, TestSize.Level0)399 HWTEST_F(ObjectManagerTest, Open001, TestSize.Level0)
400 {
401     auto manager = ObjectStoreManager::GetInstance();
402     manager->kvStoreDelegateManager_ = nullptr;
403     auto result = manager->Open();
404     ASSERT_EQ(result, DistributedObject::OBJECT_INNER_ERROR);
405     std::string dataDir = "/data/app/el2/100/database";
406     manager->SetData(dataDir, userId_);
407     manager->delegate_ = nullptr;
408     result = manager->Open();
409     ASSERT_EQ(result, DistributedObject::OBJECT_SUCCESS);
410     manager->delegate_ = manager->OpenObjectKvStore();
411     result = manager->Open();
412     ASSERT_EQ(result, DistributedObject::OBJECT_SUCCESS);
413 }
414 
415 /**
416 * @tc.name: OnAssetChanged001
417 * @tc.desc: OnAssetChanged test.
418 * @tc.type: FUNC
419 * @tc.require:
420 * @tc.author: wangbin
421 */
HWTEST_F(ObjectManagerTest, OnAssetChanged001, TestSize.Level0)422 HWTEST_F(ObjectManagerTest, OnAssetChanged001, TestSize.Level0)
423 {
424     auto manager = ObjectStoreManager::GetInstance();
425     std::shared_ptr<Snapshot> snapshot = std::make_shared<ObjectSnapshot>();
426     auto snapshotKey = appId_ + "_" + sessionId_;
427     auto result = manager->OnAssetChanged(tokenId_, appId_, sessionId_, deviceId_, assetValue_);
428     ASSERT_EQ(result, DistributedObject::OBJECT_INNER_ERROR);
429     manager->snapshots_.Insert(snapshotKey, snapshot);
430     result = manager->OnAssetChanged(tokenId_, appId_, sessionId_, deviceId_, assetValue_);
431     ASSERT_EQ(result, DistributedObject::OBJECT_SUCCESS);
432 }
433 
434 /**
435 * @tc.name: DeleteSnapshot001
436 * @tc.desc: DeleteSnapshot test.
437 * @tc.type: FUNC
438 * @tc.require:
439 * @tc.author: wangbin
440 */
HWTEST_F(ObjectManagerTest, DeleteSnapshot001, TestSize.Level0)441 HWTEST_F(ObjectManagerTest, DeleteSnapshot001, TestSize.Level0)
442 {
443     auto manager = ObjectStoreManager::GetInstance();
444     std::shared_ptr<Snapshot> snapshot = std::make_shared<ObjectSnapshot>();
445     auto snapshotKey = bundleName_ + "_" + sessionId_;
446     auto snapshots = manager->snapshots_.Find(snapshotKey).second;
447     ASSERT_EQ(snapshots, nullptr);
448     manager->DeleteSnapshot(bundleName_, sessionId_);
449 
450     manager->snapshots_.Insert(snapshotKey, snapshot);
451     snapshots = manager->snapshots_.Find(snapshotKey).second;
452     ASSERT_NE(snapshots, nullptr);
453     manager->DeleteSnapshot(bundleName_, sessionId_);
454 }
455 
456 /**
457 * @tc.name: OpenObjectKvStore001
458 * @tc.desc: OpenObjectKvStore test.
459 * @tc.type: FUNC
460 * @tc.require:
461 * @tc.author: wangbin
462 */
HWTEST_F(ObjectManagerTest, OpenObjectKvStore001, TestSize.Level0)463 HWTEST_F(ObjectManagerTest, OpenObjectKvStore001, TestSize.Level0)
464 {
465     auto manager = ObjectStoreManager::GetInstance();
466     manager->objectDataListener_ = nullptr;
467     ASSERT_EQ(manager->objectDataListener_, nullptr);
468     manager->OpenObjectKvStore();
469     ASSERT_NE(manager->objectDataListener_, nullptr);
470     manager->OpenObjectKvStore();
471 }
472 
473 /**
474 * @tc.name: FlushClosedStore001
475 * @tc.desc: FlushClosedStore test.
476 * @tc.type: FUNC
477 * @tc.require:
478 * @tc.author: wangbin
479 */
HWTEST_F(ObjectManagerTest, FlushClosedStore001, TestSize.Level0)480 HWTEST_F(ObjectManagerTest, FlushClosedStore001, TestSize.Level0)
481 {
482     auto manager = ObjectStoreManager::GetInstance();
483     manager->isSyncing_ = true;
484     manager->syncCount_ = 10; // test syncCount_
485     manager->delegate_ = nullptr;
486     manager->FlushClosedStore();
487     manager->isSyncing_ = false;
488     manager->FlushClosedStore();
489     manager->syncCount_ = 0; // test syncCount_
490     manager->FlushClosedStore();
491     manager->delegate_ = manager->OpenObjectKvStore();
492     manager->FlushClosedStore();
493 }
494 
495 /**
496 * @tc.name: Close001
497 * @tc.desc: Close test.
498 * @tc.type: FUNC
499 * @tc.require:
500 * @tc.author: wangbin
501 */
HWTEST_F(ObjectManagerTest, Close001, TestSize.Level0)502 HWTEST_F(ObjectManagerTest, Close001, TestSize.Level0)
503 {
504     auto manager = ObjectStoreManager::GetInstance();
505     manager->syncCount_ = 1; // test syncCount_
506     manager->Close();
507     ASSERT_EQ(manager->syncCount_, 1); // 1 is for testing
508     manager->delegate_ = manager->OpenObjectKvStore();
509     manager->Close();
510     ASSERT_EQ(manager->syncCount_, 0); // 0 is for testing
511 }
512 
513 /**
514 * @tc.name: SyncOnStore001
515 * @tc.desc: SyncOnStore test.
516 * @tc.type: FUNC
517 * @tc.require:
518 * @tc.author: wangbin
519 */
HWTEST_F(ObjectManagerTest, SyncOnStore001, TestSize.Level0)520 HWTEST_F(ObjectManagerTest, SyncOnStore001, TestSize.Level0)
521 {
522     auto manager = ObjectStoreManager::GetInstance();
523     std::function<void(const std::map<std::string, int32_t> &results)> func;
524     func = [](const std::map<std::string, int32_t> &results) {
525         return results;
526     };
527     std::string prefix = "ObjectManagerTest";
528     std::vector<std::string> deviceList;
529     deviceList.push_back("local");
530     deviceList.push_back("local1");
531     auto result = manager->SyncOnStore(prefix, deviceList, func);
532     ASSERT_EQ(result, OBJECT_SUCCESS);
533 }
534 
535 /**
536 * @tc.name: RevokeSaveToStore001
537 * @tc.desc: RetrieveFromStore test.
538 * @tc.type: FUNC
539 * @tc.require:
540 * @tc.author: wangbin
541 */
HWTEST_F(ObjectManagerTest, RevokeSaveToStore001, TestSize.Level0)542 HWTEST_F(ObjectManagerTest, RevokeSaveToStore001, TestSize.Level0)
543 {
544     auto manager = ObjectStoreManager::GetInstance();
545     DistributedDB::KvStoreNbDelegateMock mockDelegate;
546     manager->delegate_ = &mockDelegate;
547     std::vector<uint8_t> id;
548     id.push_back(1);  // for testing
549     id.push_back(2);  // for testing
550     std::map<std::string, std::vector<uint8_t>> results;
551     results = {{ "test_cloud", id }};
552     auto result = manager->RetrieveFromStore(appId_, sessionId_, results);
553     ASSERT_EQ(result, OBJECT_SUCCESS);
554 }
555 
556 /**
557 * @tc.name: SyncCompleted001
558 * @tc.desc: SyncCompleted test.
559 * @tc.type: FUNC
560 * @tc.require:
561 * @tc.author: wangbin
562 */
HWTEST_F(ObjectManagerTest, SyncCompleted001, TestSize.Level0)563 HWTEST_F(ObjectManagerTest, SyncCompleted001, TestSize.Level0)
564 {
565     auto manager = ObjectStoreManager::GetInstance();
566     auto syncManager = SequenceSyncManager::GetInstance();
567     std::map<std::string, DistributedDB::DBStatus> results;
568     results = {{ "test_cloud", DistributedDB::DBStatus::OK }};
569     std::function<void(const std::map<std::string, int32_t> &results)> func;
570     func = [](const std::map<std::string, int32_t> &results) {
571         return results;
572     };
573     manager->userId_ = "99";
574     std::vector<uint64_t> userId;
575     userId.push_back(99);
576     userId.push_back(100);
577     manager->SyncCompleted(results, sequenceId_);
578     syncManager->userIdSeqIdRelations_ = {{ "test_cloud", userId }};
579     manager->SyncCompleted(results, sequenceId_);
580     userId.clear();
581     syncManager->seqIdCallbackRelations_.emplace(sequenceId_, func);
582     manager->SyncCompleted(results, sequenceId_);
583     userId.push_back(99);
584     userId.push_back(100);
585     manager->SyncCompleted(results, sequenceId_);
586 }
587 
588 /**
589 * @tc.name: SplitEntryKey001
590 * @tc.desc: SplitEntryKey test.
591 * @tc.type: FUNC
592 * @tc.require:
593 * @tc.author: wangbin
594 */
HWTEST_F(ObjectManagerTest, SplitEntryKey001, TestSize.Level0)595 HWTEST_F(ObjectManagerTest, SplitEntryKey001, TestSize.Level0)
596 {
597     auto manager = ObjectStoreManager::GetInstance();
598     std::string key1 = "";
599     std::string key2 = "ObjectManagerTest";
600     auto result = manager->SplitEntryKey(key1);
601     ASSERT_EQ(result.empty(), true);
602     result = manager->SplitEntryKey(key2);
603     ASSERT_EQ(result.empty(), true);
604 }
605 
606 /**
607 * @tc.name: SplitEntryKey002
608 * @tc.desc: SplitEntryKey test.
609 * @tc.type: FUNC
610 */
HWTEST_F(ObjectManagerTest, SplitEntryKey002, TestSize.Level0)611 HWTEST_F(ObjectManagerTest, SplitEntryKey002, TestSize.Level0)
612 {
613     auto manager = ObjectStoreManager::GetInstance();
614     std::string key1 = "com.example.myapplication_sessionId_source_target_1234567890_p_propertyName";
615     auto res = manager->SplitEntryKey(key1);
616     EXPECT_EQ(res[0], "com.example.myapplication");
617     EXPECT_EQ(res[1], "sessionId");
618     EXPECT_EQ(res[2], "source");
619     EXPECT_EQ(res[3], "target");
620     EXPECT_EQ(res[4], "1234567890");
621     EXPECT_EQ(res[5], "p_propertyName");
622 
623     std::string key2 = "com.example.myapplication_sessionId_source_target_000_p_propertyName";
624     res = manager->SplitEntryKey(key2);
625     EXPECT_TRUE(res.empty());
626 
627     std::string key3 = "com.example.myapplicationsessionIdsourcetarget_1234567890_p_propertyName";
628     res = manager->SplitEntryKey(key3);
629     EXPECT_TRUE(res.empty());
630 
631     std::string key4 = "com.example.myapplicationsessionIdsource_target_1234567890_p_propertyName";
632     res = manager->SplitEntryKey(key4);
633     EXPECT_TRUE(res.empty());
634 
635     std::string key5 = "com.example.myapplicationsessionId_source_target_1234567890_p_propertyName";
636     res = manager->SplitEntryKey(key5);
637     EXPECT_TRUE(res.empty());
638 }
639 
640 /**
641 * @tc.name: ProcessOldEntry001
642 * @tc.desc: ProcessOldEntry test.
643 * @tc.type: FUNC
644 * @tc.require:
645 * @tc.author: wangbin
646 */
HWTEST_F(ObjectManagerTest, ProcessOldEntry001, TestSize.Level0)647 HWTEST_F(ObjectManagerTest, ProcessOldEntry001, TestSize.Level0)
648 {
649     auto manager = ObjectStoreManager::GetInstance();
650     manager->delegate_ = manager->OpenObjectKvStore();
651     std::vector<DistributedDB::Entry> entries;
652     auto status = manager->delegate_->GetEntries(std::vector<uint8_t>(appId_.begin(), appId_.end()), entries);
653     ASSERT_EQ(status, DistributedDB::DBStatus::NOT_FOUND);
654     manager->ProcessOldEntry(appId_);
655 
656     DistributedDB::KvStoreNbDelegateMock mockDelegate;
657     manager->delegate_ = &mockDelegate;
658     status = manager->delegate_->GetEntries(std::vector<uint8_t>(appId_.begin(), appId_.end()), entries);
659     ASSERT_EQ(status, DistributedDB::DBStatus::OK);
660     manager->ProcessOldEntry(appId_);
661 }
662 
663 /**
664 * @tc.name: ProcessSyncCallback001
665 * @tc.desc: ProcessSyncCallback test.
666 * @tc.type: FUNC
667 * @tc.require:
668 * @tc.author: wangbin
669 */
HWTEST_F(ObjectManagerTest, ProcessSyncCallback001, TestSize.Level0)670 HWTEST_F(ObjectManagerTest, ProcessSyncCallback001, TestSize.Level0)
671 {
672     auto manager = ObjectStoreManager::GetInstance();
673     std::map<std::string, int32_t> results;
674     manager->ProcessSyncCallback(results, appId_, sessionId_, deviceId_);
675     results.insert({"local", 1}); // for testing
676     ASSERT_EQ(results.empty(), false);
677     ASSERT_NE(results.find("local"), results.end());
678     manager->ProcessSyncCallback(results, appId_, sessionId_, deviceId_);
679 }
680 
681 /**
682 * @tc.name: IsAssetComplete001
683 * @tc.desc: IsAssetComplete test.
684 * @tc.type: FUNC
685 * @tc.require:
686 * @tc.author: wangbin
687 */
HWTEST_F(ObjectManagerTest, IsAssetComplete001, TestSize.Level0)688 HWTEST_F(ObjectManagerTest, IsAssetComplete001, TestSize.Level0)
689 {
690     auto manager = ObjectStoreManager::GetInstance();
691     std::map<std::string, std::vector<uint8_t>> results;
692     std::vector<uint8_t> completes;
693     completes.push_back(1); // for testing
694     completes.push_back(2); // for testing
695     std::string assetPrefix = "IsAssetComplete_test";
696     results.insert({assetPrefix, completes});
697     auto result = manager->IsAssetComplete(results, assetPrefix);
698     ASSERT_EQ(result, false);
699     results.insert({assetPrefix + ObjectStore::NAME_SUFFIX, completes});
700     result = manager->IsAssetComplete(results, assetPrefix);
701     ASSERT_EQ(result, false);
702     results.insert({assetPrefix + ObjectStore::URI_SUFFIX, completes});
703     result = manager->IsAssetComplete(results, assetPrefix);
704     ASSERT_EQ(result, false);
705     results.insert({assetPrefix + ObjectStore::PATH_SUFFIX, completes});
706     result = manager->IsAssetComplete(results, assetPrefix);
707     ASSERT_EQ(result, false);
708     results.insert({assetPrefix + ObjectStore::CREATE_TIME_SUFFIX, completes});
709     result = manager->IsAssetComplete(results, assetPrefix);
710     ASSERT_EQ(result, false);
711     results.insert({assetPrefix + ObjectStore::MODIFY_TIME_SUFFIX, completes});
712     result = manager->IsAssetComplete(results, assetPrefix);
713     ASSERT_EQ(result, false);
714     results.insert({assetPrefix + ObjectStore::SIZE_SUFFIX, completes});
715     result = manager->IsAssetComplete(results, assetPrefix);
716     ASSERT_EQ(result, true);
717 }
718 
719 /**
720 * @tc.name: GetAssetsFromDBRecords001
721 * @tc.desc: GetAssetsFromDBRecords test.
722 * @tc.type: FUNC
723 * @tc.require:
724 * @tc.author: wangbin
725 */
HWTEST_F(ObjectManagerTest, GetAssetsFromDBRecords001, TestSize.Level0)726 HWTEST_F(ObjectManagerTest, GetAssetsFromDBRecords001, TestSize.Level0)
727 {
728     auto manager = ObjectStoreManager::GetInstance();
729     std::map<std::string, std::vector<uint8_t>> results;
730     std::vector<uint8_t> completes;
731     completes.push_back(1); // for testing
732     completes.push_back(2); // for testing
733     std::string assetPrefix = "IsAssetComplete_test";
734     results.insert({assetPrefix, completes});
735     results.insert({assetPrefix + ObjectStore::NAME_SUFFIX, completes});
736     results.insert({assetPrefix + ObjectStore::URI_SUFFIX, completes});
737     results.insert({assetPrefix + ObjectStore::MODIFY_TIME_SUFFIX, completes});
738     results.insert({assetPrefix + ObjectStore::SIZE_SUFFIX, completes});
739     auto result = manager->GetAssetsFromDBRecords(results);
740     ASSERT_EQ(result.empty(), false);
741 }
742 
743 /**
744 * @tc.name: GetAssetsFromDBRecords002
745 * @tc.desc: GetAssetsFromDBRecords test.
746 * @tc.type: FUNC
747 */
HWTEST_F(ObjectManagerTest, GetAssetsFromDBRecords002, TestSize.Level0)748 HWTEST_F(ObjectManagerTest, GetAssetsFromDBRecords002, TestSize.Level0)
749 {
750     auto manager = ObjectStoreManager::GetInstance();
751     std::map<std::string, std::vector<uint8_t>> result;
752 
753     std::vector<uint8_t> value0{0};
754     std::string data0 = "[STRING]test";
755     value0.insert(value0.end(), data0.begin(), data0.end());
756 
757     std::vector<uint8_t> value1{0};
758     std::string data1 = "(string)test";
759     value1.insert(value1.end(), data1.begin(), data1.end());
760 
761     std::string prefix = "bundleName_sessionId_source_target_timestamp";
762     std::string dataKey = prefix + "_p_data";
763     std::string assetPrefix0 = prefix + "_p_asset0";
764     std::string assetPrefix1 = prefix + "_p_asset1";
765 
766     result.insert({dataKey, value0});
767     auto assets = manager->GetAssetsFromDBRecords(result);
768     EXPECT_TRUE(assets.empty());
769 
770     result.clear();
771     result.insert({assetPrefix0 + ObjectStore::URI_SUFFIX, value0});
772     assets = manager->GetAssetsFromDBRecords(result);
773     EXPECT_TRUE(assets.empty());
774 
775     result.clear();
776     result.insert({assetPrefix1 + ObjectStore::NAME_SUFFIX, value1});
777     assets = manager->GetAssetsFromDBRecords(result);
778     EXPECT_TRUE(assets.empty());
779 
780     result.clear();
781     result.insert({assetPrefix0 + ObjectStore::NAME_SUFFIX, value0});
782     result.insert({assetPrefix0 + ObjectStore::URI_SUFFIX, value0});
783     result.insert({assetPrefix0 + ObjectStore::MODIFY_TIME_SUFFIX, value0});
784     result.insert({assetPrefix0 + ObjectStore::SIZE_SUFFIX, value0});
785     assets = manager->GetAssetsFromDBRecords(result);
786     ASSERT_EQ(assets.size(), 1);
787     EXPECT_EQ(assets[0].name, "test");
788     EXPECT_EQ(assets[0].uri, "test");
789     EXPECT_EQ(assets[0].modifyTime, "test");
790     EXPECT_EQ(assets[0].size, "test");
791     EXPECT_EQ(assets[0].hash, "test_test");
792 
793     result.clear();
794     result.insert({assetPrefix1 + ObjectStore::NAME_SUFFIX, value1});
795     result.insert({assetPrefix1 + ObjectStore::URI_SUFFIX, value1});
796     result.insert({assetPrefix1 + ObjectStore::MODIFY_TIME_SUFFIX, value1});
797     result.insert({assetPrefix1 + ObjectStore::SIZE_SUFFIX, value1});
798     assets = manager->GetAssetsFromDBRecords(result);
799     ASSERT_EQ(assets.size(), 1);
800     EXPECT_EQ(assets[0].name, "(string)test");
801     EXPECT_EQ(assets[0].uri, "(string)test");
802     EXPECT_EQ(assets[0].modifyTime, "(string)test");
803     EXPECT_EQ(assets[0].size, "(string)test");
804     EXPECT_EQ(assets[0].hash, "(string)test_(string)test");
805 }
806 
807 /**
808 * @tc.name: RegisterAssetsLister001
809 * @tc.desc: RegisterAssetsLister test.
810 * @tc.type: FUNC
811 * @tc.require:
812 * @tc.author: wangbin
813 */
HWTEST_F(ObjectManagerTest, RegisterAssetsLister001, TestSize.Level0)814 HWTEST_F(ObjectManagerTest, RegisterAssetsLister001, TestSize.Level0)
815 {
816     auto manager = ObjectStoreManager::GetInstance();
817     manager->objectAssetsSendListener_ = nullptr;
818     manager->objectAssetsRecvListener_ = nullptr;
819     auto result = manager->RegisterAssetsLister();
820     ASSERT_EQ(result, true);
821     manager->objectAssetsSendListener_ = new ObjectAssetsSendListener();
822     manager->objectAssetsRecvListener_ = new ObjectAssetsRecvListener();;
823     result = manager->RegisterAssetsLister();
824     ASSERT_EQ(result, true);
825 }
826 
827 /**
828 * @tc.name: RegisterAssetsLister001
829 * @tc.desc: PushAssets test.
830 * @tc.type: FUNC
831 * @tc.require:
832 * @tc.author: wangbin
833 */
HWTEST_F(ObjectManagerTest, PushAssets001, TestSize.Level0)834 HWTEST_F(ObjectManagerTest, PushAssets001, TestSize.Level0)
835 {
836     auto manager = ObjectStoreManager::GetInstance();
837     std::map<std::string, std::vector<uint8_t>> data;
838     std::string assetPrefix = "PushAssets_test";
839     std::vector<uint8_t> completes;
840     completes.push_back(1); // for testing
841     completes.push_back(2); // for testing
842     data.insert({assetPrefix, completes});
843     auto result = manager->PushAssets(appId_, appId_, sessionId_, data, deviceId_);
844     ASSERT_EQ(result, DistributedObject::OBJECT_SUCCESS);
845 }
846 
847 /**
848 * @tc.name: AddNotifier001
849 * @tc.desc: AddNotifie and DeleteNotifier test.
850 * @tc.type: FUNC
851 */
HWTEST_F(ObjectManagerTest, AddNotifier001, TestSize.Level0)852 HWTEST_F(ObjectManagerTest, AddNotifier001, TestSize.Level0)
853 {
854     auto syncManager = SequenceSyncManager::GetInstance();
855     std::function<void(const std::map<std::string, int32_t> &results)> func;
856     func = [](const std::map<std::string, int32_t> &results) {
857         return results;
858     };
859     auto sequenceId_ = syncManager->AddNotifier(userId_, func);
860     auto result = syncManager->DeleteNotifier(sequenceId_, userId_);
861     ASSERT_EQ(result, SequenceSyncManager::SUCCESS_USER_HAS_FINISHED);
862 }
863 
864 /**
865 * @tc.name: AddNotifier002
866 * @tc.desc: AddNotifie and DeleteNotifier test.
867 * @tc.type: FUNC
868 */
HWTEST_F(ObjectManagerTest, AddNotifier002, TestSize.Level0)869 HWTEST_F(ObjectManagerTest, AddNotifier002, TestSize.Level0)
870 {
871     auto syncManager = SequenceSyncManager::GetInstance();
872     std::function<void(const std::map<std::string, int32_t> &results)> func;
873     func = [](const std::map<std::string, int32_t> &results) {
874         return results;
875     };
876     auto sequenceId = syncManager->AddNotifier(userId_, func);
877     ASSERT_NE(sequenceId, sequenceId_);
878     auto result = syncManager->DeleteNotifier(sequenceId_, userId_);
879     ASSERT_EQ(result, SequenceSyncManager::ERR_SID_NOT_EXIST);
880 }
881 
882 /**
883 * @tc.name: BindAsset 001
884 * @tc.desc: BindAsset test.
885 * @tc.type: FUNC
886 */
HWTEST_F(ObjectManagerTest, BindAsset001, TestSize.Level0)887 HWTEST_F(ObjectManagerTest, BindAsset001, TestSize.Level0)
888 {
889     auto manager = ObjectStoreManager::GetInstance();
890     std::string bundleName = "BindAsset";
891     uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
892     auto result = manager->BindAsset(tokenId, bundleName, sessionId_, assetValue_, assetBindInfo_);
893     ASSERT_EQ(result, DistributedObject::OBJECT_DBSTATUS_ERROR);
894 }
895 } // namespace OHOS::Test
896