1 /*
2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include <cstdlib>
18 #include <string>
19
20 #define private public
21 #define protected public
22 #include "pt_json.h"
23 #undef private
24 #undef protected
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace {
31 const std::string JSON_STRING = "{"
32 "\"name\": \"Json.CN\","
33 "\"app\": \"apptest\","
34 "\"module\": {"
35 "\"name\": \"nametest\","
36 "\"type\": \"typetest\","
37 "\"deviceTypes\": ["
38 "\"aaaaaaaaaaa\","
39 "\"bbbbbbbbbbb\","
40 "\"ccccccccccc\""
41 "]"
42 "},"
43 "\"bundleName\": \"bundleNametest\","
44 "\"versionCode\": 100,"
45 "\"versionName\": \"versionNametest\","
46 "\"patchVersionCode\": 200,"
47 "\"patchVersionName\": \"patchVersionNametest\","
48 "\"originalModuleHash\": \"originalModuleHashtest\""
49 "}";
50 }
51
52 class PtJsonTest : public testing::Test {
53 public:
PtJsonTest()54 PtJsonTest() {}
~PtJsonTest()55 virtual ~PtJsonTest() {}
56
57 static void SetUpTestCase();
58
59 static void TearDownTestCase();
60
61 void SetUp();
62
63 void TearDown();
64 };
65
SetUpTestCase()66 void PtJsonTest::SetUpTestCase() {}
67
TearDownTestCase()68 void PtJsonTest::TearDownTestCase() {}
69
SetUp()70 void PtJsonTest::SetUp() {}
71
TearDown()72 void PtJsonTest::TearDown() {}
73
74 /*
75 * @tc.name: CreateObject_0100
76 * @tc.desc: CreateObject.
77 * @tc.type: FUNC
78 * @tc.require:
79 */
80 HWTEST_F(PtJsonTest, CreateObject_0100, Function | MediumTest | Level1)
81 {
82 cJSON *cjson = new cJSON();
83 OHOS::AppPackingTool::PtJson ptJson(cjson);
84
85 std::unique_ptr<OHOS::AppPackingTool::PtJson> ptjsonObject = ptJson.CreateObject();
86 EXPECT_TRUE(ptjsonObject != NULL);
87 }
88
89 /*
90 * @tc.name: CreateArray_0200
91 * @tc.desc: CreateArray.
92 * @tc.type: FUNC
93 * @tc.require:
94 */
95 HWTEST_F(PtJsonTest, CreateArray_0200, Function | MediumTest | Level1)
96 {
97 cJSON *cjson = new cJSON();
98 OHOS::AppPackingTool::PtJson ptJson(cjson);
99
100 std::unique_ptr<OHOS::AppPackingTool::PtJson> ptjsonArray = ptJson.CreateArray();
101 EXPECT_TRUE(ptjsonArray != NULL);
102 }
103
104 /*
105 * @tc.name: ReleaseRoot_0300
106 * @tc.desc: ReleaseRoot.
107 * @tc.type: FUNC
108 * @tc.require:
109 */
110 HWTEST_F(PtJsonTest, ReleaseRoot_0300, Function | MediumTest | Level1)
111 {
112 cJSON *cjson = new cJSON();
113 OHOS::AppPackingTool::PtJson ptJson(cjson);
114
115 ptJson.ReleaseRoot();
116 EXPECT_TRUE(ptJson.object_ == NULL);
117 }
118
119 /*
120 * @tc.name: Parse_0400
121 * @tc.desc: Parse.
122 * @tc.type: FUNC
123 * @tc.require:
124 */
125 HWTEST_F(PtJsonTest, Parse_0400, Function | MediumTest | Level1)
126 {
127 cJSON *cjson = new cJSON();
128 OHOS::AppPackingTool::PtJson ptJson(cjson);
129
130 std::unique_ptr<OHOS::AppPackingTool::PtJson> ptjson = ptJson.Parse(JSON_STRING);
131 EXPECT_TRUE(ptjson != NULL);
132 }
133
134 /*
135 * @tc.name: Stringify_0500
136 * @tc.desc: Stringify.
137 * @tc.type: FUNC
138 * @tc.require:
139 */
140 HWTEST_F(PtJsonTest, Stringify_0500, Function | MediumTest | Level1)
141 {
142 cJSON *cjson = new cJSON();
143 OHOS::AppPackingTool::PtJson ptJson(cjson);
144
145 std::unique_ptr<OHOS::AppPackingTool::PtJson> ptjson = ptJson.Parse(JSON_STRING);
146 EXPECT_TRUE(ptjson != NULL);
147 EXPECT_TRUE(!ptjson->Stringify().empty());
148 }
149
150 /*
151 * @tc.name: Add_0600
152 * @tc.desc: Add.
153 * @tc.type: FUNC
154 * @tc.require:
155 */
156 HWTEST_F(PtJsonTest, Add_0600, Function | MediumTest | Level1)
157 {
158 cJSON *cjson = new cJSON();
159 OHOS::AppPackingTool::PtJson ptJson(cjson);
160
161 std::unique_ptr<OHOS::AppPackingTool::PtJson> ptjsonObject = ptJson.CreateObject();
162 EXPECT_TRUE(ptjsonObject != NULL);
163
164 std::list<std::string> values = {"a", "b", "c"};
165
166 EXPECT_TRUE(ptJson.Add("AAA", true));
167 EXPECT_TRUE(ptJson.Add("BBB", 123));
168 EXPECT_TRUE(ptJson.Add("CCC", 123.5));
169 EXPECT_TRUE(ptJson.Add("DDD", "ABC"));
170 EXPECT_TRUE(ptJson.Add("EEE", ptjsonObject));
171 EXPECT_TRUE(ptJson.Add("FFF", values));
172 }
173
174 /*
175 * @tc.name: Push_0700
176 * @tc.desc: Push.
177 * @tc.type: FUNC
178 * @tc.require:
179 */
180 HWTEST_F(PtJsonTest, Push_0700, Function | MediumTest | Level1)
181 {
182 cJSON *cjson = new cJSON();
183 OHOS::AppPackingTool::PtJson ptJson(cjson);
184
185 std::unique_ptr<OHOS::AppPackingTool::PtJson> ptjsonObject = ptJson.CreateObject();
186 EXPECT_TRUE(ptjsonObject != NULL);
187
188 std::unique_ptr<OHOS::AppPackingTool::PtJson> ptjsonArray = ptJson.CreateArray();
189 EXPECT_TRUE(ptjsonArray != NULL);
190
191 EXPECT_TRUE(ptJson.Add("AAA", true));
192 EXPECT_TRUE(ptJson.Add("BBB", 123));
193 EXPECT_TRUE(ptJson.Add("CCC", 123.5));
194 EXPECT_TRUE(ptJson.Add("DDD", "ABC"));
195 EXPECT_TRUE(ptJson.Add("EEE", ptjsonObject));
196
197 EXPECT_TRUE(ptJson.Push(true));
198 EXPECT_TRUE(ptJson.Push(123));
199 EXPECT_TRUE(ptJson.Push(123.5));
200 EXPECT_TRUE(ptJson.Push("ABC"));
201 EXPECT_TRUE(ptJson.Push(ptjsonArray));
202 }
203
204 /*
205 * @tc.name: Contains_0800
206 * @tc.desc: Contains.
207 * @tc.type: FUNC
208 * @tc.require:
209 */
210 HWTEST_F(PtJsonTest, Contains_0800, Function | MediumTest | Level1)
211 {
212 cJSON *cjson = new cJSON();
213 OHOS::AppPackingTool::PtJson ptJson(cjson);
214 EXPECT_TRUE(ptJson.Add("AAA", true));
215 EXPECT_TRUE(ptJson.Remove("AAA"));
216 }
217
218 /*
219 * @tc.name: Contains_0900
220 * @tc.desc: Contains.
221 * @tc.type: FUNC
222 * @tc.require:
223 */
224 HWTEST_F(PtJsonTest, Contains_0900, Function | MediumTest | Level1)
225 {
226 cJSON *cjson = new cJSON();
227 OHOS::AppPackingTool::PtJson ptJson(cjson);
228 EXPECT_TRUE(ptJson.Add("AAA", true));
229 EXPECT_TRUE(ptJson.Contains("AAA"));
230 }
231
232 /*
233 * @tc.name: GetKey_1000
234 * @tc.desc: GetKey.
235 * @tc.type: FUNC
236 * @tc.require:
237 */
238 HWTEST_F(PtJsonTest, GetKey_1000, Function | MediumTest | Level1)
239 {
240 char a[] = "{\"firstName\":\"Brett\"}";
241 cJSON*root = cJSON_Parse(a);
242 cJSON*item = cJSON_GetObjectItem(root, "firstName");
243 OHOS::AppPackingTool::PtJson ptJson(item);
244 EXPECT_TRUE(!ptJson.GetKey().empty());
245 EXPECT_TRUE(!ptJson.Stringify().empty());
246 }
247
248 /*
249 * @tc.name: GetJson_1100
250 * @tc.desc: GetJson.
251 * @tc.type: FUNC
252 * @tc.require:
253 */
254 HWTEST_F(PtJsonTest, GetJson_1100, Function | MediumTest | Level1)
255 {
256 cJSON *cjson = new cJSON();
257 OHOS::AppPackingTool::PtJson ptJson(cjson);
258
259 EXPECT_TRUE(ptJson.GetJson() != nullptr);
260 }
261
262 /*
263 * @tc.name: IsBool_1200
264 * @tc.desc: IsBool.
265 * @tc.type: FUNC
266 * @tc.require:
267 */
268 HWTEST_F(PtJsonTest, IsBool_1200, Function | MediumTest | Level1)
269 {
270 cJSON *node = cJSON_CreateBool(true);
271 OHOS::AppPackingTool::PtJson ptJsonBool(node);
272
273 EXPECT_TRUE(ptJsonBool.IsBool());
274 EXPECT_TRUE(ptJsonBool.GetBool(true));
275 }
276
277 /*
278 * @tc.name: IsNumber_1300
279 * @tc.desc: IsNumber.
280 * @tc.type: FUNC
281 * @tc.require:
282 */
283 HWTEST_F(PtJsonTest, IsNumber_1300, Function | MediumTest | Level1)
284 {
285 cJSON *node = cJSON_CreateNumber(12345);
286 OHOS::AppPackingTool::PtJson ptJsonNumber(node);
287
288 EXPECT_TRUE(ptJsonNumber.IsNumber());
289 EXPECT_EQ(ptJsonNumber.GetInt(12345), 12345);
290 EXPECT_EQ(ptJsonNumber.GetInt64(12345), 12345);
291 EXPECT_EQ(ptJsonNumber.GetUInt(12345), 12345);
292 EXPECT_EQ(ptJsonNumber.GetUInt64(12345), 12345);
293 EXPECT_EQ(ptJsonNumber.GetDouble(12345.5), 12345);
294 }
295
296 /*
297 * @tc.name: IsString_1400
298 * @tc.desc: IsString.
299 * @tc.type: FUNC
300 * @tc.require:
301 */
302 HWTEST_F(PtJsonTest, IsString_1400, Function | MediumTest | Level1)
303 {
304 cJSON *node = cJSON_CreateString("abcd");
305 OHOS::AppPackingTool::PtJson ptJsonString(node);
306
307 EXPECT_TRUE(ptJsonString.IsString());
308 EXPECT_STREQ(ptJsonString.GetString().c_str(), "abcd");
309 }
310
311 /*
312 * @tc.name: IsObject_1500
313 * @tc.desc: IsObject.
314 * @tc.type: FUNC
315 * @tc.require:
316 */
317 HWTEST_F(PtJsonTest, IsObject_1500, Function | MediumTest | Level1)
318 {
319 cJSON *cjson = new cJSON();
320 OHOS::AppPackingTool::PtJson ptJson(cjson);
321
322 std::unique_ptr<OHOS::AppPackingTool::PtJson> ptjsonObject = ptJson.CreateObject();
323 EXPECT_TRUE(ptjsonObject != NULL);
324 EXPECT_TRUE(ptjsonObject->IsObject());
325 }
326
327 /*
328 * @tc.name: IsArray_1600
329 * @tc.desc: IsArray.
330 * @tc.type: FUNC
331 * @tc.require:
332 */
333 HWTEST_F(PtJsonTest, IsArray_1600, Function | MediumTest | Level1)
334 {
335 cJSON *cjson = new cJSON();
336 OHOS::AppPackingTool::PtJson ptJson(cjson);
337
338 std::unique_ptr<OHOS::AppPackingTool::PtJson> ptjsonArray = ptJson.CreateArray();
339 EXPECT_TRUE(ptjsonArray != NULL);
340 EXPECT_TRUE(ptjsonArray->IsArray());
341 ptjsonArray->Push("11111");
342 ptjsonArray->Push("22222");
343 EXPECT_STREQ(ptjsonArray->Get(1)->GetString().c_str(), "22222");
344 EXPECT_TRUE(ptjsonArray->GetSize() > 0);
345 }
346
347 /*
348 * @tc.name: IsNull_1700
349 * @tc.desc: IsNull.
350 * @tc.type: FUNC
351 * @tc.require:
352 */
353 HWTEST_F(PtJsonTest, IsNull_1700, Function | MediumTest | Level1)
354 {
355 cJSON *cjson = cJSON_CreateNull();
356 OHOS::AppPackingTool::PtJson ptJson(cjson);
357 EXPECT_TRUE(ptJson.IsNull());
358 }
359
360 /*
361 * @tc.name: SetBool_1800
362 * @tc.desc: SetBool.
363 * @tc.type: FUNC
364 * @tc.require:
365 */
366 HWTEST_F(PtJsonTest, SetBool_1800, Function | MediumTest | Level1)
367 {
368 cJSON *cjson = new cJSON();
369 OHOS::AppPackingTool::PtJson ptJson(cjson);
370
371 bool flag = false;
372 ptJson.Add("AAA", true);
373 EXPECT_EQ(ptJson.SetBool("AAA", flag), OHOS::AppPackingTool::Result::SUCCESS);
374 ptJson.GetBool("AAA", &flag);
375 EXPECT_FALSE(flag);
376 }
377
378 /*
379 * @tc.name: SetInt_1900
380 * @tc.desc: SetInt.
381 * @tc.type: FUNC
382 * @tc.require:
383 */
384 HWTEST_F(PtJsonTest, SetInt_1900, Function | MediumTest | Level1)
385 {
386 cJSON *cjson = new cJSON();
387 OHOS::AppPackingTool::PtJson ptJson(cjson);
388
389 EXPECT_TRUE(ptJson.Add("BBB", 123));
390 int number = 0;
391 EXPECT_EQ(ptJson.SetInt("BBB", 321), OHOS::AppPackingTool::Result::SUCCESS);
392 ptJson.GetInt("BBB", &number);
393 EXPECT_EQ(number, 321);
394 }
395
396 /*
397 * @tc.name: SetInt64_2000
398 * @tc.desc: SetInt64.
399 * @tc.type: FUNC
400 * @tc.require:
401 */
402 HWTEST_F(PtJsonTest, SetInt64_2000, Function | MediumTest | Level1)
403 {
404 cJSON *cjson = new cJSON();
405 OHOS::AppPackingTool::PtJson ptJson(cjson);
406
407 EXPECT_TRUE(ptJson.Add("BBB", 123));
408 EXPECT_TRUE(ptJson.Add("CCC", 123.5));
409
410 int64_t value64 = 11111;
411 EXPECT_EQ(ptJson.SetInt64("BBB", value64), OHOS::AppPackingTool::Result::SUCCESS);
412 int64_t value642;
413 EXPECT_EQ(ptJson.GetInt64("BBB", &value642), OHOS::AppPackingTool::Result::SUCCESS);
414 EXPECT_EQ(value642, 11111);
415
416 uint32_t value32 = 2222;
417 EXPECT_EQ(ptJson.SetUInt("BBB", value32), OHOS::AppPackingTool::Result::SUCCESS);
418 uint32_t value322;
419 EXPECT_EQ(ptJson.GetUInt("BBB", &value322), OHOS::AppPackingTool::Result::SUCCESS);
420 EXPECT_EQ(value322, 2222);
421
422 uint64_t valueInt64 = 3333;
423 EXPECT_EQ(ptJson.SetUInt64("BBB", valueInt64), OHOS::AppPackingTool::Result::SUCCESS);
424 uint64_t valueInt642;
425 EXPECT_EQ(ptJson.GetUInt64("BBB", &valueInt642), OHOS::AppPackingTool::Result::SUCCESS);
426 EXPECT_EQ(valueInt642, 3333);
427
428 double valueDouble = 4444;
429 EXPECT_EQ(ptJson.SetDouble("CCC", valueDouble), OHOS::AppPackingTool::Result::SUCCESS);
430 double valueDouble2;
431 EXPECT_EQ(ptJson.GetDouble("CCC", &valueDouble2), OHOS::AppPackingTool::Result::SUCCESS);
432 EXPECT_EQ(valueDouble2, 4444);
433 }
434
435 /*
436 * @tc.name: SetString_2100
437 * @tc.desc: SetString.
438 * @tc.type: FUNC
439 * @tc.require:
440 */
441 HWTEST_F(PtJsonTest, SetString_2100, Function | MediumTest | Level1)
442 {
443 cJSON *cjson = new cJSON();
444 OHOS::AppPackingTool::PtJson ptJson(cjson);
445
446 EXPECT_TRUE(ptJson.Add("DDD", "ABC"));
447 std::string str("1234567890");
448 EXPECT_EQ(ptJson.SetString("DDD", str), OHOS::AppPackingTool::Result::SUCCESS);
449 std::string str2;
450 EXPECT_EQ(ptJson.GetString("DDD", &str2), OHOS::AppPackingTool::Result::SUCCESS);
451 EXPECT_STREQ(str2.c_str(), str.c_str());
452 }
453
454 /*
455 * @tc.name: GetObject_2200
456 * @tc.desc: GetObject.
457 * @tc.type: FUNC
458 * @tc.require:
459 */
460 HWTEST_F(PtJsonTest, GetObject_2200, Function | MediumTest | Level1)
461 {
462 cJSON *cjson = new cJSON();
463 OHOS::AppPackingTool::PtJson ptJson(cjson);
464
465 std::unique_ptr<OHOS::AppPackingTool::PtJson> ptjsonObject = ptJson.CreateObject();
466 EXPECT_TRUE(ptjsonObject != NULL);
467 EXPECT_TRUE(ptJson.Add("EEE", ptjsonObject));
468 std::unique_ptr<OHOS::AppPackingTool::PtJson> Object;
469 EXPECT_EQ(ptJson.GetObject("EEE", &Object), OHOS::AppPackingTool::Result::SUCCESS);
470 }
471
472 /*
473 * @tc.name: GetArray_2300
474 * @tc.desc: GetArray.
475 * @tc.type: FUNC
476 * @tc.require:
477 */
478 HWTEST_F(PtJsonTest, GetArray_2300, Function | MediumTest | Level1)
479 {
480 cJSON *cjson = new cJSON();
481 OHOS::AppPackingTool::PtJson ptJson(cjson);
482
483 ptJson.Add("FFF", ptJson.CreateArray());
484 std::unique_ptr<OHOS::AppPackingTool::PtJson> Array;
485 EXPECT_EQ(ptJson.GetArray("FFF", &Array), OHOS::AppPackingTool::Result::SUCCESS);
486 }
487
488 /*
489 * @tc.name: GetAny_2400
490 * @tc.desc: GetAny.
491 * @tc.type: FUNC
492 * @tc.require:
493 */
494 HWTEST_F(PtJsonTest, GetAny_2400, Function | MediumTest | Level1)
495 {
496 cJSON *cjson = new cJSON();
497 OHOS::AppPackingTool::PtJson ptJson(cjson);
498
499 ptJson.Add("FFF", ptJson.CreateArray());
500 std::unique_ptr<OHOS::AppPackingTool::PtJson> Object;
501 EXPECT_EQ(ptJson.GetAny("FFF", &Object), OHOS::AppPackingTool::Result::SUCCESS);
502 }
503
504 /*
505 * @tc.name: Parse_0100
506 * @tc.desc: Parse
507 * @tc.type: FUNC
508 */
509 HWTEST_F(PtJsonTest, Parse_0100, Function | MediumTest | Level1)
510 {
511 std::string data;
512 auto ret = OHOS::AppPackingTool::PtJson::Parse(data);
513 EXPECT_EQ(ret, nullptr);
514 }
515
516 /*
517 * @tc.name: Stringify_0100
518 * @tc.desc: Stringify
519 * @tc.type: FUNC
520 */
521 HWTEST_F(PtJsonTest, Stringify_0100, Function | MediumTest | Level1)
522 {
523 OHOS::AppPackingTool::PtJson ptJson;
524
525 std::string ret = ptJson.Stringify();
526 EXPECT_EQ(ret, "");
527 }
528
529 /*
530 * @tc.name: Stringify_0200
531 * @tc.desc: Stringify
532 * @tc.type: FUNC
533 */
534 HWTEST_F(PtJsonTest, Stringify_0200, Function | MediumTest | Level1)
535 {
536 cJSON *cjson = new cJSON();
537 OHOS::AppPackingTool::PtJson ptJson(cjson);
538
539 std::string ret = ptJson.Stringify();
540 EXPECT_EQ(ret, "");
541 }
542
543 /*
544 * @tc.name: Add_0100
545 * @tc.desc: Add
546 * @tc.type: FUNC
547 */
548 HWTEST_F(PtJsonTest, Add_0100, Function | MediumTest | Level1)
549 {
550 cJSON *cjson = new cJSON();
551 OHOS::AppPackingTool::PtJson ptJson(cjson);
552
553 bool ret = ptJson.Add(nullptr, true);
554 EXPECT_FALSE(ret);
555
556 ret = ptJson.Add("addf", true);
557 ret = ptJson.Add("addf", false);
558 EXPECT_FALSE(ret);
559 }
560
561 /*
562 * @tc.name: Add_0200
563 * @tc.desc: Add
564 * @tc.type: FUNC
565 */
566 HWTEST_F(PtJsonTest, Add_0200, Function | MediumTest | Level1)
567 {
568 cJSON *cjson = new cJSON();
569 OHOS::AppPackingTool::PtJson ptJson(cjson);
570
571 int64_t value = 100;
572 bool ret = ptJson.Add("adda", value);
573 EXPECT_TRUE(ret);
574 }
575
576 /*
577 * @tc.name: Add_0300
578 * @tc.desc: Add
579 * @tc.type: FUNC
580 */
581 HWTEST_F(PtJsonTest, Add_0300, Function | MediumTest | Level1)
582 {
583 cJSON *cjson = new cJSON();
584 OHOS::AppPackingTool::PtJson ptJson(cjson);
585
586 uint32_t value = 100;
587 bool ret = ptJson.Add("addb", value);
588 EXPECT_TRUE(ret);
589 }
590
591 /*
592 * @tc.name: Add_0400
593 * @tc.desc: Add
594 * @tc.type: FUNC
595 */
596 HWTEST_F(PtJsonTest, Add_0400, Function | MediumTest | Level1)
597 {
598 cJSON *cjson = new cJSON();
599 OHOS::AppPackingTool::PtJson ptJson(cjson);
600
601 double value = 100.0;
602 bool ret = ptJson.Add(nullptr, value);
603 EXPECT_FALSE(ret);
604 }
605
606 /*
607 * @tc.name: Add_0500
608 * @tc.desc: Add
609 * @tc.type: FUNC
610 */
611 HWTEST_F(PtJsonTest, Add_0500, Function | MediumTest | Level1)
612 {
613 cJSON *cjson = new cJSON();
614 OHOS::AppPackingTool::PtJson ptJson(cjson);
615
616 bool ret = ptJson.Add(nullptr, "value");
617 EXPECT_FALSE(ret);
618
619 ret = ptJson.Add("addd", nullptr);
620 EXPECT_FALSE(ret);
621
622 ptJson.Add("nullptr", "nullptr");
623 ret = ptJson.Add("nullptr", "nullptr");
624 EXPECT_FALSE(ret);
625 }
626
627 /*
628 * @tc.name: Add_0700
629 * @tc.desc: Add
630 * @tc.type: FUNC
631 */
632 HWTEST_F(PtJsonTest, Add_0700, Function | MediumTest | Level1)
633 {
634 cJSON *cjson = new cJSON();
635 OHOS::AppPackingTool::PtJson ptJson(cjson);
636
637 std::unique_ptr<AppPackingTool::PtJson> value = std::make_unique<AppPackingTool::PtJson>();
638 bool ret = ptJson.Add(nullptr, value);
639 EXPECT_FALSE(ret);
640
641 ret = ptJson.Add("aaa", value);
642 EXPECT_FALSE(ret);
643
644 ptJson.Add("bbb", value);
645 ret = ptJson.Add("bbb", value);
646 EXPECT_FALSE(ret);
647 }
648
649 /*
650 * @tc.name: Push_0100
651 * @tc.desc: Push
652 * @tc.type: FUNC
653 */
654 HWTEST_F(PtJsonTest, Push_0100, Function | MediumTest | Level1)
655 {
656 cJSON *cjson = new cJSON();
657 OHOS::AppPackingTool::PtJson ptJson(cjson);
658
659 int64_t value = 100;
660 bool ret = ptJson.Push(value);
661 EXPECT_TRUE(ret);
662 }
663
664 /*
665 * @tc.name: Push_0200
666 * @tc.desc: Push
667 * @tc.type: FUNC
668 */
669 HWTEST_F(PtJsonTest, Push_0200, Function | MediumTest | Level1)
670 {
671 cJSON *cjson = new cJSON();
672 OHOS::AppPackingTool::PtJson ptJson(cjson);
673
674 uint32_t value = 100;
675 bool ret = ptJson.Push(value);
676 EXPECT_TRUE(ret);
677 }
678
679 /*
680 * @tc.name: Push_0300
681 * @tc.desc: Push
682 * @tc.type: FUNC
683 */
684 HWTEST_F(PtJsonTest, Push_0300, Function | MediumTest | Level1)
685 {
686 cJSON *cjson = new cJSON();
687 OHOS::AppPackingTool::PtJson ptJson(cjson);
688
689 cJSON *node = nullptr;
690 bool ret = ptJson.Push(node);
691 EXPECT_FALSE(ret);
692 }
693
694 /*
695 * @tc.name: Push_0400
696 * @tc.desc: Push
697 * @tc.type: FUNC
698 */
699 HWTEST_F(PtJsonTest, Push_0400, Function | MediumTest | Level1)
700 {
701 cJSON *cjson = new cJSON();
702 OHOS::AppPackingTool::PtJson ptJson(cjson);
703
704 std::unique_ptr<AppPackingTool::PtJson> value = nullptr;
705 bool ret = ptJson.Push(value);
706 EXPECT_FALSE(ret);
707 }
708
709 /*
710 * @tc.name: Remove_0100
711 * @tc.desc: Remove
712 * @tc.type: FUNC
713 */
714 HWTEST_F(PtJsonTest, Remove_0100, Function | MediumTest | Level1)
715 {
716 cJSON *cjson = new cJSON();
717 OHOS::AppPackingTool::PtJson ptJson(cjson);
718
719 bool ret = ptJson.Remove(nullptr);
720 EXPECT_FALSE(ret);
721 }
722
723 /*
724 * @tc.name: GetKey_0100
725 * @tc.desc: GetKey
726 * @tc.type: FUNC
727 */
728 HWTEST_F(PtJsonTest, GetKey_0100, Function | MediumTest | Level1)
729 {
730 cJSON *cjson = new cJSON();
731 OHOS::AppPackingTool::PtJson ptJson(cjson);
732
733 std::string ret = ptJson.GetKey();
734 EXPECT_EQ(ret, "");
735 }
736
737 /*
738 * @tc.name: GetBool_0100
739 * @tc.desc: GetBool
740 * @tc.type: FUNC
741 */
742 HWTEST_F(PtJsonTest, GetBool_0100, Function | MediumTest | Level1)
743 {
744 OHOS::AppPackingTool::PtJson ptJson;
745
746 bool ret = ptJson.GetBool(true);
747 EXPECT_TRUE(ret);
748 }
749
750 /*
751 * @tc.name: GetInt64_0100
752 * @tc.desc: GetInt64
753 * @tc.type: FUNC
754 */
755 HWTEST_F(PtJsonTest, GetInt64_0100, Function | MediumTest | Level1)
756 {
757 OHOS::AppPackingTool::PtJson ptJson;
758
759 int64_t value = 100;
760 int64_t ret = ptJson.GetInt64(value);
761 EXPECT_EQ(ret, value);
762 }
763
764 /*
765 * @tc.name: GetUInt_0100
766 * @tc.desc: GetUInt
767 * @tc.type: FUNC
768 */
769 HWTEST_F(PtJsonTest, GetUInt_0100, Function | MediumTest | Level1)
770 {
771 OHOS::AppPackingTool::PtJson ptJson;
772
773 uint32_t value = 100;
774 uint32_t ret = ptJson.GetUInt(value);
775 EXPECT_EQ(ret, value);
776 }
777
778 /*
779 * @tc.name: GetUInt64_0100
780 * @tc.desc: GetUInt64
781 * @tc.type: FUNC
782 */
783 HWTEST_F(PtJsonTest, GetUInt64_0100, Function | MediumTest | Level1)
784 {
785 OHOS::AppPackingTool::PtJson ptJson;
786
787 uint64_t value = 100;
788 uint64_t ret = ptJson.GetUInt64(value);
789 EXPECT_EQ(ret, value);
790 }
791
792 /*
793 * @tc.name: GetDouble_0100
794 * @tc.desc: GetDouble
795 * @tc.type: FUNC
796 */
797 HWTEST_F(PtJsonTest, GetDouble_0100, Function | MediumTest | Level1)
798 {
799 OHOS::AppPackingTool::PtJson ptJson;
800
801 double value = 100;
802 double ret = ptJson.GetDouble(value);
803 EXPECT_EQ(ret, value);
804 }
805
806 /*
807 * @tc.name: GetString_0100
808 * @tc.desc: GetString
809 * @tc.type: FUNC
810 */
811 HWTEST_F(PtJsonTest, GetString_0100, Function | MediumTest | Level1)
812 {
813 OHOS::AppPackingTool::PtJson ptJson;
814
815 std::string ret = ptJson.GetString();
816 EXPECT_EQ(ret, "");
817 }
818
819 /*
820 * @tc.name: GetBool_0200
821 * @tc.desc: GetBool
822 * @tc.type: FUNC
823 */
824 HWTEST_F(PtJsonTest, GetBool_0200, Function | MediumTest | Level1)
825 {
826 cJSON *cjson = new cJSON();
827 OHOS::AppPackingTool::PtJson ptJson(cjson);
828
829 bool * value = new bool();
830 *value = true;
831 AppPackingTool::Result ret = ptJson.GetBool("key", value);
832 EXPECT_EQ(ret, AppPackingTool::Result::NOT_EXIST);
833
834 ptJson.Add("key", "value");
835 ret = ptJson.GetBool("key", value);
836 EXPECT_EQ(ret, AppPackingTool::Result::TYPE_ERROR);
837 }
838
839 /*
840 * @tc.name: SetBool_0100
841 * @tc.desc: SetBool
842 * @tc.type: FUNC
843 */
844 HWTEST_F(PtJsonTest, SetBool_0100, Function | MediumTest | Level1)
845 {
846 cJSON *cjson = new cJSON();
847 OHOS::AppPackingTool::PtJson ptJson(cjson);
848
849 bool value = true;
850 AppPackingTool::Result ret = ptJson.SetBool("key", value);
851 EXPECT_EQ(ret, AppPackingTool::Result::NOT_EXIST);
852 }
853
854 /*
855 * @tc.name: SetInt_0100
856 * @tc.desc: SetInt
857 * @tc.type: FUNC
858 */
859 HWTEST_F(PtJsonTest, SetInt_0100, Function | MediumTest | Level1)
860 {
861 cJSON *cjson = new cJSON();
862 OHOS::AppPackingTool::PtJson ptJson(cjson);
863
864 int32_t value = 100;
865 AppPackingTool::Result ret = ptJson.SetInt("key", value);
866 EXPECT_EQ(ret, AppPackingTool::Result::NOT_EXIST);
867 }
868
869 /*
870 * @tc.name: SetInt64_0100
871 * @tc.desc: SetInt64
872 * @tc.type: FUNC
873 */
874 HWTEST_F(PtJsonTest, SetInt64_0100, Function | MediumTest | Level1)
875 {
876 cJSON *cjson = new cJSON();
877 OHOS::AppPackingTool::PtJson ptJson(cjson);
878
879 int32_t value = 100;
880 AppPackingTool::Result ret = ptJson.SetInt64("key", value);
881 EXPECT_EQ(ret, AppPackingTool::Result::NOT_EXIST);
882 }
883
884 /*
885 * @tc.name: SetUInt_0100
886 * @tc.desc: SetUInt
887 * @tc.type: FUNC
888 */
889 HWTEST_F(PtJsonTest, SetUInt_0100, Function | MediumTest | Level1)
890 {
891 cJSON *cjson = new cJSON();
892 OHOS::AppPackingTool::PtJson ptJson(cjson);
893
894 uint32_t value = 100;
895 AppPackingTool::Result ret = ptJson.SetUInt("key", value);
896 EXPECT_EQ(ret, AppPackingTool::Result::NOT_EXIST);
897 }
898
899 /*
900 * @tc.name: SetUInt64_0100
901 * @tc.desc: SetUInt64
902 * @tc.type: FUNC
903 */
904 HWTEST_F(PtJsonTest, SetUInt64_0100, Function | MediumTest | Level1)
905 {
906 cJSON *cjson = new cJSON();
907 OHOS::AppPackingTool::PtJson ptJson(cjson);
908
909 uint32_t value = 100;
910 AppPackingTool::Result ret = ptJson.SetUInt64("key", value);
911 EXPECT_EQ(ret, AppPackingTool::Result::NOT_EXIST);
912 }
913
914 /*
915 * @tc.name: GetDouble_0200
916 * @tc.desc: GetDouble
917 * @tc.type: FUNC
918 */
919 HWTEST_F(PtJsonTest, GetDouble_0200, Function | MediumTest | Level1)
920 {
921 cJSON *cjson = new cJSON();
922 OHOS::AppPackingTool::PtJson ptJson(cjson);
923
924 double *value = new double();
925 *value = 100.0;
926 AppPackingTool::Result ret = ptJson.GetDouble("key", value);
927 EXPECT_EQ(ret, AppPackingTool::Result::NOT_EXIST);
928
929 ptJson.Add("key", "value");
930 ret = ptJson.GetDouble("key", value);
931 EXPECT_EQ(ret, AppPackingTool::Result::TYPE_ERROR);
932 }
933
934 /*
935 * @tc.name: SetDouble_0100
936 * @tc.desc: SetDouble
937 * @tc.type: FUNC
938 */
939 HWTEST_F(PtJsonTest, SetDouble_0100, Function | MediumTest | Level1)
940 {
941 cJSON *cjson = new cJSON();
942 OHOS::AppPackingTool::PtJson ptJson(cjson);
943
944 double value = 100.0;
945 AppPackingTool::Result ret = ptJson.SetDouble("key", value);
946 EXPECT_EQ(ret, AppPackingTool::Result::NOT_EXIST);
947 }
948
949 /*
950 * @tc.name: GetString_0200
951 * @tc.desc: GetString
952 * @tc.type: FUNC
953 */
954 HWTEST_F(PtJsonTest, GetString_0200, Function | MediumTest | Level1)
955 {
956 cJSON *cjson = new cJSON();
957 OHOS::AppPackingTool::PtJson ptJson(cjson);
958
959 std::string *value = new std::string();
960 *value = "value";
961 AppPackingTool::Result ret = ptJson.GetString("key", value);
962 EXPECT_EQ(ret, AppPackingTool::Result::NOT_EXIST);
963
964 ptJson.Add("key", true);
965 ret = ptJson.GetString("key", value);
966 EXPECT_EQ(ret, AppPackingTool::Result::TYPE_ERROR);
967 }
968
969 /*
970 * @tc.name: SetString_0100
971 * @tc.desc: SetString
972 * @tc.type: FUNC
973 */
974 HWTEST_F(PtJsonTest, SetString_0100, Function | MediumTest | Level1)
975 {
976 cJSON *cjson = new cJSON();
977 OHOS::AppPackingTool::PtJson ptJson(cjson);
978
979 std::string value = "value";
980 AppPackingTool::Result ret = ptJson.SetString("key", value);
981 EXPECT_EQ(ret, AppPackingTool::Result::NOT_EXIST);
982
983 ptJson.Add("key", true);
984 ret = ptJson.SetString("key", value);
985 EXPECT_EQ(ret, AppPackingTool::Result::TYPE_ERROR);
986 }
987
988 /*
989 * @tc.name: GetObject_0100
990 * @tc.desc: GetObject
991 * @tc.type: FUNC
992 */
993 HWTEST_F(PtJsonTest, GetObject_0100, Function | MediumTest | Level1)
994 {
995 cJSON *cjson = new cJSON();
996 OHOS::AppPackingTool::PtJson ptJson(cjson);
997
998 std::unique_ptr<AppPackingTool::PtJson> *value = new std::unique_ptr<AppPackingTool::PtJson>();
999 AppPackingTool::Result ret = ptJson.GetObject("key", value);
1000 EXPECT_EQ(ret, AppPackingTool::Result::NOT_EXIST);
1001 }
1002
1003 /*
1004 * @tc.name: GetArray_0100
1005 * @tc.desc: GetArray
1006 * @tc.type: FUNC
1007 */
1008 HWTEST_F(PtJsonTest, GetArray_0100, Function | MediumTest | Level1)
1009 {
1010 cJSON *cjson = new cJSON();
1011 OHOS::AppPackingTool::PtJson ptJson(cjson);
1012
1013 std::unique_ptr<AppPackingTool::PtJson> *value = new std::unique_ptr<AppPackingTool::PtJson>();
1014 AppPackingTool::Result ret = ptJson.GetArray("key", value);
1015 EXPECT_EQ(ret, AppPackingTool::Result::NOT_EXIST);
1016
1017 ptJson.Add("key", true);
1018 ret = ptJson.GetArray("key", value);
1019 EXPECT_EQ(ret, AppPackingTool::Result::TYPE_ERROR);
1020 }
1021
1022 /*
1023 * @tc.name: GetAny_0100
1024 * @tc.desc: GetAny
1025 * @tc.type: FUNC
1026 */
1027 HWTEST_F(PtJsonTest, GetAny_0100, Function | MediumTest | Level1)
1028 {
1029 cJSON *cjson = new cJSON();
1030 OHOS::AppPackingTool::PtJson ptJson(cjson);
1031
1032 std::unique_ptr<AppPackingTool::PtJson> *value = new std::unique_ptr<AppPackingTool::PtJson>();
1033 AppPackingTool::Result ret = ptJson.GetAny("key", value);
1034 EXPECT_EQ(ret, AppPackingTool::Result::NOT_EXIST);
1035 }
1036
1037 /*
1038 * @tc.name: SetArray_0100
1039 * @tc.desc: SetArray.
1040 * @tc.type: FUNC
1041 * @tc.require:
1042 */
1043 HWTEST_F(PtJsonTest, SetArray_0100, Function | MediumTest | Level1)
1044 {
1045 cJSON *cjson = new cJSON();
1046 OHOS::AppPackingTool::PtJson ptJson(cjson);
1047 std::list<std::string> value;
1048 EXPECT_EQ(ptJson.SetArray("AAA", value), OHOS::AppPackingTool::Result::NOT_EXIST);
1049
1050 ptJson.Add("AAA", true);
1051 EXPECT_EQ(ptJson.SetArray("AAA", value), OHOS::AppPackingTool::Result::TYPE_ERROR);
1052
1053 value.push_back("123");
1054 value.push_back("456");
1055 ptJson.Add("BBB", value);
1056 EXPECT_EQ(ptJson.SetArray("BBB", value), OHOS::AppPackingTool::Result::SUCCESS);
1057 }
1058 } // namespace OHOS
1059