jansson/test/suites/api/test_load_callback.c

79 lines
2.0 KiB
C
Raw Normal View History

2012-03-21 16:34:14 +08:00
/*
* 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.
*/
2019-10-17 14:08:51 +08:00
#include "util.h"
2012-03-21 16:34:14 +08:00
#include <jansson.h>
#include <stdlib.h>
2019-10-17 14:08:51 +08:00
#include <string.h>
2012-03-21 16:34:14 +08:00
struct my_source {
const char *buf;
size_t off;
size_t cap;
};
2019-10-17 14:08:51 +08:00
static const char my_str[] =
"[\"A\", {\"B\": \"C\", \"e\": false}, 1, null, \"foo\"]";
2012-03-21 16:34:14 +08:00
2019-10-17 14:08:51 +08:00
static size_t greedy_reader(void *buf, size_t buflen, void *arg) {
2012-03-21 16:34:14 +08:00
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;
}
}
2019-10-17 14:08:51 +08:00
static void run_tests() {
2012-03-21 16:34:14 +08:00
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);
2019-10-17 14:08:51 +08:00
fail("json_load_callback should have failed on an incomplete stream, "
"but it didn't");
2012-03-21 16:34:14 +08:00
}
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) {
2019-10-17 14:08:51 +08:00
fail("json_load_callback returned an invalid error message for an "
"unclosed top-level array");
2012-03-21 16:34:14 +08:00
}
json = json_load_callback(NULL, NULL, 0, &error);
if (json) {
json_decref(json);
2019-10-17 14:08:51 +08:00
fail("json_load_callback should have failed on NULL load callback, but "
"it didn't");
2012-03-21 16:34:14 +08:00
}
if (strcmp(error.text, "wrong arguments") != 0) {
2019-10-17 14:08:51 +08:00
fail("json_load_callback returned an invalid error message for a NULL "
"load callback");
2012-03-21 16:34:14 +08:00
}
}