diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7d84442..e339602 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -28,6 +28,8 @@ set(ALL_TEST_NAMES test_null test_null_keys_add test_null_keys_get + test_null_keys_del + test_null_keys_print test_parse test_parse_int64 test_printbuf diff --git a/tests/test_null_keys_del.c b/tests/test_null_keys_del.c new file mode 100644 index 0000000..a2f9c34 --- /dev/null +++ b/tests/test_null_keys_del.c @@ -0,0 +1,100 @@ +/* + * Tests if binary strings are supported. + */ + +#include "config.h" +#include +#include + +#include "json_inttypes.h" +#include "json_object.h" +#include "json_tokener.h" + +int main(void) +{ + /* this test has embedded null characters in the key and value. + * check that it's still included after deserializing. */ + const char *input = "{ \"biff\\u0000\": null, \"\\u0000zxcvbnm\": 178 }"; + const char *expected_biff = "biff\0"; + const int expected_biff_len = 5; + const char *expected_biff_printable = "biff\\u0000"; + const char *expected_zxcv = "\0zxcvbnm"; + const int expected_zxcv_len = 8; + const char *expected_zxcv_printable = "\\u0000zxcvbnm"; + + struct json_object *parsed = json_tokener_parse(input); + struct json_object *val; + json_bool key_present; + + printf("Parsed input: %s\n", input); + printf("Result is "); + if (parsed == NULL) + { + printf("NULL (error!)\n"); + return 1; // Error somewhere + } + else if (!json_object_is_type(parsed, json_type_object)) + { + printf("not `json_type_object` (error!)\n"); + return 1; // Error somewhere + } + else + { + printf("`json_type_object`\n"); + } + + // Check nothing odd happened in parsing + if (json_object_object_length(parsed) != 2) + { + printf("Should contain two fields (has %d) (error!)", + json_object_object_length(parsed)); + return 1; + } + + key_present = json_object_object_get_ex_len(parsed, expected_biff, expected_biff_len, &val); + if (!key_present || !json_object_is_type(val, json_type_null)) + { + printf("The key \"%s\" should be present and should be NULL (error!)\n", + expected_biff_printable); + return 1; + } + key_present = json_object_object_get_ex_len(parsed, expected_zxcv, expected_zxcv_len, &val); + if (!key_present || !json_object_is_type(val, json_type_int) || + json_object_get_int(val) != 178) + { + printf("The key \"%s\" should be present and should be 178 (error!)\n", + expected_zxcv_printable); + return 1; + } + printf("Expected keys (\"%s\" and \"%s\") present with expected values\n", + expected_biff_printable, expected_zxcv_printable); + + // Delete one key + json_object_object_del_len(parsed, expected_zxcv, expected_zxcv_len); + + // Check it is gone + if (json_object_object_length(parsed) != 1) + { + printf("Should contain only one field (has %d) (error!)", + json_object_object_length(parsed)); + return 1; + } + key_present = json_object_object_get_ex_len(parsed, expected_biff, expected_biff_len, &val); + if (!key_present || !json_object_is_type(val, json_type_null)) + { + printf("The key \"%s\" should be present and should be NULL (error!)\n", + expected_biff_printable); + return 1; + } + key_present = json_object_object_get_ex_len(parsed, expected_zxcv, expected_zxcv_len, &val); + if (key_present) + { + printf("The key \"%s\" should not be present (error!)\n", expected_zxcv_printable); + return 1; + } + printf("Key deleted properly\n"); + + json_object_put(parsed); + printf("PASS\n"); + return 0; +} diff --git a/tests/test_null_keys_del.expected b/tests/test_null_keys_del.expected new file mode 100644 index 0000000..44f1682 --- /dev/null +++ b/tests/test_null_keys_del.expected @@ -0,0 +1,5 @@ +Parsed input: { "biff\u0000": null, "\u0000zxcvbnm": 178 } +Result is `json_type_object` +Expected keys ("biff\0" and "\0zxcvbnm") present with expected values +Key deleted properly +PASS diff --git a/tests/test_null_keys_del.test b/tests/test_null_keys_del.test new file mode 120000 index 0000000..58a13f4 --- /dev/null +++ b/tests/test_null_keys_del.test @@ -0,0 +1 @@ +test_basic.test \ No newline at end of file diff --git a/tests/test_null_keys_get.c b/tests/test_null_keys_get.c index d63f579..b0c7d26 100644 --- a/tests/test_null_keys_get.c +++ b/tests/test_null_keys_get.c @@ -19,7 +19,7 @@ int main(void) const int expected_key_len = 7; const char *expected_key_printable = "foo\\0bar"; const char *expected_value = "qwerty\0asdf"; - const int expected_value_len = 12; + const int expected_value_len = 11; const char *expected_value_printable = "qwerty\\0asdf"; struct json_object *parsed = json_tokener_parse(input); diff --git a/tests/test_null_keys_print.c b/tests/test_null_keys_print.c new file mode 100644 index 0000000..03b74c8 --- /dev/null +++ b/tests/test_null_keys_print.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include + +#include "json.h" +#include "parse_flags.h" + +int main(int argc, char **argv) +{ + struct json_object *new_obj; + + new_obj = json_tokener_parse("{ \"foo\\u0000bar\": \"qwerty\\u0000asdf\" }"); + printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); + json_object_put(new_obj); + + return EXIT_SUCCESS; +} diff --git a/tests/test_null_keys_print.expected b/tests/test_null_keys_print.expected new file mode 100644 index 0000000..0af8163 --- /dev/null +++ b/tests/test_null_keys_print.expected @@ -0,0 +1 @@ +new_obj.to_string()={ "foo\u0000bar": "qwerty\u0000asdf" } diff --git a/tests/test_null_keys_print.test b/tests/test_null_keys_print.test new file mode 120000 index 0000000..58a13f4 --- /dev/null +++ b/tests/test_null_keys_print.test @@ -0,0 +1 @@ +test_basic.test \ No newline at end of file