; Merge from origin/emacs-26
[emacs.git] / test / lisp / xml-tests.el
blob016ba9a2e2ccc654deeab82871a8aeb8485ef55e
1 ;;; xml-parse-tests.el --- Test suite for XML parsing.
3 ;; Copyright (C) 2012-2018 Free Software Foundation, Inc.
5 ;; Author: Chong Yidong <cyd@stupidchicken.com>
6 ;; Keywords: internal
7 ;; Human-Keywords: internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Type M-x test-xml-parse RET to generate the test buffer.
28 ;;; Code:
30 (require 'ert)
31 (require 'xml)
33 (defvar xml-parse-tests--data
34 `(;; General entity substitution
35 ("<?xml version=\"1.0\"?><!DOCTYPE foo SYSTEM \"bar.dtd\" [<!ENTITY ent \"AbC\">]><foo a=\"b\"><bar>&ent;;</bar></foo>" .
36 ((foo ((a . "b")) (bar nil "AbC;"))))
37 ("<?xml version=\"1.0\"?><foo>&amp;amp;&#x26;apos;&apos;&lt;&gt;&quot;</foo>" .
38 ((foo () "&amp;&apos;'<>\"")))
39 ;; Parameter entity substitution
40 ("<?xml version=\"1.0\"?><!DOCTYPE foo SYSTEM \"bar.dtd\" [<!ENTITY % pent \"AbC\"><!ENTITY ent \"%pent;\">]><foo a=\"b\"><bar>&ent;;</bar></foo>" .
41 ((foo ((a . "b")) (bar nil "AbC;"))))
42 ;; Tricky parameter entity substitution (like XML spec Appendix D)
43 ("<?xml version='1.0'?><!DOCTYPE foo [ <!ENTITY % xx '&#37;zz;'><!ENTITY % zz '&#60;!ENTITY ent \"b\" >' > %xx; ]><foo>A&ent;C</foo>" .
44 ((foo () "AbC")))
45 ;; Bug#7172
46 ("<?xml version=\"1.0\"?><!DOCTYPE foo [ <!ELEMENT EXAM_PLE EMPTY> ]><foo></foo>" .
47 ((foo ())))
48 ;; Entities referencing entities, in character data
49 ("<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">]><foo>&abc;</foo>" .
50 ((foo () "aBc")))
51 ;; Entities referencing entities, in attribute values
52 ("<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">]><foo a=\"-&abc;-\">1</foo>" .
53 ((foo ((a . "-aBc-")) "1")))
54 ;; Character references must be treated as character data
55 ("<foo>AT&amp;T;</foo>" . ((foo () "AT&T;")))
56 ("<foo>&#38;amp;</foo>" . ((foo () "&amp;")))
57 ("<foo>&#x26;amp;</foo>" . ((foo () "&amp;")))
58 ;; Unusual but valid XML names [5]
59 ("<ÀÖØö.3·-‿⁀󯿿>abc</ÀÖØö.3·-‿⁀󯿿>" . ((,(intern "ÀÖØö.3·-‿⁀󯿿") () "abc")))
60 ("<:>abc</:>" . ((,(intern ":") () "abc"))))
61 "Alist of XML strings and their expected parse trees.")
63 (defvar xml-parse-tests--bad-data
64 '(;; XML bomb in content
65 "<!DOCTYPE foo [<!ENTITY lol \"lol\"><!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\"><!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">]><foo>&lol2;</foo>"
66 ;; XML bomb in attribute value
67 "<!DOCTYPE foo [<!ENTITY lol \"lol\"><!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\"><!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">]><foo a=\"&lol2;\">!</foo>"
68 ;; Non-terminating DTD
69 "<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">"
70 "<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">asdf"
71 "<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">asdf&abc;"
72 ;; Invalid XML names
73 "<0foo>abc</0foo>"
74 "<‿foo>abc</‿foo>"
75 "<f¿>abc</f¿>"
76 ;; Two root tags
77 "<a/><b></b>"
78 ;; Bug#16344
79 "<!----><x>< /x>"
80 "<a>< b/></a>")
81 "List of XML strings that should signal an error in the parser")
83 (defvar xml-parse-tests--qnames
84 '( ;; Test data for name expansion
85 ("<?xml version=\"1.0\" encoding=\"UTF-8\"?><D:multistatus xmlns:D=\"DAV:\"><D:response><D:href>/calendar/events/</D:href><D:propstat><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response></D:multistatus>"
86 ;; Result with qnames as cons
87 ((("DAV:" . "multistatus")
88 ((("http://www.w3.org/2000/xmlns/" . "D") . "DAV:"))
89 (("DAV:" . "response") nil (("DAV:" . "href") nil "/calendar/events/")
90 (("DAV:" . "propstat") nil (("DAV:" . "status") nil "HTTP/1.1 200 OK")))))
91 ;; Result with qnames as symbols
92 ((DAV:multistatus
93 ((("http://www.w3.org/2000/xmlns/" . "D") . "DAV:"))
94 (DAV:response nil (DAV:href nil "/calendar/events/")
95 (DAV:propstat nil (DAV:status nil "HTTP/1.1 200 OK"))))))
96 ("<?xml version=\"1.0\" encoding=\"UTF-8\"?><F:something>hi there</F:something>"
97 ((("FOOBAR:" . "something") nil "hi there"))
98 ((FOOBAR:something nil "hi there"))))
99 "List of strings which are parsed using namespace expansion.
100 Parser is called with and without 'symbol-qnames argument.")
102 (ert-deftest xml-parse-tests ()
103 "Test XML parsing."
104 (with-temp-buffer
105 (dolist (test xml-parse-tests--data)
106 (erase-buffer)
107 (insert (car test))
108 (should (equal (cdr test) (xml-parse-region))))
109 (let ((xml-entity-expansion-limit 50))
110 (dolist (test xml-parse-tests--bad-data)
111 (erase-buffer)
112 (insert test)
113 (should-error (xml-parse-region))))
114 (let ((testdata (car xml-parse-tests--qnames)))
115 (erase-buffer)
116 (insert (car testdata))
117 (should (equal (nth 1 testdata)
118 (xml-parse-region nil nil nil nil t)))
119 (should (equal (nth 2 testdata)
120 (xml-parse-region nil nil nil nil 'symbol-qnames))))
121 (let ((testdata (nth 1 xml-parse-tests--qnames)))
122 (erase-buffer)
123 (insert (car testdata))
124 ;; Provide additional namespace-URI mapping
125 (should (equal (nth 1 testdata)
126 (xml-parse-region
127 nil nil nil nil
128 (append xml-default-ns
129 '(("F" . "FOOBAR:"))))))
130 (should (equal (nth 2 testdata)
131 (xml-parse-region
132 nil nil nil nil
133 (cons 'symbol-qnames
134 (append xml-default-ns
135 '(("F" . "FOOBAR:"))))))))))
137 ;; Test bug #23440 (proper expansion of default namespace)
138 ; Test data for default namespace
139 (defvar xml-parse-test--default-namespace-qnames
140 (cons "<something xmlns=\"myns:\"><whatever></whatever></something>"
141 '((myns:something
142 ((("http://www.w3.org/2000/xmlns/" . "")
143 . "myns:"))
144 (myns:whatever nil)))))
146 (ert-deftest xml-parse-test-default-namespace-qnames ()
147 (with-temp-buffer
148 (insert (car xml-parse-test--default-namespace-qnames))
149 (should (equal (cdr xml-parse-test--default-namespace-qnames)
150 (xml-parse-region nil nil nil nil 'symbol-qnames)))))
152 ;; Test bug #26533 (proper expansion in prefixed attributes with 'symbol-qnames)
153 (defvar xml-parse-test--namespace-attribute-qnames
154 (cons "<something xmlns:a=\"myns:\"><whatever a:b='c'></whatever></something>"
155 '((something
156 ((("http://www.w3.org/2000/xmlns/" . "a")
157 . "myns:"))
158 (whatever
159 ((myns:b . "c")))))))
161 (ert-deftest xml-parse-namespace-attribute-qnames ()
162 (with-temp-buffer
163 (insert (car xml-parse-test--namespace-attribute-qnames))
164 (should (equal (cdr xml-parse-test--namespace-attribute-qnames)
165 (xml-parse-region nil nil nil nil 'symbol-qnames)))))
167 ;; Local Variables:
168 ;; no-byte-compile: t
169 ;; End:
171 ;;; xml-parse-tests.el ends here.