aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorCorey Farrell <git@cfware.com>2018-09-25 17:34:25 -0400
committerCorey Farrell <git@cfware.com>2018-09-25 18:03:06 -0400
commit8d659113d53d7ef60eae6a6e2c5b0ecfc89fc74b (patch)
tree4282d973a6da6bfed78bb772ab6ddc97fb0c91ea /test
parent5df5fc5b13cac5212482d36e7f3a78951782cfb5 (diff)
downloadjansson-8d659113d53d7ef60eae6a6e2c5b0ecfc89fc74b.zip
jansson-8d659113d53d7ef60eae6a6e2c5b0ecfc89fc74b.tar.gz
jansson-8d659113d53d7ef60eae6a6e2c5b0ecfc89fc74b.tar.bz2
More work on json_pack error reporting.
* Remove errant line-feed from pack_object error message. * Correct error message in pack_object_inter. * Create pack_integer / pack_real to get the correct error messages on failure when packing numeric values. * Add tests for packing NAN and infinity directly, in an array and as an object value.
Diffstat (limited to 'test')
-rw-r--r--test/suites/api/test_pack.c54
1 files changed, 52 insertions, 2 deletions
diff --git a/test/suites/api/test_pack.c b/test/suites/api/test_pack.c
index 7e1c0b5..ab3aa91 100644
--- a/test/suites/api/test_pack.c
+++ b/test/suites/api/test_pack.c
@@ -15,8 +15,39 @@
#include <string.h>
#include <jansson.h>
#include <stdio.h>
+#include <math.h>
#include "util.h"
+#ifdef INFINITY
+// This test triggers "warning C4756: overflow in constant arithmetic"
+// in Visual Studio. This warning is triggered here by design, so disable it.
+// (This can only be done on function level so we keep these tests separate)
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning (disable: 4756)
+#endif
+static void test_inifity()
+{
+ json_error_t error;
+
+ if (json_pack_ex(&error, 0, "f", INFINITY))
+ fail("json_pack infinity incorrectly succeeded");
+ check_error(json_error_numeric_overflow, "Invalid floating point value", "<args>", 1, 1, 1);
+
+ if (json_pack_ex(&error, 0, "[f]", INFINITY))
+ fail("json_pack infinity array element incorrectly succeeded");
+ check_error(json_error_numeric_overflow, "Invalid floating point value", "<args>", 1, 2, 2);
+
+ if (json_pack_ex(&error, 0, "{s:f}", "key", INFINITY))
+ fail("json_pack infinity object value incorrectly succeeded");
+ check_error(json_error_numeric_overflow, "Invalid floating point value", "<args>", 1, 4, 4);
+
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
+}
+#endif // INFINITY
+
static void run_tests()
{
json_t *value;
@@ -313,6 +344,25 @@ static void run_tests()
fail("json_pack array optional failed");
json_decref(value);
+#ifdef NAN
+ /* Invalid float values */
+ if (json_pack_ex(&error, 0, "f", NAN))
+ fail("json_pack NAN incorrectly succeeded");
+ check_error(json_error_numeric_overflow, "Invalid floating point value", "<args>", 1, 1, 1);
+
+ if (json_pack_ex(&error, 0, "[f]", NAN))
+ fail("json_pack NAN array element incorrectly succeeded");
+ check_error(json_error_numeric_overflow, "Invalid floating point value", "<args>", 1, 2, 2);
+
+ if (json_pack_ex(&error, 0, "{s:f}", "key", NAN))
+ fail("json_pack NAN object value incorrectly succeeded");
+ check_error(json_error_numeric_overflow, "Invalid floating point value", "<args>", 1, 4, 4);
+#endif
+
+#ifdef INFINITY
+ test_inifity();
+#endif
+
/* Whitespace; regular string */
value = json_pack(" s\t ", "test");
if(!json_is_string(value) || strcmp("test", json_string_value(value)))
@@ -439,9 +489,9 @@ static void run_tests()
if(json_pack_ex(&error, 0, "{s:o}", "foo", NULL))
fail("json_pack failed to catch nullable object");
- check_error(json_error_null_value, "NULL object key", "<args>", 1, 4, 4);
+ check_error(json_error_null_value, "NULL object", "<args>", 1, 4, 4);
if(json_pack_ex(&error, 0, "{s:O}", "foo", NULL))
fail("json_pack failed to catch nullable incref object");
- check_error(json_error_null_value, "NULL object key", "<args>", 1, 4, 4);
+ check_error(json_error_null_value, "NULL object", "<args>", 1, 4, 4);
}