emacs/test/lisp/xml-tests.el

199 lines
8.5 KiB
EmacsLisp
Raw Normal View History

;;; xml-tests.el --- Test suite for XML parsing. -*- lexical-binding:t -*-
2022-01-01 02:45:51 -05:00
;; Copyright (C) 2012-2022 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Type M-x test-xml-parse RET to generate the test buffer.
;;; Code:
(require 'ert)
(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;"))))
("<?xml version=\"1.0\"?><foo>&amp;amp;&#x26;apos;&apos;&lt;&gt;&quot;</foo>" .
((foo () "&amp;&apos;'<>\"")))
;; 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 '&#37;zz;'><!ENTITY % zz '&#60;!ENTITY ent \"b\" >' > %xx; ]><foo>A&ent;C</foo>" .
((foo () "AbC")))
;; Bug#7172
("<?xml version=\"1.0\"?><!DOCTYPE foo [ <!ELEMENT EXAM_PLE EMPTY> ]><foo></foo>" .
((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&amp;T;</foo>" . ((foo () "AT&T;")))
("<foo>&#38;amp;</foo>" . ((foo () "&amp;")))
("<foo>&#x26;amp;</foo>" . ((foo () "&amp;")))
;; Unusual but valid XML names [5]
("<ÀÖØö.3·-‿⁀󯿿>abc</ÀÖØö.3·-‿⁀󯿿>" . ((,(intern "ÀÖØö.3·-‿⁀󯿿") () "abc")))
("<:>abc</:>" . ((,(intern ":") () "abc"))))
"Alist of XML strings and their expected parse trees.")
(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;"
;; Invalid XML names
"<0foo>abc</0foo>"
"<‿foo>abc</‿foo>"
"<f¿>abc</f¿>"
;; Two root tags
"<a/><b></b>"
;; Bug#16344
"<!----><x>< /x>"
"<a>< b/></a>")
"List of XML strings that should signal an error in the parser.")
(defvar xml-parse-tests--qnames
'( ;; Test data for name expansion
("<?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>"
;; Result with qnames as cons
((("DAV:" . "multistatus")
((("http://www.w3.org/2000/xmlns/" . "D") . "DAV:"))
(("DAV:" . "response") nil (("DAV:" . "href") nil "/calendar/events/")
(("DAV:" . "propstat") nil (("DAV:" . "status") nil "HTTP/1.1 200 OK")))))
;; Result with qnames as symbols
((DAV:multistatus
((("http://www.w3.org/2000/xmlns/" . "D") . "DAV:"))
(DAV:response nil (DAV:href nil "/calendar/events/")
(DAV:propstat nil (DAV:status nil "HTTP/1.1 200 OK"))))))
("<?xml version=\"1.0\" encoding=\"UTF-8\"?><F:something>hi there</F:something>"
((("FOOBAR:" . "something") nil "hi there"))
((FOOBAR:something nil "hi there"))))
"List of strings which are parsed using namespace expansion.
Audit quoting the quote character in doc strings * test/src/regex-emacs-tests.el (regex-tests-compare): (regex-tests-compare): (regex-tests-match): * test/lisp/xml-tests.el (xml-parse-tests--qnames): * test/lisp/mh-e/mh-thread-tests.el (mh-thread-tests-before-from): * test/lisp/cedet/srecode-utest-template.el (srecode-utest-map-reset): * test/lisp/calc/calc-tests.el (calc-tests-equal): * lisp/window.el (get-lru-window): (get-mru-window): (get-largest-window): (quit-restore-window): (display-buffer): * lisp/vc/vc-rcs.el (vc-rcs-consult-headers): * lisp/url/url-auth.el (url-digest-auth-build-response): * lisp/tutorial.el (tutorial--find-changed-keys): * lisp/transient.el (transient-suffix-object): * lisp/textmodes/rst.el (rst-insert-list-new-item): * lisp/textmodes/bibtex.el (bibtex-clean-entry): * lisp/tab-bar.el (tab-bar--key-to-number): (toggle-frame-tab-bar): * lisp/ses.el (ses-recalculate-cell): (ses-define-local-printer): (ses-prin1): * lisp/progmodes/xref.el (xref--find-ignores-arguments): * lisp/progmodes/verilog-mode.el (verilog-single-declaration-end): * lisp/progmodes/tcl.el (tcl-mode-hook): * lisp/progmodes/gdb-mi.el (gdb-get-buffer-create): * lisp/progmodes/elisp-mode.el (elisp--xref-make-xref): * lisp/play/dunnet.el (dun-room-objects): * lisp/outline.el (outline--cycle-state): * lisp/org/ox-publish.el (org-publish-find-property): * lisp/org/ox-html.el (org-html--unlabel-latex-environment): * lisp/org/org-table.el (org-table-collapse-header): * lisp/org/org-plot.el (org--plot/prime-factors): * lisp/org/org-agenda.el (org-agenda--mark-blocked-entry): (org-agenda-set-restriction-lock): * lisp/org/ob-lua.el (org-babel-lua-read-string): * lisp/org/ob-julia.el (org-babel-julia-evaluate-external-process): (org-babel-julia-evaluate-session): * lisp/org/ob-core.el (org-babel-default-header-args): * lisp/obsolete/mouse-sel.el (mouse-select): (mouse-select-secondary): * lisp/net/tramp.el (tramp-methods): * lisp/net/eww.el (eww-accept-content-types): * lisp/net/dictionary-connection.el (dictionary-connection-status): * lisp/minibuffer.el (completion-flex--make-flex-pattern): * lisp/mh-e/mh-mime.el (mh-have-file-command): * lisp/mh-e/mh-limit.el (mh-subject-to-sequence): (mh-subject-to-sequence-threaded): (mh-subject-to-sequence-unthreaded): * lisp/mail/feedmail.el (feedmail-queue-buffer-file-name): (feedmail-vm-mail-mode): * lisp/ls-lisp.el (ls-lisp--sanitize-switches): * lisp/keymap.el (key-valid-p): * lisp/international/ccl.el (ccl-compile-branch-blocks): * lisp/image/image-converter.el (image-convert): * lisp/gnus/spam.el (spam-backend-check): * lisp/gnus/nnselect.el (nnselect-generate-artlist): * lisp/gnus/nnmairix.el (nnmairix-widget-other): * lisp/gnus/message.el (message-mailto): * lisp/gnus/gnus-sum.el (gnus-collect-urls-from-article): * lisp/gnus/gnus-search.el (gnus-search-prepare-query): * lisp/frame.el (frame-size-history): * lisp/eshell/esh-var.el (eshell-parse-variable-ref): * lisp/eshell/em-dirs.el (eshell-expand-multiple-dots): * lisp/erc/erc-backend.el (erc-bounds-of-word-at-point): * lisp/emulation/cua-rect.el (cua--rectangle-operation): * lisp/emacs-lisp/text-property-search.el (text-property-search-forward): * lisp/emacs-lisp/package.el (package-desc-suffix): * lisp/emacs-lisp/faceup.el (faceup-test-explain): * lisp/emacs-lisp/comp.el (comp-curr-allocation-class): (comp-alloc-class-to-container): (comp-add-cstrs): (comp-remove-type-hints-func): (batch-byte+native-compile): * lisp/emacs-lisp/cl-macs.el (cl--optimize): * lisp/elec-pair.el (electric-pair--syntax-ppss): * lisp/doc-view.el (doc-view-doc-type): * lisp/cedet/semantic/symref.el (semantic-symref-tool-alist): (semantic-symref-hit-to-tag-via-db): (semantic-symref-hit-to-tag-via-buffer): * lisp/cedet/semantic/lex-spp.el (semantic-lex-spp-get-overlay): * lisp/cedet/semantic/java.el (semantic-java-doc-keywords-map): * lisp/cedet/semantic/find.el (semantic-brute-find-tag-by-function): * lisp/cedet/semantic/db.el (semanticdb-project-predicate-functions): * lisp/cedet/semantic.el (semantic-working-type): * lisp/cedet/ede/files.el (ede-flush-directory-hash): * lisp/calc/calc.el (calc--header-line): * lisp/auth-source.el (auth-source-pick-first-password): (auth-source--decode-octal-string): * etc/themes/modus-themes.el (modus-themes--paren): (modus-themes--agenda-habit): * admin/cus-test.el (cus-test-vars-with-changed-state): Fix quoting in doc strings. In code examples, the ' character is quoted with \\=, and regularize 'foo to `foo', and quote strings like "foo" instead of 'foo'.
2022-04-22 16:17:22 +02:00
Parser is called with and without `symbol-qnames' argument.")
(ert-deftest xml-parse-tests ()
"Test XML parsing."
(with-temp-buffer
(dolist (test xml-parse-tests--data)
(erase-buffer)
(insert (car test))
(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))))
(let ((testdata (car xml-parse-tests--qnames)))
(erase-buffer)
(insert (car testdata))
(should (equal (nth 1 testdata)
(xml-parse-region nil nil nil nil t)))
(should (equal (nth 2 testdata)
(xml-parse-region nil nil nil nil 'symbol-qnames))))
(let ((testdata (nth 1 xml-parse-tests--qnames)))
(erase-buffer)
(insert (car testdata))
;; Provide additional namespace-URI mapping
(should (equal (nth 1 testdata)
(xml-parse-region
nil nil nil nil
(append xml-default-ns
'(("F" . "FOOBAR:"))))))
(should (equal (nth 2 testdata)
(xml-parse-region
nil nil nil nil
(cons 'symbol-qnames
(append xml-default-ns
'(("F" . "FOOBAR:"))))))))))
;; Test bug #23440 (proper expansion of default namespace)
; Test data for default namespace
(defvar xml-parse-test--default-namespace-qnames
(cons "<something xmlns=\"myns:\"><whatever></whatever></something>"
'((myns:something
((("http://www.w3.org/2000/xmlns/" . "")
. "myns:"))
(myns:whatever nil)))))
(ert-deftest xml-parse-test-default-namespace-qnames ()
(with-temp-buffer
(insert (car xml-parse-test--default-namespace-qnames))
(should (equal (cdr xml-parse-test--default-namespace-qnames)
(xml-parse-region nil nil nil nil 'symbol-qnames)))))
;; Test bug #26533 (proper expansion in prefixed attributes with 'symbol-qnames)
(defvar xml-parse-test--namespace-attribute-qnames
(cons "<something xmlns:a=\"myns:\"><whatever a:b='c'></whatever></something>"
'((something
((("http://www.w3.org/2000/xmlns/" . "a")
. "myns:"))
(whatever
((myns:b . "c")))))))
(ert-deftest xml-parse-namespace-attribute-qnames ()
(with-temp-buffer
(insert (car xml-parse-test--namespace-attribute-qnames))
(should (equal (cdr xml-parse-test--namespace-attribute-qnames)
(xml-parse-region nil nil nil nil 'symbol-qnames)))))
(ert-deftest xml-print-invalid-cdata ()
"Check that Bug#41094 is fixed."
(with-temp-buffer
(should (equal (should-error (xml-print '((foo () "\0")))
:type 'xml-invalid-character)
'(xml-invalid-character 0 1)))
(should (equal (should-error (xml-print '((foo () "\u00FF \xFF")))
:type 'xml-invalid-character)
'(xml-invalid-character #x3FFFFF 3)))))
(defvar xml-tests--data-with-comments
`(;; simple case
("<?xml version=\"1.0\"?><foo baz=\"true\">bar</foo>"
. ((foo ((baz . "true")) "bar")))
;; toplevel comments -- first document child must not get lost
(,(concat "<?xml version=\"1.0\"?><foo>bar</foo><!--comment-1-->"
"<!--comment-2-->")
. ((foo nil "bar")))
(,(concat "<?xml version=\"1.0\"?><!--comment-a--><foo a=\"b\">"
"<bar>blub</bar></foo><!--comment-b--><!--comment-c-->")
. ((foo ((a . "b")) (bar nil "blub")))))
"Alist of XML strings and their expected parse trees for discarded comments.")
(ert-deftest xml-remove-comments ()
(dolist (test xml-tests--data-with-comments)
(erase-buffer)
(insert (car test))
(xml-remove-comments (point-min) (point-max))
(should (equal (cdr test)
(xml-parse-region (point-min) (point-max))))))
;;; xml-tests.el ends here