tests/test_json.c

changeset 1156
96f16b5a0029
parent 1154
afd12f31d28a
child 1157
3565ae61a5a2
--- a/tests/test_json.c	Sun Jan 26 12:24:49 2025 +0100
+++ b/tests/test_json.c	Sun Jan 26 13:20:05 2025 +0100
@@ -226,6 +226,64 @@
     cxJsonDestroy(&json);
 }
 
+CX_TEST(test_json_escaped_unicode_malformed) {
+    CxJson json;
+    cxJsonInit(&json, NULL);
+    CxJsonValue *obj;
+    CxJsonStatus result;
+    CX_TEST_DO {
+        cxJsonFill(&json, "\"too few \\u123 digits\"");
+        result = cxJsonNext(&json, &obj);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
+        CX_TEST_ASSERT(cxJsonIsString(obj));
+        CX_TEST_ASSERT(0 == cx_strcmp(
+            cxJsonAsCxString(obj),
+            CX_STR("too few \\u123 digits")
+        ));
+        cxJsonFill(&json, "\"too many \\u00E456 digits\"");
+        result = cxJsonNext(&json, &obj);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
+        CX_TEST_ASSERT(cxJsonIsString(obj));
+        CX_TEST_ASSERT(0 == cx_strcmp(
+            cxJsonAsCxString(obj),
+            CX_STR("too many รค56 digits")
+        ));
+        cxJsonFill(&json, "\"only high \\uD800 surrogate\"");
+        result = cxJsonNext(&json, &obj);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
+        CX_TEST_ASSERT(cxJsonIsString(obj));
+        CX_TEST_ASSERT(0 == cx_strcmp(
+            cxJsonAsCxString(obj),
+            CX_STR("only high \\uD800 surrogate")
+        ));
+        cxJsonFill(&json, "\"only low \\uDC00 surrogate\"");
+        result = cxJsonNext(&json, &obj);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
+        CX_TEST_ASSERT(cxJsonIsString(obj));
+        CX_TEST_ASSERT(0 == cx_strcmp(
+            cxJsonAsCxString(obj),
+            CX_STR("only low \\uDC00 surrogate")
+        ));
+        cxJsonFill(&json, "\"two high \\uD800\\uD800 surrogates\"");
+        result = cxJsonNext(&json, &obj);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
+        CX_TEST_ASSERT(cxJsonIsString(obj));
+        CX_TEST_ASSERT(0 == cx_strcmp(
+            cxJsonAsCxString(obj),
+            CX_STR("two high \\uD800\\uD800 surrogates")
+        ));
+        cxJsonFill(&json, "\"high plus bullshit \\uD800\\u567 foo\"");
+        result = cxJsonNext(&json, &obj);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
+        CX_TEST_ASSERT(cxJsonIsString(obj));
+        CX_TEST_ASSERT(0 == cx_strcmp(
+            cxJsonAsCxString(obj),
+            CX_STR("high plus bullshit \\uD800\\u567 foo")
+        ));
+    }
+    cxJsonDestroy(&json);
+}
+
 CX_TEST(test_json_escaped_end_of_string) {
     CxJson json;
     cxJsonInit(&json, NULL);
@@ -1126,6 +1184,7 @@
     cx_test_register(suite, test_json_simple_object);
     cx_test_register(suite, test_json_escaped_strings);
     cx_test_register(suite, test_json_escaped_unicode_strings);
+    cx_test_register(suite, test_json_escaped_unicode_malformed);
     cx_test_register(suite, test_json_escaped_end_of_string);
     cx_test_register(suite, test_json_object_incomplete_token);
     cx_test_register(suite, test_json_token_wrongly_completed);

mercurial