/
test_parser.rb
426 lines (360 loc) · 14.1 KB
/
test_parser.rb
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# frozen_string_literal: true
require "helper"
module Nokogiri
module XML
module SAX
class TestParser < Nokogiri::SAX::TestCase
def setup
super
@parser = XML::SAX::Parser.new(Doc.new)
end
def test_parser_context_yielded_io
doc = Doc.new
parser = XML::SAX::Parser.new(doc)
xml = "<foo a='&b'/>"
block_called = false
parser.parse(StringIO.new(xml)) do |ctx|
block_called = true
ctx.replace_entities = true
end
assert(block_called)
assert_equal([["foo", [["a", "&b"]]]], doc.start_elements)
end
def test_parser_context_yielded_in_memory
doc = Doc.new
parser = XML::SAX::Parser.new(doc)
xml = "<foo a='&b'/>"
block_called = false
parser.parse(xml) do |ctx|
block_called = true
ctx.replace_entities = true
end
assert(block_called)
assert_equal([["foo", [["a", "&b"]]]], doc.start_elements)
end
def test_empty_decl
parser = XML::SAX::Parser.new(Doc.new)
xml = "<root />"
parser.parse(xml)
assert(parser.document.start_document_called, xml)
assert_nil(parser.document.xmldecls, xml)
end
def test_xml_decl
[
['<?xml version="1.0" ?>', ["1.0"]],
['<?xml version="1.0" encoding="UTF-8" ?>', ["1.0", "UTF-8"]],
['<?xml version="1.0" standalone="yes"?>', ["1.0", "yes"]],
['<?xml version="1.0" standalone="no"?>', ["1.0", "no"]],
['<?xml version="1.0" encoding="UTF-8" standalone="no"?>', ["1.0", "UTF-8", "no"]],
['<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>', ["1.0", "ISO-8859-1", "yes"]],
].each do |decl, value|
parser = XML::SAX::Parser.new(Doc.new)
xml = "#{decl}\n<root />"
parser.parse(xml)
assert(parser.document.start_document_called, xml)
assert_equal(value, parser.document.xmldecls, xml)
end
end
def test_parse_empty
assert_raises(RuntimeError) do
@parser.parse("")
end
end
def test_namespace_declaration_order_is_saved
@parser.parse(<<~eoxml)
<root xmlns:foo='http://foo.example.com/' xmlns='http://example.com/'>
<a foo:bar='hello' />
</root>
eoxml
assert_equal(2, @parser.document.start_elements_namespace.length)
el = @parser.document.start_elements_namespace.first
namespaces = el.last
assert_equal(["foo", "http://foo.example.com/"], namespaces.first)
assert_equal([nil, "http://example.com/"], namespaces.last)
end
def test_bad_document_calls_error_handler
@parser.parse("<foo><bar></foo>")
assert(@parser.document.errors)
assert(@parser.document.errors.length > 0)
end
def test_namespace_are_super_fun_to_parse
@parser.parse(<<~eoxml)
<root xmlns:foo='http://foo.example.com/'>
<a foo:bar='hello' />
<b xmlns:foo='http://bar.example.com/'>
<a foo:bar='hello' />
</b>
<foo:bar>hello world</foo:bar>
</root>
eoxml
assert(@parser.document.start_elements_namespace.length > 0)
el = @parser.document.start_elements_namespace[1]
assert_equal("a", el.first)
assert_equal(1, el[1].length)
attribute = el[1].first
assert_equal("bar", attribute.localname)
assert_equal("foo", attribute.prefix)
assert_equal("hello", attribute.value)
assert_equal("http://foo.example.com/", attribute.uri)
end
def test_sax_v1_namespace_attribute_declarations
@parser.parse(<<~eoxml)
<root xmlns:foo='http://foo.example.com/' xmlns='http://example.com/'>
<a foo:bar='hello' />
<b xmlns:foo='http://bar.example.com/'>
<a foo:bar='hello' />
</b>
<foo:bar>hello world</foo:bar>
</root>
eoxml
assert(@parser.document.start_elements.length > 0)
elm = @parser.document.start_elements.first
assert_equal("root", elm.first)
assert(elm[1].include?(["xmlns:foo", "http://foo.example.com/"]))
assert(elm[1].include?(["xmlns", "http://example.com/"]))
end
def test_sax_v1_namespace_nodes
@parser.parse(<<~eoxml)
<root xmlns:foo='http://foo.example.com/' xmlns='http://example.com/'>
<a foo:bar='hello' />
<b xmlns:foo='http://bar.example.com/'>
<a foo:bar='hello' />
</b>
<foo:bar>hello world</foo:bar>
</root>
eoxml
assert_equal(5, @parser.document.start_elements.length)
assert(@parser.document.start_elements.map(&:first).include?("foo:bar"))
assert(@parser.document.end_elements.map(&:first).include?("foo:bar"))
end
def test_start_is_called_without_namespace
@parser.parse(<<~eoxml)
<root xmlns:foo='http://foo.example.com/' xmlns='http://example.com/'>
<foo:f><bar></foo:f>
</root>
eoxml
assert_equal(["root", "foo:f", "bar"],
@parser.document.start_elements.map(&:first))
end
def test_parser_sets_encoding
parser = XML::SAX::Parser.new(Doc.new, "UTF-8")
assert_equal("UTF-8", parser.encoding)
end
def test_errors_set_after_parsing_bad_dom
doc = Nokogiri::XML("<foo><bar></foo>")
assert(doc.errors)
@parser.parse("<foo><bar></foo>")
assert(@parser.document.errors)
assert(@parser.document.errors.length > 0)
doc.errors.each do |error|
assert_equal("UTF-8", error.message.encoding.name)
end
# when using JRuby Nokogiri, more errors will be generated as the DOM
# parser continue to parse an ill formed document, while the sax parser
# will stop at the first error
unless Nokogiri.jruby?
assert_equal(doc.errors.length, @parser.document.errors.length)
end
end
def test_parse_with_memory_argument
@parser.parse(File.read(XML_FILE))
assert(@parser.document.cdata_blocks.length > 0)
end
def test_parse_with_io_argument
File.open(XML_FILE, "rb") do |f|
@parser.parse(f)
end
assert(@parser.document.cdata_blocks.length > 0)
end
def test_parse_io
call_parse_io_with_encoding("UTF-8")
end
# issue #828
def test_parse_io_lower_case_encoding
call_parse_io_with_encoding("utf-8")
end
def call_parse_io_with_encoding(encoding)
File.open(XML_FILE, "rb") do |f|
@parser.parse_io(f, encoding)
end
assert(@parser.document.cdata_blocks.length > 0)
called = false
@parser.document.start_elements.flatten.each do |thing|
assert_equal("UTF-8", thing.encoding.name)
called = true
end
assert(called)
called = false
@parser.document.end_elements.flatten.each do |thing|
assert_equal("UTF-8", thing.encoding.name)
called = true
end
assert(called)
called = false
@parser.document.data.each do |thing|
assert_equal("UTF-8", thing.encoding.name)
called = true
end
assert(called)
called = false
@parser.document.comments.flatten.each do |thing|
assert_equal("UTF-8", thing.encoding.name)
called = true
end
assert(called)
called = false
@parser.document.cdata_blocks.flatten.each do |thing|
assert_equal("UTF-8", thing.encoding.name)
called = true
end
assert(called)
end
def test_parse_file
@parser.parse_file(XML_FILE)
assert_raises(ArgumentError) do
@parser.parse_file(nil)
end
assert_raises(Errno::ENOENT) do
@parser.parse_file("")
end
assert_raises(Errno::EISDIR) do
@parser.parse_file(File.expand_path(File.dirname(__FILE__)))
end
end
def test_render_parse_nil_param
assert_raises(ArgumentError) { @parser.parse_memory(nil) }
end
def test_bad_encoding_args
assert_raises(ArgumentError) { XML::SAX::Parser.new(Doc.new, "not an encoding") }
assert_raises(ArgumentError) { @parser.parse_io(StringIO.new("<root/>"), "not an encoding") }
end
def test_ctag
@parser.parse_memory(<<~eoxml)
<p id="asdfasdf">
<![CDATA[ This is a comment ]]>
Paragraph 1
</p>
eoxml
assert_equal([" This is a comment "], @parser.document.cdata_blocks)
end
def test_comment
@parser.parse_memory(<<~eoxml)
<p id="asdfasdf">
<!-- This is a comment -->
Paragraph 1
</p>
eoxml
assert_equal([" This is a comment "], @parser.document.comments)
end
def test_characters
@parser.parse_memory(<<~eoxml)
<p id="asdfasdf">Paragraph 1</p>
eoxml
assert_equal(["Paragraph 1"], @parser.document.data)
end
def test_end_document
@parser.parse_memory(<<~eoxml)
<p id="asdfasdf">Paragraph 1</p>
eoxml
assert(@parser.document.end_document_called)
end
def test_end_element
@parser.parse_memory(<<~eoxml)
<p id="asdfasdf">Paragraph 1</p>
eoxml
assert_equal([["p"]], @parser.document.end_elements)
end
def test_start_element_attrs
@parser.parse_memory(<<~eoxml)
<p id="asdfasdf">Paragraph 1</p>
eoxml
assert_equal([["p", [["id", "asdfasdf"]]]], @parser.document.start_elements)
end
def test_start_element_attrs_include_namespaces
@parser.parse_memory(<<~eoxml)
<p xmlns:foo='http://foo.example.com/'>Paragraph 1</p>
eoxml
assert_equal([["p", [["xmlns:foo", "http://foo.example.com/"]]]],
@parser.document.start_elements)
end
def test_processing_instruction
@parser.parse_memory(<<~eoxml)
<?xml-stylesheet href="a.xsl" type="text/xsl"?>
<?xml version="1.0"?>
eoxml
assert_equal([["xml-stylesheet", 'href="a.xsl" type="text/xsl"']],
@parser.document.processing_instructions)
end
def test_parse_document
skip("JRuby SAXParser only parses well-formed XML documents") unless Nokogiri.uses_libxml?
@parser.parse_memory(<<~eoxml)
<p>Paragraph 1</p>
<p>Paragraph 2</p>
eoxml
end
def test_parser_attributes
xml = <<~eoxml
<?xml version="1.0" ?><root><foo a="&b" c=">d" /></root>
eoxml
block_called = false
@parser.parse(xml) do |ctx|
block_called = true
ctx.replace_entities = true
end
assert(block_called)
assert_equal([["root", []], ["foo", [["a", "&b"], ["c", ">d"]]]], @parser.document.start_elements)
end
def test_recovery_from_incorrect_xml
xml = <<~eoxml
<?xml version="1.0" ?><Root><Data><?xml version='1.0'?><Item>hey</Item></Data><Data><Item>hey yourself</Item></Data></Root>
eoxml
block_called = false
@parser.parse(xml) do |ctx|
block_called = true
ctx.recovery = true
end
assert(block_called)
assert_equal([["Root", []], ["Data", []], ["Item", []], ["Data", []], ["Item", []]],
@parser.document.start_elements)
end
def test_square_bracket_in_text # issue 1261
xml = <<~eoxml
<tu tuid="87dea04cf60af103ff09d1dba36ae820" segtype="block">
<prop type="x-smartling-string-variant">en:#:home_page:#:stories:#:[6]:#:name</prop>
<tuv xml:lang="en-US"><seg>Sandy S.</seg></tuv>
</tu>
eoxml
@parser.parse(xml)
assert_includes(@parser.document.data, "en:#:home_page:#:stories:#:[6]:#:name")
end
def test_large_cdata_is_handled
# see #2132 and https://gitlab.gnome.org/GNOME/libxml2/-/issues/200
skip("Upstream libxml2 <= 2.9.10 needs to be patched") if Nokogiri::VersionInfo.instance.libxml2_using_system?
template = <<~EOF
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://example.com">
<soapenv:Header>
<AuthHeader xsi:type="ns:vAuthHeader">
<userName xsi:type="xsd:string">gorilla</userName>
<password xsi:type="xsd:string">secret</password>
</AuthHeader>
</soapenv:Header>
<soapenv:Body>
<ns:checkToken soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<checkToken xsi:type="xsd:string"><![CDATA[%s]]></checkToken>
</ns:checkToken>
</soapenv:Body>
</soapenv:Envelope>
EOF
factor = 10
huge_data = "a" * (1024 * 1024 * factor)
xml = StringIO.new(template % (huge_data))
handler = Nokogiri::SAX::TestCase::Doc.new
parser = Nokogiri::XML::SAX::Parser.new(handler)
parser.parse(xml)
assert_predicate(handler.errors, :empty?)
end
end
end
end
end