1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <gtest/gtest.h> 17 18 #include "updateservice_json_utils.h" 19 20 using namespace testing::ext; 21 using namespace testing; 22 23 namespace OHOS::UpdateService { 24 class UpdateServiceJsonUtilsTest : public testing::Test { 25 public: SetupTestCase(void)26 static void SetupTestCase(void) {}; TearDownTestCase(void)27 static void TearDownTestCase(void) {}; SetUp()28 void SetUp() final {}; TearDown()29 void TearDown() final {}; 30 }; 31 32 /** 33 * @tc.name: ParseAndGetJsonObjectTrue 34 * @tc.desc: Test json string is true 35 * @tc.type: FUNC 36 */ 37 HWTEST_F(UpdateServiceJsonUtilsTest, ParseAndGetJsonObjectTrue, TestSize.Level1) 38 { 39 std::string jsonStr = "{\"name\":\"Alice\", \"age\":25}"; 40 cJSON *root = UpdateServiceJsonUtils::ParseAndGetJsonObject(jsonStr); 41 EXPECT_NE(nullptr, root); 42 cJSON_Delete(root); 43 } 44 45 /** 46 * @tc.name: ParseAndGetJsonObjectFalse 47 * @tc.desc: Test json string is false 48 * @tc.type: FUNC 49 */ 50 HWTEST_F(UpdateServiceJsonUtilsTest, ParseAndGetJsonObjectFalse, TestSize.Level1) 51 { 52 std::string jsonStr = "{\"name\":\"Alice\", \"age\":25, abc}"; 53 cJSON *root = UpdateServiceJsonUtils::ParseAndGetJsonObject(jsonStr); 54 EXPECT_EQ(nullptr, root); 55 } 56 57 /** 58 * @tc.name: ParseJsonTrue 59 * @tc.desc: Test json string is true 60 * @tc.type: FUNC 61 */ 62 HWTEST_F(UpdateServiceJsonUtilsTest, ParseJsonTrue, TestSize.Level1) 63 { 64 std::string jsonStr = "{\"name\":\"Alice\", \"age\":25}"; 65 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 66 EXPECT_NE(nullptr, root); 67 } 68 69 /** 70 * @tc.name: ParseJsonFalse 71 * @tc.desc: Test json string is true 72 * @tc.type: FUNC 73 */ 74 HWTEST_F(UpdateServiceJsonUtilsTest, ParseJsonFalse, TestSize.Level1) 75 { 76 std::string jsonStr = "{\"name\":\"Alice\", \"age\":25, xxx}"; 77 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 78 EXPECT_EQ(nullptr, root); 79 } 80 81 /** 82 * @tc.name: GetValueAndSetToNullptr 83 * @tc.desc: Test json string, check root is nullptr 84 * @tc.type: FUNC 85 */ 86 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToNullptr, TestSize.Level1) 87 { 88 std::string jsonStr = "{\"age\": 54, \"file\": \"a.txt\", xxx}"; 89 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 90 int32_t ageNumber; 91 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); 92 EXPECT_EQ(CAST_INT(JsonParseError::MISSING_PROP), ret); 93 } 94 95 /** 96 * @tc.name: GetValueAndSetToKeyNullptr 97 * @tc.desc: Test json string, key is nullptr 98 * @tc.type: FUNC 99 */ 100 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToKeyNullptr, TestSize.Level1) 101 { 102 std::string jsonStr = "{\"age\": 54, \"file\": \"a.txt\"}"; 103 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 104 int32_t ageNumber; 105 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "", ageNumber); 106 EXPECT_EQ(CAST_INT(JsonParseError::MISSING_PROP), ret); 107 } 108 109 /** 110 * @tc.name: GetValueAndSetToKeyNotFound 111 * @tc.desc: Test json string, key not found 112 * @tc.type: FUNC 113 */ 114 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToKeyNotFound, TestSize.Level1) 115 { 116 std::string jsonStr = "{\"age\": 54, \"file\": \"a.txt\"}"; 117 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 118 int32_t ageNumber; 119 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "book", ageNumber); 120 EXPECT_EQ(CAST_INT(JsonParseError::MISSING_PROP), ret); 121 } 122 123 /** 124 * @tc.name: GetValueAndSetToStringTypeErr 125 * @tc.desc: Test json string, get string type error 126 * @tc.type: FUNC 127 */ 128 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToStringTypeErr, TestSize.Level1) 129 { 130 std::string jsonStr = "{\"age\": 54, \"file\": 1234}"; 131 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 132 std::string fileName; 133 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "file", fileName); 134 EXPECT_EQ(CAST_INT(JsonParseError::TYPE_ERROR), ret); 135 } 136 137 /** 138 * @tc.name: GetValueAndSetToStringOk 139 * @tc.desc: Test json string, get string ok 140 * @tc.type: FUNC 141 */ 142 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToStringOk, TestSize.Level1) 143 { 144 std::string jsonStr = "{\"age\": 54, \"file\": \"a.txt\"}"; 145 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 146 std::string fileName; 147 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "file", fileName); 148 EXPECT_EQ(CAST_INT(JsonParseError::ERR_OK), ret); 149 EXPECT_EQ("a.txt", fileName); 150 } 151 152 /** 153 * @tc.name: GetValueAndSetToBoolTypeErr 154 * @tc.desc: Test json string, get Bool type error 155 * @tc.type: FUNC 156 */ 157 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToBoolTypeErr, TestSize.Level1) 158 { 159 std::string jsonStr = "{\"age\": 54, \"boolFlag\": 123}"; 160 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 161 bool boolFlag; 162 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "boolFlag", boolFlag); 163 EXPECT_EQ(CAST_INT(JsonParseError::TYPE_ERROR), ret); 164 } 165 166 /** 167 * @tc.name: GetValueAndSetToBoolTypeOk 168 * @tc.desc: Test json string, get Bool type ok 169 * @tc.type: FUNC 170 */ 171 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToBoolTypeOk, TestSize.Level1) 172 { 173 std::string jsonStr = "{\"age\": 54, \"boolFlag\": false}"; 174 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 175 bool boolFlag; 176 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "boolFlag", boolFlag); 177 EXPECT_EQ(CAST_INT(JsonParseError::ERR_OK), ret); 178 EXPECT_EQ(false, boolFlag); 179 } 180 181 /** 182 * @tc.name: GetValueAndSetToInt32TypeError 183 * @tc.desc: Test json string, int32_t type error 184 * @tc.type: FUNC 185 */ 186 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToInt32TypeError, TestSize.Level1) 187 { 188 std::string jsonStr = "{\"age\": 54.22, \"file\": \"a.txt\"}"; 189 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 190 int32_t ageNumber; 191 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); 192 EXPECT_EQ(CAST_INT(JsonParseError::TYPE_ERROR), ret); 193 } 194 195 /** 196 * @tc.name: GetValueAndSetToInt32Ok 197 * @tc.desc: Test json string, get int32_t ok 198 * @tc.type: FUNC 199 */ 200 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToInt32Ok, TestSize.Level1) 201 { 202 std::string jsonStr = "{\"age\": 54, \"file\": \"a.txt\"}"; 203 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 204 int32_t ageNumber; 205 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); 206 EXPECT_EQ(CAST_INT(JsonParseError::ERR_OK), ret); 207 EXPECT_EQ(54, ageNumber); 208 } 209 210 /** 211 * @tc.name: GetValueAndSetToInt64TypeError 212 * @tc.desc: Test json string, int64_t type error 213 * @tc.type: FUNC 214 */ 215 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToInt64TypeError, TestSize.Level1) 216 { 217 std::string jsonStr = "{\"age\": 541212121.44, \"file\": \"a.txt\"}"; 218 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 219 int64_t ageNumber; 220 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); 221 EXPECT_EQ(CAST_INT(JsonParseError::TYPE_ERROR), ret); 222 } 223 224 /** 225 * @tc.name: GetValueAndSetToInt64Ok 226 * @tc.desc: Test json string, get int64_t ok 227 * @tc.type: FUNC 228 */ 229 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToInt64Ok, TestSize.Level1) 230 { 231 std::string jsonStr = "{\"age\": 54121212123, \"file\": \"a.txt\"}"; 232 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 233 int64_t ageNumber; 234 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); 235 EXPECT_EQ(CAST_INT(JsonParseError::ERR_OK), ret); 236 EXPECT_EQ(54121212123, ageNumber); 237 } 238 239 /** 240 * @tc.name: GetValueAndSetToUInt32TypeError 241 * @tc.desc: Test json string, uint32_t type error 242 * @tc.type: FUNC 243 */ 244 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToUInt32TypeError, TestSize.Level1) 245 { 246 std::string jsonStr = "{\"age\": -111, \"file\": \"a.txt\"}"; 247 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 248 uint32_t ageNumber; 249 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); 250 EXPECT_EQ(CAST_INT(JsonParseError::TYPE_ERROR), ret); 251 } 252 253 /** 254 * @tc.name: GetValueAndSetToUInt32Ok 255 * @tc.desc: Test json string, get uint32_t ok 256 * @tc.type: FUNC 257 */ 258 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToUInt32Ok, TestSize.Level1) 259 { 260 std::string jsonStr = "{\"age\": 12, \"file\": \"a.txt\"}"; 261 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 262 uint32_t ageNumber; 263 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); 264 EXPECT_EQ(CAST_INT(JsonParseError::ERR_OK), ret); 265 EXPECT_EQ(12, ageNumber); 266 } 267 268 /** 269 * @tc.name: GetValueAndSetToUInt64TypeError 270 * @tc.desc: Test json string, uint64_t type error 271 * @tc.type: FUNC 272 */ 273 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToUInt64TypeError, TestSize.Level1) 274 { 275 std::string jsonStr = "{\"age\": -100, \"file\": \"a.txt\"}"; 276 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 277 uint64_t ageNumber; 278 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); 279 EXPECT_EQ(CAST_INT(JsonParseError::TYPE_ERROR), ret); 280 } 281 282 /** 283 * @tc.name: GetValueAndSetToUInt64Ok 284 * @tc.desc: Test json string, get uint64_t ok 285 * @tc.type: FUNC 286 */ 287 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToUInt64Ok, TestSize.Level1) 288 { 289 std::string jsonStr = "{\"age\": 1212121211255, \"file\": \"a.txt\"}"; 290 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 291 uint64_t ageNumber; 292 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); 293 EXPECT_EQ(CAST_INT(JsonParseError::ERR_OK), ret); 294 EXPECT_EQ(1212121211255, ageNumber); 295 } 296 297 /** 298 * @tc.name: GetValueAndSetToDoubleTypeError 299 * @tc.desc: Test json string, Double type error 300 * @tc.type: FUNC 301 */ 302 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToDoubleTypeError, TestSize.Level1) 303 { 304 std::string jsonStr = "{\"age\": \"NAN\", \"file\": \"a.txt\"}"; 305 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 306 double ageNumber; 307 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); 308 EXPECT_EQ(CAST_INT(JsonParseError::TYPE_ERROR), ret); 309 } 310 311 /** 312 * @tc.name: GetValueAndSetToDoubleOk 313 * @tc.desc: Test json string, get double ok 314 * @tc.type: FUNC 315 */ 316 HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToDoubleOk, TestSize.Level1) 317 { 318 std::string jsonStr = "{\"age\": 1212121211255.99, \"file\": \"a.txt\"}"; 319 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 320 double ageNumber; 321 int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); 322 EXPECT_EQ(CAST_INT(JsonParseError::ERR_OK), ret); 323 EXPECT_EQ(1212121211255.99, ageNumber); 324 } 325 326 /** 327 * @tc.name: IsIntegerTrue 328 * @tc.desc: check isInteger true 329 * @tc.type: FUNC 330 */ 331 HWTEST_F(UpdateServiceJsonUtilsTest, IsIntegerTrue, TestSize.Level1) 332 { 333 double d = 100; 334 bool bl = UpdateServiceJsonUtils::IsInteger(d); 335 EXPECT_EQ(true, bl); 336 } 337 338 /** 339 * @tc.name: IsIntegerFalse 340 * @tc.desc: check isInteger false 341 * @tc.type: FUNC 342 */ 343 HWTEST_F(UpdateServiceJsonUtilsTest, IsIntegerFalse, TestSize.Level1) 344 { 345 double d = 100.123; 346 bool bl = UpdateServiceJsonUtils::IsInteger(d); 347 EXPECT_EQ(false, bl); 348 } 349 350 /** 351 * @tc.name: CheckIntegerInt32True 352 * @tc.desc: test json string, check int32 true 353 * @tc.type: FUNC 354 */ 355 HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerInt32True, TestSize.Level1) 356 { 357 std::string jsonStr = "{\"age\": 123, \"file\": \"a.txt\"}"; 358 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 359 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 360 ASSERT_NE(nullptr, item); 361 int32_t ageNumber; 362 bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); 363 EXPECT_EQ(true, bl); 364 } 365 366 /** 367 * @tc.name: CheckIntegerInt32False 368 * @tc.desc: test json string, check int32 false 369 * @tc.type: FUNC 370 */ 371 HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerInt32False, TestSize.Level1) 372 { 373 std::string jsonStr = "{\"age\": 123.11, \"file\": \"a.txt\"}"; 374 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 375 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 376 ASSERT_NE(nullptr, item); 377 int32_t ageNumber; 378 bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); 379 EXPECT_EQ(false, bl); 380 } 381 382 /** 383 * @tc.name: CheckIntegerInt64True 384 * @tc.desc: test json string, check int64 true 385 * @tc.type: FUNC 386 */ 387 HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerInt64True, TestSize.Level1) 388 { 389 std::string jsonStr = "{\"age\": 123454545, \"file\": \"a.txt\"}"; 390 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 391 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 392 ASSERT_NE(nullptr, item); 393 int64_t ageNumber; 394 bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); 395 EXPECT_EQ(true, bl); 396 } 397 398 /** 399 * @tc.name: CheckIntegerInt64False 400 * @tc.desc: test json string, check int64 false 401 * @tc.type: FUNC 402 */ 403 HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerInt64False, TestSize.Level1) 404 { 405 std::string jsonStr = "{\"age\": 123.11111, \"file\": \"a.txt\"}"; 406 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 407 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 408 ASSERT_NE(nullptr, item); 409 int64_t ageNumber; 410 bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); 411 EXPECT_EQ(false, bl); 412 } 413 414 /** 415 * @tc.name: CheckIntegerUInt32True 416 * @tc.desc: test json string, check uint32 true 417 * @tc.type: FUNC 418 */ 419 HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerUInt32True, TestSize.Level1) 420 { 421 std::string jsonStr = "{\"age\": 123, \"file\": \"a.txt\"}"; 422 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 423 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 424 ASSERT_NE(nullptr, item); 425 uint32_t ageNumber; 426 bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); 427 EXPECT_EQ(true, bl); 428 } 429 430 /** 431 * @tc.name: CheckIntegerUInt32False 432 * @tc.desc: test json string, check uint32 false 433 * @tc.type: FUNC 434 */ 435 HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerUInt32False, TestSize.Level1) 436 { 437 std::string jsonStr = "{\"age\": -111, \"file\": \"a.txt\"}"; 438 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 439 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 440 ASSERT_NE(nullptr, item); 441 uint32_t ageNumber; 442 bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); 443 EXPECT_EQ(false, bl); 444 } 445 446 447 /** 448 * @tc.name: CheckIntegerUInt64True 449 * @tc.desc: test json string, check uint64 true 450 * @tc.type: FUNC 451 */ 452 HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerUInt64True, TestSize.Level1) 453 { 454 std::string jsonStr = "{\"age\": 123, \"file\": \"a.txt\"}"; 455 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 456 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 457 ASSERT_NE(nullptr, item); 458 uint64_t ageNumber; 459 bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); 460 EXPECT_EQ(true, bl); 461 } 462 463 /** 464 * @tc.name: CheckIntegerUInt64False 465 * @tc.desc: test json string, check uint64 false 466 * @tc.type: FUNC 467 */ 468 HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerUInt64False, TestSize.Level1) 469 { 470 std::string jsonStr = "{\"age\": -111, \"file\": \"a.txt\"}"; 471 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 472 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 473 ASSERT_NE(nullptr, item); 474 uint64_t ageNumber; 475 bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); 476 EXPECT_EQ(false, bl); 477 } 478 479 //CheckTypeAndAsign 480 /** 481 * @tc.name: CheckTypeAndAsignStringError 482 * @tc.desc: test json string, check String is error 483 * @tc.type: FUNC 484 */ 485 HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignStringError, TestSize.Level1) 486 { 487 std::string jsonStr = "{\"age\": -111, \"file\": 1234}"; 488 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 489 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "file"); 490 ASSERT_NE(nullptr, item); 491 std::string fileName; 492 bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, fileName); 493 EXPECT_EQ(false, bl); 494 } 495 496 /** 497 * @tc.name: CheckTypeAndAsignStringOk 498 * @tc.desc: test json string, check String is ok 499 * @tc.type: FUNC 500 */ 501 HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignStringOk, TestSize.Level1) 502 { 503 std::string jsonStr = "{\"age\": -111, \"file\": \"1234.txt\"}"; 504 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 505 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "file"); 506 ASSERT_NE(nullptr, item); 507 std::string fileName; 508 bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, fileName); 509 EXPECT_EQ(true, bl); 510 EXPECT_EQ("1234.txt", fileName); 511 } 512 513 /** 514 * @tc.name: CheckTypeAndAsignBoolError 515 * @tc.desc: test json string, check bool is error 516 * @tc.type: FUNC 517 */ 518 HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignBoolError, TestSize.Level1) 519 { 520 std::string jsonStr = "{\"age\": 54, \"boolFlag\": 123}"; 521 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 522 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "boolFlag"); 523 ASSERT_NE(nullptr, item); 524 bool boolFlag; 525 bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, boolFlag); 526 EXPECT_EQ(false, bl); 527 } 528 529 /** 530 * @tc.name: CheckTypeAndAsignBoolOk 531 * @tc.desc: test json string, check bool is ok 532 * @tc.type: FUNC 533 */ 534 HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignBoolOk, TestSize.Level1) 535 { 536 std::string jsonStr = "{\"age\": 54, \"boolFlag\": true}"; 537 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 538 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "boolFlag"); 539 ASSERT_NE(nullptr, item); 540 bool boolFlag = false; 541 bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, boolFlag); 542 EXPECT_EQ(true, bl); 543 EXPECT_EQ(true, bl); 544 } 545 546 /** 547 * @tc.name: CheckTypeAndAsignInt32True 548 * @tc.desc: test json string, check int32 true 549 * @tc.type: FUNC 550 */ 551 HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignInt32True, TestSize.Level1) 552 { 553 std::string jsonStr = "{\"age\": 123, \"file\": \"a.txt\"}"; 554 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 555 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 556 ASSERT_NE(nullptr, item); 557 int32_t ageNumber; 558 bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); 559 EXPECT_EQ(true, bl); 560 EXPECT_EQ(123, ageNumber); 561 } 562 563 /** 564 * @tc.name: CheckTypeAndAsignInt32False 565 * @tc.desc: test json string, check int32 false 566 * @tc.type: FUNC 567 */ 568 HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignInt32False, TestSize.Level1) 569 { 570 std::string jsonStr = "{\"age\": 123.11, \"file\": \"a.txt\"}"; 571 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 572 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 573 ASSERT_NE(nullptr, item); 574 int32_t ageNumber; 575 bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); 576 EXPECT_EQ(false, bl); 577 } 578 579 /** 580 * @tc.name: CheckTypeAndAsignInt64True 581 * @tc.desc: test json string, check int64 true 582 * @tc.type: FUNC 583 */ 584 HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignInt64True, TestSize.Level1) 585 { 586 std::string jsonStr = "{\"age\": 123454545, \"file\": \"a.txt\"}"; 587 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 588 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 589 ASSERT_NE(nullptr, item); 590 int64_t ageNumber; 591 bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); 592 EXPECT_EQ(true, bl); 593 EXPECT_EQ(123454545, ageNumber); 594 } 595 596 /** 597 * @tc.name: CheckTypeAndAsignInt64False 598 * @tc.desc: test json string, check int64 false 599 * @tc.type: FUNC 600 */ 601 HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignInt64False, TestSize.Level1) 602 { 603 std::string jsonStr = "{\"age\": 123.11111, \"file\": \"a.txt\"}"; 604 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 605 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 606 ASSERT_NE(nullptr, item); 607 int64_t ageNumber; 608 bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); 609 EXPECT_EQ(false, bl); 610 } 611 612 /** 613 * @tc.name: CheckTypeAndAsignUInt32True 614 * @tc.desc: test json string, check uint32 true 615 * @tc.type: FUNC 616 */ 617 HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignUInt32True, TestSize.Level1) 618 { 619 std::string jsonStr = "{\"age\": 123, \"file\": \"a.txt\"}"; 620 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 621 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 622 ASSERT_NE(nullptr, item); 623 uint32_t ageNumber; 624 bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); 625 EXPECT_EQ(true, bl); 626 EXPECT_EQ(123, ageNumber); 627 } 628 629 /** 630 * @tc.name: CheckTypeAndAsignUInt32False 631 * @tc.desc: test json string, check uint32 false 632 * @tc.type: FUNC 633 */ 634 HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignUInt32False, TestSize.Level1) 635 { 636 std::string jsonStr = "{\"age\": -111, \"file\": \"a.txt\"}"; 637 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 638 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 639 ASSERT_NE(nullptr, item); 640 uint32_t ageNumber; 641 bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); 642 EXPECT_EQ(false, bl); 643 } 644 645 /** 646 * @tc.name: CheckTypeAndAsignUInt64True 647 * @tc.desc: test json string, check uint64 true 648 * @tc.type: FUNC 649 */ 650 HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignUInt64True, TestSize.Level1) 651 { 652 std::string jsonStr = "{\"age\": 12312121, \"file\": \"a.txt\"}"; 653 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 654 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 655 ASSERT_NE(nullptr, item); 656 uint64_t ageNumber; 657 bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); 658 EXPECT_EQ(true, bl); 659 EXPECT_EQ(12312121, ageNumber); 660 } 661 662 /** 663 * @tc.name: CheckTypeAndAsignUInt64False 664 * @tc.desc: test json string, check uint64 false 665 * @tc.type: FUNC 666 */ 667 HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignUInt64False, TestSize.Level1) 668 { 669 std::string jsonStr = "{\"age\": -111, \"file\": \"a.txt\"}"; 670 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 671 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 672 ASSERT_NE(nullptr, item); 673 uint64_t ageNumber; 674 bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); 675 EXPECT_EQ(false, bl); 676 } 677 678 /** 679 * @tc.name: CheckTypeAndAsignDoubleFalse 680 * @tc.desc: Test json string, Double type error 681 * @tc.type: FUNC 682 */ 683 HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignDoubleFalse, TestSize.Level1) 684 { 685 std::string jsonStr = "{\"age\": \"NAN\", \"file\": \"a.txt\"}"; 686 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 687 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 688 ASSERT_NE(nullptr, item); 689 double ageNumber; 690 bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); 691 EXPECT_EQ(false, bl); 692 } 693 694 /** 695 * @tc.name: CheckTypeAndAsignDoubleTrue 696 * @tc.desc: Test json string, get double ok 697 * @tc.type: FUNC 698 */ 699 HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignDoubleTrue, TestSize.Level1) 700 { 701 std::string jsonStr = "{\"age\": 1212121211255.99, \"file\": \"a.txt\"}"; 702 auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); 703 cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); 704 ASSERT_NE(nullptr, item); 705 double ageNumber; 706 bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); 707 EXPECT_EQ(true, bl); 708 EXPECT_EQ(1212121211255.99, ageNumber); 709 } 710 } // namespace OHOS::UpdateService