]> git.eshelyaron.com Git - emacs.git/commitdiff
JSON: Add tests for Unicode edge cases
authorPhilipp Stephani <phst@google.com>
Mon, 18 Dec 2017 23:07:45 +0000 (00:07 +0100)
committerPhilipp Stephani <phst@google.com>
Mon, 18 Dec 2017 23:10:24 +0000 (00:10 +0100)
* 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

index 07eb41d093079649b98d2f90b4f4155236beb38e..551f8ac5fe4975d0bf309bbd21c9068bbad0b9b8 100644 (file)
 
 (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)
   (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))