1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "spdk_cunit.h" 37 38 #include "json_util.c" 39 40 #define NUM_SETUP(x) \ 41 snprintf(buf, sizeof(buf), "%s", x); \ 42 v.type = SPDK_JSON_VAL_NUMBER; \ 43 v.start = buf; \ 44 v.len = sizeof(x) - 1 45 46 #define NUM_INT32_PASS(s, i) \ 47 NUM_SETUP(s); \ 48 CU_ASSERT(spdk_json_number_to_int32(&v, &i32) == 0); \ 49 CU_ASSERT(i32 == i) 50 51 #define NUM_INT32_FAIL(s) \ 52 NUM_SETUP(s); \ 53 CU_ASSERT(spdk_json_number_to_int32(&v, &i32) != 0) 54 55 #define NUM_UINT64_PASS(s, i) \ 56 NUM_SETUP(s); \ 57 CU_ASSERT(spdk_json_number_to_uint64(&v, &u64) == 0); \ 58 CU_ASSERT(u64 == i) 59 60 #define NUM_UINT64_FAIL(s) \ 61 NUM_SETUP(s); \ 62 CU_ASSERT(spdk_json_number_to_uint64(&v, &u64) != 0) 63 64 static void 65 test_strequal(void) 66 { 67 struct spdk_json_val v; 68 69 v.type = SPDK_JSON_VAL_STRING; 70 v.start = "test"; 71 v.len = sizeof("test") - 1; 72 CU_ASSERT(spdk_json_strequal(&v, "test") == true); 73 CU_ASSERT(spdk_json_strequal(&v, "TEST") == false); 74 CU_ASSERT(spdk_json_strequal(&v, "hello") == false); 75 CU_ASSERT(spdk_json_strequal(&v, "t") == false); 76 77 v.type = SPDK_JSON_VAL_NAME; 78 CU_ASSERT(spdk_json_strequal(&v, "test") == true); 79 80 v.type = SPDK_JSON_VAL_NUMBER; 81 CU_ASSERT(spdk_json_strequal(&v, "test") == false); 82 83 v.type = SPDK_JSON_VAL_STRING; 84 v.start = "test\0hello"; 85 v.len = sizeof("test\0hello") - 1; 86 CU_ASSERT(spdk_json_strequal(&v, "test") == false); 87 } 88 89 static void 90 test_num_to_int32(void) 91 { 92 struct spdk_json_val v; 93 char buf[100]; 94 int32_t i32 = 0; 95 96 NUM_SETUP("1234"); 97 CU_ASSERT(spdk_json_number_to_int32(&v, &i32) == 0); 98 CU_ASSERT(i32 == 1234); 99 100 101 NUM_INT32_PASS("0", 0); 102 NUM_INT32_PASS("1234", 1234); 103 NUM_INT32_PASS("-1234", -1234); 104 NUM_INT32_PASS("1234.00000", 1234); 105 NUM_INT32_PASS("1.2e1", 12); 106 NUM_INT32_PASS("12340e-1", 1234); 107 NUM_INT32_PASS("-0", 0); 108 109 NUM_INT32_FAIL("1.2"); 110 NUM_INT32_FAIL("1.2E0"); 111 NUM_INT32_FAIL("1.234e1"); 112 NUM_INT32_FAIL("12341e-1"); 113 } 114 115 static void 116 test_num_to_uint64(void) 117 { 118 struct spdk_json_val v; 119 char buf[100]; 120 uint64_t u64 = 0; 121 122 NUM_SETUP("1234"); 123 CU_ASSERT(spdk_json_number_to_uint64(&v, &u64) == 0); 124 CU_ASSERT(u64 == 1234); 125 126 127 NUM_UINT64_PASS("0", 0); 128 NUM_UINT64_PASS("1234", 1234); 129 NUM_UINT64_PASS("1234.00000", 1234); 130 NUM_UINT64_PASS("1.2e1", 12); 131 NUM_UINT64_PASS("12340e-1", 1234); 132 NUM_UINT64_PASS("123456780e-1", 12345678); 133 134 NUM_UINT64_FAIL("1.2"); 135 NUM_UINT64_FAIL("-1234"); 136 NUM_UINT64_FAIL("1.2E0"); 137 NUM_UINT64_FAIL("1.234e1"); 138 NUM_UINT64_FAIL("12341e-1"); 139 NUM_UINT64_FAIL("123456781e-1"); 140 } 141 142 static void 143 test_decode_object(void) 144 { 145 struct my_object { 146 char *my_name; 147 uint32_t my_int; 148 bool my_bool; 149 }; 150 struct spdk_json_val object[] = { 151 {"", 6, SPDK_JSON_VAL_OBJECT_BEGIN}, 152 {"first", 5, SPDK_JSON_VAL_NAME}, 153 {"HELLO", 5, SPDK_JSON_VAL_STRING}, 154 {"second", 6, SPDK_JSON_VAL_NAME}, 155 {"234", 3, SPDK_JSON_VAL_NUMBER}, 156 {"third", 5, SPDK_JSON_VAL_NAME}, 157 {"", 1, SPDK_JSON_VAL_TRUE}, 158 {"", 0, SPDK_JSON_VAL_OBJECT_END}, 159 }; 160 161 struct spdk_json_object_decoder decoders[] = { 162 {"first", offsetof(struct my_object, my_name), spdk_json_decode_string, false}, 163 {"second", offsetof(struct my_object, my_int), spdk_json_decode_uint32, false}, 164 {"third", offsetof(struct my_object, my_bool), spdk_json_decode_bool, false}, 165 {"fourth", offsetof(struct my_object, my_bool), spdk_json_decode_bool, true}, 166 }; 167 struct my_object output; 168 output.my_name = NULL; 169 uint32_t answer = 234; 170 char *answer_str = "HELLO"; 171 bool answer_bool = true; 172 173 /* Passing Test: object containing simple types */ 174 CU_ASSERT(spdk_json_decode_object(object, decoders, 4, &output) == 0); 175 CU_ASSERT(memcmp(output.my_name, answer_str, 6) == 0); 176 CU_ASSERT(output.my_int == answer); 177 CU_ASSERT(output.my_bool == answer_bool); 178 179 /* Failing Test: member with no matching decoder */ 180 /* i.e. I remove the matching decoder from the boolean argument */ 181 CU_ASSERT(spdk_json_decode_object(object, decoders, 2, &output) != 0); 182 183 /* Failing Test: non-optional decoder with no corresponding member */ 184 185 decoders[3].optional = false; 186 CU_ASSERT(spdk_json_decode_object(object, decoders, 4, &output) != 0); 187 188 /* return to base state */ 189 decoders[3].optional = true; 190 191 /* Failing Test: duplicated names for json values */ 192 object[3].start = "first"; 193 object[3].len = 5; 194 CU_ASSERT(spdk_json_decode_object(object, decoders, 3, &output) != 0); 195 196 /* return to base state */ 197 object[3].start = "second"; 198 object[3].len = 6; 199 200 /* Failing Test: invalid value for decoder */ 201 object[2].start = "HELO"; 202 CU_ASSERT(spdk_json_decode_object(object, decoders, 3, &output) != 0); 203 204 /* return to base state */ 205 object[2].start = "HELLO"; 206 207 /* Failing Test: not an object */ 208 object[0].type = SPDK_JSON_VAL_ARRAY_BEGIN; 209 CU_ASSERT(spdk_json_decode_object(object, decoders, 3, &output) != 0); 210 211 free(output.my_name); 212 } 213 214 static void 215 test_decode_array(void) 216 { 217 struct spdk_json_val values[4]; 218 uint32_t my_int[2] = {0, 0}; 219 char *my_string[2] = {NULL, NULL}; 220 size_t out_size; 221 222 /* passing integer test */ 223 values[0].type = SPDK_JSON_VAL_ARRAY_BEGIN; 224 values[0].len = 2; 225 values[1].type = SPDK_JSON_VAL_NUMBER; 226 values[1].len = 4; 227 values[1].start = "1234"; 228 values[2].type = SPDK_JSON_VAL_NUMBER; 229 values[2].len = 4; 230 values[2].start = "5678"; 231 values[3].type = SPDK_JSON_VAL_ARRAY_END; 232 CU_ASSERT(spdk_json_decode_array(values, spdk_json_decode_uint32, my_int, 2, &out_size, 233 sizeof(uint32_t)) == 0); 234 CU_ASSERT(my_int[0] == 1234); 235 CU_ASSERT(my_int[1] == 5678); 236 CU_ASSERT(out_size == 2); 237 238 /* array length exceeds max */ 239 values[0].len = 3; 240 CU_ASSERT(spdk_json_decode_array(values, spdk_json_decode_uint32, my_int, 2, &out_size, 241 sizeof(uint32_t)) != 0); 242 243 /* mixed types */ 244 values[0].len = 2; 245 values[2].type = SPDK_JSON_VAL_STRING; 246 values[2].len = 5; 247 values[2].start = "HELLO"; 248 CU_ASSERT(spdk_json_decode_array(values, spdk_json_decode_uint32, my_int, 2, &out_size, 249 sizeof(uint32_t)) != 0); 250 251 /* no array start */ 252 values[0].type = SPDK_JSON_VAL_NUMBER; 253 values[2].type = SPDK_JSON_VAL_NUMBER; 254 values[2].len = 4; 255 values[2].start = "5678"; 256 CU_ASSERT(spdk_json_decode_array(values, spdk_json_decode_uint32, my_int, 2, &out_size, 257 sizeof(uint32_t)) != 0); 258 259 /* mismatched array type and parser */ 260 values[0].type = SPDK_JSON_VAL_ARRAY_BEGIN; 261 values[1].type = SPDK_JSON_VAL_STRING; 262 values[1].len = 5; 263 values[1].start = "HELLO"; 264 values[2].type = SPDK_JSON_VAL_STRING; 265 values[2].len = 5; 266 values[2].start = "WORLD"; 267 CU_ASSERT(spdk_json_decode_array(values, spdk_json_decode_uint32, my_int, 2, &out_size, 268 sizeof(uint32_t)) != 0); 269 270 /* passing String example */ 271 CU_ASSERT(spdk_json_decode_array(values, spdk_json_decode_string, my_string, 2, &out_size, 272 sizeof(char *)) == 0); 273 SPDK_CU_ASSERT_FATAL(my_string[0] != NULL); 274 SPDK_CU_ASSERT_FATAL(my_string[1] != NULL); 275 CU_ASSERT(memcmp(my_string[0], "HELLO", 6) == 0); 276 CU_ASSERT(memcmp(my_string[1], "WORLD", 6) == 0); 277 CU_ASSERT(out_size == 2); 278 279 free(my_string[0]); 280 free(my_string[1]); 281 } 282 283 static void 284 test_decode_bool(void) 285 { 286 struct spdk_json_val v; 287 bool b; 288 289 /* valid bool (true) */ 290 v.type = SPDK_JSON_VAL_TRUE; 291 b = false; 292 CU_ASSERT(spdk_json_decode_bool(&v, &b) == 0); 293 CU_ASSERT(b == true); 294 295 /* valid bool (false) */ 296 v.type = SPDK_JSON_VAL_FALSE; 297 b = true; 298 CU_ASSERT(spdk_json_decode_bool(&v, &b) == 0); 299 CU_ASSERT(b == false); 300 301 /* incorrect type */ 302 v.type = SPDK_JSON_VAL_NULL; 303 CU_ASSERT(spdk_json_decode_bool(&v, &b) != 0); 304 } 305 306 static void 307 test_decode_int32(void) 308 { 309 struct spdk_json_val v; 310 int32_t i; 311 312 /* correct type and valid value */ 313 v.type = SPDK_JSON_VAL_NUMBER; 314 v.start = "33"; 315 v.len = 2; 316 i = 0; 317 CU_ASSERT(spdk_json_decode_int32(&v, &i) == 0); 318 CU_ASSERT(i == 33) 319 320 /* correct type and invalid value (float) */ 321 v.start = "32.45"; 322 v.len = 5; 323 i = 0; 324 CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0); 325 326 /* incorrect type */ 327 v.type = SPDK_JSON_VAL_STRING; 328 v.start = "String"; 329 v.len = 6; 330 i = 0; 331 CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0); 332 333 /* incorrect type */ 334 v.type = SPDK_JSON_VAL_TRUE; 335 CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0); 336 337 /* edge case (integer max) */ 338 v.type = SPDK_JSON_VAL_NUMBER; 339 v.start = "2147483647"; 340 v.len = 10; 341 i = 0; 342 CU_ASSERT(spdk_json_decode_int32(&v, &i) == 0); 343 CU_ASSERT(i == 2147483647); 344 345 /* invalid value (overflow) */ 346 v.start = "2147483648"; 347 i = 0; 348 CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0); 349 350 /* edge case (integer min) */ 351 v.type = SPDK_JSON_VAL_NUMBER; 352 v.start = "-2147483648"; 353 v.len = 11; 354 i = 0; 355 CU_ASSERT(spdk_json_decode_int32(&v, &i) == 0); 356 CU_ASSERT(i == -2147483648); 357 358 /* invalid value (overflow) */ 359 v.start = "-2147483649"; 360 CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0); 361 362 /* valid exponent */ 363 v.start = "4e3"; 364 v.len = 3; 365 i = 0; 366 CU_ASSERT(spdk_json_decode_int32(&v, &i) == 0); 367 CU_ASSERT(i == 4000); 368 369 /* invalid negative exponent */ 370 v.start = "-400e-4"; 371 v.len = 7; 372 i = 0; 373 CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0); 374 375 /* invalid negative exponent */ 376 v.start = "400e-4"; 377 v.len = 6; 378 i = 0; 379 CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0); 380 381 /* valid negative exponent */ 382 v.start = "-400e-2"; 383 v.len = 7; 384 i = 0; 385 CU_ASSERT(spdk_json_decode_int32(&v, &i) == 0); 386 CU_ASSERT(i == -4) 387 388 /* invalid exponent (overflow) */ 389 v.start = "-2e32"; 390 v.len = 5; 391 i = 0; 392 CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0); 393 394 /* valid exponent with decimal */ 395 v.start = "2.13e2"; 396 v.len = 6; 397 i = 0; 398 CU_ASSERT(spdk_json_decode_int32(&v, &i) == 0); 399 CU_ASSERT(i == 213) 400 401 /* invalid exponent with decimal */ 402 v.start = "2.134e2"; 403 v.len = 7; 404 i = 0; 405 CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0); 406 } 407 408 static void 409 test_decode_uint32(void) 410 { 411 struct spdk_json_val v; 412 uint32_t i; 413 414 /* incorrect type */ 415 v.type = SPDK_JSON_VAL_STRING; 416 v.start = "String"; 417 v.len = 6; 418 CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0); 419 420 /* invalid value (float) */ 421 v.type = SPDK_JSON_VAL_NUMBER; 422 v.start = "123.45"; 423 v.len = 6; 424 CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0); 425 426 /* edge case (0) */ 427 v.start = "0"; 428 v.len = 1; 429 i = 456; 430 CU_ASSERT(spdk_json_decode_uint32(&v, &i) == 0); 431 CU_ASSERT(i == 0); 432 433 /* invalid value (negative) */ 434 v.start = "-1"; 435 v.len = 2; 436 CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0); 437 438 /* edge case (maximum) */ 439 v.start = "4294967295"; 440 v.len = 10; 441 i = 0; 442 CU_ASSERT(spdk_json_decode_uint32(&v, &i) == 0); 443 CU_ASSERT(i == 4294967295); 444 445 /* invalid value (overflow) */ 446 v.start = "4294967296"; 447 v.len = 10; 448 i = 0; 449 CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0); 450 451 /* valid exponent */ 452 v.start = "42E2"; 453 v.len = 4; 454 i = 0; 455 CU_ASSERT(spdk_json_decode_uint32(&v, &i) == 0); 456 CU_ASSERT(i == 4200); 457 458 /* invalid exponent (overflow) */ 459 v.start = "42e32"; 460 v.len = 5; 461 i = 0; 462 CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0); 463 464 /* invalid exponent (decimal) */ 465 v.start = "42.323E2"; 466 v.len = 8; 467 i = 0; 468 CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0); 469 470 /* valid exponent with decimal */ 471 v.start = "42.32E2"; 472 v.len = 7; 473 i = 0; 474 CU_ASSERT(spdk_json_decode_uint32(&v, &i) == 0); 475 CU_ASSERT(i == 4232); 476 477 /* invalid negative exponent */ 478 v.start = "400e-4"; 479 v.len = 6; 480 i = 0; 481 CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0); 482 483 /* invalid negative exponent */ 484 v.start = "-400e-2"; 485 v.len = 7; 486 i = 0; 487 CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0); 488 489 /* valid negative exponent */ 490 v.start = "400e-2"; 491 v.len = 6; 492 i = 0; 493 CU_ASSERT(spdk_json_decode_uint32(&v, &i) == 0); 494 CU_ASSERT(i == 4); 495 496 /* valid negative exponent */ 497 v.start = "10e-1"; 498 v.len = 5; 499 i = 0; 500 CU_ASSERT(spdk_json_decode_uint32(&v, &i) == 0); 501 CU_ASSERT(i == 1) 502 } 503 504 static void 505 test_decode_uint64(void) 506 { 507 struct spdk_json_val v; 508 uint64_t i; 509 510 /* incorrect type */ 511 v.type = SPDK_JSON_VAL_STRING; 512 v.start = "String"; 513 v.len = 6; 514 CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0); 515 516 /* invalid value (float) */ 517 v.type = SPDK_JSON_VAL_NUMBER; 518 v.start = "123.45"; 519 v.len = 6; 520 CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0); 521 522 /* edge case (0) */ 523 v.start = "0"; 524 v.len = 1; 525 i = 456; 526 CU_ASSERT(spdk_json_decode_uint64(&v, &i) == 0); 527 CU_ASSERT(i == 0); 528 529 /* invalid value (negative) */ 530 v.start = "-1"; 531 v.len = 2; 532 CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0); 533 534 /* edge case (maximum) */ 535 v.start = "18446744073709551615"; 536 v.len = 20; 537 i = 0; 538 CU_ASSERT(spdk_json_decode_uint64(&v, &i) == 0); 539 CU_ASSERT(i == 18446744073709551615U); 540 541 /* invalid value (overflow) */ 542 v.start = "18446744073709551616"; 543 v.len = 20; 544 i = 0; 545 CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0); 546 547 /* valid exponent */ 548 v.start = "42E2"; 549 v.len = 4; 550 i = 0; 551 CU_ASSERT(spdk_json_decode_uint64(&v, &i) == 0); 552 CU_ASSERT(i == 4200); 553 554 /* invalid exponent (overflow) */ 555 v.start = "42e64"; 556 v.len = 5; 557 i = 0; 558 CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0); 559 560 /* invalid exponent (decimal) */ 561 v.start = "42.323E2"; 562 v.len = 8; 563 i = 0; 564 CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0); 565 566 /* valid exponent with decimal */ 567 v.start = "42.32E2"; 568 v.len = 7; 569 i = 0; 570 CU_ASSERT(spdk_json_decode_uint64(&v, &i) == 0); 571 CU_ASSERT(i == 4232); 572 573 /* invalid negative exponent */ 574 v.start = "400e-4"; 575 v.len = 6; 576 i = 0; 577 CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0); 578 579 /* invalid negative exponent */ 580 v.start = "-400e-2"; 581 v.len = 7; 582 i = 0; 583 CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0); 584 585 /* valid negative exponent */ 586 v.start = "400e-2"; 587 v.len = 6; 588 i = 0; 589 CU_ASSERT(spdk_json_decode_uint64(&v, &i) == 0); 590 CU_ASSERT(i == 4) 591 } 592 593 static void 594 test_decode_string(void) 595 { 596 struct spdk_json_val v; 597 char *value = NULL; 598 599 /* Passing Test: Standard */ 600 v.type = SPDK_JSON_VAL_STRING; 601 v.start = "HELLO"; 602 v.len = 5; 603 CU_ASSERT(spdk_json_decode_string(&v, &value) == 0); 604 SPDK_CU_ASSERT_FATAL(value != NULL); 605 CU_ASSERT(memcmp(value, v.start, 6) == 0); 606 607 /* Edge Test: Empty String */ 608 v.start = ""; 609 v.len = 0; 610 CU_ASSERT(spdk_json_decode_string(&v, &value) == 0); 611 SPDK_CU_ASSERT_FATAL(value != NULL); 612 CU_ASSERT(memcmp(value, v.start, 1) == 0); 613 614 /* 615 * Failing Test: Null Terminator In String 616 * It is valid for a json string to contain \u0000 and the parser will accept it. 617 * However, a null terminated C string cannot contain '\0' and should be rejected 618 * if that character is found before the end of the string. 619 */ 620 v.start = "HELO"; 621 v.len = 5; 622 CU_ASSERT(spdk_json_decode_string(&v, &value) != 0); 623 624 /* Failing Test: Wrong Type */ 625 v.start = "45673"; 626 v.type = SPDK_JSON_VAL_NUMBER; 627 CU_ASSERT(spdk_json_decode_string(&v, &value) != 0); 628 629 /* Passing Test: Special Characters */ 630 v.type = SPDK_JSON_VAL_STRING; 631 v.start = "HE\bLL\tO\\WORLD"; 632 v.len = 13; 633 CU_ASSERT(spdk_json_decode_string(&v, &value) == 0); 634 SPDK_CU_ASSERT_FATAL(value != NULL); 635 CU_ASSERT(memcmp(value, v.start, 14) == 0); 636 637 free(value); 638 } 639 640 int main(int argc, char **argv) 641 { 642 CU_pSuite suite = NULL; 643 unsigned int num_failures; 644 645 if (CU_initialize_registry() != CUE_SUCCESS) { 646 return CU_get_error(); 647 } 648 649 suite = CU_add_suite("json", NULL, NULL); 650 if (suite == NULL) { 651 CU_cleanup_registry(); 652 return CU_get_error(); 653 } 654 655 if ( 656 CU_add_test(suite, "strequal", test_strequal) == NULL || 657 CU_add_test(suite, "num_to_int32", test_num_to_int32) == NULL || 658 CU_add_test(suite, "num_to_uint64", test_num_to_uint64) == NULL || 659 CU_add_test(suite, "decode_object", test_decode_object) == NULL || 660 CU_add_test(suite, "decode_array", test_decode_array) == NULL || 661 CU_add_test(suite, "decode_bool", test_decode_bool) == NULL || 662 CU_add_test(suite, "decode_int32", test_decode_int32) == NULL || 663 CU_add_test(suite, "decode_uint32", test_decode_uint32) == NULL || 664 CU_add_test(suite, "decode_uint64", test_decode_uint64) == NULL || 665 CU_add_test(suite, "decode_string", test_decode_string) == NULL) { 666 CU_cleanup_registry(); 667 return CU_get_error(); 668 } 669 670 CU_basic_set_mode(CU_BRM_VERBOSE); 671 672 CU_basic_run_tests(); 673 674 num_failures = CU_get_number_of_failures(); 675 CU_cleanup_registry(); 676 677 return num_failures; 678 } 679