Skip to content

Commit

Permalink
Add more broken tests for the XML reader entity reference handling. R…
Browse files Browse the repository at this point in the history
…ename the tests to test_<parser_name>_entity_reference_<options>.
  • Loading branch information
jvshahid committed Jun 17, 2012
1 parent 7272ba1 commit 75a4933
Showing 1 changed file with 74 additions and 60 deletions.
134 changes: 74 additions & 60 deletions test/xml/test_entity_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def setup
@parser = Nokogiri::XML::Document
end

test_relative_and_absolute_path :test_document_dtd_loading do
test_relative_and_absolute_path :test_dom_entity_reference_with_dtdloda do
# Make sure that we can parse entity references and include them in the document
html = File.read xml_document
doc = @parser.parse html, path do |cfg|
Expand All @@ -69,7 +69,7 @@ def setup
assert_equal "foobar", doc.xpath('//blah').text
end

test_relative_and_absolute_path :test_document_dtd_loading_with_dtdvalid do
test_relative_and_absolute_path :test_dom_entity_reference_with_dtdvalid do
# Make sure that we can parse entity references and include them in the document
html = File.read xml_document
doc = @parser.parse html, path do |cfg|
Expand All @@ -81,7 +81,7 @@ def setup
assert_equal "foobar", doc.xpath('//blah').text
end

test_absolute_path :test_document_dtd_loading_absolute_dtd_path do
test_absolute_path :test_dom_dtd_loading_with_absolute_path do
# Make sure that we can parse entity references and include them in the document
html = %Q[<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE document SYSTEM "#{path}/document.dtd">
Expand All @@ -98,19 +98,7 @@ def setup
assert_equal "foobar", doc.xpath('//blah').text
end

test_relative_and_absolute_path :test_document_dtd_loading_with_dtdvalid do
# Make sure that we can parse entity references and include them in the document
html = File.read xml_document
doc = @parser.parse html, path do |cfg|
cfg.default_xml
cfg.dtdvalid
cfg.noent
end
assert_equal [], doc.errors
assert_equal "foobar", doc.xpath('//blah').text
end

test_relative_and_absolute_path :test_document_dtd_loading_with_io do
test_relative_and_absolute_path :test_dom_entity_reference_with_io do
# Make sure that we can parse entity references and include them in the document
html = File.open xml_document
doc = @parser.parse html, nil do |cfg|
Expand All @@ -122,24 +110,7 @@ def setup
assert_equal "foobar", doc.xpath('//blah').text
end

test_relative_and_absolute_path :test_document_dtd_loading_from_current_working_directory do
pwd = Dir.pwd
begin
Dir.chdir path
html = File.read 'document.xml'
doc = @parser.parse html, nil do |cfg|
cfg.default_xml
cfg.dtdload
cfg.noent
end
assert_equal [], doc.errors
assert_equal "foobar", doc.xpath('//blah').text
ensure
Dir.chdir pwd
end
end

test_relative_and_absolute_path :test_document_dtd_loading_without_noent do
test_relative_and_absolute_path :test_dom_entity_reference_without_noent do
# Make sure that we don't include entity references unless NOENT is set to true
html = File.read xml_document
doc = @parser.parse html, path do |cfg|
Expand All @@ -150,7 +121,7 @@ def setup
assert_kind_of Nokogiri::XML::EntityReference, doc.xpath('//body').first.children.first
end

test_relative_and_absolute_path :test_document_dtd_loading_without_dtdload do
test_relative_and_absolute_path :test_dom_entity_reference_without_dtdload do
# Make sure that we don't include entity references unless NOENT is set to true
html = File.read xml_document
doc = @parser.parse html, path do |cfg|
Expand All @@ -163,7 +134,7 @@ def setup
end

test_relative_and_absolute_path :test_document_dtd_loading_with_nonet do
# Make sure that we don't include entity references unless NOENT is set to true
# Make sure that we don't include remote entities unless NOENT is set to true
html = %Q[<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE document SYSTEM "http://foo.bar.com/">
<document>
Expand All @@ -181,41 +152,84 @@ def setup
assert_equal ["Attempt to load network entity http://foo.bar.com/"], doc.errors.map(&:to_s)
end
end
# TODO: can we retreive a resource pointing to localhost when NONET is set to true ?
end

class TestReaderEntityReference < Nokogiri::TestCase
end

class TestSaxEntityReference < Nokogiri::SAX::TestCase
include Common
class TestSaxEntityReference < Nokogiri::SAX::TestCase
include Common

def setup
super
@parser = XML::SAX::Parser.new(Doc.new) do |ctx|
ctx.replace_entities = true
end
def setup
super
@parser = XML::SAX::Parser.new(Doc.new) do |ctx|
ctx.replace_entities = true
end
end

test_relative_and_absolute_path :test_document_dtd_loading do
# Make sure that we can parse entity references and include them in the document
html = File.read xml_document
@parser.parse html
refute_nil @parser.document.errors
assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip)
end
test_relative_and_absolute_path :test_sax_entity_reference do
# Make sure that we can parse entity references and include them in the document
html = File.read xml_document
@parser.parse html
refute_nil @parser.document.errors
assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip)
end

test_relative_and_absolute_path :test_document_dtd_loading_with_nonet do
# Make sure that we don't include entity references unless NOENT is set to true
html = %Q[<?xml version="1.0" encoding="UTF-8" ?>
test_relative_and_absolute_path :test_sax_entity_reference do
# Make sure that we don't include entity references unless NOENT is set to true
html = %Q[<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE document SYSTEM "http://foo.bar.com/">
<document>
<body>&bar;</body>
</document>
]
@parser.parse html
refute_nil @parser.document.errors
assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip)
@parser.parse html
refute_nil @parser.document.errors
assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip)
end
end

class TestReaderEntityReference < Nokogiri::TestCase
include Common

def setup
super
end

test_relative_and_absolute_path :test_reader_entity_reference do
# Make sure that we can parse entity references and include them in the document
html = File.read xml_document
reader = Nokogiri::XML::Reader html, path do |cfg|
cfg.default_xml
cfg.dtdload
cfg.noent
end
end # TODO: can we retreive a resource pointing to localhost when NONET is set to true ?
nodes = []
reader.each { |n| nodes << n.value }
assert_equal ['foobar'], nodes.compact.map(&:strip).reject(&:empty?)
end

test_relative_and_absolute_path :test_reader_entity_reference_without_noent do
# Make sure that we can parse entity references and include them in the document
html = File.read xml_document
reader = Nokogiri::XML::Reader html, path do |cfg|
cfg.default_xml
cfg.dtdload
end
nodes = []
reader.each { |n| nodes << n.value }
assert_equal [], nodes.compact.map(&:strip).reject(&:empty?)
end

test_relative_and_absolute_path :test_reader_entity_reference_without_dtdload do
# Make sure that we can parse entity references and include them in the document
html = File.read xml_document
assert_raises(Nokogiri::XML::SyntaxError)do
reader = Nokogiri::XML::Reader html, path do |cfg|
cfg.default_xml
end
nodes = []
reader.each { |n| nodes << n.value }
end
end
end
end
end

0 comments on commit 75a4933

Please sign in to comment.