diff options
author | Petri Lehtinen <petri@digip.org> | 2013-09-30 10:44:35 +0300 |
---|---|---|
committer | Petri Lehtinen <petri@digip.org> | 2013-09-30 10:45:02 +0300 |
commit | 1bfc33362e16e1307eb38b1d045c4c1f86905057 (patch) | |
tree | d5484a58e39cedbf626807bb7e698f0b38ef6b4c /test/suites/api | |
parent | 5744468c990dff6dc2c8647014814515597b107c (diff) | |
download | jansson-1bfc33362e16e1307eb38b1d045c4c1f86905057.zip jansson-1bfc33362e16e1307eb38b1d045c4c1f86905057.tar.gz jansson-1bfc33362e16e1307eb38b1d045c4c1f86905057.tar.bz2 |
Add JSON_ALLOW_NUL decoding flag for enabling NUL byte support
Diffstat (limited to 'test/suites/api')
-rw-r--r-- | test/suites/api/test_dump.c | 15 | ||||
-rw-r--r-- | test/suites/api/test_load.c | 21 |
2 files changed, 36 insertions, 0 deletions
diff --git a/test/suites/api/test_dump.c b/test/suites/api/test_dump.c index 64c0863..89e73a7 100644 --- a/test/suites/api/test_dump.c +++ b/test/suites/api/test_dump.c @@ -180,6 +180,20 @@ static void escape_slashes() json_decref(json); } +static void encode_nul_byte() +{ + json_t *json; + char *result; + + json = json_stringn("nul byte \0 in string", 20); + result = json_dumps(json, JSON_ENCODE_ANY); + if(!result || memcmp(result, "\"nul byte \\u0000 in string\"", 27)) + fail("json_dumps failed to dump an embedded NUL byte"); + + free(result); + json_decref(json); +} + static void run_tests() { encode_null(); @@ -187,4 +201,5 @@ static void run_tests() circular_references(); encode_other_than_array_or_object(); escape_slashes(); + encode_nul_byte(); } diff --git a/test/suites/api/test_load.c b/test/suites/api/test_load.c index 944b4d8..eb52323 100644 --- a/test/suites/api/test_load.c +++ b/test/suites/api/test_load.c @@ -115,6 +115,26 @@ static void decode_int_as_real() #endif } +static void allow_nul() +{ + const char *text = "\"nul byte \\u0000 in string\""; + const char *expected = "nul byte \0 in string"; + size_t len = 20; + json_t *json; + + json = json_loads(text, JSON_ALLOW_NUL | JSON_DECODE_ANY, NULL); + if(!json || !json_is_string(json)) + fail("unable to decode embedded NUL byte"); + + if(json_string_length(json) != len) + fail("decoder returned wrong string length"); + + if(memcmp(json_string_value(json), expected, len + 1)) + fail("decoder returned wrong string content"); + + json_decref(json); +} + static void load_wrong_args() { json_t *json; @@ -161,6 +181,7 @@ static void run_tests() disable_eof_check(); decode_any(); decode_int_as_real(); + allow_nul(); load_wrong_args(); position(); } |