first commit

This commit is contained in:
hinomori_minori
2026-09-12 05:38:04 +09:00
commit 3e167c7e13
1844 changed files with 841995 additions and 0 deletions
@@ -0,0 +1,74 @@
cmake_minimum_required(VERSION 3.9...3.12) # see ../CMakeLists.txt for why
add_executable(test1Formatted test1.c parse_flags.c parse_flags.h)
target_compile_definitions(test1Formatted PRIVATE TEST_FORMATTED=1)
target_link_libraries(test1Formatted PRIVATE ${PROJECT_NAME})
add_executable(test2Formatted test2.c parse_flags.c parse_flags.h)
target_compile_definitions(test2Formatted PRIVATE TEST_FORMATTED=1)
target_link_libraries(test2Formatted PRIVATE ${PROJECT_NAME})
# https://cmake.org/cmake/help/v3.0/command/add_test.html
# https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/
include_directories(PUBLIC ${CMAKE_SOURCE_DIR})
set(ALL_TEST_NAMES
test1
test2
test4
testReplaceExisting
test_cast
test_charcase
test_compare
test_deep_copy
test_deep_nesting
test_double_serializer
test_float
test_int_add
test_int_get
test_locale
test_null
test_parse
test_parse_int64
test_printbuf
test_set_serializer
test_set_value
test_strerror
test_util_file
test_visit
test_object_iterator)
set(NOBUILD_TEST_NAMES
test_json_parse_cli
)
if (NOT DISABLE_JSON_POINTER)
set(ALL_TEST_NAMES ${ALL_TEST_NAMES} test_json_pointer)
set(ALL_TEST_NAMES ${ALL_TEST_NAMES} test_safe_json_pointer_set)
if (NOT DISABLE_JSON_PATCH)
set(ALL_TEST_NAMES ${ALL_TEST_NAMES} test_json_patch)
endif()
endif()
foreach(TESTNAME ${NOBUILD_TEST_NAMES})
add_test(NAME ${TESTNAME} COMMAND ${PROJECT_SOURCE_DIR}/tests/${TESTNAME}.test)
endforeach(TESTNAME)
foreach(TESTNAME ${ALL_TEST_NAMES})
add_executable(${TESTNAME} ${TESTNAME}.c)
add_test(NAME ${TESTNAME} COMMAND ${PROJECT_SOURCE_DIR}/tests/${TESTNAME}.test)
# XXX using the non-target_ versions of these doesn't work :(
target_include_directories(
${TESTNAME}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
target_link_libraries(
${TESTNAME}
PRIVATE
${PROJECT_NAME}
)
endforeach(TESTNAME)
@@ -0,0 +1,233 @@
[
{
"comment": "4.1. add with missing object",
"doc": { "q": { "bar": 2 } },
"patch": [ {"op": "add", "path": "/a/b", "value": 1} ],
"error":
"path /a does not exist -- missing objects are not created recursively"
},
{
"comment": "A.1. Adding an Object Member",
"doc": {
"foo": "bar"
},
"patch": [
{ "op": "add", "path": "/baz", "value": "qux" }
],
"expected": {
"baz": "qux",
"foo": "bar"
}
},
{
"comment": "A.2. Adding an Array Element",
"doc": {
"foo": [ "bar", "baz" ]
},
"patch": [
{ "op": "add", "path": "/foo/1", "value": "qux" }
],
"expected": {
"foo": [ "bar", "qux", "baz" ]
}
},
{
"comment": "A.3. Removing an Object Member",
"doc": {
"baz": "qux",
"foo": "bar"
},
"patch": [
{ "op": "remove", "path": "/baz" }
],
"expected": {
"foo": "bar"
}
},
{
"comment": "A.4. Removing an Array Element",
"doc": {
"foo": [ "bar", "qux", "baz" ]
},
"patch": [
{ "op": "remove", "path": "/foo/1" }
],
"expected": {
"foo": [ "bar", "baz" ]
}
},
{
"comment": "A.5. Replacing a Value",
"doc": {
"baz": "qux",
"foo": "bar"
},
"patch": [
{ "op": "replace", "path": "/baz", "value": "boo" }
],
"expected": {
"baz": "boo",
"foo": "bar"
}
},
{
"comment": "A.6. Moving a Value",
"doc": {
"foo": {
"bar": "baz",
"waldo": "fred"
},
"qux": {
"corge": "grault"
}
},
"patch": [
{ "op": "move", "from": "/foo/waldo", "path": "/qux/thud" }
],
"expected": {
"foo": {
"bar": "baz"
},
"qux": {
"corge": "grault",
"thud": "fred"
}
}
},
{
"comment": "A.7. Moving an Array Element",
"doc": {
"foo": [ "all", "grass", "cows", "eat" ]
},
"patch": [
{ "op": "move", "from": "/foo/1", "path": "/foo/3" }
],
"expected": {
"foo": [ "all", "cows", "eat", "grass" ]
}
},
{
"comment": "A.8. Testing a Value: Success",
"doc": {
"baz": "qux",
"foo": [ "a", 2, "c" ]
},
"patch": [
{ "op": "test", "path": "/baz", "value": "qux" },
{ "op": "test", "path": "/foo/1", "value": 2 }
],
"expected": {
"baz": "qux",
"foo": [ "a", 2, "c" ]
}
},
{
"comment": "A.9. Testing a Value: Error",
"doc": {
"baz": "qux"
},
"patch": [
{ "op": "test", "path": "/baz", "value": "bar" }
],
"error": "string not equivalent"
},
{
"comment": "A.10. Adding a nested Member Object",
"doc": {
"foo": "bar"
},
"patch": [
{ "op": "add", "path": "/child", "value": { "grandchild": { } } }
],
"expected": {
"foo": "bar",
"child": {
"grandchild": {
}
}
}
},
{
"comment": "A.11. Ignoring Unrecognized Elements",
"doc": {
"foo":"bar"
},
"patch": [
{ "op": "add", "path": "/baz", "value": "qux", "xyz": 123 }
],
"expected": {
"foo":"bar",
"baz":"qux"
}
},
{
"comment": "A.12. Adding to a Non-existent Target",
"doc": {
"foo": "bar"
},
"patch": [
{ "op": "add", "path": "/baz/bat", "value": "qux" }
],
"error": "add to a non-existent target"
},
{
"comment": "A.13 Invalid JSON Patch Document",
"doc": {
"foo": "bar"
},
"patch": [
{ "op": "add", "path": "/baz", "value": "qux", "op": "remove" }
],
"error_wont_happen_in_jsonc": "operation has two 'op' members",
"error": "Did not find element referenced by path field"
},
{
"comment": "A.14. ~ Escape Ordering",
"doc": {
"/": 9,
"~1": 10
},
"patch": [{"op": "test", "path": "/~01", "value": 10}],
"expected": {
"/": 9,
"~1": 10
}
},
{
"comment": "A.15. Comparing Strings and Numbers",
"doc": {
"/": 9,
"~1": 10
},
"patch": [{"op": "test", "path": "/~01", "value": "10"}],
"error": "number is not equal to string"
},
{
"comment": "A.16. Adding an Array Value",
"doc": {
"foo": ["bar"]
},
"patch": [{ "op": "add", "path": "/foo/-", "value": ["abc", "def"] }],
"expected": {
"foo": ["bar", ["abc", "def"]]
}
}
]
@@ -0,0 +1,547 @@
[
{ "comment": "empty list, empty docs",
"doc": {},
"patch": [],
"expected": {} },
{ "comment": "empty patch list",
"doc": {"foo": 1},
"patch": [],
"expected": {"foo": 1} },
{ "comment": "rearrangements OK?",
"doc": {"foo": 1, "bar": 2},
"patch": [],
"expected": {"bar":2, "foo": 1} },
{ "comment": "rearrangements OK? How about one level down ... array",
"doc": [{"foo": 1, "bar": 2}],
"patch": [],
"expected": [{"bar":2, "foo": 1}] },
{ "comment": "rearrangements OK? How about one level down...",
"doc": {"foo":{"foo": 1, "bar": 2}},
"patch": [],
"expected": {"foo":{"bar":2, "foo": 1}} },
{ "comment": "add replaces any existing field",
"doc": {"foo": null},
"patch": [{"op": "add", "path": "/foo", "value":1}],
"expected": {"foo": 1} },
{ "comment": "toplevel array",
"doc": [],
"patch": [{"op": "add", "path": "/0", "value": "foo"}],
"expected": ["foo"] },
{ "comment": "toplevel array, no change",
"doc": ["foo"],
"patch": [],
"expected": ["foo"] },
{ "comment": "toplevel object, numeric string",
"doc": {},
"patch": [{"op": "add", "path": "/foo", "value": "1"}],
"expected": {"foo":"1"} },
{ "comment": "toplevel object, integer",
"doc": {},
"patch": [{"op": "add", "path": "/foo", "value": 1}],
"expected": {"foo":1} },
{ "comment": "Toplevel scalar values OK?",
"doc": "foo",
"patch": [{"op": "replace", "path": "", "value": "bar"}],
"expected": "bar"
},
{ "comment": "replace object document with array document?",
"doc": {},
"patch": [{"op": "add", "path": "", "value": []}],
"expected": [] },
{ "comment": "replace array document with object document?",
"doc": [],
"patch": [{"op": "add", "path": "", "value": {}}],
"expected": {} },
{ "comment": "append to root array document?",
"doc": [],
"patch": [{"op": "add", "path": "/-", "value": "hi"}],
"expected": ["hi"] },
{ "comment": "Add, / target",
"doc": {},
"patch": [ {"op": "add", "path": "/", "value":1 } ],
"expected": {"":1} },
{ "comment": "Add, /foo/ deep target (trailing slash)",
"doc": {"foo": {}},
"patch": [ {"op": "add", "path": "/foo/", "value":1 } ],
"expected": {"foo":{"": 1}} },
{ "comment": "Add composite value at top level",
"doc": {"foo": 1},
"patch": [{"op": "add", "path": "/bar", "value": [1, 2]}],
"expected": {"foo": 1, "bar": [1, 2]} },
{ "comment": "Add into composite value",
"doc": {"foo": 1, "baz": [{"qux": "hello"}]},
"patch": [{"op": "add", "path": "/baz/0/foo", "value": "world"}],
"expected": {"foo": 1, "baz": [{"qux": "hello", "foo": "world"}]} },
{ "doc": {"bar": [1, 2]},
"patch": [{"op": "add", "path": "/bar/8", "value": "5"}],
"error": "Out of bounds (upper)" },
{ "doc": {"bar": [1, 2]},
"patch": [{"op": "add", "path": "/bar/-1", "value": "5"}],
"error": "Out of bounds (lower)" },
{ "doc": {"foo": 1},
"patch": [{"op": "add", "path": "/bar", "value": true}],
"expected": {"foo": 1, "bar": true} },
{ "doc": {"foo": 1},
"patch": [{"op": "add", "path": "/bar", "value": false}],
"expected": {"foo": 1, "bar": false} },
{ "doc": {"foo": 1},
"patch": [{"op": "add", "path": "/bar", "value": null}],
"expected": {"foo": 1, "bar": null} },
{ "comment": "0 can be an array index or object element name",
"doc": {"foo": 1},
"patch": [{"op": "add", "path": "/0", "value": "bar"}],
"expected": {"foo": 1, "0": "bar" } },
{ "doc": ["foo"],
"patch": [{"op": "add", "path": "/1", "value": "bar"}],
"expected": ["foo", "bar"] },
{ "doc": ["foo", "sil"],
"patch": [{"op": "add", "path": "/1", "value": "bar"}],
"expected": ["foo", "bar", "sil"] },
{ "doc": ["foo", "sil"],
"patch": [{"op": "add", "path": "/0", "value": "bar"}],
"expected": ["bar", "foo", "sil"] },
{ "comment": "push item to array via last index + 1",
"doc": ["foo", "sil"],
"patch": [{"op":"add", "path": "/2", "value": "bar"}],
"expected": ["foo", "sil", "bar"] },
{ "comment": "add item to array at index > length should fail",
"doc": ["foo", "sil"],
"patch": [{"op":"add", "path": "/3", "value": "bar"}],
"error": "index is greater than number of items in array" },
{ "comment": "test against implementation-specific numeric parsing",
"doc": {"1e0": "foo"},
"patch": [{"op": "test", "path": "/1e0", "value": "foo"}],
"expected": {"1e0": "foo"} },
{ "comment": "test with bad number should fail",
"doc": ["foo", "bar"],
"patch": [{"op": "test", "path": "/1e0", "value": "bar"}],
"error": "test op shouldn't get array element 1" },
{ "doc": ["foo", "sil"],
"patch": [{"op": "add", "path": "/bar", "value": 42}],
"error": "Object operation on array target" },
{ "doc": ["foo", "sil"],
"patch": [{"op": "add", "path": "/1", "value": ["bar", "baz"]}],
"expected": ["foo", ["bar", "baz"], "sil"],
"comment": "value in array add not flattened" },
{ "doc": {"foo": 1, "bar": [1, 2, 3, 4]},
"patch": [{"op": "remove", "path": "/bar"}],
"expected": {"foo": 1} },
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
"patch": [{"op": "remove", "path": "/baz/0/qux"}],
"expected": {"foo": 1, "baz": [{}]} },
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
"patch": [{"op": "replace", "path": "/foo", "value": [1, 2, 3, 4]}],
"expected": {"foo": [1, 2, 3, 4], "baz": [{"qux": "hello"}]} },
{ "doc": {"foo": [1, 2, 3, 4], "baz": [{"qux": "hello"}]},
"patch": [{"op": "replace", "path": "/baz/0/qux", "value": "world"}],
"expected": {"foo": [1, 2, 3, 4], "baz": [{"qux": "world"}]} },
{ "doc": ["foo"],
"patch": [{"op": "replace", "path": "/0", "value": "bar"}],
"expected": ["bar"] },
{ "doc": [""],
"patch": [{"op": "replace", "path": "/0", "value": 0}],
"expected": [0] },
{ "doc": [""],
"patch": [{"op": "replace", "path": "/0", "value": true}],
"expected": [true] },
{ "doc": [""],
"patch": [{"op": "replace", "path": "/0", "value": false}],
"expected": [false] },
{ "doc": [""],
"patch": [{"op": "replace", "path": "/0", "value": null}],
"expected": [null] },
{ "doc": ["foo", "sil"],
"patch": [{"op": "replace", "path": "/1", "value": ["bar", "baz"]}],
"expected": ["foo", ["bar", "baz"]],
"comment": "value in array replace not flattened" },
{ "comment": "replace whole document",
"doc": {"foo": "bar"},
"patch": [{"op": "replace", "path": "", "value": {"baz": "qux"}}],
"expected": {"baz": "qux"} },
{ "comment": "add whole document, null",
"doc": {},
"Note1": "We can't pass null in to json_patch_apply, so start with _something_ and remove it",
"patch": [
{"op": "remove", "path": ""},
{"op": "add", "path": "", "value": {"baz": "qux"}}
],
"expected": {"baz": "qux"} },
{ "comment": "replace whole document, null",
"doc": {},
"Note1": "We can't pass null in to json_patch_apply, so start with _something_ and remove it",
"patch": [
{"op": "remove", "path": ""},
{"op": "replace", "path": "", "value": {"baz": "qux"}}
],
"error": "The spec says the target location must exist, so replacing a null document fails"
},
{ "comment": "remove whole document",
"doc": {"foo": "bar"},
"patch": [{"op": "remove", "path": ""}],
"expected": null },
{ "comment": "remove whole document",
"doc": {"foo": "bar"},
"patch": [{"op": "remove", "path": ""}],
"expected": null },
{ "comment": "remove whole document, array",
"doc": ["foo", "bar"],
"patch": [{"op": "remove", "path": ""}],
"expected": null },
{ "comment": "remove whole document, string",
"doc": "foo",
"patch": [{"op": "remove", "path": ""}],
"expected": null },
{ "comment": "remove whole document, null",
"doc": {},
"Note1": "We can't pass null in to json_patch_apply, so start with _something_ and remove it",
"patch": [
{"op": "remove", "path": ""},
{"op": "remove", "path": ""},
],
"error": "The spec says the target location must exist, so removing a null document fails"
},
{ "comment": "test replace with missing parent key should fail",
"doc": {"bar": "baz"},
"patch": [{"op": "replace", "path": "/foo/bar", "value": false}],
"error": "replace op should fail with missing parent key" },
{ "comment": "spurious patch properties",
"doc": {"foo": 1},
"patch": [{"op": "test", "path": "/foo", "value": 1, "spurious": 1}],
"expected": {"foo": 1} },
{ "doc": {"foo": null},
"patch": [{"op": "test", "path": "/foo", "value": null}],
"expected": {"foo": null},
"comment": "null value should be valid obj property" },
{ "doc": {"foo": null},
"patch": [{"op": "replace", "path": "/foo", "value": "truthy"}],
"expected": {"foo": "truthy"},
"comment": "null value should be valid obj property to be replaced with something truthy" },
{ "doc": {"foo": null},
"patch": [{"op": "move", "from": "/foo", "path": "/bar"}],
"expected": {"bar": null},
"comment": "null value should be valid obj property to be moved" },
{ "doc": {"foo": null},
"patch": [{"op": "copy", "from": "/foo", "path": "/bar"}],
"expected": {"foo": null, "bar": null},
"comment": "null value should be valid obj property to be copied" },
{ "doc": {"foo": null},
"patch": [{"op": "remove", "path": "/foo"}],
"expected": {},
"comment": "null value should be valid obj property to be removed" },
{ "doc": {"foo": "bar"},
"patch": [{"op": "replace", "path": "/foo", "value": null}],
"expected": {"foo": null},
"comment": "null value should still be valid obj property replace other value" },
{ "doc": {"foo": {"foo": 1, "bar": 2}},
"patch": [{"op": "test", "path": "/foo", "value": {"bar": 2, "foo": 1}}],
"expected": {"foo": {"foo": 1, "bar": 2}},
"comment": "test should pass despite rearrangement" },
{ "doc": {"foo": [{"foo": 1, "bar": 2}]},
"patch": [{"op": "test", "path": "/foo", "value": [{"bar": 2, "foo": 1}]}],
"expected": {"foo": [{"foo": 1, "bar": 2}]},
"comment": "test should pass despite (nested) rearrangement" },
{ "doc": {"foo": {"bar": [1, 2, 5, 4]}},
"patch": [{"op": "test", "path": "/foo", "value": {"bar": [1, 2, 5, 4]}}],
"expected": {"foo": {"bar": [1, 2, 5, 4]}},
"comment": "test should pass - no error" },
{ "doc": {"foo": {"bar": [1, 2, 5, 4]}},
"patch": [{"op": "test", "path": "/foo", "value": [1, 2]}],
"error": "test op should fail" },
{ "comment": "Test the whole document",
"doc": { "foo": 1 },
"patch": [{"op": "test", "path": "", "value": {"foo": 1}}],
"expected": { "foo": 1 } },
{ "comment": "Test the whole document, no match",
"doc": { "foo": 1 },
"patch": [{"op": "test", "path": "", "value": {"foo": 2}}],
"expected": { "foo": 1 },
"error": "Tested value does not match original doc" },
{ "comment": "Empty-string element",
"doc": { "": 1 },
"patch": [{"op": "test", "path": "/", "value": 1}],
"expected": { "": 1 } },
{ "doc": {
"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
},
"patch": [{"op": "test", "path": "/foo", "value": ["bar", "baz"]},
{"op": "test", "path": "/foo/0", "value": "bar"},
{"op": "test", "path": "/", "value": 0},
{"op": "test", "path": "/a~1b", "value": 1},
{"op": "test", "path": "/c%d", "value": 2},
{"op": "test", "path": "/e^f", "value": 3},
{"op": "test", "path": "/g|h", "value": 4},
{"op": "test", "path": "/i\\j", "value": 5},
{"op": "test", "path": "/k\"l", "value": 6},
{"op": "test", "path": "/ ", "value": 7},
{"op": "test", "path": "/m~0n", "value": 8}],
"expected": {
"": 0,
" ": 7,
"a/b": 1,
"c%d": 2,
"e^f": 3,
"foo": [
"bar",
"baz"
],
"g|h": 4,
"i\\j": 5,
"k\"l": 6,
"m~n": 8
}
},
{ "comment": "Move to same location has no effect",
"doc": {"foo": 1},
"patch": [{"op": "move", "from": "/foo", "path": "/foo"}],
"expected": {"foo": 1} },
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
"patch": [{"op": "move", "from": "/foo", "path": "/bar"}],
"expected": {"baz": [{"qux": "hello"}], "bar": 1} },
{ "doc": {"baz": [{"qux": "hello"}], "bar": 1},
"patch": [{"op": "move", "from": "/baz/0/qux", "path": "/baz/1"}],
"expected": {"baz": [{}, "hello"], "bar": 1} },
{ "doc": {"baz": [{"qux": "hello"}], "bar": 1},
"patch": [{"op": "copy", "from": "/baz/0", "path": "/boo"}],
"expected": {"baz":[{"qux":"hello"}],"bar":1,"boo":{"qux":"hello"}} },
{ "comment": "replacing the root of the document is possible with add",
"doc": {"foo": "bar"},
"patch": [{"op": "add", "path": "", "value": {"baz": "qux"}}],
"expected": {"baz":"qux"}},
{ "comment": "Adding to \"/-\" adds to the end of the array",
"doc": [ 1, 2 ],
"patch": [ { "op": "add", "path": "/-", "value": { "foo": [ "bar", "baz" ] } } ],
"expected": [ 1, 2, { "foo": [ "bar", "baz" ] } ]},
{ "comment": "Adding to \"/-\" adds to the end of the array, even n levels down",
"doc": [ 1, 2, [ 3, [ 4, 5 ] ] ],
"patch": [ { "op": "add", "path": "/2/1/-", "value": { "foo": [ "bar", "baz" ] } } ],
"expected": [ 1, 2, [ 3, [ 4, 5, { "foo": [ "bar", "baz" ] } ] ] ]},
{ "comment": "test remove with bad number should fail",
"doc": {"foo": 1, "baz": [{"qux": "hello"}]},
"patch": [{"op": "remove", "path": "/baz/1e0/qux"}],
"error": "remove op shouldn't remove from array with bad number" },
{ "comment": "test remove on array",
"doc": [1, 2, 3, 4],
"patch": [{"op": "remove", "path": "/0"}],
"expected": [2, 3, 4] },
{ "comment": "test repeated removes",
"doc": [1, 2, 3, 4],
"patch": [{ "op": "remove", "path": "/1" },
{ "op": "remove", "path": "/2" }],
"expected": [1, 3] },
{ "comment": "test remove with bad index should fail",
"doc": [1, 2, 3, 4],
"patch": [{"op": "remove", "path": "/1e0"}],
"error": "remove op shouldn't remove from array with bad number" },
{ "comment": "test replace with bad number should fail",
"doc": [""],
"patch": [{"op": "replace", "path": "/1e0", "value": false}],
"error": "replace op shouldn't replace in array with bad number" },
{ "comment": "test copy with bad number should fail",
"doc": {"baz": [1,2,3], "bar": 1},
"patch": [{"op": "copy", "from": "/baz/1e0", "path": "/boo"}],
"error": "copy op shouldn't work with bad number" },
{ "comment": "test move with bad number should fail",
"doc": {"foo": 1, "baz": [1,2,3,4]},
"patch": [{"op": "move", "from": "/baz/1e0", "path": "/foo"}],
"error": "move op shouldn't work with bad number" },
{ "comment": "test add with bad number should fail",
"doc": ["foo", "sil"],
"patch": [{"op": "add", "path": "/1e0", "value": "bar"}],
"error": "add op shouldn't add to array with bad number" },
{ "comment": "missing 'path' parameter",
"doc": {},
"patch": [ { "op": "add", "value": "bar" } ],
"error": "missing 'path' parameter" },
{ "comment": "'path' parameter with null value",
"doc": {},
"patch": [ { "op": "add", "path": null, "value": "bar" } ],
"error": "null is not valid value for 'path'" },
{ "comment": "invalid JSON Pointer token",
"doc": {},
"patch": [ { "op": "add", "path": "foo", "value": "bar" } ],
"error": "JSON Pointer should start with a slash" },
{ "comment": "missing 'value' parameter to add",
"doc": [ 1 ],
"patch": [ { "op": "add", "path": "/-" } ],
"error": "missing 'value' parameter" },
{ "comment": "missing 'value' parameter to replace",
"doc": [ 1 ],
"patch": [ { "op": "replace", "path": "/0" } ],
"error": "missing 'value' parameter" },
{ "comment": "missing 'value' parameter to test",
"doc": [ null ],
"patch": [ { "op": "test", "path": "/0" } ],
"error": "missing 'value' parameter" },
{ "comment": "missing value parameter to test - where undef is falsy",
"doc": [ false ],
"patch": [ { "op": "test", "path": "/0" } ],
"error": "missing 'value' parameter" },
{ "comment": "missing from parameter to copy",
"doc": [ 1 ],
"patch": [ { "op": "copy", "path": "/-" } ],
"error": "missing 'from' parameter" },
{ "comment": "missing from location to copy",
"doc": { "foo": 1 },
"patch": [ { "op": "copy", "from": "/bar", "path": "/foo" } ],
"error": "missing 'from' location" },
{ "comment": "missing from parameter to move",
"doc": { "foo": 1 },
"patch": [ { "op": "move", "path": "" } ],
"error": "missing 'from' parameter" },
{ "comment": "missing from location to move",
"doc": { "foo": 1 },
"patch": [ { "op": "move", "from": "/bar", "path": "/foo" } ],
"error": "missing 'from' location" },
{ "comment": "duplicate ops, json-c parses this as op:move",
"doc": { "foo": "bar" },
"patch": [ { "op": "add", "path": "/baz", "value": "qux",
"op": "move", "from":"/foo" } ],
"error_wont_happen_in_jsonc": "patch has two 'op' members",
"expected": { "baz": "bar" }
},
{ "comment": "unrecognized op should fail",
"doc": {"foo": 1},
"patch": [{"op": "spam", "path": "/foo", "value": 1}],
"error": "Unrecognized op 'spam'" },
{ "comment": "test with bad array number that has leading zeros",
"doc": ["foo", "bar"],
"patch": [{"op": "test", "path": "/00", "value": "foo"}],
"error": "test op should reject the array value, it has leading zeros" },
{ "comment": "test with bad array number that has leading zeros",
"doc": ["foo", "bar"],
"patch": [{"op": "test", "path": "/01", "value": "bar"}],
"error": "test op should reject the array value, it has leading zeros" },
{ "comment": "Removing nonexistent field",
"doc": {"foo" : "bar"},
"patch": [{"op": "remove", "path": "/baz"}],
"error": "removing a nonexistent field should fail" },
{ "comment": "Removing deep nonexistent path",
"doc": {"foo" : "bar"},
"patch": [{"op": "remove", "path": "/missing1/missing2"}],
"error": "removing a nonexistent field should fail" },
{ "comment": "Removing nonexistent index",
"doc": ["foo", "bar"],
"patch": [{"op": "remove", "path": "/2"}],
"error": "removing a nonexistent index should fail" },
{ "comment": "Patch with different capitalisation than doc",
"doc": {"foo":"bar"},
"patch": [{"op": "add", "path": "/FOO", "value": "BAR"}],
"expected": {"foo": "bar", "FOO": "BAR"}
},
{ "comment": "copy must duplicate the value, not alias it, so no cycle can form",
"doc": {"a": {"p": {}}},
"patch": [{"op": "copy", "from": "/a", "path": "/b"},
{"op": "copy", "from": "/b", "path": "/a/p/x"}],
"expected": {"a": {"p": {"x": {"p": {}}}}, "b": {"p": {}}}
}
]
@@ -0,0 +1,76 @@
#This hack is needed for Windows tests
test_lib = static_library(
'jsonctest',
objects: libjson.extract_all_objects(
recursive: false,
),
install: false,
)
test_deps = declare_dependency(
include_directories: '..',
link_with: test_lib,
)
# List of test sources and expected output files
test_cases = [
['test1', 'test1.expected'],
['test2', 'test2.expected'],
['test4', 'test4.expected'],
['testReplaceExisting', 'testReplaceExisting.expected'],
['test_cast', 'test_cast.expected'],
['test_charcase', 'test_charcase.expected'],
['test_compare', 'test_compare.expected'],
['test_deep_copy', 'test_deep_copy.expected'],
['test_double_serializer', 'test_double_serializer.expected'],
['test_float', 'test_float.expected'],
['test_int_add', 'test_int_add.expected'],
['test_int_get', 'test_int_get.expected'],
['test_locale', 'test_locale.expected'],
['test_null', 'test_null.expected'],
['test_parse', 'test_parse.expected'],
['test_parse_int64', 'test_parse_int64.expected'],
['test_printbuf', 'test_printbuf.expected'],
['test_set_serializer', 'test_set_serializer.expected'],
['test_set_value', 'test_set_value.expected'],
['test_strerror', 'test_strerror.expected'],
['test_util_file', 'test_util_file.expected'],
['test_visit', 'test_visit.expected'],
['test_object_iterator', 'test_object_iterator.expected'],
['test_json_pointer', 'test_json_pointer.expected'],
['test_safe_json_pointer_set', 'test_safe_json_pointer_set.expected'],
['test_json_patch', 'test_json_patch.expected'],
]
# Copy expected files and test data
expected_files = []
foreach t : test_cases
expected_files += t[1]
endforeach
foreach f : expected_files + ['valid.json', 'valid_nested.json', 'json_patch_spec_tests.json', 'json_patch_tests.json']
configure_file(input: f, output: f, copy: true)
endforeach
# Build and register tests
special_args = {
'test_json_patch': ['.'],
'test_util_file': ['.'],
}
testdir = meson.current_build_dir()
message('Test data directory: ' + testdir)
foreach t : test_cases
name = t[0]
expected = t[1]
exe = executable(name, name + '.c',
dependencies: test_deps
)
test(name, exe,
args: special_args.get(name, []),
env: ['EXPECTED_FILE=' + meson.current_build_dir() / expected],
workdir: testdir
)
endforeach
@@ -0,0 +1,55 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "json.h"
#include "parse_flags.h"
#if !defined(HAVE_STRCASECMP) && defined(_MSC_VER)
#define strcasecmp _stricmp
#elif !defined(HAVE_STRCASECMP)
#error You do not have strcasecmp on your system.
#endif /* HAVE_STRNCASECMP */
static struct
{
const char *arg;
int flag;
} format_args[] = {
{"plain", JSON_C_TO_STRING_PLAIN},
{"spaced", JSON_C_TO_STRING_SPACED},
{"pretty", JSON_C_TO_STRING_PRETTY},
{"pretty_tab", JSON_C_TO_STRING_PRETTY_TAB},
};
#ifndef NELEM
#define NELEM(x) (sizeof(x) / sizeof(x[0]))
#endif
int parse_flags(int argc, char **argv)
{
int arg_idx;
int sflags = 0;
for (arg_idx = 1; arg_idx < argc; arg_idx++)
{
int jj;
for (jj = 0; jj < (int)NELEM(format_args); jj++)
{
if (strcasecmp(argv[arg_idx], format_args[jj].arg) == 0)
{
sflags |= format_args[jj].flag;
break;
}
}
if (jj == NELEM(format_args))
{
printf("Unknown arg: %s\n", argv[arg_idx]);
exit(1);
}
}
return sflags;
}
@@ -0,0 +1,4 @@
#ifndef __parse_flags_h
#define __parse_flags_h
int parse_flags(int argc, char **argv);
#endif
+144
View File
@@ -0,0 +1,144 @@
#!/bin/sh
if [ ! -z "$JSONC_TEST_TRACE" ] ; then
VERBOSE=1
set -x
set -v
fi
# Make sure srcdir is an absolute path. Supply the variable
# if it does not exist. We want to be able to run the tests
# stand-alone!!
#
srcdir=${srcdir-.}
if test ! -d $srcdir ; then
echo "test-defs.sh: installation error" 1>&2
exit 1
fi
# Use absolute paths
case "$srcdir" in
/* | [A-Za-z]:\\*) ;;
*) srcdir=`\cd $srcdir && pwd` ;;
esac
case "$top_builddir" in
/* | [A-Za-z]:\\*) ;;
*) top_builddir=`\cd ${top_builddir-..} && pwd` ;;
esac
top_builddir=${top_builddir}/tests
progname=`echo "$0" | sed 's,^.*/,,'`
testname=`echo "$progname" | sed 's,-.*$,,'`
testsubdir=${testsubdir-testSubDir}
testsubdir=${testsubdir}/${progname}
# User can set VERBOSE to cause output redirection
case "$VERBOSE" in
[Nn]|[Nn][Oo]|0|"")
VERBOSE=0
exec > /dev/null
;;
[Yy]|[Yy][Ee][Ss])
VERBOSE=1
;;
esac
rm -rf "$testsubdir" > /dev/null 2>&1
mkdir -p "$testsubdir"
CURDIR=$(pwd)
cd "$testsubdir" \
|| { echo "Cannot make or change into $testsubdir"; exit 1; }
echo "=== Running test $progname"
CMP="${CMP-cmp}"
use_valgrind=${USE_VALGRIND-1}
case "${use_valgrind}" in
[0Nn]*)
use_valgrind=0
;;
*)
use_valgrind=1
;;
esac
valgrind_path=$(which valgrind 2> /dev/null)
if [ -z "${valgrind_path}" -o ! -x "${valgrind_path}" ] ; then
use_valgrind=0
fi
#
# This is a common function to check the results of a test program
# that is intended to generate consistent output across runs.
#
# ${top_builddir} must be set to the top level build directory.
#
# Output will be written to the current directory.
#
# It must be passed the name of the test command to run, which must be present
# in the ${top_builddir} directory.
#
# It will compare the output of running that against <name of command>.expected
#
run_output_test()
{
EXPECTED_EXITVAL=0
if [ "$1" = "-o" ] ; then
TEST_OUTPUT="$2"
shift 2
fi
if [ "$1" = "--exit" ] ; then
EXPECTED_EXITVAL="$2"
shift 2
fi
TEST_COMMAND="$1"
shift
if [ -z "${TEST_OUTPUT}" ] ; then
TEST_OUTPUT=${TEST_COMMAND}
fi
REDIR_OUTPUT="> \"${TEST_OUTPUT}.out\" 2>&1"
if [ $VERBOSE -gt 1 ] ; then
REDIR_OUTPUT="2>&1 | tee \"${TEST_OUTPUT}.out\""
fi
if [ $use_valgrind -eq 1 ] ; then
eval valgrind --tool=memcheck \
--trace-children=yes \
--demangle=yes \
--log-file="${TEST_OUTPUT}.vg.out" \
--leak-check=full \
--show-reachable=yes \
--run-libc-freeres=yes \
"\"${top_builddir}/${TEST_COMMAND}\"" \"\$@\" ${REDIR_OUTPUT}
exitval=$?
else
eval "\"${top_builddir}/${TEST_COMMAND}"\" \"\$@\" ${REDIR_OUTPUT}
exitval=$?
fi
if [ $exitval -ne $EXPECTED_EXITVAL ] ; then
echo "ERROR: \"${TEST_COMMAND} $@\" exited with incorrect exit status: $err != $EXPECTED_EXITVAL" 1>&2
err=1
fi
if [ $use_valgrind -eq 1 ] ; then
if ! tail -1 "${TEST_OUTPUT}.vg.out" | grep -q "ERROR SUMMARY: 0 errors" ; then
echo "ERROR: valgrind found errors during execution:" 1>&2
cat "${TEST_OUTPUT}.vg.out"
err=1
fi
fi
if ! "$CMP" -s "${srcdir}/${TEST_OUTPUT}.expected" "${TEST_OUTPUT}.out" ; then
echo "ERROR: \"${TEST_COMMAND} $@\" (${TEST_OUTPUT}) failed (set VERBOSE=1 to see full output):" 1>&2
(cd "${CURDIR}" ; set -x ; diff "${srcdir}/${TEST_OUTPUT}.expected" "$testsubdir/${TEST_OUTPUT}.out")
echo "cp \"$testsubdir/${TEST_OUTPUT}.out\" \"${srcdir}/${TEST_OUTPUT}.expected\"" 1>&2
err=1
fi
return $err
}
@@ -0,0 +1,387 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "json.h"
#include "parse_flags.h"
static int sort_fn(const void *j1, const void *j2)
{
json_object *const *jso1, *const *jso2;
int i1, i2;
jso1 = (json_object *const *)j1;
jso2 = (json_object *const *)j2;
if (!*jso1 && !*jso2)
return 0;
if (!*jso1)
return -1;
if (!*jso2)
return 1;
i1 = json_object_get_int(*jso1);
i2 = json_object_get_int(*jso2);
return i1 - i2;
}
#ifdef TEST_FORMATTED
static const char *to_json_string(json_object *obj, int flags)
{
size_t length;
char *copy;
const char *result;
result = json_object_to_json_string_length(obj, flags, &length);
copy = strdup(result);
if (copy == NULL)
printf("to_json_string: Allocation failed!\n");
else
{
result = json_object_to_json_string_ext(obj, flags);
if (length != strlen(result))
printf("to_json_string: Length mismatch!\n");
if (strcmp(copy, result) != 0)
printf("to_json_string: Comparison Failed!\n");
free(copy);
}
return result;
}
#define json_object_to_json_string(obj) to_json_string(obj, sflags)
#else
/* no special define */
#endif
json_object *make_array(void);
json_object *make_array(void)
{
json_object *my_array;
my_array = json_object_new_array();
json_object_array_add(my_array, json_object_new_int(1));
json_object_array_add(my_array, json_object_new_int(2));
json_object_array_add(my_array, json_object_new_int(3));
json_object_array_put_idx(my_array, 4, json_object_new_int(5));
json_object_array_put_idx(my_array, 3, json_object_new_int(4));
json_object_array_put_idx(my_array, 6, json_object_new_int(7));
return my_array;
}
void test_array_del_idx(void);
void test_array_del_idx(void)
{
int rc;
size_t ii;
size_t orig_array_len;
json_object *my_array;
#ifdef TEST_FORMATTED
int sflags = 0;
#endif
my_array = make_array();
orig_array_len = json_object_array_length(my_array);
printf("my_array=\n");
for (ii = 0; ii < json_object_array_length(my_array); ii++)
{
json_object *obj = json_object_array_get_idx(my_array, ii);
printf("\t[%d]=%s\n", (int)ii, json_object_to_json_string(obj));
}
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
for (ii = 0; ii < orig_array_len; ii++)
{
rc = json_object_array_del_idx(my_array, 0, 1);
printf("after del_idx(0,1)=%d, my_array.to_string()=%s\n", rc,
json_object_to_json_string(my_array));
}
/* One more time, with the empty array: */
rc = json_object_array_del_idx(my_array, 0, 1);
printf("after del_idx(0,1)=%d, my_array.to_string()=%s\n", rc,
json_object_to_json_string(my_array));
json_object_put(my_array);
/* Delete all array indexes at once */
my_array = make_array();
rc = json_object_array_del_idx(my_array, 0, orig_array_len);
printf("after del_idx(0,%d)=%d, my_array.to_string()=%s\n", (int)orig_array_len, rc,
json_object_to_json_string(my_array));
json_object_put(my_array);
/* Delete *more* than all array indexes at once */
my_array = make_array();
rc = json_object_array_del_idx(my_array, 0, orig_array_len + 1);
printf("after del_idx(0,%d)=%d, my_array.to_string()=%s\n", (int)(orig_array_len + 1), rc,
json_object_to_json_string(my_array));
json_object_put(my_array);
/* Delete some array indexes, then add more */
my_array = make_array();
rc = json_object_array_del_idx(my_array, 0, orig_array_len - 1);
printf("after del_idx(0,%d)=%d, my_array.to_string()=%s\n", (int)(orig_array_len - 1), rc,
json_object_to_json_string(my_array));
json_object_array_add(my_array, json_object_new_string("s1"));
json_object_array_add(my_array, json_object_new_string("s2"));
json_object_array_add(my_array, json_object_new_string("s3"));
printf("after adding more entries, my_array.to_string()=%s\n",
json_object_to_json_string(my_array));
json_object_put(my_array);
}
void test_array_list_expand_internal(void);
void test_array_list_expand_internal(void)
{
int rc;
size_t ii;
size_t idx;
json_object *my_array;
#ifdef TEST_FORMATTED
int sflags = 0;
#endif
my_array = make_array();
printf("my_array=\n");
for (ii = 0; ii < json_object_array_length(my_array); ii++)
{
json_object *obj = json_object_array_get_idx(my_array, ii);
printf("\t[%d]=%s\n", (int)ii, json_object_to_json_string(obj));
}
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
/* Put iNdex < array->size, no expand. */
rc = json_object_array_put_idx(my_array, 5, json_object_new_int(6));
printf("put_idx(5,6)=%d\n", rc);
/* array->size < Put Index < array->size * 2 <= SIZE_T_MAX, the size = array->size * 2. */
idx = ARRAY_LIST_DEFAULT_SIZE * 2 - 1;
rc = json_object_array_put_idx(my_array, idx, json_object_new_int(0));
printf("put_idx(%d,0)=%d\n", (int)(idx), rc);
/* array->size * 2 < Put Index, the size = Put Index. */
idx = ARRAY_LIST_DEFAULT_SIZE * 2 * 2 + 1;
rc = json_object_array_put_idx(my_array, idx, json_object_new_int(0));
printf("put_idx(%d,0)=%d\n", (int)(idx), rc);
/* SIZE_T_MAX <= Put Index, it will fail and the size will no change. */
idx = SIZE_MAX; // SIZE_MAX = SIZE_T_MAX
json_object *tmp = json_object_new_int(10);
rc = json_object_array_put_idx(my_array, idx, tmp);
printf("put_idx(SIZE_T_MAX,0)=%d\n", rc);
if (rc == -1)
{
json_object_put(tmp);
}
json_object_put(my_array);
}
void test_array_insert_idx(void);
void test_array_insert_idx(void)
{
json_object *my_array;
struct json_object *jo1;
my_array = json_object_new_array();
json_object_array_add(my_array, json_object_new_int(1));
json_object_array_add(my_array, json_object_new_int(2));
json_object_array_add(my_array, json_object_new_int(5));
json_object_array_insert_idx(my_array, 2, json_object_new_int(4));
jo1 = json_tokener_parse("[1, 2, 4, 5]");
assert(1 == json_object_equal(my_array, jo1));
json_object_put(jo1);
json_object_array_insert_idx(my_array, 2, json_object_new_int(3));
jo1 = json_tokener_parse("[1, 2, 3, 4, 5]");
assert(1 == json_object_equal(my_array, jo1));
json_object_put(jo1);
json_object_array_insert_idx(my_array, 5, json_object_new_int(6));
jo1 = json_tokener_parse("[1, 2, 3, 4, 5, 6]");
assert(1 == json_object_equal(my_array, jo1));
json_object_put(jo1);
json_object_array_insert_idx(my_array, 7, json_object_new_int(8));
jo1 = json_tokener_parse("[1, 2, 3, 4, 5, 6, null, 8]");
assert(1 == json_object_equal(my_array, jo1));
json_object_put(jo1);
json_object_put(my_array);
}
int main(int argc, char **argv)
{
json_object *my_string, *my_int, *my_null, *my_object, *my_array;
size_t i;
#ifdef TEST_FORMATTED
int sflags = 0;
#endif
MC_SET_DEBUG(1);
#ifdef TEST_FORMATTED
sflags = parse_flags(argc, argv);
#endif
my_string = json_object_new_string("\t");
printf("my_string=%s\n", json_object_get_string(my_string));
printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
json_object_put(my_string);
my_string = json_object_new_string("\\");
printf("my_string=%s\n", json_object_get_string(my_string));
printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
json_object_put(my_string);
my_string = json_object_new_string("/");
printf("my_string=%s\n", json_object_get_string(my_string));
printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
printf("my_string.to_string(NOSLASHESCAPE)=%s\n",
json_object_to_json_string_ext(my_string, JSON_C_TO_STRING_NOSLASHESCAPE));
json_object_put(my_string);
my_string = json_object_new_string("/foo/bar/baz");
printf("my_string=%s\n", json_object_get_string(my_string));
printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
printf("my_string.to_string(NOSLASHESCAPE)=%s\n",
json_object_to_json_string_ext(my_string, JSON_C_TO_STRING_NOSLASHESCAPE));
json_object_put(my_string);
my_string = json_object_new_string("foo");
printf("my_string=%s\n", json_object_get_string(my_string));
printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
my_int = json_object_new_int(9);
printf("my_int=%d\n", json_object_get_int(my_int));
printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int));
my_null = json_object_new_null();
printf("my_null.to_string()=%s\n", json_object_to_json_string(my_null));
my_array = json_object_new_array();
json_object_array_add(my_array, json_object_new_int(1));
json_object_array_add(my_array, json_object_new_int(2));
json_object_array_add(my_array, json_object_new_int(3));
json_object_array_put_idx(my_array, 4, json_object_new_int(5));
printf("my_array=\n");
for (i = 0; i < json_object_array_length(my_array); i++)
{
json_object *obj = json_object_array_get_idx(my_array, i);
printf("\t[%d]=%s\n", (int)i, json_object_to_json_string(obj));
}
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
json_object_put(my_array);
test_array_insert_idx();
test_array_del_idx();
test_array_list_expand_internal();
my_array = json_object_new_array_ext(5);
json_object_array_add(my_array, json_object_new_int(3));
json_object_array_add(my_array, json_object_new_int(1));
json_object_array_add(my_array, json_object_new_int(2));
json_object_array_put_idx(my_array, 4, json_object_new_int(0));
printf("my_array=\n");
for (i = 0; i < json_object_array_length(my_array); i++)
{
json_object *obj = json_object_array_get_idx(my_array, i);
printf("\t[%d]=%s\n", (int)i, json_object_to_json_string(obj));
}
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
json_object_array_sort(my_array, sort_fn);
printf("my_array=\n");
for (i = 0; i < json_object_array_length(my_array); i++)
{
json_object *obj = json_object_array_get_idx(my_array, i);
printf("\t[%d]=%s\n", (int)i, json_object_to_json_string(obj));
}
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
json_object *one = json_object_new_int(1);
json_object *result = json_object_array_bsearch(one, my_array, sort_fn);
printf("find json_object(1) in my_array successfully: %s\n",
json_object_to_json_string(result));
json_object_put(one);
my_object = json_object_new_object();
int rc = json_object_object_add(my_object, "abc", my_object);
if (rc != -1)
{
printf("ERROR: able to successfully add object to itself!\n");
fflush(stdout);
}
json_object_object_add(my_object, "abc", json_object_new_int(12));
json_object_object_add(my_object, "foo", json_object_new_string("bar"));
json_object_object_add(my_object, "bool0", json_object_new_boolean(0));
json_object_object_add(my_object, "bool1", json_object_new_boolean(1));
json_object_object_add(my_object, "baz", json_object_new_string("bang"));
json_object *baz_obj = json_object_new_string("fark");
json_object_get(baz_obj);
json_object_object_add(my_object, "baz", baz_obj);
json_object_object_del(my_object, "baz");
/* baz_obj should still be valid */
printf("baz_obj.to_string()=%s\n", json_object_to_json_string(baz_obj));
json_object_put(baz_obj);
/*json_object_object_add(my_object, "arr", my_array);*/
printf("my_object=\n");
json_object_object_foreach(my_object, key, val)
{
printf("\t%s: %s\n", key, json_object_to_json_string(val));
}
json_object *empty_array = json_object_new_array();
json_object *empty_obj = json_object_new_object();
json_object_object_add(my_object, "empty_array", empty_array);
json_object_object_add(my_object, "empty_obj", empty_obj);
printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
json_object_put(my_array);
my_array = json_object_new_array_ext(INT_MIN + 1);
if (my_array != NULL)
{
printf("ERROR: able to allocate an array of negative size!\n");
fflush(stdout);
json_object_put(my_array);
my_array = NULL;
}
#if SIZEOF_SIZE_T == SIZEOF_INT
my_array = json_object_new_array_ext(INT_MAX / 2 + 2);
if (my_array != NULL)
{
printf("ERROR: able to allocate an array of insufficient size!\n");
fflush(stdout);
json_object_put(my_array);
my_array = NULL;
}
#endif
json_object_put(my_string);
json_object_put(my_int);
json_object_put(my_null);
json_object_put(my_object);
json_object_put(my_array);
return EXIT_SUCCESS;
}
@@ -0,0 +1,78 @@
my_string=
my_string.to_string()="\t"
my_string=\
my_string.to_string()="\\"
my_string=/
my_string.to_string()="\/"
my_string.to_string(NOSLASHESCAPE)="/"
my_string=/foo/bar/baz
my_string.to_string()="\/foo\/bar\/baz"
my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz"
my_string=foo
my_string.to_string()="foo"
my_int=9
my_int.to_string()=9
my_null.to_string()=null
my_array=
[0]=1
[1]=2
[2]=3
[3]=null
[4]=5
my_array.to_string()=[ 1, 2, 3, null, 5 ]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[ 1, 2, 3, 4, 5, null, 7 ]
after del_idx(0,1)=0, my_array.to_string()=[ 2, 3, 4, 5, null, 7 ]
after del_idx(0,1)=0, my_array.to_string()=[ 3, 4, 5, null, 7 ]
after del_idx(0,1)=0, my_array.to_string()=[ 4, 5, null, 7 ]
after del_idx(0,1)=0, my_array.to_string()=[ 5, null, 7 ]
after del_idx(0,1)=0, my_array.to_string()=[ null, 7 ]
after del_idx(0,1)=0, my_array.to_string()=[ 7 ]
after del_idx(0,1)=0, my_array.to_string()=[ ]
after del_idx(0,1)=-1, my_array.to_string()=[ ]
after del_idx(0,7)=0, my_array.to_string()=[ ]
after del_idx(0,8)=-1, my_array.to_string()=[ 1, 2, 3, 4, 5, null, 7 ]
after del_idx(0,6)=0, my_array.to_string()=[ 7 ]
after adding more entries, my_array.to_string()=[ 7, "s1", "s2", "s3" ]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[ 1, 2, 3, 4, 5, null, 7 ]
put_idx(5,6)=0
put_idx(63,0)=0
put_idx(129,0)=0
put_idx(SIZE_T_MAX,0)=-1
my_array=
[0]=3
[1]=1
[2]=2
[3]=null
[4]=0
my_array.to_string()=[ 3, 1, 2, null, 0 ]
my_array=
[0]=null
[1]=0
[2]=1
[3]=2
[4]=3
my_array.to_string()=[ null, 0, 1, 2, 3 ]
find json_object(1) in my_array successfully: 1
baz_obj.to_string()="fark"
my_object=
abc: 12
foo: "bar"
bool0: false
bool1: true
my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "empty_array": [ ], "empty_obj": { } }
+38
View File
@@ -0,0 +1,38 @@
#!/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 test1
_err=$?
for flag in plain spaced pretty ; do
run_output_test -o test1Formatted_${flag} test1Formatted ${flag}
_err2=$?
if [ $_err -eq 0 ] ; then
_err=$_err2
fi
done
# Spaced and pretty JSON string
run_output_test -o test1Formatted_spaced_pretty \
test1Formatted spaced pretty
_err2=$?
if [ $_err -eq 0 ] ; then
_err=$_err2
fi
# Spaced and pretty JSON string using tabs
run_output_test -o test1Formatted_spaced_pretty_pretty_tab \
test1Formatted spaced pretty pretty_tab
_err2=$?
if [ $_err -eq 0 ] ; then
_err=$_err2
fi
exit $_err
@@ -0,0 +1,78 @@
my_string=
my_string.to_string()="\t"
my_string=\
my_string.to_string()="\\"
my_string=/
my_string.to_string()="\/"
my_string.to_string(NOSLASHESCAPE)="/"
my_string=/foo/bar/baz
my_string.to_string()="\/foo\/bar\/baz"
my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz"
my_string=foo
my_string.to_string()="foo"
my_int=9
my_int.to_string()=9
my_null.to_string()=null
my_array=
[0]=1
[1]=2
[2]=3
[3]=null
[4]=5
my_array.to_string()=[1,2,3,null,5]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[2,3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[null,7]
after del_idx(0,1)=0, my_array.to_string()=[7]
after del_idx(0,1)=0, my_array.to_string()=[]
after del_idx(0,1)=-1, my_array.to_string()=[]
after del_idx(0,7)=0, my_array.to_string()=[]
after del_idx(0,8)=-1, my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,6)=0, my_array.to_string()=[7]
after adding more entries, my_array.to_string()=[7,"s1","s2","s3"]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
put_idx(5,6)=0
put_idx(63,0)=0
put_idx(129,0)=0
put_idx(SIZE_T_MAX,0)=-1
my_array=
[0]=3
[1]=1
[2]=2
[3]=null
[4]=0
my_array.to_string()=[3,1,2,null,0]
my_array=
[0]=null
[1]=0
[2]=1
[3]=2
[4]=3
my_array.to_string()=[null,0,1,2,3]
find json_object(1) in my_array successfully: 1
baz_obj.to_string()="fark"
my_object=
abc: 12
foo: "bar"
bool0: false
bool1: true
my_object.to_string()={"abc":12,"foo":"bar","bool0":false,"bool1":true,"empty_array":[],"empty_obj":{}}
@@ -0,0 +1,103 @@
my_string=
my_string.to_string()="\t"
my_string=\
my_string.to_string()="\\"
my_string=/
my_string.to_string()="\/"
my_string.to_string(NOSLASHESCAPE)="/"
my_string=/foo/bar/baz
my_string.to_string()="\/foo\/bar\/baz"
my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz"
my_string=foo
my_string.to_string()="foo"
my_int=9
my_int.to_string()=9
my_null.to_string()=null
my_array=
[0]=1
[1]=2
[2]=3
[3]=null
[4]=5
my_array.to_string()=[
1,
2,
3,
null,
5
]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[2,3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[null,7]
after del_idx(0,1)=0, my_array.to_string()=[7]
after del_idx(0,1)=0, my_array.to_string()=[]
after del_idx(0,1)=-1, my_array.to_string()=[]
after del_idx(0,7)=0, my_array.to_string()=[]
after del_idx(0,8)=-1, my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,6)=0, my_array.to_string()=[7]
after adding more entries, my_array.to_string()=[7,"s1","s2","s3"]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
put_idx(5,6)=0
put_idx(63,0)=0
put_idx(129,0)=0
put_idx(SIZE_T_MAX,0)=-1
my_array=
[0]=3
[1]=1
[2]=2
[3]=null
[4]=0
my_array.to_string()=[
3,
1,
2,
null,
0
]
my_array=
[0]=null
[1]=0
[2]=1
[3]=2
[4]=3
my_array.to_string()=[
null,
0,
1,
2,
3
]
find json_object(1) in my_array successfully: 1
baz_obj.to_string()="fark"
my_object=
abc: 12
foo: "bar"
bool0: false
bool1: true
my_object.to_string()={
"abc":12,
"foo":"bar",
"bool0":false,
"bool1":true,
"empty_array":[],
"empty_obj":{}
}
@@ -0,0 +1,78 @@
my_string=
my_string.to_string()="\t"
my_string=\
my_string.to_string()="\\"
my_string=/
my_string.to_string()="\/"
my_string.to_string(NOSLASHESCAPE)="/"
my_string=/foo/bar/baz
my_string.to_string()="\/foo\/bar\/baz"
my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz"
my_string=foo
my_string.to_string()="foo"
my_int=9
my_int.to_string()=9
my_null.to_string()=null
my_array=
[0]=1
[1]=2
[2]=3
[3]=null
[4]=5
my_array.to_string()=[ 1, 2, 3, null, 5 ]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[2,3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[null,7]
after del_idx(0,1)=0, my_array.to_string()=[7]
after del_idx(0,1)=0, my_array.to_string()=[]
after del_idx(0,1)=-1, my_array.to_string()=[]
after del_idx(0,7)=0, my_array.to_string()=[]
after del_idx(0,8)=-1, my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,6)=0, my_array.to_string()=[7]
after adding more entries, my_array.to_string()=[7,"s1","s2","s3"]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
put_idx(5,6)=0
put_idx(63,0)=0
put_idx(129,0)=0
put_idx(SIZE_T_MAX,0)=-1
my_array=
[0]=3
[1]=1
[2]=2
[3]=null
[4]=0
my_array.to_string()=[ 3, 1, 2, null, 0 ]
my_array=
[0]=null
[1]=0
[2]=1
[3]=2
[4]=3
my_array.to_string()=[ null, 0, 1, 2, 3 ]
find json_object(1) in my_array successfully: 1
baz_obj.to_string()="fark"
my_object=
abc: 12
foo: "bar"
bool0: false
bool1: true
my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "empty_array": [ ], "empty_obj": { } }
@@ -0,0 +1,103 @@
my_string=
my_string.to_string()="\t"
my_string=\
my_string.to_string()="\\"
my_string=/
my_string.to_string()="\/"
my_string.to_string(NOSLASHESCAPE)="/"
my_string=/foo/bar/baz
my_string.to_string()="\/foo\/bar\/baz"
my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz"
my_string=foo
my_string.to_string()="foo"
my_int=9
my_int.to_string()=9
my_null.to_string()=null
my_array=
[0]=1
[1]=2
[2]=3
[3]=null
[4]=5
my_array.to_string()=[
1,
2,
3,
null,
5
]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[2,3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[null,7]
after del_idx(0,1)=0, my_array.to_string()=[7]
after del_idx(0,1)=0, my_array.to_string()=[]
after del_idx(0,1)=-1, my_array.to_string()=[]
after del_idx(0,7)=0, my_array.to_string()=[]
after del_idx(0,8)=-1, my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,6)=0, my_array.to_string()=[7]
after adding more entries, my_array.to_string()=[7,"s1","s2","s3"]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
put_idx(5,6)=0
put_idx(63,0)=0
put_idx(129,0)=0
put_idx(SIZE_T_MAX,0)=-1
my_array=
[0]=3
[1]=1
[2]=2
[3]=null
[4]=0
my_array.to_string()=[
3,
1,
2,
null,
0
]
my_array=
[0]=null
[1]=0
[2]=1
[3]=2
[4]=3
my_array.to_string()=[
null,
0,
1,
2,
3
]
find json_object(1) in my_array successfully: 1
baz_obj.to_string()="fark"
my_object=
abc: 12
foo: "bar"
bool0: false
bool1: true
my_object.to_string()={
"abc": 12,
"foo": "bar",
"bool0": false,
"bool1": true,
"empty_array": [],
"empty_obj": {}
}
@@ -0,0 +1,103 @@
my_string=
my_string.to_string()="\t"
my_string=\
my_string.to_string()="\\"
my_string=/
my_string.to_string()="\/"
my_string.to_string(NOSLASHESCAPE)="/"
my_string=/foo/bar/baz
my_string.to_string()="\/foo\/bar\/baz"
my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz"
my_string=foo
my_string.to_string()="foo"
my_int=9
my_int.to_string()=9
my_null.to_string()=null
my_array=
[0]=1
[1]=2
[2]=3
[3]=null
[4]=5
my_array.to_string()=[
1,
2,
3,
null,
5
]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[2,3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[null,7]
after del_idx(0,1)=0, my_array.to_string()=[7]
after del_idx(0,1)=0, my_array.to_string()=[]
after del_idx(0,1)=-1, my_array.to_string()=[]
after del_idx(0,7)=0, my_array.to_string()=[]
after del_idx(0,8)=-1, my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,6)=0, my_array.to_string()=[7]
after adding more entries, my_array.to_string()=[7,"s1","s2","s3"]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
put_idx(5,6)=0
put_idx(63,0)=0
put_idx(129,0)=0
put_idx(SIZE_T_MAX,0)=-1
my_array=
[0]=3
[1]=1
[2]=2
[3]=null
[4]=0
my_array.to_string()=[
3,
1,
2,
null,
0
]
my_array=
[0]=null
[1]=0
[2]=1
[3]=2
[4]=3
my_array.to_string()=[
null,
0,
1,
2,
3
]
find json_object(1) in my_array successfully: 1
baz_obj.to_string()="fark"
my_object=
abc: 12
foo: "bar"
bool0: false
bool1: true
my_object.to_string()={
"abc": 12,
"foo": "bar",
"bool0": false,
"bool1": true,
"empty_array": [],
"empty_obj": {}
}
@@ -0,0 +1,42 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "json.h"
#include "parse_flags.h"
#ifdef TEST_FORMATTED
#define json_object_to_json_string(obj) json_object_to_json_string_ext(obj, sflags)
#else
/* no special define */
#endif
int main(int argc, char **argv)
{
json_object *new_obj;
#ifdef TEST_FORMATTED
int sflags = 0;
#endif
MC_SET_DEBUG(1);
#ifdef TEST_FORMATTED
sflags = parse_flags(argc, argv);
#endif
new_obj = json_tokener_parse(
"/* more difficult test case */"
"{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", "
"\"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard "
"Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", "
"\"GlossDef\": \"A meta-markup language, used to create markup languages such as "
"DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);
return EXIT_SUCCESS;
}
@@ -0,0 +1 @@
new_obj.to_string()={ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": [ { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": [ "GML", "XML", "markup" ] } ] } } }
+38
View File
@@ -0,0 +1,38 @@
#!/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 test2
_err=$?
for flag in plain spaced pretty ; do
run_output_test -o test2Formatted_${flag} test2Formatted ${flag}
_err2=$?
if [ $_err -eq 0 ] ; then
_err=$_err2
fi
done
# Spaced and pretty JSON string
run_output_test -o test2Formatted_spaced_pretty \
test2Formatted spaced pretty
_err2=$?
if [ $_err -eq 0 ] ; then
_err=$_err2
fi
# Spaced and pretty JSON string using tabs
run_output_test -o test2Formatted_spaced_pretty_pretty_tab \
test2Formatted spaced pretty pretty_tab
_err2=$?
if [ $_err -eq 0 ] ; then
_err=$_err2
fi
exit $_err
@@ -0,0 +1 @@
new_obj.to_string()={"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":[{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML","markup"]}]}}}
@@ -0,0 +1,23 @@
new_obj.to_string()={
"glossary":{
"title":"example glossary",
"GlossDiv":{
"title":"S",
"GlossList":[
{
"ID":"SGML",
"SortAs":"SGML",
"GlossTerm":"Standard Generalized Markup Language",
"Acronym":"SGML",
"Abbrev":"ISO 8879:1986",
"GlossDef":"A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso":[
"GML",
"XML",
"markup"
]
}
]
}
}
}
@@ -0,0 +1 @@
new_obj.to_string()={ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": [ { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": [ "GML", "XML", "markup" ] } ] } } }
@@ -0,0 +1,23 @@
new_obj.to_string()={
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": [
{
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML",
"markup"
]
}
]
}
}
}
@@ -0,0 +1,23 @@
new_obj.to_string()={
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": [
{
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML",
"markup"
]
}
]
}
}
}
@@ -0,0 +1,88 @@
/*
* gcc -o utf8 utf8.c -I/home/y/include -L./.libs -ljson
*/
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "json_inttypes.h"
#include "json_object.h"
#include "json_tokener.h"
#include "snprintf_compat.h"
void print_hex(const char *s)
{
const char *iter = s;
unsigned char ch;
while ((ch = *iter++) != 0)
{
if (',' != ch)
printf("%x ", ch);
else
printf(",");
}
putchar('\n');
}
static void test_lot_of_adds(void);
static void test_lot_of_adds(void)
{
int ii;
char key[50];
json_object *jobj = json_object_new_object();
assert(jobj != NULL);
for (ii = 0; ii < 500; ii++)
{
snprintf(key, sizeof(key), "k%d", ii);
json_object *iobj = json_object_new_int(ii);
assert(iobj != NULL);
if (json_object_object_add(jobj, key, iobj))
{
fprintf(stderr, "FAILED to add object #%d\n", ii);
abort();
}
}
printf("%s\n", json_object_to_json_string(jobj));
assert(json_object_object_length(jobj) == 500);
json_object_put(jobj);
}
int main(void)
{
const char *input = "\"\\ud840\\udd26,\\ud840\\udd27,\\ud800\\udd26,\\ud800\\udd27\"";
const char *expected =
"\xF0\xA0\x84\xA6,\xF0\xA0\x84\xA7,\xF0\x90\x84\xA6,\xF0\x90\x84\xA7";
struct json_object *parse_result = json_tokener_parse(input);
const char *unjson = json_object_get_string(parse_result);
printf("input: %s\n", input);
int strings_match = !strcmp(expected, unjson);
int retval = 0;
if (strings_match)
{
printf("JSON parse result is correct: %s\n", unjson);
puts("PASS");
}
else
{
printf("JSON parse result doesn't match expected string\n");
printf("expected string bytes: ");
print_hex(expected);
printf("parsed string bytes: ");
print_hex(unjson);
puts("FAIL");
retval = 1;
}
json_object_put(parse_result);
test_lot_of_adds();
return retval;
}
File diff suppressed because one or more lines are too long
+1
View File
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,81 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "json.h"
int main(int argc, char **argv)
{
MC_SET_DEBUG(1);
/*
* Check that replacing an existing object keeps the key valid,
* and that it keeps the order the same.
*/
json_object *my_object = json_object_new_object();
json_object_object_add(my_object, "foo1", json_object_new_string("bar1"));
json_object_object_add(my_object, "foo2", json_object_new_string("bar2"));
json_object_object_add(my_object, "deleteme", json_object_new_string("bar2"));
json_object_object_add(my_object, "foo3", json_object_new_string("bar3"));
printf("==== delete-in-loop test starting ====\n");
int orig_count = 0;
json_object_object_foreach(my_object, key0, val0)
{
printf("Key at index %d is [%s] %d", orig_count, key0, (val0 == NULL));
if (strcmp(key0, "deleteme") == 0)
{
json_object_object_del(my_object, key0);
printf(" (deleted)\n");
}
else
printf(" (kept)\n");
orig_count++;
}
printf("==== replace-value first loop starting ====\n");
const char *original_key = NULL;
orig_count = 0;
json_object_object_foreach(my_object, key, val)
{
printf("Key at index %d is [%s] %d\n", orig_count, key, (val == NULL));
orig_count++;
if (strcmp(key, "foo2") != 0)
continue;
printf("replacing value for key [%s]\n", key);
original_key = key;
json_object_object_add(my_object, key, json_object_new_string("zzz"));
}
printf("==== second loop starting ====\n");
int new_count = 0;
int retval = 0;
json_object_object_foreach(my_object, key2, val2)
{
printf("Key at index %d is [%s] %d\n", new_count, key2, (val2 == NULL));
new_count++;
if (strcmp(key2, "foo2") != 0)
continue;
printf("pointer for key [%s] does %smatch\n", key2,
(key2 == original_key) ? "" : "NOT ");
if (key2 != original_key)
retval = 1;
}
if (new_count != orig_count)
{
printf("mismatch between original count (%d) and new count (%d)\n", orig_count,
new_count);
retval = 1;
}
json_object_put(my_object);
return retval;
}
@@ -0,0 +1,15 @@
==== delete-in-loop test starting ====
Key at index 0 is [foo1] 0 (kept)
Key at index 1 is [foo2] 0 (kept)
Key at index 2 is [deleteme] 0 (deleted)
Key at index 3 is [foo3] 0 (kept)
==== replace-value first loop starting ====
Key at index 0 is [foo1] 0
Key at index 1 is [foo2] 0
replacing value for key [foo2]
Key at index 2 is [foo3] 0
==== second loop starting ====
Key at index 0 is [foo1] 0
Key at index 1 is [foo2] 0
pointer for key [foo2] does match
Key at index 2 is [foo3] 0
@@ -0,0 +1 @@
test_basic.test
+21
View File
@@ -0,0 +1,21 @@
#!/bin/sh
export _JSON_C_STRERROR_ENABLE=1
# Common definitions
if test -z "$srcdir"; then
srcdir="${0%/*}"
test "$srcdir" = "$0" && srcdir=.
test -z "$srcdir" && srcdir=.
fi
. "$srcdir/test-defs.sh"
filename=$(basename "$0")
filename="${filename%.*}"
# This is only for the test_util_file.test ;
# more stuff could be extended
cp -f "$srcdir/valid.json" .
run_output_test $filename "$srcdir"
exit $?
@@ -0,0 +1,121 @@
/*
* Tests if casting within the json_object_get_* functions work correctly.
* Also checks the json_object_get_type and json_object_is_type functions.
*/
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "json_inttypes.h"
#include "json_object.h"
#include "json_tokener.h"
#include "json_util.h"
static void getit(struct json_object *new_obj, const char *field);
static void checktype_header(void);
static void checktype(struct json_object *new_obj, const char *field);
int main(int argc, char **argv)
{
const char *input = "{\n\
\"string_of_digits\": \"123\",\n\
\"regular_number\": 222,\n\
\"decimal_number\": 99.55,\n\
\"boolean_true\": true,\n\
\"boolean_false\": false,\n\
\"int64_number\": 2147483649,\n\
\"negative_number\": -321321321,\n\
\"a_null\": null,\n\
\"empty_array\": [],\n\
\"nonempty_array\": [ 123 ],\n\
\"array_with_zero\": [ 0 ],\n\
\"empty_object\": {},\n\
\"nonempty_object\": { \"a\": 123 },\n\
\"nan\": NaN,\n\
}";
/* Note: 2147483649 = INT_MAX + 2 */
/* Note: 9223372036854775809 = INT64_MAX + 2 */
/* Note: 18446744073709551617 = UINT64_MAX + 2 */
struct json_object *new_obj;
new_obj = json_tokener_parse(input);
printf("Parsed input: %s\n", input);
printf("Result is %s\n", (new_obj == NULL) ? "NULL (error!)" : "not NULL");
if (!new_obj)
return 1; // oops, we failed.
getit(new_obj, "string_of_digits");
getit(new_obj, "regular_number");
getit(new_obj, "decimal_number");
getit(new_obj, "boolean_true");
getit(new_obj, "boolean_false");
getit(new_obj, "int64_number");
getit(new_obj, "negative_number");
getit(new_obj, "a_null");
getit(new_obj, "empty_array");
getit(new_obj, "nonempty_array");
getit(new_obj, "array_with_zero");
getit(new_obj, "empty_object");
getit(new_obj, "nonempty_object");
getit(new_obj, "nan");
// Now check the behaviour of the json_object_is_type() function.
printf("\n================================\n");
checktype_header();
checktype(new_obj, NULL);
checktype(new_obj, "string_of_digits");
checktype(new_obj, "regular_number");
checktype(new_obj, "decimal_number");
checktype(new_obj, "boolean_true");
checktype(new_obj, "boolean_false");
checktype(new_obj, "int64_number");
checktype(new_obj, "negative_number");
checktype(new_obj, "a_null");
checktype(new_obj, "nan");
json_object_put(new_obj);
return 0;
}
static void getit(struct json_object *new_obj, const char *field)
{
struct json_object *o = NULL;
if (!json_object_object_get_ex(new_obj, field, &o))
printf("Field %s does not exist\n", field);
enum json_type o_type = json_object_get_type(o);
printf("new_obj.%s json_object_get_type()=%s\n", field, json_type_to_name(o_type));
printf("new_obj.%s json_object_get_int()=%d\n", field, json_object_get_int(o));
printf("new_obj.%s json_object_get_int64()=%" PRId64 "\n", field, json_object_get_int64(o));
printf("new_obj.%s json_object_get_uint64()=%" PRIu64 "\n", field,
json_object_get_uint64(o));
printf("new_obj.%s json_object_get_boolean()=%d\n", field, json_object_get_boolean(o));
printf("new_obj.%s json_object_get_double()=%f\n", field, json_object_get_double(o));
}
static void checktype_header(void)
{
printf("json_object_is_type: %s,%s,%s,%s,%s,%s,%s\n", json_type_to_name(json_type_null),
json_type_to_name(json_type_boolean), json_type_to_name(json_type_double),
json_type_to_name(json_type_int), json_type_to_name(json_type_object),
json_type_to_name(json_type_array), json_type_to_name(json_type_string));
}
static void checktype(struct json_object *new_obj, const char *field)
{
struct json_object *o = new_obj;
if (field && !json_object_object_get_ex(new_obj, field, &o))
printf("Field %s does not exist\n", field);
printf("new_obj%s%-18s: %d,%d,%d,%d,%d,%d,%d\n", field ? "." : " ", field ? field : "",
json_object_is_type(o, json_type_null), json_object_is_type(o, json_type_boolean),
json_object_is_type(o, json_type_double), json_object_is_type(o, json_type_int),
json_object_is_type(o, json_type_object), json_object_is_type(o, json_type_array),
json_object_is_type(o, json_type_string));
}
@@ -0,0 +1,114 @@
Parsed input: {
"string_of_digits": "123",
"regular_number": 222,
"decimal_number": 99.55,
"boolean_true": true,
"boolean_false": false,
"int64_number": 2147483649,
"negative_number": -321321321,
"a_null": null,
"empty_array": [],
"nonempty_array": [ 123 ],
"array_with_zero": [ 0 ],
"empty_object": {},
"nonempty_object": { "a": 123 },
"nan": NaN,
}
Result is not NULL
new_obj.string_of_digits json_object_get_type()=string
new_obj.string_of_digits json_object_get_int()=123
new_obj.string_of_digits json_object_get_int64()=123
new_obj.string_of_digits json_object_get_uint64()=123
new_obj.string_of_digits json_object_get_boolean()=1
new_obj.string_of_digits json_object_get_double()=123.000000
new_obj.regular_number json_object_get_type()=int
new_obj.regular_number json_object_get_int()=222
new_obj.regular_number json_object_get_int64()=222
new_obj.regular_number json_object_get_uint64()=222
new_obj.regular_number json_object_get_boolean()=1
new_obj.regular_number json_object_get_double()=222.000000
new_obj.decimal_number json_object_get_type()=double
new_obj.decimal_number json_object_get_int()=99
new_obj.decimal_number json_object_get_int64()=99
new_obj.decimal_number json_object_get_uint64()=99
new_obj.decimal_number json_object_get_boolean()=1
new_obj.decimal_number json_object_get_double()=99.550000
new_obj.boolean_true json_object_get_type()=boolean
new_obj.boolean_true json_object_get_int()=1
new_obj.boolean_true json_object_get_int64()=1
new_obj.boolean_true json_object_get_uint64()=1
new_obj.boolean_true json_object_get_boolean()=1
new_obj.boolean_true json_object_get_double()=1.000000
new_obj.boolean_false json_object_get_type()=boolean
new_obj.boolean_false json_object_get_int()=0
new_obj.boolean_false json_object_get_int64()=0
new_obj.boolean_false json_object_get_uint64()=0
new_obj.boolean_false json_object_get_boolean()=0
new_obj.boolean_false json_object_get_double()=0.000000
new_obj.int64_number json_object_get_type()=int
new_obj.int64_number json_object_get_int()=2147483647
new_obj.int64_number json_object_get_int64()=2147483649
new_obj.int64_number json_object_get_uint64()=2147483649
new_obj.int64_number json_object_get_boolean()=1
new_obj.int64_number json_object_get_double()=2147483649.000000
new_obj.negative_number json_object_get_type()=int
new_obj.negative_number json_object_get_int()=-321321321
new_obj.negative_number json_object_get_int64()=-321321321
new_obj.negative_number json_object_get_uint64()=0
new_obj.negative_number json_object_get_boolean()=1
new_obj.negative_number json_object_get_double()=-321321321.000000
new_obj.a_null json_object_get_type()=null
new_obj.a_null json_object_get_int()=0
new_obj.a_null json_object_get_int64()=0
new_obj.a_null json_object_get_uint64()=0
new_obj.a_null json_object_get_boolean()=0
new_obj.a_null json_object_get_double()=0.000000
new_obj.empty_array json_object_get_type()=array
new_obj.empty_array json_object_get_int()=0
new_obj.empty_array json_object_get_int64()=0
new_obj.empty_array json_object_get_uint64()=0
new_obj.empty_array json_object_get_boolean()=0
new_obj.empty_array json_object_get_double()=0.000000
new_obj.nonempty_array json_object_get_type()=array
new_obj.nonempty_array json_object_get_int()=0
new_obj.nonempty_array json_object_get_int64()=0
new_obj.nonempty_array json_object_get_uint64()=0
new_obj.nonempty_array json_object_get_boolean()=0
new_obj.nonempty_array json_object_get_double()=0.000000
new_obj.array_with_zero json_object_get_type()=array
new_obj.array_with_zero json_object_get_int()=0
new_obj.array_with_zero json_object_get_int64()=0
new_obj.array_with_zero json_object_get_uint64()=0
new_obj.array_with_zero json_object_get_boolean()=0
new_obj.array_with_zero json_object_get_double()=0.000000
new_obj.empty_object json_object_get_type()=object
new_obj.empty_object json_object_get_int()=0
new_obj.empty_object json_object_get_int64()=0
new_obj.empty_object json_object_get_uint64()=0
new_obj.empty_object json_object_get_boolean()=0
new_obj.empty_object json_object_get_double()=0.000000
new_obj.nonempty_object json_object_get_type()=object
new_obj.nonempty_object json_object_get_int()=0
new_obj.nonempty_object json_object_get_int64()=0
new_obj.nonempty_object json_object_get_uint64()=0
new_obj.nonempty_object json_object_get_boolean()=0
new_obj.nonempty_object json_object_get_double()=0.000000
new_obj.nan json_object_get_type()=double
new_obj.nan json_object_get_int()=-2147483648
new_obj.nan json_object_get_int64()=-9223372036854775808
new_obj.nan json_object_get_uint64()=0
new_obj.nan json_object_get_boolean()=1
new_obj.nan json_object_get_double()=nan
================================
json_object_is_type: null,boolean,double,int,object,array,string
new_obj : 0,0,0,0,1,0,0
new_obj.string_of_digits : 0,0,0,0,0,0,1
new_obj.regular_number : 0,0,0,1,0,0,0
new_obj.decimal_number : 0,0,1,0,0,0,0
new_obj.boolean_true : 0,1,0,0,0,0,0
new_obj.boolean_false : 0,1,0,0,0,0,0
new_obj.int64_number : 0,0,0,1,0,0,0
new_obj.negative_number : 0,0,0,1,0,0,0
new_obj.a_null : 1,0,0,0,0,0,0
new_obj.nan : 0,0,1,0,0,0,0
+1
View File
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,45 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "json.h"
#include "json_tokener.h"
static void test_case_parse(void);
int main(int argc, char **argv)
{
MC_SET_DEBUG(1);
test_case_parse();
return 0;
}
/* make sure only lowercase forms are parsed in strict mode */
static void test_case_parse(void)
{
struct json_tokener *tok;
json_object *new_obj;
tok = json_tokener_new();
json_tokener_set_flags(tok, JSON_TOKENER_STRICT);
new_obj = json_tokener_parse_ex(tok, "True", 4);
assert(new_obj == NULL);
new_obj = json_tokener_parse_ex(tok, "False", 5);
assert(new_obj == NULL);
new_obj = json_tokener_parse_ex(tok, "Null", 4);
assert(new_obj == NULL);
printf("OK\n");
json_tokener_free(tok);
}
@@ -0,0 +1 @@
OK
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,260 @@
/*
* Tests if json_object_equal behaves correct.
*/
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "json_inttypes.h"
#include "json_object.h"
int main(int argc, char **argv)
{
/* integer tests */
struct json_object *int1 = json_object_new_int(0);
struct json_object *int2 = json_object_new_int(1);
struct json_object *int3 = json_object_new_int(1);
struct json_object *int4 = json_object_new_int(-1);
struct json_object *uint1 = json_object_new_uint64(0);
struct json_object *uint2 = json_object_new_uint64(1);
struct json_object *uint3 = json_object_new_uint64(1);
struct json_object *uint4 = json_object_new_uint64((uint64_t)INT64_MAX + 100);
if (!json_object_equal(int1, int2))
printf("JSON integer comparison is correct\n");
else
printf("JSON integer comparison failed\n");
if (json_object_equal(int1, int1))
printf("JSON same object comparison is correct\n");
else
printf("JSON same object comparison failed\n");
if (json_object_equal(int2, int3))
printf("JSON same integer comparison is correct\n");
else
printf("JSON same integer comparison failed\n");
if (!json_object_equal(uint1, uint2))
printf("JSON usigned integer comparison is correct\n");
else
printf("JSON usigned integer comparison failed\n");
if (json_object_equal(uint1, uint1))
printf("JSON same usigned object comparison is correct\n");
else
printf("JSON same usigned object comparison failed\n");
if (json_object_equal(uint2, uint3))
printf("JSON same usigned integer comparison is correct\n");
else
printf("JSON same usigned integer comparison failed\n");
if (json_object_equal(int2, uint2))
printf("JSON integer & usigned integer comparison is correct\n");
else
printf("JSON integer & usigned integer comparison failed\n");
if (!json_object_equal(int2, uint4))
printf("JSON integer & usigned integer comparison is correct\n");
else
printf("JSON integer & usigned integer comparison failed\n");
if (!json_object_equal(int4, uint2))
printf("JSON integer & usigned integer comparison is correct\n");
else
printf("JSON integer & usigned integer comparison failed\n");
if (!json_object_equal(int4, uint4))
printf("JSON integer & usigned integer comparison is correct\n");
else
printf("JSON integer & usigned integer comparison failed\n");
if (json_object_equal(uint2, int2))
printf("JSON usigned integer & integer comparison is correct\n");
else
printf("JSON usigned integer & integer comparison failed\n");
if (!json_object_equal(uint2, int4))
printf("JSON usigned integer & integer comparison is correct\n");
else
printf("JSON usigned integer & integer comparison failed\n");
if (!json_object_equal(uint4, int2))
printf("JSON usigned integer & integer comparison is correct\n");
else
printf("JSON usigned integer & integer comparison failed\n");
if (!json_object_equal(uint4, int4))
printf("JSON usigned integer & integer comparison is correct\n");
else
printf("JSON usigned integer & integer comparison failed\n");
json_object_put(int1);
json_object_put(int2);
json_object_put(int3);
json_object_put(int4);
json_object_put(uint1);
json_object_put(uint2);
json_object_put(uint3);
json_object_put(uint4);
/* string tests */
struct json_object *str1 = json_object_new_string("TESTSTRING");
struct json_object *str2 = json_object_new_string("TESTSTRING");
struct json_object *str3 = json_object_new_string("DIFFERENT");
if (json_object_equal(str1, str2))
printf("Comparing equal strings is correct\n");
else
printf("Comparing equal strings failed\n");
if (!json_object_equal(str1, str3))
printf("Comparing different strings is correct\n");
else
printf("Comparing different strings failed\n");
json_object_put(str1);
json_object_put(str2);
json_object_put(str3);
/* double tests */
struct json_object *dbl1 = json_object_new_double(3.14159);
struct json_object *dbl2 = json_object_new_double(3.14159);
struct json_object *dbl3 = json_object_new_double(3.0);
if (json_object_equal(dbl1, dbl2))
printf("Comparing equal doubles is correct\n");
else
printf("Comparing equal doubles failed\n");
if (!json_object_equal(dbl1, dbl3))
printf("Comparing different doubles is correct\n");
else
printf("Comparing different doubles failed\n");
json_object_put(dbl1);
json_object_put(dbl2);
json_object_put(dbl3);
/* array tests */
struct json_object *ar1 = json_object_new_array();
struct json_object *ar2 = json_object_new_array();
struct json_object *ar3 = json_object_new_array();
struct json_object *ar4 = json_object_new_array();
json_object_array_add(ar1, json_object_new_int(1));
json_object_array_add(ar1, json_object_new_int(2));
json_object_array_add(ar2, json_object_new_int(1));
json_object_array_add(ar2, json_object_new_int(2));
json_object_array_add(ar3, json_object_new_int(1));
json_object_array_add(ar3, json_object_new_int(1));
if (json_object_equal(ar1, ar2))
printf("Comparing equal arrays is correct\n");
else
printf("Comparing equal arrays failed\n");
json_object_array_add(ar2, json_object_new_int(1));
if (!json_object_equal(ar1, ar2))
printf("Comparing arrays of different len is correct\n");
else
printf("Comparing arrays of different len failed\n");
if (!json_object_equal(ar1, ar3))
printf("Comparing different arrays is correct\n");
else
printf("Comparing different arrays failed\n");
if (!json_object_equal(ar1, ar4))
printf("Comparing different arrays (one empty) is correct\n");
else
printf("Comparing different arrays (one empty) failed\n");
json_object_put(ar1);
json_object_put(ar2);
json_object_put(ar3);
json_object_put(ar4);
/* object tests */
struct json_object *obj1 = json_object_new_object();
struct json_object *obj2 = json_object_new_object();
json_object_object_add(obj1, "test1", json_object_new_int(123));
json_object_object_add(obj1, "test2", json_object_new_int(321));
json_object_object_add(obj1, "test3", json_object_new_int(320));
json_object_object_add(obj1, "test4", json_object_new_int(319));
json_object_object_add(obj1, "test5", json_object_new_int(318));
json_object_object_add(obj2, "test5", json_object_new_int(318));
json_object_object_add(obj2, "test4", json_object_new_int(319));
json_object_object_add(obj2, "test3", json_object_new_int(320));
json_object_object_add(obj2, "test2", json_object_new_int(321));
json_object_object_add(obj2, "test1", json_object_new_int(123));
/* key-order is different between obj1 and obj2, should still be equal */
if (json_object_equal(obj1, obj2))
printf("Comparing JSON object with different key order is correct\n");
else
printf("Comparing JSON object with different key order is incorrect\n");
/* make obj2 look different to obj1 */
json_object_object_add(obj2, "test3", json_object_new_int(234));
if (!json_object_equal(obj1, obj2))
printf("Comparing different objects is correct\n");
else
printf("Comparing different objects is incorrect\n");
/* iterate over jso2 keys to see if any exist that are not in jso1 */
json_object_object_add(obj2, "test3", json_object_new_int(320));
json_object_object_add(obj2, "test6", json_object_new_int(321));
if (!json_object_equal(obj1, obj2))
printf("Comparing different objects is correct\n");
else
printf("Comparing different objects is incorrect\n");
/* iterate over jso1 keys and see if they exist in jso1 */
json_object_object_add(obj1, "test6", json_object_new_int(321));
if (json_object_equal(obj1, obj2))
printf("Comparing different objects is correct\n");
else
printf("Comparing different objects is incorrect\n");
json_object_object_add(obj1, "test7", json_object_new_int(322));
if (!json_object_equal(obj1, obj2))
printf("Comparing different objects is correct\n");
else
printf("Comparing different objects is incorrect\n");
json_object_put(obj1);
json_object_put(obj2);
/* different types tests */
struct json_object *int5 = json_object_new_int(0);
struct json_object *dbl5 = json_object_new_double(3.14159);
if (!json_object_equal(int5, NULL))
printf("JSON integer and NULL comparison is correct\n");
else
printf("JSON integer and NULL comparison failed\n");
if (!json_object_equal(NULL, dbl5))
printf("JSON NULL and double comparison is correct\n");
else
printf("JSON NULL and double comparison failed\n");
if (!json_object_equal(int5, dbl5))
printf("JSON integer and double comparison is correct\n");
else
printf("JSON integer and double comparison failed\n");
json_object_put(int5);
json_object_put(dbl5);
return 0;
}
@@ -0,0 +1,30 @@
JSON integer comparison is correct
JSON same object comparison is correct
JSON same integer comparison is correct
JSON usigned integer comparison is correct
JSON same usigned object comparison is correct
JSON same usigned integer comparison is correct
JSON integer & usigned integer comparison is correct
JSON integer & usigned integer comparison is correct
JSON integer & usigned integer comparison is correct
JSON integer & usigned integer comparison is correct
JSON usigned integer & integer comparison is correct
JSON usigned integer & integer comparison is correct
JSON usigned integer & integer comparison is correct
JSON usigned integer & integer comparison is correct
Comparing equal strings is correct
Comparing different strings is correct
Comparing equal doubles is correct
Comparing different doubles is correct
Comparing equal arrays is correct
Comparing arrays of different len is correct
Comparing different arrays is correct
Comparing different arrays (one empty) is correct
Comparing JSON object with different key order is correct
Comparing different objects is correct
Comparing different objects is correct
Comparing different objects is correct
Comparing different objects is correct
JSON integer and NULL comparison is correct
JSON NULL and double comparison is correct
JSON integer and double comparison is correct
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,261 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <errno.h>
#include <time.h>
#include "json.h"
#include "printbuf.h"
static void do_benchmark(json_object *src1);
static const char *json_str1 =
"{"
" \"glossary\": {"
" \"title\": \"example glossary\","
" \"GlossDiv\": {"
" \"number\": 16446744073709551615,"
" \"title\": \"S\","
" \"null_obj\": null, "
" \"exist\": false,"
" \"quantity\":20,"
" \"univalent\":19.8,"
" \"GlossList\": {"
" \"GlossEntry\": {"
" \"ID\": \"SGML\","
" \"SortAs\": \"SGML\","
" \"GlossTerm\": \"Standard Generalized Markup Language\","
" \"Acronym\": \"SGML\","
" \"Abbrev\": \"ISO 8879:1986\","
" \"GlossDef\": {"
" \"para\": \"A meta-markup language, used to create markup languages "
"such as DocBook.\","
" \"GlossSeeAlso\": [\"GML\", \"XML\"]"
" },"
" \"GlossSee\": \"markup\""
" }"
" }"
" }"
" }"
"}";
static const char *json_str2 =
"{\"menu\": {"
" \"header\": \"SVG Viewer\","
" \"items\": ["
" {\"id\": \"Open\"},"
" {\"id\": \"OpenNew\", \"label\": \"Open New\"},"
" null,"
" {\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},"
" {\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},"
" {\"id\": \"OriginalView\", \"label\": \"Original View\"},"
" null,"
" {\"id\": \"Quality\", \"another_null\": null},"
" {\"id\": \"Pause\"},"
" {\"id\": \"Mute\"},"
" null,"
" {\"id\": \"Find\", \"label\": \"Find...\"},"
" {\"id\": \"FindAgain\", \"label\": \"Find Again\"},"
" {\"id\": \"Copy\"},"
" {\"id\": \"CopyAgain\", \"label\": \"Copy Again\"},"
" {\"id\": \"CopySVG\", \"label\": \"Copy SVG\"},"
" {\"id\": \"ViewSVG\", \"label\": \"View SVG\"},"
" {\"id\": \"ViewSource\", \"label\": \"View Source\"},"
" {\"id\": \"SaveAs\", \"label\": \"Save As\"},"
" null,"
" {\"id\": \"Help\"},"
" {\"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\"}"
" ]"
"}}";
static const char *json_str3 = "{\"menu\": {"
" \"id\": \"file\","
" \"value\": \"File\","
" \"popup\": {"
" \"menuitem\": ["
" {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},"
" {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},"
" {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}"
" ]"
" }"
"}}";
json_object_to_json_string_fn my_custom_serializer;
int my_custom_serializer(struct json_object *jso, struct printbuf *pb, int level, int flags)
{
sprintbuf(pb, "OTHER");
return 0;
}
json_c_shallow_copy_fn my_shallow_copy;
int my_shallow_copy(json_object *src, json_object *parent, const char *key, size_t index,
json_object **dst)
{
int rc;
rc = json_c_shallow_copy_default(src, parent, key, index, dst);
if (rc < 0)
return rc;
if (key != NULL && strcmp(key, "with_serializer") == 0)
{
printf("CALLED: my_shallow_copy on with_serializer object\n");
void *userdata = json_object_get_userdata(src);
json_object_set_serializer(*dst, my_custom_serializer, userdata, NULL);
return 2;
}
return rc;
}
int main(int argc, char **argv)
{
struct json_object *src1, *src2, *src3;
struct json_object *dst1 = NULL, *dst2 = NULL, *dst3 = NULL;
int benchmark = 0;
if (argc > 1 && strcmp(argv[1], "--benchmark") == 0)
{
benchmark = 1;
}
src1 = json_tokener_parse(json_str1);
src2 = json_tokener_parse(json_str2);
src3 = json_tokener_parse(json_str3);
assert(src1 != NULL);
assert(src2 != NULL);
assert(src3 != NULL);
printf("PASSED - loaded input data\n");
/* do this 3 times to make sure overwriting it works */
assert(0 == json_object_deep_copy(src1, &dst1, NULL));
assert(0 == json_object_deep_copy(src2, &dst2, NULL));
assert(0 == json_object_deep_copy(src3, &dst3, NULL));
printf("PASSED - all json_object_deep_copy() returned successful\n");
assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
assert(errno == EINVAL);
assert(-1 == json_object_deep_copy(src2, &dst2, NULL));
assert(errno == EINVAL);
assert(-1 == json_object_deep_copy(src3, &dst3, NULL));
assert(errno == EINVAL);
printf("PASSED - all json_object_deep_copy() returned EINVAL for non-null pointer\n");
assert(1 == json_object_equal(src1, dst1));
assert(1 == json_object_equal(src2, dst2));
assert(1 == json_object_equal(src3, dst3));
printf("PASSED - all json_object_equal() tests returned successful\n");
assert(0 == strcmp(json_object_to_json_string_ext(src1, JSON_C_TO_STRING_PRETTY),
json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY)));
assert(0 == strcmp(json_object_to_json_string_ext(src2, JSON_C_TO_STRING_PRETTY),
json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY)));
assert(0 == strcmp(json_object_to_json_string_ext(src3, JSON_C_TO_STRING_PRETTY),
json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY)));
printf("PASSED - comparison of string output\n");
json_object_get(dst1);
assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
assert(errno == EINVAL);
json_object_put(dst1);
printf("PASSED - trying to overrwrite an object that has refcount > 1");
printf("\nPrinting JSON objects for visual inspection\n");
printf("------------------------------------------------\n");
printf(" JSON1\n");
printf("%s\n", json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY));
printf("------------------------------------------------\n");
printf("------------------------------------------------\n");
printf(" JSON2\n");
printf("%s\n", json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY));
printf("------------------------------------------------\n");
printf("------------------------------------------------\n");
printf(" JSON3\n");
printf("------------------------------------------------\n");
printf("%s\n", json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY));
printf("------------------------------------------------\n");
json_object_put(dst1);
json_object_put(dst2);
json_object_put(dst3);
printf("\nTesting deep_copy with a custom serializer set\n");
json_object *with_serializer = json_object_new_string("notemitted");
char udata[] = "dummy userdata";
json_object_set_serializer(with_serializer, my_custom_serializer, udata, NULL);
json_object_object_add(src1, "with_serializer", with_serializer);
dst1 = NULL;
/* With a custom serializer in use, a custom shallow_copy function must also be used */
assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
assert(0 == json_object_deep_copy(src1, &dst1, my_shallow_copy));
json_object *dest_with_serializer = json_object_object_get(dst1, "with_serializer");
assert(dest_with_serializer != NULL);
char *dst_userdata = json_object_get_userdata(dest_with_serializer);
assert(strcmp(dst_userdata, "dummy userdata") == 0);
const char *special_output = json_object_to_json_string(dest_with_serializer);
assert(strcmp(special_output, "OTHER") == 0);
printf("\ndeep_copy with custom serializer worked OK.\n");
json_object_put(dst1);
if (benchmark)
{
do_benchmark(src2);
}
json_object_put(src1);
json_object_put(src2);
json_object_put(src3);
return 0;
}
static void do_benchmark(json_object *src2)
{
json_object *dst2 = NULL;
int ii;
/**
* The numbers that I got are:
* BENCHMARK - 1000000 iterations of 'dst2 = json_tokener_parse(json_object_get_string(src2))' took 71 seconds
* BENCHMARK - 1000000 iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took 29 seconds
*/
int iterations = 1000000;
time_t start = time(NULL);
start = time(NULL);
for (ii = 0; ii < iterations; ii++)
{
dst2 = json_tokener_parse(json_object_get_string(src2));
json_object_put(dst2);
}
printf("BENCHMARK - %d iterations of 'dst2 = "
"json_tokener_parse(json_object_get_string(src2))' took %d seconds\n",
iterations, (int)(time(NULL) - start));
start = time(NULL);
dst2 = NULL;
for (ii = 0; ii < iterations; ii++)
{
json_object_deep_copy(src2, &dst2, NULL);
json_object_put(dst2);
dst2 = NULL;
}
printf("BENCHMARK - %d iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took %d "
"seconds\n",
iterations, (int)(time(NULL) - start));
}
@@ -0,0 +1,152 @@
PASSED - loaded input data
PASSED - all json_object_deep_copy() returned successful
PASSED - all json_object_deep_copy() returned EINVAL for non-null pointer
PASSED - all json_object_equal() tests returned successful
PASSED - comparison of string output
PASSED - trying to overrwrite an object that has refcount > 1
Printing JSON objects for visual inspection
------------------------------------------------
JSON1
{
"glossary":{
"title":"example glossary",
"GlossDiv":{
"number":16446744073709551615,
"title":"S",
"null_obj":null,
"exist":false,
"quantity":20,
"univalent":19.8,
"GlossList":{
"GlossEntry":{
"ID":"SGML",
"SortAs":"SGML",
"GlossTerm":"Standard Generalized Markup Language",
"Acronym":"SGML",
"Abbrev":"ISO 8879:1986",
"GlossDef":{
"para":"A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso":[
"GML",
"XML"
]
},
"GlossSee":"markup"
}
}
}
}
}
------------------------------------------------
------------------------------------------------
JSON2
{
"menu":{
"header":"SVG Viewer",
"items":[
{
"id":"Open"
},
{
"id":"OpenNew",
"label":"Open New"
},
null,
{
"id":"ZoomIn",
"label":"Zoom In"
},
{
"id":"ZoomOut",
"label":"Zoom Out"
},
{
"id":"OriginalView",
"label":"Original View"
},
null,
{
"id":"Quality",
"another_null":null
},
{
"id":"Pause"
},
{
"id":"Mute"
},
null,
{
"id":"Find",
"label":"Find..."
},
{
"id":"FindAgain",
"label":"Find Again"
},
{
"id":"Copy"
},
{
"id":"CopyAgain",
"label":"Copy Again"
},
{
"id":"CopySVG",
"label":"Copy SVG"
},
{
"id":"ViewSVG",
"label":"View SVG"
},
{
"id":"ViewSource",
"label":"View Source"
},
{
"id":"SaveAs",
"label":"Save As"
},
null,
{
"id":"Help"
},
{
"id":"About",
"label":"About Adobe CVG Viewer..."
}
]
}
}
------------------------------------------------
------------------------------------------------
JSON3
------------------------------------------------
{
"menu":{
"id":"file",
"value":"File",
"popup":{
"menuitem":[
{
"value":"New",
"onclick":"CreateNewDoc()"
},
{
"value":"Open",
"onclick":"OpenDoc()"
},
{
"value":"Close",
"onclick":"CloseDoc()"
}
]
}
}
}
------------------------------------------------
Testing deep_copy with a custom serializer set
CALLED: my_shallow_copy on with_serializer object
deep_copy with custom serializer worked OK.
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,81 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#include "config.h"
#include "json.h"
#define NESTING_DEPTH 100000
static char *generate_json_string(void)
{
char *str;
int depth = NESTING_DEPTH;
str = malloc(depth * 2 + 1);
memset(str, '[', depth);
memset(str + depth, ']', depth);
str[depth * 2] = '\0';
return str;
}
static void test_deep_nesting_put(const char *str)
{
json_object *my_array;
struct json_tokener *tok = json_tokener_new_ex(NESTING_DEPTH);
my_array = json_tokener_parse_ex(tok, str, strlen(str) + 1);
printf("Parsed depth %d string to json_object: %s\n", NESTING_DEPTH, (my_array == NULL) ? "NO" : "yes");
json_object_put(my_array);
printf("Freed json_object\n");
json_tokener_free(tok);
}
static void test_deep_nesting_tostring(const char *str)
{
json_object *my_array;
struct json_tokener *tok = json_tokener_new_ex(NESTING_DEPTH);
my_array = json_tokener_parse_ex(tok, str, strlen(str) + 1);
printf("Parsed depth %d string to json_object: %s\n", NESTING_DEPTH, (my_array == NULL) ? "NO" : "yes");
const char *res = json_object_to_json_string_ext(my_array, JSON_C_TO_STRING_PLAIN);
printf("Serialized to string of length %ld\n", (long)strlen(res));
json_object_put(my_array);
printf("Freed json_object\n");
json_tokener_free(tok);
}
int main(int argc, char **argv)
{
char *str;
#ifdef HAVE_SETRLIMIT
struct rlimit rl;
rl.rlim_cur = 2048;
rl.rlim_max = 2048;
setrlimit(RLIMIT_STACK, &rl);
#endif
str = generate_json_string();
MC_SET_DEBUG(1);
test_deep_nesting_put(str);
if (0) // TODO: make json_object_to_json_string non-recursive
test_deep_nesting_tostring(str);
free(str);
return EXIT_SUCCESS;
}
@@ -0,0 +1,2 @@
Parsed depth 100000 string to json_object: yes
Freed json_object
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,113 @@
/*
* Tests if the format string for double serialization is handled correctly
*/
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include <stdio.h>
#include "json_object.h"
#include "json_object_private.h"
/* Avoid compiler warnings about diving by constant zero */
double zero_dot_zero = 0.0;
int main(int argc, char **argv)
{
struct json_object *obj = json_object_new_double(0.5);
char udata[] = "test";
printf("Test default serializer:\n");
printf("obj.to_string(standard)=%s\n", json_object_to_json_string(obj));
printf("Test default serializer with custom userdata:\n");
obj->_userdata = udata;
printf("obj.to_string(userdata)=%s\n", json_object_to_json_string(obj));
printf("Test explicit serializer with custom userdata:\n");
json_object_set_serializer(obj, json_object_double_to_json_string, udata, NULL);
printf("obj.to_string(custom)=%s\n", json_object_to_json_string(obj));
printf("Test reset serializer:\n");
json_object_set_serializer(obj, NULL, NULL, NULL);
printf("obj.to_string(reset)=%s\n", json_object_to_json_string(obj));
json_object_put(obj);
printf("Test no zero reset serializer:\n");
obj = json_object_new_double(3.1415000);
char data[] = "%.17g";
json_object_set_serializer(obj, json_object_double_to_json_string, data, NULL);
printf("obj.to_string(reset)=%s\n", json_object_to_json_string_ext(obj, 4));
json_object_put(obj);
obj = json_object_new_double(0.52381);
printf("obj.to_string(default format)=%s\n", json_object_to_json_string(obj));
if (json_c_set_serialization_double_format("x%0.3fy", JSON_C_OPTION_GLOBAL) < 0)
printf("ERROR: json_c_set_serialization_double_format() failed");
printf("obj.to_string(with global format)=%s\n", json_object_to_json_string(obj));
#ifdef HAVE___THREAD
if (json_c_set_serialization_double_format("T%0.2fX", JSON_C_OPTION_THREAD) < 0)
printf("ERROR: json_c_set_serialization_double_format() failed");
printf("obj.to_string(with thread format)=%s\n", json_object_to_json_string(obj));
if (json_c_set_serialization_double_format("Ttttttttttttt%0.2fxxxxxxxxxxxxxxxxxxX",
JSON_C_OPTION_THREAD) < 0)
printf("ERROR: json_c_set_serialization_double_format() failed");
printf("obj.to_string(long thread format)=%s\n", json_object_to_json_string(obj));
if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_THREAD) < 0)
printf("ERROR: json_c_set_serialization_double_format() failed");
printf("obj.to_string(back to global format)=%s\n", json_object_to_json_string(obj));
#else
// Just fake it up, so the output matches.
printf("obj.to_string(with thread format)=%s\n", "T0.52X");
printf("obj.to_string(long thread format)=%s\n", "Ttttttttttttt0.52xxxxxxxxxxxxxxxxxxX");
printf("obj.to_string(back to global format)=%s\n", "x0.524y");
#endif
if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_GLOBAL) < 0)
printf("ERROR: json_c_set_serialization_double_format() failed");
printf("obj.to_string(back to default format)=%s\n", json_object_to_json_string(obj));
json_object_put(obj);
obj = json_object_new_double(12.0);
printf("obj(12.0).to_string(default format)=%s\n", json_object_to_json_string(obj));
if (json_c_set_serialization_double_format("%.0f", JSON_C_OPTION_GLOBAL) < 0)
printf("ERROR: json_c_set_serialization_double_format() failed");
printf("obj(12.0).to_string(%%.0f)=%s\n", json_object_to_json_string(obj));
if (json_c_set_serialization_double_format("%.0g", JSON_C_OPTION_GLOBAL) < 0)
printf("ERROR: json_c_set_serialization_double_format() failed");
printf("obj(12.0).to_string(%%.0g)=%s\n", json_object_to_json_string(obj));
if (json_c_set_serialization_double_format("%.2g", JSON_C_OPTION_GLOBAL) < 0)
printf("ERROR: json_c_set_serialization_double_format() failed");
printf("obj(12.0).to_string(%%.1g)=%s\n", json_object_to_json_string(obj));
// Reset to default to free memory
if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_GLOBAL) < 0)
printf("ERROR: json_c_set_serialization_double_format() failed");
json_object_put(obj);
obj = json_object_new_double(-12.0);
printf("obj(-12.0).to_string(default format)=%s\n", json_object_to_json_string(obj));
json_object_put(obj);
/* Test NaN handling */
obj = json_object_new_double(zero_dot_zero / zero_dot_zero);
printf("obj(0.0/0.0)=%s\n", json_object_to_json_string(obj));
json_object_put(obj);
/* Test Infinity and -Infinity handling */
obj = json_object_new_double(1.0 / zero_dot_zero);
printf("obj(1.0/0.0)=%s\n", json_object_to_json_string(obj));
json_object_put(obj);
obj = json_object_new_double(-1.0 / zero_dot_zero);
printf("obj(-1.0/0.0)=%s\n", json_object_to_json_string(obj));
json_object_put(obj);
return 0;
}
@@ -0,0 +1,24 @@
Test default serializer:
obj.to_string(standard)=0.5
Test default serializer with custom userdata:
obj.to_string(userdata)=0.5
Test explicit serializer with custom userdata:
obj.to_string(custom)=test
Test reset serializer:
obj.to_string(reset)=0.5
Test no zero reset serializer:
obj.to_string(reset)=3.1415000000000002
obj.to_string(default format)=0.52381
obj.to_string(with global format)=x0.524y
obj.to_string(with thread format)=T0.52X
obj.to_string(long thread format)=Ttttttttttttt0.52xxxxxxxxxxxxxxxxxxX
obj.to_string(back to global format)=x0.524y
obj.to_string(back to default format)=0.52381
obj(12.0).to_string(default format)=12.0
obj(12.0).to_string(%.0f)=12
obj(12.0).to_string(%.0g)=1e+01
obj(12.0).to_string(%.1g)=12.0
obj(-12.0).to_string(default format)=-12.0
obj(0.0/0.0)=NaN
obj(1.0/0.0)=Infinity
obj(-1.0/0.0)=-Infinity
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,31 @@
/* Copyright (C) 2016 by Rainer Gerhards
* Released under ASL 2.0 */
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include "json_object.h"
#include "json_tokener.h"
#include <stdio.h>
int main(void)
{
json_object *json;
json = json_object_new_double(1.0);
printf("json = %s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
json_object_put(json);
json = json_object_new_double(-1.0);
printf("json = %s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
json_object_put(json);
json = json_object_new_double(1.23);
printf("json = %s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
json_object_put(json);
json = json_object_new_double(123456789.0);
printf("json = %s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
json_object_put(json);
json = json_object_new_double(123456789.123);
printf("json = %s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
json_object_put(json);
return 0;
}
@@ -0,0 +1,5 @@
json = 1.0
json = -1.0
json = 1.23
json = 123456789.0
json = 123456789.123
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,74 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stdio.h>
#include "json.h"
int main(int argc, char **argv)
{
json_object *tmp = json_object_new_int(123);
json_object_int_inc(tmp, 123);
assert(json_object_get_int(tmp) == 246);
json_object_put(tmp);
printf("INT ADD PASSED\n");
tmp = json_object_new_int(INT32_MAX);
json_object_int_inc(tmp, 100);
assert(json_object_get_int(tmp) == INT32_MAX);
assert(json_object_get_int64(tmp) == (int64_t)INT32_MAX + 100L);
json_object_put(tmp);
printf("INT ADD OVERFLOW PASSED\n");
tmp = json_object_new_int(INT32_MIN);
json_object_int_inc(tmp, -100);
assert(json_object_get_int(tmp) == INT32_MIN);
assert(json_object_get_int64(tmp) == (int64_t)INT32_MIN - 100L);
json_object_put(tmp);
printf("INT ADD UNDERFLOW PASSED\n");
tmp = json_object_new_int64(321321321);
json_object_int_inc(tmp, 321321321);
assert(json_object_get_int(tmp) == 642642642);
json_object_put(tmp);
printf("INT64 ADD PASSED\n");
tmp = json_object_new_int64(INT64_MAX);
json_object_int_inc(tmp, 100);
assert(json_object_get_int64(tmp) == INT64_MAX);
assert(json_object_get_uint64(tmp) == (uint64_t)INT64_MAX + 100U);
json_object_int_inc(tmp, -100);
assert(json_object_get_int64(tmp) == INT64_MAX);
assert(json_object_get_uint64(tmp) == (uint64_t)INT64_MAX);
json_object_put(tmp);
printf("INT64 ADD OVERFLOW PASSED\n");
tmp = json_object_new_int64(INT64_MIN);
json_object_int_inc(tmp, -100);
assert(json_object_get_int64(tmp) == INT64_MIN);
json_object_int_inc(tmp, 100);
assert(json_object_get_int64(tmp) != INT64_MIN);
json_object_put(tmp);
printf("INT64 ADD UNDERFLOW PASSED\n");
// uint64 + negative int64--> negative int64
tmp = json_object_new_uint64(400);
json_object_int_inc(tmp, -200);
assert(json_object_get_int64(tmp) == 200);
assert(json_object_get_uint64(tmp) == 200);
json_object_int_inc(tmp, 200);
assert(json_object_get_int64(tmp) == 400);
assert(json_object_get_uint64(tmp) == 400);
json_object_put(tmp);
printf("UINT64 ADD PASSED\n");
tmp = json_object_new_uint64(UINT64_MAX-50);
json_object_int_inc(tmp, 100);
assert(json_object_get_int64(tmp) == INT64_MAX);
assert(json_object_get_uint64(tmp) == UINT64_MAX);
json_object_put(tmp);
printf("UINT64 ADD OVERFLOW PASSED\n");
tmp = json_object_new_uint64(100);
json_object_int_inc(tmp, -200);
assert(json_object_get_int64(tmp) == -100);
assert(json_object_get_uint64(tmp) == 0);
json_object_put(tmp);
printf("UINT64 ADD UNDERFLOW PASSED\n");
printf("PASSED\n");
return 0;
}
@@ -0,0 +1,10 @@
INT ADD PASSED
INT ADD OVERFLOW PASSED
INT ADD UNDERFLOW PASSED
INT64 ADD PASSED
INT64 ADD OVERFLOW PASSED
INT64 ADD UNDERFLOW PASSED
UINT64 ADD PASSED
UINT64 ADD OVERFLOW PASSED
UINT64 ADD UNDERFLOW PASSED
PASSED
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,80 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include "json.h"
#define I32_MAX_S "2147483647"
#define I32_MIN_S "-2147483648"
#define I64_MAX_S "9223372036854775807"
#define I64_OVER "9223372036854775808"
#define I64_MIN_S "-9223372036854775808"
#define I64_UNDER "-9223372036854775809"
#define U64_MAX_S "18446744073709551615"
#define U64_OUT_S "18446744073709551616"
#define CHECK_GET(GET_F, J, EXPECTED) { struct json_object *jtmp = J; errno = 0; assert(GET_F(jtmp) == EXPECTED); json_object_put(jtmp); }
#define CHECK_GET_INT(J, EXPECTED) CHECK_GET(json_object_get_int, J, EXPECTED)
#define CHECK_GET_INT64(J, EXPECTED) CHECK_GET(json_object_get_int64, J, EXPECTED)
#define CHECK_GET_UINT64(J, EXPECTED) CHECK_GET(json_object_get_uint64, J, EXPECTED)
#define CHECK_BASE(J, EXPECTED) CHECK_GET_INT(J, EXPECTED); CHECK_GET_INT64(J, EXPECTED); CHECK_GET_UINT64(J, EXPECTED)
#define N_INT json_object_new_int
#define N_I64 json_object_new_int64
#define N_U64 json_object_new_uint64
#define N_STR json_object_new_string
#define N_DBL json_object_new_double
int main(int argc, char **argv)
{
CHECK_BASE(N_INT(5), 5);
CHECK_BASE(N_INT(0), 0);
CHECK_BASE(N_STR("0"), 0);
CHECK_BASE(N_STR("00000"), 0);
CHECK_BASE(N_STR("000004568789"), 4568789);
CHECK_BASE(N_STR("0xFF"), 0 && errno == 0); // Hex-string values being parsed as 0 is the intended behavior
CHECK_BASE(N_STR("333this_seems_a_valid_string"), 333);
CHECK_BASE(N_STR("this_is_not_a_number"), 0 && errno == EINVAL);
CHECK_BASE(N_STR("B0"), 0 && errno == EINVAL);
printf("BASE CHECK PASSED\n");
CHECK_GET_INT(N_I64(INT32_MAX), INT32_MAX && errno == 0);
CHECK_GET_INT(N_I64(INT32_MIN), INT32_MIN && errno == 0);
CHECK_GET_INT(N_I64(INT64_MAX), INT32_MAX && errno == ERANGE);
CHECK_GET_INT(N_I64(INT64_MIN), INT32_MIN && errno == ERANGE);
CHECK_GET_INT(N_STR(I32_MAX_S), INT32_MAX && errno == 0);
CHECK_GET_INT(N_STR(I32_MIN_S), INT32_MIN && errno == 0);
CHECK_GET_INT(N_STR(I64_MAX_S), INT32_MAX && errno == ERANGE);
CHECK_GET_INT(N_STR(I64_MIN_S), INT32_MIN && errno == ERANGE);
CHECK_GET_INT(N_DBL(INFINITY), INT32_MAX && errno == ERANGE);
CHECK_GET_INT(N_DBL(-INFINITY), INT32_MIN && errno == ERANGE);
CHECK_GET_INT(N_DBL(NAN), INT32_MIN && errno == EINVAL);
printf("INT GET PASSED\n");
CHECK_GET_INT64(N_I64(INT64_MAX), INT64_MAX && errno == 0);
CHECK_GET_INT64(N_I64(INT64_MIN), INT64_MIN && errno == 0);
CHECK_GET_INT64(N_STR(I64_MAX_S), INT64_MAX && errno == 0);
CHECK_GET_INT64(N_STR(I64_MIN_S), INT64_MIN && errno == 0);
CHECK_GET_INT64(N_STR(I64_OVER), INT64_MAX && errno == ERANGE);
CHECK_GET_INT64(N_STR(I64_UNDER), INT64_MIN && errno == ERANGE);
CHECK_GET_INT64(N_DBL(INFINITY), INT64_MAX && errno == ERANGE);
CHECK_GET_INT64(N_DBL(-INFINITY), INT64_MIN && errno == ERANGE);
CHECK_GET_INT64(N_DBL(NAN), INT64_MIN && errno == EINVAL);
printf("INT64 GET PASSED\n");
CHECK_GET_UINT64(N_U64(UINT64_MAX), UINT64_MAX && errno == 0);
CHECK_GET_UINT64(N_U64(-1), UINT64_MAX && errno == 0);
CHECK_GET_UINT64(N_STR(U64_OUT_S), UINT64_MAX && errno == ERANGE);
CHECK_GET_UINT64(N_DBL(INFINITY), UINT64_MAX && errno == ERANGE);
CHECK_GET_UINT64(N_DBL(-INFINITY), 0 && errno == ERANGE);
CHECK_GET_UINT64(N_DBL(NAN), 0 && errno == EINVAL);
printf("UINT64 GET PASSED\n");
printf("PASSED\n");
return 0;
}
@@ -0,0 +1,5 @@
BASE CHECK PASSED
INT GET PASSED
INT64 GET PASSED
UINT64 GET PASSED
PASSED
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,2 @@
Successfully parsed object from stdin
Failed at offset 71: invalid utf-8 string: char=0x00
@@ -0,0 +1,21 @@
#!/bin/sh
# Common definitions
if test -z "$srcdir"; then
srcdir="${0%/*}"
test "$srcdir" = "$0" && srcdir=.
test -z "$srcdir" && srcdir=.
fi
. "$srcdir/test-defs.sh"
set -x -v
echo -n '"tenant=blue;note=CANARY_STACK_WINDOW_2026;status=ok"' > file1.dat
(printf '"' ; printf 'A%.0s' $(seq 1 16) ; echo 'e2' | xxd -r -p) > file2.dat
TESTNAME="${0##*/}"
TESTNAME="${TESTNAME%.test}"
( cat file1.dat ; cat file2.dat ) | run_output_test -o "${TESTNAME}" --exit 1 ../apps/json_parse -u -N -n -
_err=$?
exit $_err
@@ -0,0 +1,127 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "strerror_override.h"
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "json.h"
#include "snprintf_compat.h"
#ifndef PATH_MAX
#define PATH_MAX 256
#endif
void test_json_patch_op(struct json_object *jo)
{
const char *comment = json_object_get_string(json_object_object_get(jo, "comment"));
struct json_object *doc = json_object_object_get(jo, "doc");
struct json_object *patch = json_object_object_get(jo, "patch");
struct json_object *expected = NULL;
json_bool have_expected = json_object_object_get_ex(jo, "expected", &expected);
struct json_object *error = json_object_object_get(jo, "error");
const char *error_s = json_object_get_string(error);
struct json_object *res = NULL;
int ret;
printf("Testing '%s', doc '%s' patch '%s' : ",
comment ? comment : error_s,
json_object_get_string(doc),
json_object_get_string(patch));
if (!error && !have_expected) {
printf("BAD TEST - no expected or error conditions in test: %s\n", json_object_to_json_string(jo));
assert(0);
}
fflush(stdout);
struct json_patch_error jperr;
if (error) {
assert(-1 == json_patch_apply(doc, patch, &res, &jperr));
assert(jperr.errno_code != 0);
printf("OK\n");
printf(" => json_patch_apply failed as expected: %s at patch idx %zu: %s\n",
strerror(jperr.errno_code), jperr.patch_failure_idx, jperr.errmsg);
json_object_put(res);
} else {
ret = json_patch_apply(doc, patch, &res, &jperr);
if (ret) {
fprintf(stderr, "json_patch_apply() returned '%d'\n", ret);
fprintf(stderr, "Expected: %s\n", json_object_get_string(expected));
fprintf(stderr, "Got: %s\n", res ? json_object_get_string(res) : "(null)");
fprintf(stderr, "json_patch_apply failed: %s at patch idx %zu: %s\n",
strerror(jperr.errno_code), jperr.patch_failure_idx, jperr.errmsg);
fflush(stderr);
assert(0);
}
// Note: res might be NULL if the whole document was removed
assert(jperr.errno_code == 0);
ret = json_object_equal(expected, res);
if (ret == 0) {
fprintf(stderr, "json_object_equal() returned '%d'\n", ret);
fprintf(stderr, "Expected: %s\n", json_object_get_string(expected));
fprintf(stderr, "Got: %s\n", json_object_get_string(res));
fflush(stderr);
assert(0);
}
json_object_put(res);
res = NULL;
printf("OK\n");
}
}
void test_json_patch_using_file(const char *testdir, const char *filename)
{
char full_filename[PATH_MAX];
(void)snprintf(full_filename, sizeof(full_filename), "%s/%s", testdir, filename);
size_t ii;
printf("Testing using file %s\n", filename);
json_object *jo = json_object_from_file(full_filename);
if (!jo) {
fprintf(stderr, "FAIL: unable to open %s: %s\n", full_filename, strerror(errno));
exit(EXIT_FAILURE);
}
for (ii = 0; ii < json_object_array_length(jo); ii++) {
struct json_object *jo1 = json_object_array_get_idx(jo, ii);
test_json_patch_op(jo1);
}
json_object_put(jo);
}
int main(int argc, char **argv)
{
const char *testdir;
if (argc < 2)
{
fprintf(stderr,
"Usage: %s <testdir>\n"
" <testdir> is the location of input files\n",
argv[0]);
return EXIT_FAILURE;
}
testdir = argv[1];
// Test json_c_version.c
if (strncmp(json_c_version(), JSON_C_VERSION, sizeof(JSON_C_VERSION)))
{
printf("FAIL: Output from json_c_version(): %s does not match %s",
json_c_version(), JSON_C_VERSION);
return EXIT_FAILURE;
}
if (json_c_version_num() != JSON_C_VERSION_NUM)
{
printf("FAIL: Output from json_c_version_num(): %d does not match %d",
json_c_version_num(), JSON_C_VERSION_NUM);
return EXIT_FAILURE;
}
test_json_patch_using_file(testdir, "json_patch_spec_tests.json");
test_json_patch_using_file(testdir, "json_patch_tests.json");
return 0;
}
@@ -0,0 +1,159 @@
Testing using file json_patch_spec_tests.json
Testing '4.1. add with missing object', doc '{ "q": { "bar": 2 } }' patch '[ { "op": "add", "path": "\/a\/b", "value": 1 } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Failed to set value at path referenced by 'path' field
Testing 'A.1. Adding an Object Member', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/baz", "value": "qux" } ]' : OK
Testing 'A.2. Adding an Array Element', doc '{ "foo": [ "bar", "baz" ] }' patch '[ { "op": "add", "path": "\/foo\/1", "value": "qux" } ]' : OK
Testing 'A.3. Removing an Object Member', doc '{ "baz": "qux", "foo": "bar" }' patch '[ { "op": "remove", "path": "\/baz" } ]' : OK
Testing 'A.4. Removing an Array Element', doc '{ "foo": [ "bar", "qux", "baz" ] }' patch '[ { "op": "remove", "path": "\/foo\/1" } ]' : OK
Testing 'A.5. Replacing a Value', doc '{ "baz": "qux", "foo": "bar" }' patch '[ { "op": "replace", "path": "\/baz", "value": "boo" } ]' : OK
Testing 'A.6. Moving a Value', doc '{ "foo": { "bar": "baz", "waldo": "fred" }, "qux": { "corge": "grault" } }' patch '[ { "op": "move", "from": "\/foo\/waldo", "path": "\/qux\/thud" } ]' : OK
Testing 'A.7. Moving an Array Element', doc '{ "foo": [ "all", "grass", "cows", "eat" ] }' patch '[ { "op": "move", "from": "\/foo\/1", "path": "\/foo\/3" } ]' : OK
Testing 'A.8. Testing a Value: Success', doc '{ "baz": "qux", "foo": [ "a", 2, "c" ] }' patch '[ { "op": "test", "path": "\/baz", "value": "qux" }, { "op": "test", "path": "\/foo\/1", "value": 2 } ]' : OK
Testing 'A.9. Testing a Value: Error', doc '{ "baz": "qux" }' patch '[ { "op": "test", "path": "\/baz", "value": "bar" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
Testing 'A.10. Adding a nested Member Object', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/child", "value": { "grandchild": { } } } ]' : OK
Testing 'A.11. Ignoring Unrecognized Elements', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/baz", "value": "qux", "xyz": 123 } ]' : OK
Testing 'A.12. Adding to a Non-existent Target', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/baz\/bat", "value": "qux" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Failed to set value at path referenced by 'path' field
Testing 'A.13 Invalid JSON Patch Document', doc '{ "foo": "bar" }' patch '[ { "op": "remove", "path": "\/baz", "value": "qux" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field
Testing 'A.14. ~ Escape Ordering', doc '{ "\/": 9, "~1": 10 }' patch '[ { "op": "test", "path": "\/~01", "value": 10 } ]' : OK
Testing 'A.15. Comparing Strings and Numbers', doc '{ "\/": 9, "~1": 10 }' patch '[ { "op": "test", "path": "\/~01", "value": "10" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
Testing 'A.16. Adding an Array Value', doc '{ "foo": [ "bar" ] }' patch '[ { "op": "add", "path": "\/foo\/-", "value": [ "abc", "def" ] } ]' : OK
Testing using file json_patch_tests.json
Testing 'empty list, empty docs', doc '{ }' patch '[ ]' : OK
Testing 'empty patch list', doc '{ "foo": 1 }' patch '[ ]' : OK
Testing 'rearrangements OK?', doc '{ "foo": 1, "bar": 2 }' patch '[ ]' : OK
Testing 'rearrangements OK? How about one level down ... array', doc '[ { "foo": 1, "bar": 2 } ]' patch '[ ]' : OK
Testing 'rearrangements OK? How about one level down...', doc '{ "foo": { "foo": 1, "bar": 2 } }' patch '[ ]' : OK
Testing 'add replaces any existing field', doc '{ "foo": null }' patch '[ { "op": "add", "path": "\/foo", "value": 1 } ]' : OK
Testing 'toplevel array', doc '[ ]' patch '[ { "op": "add", "path": "\/0", "value": "foo" } ]' : OK
Testing 'toplevel array, no change', doc '[ "foo" ]' patch '[ ]' : OK
Testing 'toplevel object, numeric string', doc '{ }' patch '[ { "op": "add", "path": "\/foo", "value": "1" } ]' : OK
Testing 'toplevel object, integer', doc '{ }' patch '[ { "op": "add", "path": "\/foo", "value": 1 } ]' : OK
Testing 'Toplevel scalar values OK?', doc 'foo' patch '[ { "op": "replace", "path": "", "value": "bar" } ]' : OK
Testing 'replace object document with array document?', doc '{ }' patch '[ { "op": "add", "path": "", "value": [ ] } ]' : OK
Testing 'replace array document with object document?', doc '[ ]' patch '[ { "op": "add", "path": "", "value": { } } ]' : OK
Testing 'append to root array document?', doc '[ ]' patch '[ { "op": "add", "path": "\/-", "value": "hi" } ]' : OK
Testing 'Add, / target', doc '{ }' patch '[ { "op": "add", "path": "\/", "value": 1 } ]' : OK
Testing 'Add, /foo/ deep target (trailing slash)', doc '{ "foo": { } }' patch '[ { "op": "add", "path": "\/foo\/", "value": 1 } ]' : OK
Testing 'Add composite value at top level', doc '{ "foo": 1 }' patch '[ { "op": "add", "path": "\/bar", "value": [ 1, 2 ] } ]' : OK
Testing 'Add into composite value', doc '{ "foo": 1, "baz": [ { "qux": "hello" } ] }' patch '[ { "op": "add", "path": "\/baz\/0\/foo", "value": "world" } ]' : OK
Testing 'Out of bounds (upper)', doc '{ "bar": [ 1, 2 ] }' patch '[ { "op": "add", "path": "\/bar\/8", "value": "5" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
Testing 'Out of bounds (lower)', doc '{ "bar": [ 1, 2 ] }' patch '[ { "op": "add", "path": "\/bar\/-1", "value": "5" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
Testing '(null)', doc '{ "foo": 1 }' patch '[ { "op": "add", "path": "\/bar", "value": true } ]' : OK
Testing '(null)', doc '{ "foo": 1 }' patch '[ { "op": "add", "path": "\/bar", "value": false } ]' : OK
Testing '(null)', doc '{ "foo": 1 }' patch '[ { "op": "add", "path": "\/bar", "value": null } ]' : OK
Testing '0 can be an array index or object element name', doc '{ "foo": 1 }' patch '[ { "op": "add", "path": "\/0", "value": "bar" } ]' : OK
Testing '(null)', doc '[ "foo" ]' patch '[ { "op": "add", "path": "\/1", "value": "bar" } ]' : OK
Testing '(null)', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/1", "value": "bar" } ]' : OK
Testing '(null)', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/0", "value": "bar" } ]' : OK
Testing 'push item to array via last index + 1', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/2", "value": "bar" } ]' : OK
Testing 'add item to array at index > length should fail', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/3", "value": "bar" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
Testing 'test against implementation-specific numeric parsing', doc '{ "1e0": "foo" }' patch '[ { "op": "test", "path": "\/1e0", "value": "foo" } ]' : OK
Testing 'test with bad number should fail', doc '[ "foo", "bar" ]' patch '[ { "op": "test", "path": "\/1e0", "value": "bar" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field
Testing 'Object operation on array target', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/bar", "value": 42 } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
Testing 'value in array add not flattened', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/1", "value": [ "bar", "baz" ] } ]' : OK
Testing '(null)', doc '{ "foo": 1, "bar": [ 1, 2, 3, 4 ] }' patch '[ { "op": "remove", "path": "\/bar" } ]' : OK
Testing '(null)', doc '{ "foo": 1, "baz": [ { "qux": "hello" } ] }' patch '[ { "op": "remove", "path": "\/baz\/0\/qux" } ]' : OK
Testing '(null)', doc '{ "foo": 1, "baz": [ { "qux": "hello" } ] }' patch '[ { "op": "replace", "path": "\/foo", "value": [ 1, 2, 3, 4 ] } ]' : OK
Testing '(null)', doc '{ "foo": [ 1, 2, 3, 4 ], "baz": [ { "qux": "hello" } ] }' patch '[ { "op": "replace", "path": "\/baz\/0\/qux", "value": "world" } ]' : OK
Testing '(null)', doc '[ "foo" ]' patch '[ { "op": "replace", "path": "\/0", "value": "bar" } ]' : OK
Testing '(null)', doc '[ "" ]' patch '[ { "op": "replace", "path": "\/0", "value": 0 } ]' : OK
Testing '(null)', doc '[ "" ]' patch '[ { "op": "replace", "path": "\/0", "value": true } ]' : OK
Testing '(null)', doc '[ "" ]' patch '[ { "op": "replace", "path": "\/0", "value": false } ]' : OK
Testing '(null)', doc '[ "" ]' patch '[ { "op": "replace", "path": "\/0", "value": null } ]' : OK
Testing 'value in array replace not flattened', doc '[ "foo", "sil" ]' patch '[ { "op": "replace", "path": "\/1", "value": [ "bar", "baz" ] } ]' : OK
Testing 'replace whole document', doc '{ "foo": "bar" }' patch '[ { "op": "replace", "path": "", "value": { "baz": "qux" } } ]' : OK
Testing 'add whole document, null', doc '{ }' patch '[ { "op": "remove", "path": "" }, { "op": "add", "path": "", "value": { "baz": "qux" } } ]' : OK
Testing 'replace whole document, null', doc '{ }' patch '[ { "op": "remove", "path": "" }, { "op": "replace", "path": "", "value": { "baz": "qux" } } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 1: Invalid path field
Testing 'remove whole document', doc '{ "foo": "bar" }' patch '[ { "op": "remove", "path": "" } ]' : OK
Testing 'remove whole document', doc '{ "foo": "bar" }' patch '[ { "op": "remove", "path": "" } ]' : OK
Testing 'remove whole document, array', doc '[ "foo", "bar" ]' patch '[ { "op": "remove", "path": "" } ]' : OK
Testing 'remove whole document, string', doc 'foo' patch '[ { "op": "remove", "path": "" } ]' : OK
Testing 'remove whole document, null', doc '{ }' patch '[ { "op": "remove", "path": "" }, { "op": "remove", "path": "" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 1: Invalid path field
Testing 'test replace with missing parent key should fail', doc '{ "bar": "baz" }' patch '[ { "op": "replace", "path": "\/foo\/bar", "value": false } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field
Testing 'spurious patch properties', doc '{ "foo": 1 }' patch '[ { "op": "test", "path": "\/foo", "value": 1, "spurious": 1 } ]' : OK
Testing 'null value should be valid obj property', doc '{ "foo": null }' patch '[ { "op": "test", "path": "\/foo", "value": null } ]' : OK
Testing 'null value should be valid obj property to be replaced with something truthy', doc '{ "foo": null }' patch '[ { "op": "replace", "path": "\/foo", "value": "truthy" } ]' : OK
Testing 'null value should be valid obj property to be moved', doc '{ "foo": null }' patch '[ { "op": "move", "from": "\/foo", "path": "\/bar" } ]' : OK
Testing 'null value should be valid obj property to be copied', doc '{ "foo": null }' patch '[ { "op": "copy", "from": "\/foo", "path": "\/bar" } ]' : OK
Testing 'null value should be valid obj property to be removed', doc '{ "foo": null }' patch '[ { "op": "remove", "path": "\/foo" } ]' : OK
Testing 'null value should still be valid obj property replace other value', doc '{ "foo": "bar" }' patch '[ { "op": "replace", "path": "\/foo", "value": null } ]' : OK
Testing 'test should pass despite rearrangement', doc '{ "foo": { "foo": 1, "bar": 2 } }' patch '[ { "op": "test", "path": "\/foo", "value": { "bar": 2, "foo": 1 } } ]' : OK
Testing 'test should pass despite (nested) rearrangement', doc '{ "foo": [ { "foo": 1, "bar": 2 } ] }' patch '[ { "op": "test", "path": "\/foo", "value": [ { "bar": 2, "foo": 1 } ] } ]' : OK
Testing 'test should pass - no error', doc '{ "foo": { "bar": [ 1, 2, 5, 4 ] } }' patch '[ { "op": "test", "path": "\/foo", "value": { "bar": [ 1, 2, 5, 4 ] } } ]' : OK
Testing 'test op should fail', doc '{ "foo": { "bar": [ 1, 2, 5, 4 ] } }' patch '[ { "op": "test", "path": "\/foo", "value": [ 1, 2 ] } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
Testing 'Test the whole document', doc '{ "foo": 1 }' patch '[ { "op": "test", "path": "", "value": { "foo": 1 } } ]' : OK
Testing 'Test the whole document, no match', doc '{ "foo": 1 }' patch '[ { "op": "test", "path": "", "value": { "foo": 2 } } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
Testing 'Empty-string element', doc '{ "": 1 }' patch '[ { "op": "test", "path": "\/", "value": 1 } ]' : OK
Testing '(null)', doc '{ "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 }' patch '[ { "op": "test", "path": "\/foo", "value": [ "bar", "baz" ] }, { "op": "test", "path": "\/foo\/0", "value": "bar" }, { "op": "test", "path": "\/", "value": 0 }, { "op": "test", "path": "\/a~1b", "value": 1 }, { "op": "test", "path": "\/c%d", "value": 2 }, { "op": "test", "path": "\/e^f", "value": 3 }, { "op": "test", "path": "\/g|h", "value": 4 }, { "op": "test", "path": "\/i\\j", "value": 5 }, { "op": "test", "path": "\/k\"l", "value": 6 }, { "op": "test", "path": "\/ ", "value": 7 }, { "op": "test", "path": "\/m~0n", "value": 8 } ]' : OK
Testing 'Move to same location has no effect', doc '{ "foo": 1 }' patch '[ { "op": "move", "from": "\/foo", "path": "\/foo" } ]' : OK
Testing '(null)', doc '{ "foo": 1, "baz": [ { "qux": "hello" } ] }' patch '[ { "op": "move", "from": "\/foo", "path": "\/bar" } ]' : OK
Testing '(null)', doc '{ "baz": [ { "qux": "hello" } ], "bar": 1 }' patch '[ { "op": "move", "from": "\/baz\/0\/qux", "path": "\/baz\/1" } ]' : OK
Testing '(null)', doc '{ "baz": [ { "qux": "hello" } ], "bar": 1 }' patch '[ { "op": "copy", "from": "\/baz\/0", "path": "\/boo" } ]' : OK
Testing 'replacing the root of the document is possible with add', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "", "value": { "baz": "qux" } } ]' : OK
Testing 'Adding to "/-" adds to the end of the array', doc '[ 1, 2 ]' patch '[ { "op": "add", "path": "\/-", "value": { "foo": [ "bar", "baz" ] } } ]' : OK
Testing 'Adding to "/-" adds to the end of the array, even n levels down', doc '[ 1, 2, [ 3, [ 4, 5 ] ] ]' patch '[ { "op": "add", "path": "\/2\/1\/-", "value": { "foo": [ "bar", "baz" ] } } ]' : OK
Testing 'test remove with bad number should fail', doc '{ "foo": 1, "baz": [ { "qux": "hello" } ] }' patch '[ { "op": "remove", "path": "\/baz\/1e0\/qux" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field
Testing 'test remove on array', doc '[ 1, 2, 3, 4 ]' patch '[ { "op": "remove", "path": "\/0" } ]' : OK
Testing 'test repeated removes', doc '[ 1, 2, 3, 4 ]' patch '[ { "op": "remove", "path": "\/1" }, { "op": "remove", "path": "\/2" } ]' : OK
Testing 'test remove with bad index should fail', doc '[ 1, 2, 3, 4 ]' patch '[ { "op": "remove", "path": "\/1e0" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field
Testing 'test replace with bad number should fail', doc '[ "" ]' patch '[ { "op": "replace", "path": "\/1e0", "value": false } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field
Testing 'test copy with bad number should fail', doc '{ "baz": [ 1, 2, 3 ], "bar": 1 }' patch '[ { "op": "copy", "from": "\/baz\/1e0", "path": "\/boo" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid from field
Testing 'test move with bad number should fail', doc '{ "foo": 1, "baz": [ 1, 2, 3, 4 ] }' patch '[ { "op": "move", "from": "\/baz\/1e0", "path": "\/foo" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid from field
Testing 'test add with bad number should fail', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/1e0", "value": "bar" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
Testing 'missing 'path' parameter', doc '{ }' patch '[ { "op": "add", "value": "bar" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain 'path' field
Testing ''path' parameter with null value', doc '{ }' patch '[ { "op": "add", "path": null, "value": "bar" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
Testing 'invalid JSON Pointer token', doc '{ }' patch '[ { "op": "add", "path": "foo", "value": "bar" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
Testing 'missing 'value' parameter to add', doc '[ 1 ]' patch '[ { "op": "add", "path": "\/-" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain a 'value' field
Testing 'missing 'value' parameter to replace', doc '[ 1 ]' patch '[ { "op": "replace", "path": "\/0" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain a 'value' field
Testing 'missing 'value' parameter to test', doc '[ null ]' patch '[ { "op": "test", "path": "\/0" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain a 'value' field
Testing 'missing value parameter to test - where undef is falsy', doc '[ false ]' patch '[ { "op": "test", "path": "\/0" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain a 'value' field
Testing 'missing from parameter to copy', doc '[ 1 ]' patch '[ { "op": "copy", "path": "\/-" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch does not contain a 'from' field
Testing 'missing from location to copy', doc '{ "foo": 1 }' patch '[ { "op": "copy", "from": "\/bar", "path": "\/foo" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by from field
Testing 'missing from parameter to move', doc '{ "foo": 1 }' patch '[ { "op": "move", "path": "" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch does not contain a 'from' field
Testing 'missing from location to move', doc '{ "foo": 1 }' patch '[ { "op": "move", "from": "\/bar", "path": "\/foo" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by from field
Testing 'duplicate ops, json-c parses this as op:move', doc '{ "foo": "bar" }' patch '[ { "op": "move", "path": "\/baz", "value": "qux", "from": "\/foo" } ]' : OK
Testing 'unrecognized op should fail', doc '{ "foo": 1 }' patch '[ { "op": "spam", "path": "\/foo", "value": 1 } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object has invalid 'op' field
Testing 'test with bad array number that has leading zeros', doc '[ "foo", "bar" ]' patch '[ { "op": "test", "path": "\/00", "value": "foo" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field
Testing 'test with bad array number that has leading zeros', doc '[ "foo", "bar" ]' patch '[ { "op": "test", "path": "\/01", "value": "bar" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field
Testing 'Removing nonexistent field', doc '{ "foo": "bar" }' patch '[ { "op": "remove", "path": "\/baz" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field
Testing 'Removing deep nonexistent path', doc '{ "foo": "bar" }' patch '[ { "op": "remove", "path": "\/missing1\/missing2" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field
Testing 'Removing nonexistent index', doc '[ "foo", "bar" ]' patch '[ { "op": "remove", "path": "\/2" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field
Testing 'Patch with different capitalisation than doc', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/FOO", "value": "BAR" } ]' : OK
Testing 'copy must duplicate the value, not alias it, so no cycle can form', doc '{ "a": { "p": { } } }' patch '[ { "op": "copy", "from": "\/a", "path": "\/b" }, { "op": "copy", "from": "\/b", "path": "\/a\/p\/x" } ]' : OK
+17
View File
@@ -0,0 +1,17 @@
#!/bin/sh
export _JSON_C_STRERROR_ENABLE=1
# Common definitions
if test -z "$srcdir"; then
srcdir="${0%/*}"
test "$srcdir" = "$0" && srcdir=.
test -z "$srcdir" && srcdir=.
fi
. "$srcdir/test-defs.sh"
filename=$(basename "$0")
filename="${filename%.*}"
run_output_test $filename "$srcdir"
exit $?
@@ -0,0 +1,344 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#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 "
"}";
/* clang-format off */
static const char *rec_input_json_str =
"{"
"'arr' : ["
"{"
"'obj': ["
"{},{},"
"{"
"'obj1': 0,"
"'obj2': \"1\""
"}"
"]"
"}"
"],"
"'obj' : {"
"'obj': {"
"'obj': ["
"{"
"'obj1': 0,"
"'obj2': \"1\""
"}"
"]"
"}"
"}"
"}";
/* clang-format on */
/* Example from RFC */
static void test_example_get(void)
{
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 */
/* clang-format off */
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}
};
/* clang-format on */
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 recursion of the json_pointer_get() function */
static void test_recursion_get(void)
{
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(0 == json_pointer_getf(jo1, &jo2, "/%s/%d/%s/%d/%s", "arr", 0, "obj", 2, "obj2"));
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)));
assert(0 == json_pointer_getf(jo1, &jo2, "%s", "\0"));
printf("PASSED - GET - RECURSION TEST\n");
json_object_put(jo1);
}
static void test_wrong_inputs_get(void)
{
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_getf(NULL, NULL, NULL));
assert(errno == EINVAL);
errno = 0;
assert(0 != json_pointer_get(jo1, NULL, NULL));
assert(errno == EINVAL);
errno = 0;
assert(0 != json_pointer_getf(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/01", NULL));
assert(errno == EINVAL);
errno = 0;
assert(0 != json_pointer_getf(jo1, NULL, "/%s/a", "foo"));
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_getf(jo1, NULL, "%s", "/foo/22"));
assert(errno == ENOENT);
errno = 0;
assert(0 != json_pointer_getf(jo1, NULL, "/%s/%d", "foo", 22));
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(void)
{
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)));
assert(0 == json_pointer_setf(&jo1, json_object_new_int(0), "%s%s/%d", "/fud", "/gaw", 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);
jo1 = json_tokener_parse("[0, 1, 2, 3]");
jo2 = json_tokener_parse("[0, 1, 2, 3, null, null, null, 7]");
assert(0 == json_pointer_set(&jo1, "/7", json_object_new_int(7)));
assert(1 == json_object_equal(jo1, jo2));
json_object_put(jo1);
jo1 = json_tokener_parse("[0, 1, 2, 3]");
assert(0 == json_pointer_setf(&jo1, json_object_new_int(7), "/%u", 7));
assert(1 == json_object_equal(jo1, jo2));
json_object_put(jo1);
json_object_put(jo2);
}
static void test_wrong_inputs_set(void)
{
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(NULL, NULL, NULL));
assert(0 != json_pointer_setf(NULL, NULL, NULL));
assert(0 != json_pointer_set(&jo1, NULL, NULL));
assert(0 != json_pointer_setf(&jo1, NULL, NULL));
printf("PASSED - SET - failed with NULL params for input json & path\n");
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_setf(&jo1, (jo2 = json_object_new_string("cod")), "%s", "foo/bar"));
printf("PASSED - SET - failed 'cod' with path 'foo/bar'\n");
json_object_put(jo2);
assert(0 != json_pointer_set(&jo1, "0", (jo2 = json_object_new_string("cod"))));
printf("PASSED - SET - failed with invalid array index'\n");
json_object_put(jo2);
jo2 = json_object_new_string("whatever");
assert(0 != json_pointer_set(&jo1, "/fud/gaw", jo2));
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 */
// ownership of jo2 transferred into jo1
jo2 = json_object_new_int(0);
assert(0 != json_pointer_set(&jo1, "/fud/gaw/0", jo2));
json_object_put(jo2);
jo2 = json_object_new_int(0);
assert(0 != json_pointer_set(&jo1, "/fud/gaw/", jo2));
json_object_put(jo2);
printf("PASSED - SET - failed to set index to non-array\n");
assert(0 == json_pointer_setf(&jo1, json_object_new_string("cod"), "%s", "\0"));
json_object_put(jo1);
}
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;
}
@@ -0,0 +1,39 @@
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 with NULL params for input json & path
PASSED - SET - failed 'cod' with path 'foo/bar'
PASSED - SET - failed 'cod' with path 'foo/bar'
PASSED - SET - failed with invalid array index'
PASSED - SET - failed to set index to non-array
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,76 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "json.h"
#include "json_tokener.h"
#include "snprintf_compat.h"
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif /* HAVE_LOCALE_H */
#ifdef HAVE_XLOCALE_H
#include <xlocale.h>
#endif
int main(int argc, char **argv)
{
json_object *new_obj;
#ifdef HAVE_SETLOCALE
setlocale(LC_NUMERIC, "de_DE");
#endif
char buf1[10], buf2[10];
// Should result in "0,1", if the locale is installed.
// Regardless of what it generates, we check that it's
// consistent below.
(void)snprintf(buf1, sizeof(buf1), "%f", 0.1);
MC_SET_DEBUG(1);
new_obj = json_tokener_parse("[1.2,3.4,123456.78,5.0,2.3e10]");
(void)snprintf(buf2, sizeof(buf2), "%f", 0.1);
if (strcmp(buf1, buf2) != 0)
printf("ERROR: Original locale not restored \"%s\" != \"%s\"", buf1, buf2);
// json_object_get_double() on a json_type_string must not depend on the
// locale's decimal point. Under a comma-radix locale such as de_DE the
// '.' used to be treated as a stray char, so "19.95" came back as 0.
// (If the de_DE locale isn't installed we're still in "C" here, where
// this passes anyway, so the check is safe to always run.)
{
json_object *numstr = json_object_new_string("19.95");
double d = json_object_get_double(numstr);
assert(d > 19.94 && d < 19.96);
json_object_put(numstr);
}
#ifdef HAVE_SETLOCALE
setlocale(LC_NUMERIC, "C");
#endif
// Explicitly print each value, to avoid having the "special"
// serialization done for parsed doubles simply re-emit the original
// string that was parsed. (see json_object_new_double_s())
printf("new_obj.to_string()=[");
unsigned int ii;
for (ii = 0; ii < json_object_array_length(new_obj); ii++)
{
json_object *val = json_object_array_get_idx(new_obj, ii);
printf("%s%.2lf", (ii > 0) ? "," : "", json_object_get_double(val));
}
printf("]\n");
printf("new_obj.to_string()=%s\n",
json_object_to_json_string_ext(new_obj, JSON_C_TO_STRING_NOZERO));
json_object_put(new_obj);
return 0;
}
@@ -0,0 +1,2 @@
new_obj.to_string()=[1.20,3.40,123456.78,5.00,23000000000.00]
new_obj.to_string()=[1.2,3.4,123456.78,5.0,2.3e10]
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,62 @@
/*
* Tests if binary strings are supported.
*/
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "json_inttypes.h"
#include "json_object.h"
#include "json_tokener.h"
int main(void)
{
/* this test has a space after the null character. check that it's still included */
const char *input = " \0 ";
const char *expected = "\" \\u0000 \"";
struct json_object *string = json_object_new_string_len(input, 3);
const char *json = json_object_to_json_string(string);
int strings_match = !strcmp(expected, json);
int retval = 0;
if (strings_match)
{
printf("JSON write result is correct: %s\n", json);
puts("PASS");
}
else
{
puts("JSON write result doesn't match expected string");
printf("expected string: ");
puts(expected);
printf("parsed string: ");
puts(json);
puts("FAIL");
retval = 1;
}
json_object_put(string);
struct json_object *parsed_str = json_tokener_parse(expected);
if (parsed_str)
{
int parsed_len = json_object_get_string_len(parsed_str);
const char *parsed_cstr = json_object_get_string(parsed_str);
int ii;
printf("Re-parsed object string len=%d, chars=[", parsed_len);
for (ii = 0; ii < parsed_len; ii++)
{
printf("%s%d", (ii ? ", " : ""), (int)parsed_cstr[ii]);
}
puts("]");
json_object_put(parsed_str);
}
else
{
puts("ERROR: failed to parse");
}
return retval;
}
@@ -0,0 +1,3 @@
JSON write result is correct: " \u0000 "
PASS
Re-parsed object string len=3, chars=[32, 0, 32]
+1
View File
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,77 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "json_object.h"
#include "json_object_iterator.h"
#include "json_tokener.h"
int main(int atgc, char **argv)
{
const char *input = "{\n\
\"string_of_digits\": \"123\",\n\
\"regular_number\": 222,\n\
\"decimal_number\": 99.55,\n\
\"boolean_true\": true,\n\
\"boolean_false\": false,\n\
\"big_number\": 2147483649,\n\
\"a_null\": null,\n\
\"an_object\": { \"foo\": \"bar\" },\n\
}";
struct json_object *new_obj;
struct json_object_iterator it;
struct json_object_iterator itEnd;
it = json_object_iter_init_default();
new_obj = json_tokener_parse(input);
it = json_object_iter_begin(new_obj);
itEnd = json_object_iter_end(new_obj);
// If this changes size on any platform, it'd break the ABI
printf("sizeof(struct json_object_iterator)/sizeof(void *)=%zu\n", sizeof(struct json_object_iterator) / sizeof(void *));
while (!json_object_iter_equal(&it, &itEnd))
{
const char *key = json_object_iter_peek_name(&it);
json_object *value = json_object_iter_peek_value(&it);
printf("key: %s\n", key);
printf("pre change: %s\n", json_object_to_json_string(value));
if (json_object_get_type(value) == json_type_object)
{
// Changing sub-objects is ok
assert(json_object_object_add(value, "foo", json_object_new_string("change foo")) == 0);
assert(json_object_object_add(value, "newkey", json_object_new_string("new value")) == 0);
}
else
{
// Changing the value of pairs is ok
assert(json_object_object_add(new_obj, key, json_object_new_string("switched")) == 0);
}
if (0) // DO NOT DO THIS
{
// Adding new keys is NOT ok, and invalidates the iterator
json_object_object_add(value, "somenewkey", json_object_new_string("foo"));
// Deleting any keys ALSO invalidates the iterator, even if
// the key is immediately re-added.
json_object_object_del(value, key);
json_object_object_add(value, key, json_object_new_string("switched"));
}
value = json_object_iter_peek_value(&it);
printf("post change: %s\n", json_object_to_json_string(value));
json_object_iter_next(&it);
}
json_object_put(new_obj);
return 0;
}
@@ -0,0 +1,25 @@
sizeof(struct json_object_iterator)/sizeof(void *)=1
key: string_of_digits
pre change: "123"
post change: "switched"
key: regular_number
pre change: 222
post change: "switched"
key: decimal_number
pre change: 99.55
post change: "switched"
key: boolean_true
pre change: true
post change: "switched"
key: boolean_false
pre change: false
post change: "switched"
key: big_number
pre change: 2147483649
post change: "switched"
key: a_null
pre change: null
post change: "switched"
key: an_object
pre change: { "foo": "bar" }
post change: { "foo": "change foo", "newkey": "new value" }
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,790 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "json.h"
#include "json_tokener.h"
#include "json_visit.h"
static void test_basic_parse(void);
static void test_utf8_parse(void);
static void test_verbose_parse(void);
static void test_incremental_parse(void);
int main(void)
{
MC_SET_DEBUG(1);
static const char separator[] = "==================================";
test_basic_parse();
puts(separator);
test_utf8_parse();
puts(separator);
test_verbose_parse();
puts(separator);
test_incremental_parse();
puts(separator);
return 0;
}
static json_c_visit_userfunc clear_serializer;
static void do_clear_serializer(json_object *jso);
static void single_incremental_parse(const char *test_string, int clear_serializer)
{
size_t ii;
int chunksize = atoi(getenv("TEST_PARSE_CHUNKSIZE"));
struct json_tokener *tok;
enum json_tokener_error jerr;
json_object *all_at_once_obj, *new_obj;
const char *all_at_once_str, *new_str;
new_obj = NULL;
assert(chunksize > 0);
all_at_once_obj = json_tokener_parse(test_string);
if (clear_serializer)
do_clear_serializer(all_at_once_obj);
all_at_once_str = json_object_to_json_string(all_at_once_obj);
tok = json_tokener_new();
size_t test_string_len = strlen(test_string) + 1; // Including '\0' !
for (ii = 0; ii < test_string_len; ii += chunksize)
{
int len_to_parse = chunksize;
if (ii + chunksize > test_string_len)
len_to_parse = test_string_len - ii;
if (getenv("TEST_PARSE_DEBUG") != NULL)
printf(" chunk: %.*s\n", len_to_parse, &test_string[ii]);
new_obj = json_tokener_parse_ex(tok, &test_string[ii], len_to_parse);
jerr = json_tokener_get_error(tok);
if (jerr != json_tokener_continue || new_obj)
break;
}
if (clear_serializer && new_obj)
do_clear_serializer(new_obj);
new_str = json_object_to_json_string(new_obj);
if (strcmp(all_at_once_str, new_str) != 0)
{
printf("ERROR: failed to parse (%s) in %d byte chunks: %s != %s\n", test_string,
chunksize, all_at_once_str, new_str);
}
json_tokener_free(tok);
if (all_at_once_obj)
json_object_put(all_at_once_obj);
if (new_obj)
json_object_put(new_obj);
}
static void single_basic_parse(const char *test_string, int clear_serializer)
{
json_object *new_obj;
new_obj = json_tokener_parse(test_string);
if (clear_serializer)
do_clear_serializer(new_obj);
printf("new_obj.to_string(%s)=%s\n", test_string, json_object_to_json_string(new_obj));
json_object_put(new_obj);
if (getenv("TEST_PARSE_CHUNKSIZE") != NULL)
single_incremental_parse(test_string, clear_serializer);
}
static void test_basic_parse(void)
{
single_basic_parse("\"\003\"", 0);
single_basic_parse("/* hello */\"foo\"", 0);
single_basic_parse("// hello\n\"foo\"", 0);
single_basic_parse("\"foo\"blue", 0);
single_basic_parse("\'foo\'", 0);
single_basic_parse("\"\\u0041\\u0042\\u0043\"", 0);
single_basic_parse("\"\\u4e16\\u754c\\u00df\"", 0);
single_basic_parse("\"\\u4E16\"", 0);
single_basic_parse("\"\\u4e1\"", 0);
single_basic_parse("\"\\u4e1@\"", 0);
single_basic_parse("\"\\ud840\\u4e16\"", 0);
single_basic_parse("\"\\ud840\"", 0);
single_basic_parse("\"\\udd27\"", 0);
// Test with a "short" high surrogate
single_basic_parse("[9,'\\uDAD", 0);
single_basic_parse("\"[9,'\\uDAD\"", 0);
// Test with a supplemental character that looks like a high surrogate
single_basic_parse("\"\\uD836\\uDE87\"", 0);
single_basic_parse("null", 0);
single_basic_parse("NaN", 0);
single_basic_parse("-NaN", 0); /* non-sensical, returns null */
single_basic_parse("Inf", 0); /* must use full string, returns null */
single_basic_parse("inf", 0); /* must use full string, returns null */
single_basic_parse("Infinity", 0);
single_basic_parse("infinity", 0);
single_basic_parse("-Infinity", 0);
single_basic_parse("-infinity", 0);
single_basic_parse("{ \"min\": Infinity, \"max\": -Infinity}", 0);
single_basic_parse("Infinity!", 0);
single_basic_parse("Infinitynull", 0);
single_basic_parse("InfinityXXXX", 0);
single_basic_parse("-Infinitynull", 0);
single_basic_parse("-InfinityXXXX", 0);
single_basic_parse("Infinoodle", 0);
single_basic_parse("InfinAAA", 0);
single_basic_parse("-Infinoodle", 0);
single_basic_parse("-InfinAAA", 0);
single_basic_parse("True", 0);
single_basic_parse("False", 0);
/* not case sensitive */
single_basic_parse("tRue", 0);
single_basic_parse("fAlse", 0);
single_basic_parse("nAn", 0);
single_basic_parse("iNfinity", 0);
single_basic_parse("12", 0);
single_basic_parse("12.3", 0);
/* Even though, when using json_tokener_parse() there's no way to
* know when there is more data after the parsed object,
* an object is successfully returned anyway (in some cases)
*/
single_basic_parse("12.3.4", 0);
single_basic_parse("2015-01-15", 0);
single_basic_parse("12.3xxx", 0);
single_basic_parse("12.3{\"a\":123}", 0);
single_basic_parse("12.3\n", 0);
single_basic_parse("12.3 ", 0);
single_basic_parse("{\"FoO\" : -12.3E512}", 0);
single_basic_parse("{\"FoO\" : -12.3e512}", 0);
single_basic_parse("{\"FoO\" : -12.3E51.2}", 0); /* non-sensical, returns null */
single_basic_parse("{\"FoO\" : -12.3E512E12}", 0); /* non-sensical, returns null */
single_basic_parse("[\"\\n\"]", 0);
single_basic_parse("[\"\\nabc\\n\"]", 0);
single_basic_parse("[null]", 0);
single_basic_parse("[]", 0);
single_basic_parse("[false]", 0);
single_basic_parse("[\"abc\",null,\"def\",12]", 0);
single_basic_parse("{}", 0);
single_basic_parse("{ \"foo\": \"bar\" }", 0);
single_basic_parse("{ \'foo\': \'bar\' }", 0);
single_basic_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }", 0);
single_basic_parse("{ \"foo\": [null, \"foo\"] }", 0);
single_basic_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, "
"\"arr\": [ 1, 2, 3, null, 5 ] }",
0);
single_basic_parse("{ \"abc\": \"blue\nred\\ngreen\" }", 0);
// Clear serializer for these tests so we see the actual parsed value.
single_basic_parse("null", 1);
single_basic_parse("false", 1);
single_basic_parse("[0e]", 1);
single_basic_parse("[0e+]", 1);
single_basic_parse("[0e+-1]", 1);
single_basic_parse("\"hello world!\"", 1);
// uint64/int64 range test
single_basic_parse("[9223372036854775806]", 1);
single_basic_parse("[9223372036854775807]", 1);
single_basic_parse("[9223372036854775808]", 1);
single_basic_parse("[-9223372036854775807]", 1);
single_basic_parse("[-9223372036854775808]", 1);
single_basic_parse("[-9223372036854775809]", 1);
single_basic_parse("[18446744073709551614]", 1);
single_basic_parse("[18446744073709551615]", 1);
single_basic_parse("[18446744073709551616]", 1);
}
static void test_utf8_parse(void)
{
// json_tokener_parse doesn't support checking for byte order marks.
// It's the responsibility of the caller to detect and skip a BOM.
// Both of these checks return null.
const char *utf8_bom = "\xEF\xBB\xBF";
const char *utf8_bom_and_chars = "\xEF\xBB\xBF{}";
single_basic_parse(utf8_bom, 0);
single_basic_parse(utf8_bom_and_chars, 0);
}
// Clear the re-serialization information that the tokener
// saves to ensure that the output reflects the actual
// values we parsed, rather than just the original input.
static void do_clear_serializer(json_object *jso)
{
json_c_visit(jso, 0, clear_serializer, NULL);
}
static int clear_serializer(json_object *jso, int flags, json_object *parent_jso,
const char *jso_key, size_t *jso_index, void *userarg)
{
if (jso)
json_object_set_serializer(jso, NULL, NULL, NULL);
return JSON_C_VISIT_RETURN_CONTINUE;
}
static void test_verbose_parse(void)
{
json_object *new_obj;
enum json_tokener_error error = json_tokener_success;
new_obj = json_tokener_parse_verbose("{ foo }", &error);
assert(error == json_tokener_error_parse_object_key_name);
assert(new_obj == NULL);
new_obj = json_tokener_parse("{ foo }");
assert(new_obj == NULL);
new_obj = json_tokener_parse("foo");
assert(new_obj == NULL);
new_obj = json_tokener_parse_verbose("foo", &error);
assert(new_obj == NULL);
/* b/c the string starts with 'f' parsing return a boolean error */
assert(error == json_tokener_error_parse_boolean);
puts("json_tokener_parse_verbose() OK");
}
struct incremental_step
{
const char *string_to_parse;
int length;
int char_offset;
enum json_tokener_error expected_error;
int reset_tokener; /* Set to 1 to call json_tokener_reset() after parsing */
int tok_flags; /* JSON_TOKENER_* flags to pass to json_tokener_set_flags() */
} incremental_steps[] = {
/* Check that full json messages can be parsed, both w/ and w/o a reset */
{"{ \"foo\": 123 }", -1, -1, json_tokener_success, 0, 0},
{"{ \"foo\": 456 }", -1, -1, json_tokener_success, 1, 0},
{"{ \"foo\": 789 }", -1, -1, json_tokener_success, 1, 0},
/* Check the comment parse*/
{"/* hello */{ \"foo\"", -1, -1, json_tokener_continue, 0, 0},
{"/* hello */:/* hello */", -1, -1, json_tokener_continue, 0, 0},
{"\"bar\"/* hello */", -1, -1, json_tokener_continue, 0, 0},
{"}/* hello */", -1, -1, json_tokener_success, 1, 0},
{"/ hello ", -1, 1, json_tokener_error_parse_comment, 1, 0},
{"/* hello\"foo\"", -1, -1, json_tokener_continue, 1, 0},
{"/* hello*\"foo\"", -1, -1, json_tokener_continue, 1, 0},
{"// hello\"foo\"", -1, -1, json_tokener_continue, 1, 0},
/* Check a basic incremental parse */
{"{ \"foo", -1, -1, json_tokener_continue, 0, 0},
{"\": {\"bar", -1, -1, json_tokener_continue, 0, 0},
{"\":13}}", -1, -1, json_tokener_success, 1, 0},
/* Check the UTF-16 surrogate pair handling in various ways.
* Note: \ud843\udd1e is u+1D11E, Musical Symbol G Clef
* Your terminal may not display these correctly, in particular
* PuTTY doesn't currently show this character.
*/
/* parse one char at every time */
{"\"\\", -1, -1, json_tokener_continue, 0, 0},
{"u", -1, -1, json_tokener_continue, 0, 0},
{"d", -1, -1, json_tokener_continue, 0, 0},
{"8", -1, -1, json_tokener_continue, 0, 0},
{"3", -1, -1, json_tokener_continue, 0, 0},
{"4", -1, -1, json_tokener_continue, 0, 0},
{"\\", -1, -1, json_tokener_continue, 0, 0},
{"u", -1, -1, json_tokener_continue, 0, 0},
{"d", -1, -1, json_tokener_continue, 0, 0},
{"d", -1, -1, json_tokener_continue, 0, 0},
{"1", -1, -1, json_tokener_continue, 0, 0},
{"e\"", -1, -1, json_tokener_success, 1, 0},
/* parse two char at every time */
{"\"\\u", -1, -1, json_tokener_continue, 0, 0},
{"d8", -1, -1, json_tokener_continue, 0, 0},
{"34", -1, -1, json_tokener_continue, 0, 0},
{"\\u", -1, -1, json_tokener_continue, 0, 0},
{"dd", -1, -1, json_tokener_continue, 0, 0},
{"1e\"", -1, -1, json_tokener_success, 1, 0},
/* check the low surrogate pair */
{"\"\\ud834", -1, -1, json_tokener_continue, 0, 0},
{"\\udd1e\"", -1, -1, json_tokener_success, 1, 0},
{"\"\\ud834\\", -1, -1, json_tokener_continue, 0, 0},
{"udd1e\"", -1, -1, json_tokener_success, 1, 0},
{"\"\\ud834\\u", -1, -1, json_tokener_continue, 0, 0},
{"dd1e\"", -1, -1, json_tokener_success, 1, 0},
{"\"fff \\ud834\\ud", -1, -1, json_tokener_continue, 0, 0},
{"d1e bar\"", -1, -1, json_tokener_success, 1, 0},
{"\"fff \\ud834\\udd", -1, -1, json_tokener_continue, 0, 0},
{"1e bar\"", -1, -1, json_tokener_success, 1, 0},
/* \ud83d\ude00 is U+1F600, Grinning Face
* Displays fine in PuTTY, though you may need "less -r"
*/
{"\"fff \\ud83d\\ude", -1, -1, json_tokener_continue, 0, 0},
{"00 bar\"", -1, -1, json_tokener_success, 1, 0},
/* Check a utf-8 char (a+umlaut) that has bytes that look negative when
char are signed (see also control char check below) */
{"\"\xc3\xa4\"", -1, -1, json_tokener_success, 1, 0},
{"\"\xc3\xa4\"", -1, -1, json_tokener_success, 1, JSON_TOKENER_STRICT},
/* Check that json_tokener_reset actually resets */
{"{ \"foo", -1, -1, json_tokener_continue, 1, 0},
{": \"bar\"}", -1, 0, json_tokener_error_parse_unexpected, 1, 0},
/* Check a supplemental code point that looks like a high surrogate */
{"\"\\uD836", -1, -1, json_tokener_continue, 0, 0},
{"\\uDE87", -1, -1, json_tokener_continue, 0, 0},
{"\"", -1, -1, json_tokener_success, 1, 0},
/* Check incremental parsing with trailing characters */
{"{ \"foo", -1, -1, json_tokener_continue, 0, 0},
{"\": {\"bar", -1, -1, json_tokener_continue, 0, 0},
{"\":13}}XXXX", 10, 6, json_tokener_success, 0, 0},
{"XXXX", 4, 0, json_tokener_error_parse_unexpected, 1, 0},
/* Check that trailing characters can change w/o a reset */
{"{\"x\": 123 }\"X\"", -1, 11, json_tokener_success, 0, 0},
{"\"Y\"", -1, -1, json_tokener_success, 1, 0},
/* Trailing characters should cause a failure in strict mode */
{"{\"foo\":9}{\"bar\":8}", -1, 9, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
/* ... unless explicitly allowed. */
{"{\"foo\":9}{\"bar\":8}", -1, 9, json_tokener_success, 0,
JSON_TOKENER_STRICT | JSON_TOKENER_ALLOW_TRAILING_CHARS},
{"{\"b\":8}ignored garbage", -1, 7, json_tokener_success, 1,
JSON_TOKENER_STRICT | JSON_TOKENER_ALLOW_TRAILING_CHARS},
/* To stop parsing a number we need to reach a non-digit, e.g. a \0 */
{"1", 1, 1, json_tokener_continue, 0, 0},
/* This should parse as the number 12, since it continues the "1" */
{"2", 2, 1, json_tokener_success, 0, 0},
{"12{", 3, 2, json_tokener_success, 1, 0},
/* Parse number in strict mode */
{"[02]", -1, 3, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
{"0e+0", 5, 4, json_tokener_success, 1, 0},
{"[0e+0]", -1, -1, json_tokener_success, 1, 0},
/* The behavior when missing the exponent varies slightly */
{"0e", 2, 2, json_tokener_continue, 1, 0},
{"0e", 3, 2, json_tokener_success, 1, 0},
{"0e", 3, 2, json_tokener_error_parse_eof, 1, JSON_TOKENER_STRICT},
{"[0e]", -1, -1, json_tokener_success, 1, 0},
{"[0e]", -1, 3, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
{"0e+", 3, 3, json_tokener_continue, 1, 0},
{"0e+", 4, 3, json_tokener_success, 1, 0},
{"0e+", 4, 3, json_tokener_error_parse_eof, 1, JSON_TOKENER_STRICT},
{"[0e+]", -1, -1, json_tokener_success, 1, 0},
{"[0e+]", -1, 4, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
{"0e-", 3, 3, json_tokener_continue, 1, 0},
{"0e-", 4, 3, json_tokener_success, 1, 0},
{"0e-", 4, 3, json_tokener_error_parse_eof, 1, JSON_TOKENER_STRICT},
{"[0e-]", -1, -1, json_tokener_success, 1, 0},
{"[0e-]", -1, 4, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
/* You might expect this to fail, but it won't because
it's a valid partial parse; note the char_offset: */
{"0e+-", 5, 3, json_tokener_success, 1, 0},
{"0e+-", 5, 3, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
{"[0e+-]", -1, 4, json_tokener_error_parse_number, 1, 0},
/* Similar tests for other kinds of objects: */
/* These could all return success immediately, since regardless of
what follows the false/true/null token we *will* return a json object,
but it currently doesn't work that way. hmm... */
{"false", 5, 5, json_tokener_continue, 1, 0},
{"false", 6, 5, json_tokener_success, 1, 0},
{"true", 4, 4, json_tokener_continue, 1, 0},
{"true", 5, 4, json_tokener_success, 1, 0},
{"null", 4, 4, json_tokener_continue, 1, 0},
{"null", 5, 4, json_tokener_success, 1, 0},
{"Infinity", 9, 8, json_tokener_success, 1, 0},
{"infinity", 9, 8, json_tokener_success, 1, 0},
{"infinity", 9, 0, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
{"-infinity", 10, 9, json_tokener_success, 1, 0},
{"-infinity", 10, 1, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
{"inf", 3, 3, json_tokener_continue, 0, 0},
{"inity", 6, 5, json_tokener_success, 1, 0},
{"-inf", 4, 4, json_tokener_continue, 0, 0},
{"inity", 6, 5, json_tokener_success, 1, 0},
{"i", 1, 1, json_tokener_continue, 0, 0},
{"n", 1, 1, json_tokener_continue, 0, 0},
{"f", 1, 1, json_tokener_continue, 0, 0},
{"i", 1, 1, json_tokener_continue, 0, 0},
{"n", 1, 1, json_tokener_continue, 0, 0},
{"i", 1, 1, json_tokener_continue, 0, 0},
{"t", 1, 1, json_tokener_continue, 0, 0},
{"y", 1, 1, json_tokener_continue, 0, 0},
{"", 1, 0, json_tokener_success, 1, 0},
{"-", 1, 1, json_tokener_continue, 0, 0},
{"inf", 3, 3, json_tokener_continue, 0, 0},
{"ini", 3, 3, json_tokener_continue, 0, 0},
{"ty", 3, 2, json_tokener_success, 1, 0},
{"-", 1, 1, json_tokener_continue, 0, 0},
{"i", 1, 1, json_tokener_continue, 0, 0},
{"nfini", 5, 5, json_tokener_continue, 0, 0},
{"ty", 3, 2, json_tokener_success, 1, 0},
{"-i", 2, 2, json_tokener_continue, 0, 0},
{"nfinity", 8, 7, json_tokener_success, 1, 0},
{"InfinityX", 10, 8, json_tokener_success, 0, 0},
{"X", 1, 0, json_tokener_error_parse_unexpected, 1, 0},
{"Infinity1234", 13, 8, json_tokener_success, 0, 0},
{"1234", 5, 4, json_tokener_success, 1, 0},
{"Infinity9999", 8, 8, json_tokener_continue, 0, 0},
/* returns the Infinity loaded up by the previous call: */
{"1234", 5, 0, json_tokener_success, 0, 0},
{"1234", 5, 4, json_tokener_success, 1, 0},
/* INT64_MAX */
{"[9223372036854775807]", 22, 21, json_tokener_success, 1, 0},
/* INT64_MAX+1 => parsed as uint64 */
{"[9223372036854775808]", 22, 21, json_tokener_success, 1, 0},
/* INT64_MIN */
{"[-9223372036854775808]", 23, 22, json_tokener_success, 1, 0},
/* INT64_MIN-1 => success, but value ends up capped */
{"[-9223372036854775809]", 23, 22, json_tokener_success, 1, 0},
/* INT64_MIN-1 => failure due to underflow detected */
{"[-9223372036854775809]", 23, 21, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
/* UINT64_MAX */
{"[18446744073709551615]", 23, 22, json_tokener_success, 1, 0},
/* UINT64_MAX+1 => success, but value ends up capped */
{"[18446744073709551616]", 23, 22, json_tokener_success, 1, 0},
/* UINT64_MAX+1 => failure due to overflow detected */
{"[18446744073709551616]", 23, 21, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
/* XXX this seems like a bug, should fail with _error_parse_number instead */
{"18446744073709551616", 21, 20, json_tokener_success, 1, 0},
{"18446744073709551616", 21, 20, json_tokener_error_parse_eof, 1, JSON_TOKENER_STRICT},
/* Exceeding integer limits as double parse OK */
{"[9223372036854775808.0]", 24, 23, json_tokener_success, 1, 0},
{"[-9223372036854775809.0]", 25, 24, json_tokener_success, 1, 0},
{"[-9223372036854775809.0]", 25, 24, json_tokener_success, 1, JSON_TOKENER_STRICT},
{"[18446744073709551615.0]", 25, 24, json_tokener_success, 1, 0},
{"[18446744073709551616.0]", 25, 24, json_tokener_success, 1, 0},
{"[18446744073709551616.0]", 25, 24, json_tokener_success, 1, JSON_TOKENER_STRICT},
/* offset=1 because "n" is the start of "null". hmm... */
{"noodle", 7, 1, json_tokener_error_parse_null, 1, 0},
/* offset=2 because "na" is the start of "nan". hmm... */
{"naodle", 7, 2, json_tokener_error_parse_null, 1, 0},
/* offset=2 because "tr" is the start of "true". hmm... */
{"track", 6, 2, json_tokener_error_parse_boolean, 1, 0},
{"fail", 5, 2, json_tokener_error_parse_boolean, 1, 0},
/* Although they may initially look like they should fail,
* the next few tests check that parsing multiple sequential
* json objects in the input works as expected
*/
{"null123", 8, 4, json_tokener_success, 0, 0},
{&"null123"[4], 4, 3, json_tokener_success, 1, 0},
{"nullx", 6, 4, json_tokener_success, 0, 0},
{&"nullx"[4], 2, 0, json_tokener_error_parse_unexpected, 1, 0},
{"{\"a\":1}{\"b\":2}", 15, 7, json_tokener_success, 0, 0},
{&"{\"a\":1}{\"b\":2}"[7], 8, 7, json_tokener_success, 1, 0},
/*
* Though this may seem invalid at first glance, it
* parses as three separate numbers, 2015, -1 and -15
* Of course, simply pasting together a stream of arbitrary
* positive numbers won't work, since there'll be no way to
* tell where in e.g. "2015015" the next number stats, so
* a reliably parsable stream must not include json_type_int
* or json_type_double objects without some other delimiter.
* e.g. whitespace
*/
{&"2015-01-15"[0], 11, 4, json_tokener_success, 1, 0},
{&"2015-01-15"[4], 7, 3, json_tokener_success, 1, 0},
{&"2015-01-15"[7], 4, 3, json_tokener_success, 1, 0},
{&"2015 01 15"[0], 11, 5, json_tokener_success, 1, 0},
{&"2015 01 15"[4], 7, 4, json_tokener_success, 1, 0},
{&"2015 01 15"[7], 4, 3, json_tokener_success, 1, 0},
/* Strings have a well defined end point, so we can stop at the quote */
{"\"blue\"", -1, -1, json_tokener_success, 0, 0},
/* Check each of the escape sequences defined by the spec */
{"\"\\\"\"", -1, -1, json_tokener_success, 0, 0},
{"\"\\\\\"", -1, -1, json_tokener_success, 0, 0},
{"\"\\b\"", -1, -1, json_tokener_success, 0, 0},
{"\"\\f\"", -1, -1, json_tokener_success, 0, 0},
{"\"\\n\"", -1, -1, json_tokener_success, 0, 0},
{"\"\\r\"", -1, -1, json_tokener_success, 0, 0},
{"\"\\t\"", -1, -1, json_tokener_success, 0, 0},
{"\"\\/\"", -1, -1, json_tokener_success, 0, 0},
// Escaping a forward slash is optional
{"\"/\"", -1, -1, json_tokener_success, 0, 0},
/* Check wrong escape sequences */
{"\"\\a\"", -1, 2, json_tokener_error_parse_string, 1, 0},
/* Check '\'' in strict model */
{"\'foo\'", -1, 5, json_tokener_success, 1, 0},
{"\'foo\'", -1, 0, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
/* Parse array/object */
{"[1,2,3]", -1, -1, json_tokener_success, 0, 0},
{"[1,2,3}", -1, 6, json_tokener_error_parse_array, 1, 0},
{"{\"a\"}", -1, 4, json_tokener_error_parse_object_key_sep, 1, 0},
{"{\"a\":1]", -1, 6, json_tokener_error_parse_object_value_sep, 1, 0},
{"{\"a\"::1}", -1, 5, json_tokener_error_parse_unexpected, 1, 0},
{"{\"a\":}", -1, 5, json_tokener_error_parse_unexpected, 1, 0},
{"{\"a\":1,\"a\":2}", -1, -1, json_tokener_success, 1, 0},
{"\"a\":1}", -1, 3, json_tokener_success, 1, 0},
{"{\"a\":1", -1, -1, json_tokener_continue, 1, 0}, //}
{"[,]", -1, 1, json_tokener_error_parse_unexpected, 1, 0},
{"[,1]", -1, 1, json_tokener_error_parse_unexpected, 1, 0},
/* This behaviour doesn't entirely follow the json spec, but until we have
* a way to specify how strict to be we follow Postel's Law and be liberal
* in what we accept (up to a point).
*/
{"[1,2,3,]", -1, -1, json_tokener_success, 0, 0},
{"[1,2,3,]", -1, 7, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
{"[1,2,,3,]", -1, 5, json_tokener_error_parse_unexpected, 0, 0},
{"[1,2,,3,]", -1, 5, json_tokener_error_parse_unexpected, 0, JSON_TOKENER_STRICT},
{"{\"a\":1,}", -1, 7, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
// utf-8 test
// acsll encoding
{"\x22\x31\x32\x33\x61\x73\x63\x24\x25\x26\x22", -1, -1, json_tokener_success, 1,
JSON_TOKENER_VALIDATE_UTF8},
{"\x22\x31\x32\x33\x61\x73\x63\x24\x25\x26\x22", -1, -1, json_tokener_success, 1, 0},
// utf-8 encoding
{"\x22\xe4\xb8\x96\xe7\x95\x8c\x22", -1, -1, json_tokener_success, 1,
JSON_TOKENER_VALIDATE_UTF8},
{"\x22\xe4\xb8", -1, 3, json_tokener_error_parse_utf8_string, 0, JSON_TOKENER_VALIDATE_UTF8},
{"\x96\xe7\x95\x8c\x22", -1, 0, json_tokener_error_parse_utf8_string, 1,
JSON_TOKENER_VALIDATE_UTF8},
{"\x22\xe4\xb8\x96\xe7\x95\x8c\x22", -1, -1, json_tokener_success, 1, 0},
{"\x22\xcf\x80\xcf\x86\x22", -1, -1, json_tokener_success, 1, JSON_TOKENER_VALIDATE_UTF8},
{"\x22\xf0\xa5\x91\x95\x22", -1, -1, json_tokener_success, 1, JSON_TOKENER_VALIDATE_UTF8},
// wrong utf-8 encoding
{"\x22\xe6\x9d\x4e\x22", -1, 3, json_tokener_error_parse_utf8_string, 1,
JSON_TOKENER_VALIDATE_UTF8},
{"\x22\xe6\x9d\x4e\x22", -1, 5, json_tokener_success, 1, 0},
// GBK encoding
{"\x22\xc0\xee\xc5\xf4\x22", -1, 2, json_tokener_error_parse_utf8_string, 1,
JSON_TOKENER_VALIDATE_UTF8},
{"\x22\xc0\xee\xc5\xf4\x22", -1, 6, json_tokener_success, 1, 0},
// char after space
{"\x20\x20\x22\xe4\xb8\x96\x22", -1, -1, json_tokener_success, 1, JSON_TOKENER_VALIDATE_UTF8},
{"\x20\x20\x81\x22\xe4\xb8\x96\x22", -1, 2, json_tokener_error_parse_utf8_string, 1,
JSON_TOKENER_VALIDATE_UTF8},
{"\x5b\x20\x81\x31\x5d", -1, 2, json_tokener_error_parse_utf8_string, 1,
JSON_TOKENER_VALIDATE_UTF8},
// char in state inf
{"\x49\x6e\x66\x69\x6e\x69\x74\x79", 9, 8, json_tokener_success, 1, 0},
{"\x49\x6e\x66\x81\x6e\x69\x74\x79", -1, 3, json_tokener_error_parse_utf8_string, 1,
JSON_TOKENER_VALIDATE_UTF8},
// char in escape unicode
{"\x22\x5c\x75\x64\x38\x35\x35\x5c\x75\x64\x63\x35\x35\x22", 15, 14, json_tokener_success, 1,
JSON_TOKENER_VALIDATE_UTF8},
{"\x22\x5c\x75\x64\x38\x35\x35\xc0\x75\x64\x63\x35\x35\x22", -1, 8,
json_tokener_error_parse_utf8_string, 1, JSON_TOKENER_VALIDATE_UTF8},
{"\x22\x5c\x75\x64\x30\x30\x33\x31\xc0\x22", -1, 9, json_tokener_error_parse_utf8_string, 1,
JSON_TOKENER_VALIDATE_UTF8},
// char in number
{"\x31\x31\x81\x31\x31", -1, 2, json_tokener_error_parse_utf8_string, 1,
JSON_TOKENER_VALIDATE_UTF8},
// char in object
{"\x7b\x22\x31\x81\x22\x3a\x31\x7d", -1, 3, json_tokener_error_parse_utf8_string, 1,
JSON_TOKENER_VALIDATE_UTF8},
// Note, current asciiz APIs can't parse \x00, skip it
{ "\"0\x01\x02\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
"\x10\x11\x12\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\"",
-1, -1, json_tokener_success, 1, 0 },
{ "{\"0\x01\x02\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
"\x10\x11\x12\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\":1}",
-1, -1, json_tokener_success, 1, 0 },
// Test control chars again, this time in strict mode, which should fail
{ "\"\x01\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x02\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x03\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x04\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x05\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x06\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x07\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x08\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x09\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x0a\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x0b\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x0c\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x0d\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x0e\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x0f\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x10\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x11\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x12\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x13\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x14\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x15\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x16\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x17\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x18\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x19\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x1a\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x1b\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x1c\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x1d\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x1e\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "\"\x1f\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x01\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x02\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x03\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x04\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x05\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x06\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x07\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x08\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x09\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x0a\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x0b\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x0c\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x0d\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x0e\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x0f\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x10\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x11\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x12\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x13\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x14\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x15\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x16\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x17\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x18\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x19\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x1a\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x1b\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x1c\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x1d\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x1e\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{ "{\"\x1f\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
{NULL, -1, -1, json_tokener_success, 0, 0},
};
static void test_incremental_parse(void)
{
json_object *new_obj;
enum json_tokener_error jerr;
struct json_tokener *tok;
const char *string_to_parse;
int ii;
int num_ok, num_error;
num_ok = 0;
num_error = 0;
printf("Starting incremental tests.\n");
printf("Note: quotes and backslashes seen in the output here are literal values passed\n");
printf(" to the parse functions. e.g. this is 4 characters: \"\\f\"\n");
string_to_parse = "{ \"foo"; /* } */
printf("json_tokener_parse(%s) ... ", string_to_parse);
new_obj = json_tokener_parse(string_to_parse);
if (new_obj == NULL)
printf("%s", "got error as expected\n");
/* test incremental parsing in various forms */
tok = json_tokener_new();
for (ii = 0; incremental_steps[ii].string_to_parse != NULL; ii++)
{
int this_step_ok = 0;
struct incremental_step *step = &incremental_steps[ii];
int length = step->length;
size_t expected_char_offset;
json_tokener_set_flags(tok, step->tok_flags);
if (length == -1)
length = (int)strlen(step->string_to_parse);
if (step->char_offset == -1)
expected_char_offset = length;
else
expected_char_offset = step->char_offset;
printf("json_tokener_parse_ex(tok, %-12s, %3d) ... ", step->string_to_parse,
length);
new_obj = json_tokener_parse_ex(tok, step->string_to_parse, length);
jerr = json_tokener_get_error(tok);
if (step->expected_error != json_tokener_success)
{
if (new_obj != NULL)
printf("ERROR: invalid object returned: %s\n",
json_object_to_json_string(new_obj));
else if (jerr != step->expected_error)
printf("ERROR: got wrong error: %s\n",
json_tokener_error_desc(jerr));
else if (json_tokener_get_parse_end(tok) != expected_char_offset)
printf("ERROR: wrong char_offset %zu != expected %zu\n",
json_tokener_get_parse_end(tok), expected_char_offset);
else
{
printf("OK: got correct error: %s\n",
json_tokener_error_desc(jerr));
this_step_ok = 1;
}
}
else
{
if (new_obj == NULL &&
!(step->length >= 4 && strncmp(step->string_to_parse, "null", 4) == 0))
printf("ERROR: expected valid object, instead: %s\n",
json_tokener_error_desc(jerr));
else if (json_tokener_get_parse_end(tok) != expected_char_offset)
printf("ERROR: wrong char_offset %zu != expected %zu\n",
json_tokener_get_parse_end(tok), expected_char_offset);
else
{
printf("OK: got object of type [%s]: %s\n",
json_type_to_name(json_object_get_type(new_obj)),
json_object_to_json_string(new_obj));
this_step_ok = 1;
}
}
if (new_obj)
json_object_put(new_obj);
if (step->reset_tokener & 1)
json_tokener_reset(tok);
if (this_step_ok)
num_ok++;
else
num_error++;
}
json_tokener_free(tok);
printf("End Incremental Tests OK=%d ERROR=%d\n", num_ok, num_error);
}
@@ -0,0 +1,372 @@
new_obj.to_string("")="\u0003"
new_obj.to_string(/* hello */"foo")="foo"
new_obj.to_string(// hello
"foo")="foo"
new_obj.to_string("foo"blue)="foo"
new_obj.to_string('foo')="foo"
new_obj.to_string("\u0041\u0042\u0043")="ABC"
new_obj.to_string("\u4e16\u754c\u00df")="世界ß"
new_obj.to_string("\u4E16")="世"
new_obj.to_string("\u4e1")=null
new_obj.to_string("\u4e1@")=null
new_obj.to_string("\ud840\u4e16")="�世"
new_obj.to_string("\ud840")="�"
new_obj.to_string("\udd27")="�"
new_obj.to_string([9,'\uDAD)=null
new_obj.to_string("[9,'\uDAD")=null
new_obj.to_string("\uD836\uDE87")="�"
new_obj.to_string(null)=null
new_obj.to_string(NaN)=NaN
new_obj.to_string(-NaN)=null
new_obj.to_string(Inf)=null
new_obj.to_string(inf)=null
new_obj.to_string(Infinity)=Infinity
new_obj.to_string(infinity)=Infinity
new_obj.to_string(-Infinity)=-Infinity
new_obj.to_string(-infinity)=-Infinity
new_obj.to_string({ "min": Infinity, "max": -Infinity})={ "min": Infinity, "max": -Infinity }
new_obj.to_string(Infinity!)=Infinity
new_obj.to_string(Infinitynull)=Infinity
new_obj.to_string(InfinityXXXX)=Infinity
new_obj.to_string(-Infinitynull)=-Infinity
new_obj.to_string(-InfinityXXXX)=-Infinity
new_obj.to_string(Infinoodle)=null
new_obj.to_string(InfinAAA)=null
new_obj.to_string(-Infinoodle)=null
new_obj.to_string(-InfinAAA)=null
new_obj.to_string(True)=true
new_obj.to_string(False)=false
new_obj.to_string(tRue)=true
new_obj.to_string(fAlse)=false
new_obj.to_string(nAn)=NaN
new_obj.to_string(iNfinity)=Infinity
new_obj.to_string(12)=12
new_obj.to_string(12.3)=12.3
new_obj.to_string(12.3.4)=12.3
new_obj.to_string(2015-01-15)=2015
new_obj.to_string(12.3xxx)=12.3
new_obj.to_string(12.3{"a":123})=12.3
new_obj.to_string(12.3
)=12.3
new_obj.to_string(12.3 )=12.3
new_obj.to_string({"FoO" : -12.3E512})={ "FoO": -12.3E512 }
new_obj.to_string({"FoO" : -12.3e512})={ "FoO": -12.3e512 }
new_obj.to_string({"FoO" : -12.3E51.2})=null
new_obj.to_string({"FoO" : -12.3E512E12})=null
new_obj.to_string(["\n"])=[ "\n" ]
new_obj.to_string(["\nabc\n"])=[ "\nabc\n" ]
new_obj.to_string([null])=[ null ]
new_obj.to_string([])=[ ]
new_obj.to_string([false])=[ false ]
new_obj.to_string(["abc",null,"def",12])=[ "abc", null, "def", 12 ]
new_obj.to_string({})={ }
new_obj.to_string({ "foo": "bar" })={ "foo": "bar" }
new_obj.to_string({ 'foo': 'bar' })={ "foo": "bar" }
new_obj.to_string({ "foo": "bar", "baz": null, "bool0": true })={ "foo": "bar", "baz": null, "bool0": true }
new_obj.to_string({ "foo": [null, "foo"] })={ "foo": [ null, "foo" ] }
new_obj.to_string({ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "arr": [ 1, 2, 3, null, 5 ] })={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "arr": [ 1, 2, 3, null, 5 ] }
new_obj.to_string({ "abc": "blue
red\ngreen" })={ "abc": "blue\nred\ngreen" }
new_obj.to_string(null)=null
new_obj.to_string(false)=false
new_obj.to_string([0e])=[ 0.0 ]
new_obj.to_string([0e+])=[ 0.0 ]
new_obj.to_string([0e+-1])=null
new_obj.to_string("hello world!")="hello world!"
new_obj.to_string([9223372036854775806])=[ 9223372036854775806 ]
new_obj.to_string([9223372036854775807])=[ 9223372036854775807 ]
new_obj.to_string([9223372036854775808])=[ 9223372036854775808 ]
new_obj.to_string([-9223372036854775807])=[ -9223372036854775807 ]
new_obj.to_string([-9223372036854775808])=[ -9223372036854775808 ]
new_obj.to_string([-9223372036854775809])=[ -9223372036854775808 ]
new_obj.to_string([18446744073709551614])=[ 18446744073709551614 ]
new_obj.to_string([18446744073709551615])=[ 18446744073709551615 ]
new_obj.to_string([18446744073709551616])=[ 18446744073709551615 ]
==================================
new_obj.to_string()=null
new_obj.to_string({})=null
==================================
json_tokener_parse_verbose() OK
==================================
Starting incremental tests.
Note: quotes and backslashes seen in the output here are literal values passed
to the parse functions. e.g. this is 4 characters: "\f"
json_tokener_parse({ "foo) ... got error as expected
json_tokener_parse_ex(tok, { "foo": 123 }, 14) ... OK: got object of type [object]: { "foo": 123 }
json_tokener_parse_ex(tok, { "foo": 456 }, 14) ... OK: got object of type [object]: { "foo": 456 }
json_tokener_parse_ex(tok, { "foo": 789 }, 14) ... OK: got object of type [object]: { "foo": 789 }
json_tokener_parse_ex(tok, /* hello */{ "foo", 18) ... OK: got correct error: continue
json_tokener_parse_ex(tok, /* hello */:/* hello */, 23) ... OK: got correct error: continue
json_tokener_parse_ex(tok, "bar"/* hello */, 16) ... OK: got correct error: continue
json_tokener_parse_ex(tok, }/* hello */, 12) ... OK: got object of type [object]: { "foo": "bar" }
json_tokener_parse_ex(tok, / hello , 8) ... OK: got correct error: expected comment
json_tokener_parse_ex(tok, /* hello"foo", 13) ... OK: got correct error: continue
json_tokener_parse_ex(tok, /* hello*"foo", 14) ... OK: got correct error: continue
json_tokener_parse_ex(tok, // hello"foo", 13) ... OK: got correct error: continue
json_tokener_parse_ex(tok, { "foo , 6) ... OK: got correct error: continue
json_tokener_parse_ex(tok, ": {"bar , 8) ... OK: got correct error: continue
json_tokener_parse_ex(tok, ":13}} , 6) ... OK: got object of type [object]: { "foo": { "bar": 13 } }
json_tokener_parse_ex(tok, "\ , 2) ... OK: got correct error: continue
json_tokener_parse_ex(tok, u , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, d , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, 8 , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, 3 , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, 4 , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, \ , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, u , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, d , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, d , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, 1 , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, e" , 2) ... OK: got object of type [string]: "�"
json_tokener_parse_ex(tok, "\u , 3) ... OK: got correct error: continue
json_tokener_parse_ex(tok, d8 , 2) ... OK: got correct error: continue
json_tokener_parse_ex(tok, 34 , 2) ... OK: got correct error: continue
json_tokener_parse_ex(tok, \u , 2) ... OK: got correct error: continue
json_tokener_parse_ex(tok, dd , 2) ... OK: got correct error: continue
json_tokener_parse_ex(tok, 1e" , 3) ... OK: got object of type [string]: "�"
json_tokener_parse_ex(tok, "\ud834 , 7) ... OK: got correct error: continue
json_tokener_parse_ex(tok, \udd1e" , 7) ... OK: got object of type [string]: "�"
json_tokener_parse_ex(tok, "\ud834\ , 8) ... OK: got correct error: continue
json_tokener_parse_ex(tok, udd1e" , 6) ... OK: got object of type [string]: "�"
json_tokener_parse_ex(tok, "\ud834\u , 9) ... OK: got correct error: continue
json_tokener_parse_ex(tok, dd1e" , 5) ... OK: got object of type [string]: "�"
json_tokener_parse_ex(tok, "fff \ud834\ud, 14) ... OK: got correct error: continue
json_tokener_parse_ex(tok, d1e bar" , 8) ... OK: got object of type [string]: "fff � bar"
json_tokener_parse_ex(tok, "fff \ud834\udd, 15) ... OK: got correct error: continue
json_tokener_parse_ex(tok, 1e bar" , 7) ... OK: got object of type [string]: "fff � bar"
json_tokener_parse_ex(tok, "fff \ud83d\ude, 15) ... OK: got correct error: continue
json_tokener_parse_ex(tok, 00 bar" , 7) ... OK: got object of type [string]: "fff 😀 bar"
json_tokener_parse_ex(tok, "ä" , 4) ... OK: got object of type [string]: "ä"
json_tokener_parse_ex(tok, "ä" , 4) ... OK: got object of type [string]: "ä"
json_tokener_parse_ex(tok, { "foo , 6) ... OK: got correct error: continue
json_tokener_parse_ex(tok, : "bar"} , 8) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, "\uD836 , 7) ... OK: got correct error: continue
json_tokener_parse_ex(tok, \uDE87 , 6) ... OK: got correct error: continue
json_tokener_parse_ex(tok, " , 1) ... OK: got object of type [string]: "�"
json_tokener_parse_ex(tok, { "foo , 6) ... OK: got correct error: continue
json_tokener_parse_ex(tok, ": {"bar , 8) ... OK: got correct error: continue
json_tokener_parse_ex(tok, ":13}}XXXX , 10) ... OK: got object of type [object]: { "foo": { "bar": 13 } }
json_tokener_parse_ex(tok, XXXX , 4) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, {"x": 123 }"X", 14) ... OK: got object of type [object]: { "x": 123 }
json_tokener_parse_ex(tok, "Y" , 3) ... OK: got object of type [string]: "Y"
json_tokener_parse_ex(tok, {"foo":9}{"bar":8}, 18) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, {"foo":9}{"bar":8}, 18) ... OK: got object of type [object]: { "foo": 9 }
json_tokener_parse_ex(tok, {"b":8}ignored garbage, 22) ... OK: got object of type [object]: { "b": 8 }
json_tokener_parse_ex(tok, 1 , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, 2 , 2) ... OK: got object of type [int]: 12
json_tokener_parse_ex(tok, 12{ , 3) ... OK: got object of type [int]: 12
json_tokener_parse_ex(tok, [02] , 4) ... OK: got correct error: number expected
json_tokener_parse_ex(tok, 0e+0 , 5) ... OK: got object of type [double]: 0e+0
json_tokener_parse_ex(tok, [0e+0] , 6) ... OK: got object of type [array]: [ 0e+0 ]
json_tokener_parse_ex(tok, 0e , 2) ... OK: got correct error: continue
json_tokener_parse_ex(tok, 0e , 3) ... OK: got object of type [double]: 0
json_tokener_parse_ex(tok, 0e , 3) ... OK: got correct error: unexpected end of data
json_tokener_parse_ex(tok, [0e] , 4) ... OK: got object of type [array]: [ 0 ]
json_tokener_parse_ex(tok, [0e] , 4) ... OK: got correct error: number expected
json_tokener_parse_ex(tok, 0e+ , 3) ... OK: got correct error: continue
json_tokener_parse_ex(tok, 0e+ , 4) ... OK: got object of type [double]: 0
json_tokener_parse_ex(tok, 0e+ , 4) ... OK: got correct error: unexpected end of data
json_tokener_parse_ex(tok, [0e+] , 5) ... OK: got object of type [array]: [ 0 ]
json_tokener_parse_ex(tok, [0e+] , 5) ... OK: got correct error: number expected
json_tokener_parse_ex(tok, 0e- , 3) ... OK: got correct error: continue
json_tokener_parse_ex(tok, 0e- , 4) ... OK: got object of type [double]: 0
json_tokener_parse_ex(tok, 0e- , 4) ... OK: got correct error: unexpected end of data
json_tokener_parse_ex(tok, [0e-] , 5) ... OK: got object of type [array]: [ 0 ]
json_tokener_parse_ex(tok, [0e-] , 5) ... OK: got correct error: number expected
json_tokener_parse_ex(tok, 0e+- , 5) ... OK: got object of type [double]: 0
json_tokener_parse_ex(tok, 0e+- , 5) ... OK: got correct error: number expected
json_tokener_parse_ex(tok, [0e+-] , 6) ... OK: got correct error: number expected
json_tokener_parse_ex(tok, false , 5) ... OK: got correct error: continue
json_tokener_parse_ex(tok, false , 6) ... OK: got object of type [boolean]: false
json_tokener_parse_ex(tok, true , 4) ... OK: got correct error: continue
json_tokener_parse_ex(tok, true , 5) ... OK: got object of type [boolean]: true
json_tokener_parse_ex(tok, null , 4) ... OK: got correct error: continue
json_tokener_parse_ex(tok, null , 5) ... OK: got object of type [null]: null
json_tokener_parse_ex(tok, Infinity , 9) ... OK: got object of type [double]: Infinity
json_tokener_parse_ex(tok, infinity , 9) ... OK: got object of type [double]: Infinity
json_tokener_parse_ex(tok, infinity , 9) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, -infinity , 10) ... OK: got object of type [double]: -Infinity
json_tokener_parse_ex(tok, -infinity , 10) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, inf , 3) ... OK: got correct error: continue
json_tokener_parse_ex(tok, inity , 6) ... OK: got object of type [double]: Infinity
json_tokener_parse_ex(tok, -inf , 4) ... OK: got correct error: continue
json_tokener_parse_ex(tok, inity , 6) ... OK: got object of type [double]: -Infinity
json_tokener_parse_ex(tok, i , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, n , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, f , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, i , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, n , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, i , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, t , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, y , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, , 1) ... OK: got object of type [double]: Infinity
json_tokener_parse_ex(tok, - , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, inf , 3) ... OK: got correct error: continue
json_tokener_parse_ex(tok, ini , 3) ... OK: got correct error: continue
json_tokener_parse_ex(tok, ty , 3) ... OK: got object of type [double]: -Infinity
json_tokener_parse_ex(tok, - , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, i , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, nfini , 5) ... OK: got correct error: continue
json_tokener_parse_ex(tok, ty , 3) ... OK: got object of type [double]: -Infinity
json_tokener_parse_ex(tok, -i , 2) ... OK: got correct error: continue
json_tokener_parse_ex(tok, nfinity , 8) ... OK: got object of type [double]: -Infinity
json_tokener_parse_ex(tok, InfinityX , 10) ... OK: got object of type [double]: Infinity
json_tokener_parse_ex(tok, X , 1) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, Infinity1234, 13) ... OK: got object of type [double]: Infinity
json_tokener_parse_ex(tok, 1234 , 5) ... OK: got object of type [int]: 1234
json_tokener_parse_ex(tok, Infinity9999, 8) ... OK: got correct error: continue
json_tokener_parse_ex(tok, 1234 , 5) ... OK: got object of type [double]: Infinity
json_tokener_parse_ex(tok, 1234 , 5) ... OK: got object of type [int]: 1234
json_tokener_parse_ex(tok, [9223372036854775807], 22) ... OK: got object of type [array]: [ 9223372036854775807 ]
json_tokener_parse_ex(tok, [9223372036854775808], 22) ... OK: got object of type [array]: [ 9223372036854775808 ]
json_tokener_parse_ex(tok, [-9223372036854775808], 23) ... OK: got object of type [array]: [ -9223372036854775808 ]
json_tokener_parse_ex(tok, [-9223372036854775809], 23) ... OK: got object of type [array]: [ -9223372036854775808 ]
json_tokener_parse_ex(tok, [-9223372036854775809], 23) ... OK: got correct error: number expected
json_tokener_parse_ex(tok, [18446744073709551615], 23) ... OK: got object of type [array]: [ 18446744073709551615 ]
json_tokener_parse_ex(tok, [18446744073709551616], 23) ... OK: got object of type [array]: [ 18446744073709551615 ]
json_tokener_parse_ex(tok, [18446744073709551616], 23) ... OK: got correct error: number expected
json_tokener_parse_ex(tok, 18446744073709551616, 21) ... OK: got object of type [int]: 18446744073709551615
json_tokener_parse_ex(tok, 18446744073709551616, 21) ... OK: got correct error: unexpected end of data
json_tokener_parse_ex(tok, [9223372036854775808.0], 24) ... OK: got object of type [array]: [ 9223372036854775808.0 ]
json_tokener_parse_ex(tok, [-9223372036854775809.0], 25) ... OK: got object of type [array]: [ -9223372036854775809.0 ]
json_tokener_parse_ex(tok, [-9223372036854775809.0], 25) ... OK: got object of type [array]: [ -9223372036854775809.0 ]
json_tokener_parse_ex(tok, [18446744073709551615.0], 25) ... OK: got object of type [array]: [ 18446744073709551615.0 ]
json_tokener_parse_ex(tok, [18446744073709551616.0], 25) ... OK: got object of type [array]: [ 18446744073709551616.0 ]
json_tokener_parse_ex(tok, [18446744073709551616.0], 25) ... OK: got object of type [array]: [ 18446744073709551616.0 ]
json_tokener_parse_ex(tok, noodle , 7) ... OK: got correct error: null expected
json_tokener_parse_ex(tok, naodle , 7) ... OK: got correct error: null expected
json_tokener_parse_ex(tok, track , 6) ... OK: got correct error: boolean expected
json_tokener_parse_ex(tok, fail , 5) ... OK: got correct error: boolean expected
json_tokener_parse_ex(tok, null123 , 8) ... OK: got object of type [null]: null
json_tokener_parse_ex(tok, 123 , 4) ... OK: got object of type [int]: 123
json_tokener_parse_ex(tok, nullx , 6) ... OK: got object of type [null]: null
json_tokener_parse_ex(tok, x , 2) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, {"a":1}{"b":2}, 15) ... OK: got object of type [object]: { "a": 1 }
json_tokener_parse_ex(tok, {"b":2} , 8) ... OK: got object of type [object]: { "b": 2 }
json_tokener_parse_ex(tok, 2015-01-15 , 11) ... OK: got object of type [int]: 2015
json_tokener_parse_ex(tok, -01-15 , 7) ... OK: got object of type [int]: -1
json_tokener_parse_ex(tok, -15 , 4) ... OK: got object of type [int]: -15
json_tokener_parse_ex(tok, 2015 01 15 , 11) ... OK: got object of type [int]: 2015
json_tokener_parse_ex(tok, 01 15 , 7) ... OK: got object of type [int]: 1
json_tokener_parse_ex(tok, 15 , 4) ... OK: got object of type [int]: 15
json_tokener_parse_ex(tok, "blue" , 6) ... OK: got object of type [string]: "blue"
json_tokener_parse_ex(tok, "\"" , 4) ... OK: got object of type [string]: "\""
json_tokener_parse_ex(tok, "\\" , 4) ... OK: got object of type [string]: "\\"
json_tokener_parse_ex(tok, "\b" , 4) ... OK: got object of type [string]: "\b"
json_tokener_parse_ex(tok, "\f" , 4) ... OK: got object of type [string]: "\f"
json_tokener_parse_ex(tok, "\n" , 4) ... OK: got object of type [string]: "\n"
json_tokener_parse_ex(tok, "\r" , 4) ... OK: got object of type [string]: "\r"
json_tokener_parse_ex(tok, "\t" , 4) ... OK: got object of type [string]: "\t"
json_tokener_parse_ex(tok, "\/" , 4) ... OK: got object of type [string]: "\/"
json_tokener_parse_ex(tok, "/" , 3) ... OK: got object of type [string]: "\/"
json_tokener_parse_ex(tok, "\a" , 4) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, 'foo' , 5) ... OK: got object of type [string]: "foo"
json_tokener_parse_ex(tok, 'foo' , 5) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, [1,2,3] , 7) ... OK: got object of type [array]: [ 1, 2, 3 ]
json_tokener_parse_ex(tok, [1,2,3} , 7) ... OK: got correct error: array value separator ',' expected
json_tokener_parse_ex(tok, {"a"} , 5) ... OK: got correct error: object property name separator ':' expected
json_tokener_parse_ex(tok, {"a":1] , 7) ... OK: got correct error: object value separator ',' expected
json_tokener_parse_ex(tok, {"a"::1} , 8) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, {"a":} , 6) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, {"a":1,"a":2}, 13) ... OK: got object of type [object]: { "a": 2 }
json_tokener_parse_ex(tok, "a":1} , 6) ... OK: got object of type [string]: "a"
json_tokener_parse_ex(tok, {"a":1 , 6) ... OK: got correct error: continue
json_tokener_parse_ex(tok, [,] , 3) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, [,1] , 4) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, [1,2,3,] , 8) ... OK: got object of type [array]: [ 1, 2, 3 ]
json_tokener_parse_ex(tok, [1,2,3,] , 8) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, [1,2,,3,] , 9) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, [1,2,,3,] , 9) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, {"a":1,} , 8) ... OK: got correct error: unexpected character
json_tokener_parse_ex(tok, "123asc$%&" , 11) ... OK: got object of type [string]: "123asc$%&"
json_tokener_parse_ex(tok, "123asc$%&" , 11) ... OK: got object of type [string]: "123asc$%&"
json_tokener_parse_ex(tok, "世界" , 8) ... OK: got object of type [string]: "世界"
json_tokener_parse_ex(tok, "ä¸ , 3) ... OK: got correct error: invalid utf-8 string
json_tokener_parse_ex(tok, –界" , 5) ... OK: got correct error: invalid utf-8 string
json_tokener_parse_ex(tok, "世界" , 8) ... OK: got object of type [string]: "世界"
json_tokener_parse_ex(tok, "πφ" , 6) ... OK: got object of type [string]: "πφ"
json_tokener_parse_ex(tok, "𥑕" , 6) ... OK: got object of type [string]: "𥑕"
json_tokener_parse_ex(tok, "æ�N" , 5) ... OK: got correct error: invalid utf-8 string
json_tokener_parse_ex(tok, "æ�N" , 5) ... OK: got object of type [string]: "æ�N"
json_tokener_parse_ex(tok, "ÀîÅô" , 6) ... OK: got correct error: invalid utf-8 string
json_tokener_parse_ex(tok, "ÀîÅô" , 6) ... OK: got object of type [string]: "ÀîÅô"
json_tokener_parse_ex(tok, "世" , 7) ... OK: got object of type [string]: "世"
json_tokener_parse_ex(tok, �"世" , 8) ... OK: got correct error: invalid utf-8 string
json_tokener_parse_ex(tok, [ �1] , 5) ... OK: got correct error: invalid utf-8 string
json_tokener_parse_ex(tok, Infinity , 9) ... OK: got object of type [double]: Infinity
json_tokener_parse_ex(tok, Inf�nity , 8) ... OK: got correct error: invalid utf-8 string
json_tokener_parse_ex(tok, "\ud855\udc55", 15) ... OK: got object of type [string]: "𥑕"
json_tokener_parse_ex(tok, "\ud855Àudc55", 14) ... OK: got correct error: invalid utf-8 string
json_tokener_parse_ex(tok, "\ud0031À" , 10) ... OK: got correct error: invalid utf-8 string
json_tokener_parse_ex(tok, 11�11 , 5) ... OK: got correct error: invalid utf-8 string
json_tokener_parse_ex(tok, {"1�":1} , 8) ... OK: got correct error: invalid utf-8 string
json_tokener_parse_ex(tok, "0
␍", 36) ... OK: got object of type [string]: "0\u0001\u0002\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f"
json_tokener_parse_ex(tok, {"0
␍":1}, 40) ... OK: got object of type [object]: { "0\u0001\u0002\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f": 1 }
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, " " , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "
" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, " " , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, " " , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "␍" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {" ":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"
":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {" ":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {" ":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"␍":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
End Incremental Tests OK=272 ERROR=0
==================================
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,192 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "json_inttypes.h"
#include "json_util.h"
void checkit(const char *buf)
{
int64_t cint64 = -666;
int retval = json_parse_int64(buf, &cint64);
printf("buf=%s parseit=%d, value=%" PRId64 " \n", buf, retval, cint64);
}
void checkit_uint(const char *buf)
{
uint64_t cuint64 = 666;
int retval = json_parse_uint64(buf, &cuint64);
printf("buf=%s parseit=%d, value=%" PRIu64 " \n", buf, retval, cuint64);
}
/**
* This test calls json_parse_int64 and json_parse_int64 with a variety
* of different strings. It's purpose is to ensure that the results are
* consistent across all different environments that it might be executed in.
*
* This always exits with a 0 exit value. The output should be compared
* against previously saved expected output.
*/
int main(int argc, char **argv)
{
char buf[100];
printf("==========json_parse_int64() test===========\n");
checkit("x");
checkit("0");
checkit("-0");
checkit("00000000");
checkit("-00000000");
checkit("1");
strcpy(buf, "2147483647"); // aka INT32_MAX
checkit(buf);
strcpy(buf, "-1");
checkit(buf);
strcpy(buf, " -1");
checkit(buf);
strcpy(buf, "00001234");
checkit(buf);
strcpy(buf, "0001234x");
checkit(buf);
strcpy(buf, "-00001234");
checkit(buf);
strcpy(buf, "-00001234x");
checkit(buf);
strcpy(buf, "4294967295"); // aka UINT32_MAX
checkit(buf);
strcpy(buf, "4294967296"); // aka UINT32_MAX + 1
checkit(buf);
strcpy(buf, "21474836470"); // INT32_MAX * 10
checkit(buf);
strcpy(buf, "31474836470"); // INT32_MAX * 10 + a bunch
checkit(buf);
strcpy(buf, "-2147483647"); // INT32_MIN + 1
checkit(buf);
strcpy(buf, "-2147483648"); // INT32_MIN
checkit(buf);
strcpy(buf, "-2147483649"); // INT32_MIN - 1
checkit(buf);
strcpy(buf, "-21474836480"); // INT32_MIN * 10
checkit(buf);
strcpy(buf, "9223372036854775806"); // INT64_MAX - 1
checkit(buf);
strcpy(buf, "9223372036854775807"); // INT64_MAX
checkit(buf);
strcpy(buf, "9223372036854775808"); // INT64_MAX + 1
checkit(buf);
strcpy(buf, "-9223372036854775808"); // INT64_MIN
checkit(buf);
strcpy(buf, "-9223372036854775809"); // INT64_MIN - 1
checkit(buf);
strcpy(buf, "18446744073709551614"); // UINT64_MAX - 1
checkit(buf);
strcpy(buf, "18446744073709551615"); // UINT64_MAX
checkit(buf);
strcpy(buf, "18446744073709551616"); // UINT64_MAX + 1
checkit(buf);
strcpy(buf, "-18446744073709551616"); // -UINT64_MAX
checkit(buf);
// Ensure we can still parse valid numbers after parsing out of range ones.
strcpy(buf, "123");
checkit(buf);
printf("\n==========json_parse_uint64() test===========\n");
checkit_uint("x");
checkit_uint("0");
checkit_uint("-0");
checkit_uint("00000000");
checkit_uint("-00000000");
checkit_uint("1");
strcpy(buf, "2147483647"); // aka INT32_MAX
checkit_uint(buf);
strcpy(buf, "-1");
checkit_uint(buf);
strcpy(buf, "-9223372036854775808");
checkit_uint(buf);
strcpy(buf, " 1");
checkit_uint(buf);
strcpy(buf, "00001234");
checkit_uint(buf);
strcpy(buf, "0001234x");
checkit_uint(buf);
strcpy(buf, "4294967295"); // aka UINT32_MAX
checkit_uint(buf);
strcpy(buf, "4294967296"); // aka UINT32_MAX + 1
checkit_uint(buf);
strcpy(buf, "21474836470"); // INT32_MAX * 10
checkit_uint(buf);
strcpy(buf, "31474836470"); // INT32_MAX * 10 + a bunch
checkit_uint(buf);
strcpy(buf, "9223372036854775806"); // INT64_MAX - 1
checkit_uint(buf);
strcpy(buf, "9223372036854775807"); // INT64_MAX
checkit_uint(buf);
strcpy(buf, "9223372036854775808"); // INT64_MAX + 1
checkit_uint(buf);
strcpy(buf, "18446744073709551614"); // UINT64_MAX - 1
checkit_uint(buf);
strcpy(buf, "18446744073709551615"); // UINT64_MAX
checkit_uint(buf);
strcpy(buf, "18446744073709551616"); // UINT64_MAX + 1
checkit_uint(buf);
// Ensure we can still parse valid numbers after parsing out of range ones.
strcpy(buf, "123");
checkit_uint(buf);
return 0;
}
@@ -0,0 +1,57 @@
==========json_parse_int64() test===========
buf=x parseit=1, value=-666
buf=0 parseit=0, value=0
buf=-0 parseit=0, value=0
buf=00000000 parseit=0, value=0
buf=-00000000 parseit=0, value=0
buf=1 parseit=0, value=1
buf=2147483647 parseit=0, value=2147483647
buf=-1 parseit=0, value=-1
buf= -1 parseit=0, value=-1
buf=00001234 parseit=0, value=1234
buf=0001234x parseit=0, value=1234
buf=-00001234 parseit=0, value=-1234
buf=-00001234x parseit=0, value=-1234
buf=4294967295 parseit=0, value=4294967295
buf=4294967296 parseit=0, value=4294967296
buf=21474836470 parseit=0, value=21474836470
buf=31474836470 parseit=0, value=31474836470
buf=-2147483647 parseit=0, value=-2147483647
buf=-2147483648 parseit=0, value=-2147483648
buf=-2147483649 parseit=0, value=-2147483649
buf=-21474836480 parseit=0, value=-21474836480
buf=9223372036854775806 parseit=0, value=9223372036854775806
buf=9223372036854775807 parseit=0, value=9223372036854775807
buf=9223372036854775808 parseit=0, value=9223372036854775807
buf=-9223372036854775808 parseit=0, value=-9223372036854775808
buf=-9223372036854775809 parseit=0, value=-9223372036854775808
buf=18446744073709551614 parseit=0, value=9223372036854775807
buf=18446744073709551615 parseit=0, value=9223372036854775807
buf=18446744073709551616 parseit=0, value=9223372036854775807
buf=-18446744073709551616 parseit=0, value=-9223372036854775808
buf=123 parseit=0, value=123
==========json_parse_uint64() test===========
buf=x parseit=1, value=666
buf=0 parseit=0, value=0
buf=-0 parseit=1, value=666
buf=00000000 parseit=0, value=0
buf=-00000000 parseit=1, value=666
buf=1 parseit=0, value=1
buf=2147483647 parseit=0, value=2147483647
buf=-1 parseit=1, value=666
buf=-9223372036854775808 parseit=1, value=666
buf= 1 parseit=0, value=1
buf=00001234 parseit=0, value=1234
buf=0001234x parseit=0, value=1234
buf=4294967295 parseit=0, value=4294967295
buf=4294967296 parseit=0, value=4294967296
buf=21474836470 parseit=0, value=21474836470
buf=31474836470 parseit=0, value=31474836470
buf=9223372036854775806 parseit=0, value=9223372036854775806
buf=9223372036854775807 parseit=0, value=9223372036854775807
buf=9223372036854775808 parseit=0, value=9223372036854775808
buf=18446744073709551614 parseit=0, value=18446744073709551614
buf=18446744073709551615 parseit=0, value=18446744073709551615
buf=18446744073709551616 parseit=0, value=18446744073709551615
buf=123 parseit=0, value=123
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,189 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "debug.h"
#include "printbuf.h"
static void test_basic_printbuf_memset(void);
static void test_printbuf_memset_length(void);
#ifndef __func__
/* VC++ compat */
#define __func__ __FUNCTION__
#endif
static void test_basic_printbuf_memset(void)
{
struct printbuf *pb;
printf("%s: starting test\n", __func__);
pb = printbuf_new();
sprintbuf(pb, "blue:%d", 1);
printbuf_memset(pb, -1, 'x', 52);
printf("Buffer contents:%.*s\n", printbuf_length(pb), pb->buf);
printbuf_free(pb);
printf("%s: end test\n", __func__);
}
static void test_printbuf_memset_length(void)
{
struct printbuf *pb;
printf("%s: starting test\n", __func__);
pb = printbuf_new();
printbuf_memset(pb, -1, ' ', 0);
printbuf_memset(pb, -1, ' ', 0);
printbuf_memset(pb, -1, ' ', 0);
printbuf_memset(pb, -1, ' ', 0);
printbuf_memset(pb, -1, ' ', 0);
printf("Buffer length: %d\n", printbuf_length(pb));
printbuf_memset(pb, -1, ' ', 2);
printbuf_memset(pb, -1, ' ', 4);
printbuf_memset(pb, -1, ' ', 6);
printf("Buffer length: %d\n", printbuf_length(pb));
printbuf_memset(pb, -1, ' ', 6);
printf("Buffer length: %d\n", printbuf_length(pb));
printbuf_memset(pb, -1, ' ', 8);
printbuf_memset(pb, -1, ' ', 10);
printbuf_memset(pb, -1, ' ', 10);
printbuf_memset(pb, -1, ' ', 10);
printbuf_memset(pb, -1, ' ', 20);
printf("Buffer length: %d\n", printbuf_length(pb));
// No length change should occur
printbuf_memset(pb, 0, 'x', 30);
printf("Buffer length: %d\n", printbuf_length(pb));
// This should extend it by one.
printbuf_memset(pb, 0, 'x', printbuf_length(pb) + 1);
printf("Buffer length: %d\n", printbuf_length(pb));
printbuf_free(pb);
printf("%s: end test\n", __func__);
}
static void test_printbuf_memappend(int *before_resize);
static void test_printbuf_memappend(int *before_resize)
{
struct printbuf *pb;
int initial_size;
printf("%s: starting test\n", __func__);
pb = printbuf_new();
printf("Buffer length: %d\n", printbuf_length(pb));
initial_size = pb->size;
while (pb->size == initial_size)
{
printbuf_memappend_fast(pb, "x", 1);
}
*before_resize = printbuf_length(pb) - 1;
printf("Appended %d bytes for resize: [%s]\n", *before_resize + 1, pb->buf);
printbuf_reset(pb);
printbuf_memappend_fast(pb, "bluexyz123", 3);
printf("Partial append: %d, [%s]\n", printbuf_length(pb), pb->buf);
char with_nulls[] = {'a', 'b', '\0', 'c'};
printbuf_reset(pb);
printbuf_memappend_fast(pb, with_nulls, (int)sizeof(with_nulls));
printf("With embedded \\0 character: %d, [%s]\n", printbuf_length(pb), pb->buf);
printbuf_free(pb);
pb = printbuf_new();
char *data = malloc(*before_resize);
memset(data, 'X', *before_resize);
printbuf_memappend_fast(pb, data, *before_resize);
printf("Append to just before resize: %d, [%s]\n", printbuf_length(pb), pb->buf);
free(data);
printbuf_free(pb);
pb = printbuf_new();
data = malloc(*before_resize + 1);
memset(data, 'X', *before_resize + 1);
printbuf_memappend_fast(pb, data, *before_resize + 1);
printf("Append to just after resize: %d, [%s]\n", printbuf_length(pb), pb->buf);
free(data);
printbuf_free(pb);
#define SA_TEST_STR "XXXXXXXXXXXXXXXX"
pb = printbuf_new();
printbuf_strappend(pb, SA_TEST_STR);
printf("Buffer size after printbuf_strappend(): %d, [%s]\n", printbuf_length(pb), pb->buf);
printbuf_free(pb);
#undef SA_TEST_STR
printf("%s: end test\n", __func__);
}
static void test_sprintbuf(int before_resize);
static void test_sprintbuf(int before_resize)
{
struct printbuf *pb;
const char *max_char =
"if string is greater than stack buffer, then use dynamic string"
" with vasprintf. Note: some implementation of vsnprintf return -1 "
" if output is truncated whereas some return the number of bytes that "
" would have been written - this code handles both cases.";
printf("%s: starting test\n", __func__);
pb = printbuf_new();
printf("Buffer length: %d\n", printbuf_length(pb));
char *data = malloc(before_resize + 1 + 1);
memset(data, 'X', before_resize + 1 + 1);
data[before_resize + 1] = '\0';
sprintbuf(pb, "%s", data);
free(data);
printf("sprintbuf to just after resize(%d+1): %d, [%s], strlen(buf)=%d\n", before_resize,
printbuf_length(pb), pb->buf, (int)strlen(pb->buf));
printbuf_reset(pb);
sprintbuf(pb, "plain");
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
sprintbuf(pb, "%d", 1);
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
sprintbuf(pb, "%d", INT_MAX);
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
sprintbuf(pb, "%d", INT_MIN);
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
sprintbuf(pb, "%s", "%s");
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
sprintbuf(pb, max_char);
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
printbuf_free(pb);
printf("%s: end test\n", __func__);
}
int main(int argc, char **argv)
{
int before_resize = 0;
MC_SET_DEBUG(1);
test_basic_printbuf_memset();
printf("========================================\n");
test_printbuf_memset_length();
printf("========================================\n");
test_printbuf_memappend(&before_resize);
printf("========================================\n");
test_sprintbuf(before_resize);
printf("========================================\n");
return 0;
}
@@ -0,0 +1,34 @@
test_basic_printbuf_memset: starting test
Buffer contents:blue:1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
test_basic_printbuf_memset: end test
========================================
test_printbuf_memset_length: starting test
Buffer length: 0
Buffer length: 12
Buffer length: 18
Buffer length: 76
Buffer length: 76
Buffer length: 77
test_printbuf_memset_length: end test
========================================
test_printbuf_memappend: starting test
Buffer length: 0
Appended 32 bytes for resize: [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]
Partial append: 3, [blu]
With embedded \0 character: 4, [ab]
Append to just before resize: 31, [XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX]
Append to just after resize: 32, [XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX]
Buffer size after printbuf_strappend(): 16, [XXXXXXXXXXXXXXXX]
test_printbuf_memappend: end test
========================================
test_sprintbuf: starting test
Buffer length: 0
sprintbuf to just after resize(31+1): 32, [XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX], strlen(buf)=32
5, [plain]
6, [plain1]
16, [plain12147483647]
27, [plain12147483647-2147483648]
29, [plain12147483647-2147483648%s]
284, [plain12147483647-2147483648%sif string is greater than stack buffer, then use dynamic string with vasprintf. Note: some implementation of vsnprintf return -1 if output is truncated whereas some return the number of bytes that would have been written - this code handles both cases.]
test_sprintbuf: end test
========================================
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,249 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "json.h"
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 void test_example_set_with_limit_index(void)
{
struct json_object *jo2, *jo1 = json_tokener_parse(input_json_str);
size_t limit_index = 10;
// testing if json_pointer_set_with_limit_index() works as json_pointer_set()
assert(jo1 != NULL);
printf("PASSED - SET_WITH_LIMIT - LOADED TEST JSON\n");
printf("%s\n", json_object_get_string(jo1));
assert(0 == json_pointer_set_with_limit_index(&jo1, "/foo/1", json_object_new_string("cod"), limit_index));
assert(0 == strcmp("cod", json_object_get_string(json_object_array_get_idx(
json_object_object_get(jo1, "foo"), 1))));
printf("PASSED - SET_WITH_LIMIT - 'cod' in /foo/1\n");
assert(0 != json_pointer_set_with_limit_index(&jo1, "/fud/gaw", (jo2 = json_tokener_parse("[1,2,3]")), limit_index));
assert(errno == ENOENT);
printf("PASSED - SET_WITH_LIMIT - non-existing /fud/gaw\n");
assert(0 == json_pointer_set_with_limit_index(&jo1, "/fud", json_object_new_object(), limit_index));
printf("PASSED - SET_WITH_LIMIT - /fud == {}\n");
assert(0 == json_pointer_set_with_limit_index(&jo1, "/fud/gaw", jo2, limit_index)); /* re-using jo2 from above */
printf("PASSED - SET_WITH_LIMIT - /fug/gaw == [1,2,3]\n");
assert(0 == json_pointer_set_with_limit_index(&jo1, "/fud/gaw/0", json_object_new_int(0), limit_index));
assert(0 == json_pointer_setf(&jo1, json_object_new_int(0), "%s%s/%d", "/fud", "/gaw", 0));
printf("PASSED - SET_WITH_LIMIT - /fug/gaw == [0,2,3]\n");
assert(0 == json_pointer_set_with_limit_index(&jo1, "/fud/gaw/-", json_object_new_int(4), limit_index));
printf("PASSED - SET_WITH_LIMIT - /fug/gaw == [0,2,3,4]\n");
assert(0 == json_pointer_set_with_limit_index(&jo1, "/", json_object_new_int(9), limit_index));
printf("PASSED - SET_WITH_LIMIT - / == 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_WITH_LIMIT - Final JSON is: %s\n", json_object_get_string(jo1));
json_object_put(jo2);
assert(0 == json_pointer_set_with_limit_index(&jo1, "", json_object_new_int(10), limit_index));
assert(10 == json_object_get_int(jo1));
printf("%s\n", json_object_get_string(jo1));
json_object_put(jo1);
jo1 = json_tokener_parse("[0, 1, 2, 3]");
jo2 = json_tokener_parse("[0, 1, 2, 3, null, null, null, 7]");
assert(0 == json_pointer_set_with_limit_index(&jo1, "/7", json_object_new_int(7), limit_index));
assert(1 == json_object_equal(jo1, jo2));
json_object_put(jo1);
jo1 = json_tokener_parse("[0, 1, 2, 3]");
assert(0 == json_pointer_setf(&jo1, json_object_new_int(7), "/%u", 7));
assert(1 == json_object_equal(jo1, jo2));
json_object_put(jo1);
json_object_put(jo2);
// testing with limit_index
jo1 = json_tokener_parse("{'foo': ['bar', 'baz']}");
jo2 = json_tokener_parse("{'foo': ['bar', 'cod']}");
assert(0 == json_pointer_set_with_limit_index(&jo1, "/foo/1", json_object_new_string("cod"), limit_index));
assert(json_object_equal(jo1, jo2));
printf("PASSED - SET_LIMIT - Set value within limit (/foo/1 with limit 10)\n");
assert(0 == json_pointer_set_with_limit_index(&jo1, "/bar", json_object_new_string("new_field"), limit_index));
printf("PASSED - SET_LIMIT - Set value on an object (limit is ignored)\n");
assert(0 == json_pointer_set_with_limit_index(&jo1, "/foo/20", json_object_new_string("big_index"), (size_t)-1));
printf("PASSED - SET_LIMIT - Set value with limit_index = -1 (no limit)\n");
json_object_put(jo1);
json_object_put(jo2);
}
static void test_wrong_inputs_set_with_limit_index(void)
{
struct json_object *jo2, *jo1 = json_tokener_parse(input_json_str);
size_t limit_index = 10;
// testing if json_pointer_set_with_limit_index() works as json_pointer_set()
assert(jo1 != NULL);
printf("PASSED - SET_WITH_LIMIT - LOADED TEST JSON\n");
printf("%s\n", json_object_get_string(jo1));
assert(0 != json_pointer_set_with_limit_index(NULL, NULL, NULL, limit_index));
assert(0 != json_pointer_set_with_limit_index(&jo1, NULL, NULL, limit_index));
printf("PASSED - SET_WITH_LIMIT - failed with NULL params for input json & path\n");
assert(0 != json_pointer_set_with_limit_index(&jo1, "foo/bar", (jo2 = json_object_new_string("cod")), limit_index));
printf("PASSED - SET_WITH_LIMIT - failed 'cod' with path 'foo/bar'\n");
json_object_put(jo2);
// assert(0 !=
// json_pointer_setf(&jo1, (jo2 = json_object_new_string("cod")), "%s", "foo/bar"));
// printf("PASSED - SET_WITH_LIMIT - failed 'cod' with path 'foo/bar'\n");
// json_object_put(jo2);
assert(0 != json_pointer_set_with_limit_index(&jo1, "0", (jo2 = json_object_new_string("cod")), limit_index));
printf("PASSED - SET_WITH_LIMIT - failed with invalid array index'\n");
json_object_put(jo2);
jo2 = json_object_new_string("whatever");
assert(0 != json_pointer_set_with_limit_index(&jo1, "/fud/gaw", jo2, limit_index));
assert(0 == json_pointer_set_with_limit_index(&jo1, "/fud", json_object_new_object(), limit_index));
assert(0 == json_pointer_set_with_limit_index(&jo1, "/fud/gaw", jo2, limit_index)); /* re-using jo2 from above */
// ownership of jo2 transferred into jo1
jo2 = json_object_new_int(0);
assert(0 != json_pointer_set_with_limit_index(&jo1, "/fud/gaw/0", jo2, limit_index));
json_object_put(jo2);
jo2 = json_object_new_int(0);
assert(0 != json_pointer_set_with_limit_index(&jo1, "/fud/gaw/", jo2, limit_index));
json_object_put(jo2);
printf("PASSED - SET_WITH_LIMIT - failed to set index to non-array\n");
// assert(0 == json_pointer_setf(&jo1, json_object_new_string("cod"), "%s", "\0"));
json_object_put(jo1);
// testing with limit_index
jo1 = json_tokener_parse("{'foo': ['bar', 'baz']}");
errno = 0;
jo2 = json_object_new_string("out_of_bounds");
assert(0 != json_pointer_set_with_limit_index(&jo1, "/foo/20", jo2, limit_index));
assert(errno == EINVAL);
printf("PASSED - SET_LIMIT - Failed to set index 20 with limit 10\n");
// The value object was not consumed, so we must put it.
json_object_put(jo2);
// corner case: setting an index that equals the limit (should be ok, as it's idx > max_idx)
errno = 0;
assert(0 == json_pointer_set_with_limit_index(&jo1, "/foo/10", json_object_new_string("at_the_limit"), limit_index));
printf("PASSED - SET_LIMIT - Succeeded to set index 10 with limit 10\n");
json_object_put(jo1);
}
// callback for testing json_pointer_set_with_cb()
static int test_cb_print_msg(json_object *parent, const char *key, size_t idx,
json_object *value, void *priv)
{
printf("PASSED - SET_WITH_CB - This callback is called\n");
json_object_put(value);
return 0;
}
// callback for testing json_pointer_set_with_cb() with rejection logic
static int test_cb_reject_logic(json_object *parent, const char *key, size_t idx,
json_object *value, void *priv)
{
// Reject any operation if the key is "reject"
if (key && strcmp(key, "reject") == 0)
{
printf("PASSED - SET_WITH_CB - Callback correctly identified key 'reject' to reject\n");
// Note: caller of json_pointer_set must free value
return -1;
}
json_object_put(value);
return 0;
}
static void test_set_with_cb(void)
{
struct json_object *jo2, *jo1 = json_tokener_parse(input_json_str);
size_t limit_index = 5;
assert(jo1 != NULL);
printf("PASSED - SET_WITH_CB - LOADED TEST JSON\n");
printf("%s\n", json_object_get_string(jo1));
assert(0 == json_pointer_set_with_cb(&jo1, "/foo/1", json_object_new_string("cod"), test_cb_print_msg, 1, NULL));
printf("PASSED - SET_WITH_CB - callback test_cb_print_msg for /foo/1\n");
assert(0 == json_pointer_set_with_cb(&jo1, "/foo/4", json_object_new_string("in"), json_object_array_put_with_idx_limit_cb, 0, &limit_index));
printf("PASSED - SET_WITH_CB - callback json_object_array_put_with_idx_limit_cb for /foo/4 with limit_index 5\n");
assert(0 == json_pointer_set_with_cb(&jo1, "/foo/5", json_object_new_string("border"), json_object_array_put_with_idx_limit_cb, 0, &limit_index));
printf("PASSED - SET_WITH_CB - failed with callback json_object_array_put_with_idx_limit_cb for /foo/5 with limit_index 5\n");
assert(0 != json_pointer_set_with_cb(&jo1, "/foo/10", (jo2 = json_object_new_string("out")), json_object_array_put_with_idx_limit_cb, 0, &limit_index));
json_object_put(jo2);
assert(errno == EINVAL);
printf("PASSED - SET_WITH_CB - failed with callback json_object_array_put_with_idx_limit_cb for /foo/10 with limit_index 5\n");
assert(0 != json_pointer_set_with_cb(&jo1, "/foo/2", (jo2 = json_object_new_string("null_priv")), json_object_array_put_with_idx_limit_cb, 0, NULL));
json_object_put(jo2);
assert(errno == EFAULT);
printf("PASSED - SET_WITH_CB - failed with callback json_object_array_put_with_idx_limit_cb with NULL priv\n");
json_object_put(jo1);
jo1 = json_tokener_parse("{'foo': 'bar'}");
assert(jo1 != NULL);
assert(0 == json_pointer_set_with_cb(&jo1, "/foo", json_object_new_string("cod"), test_cb_print_msg, 1, NULL));
printf("PASSED - SET_WITH_CB - cb_handles_obj=1: callback was triggered for object operation\n");
assert(0 == json_pointer_set_with_cb(&jo1, "/new_key", json_object_new_string("new_value"), test_cb_print_msg, 0, NULL));
printf("PASSED - SET_WITH_CB - cb_handles_obj=0: callback was NOT triggered for object operation, default logic was used\n");
json_object_put(jo1);
// testing rejection logic callback
jo1 = json_tokener_parse("{'data': {} }");
assert(jo1 != NULL);
assert(0 == json_pointer_set_with_cb(&jo1, "/data/accept", json_object_new_string("accepted_value"), test_cb_reject_logic, 1, NULL));
printf("PASSED - SET_WITH_CB - Rejection callback approved an allowed key\n");
assert(0 != json_pointer_set_with_cb(&jo1, "/data/reject", (jo2 = json_object_new_string("rejected_value")), test_cb_reject_logic, 1, NULL));
json_object_put(jo2);
printf("PASSED - SET_WITH_CB - Rejection callback rejected a forbidden key\n");
json_object_put(jo1);
}
int main(int argc, char **argv)
{
test_example_set_with_limit_index();
test_wrong_inputs_set_with_limit_index();
test_set_with_cb();
return 0;
}
@@ -0,0 +1,36 @@
PASSED - SET_WITH_LIMIT - 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_WITH_LIMIT - 'cod' in /foo/1
PASSED - SET_WITH_LIMIT - non-existing /fud/gaw
PASSED - SET_WITH_LIMIT - /fud == {}
PASSED - SET_WITH_LIMIT - /fug/gaw == [1,2,3]
PASSED - SET_WITH_LIMIT - /fug/gaw == [0,2,3]
PASSED - SET_WITH_LIMIT - /fug/gaw == [0,2,3,4]
PASSED - SET_WITH_LIMIT - / == 9
PASSED - SET_WITH_LIMIT - 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_LIMIT - Set value within limit (/foo/1 with limit 10)
PASSED - SET_LIMIT - Set value on an object (limit is ignored)
PASSED - SET_LIMIT - Set value with limit_index = -1 (no limit)
PASSED - SET_WITH_LIMIT - 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_WITH_LIMIT - failed with NULL params for input json & path
PASSED - SET_WITH_LIMIT - failed 'cod' with path 'foo/bar'
PASSED - SET_WITH_LIMIT - failed with invalid array index'
PASSED - SET_WITH_LIMIT - failed to set index to non-array
PASSED - SET_LIMIT - Failed to set index 20 with limit 10
PASSED - SET_LIMIT - Succeeded to set index 10 with limit 10
PASSED - SET_WITH_CB - 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_WITH_CB - This callback is called
PASSED - SET_WITH_CB - callback test_cb_print_msg for /foo/1
PASSED - SET_WITH_CB - callback json_object_array_put_with_idx_limit_cb for /foo/4 with limit_index 5
PASSED - SET_WITH_CB - failed with callback json_object_array_put_with_idx_limit_cb for /foo/5 with limit_index 5
PASSED - SET_WITH_CB - failed with callback json_object_array_put_with_idx_limit_cb for /foo/10 with limit_index 5
PASSED - SET_WITH_CB - failed with callback json_object_array_put_with_idx_limit_cb with NULL priv
PASSED - SET_WITH_CB - This callback is called
PASSED - SET_WITH_CB - cb_handles_obj=1: callback was triggered for object operation
PASSED - SET_WITH_CB - cb_handles_obj=0: callback was NOT triggered for object operation, default logic was used
PASSED - SET_WITH_CB - Rejection callback approved an allowed key
PASSED - SET_WITH_CB - Callback correctly identified key 'reject' to reject
PASSED - SET_WITH_CB - Rejection callback rejected a forbidden key
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,87 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "json.h"
#include "printbuf.h"
struct myinfo
{
int value;
};
static int freeit_was_called = 0;
static void freeit(json_object *jso, void *userdata)
{
struct myinfo *info = userdata;
printf("freeit, value=%d\n", info->value);
// Don't actually free anything here, the userdata is stack allocated.
freeit_was_called = 1;
}
static int custom_serializer(struct json_object *o, struct printbuf *pb, int level, int flags)
{
sprintbuf(pb, "Custom Output");
return 0;
}
int main(int argc, char **argv)
{
json_object *my_object, *my_sub_object;
MC_SET_DEBUG(1);
printf("Test setting, then resetting a custom serializer:\n");
my_object = json_object_new_object();
json_object_object_add(my_object, "abc", json_object_new_int(12));
json_object_object_add(my_object, "foo", json_object_new_string("bar"));
printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object));
struct myinfo userdata = {.value = 123};
json_object_set_serializer(my_object, custom_serializer, &userdata, freeit);
printf("my_object.to_string(custom serializer)=%s\n",
json_object_to_json_string(my_object));
printf("Next line of output should be from the custom freeit function:\n");
freeit_was_called = 0;
json_object_set_serializer(my_object, NULL, NULL, NULL);
assert(freeit_was_called);
printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object));
json_object_put(my_object);
// ============================================
my_object = json_object_new_object();
printf("Check that the custom serializer isn't free'd until the last json_object_put:\n");
json_object_set_serializer(my_object, custom_serializer, &userdata, freeit);
json_object_get(my_object);
json_object_put(my_object);
printf("my_object.to_string(custom serializer)=%s\n",
json_object_to_json_string(my_object));
printf("Next line of output should be from the custom freeit function:\n");
freeit_was_called = 0;
json_object_put(my_object);
assert(freeit_was_called);
// ============================================
my_object = json_object_new_object();
my_sub_object = json_object_new_double(1.0);
json_object_object_add(my_object, "double", my_sub_object);
printf("Check that the custom serializer does not include nul byte:\n");
#define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
json_object_set_serializer(my_sub_object, json_object_double_to_json_string, UNCONST("%125.0f"), NULL);
printf("my_object.to_string(custom serializer)=%s\n",
json_object_to_json_string_ext(my_object, JSON_C_TO_STRING_NOZERO));
json_object_put(my_object);
return 0;
}
@@ -0,0 +1,12 @@
Test setting, then resetting a custom serializer:
my_object.to_string(standard)={ "abc": 12, "foo": "bar" }
my_object.to_string(custom serializer)=Custom Output
Next line of output should be from the custom freeit function:
freeit, value=123
my_object.to_string(standard)={ "abc": 12, "foo": "bar" }
Check that the custom serializer isn't free'd until the last json_object_put:
my_object.to_string(custom serializer)=Custom Output
Next line of output should be from the custom freeit function:
freeit, value=123
Check that the custom serializer does not include nul byte:
my_object.to_string(custom serializer)={"double": 1}
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,119 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "json.h"
int main(int argc, char **argv)
{
json_object *tmp = json_object_new_int(123);
assert(json_object_get_int(tmp) == 123);
json_object_set_int(tmp, 321);
assert(json_object_get_int(tmp) == 321);
printf("INT PASSED\n");
json_object_set_int64(tmp, (int64_t)321321321);
assert(json_object_get_int64(tmp) == 321321321);
json_object_put(tmp);
printf("INT64 PASSED\n");
tmp = json_object_new_uint64(123);
assert(json_object_get_boolean(tmp) == 1);
assert(json_object_get_int(tmp) == 123);
assert(json_object_get_int64(tmp) == 123);
assert(json_object_get_uint64(tmp) == 123);
assert(json_object_get_double(tmp) == 123.000000);
json_object_set_uint64(tmp, (uint64_t)321321321);
assert(json_object_get_uint64(tmp) == 321321321);
json_object_set_uint64(tmp, 9223372036854775808U);
assert(json_object_get_int(tmp) == INT32_MAX);
assert(json_object_get_uint64(tmp) == 9223372036854775808U);
json_object_put(tmp);
printf("UINT64 PASSED\n");
tmp = json_object_new_boolean(1);
assert(json_object_get_boolean(tmp) == 1);
json_object_set_boolean(tmp, 0);
assert(json_object_get_boolean(tmp) == 0);
json_object_set_boolean(tmp, 1);
assert(json_object_get_boolean(tmp) == 1);
json_object_put(tmp);
printf("BOOL PASSED\n");
tmp = json_object_new_double(12.34);
assert(json_object_get_double(tmp) == 12.34);
json_object_set_double(tmp, 34.56);
assert(json_object_get_double(tmp) == 34.56);
json_object_set_double(tmp, 6435.34);
assert(json_object_get_double(tmp) == 6435.34);
json_object_set_double(tmp, 2e21);
assert(json_object_get_int(tmp) == INT32_MAX);
assert(json_object_get_int64(tmp) == INT64_MAX);
assert(json_object_get_uint64(tmp) == UINT64_MAX);
json_object_set_double(tmp, -2e21);
assert(json_object_get_int(tmp) == INT32_MIN);
assert(json_object_get_int64(tmp) == INT64_MIN);
assert(json_object_get_uint64(tmp) == 0);
json_object_put(tmp);
printf("DOUBLE PASSED\n");
#define SHORT "SHORT"
#define MID "A MID STRING"
// 12345678901234567890123456789012....
#define HUGE "A string longer than 32 chars as to check non local buf codepath"
tmp = json_object_new_string(MID);
assert(strcmp(json_object_get_string(tmp), MID) == 0);
assert(strcmp(json_object_to_json_string(tmp), "\"" MID "\"") == 0);
json_object_set_string(tmp, SHORT);
assert(strcmp(json_object_get_string(tmp), SHORT) == 0);
assert(strcmp(json_object_to_json_string(tmp), "\"" SHORT "\"") == 0);
json_object_set_string(tmp, HUGE);
assert(strcmp(json_object_get_string(tmp), HUGE) == 0);
assert(strcmp(json_object_to_json_string(tmp), "\"" HUGE "\"") == 0);
json_object_set_string(tmp, SHORT);
assert(strcmp(json_object_get_string(tmp), SHORT) == 0);
assert(strcmp(json_object_to_json_string(tmp), "\"" SHORT "\"") == 0);
// Set an empty string a couple times to try to trigger
// a case that used to leak memory.
json_object_set_string(tmp, "");
json_object_set_string(tmp, HUGE);
json_object_set_string(tmp, "");
json_object_set_string(tmp, HUGE);
json_object_put(tmp);
printf("STRING PASSED\n");
#define STR "STR"
#define DOUBLE "123.123"
#define DOUBLE_E "12E+3"
#define DOUBLE_STR "123.123STR"
#define DOUBLE_OVER "1.8E+308"
#define DOUBLE_OVER_NEGATIVE "-1.8E+308"
tmp = json_object_new_string(STR);
assert(json_object_get_double(tmp) == 0.0);
json_object_set_string(tmp, DOUBLE);
assert(json_object_get_double(tmp) == 123.123000);
json_object_set_string(tmp, DOUBLE_E);
assert(json_object_get_double(tmp) == 12000.000000);
json_object_set_string(tmp, DOUBLE_STR);
assert(json_object_get_double(tmp) == 0.0);
json_object_set_string(tmp, DOUBLE_OVER);
assert(json_object_get_double(tmp) == 0.0);
json_object_set_string(tmp, DOUBLE_OVER_NEGATIVE);
assert(json_object_get_double(tmp) == 0.0);
json_object_put(tmp);
printf("STRINGTODOUBLE PASSED\n");
tmp = json_tokener_parse("1.234");
json_object_set_double(tmp, 12.3);
const char *serialized = json_object_to_json_string(tmp);
if (getenv("JSONC_TEST_TRACE") != NULL)
// This output might be different on different systems
fprintf(stderr, "%s\n", serialized);
assert(strncmp(serialized, "12.3", 4) == 0);
json_object_put(tmp);
printf("PARSE AND SET PASSED\n");
printf("PASSED\n");
return 0;
}
@@ -0,0 +1,9 @@
INT PASSED
INT64 PASSED
UINT64 PASSED
BOOL PASSED
DOUBLE PASSED
STRING PASSED
STRINGTODOUBLE PASSED
PARSE AND SET PASSED
PASSED
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,13 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "strerror_override.h"
#include <stdio.h>
int main(int argc, char **argv)
{
puts(strerror(10000));
puts(strerror(999));
return 0;
}
@@ -0,0 +1,2 @@
ERRNO=10000
ERRNO=999
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,329 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "strerror_override.h"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <io.h>
#include <windows.h>
#endif /* defined(_WIN32) */
#include <fcntl.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "json.h"
#include "json_util.h"
#include "snprintf_compat.h"
static void test_read_valid_with_fd(const char *testdir);
static void test_read_valid_nested_with_fd(const char *testdir);
static void test_read_nonexistant(void);
#ifndef _WIN32
static void test_read_closed(void);
#endif
static void test_write_to_file(void);
static void stat_and_cat(const char *file);
static void test_read_fd_equal(const char *testdir);
#ifndef PATH_MAX
#define PATH_MAX 256
#endif
static void test_write_to_file(void)
{
json_object *jso;
jso = json_tokener_parse("{"
"\"foo\":1234,"
"\"foo1\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo2\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo3\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo4\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo5\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo6\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo7\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo8\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo9\":\"abcdefghijklmnopqrstuvwxyz\""
"}");
const char *outfile = "json.out";
int rv = json_object_to_file(outfile, jso);
printf("%s: json_object_to_file(%s, jso)=%d\n", (rv == 0) ? "OK" : "FAIL", outfile, rv);
if (rv == 0)
stat_and_cat(outfile);
putchar('\n');
const char *outfile2 = "json2.out";
rv = json_object_to_file_ext(outfile2, jso, JSON_C_TO_STRING_PRETTY);
printf("%s: json_object_to_file_ext(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n",
(rv == 0) ? "OK" : "FAIL", outfile2, rv);
if (rv == 0)
stat_and_cat(outfile2);
const char *outfile3 = "json3.out";
int d = open(outfile3, O_WRONLY | O_CREAT, 0600);
if (d < 0)
{
printf("FAIL: unable to open %s %s\n", outfile3, strerror(errno));
return;
}
rv = json_object_to_fd(d, jso, JSON_C_TO_STRING_PRETTY);
printf("%s: json_object_to_fd(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n",
(rv == 0) ? "OK" : "FAIL", outfile3, rv);
// Write the same object twice
rv = json_object_to_fd(d, jso, JSON_C_TO_STRING_PLAIN);
printf("%s: json_object_to_fd(%s, jso, JSON_C_TO_STRING_PLAIN)=%d\n",
(rv == 0) ? "OK" : "FAIL", outfile3, rv);
close(d);
if (rv == 0)
stat_and_cat(outfile3);
json_object_put(jso);
}
static void stat_and_cat(const char *file)
{
struct stat sb;
int flags = O_RDONLY;
#ifdef O_BINARY
// This fixes Windows which otherwise opens this in text mode and returns different counts
flags |= O_BINARY;
#endif
int d = open(file, flags);
if (d < 0)
{
printf("FAIL: unable to open %s: %s\n", file, strerror(errno));
return;
}
if (fstat(d, &sb) < 0)
{
printf("FAIL: unable to stat %s: %s\n", file, strerror(errno));
close(d);
return;
}
char *buf = malloc(sb.st_size + 1);
if (!buf)
{
printf("FAIL: unable to allocate memory\n");
close(d);
return;
}
if (read(d, buf, sb.st_size) < sb.st_size)
{
printf("FAIL: unable to read all of %s: %s\n", file, strerror(errno));
free(buf);
close(d);
return;
}
buf[sb.st_size] = '\0';
printf("file[%s], size=%d, contents=%s\n", file, (int)sb.st_size, buf);
free(buf);
close(d);
}
int main(int argc, char **argv)
{
// json_object_to_file(file, obj);
// json_object_to_file_ext(file, obj, flags);
const char *testdir;
if (argc < 2)
{
fprintf(stderr,
"Usage: %s <testdir>\n"
" <testdir> is the location of input files\n",
argv[0]);
return EXIT_FAILURE;
}
testdir = argv[1];
// Test json_c_version.c
if (strncmp(json_c_version(), JSON_C_VERSION, sizeof(JSON_C_VERSION)))
{
printf("FAIL: Output from json_c_version(): %s "
"does not match %s",
json_c_version(), JSON_C_VERSION);
return EXIT_FAILURE;
}
if (json_c_version_num() != JSON_C_VERSION_NUM)
{
printf("FAIL: Output from json_c_version_num(): %d "
"does not match %d",
json_c_version_num(), JSON_C_VERSION_NUM);
return EXIT_FAILURE;
}
test_read_valid_with_fd(testdir);
test_read_valid_nested_with_fd(testdir);
test_read_nonexistant();
#ifndef _WIN32
// Disabled because the Windows CRT causes a crash during this test that cannot be disabled/stopped/worked around
test_read_closed();
#endif
test_write_to_file();
test_read_fd_equal(testdir);
return EXIT_SUCCESS;
}
static void test_read_valid_with_fd(const char *testdir)
{
char filename[PATH_MAX];
(void)snprintf(filename, sizeof(filename), "%s/valid.json", testdir);
int flags = O_RDONLY;
#ifdef O_BINARY
// This fixes Windows which otherwise opens this in text mode and returns different counts
flags |= O_BINARY;
#endif
int d = open(filename, flags);
if (d < 0)
{
fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
json_object *jso = json_object_from_fd(d);
if (jso != NULL)
{
printf("OK: json_object_from_fd(valid.json)=%s\n", json_object_to_json_string(jso));
json_object_put(jso);
}
else
{
fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename,
json_util_get_last_err());
}
close(d);
}
static void test_read_valid_nested_with_fd(const char *testdir)
{
char filename[PATH_MAX];
(void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
int flags = O_RDONLY;
#ifdef O_BINARY
// This fixes Windows which otherwise opens this in text mode and returns different counts
flags |= O_BINARY;
#endif
int d = open(filename, flags);
if (d < 0)
{
fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
assert(NULL == json_object_from_fd_ex(d, -2));
json_object *jso = json_object_from_fd_ex(d, 20);
if (jso != NULL)
{
printf("OK: json_object_from_fd_ex(valid_nested.json, 20)=%s\n",
json_object_to_json_string(jso));
json_object_put(jso);
}
else
{
fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename,
json_util_get_last_err());
}
(void)lseek(d, SEEK_SET, 0);
jso = json_object_from_fd_ex(d, 3);
if (jso != NULL)
{
printf("FAIL: json_object_from_fd_ex(%s, 3)=%s\n", filename,
json_object_to_json_string(jso));
json_object_put(jso);
}
else
{
printf("OK: correctly unable to parse contents of valid_nested.json with low max "
"depth: %s\n",
json_util_get_last_err());
}
close(d);
}
static void test_read_nonexistant(void)
{
const char *filename = "./not_present.json";
json_object *jso = json_object_from_file(filename);
if (jso != NULL)
{
printf("FAIL: json_object_from_file(%s) returned %p when NULL expected\n", filename,
(void *)jso);
json_object_put(jso);
}
else
{
printf("OK: json_object_from_file(%s) correctly returned NULL: %s\n", filename,
json_util_get_last_err());
}
}
#ifndef _WIN32
static void test_read_closed(void)
{
// Test reading from a closed fd
int d = open("/dev/null", O_RDONLY);
if (d < 0)
{
puts("FAIL: unable to open");
return;
}
// Copy over to a fixed fd number so test output is consistent.
int fixed_d = 10;
if (dup2(d, fixed_d) < 0)
{
printf("FAIL: unable to dup to fd %d", fixed_d);
}
close(d);
close(fixed_d);
json_object *jso = json_object_from_fd(fixed_d);
if (jso != NULL)
{
printf("FAIL: read from closed fd returning non-NULL: %p\n", (void *)jso);
fflush(stdout);
printf(" jso=%s\n", json_object_to_json_string(jso));
json_object_put(jso);
return;
}
printf("OK: json_object_from_fd(closed_fd), "
"expecting NULL, EBADF, got:NULL, %s\n",
json_util_get_last_err());
}
#endif
static void test_read_fd_equal(const char *testdir)
{
char filename[PATH_MAX];
(void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
json_object *jso = json_object_from_file(filename);
int flags = O_RDONLY;
#ifdef O_BINARY
// This fixes Windows which otherwise opens this in text mode and returns different counts
flags |= O_BINARY;
#endif
int d = open(filename, flags);
if (d < 0)
{
fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
json_object *new_jso = json_object_from_fd(d);
close(d);
printf("OK: json_object_from_file(valid.json)=%s\n", json_object_to_json_string(jso));
printf("OK: json_object_from_fd(valid.json)=%s\n", json_object_to_json_string(new_jso));
json_object_put(jso);
json_object_put(new_jso);
}
@@ -0,0 +1,40 @@
OK: json_object_from_fd(valid.json)={ "foo": 123 }
OK: json_object_from_fd_ex(valid_nested.json, 20)={ "foo": 123, "obj2": { "obj3": { "obj4": { "foo": 999 } } } }
OK: correctly unable to parse contents of valid_nested.json with low max depth: json_tokener_parse_ex failed: nesting too deep
OK: json_object_from_file(./not_present.json) correctly returned NULL: json_object_from_file: error opening file ./not_present.json: ERRNO=ENOENT
OK: json_object_from_fd(closed_fd), expecting NULL, EBADF, got:NULL, json_object_from_fd_ex: error reading fd 10: ERRNO=EBADF
OK: json_object_to_file(json.out, jso)=0
file[json.out], size=336, contents={"foo":1234,"foo1":"abcdefghijklmnopqrstuvwxyz","foo2":"abcdefghijklmnopqrstuvwxyz","foo3":"abcdefghijklmnopqrstuvwxyz","foo4":"abcdefghijklmnopqrstuvwxyz","foo5":"abcdefghijklmnopqrstuvwxyz","foo6":"abcdefghijklmnopqrstuvwxyz","foo7":"abcdefghijklmnopqrstuvwxyz","foo8":"abcdefghijklmnopqrstuvwxyz","foo9":"abcdefghijklmnopqrstuvwxyz"}
OK: json_object_to_file_ext(json2.out, jso, JSON_C_TO_STRING_PRETTY)=0
file[json2.out], size=367, contents={
"foo":1234,
"foo1":"abcdefghijklmnopqrstuvwxyz",
"foo2":"abcdefghijklmnopqrstuvwxyz",
"foo3":"abcdefghijklmnopqrstuvwxyz",
"foo4":"abcdefghijklmnopqrstuvwxyz",
"foo5":"abcdefghijklmnopqrstuvwxyz",
"foo6":"abcdefghijklmnopqrstuvwxyz",
"foo7":"abcdefghijklmnopqrstuvwxyz",
"foo8":"abcdefghijklmnopqrstuvwxyz",
"foo9":"abcdefghijklmnopqrstuvwxyz"
}
OK: json_object_to_fd(json3.out, jso, JSON_C_TO_STRING_PRETTY)=0
OK: json_object_to_fd(json3.out, jso, JSON_C_TO_STRING_PLAIN)=0
file[json3.out], size=703, contents={
"foo":1234,
"foo1":"abcdefghijklmnopqrstuvwxyz",
"foo2":"abcdefghijklmnopqrstuvwxyz",
"foo3":"abcdefghijklmnopqrstuvwxyz",
"foo4":"abcdefghijklmnopqrstuvwxyz",
"foo5":"abcdefghijklmnopqrstuvwxyz",
"foo6":"abcdefghijklmnopqrstuvwxyz",
"foo7":"abcdefghijklmnopqrstuvwxyz",
"foo8":"abcdefghijklmnopqrstuvwxyz",
"foo9":"abcdefghijklmnopqrstuvwxyz"
}{"foo":1234,"foo1":"abcdefghijklmnopqrstuvwxyz","foo2":"abcdefghijklmnopqrstuvwxyz","foo3":"abcdefghijklmnopqrstuvwxyz","foo4":"abcdefghijklmnopqrstuvwxyz","foo5":"abcdefghijklmnopqrstuvwxyz","foo6":"abcdefghijklmnopqrstuvwxyz","foo7":"abcdefghijklmnopqrstuvwxyz","foo8":"abcdefghijklmnopqrstuvwxyz","foo9":"abcdefghijklmnopqrstuvwxyz"}
OK: json_object_from_file(valid.json)={ "foo": 123, "obj2": { "obj3": { "obj4": { "foo": 999 } } } }
OK: json_object_from_fd(valid.json)={ "foo": 123, "obj2": { "obj3": { "obj4": { "foo": 999 } } } }
@@ -0,0 +1 @@
test_basic.test
@@ -0,0 +1,154 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "json.h"
#include "json_tokener.h"
#include "json_visit.h"
static json_c_visit_userfunc emit_object;
static json_c_visit_userfunc skip_arrays;
static json_c_visit_userfunc pop_and_stop;
static json_c_visit_userfunc err_on_subobj2;
static json_c_visit_userfunc pop_array;
static json_c_visit_userfunc stop_array;
static json_c_visit_userfunc err_return;
int main(void)
{
MC_SET_DEBUG(1);
const char *input = "{\
\"obj1\": 123,\
\"obj2\": {\
\"subobj1\": \"aaa\",\
\"subobj2\": \"bbb\",\
\"subobj3\": [ \"elem1\", \"elem2\", true ],\
},\
\"obj3\": 1.234,\
\"obj4\": [ true, false, null ]\
}";
json_object *jso = json_tokener_parse(input);
printf("jso.to_string()=%s\n", json_object_to_json_string(jso));
int rv;
rv = json_c_visit(jso, 0, emit_object, NULL);
printf("json_c_visit(emit_object)=%d\n", rv);
printf("================================\n\n");
rv = json_c_visit(jso, 0, skip_arrays, NULL);
printf("json_c_visit(skip_arrays)=%d\n", rv);
printf("================================\n\n");
rv = json_c_visit(jso, 0, pop_and_stop, NULL);
printf("json_c_visit(pop_and_stop)=%d\n", rv);
printf("================================\n\n");
rv = json_c_visit(jso, 0, err_on_subobj2, NULL);
printf("json_c_visit(err_on_subobj2)=%d\n", rv);
printf("================================\n\n");
rv = json_c_visit(jso, 0, pop_array, NULL);
printf("json_c_visit(pop_array)=%d\n", rv);
printf("================================\n\n");
rv = json_c_visit(jso, 0, stop_array, NULL);
printf("json_c_visit(stop_array)=%d\n", rv);
printf("================================\n\n");
rv = json_c_visit(jso, 0, err_return, NULL);
printf("json_c_visit(err_return)=%d\n", rv);
printf("================================\n\n");
json_object_put(jso);
return 0;
}
static int emit_object(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
{
printf("flags: 0x%x, key: %s, index: %ld, value: %s\n", flags,
(jso_key ? jso_key : "(null)"), (jso_index ? (long)*jso_index : -1L),
json_object_to_json_string(jso));
fflush(stdout);
return JSON_C_VISIT_RETURN_CONTINUE;
}
static int skip_arrays(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
{
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
if (json_object_get_type(jso) == json_type_array)
return JSON_C_VISIT_RETURN_SKIP;
return JSON_C_VISIT_RETURN_CONTINUE;
}
static int pop_and_stop(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
{
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
if (jso_key != NULL && strcmp(jso_key, "subobj1") == 0)
{
printf("POP after handling subobj1\n");
return JSON_C_VISIT_RETURN_POP;
}
if (jso_key != NULL && strcmp(jso_key, "obj3") == 0)
{
printf("STOP after handling obj3\n");
return JSON_C_VISIT_RETURN_STOP;
}
return JSON_C_VISIT_RETURN_CONTINUE;
}
static int err_on_subobj2(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
{
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
if (jso_key != NULL && strcmp(jso_key, "subobj2") == 0)
{
printf("ERROR after handling subobj1\n");
return JSON_C_VISIT_RETURN_ERROR;
}
return JSON_C_VISIT_RETURN_CONTINUE;
}
static int pop_array(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
{
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
if (jso_index != NULL && (*jso_index == 0))
{
printf("POP after handling array[0]\n");
return JSON_C_VISIT_RETURN_POP;
}
return JSON_C_VISIT_RETURN_CONTINUE;
}
static int stop_array(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
{
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
if (jso_index != NULL && (*jso_index == 0))
{
printf("STOP after handling array[1]\n");
return JSON_C_VISIT_RETURN_STOP;
}
return JSON_C_VISIT_RETURN_CONTINUE;
}
static int err_return(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
{
printf("flags: 0x%x, key: %s, index: %ld, value: %s\n", flags,
(jso_key ? jso_key : "(null)"), (jso_index ? (long)*jso_index : -1L),
json_object_to_json_string(jso));
fflush(stdout);
return 100;
}
@@ -0,0 +1,90 @@
jso.to_string()={ "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] }
flags: 0x0, key: (null), index: -1, value: { "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] }
flags: 0x0, key: obj1, index: -1, value: 123
flags: 0x0, key: obj2, index: -1, value: { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }
flags: 0x0, key: subobj1, index: -1, value: "aaa"
flags: 0x0, key: subobj2, index: -1, value: "bbb"
flags: 0x0, key: subobj3, index: -1, value: [ "elem1", "elem2", true ]
flags: 0x0, key: (null), index: 0, value: "elem1"
flags: 0x0, key: (null), index: 1, value: "elem2"
flags: 0x0, key: (null), index: 2, value: true
flags: 0x2, key: subobj3, index: -1, value: [ "elem1", "elem2", true ]
flags: 0x2, key: obj2, index: -1, value: { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }
flags: 0x0, key: obj3, index: -1, value: 1.234
flags: 0x0, key: obj4, index: -1, value: [ true, false, null ]
flags: 0x0, key: (null), index: 0, value: true
flags: 0x0, key: (null), index: 1, value: false
flags: 0x0, key: (null), index: 2, value: null
flags: 0x2, key: obj4, index: -1, value: [ true, false, null ]
flags: 0x2, key: (null), index: -1, value: { "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] }
json_c_visit(emit_object)=0
================================
flags: 0x0, key: (null), index: -1, value: { "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] }
flags: 0x0, key: obj1, index: -1, value: 123
flags: 0x0, key: obj2, index: -1, value: { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }
flags: 0x0, key: subobj1, index: -1, value: "aaa"
flags: 0x0, key: subobj2, index: -1, value: "bbb"
flags: 0x0, key: subobj3, index: -1, value: [ "elem1", "elem2", true ]
flags: 0x2, key: obj2, index: -1, value: { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }
flags: 0x0, key: obj3, index: -1, value: 1.234
flags: 0x0, key: obj4, index: -1, value: [ true, false, null ]
flags: 0x2, key: (null), index: -1, value: { "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] }
json_c_visit(skip_arrays)=0
================================
flags: 0x0, key: (null), index: -1, value: { "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] }
flags: 0x0, key: obj1, index: -1, value: 123
flags: 0x0, key: obj2, index: -1, value: { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }
flags: 0x0, key: subobj1, index: -1, value: "aaa"
POP after handling subobj1
flags: 0x2, key: obj2, index: -1, value: { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }
flags: 0x0, key: obj3, index: -1, value: 1.234
STOP after handling obj3
json_c_visit(pop_and_stop)=0
================================
flags: 0x0, key: (null), index: -1, value: { "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] }
flags: 0x0, key: obj1, index: -1, value: 123
flags: 0x0, key: obj2, index: -1, value: { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }
flags: 0x0, key: subobj1, index: -1, value: "aaa"
flags: 0x0, key: subobj2, index: -1, value: "bbb"
ERROR after handling subobj1
json_c_visit(err_on_subobj2)=-1
================================
flags: 0x0, key: (null), index: -1, value: { "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] }
flags: 0x0, key: obj1, index: -1, value: 123
flags: 0x0, key: obj2, index: -1, value: { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }
flags: 0x0, key: subobj1, index: -1, value: "aaa"
flags: 0x0, key: subobj2, index: -1, value: "bbb"
flags: 0x0, key: subobj3, index: -1, value: [ "elem1", "elem2", true ]
flags: 0x0, key: (null), index: 0, value: "elem1"
POP after handling array[0]
flags: 0x2, key: subobj3, index: -1, value: [ "elem1", "elem2", true ]
flags: 0x2, key: obj2, index: -1, value: { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }
flags: 0x0, key: obj3, index: -1, value: 1.234
flags: 0x0, key: obj4, index: -1, value: [ true, false, null ]
flags: 0x0, key: (null), index: 0, value: true
POP after handling array[0]
flags: 0x2, key: obj4, index: -1, value: [ true, false, null ]
flags: 0x2, key: (null), index: -1, value: { "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] }
json_c_visit(pop_array)=0
================================
flags: 0x0, key: (null), index: -1, value: { "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] }
flags: 0x0, key: obj1, index: -1, value: 123
flags: 0x0, key: obj2, index: -1, value: { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }
flags: 0x0, key: subobj1, index: -1, value: "aaa"
flags: 0x0, key: subobj2, index: -1, value: "bbb"
flags: 0x0, key: subobj3, index: -1, value: [ "elem1", "elem2", true ]
flags: 0x0, key: (null), index: 0, value: "elem1"
STOP after handling array[1]
json_c_visit(stop_array)=0
================================
flags: 0x0, key: (null), index: -1, value: { "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] }
ERROR: invalid return value from json_c_visit userfunc: 100
json_c_visit(err_return)=-1
================================

Some files were not shown because too many files have changed in this diff Show More