From 004ab98dacf799999083c705ccc268966706d59e Mon Sep 17 00:00:00 2001 From: Jim Harris Date: Tue, 23 Aug 2022 16:32:54 +0000 Subject: [PATCH] test/unit: add spdk_json_find test for an array Signed-off-by: Jim Harris Change-Id: Ic63277fbf7f82b58231b2554939a7faca8e4b41e Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/14162 Reviewed-by: Tomasz Zawadzki Reviewed-by: Dong Yi Reviewed-by: Shuhei Matsumoto Tested-by: SPDK CI Jenkins Community-CI: Mellanox Build Bot --- test/unit/lib/json/json_util.c/json_util_ut.c | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/unit/lib/json/json_util.c/json_util_ut.c b/test/unit/lib/json/json_util.c/json_util_ut.c index 25c51e7bf..c9e7c8977 100644 --- a/test/unit/lib/json/json_util.c/json_util_ut.c +++ b/test/unit/lib/json/json_util.c/json_util_ut.c @@ -836,6 +836,34 @@ test_find(void) free(values); } +static void +test_find_array(void) +{ + char array_json_text[] = "[ \"Text\", 2, {} ]"; + struct spdk_json_val *values, *key; + ssize_t values_cnt; + ssize_t rc; + + values_cnt = spdk_json_parse(array_json_text, strlen(array_json_text), NULL, 0, NULL, 0); + SPDK_CU_ASSERT_FATAL(values_cnt > 0); + + values = calloc(values_cnt, sizeof(struct spdk_json_val)); + SPDK_CU_ASSERT_FATAL(values != NULL); + + rc = spdk_json_parse(array_json_text, strlen(array_json_text), values, values_cnt, NULL, 0); + SPDK_CU_ASSERT_FATAL(values_cnt == rc); + + /* spdk_json_find cannot be used on arrays. The element "Text" does exist in the array, + * but spdk_json_find can only be used for finding keys in an object. So this + * test should fail. + */ + key = NULL; + rc = spdk_json_find(values, "Text", &key, NULL, SPDK_JSON_VAL_STRING); + CU_ASSERT(rc == -EPROTOTYPE); + + free(values); +} + static void test_iterating(void) { @@ -945,6 +973,7 @@ main(int argc, char **argv) CU_ADD_TEST(suite, test_decode_uint64); CU_ADD_TEST(suite, test_decode_string); CU_ADD_TEST(suite, test_find); + CU_ADD_TEST(suite, test_find_array); CU_ADD_TEST(suite, test_iterating); CU_ADD_TEST(suite, test_free_object);