1 /*
2 * Copyright (c) 2024-2025 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
18 #include "cJSON.h"
19
20 #include "constants.h"
21 #include "dh_utils_tool.h"
22 #include "dh_context.h"
23 #include "distributed_hardware_errno.h"
24 #include "meta_capability_info.h"
25 #include "meta_info_manager.h"
26 #include "task_board.h"
27 #include "impl_utils.h"
28
29 using namespace testing::ext;
30 using namespace std;
31
32 namespace OHOS {
33 namespace DistributedHardware {
34 namespace {
35 constexpr uint16_t DEV_TYPE_TEST = 14;
36 constexpr uint32_t MAX_DB_RECORD_LENGTH = 10005;
37 }
38
39 class MetaInfoMgrTest : public testing::Test {
40 public:
41 static void SetUpTestCase(void);
42 static void TearDownTestCase(void);
43 void SetUp();
44 void TearDown();
45 };
46
SetUp()47 void MetaInfoMgrTest::SetUp() {}
48
TearDown()49 void MetaInfoMgrTest::TearDown() {}
50
SetUpTestCase()51 void MetaInfoMgrTest::SetUpTestCase() {}
52
TearDownTestCase()53 void MetaInfoMgrTest::TearDownTestCase() {}
54
55 HWTEST_F(MetaInfoMgrTest, Init_001, TestSize.Level0)
56 {
57 auto ret = MetaInfoManager::GetInstance()->Init();
58 EXPECT_EQ(DH_FWK_SUCCESS, ret);
59 ret = MetaInfoManager::GetInstance()->UnInit();
60 EXPECT_EQ(DH_FWK_SUCCESS, ret);
61 }
62
63 HWTEST_F(MetaInfoMgrTest, UnInit_001, TestSize.Level0)
64 {
65 MetaInfoManager::GetInstance()->dbAdapterPtr_ = nullptr;
66 auto ret = MetaInfoManager::GetInstance()->UnInit();
67 EXPECT_EQ(ERR_DH_FWK_RESOURCE_UNINIT_DB_FAILED, ret);
68 }
69
70 HWTEST_F(MetaInfoMgrTest, AddMetaCapInfos_001, TestSize.Level0)
71 {
72 std::string udidHash = "udidHash_test";
73 std::vector<std::shared_ptr<MetaCapabilityInfo>> metaCapInfos;
74 auto ret = MetaInfoManager::GetInstance()->AddMetaCapInfos(metaCapInfos);
75 EXPECT_EQ(ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID, ret);
76
77 for (int32_t i = 1; i < MAX_DB_RECORD_LENGTH; i++) {
78 std::shared_ptr<MetaCapabilityInfo> mateCapInfoTest = make_shared<MetaCapabilityInfo>(std::to_string(i),
79 std::to_string(i), "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs", "subtype", udidHash,
80 CompVersion{ .sinkVersion = "1.0" });
81 metaCapInfos.push_back(mateCapInfoTest);
82 }
83 ret = MetaInfoManager::GetInstance()->AddMetaCapInfos(metaCapInfos);
84 EXPECT_EQ(ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID, ret);
85 }
86
87 HWTEST_F(MetaInfoMgrTest, AddMetaCapInfos_002, TestSize.Level0)
88 {
89 std::string deviceId = "deviceId_test";
90 std::string udidHash = "udidHash_test";
91 std::string dhId = "dhId_test";
92 CompVersion compVersion;
93 compVersion.sinkVersion = "1.0";
94 std::shared_ptr<MetaCapabilityInfo> metaCapInfo = std::make_shared<MetaCapabilityInfo>(
95 dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash, compVersion);
96 std::vector<std::shared_ptr<MetaCapabilityInfo>> metaCapInfos;
97 metaCapInfos.push_back(metaCapInfo);
98 auto ret = MetaInfoManager::GetInstance()->AddMetaCapInfos(metaCapInfos);
99 EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret);
100
101 MetaInfoManager::GetInstance()->Init();
102 std::shared_ptr<MetaCapabilityInfo> mateCapInfoTest = nullptr;
103 metaCapInfos.push_back(mateCapInfoTest);
104 ret = MetaInfoManager::GetInstance()->AddMetaCapInfos(metaCapInfos);
105 EXPECT_EQ(DH_FWK_SUCCESS, ret);
106 MetaInfoManager::GetInstance()->UnInit();
107 }
108
109 HWTEST_F(MetaInfoMgrTest, SyncMetaInfoFromDB_001, TestSize.Level0)
110 {
111 std::string udidHash = "";
112 auto ret = MetaInfoManager::GetInstance()->SyncMetaInfoFromDB(udidHash);
113 EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret);
114 }
115
116 HWTEST_F(MetaInfoMgrTest, SyncMetaInfoFromDB_002, TestSize.Level0)
117 {
118 std::string udidHash = "132456798";
119 auto ret = MetaInfoManager::GetInstance()->SyncMetaInfoFromDB(udidHash);
120 EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret);
121 }
122
123 HWTEST_F(MetaInfoMgrTest, SyncMetaInfoFromDB_003, TestSize.Level0)
124 {
125 std::string udidHash = "132456798";
126 MetaInfoManager::GetInstance()->Init();
127 auto ret = MetaInfoManager::GetInstance()->SyncMetaInfoFromDB(udidHash);
128 EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret);
129 MetaInfoManager::GetInstance()->UnInit();
130 }
131
132 HWTEST_F(MetaInfoMgrTest, SyncRemoteMetaInfos_001, TestSize.Level0)
133 {
134 auto ret = MetaInfoManager::GetInstance()->SyncRemoteMetaInfos();
135 EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret);
136
137 MetaInfoManager::GetInstance()->Init();
138 ret = MetaInfoManager::GetInstance()->SyncRemoteMetaInfos();
139 EXPECT_EQ(DH_FWK_SUCCESS, ret);
140 MetaInfoManager::GetInstance()->UnInit();
141 }
142
143 HWTEST_F(MetaInfoMgrTest, SyncRemoteMetaInfos_002, TestSize.Level0)
144 {
145 std::string udid = "111111";
146 std::string uuid = "222222";
147 std::string networkId = "333333";
148 DHContext::GetInstance().AddOnlineDevice(udid, uuid, networkId);
149 MetaInfoManager::GetInstance()->Init();
150 auto ret = MetaInfoManager::GetInstance()->SyncRemoteMetaInfos();
151 EXPECT_EQ(DH_FWK_SUCCESS, ret);
152 MetaInfoManager::GetInstance()->UnInit();
153 DHContext::GetInstance().RemoveOnlineDeviceIdEntryByNetworkId(networkId);
154 }
155
156 HWTEST_F(MetaInfoMgrTest, GetDataByKeyPrefix_001, TestSize.Level0)
157 {
158 std::string keyPrefix = "";
159 MetaCapInfoMap metaCapMap;
160 auto ret = MetaInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, metaCapMap);
161 EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret);
162
163 keyPrefix = "keyPrefix_test";
164 ret = MetaInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, metaCapMap);
165 EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret);
166 }
167
168 HWTEST_F(MetaInfoMgrTest, GetDataByKeyPrefix_002, TestSize.Level0)
169 {
170 std::string keyPrefix = "keyPrefix_test";
171 MetaCapInfoMap metaCapMap;
172 MetaInfoManager::GetInstance()->Init();
173 auto ret = MetaInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, metaCapMap);
174 EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret);
175 MetaInfoManager::GetInstance()->UnInit();
176 }
177
178 HWTEST_F(MetaInfoMgrTest, RemoveMetaInfoByKey_001, TestSize.Level0)
179 {
180 std::string key = "";
181 auto ret = MetaInfoManager::GetInstance()->RemoveMetaInfoByKey(key);
182 EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret);
183
184 key = "key_test";
185 ret = MetaInfoManager::GetInstance()->RemoveMetaInfoByKey(key);
186 EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret);
187 }
188
189 HWTEST_F(MetaInfoMgrTest, RemoveMetaInfoByKey_002, TestSize.Level0)
190 {
191 std::string key = "key_test";
192 MetaInfoManager::GetInstance()->Init();
193 auto ret = MetaInfoManager::GetInstance()->RemoveMetaInfoByKey(key);
194 EXPECT_EQ(DH_FWK_SUCCESS, ret);
195 MetaInfoManager::GetInstance()->UnInit();
196 }
197
198 HWTEST_F(MetaInfoMgrTest, GetMetaCapInfo_001, TestSize.Level0)
199 {
200 std::string udidHash = "";
201 std::string dhId = "";
202 std::shared_ptr<MetaCapabilityInfo> metaCapPtr;
203 auto ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(udidHash, dhId, metaCapPtr);
204 EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret);
205
206 udidHash = "123456789";
207 ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(udidHash, dhId, metaCapPtr);
208 EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret);
209
210 udidHash = "";
211 dhId = "123456789";
212 ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(udidHash, dhId, metaCapPtr);
213 EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret);
214 }
215
216 HWTEST_F(MetaInfoMgrTest, GetMetaCapInfo_002, TestSize.Level0)
217 {
218 std::string deviceId = "deviceId_test";
219 std::string udidHash = "udidHash_test";
220 std::string dhId = "dhId_test";
221 CompVersion compVersion{ .sinkVersion = "1.0" };
222 std::shared_ptr<MetaCapabilityInfo> metaCapPtr;
223 std::shared_ptr<MetaCapabilityInfo> dhMetaCapInfo = std::make_shared<MetaCapabilityInfo>(
224 dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash, compVersion);
225 std::string key = udidHash + "###" + dhId;
226 MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = dhMetaCapInfo;
227 auto ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(deviceId, dhId, metaCapPtr);
228 EXPECT_EQ(ERR_DH_FWK_RESOURCE_CAPABILITY_MAP_NOT_FOUND, ret);
229
230 ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(udidHash, dhId, metaCapPtr);
231 EXPECT_EQ(DH_FWK_SUCCESS, ret);
232 MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear();
233 }
234
235 HWTEST_F(MetaInfoMgrTest, GetMetaCapInfosByUdidHash_001, TestSize.Level0)
236 {
237 std::string udidHash = "";
238 std::vector<std::shared_ptr<MetaCapabilityInfo>> metaCapInfos;
239 MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(udidHash, metaCapInfos);
240
241 udidHash = "udidHash_test";
242 std::string deviceId = "deviceId_test";
243 std::string dhId = "dhId_test";
244 std::shared_ptr<MetaCapabilityInfo> metaCapInfo = std::make_shared<MetaCapabilityInfo>(
245 dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash,
246 CompVersion{ .sinkVersion = "1.0" });
247 std::string key = udidHash + "###" + dhId;
248 MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = metaCapInfo;
249 MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(deviceId, metaCapInfos);
250 EXPECT_TRUE(metaCapInfos.empty());
251
252 MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(udidHash, metaCapInfos);
253 EXPECT_FALSE(metaCapInfos.empty());
254 MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear();
255 }
256
257 HWTEST_F(MetaInfoMgrTest, GetMetaCapByValue_001, TestSize.Level0)
258 {
259 std::string value = "";
260 std::shared_ptr<MetaCapabilityInfo> metaCapPtr = nullptr;
261 auto ret = MetaInfoManager::GetInstance()->GetMetaCapByValue(value, metaCapPtr);
262 EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret);
263
264 value = "invalid JSON string";
265 ret = MetaInfoManager::GetInstance()->GetMetaCapByValue(value, metaCapPtr);
266 EXPECT_EQ(ERR_DH_FWK_JSON_PARSE_FAILED, ret);
267
268 std::string deviceId = "deviceId_test";
269 std::string udidHash = "udidHash_test";
270 std::string dhId = "dhId_test";
271 metaCapPtr = std::make_shared<MetaCapabilityInfo>(
272 dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash,
273 CompVersion{ .sinkVersion = "1.0" });
274 ret = MetaInfoManager::GetInstance()->GetMetaCapByValue(value, metaCapPtr);
275 EXPECT_EQ(ERR_DH_FWK_JSON_PARSE_FAILED, ret);
276 }
277
278 HWTEST_F(MetaInfoMgrTest, GetMetaDataByDHType_001, TestSize.Level0)
279 {
280 std::string deviceId = "deviceId_test";
281 std::string udidHash = "udidHash_test";
282 std::string dhId = "dhId_test";
283 MetaCapInfoMap metaInfoMap;
284 std::shared_ptr<MetaCapabilityInfo> dhMetaCapInfo = std::make_shared<MetaCapabilityInfo>(
285 dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash,
286 CompVersion{ .sinkVersion = "1.0" });
287 std::string key = udidHash + "###" + dhId;
288 MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = dhMetaCapInfo;
289 auto ret = MetaInfoManager::GetInstance()->GetMetaDataByDHType(DHType::AUDIO, metaInfoMap);
290 EXPECT_EQ(DH_FWK_SUCCESS, ret);
291
292 ret = MetaInfoManager::GetInstance()->GetMetaDataByDHType(DHType::CAMERA, metaInfoMap);
293 EXPECT_EQ(DH_FWK_SUCCESS, ret);
294 MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear();
295 }
296
297 HWTEST_F(MetaInfoMgrTest, SyncDataByNetworkId_001, TestSize.Level0)
298 {
299 std::string networkId = "";
300 auto ret = MetaInfoManager::GetInstance()->SyncDataByNetworkId(networkId);
301 EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret);
302
303 networkId = "networkId_test";
304 ret = MetaInfoManager::GetInstance()->SyncDataByNetworkId(networkId);
305 EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret);
306 }
307
308 HWTEST_F(MetaInfoMgrTest, SyncDataByNetworkId_002, TestSize.Level0)
309 {
310 std::string networkId = "networkId_test";
311 MetaInfoManager::GetInstance()->Init();
312 auto ret = MetaInfoManager::GetInstance()->SyncDataByNetworkId(networkId);
313 EXPECT_EQ(DH_FWK_SUCCESS, ret);
314 MetaInfoManager::GetInstance()->UnInit();
315 }
316
317 HWTEST_F(MetaInfoMgrTest, RemoveMetaInfoInMemByUdid_001, TestSize.Level0)
318 {
319 std::string peerudid = "123456789";
320 std::string peerudIdHash = Sha256(peerudid);
321 std::string deviceId = "deviceId_test";
322 std::string udidHash = "udidHash_test";
323 std::string dhId = "dhId_test";
324 std::shared_ptr<MetaCapabilityInfo> metaCapPtr = std::make_shared<MetaCapabilityInfo>(
325 dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash,
326 CompVersion{ .sinkVersion = "1.0" });
327 std::string key = peerudIdHash + "###" + dhId;
328 MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = metaCapPtr;
329 auto ret = MetaInfoManager::GetInstance()->RemoveMetaInfoInMemByUdid(deviceId);
330 EXPECT_EQ(DH_FWK_SUCCESS, ret);
331
332 ret = MetaInfoManager::GetInstance()->RemoveMetaInfoInMemByUdid(peerudid);
333 EXPECT_EQ(DH_FWK_SUCCESS, ret);
334 }
335
336 HWTEST_F(MetaInfoMgrTest, ClearRemoteDeviceMetaInfoData_001, TestSize.Level0)
337 {
338 MetaInfoManager::GetInstance()->Init();
339 std::string peerudid = "";
340 std::string peeruuid = "";
341 auto ret = MetaInfoManager::GetInstance()->ClearRemoteDeviceMetaInfoData(peerudid, peeruuid);
342 EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret);
343
344 peerudid = "peerudid_test";
345 peeruuid = "peeruuid_test";
346 ret = MetaInfoManager::GetInstance()->ClearRemoteDeviceMetaInfoData(peerudid, peeruuid);
347 EXPECT_EQ(DH_FWK_SUCCESS, ret);
348
349 MetaInfoManager::GetInstance()->UnInit();
350 ret = MetaInfoManager::GetInstance()->ClearRemoteDeviceMetaInfoData(peerudid, peeruuid);
351 EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret);
352 }
353
354 HWTEST_F(MetaInfoMgrTest, OnChange_001, TestSize.Level0)
355 {
356 DistributedKv::Entry insert, update, del;
357 std::vector<DistributedKv::Entry> inserts, updates, deleteds;
358 inserts.push_back(insert);
359 updates.push_back(update);
360 deleteds.push_back(del);
361 DistributedKv::ChangeNotification changeIn(std::move(inserts), std::move(updates), std::move(deleteds), "", true);
362 ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->OnChange(changeIn));
363 }
364
365 HWTEST_F(MetaInfoMgrTest, OnChange_002, TestSize.Level0)
366 {
367 DistributedKv::Entry insert, update, del;
368 std::vector<DistributedKv::Entry> inserts, updates, deleteds;
369 std::string tempStr;
370 for (int32_t i = 1; i < MAX_DB_RECORD_LENGTH; i++) {
371 tempStr = std::to_string(i);
372 insert.key = tempStr.c_str();
373 update.key = tempStr.c_str();
374 del.key = tempStr.c_str();
375 insert.value = tempStr.c_str();
376 update.value = tempStr.c_str();
377 del.value = tempStr.c_str();
378 inserts.push_back(insert);
379 updates.push_back(update);
380 deleteds.push_back(del);
381 }
382 DistributedKv::ChangeNotification changeIn(std::move(inserts), std::move(updates), std::move(deleteds), "", true);
383 ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->OnChange(changeIn));
384 }
385
386 HWTEST_F(MetaInfoMgrTest, OnChange_003, TestSize.Level0)
387 {
388 DistributedKv::Entry insert, update, del;
389 insert.key = "insert_key";
390 update.key = "update_key";
391 del.key = "del_key";
392 insert.value = "insert_value";
393 update.value = "update_value";
394 del.value = "del_value";
395 std::vector<DistributedKv::Entry> inserts, updates, deleteds;
396 inserts.push_back(insert);
397 updates.push_back(update);
398 deleteds.push_back(del);
399
400 DistributedKv::ChangeNotification changeIn(std::move(inserts), std::move(updates), std::move(deleteds), "", true);
401 ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->OnChange(changeIn));
402 }
403
404 HWTEST_F(MetaInfoMgrTest, OnChange_004, TestSize.Level0)
405 {
406 DistributedKv::DataOrigin origin;
407 origin.id = {};
408 origin.store = "";
409 MetaInfoManager::Keys keys;
410 ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->OnChange(origin, std::move(keys)));
411 }
412
413 HWTEST_F(MetaInfoMgrTest, OnChange_005, TestSize.Level0)
414 {
415 DistributedKv::DataOrigin origin;
416 origin.id = {};
417 origin.store = "";
418 MetaInfoManager::Keys keys;
419 keys[MetaInfoManager::OP_INSERT] = {"insert"};
420 keys[MetaInfoManager::OP_UPDATE] = {"update"};
421 keys[MetaInfoManager::OP_DELETE] = {"delete"};
422 ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->OnChange(origin, std::move(keys)));
423 }
424
425 HWTEST_F(MetaInfoMgrTest, HandleMetaCapabilityAddChange_001, TestSize.Level0)
426 {
427 cJSON *jsonObj = cJSON_CreateObject();
428 ASSERT_TRUE(jsonObj != nullptr);
429 cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "111111");
430 cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), "222222");
431 char* cjson = cJSON_PrintUnformatted(jsonObj);
432 if (cjson == nullptr) {
433 cJSON_Delete(jsonObj);
434 return;
435 }
436 std::string jsonStr(cjson);
437 std::vector<DistributedKv::Entry> insertRecords;
438 DistributedKv::Entry entry;
439 entry.key = "insert";
440 entry.value = jsonStr.c_str();
441 insertRecords.push_back(entry);
442 ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityAddChange(insertRecords));
443 cJSON_free(cjson);
444 cJSON_Delete(jsonObj);
445 }
446
447 HWTEST_F(MetaInfoMgrTest, HandleMetaCapabilityAddChange_002, TestSize.Level0)
448 {
449 std::string uuid = "123456789";
450 std::string deviceId = Sha256(uuid);
451 DeviceIdEntry idEntry = {
452 .networkId = "",
453 .uuid = uuid,
454 .deviceId = deviceId
455 };
456 DHContext::GetInstance().devIdEntrySet_.insert(idEntry);
457 cJSON *jsonObj = cJSON_CreateObject();
458 ASSERT_TRUE(jsonObj != nullptr);
459 cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "111111");
460 cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), deviceId.c_str());
461 char* cjson = cJSON_PrintUnformatted(jsonObj);
462 if (cjson == nullptr) {
463 cJSON_Delete(jsonObj);
464 return;
465 }
466 std::string jsonStr(cjson);
467 std::vector<DistributedKv::Entry> insertRecords;
468 DistributedKv::Entry entry;
469 entry.key = "insert";
470 entry.value = jsonStr.c_str();
471 insertRecords.push_back(entry);
472 ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityAddChange(insertRecords));
473 DHContext::GetInstance().devIdEntrySet_.clear();
474 cJSON_free(cjson);
475 cJSON_Delete(jsonObj);
476 }
477
478 HWTEST_F(MetaInfoMgrTest, HandleMetaCapabilityAddChange_003, TestSize.Level0)
479 {
480 std::string uuid = "123456789";
481 std::string deviceId = Sha256(uuid);
482 cJSON *jsonObj = cJSON_CreateObject();
483 ASSERT_TRUE(jsonObj != nullptr);
484 cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "111111");
485 cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), deviceId.c_str());
486 char* cjson = cJSON_PrintUnformatted(jsonObj);
487 if (cjson == nullptr) {
488 cJSON_Delete(jsonObj);
489 return;
490 }
491 std::string jsonStr(cjson);
492 DHContext::GetInstance().AddOnlineDevice("111111", uuid, "222222");
493 std::vector<DistributedKv::Entry> insertRecords;
494 DistributedKv::Entry entry;
495 entry.key = "insert";
496 entry.value = jsonStr.c_str();
497 insertRecords.push_back(entry);
498 ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityAddChange(insertRecords));
499 DHContext::GetInstance().RemoveOnlineDeviceIdEntryByNetworkId("222222");
500 cJSON_free(cjson);
501 cJSON_Delete(jsonObj);
502 }
503
504 HWTEST_F(MetaInfoMgrTest, HandleMetaCapabilityUpdateChange_001, TestSize.Level0)
505 {
506 cJSON *jsonObj = cJSON_CreateObject();
507 ASSERT_TRUE(jsonObj != nullptr);
508 cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "111111");
509 cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), "222222");
510 char* cjson = cJSON_PrintUnformatted(jsonObj);
511 if (cjson == nullptr) {
512 cJSON_Delete(jsonObj);
513 return;
514 }
515 std::string jsonStr(cjson);
516 std::vector<DistributedKv::Entry> updateRecords;
517 DistributedKv::Entry update;
518 update.key = "update";
519 update.value = jsonStr.c_str();
520 updateRecords.push_back(update);
521 ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityUpdateChange(updateRecords));
522 cJSON_free(cjson);
523 cJSON_Delete(jsonObj);
524 }
525
526 HWTEST_F(MetaInfoMgrTest, HandleMetaCapabilityUpdateChange_002, TestSize.Level0)
527 {
528 std::string uuid = "123456789";
529 std::string deviceId = Sha256(uuid);
530 DeviceIdEntry idEntry = {
531 .networkId = "",
532 .uuid = uuid,
533 .deviceId = deviceId
534 };
535 cJSON *jsonObj = cJSON_CreateObject();
536 ASSERT_TRUE(jsonObj != nullptr);
537 cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "111111");
538 cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), deviceId.c_str());
539 char* cjson = cJSON_PrintUnformatted(jsonObj);
540 if (cjson == nullptr) {
541 cJSON_Delete(jsonObj);
542 return;
543 }
544 std::string jsonStr(cjson);
545 std::vector<DistributedKv::Entry> updateRecords;
546 DistributedKv::Entry update;
547 update.key = "update";
548 update.value = jsonStr.c_str();
549 updateRecords.push_back(update);
550 ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityUpdateChange(updateRecords));
551 DHContext::GetInstance().devIdEntrySet_.clear();
552 cJSON_free(cjson);
553 cJSON_Delete(jsonObj);
554 }
555
556 HWTEST_F(MetaInfoMgrTest, HandleMetaCapabilityUpdateChange_003, TestSize.Level0)
557 {
558 std::string uuid = "123456789";
559 std::string deviceId = Sha256(uuid);
560 cJSON *jsonObj = cJSON_CreateObject();
561 ASSERT_TRUE(jsonObj != nullptr);
562 cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "111111");
563 cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), deviceId.c_str());
564 char* cjson = cJSON_PrintUnformatted(jsonObj);
565 if (cjson == nullptr) {
566 cJSON_Delete(jsonObj);
567 return;
568 }
569 std::string jsonStr(cjson);
570 std::vector<DistributedKv::Entry> updateRecords;
571 DHContext::GetInstance().AddOnlineDevice("111111", uuid, "222222");
572 DistributedKv::Entry update;
573 update.key = "update";
574 update.value = jsonStr.c_str();
575 updateRecords.push_back(update);
576 ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityUpdateChange(updateRecords));
577
578 std::string enabledDeviceKey = deviceId + RESOURCE_SEPARATOR + "111111";
579 TaskParam taskParam;
580 TaskBoard::GetInstance().SaveEnabledDevice(enabledDeviceKey, taskParam);
581 ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityUpdateChange(updateRecords));
582
583 DHContext::GetInstance().RemoveOnlineDeviceIdEntryByNetworkId("222222");
584 cJSON_free(cjson);
585 cJSON_Delete(jsonObj);
586 }
587
588 HWTEST_F(MetaInfoMgrTest, HandleMetaCapabilityDeleteChange_001, TestSize.Level0)
589 {
590 cJSON *jsonObj = cJSON_CreateObject();
591 ASSERT_TRUE(jsonObj != nullptr);
592 cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "111111");
593 cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), "222222");
594 char* cjson = cJSON_PrintUnformatted(jsonObj);
595 if (cjson == nullptr) {
596 cJSON_Delete(jsonObj);
597 return;
598 }
599 std::string jsonStr(cjson);
600 std::vector<DistributedKv::Entry> deleteRecords;
601 DistributedKv::Entry del;
602 del.key = "update";
603 del.value = jsonStr.c_str();
604 deleteRecords.push_back(del);
605 ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityDeleteChange(deleteRecords));
606 cJSON_free(cjson);
607 cJSON_Delete(jsonObj);
608 }
609
610 HWTEST_F(MetaInfoMgrTest, GetEntriesByKeys_001, TestSize.Level0)
611 {
612 std::vector<std::string> keys;
613 auto ret = MetaInfoManager::GetInstance()->GetEntriesByKeys(keys);
614 EXPECT_EQ(0, ret.size());
615
616 keys.push_back("key_test");
617 ret = MetaInfoManager::GetInstance()->GetEntriesByKeys(keys);
618 EXPECT_EQ(0, ret.size());
619
620 MetaInfoManager::GetInstance()->Init();
621 ret = MetaInfoManager::GetInstance()->GetEntriesByKeys(keys);
622 EXPECT_EQ(0, ret.size());
623 MetaInfoManager::GetInstance()->UnInit();
624 }
625 }
626 }