summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/jansson-e23f558/test/suites/api/test_load_callback.c
diff options
context:
space:
mode:
Diffstat (limited to 'fluent-bit/lib/jansson-e23f558/test/suites/api/test_load_callback.c')
-rw-r--r--fluent-bit/lib/jansson-e23f558/test/suites/api/test_load_callback.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/fluent-bit/lib/jansson-e23f558/test/suites/api/test_load_callback.c b/fluent-bit/lib/jansson-e23f558/test/suites/api/test_load_callback.c
new file mode 100644
index 000000000..b292fcf42
--- /dev/null
+++ b/fluent-bit/lib/jansson-e23f558/test/suites/api/test_load_callback.c
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "util.h"
+#include <jansson.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct my_source {
+ const char *buf;
+ size_t off;
+ size_t cap;
+};
+
+static const char my_str[] = "[\"A\", {\"B\": \"C\", \"e\": false}, 1, null, \"foo\"]";
+
+static size_t greedy_reader(void *buf, size_t buflen, void *arg) {
+ struct my_source *s = arg;
+ if (buflen > s->cap - s->off)
+ buflen = s->cap - s->off;
+ if (buflen > 0) {
+ memcpy(buf, s->buf + s->off, buflen);
+ s->off += buflen;
+ return buflen;
+ } else {
+ return 0;
+ }
+}
+
+static void run_tests() {
+ struct my_source s;
+ json_t *json;
+ json_error_t error;
+
+ s.off = 0;
+ s.cap = strlen(my_str);
+ s.buf = my_str;
+
+ json = json_load_callback(greedy_reader, &s, 0, &error);
+
+ if (!json)
+ fail("json_load_callback failed on a valid callback");
+ json_decref(json);
+
+ s.off = 0;
+ s.cap = strlen(my_str) - 1;
+ s.buf = my_str;
+
+ json = json_load_callback(greedy_reader, &s, 0, &error);
+ if (json) {
+ json_decref(json);
+ fail("json_load_callback should have failed on an incomplete stream, "
+ "but it didn't");
+ }
+ if (strcmp(error.source, "<callback>") != 0) {
+ fail("json_load_callback returned an invalid error source");
+ }
+ if (strcmp(error.text, "']' expected near end of file") != 0) {
+ fail("json_load_callback returned an invalid error message for an "
+ "unclosed top-level array");
+ }
+
+ json = json_load_callback(NULL, NULL, 0, &error);
+ if (json) {
+ json_decref(json);
+ fail("json_load_callback should have failed on NULL load callback, but "
+ "it didn't");
+ }
+ if (strcmp(error.text, "wrong arguments") != 0) {
+ fail("json_load_callback returned an invalid error message for a NULL "
+ "load callback");
+ }
+}