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>
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/>.
26 ;; Type M-x test-xml-parse RET to generate the test buffer.
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;&apos;'<>"</foo>" .
38 ((foo () "&''<>\"")))
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 '%zz;'><!ENTITY % zz '<!ENTITY ent \"b\" >' > %xx; ]><foo>A&ent;C</foo>" .
46 ("<?xml version=\"1.0\"?><!DOCTYPE foo [ <!ELEMENT EXAM_PLE EMPTY> ]><foo></foo>" .
48 ;; Entities referencing entities, in character data
49 ("<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">]><foo>&abc;</foo>" .
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&T;</foo>" .
((foo () "AT&T;")))
56 ("<foo>&amp;</foo>" .
((foo () "&")))
57 ("<foo>&amp;</foo>" .
((foo () "&")))
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;"
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
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
()
105 (dolist (test xml-parse-tests--data
)
108 (should (equal (cdr test
) (xml-parse-region))))
109 (let ((xml-entity-expansion-limit 50))
110 (dolist (test xml-parse-tests--bad-data
)
113 (should-error (xml-parse-region))))
114 (let ((testdata (car xml-parse-tests--qnames
)))
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
)))
123 (insert (car testdata
))
124 ;; Provide additional namespace-URI mapping
125 (should (equal (nth 1 testdata
)
128 (append xml-default-ns
129 '(("F" .
"FOOBAR:"))))))
130 (should (equal (nth 2 testdata
)
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>"
142 ((("http://www.w3.org/2000/xmlns/" .
"")
144 (myns:whatever nil
)))))
146 (ert-deftest xml-parse-test-default-namespace-qnames
()
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>"
156 ((("http://www.w3.org/2000/xmlns/" .
"a")
159 ((myns:b .
"c")))))))
161 (ert-deftest xml-parse-namespace-attribute-qnames
()
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
)))))
168 ;; no-byte-compile: t
171 ;;; xml-parse-tests.el ends here.