From 994ce51b28384bb2ea7a88248a105fcdc7c53a7b Mon Sep 17 00:00:00 2001 From: Philipp Stephani Date: Tue, 19 Dec 2017 00:07:45 +0100 Subject: [PATCH] JSON: Add tests for Unicode edge cases * test/src/json-tests.el (json-serialize/string): Add test for serializing the null character. (json-parse-string/null): Add test for parsing the null character. (json-serialize/invalid-unicode): Add tests for invalid Unicode strings. (json-serialize/roundtrip): Add Unicode noncharacter, non-BMP characters, and syntactic characters. --- test/src/json-tests.el | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/test/src/json-tests.el b/test/src/json-tests.el index 07eb41d0930..551f8ac5fe4 100644 --- a/test/src/json-tests.el +++ b/test/src/json-tests.el @@ -28,8 +28,10 @@ (ert-deftest json-serialize/roundtrip () (skip-unless (fboundp 'json-serialize)) - (let ((lisp [:null :false t 0 123 -456 3.75 "abcαβγ"]) - (json "[null,false,true,0,123,-456,3.75,\"abcαβγ\"]")) + ;; The noncharacter U+FFFF should be passed through, + ;; cf. https://www.unicode.org/faq/private_use.html#noncharacters. + (let ((lisp [:null :false t 0 123 -456 3.75 "abc\uFFFFαβγ𝔸𝐁𝖢\"\\"]) + (json "[null,false,true,0,123,-456,3.75,\"abc\uFFFFαβγ𝔸𝐁𝖢\\\"\\\\\"]")) (should (equal (json-serialize lisp) json)) (with-temp-buffer (json-insert lisp) @@ -75,7 +77,22 @@ (should (equal (json-serialize ["foo"]) "[\"foo\"]")) (should (equal (json-serialize ["a\n\fb"]) "[\"a\\n\\fb\"]")) (should (equal (json-serialize ["\nasdфыв\u001f\u007ffgh\t"]) - "[\"\\nasdфыв\\u001F\u007ffgh\\t\"]"))) + "[\"\\nasdфыв\\u001F\u007ffgh\\t\"]")) + (should (equal (json-serialize ["a\0b"]) "[\"a\\u0000b\"]"))) + +(ert-deftest json-serialize/invalid-unicode () + (skip-unless (fboundp 'json-serialize)) + ;; FIXME: "out of memory" is the wrong error signal, but we don't + ;; currently distinguish between error types when serializing. + (should-error (json-serialize ["a\uDBBBb"]) :type 'json-out-of-memory) + (should-error (json-serialize (vector (string ?a #x110000 ?b))) + :type 'json-out-of-memory) + (should-error (json-serialize ["a\xCCb"] :type 'json-out-of-memory))) + +(ert-deftest json-parse-string/null () + (skip-unless (fboundp 'json-parse-string)) + ;; FIXME: Reconsider whether this is the right behavior. + (should-error (json-parse-string "[a\\u0000b]") :type 'json-parse-error)) (ert-deftest json-parse-string/incomplete () (skip-unless (fboundp 'json-parse-string)) -- 2.39.2