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 "UTTest_relationship_sync_mgr.h"
17
18 #include "dm_constants.h"
19
20 namespace OHOS {
21 namespace DistributedHardware {
22 constexpr int32_t CREDID_PAYLOAD_LEN = 8;
23 constexpr int32_t USERID_PAYLOAD_LEN = 2;
24 const int32_t SHARE_UNBIND_PAYLOAD_LEN = 9;
25 const int32_t MAX_USER_ID_NUM = 5;
SetUp()26 void ReleationShipSyncMgrTest::SetUp()
27 {
28 }
29
TearDown()30 void ReleationShipSyncMgrTest::TearDown()
31 {
32 }
33
SetUpTestCase()34 void ReleationShipSyncMgrTest::SetUpTestCase()
35 {
36 }
37
TearDownTestCase()38 void ReleationShipSyncMgrTest::TearDownTestCase()
39 {
40 }
41
42 namespace {
43
44 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_001, testing::ext::TestSize.Level1)
45 {
46 std::vector<std::string> peerUdids;
47 RelationShipChangeMsg msg;
48 msg.type = RelationShipChangeType::ACCOUNT_LOGOUT;
49 msg.userId = UINT32_MAX;
50 msg.peerUdids = peerUdids;
51 msg.accountId = "1";
52 msg.accountName = "1";
53 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
54 ASSERT_EQ(broadCastMsg, "");
55 }
56
57 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_002, testing::ext::TestSize.Level1)
58 {
59 std::vector<std::string> peerUdids;
60 RelationShipChangeMsg msg;
61 msg.type = RelationShipChangeType::ACCOUNT_LOGOUT;
62 msg.userId = 1;
63 msg.peerUdids = peerUdids;
64 msg.accountId = "1";
65 msg.accountName = "1";
66 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
67 ASSERT_EQ(broadCastMsg, "");
68 }
69
70 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_003, testing::ext::TestSize.Level1)
71 {
72 std::vector<std::string> peerUdids;
73 RelationShipChangeMsg msg;
74 msg.type = RelationShipChangeType::ACCOUNT_LOGOUT;
75 msg.userId = 1;
76 msg.peerUdids = peerUdids;
77 msg.accountId = "1111111";
78 msg.accountName = "1";
79 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
80 ASSERT_NE(broadCastMsg, "");
81 }
82
83 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_004, testing::ext::TestSize.Level1)
84 {
85 std::vector<std::string> peerUdids;
86 RelationShipChangeMsg msg;
87 msg.type = RelationShipChangeType::DEVICE_UNBIND;
88 msg.userId = UINT32_MAX;
89 msg.peerUdids = peerUdids;
90 msg.accountId = "1111111";
91 msg.accountName = "1";
92 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
93 ASSERT_EQ(broadCastMsg, "");
94 }
95
96 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_005, testing::ext::TestSize.Level1)
97 {
98 std::vector<std::string> peerUdids;
99 RelationShipChangeMsg msg;
100 msg.type = RelationShipChangeType::DEVICE_UNBIND;
101 msg.userId = 1;
102 msg.peerUdids = peerUdids;
103 msg.accountId = "1111111";
104 msg.accountName = "1";
105 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
106 ASSERT_NE(broadCastMsg, "");
107 }
108
109 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_006, testing::ext::TestSize.Level1)
110 {
111 std::vector<std::string> peerUdids;
112 RelationShipChangeMsg msg;
113 msg.type = RelationShipChangeType::APP_UNBIND;
114 msg.userId = UINT32_MAX;
115 msg.peerUdids = peerUdids;
116 msg.accountId = "1111111";
117 msg.accountName = "1";
118 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
119 ASSERT_EQ(broadCastMsg, "");
120 }
121
122 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_007, testing::ext::TestSize.Level1)
123 {
124 std::vector<std::string> peerUdids;
125 RelationShipChangeMsg msg;
126 msg.type = RelationShipChangeType::APP_UNBIND;
127 msg.userId = 1;
128 msg.peerUdids = peerUdids;
129 msg.accountId = "1111111";
130 msg.accountName = "1";
131 msg.tokenId = UINT64_MAX;
132 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
133 ASSERT_EQ(broadCastMsg, "");
134 }
135
136 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_008, testing::ext::TestSize.Level1)
137 {
138 std::vector<std::string> peerUdids;
139 RelationShipChangeMsg msg;
140 msg.type = RelationShipChangeType::APP_UNBIND;
141 msg.userId = 1;
142 msg.peerUdids = peerUdids;
143 msg.accountId = "1111111";
144 msg.accountName = "1";
145 msg.tokenId = 1;
146 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
147 ASSERT_NE(broadCastMsg, "");
148 }
149
150 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_009, testing::ext::TestSize.Level1)
151 {
152 std::vector<std::string> peerUdids;
153 RelationShipChangeMsg msg;
154 msg.type = RelationShipChangeType::SERVICE_UNBIND;
155 msg.userId = 1;
156 msg.peerUdids = peerUdids;
157 msg.accountId = "1111111";
158 msg.accountName = "1";
159 msg.tokenId = 1;
160 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
161 msg.type = RelationShipChangeType::DEL_USER;
162 broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
163 msg.type = RelationShipChangeType::APP_UNINSTALL;
164 broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
165 msg.type = RelationShipChangeType::TYPE_MAX;
166 broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
167 ASSERT_EQ(broadCastMsg, "");
168 }
169
170 HWTEST_F(ReleationShipSyncMgrTest, ParseTrustRelationShipChange_006, testing::ext::TestSize.Level1)
171 {
172 std::string msg = R"(
173 {
174 "TYPE": 0,
175 "VALUE": [
176 { "type": 1, "userId": 1 },
177 { "type": 2, "userId": 2 }
178 ]
179 }
180 )";
181 RelationShipChangeMsg relationShipMsg =
182 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
183 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
184 msg = R"(
185 {
186 "TYPE": 1,
187 "VALUE": [
188 { "type": 1, "userId": 1 },
189 { "type": 2, "userId": 2 }
190 ]
191 }
192 )";
193 relationShipMsg = ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
194 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
195 msg = R"(
196 {
197 "TYPE": 2,
198 "VALUE": [
199 { "type": 1, "userId": 1 },
200 { "type": 2, "userId": 2 }
201 ]
202 }
203 )";
204 relationShipMsg = ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
205 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
206 }
207
208 HWTEST_F(ReleationShipSyncMgrTest, ParseTrustRelationShipChange_007, testing::ext::TestSize.Level1)
209 {
210 std::string msg = R"(
211 {
212 "TYPE": 6,
213 "VALUE": [
214 { "type": 1, "userId": 1 },
215 { "type": 2, "userId": 2 }
216 ]
217 }
218 )";
219 RelationShipChangeMsg relationShipMsg =
220 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
221 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
222 }
223
224 HWTEST_F(ReleationShipSyncMgrTest, IsValid_001, testing::ext::TestSize.Level1)
225 {
226 RelationShipChangeMsg msgObj;
227 msgObj.type = static_cast<RelationShipChangeType>(10);
228 bool ret = msgObj.IsValid();
229 ASSERT_EQ(ret, false);
230 }
231
232 HWTEST_F(ReleationShipSyncMgrTest, FromBroadcastPayLoad_001, testing::ext::TestSize.Level1)
233 {
234 RelationShipChangeMsg msgObj;
235 cJSON payloadJson;
236 RelationShipChangeType type = RelationShipChangeType::TYPE_MAX;
237 bool ret = msgObj.FromBroadcastPayLoad(&payloadJson, type);
238 ASSERT_EQ(ret, false);
239 type = RelationShipChangeType::SERVICE_UNBIND;
240 ret = msgObj.FromBroadcastPayLoad(&payloadJson, type);
241 ASSERT_EQ(ret, false);
242 }
243
244 HWTEST_F(ReleationShipSyncMgrTest, ToSyncFrontOrBackUserIdPayLoad_001, testing::ext::TestSize.Level1)
245 {
246 RelationShipChangeMsg msg;
247 msg.userIdInfos.push_back({1, false});
248 msg.userIdInfos.push_back({2, false});
249 msg.userIdInfos.push_back({3, false});
250 msg.userIdInfos.push_back({4, false});
251 msg.userIdInfos.push_back({5, false});
252 msg.userIdInfos.push_back({6, true});
253 uint8_t* msgPtr = nullptr;
254 uint32_t len = 0;
255 bool result = msg.ToSyncFrontOrBackUserIdPayLoad(msgPtr, len);
256 ASSERT_EQ(result, false);
257 if (msgPtr != nullptr) {
258 delete[] msgPtr;
259 msgPtr = nullptr;
260 }
261 }
262
263 HWTEST_F(ReleationShipSyncMgrTest, ToSyncFrontOrBackUserIdPayLoad_002, testing::ext::TestSize.Level1)
264 {
265 RelationShipChangeMsg msg;
266 msg.userIdInfos.push_back({1, false});
267 msg.userIdInfos.push_back({2, true});
268 uint8_t* msgPtr = nullptr;
269 uint32_t len = 10;
270 msg.syncUserIdFlag = true;
271 bool result = msg.ToSyncFrontOrBackUserIdPayLoad(msgPtr, len);
272 ASSERT_EQ(result, true);
273 if (msgPtr != nullptr) {
274 delete[] msgPtr;
275 msgPtr = nullptr;
276 }
277 }
278
279 HWTEST_F(ReleationShipSyncMgrTest, FromAccountLogoutPayLoad_001, testing::ext::TestSize.Level1)
280 {
281 RelationShipChangeMsg msg;
282 cJSON payloadJson;
283 bool result = msg.FromAccountLogoutPayLoad(&payloadJson);
284 ASSERT_EQ(result, false);
285 result = msg.FromAccountLogoutPayLoad(nullptr);
286 ASSERT_EQ(result, false);
287 }
288
289 HWTEST_F(ReleationShipSyncMgrTest, FromAccountLogoutPayLoad_002, testing::ext::TestSize.Level1)
290 {
291 RelationShipChangeMsg msg;
292 const char* jsonString = R"({
293 "MsgType": 0,
294 "userId": 12345,
295 "accountId": "a******3",
296 "tokenId": 67890,
297 "peerUdids": ["u******1", "u******2"],
298 "peerUdid": "p******d",
299 "accountName": "t******t",
300 "syncUserIdFlag": 1,
301 "userIds": [
302 {"type": 1, "userId": 111},
303 {"type": 0, "userId": 222}
304 ]
305 })";
306 cJSON *jsonObject = cJSON_Parse(jsonString);
307 bool result = msg.FromAccountLogoutPayLoad(jsonObject);
308 ASSERT_EQ(result, true);
309 }
310
311 HWTEST_F(ReleationShipSyncMgrTest, FromAccountLogoutPayLoad_003, testing::ext::TestSize.Level1)
312 {
313 RelationShipChangeMsg msg;
314 const char* jsonString = R"({
315 "MsgType": "0",
316 "userId": "12345",
317 "accountId": "a******3",
318 "tokenId": 67890,
319 "peerUdids": ["u******1", "u******2"],
320 "peerUdid": "p******d",
321 "accountName": "t******t",
322 "syncUserIdFlag": 1,
323 "userIds": [
324 {"type": 1, "userId": 111},
325 {"type": 0, "userId": 222}
326 ]
327 })";
328 cJSON *jsonObject = cJSON_Parse(jsonString);
329 bool result = msg.FromAccountLogoutPayLoad(jsonObject);
330 ASSERT_EQ(result, true);
331 }
332
333 HWTEST_F(ReleationShipSyncMgrTest, FromAccountLogoutPayLoad_004, testing::ext::TestSize.Level1)
334 {
335 RelationShipChangeMsg msg;
336 const char* jsonString = R"({
337 "MsgType": "0",
338 "userId": "12345",
339 "accountId": "a******3",
340 "tokenId": 67890,
341 "peerUdids": ["u******1", "u******2"],
342 "peerUdid": "p******d",
343 "accountName": "t******t",
344 "syncUserIdFlag": 1,
345 "test1": 1,
346 "test2": 2,
347 "test3": 3,
348 "test4": 4,
349 "userIds": [
350 {"type": 1, "userId": 111},
351 {"type": 0, "userId": 222}
352 ]
353 })";
354 cJSON *jsonObject = cJSON_Parse(jsonString);
355 bool result = msg.FromAccountLogoutPayLoad(jsonObject);
356 ASSERT_EQ(result, false);
357 }
358
359 HWTEST_F(ReleationShipSyncMgrTest, FromDeviceUnbindPayLoad_001, testing::ext::TestSize.Level1)
360 {
361 RelationShipChangeMsg msg;
362 cJSON payloadJson;
363 bool result = msg.FromDeviceUnbindPayLoad(&payloadJson);
364 ASSERT_EQ(result, false);
365 result = msg.FromDeviceUnbindPayLoad(nullptr);
366 ASSERT_EQ(result, false);
367 const char* jsonString = R"({
368 "MsgType": "0",
369 "userId": "12345",
370 "accountId": "a******3",
371 "tokenId": 67890,
372 "peerUdids": ["u******1", "u******2"],
373 "peerUdid": "p******d",
374 "accountName": "t******t",
375 "syncUserIdFlag": 1,
376 "test1": 1,
377 "test2": 2,
378 "test3": 3,
379 "test4": 4,
380 "userIds": [
381 {"type": 1, "userId": 111},
382 {"type": 0, "userId": 222}
383 ]
384 })";
385 cJSON *jsonObject = cJSON_Parse(jsonString);
386 result = msg.FromDeviceUnbindPayLoad(jsonObject);
387 ASSERT_EQ(result, false);
388 }
389
390 HWTEST_F(ReleationShipSyncMgrTest, FromDeviceUnbindPayLoad_002, testing::ext::TestSize.Level1)
391 {
392 RelationShipChangeMsg msg;
393 const char* jsonString = R"({
394 "MsgType": "0",
395 "userId": "12345",
396 "accountId": "a******3",
397 "tokenId": 67890,
398 "peerUdids": ["u******1", "u******2"],
399 "peerUdid": "p******d",
400 "accountName": "t******t",
401 "syncUserIdFlag": 1,
402 "userIds": [
403 {"type": 1, "userId": 111},
404 {"type": 0, "userId": 222}
405 ]
406 })";
407 cJSON *jsonObject = cJSON_Parse(jsonString);
408 bool result = msg.FromDeviceUnbindPayLoad(jsonObject);
409 ASSERT_EQ(result, true);
410 }
411
412 HWTEST_F(ReleationShipSyncMgrTest, FromDeviceUnbindPayLoad_003, testing::ext::TestSize.Level1)
413 {
414 RelationShipChangeMsg msg;
415 const char* jsonString = R"({
416 "MsgType": 0,
417 "userId": 12345,
418 "accountId": "a******3",
419 "tokenId": 67890,
420 "peerUdids": ["u******1", "u******2"],
421 "peerUdid": "p******d",
422 "accountName": "t******t",
423 "syncUserIdFlag": 1,
424 "userIds": [
425 {"type": 1, "userId": 111},
426 {"type": 0, "userId": 222}
427 ]
428 })";
429 cJSON *jsonObject = cJSON_Parse(jsonString);
430 bool result = msg.FromDeviceUnbindPayLoad(jsonObject);
431 ASSERT_EQ(result, true);
432 }
433
434 HWTEST_F(ReleationShipSyncMgrTest, FromAppUnbindPayLoad_001, testing::ext::TestSize.Level1)
435 {
436 RelationShipChangeMsg msg;
437 cJSON payloadJson;
438 bool result = msg.FromAppUnbindPayLoad(&payloadJson);
439 ASSERT_EQ(result, false);
440 result = msg.FromAppUnbindPayLoad(nullptr);
441 ASSERT_EQ(result, false);
442 const char* jsonString = R"({
443 "MsgType": "0",
444 "userId": "12345",
445 "accountId": "a******3",
446 "tokenId": 67890,
447 "peerUdids": ["u******1", "u******2"],
448 "peerUdid": "p******d",
449 "accountName": "t******t",
450 "syncUserIdFlag": 1,
451 "test1": 1,
452 "test2": 2,
453 "test3": 3,
454 "test4": 4,
455 "userIds": [
456 {"type": 1, "userId": 111},
457 {"type": 0, "userId": 222}
458 ]
459 })";
460 cJSON *jsonObject = cJSON_Parse(jsonString);
461 result = msg.FromAppUnbindPayLoad(jsonObject);
462 ASSERT_EQ(result, false);
463 }
464
465 HWTEST_F(ReleationShipSyncMgrTest, FromAppUnbindPayLoad_002, testing::ext::TestSize.Level1)
466 {
467 RelationShipChangeMsg msg;
468 const char* jsonString = R"({
469 "MsgType": "0",
470 "userId": "12345",
471 "accountId": "a******3",
472 "tokenId": 67890,
473 "peerUdids": ["u******1", "u******2"],
474 "peerUdid": "p******d",
475 "accountName": "t******t",
476 "syncUserIdFlag": 1,
477 "userIds": [
478 {"type": 1, "userId": 111},
479 {"type": 0, "userId": 222}
480 ]
481 })";
482 cJSON *jsonObject = cJSON_Parse(jsonString);
483 bool result = msg.FromAppUnbindPayLoad(jsonObject);
484 ASSERT_EQ(result, false);
485 }
486
487 HWTEST_F(ReleationShipSyncMgrTest, FromAppUnbindPayLoad_003, testing::ext::TestSize.Level1)
488 {
489 RelationShipChangeMsg msg;
490 const char* jsonString = R"({
491 "MsgType": 0,
492 "userId": 12345,
493 "accountId": "a******3",
494 "tokenId": 67890,
495 "peerUdids": ["u******1", "u******2"],
496 "peerUdid": "p******d",
497 "accountName": "t******t",
498 "syncUserIdFlag": 1,
499 "userIds": [
500 {"type": 1, "userId": 111},
501 {"type": 0, "userId": 222}
502 ]
503 })";
504 cJSON *jsonObject = cJSON_Parse(jsonString);
505 bool result = msg.FromAppUnbindPayLoad(jsonObject);
506 ASSERT_EQ(result, false);
507 }
508
509 HWTEST_F(ReleationShipSyncMgrTest, FromSyncFrontOrBackUserIdPayLoad_001, testing::ext::TestSize.Level1)
510 {
511 RelationShipChangeMsg msg;
512 cJSON payloadJson;
513 bool result = msg.FromSyncFrontOrBackUserIdPayLoad(&payloadJson);
514 ASSERT_EQ(result, false);
515 result = msg.FromSyncFrontOrBackUserIdPayLoad(nullptr);
516 ASSERT_EQ(result, false);
517 const char* jsonString = R"({
518 "MsgType": "0",
519 "userId": "12345",
520 "accountId": "a******3",
521 "tokenId": 67890,
522 "peerUdids": ["u******1", "u******2"],
523 "peerUdid": "p******d",
524 "accountName": "t******t",
525 "syncUserIdFlag": 1,
526 "test1": 1,
527 "test2": 2,
528 "test3": 3,
529 "test4": 4,
530 "userIds": [
531 {"type": 1, "userId": 111},
532 {"type": 0, "userId": 222}
533 ]
534 })";
535 cJSON *jsonObject = cJSON_Parse(jsonString);
536 result = msg.FromSyncFrontOrBackUserIdPayLoad(jsonObject);
537 ASSERT_EQ(result, false);
538 }
539
540 HWTEST_F(ReleationShipSyncMgrTest, FromSyncFrontOrBackUserIdPayLoad_002, testing::ext::TestSize.Level1)
541 {
542 RelationShipChangeMsg msg;
543 const char* jsonString = R"({
544 "MsgType": 6,
545 "userId": "12345",
546 "accountId": "a******3",
547 "tokenId": 67890,
548 "peerUdids": ["u******1", "u******2"],
549 "peerUdid": "p******d",
550 "accountName": "t******t",
551 "syncUserIdFlag": 1,
552 "userIds": [
553 {"type": 1, "userId": 111},
554 {"type": 0, "userId": 222}
555 ]
556 })";
557 cJSON *jsonObject = cJSON_Parse(jsonString);
558 bool result = msg.FromSyncFrontOrBackUserIdPayLoad(jsonObject);
559 ASSERT_EQ(result, false);
560 }
561
562 HWTEST_F(ReleationShipSyncMgrTest, FromSyncFrontOrBackUserIdPayLoad_003, testing::ext::TestSize.Level1)
563 {
564 RelationShipChangeMsg msg;
565 const char* jsonString = R"({
566 "MsgType": 1,
567 "userId": "12345",
568 "accountId": "a******3",
569 "tokenId": 67890,
570 "peerUdids": ["u******1", "u******2"],
571 "peerUdid": "p******d",
572 "accountName": "t******t",
573 "syncUserIdFlag": 1,
574 "userIds": [
575 {"type": 1, "userId": 111},
576 {"type": 0, "userId": 222}
577 ]
578 })";
579 cJSON *jsonObject = cJSON_Parse(jsonString);
580 bool result = msg.FromSyncFrontOrBackUserIdPayLoad(jsonObject);
581 ASSERT_EQ(result, false);
582 }
583
584 HWTEST_F(ReleationShipSyncMgrTest, FromSyncFrontOrBackUserIdPayLoad_004, testing::ext::TestSize.Level1)
585 {
586 RelationShipChangeMsg msg;
587 const char* jsonString = R"({
588 "MsgType": "1",
589 "userId": "12345",
590 "accountId": "a******3",
591 "tokenId": 67890,
592 "peerUdids": ["u******1", "u******2"],
593 "peerUdid": "p******d",
594 "accountName": "t******t",
595 "syncUserIdFlag": 1,
596 "userIds": [
597 {"type": 1, "userId": 111},
598 {"type": 0, "userId": 222}
599 ]
600 })";
601 cJSON *jsonObject = cJSON_Parse(jsonString);
602 bool result = msg.FromSyncFrontOrBackUserIdPayLoad(jsonObject);
603 ASSERT_EQ(result, false);
604 }
605
606 HWTEST_F(ReleationShipSyncMgrTest, FromSyncFrontOrBackUserIdPayLoad_005, testing::ext::TestSize.Level1)
607 {
608 RelationShipChangeMsg msg;
609 const char* jsonString = R"({
610 "MsgType": 1,
611 "userId": 12345,
612 "accountId": 3,
613 "tokenId": 67890,
614 "peerUdids": ["u******1", "u******2"],
615 "peerUdid": "p******d",
616 "accountName": "t******t",
617 "syncUserIdFlag": "1",
618 "userIds": [
619 {"type": 1, "userId": 111},
620 {"type": 0, "userId": 222}
621 ]
622 })";
623 cJSON *jsonObject = cJSON_Parse(jsonString);
624 bool result = msg.FromSyncFrontOrBackUserIdPayLoad(jsonObject);
625 ASSERT_EQ(result, true);
626 }
627
628 HWTEST_F(ReleationShipSyncMgrTest, FromDelUserPayLoad_001, testing::ext::TestSize.Level1)
629 {
630 RelationShipChangeMsg msg;
631 cJSON payloadJson;
632 bool result = msg.FromDelUserPayLoad(&payloadJson);
633 ASSERT_EQ(result, false);
634 result = msg.FromDelUserPayLoad(nullptr);
635 ASSERT_EQ(result, false);
636 const char* jsonString = R"({
637 "MsgType": 0,
638 "userId": "12345",
639 "accountId": "a******3",
640 "tokenId": 67890
641 })";
642 cJSON *jsonObject = cJSON_Parse(jsonString);
643 result = msg.FromDelUserPayLoad(jsonObject);
644 ASSERT_EQ(result, true);
645 }
646
647 HWTEST_F(ReleationShipSyncMgrTest, ToString_ValidData_001, testing::ext::TestSize.Level1)
648 {
649 RelationShipChangeMsg msg;
650 msg.type = RelationShipChangeType::APP_UNBIND;
651 msg.userId = 12345;
652 msg.accountId = "account_123";
653 msg.tokenId = 67890;
654 msg.peerUdids = {"udid1", "udid2"};
655 msg.peerUdid = "peer_udid";
656 msg.accountName = "test_account";
657 msg.syncUserIdFlag = true;
658
659 std::string expected = msg.ToString();
660 EXPECT_EQ(msg.ToString(), expected);
661 }
662
663 HWTEST_F(ReleationShipSyncMgrTest, ParseTrustRelationShipChange_001, testing::ext::TestSize.Level1)
664 {
665 std::string msg = "";
666 RelationShipChangeMsg relationShipMsg =
667 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
668 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
669 }
670
671 HWTEST_F(ReleationShipSyncMgrTest, ParseTrustRelationShipChange_002, testing::ext::TestSize.Level1)
672 {
673 std::string msg = R"(
674 {
675 "xx": "xx"
676 }
677 )";
678 RelationShipChangeMsg relationShipMsg =
679 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
680 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
681 }
682
683 HWTEST_F(ReleationShipSyncMgrTest, ParseTrustRelationShipChange_003, testing::ext::TestSize.Level1)
684 {
685 std::string msg = R"(
686 {
687 "TYPE": "xx"
688 }
689 )";
690 RelationShipChangeMsg relationShipMsg =
691 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
692 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
693 }
694
695 HWTEST_F(ReleationShipSyncMgrTest, ParseTrustRelationShipChange_004, testing::ext::TestSize.Level1)
696 {
697 std::string msg = R"(
698 {
699 "TYPE": 0
700 }
701 )";
702 RelationShipChangeMsg relationShipMsg =
703 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
704 msg = R"(
705 {
706 "TYPE": 1
707 }
708 )";
709 relationShipMsg =
710 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
711 msg = R"(
712 {
713 "TYPE": 2
714 }
715 )";
716 relationShipMsg =
717 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
718 msg = R"(
719 {
720 "TYPE": 3
721 }
722 )";
723 relationShipMsg =
724 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
725 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
726 }
727
728 HWTEST_F(ReleationShipSyncMgrTest, ParseTrustRelationShipChange_005, testing::ext::TestSize.Level1)
729 {
730 std::string msg = R"(
731 {
732 "TYPE": 0,
733 "VALUE": ""
734 }
735 )";
736 RelationShipChangeMsg relationShipMsg =
737 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
738 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
739 }
740
741 HWTEST_F(ReleationShipSyncMgrTest, ToSyncFrontOrBackUserIdPayLoad_ValidInput, testing::ext::TestSize.Level1)
742 {
743 RelationShipChangeMsg msg;
744 msg.userIdInfos.push_back({12345, true});
745 msg.userIdInfos.push_back({67890, false});
746
747 uint8_t* msgPtr = nullptr;
748 uint32_t len = 0;
749
750 bool result = msg.ToSyncFrontOrBackUserIdPayLoad(msgPtr, len);
751
752 ASSERT_TRUE(result);
753 ASSERT_EQ(len, 6);
754 ASSERT_EQ(msgPtr[0], 2);
755
756 delete[] msgPtr;
757 msg.userIdInfos.clear();
758 }
759
760 HWTEST_F(ReleationShipSyncMgrTest, ToDelUserPayLoad_ValidInput, testing::ext::TestSize.Level1)
761 {
762 RelationShipChangeMsg msg;
763 msg.userId = 12345;
764
765 uint8_t* msgPtr = nullptr;
766 uint32_t len = 0;
767
768 msg.ToDelUserPayLoad(msgPtr, len);
769
770 ASSERT_EQ(len, 3);
771 ASSERT_EQ(msgPtr[0], 0x39);
772
773 delete[] msgPtr;
774 msg.userIdInfos.clear();
775 }
776
777 HWTEST_F(ReleationShipSyncMgrTest, ToSyncFrontOrBackUserIdPayLoad_TooManyUserIds, testing::ext::TestSize.Level1)
778 {
779 RelationShipChangeMsg msg;
780 for (int i = 0; i < MAX_USER_ID_NUM + 1; ++i) {
781 msg.userIdInfos.push_back({i, true});
782 }
783
784 uint8_t* msgPtr = nullptr;
785 uint32_t len = 0;
786
787 bool result = msg.ToSyncFrontOrBackUserIdPayLoad(msgPtr, len);
788
789 ASSERT_FALSE(result);
790 ASSERT_EQ(msgPtr, nullptr);
791 msg.userIdInfos.clear();
792 }
793
794 HWTEST_F(ReleationShipSyncMgrTest, ToSyncFrontOrBackUserIdPayLoad_LengthExceedsLimit, testing::ext::TestSize.Level1)
795 {
796 RelationShipChangeMsg msg;
797 msg.userIdInfos.push_back({12345, true});
798 msg.userIdInfos.push_back({67890, false});
799 uint8_t* msgPtr = nullptr;
800 uint32_t len = 0;
801 bool result = msg.ToSyncFrontOrBackUserIdPayLoad(msgPtr, len);
802 ASSERT_TRUE(result);
803 }
804
805 HWTEST_F(ReleationShipSyncMgrTest, ToString_ValidData, testing::ext::TestSize.Level1)
806 {
807 RelationShipChangeMsg msg;
808 msg.type = RelationShipChangeType::APP_UNBIND;
809 msg.userId = 12345;
810 msg.accountId = "account_123";
811 msg.tokenId = 67890;
812 msg.peerUdids = {"udid1", "udid2"};
813 msg.peerUdid = "peer_udid";
814 msg.accountName = "test_account";
815 msg.syncUserIdFlag = true;
816 msg.userIdInfos = {{true, 111}, {false, 222}};
817
818 std::string expected = "{ MsgType: " + std::to_string(static_cast<uint32_t>(msg.type)) +
819 "{ isNewEvent: false, userId: 12345, accountId: a******3, tokenId: 67890, " +
820 "peerUdids: [ u******1, u******2 ], peerUdid: p******d, " +
821 "accountName: t******t, syncUserIdFlag: 1, " +
822 "userIds: [ { 1, userId: 111 }, { 0, userId: 222 } ], broadCastId: 0 }";
823 EXPECT_NE(msg.ToString(), expected);
824 }
825
826 HWTEST_F(ReleationShipSyncMgrTest, ToString_ValidData02, testing::ext::TestSize.Level1)
827 {
828 bool isForeground = true;
829 std::uint16_t userId = 123;
830 UserIdInfo userIdInfo(isForeground, userId);
831 std::string expected = "{ 1, userId: 123 }";
832 EXPECT_EQ(userIdInfo.ToString(), expected);
833 }
834
835 HWTEST_F(ReleationShipSyncMgrTest, ToString_ZeroUserId, testing::ext::TestSize.Level1)
836 {
837 bool isForeground = false;
838 std::uint16_t userId = 0;
839 UserIdInfo userIdInfo(isForeground, userId);
840
841 std::string expected = "{ 0, userId: 0 }";
842 EXPECT_EQ(userIdInfo.ToString(), expected);
843 }
844
845 HWTEST_F(ReleationShipSyncMgrTest, ValidList, testing::ext::TestSize.Level1)
846 {
847 std::vector<UserIdInfo> list = {{true, 1}, {false, 2}};
848 std::string expected = "[ { 1, userId: 1 }, { 0, userId: 2 } ]";
849
850 EXPECT_EQ(GetUserIdInfoList(list), expected);
851 }
852
853 HWTEST_F(ReleationShipSyncMgrTest, EmptyList, testing::ext::TestSize.Level1)
854 {
855 std::vector<UserIdInfo> list;
856 std::string expected = "[ ]";
857
858 EXPECT_EQ(GetUserIdInfoList(list), expected);
859 }
860
861 HWTEST_F(ReleationShipSyncMgrTest, ValidData, testing::ext::TestSize.Level1)
862 {
863 std::vector<UserIdInfo> remoteUserIdInfos = {{true, 1}, {false, 2}, {true, 3}};
864 std::vector<UserIdInfo> foregroundUserIdInfos;
865 std::vector<UserIdInfo> backgroundUserIdInfos;
866
867 GetFrontAndBackUserIdInfos(remoteUserIdInfos, foregroundUserIdInfos, backgroundUserIdInfos);
868
869 EXPECT_EQ(foregroundUserIdInfos.size(), 2);
870 EXPECT_EQ(backgroundUserIdInfos.size(), 1);
871 EXPECT_EQ(foregroundUserIdInfos[0].userId, 1);
872 EXPECT_EQ(foregroundUserIdInfos[1].userId, 3);
873 EXPECT_EQ(backgroundUserIdInfos[0].userId, 2);
874 }
875
876 HWTEST_F(ReleationShipSyncMgrTest, EmptyRemoteList, testing::ext::TestSize.Level1)
877 {
878 std::vector<UserIdInfo> remoteUserIdInfos;
879 std::vector<UserIdInfo> foregroundUserIdInfos;
880 std::vector<UserIdInfo> backgroundUserIdInfos;
881
882 GetFrontAndBackUserIdInfos(remoteUserIdInfos, foregroundUserIdInfos, backgroundUserIdInfos);
883
884 EXPECT_TRUE(foregroundUserIdInfos.empty());
885 EXPECT_TRUE(backgroundUserIdInfos.empty());
886 }
887
888 HWTEST_F(ReleationShipSyncMgrTest, FromBroadcastPayLoad_010, testing::ext::TestSize.Level1)
889 {
890 RelationShipChangeMsg msg;
891 cJSON *payloadJson = NULL;
892 RelationShipChangeType type = RelationShipChangeType::TYPE_MAX;
893 bool ret = msg.FromBroadcastPayLoad(payloadJson, type);
894 EXPECT_FALSE(ret);
895
896 type = RelationShipChangeType::ACCOUNT_LOGOUT;
897 ret = msg.FromBroadcastPayLoad(payloadJson, type);
898 EXPECT_FALSE(ret);
899
900 type = RelationShipChangeType::DEVICE_UNBIND;
901 ret = msg.FromBroadcastPayLoad(payloadJson, type);
902 EXPECT_FALSE(ret);
903
904 type = RelationShipChangeType::APP_UNBIND;
905 ret = msg.FromBroadcastPayLoad(payloadJson, type);
906 EXPECT_FALSE(ret);
907
908 type = RelationShipChangeType::SYNC_USERID;
909 ret = msg.FromBroadcastPayLoad(payloadJson, type);
910 EXPECT_FALSE(ret);
911
912 type = RelationShipChangeType::DEL_USER;
913 ret = msg.FromBroadcastPayLoad(payloadJson, type);
914 EXPECT_FALSE(ret);
915
916 type = RelationShipChangeType::APP_UNINSTALL;
917 ret = msg.FromBroadcastPayLoad(payloadJson, type);
918 EXPECT_FALSE(ret);
919
920 type = RelationShipChangeType::SHARE_UNBIND;
921 ret = msg.FromBroadcastPayLoad(payloadJson, type);
922 EXPECT_FALSE(ret);
923
924 type = RelationShipChangeType::APP_UNINSTALL;
925 ret = msg.FromBroadcastPayLoad(payloadJson, type);
926 EXPECT_FALSE(ret);
927 }
928
929 HWTEST_F(ReleationShipSyncMgrTest, IsChangeTypeValid_001, testing::ext::TestSize.Level1)
930 {
931 RelationShipChangeMsg msg;
932 msg.type = RelationShipChangeType::ACCOUNT_LOGOUT;
933 bool ret = msg.IsChangeTypeValid();
934 EXPECT_TRUE(ret);
935
936 msg.type = RelationShipChangeType::DEVICE_UNBIND;
937 ret = msg.IsChangeTypeValid();
938 EXPECT_TRUE(ret);
939
940 msg.type = RelationShipChangeType::APP_UNBIND;
941 ret = msg.IsChangeTypeValid();
942 EXPECT_TRUE(ret);
943
944 msg.type = RelationShipChangeType::SYNC_USERID;
945 ret = msg.IsChangeTypeValid();
946 EXPECT_TRUE(ret);
947
948 msg.type = RelationShipChangeType::DEL_USER;
949 ret = msg.IsChangeTypeValid();
950 EXPECT_TRUE(ret);
951
952 msg.type = RelationShipChangeType::STOP_USER;
953 ret = msg.IsChangeTypeValid();
954 EXPECT_TRUE(ret);
955 }
956
957 HWTEST_F(ReleationShipSyncMgrTest, IsValid_010, testing::ext::TestSize.Level1)
958 {
959 RelationShipChangeMsg msg;
960 msg.type = RelationShipChangeType::SYNC_USERID;
961 msg.userIdInfos.push_back({1, false});
962 msg.userIdInfos.push_back({2, true});
963 bool ret = msg.IsValid();
964 EXPECT_TRUE(ret);
965
966 msg.type = static_cast<RelationShipChangeType>(9);
967 ret = msg.IsValid();
968 EXPECT_FALSE(ret);
969 }
970
971 HWTEST_F(ReleationShipSyncMgrTest, IsValid_011, testing::ext::TestSize.Level1)
972 {
973 RelationShipChangeMsg msg;
974 msg.type = RelationShipChangeType::SHARE_UNBIND;
975 msg.userId = 12345;
976
977 ASSERT_TRUE(msg.IsValid());
978
979 msg.userId = UINT32_MAX;
980 ASSERT_FALSE(msg.IsValid());
981 }
982
983 HWTEST_F(ReleationShipSyncMgrTest, IsValid_012, testing::ext::TestSize.Level1)
984 {
985 RelationShipChangeMsg msg;
986 msg.type = RelationShipChangeType::APP_UNINSTALL;
987 msg.userId = 12345;
988 msg.tokenId = 12345;
989
990 ASSERT_TRUE(msg.IsValid());
991
992 msg.userId = UINT32_MAX;
993 ASSERT_FALSE(msg.IsValid());
994 }
995
996 /**
997 * @tc.name: ToBroadcastPayLoad_001
998 * @tc.type: FUNC
999 */
1000 HWTEST_F(ReleationShipSyncMgrTest, ToBroadcastPayLoad_001, testing::ext::TestSize.Level1)
1001 {
1002 RelationShipChangeMsg msg;
1003 msg.type = RelationShipChangeType::ACCOUNT_LOGOUT;
1004 msg.userId = 12345;
1005 msg.accountId = "test_account";
1006
1007 uint8_t* msgPtr = nullptr;
1008 uint32_t len = 0;
1009
1010 bool result = msg.ToBroadcastPayLoad(msgPtr, len);
1011 ASSERT_TRUE(result);
1012 ASSERT_NE(msgPtr, nullptr);
1013 ASSERT_GT(len, 0);
1014 delete[] msgPtr;
1015 msgPtr = nullptr;
1016
1017 msg.userId = 0;
1018 msg.accountId = "test_01";
1019 uint8_t *load = nullptr;
1020 len = 0;
1021 result = msg.ToBroadcastPayLoad(load, len);
1022 EXPECT_EQ(len, 9);
1023 EXPECT_EQ(result, true);
1024 if (load != nullptr) {
1025 delete[] load;
1026 load = nullptr;
1027 }
1028 }
1029
1030 /**
1031 * @tc.name: ToBroadcastPayLoad_002
1032 * @tc.type: FUNC
1033 */
1034 HWTEST_F(ReleationShipSyncMgrTest, ToBroadcastPayLoad_002, testing::ext::TestSize.Level1)
1035 {
1036 RelationShipChangeMsg msg;
1037 msg.type = RelationShipChangeType::DEVICE_UNBIND;
1038 msg.userId = 12345;
1039 msg.peerUdids = {"udid1", "udid2"};
1040
1041 uint8_t* msgPtr = nullptr;
1042 uint32_t len = 0;
1043
1044 bool result = msg.ToBroadcastPayLoad(msgPtr, len);
1045 ASSERT_TRUE(result);
1046 ASSERT_NE(msgPtr, nullptr);
1047 ASSERT_GT(len, 0);
1048 delete[] msgPtr;
1049 msgPtr = nullptr;
1050
1051 msg.userId = 1;
1052 msg.accountId = "test";
1053 uint8_t *load= nullptr;
1054 len = 0;
1055 result = msg.ToBroadcastPayLoad(load, len);
1056 EXPECT_EQ(result, true);
1057 if (load != nullptr) {
1058 delete[] load;
1059 load = nullptr;
1060 }
1061 }
1062
1063 /**
1064 * @tc.name: ToBroadcastPayLoad_003
1065 * @tc.type: FUNC
1066 */
1067 HWTEST_F(ReleationShipSyncMgrTest, ToBroadcastPayLoad_003, testing::ext::TestSize.Level1)
1068 {
1069 RelationShipChangeMsg msg;
1070 msg.type = RelationShipChangeType::APP_UNBIND;
1071 msg.userId = 12345;
1072 msg.tokenId = 67890;
1073
1074 uint8_t* msgPtr = nullptr;
1075 uint32_t len = 0;
1076
1077 bool result = msg.ToBroadcastPayLoad(msgPtr, len);
1078 ASSERT_TRUE(result);
1079 ASSERT_NE(msgPtr, nullptr);
1080 ASSERT_GT(len, 0);
1081 delete[] msgPtr;
1082 msgPtr = nullptr;
1083 }
1084
1085 HWTEST_F(ReleationShipSyncMgrTest, ToBroadcastPayLoad_004, testing::ext::TestSize.Level1)
1086 {
1087 RelationShipChangeMsg msg;
1088 msg.type = RelationShipChangeType::SYNC_USERID;
1089 msg.userIdInfos.push_back({12345, true});
1090 msg.userIdInfos.push_back({67890, false});
1091
1092 uint8_t* msgPtr = nullptr;
1093 uint32_t len = 0;
1094
1095 bool result = msg.ToBroadcastPayLoad(msgPtr, len);
1096 ASSERT_TRUE(result);
1097 ASSERT_NE(msgPtr, nullptr);
1098 ASSERT_GT(len, 0);
1099 delete[] msgPtr;
1100 msgPtr = nullptr;
1101 }
1102
1103 HWTEST_F(ReleationShipSyncMgrTest, ToBroadcastPayLoad_005, testing::ext::TestSize.Level1)
1104 {
1105 RelationShipChangeMsg msg;
1106 msg.type = RelationShipChangeType::SYNC_USERID;
1107 for (int i = 0; i < MAX_USER_ID_NUM + 1; ++i) {
1108 msg.userIdInfos.push_back({i, true});
1109 }
1110
1111 uint8_t* msgPtr = nullptr;
1112 uint32_t len = 0;
1113
1114 bool result = msg.ToBroadcastPayLoad(msgPtr, len);
1115 ASSERT_FALSE(result);
1116 ASSERT_EQ(msgPtr, nullptr);
1117
1118 msg.userId = 2;
1119 msg.accountId = "test";
1120 msg.tokenId = 100;
1121 uint8_t *load = nullptr;
1122 len = 0;
1123 result = msg.ToBroadcastPayLoad(load, len);
1124 EXPECT_EQ(result, false);
1125 if (load != nullptr) {
1126 delete[] load;
1127 load = nullptr;
1128 }
1129 }
1130
1131 /**
1132 * @tc.name: ToBroadcastPayLoad_006
1133 * @tc.type: FUNC
1134 */
1135 HWTEST_F(ReleationShipSyncMgrTest, ToBroadcastPayLoad_006, testing::ext::TestSize.Level1)
1136 {
1137 RelationShipChangeMsg msg;
1138 msg.type = RelationShipChangeType::SERVICE_UNBIND;
1139 msg.userId = 4;
1140 msg.accountId = "test";
1141 uint8_t *load = nullptr;
1142 uint32_t len = 0;
1143 bool result = msg.ToBroadcastPayLoad(load, len);
1144 EXPECT_EQ(result, true);
1145 if (load != nullptr) {
1146 delete[] load;
1147 load = nullptr;
1148 }
1149 }
1150
1151 /**
1152 * @tc.name: ToBroadcastPayLoad_007
1153 * @tc.type: FUNC
1154 */
1155 HWTEST_F(ReleationShipSyncMgrTest, ToBroadcastPayLoad_007, testing::ext::TestSize.Level1)
1156 {
1157 RelationShipChangeMsg msg;
1158 UserIdInfo foregroundUser(true, 1);
1159 UserIdInfo backgroundUser(false, 2);
1160 msg.type = RelationShipChangeType::SYNC_USERID;
1161 msg.userId = 5;
1162 msg.accountId = "test";
1163 msg.userIdInfos.push_back(foregroundUser);
1164 msg.userIdInfos.push_back(backgroundUser);
1165 uint8_t *load = nullptr;
1166 uint32_t len = 0;
1167 bool result = msg.ToBroadcastPayLoad(load, len);
1168 EXPECT_EQ(result, true);
1169 if (load != nullptr) {
1170 delete[] load;
1171 load = nullptr;
1172 }
1173 }
1174
1175 /**
1176 * @tc.name: ToBroadcastPayLoad_008
1177 * @tc.type: FUNC
1178 */
1179 HWTEST_F(ReleationShipSyncMgrTest, ToBroadcastPayLoad_008, testing::ext::TestSize.Level1)
1180 {
1181 RelationShipChangeMsg msg;
1182 msg.type = RelationShipChangeType::DEL_USER;
1183 msg.userId = 12345;
1184
1185 uint8_t* msgPtr = nullptr;
1186 uint32_t len = 0;
1187
1188 bool result = msg.ToBroadcastPayLoad(msgPtr, len);
1189 ASSERT_TRUE(result);
1190 ASSERT_NE(msgPtr, nullptr);
1191 ASSERT_GT(len, 0);
1192 delete[] msgPtr;
1193 msgPtr = nullptr;
1194
1195 msg.userId = 7;
1196 msg.accountId = "test";
1197 uint8_t *load = nullptr;
1198 len = 0;
1199 result = msg.ToBroadcastPayLoad(load, len);
1200 EXPECT_EQ(result, true);
1201 if (load != nullptr) {
1202 delete[] load;
1203 load = nullptr;
1204 }
1205 }
1206
1207 /**
1208 * @tc.name: ToBroadcastPayLoad_009
1209 * @tc.type: FUNC
1210 */
1211 HWTEST_F(ReleationShipSyncMgrTest, ToBroadcastPayLoad_009, testing::ext::TestSize.Level1)
1212 {
1213 RelationShipChangeMsg msg;
1214 msg.type = RelationShipChangeType::STOP_USER;
1215 msg.userId = 12345;
1216
1217 uint8_t* msgPtr = nullptr;
1218 uint32_t len = 0;
1219
1220 bool result = msg.ToBroadcastPayLoad(msgPtr, len);
1221 ASSERT_TRUE(result);
1222 ASSERT_NE(msgPtr, nullptr);
1223 ASSERT_GT(len, 0);
1224 delete[] msgPtr;
1225 msgPtr = nullptr;
1226 }
1227
1228 HWTEST_F(ReleationShipSyncMgrTest, ToBroadcastPayLoad_010, testing::ext::TestSize.Level1)
1229 {
1230 RelationShipChangeMsg msg;
1231 msg.type = RelationShipChangeType::SHARE_UNBIND;
1232 msg.userId = 12345;
1233 msg.peerUdids = {"udid1", "udid2"};
1234
1235 uint8_t* msgPtr = nullptr;
1236 uint32_t len = 0;
1237
1238 bool result = msg.ToBroadcastPayLoad(msgPtr, len);
1239 ASSERT_TRUE(result);
1240 ASSERT_EQ(msgPtr, nullptr);
1241 delete[] msgPtr;
1242 msgPtr = nullptr;
1243 }
1244
1245 HWTEST_F(ReleationShipSyncMgrTest, ToBroadcastPayLoad_011, testing::ext::TestSize.Level1)
1246 {
1247 RelationShipChangeMsg msg;
1248 msg.type = static_cast<RelationShipChangeType>(99);
1249
1250 uint8_t* msgPtr = nullptr;
1251 uint32_t len = 0;
1252
1253 bool result = msg.ToBroadcastPayLoad(msgPtr, len);
1254 ASSERT_FALSE(result);
1255 ASSERT_EQ(msgPtr, nullptr);
1256 ASSERT_EQ(len, 0);
1257
1258 msg.userId = 8;
1259 msg.accountId = "test";
1260 uint8_t *load = nullptr;
1261 len = 0;
1262 result = msg.ToBroadcastPayLoad(load, len);
1263 EXPECT_EQ(result, false);
1264 if (load != nullptr) {
1265 delete[] load;
1266 load = nullptr;
1267 }
1268 }
1269
1270 /**
1271 * @tc.name: ToBroadcastPayLoad_012
1272 * @tc.type: FUNC
1273 */
1274 HWTEST_F(ReleationShipSyncMgrTest, ToBroadcastPayLoad_012, testing::ext::TestSize.Level1)
1275 {
1276 RelationShipChangeMsg msg;
1277 msg.type = RelationShipChangeType::TYPE_MAX;
1278 msg.userId = 9;
1279 msg.accountId = "test";
1280 uint8_t *load = nullptr;
1281 uint32_t len = 0;
1282 bool result = msg.ToBroadcastPayLoad(load, len);
1283 EXPECT_EQ(result, false);
1284 if (load != nullptr) {
1285 delete[] load;
1286 load = nullptr;
1287 }
1288 }
1289
1290 HWTEST_F(ReleationShipSyncMgrTest, ToBroadcastPayLoad_013, testing::ext::TestSize.Level1)
1291 {
1292 RelationShipChangeMsg msg;
1293 msg.type = RelationShipChangeType::APP_UNINSTALL;
1294 msg.userId = 12345;
1295 msg.tokenId = 67890;
1296 msg.peerUdids = {"udid1", "udid2"};
1297
1298 uint8_t* msgPtr = nullptr;
1299 uint32_t len = 0;
1300
1301 bool result = msg.ToBroadcastPayLoad(msgPtr, len);
1302 ASSERT_TRUE(result);
1303 delete[] msgPtr;
1304 msgPtr = nullptr;
1305 }
1306
1307 HWTEST_F(ReleationShipSyncMgrTest, ToShareUnbindPayLoad_001, testing::ext::TestSize.Level1)
1308 {
1309 RelationShipChangeMsg msg;
1310 msg.userId = 12345;
1311 msg.credId = "12345678";
1312
1313 uint8_t* msgPtr = nullptr;
1314 uint32_t len = 0;
1315
1316 msg.ToShareUnbindPayLoad(msgPtr, len);
1317
1318 ASSERT_EQ(len, 9);
1319 ASSERT_NE(msgPtr, nullptr);
1320
1321 ASSERT_EQ(msgPtr[0], static_cast<uint8_t>(msg.userId & 0xFF));
1322 ASSERT_EQ(msgPtr[1], static_cast<uint8_t>((msg.userId >> 8) & 0xFF));
1323
1324 for (int i = 0; i < CREDID_PAYLOAD_LEN - USERID_PAYLOAD_LEN; i++) {
1325 ASSERT_EQ(msgPtr[USERID_PAYLOAD_LEN + i], msg.credId[i]);
1326 }
1327
1328 delete[] msgPtr;
1329 }
1330
1331 HWTEST_F(ReleationShipSyncMgrTest, ToShareUnbindPayLoad_002, testing::ext::TestSize.Level1)
1332 {
1333 RelationShipChangeMsg msg;
1334 msg.userId = 12345;
1335 msg.credId = "12345678";
1336
1337 uint8_t* msgPtr = nullptr;
1338 uint32_t len = 0;
1339
1340 msg.ToShareUnbindPayLoad(msgPtr, len);
1341
1342 ASSERT_EQ(len, SHARE_UNBIND_PAYLOAD_LEN);
1343 ASSERT_NE(msgPtr, nullptr);
1344
1345 ASSERT_EQ(msgPtr[0], static_cast<uint8_t>(msg.userId & 0xFF));
1346 ASSERT_EQ(msgPtr[1], static_cast<uint8_t>((msg.userId >> 8) & 0xFF));
1347
1348 delete[] msgPtr;
1349 }
1350
1351 HWTEST_F(ReleationShipSyncMgrTest, ToShareUnbindPayLoad_003, testing::ext::TestSize.Level1)
1352 {
1353 RelationShipChangeMsg msg;
1354 msg.userId = UINT16_MAX;
1355 msg.credId = "12345678";
1356
1357 uint8_t* msgPtr = nullptr;
1358 uint32_t len = 0;
1359
1360 msg.ToShareUnbindPayLoad(msgPtr, len);
1361
1362 ASSERT_EQ(len, SHARE_UNBIND_PAYLOAD_LEN);
1363 ASSERT_NE(msgPtr, nullptr);
1364
1365 ASSERT_EQ(msgPtr[0], static_cast<uint8_t>(msg.userId & 0xFF));
1366 ASSERT_EQ(msgPtr[1], static_cast<uint8_t>((msg.userId >> 8) & 0xFF));
1367
1368 for (int i = 0; i < CREDID_PAYLOAD_LEN - USERID_PAYLOAD_LEN; i++) {
1369 ASSERT_EQ(msgPtr[USERID_PAYLOAD_LEN + i], msg.credId[i]);
1370 }
1371
1372 delete[] msgPtr;
1373 }
1374
1375 HWTEST_F(ReleationShipSyncMgrTest, FromShareUnbindPayLoad_001, testing::ext::TestSize.Level1)
1376 {
1377 RelationShipChangeMsg msg;
1378 cJSON *payloadJson = nullptr;
1379
1380 bool result = msg.FromShareUnbindPayLoad(payloadJson);
1381
1382 ASSERT_FALSE(result);
1383 }
1384
1385 HWTEST_F(ReleationShipSyncMgrTest, FromShareUnbindPayLoad_002, testing::ext::TestSize.Level1)
1386 {
1387 RelationShipChangeMsg msg;
1388 cJSON *payloadJson = cJSON_CreateArray();
1389
1390 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber(1));
1391
1392 bool result = msg.FromShareUnbindPayLoad(payloadJson);
1393
1394 ASSERT_FALSE(result);
1395
1396 cJSON_Delete(payloadJson);
1397 }
1398
1399 HWTEST_F(ReleationShipSyncMgrTest, FromShareUnbindPayLoad_003, testing::ext::TestSize.Level1)
1400 {
1401 RelationShipChangeMsg msg;
1402 cJSON *payloadJson = cJSON_CreateArray();
1403
1404 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber(0x12));
1405 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber(0x34));
1406 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber('c'));
1407 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber('r'));
1408 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber('e'));
1409 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber('d'));
1410 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber('1'));
1411 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber('2'));
1412 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber('3'));
1413
1414 bool result = msg.FromShareUnbindPayLoad(payloadJson);
1415
1416 ASSERT_TRUE(result);
1417 ASSERT_EQ(msg.userId, 0x3412);
1418 ASSERT_EQ(msg.credId, "cred12");
1419
1420 cJSON_Delete(payloadJson);
1421 }
1422
1423 HWTEST_F(ReleationShipSyncMgrTest, FromShareUnbindPayLoad_004, testing::ext::TestSize.Level1)
1424 {
1425 RelationShipChangeMsg msg;
1426 cJSON *payloadJson = cJSON_CreateArray();
1427
1428 cJSON_AddItemToArray(payloadJson, cJSON_CreateString("invalid"));
1429
1430 bool result = msg.FromShareUnbindPayLoad(payloadJson);
1431
1432 ASSERT_FALSE(result);
1433
1434 cJSON_Delete(payloadJson);
1435 }
1436
1437 HWTEST_F(ReleationShipSyncMgrTest, FromShareUnbindPayLoad_005, testing::ext::TestSize.Level1)
1438 {
1439 RelationShipChangeMsg msg;
1440 cJSON *payloadJson = cJSON_CreateArray();
1441
1442 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber(0x12));
1443 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber(0x34));
1444 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber('c'));
1445 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber('r'));
1446 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber('e'));
1447 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber('d'));
1448 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber('1'));
1449 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber('2'));
1450
1451 cJSON_AddItemToArray(payloadJson, cJSON_CreateNumber(0x56));
1452
1453 bool result = msg.FromShareUnbindPayLoad(payloadJson);
1454
1455 ASSERT_TRUE(result);
1456
1457 cJSON_Delete(payloadJson);
1458 }
1459
1460 HWTEST_F(ReleationShipSyncMgrTest, ToAppUninstallPayLoad_001, testing::ext::TestSize.Level1)
1461 {
1462 RelationShipChangeMsg msg;
1463 msg.userId = 12345;
1464 msg.tokenId = 67890;
1465 msg.broadCastId = 0;
1466 uint8_t* msgPtr = nullptr;
1467 uint32_t len = 0;
1468 msg.ToAppUninstallPayLoad(msgPtr, len);
1469 ASSERT_EQ(len, 7);
1470 }
1471
1472 HWTEST_F(ReleationShipSyncMgrTest, FromAppUninstallPayLoad_001, testing::ext::TestSize.Level1)
1473 {
1474 RelationShipChangeMsg msg;
1475 cJSON payloadJson;
1476 bool result = msg.FromAppUninstallPayLoad(&payloadJson);
1477 ASSERT_EQ(result, false);
1478 result = msg.FromAppUninstallPayLoad(nullptr);
1479 ASSERT_EQ(result, false);
1480 const char* jsonString = R"({
1481 "MsgType": "0",
1482 "userId": "12345",
1483 "accountId": "a******3",
1484 "tokenId": 67890,
1485 "peerUdids": ["u******1", "u******2"],
1486 "peerUdid": "p******d",
1487 "accountName": "t******t",
1488 "syncUserIdFlag": 1,
1489 "test1": 1,
1490 "test2": 2,
1491 "test3": 3,
1492 "test4": 4,
1493 "userIds": [
1494 {"type": 1, "userId": 111},
1495 {"type": 0, "userId": 222}
1496 ]
1497 })";
1498 cJSON *jsonObject = cJSON_Parse(jsonString);
1499 result = msg.FromAppUninstallPayLoad(jsonObject);
1500 ASSERT_EQ(result, false);
1501 }
1502
1503 }
1504 } // namespace DistributedHardware
1505 } // namespace OHOS
1506