summaryrefslogtreecommitdiff
path: root/test/automated/xml-parse-tests.el
blob: 95eb2865afc042f3b891857a7680ed4519fb11ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
;;; xml-parse-tests.el --- Test suite for XML parsing.

;; Copyright (C) 2012-2015 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 '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¿>")
  "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.
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:"))))))))))

;; Local Variables:
;; no-byte-compile: t
;; End:

;;; xml-parse-tests.el ends here.