Files
mcde/lib/libDtChat/test_json.c
T
lotherk e645e8160d dtchat: add the core API, JSON reader and line framing (T1.1)
include/DtChat/DtChat.h and Ipc.h define the UI-facing core API and the adapter contract. lib/libDtChat builds libDtChatIpc (strict JSON parser/writer, 1 MiB line framing) and the libDtChat core stub; test_json and test_frame cover escapes, UTF-8, depth 32, duplicate keys, overlong lines, CRLF, NUL and the write path. Wired into configure.ac, lib/Makefile.am and the installed headers. Standalone tests pass, ASan/UBSan clean.
2026-11-16 16:35:48 +00:00

293 lines
7.0 KiB
C

/*
* test_json.c -- unit tests for libDtChatIpc's JSON reader/writer
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*/
#include "DtChat/Ipc.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int fails;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
#cond); \
fails++; \
} \
} while (0)
static DcJson *
parse_ok(const char *text)
{
char err[256];
DcJson *json = dc_json_parse(text, strlen(text), err, sizeof(err));
if (json == NULL)
fprintf(stderr, "parse failed for [%s]: %s\n", text, err);
return json;
}
static void
expect_bad(const char *text)
{
char err[256];
DcJson *json = dc_json_parse(text, strlen(text), err, sizeof(err));
if (json != NULL) {
fprintf(stderr, "expected failure for [%s]\n", text);
fails++;
dc_json_free(json);
}
}
static void
test_basic(void)
{
DcJson *json = parse_ok("{\"a\":1,\"b\":\"x\",\"c\":true,"
"\"d\":false,\"e\":null,\"f\":[1,2,3]}");
if (json == NULL)
return;
CHECK(dc_json_type(json) == DC_JSON_OBJECT);
CHECK(dc_json_int(dc_json_get(json, "a")) == 1);
CHECK(strcmp(dc_json_string(dc_json_get(json, "b")), "x") == 0);
CHECK(dc_json_bool(dc_json_get(json, "c")) == 1);
CHECK(dc_json_bool(dc_json_get(json, "d")) == 0);
CHECK(dc_json_type(dc_json_get(json, "e")) == DC_JSON_NULL);
CHECK(dc_json_array_size(dc_json_get(json, "f")) == 3);
CHECK(dc_json_int(dc_json_array_get(dc_json_get(json, "f"), 2)) == 3);
CHECK(dc_json_array_get(dc_json_get(json, "f"), 3) == NULL);
CHECK(dc_json_get(json, "missing") == NULL);
CHECK(dc_json_string(json) == NULL);
dc_json_free(json);
}
static void
test_strings(void)
{
DcJson *json = parse_ok("\"\\u00e4 \\u20ac \\ud83d\\ude00\"");
const char *s;
if (json == NULL)
return;
s = dc_json_string(json);
CHECK(s != NULL);
if (s != NULL) {
CHECK(strcmp(s, "\xc3\xa4 \xe2\x82\xac \xf0\x9f\x98\x80") == 0);
CHECK(dc_utf8_valid(s, strlen(s)));
}
dc_json_free(json);
json = parse_ok("\"a\\\"b\\\\c\\/d\\b\\f\\n\\r\\t\"");
if (json != NULL) {
CHECK(strcmp(dc_json_string(json), "a\"b\\c/d\b\f\n\r\t") == 0);
dc_json_free(json);
}
}
static void
test_numbers(void)
{
DcJson *json = parse_ok("[-9223372036854775808,1.5,1e3,-0]");
if (json == NULL)
return;
CHECK(dc_json_type(dc_json_array_get(json, 0)) == DC_JSON_INT);
CHECK(dc_json_int(dc_json_array_get(json, 0)) == (-9223372036854775807LL - 1));
CHECK(dc_json_double(dc_json_array_get(json, 1)) == 1.5);
CHECK(dc_json_double(dc_json_array_get(json, 2)) == 1000.0);
CHECK(dc_json_type(dc_json_array_get(json, 3)) == DC_JSON_INT);
dc_json_free(json);
}
static void
test_invalid(void)
{
expect_bad("");
expect_bad("{");
expect_bad("}");
expect_bad("[]x");
expect_bad("{\"a\":1,}");
expect_bad("[1,]");
expect_bad("{\"a\":1,\"a\":2}");
expect_bad("\"\\u0000\"");
expect_bad("\"\\ud800\"");
expect_bad("\"\\udc00\"");
expect_bad("\"\\ud800\\ud800\"");
expect_bad("\"\\u12\"");
expect_bad("\"\\q\"");
expect_bad("\"a\nb\"");
expect_bad("01");
expect_bad("-");
expect_bad("1.");
expect_bad("1e");
expect_bad("tru");
expect_bad("nul");
expect_bad("{\"a\" 1}");
expect_bad("{\"a\":}");
}
static void
test_utf8(void)
{
const char bad1[] = "\"\xc0\xaf\"";
const char bad2[] = "\"\xed\xa0\x80\"";
const char good[] = "\"\xc3\xa4\"";
char err[256];
DcJson *json;
CHECK(dc_utf8_valid("hello", 5));
CHECK(dc_utf8_valid("\xc3\xa4", 2));
CHECK(!dc_utf8_valid("\xc0\xaf", 2));
CHECK(!dc_utf8_valid("\xed\xa0\x80", 3));
CHECK(!dc_utf8_valid("\xf4\x90\x80\x80", 4));
CHECK(!dc_utf8_valid("\xc3", 1));
json = dc_json_parse(bad1, sizeof(bad1) - 1, err, sizeof(err));
CHECK(json == NULL);
json = dc_json_parse(bad2, sizeof(bad2) - 1, err, sizeof(err));
CHECK(json == NULL);
json = dc_json_parse(good, sizeof(good) - 1, err, sizeof(err));
CHECK(json != NULL);
dc_json_free(json);
}
static void
test_depth(void)
{
char buf[128];
size_t i;
char err[256];
DcJson *json;
for (i = 0; i < 33; i++)
buf[i] = '[';
for (i = 0; i < 33; i++)
buf[33 + i] = ']';
buf[66] = '\0';
json = dc_json_parse(buf, 66, err, sizeof(err));
CHECK(json == NULL);
if (json != NULL)
dc_json_free(json);
buf[32] = '\0';
for (i = 0; i < 32; i++)
buf[32 + i] = ']';
buf[64] = '\0';
json = dc_json_parse(buf, 64, err, sizeof(err));
CHECK(json != NULL);
dc_json_free(json);
}
static void
test_too_long(void)
{
char *buf = malloc(DC_IPC_MAX_LINE + 1);
char err[256];
DcJson *json;
if (buf == NULL)
return;
memset(buf, 'a', DC_IPC_MAX_LINE + 1);
json = dc_json_parse(buf, DC_IPC_MAX_LINE + 1, err, sizeof(err));
CHECK(json == NULL);
free(buf);
}
static void
test_writer(void)
{
DcJson *obj = dc_json_new_object();
DcJson *arr = dc_json_new_array();
DcJson *nested = dc_json_new_object();
char *text;
size_t len;
DcJson *again;
CHECK(obj != NULL && arr != NULL && nested != NULL);
CHECK(dc_json_object_set(obj, "a", dc_json_new_int(1)) == 0);
CHECK(dc_json_object_set(obj, "b", dc_json_new_string("x")) == 0);
CHECK(dc_json_array_append(arr, dc_json_new_bool(1)) == 0);
CHECK(dc_json_array_append(arr, dc_json_new_null()) == 0);
CHECK(dc_json_object_set(obj, "c", arr) == 0);
CHECK(dc_json_object_set(nested, "e", dc_json_new_int(2)) == 0);
CHECK(dc_json_object_set(obj, "d", nested) == 0);
text = dc_json_write(obj, &len);
CHECK(text != NULL);
if (text != NULL) {
again = dc_json_parse(text, len, NULL, 0);
CHECK(again != NULL);
if (again != NULL) {
CHECK(dc_json_int(dc_json_get(again, "a")) == 1);
CHECK(strcmp(dc_json_string(dc_json_get(again, "b")),
"x") == 0);
CHECK(dc_json_array_size(dc_json_get(again, "c")) == 2);
CHECK(dc_json_int(dc_json_get(
dc_json_get(again, "d"), "e")) == 2);
dc_json_free(again);
}
free(text);
}
/* replace an existing key */
CHECK(dc_json_object_set(obj, "a", dc_json_new_int(3)) == 0);
text = dc_json_write(obj, &len);
CHECK(text != NULL);
if (text != NULL) {
again = dc_json_parse(text, len, NULL, 0);
CHECK(again != NULL);
if (again != NULL) {
CHECK(dc_json_int(dc_json_get(again, "a")) == 3);
dc_json_free(again);
}
free(text);
}
dc_json_free(obj);
/* escaping round-trip */
obj = dc_json_new_object();
CHECK(dc_json_object_set(obj, "s",
dc_json_new_string("a\"b\\c\n\t\xc3\xa4")) == 0);
text = dc_json_write(obj, &len);
CHECK(text != NULL);
if (text != NULL) {
again = dc_json_parse(text, len, NULL, 0);
CHECK(again != NULL);
if (again != NULL) {
CHECK(strcmp(dc_json_string(dc_json_get(again, "s")),
"a\"b\\c\n\t\xc3\xa4") == 0);
dc_json_free(again);
}
free(text);
}
dc_json_free(obj);
CHECK(dc_json_new_string("\xc0\xaf") == NULL);
CHECK(dc_json_new_double(NAN) == NULL);
CHECK(dc_json_new_double(INFINITY) == NULL);
}
int
main(void)
{
test_basic();
test_strings();
test_numbers();
test_invalid();
test_utf8();
test_depth();
test_too_long();
test_writer();
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
}
printf("test_json: all ok\n");
return 0;
}