File: test_parser.rb

package info (click to toggle)
ruby-libxml 5.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,800 kB
  • sloc: xml: 18,263; ansic: 7,669; ruby: 5,809; makefile: 6
file content (333 lines) | stat: -rw-r--r-- 10,200 bytes parent folder | download
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
# encoding: UTF-8

require_relative './test_helper'
require 'stringio'

class TestParser < Minitest::Test
  def setup
    LibXML::XML::Error.set_handler(&LibXML::XML::Error::QUIET_HANDLER)
  end

  # -----  Sources  -------
  def test_document
    file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
    parser = LibXML::XML::Parser.file(file)
    doc = parser.parse

    parser = LibXML::XML::Parser.document(doc)

    doc = parser.parse

    assert_instance_of(LibXML::XML::Document, doc)
    assert_instance_of(LibXML::XML::Parser::Context, parser.context)
  end

  def test_nil_document
    error = assert_raises(TypeError) do
      LibXML::XML::Parser.document(nil)
    end

    assert_equal("Must pass an LibXML::XML::Document object", error.message)
  end

  def test_file
    file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))

    parser = LibXML::XML::Parser.file(file)
    doc = parser.parse
    assert_instance_of(LibXML::XML::Document, doc)
    assert_instance_of(LibXML::XML::Parser::Context, parser.context)
  end

  def test_noexistent_file
    error = assert_raises(LibXML::XML::Error) do
      LibXML::XML::Parser.file('i_dont_exist.xml')
    end

    assert_equal('Warning: failed to load external entity "i_dont_exist.xml".', error.message)
  end

  def test_nil_file
    error = assert_raises(TypeError) do
      LibXML::XML::Parser.file(nil)
    end

    assert_match(/nil into String/, error.message)
  end

  def test_file_encoding
    file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
    parser = LibXML::XML::Parser.file(file, :encoding => LibXML::XML::Encoding::ISO_8859_1)

    error = assert_raises(LibXML::XML::Error) do
      parser.parse
    end

    if windows?
      assert_match(/Fatal error: Opening and ending tag mismatch/, error.message)
    else
      assert_match(/Fatal error: Extra content at the end of the document/, error.message)
    end

    parser = LibXML::XML::Parser.file(file, :encoding => LibXML::XML::Encoding::UTF_8)
    doc = parser.parse
    refute_nil(doc)
  end

  def test_file_base_uri
    file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))

    parser = LibXML::XML::Parser.file(file)
    doc = parser.parse
    assert(doc.child.base_uri.match(/test\/model\/bands.utf-8.xml/))

    parser = LibXML::XML::Parser.file(file, :base_uri => "http://libxml.org")
    doc = parser.parse
    assert(doc.child.base_uri.match(/test\/model\/bands.utf-8.xml/))
  end

  def test_io
    File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |io|
      parser = LibXML::XML::Parser.io(io)
      assert_instance_of(LibXML::XML::Parser, parser)

      doc = parser.parse
      assert_instance_of(LibXML::XML::Document, doc)
      assert_instance_of(LibXML::XML::Parser::Context, parser.context)
    end
  end

  def test_io_gc
    # Test that the reader keeps a reference
    # to the io object
    file = File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
    parser = LibXML::XML::Parser.io(file)
    file = nil
    GC.start
    assert(parser.parse)
  end

  def test_nil_io
    error = assert_raises(TypeError) do
      LibXML::XML::Parser.io(nil)
    end

    assert_equal("Must pass in an IO object", error.message)
  end

  def test_string_io
    data = File.read(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
    string_io = StringIO.new(data)
    parser = LibXML::XML::Parser.io(string_io)

    doc = parser.parse
    assert_instance_of(LibXML::XML::Document, doc)
    assert_instance_of(LibXML::XML::Parser::Context, parser.context)
  end

  def test_string_io_thread
    thread = Thread.new do
      data = File.read(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
      string_io = StringIO.new(data)
      parser = LibXML::XML::Parser.io(string_io)

      doc = parser.parse
      assert_instance_of(LibXML::XML::Document, doc)
      assert_instance_of(LibXML::XML::Parser::Context, parser.context)
    end

    thread.join
    assert(true)
    puts 'Thread completed'
  end

  def test_string
    str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'

    parser = LibXML::XML::Parser.string(str)
    assert_instance_of(LibXML::XML::Parser, parser)

    doc = parser.parse
    assert_instance_of(LibXML::XML::Document, doc)
    assert_instance_of(LibXML::XML::Parser::Context, parser.context)
  end

  def test_nil_string
    error = assert_raises(TypeError) do
      LibXML::XML::Parser.string(nil)
    end

    assert_equal("wrong argument type nil (expected String)", error.message)
  end

  def test_string_options
    xml = <<-EOS
      <!DOCTYPE foo [<!ENTITY foo 'bar'>]>
      <test>
        <cdata><![CDATA[something]]></cdata>
        <entity>&foo;</entity>
      </test>
    EOS

    # Parse normally - entities won't be substituted
    parser = LibXML::XML::Parser.string(xml)
    doc = parser.parse
    assert_nil(doc.child.base_uri)

    # Cdata section should be cdata nodes
    node = doc.find_first('/test/cdata').child
    assert_equal(LibXML::XML::Node::CDATA_SECTION_NODE, node.node_type)

    # Entities should not be substituted
    node = doc.find_first('/test/entity')
    assert_equal('&foo;', node.child.to_s)

    # Parse with options
    parser = LibXML::XML::Parser.string(xml, base_uri: 'http://libxml.rubyforge.org',
                                             options: LibXML::XML::Parser::Options::NOCDATA | LibXML::XML::Parser::Options::NOENT)
    doc = parser.parse
    assert_equal(doc.child.base_uri, 'http://libxml.rubyforge.org')

    # Cdata section should be text nodes
    node = doc.find_first('/test/cdata').child
    assert_equal(LibXML::XML::Node::TEXT_NODE, node.node_type)

    # Entities should be subtituted
    node = doc.find_first('/test/entity')
    assert_equal('bar', node.child.to_s)
  end

  def test_string_encoding
    # ISO_8859_1:
    # ö - f6 in hex, \366 in octal
    # ü - fc in hex, \374 in octal

    xml = <<-EOS
      <bands>
        <metal>m\366tley_cr\374e</metal>
      </bands>
    EOS

    # Parse as UTF_8
    parser = LibXML::XML::Parser.string(xml, :encoding => LibXML::XML::Encoding::UTF_8)

    error = assert_raises(LibXML::XML::Error) do
      parser.parse
    end

    assert_equal("Fatal error: Input is not proper UTF-8, indicate encoding !\nBytes: 0xF6 0x74 0x6C 0x65 at :2.",
                 error.message)

    # Parse as ISO_8859_1:
    parser = LibXML::XML::Parser.string(xml, :encoding => LibXML::XML::Encoding::ISO_8859_1)
    doc = parser.parse
    node = doc.find_first('//metal')
    assert_equal(Encoding::UTF_8, node.content.encoding)
    assert_equal("m\303\266tley_cr\303\274e", node.content)
  end

  def test_fd_gc
    # Test opening # of documents up to the file limit for the OS.
    # Ideally it should run until libxml emits a warning,
    # thereby knowing we've done a GC sweep. For the time being,
    # re-open the same doc `limit descriptors` times.
    # If we make it to the end, then we've succeeded,
    # otherwise an exception will be thrown.
    LibXML::XML::Error.set_handler {|error|}

    max_fd = if RUBY_PLATFORM.match(/mswin32|mswin64|mingw/i)
      500
    else
      Process.getrlimit(Process::RLIMIT_NOFILE)[0] + 1
    end

    file = File.join(File.dirname(__FILE__), 'model/rubynet.xml')
    max_fd.times do
       LibXML::XML::Parser.file(file).parse
    end
    LibXML::XML::Error.reset_handler {|error|}
  end

  def test_open_many_files
    file = File.expand_path(File.join(File.dirname(__FILE__), 'model/atom.xml'))
    1000.times do
      LibXML::XML::Parser.file(file).parse
    end
  end

  # -----  Errors  ------
  def test_error
    error = assert_raises(LibXML::XML::Error) do
      LibXML::XML::Parser.string('<foo><bar/></foz>').parse
    end

    refute_nil(error)
    assert_kind_of(LibXML::XML::Error, error)
    assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.", error.message)
    assert_equal(LibXML::XML::Error::PARSER, error.domain)
    assert_equal(LibXML::XML::Error::TAG_NAME_MISMATCH, error.code)
    assert_equal(LibXML::XML::Error::FATAL, error.level)
    assert_nil(error.file)
    assert_equal(1, error.line)
    assert_equal('foo', error.str1)
    assert_equal('foz', error.str2)
    assert_nil(error.str3)
    assert_equal(1, error.int1)
    assert([18, 20].include?(error.int2))
    assert_nil(error.node)
  end

  def test_bad_xml
    parser = LibXML::XML::Parser.string('<ruby_array uga="booga" foo="bar"<fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>')
    error = assert_raises(LibXML::XML::Error) do
      refute_nil(parser.parse)
    end

    refute_nil(error)
    assert_kind_of(LibXML::XML::Error, error)
    if windows?
      assert_equal("Fatal error: Couldn't find end of Start Tag ruby_array line 1 at :1.", error.message)
      assert_equal(LibXML::XML::Error::GT_REQUIRED, error.code)
      assert_equal("ruby_array", error.str1)
      assert_equal(1, error.int1)
    else
      assert_equal("Fatal error: Extra content at the end of the document at :1.", error.message)
      assert_equal(LibXML::XML::Error::DOCUMENT_END, error.code)
      assert_nil(error.str1)
      assert_equal(0, error.int1)
    end
    assert_equal(LibXML::XML::Error::PARSER, error.domain)
    assert_equal(LibXML::XML::Error::FATAL, error.level)
    assert_nil(error.file)
    assert_equal(1, error.line)
    assert_nil(error.str2)
    assert_nil(error.str3)
    assert_equal(34, error.int2)
    assert_nil(error.node)
  end

  def test_errors_from_background_thread
    errors = []
    background_errors = []

    begin
      LibXML::XML::Error.set_handler do |error|
        errors << error
      end
      parser = LibXML::XML::Parser.string("<moo>")

      thread = Thread.new do
        LibXML::XML::Error.set_handler do |error|
          background_errors << error
        end
        parser.parse rescue nil
      end
      thread.join
    ensure
      LibXML::XML::Error.set_handler(&LibXML::XML::Error::QUIET_HANDLER)
    end

    assert_equal(errors.size, 0)
    assert_equal(background_errors.size, 1)
  end
end