From 972caf1603b50872f3763e6e280124e228be1007 Mon Sep 17 00:00:00 2001 From: Seth Howell Date: Wed, 24 May 2017 15:45:17 -0700 Subject: [PATCH] test/json_util: add string decode tests. Change-Id: Ia41bfb8d7611ee9e856669d81cd95f41a38f31bd Signed-off-by: Seth Howell Reviewed-on: https://review.gerrithub.io/362429 Reviewed-by: Daniel Verkamp Tested-by: SPDK Automated Test System Reviewed-by: Jim Harris --- test/lib/json/util/json_util_ut.c | 50 ++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/test/lib/json/util/json_util_ut.c b/test/lib/json/util/json_util_ut.c index 3ab42d034..6704d6acb 100644 --- a/test/lib/json/util/json_util_ut.c +++ b/test/lib/json/util/json_util_ut.c @@ -386,6 +386,53 @@ test_decode_uint32(void) CU_ASSERT(i == 4) } +static void +test_decode_string(void) +{ + struct spdk_json_val v; + char *value = NULL; + + /* Passing Test: Standard */ + v.type = SPDK_JSON_VAL_STRING; + v.start = "HELLO"; + v.len = 5; + CU_ASSERT(spdk_json_decode_string(&v, &value) == 0); + SPDK_CU_ASSERT_FATAL(value != NULL); + CU_ASSERT(memcmp(value, v.start, 6) == 0); + + /* Edge Test: Empty String */ + v.start = ""; + v.len = 0; + CU_ASSERT(spdk_json_decode_string(&v, &value) == 0); + SPDK_CU_ASSERT_FATAL(value != NULL); + CU_ASSERT(memcmp(value, v.start, 1) == 0); + + /* + * Failing Test: Null Terminator In String + * It is valid for a json string to contain \u0000 and the parser will accept it. + * However, a null terminated C string cannot contain '\0' and should be rejected + * if that character is found before the end of the string. + */ + v.start = "HELO"; + v.len = 5; + CU_ASSERT(spdk_json_decode_string(&v, &value) != 0); + + /* Failing Test: Wrong Type */ + v.start = "45673"; + v.type = SPDK_JSON_VAL_NUMBER; + CU_ASSERT(spdk_json_decode_string(&v, &value) != 0); + + /* Passing Test: Special Characters */ + v.type = SPDK_JSON_VAL_STRING; + v.start = "HE\bLL\tO\\WORLD"; + v.len = 13; + CU_ASSERT(spdk_json_decode_string(&v, &value) == 0); + SPDK_CU_ASSERT_FATAL(value != NULL); + CU_ASSERT(memcmp(value, v.start, 14) == 0); + + free(value); +} + int main(int argc, char **argv) { CU_pSuite suite = NULL; @@ -407,7 +454,8 @@ int main(int argc, char **argv) CU_add_test(suite, "decode_array", test_decode_array) == NULL || CU_add_test(suite, "decode_bool", test_decode_bool) == NULL || CU_add_test(suite, "decode_int32", test_decode_int32) == NULL || - CU_add_test(suite, "decode_uint32", test_decode_uint32) == NULL) { + CU_add_test(suite, "decode_uint32", test_decode_uint32) == NULL || + CU_add_test(suite, "decode_string", test_decode_string) == NULL) { CU_cleanup_registry(); return CU_get_error(); }