1 /*
2 * Copyright (c) 2022 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 #include "avsession_errors.h"
18 #include "avsession_log.h"
19 #include "json_utils.h"
20
21 using namespace testing::ext;
22 using namespace OHOS::AVSession;
23
24 class JsonUtilsTest : public testing::Test {
25 public:
26 static void SetUpTestCase(void);
27 static void TearDownTestCase(void);
28 void SetUp();
29 void TearDown();
30 };
31
SetUpTestCase()32 void JsonUtilsTest::SetUpTestCase()
33 {}
34
TearDownTestCase()35 void JsonUtilsTest::TearDownTestCase()
36 {}
37
SetUp()38 void JsonUtilsTest::SetUp()
39 {}
40
TearDown()41 void JsonUtilsTest::TearDown()
42 {}
43
44 /**
45 * @tc.name: IsInt32_001
46 * @tc.desc: test IsInt32
47 * @tc.type: FUNC
48 * @tc.require: #I62OZV
49 */
50 static HWTEST(JsonUtilsTest, IsInt32_001, TestSize.Level0)
51 {
52 SLOGI("IsInt32_001 begin!");
53
54 cJSON* jsonTest = cJSON_CreateObject();
55 if (jsonTest == nullptr) {
56 SLOGE("create jsontest fail");
57 FAIL();
58 }
59 cJSON_AddNumberToObject(jsonTest, "test", 1);
60 bool ret = JsonUtils::IsInt32(jsonTest, "test");
61 EXPECT_EQ(ret, true);
62
63 cJSON_Delete(jsonTest);
64 SLOGI("IsInt32_001 end!");
65 }
66
67 /**
68 * @tc.name: IsInt32_002
69 * @tc.desc: test IsInt32
70 * @tc.type: FUNC
71 * @tc.require: #I62OZV
72 */
73 static HWTEST(JsonUtilsTest, IsInt32_002, TestSize.Level0)
74 {
75 SLOGI("IsInt32_002 begin!");
76
77 cJSON* jsonTest = cJSON_CreateObject();
78 if (jsonTest == nullptr) {
79 SLOGE("create jsontest fail");
80 FAIL();
81 }
82 cJSON_AddStringToObject(jsonTest, "test", "1");
83 bool ret = JsonUtils::IsInt32(jsonTest, "test");
84 EXPECT_EQ(ret, false);
85
86 cJSON_Delete(jsonTest);
87 SLOGI("IsInt32_002 end!");
88 }
89
90 /**
91 * @tc.name: IsString_001
92 * @tc.desc: test IsString
93 * @tc.type: FUNC
94 * @tc.require: #I62OZV
95 */
96 static HWTEST(JsonUtilsTest, IsString_001, TestSize.Level0)
97 {
98 SLOGI("IsString_001 begin!");
99
100 cJSON* jsonTest = cJSON_CreateObject();
101 if (jsonTest == nullptr) {
102 SLOGE("create jsontest fail");
103 FAIL();
104 }
105 cJSON_AddStringToObject(jsonTest, "test", "123");
106 bool ret = JsonUtils::IsString(jsonTest, "test");
107 EXPECT_EQ(ret, true);
108
109 cJSON_Delete(jsonTest);
110 SLOGI("IsString_001 end!");
111 }
112
113 /**
114 * @tc.name: IsString_002
115 * @tc.desc: test IsString
116 * @tc.type: FUNC
117 * @tc.require: #I62OZV
118 */
119 static HWTEST(JsonUtilsTest, IsString_002, TestSize.Level0)
120 {
121 SLOGI("IsString_002 begin!");
122
123 cJSON* jsonTest = cJSON_CreateObject();
124 if (jsonTest == nullptr) {
125 SLOGE("create jsontest fail");
126 FAIL();
127 }
128 cJSON_AddNumberToObject(jsonTest, "test", 1);
129 bool ret = JsonUtils::IsString(jsonTest, "test");
130 EXPECT_EQ(ret, false);
131
132 cJSON_Delete(jsonTest);
133 SLOGI("IsString_002 end!");
134 }
135
136 /**
137 * @tc.name: IsBool_001
138 * @tc.desc: test IsBool
139 * @tc.type: FUNC
140 * @tc.require: #I62OZV
141 */
142 static HWTEST(JsonUtilsTest, IsBool_001, TestSize.Level0)
143 {
144 SLOGI("IsBool_001 begin!");
145
146 cJSON* jsonTest = cJSON_CreateObject();
147 if (jsonTest == nullptr) {
148 SLOGE("create jsontest fail");
149 FAIL();
150 }
151 cJSON_AddBoolToObject(jsonTest, "test", true);
152 bool ret = JsonUtils::IsBool(jsonTest, "test");
153 EXPECT_EQ(ret, true);
154
155 cJSON_Delete(jsonTest);
156 SLOGI("IsBool_001 end!");
157 }
158
159 /**
160 * @tc.name: IsBool_002
161 * @tc.desc: test IsBool
162 * @tc.type: FUNC
163 * @tc.require: #I62OZV
164 */
165 static HWTEST(JsonUtilsTest, IsBool_002, TestSize.Level0)
166 {
167 SLOGI("IsBool_002 begin!");
168
169 cJSON* jsonTest = cJSON_CreateObject();
170 if (jsonTest == nullptr) {
171 SLOGE("create jsontest fail");
172 FAIL();
173 }
174 cJSON_AddStringToObject(jsonTest, "test", "123");
175 bool ret = JsonUtils::IsBool(jsonTest, "test");
176 EXPECT_EQ(ret, false);
177
178 cJSON_Delete(jsonTest);
179 SLOGI("IsBool_002 end!");
180 }
181
182 /**
183 * @tc.name: SetSessionBasicInfo001
184 * @tc.desc: test SetSessionBasicInfo
185 * @tc.type: FUNC
186 * @tc.require: #I62OZV
187 */
188 static HWTEST(JsonUtilsTest, SetSessionBasicInfo001, TestSize.Level0)
189 {
190 SLOGI("SetSessionBasicInfo001 begin!");
191 std::string jsonStr = R"(
192 {"test":1}
193 )";
194 AVSessionBasicInfo basicInfo;
195 int32_t ret = JsonUtils::SetSessionBasicInfo(jsonStr, basicInfo);
196 EXPECT_EQ(ret, AVSESSION_SUCCESS);
197 SLOGI("SetSessionBasicInfo001 end!");
198 }
199
200 /**
201 * @tc.name: SetSessionDescriptors001
202 * @tc.desc: test SetSessionDescriptors
203 * @tc.type: FUNC
204 * @tc.require: #I62OZV
205 */
206 static HWTEST(JsonUtilsTest, SetSessionDescriptors001, TestSize.Level0)
207 {
208 SLOGI("SetSessionDescriptors001 begin!");
209 std::string jsonStr = "";
210 std::vector<AVSessionDescriptor> descriptors;
211 int32_t ret = JsonUtils::SetSessionDescriptors(jsonStr, descriptors);
212 EXPECT_EQ(ret, AVSESSION_SUCCESS);
213 SLOGI("SetSessionDescriptors001 end!");
214 }
215
216 /**
217 * @tc.name: SetSessionDescriptors002
218 * @tc.desc: test SetSessionDescriptors
219 * @tc.type: FUNC
220 * @tc.require: #I62OZV
221 */
222 static HWTEST(JsonUtilsTest, SetSessionDescriptors002, TestSize.Level0)
223 {
224 SLOGI("SetSessionDescriptors002 begin!");
225 std::string jsonStr = R"(
226 {"test":1}
227 )";
228 std::vector<AVSessionDescriptor> descriptors;
229 int32_t ret = JsonUtils::SetSessionDescriptors(jsonStr, descriptors);
230 EXPECT_EQ(ret, AVSESSION_SUCCESS);
231 SLOGI("SetSessionDescriptors002 end!");
232 }
233
234 /**
235 * @tc.name: SetSessionDescriptors003
236 * @tc.desc: test SetSessionDescriptors
237 * @tc.type: FUNC
238 * @tc.require: #I62OZV
239 */
240 static HWTEST(JsonUtilsTest, SetSessionDescriptors003, TestSize.Level0)
241 {
242 SLOGI("SetSessionDescriptors003 begin!");
243 std::string jsonStr = R"(
244 {(])}
245 )";
246 std::vector<AVSessionDescriptor> descriptors;
247 int32_t ret = JsonUtils::SetSessionDescriptors(jsonStr, descriptors);
248 EXPECT_EQ(ret, AVSESSION_ERROR);
249 SLOGI("SetSessionDescriptors003 end!");
250 }
251
252 /**
253 * @tc.name: SetSessionDescriptor001
254 * @tc.desc: test SetSessionDescriptor
255 * @tc.type: FUNC
256 * @tc.require: #I62OZV
257 */
258 static HWTEST(JsonUtilsTest, SetSessionDescriptor001, TestSize.Level0)
259 {
260 SLOGI("SetSessionDescriptor001 begin!");
261 std::string jsonStr = "";
262 AVSessionDescriptor descriptor;
263 int32_t ret = JsonUtils::SetSessionDescriptor(jsonStr, descriptor);
264 EXPECT_EQ(ret, AVSESSION_SUCCESS);
265 SLOGI("SetSessionDescriptor001 end!");
266 }
267
268 /**
269 * @tc.name: SetSessionDescriptor002
270 * @tc.desc: test SetSessionDescriptor
271 * @tc.type: FUNC
272 * @tc.require: #I62OZV
273 */
274 static HWTEST(JsonUtilsTest, SetSessionDescriptor002, TestSize.Level0)
275 {
276 SLOGI("SetSessionDescriptor002 begin!");
277 std::string jsonStr = R"(
278 {(])}
279 )";
280 AVSessionDescriptor descriptor;
281 int32_t ret = JsonUtils::SetSessionDescriptor(jsonStr, descriptor);
282 EXPECT_EQ(ret, AVSESSION_ERROR);
283 SLOGI("SetSessionDescriptor002 end!");
284 }
285
286 /**
287 * @tc.name: JsonToVector001
288 * @tc.desc: test GetVectorCapability
289 * @tc.type: FUNC
290 * @tc.require: #I62OZV
291 */
292 static HWTEST(JsonUtilsTest, JsonToVector001, TestSize.Level0)
293 {
294 SLOGI("JsonToVector001 begin!");
295 cJSON* object = nullptr;
296 std::vector<int32_t> value;
297 int32_t ret = JsonUtils::JsonToVector(object, value);
298 EXPECT_EQ(ret, AVSESSION_ERROR);
299 SLOGI("JsonToVector001 end!");
300 }
301
302 /**
303 * @tc.name: JsonToVector002
304 * @tc.desc: test GetVectorCapability
305 * @tc.type: FUNC
306 * @tc.require: #I62OZV
307 */
308 static HWTEST(JsonUtilsTest, JsonToVector002, TestSize.Level0)
309 {
310 SLOGI("JsonToVector002 begin!");
311 cJSON* object = cJSON_CreateObject();
312 if (object == nullptr) {
313 SLOGE("create object fail");
314 FAIL();
315 }
316 cJSON_AddStringToObject(object, "test", "123");
317 std::vector<int32_t> value;
318 int32_t ret = JsonUtils::JsonToVector(object, value);
319 EXPECT_EQ(ret, AVSESSION_ERROR);
320 cJSON_Delete(object);
321 SLOGI("JsonToVector002 end!");
322 }
323
324 /**
325 * @tc.name: JsonToVector003
326 * @tc.desc: test GetVectorCapability
327 * @tc.type: FUNC
328 * @tc.require: #I62OZV
329 */
330 static HWTEST(JsonUtilsTest, JsonToVector003, TestSize.Level0)
331 {
332 SLOGI("JsonToVector003 begin!");
333 cJSON* array = cJSON_CreateArray();
334 if (array == nullptr) {
335 SLOGE("create array fail");
336 FAIL();
337 }
338 cJSON* object = cJSON_CreateNumber(123);
339 cJSON_AddItemToArray(array, object);
340 std::vector<int32_t> value;
341 int32_t ret = JsonUtils::JsonToVector(array, value);
342 EXPECT_EQ(ret, AVSESSION_SUCCESS);
343 cJSON_Delete(array);
344 SLOGI("JsonToVector003 end!");
345 }
346
347 /**
348 * @tc.name: GetVectorCapability001
349 * @tc.desc: test GetVectorCapability
350 * @tc.type: FUNC
351 * @tc.require: #I62OZV
352 */
353 static HWTEST(JsonUtilsTest, GetVectorCapability001, TestSize.Level0)
354 {
355 SLOGI("GetVectorCapability001 begin!");
356 std::string sinkCapability = "";
357 std::vector<std::vector<int32_t>> value;
358 int32_t ret = JsonUtils::GetVectorCapability(sinkCapability, value);
359 EXPECT_EQ(ret, AVSESSION_ERROR);
360 SLOGI("GetVectorCapability001 end!");
361 }
362
363
364 /**
365 * @tc.name: GetVectorCapability002
366 * @tc.desc: test GetVectorCapability
367 * @tc.type: FUNC
368 * @tc.require: #I62OZV
369 */
370 static HWTEST(JsonUtilsTest, GetVectorCapability002, TestSize.Level0)
371 {
372 SLOGI("GetVectorCapability002 begin!");
373 std::string sinkCapability = R"(
374 {(])}
375 )";
376 std::vector<std::vector<int32_t>> value;
377 int32_t ret = JsonUtils::GetVectorCapability(sinkCapability, value);
378 EXPECT_EQ(ret, AVSESSION_ERROR);
379 SLOGI("GetVectorCapability002 end!");
380 }
381
382 /**
383 * @tc.name: GetVectorCapability003
384 * @tc.desc: test GetVectorCapability
385 * @tc.type: FUNC
386 * @tc.require: #I62OZV
387 */
388 static HWTEST(JsonUtilsTest, GetVectorCapability003, TestSize.Level0)
389 {
390 SLOGI("GetVectorCapability003 begin!");
391 std::string sinkCapability = "{ \"key\": \"value\", }";
392 std::vector<std::vector<int32_t>> value;
393 int32_t ret = JsonUtils::GetVectorCapability(sinkCapability, value);
394 EXPECT_EQ(ret, AVSESSION_ERROR);
395 SLOGI("GetVectorCapability003 end!");
396 }
397
398 /**
399 * @tc.name: GetVectorCapability004
400 * @tc.desc: test GetVectorCapability
401 * @tc.type: FUNC
402 * @tc.require: #I62OZV
403 */
404 static HWTEST(JsonUtilsTest, GetVectorCapability004, TestSize.Level0)
405 {
406 SLOGI("GetVectorCapability004 begin!");
407 std::string sinkCapability = R"({
408 "metaData": [1, 2, 3],
409 "playbackState": [1, 2, 3],
410 "controlCommand": [1, 2, 3]
411 })";
412 std::vector<std::vector<int32_t>> value(3);
413 int32_t ret = JsonUtils::GetVectorCapability(sinkCapability, value);
414 EXPECT_EQ(ret, AVSESSION_SUCCESS);
415 SLOGI("GetVectorCapability004 end!");
416 }
417
418 /**
419 * @tc.name: IsInt32_003
420 * @tc.desc: test IsInt32
421 * @tc.type: FUNC
422 * @tc.require: #I62OZV
423 */
424 static HWTEST(JsonUtilsTest, IsInt32_003, TestSize.Level0)
425 {
426 SLOGI("IsInt32_003 begin!");
427
428 cJSON* jsonTest = cJSON_CreateObject();
429 if (jsonTest == nullptr) {
430 SLOGE("create jsontest fail");
431 FAIL();
432 }
433 cJSON_AddStringToObject(jsonTest, "TEST", "1");
434 bool ret = JsonUtils::IsInt32(jsonTest, "test");
435 EXPECT_EQ(ret, false);
436
437 cJSON_Delete(jsonTest);
438 SLOGI("IsInt32_003 end!");
439 }
440
441 /**
442 * @tc.name: IsInt32_004
443 * @tc.desc: test IsInt32
444 * @tc.type: FUNC
445 * @tc.require: #I62OZV
446 */
447 static HWTEST(JsonUtilsTest, IsInt32_004, TestSize.Level0)
448 {
449 SLOGI("IsInt32_004 begin!");
450
451 cJSON* jsonTest = cJSON_CreateObject();
452 if (jsonTest == nullptr) {
453 SLOGE("create jsontest fail");
454 FAIL();
455 }
456 cJSON_AddStringToObject(jsonTest, "test1", "1");
457 bool ret = JsonUtils::IsInt32(jsonTest, "test2");
458 EXPECT_EQ(ret, false);
459
460 cJSON_Delete(jsonTest);
461 SLOGI("IsInt32_004 end!");
462 }
463
464 /**
465 * @tc.name: IsString_003
466 * @tc.desc: test IsString
467 * @tc.type: FUNC
468 * @tc.require: #I62OZV
469 */
470 static HWTEST(JsonUtilsTest, IsString_003, TestSize.Level0)
471 {
472 SLOGI("IsString_003 begin!");
473
474 cJSON* jsonTest = cJSON_CreateObject();
475 if (jsonTest == nullptr) {
476 SLOGE("create jsontest fail");
477 FAIL();
478 }
479 cJSON_AddStringToObject(jsonTest, "test1", "123");
480 bool ret = JsonUtils::IsString(jsonTest, "test2");
481 EXPECT_EQ(ret, false);
482
483 cJSON_Delete(jsonTest);
484 SLOGI("IsString_003 end!");
485 }
486
487 /**
488 * @tc.name: IsBool_003
489 * @tc.desc: test IsString
490 * @tc.type: FUNC
491 * @tc.require: #I62OZV
492 */
493 static HWTEST(JsonUtilsTest, IsBool_003, TestSize.Level0)
494 {
495 SLOGI("IsBool_003 begin!");
496
497 cJSON* jsonTest = cJSON_CreateObject();
498 if (jsonTest == nullptr) {
499 SLOGE("create jsontest fail");
500 FAIL();
501 }
502 cJSON_AddBoolToObject(jsonTest, "test1", false);
503 bool ret = JsonUtils::IsString(jsonTest, "test2");
504 EXPECT_EQ(ret, false);
505
506 cJSON_Delete(jsonTest);
507 SLOGI("IsBool_003 end!");
508 }
509
510 /**
511 * @tc.name: GetAllCapability001
512 * @tc.desc: test GetAllCapability
513 * @tc.type: FUNC
514 * @tc.require: #I62OZV
515 */
516 static HWTEST(JsonUtilsTest, GetAllCapability001, TestSize.Level0)
517 {
518 SLOGI("GetAllCapability001 begin!");
519 std::string sessionInfoStr = R"({
520 "compatibility": {
521 "capabilitySet": {
522 "key": "value"
523 }
524 }
525 })";
526 std::string outStr;
527 int32_t ret = JsonUtils::GetAllCapability(sessionInfoStr, outStr);
528 EXPECT_EQ(ret, AVSESSION_SUCCESS);
529 SLOGI("GetAllCapability001 end!");
530 }
531
532 /**
533 * @tc.name: GetAllCapability002
534 * @tc.desc: test GetAllCapability
535 * @tc.type: FUNC
536 * @tc.require: #I62OZV
537 */
538 static HWTEST(JsonUtilsTest, GetAllCapability002, TestSize.Level0)
539 {
540 SLOGI("GetAllCapability002 begin!");
541 std::string sessionInfoStr = R"({
542 "compatibility": {
543 "capabilitySet": {
544 "key": "value"
545 }
546 }{}
547 })";
548 std::string outStr;
549 int32_t ret = JsonUtils::GetAllCapability(sessionInfoStr, outStr);
550 EXPECT_EQ(ret, AVSESSION_ERROR);
551 SLOGI("GetAllCapability002 end!");
552 }
553
554 /**
555 * @tc.name: GetAllCapability003
556 * @tc.desc: test GetAllCapability
557 * @tc.type: FUNC
558 * @tc.require: #I62OZV
559 */
560 static HWTEST(JsonUtilsTest, GetAllCapability003, TestSize.Level0)
561 {
562 SLOGI("GetAllCapability003 begin!");
563 std::string sessionInfoStr = R"({
564 "compatibility1": {
565 "capabilitySet": {
566 "key": "value"
567 }
568 }
569 })";
570 std::string outStr;
571 int32_t ret = JsonUtils::GetAllCapability(sessionInfoStr, outStr);
572 EXPECT_EQ(ret, AVSESSION_ERROR);
573 SLOGI("GetAllCapability003 end!");
574 }
575
576 /**
577 * @tc.name: GetAllCapability004
578 * @tc.desc: test GetAllCapability
579 * @tc.type: FUNC
580 * @tc.require: #I62OZV
581 */
582 static HWTEST(JsonUtilsTest, GetAllCapability004, TestSize.Level0)
583 {
584 SLOGI("GetAllCapability004 begin!");
585 std::string sessionInfoStr = R"({
586 "compatibility": {
587 "capabilitySet1": {
588 "key": "value"
589 }
590 }
591 })";
592 std::string outStr;
593 int32_t ret = JsonUtils::GetAllCapability(sessionInfoStr, outStr);
594 EXPECT_EQ(ret, AVSESSION_ERROR);
595 SLOGI("GetAllCapability004 end!");
596 }
597
598 /**
599 * @tc.name: GetAllCapability005
600 * @tc.desc: test GetAllCapability
601 * @tc.type: FUNC
602 * @tc.require: #I62OZV
603 */
604 static HWTEST(JsonUtilsTest, GetAllCapability005, TestSize.Level0)
605 {
606 SLOGI("GetAllCapability005 begin!");
607 std::string sessionInfoStr = R"({
608 "compatibility": "invalid_string"
609 })";
610 std::string outStr;
611 int32_t ret = JsonUtils::GetAllCapability(sessionInfoStr, outStr);
612 EXPECT_EQ(ret, AVSESSION_ERROR);
613 SLOGI("GetAllCapability005 end!");
614 }
615
616 /**
617 * @tc.name: GetAllCapability006
618 * @tc.desc: test GetAllCapability
619 * @tc.type: FUNC
620 * @tc.require: #I62OZV
621 */
622 static HWTEST(JsonUtilsTest, GetAllCapability006, TestSize.Level0)
623 {
624 SLOGI("GetAllCapability006 begin!");
625 std::string sessionInfoStr = R"({
626 "compatibility": {
627 "capabilitySet": null
628 }
629 })";
630 std::string outStr;
631 int32_t ret = JsonUtils::GetAllCapability(sessionInfoStr, outStr);
632 EXPECT_EQ(ret, AVSESSION_ERROR);
633 SLOGI("GetAllCapability006 end!");
634 }
635
636 /**
637 * @tc.name: GetAllCapability007
638 * @tc.desc: test GetAllCapability
639 * @tc.type: FUNC
640 * @tc.require: #I62OZV
641 */
642 static HWTEST(JsonUtilsTest, GetAllCapability007, TestSize.Level0)
643 {
644 SLOGI("GetAllCapability007 begin!");
645 std::string sessionInfoStr = R"({
646 "compatibility": {
647 "capabilitySet": {}
648 }
649 })";
650 std::string outStr;
651 int32_t ret = JsonUtils::GetAllCapability(sessionInfoStr, outStr);
652 EXPECT_EQ(ret, AVSESSION_SUCCESS);
653 SLOGI("GetAllCapability007 end!");
654 }
655
656 /**
657 * @tc.name: SetSessionCapabilitySet001
658 * @tc.desc: test SetSessionCapabilitySet
659 * @tc.type: FUNC
660 * @tc.require: #I62OZV
661 */
662 static HWTEST(JsonUtilsTest, SetSessionCapabilitySet001, TestSize.Level0)
663 {
664 SLOGI("SetSessionCapabilitySet001 begin!");
665 AVSessionBasicInfo basicInfo;
666 basicInfo.metaDataCap_ = {1, 2, 3};
667 basicInfo.playBackStateCap_ = {1, 2, 3};
668 basicInfo.controlCommandCap_ = {1, 2, 3};
669 std::string sessionInfoStr = R"({
670 "capabilitySet": {}
671 })";
672 cJSON* jsonObj = cJSON_Parse(sessionInfoStr.c_str());
673 ASSERT_TRUE(jsonObj != nullptr);
674 int32_t ret = JsonUtils::SetSessionCapabilitySet(jsonObj, basicInfo);
675 EXPECT_EQ(ret, AVSESSION_SUCCESS);
676
677 cJSON_Delete(jsonObj);
678 SLOGI("SetSessionCapabilitySet001 end!");
679 }
680
681 /**
682 * @tc.name: SetSessionCapabilitySet002
683 * @tc.desc: test SetSessionCapabilitySet
684 * @tc.type: FUNC
685 * @tc.require: #I62OZV
686 */
687 static HWTEST(JsonUtilsTest, SetSessionCapabilitySet002, TestSize.Level0)
688 {
689 SLOGI("SetSessionCapabilitySet002 begin!");
690 AVSessionBasicInfo basicInfo;
691 std::string sessionInfoStr = R"({
692 "capabilitySet1": {
693 "key": "value"
694 }
695 })";
696 cJSON* jsonObj = cJSON_Parse(sessionInfoStr.c_str());
697 ASSERT_TRUE(jsonObj != nullptr);
698 int32_t ret = JsonUtils::SetSessionCapabilitySet(jsonObj, basicInfo);
699 EXPECT_EQ(ret, AVSESSION_SUCCESS);
700
701 cJSON_Delete(jsonObj);
702 SLOGI("SetSessionCapabilitySet002 end!");
703 }
704
705 /**
706 * @tc.name: SetSessionCapabilitySet003
707 * @tc.desc: test SetSessionCapabilitySet
708 * @tc.type: FUNC
709 * @tc.require: #I62OZV
710 */
711 static HWTEST(JsonUtilsTest, SetSessionCapabilitySet003, TestSize.Level0)
712 {
713 SLOGI("SetSessionCapabilitySet003 begin!");
714 AVSessionBasicInfo basicInfo;
715 std::string sessionInfoStr = R"({
716 "capabilitySet": {
717 "key": "value"
718 }
719 })";
720 cJSON* jsonObj = cJSON_Parse(sessionInfoStr.c_str());
721 ASSERT_TRUE(jsonObj != nullptr);
722 int32_t ret = JsonUtils::SetSessionCapabilitySet(jsonObj, basicInfo);
723 EXPECT_EQ(ret, AVSESSION_SUCCESS);
724
725 cJSON_Delete(jsonObj);
726 SLOGI("SetSessionCapabilitySet003 end!");
727 }
728
729 /**
730 * @tc.name: SetSessionCompatibility001
731 * @tc.desc: test SetSessionCompatibility
732 * @tc.type: FUNC
733 * @tc.require: #I62OZV
734 */
735 static HWTEST(JsonUtilsTest, SetSessionCompatibility001, TestSize.Level0)
736 {
737 SLOGI("SetSessionCompatibility001 begin!");
738 AVSessionBasicInfo basicInfo;
739 basicInfo.reserve_ = {1, 2, 3};
740 basicInfo.feature_ = {1, 2, 3};
741 basicInfo.extendCapability_ = {1, 2, 3};
742 std::string sessionInfoStr = R"({
743 "compatibility": {}
744 })";
745 cJSON* jsonObj = cJSON_Parse(sessionInfoStr.c_str());
746 ASSERT_TRUE(jsonObj != nullptr);
747 int32_t ret = JsonUtils::SetSessionCompatibility(jsonObj, basicInfo);
748 EXPECT_EQ(ret, AVSESSION_SUCCESS);
749
750 cJSON_Delete(jsonObj);
751 SLOGI("SetSessionCompatibility001 end!");
752 }
753
754 /**
755 * @tc.name: SetSessionCompatibility002
756 * @tc.desc: test SetSessionCompatibility
757 * @tc.type: FUNC
758 * @tc.require: #I62OZV
759 */
760 static HWTEST(JsonUtilsTest, SetSessionCompatibility002, TestSize.Level0)
761 {
762 SLOGI("SetSessionCompatibility002 begin!");
763 AVSessionBasicInfo basicInfo;
764 std::string sessionInfoStr = R"({
765 "compatibility1": {
766 "key": "value"
767 }
768 })";
769 cJSON* jsonObj = cJSON_Parse(sessionInfoStr.c_str());
770 ASSERT_TRUE(jsonObj != nullptr);
771 int32_t ret = JsonUtils::SetSessionCompatibility(jsonObj, basicInfo);
772 EXPECT_EQ(ret, AVSESSION_SUCCESS);
773
774 cJSON_Delete(jsonObj);
775 SLOGI("SetSessionCompatibility002 end!");
776 }
777
778 /**
779 * @tc.name: SetSessionCompatibility003
780 * @tc.desc: test SetSessionCompatibility
781 * @tc.type: FUNC
782 * @tc.require: #I62OZV
783 */
784 static HWTEST(JsonUtilsTest, SetSessionCompatibility003, TestSize.Level0)
785 {
786 SLOGI("SetSessionCompatibility003 begin!");
787 AVSessionBasicInfo basicInfo;
788 std::string sessionInfoStr = R"({
789 "compatibility": {
790 "key": "value"
791 }
792 })";
793 cJSON* jsonObj = cJSON_Parse(sessionInfoStr.c_str());
794 ASSERT_TRUE(jsonObj != nullptr);
795 int32_t ret = JsonUtils::SetSessionCompatibility(jsonObj, basicInfo);
796 EXPECT_EQ(ret, AVSESSION_SUCCESS);
797
798 cJSON_Delete(jsonObj);
799 SLOGI("SetSessionCompatibility003 end!");
800 }
801
802 /**
803 * @tc.name: SetSessionData001
804 * @tc.desc: test SetSessionData
805 * @tc.type: FUNC
806 * @tc.require: #I62OZV
807 */
808 static HWTEST(JsonUtilsTest, SetSessionData001, TestSize.Level0)
809 {
810 SLOGI("SetSessionData001 begin!");
811 AVSessionBasicInfo basicInfo;
812 std::string sessionInfoStr = R"({
813 "compatibility": {}
814 })";
815 cJSON* jsonObj = cJSON_Parse(sessionInfoStr.c_str());
816 ASSERT_TRUE(jsonObj != nullptr);
817 int32_t ret = JsonUtils::SetSessionData(jsonObj, basicInfo);
818 EXPECT_EQ(ret, AVSESSION_SUCCESS);
819
820 cJSON_Delete(jsonObj);
821 SLOGI("SetSessionData001 end!");
822 }
823
824 /**
825 * @tc.name: SetSessionData002
826 * @tc.desc: test SetSessionData
827 * @tc.type: FUNC
828 * @tc.require: #I62OZV
829 */
830 static HWTEST(JsonUtilsTest, SetSessionData002, TestSize.Level0)
831 {
832 SLOGI("SetSessionData002 begin!");
833 AVSessionBasicInfo basicInfo;
834 basicInfo.extend_ = {1, 2, 3};
835 std::string sessionInfoStr = R"({
836 "compatibility1": {
837 "key": "value"
838 }
839 })";
840 cJSON* jsonObj = cJSON_Parse(sessionInfoStr.c_str());
841 ASSERT_TRUE(jsonObj != nullptr);
842 int32_t ret = JsonUtils::SetSessionData(jsonObj, basicInfo);
843 EXPECT_EQ(ret, AVSESSION_SUCCESS);
844
845 cJSON_Delete(jsonObj);
846 SLOGI("SetSessionData002 end!");
847 }
848
849 /**
850 * @tc.name: SetSessionData003
851 * @tc.desc: test SetSessionData
852 * @tc.type: FUNC
853 * @tc.require: #I62OZV
854 */
855 static HWTEST(JsonUtilsTest, SetSessionData003, TestSize.Level0)
856 {
857 SLOGI("SetSessionData003 begin!");
858 AVSessionBasicInfo basicInfo;
859 std::string sessionInfoStr = R"({
860 "compatibility": {
861 "key": "value"
862 }
863 })";
864 cJSON* jsonObj = cJSON_Parse(sessionInfoStr.c_str());
865 ASSERT_TRUE(jsonObj != nullptr);
866 int32_t ret = JsonUtils::SetSessionData(jsonObj, basicInfo);
867 EXPECT_EQ(ret, AVSESSION_SUCCESS);
868
869 cJSON_Delete(jsonObj);
870 SLOGI("SetSessionData003 end!");
871 }
872
873 /**
874 * @tc.name: SetSessionBasicInfo002
875 * @tc.desc: test SetSessionBasicInfo
876 * @tc.type: FUNC
877 * @tc.require: #I62OZV
878 */
879 static HWTEST(JsonUtilsTest, SetSessionBasicInfo002, TestSize.Level0)
880 {
881 SLOGI("SetSessionBasicInfo002 begin!");
882 std::string sessionInfoStr = R"({
883 "compatibility": {
884 "capabilitySet": {
885 "key": "value"
886 }
887 }
888 }})";
889 AVSessionBasicInfo basicInfo;
890 int32_t ret = JsonUtils::SetSessionBasicInfo(sessionInfoStr, basicInfo);
891 EXPECT_EQ(ret, AVSESSION_SUCCESS);
892 SLOGI("SetSessionBasicInfo002 end!");
893 }
894
895 /**
896 * @tc.name: GetSessionCapabilitySet001
897 * @tc.desc: test GetSessionCapabilitySet
898 * @tc.type: FUNC
899 * @tc.require: #I62OZV
900 */
901 static HWTEST(JsonUtilsTest, GetSessionCapabilitySet001, TestSize.Level0)
902 {
903 SLOGI("GetSessionCapabilitySet001 begin!");
904 AVSessionBasicInfo basicInfo;
905 std::string sessionInfoStr = R"({
906 "metaData": [1, 2, 3],
907 "playbackState": [1, 2, 3],
908 "controlCommand": [1, 2, 3]
909 })";
910 cJSON* jsonObj = cJSON_Parse(sessionInfoStr.c_str());
911 ASSERT_TRUE(jsonObj != nullptr);
912 int32_t ret = JsonUtils::GetSessionCapabilitySet(jsonObj, basicInfo);
913 EXPECT_EQ(ret, AVSESSION_SUCCESS);
914
915 cJSON_Delete(jsonObj);
916 SLOGI("GetSessionCapabilitySet001 end!");
917 }
918
919 /**
920 * @tc.name: GetSessionCompatibility001
921 * @tc.desc: test GetSessionCompatibility
922 * @tc.type: FUNC
923 * @tc.require: #I62OZV
924 */
925 static HWTEST(JsonUtilsTest, GetSessionCompatibility001, TestSize.Level0)
926 {
927 SLOGI("GetSessionCompatibility001 begin!");
928 const char* jsonStr = R"({
929 "networkId": "network123",
930 "vendorId": "vendor456",
931 "deviceType": "type789",
932 "systemVersion": "versionXYZ",
933 "avsessionVersion": 1,
934 "reserve": ["reserve1", "reserve2"],
935 "features": ["feature1", "feature2"],
936 "capabilitySet": {
937 "metaData": [1, 2, 3],
938 "playbackState": [1, 2, 3],
939 "controlCommand": [1, 2, 3]
940 },
941 "extendCapability": ["extend1", "extend2"]
942 })";
943
944 cJSON* json = cJSON_Parse(jsonStr);
945 ASSERT_TRUE(json != nullptr);
946
947 AVSessionBasicInfo basicInfo;
948 int32_t ret = JsonUtils::GetSessionCompatibility(json, basicInfo);
949
950 EXPECT_EQ(ret, AVSESSION_SUCCESS);
951
952 cJSON_Delete(json);
953 SLOGI("GetSessionCompatibility001 end!");
954 }
955
956
957 /**
958 * @tc.name: GetSessionData001
959 * @tc.desc: test GetSessionData
960 * @tc.type: FUNC
961 * @tc.require: #I62OZV
962 */
963 static HWTEST(JsonUtilsTest, GetSessionData001, TestSize.Level0)
964 {
965 SLOGI("GetSessionData001 begin!");
966 const char* jsonStr = R"({
967 "systemTime": 123456789,
968 "extend": ["extend1", "extend2"]
969 })";
970
971 cJSON* json = cJSON_Parse(jsonStr);
972 ASSERT_TRUE(json != nullptr);
973
974 AVSessionBasicInfo basicInfo;
975 int32_t ret = JsonUtils::GetSessionData(json, basicInfo);
976
977 EXPECT_EQ(ret, AVSESSION_SUCCESS);
978
979 cJSON_Delete(json);
980 SLOGI("GetSessionData001 end!");
981 }
982
983 /**
984 * @tc.name: GetSessionBasicInfo001
985 * @tc.desc: test GetSessionBasicInfo
986 * @tc.type: FUNC
987 * @tc.require: #I62OZV
988 */
989 static HWTEST(JsonUtilsTest, GetSessionBasicInfo001, TestSize.Level0)
990 {
991 SLOGI("GetSessionBasicInfo001 begin!");
992 const std::string sessionInfo = R"({
993 "compatibility": {
994 "networkId": "network123",
995 "vendorId": "vendor456",
996 "deviceType": "type789",
997 "systemVersion": "versionXYZ",
998 "avsessionVersion": 1,
999 "reserve": ["reserve1", "reserve2"],
1000 "features": ["feature1", "feature2"],
1001 "capabilitySet": {
1002 "metaData": [1, 2, 3],
1003 "playbackState": [1, 2, 3],
1004 "controlCommand": [1, 2, 3]
1005 },
1006 "extendCapability": ["extend1", "extend2"]
1007 },
1008 "data": {
1009 "systemTime": 123456789,
1010 "extend": ["extend1", "extend2"]
1011 }
1012 })";
1013
1014 AVSessionBasicInfo basicInfo;
1015 int32_t ret = JsonUtils::GetSessionBasicInfo(sessionInfo, basicInfo);
1016
1017 EXPECT_EQ(ret, AVSESSION_SUCCESS);
1018 SLOGI("GetSessionBasicInfo001 end!");
1019 }
1020
1021 /**
1022 * @tc.name: GetSessionDescriptors001
1023 * @tc.desc: test GetSessionDescriptors
1024 * @tc.type: FUNC
1025 * @tc.require: #I62OZV
1026 */
1027 static HWTEST(JsonUtilsTest, GetSessionDescriptors001, TestSize.Level0)
1028 {
1029 SLOGI("GetSessionDescriptors001 begin!");
1030 const std::string sessionInfo = R"({
1031 "data": {
1032 "sessionDescriptors": [
1033 {
1034 "sessionId": "session1",
1035 "type": "audio",
1036 "bundleName": "com.example.app1",
1037 "abilityName": "MainAbility1",
1038 "tag": "tag1",
1039 "isThirdPartyApp": true
1040 },
1041 {
1042 "sessionId": "session2",
1043 "type": "video",
1044 "bundleName": "com.example.app2",
1045 "abilityName": "MainAbility2",
1046 "tag": "tag2",
1047 "isThirdPartyApp": false
1048 }
1049 ]
1050 }
1051 })";
1052
1053 std::vector<AVSessionDescriptor> descriptors;
1054 int32_t ret = JsonUtils::GetSessionDescriptors(sessionInfo, descriptors);
1055
1056 EXPECT_EQ(ret, AVSESSION_SUCCESS);
1057 SLOGI("GetSessionDescriptors001 end!");
1058 }
1059
1060 /**
1061 * @tc.name: GetSessionDescriptors002
1062 * @tc.desc: test GetSessionDescriptors
1063 * @tc.type: FUNC
1064 * @tc.require: #I62OZV
1065 */
1066 static HWTEST(JsonUtilsTest, GetSessionDescriptors002, TestSize.Level0)
1067 {
1068 SLOGI("GetSessionDescriptors002 begin!");
1069 const std::string sessionInfo = R"({
1070 "test": {
1071 "sessionDescriptors": [
1072 {
1073 "sessionId": "session1",
1074 "type": "audio",
1075 "bundleName": "com.example.app1",
1076 "abilityName": "MainAbility1",
1077 "tag": "tag1",
1078 "isThirdPartyApp": true
1079 },
1080 {
1081 "sessionId": "session2",
1082 "type": "video",
1083 "bundleName": "com.example.app2",
1084 "abilityName": "MainAbility2",
1085 "tag": "tag2",
1086 "isThirdPartyApp": false
1087 }
1088 ]
1089 }
1090 })";
1091
1092 std::vector<AVSessionDescriptor> descriptors;
1093 int32_t ret = JsonUtils::GetSessionDescriptors(sessionInfo, descriptors);
1094 EXPECT_EQ(ret, AVSESSION_ERROR);
1095 SLOGI("GetSessionDescriptors002 end!");
1096 }
1097
1098 /**
1099 * @tc.name: GetSessionDescriptors003
1100 * @tc.desc: test GetSessionDescriptors
1101 * @tc.type: FUNC
1102 * @tc.require: #I62OZV
1103 */
1104 static HWTEST(JsonUtilsTest, GetSessionDescriptors003, TestSize.Level0)
1105 {
1106 SLOGI("GetSessionDescriptors003 begin!");
1107 const std::string sessionInfo = R"({
1108 "data": {
1109 "Descriptors": [
1110 {
1111 "sessionId": "session1",
1112 "type": "audio",
1113 "bundleName": "com.example.app1",
1114 "abilityName": "MainAbility1",
1115 "tag": "tag1",
1116 "isThirdPartyApp": true
1117 },
1118 {
1119 "sessionId": "session2",
1120 "type": "video",
1121 "bundleName": "com.example.app2",
1122 "abilityName": "MainAbility2",
1123 "tag": "tag2",
1124 "isThirdPartyApp": false
1125 }
1126 ]
1127 }
1128 })";
1129
1130 std::vector<AVSessionDescriptor> descriptors;
1131 int32_t ret = JsonUtils::GetSessionDescriptors(sessionInfo, descriptors);
1132 EXPECT_EQ(ret, AVSESSION_ERROR);
1133 SLOGI("GetSessionDescriptors003 end!");
1134 }
1135
1136 static HWTEST(JsonUtilsTest, GetSessionDescriptor001, TestSize.Level0)
1137 {
1138 SLOGI("GetSessionDescriptor001 begin!");
1139 const std::string sessionInfo = R"({
1140 "test": {
1141 "sessionDescriptors": [
1142 {
1143 "sessionId": "session1",
1144 "type": "audio",
1145 "bundleName": "com.example.app1",
1146 "abilityName": "MainAbility1",
1147 "tag": "tag1",
1148 "isThirdPartyApp": true
1149 },
1150 {
1151 "sessionId": "session2",
1152 "type": "video",
1153 "bundleName": "com.example.app2",
1154 "abilityName": "MainAbility2",
1155 "tag": "tag2",
1156 "isThirdPartyApp": false
1157 }
1158 ]
1159 }
1160 })";
1161 AVSessionDescriptor descriptor;
1162 int32_t ret = JsonUtils::GetSessionDescriptor(sessionInfo, descriptor);
1163 EXPECT_EQ(ret, AVSESSION_ERROR);
1164 SLOGI("GetSessionDescriptor001 end!");
1165 }
1166
1167 static HWTEST(JsonUtilsTest, GetSessionDescriptor002, TestSize.Level0)
1168 {
1169 SLOGI("GetSessionDescriptor002 begin!");
1170 const std::string sessionInfo = R"({
1171 "data": {
1172 "Descriptors": [
1173 {
1174 "sessionId": "session1",
1175 "type": "audio",
1176 "bundleName": "com.example.app1",
1177 "abilityName": "MainAbility1",
1178 "tag": "tag1",
1179 "isThirdPartyApp": true
1180 },
1181 {
1182 "sessionId": "session2",
1183 "type": "video",
1184 "bundleName": "com.example.app2",
1185 "abilityName": "MainAbility2",
1186 "tag": "tag2",
1187 "isThirdPartyApp": false
1188 }
1189 ]
1190 }
1191 })";
1192 AVSessionDescriptor descriptor;
1193 int32_t ret = JsonUtils::GetSessionDescriptor(sessionInfo, descriptor);
1194 EXPECT_EQ(ret, AVSESSION_ERROR);
1195 SLOGI("GetSessionDescriptor002 end!");
1196 }
1197
1198 static HWTEST(JsonUtilsTest, GetSessionDescriptor003, TestSize.Level0)
1199 {
1200 SLOGI("GetSessionDescriptor003 begin!");
1201 const std::string sessionInfo = R"({
1202 "data": {
1203 "sessionDescriptors": [
1204 {
1205 "sessionId": true,
1206 "type": "audio",
1207 "bundleName": "com.example.app1",
1208 "abilityName": "MainAbility1",
1209 "tag": "tag1",
1210 "isThirdPartyApp": true
1211 },
1212 {
1213 "sessionId": false,
1214 "type": "video",
1215 "bundleName": "com.example.app2",
1216 "abilityName": "MainAbility2",
1217 "tag": "tag2",
1218 "isThirdPartyApp": false
1219 }
1220 ]
1221 }
1222 })";
1223 AVSessionDescriptor descriptor;
1224 int32_t ret = JsonUtils::GetSessionDescriptor(sessionInfo, descriptor);
1225 EXPECT_EQ(ret, AVSESSION_ERROR);
1226 SLOGI("GetSessionDescriptor003 end!");
1227 }
1228
1229 static HWTEST(JsonUtilsTest, SetSessionDescriptorByCJSON001, TestSize.Level0)
1230 {
1231 SLOGI("SetSessionDescriptorByCJSON001 begin!");
1232 AVSessionDescriptor descriptor;
1233 cJSON* test = nullptr;
1234 int32_t ret = OHOS::AVSession::JsonUtils::SetSessionDescriptorByCJSON(test, descriptor);
1235 EXPECT_EQ(ret, AVSESSION_ERROR);
1236 SLOGI("SetSessionDescriptorByCJSON001 end!");
1237 }