aboutsummaryrefslogtreecommitdiff
path: root/test/suites
diff options
context:
space:
mode:
authorPhilipp Stephani <phst@google.com>2017-10-03 11:42:07 +0200
committerPhilipp Stephani <phst@google.com>2017-10-03 11:42:07 +0200
commit112ccbd820449b85d7a7541d6b39cb31bdbb1bbb (patch)
tree5a2baa21a46d6d8ff2ae297cb7c3c60ba7327836 /test/suites
parent271ffda903d2b64e3971f41a3e03af1ad0ddc217 (diff)
downloadjansson-112ccbd820449b85d7a7541d6b39cb31bdbb1bbb.zip
jansson-112ccbd820449b85d7a7541d6b39cb31bdbb1bbb.tar.gz
jansson-112ccbd820449b85d7a7541d6b39cb31bdbb1bbb.tar.bz2
Use last byte of error text as numeric error code
Fixes #352
Diffstat (limited to 'test/suites')
-rw-r--r--test/suites/api/test_load.c24
-rw-r--r--test/suites/api/test_pack.c32
-rw-r--r--test/suites/api/test_unpack.c64
-rw-r--r--test/suites/api/util.h13
4 files changed, 79 insertions, 54 deletions
diff --git a/test/suites/api/test_load.c b/test/suites/api/test_load.c
index 3416af1..a708de5 100644
--- a/test/suites/api/test_load.c
+++ b/test/suites/api/test_load.c
@@ -32,6 +32,8 @@ static void file_not_found()
if(strcmp(error.text, "unable to open /path/to/nonexistent/file.json") != 0)
fail("json_load_file returned an invalid error message");
+ if(json_error_code(&error) != json_error_cannot_open_file)
+ fail("json_load_file returned an invalid error code");
}
static void very_long_file_name() {
@@ -46,6 +48,8 @@ static void very_long_file_name() {
if (strncmp(error.source, "...aaa", 6) != 0)
fail("error source was set incorrectly");
+ if(json_error_code(&error) != json_error_cannot_open_file)
+ fail("error code was set incorrectly");
}
static void reject_duplicates()
@@ -54,7 +58,7 @@ static void reject_duplicates()
if(json_loads("{\"foo\": 1, \"foo\": 2}", JSON_REJECT_DUPLICATES, &error))
fail("json_loads did not detect a duplicate key");
- check_error("duplicate object key near '\"foo\"'", "<string>", 1, 16, 16);
+ check_error(json_error_duplicate_key, "duplicate object key near '\"foo\"'", "<string>", 1, 16, 16);
}
static void disable_eof_check()
@@ -66,7 +70,7 @@ static void disable_eof_check()
if(json_loads(text, 0, &error))
fail("json_loads did not detect garbage after JSON text");
- check_error("end of file expected near 'garbage'", "<string>", 1, 18, 18);
+ check_error(json_error_end_of_input_expected, "end of file expected near 'garbage'", "<string>", 1, 18, 18);
json = json_loads(text, JSON_DISABLE_EOF_CHECK, &error);
if(!json)
@@ -137,7 +141,8 @@ static void decode_int_as_real()
big[310] = '\0';
json = json_loads(big, JSON_DECODE_INT_AS_REAL | JSON_DECODE_ANY, &error);
- if (json || strcmp(error.text, "real number overflow") != 0)
+ if (json || strcmp(error.text, "real number overflow") != 0 ||
+ json_error_code(&error) != json_error_numeric_overflow)
fail("json_load decode int as real failed - expected overflow");
json_decref(json);
@@ -206,6 +211,18 @@ static void position()
json_decref(json);
}
+static void error_code()
+{
+ json_error_t error;
+ json_t *json = json_loads("[123] garbage", 0, &error);
+ if(json != NULL)
+ fail("json_loads returned not NULL");
+ if(strlen(error.text) >= JSON_ERROR_TEXT_LENGTH)
+ fail("error.text longer than expected");
+ if(json_error_code(&error) != json_error_end_of_input_expected)
+ fail("json_loads returned incorrect error code");
+}
+
static void run_tests()
{
file_not_found();
@@ -217,4 +234,5 @@ static void run_tests()
allow_nul();
load_wrong_args();
position();
+ error_code();
}
diff --git a/test/suites/api/test_pack.c b/test/suites/api/test_pack.c
index 65783b1..02631a0 100644
--- a/test/suites/api/test_pack.c
+++ b/test/suites/api/test_pack.c
@@ -302,78 +302,78 @@ static void run_tests()
/* newline in format string */
if(json_pack_ex(&error, 0, "{\n\n1"))
fail("json_pack failed to catch invalid format '1'");
- check_error("Expected format 's', got '1'", "<format>", 3, 1, 4);
+ check_error(json_error_invalid_format, "Expected format 's', got '1'", "<format>", 3, 1, 4);
/* mismatched open/close array/object */
if(json_pack_ex(&error, 0, "[}"))
fail("json_pack failed to catch mismatched '}'");
- check_error("Unexpected format character '}'", "<format>", 1, 2, 2);
+ check_error(json_error_invalid_format, "Unexpected format character '}'", "<format>", 1, 2, 2);
if(json_pack_ex(&error, 0, "{]"))
fail("json_pack failed to catch mismatched ']'");
- check_error("Expected format 's', got ']'", "<format>", 1, 2, 2);
+ check_error(json_error_invalid_format, "Expected format 's', got ']'", "<format>", 1, 2, 2);
/* missing close array */
if(json_pack_ex(&error, 0, "["))
fail("json_pack failed to catch missing ']'");
- check_error("Unexpected end of format string", "<format>", 1, 2, 2);
+ check_error(json_error_invalid_format, "Unexpected end of format string", "<format>", 1, 2, 2);
/* missing close object */
if(json_pack_ex(&error, 0, "{"))
fail("json_pack failed to catch missing '}'");
- check_error("Unexpected end of format string", "<format>", 1, 2, 2);
+ check_error(json_error_invalid_format, "Unexpected end of format string", "<format>", 1, 2, 2);
/* garbage after format string */
if(json_pack_ex(&error, 0, "[i]a", 42))
fail("json_pack failed to catch garbage after format string");
- check_error("Garbage after format string", "<format>", 1, 4, 4);
+ check_error(json_error_invalid_format, "Garbage after format string", "<format>", 1, 4, 4);
if(json_pack_ex(&error, 0, "ia", 42))
fail("json_pack failed to catch garbage after format string");
- check_error("Garbage after format string", "<format>", 1, 2, 2);
+ check_error(json_error_invalid_format, "Garbage after format string", "<format>", 1, 2, 2);
/* NULL string */
if(json_pack_ex(&error, 0, "s", NULL))
fail("json_pack failed to catch null argument string");
- check_error("NULL string argument", "<args>", 1, 1, 1);
+ check_error(json_error_null_value, "NULL string argument", "<args>", 1, 1, 1);
/* + on its own */
if(json_pack_ex(&error, 0, "+", NULL))
fail("json_pack failed to a lone +");
- check_error("Unexpected format character '+'", "<format>", 1, 1, 1);
+ check_error(json_error_invalid_format, "Unexpected format character '+'", "<format>", 1, 1, 1);
/* NULL format */
if(json_pack_ex(&error, 0, NULL))
fail("json_pack failed to catch NULL format string");
- check_error("NULL or empty format string", "<format>", -1, -1, 0);
+ check_error(json_error_invalid_argument, "NULL or empty format string", "<format>", -1, -1, 0);
/* NULL key */
if(json_pack_ex(&error, 0, "{s:i}", NULL, 1))
fail("json_pack failed to catch NULL key");
- check_error("NULL string argument", "<args>", 1, 2, 2);
+ check_error(json_error_null_value, "NULL string argument", "<args>", 1, 2, 2);
/* More complicated checks for row/columns */
if(json_pack_ex(&error, 0, "{ {}: s }", "foo"))
fail("json_pack failed to catch object as key");
- check_error("Expected format 's', got '{'", "<format>", 1, 3, 3);
+ check_error(json_error_invalid_format, "Expected format 's', got '{'", "<format>", 1, 3, 3);
/* Complex object */
if(json_pack_ex(&error, 0, "{ s: {}, s:[ii{} }", "foo", "bar", 12, 13))
fail("json_pack failed to catch missing ]");
- check_error("Unexpected format character '}'", "<format>", 1, 19, 19);
+ check_error(json_error_invalid_format, "Unexpected format character '}'", "<format>", 1, 19, 19);
/* Complex array */
if(json_pack_ex(&error, 0, "[[[[[ [[[[[ [[[[ }]]]] ]]]] ]]]]]"))
fail("json_pack failed to catch extra }");
- check_error("Unexpected format character '}'", "<format>", 1, 21, 21);
+ check_error(json_error_invalid_format, "Unexpected format character '}'", "<format>", 1, 21, 21);
/* Invalid UTF-8 in object key */
if(json_pack_ex(&error, 0, "{s:i}", "\xff\xff", 42))
fail("json_pack failed to catch invalid UTF-8 in an object key");
- check_error("Invalid UTF-8 object key", "<args>", 1, 2, 2);
+ check_error(json_error_invalid_utf8, "Invalid UTF-8 object key", "<args>", 1, 2, 2);
/* Invalid UTF-8 in a string */
if(json_pack_ex(&error, 0, "{s:s}", "foo", "\xff\xff"))
fail("json_pack failed to catch invalid UTF-8 in a string");
- check_error("Invalid UTF-8 string", "<args>", 1, 4, 4);
+ check_error(json_error_invalid_utf8, "Invalid UTF-8 string", "<args>", 1, 4, 4);
}
diff --git a/test/suites/api/test_unpack.c b/test/suites/api/test_unpack.c
index a3d208a..4516917 100644
--- a/test/suites/api/test_unpack.c
+++ b/test/suites/api/test_unpack.c
@@ -144,65 +144,65 @@ static void run_tests()
j = json_integer(42);
if(!json_unpack_ex(j, &error, 0, "z"))
fail("json_unpack succeeded with invalid format character");
- check_error("Unexpected format character 'z'", "<format>", 1, 1, 1);
+ check_error(json_error_invalid_format, "Unexpected format character 'z'", "<format>", 1, 1, 1);
if(!json_unpack_ex(NULL, &error, 0, "[i]"))
fail("json_unpack succeeded with NULL root");
- check_error("NULL root value", "<root>", -1, -1, 0);
+ check_error(json_error_null_value, "NULL root value", "<root>", -1, -1, 0);
json_decref(j);
/* mismatched open/close array/object */
j = json_pack("[]");
if(!json_unpack_ex(j, &error, 0, "[}"))
fail("json_unpack failed to catch mismatched ']'");
- check_error("Unexpected format character '}'", "<format>", 1, 2, 2);
+ check_error(json_error_invalid_format, "Unexpected format character '}'", "<format>", 1, 2, 2);
json_decref(j);
j = json_pack("{}");
if(!json_unpack_ex(j, &error, 0, "{]"))
fail("json_unpack failed to catch mismatched '}'");
- check_error("Expected format 's', got ']'", "<format>", 1, 2, 2);
+ check_error(json_error_invalid_format, "Expected format 's', got ']'", "<format>", 1, 2, 2);
json_decref(j);
/* missing close array */
j = json_pack("[]");
if(!json_unpack_ex(j, &error, 0, "["))
fail("json_unpack failed to catch missing ']'");
- check_error("Unexpected end of format string", "<format>", 1, 2, 2);
+ check_error(json_error_invalid_format, "Unexpected end of format string", "<format>", 1, 2, 2);
json_decref(j);
/* missing close object */
j = json_pack("{}");
if(!json_unpack_ex(j, &error, 0, "{"))
fail("json_unpack failed to catch missing '}'");
- check_error("Unexpected end of format string", "<format>", 1, 2, 2);
+ check_error(json_error_invalid_format, "Unexpected end of format string", "<format>", 1, 2, 2);
json_decref(j);
/* garbage after format string */
j = json_pack("[i]", 42);
if(!json_unpack_ex(j, &error, 0, "[i]a", &i1))
fail("json_unpack failed to catch garbage after format string");
- check_error("Garbage after format string", "<format>", 1, 4, 4);
+ check_error(json_error_invalid_format, "Garbage after format string", "<format>", 1, 4, 4);
json_decref(j);
j = json_integer(12345);
if(!json_unpack_ex(j, &error, 0, "ia", &i1))
fail("json_unpack failed to catch garbage after format string");
- check_error("Garbage after format string", "<format>", 1, 2, 2);
+ check_error(json_error_invalid_format, "Garbage after format string", "<format>", 1, 2, 2);
json_decref(j);
/* NULL format string */
j = json_pack("[]");
if(!json_unpack_ex(j, &error, 0, NULL))
fail("json_unpack failed to catch null format string");
- check_error("NULL or empty format string", "<format>", -1, -1, 0);
+ check_error(json_error_invalid_argument, "NULL or empty format string", "<format>", -1, -1, 0);
json_decref(j);
/* NULL string pointer */
j = json_string("foobie");
if(!json_unpack_ex(j, &error, 0, "s", NULL))
fail("json_unpack failed to catch null string pointer");
- check_error("NULL string argument", "<args>", 1, 1, 1);
+ check_error(json_error_null_value, "NULL string argument", "<args>", 1, 1, 1);
json_decref(j);
/* invalid types */
@@ -210,39 +210,39 @@ static void run_tests()
j2 = json_string("foo");
if(!json_unpack_ex(j, &error, 0, "s"))
fail("json_unpack failed to catch invalid type");
- check_error("Expected string, got integer", "<validation>", 1, 1, 1);
+ check_error(json_error_wrong_type, "Expected string, got integer", "<validation>", 1, 1, 1);
if(!json_unpack_ex(j, &error, 0, "n"))
fail("json_unpack failed to catch invalid type");
- check_error("Expected null, got integer", "<validation>", 1, 1, 1);
+ check_error(json_error_wrong_type, "Expected null, got integer", "<validation>", 1, 1, 1);
if(!json_unpack_ex(j, &error, 0, "b"))
fail("json_unpack failed to catch invalid type");
- check_error("Expected true or false, got integer", "<validation>", 1, 1, 1);
+ check_error(json_error_wrong_type, "Expected true or false, got integer", "<validation>", 1, 1, 1);
if(!json_unpack_ex(j2, &error, 0, "i"))
fail("json_unpack failed to catch invalid type");
- check_error("Expected integer, got string", "<validation>", 1, 1, 1);
+ check_error(json_error_wrong_type, "Expected integer, got string", "<validation>", 1, 1, 1);
if(!json_unpack_ex(j2, &error, 0, "I"))
fail("json_unpack failed to catch invalid type");
- check_error("Expected integer, got string", "<validation>", 1, 1, 1);
+ check_error(json_error_wrong_type, "Expected integer, got string", "<validation>", 1, 1, 1);
if(!json_unpack_ex(j, &error, 0, "f"))
fail("json_unpack failed to catch invalid type");
- check_error("Expected real, got integer", "<validation>", 1, 1, 1);
+ check_error(json_error_wrong_type, "Expected real, got integer", "<validation>", 1, 1, 1);
if(!json_unpack_ex(j2, &error, 0, "F"))
fail("json_unpack failed to catch invalid type");
- check_error("Expected real or integer, got string", "<validation>", 1, 1, 1);
+ check_error(json_error_wrong_type, "Expected real or integer, got string", "<validation>", 1, 1, 1);
if(!json_unpack_ex(j, &error, 0, "[i]"))
fail("json_unpack failed to catch invalid type");
- check_error("Expected array, got integer", "<validation>", 1, 1, 1);
+ check_error(json_error_wrong_type, "Expected array, got integer", "<validation>", 1, 1, 1);
if(!json_unpack_ex(j, &error, 0, "{si}", "foo"))
fail("json_unpack failed to catch invalid type");
- check_error("Expected object, got integer", "<validation>", 1, 1, 1);
+ check_error(json_error_wrong_type, "Expected object, got integer", "<validation>", 1, 1, 1);
json_decref(j);
json_decref(j2);
@@ -251,21 +251,21 @@ static void run_tests()
j = json_pack("[i]", 1);
if(!json_unpack_ex(j, &error, 0, "[ii]", &i1, &i2))
fail("json_unpack failed to catch index out of array bounds");
- check_error("Array index 1 out of range", "<validation>", 1, 3, 3);
+ check_error(json_error_index_out_of_range, "Array index 1 out of range", "<validation>", 1, 3, 3);
json_decref(j);
/* NULL object key */
j = json_pack("{si}", "foo", 42);
if(!json_unpack_ex(j, &error, 0, "{si}", NULL, &i1))
fail("json_unpack failed to catch null string pointer");
- check_error("NULL object key", "<args>", 1, 2, 2);
+ check_error(json_error_null_value, "NULL object key", "<args>", 1, 2, 2);
json_decref(j);
/* Object key not found */
j = json_pack("{si}", "foo", 42);
if(!json_unpack_ex(j, &error, 0, "{si}", "baz", &i1))
fail("json_unpack failed to catch null string pointer");
- check_error("Object item not found: baz", "<validation>", 1, 3, 3);
+ check_error(json_error_item_not_found, "Object item not found: baz", "<validation>", 1, 3, 3);
json_decref(j);
/*
@@ -281,14 +281,14 @@ static void run_tests()
j = json_pack("[iii]", 1, 2, 3);
if(!json_unpack_ex(j, &error, 0, "[ii!]", &i1, &i2))
fail("json_unpack array with strict validation failed");
- check_error("1 array item(s) left unpacked", "<validation>", 1, 5, 5);
+ check_error(json_error_end_of_input_expected, "1 array item(s) left unpacked", "<validation>", 1, 5, 5);
json_decref(j);
/* Like above, but with JSON_STRICT instead of '!' format */
j = json_pack("[iii]", 1, 2, 3);
if(!json_unpack_ex(j, &error, JSON_STRICT, "[ii]", &i1, &i2))
fail("json_unpack array with strict validation failed");
- check_error("1 array item(s) left unpacked", "<validation>", 1, 4, 4);
+ check_error(json_error_end_of_input_expected, "1 array item(s) left unpacked", "<validation>", 1, 4, 4);
json_decref(j);
j = json_pack("{s:s, s:i}", "foo", "bar", "baz", 42);
@@ -306,7 +306,7 @@ static void run_tests()
"2 object item(s) left unpacked: baz, quux",
"2 object item(s) left unpacked: quux, baz"
};
- check_errors(possible_errors, 2, "<validation>", 1, 10, 10);
+ check_errors(json_error_end_of_input_expected, possible_errors, 2, "<validation>", 1, 10, 10);
}
json_decref(j);
@@ -320,35 +320,35 @@ static void run_tests()
j = json_pack("[ii]", 1, 2);
if(!json_unpack_ex(j, &error, 0, "[i!i]", &i1, &i2))
fail("json_unpack failed to catch ! in the middle of an array");
- check_error("Expected ']' after '!', got 'i'", "<format>", 1, 4, 4);
+ check_error(json_error_invalid_format, "Expected ']' after '!', got 'i'", "<format>", 1, 4, 4);
if(!json_unpack_ex(j, &error, 0, "[i*i]", &i1, &i2))
fail("json_unpack failed to catch * in the middle of an array");
- check_error("Expected ']' after '*', got 'i'", "<format>", 1, 4, 4);
+ check_error(json_error_invalid_format, "Expected ']' after '*', got 'i'", "<format>", 1, 4, 4);
json_decref(j);
j = json_pack("{sssi}", "foo", "bar", "baz", 42);
if(!json_unpack_ex(j, &error, 0, "{ss!si}", "foo", &s, "baz", &i1))
fail("json_unpack failed to catch ! in the middle of an object");
- check_error("Expected '}' after '!', got 's'", "<format>", 1, 5, 5);
+ check_error(json_error_invalid_format, "Expected '}' after '!', got 's'", "<format>", 1, 5, 5);
if(!json_unpack_ex(j, &error, 0, "{ss*si}", "foo", &s, "baz", &i1))
fail("json_unpack failed to catch ! in the middle of an object");
- check_error("Expected '}' after '*', got 's'", "<format>", 1, 5, 5);
+ check_error(json_error_invalid_format, "Expected '}' after '*', got 's'", "<format>", 1, 5, 5);
json_decref(j);
/* Error in nested object */
j = json_pack("{s{snsn}}", "foo", "bar", "baz");
if(!json_unpack_ex(j, &error, 0, "{s{sn!}}", "foo", "bar"))
fail("json_unpack nested object with strict validation failed");
- check_error("1 object item(s) left unpacked: baz", "<validation>", 1, 7, 7);
+ check_error(json_error_end_of_input_expected, "1 object item(s) left unpacked: baz", "<validation>", 1, 7, 7);
json_decref(j);
/* Error in nested array */
j = json_pack("[[ii]]", 1, 2);
if(!json_unpack_ex(j, &error, 0, "[[i!]]", &i1))
fail("json_unpack nested array with strict validation failed");
- check_error("1 array item(s) left unpacked", "<validation>", 1, 5, 5);
+ check_error(json_error_end_of_input_expected, "1 array item(s) left unpacked", "<validation>", 1, 5, 5);
json_decref(j);
/* Optional values */
@@ -401,6 +401,6 @@ static void run_tests()
i1 = i2 = i3 = 0;
if(!json_unpack_ex(j, &error, 0, "{sis?i!}", "foo", &i1, "bar", &i2))
fail("json_unpack failed for optional values with strict mode and compensation");
- check_error("1 object item(s) left unpacked: baz", "<validation>", 1, 8, 8);
+ check_error(json_error_end_of_input_expected, "1 object item(s) left unpacked: baz", "<validation>", 1, 8, 8);
json_decref(j);
}
diff --git a/test/suites/api/util.h b/test/suites/api/util.h
index a3a27d3..9b658e7 100644
--- a/test/suites/api/util.h
+++ b/test/suites/api/util.h
@@ -30,9 +30,16 @@
} while(0)
/* Assumes json_error_t error */
-#define check_errors(texts_, num_, source_, line_, column_, position_) \
+#define check_errors(code_, texts_, num_, source_, \
+ line_, column_, position_) \
do { \
int i_, found_ = 0; \
+ if(json_error_code(&error) != code_) { \
+ failhdr; \
+ fprintf(stderr, "code: %d != %d\n", \
+ json_error_code(&error), code_); \
+ exit(1); \
+ } \
for(i_ = 0; i_ < num_; i_++) { \
if(strcmp(error.text, texts_[i_]) == 0) { \
found_ = 1; \
@@ -73,8 +80,8 @@
/* Assumes json_error_t error */
-#define check_error(text_, source_, line_, column_, position_) \
- check_errors(&text_, 1, source_, line_, column_, position_)
+#define check_error(code_, text_, source_, line_, column_, position_) \
+ check_errors(code_, &text_, 1, source_, line_, column_, position_)
static void run_tests();