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