2012-07-01 15:17:05 +08:00
|
|
|
;;; xml-parse-tests.el --- Test suite for XML parsing.
|
|
|
|
|
|
|
|
;; Copyright (C) 2012 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
;; Author: Chong Yidong <cyd@stupidchicken.com>
|
|
|
|
;; Keywords: internal
|
|
|
|
;; Human-Keywords: internal
|
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Type M-x test-xml-parse RET to generate the test buffer.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'xml)
|
|
|
|
|
|
|
|
(defvar xml-parse-tests--data
|
|
|
|
'(;; General entity substitution
|
|
|
|
("<?xml version=\"1.0\"?><!DOCTYPE foo SYSTEM \"bar.dtd\" [<!ENTITY ent \"AbC\">]><foo a=\"b\"><bar>&ent;;</bar></foo>" .
|
|
|
|
((foo ((a . "b")) (bar nil "AbC;"))))
|
2012-07-03 00:21:54 +08:00
|
|
|
("<?xml version=\"1.0\"?><foo>&amp;&apos;'<>"</foo>" .
|
|
|
|
((foo () "&''<>\"")))
|
2012-07-01 15:17:05 +08:00
|
|
|
;; Parameter entity substitution
|
|
|
|
("<?xml version=\"1.0\"?><!DOCTYPE foo SYSTEM \"bar.dtd\" [<!ENTITY % pent \"AbC\"><!ENTITY ent \"%pent;\">]><foo a=\"b\"><bar>&ent;;</bar></foo>" .
|
|
|
|
((foo ((a . "b")) (bar nil "AbC;"))))
|
|
|
|
;; Tricky parameter entity substitution (like XML spec Appendix D)
|
|
|
|
("<?xml version='1.0'?><!DOCTYPE foo [ <!ENTITY % xx '%zz;'><!ENTITY % zz '<!ENTITY ent \"b\" >' > %xx; ]><foo>A&ent;C</foo>" .
|
2012-07-03 00:21:54 +08:00
|
|
|
((foo () "AbC")))
|
2012-07-01 19:05:17 +08:00
|
|
|
;; Bug#7172
|
|
|
|
("<?xml version=\"1.0\"?><!DOCTYPE foo [ <!ELEMENT EXAM_PLE EMPTY> ]><foo></foo>" .
|
2012-07-03 00:21:54 +08:00
|
|
|
((foo ())))
|
|
|
|
;; Entities referencing entities, in character data
|
|
|
|
("<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">]><foo>&abc;</foo>" .
|
|
|
|
((foo () "aBc")))
|
|
|
|
;; Entities referencing entities, in attribute values
|
|
|
|
("<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">]><foo a=\"-&abc;-\">1</foo>" .
|
|
|
|
((foo ((a . "-aBc-")) "1")))
|
|
|
|
;; Character references must be treated as character data
|
|
|
|
("<foo>AT&T;</foo>" . ((foo () "AT&T;")))
|
|
|
|
("<foo>&amp;</foo>" . ((foo () "&"))))
|
2012-07-01 15:17:05 +08:00
|
|
|
"Alist of XML strings and their expected parse trees.")
|
|
|
|
|
2012-07-03 13:28:42 +08:00
|
|
|
(defvar xml-parse-tests--bad-data
|
|
|
|
'(;; XML bomb in content
|
|
|
|
"<!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>"
|
|
|
|
;; XML bomb in attribute value
|
|
|
|
"<!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>"
|
|
|
|
;; Non-terminating DTD
|
|
|
|
"<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">"
|
|
|
|
"<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">asdf"
|
|
|
|
"<!DOCTYPE foo [ <!ENTITY b \"B\"><!ENTITY abc \"a&b;c\">asdf&abc;")
|
|
|
|
"List of XML strings that should signal an error in the parser")
|
|
|
|
|
2012-07-01 15:17:05 +08:00
|
|
|
(ert-deftest xml-parse-tests ()
|
|
|
|
"Test XML parsing."
|
|
|
|
(with-temp-buffer
|
|
|
|
(dolist (test xml-parse-tests--data)
|
|
|
|
(erase-buffer)
|
|
|
|
(insert (car test))
|
2012-07-03 13:28:42 +08:00
|
|
|
(should (equal (cdr test) (xml-parse-region))))
|
|
|
|
(let ((xml-entity-expansion-limit 50))
|
|
|
|
(dolist (test xml-parse-tests--bad-data)
|
|
|
|
(erase-buffer)
|
|
|
|
(insert test)
|
|
|
|
(should-error (xml-parse-region))))))
|
2012-07-01 15:17:05 +08:00
|
|
|
|
|
|
|
;; Local Variables:
|
|
|
|
;; no-byte-compile: t
|
|
|
|
;; End:
|
|
|
|
|
|
|
|
;;; xml-parse-tests.el ends here.
|