| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- /* Host tests for ujson.c (make test-chat). */
- #include <stdio.h>
- #include <string.h>
- #include "ujson.h"
- static int failures;
- #define CHECK(cond) do { \
- if (!(cond)) { \
- printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
- failures++; \
- } \
- } while (0)
- static struct ujson_node nodes[4096];
- static char sbuf[65536];
- static struct ujson j;
- static int parse(const char *s)
- {
- ujson_init(&j, nodes, sizeof(nodes) / sizeof(nodes[0]), sbuf,
- sizeof(sbuf));
- return ujson_parse(&j, s, (unsigned int)strlen(s));
- }
- static void test_basic(void)
- {
- int r = parse("{\"a\": 1, \"b\": [true, false, null, \"x\"],"
- " \"c\": {\"d\": -42.5e1}, \"e\": \"\"}");
- int b, it;
- CHECK(r == 0);
- CHECK(ujson_type(&j, r) == UJSON_OBJECT);
- CHECK(ujson_count(&j, r) == 4);
- CHECK(ujson_int(&j, ujson_get(&j, r, "a"), -1) == 1);
- b = ujson_get(&j, r, "b");
- CHECK(ujson_type(&j, b) == UJSON_ARRAY);
- CHECK(ujson_count(&j, b) == 4);
- it = ujson_first(&j, b);
- CHECK(ujson_type(&j, it) == UJSON_TRUE);
- it = ujson_next(&j, it);
- CHECK(ujson_type(&j, it) == UJSON_FALSE);
- it = ujson_next(&j, it);
- CHECK(ujson_type(&j, it) == UJSON_NULL);
- it = ujson_next(&j, it);
- CHECK(strcmp(ujson_str(&j, it), "x") == 0);
- CHECK(ujson_next(&j, it) == -1);
- CHECK(ujson_int(&j, ujson_get(&j, ujson_get(&j, r, "c"), "d"), 0) ==
- -42);
- CHECK(strcmp(ujson_get_str(&j, r, "e"), "") == 0);
- CHECK(ujson_get(&j, r, "missing") == -1);
- CHECK(strcmp(ujson_get_str(&j, r, "missing"), "") == 0);
- CHECK(strcmp(ujson_get_str(&j, r, "a"), "") == 0);
- CHECK(ujson_truthy(&j, ujson_get(&j, r, "a")));
- CHECK(!ujson_truthy(&j, ujson_get(&j, r, "e")));
- }
- static void test_escapes(void)
- {
- int r = parse("[\"q\\\"b\\\\s\\/n\\n\\t\", \"\\u00e9\\u20ac\","
- " \"\\ud83d\\ude00\", \"\\ud800x\", \"caf\xc3\xa9\"]");
- int it;
- CHECK(r == 0);
- it = ujson_first(&j, r);
- CHECK(strcmp(ujson_str(&j, it), "q\"b\\s/n\n\t") == 0);
- it = ujson_next(&j, it);
- CHECK(strcmp(ujson_str(&j, it), "\xc3\xa9\xe2\x82\xac") == 0);
- it = ujson_next(&j, it);
- CHECK(strcmp(ujson_str(&j, it), "\xf0\x9f\x98\x80") == 0);
- it = ujson_next(&j, it);
- /* lone high surrogate -> U+FFFD */
- CHECK(strcmp(ujson_str(&j, it), "\xef\xbf\xbdx") == 0);
- it = ujson_next(&j, it);
- CHECK(strcmp(ujson_str(&j, it), "caf\xc3\xa9") == 0);
- }
- static void test_errors(void)
- {
- static const char *bad[] = {
- "", "{", "[1,]", "{\"a\" 1}", "{\"a\":}", "[1 2]", "\"abc",
- "tru", "{\"a\":1}x", "[\"\\x\"]", "[\"a\nb\"]", "-", "{1:2}",
- "[\"\\u12\"]",
- };
- unsigned int i;
- for (i = 0; i < sizeof(bad) / sizeof(bad[0]); i++) {
- int r = parse(bad[i]);
- if (r != -1)
- printf("FAIL: accepted bad JSON '%s'\n", bad[i]);
- CHECK(r == -1);
- CHECK(j.error != 0);
- }
- }
- static void test_depth_and_pool(void)
- {
- char deep[256];
- struct ujson_node few[3];
- char small[8];
- int i;
- for (i = 0; i < 100; i++)
- deep[i] = '[';
- for (i = 0; i < 100; i++)
- deep[100 + i] = ']';
- deep[200] = '\0';
- CHECK(parse(deep) == -1);
- ujson_init(&j, few, 3, sbuf, sizeof(sbuf));
- CHECK(ujson_parse(&j, "[1,2,3]", 7) == -1);
- ujson_init(&j, nodes, 64, small, sizeof(small));
- CHECK(ujson_parse(&j, "\"0123456789\"", 12) == -1);
- }
- static void test_api_shape(void)
- {
- /* A Chat /api/channels/:id/messages page (trimmed). */
- const char *s =
- "[{\"id\":\"m2\",\"clientId\":null,\"channelId\":\"c1\","
- "\"userId\":\"u1\",\"authorName\":\"Alice\",\"body\":\"Hi \\ud83d\\udc4b\","
- "\"type\":null,\"replyTo\":{\"id\":\"m1\",\"authorName\":\"Bob\","
- "\"body\":\"earlier\"},\"createdAt\":\"2026-11-09T10:42:00.000Z\","
- "\"deleted\":false,\"reactions\":[{\"emoji\":\"+1\",\"count\":2}],"
- "\"attachments\":[{\"filename\":\"notes.txt\",\"size\":12}],"
- "\"threadCount\":2,\"readBy\":[]},"
- "{\"id\":\"m1\",\"userId\":\"u2\",\"authorName\":\"Bob\","
- "\"body\":\"earlier\",\"createdAt\":\"2026-11-09T10:41:00.000Z\","
- "\"deleted\":false,\"threadCount\":0}]";
- int r = parse(s), m, rt, at;
- CHECK(r == 0);
- CHECK(ujson_count(&j, r) == 2);
- m = ujson_first(&j, r);
- CHECK(strcmp(ujson_get_str(&j, m, "authorName"), "Alice") == 0);
- CHECK(strcmp(ujson_get_str(&j, m, "body"), "Hi \xf0\x9f\x91\x8b") == 0);
- CHECK(ujson_type(&j, ujson_get(&j, m, "clientId")) == UJSON_NULL);
- rt = ujson_get(&j, m, "replyTo");
- CHECK(strcmp(ujson_get_str(&j, rt, "authorName"), "Bob") == 0);
- at = ujson_first(&j, ujson_get(&j, m, "attachments"));
- CHECK(strcmp(ujson_get_str(&j, at, "filename"), "notes.txt") == 0);
- CHECK(ujson_int(&j, ujson_get(&j, m, "threadCount"), 0) == 2);
- m = ujson_next(&j, m);
- CHECK(strcmp(ujson_get_str(&j, m, "id"), "m1") == 0);
- }
- static void test_escape_out(void)
- {
- char out[64];
- CHECK(ujson_escape(out, sizeof(out), "a\"b\\c\nd\x01") > 0);
- CHECK(strcmp(out, "\"a\\\"b\\\\c\\nd\\u0001\"") == 0);
- CHECK(ujson_escape(out, 4, "abcdef") == -1);
- /* round trip */
- CHECK(ujson_escape(out, sizeof(out), "p@ss\"w\\rd") > 0);
- CHECK(parse(out) == 0);
- CHECK(strcmp(ujson_str(&j, 0), "p@ss\"w\\rd") == 0);
- }
- int main(void)
- {
- test_basic();
- test_escapes();
- test_errors();
- test_depth_and_pool();
- test_api_shape();
- test_escape_out();
- if (failures) {
- printf("ujson_test: %d failure(s)\n", failures);
- return 1;
- }
- printf("ujson_test: all tests passed\n");
- return 0;
- }
|