Skip to content

Commit

Permalink
Add more invalid test cases for parsing entitly declaration
Browse files Browse the repository at this point in the history
This patch will add the test cases to verify that it raises an exception properly when parsing malformed entity declaration.
  • Loading branch information
Watson1978 committed Jul 19, 2024
1 parent 2c39c91 commit 1728756
Showing 1 changed file with 152 additions and 0 deletions.
152 changes: 152 additions & 0 deletions test/parse/test_entity_declaration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,158 @@ def parse(internal_subset)
end

public

class TestGarbagedValue < self
def test_garbaged_after_entity_definition
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY "foo" xxx > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 39
Last 80 unconsumed characters:
\"foo\" xxx > ]>
DETAIL
end

class TestSystem < self
def test_garbaged_between_system_and_literal
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY foo SYSTEM xxx "bar" > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 50
Last 80 unconsumed characters:
foo SYSTEM xxx \"bar\" > ]>
DETAIL
end

def test_garbaged_after_literal
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY foo SYSTEM "bar" xxx > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 50
Last 80 unconsumed characters:
foo SYSTEM \"bar\" xxx > ]>
DETAIL
end

def test_garbaged_between_literal_and_ndata_declaration
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY foo SYSTEM "bar" xxx NDATA gif > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 60
Last 80 unconsumed characters:
foo SYSTEM \"bar\" xxx NDATA gif > ]>
DETAIL
end

def test_garbaged_after_ndata_declaration
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY foo SYSTEM "bar" NDATA gif xxx > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 60
Last 80 unconsumed characters:
foo SYSTEM \"bar\" NDATA gif xxx > ]>
DETAIL
end
end

class TestPublic < self
def test_garbaged_between_public_and_first_literal
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY foo PUBLIC xxx "bar" "baz" > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 56
Last 80 unconsumed characters:
foo PUBLIC xxx \"bar\" \"baz\" > ]>
DETAIL
end

def test_garbaged_between_literals
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY foo PUBLIC "bar" xxx "baz" > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 56
Last 80 unconsumed characters:
foo PUBLIC \"bar\" xxx \"baz\" > ]>
DETAIL
end

def test_garbaged_after_literals
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY foo PUBLIC "bar" "baz" xxx > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 56
Last 80 unconsumed characters:
foo PUBLIC \"bar\" \"baz\" xxx > ]>
DETAIL
end

def test_garbaged_between_literal_and_ndata_declaration
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY foo PUBLIC "bar" "baz" xxx NDATA gif > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 66
Last 80 unconsumed characters:
foo PUBLIC \"bar\" \"baz\" xxx NDATA gif > ]>
DETAIL
end

def test_garbaged_after_ndata_declaration
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY foo PUBLIC "bar" "baz" NDATA gif xxx > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 66
Last 80 unconsumed characters:
foo PUBLIC \"bar\" \"baz\" NDATA gif xxx > ]>
DETAIL
end
end
end

class TestCharacterCode < self
def test_pubid_literal
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY foo PUBLIC "あいう" "bar" > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s.force_encoding('utf-8'))
Malformed entity declaration
Line: 1
Position: 58
Last 80 unconsumed characters:
foo PUBLIC \"あいう\" \"bar\" > ]>
DETAIL
end
end

def test_empty
exception = assert_raise(REXML::ParseException) do
parse(<<-INTERNAL_SUBSET)
Expand Down

0 comments on commit 1728756

Please sign in to comment.