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 const int32_t MAX_USER_ID_NUM = 5;
SetUp()23 void ReleationShipSyncMgrTest::SetUp()
24 {
25 }
26
TearDown()27 void ReleationShipSyncMgrTest::TearDown()
28 {
29 }
30
SetUpTestCase()31 void ReleationShipSyncMgrTest::SetUpTestCase()
32 {
33 }
34
TearDownTestCase()35 void ReleationShipSyncMgrTest::TearDownTestCase()
36 {
37 }
38
39 namespace {
40
41 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_001, testing::ext::TestSize.Level0)
42 {
43 std::vector<std::string> peerUdids;
44 RelationShipChangeMsg msg;
45 msg.type = RelationShipChangeType::ACCOUNT_LOGOUT;
46 msg.userId = UINT32_MAX;
47 msg.peerUdids = peerUdids;
48 msg.accountId = "1";
49 msg.accountName = "1";
50 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
51 ASSERT_EQ(broadCastMsg, "");
52 }
53
54 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_002, testing::ext::TestSize.Level0)
55 {
56 std::vector<std::string> peerUdids;
57 RelationShipChangeMsg msg;
58 msg.type = RelationShipChangeType::ACCOUNT_LOGOUT;
59 msg.userId = 1;
60 msg.peerUdids = peerUdids;
61 msg.accountId = "1";
62 msg.accountName = "1";
63 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
64 ASSERT_EQ(broadCastMsg, "");
65 }
66
67 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_003, testing::ext::TestSize.Level0)
68 {
69 std::vector<std::string> peerUdids;
70 RelationShipChangeMsg msg;
71 msg.type = RelationShipChangeType::ACCOUNT_LOGOUT;
72 msg.userId = 1;
73 msg.peerUdids = peerUdids;
74 msg.accountId = "1111111";
75 msg.accountName = "1";
76 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
77 ASSERT_NE(broadCastMsg, "");
78 }
79
80 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_004, testing::ext::TestSize.Level0)
81 {
82 std::vector<std::string> peerUdids;
83 RelationShipChangeMsg msg;
84 msg.type = RelationShipChangeType::DEVICE_UNBIND;
85 msg.userId = UINT32_MAX;
86 msg.peerUdids = peerUdids;
87 msg.accountId = "1111111";
88 msg.accountName = "1";
89 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
90 ASSERT_EQ(broadCastMsg, "");
91 }
92
93 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_005, testing::ext::TestSize.Level0)
94 {
95 std::vector<std::string> peerUdids;
96 RelationShipChangeMsg msg;
97 msg.type = RelationShipChangeType::DEVICE_UNBIND;
98 msg.userId = 1;
99 msg.peerUdids = peerUdids;
100 msg.accountId = "1111111";
101 msg.accountName = "1";
102 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
103 ASSERT_NE(broadCastMsg, "");
104 }
105
106 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_006, testing::ext::TestSize.Level0)
107 {
108 std::vector<std::string> peerUdids;
109 RelationShipChangeMsg msg;
110 msg.type = RelationShipChangeType::APP_UNBIND;
111 msg.userId = UINT32_MAX;
112 msg.peerUdids = peerUdids;
113 msg.accountId = "1111111";
114 msg.accountName = "1";
115 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
116 ASSERT_EQ(broadCastMsg, "");
117 }
118
119 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_007, testing::ext::TestSize.Level0)
120 {
121 std::vector<std::string> peerUdids;
122 RelationShipChangeMsg msg;
123 msg.type = RelationShipChangeType::APP_UNBIND;
124 msg.userId = 1;
125 msg.peerUdids = peerUdids;
126 msg.accountId = "1111111";
127 msg.accountName = "1";
128 msg.tokenId = UINT64_MAX;
129 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
130 ASSERT_EQ(broadCastMsg, "");
131 }
132
133 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_008, testing::ext::TestSize.Level0)
134 {
135 std::vector<std::string> peerUdids;
136 RelationShipChangeMsg msg;
137 msg.type = RelationShipChangeType::APP_UNBIND;
138 msg.userId = 1;
139 msg.peerUdids = peerUdids;
140 msg.accountId = "1111111";
141 msg.accountName = "1";
142 msg.tokenId = 1;
143 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
144 ASSERT_NE(broadCastMsg, "");
145 }
146
147 HWTEST_F(ReleationShipSyncMgrTest, SyncTrustRelationShip_009, testing::ext::TestSize.Level0)
148 {
149 std::vector<std::string> peerUdids;
150 RelationShipChangeMsg msg;
151 msg.type = RelationShipChangeType::SERVICE_UNBIND;
152 msg.userId = 1;
153 msg.peerUdids = peerUdids;
154 msg.accountId = "1111111";
155 msg.accountName = "1";
156 msg.tokenId = 1;
157 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
158 msg.type = RelationShipChangeType::DEL_USER;
159 broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
160 msg.type = RelationShipChangeType::APP_UNINSTALL;
161 broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
162 msg.type = RelationShipChangeType::TYPE_MAX;
163 broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
164 ASSERT_EQ(broadCastMsg, "");
165 }
166
167 HWTEST_F(ReleationShipSyncMgrTest, ParseTrustRelationShipChange_006, testing::ext::TestSize.Level0)
168 {
169 std::string msg = R"(
170 {
171 "TYPE": 0,
172 "VALUE": [
173 { "type": 1, "userId": 1 },
174 { "type": 2, "userId": 2 }
175 ]
176 }
177 )";
178 RelationShipChangeMsg relationShipMsg =
179 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
180 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
181 msg = R"(
182 {
183 "TYPE": 1,
184 "VALUE": [
185 { "type": 1, "userId": 1 },
186 { "type": 2, "userId": 2 }
187 ]
188 }
189 )";
190 relationShipMsg = ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
191 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
192 msg = R"(
193 {
194 "TYPE": 2,
195 "VALUE": [
196 { "type": 1, "userId": 1 },
197 { "type": 2, "userId": 2 }
198 ]
199 }
200 )";
201 relationShipMsg = ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
202 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
203 }
204
205 HWTEST_F(ReleationShipSyncMgrTest, ParseTrustRelationShipChange_007, testing::ext::TestSize.Level0)
206 {
207 std::string msg = R"(
208 {
209 "TYPE": 6,
210 "VALUE": [
211 { "type": 1, "userId": 1 },
212 { "type": 2, "userId": 2 }
213 ]
214 }
215 )";
216 RelationShipChangeMsg relationShipMsg =
217 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
218 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
219 msg = R"(
220 {
221 "TYPE": 4,
222 "VALUE": [
223 { "type": 1, "userId": 1 },
224 { "type": 2, "userId": 2 }
225 ]
226 }
227 )";
228 relationShipMsg = ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
229 ASSERT_EQ(relationShipMsg.userId, 0);
230 msg = R"(
231 {
232 "TYPE": 7,
233 "VALUE": [
234 { "type": 1, "userId": 1 },
235 { "type": 2, "userId": 2 }
236 ]
237 }
238 )";
239 relationShipMsg = ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
240 ASSERT_EQ(relationShipMsg.userId, 0);
241 }
242
243 HWTEST_F(ReleationShipSyncMgrTest, IsValid_001, testing::ext::TestSize.Level0)
244 {
245 RelationShipChangeMsg msgObj;
246 msgObj.type = static_cast<RelationShipChangeType>(10);
247 bool ret = msgObj.IsValid();
248 ASSERT_EQ(ret, false);
249 }
250
251 HWTEST_F(ReleationShipSyncMgrTest, FromBroadcastPayLoad_001, testing::ext::TestSize.Level0)
252 {
253 RelationShipChangeMsg msgObj;
254 cJSON payloadJson;
255 RelationShipChangeType type = RelationShipChangeType::TYPE_MAX;
256 bool ret = msgObj.FromBroadcastPayLoad(&payloadJson, type);
257 ASSERT_EQ(ret, false);
258 type = RelationShipChangeType::SERVICE_UNBIND;
259 ret = msgObj.FromBroadcastPayLoad(&payloadJson, type);
260 ASSERT_EQ(ret, false);
261 }
262
263 HWTEST_F(ReleationShipSyncMgrTest, ToSyncFrontOrBackUserIdPayLoad_001, testing::ext::TestSize.Level0)
264 {
265 RelationShipChangeMsg msg;
266 msg.userIdInfos.push_back({1, false});
267 msg.userIdInfos.push_back({2, false});
268 msg.userIdInfos.push_back({3, false});
269 msg.userIdInfos.push_back({4, false});
270 msg.userIdInfos.push_back({5, false});
271 msg.userIdInfos.push_back({6, true});
272 uint8_t* msgPtr = nullptr;
273 uint32_t len = 0;
274 bool result = msg.ToSyncFrontOrBackUserIdPayLoad(msgPtr, len);
275 ASSERT_EQ(result, false);
276 }
277
278 HWTEST_F(ReleationShipSyncMgrTest, ToSyncFrontOrBackUserIdPayLoad_002, testing::ext::TestSize.Level0)
279 {
280 RelationShipChangeMsg msg;
281 msg.userIdInfos.push_back({1, false});
282 msg.userIdInfos.push_back({2, true});
283 uint8_t* msgPtr = nullptr;
284 uint32_t len = 10;
285 msg.syncUserIdFlag = true;
286 bool result = msg.ToSyncFrontOrBackUserIdPayLoad(msgPtr, len);
287 ASSERT_EQ(result, true);
288 }
289
290 HWTEST_F(ReleationShipSyncMgrTest, FromAccountLogoutPayLoad_001, testing::ext::TestSize.Level0)
291 {
292 RelationShipChangeMsg msg;
293 cJSON payloadJson;
294 bool result = msg.FromAccountLogoutPayLoad(&payloadJson);
295 ASSERT_EQ(result, false);
296 result = msg.FromAccountLogoutPayLoad(nullptr);
297 ASSERT_EQ(result, false);
298 }
299
300 HWTEST_F(ReleationShipSyncMgrTest, FromAccountLogoutPayLoad_002, testing::ext::TestSize.Level0)
301 {
302 RelationShipChangeMsg msg;
303 const char* jsonString = R"({
304 "MsgType": 0,
305 "userId": 12345,
306 "accountId": "a******3",
307 "tokenId": 67890,
308 "peerUdids": ["u******1", "u******2"],
309 "peerUdid": "p******d",
310 "accountName": "t******t",
311 "syncUserIdFlag": 1,
312 "userIds": [
313 {"type": 1, "userId": 111},
314 {"type": 0, "userId": 222}
315 ]
316 })";
317 cJSON *jsonObject = cJSON_Parse(jsonString);
318 bool result = msg.FromAccountLogoutPayLoad(jsonObject);
319 ASSERT_EQ(result, true);
320 }
321
322 HWTEST_F(ReleationShipSyncMgrTest, FromAccountLogoutPayLoad_003, testing::ext::TestSize.Level0)
323 {
324 RelationShipChangeMsg msg;
325 const char* jsonString = R"({
326 "MsgType": "0",
327 "userId": "12345",
328 "accountId": "a******3",
329 "tokenId": 67890,
330 "peerUdids": ["u******1", "u******2"],
331 "peerUdid": "p******d",
332 "accountName": "t******t",
333 "syncUserIdFlag": 1,
334 "userIds": [
335 {"type": 1, "userId": 111},
336 {"type": 0, "userId": 222}
337 ]
338 })";
339 cJSON *jsonObject = cJSON_Parse(jsonString);
340 bool result = msg.FromAccountLogoutPayLoad(jsonObject);
341 ASSERT_EQ(result, true);
342 }
343
344 HWTEST_F(ReleationShipSyncMgrTest, FromAccountLogoutPayLoad_004, testing::ext::TestSize.Level0)
345 {
346 RelationShipChangeMsg msg;
347 const char* jsonString = R"({
348 "MsgType": "0",
349 "userId": "12345",
350 "accountId": "a******3",
351 "tokenId": 67890,
352 "peerUdids": ["u******1", "u******2"],
353 "peerUdid": "p******d",
354 "accountName": "t******t",
355 "syncUserIdFlag": 1,
356 "test1": 1,
357 "test2": 2,
358 "test3": 3,
359 "test4": 4,
360 "userIds": [
361 {"type": 1, "userId": 111},
362 {"type": 0, "userId": 222}
363 ]
364 })";
365 cJSON *jsonObject = cJSON_Parse(jsonString);
366 bool result = msg.FromAccountLogoutPayLoad(jsonObject);
367 ASSERT_EQ(result, false);
368 }
369
370 HWTEST_F(ReleationShipSyncMgrTest, FromDeviceUnbindPayLoad_001, testing::ext::TestSize.Level0)
371 {
372 RelationShipChangeMsg msg;
373 cJSON payloadJson;
374 bool result = msg.FromDeviceUnbindPayLoad(&payloadJson);
375 ASSERT_EQ(result, false);
376 result = msg.FromDeviceUnbindPayLoad(nullptr);
377 ASSERT_EQ(result, false);
378 const char* jsonString = R"({
379 "MsgType": "0",
380 "userId": "12345",
381 "accountId": "a******3",
382 "tokenId": 67890,
383 "peerUdids": ["u******1", "u******2"],
384 "peerUdid": "p******d",
385 "accountName": "t******t",
386 "syncUserIdFlag": 1,
387 "test1": 1,
388 "test2": 2,
389 "test3": 3,
390 "test4": 4,
391 "userIds": [
392 {"type": 1, "userId": 111},
393 {"type": 0, "userId": 222}
394 ]
395 })";
396 cJSON *jsonObject = cJSON_Parse(jsonString);
397 result = msg.FromDeviceUnbindPayLoad(jsonObject);
398 ASSERT_EQ(result, false);
399 }
400
401 HWTEST_F(ReleationShipSyncMgrTest, FromDeviceUnbindPayLoad_002, testing::ext::TestSize.Level0)
402 {
403 RelationShipChangeMsg msg;
404 const char* jsonString = R"({
405 "MsgType": "0",
406 "userId": "12345",
407 "accountId": "a******3",
408 "tokenId": 67890,
409 "peerUdids": ["u******1", "u******2"],
410 "peerUdid": "p******d",
411 "accountName": "t******t",
412 "syncUserIdFlag": 1,
413 "userIds": [
414 {"type": 1, "userId": 111},
415 {"type": 0, "userId": 222}
416 ]
417 })";
418 cJSON *jsonObject = cJSON_Parse(jsonString);
419 bool result = msg.FromDeviceUnbindPayLoad(jsonObject);
420 ASSERT_EQ(result, true);
421 }
422
423 HWTEST_F(ReleationShipSyncMgrTest, FromDeviceUnbindPayLoad_003, testing::ext::TestSize.Level0)
424 {
425 RelationShipChangeMsg msg;
426 const char* jsonString = R"({
427 "MsgType": 0,
428 "userId": 12345,
429 "accountId": "a******3",
430 "tokenId": 67890,
431 "peerUdids": ["u******1", "u******2"],
432 "peerUdid": "p******d",
433 "accountName": "t******t",
434 "syncUserIdFlag": 1,
435 "userIds": [
436 {"type": 1, "userId": 111},
437 {"type": 0, "userId": 222}
438 ]
439 })";
440 cJSON *jsonObject = cJSON_Parse(jsonString);
441 bool result = msg.FromDeviceUnbindPayLoad(jsonObject);
442 ASSERT_EQ(result, true);
443 }
444
445 HWTEST_F(ReleationShipSyncMgrTest, FromAppUnbindPayLoad_001, testing::ext::TestSize.Level0)
446 {
447 RelationShipChangeMsg msg;
448 cJSON payloadJson;
449 bool result = msg.FromAppUnbindPayLoad(&payloadJson);
450 ASSERT_EQ(result, false);
451 result = msg.FromAppUnbindPayLoad(nullptr);
452 ASSERT_EQ(result, false);
453 const char* jsonString = R"({
454 "MsgType": "0",
455 "userId": "12345",
456 "accountId": "a******3",
457 "tokenId": 67890,
458 "peerUdids": ["u******1", "u******2"],
459 "peerUdid": "p******d",
460 "accountName": "t******t",
461 "syncUserIdFlag": 1,
462 "test1": 1,
463 "test2": 2,
464 "test3": 3,
465 "test4": 4,
466 "userIds": [
467 {"type": 1, "userId": 111},
468 {"type": 0, "userId": 222}
469 ]
470 })";
471 cJSON *jsonObject = cJSON_Parse(jsonString);
472 result = msg.FromAppUnbindPayLoad(jsonObject);
473 ASSERT_EQ(result, false);
474 }
475
476 HWTEST_F(ReleationShipSyncMgrTest, FromAppUnbindPayLoad_002, testing::ext::TestSize.Level0)
477 {
478 RelationShipChangeMsg msg;
479 const char* jsonString = R"({
480 "MsgType": "0",
481 "userId": "12345",
482 "accountId": "a******3",
483 "tokenId": 67890,
484 "peerUdids": ["u******1", "u******2"],
485 "peerUdid": "p******d",
486 "accountName": "t******t",
487 "syncUserIdFlag": 1,
488 "userIds": [
489 {"type": 1, "userId": 111},
490 {"type": 0, "userId": 222}
491 ]
492 })";
493 cJSON *jsonObject = cJSON_Parse(jsonString);
494 bool result = msg.FromAppUnbindPayLoad(jsonObject);
495 ASSERT_EQ(result, false);
496 }
497
498 HWTEST_F(ReleationShipSyncMgrTest, FromAppUnbindPayLoad_003, testing::ext::TestSize.Level0)
499 {
500 RelationShipChangeMsg msg;
501 const char* jsonString = R"({
502 "MsgType": 0,
503 "userId": 12345,
504 "accountId": "a******3",
505 "tokenId": 67890,
506 "peerUdids": ["u******1", "u******2"],
507 "peerUdid": "p******d",
508 "accountName": "t******t",
509 "syncUserIdFlag": 1,
510 "userIds": [
511 {"type": 1, "userId": 111},
512 {"type": 0, "userId": 222}
513 ]
514 })";
515 cJSON *jsonObject = cJSON_Parse(jsonString);
516 bool result = msg.FromAppUnbindPayLoad(jsonObject);
517 ASSERT_EQ(result, false);
518 }
519
520 HWTEST_F(ReleationShipSyncMgrTest, FromSyncFrontOrBackUserIdPayLoad_001, testing::ext::TestSize.Level0)
521 {
522 RelationShipChangeMsg msg;
523 cJSON payloadJson;
524 bool result = msg.FromSyncFrontOrBackUserIdPayLoad(&payloadJson);
525 ASSERT_EQ(result, false);
526 result = msg.FromSyncFrontOrBackUserIdPayLoad(nullptr);
527 ASSERT_EQ(result, false);
528 const char* jsonString = R"({
529 "MsgType": "0",
530 "userId": "12345",
531 "accountId": "a******3",
532 "tokenId": 67890,
533 "peerUdids": ["u******1", "u******2"],
534 "peerUdid": "p******d",
535 "accountName": "t******t",
536 "syncUserIdFlag": 1,
537 "test1": 1,
538 "test2": 2,
539 "test3": 3,
540 "test4": 4,
541 "userIds": [
542 {"type": 1, "userId": 111},
543 {"type": 0, "userId": 222}
544 ]
545 })";
546 cJSON *jsonObject = cJSON_Parse(jsonString);
547 result = msg.FromSyncFrontOrBackUserIdPayLoad(jsonObject);
548 ASSERT_EQ(result, false);
549 }
550
551 HWTEST_F(ReleationShipSyncMgrTest, FromSyncFrontOrBackUserIdPayLoad_002, testing::ext::TestSize.Level0)
552 {
553 RelationShipChangeMsg msg;
554 const char* jsonString = R"({
555 "MsgType": 6,
556 "userId": "12345",
557 "accountId": "a******3",
558 "tokenId": 67890,
559 "peerUdids": ["u******1", "u******2"],
560 "peerUdid": "p******d",
561 "accountName": "t******t",
562 "syncUserIdFlag": 1,
563 "userIds": [
564 {"type": 1, "userId": 111},
565 {"type": 0, "userId": 222}
566 ]
567 })";
568 cJSON *jsonObject = cJSON_Parse(jsonString);
569 bool result = msg.FromSyncFrontOrBackUserIdPayLoad(jsonObject);
570 ASSERT_EQ(result, false);
571 }
572
573 HWTEST_F(ReleationShipSyncMgrTest, FromSyncFrontOrBackUserIdPayLoad_003, testing::ext::TestSize.Level0)
574 {
575 RelationShipChangeMsg msg;
576 const char* jsonString = R"({
577 "MsgType": 1,
578 "userId": "12345",
579 "accountId": "a******3",
580 "tokenId": 67890,
581 "peerUdids": ["u******1", "u******2"],
582 "peerUdid": "p******d",
583 "accountName": "t******t",
584 "syncUserIdFlag": 1,
585 "userIds": [
586 {"type": 1, "userId": 111},
587 {"type": 0, "userId": 222}
588 ]
589 })";
590 cJSON *jsonObject = cJSON_Parse(jsonString);
591 bool result = msg.FromSyncFrontOrBackUserIdPayLoad(jsonObject);
592 ASSERT_EQ(result, false);
593 }
594
595 HWTEST_F(ReleationShipSyncMgrTest, FromSyncFrontOrBackUserIdPayLoad_004, testing::ext::TestSize.Level0)
596 {
597 RelationShipChangeMsg msg;
598 const char* jsonString = R"({
599 "MsgType": "1",
600 "userId": "12345",
601 "accountId": "a******3",
602 "tokenId": 67890,
603 "peerUdids": ["u******1", "u******2"],
604 "peerUdid": "p******d",
605 "accountName": "t******t",
606 "syncUserIdFlag": 1,
607 "userIds": [
608 {"type": 1, "userId": 111},
609 {"type": 0, "userId": 222}
610 ]
611 })";
612 cJSON *jsonObject = cJSON_Parse(jsonString);
613 bool result = msg.FromSyncFrontOrBackUserIdPayLoad(jsonObject);
614 ASSERT_EQ(result, true);
615 }
616
617 HWTEST_F(ReleationShipSyncMgrTest, FromSyncFrontOrBackUserIdPayLoad_005, testing::ext::TestSize.Level0)
618 {
619 RelationShipChangeMsg msg;
620 const char* jsonString = R"({
621 "MsgType": 1,
622 "userId": 12345,
623 "accountId": 3,
624 "tokenId": 67890,
625 "peerUdids": ["u******1", "u******2"],
626 "peerUdid": "p******d",
627 "accountName": "t******t",
628 "syncUserIdFlag": "1",
629 "userIds": [
630 {"type": 1, "userId": 111},
631 {"type": 0, "userId": 222}
632 ]
633 })";
634 cJSON *jsonObject = cJSON_Parse(jsonString);
635 bool result = msg.FromSyncFrontOrBackUserIdPayLoad(jsonObject);
636 ASSERT_EQ(result, true);
637 }
638
639 HWTEST_F(ReleationShipSyncMgrTest, FromDelUserPayLoad_001, testing::ext::TestSize.Level0)
640 {
641 RelationShipChangeMsg msg;
642 cJSON payloadJson;
643 bool result = msg.FromDelUserPayLoad(&payloadJson);
644 ASSERT_EQ(result, false);
645 result = msg.FromDelUserPayLoad(nullptr);
646 ASSERT_EQ(result, false);
647 const char* jsonString = R"({
648 "MsgType": 0,
649 "userId": "12345",
650 "accountId": "a******3",
651 "tokenId": 67890
652 })";
653 cJSON *jsonObject = cJSON_Parse(jsonString);
654 result = msg.FromDelUserPayLoad(jsonObject);
655 ASSERT_EQ(result, true);
656 }
657
658 HWTEST_F(ReleationShipSyncMgrTest, ToString_ValidData_001, testing::ext::TestSize.Level0)
659 {
660 RelationShipChangeMsg msg;
661 msg.type = RelationShipChangeType::APP_UNBIND;
662 msg.userId = 12345;
663 msg.accountId = "account_123";
664 msg.tokenId = 67890;
665 msg.peerUdids = {"udid1", "udid2"};
666 msg.peerUdid = "peer_udid";
667 msg.accountName = "test_account";
668 msg.syncUserIdFlag = true;
669
670 std::string expected = "{ MsgType: " + std::to_string(static_cast<uint32_t>(msg.type)) +
671 ", userId: 12345, accountId: a******3, tokenId: 67890, " +
672 "peerUdids: [ u******1, u******2 ], peerUdid: p******d, " +
673 "accountName: t******t, syncUserIdFlag: 1, userIds: [ ] }";
674 EXPECT_EQ(msg.ToString(), expected);
675 }
676
677 HWTEST_F(ReleationShipSyncMgrTest, ParseTrustRelationShipChange_001, testing::ext::TestSize.Level0)
678 {
679 std::string msg = "";
680 RelationShipChangeMsg relationShipMsg =
681 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
682 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
683 }
684
685 HWTEST_F(ReleationShipSyncMgrTest, ParseTrustRelationShipChange_002, testing::ext::TestSize.Level0)
686 {
687 std::string msg = R"(
688 {
689 "xx": "xx"
690 }
691 )";
692 RelationShipChangeMsg relationShipMsg =
693 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
694 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
695 }
696
697 HWTEST_F(ReleationShipSyncMgrTest, ParseTrustRelationShipChange_003, testing::ext::TestSize.Level0)
698 {
699 std::string msg = R"(
700 {
701 "TYPE": "xx"
702 }
703 )";
704 RelationShipChangeMsg relationShipMsg =
705 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
706 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
707 }
708
709 HWTEST_F(ReleationShipSyncMgrTest, ParseTrustRelationShipChange_004, testing::ext::TestSize.Level0)
710 {
711 std::string msg = R"(
712 {
713 "TYPE": 0
714 }
715 )";
716 RelationShipChangeMsg relationShipMsg =
717 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
718 msg = R"(
719 {
720 "TYPE": 1
721 }
722 )";
723 relationShipMsg =
724 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
725 msg = R"(
726 {
727 "TYPE": 2
728 }
729 )";
730 relationShipMsg =
731 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
732 msg = R"(
733 {
734 "TYPE": 3
735 }
736 )";
737 relationShipMsg =
738 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
739 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
740 }
741
742 HWTEST_F(ReleationShipSyncMgrTest, ParseTrustRelationShipChange_005, testing::ext::TestSize.Level0)
743 {
744 std::string msg = R"(
745 {
746 "TYPE": 0,
747 "VALUE": ""
748 }
749 )";
750 RelationShipChangeMsg relationShipMsg =
751 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
752 ASSERT_EQ(relationShipMsg.userId, UINT32_MAX);
753 }
754
755 HWTEST_F(ReleationShipSyncMgrTest, ToSyncFrontOrBackUserIdPayLoad_ValidInput, testing::ext::TestSize.Level0)
756 {
757 RelationShipChangeMsg msg;
758 msg.userIdInfos.push_back({12345, true});
759 msg.userIdInfos.push_back({67890, false});
760
761 uint8_t* msgPtr = nullptr;
762 uint32_t len = 0;
763
764 bool result = msg.ToSyncFrontOrBackUserIdPayLoad(msgPtr, len);
765
766 ASSERT_TRUE(result);
767 ASSERT_EQ(len, 5);
768 ASSERT_EQ(msgPtr[0], 2);
769
770 delete[] msgPtr;
771 msg.userIdInfos.clear();
772 }
773
774 HWTEST_F(ReleationShipSyncMgrTest, ToDelUserPayLoad_ValidInput, testing::ext::TestSize.Level0)
775 {
776 RelationShipChangeMsg msg;
777 msg.userId = 12345;
778
779 uint8_t* msgPtr = nullptr;
780 uint32_t len = 0;
781
782 msg.ToDelUserPayLoad(msgPtr, len);
783
784 ASSERT_EQ(len, 2);
785 ASSERT_EQ(msgPtr[0], 0x39);
786
787 delete[] msgPtr;
788 msg.userIdInfos.clear();
789 }
790
791 HWTEST_F(ReleationShipSyncMgrTest, ToSyncFrontOrBackUserIdPayLoad_TooManyUserIds, testing::ext::TestSize.Level0)
792 {
793 RelationShipChangeMsg msg;
794 for (int i = 0; i < MAX_USER_ID_NUM + 1; ++i) {
795 msg.userIdInfos.push_back({i, true});
796 }
797
798 uint8_t* msgPtr = nullptr;
799 uint32_t len = 0;
800
801 bool result = msg.ToSyncFrontOrBackUserIdPayLoad(msgPtr, len);
802
803 ASSERT_FALSE(result);
804 ASSERT_EQ(msgPtr, nullptr);
805 msg.userIdInfos.clear();
806 }
807
808 HWTEST_F(ReleationShipSyncMgrTest, ToSyncFrontOrBackUserIdPayLoad_LengthExceedsLimit, testing::ext::TestSize.Level0)
809 {
810 RelationShipChangeMsg msg;
811 msg.userIdInfos.push_back({12345, true});
812 msg.userIdInfos.push_back({67890, false});
813 uint8_t* msgPtr = nullptr;
814 uint32_t len = 0;
815 bool result = msg.ToSyncFrontOrBackUserIdPayLoad(msgPtr, len);
816 ASSERT_TRUE(result);
817 }
818
819 HWTEST_F(ReleationShipSyncMgrTest, ToString_ValidData, testing::ext::TestSize.Level0)
820 {
821 RelationShipChangeMsg msg;
822 msg.type = RelationShipChangeType::APP_UNBIND;
823 msg.userId = 12345;
824 msg.accountId = "account_123";
825 msg.tokenId = 67890;
826 msg.peerUdids = {"udid1", "udid2"};
827 msg.peerUdid = "peer_udid";
828 msg.accountName = "test_account";
829 msg.syncUserIdFlag = true;
830 msg.userIdInfos = {{true, 111}, {false, 222}};
831
832 std::string expected = "{ MsgType: " + std::to_string(static_cast<uint32_t>(msg.type)) +
833 ", userId: 12345, accountId: a******3, tokenId: 67890, " +
834 "peerUdids: [ u******1, u******2 ], peerUdid: p******d, " +
835 "accountName: t******t, syncUserIdFlag: 1, " +
836 "userIds: [ { 1, userId: 111 }, { 0, userId: 222 } ] }";
837
838 EXPECT_EQ(msg.ToString(), expected);
839 }
840
841 HWTEST_F(ReleationShipSyncMgrTest, ToString_ValidData02, testing::ext::TestSize.Level0)
842 {
843 bool isForeground = true;
844 std::uint16_t userId = 123;
845 UserIdInfo userIdInfo(isForeground, userId);
846 std::string expected = "{ 1, userId: 123 }";
847 EXPECT_EQ(userIdInfo.ToString(), expected);
848 }
849
850 HWTEST_F(ReleationShipSyncMgrTest, ToString_ZeroUserId, testing::ext::TestSize.Level0)
851 {
852 bool isForeground = false;
853 std::uint16_t userId = 0;
854 UserIdInfo userIdInfo(isForeground, userId);
855
856 std::string expected = "{ 0, userId: 0 }";
857 EXPECT_EQ(userIdInfo.ToString(), expected);
858 }
859
860 HWTEST_F(ReleationShipSyncMgrTest, ValidList, testing::ext::TestSize.Level0)
861 {
862 std::vector<UserIdInfo> list = {{true, 1}, {false, 2}};
863 std::string expected = "[ { 1, userId: 1 }, { 0, userId: 2 } ]";
864
865 EXPECT_EQ(GetUserIdInfoList(list), expected);
866 }
867
868 HWTEST_F(ReleationShipSyncMgrTest, EmptyList, testing::ext::TestSize.Level0)
869 {
870 std::vector<UserIdInfo> list;
871 std::string expected = "[ ]";
872
873 EXPECT_EQ(GetUserIdInfoList(list), expected);
874 }
875
876 HWTEST_F(ReleationShipSyncMgrTest, ValidData, testing::ext::TestSize.Level0)
877 {
878 std::vector<UserIdInfo> remoteUserIdInfos = {{true, 1}, {false, 2}, {true, 3}};
879 std::vector<UserIdInfo> foregroundUserIdInfos;
880 std::vector<UserIdInfo> backgroundUserIdInfos;
881
882 GetFrontAndBackUserIdInfos(remoteUserIdInfos, foregroundUserIdInfos, backgroundUserIdInfos);
883
884 EXPECT_EQ(foregroundUserIdInfos.size(), 2);
885 EXPECT_EQ(backgroundUserIdInfos.size(), 1);
886 EXPECT_EQ(foregroundUserIdInfos[0].userId, 1);
887 EXPECT_EQ(foregroundUserIdInfos[1].userId, 3);
888 EXPECT_EQ(backgroundUserIdInfos[0].userId, 2);
889 }
890
891 HWTEST_F(ReleationShipSyncMgrTest, EmptyRemoteList, testing::ext::TestSize.Level0)
892 {
893 std::vector<UserIdInfo> remoteUserIdInfos;
894 std::vector<UserIdInfo> foregroundUserIdInfos;
895 std::vector<UserIdInfo> backgroundUserIdInfos;
896
897 GetFrontAndBackUserIdInfos(remoteUserIdInfos, foregroundUserIdInfos, backgroundUserIdInfos);
898
899 EXPECT_TRUE(foregroundUserIdInfos.empty());
900 EXPECT_TRUE(backgroundUserIdInfos.empty());
901 }
902
903 HWTEST_F(ReleationShipSyncMgrTest, FromBroadcastPayLoad_010, testing::ext::TestSize.Level0)
904 {
905 RelationShipChangeMsg msg;
906 cJSON *payloadJson = NULL;
907 RelationShipChangeType type = RelationShipChangeType::TYPE_MAX;
908 bool ret = msg.FromBroadcastPayLoad(payloadJson, type);
909 EXPECT_FALSE(ret);
910
911 type = RelationShipChangeType::ACCOUNT_LOGOUT;
912 ret = msg.FromBroadcastPayLoad(payloadJson, type);
913 EXPECT_FALSE(ret);
914
915 type = RelationShipChangeType::DEVICE_UNBIND;
916 ret = msg.FromBroadcastPayLoad(payloadJson, type);
917 EXPECT_FALSE(ret);
918
919 type = RelationShipChangeType::APP_UNBIND;
920 ret = msg.FromBroadcastPayLoad(payloadJson, type);
921 EXPECT_FALSE(ret);
922
923 type = RelationShipChangeType::SYNC_USERID;
924 ret = msg.FromBroadcastPayLoad(payloadJson, type);
925 EXPECT_FALSE(ret);
926
927 type = RelationShipChangeType::DEL_USER;
928 ret = msg.FromBroadcastPayLoad(payloadJson, type);
929 EXPECT_FALSE(ret);
930
931 type = RelationShipChangeType::APP_UNINSTALL;
932 ret = msg.FromBroadcastPayLoad(payloadJson, type);
933 EXPECT_FALSE(ret);
934 }
935
936 HWTEST_F(ReleationShipSyncMgrTest, IsChangeTypeValid_001, testing::ext::TestSize.Level0)
937 {
938 RelationShipChangeMsg msg;
939 msg.type = RelationShipChangeType::ACCOUNT_LOGOUT;
940 bool ret = msg.IsChangeTypeValid();
941 EXPECT_TRUE(ret);
942
943 msg.type = RelationShipChangeType::DEVICE_UNBIND;
944 ret = msg.IsChangeTypeValid();
945 EXPECT_TRUE(ret);
946
947 msg.type = RelationShipChangeType::APP_UNBIND;
948 ret = msg.IsChangeTypeValid();
949 EXPECT_TRUE(ret);
950
951 msg.type = RelationShipChangeType::SYNC_USERID;
952 ret = msg.IsChangeTypeValid();
953 EXPECT_TRUE(ret);
954
955 msg.type = RelationShipChangeType::DEL_USER;
956 ret = msg.IsChangeTypeValid();
957 EXPECT_TRUE(ret);
958 }
959
960 HWTEST_F(ReleationShipSyncMgrTest, IsValid_010, testing::ext::TestSize.Level0)
961 {
962 RelationShipChangeMsg msg;
963 msg.type = RelationShipChangeType::SYNC_USERID;
964 msg.userIdInfos.push_back({1, false});
965 msg.userIdInfos.push_back({2, true});
966 bool ret = msg.IsValid();
967 EXPECT_TRUE(ret);
968
969 msg.type = static_cast<RelationShipChangeType>(9);
970 ret = msg.IsValid();
971 EXPECT_FALSE(ret);
972 }
973 }
974 } // namespace DistributedHardware
975 } // namespace OHOS
976