From aaba8c1080533133329086e47f4f45981dfdd1ee Mon Sep 17 00:00:00 2001 From: Alexandru Ardelean Date: Wed, 26 Oct 2016 10:37:50 +0300 Subject: [PATCH 1/4] compat/strdup.h: move common compat check for strdup() to own file Signed-off-by: Alexandru Ardelean --- CMakeLists.txt | 1 + Makefile.am | 1 + compat/strdup.h | 13 +++++++++++++ json-c.vcproj | 3 +++ json-c.vcxproj | 3 ++- json-c.vcxproj.filters | 5 ++++- json_object.c | 8 +------- json_tokener.c | 8 +------- 8 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 compat/strdup.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5015708..39ffe24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ endif() include_directories(${CMAKE_CURRENT_BINARY_DIR}/include) set(JSON_C_HEADERS + ./compat/strdup.h ./json.h ${CMAKE_CURRENT_BINARY_DIR}/include/config.h ./json_config.h diff --git a/Makefile.am b/Makefile.am index a5d126b..8f7af38 100644 --- a/Makefile.am +++ b/Makefile.am @@ -18,6 +18,7 @@ libjson_cincludedir = $(includedir)/json-c libjson_cinclude_HEADERS = \ arraylist.h \ bits.h \ + compat/strdup.h \ debug.h \ json.h \ json_c_version.h \ diff --git a/compat/strdup.h b/compat/strdup.h new file mode 100644 index 0000000..6b1a34c --- /dev/null +++ b/compat/strdup.h @@ -0,0 +1,13 @@ +#ifndef __STRDUP_H_COMPAT__ +#define __STRDUP_H_COMPAT__ + +#include "../config.h" + +#if !defined(HAVE_STRDUP) && defined(_MSC_VER) + /* MSC has the version as _strdup */ +# define strdup _strdup +#elif !defined(HAVE_STRDUP) +# error You do not have strdup on your system. +#endif /* HAVE_STRDUP */ + +#endif /* __STRDUP_H_COMPAT__ */ diff --git a/json-c.vcproj b/json-c.vcproj index fcda288..184aa83 100644 --- a/json-c.vcproj +++ b/json-c.vcproj @@ -131,6 +131,9 @@ copy json_config.h.win32 json_config.h"/> + + diff --git a/json-c.vcxproj b/json-c.vcxproj index c39ef7e..d0f1e98 100644 --- a/json-c.vcxproj +++ b/json-c.vcxproj @@ -141,6 +141,7 @@ copy json_config.h.win32 json_config.h + @@ -162,4 +163,4 @@ copy json_config.h.win32 json_config.h - \ No newline at end of file + diff --git a/json-c.vcxproj.filters b/json-c.vcxproj.filters index 63b6fb1..7861064 100644 --- a/json-c.vcxproj.filters +++ b/json-c.vcxproj.filters @@ -47,6 +47,9 @@ Header Files + + Header Files + Header Files @@ -87,4 +90,4 @@ - \ No newline at end of file + diff --git a/json_object.c b/json_object.c index 8a70b5d..17fdd34 100644 --- a/json_object.c +++ b/json_object.c @@ -29,13 +29,7 @@ #include "json_object_private.h" #include "json_util.h" #include "math_compat.h" - -#if !defined(HAVE_STRDUP) && defined(_MSC_VER) - /* MSC has the version as _strdup */ -# define strdup _strdup -#elif !defined(HAVE_STRDUP) -# error You do not have strdup on your system. -#endif /* HAVE_STRDUP */ +#include "compat/strdup.h" #if !defined(HAVE_SNPRINTF) && defined(_MSC_VER) /* MSC has the version as _snprintf */ diff --git a/json_tokener.c b/json_tokener.c index 65652f4..4521ef9 100644 --- a/json_tokener.c +++ b/json_tokener.c @@ -31,6 +31,7 @@ #include "json_object.h" #include "json_tokener.h" #include "json_util.h" +#include "compat/strdup.h" #ifdef HAVE_LOCALE_H #include @@ -41,13 +42,6 @@ #define jt_hexdigit(x) (((x) <= '9') ? (x) - '0' : ((x) & 7) + 9) -#if !HAVE_STRDUP && defined(_MSC_VER) - /* MSC has the version as _strdup */ -# define strdup _strdup -#elif !HAVE_STRDUP -# error You do not have strdup on your system. -#endif /* HAVE_STRDUP */ - #if !HAVE_STRNCASECMP && defined(_MSC_VER) /* MSC has the version as _strnicmp */ # define strncasecmp _strnicmp From 0e911833009282b0c8d345904a581a0d627247f7 Mon Sep 17 00:00:00 2001 From: Alexandru Ardelean Date: Tue, 25 Oct 2016 17:39:16 +0300 Subject: [PATCH 2/4] json_pointer: add first revision Signed-off-by: Alexandru Ardelean --- CMakeLists.txt | 2 + Makefile.am | 2 + json-c.vcproj | 6 ++ json-c.vcxproj | 2 + json-c.vcxproj.filters | 6 ++ json.h | 1 + json_pointer.c | 239 +++++++++++++++++++++++++++++++++++++++++ json_pointer.h | 69 ++++++++++++ 8 files changed, 327 insertions(+) create mode 100644 json_pointer.c create mode 100644 json_pointer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 39ffe24..77a9558 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,7 @@ set(JSON_C_HEADERS ./json_inttypes.h ./json_object.h ./json_object_private.h + ./json_pointer.h ./json_tokener.h ./json_util.h ./linkhash.h @@ -44,6 +45,7 @@ set(JSON_C_SOURCES ./arraylist.c ./debug.c ./json_object.c + ./json_pointer.c ./json_tokener.c ./json_util.c ./linkhash.c diff --git a/Makefile.am b/Makefile.am index 8f7af38..58452fc 100644 --- a/Makefile.am +++ b/Makefile.am @@ -27,6 +27,7 @@ libjson_cinclude_HEADERS = \ json_object.h \ json_object_iterator.h \ json_object_private.h \ + json_pointer.h \ json_tokener.h \ json_util.h \ json_visit.h \ @@ -43,6 +44,7 @@ libjson_c_la_SOURCES = \ json_c_version.c \ json_object.c \ json_object_iterator.c \ + json_pointer.c \ json_tokener.c \ json_util.c \ json_visit.c \ diff --git a/json-c.vcproj b/json-c.vcproj index 184aa83..796856e 100644 --- a/json-c.vcproj +++ b/json-c.vcproj @@ -108,6 +108,9 @@ copy json_config.h.win32 json_config.h"/> + + @@ -143,6 +146,9 @@ copy json_config.h.win32 json_config.h"/> + + diff --git a/json-c.vcxproj b/json-c.vcxproj index d0f1e98..02e0c2d 100644 --- a/json-c.vcxproj +++ b/json-c.vcxproj @@ -133,6 +133,7 @@ copy json_config.h.win32 json_config.h + @@ -146,6 +147,7 @@ copy json_config.h.win32 json_config.h + diff --git a/json-c.vcxproj.filters b/json-c.vcxproj.filters index 7861064..cc13355 100644 --- a/json-c.vcxproj.filters +++ b/json-c.vcxproj.filters @@ -27,6 +27,9 @@ Source Files + + Source Files + Source Files @@ -59,6 +62,9 @@ Header Files + + Header Files + Header Files diff --git a/json.h b/json.h index e198f5d..dca0df5 100644 --- a/json.h +++ b/json.h @@ -22,6 +22,7 @@ extern "C" { #include "arraylist.h" #include "json_util.h" #include "json_object.h" +#include "json_pointer.h" #include "json_tokener.h" #include "json_object_iterator.h" #include "json_c_version.h" diff --git a/json_pointer.c b/json_pointer.c new file mode 100644 index 0000000..f06aed0 --- /dev/null +++ b/json_pointer.c @@ -0,0 +1,239 @@ +/* + * Copyright (c) 2016 Alexandru Ardelean. + * + * This is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See COPYING for details. + * + */ + +#include "config.h" + +#include +#include +#include +#include +#include + +#include "json_pointer.h" + +/** + * JavaScript Object Notation (JSON) Pointer + * RFC 6901 - https://tools.ietf.org/html/rfc6901 + */ + +static void string_replace_all_occurrences_with_char(char *s, const char *occur, char repl_char) +{ + int slen = strlen(s); + int skip = strlen(occur) - 1; /* length of the occurence, minus the char we're replacing */ + char *p = s; + while ((p = strstr(p, occur))) { + *p = repl_char; + p++; + slen -= skip; + memcpy(p, (p + skip), slen - (p - s) + 1); /* includes null char too */ + } +} + +static int is_valid_index(struct json_object *jo, const char *path, int32_t *idx) +{ + int i, len = strlen(path); + /* this code-path optimizes a bit, for when we reference the 0-9 index range in a JSON array + and because leading zeros not allowed */ + if (len == 1) { + if (isdigit(path[0])) { + *idx = (path[0] - '0'); + goto check_oob; + } + errno = EINVAL; + return 0; + } + /* leading zeros not allowed per RFC */ + if (path[0] == '0') { + errno = EINVAL; + return 0; + } + /* RFC states base-10 decimals */ + for (i = 0; i < len; i++) { + if (!isdigit(path[i])) { + errno = EINVAL; + return 0; + } + } + + *idx = strtol(path, NULL, 10); + if (*idx < 0) { + errno = EINVAL; + return 0; + } +check_oob: + len = json_object_array_length(jo); + if (*idx >= len) { + errno = ENOENT; + return 0; + } + + return 1; +} + +static int json_pointer_get_single_path(struct json_object *obj, char *path, struct json_object **value) +{ + if (json_object_is_type(obj, json_type_array)) { + int32_t idx; + if (!is_valid_index(obj, path, &idx)) + return -1; + obj = json_object_array_get_idx(obj, idx); + if (obj) { + if (value) + *value = obj; + return 0; + } + /* Entry not found */ + errno = ENOENT; + return -1; + } + + /* RFC states that we first must eval all ~1 then all ~0 */ + string_replace_all_occurrences_with_char(path, "~1", '/'); + string_replace_all_occurrences_with_char(path, "~0", '~'); + + if (!json_object_object_get_ex(obj, path, value)) { + errno = ENOENT; + return -1; + } + + return 0; +} + +static int json_pointer_set_single_path( + struct json_object *parent, + const char *path, + struct json_object *value) +{ + if (json_object_is_type(parent, json_type_array)) { + int32_t idx; + /* RFC (Chapter 4) states that '-' may be used to add new elements to an array */ + if (path[0] == '-' && path[1] == '\0') + return json_object_array_add(parent, value); + if (!is_valid_index(parent, path, &idx)) + return -1; + return json_object_array_put_idx(parent, idx, value); + } + + /* path replacements should have been done in json_pointer_get_single_path(), + and we should still be good here */ + if (json_object_is_type(parent, json_type_object)) + return json_object_object_add(parent, path, value); + + /* Getting here means that we tried to "dereference" a primitive JSON type (like string, int, bool). + i.e. add a sub-object to it */ + errno = ENOENT; + return -1; +} + +static int json_pointer_get_recursive( + struct json_object *obj, + char *path, + struct json_object **value) +{ + char *endp; + int rc; + + /* All paths (on each recursion level must have a leading '/' */ + if (path[0] != '/') { + errno = EINVAL; + return -1; + } + path++; + + endp = strchr(path, '/'); + if (endp) + *endp = '\0'; + + /* If we err-ed here, return here */ + if ((rc = json_pointer_get_single_path(obj, path, &obj))) + return rc; + + if (endp) { + *endp = '/'; /* Put the slash back, so that the sanity check passes on next recursion level */ + return json_pointer_get_recursive(obj, endp, value); + } + + /* We should be at the end of the recursion here */ + if (value) + *value = obj; + + return 0; +} + +int json_pointer_get(struct json_object *obj, const char *path, struct json_object **res) +{ + char *path_copy = NULL; + int rc; + + if (!obj || !path) { + errno = EINVAL; + return -1; + } + + if (path[0] == '\0') { + if (res) + *res = obj; + return 0; + } + + /* pass a working copy to the recursive call */ + if (!(path_copy = strdup(path))) { + errno = ENOMEM; + return -1; + } + rc = json_pointer_get_recursive(obj, path_copy, res); + free(path_copy); + + return rc; +} + +int json_pointer_set(struct json_object **obj, const char *path, struct json_object *value) +{ + const char *endp; + char *path_copy = NULL; + struct json_object *set = NULL; + int rc; + + if (!obj || !path) { + errno = EINVAL; + return -1; + } + + if (path[0] == '\0') { + json_object_put(*obj); + *obj = value; + return 0; + } + + if (path[0] != '/') { + errno = EINVAL; + return -1; + } + + /* If there's only 1 level to set, stop here */ + if ((endp = strrchr(path, '/')) == path) { + path++; + return json_pointer_set_single_path(*obj, path, value); + } + + /* pass a working copy to the recursive call */ + if (!(path_copy = strdup(path))) { + errno = ENOMEM; + return -1; + } + path_copy[endp - path] = '\0'; + rc = json_pointer_get_recursive(*obj, path_copy, &set); + free(path_copy); + + if (rc) + return rc; + + endp++; + return json_pointer_set_single_path(set, endp, value); +} + diff --git a/json_pointer.h b/json_pointer.h new file mode 100644 index 0000000..d661b7b --- /dev/null +++ b/json_pointer.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2016 Alexadru Ardelean. + * + * This is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See COPYING for details. + * + */ + +#ifndef _json_pointer_h_ +#define _json_pointer_h_ + +#include "json_object.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Retrieves a JSON sub-object from inside another JSON object + * using the JSON pointer notation as defined in RFC 6901 + * https://tools.ietf.org/html/rfc6901 + * + * The returned JSON sub-object is equivalent to parsing manually the + * 'obj' JSON tree ; i.e. it's not a new object that is created, but rather + * a pointer inside the JSON tree. + * + * Internally, this is equivalent to doing a series of 'json_object_object_get()' + * and 'json_object_array_get_idx()' along the given 'path'. + * + * @param obj the json_object instance/tree from where to retrieve sub-objects + * @param path a (RFC6901) string notation for the sub-object to retrieve + * @param res a pointer where to store a reference to the json_object + * associated with the given path + * + * @return negative if an error (or not found), or 0 if succeeded + */ +int json_pointer_get(struct json_object *obj, const char *path, struct json_object **res); + +/** + * Sets JSON object 'value' in the 'obj' tree at the location specified + * by the 'path'. 'path' is JSON pointer notation as defined in RFC 6901 + * https://tools.ietf.org/html/rfc6901 + * + * Note that 'obj' is a double pointer, mostly for the "" (empty string) + * case, where the entire JSON object would be replaced by 'value'. + * In the case of the "" path, the object at '*obj' will have it's refcount + * decremented with 'json_object_put()' and the 'value' object will be assigned to it. + * + * For other cases (JSON sub-objects) ownership of 'value' will be transferred into + * '*obj' via 'json_object_object_add()' & 'json_object_array_put_idx()', so the + * only time the refcount should be decremented for 'value' is when the return value of + * 'json_pointer_set()' is negative (meaning the 'value' object did not get set into '*obj'). + * + * That also implies that 'json_pointer_set()' does not do any refcount incrementing. + * (Just that single decrement that was mentioned above). + * + * @param obj the json_object instance/tree to which to add a sub-object + * @param path a (RFC6901) string notation for the sub-object to set in the tree + * @param value object to set at path + * + * @return negative if an error (or not found), or 0 if succeeded + */ +int json_pointer_set(struct json_object **obj, const char *path, struct json_object *value); + +#ifdef __cplusplus +} +#endif + +#endif From 2fbdee19dadc0856ef1e465ed9b772119ebb9883 Mon Sep 17 00:00:00 2001 From: Alexandru Ardelean Date: Tue, 1 Nov 2016 17:49:28 +0200 Subject: [PATCH 3/4] tests/strerror_override.c: fix compilation error I got this on Mac OS X at least. Not sure if it shows up in other envs error: ``` strerror_override.c:53:13: error: incompatible redeclaration of library function 'strerror' [-Werror,-Wincompatible-library-redeclaration] const char *strerror(int errno_in) ``` Signed-off-by: Alexandru Ardelean --- tests/strerror_override.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/strerror_override.c b/tests/strerror_override.c index a5c2292..d197e7f 100644 --- a/tests/strerror_override.c +++ b/tests/strerror_override.c @@ -50,7 +50,7 @@ static struct { #define PREFIX "ERRNO=" static char errno_buf[128] = PREFIX; -const char *strerror(int errno_in) +char *strerror(int errno_in) { int start_idx; char digbuf[20]; From ee7fc26de17c6d4cc667464c2665926911b11ff2 Mon Sep 17 00:00:00 2001 From: Alexandru Ardelean Date: Tue, 1 Nov 2016 18:07:03 +0200 Subject: [PATCH 4/4] tests: add test_json_pointer test Signed-off-by: Alexandru Ardelean --- .gitignore | 1 + tests/Makefile.am | 1 + tests/test_json_pointer.c | 272 +++++++++++++++++++++++++++++++ tests/test_json_pointer.expected | 36 ++++ tests/test_json_pointer.test | 12 ++ 5 files changed, 322 insertions(+) create mode 100644 tests/test_json_pointer.c create mode 100644 tests/test_json_pointer.expected create mode 100755 tests/test_json_pointer.test diff --git a/.gitignore b/.gitignore index b7098da..6e85d6f 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,7 @@ /tests/test_set_value /tests/test_util_file /tests/test_visit +/tests/test_json_pointer /tests/*.vg.out /tests/*.log /tests/*.trs diff --git a/tests/Makefile.am b/tests/Makefile.am index 1ee06c4..3d01cad 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -23,6 +23,7 @@ TESTS+= test_set_serializer.test TESTS+= test_compare.test TESTS+= test_set_value.test TESTS+= test_visit.test +TESTS+= test_json_pointer.test check_PROGRAMS= check_PROGRAMS += $(TESTS:.test=) diff --git a/tests/test_json_pointer.c b/tests/test_json_pointer.c new file mode 100644 index 0000000..aa495cc --- /dev/null +++ b/tests/test_json_pointer.c @@ -0,0 +1,272 @@ +#include +#include +#include +#include + +#include "json.h" + +static void test_example_int(struct json_object *jo1, const char *json_pointer, int expected_int) +{ + struct json_object *jo2 = NULL; + assert(0 == json_pointer_get(jo1, json_pointer, NULL)); + assert(0 == json_pointer_get(jo1, json_pointer, &jo2)); + assert(json_object_is_type(jo2, json_type_int)); + assert(expected_int == json_object_get_int(jo2)); + printf("PASSED - GET - %s == %d\n", json_pointer, expected_int); +} + +static const char *input_json_str = "{ " + "'foo': ['bar', 'baz'], " + "'': 0, " + "'a/b': 1, " + "'c\%d': 2, " + "'e^f': 3, " + "'g|h': 4, " + "'i\\\\j': 5, " + "'k\\\"l': 6, " + "' ': 7, " + "'m~n': 8 " +"}"; + + +static const char *rec_input_json_str = "{" + "'arr' : [" + "{" + "'obj': [" + "{},{}," + "{" + "'obj1': 0," + "'obj2': \"1\"" + "}" + "]" + "}" + "]," + "'obj' : {" + "'obj': {" + "'obj': [" + "{" + "'obj1': 0," + "'obj2': \"1\"" + "}" + "]" + "}" + "}" +"}"; + +/* Example from RFC */ +static void test_example_get() +{ + int i; + struct json_object *jo1, *jo2, *jo3; + struct json_pointer_map_s_i { + const char *s; + int i; + }; + /* Create a map to iterate over for the ints */ + struct json_pointer_map_s_i json_pointers[] = { + { "/", 0 }, + { "/a~1b", 1 }, + {"/c%d", 2 }, + {"/e^f", 3 }, + { "/g|h", 4 }, + { "/i\\j", 5 }, + { "/k\"l", 6 }, + { "/ ", 7 }, + { "/m~0n", 8 }, + { NULL, 0} + }; + + jo1 = json_tokener_parse(input_json_str); + assert(NULL != jo1); + printf("PASSED - GET - LOADED TEST JSON\n"); + printf("%s\n", json_object_get_string(jo1)); + + /* Test empty string returns entire object */ + jo2 = NULL; + /* For each test, we're trying to see that NULL **value works (does no segfault) */ + assert(0 == json_pointer_get(jo1, "", NULL)); + assert(0 == json_pointer_get(jo1, "", &jo2)); + assert(json_object_equal(jo2, jo1)); + printf("PASSED - GET - ENTIRE OBJECT WORKED\n"); + + /* Test /foo == ['bar', 'baz'] */ + jo3 = json_object_new_array(); + json_object_array_add(jo3, json_object_new_string("bar")); + json_object_array_add(jo3, json_object_new_string("baz")); + + jo2 = NULL; + assert(0 == json_pointer_get(jo1, "/foo", NULL)); + assert(0 == json_pointer_get(jo1, "/foo", &jo2)); + assert(NULL != jo2); + assert(json_object_equal(jo2, jo3)); + json_object_put(jo3); + printf("PASSED - GET - /foo == ['bar', 'baz']\n"); + + /* Test /foo/0 == 'bar' */ + jo2 = NULL; + assert(0 == json_pointer_get(jo1, "/foo/0", NULL)); + assert(0 == json_pointer_get(jo1, "/foo/0", &jo2)); + assert(NULL != jo2); + assert(0 == strcmp("bar", json_object_get_string(jo2))); + printf("PASSED - GET - /foo/0 == 'bar'\n"); + + for (i = 0 ; json_pointers[i].s; i++) + test_example_int(jo1, json_pointers[i].s, json_pointers[i].i); + + json_object_put(jo1); +} + +/* I'm not too happy with the RFC example to test the recusion of the json_pointer_get() function */ +static void test_recursion_get() +{ + struct json_object *jo2, *jo1 = json_tokener_parse(rec_input_json_str); + + jo2 = NULL; + assert(jo1 != NULL); + printf("%s\n", json_object_get_string(jo1)); + assert(0 == json_pointer_get(jo1, "/arr/0/obj/2/obj1", &jo2)); + assert(json_object_is_type(jo2, json_type_int)); + assert(0 == json_object_get_int(jo2)); + + assert(0 == json_pointer_get(jo1, "/arr/0/obj/2/obj2", &jo2)); + assert(json_object_is_type(jo2, json_type_string)); + assert(0 == strcmp("1", json_object_get_string(jo2))); + + assert(jo1 != NULL); + assert(0 == json_pointer_get(jo1, "/obj/obj/obj/0/obj1", &jo2)); + assert(json_object_is_type(jo2, json_type_int)); + assert(0 == json_object_get_int(jo2)); + + assert(0 == json_pointer_get(jo1, "/obj/obj/obj/0/obj2", &jo2)); + assert(json_object_is_type(jo2, json_type_string)); + assert(0 == strcmp("1", json_object_get_string(jo2))); + + printf("PASSED - GET - RECURSION TEST\n"); + + json_object_put(jo1); +} + +static void test_wrong_inputs_get() +{ + struct json_object *jo2, *jo1 = json_tokener_parse(input_json_str); + + assert(NULL != jo1); + printf("PASSED - GET - LOADED TEST JSON\n"); + printf("%s\n", json_object_get_string(jo1)); + + /* Test leading '/' missing */ + jo2 = NULL; + errno = 0; + assert(0 != json_pointer_get(jo1, "foo/bar", NULL)); + assert(0 != json_pointer_get(jo1, "foo/bar", &jo2)); + assert(errno == EINVAL); + assert(jo2 == NULL); + printf("PASSED - GET - MISSING /\n"); + + /* Test combinations of NULL params for input json & path */ + errno = 0; + assert(0 != json_pointer_get(NULL, "foo/bar", NULL)); + assert(errno == EINVAL); + errno = 0; + assert(0 != json_pointer_get(NULL, NULL, NULL)); + assert(errno == EINVAL); + errno = 0; + assert(0 != json_pointer_get(jo1, NULL, NULL)); + assert(errno == EINVAL); + printf("PASSED - GET - NULL INPUTS\n"); + + /* Test invalid indexes for array */ + errno = 0; + assert(0 != json_pointer_get(jo1, "/foo/a", NULL)); + assert(errno == EINVAL); + errno = 0; + assert(0 != json_pointer_get(jo1, "/foo/-", NULL)); + assert(errno == EINVAL); + errno = 0; + /* Test optimized array path */ + assert(0 != json_pointer_get(jo1, "/foo/4", NULL)); + assert(errno == ENOENT); + errno = 0; + /* Test non-optimized array path */ + assert(0 != json_pointer_get(jo1, "/foo/22", NULL)); + assert(errno == ENOENT); + errno = 0; + assert(0 != json_pointer_get(jo1, "/foo/-1", NULL)); + assert(errno == EINVAL); + errno = 0; + assert(0 != json_pointer_get(jo1, "/foo/10", NULL)); + assert(errno == ENOENT); + printf("PASSED - GET - INVALID INDEXES\n"); + + json_object_put(jo1); +} + +static void test_example_set() +{ + struct json_object *jo2, *jo1 = json_tokener_parse(input_json_str); + + assert(jo1 != NULL); + printf("PASSED - SET - LOADED TEST JSON\n"); + printf("%s\n", json_object_get_string(jo1)); + + assert(0 == json_pointer_set(&jo1, "/foo/1", json_object_new_string("cod"))); + assert(0 == strcmp("cod", json_object_get_string(json_object_array_get_idx(json_object_object_get(jo1, "foo"), 1)))); + printf("PASSED - SET - 'cod' in /foo/1\n"); + assert(0 != json_pointer_set(&jo1, "/fud/gaw", (jo2 = json_tokener_parse("[1,2,3]")))); + assert(errno == ENOENT); + printf("PASSED - SET - non-existing /fud/gaw\n"); + assert(0 == json_pointer_set(&jo1, "/fud", json_object_new_object())); + printf("PASSED - SET - /fud == {}\n"); + assert(0 == json_pointer_set(&jo1, "/fud/gaw", jo2)); /* re-using jo2 from above */ + printf("PASSED - SET - /fug/gaw == [1,2,3]\n"); + assert(0 == json_pointer_set(&jo1, "/fud/gaw/0", json_object_new_int(0))); + printf("PASSED - SET - /fug/gaw == [0,2,3]\n"); + assert(0 == json_pointer_set(&jo1, "/fud/gaw/-", json_object_new_int(4))); + printf("PASSED - SET - /fug/gaw == [0,2,3,4]\n"); + assert(0 == json_pointer_set(&jo1, "/", json_object_new_int(9))); + printf("PASSED - SET - / == 9\n"); + + jo2 = json_tokener_parse("{ 'foo': [ 'bar', 'cod' ], '': 9, 'a/b': 1, 'c\%d': 2, 'e^f': 3, 'g|h': 4, 'i\\\\j': 5, 'k\\\"l': 6, ' ': 7, 'm~n': 8, 'fud': { 'gaw': [ 0, 2, 3, 4 ] } }"); + assert(json_object_equal(jo2, jo1)); + printf("PASSED - SET - Final JSON is: %s\n", json_object_get_string(jo1)); + json_object_put(jo2); + + assert(0 == json_pointer_set(&jo1, "", json_object_new_int(10))); + assert(10 == json_object_get_int(jo1)); + printf("%s\n", json_object_get_string(jo1)); + + json_object_put(jo1); +} + +static void test_wrong_inputs_set() +{ + struct json_object *jo2, *jo1 = json_tokener_parse(input_json_str); + + assert(jo1 != NULL); + printf("PASSED - SET - LOADED TEST JSON\n"); + printf("%s\n", json_object_get_string(jo1)); + + assert(0 != json_pointer_set(&jo1, "foo/bar", (jo2 = json_object_new_string("cod")))); + printf("PASSED - SET - failed 'cod' with path 'foo/bar'\n"); + json_object_put(jo2); + + assert(0 != json_pointer_set(&jo1, "/fud/gaw", (jo2 = json_object_new_string("whatever")))); + assert(0 == json_pointer_set(&jo1, "/fud", json_object_new_object())); + assert(0 == json_pointer_set(&jo1, "/fud/gaw", jo2)); /* re-using jo2 from above */ + assert(0 != json_pointer_set(&jo1, "/fud/gaw/0", json_object_new_int(0))); + assert(0 != json_pointer_set(&jo1, "/fud/gaw/", json_object_new_int(0))); + printf("PASSED - SET - failed to set index to non-array\n"); + + json_object_put(jo1); + json_object_put(jo2); +} + +int main(int argc, char **argv) +{ + test_example_get(); + test_recursion_get(); + test_wrong_inputs_get(); + test_example_set(); + test_wrong_inputs_set(); + return 0; +} diff --git a/tests/test_json_pointer.expected b/tests/test_json_pointer.expected new file mode 100644 index 0000000..e77e542 --- /dev/null +++ b/tests/test_json_pointer.expected @@ -0,0 +1,36 @@ +PASSED - GET - LOADED TEST JSON +{ "foo": [ "bar", "baz" ], "": 0, "a\/b": 1, "c%d": 2, "e^f": 3, "g|h": 4, "i\\j": 5, "k\"l": 6, " ": 7, "m~n": 8 } +PASSED - GET - ENTIRE OBJECT WORKED +PASSED - GET - /foo == ['bar', 'baz'] +PASSED - GET - /foo/0 == 'bar' +PASSED - GET - / == 0 +PASSED - GET - /a~1b == 1 +PASSED - GET - /c%d == 2 +PASSED - GET - /e^f == 3 +PASSED - GET - /g|h == 4 +PASSED - GET - /i\j == 5 +PASSED - GET - /k"l == 6 +PASSED - GET - / == 7 +PASSED - GET - /m~0n == 8 +{ "arr": [ { "obj": [ { }, { }, { "obj1": 0, "obj2": "1" } ] } ], "obj": { "obj": { "obj": [ { "obj1": 0, "obj2": "1" } ] } } } +PASSED - GET - RECURSION TEST +PASSED - GET - LOADED TEST JSON +{ "foo": [ "bar", "baz" ], "": 0, "a\/b": 1, "c%d": 2, "e^f": 3, "g|h": 4, "i\\j": 5, "k\"l": 6, " ": 7, "m~n": 8 } +PASSED - GET - MISSING / +PASSED - GET - NULL INPUTS +PASSED - GET - INVALID INDEXES +PASSED - SET - LOADED TEST JSON +{ "foo": [ "bar", "baz" ], "": 0, "a\/b": 1, "c%d": 2, "e^f": 3, "g|h": 4, "i\\j": 5, "k\"l": 6, " ": 7, "m~n": 8 } +PASSED - SET - 'cod' in /foo/1 +PASSED - SET - non-existing /fud/gaw +PASSED - SET - /fud == {} +PASSED - SET - /fug/gaw == [1,2,3] +PASSED - SET - /fug/gaw == [0,2,3] +PASSED - SET - /fug/gaw == [0,2,3,4] +PASSED - SET - / == 9 +PASSED - SET - Final JSON is: { "foo": [ "bar", "cod" ], "": 9, "a\/b": 1, "c%d": 2, "e^f": 3, "g|h": 4, "i\\j": 5, "k\"l": 6, " ": 7, "m~n": 8, "fud": { "gaw": [ 0, 2, 3, 4 ] } } +10 +PASSED - SET - LOADED TEST JSON +{ "foo": [ "bar", "baz" ], "": 0, "a\/b": 1, "c%d": 2, "e^f": 3, "g|h": 4, "i\\j": 5, "k\"l": 6, " ": 7, "m~n": 8 } +PASSED - SET - failed 'cod' with path 'foo/bar' +PASSED - SET - failed to set index to non-array diff --git a/tests/test_json_pointer.test b/tests/test_json_pointer.test new file mode 100755 index 0000000..54e5a89 --- /dev/null +++ b/tests/test_json_pointer.test @@ -0,0 +1,12 @@ +#!/bin/sh + +# Common definitions +if test -z "$srcdir"; then + srcdir="${0%/*}" + test "$srcdir" = "$0" && srcdir=. + test -z "$srcdir" && srcdir=. +fi +. "$srcdir/test-defs.sh" + +run_output_test test_json_pointer +exit $?