1 ;;; xml-parse-tests.el --- Test suite for XML parsing.
3 ;; Copyright (C) 2012-2014 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 <http://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;"
76 "List of XML strings that should signal an error in the parser")
78 (defvar xml-parse-tests--qnames
79 '( ;; Test data for name expansion
80 ("<?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>"
81 ;; Result with qnames as cons
82 ((("DAV:" .
"multistatus")
83 ((("http://www.w3.org/2000/xmlns/" .
"D") .
"DAV:"))
84 (("DAV:" .
"response") nil
(("DAV:" .
"href") nil
"/calendar/events/")
85 (("DAV:" .
"propstat") nil
(("DAV:" .
"status") nil
"HTTP/1.1 200 OK")))))
86 ;; Result with qnames as symbols
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 ("<?xml version=\"1.0\" encoding=\"UTF-8\"?><F:something>hi there</F:something>"
92 ((("FOOBAR:" .
"something") nil
"hi there"))
93 ((FOOBAR:something nil
"hi there"))))
94 "List of strings which are parsed using namespace expansion.
95 Parser is called with and without 'symbol-qnames argument.")
97 (ert-deftest xml-parse-tests
()
100 (dolist (test xml-parse-tests--data
)
103 (should (equal (cdr test
) (xml-parse-region))))
104 (let ((xml-entity-expansion-limit 50))
105 (dolist (test xml-parse-tests--bad-data
)
108 (should-error (xml-parse-region))))
109 (let ((testdata (car xml-parse-tests--qnames
)))
111 (insert (car testdata
))
112 (should (equal (nth 1 testdata
)
113 (xml-parse-region nil nil nil nil t
)))
114 (should (equal (nth 2 testdata
)
115 (xml-parse-region nil nil nil nil
'symbol-qnames
))))
116 (let ((testdata (nth 1 xml-parse-tests--qnames
)))
118 (insert (car testdata
))
119 ;; Provide additional namespace-URI mapping
120 (should (equal (nth 1 testdata
)
123 (append xml-default-ns
124 '(("F" .
"FOOBAR:"))))))
125 (should (equal (nth 2 testdata
)
129 (append xml-default-ns
130 '(("F" .
"FOOBAR:"))))))))))
133 ;; no-byte-compile: t
136 ;;; xml-parse-tests.el ends here.