ujson_test.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Host tests for ujson.c (make test-chat). */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "ujson.h"
  5. static int failures;
  6. #define CHECK(cond) do { \
  7. if (!(cond)) { \
  8. printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
  9. failures++; \
  10. } \
  11. } while (0)
  12. static struct ujson_node nodes[4096];
  13. static char sbuf[65536];
  14. static struct ujson j;
  15. static int parse(const char *s)
  16. {
  17. ujson_init(&j, nodes, sizeof(nodes) / sizeof(nodes[0]), sbuf,
  18. sizeof(sbuf));
  19. return ujson_parse(&j, s, (unsigned int)strlen(s));
  20. }
  21. static void test_basic(void)
  22. {
  23. int r = parse("{\"a\": 1, \"b\": [true, false, null, \"x\"],"
  24. " \"c\": {\"d\": -42.5e1}, \"e\": \"\"}");
  25. int b, it;
  26. CHECK(r == 0);
  27. CHECK(ujson_type(&j, r) == UJSON_OBJECT);
  28. CHECK(ujson_count(&j, r) == 4);
  29. CHECK(ujson_int(&j, ujson_get(&j, r, "a"), -1) == 1);
  30. b = ujson_get(&j, r, "b");
  31. CHECK(ujson_type(&j, b) == UJSON_ARRAY);
  32. CHECK(ujson_count(&j, b) == 4);
  33. it = ujson_first(&j, b);
  34. CHECK(ujson_type(&j, it) == UJSON_TRUE);
  35. it = ujson_next(&j, it);
  36. CHECK(ujson_type(&j, it) == UJSON_FALSE);
  37. it = ujson_next(&j, it);
  38. CHECK(ujson_type(&j, it) == UJSON_NULL);
  39. it = ujson_next(&j, it);
  40. CHECK(strcmp(ujson_str(&j, it), "x") == 0);
  41. CHECK(ujson_next(&j, it) == -1);
  42. CHECK(ujson_int(&j, ujson_get(&j, ujson_get(&j, r, "c"), "d"), 0) ==
  43. -42);
  44. CHECK(strcmp(ujson_get_str(&j, r, "e"), "") == 0);
  45. CHECK(ujson_get(&j, r, "missing") == -1);
  46. CHECK(strcmp(ujson_get_str(&j, r, "missing"), "") == 0);
  47. CHECK(strcmp(ujson_get_str(&j, r, "a"), "") == 0);
  48. CHECK(ujson_truthy(&j, ujson_get(&j, r, "a")));
  49. CHECK(!ujson_truthy(&j, ujson_get(&j, r, "e")));
  50. }
  51. static void test_escapes(void)
  52. {
  53. int r = parse("[\"q\\\"b\\\\s\\/n\\n\\t\", \"\\u00e9\\u20ac\","
  54. " \"\\ud83d\\ude00\", \"\\ud800x\", \"caf\xc3\xa9\"]");
  55. int it;
  56. CHECK(r == 0);
  57. it = ujson_first(&j, r);
  58. CHECK(strcmp(ujson_str(&j, it), "q\"b\\s/n\n\t") == 0);
  59. it = ujson_next(&j, it);
  60. CHECK(strcmp(ujson_str(&j, it), "\xc3\xa9\xe2\x82\xac") == 0);
  61. it = ujson_next(&j, it);
  62. CHECK(strcmp(ujson_str(&j, it), "\xf0\x9f\x98\x80") == 0);
  63. it = ujson_next(&j, it);
  64. /* lone high surrogate -> U+FFFD */
  65. CHECK(strcmp(ujson_str(&j, it), "\xef\xbf\xbdx") == 0);
  66. it = ujson_next(&j, it);
  67. CHECK(strcmp(ujson_str(&j, it), "caf\xc3\xa9") == 0);
  68. }
  69. static void test_errors(void)
  70. {
  71. static const char *bad[] = {
  72. "", "{", "[1,]", "{\"a\" 1}", "{\"a\":}", "[1 2]", "\"abc",
  73. "tru", "{\"a\":1}x", "[\"\\x\"]", "[\"a\nb\"]", "-", "{1:2}",
  74. "[\"\\u12\"]",
  75. };
  76. unsigned int i;
  77. for (i = 0; i < sizeof(bad) / sizeof(bad[0]); i++) {
  78. int r = parse(bad[i]);
  79. if (r != -1)
  80. printf("FAIL: accepted bad JSON '%s'\n", bad[i]);
  81. CHECK(r == -1);
  82. CHECK(j.error != 0);
  83. }
  84. }
  85. static void test_depth_and_pool(void)
  86. {
  87. char deep[256];
  88. struct ujson_node few[3];
  89. char small[8];
  90. int i;
  91. for (i = 0; i < 100; i++)
  92. deep[i] = '[';
  93. for (i = 0; i < 100; i++)
  94. deep[100 + i] = ']';
  95. deep[200] = '\0';
  96. CHECK(parse(deep) == -1);
  97. ujson_init(&j, few, 3, sbuf, sizeof(sbuf));
  98. CHECK(ujson_parse(&j, "[1,2,3]", 7) == -1);
  99. ujson_init(&j, nodes, 64, small, sizeof(small));
  100. CHECK(ujson_parse(&j, "\"0123456789\"", 12) == -1);
  101. }
  102. static void test_api_shape(void)
  103. {
  104. /* A Chat /api/channels/:id/messages page (trimmed). */
  105. const char *s =
  106. "[{\"id\":\"m2\",\"clientId\":null,\"channelId\":\"c1\","
  107. "\"userId\":\"u1\",\"authorName\":\"Alice\",\"body\":\"Hi \\ud83d\\udc4b\","
  108. "\"type\":null,\"replyTo\":{\"id\":\"m1\",\"authorName\":\"Bob\","
  109. "\"body\":\"earlier\"},\"createdAt\":\"2026-11-09T10:42:00.000Z\","
  110. "\"deleted\":false,\"reactions\":[{\"emoji\":\"+1\",\"count\":2}],"
  111. "\"attachments\":[{\"filename\":\"notes.txt\",\"size\":12}],"
  112. "\"threadCount\":2,\"readBy\":[]},"
  113. "{\"id\":\"m1\",\"userId\":\"u2\",\"authorName\":\"Bob\","
  114. "\"body\":\"earlier\",\"createdAt\":\"2026-11-09T10:41:00.000Z\","
  115. "\"deleted\":false,\"threadCount\":0}]";
  116. int r = parse(s), m, rt, at;
  117. CHECK(r == 0);
  118. CHECK(ujson_count(&j, r) == 2);
  119. m = ujson_first(&j, r);
  120. CHECK(strcmp(ujson_get_str(&j, m, "authorName"), "Alice") == 0);
  121. CHECK(strcmp(ujson_get_str(&j, m, "body"), "Hi \xf0\x9f\x91\x8b") == 0);
  122. CHECK(ujson_type(&j, ujson_get(&j, m, "clientId")) == UJSON_NULL);
  123. rt = ujson_get(&j, m, "replyTo");
  124. CHECK(strcmp(ujson_get_str(&j, rt, "authorName"), "Bob") == 0);
  125. at = ujson_first(&j, ujson_get(&j, m, "attachments"));
  126. CHECK(strcmp(ujson_get_str(&j, at, "filename"), "notes.txt") == 0);
  127. CHECK(ujson_int(&j, ujson_get(&j, m, "threadCount"), 0) == 2);
  128. m = ujson_next(&j, m);
  129. CHECK(strcmp(ujson_get_str(&j, m, "id"), "m1") == 0);
  130. }
  131. static void test_escape_out(void)
  132. {
  133. char out[64];
  134. CHECK(ujson_escape(out, sizeof(out), "a\"b\\c\nd\x01") > 0);
  135. CHECK(strcmp(out, "\"a\\\"b\\\\c\\nd\\u0001\"") == 0);
  136. CHECK(ujson_escape(out, 4, "abcdef") == -1);
  137. /* round trip */
  138. CHECK(ujson_escape(out, sizeof(out), "p@ss\"w\\rd") > 0);
  139. CHECK(parse(out) == 0);
  140. CHECK(strcmp(ujson_str(&j, 0), "p@ss\"w\\rd") == 0);
  141. }
  142. int main(void)
  143. {
  144. test_basic();
  145. test_escapes();
  146. test_errors();
  147. test_depth_and_pool();
  148. test_api_shape();
  149. test_escape_out();
  150. if (failures) {
  151. printf("ujson_test: %d failure(s)\n", failures);
  152. return 1;
  153. }
  154. printf("ujson_test: all tests passed\n");
  155. return 0;
  156. }