xref: /spdk/test/unit/lib/json/json_util.c/json_util_ut.c (revision 22898a91b9b6f289933db19b0175821cfb7e7820)
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/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 		.my_name = NULL,
169 		.my_int = 0,
170 		.my_bool = false,
171 	};
172 	uint32_t answer = 234;
173 	char *answer_str = "HELLO";
174 	bool answer_bool = true;
175 
176 	/* Passing Test: object containing simple types */
177 	CU_ASSERT(spdk_json_decode_object(object, decoders, 4, &output) == 0);
178 	SPDK_CU_ASSERT_FATAL(output.my_name != NULL);
179 	CU_ASSERT(memcmp(output.my_name, answer_str, 6) == 0);
180 	CU_ASSERT(output.my_int == answer);
181 	CU_ASSERT(output.my_bool == answer_bool);
182 
183 	/* Failing Test: member with no matching decoder */
184 	/* i.e. I remove the matching decoder from the boolean argument */
185 	CU_ASSERT(spdk_json_decode_object(object, decoders, 2, &output) != 0);
186 
187 	/* Failing Test: non-optional decoder with no corresponding member */
188 
189 	decoders[3].optional = false;
190 	CU_ASSERT(spdk_json_decode_object(object, decoders, 4, &output) != 0);
191 
192 	/* return to base state */
193 	decoders[3].optional = true;
194 
195 	/* Failing Test: duplicated names for json values */
196 	object[3].start = "first";
197 	object[3].len = 5;
198 	CU_ASSERT(spdk_json_decode_object(object, decoders, 3, &output) != 0);
199 
200 	/* return to base state */
201 	object[3].start = "second";
202 	object[3].len = 6;
203 
204 	/* Failing Test: invalid value for decoder */
205 	object[2].start = "HELO";
206 	CU_ASSERT(spdk_json_decode_object(object, decoders, 3, &output) != 0);
207 
208 	/* return to base state */
209 	object[2].start = "HELLO";
210 
211 	/* Failing Test: not an object */
212 	object[0].type = SPDK_JSON_VAL_ARRAY_BEGIN;
213 	CU_ASSERT(spdk_json_decode_object(object, decoders, 3, &output) != 0);
214 
215 	free(output.my_name);
216 }
217 
218 static void
219 test_decode_array(void)
220 {
221 	struct spdk_json_val values[4];
222 	uint32_t my_int[2] = {0, 0};
223 	char *my_string[2] = {NULL, NULL};
224 	size_t out_size;
225 
226 	/* passing integer test */
227 	values[0].type = SPDK_JSON_VAL_ARRAY_BEGIN;
228 	values[0].len = 2;
229 	values[1].type = SPDK_JSON_VAL_NUMBER;
230 	values[1].len = 4;
231 	values[1].start = "1234";
232 	values[2].type = SPDK_JSON_VAL_NUMBER;
233 	values[2].len = 4;
234 	values[2].start = "5678";
235 	values[3].type = SPDK_JSON_VAL_ARRAY_END;
236 	CU_ASSERT(spdk_json_decode_array(values, spdk_json_decode_uint32, my_int, 2, &out_size,
237 					 sizeof(uint32_t)) == 0);
238 	CU_ASSERT(my_int[0] == 1234);
239 	CU_ASSERT(my_int[1] == 5678);
240 	CU_ASSERT(out_size == 2);
241 
242 	/* array length exceeds max */
243 	values[0].len = 3;
244 	CU_ASSERT(spdk_json_decode_array(values, spdk_json_decode_uint32, my_int, 2, &out_size,
245 					 sizeof(uint32_t)) != 0);
246 
247 	/* mixed types */
248 	values[0].len = 2;
249 	values[2].type = SPDK_JSON_VAL_STRING;
250 	values[2].len = 5;
251 	values[2].start = "HELLO";
252 	CU_ASSERT(spdk_json_decode_array(values, spdk_json_decode_uint32, my_int, 2, &out_size,
253 					 sizeof(uint32_t)) != 0);
254 
255 	/* no array start */
256 	values[0].type = SPDK_JSON_VAL_NUMBER;
257 	values[2].type = SPDK_JSON_VAL_NUMBER;
258 	values[2].len = 4;
259 	values[2].start = "5678";
260 	CU_ASSERT(spdk_json_decode_array(values, spdk_json_decode_uint32, my_int, 2, &out_size,
261 					 sizeof(uint32_t)) != 0);
262 
263 	/* mismatched array type and parser */
264 	values[0].type = SPDK_JSON_VAL_ARRAY_BEGIN;
265 	values[1].type = SPDK_JSON_VAL_STRING;
266 	values[1].len = 5;
267 	values[1].start = "HELLO";
268 	values[2].type = SPDK_JSON_VAL_STRING;
269 	values[2].len = 5;
270 	values[2].start = "WORLD";
271 	CU_ASSERT(spdk_json_decode_array(values, spdk_json_decode_uint32, my_int, 2, &out_size,
272 					 sizeof(uint32_t)) != 0);
273 
274 	/* passing String example */
275 	CU_ASSERT(spdk_json_decode_array(values, spdk_json_decode_string, my_string, 2, &out_size,
276 					 sizeof(char *)) == 0);
277 	SPDK_CU_ASSERT_FATAL(my_string[0] != NULL);
278 	SPDK_CU_ASSERT_FATAL(my_string[1] != NULL);
279 	CU_ASSERT(memcmp(my_string[0], "HELLO", 6) == 0);
280 	CU_ASSERT(memcmp(my_string[1], "WORLD", 6) == 0);
281 	CU_ASSERT(out_size == 2);
282 
283 	free(my_string[0]);
284 	free(my_string[1]);
285 }
286 
287 static void
288 test_decode_bool(void)
289 {
290 	struct spdk_json_val v;
291 	bool b;
292 
293 	/* valid bool (true) */
294 	v.type = SPDK_JSON_VAL_TRUE;
295 	b = false;
296 	CU_ASSERT(spdk_json_decode_bool(&v, &b) == 0);
297 	CU_ASSERT(b == true);
298 
299 	/* valid bool (false) */
300 	v.type = SPDK_JSON_VAL_FALSE;
301 	b = true;
302 	CU_ASSERT(spdk_json_decode_bool(&v, &b) == 0);
303 	CU_ASSERT(b == false);
304 
305 	/* incorrect type */
306 	v.type = SPDK_JSON_VAL_NULL;
307 	CU_ASSERT(spdk_json_decode_bool(&v, &b) != 0);
308 }
309 
310 static void
311 test_decode_int32(void)
312 {
313 	struct spdk_json_val v;
314 	int32_t i;
315 
316 	/* correct type and valid value */
317 	v.type = SPDK_JSON_VAL_NUMBER;
318 	v.start = "33";
319 	v.len = 2;
320 	i = 0;
321 	CU_ASSERT(spdk_json_decode_int32(&v, &i) == 0);
322 	CU_ASSERT(i == 33)
323 
324 	/* correct type and invalid value (float) */
325 	v.start = "32.45";
326 	v.len = 5;
327 	i = 0;
328 	CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0);
329 
330 	/* incorrect type */
331 	v.type = SPDK_JSON_VAL_STRING;
332 	v.start = "String";
333 	v.len = 6;
334 	i = 0;
335 	CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0);
336 
337 	/* incorrect type */
338 	v.type = SPDK_JSON_VAL_TRUE;
339 	CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0);
340 
341 	/* edge case (integer max) */
342 	v.type = SPDK_JSON_VAL_NUMBER;
343 	v.start = "2147483647";
344 	v.len = 10;
345 	i = 0;
346 	CU_ASSERT(spdk_json_decode_int32(&v, &i) == 0);
347 	CU_ASSERT(i == 2147483647);
348 
349 	/* invalid value (overflow) */
350 	v.start = "2147483648";
351 	i = 0;
352 	CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0);
353 
354 	/* edge case (integer min) */
355 	v.type = SPDK_JSON_VAL_NUMBER;
356 	v.start = "-2147483648";
357 	v.len = 11;
358 	i = 0;
359 	CU_ASSERT(spdk_json_decode_int32(&v, &i) == 0);
360 	CU_ASSERT(i == -2147483648);
361 
362 	/* invalid value (overflow) */
363 	v.start = "-2147483649";
364 	CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0);
365 
366 	/* valid exponent */
367 	v.start = "4e3";
368 	v.len = 3;
369 	i = 0;
370 	CU_ASSERT(spdk_json_decode_int32(&v, &i) == 0);
371 	CU_ASSERT(i == 4000);
372 
373 	/* invalid negative exponent */
374 	v.start = "-400e-4";
375 	v.len = 7;
376 	i = 0;
377 	CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0);
378 
379 	/* invalid negative exponent */
380 	v.start = "400e-4";
381 	v.len = 6;
382 	i = 0;
383 	CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0);
384 
385 	/* valid negative exponent */
386 	v.start = "-400e-2";
387 	v.len = 7;
388 	i = 0;
389 	CU_ASSERT(spdk_json_decode_int32(&v, &i) == 0);
390 	CU_ASSERT(i == -4)
391 
392 	/* invalid exponent (overflow) */
393 	v.start = "-2e32";
394 	v.len = 5;
395 	i = 0;
396 	CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0);
397 
398 	/* valid exponent with decimal */
399 	v.start = "2.13e2";
400 	v.len = 6;
401 	i = 0;
402 	CU_ASSERT(spdk_json_decode_int32(&v, &i) == 0);
403 	CU_ASSERT(i == 213)
404 
405 	/* invalid exponent with decimal */
406 	v.start = "2.134e2";
407 	v.len = 7;
408 	i = 0;
409 	CU_ASSERT(spdk_json_decode_int32(&v, &i) != 0);
410 }
411 
412 static void
413 test_decode_uint32(void)
414 {
415 	struct spdk_json_val v;
416 	uint32_t i;
417 
418 	/* incorrect type */
419 	v.type = SPDK_JSON_VAL_STRING;
420 	v.start = "String";
421 	v.len = 6;
422 	CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0);
423 
424 	/* invalid value (float) */
425 	v.type = SPDK_JSON_VAL_NUMBER;
426 	v.start = "123.45";
427 	v.len = 6;
428 	CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0);
429 
430 	/* edge case (0) */
431 	v.start = "0";
432 	v.len = 1;
433 	i = 456;
434 	CU_ASSERT(spdk_json_decode_uint32(&v, &i) == 0);
435 	CU_ASSERT(i == 0);
436 
437 	/* invalid value (negative) */
438 	v.start = "-1";
439 	v.len = 2;
440 	CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0);
441 
442 	/* edge case (maximum) */
443 	v.start = "4294967295";
444 	v.len = 10;
445 	i = 0;
446 	CU_ASSERT(spdk_json_decode_uint32(&v, &i) == 0);
447 	CU_ASSERT(i == 4294967295);
448 
449 	/* invalid value (overflow) */
450 	v.start = "4294967296";
451 	v.len = 10;
452 	i = 0;
453 	CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0);
454 
455 	/* valid exponent */
456 	v.start = "42E2";
457 	v.len = 4;
458 	i = 0;
459 	CU_ASSERT(spdk_json_decode_uint32(&v, &i) == 0);
460 	CU_ASSERT(i == 4200);
461 
462 	/* invalid exponent (overflow) */
463 	v.start = "42e32";
464 	v.len = 5;
465 	i = 0;
466 	CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0);
467 
468 	/* invalid exponent (decimal) */
469 	v.start = "42.323E2";
470 	v.len = 8;
471 	i = 0;
472 	CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0);
473 
474 	/* valid exponent with decimal */
475 	v.start = "42.32E2";
476 	v.len = 7;
477 	i = 0;
478 	CU_ASSERT(spdk_json_decode_uint32(&v, &i) == 0);
479 	CU_ASSERT(i == 4232);
480 
481 	/* invalid negative exponent */
482 	v.start = "400e-4";
483 	v.len = 6;
484 	i = 0;
485 	CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0);
486 
487 	/* invalid negative exponent */
488 	v.start = "-400e-2";
489 	v.len = 7;
490 	i = 0;
491 	CU_ASSERT(spdk_json_decode_uint32(&v, &i) != 0);
492 
493 	/* valid negative exponent */
494 	v.start = "400e-2";
495 	v.len = 6;
496 	i = 0;
497 	CU_ASSERT(spdk_json_decode_uint32(&v, &i) == 0);
498 	CU_ASSERT(i == 4);
499 
500 	/* valid negative exponent */
501 	v.start = "10e-1";
502 	v.len = 5;
503 	i = 0;
504 	CU_ASSERT(spdk_json_decode_uint32(&v, &i) == 0);
505 	CU_ASSERT(i == 1)
506 }
507 
508 static void
509 test_decode_uint64(void)
510 {
511 	struct spdk_json_val v;
512 	uint64_t i;
513 
514 	/* incorrect type */
515 	v.type = SPDK_JSON_VAL_STRING;
516 	v.start = "String";
517 	v.len = 6;
518 	CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0);
519 
520 	/* invalid value (float) */
521 	v.type = SPDK_JSON_VAL_NUMBER;
522 	v.start = "123.45";
523 	v.len = 6;
524 	CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0);
525 
526 	/* edge case (0) */
527 	v.start = "0";
528 	v.len = 1;
529 	i = 456;
530 	CU_ASSERT(spdk_json_decode_uint64(&v, &i) == 0);
531 	CU_ASSERT(i == 0);
532 
533 	/* invalid value (negative) */
534 	v.start = "-1";
535 	v.len = 2;
536 	CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0);
537 
538 	/* edge case (maximum) */
539 	v.start = "18446744073709551615";
540 	v.len = 20;
541 	i = 0;
542 	CU_ASSERT(spdk_json_decode_uint64(&v, &i) == 0);
543 	CU_ASSERT(i == 18446744073709551615U);
544 
545 	/* invalid value (overflow) */
546 	v.start = "18446744073709551616";
547 	v.len = 20;
548 	i = 0;
549 	CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0);
550 
551 	/* valid exponent */
552 	v.start = "42E2";
553 	v.len = 4;
554 	i = 0;
555 	CU_ASSERT(spdk_json_decode_uint64(&v, &i) == 0);
556 	CU_ASSERT(i == 4200);
557 
558 	/* invalid exponent (overflow) */
559 	v.start = "42e64";
560 	v.len = 5;
561 	i = 0;
562 	CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0);
563 
564 	/* invalid exponent (decimal) */
565 	v.start = "42.323E2";
566 	v.len = 8;
567 	i = 0;
568 	CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0);
569 
570 	/* valid exponent with decimal */
571 	v.start = "42.32E2";
572 	v.len = 7;
573 	i = 0;
574 	CU_ASSERT(spdk_json_decode_uint64(&v, &i) == 0);
575 	CU_ASSERT(i == 4232);
576 
577 	/* invalid negative exponent */
578 	v.start = "400e-4";
579 	v.len = 6;
580 	i = 0;
581 	CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0);
582 
583 	/* invalid negative exponent */
584 	v.start = "-400e-2";
585 	v.len = 7;
586 	i = 0;
587 	CU_ASSERT(spdk_json_decode_uint64(&v, &i) != 0);
588 
589 	/* valid negative exponent */
590 	v.start = "400e-2";
591 	v.len = 6;
592 	i = 0;
593 	CU_ASSERT(spdk_json_decode_uint64(&v, &i) == 0);
594 	CU_ASSERT(i == 4)
595 }
596 
597 static void
598 test_decode_string(void)
599 {
600 	struct spdk_json_val v;
601 	char *value = NULL;
602 
603 	/* Passing Test: Standard */
604 	v.type = SPDK_JSON_VAL_STRING;
605 	v.start = "HELLO";
606 	v.len = 5;
607 	CU_ASSERT(spdk_json_decode_string(&v, &value) == 0);
608 	SPDK_CU_ASSERT_FATAL(value != NULL);
609 	CU_ASSERT(memcmp(value, v.start, 6) == 0);
610 
611 	/* Edge Test: Empty String */
612 	v.start = "";
613 	v.len = 0;
614 	CU_ASSERT(spdk_json_decode_string(&v, &value) == 0);
615 	SPDK_CU_ASSERT_FATAL(value != NULL);
616 	CU_ASSERT(memcmp(value, v.start, 1) == 0);
617 
618 	/*
619 	 * Failing Test: Null Terminator In String
620 	 * It is valid for a json string to contain \u0000 and the parser will accept it.
621 	 * However, a null terminated C string cannot contain '\0' and should be rejected
622 	 * if that character is found before the end of the string.
623 	 */
624 	v.start = "HELO";
625 	v.len = 5;
626 	CU_ASSERT(spdk_json_decode_string(&v, &value) != 0);
627 
628 	/* Failing Test: Wrong Type */
629 	v.start = "45673";
630 	v.type = SPDK_JSON_VAL_NUMBER;
631 	CU_ASSERT(spdk_json_decode_string(&v, &value) != 0);
632 
633 	/* Passing Test: Special Characters */
634 	v.type = SPDK_JSON_VAL_STRING;
635 	v.start = "HE\bLL\tO\\WORLD";
636 	v.len = 13;
637 	CU_ASSERT(spdk_json_decode_string(&v, &value) == 0);
638 	SPDK_CU_ASSERT_FATAL(value != NULL);
639 	CU_ASSERT(memcmp(value, v.start, 14) == 0);
640 
641 	free(value);
642 }
643 
644 int main(int argc, char **argv)
645 {
646 	CU_pSuite	suite = NULL;
647 	unsigned int	num_failures;
648 
649 	if (CU_initialize_registry() != CUE_SUCCESS) {
650 		return CU_get_error();
651 	}
652 
653 	suite = CU_add_suite("json", NULL, NULL);
654 	if (suite == NULL) {
655 		CU_cleanup_registry();
656 		return CU_get_error();
657 	}
658 
659 	if (
660 		CU_add_test(suite, "strequal", test_strequal) == NULL ||
661 		CU_add_test(suite, "num_to_int32", test_num_to_int32) == NULL ||
662 		CU_add_test(suite, "num_to_uint64", test_num_to_uint64) == NULL ||
663 		CU_add_test(suite, "decode_object", test_decode_object) == NULL ||
664 		CU_add_test(suite, "decode_array", test_decode_array) == NULL ||
665 		CU_add_test(suite, "decode_bool", test_decode_bool) == NULL ||
666 		CU_add_test(suite, "decode_int32", test_decode_int32) == NULL ||
667 		CU_add_test(suite, "decode_uint32", test_decode_uint32) == NULL ||
668 		CU_add_test(suite, "decode_uint64", test_decode_uint64) == NULL ||
669 		CU_add_test(suite, "decode_string", test_decode_string) == NULL) {
670 		CU_cleanup_registry();
671 		return CU_get_error();
672 	}
673 
674 	CU_basic_set_mode(CU_BRM_VERBOSE);
675 
676 	CU_basic_run_tests();
677 
678 	num_failures = CU_get_number_of_failures();
679 	CU_cleanup_registry();
680 
681 	return num_failures;
682 }
683