Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more invalid test cases for parsing entitly declaration #183

Merged
merged 19 commits into from
Jul 24, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 233 additions & 0 deletions test/parse/test_entity_declaration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,239 @@ def parse(internal_subset)
end

public

class TestGeneralEntityDeclaration < self
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-GEDecl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use a comment for class instead of a comment in class?

Suggested change
class TestGeneralEntityDeclaration < self
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-GEDecl
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-GEDecl
class TestGeneralEntityDeclaration < self

class TestEntityDefinition < self
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EntityDef
class TestEntityValue < self
def test_no_quote
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EntityValue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class TestEntityValue < self
def test_no_quote
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EntityValue
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EntityValue
class TestEntityValue < self
def test_no_quote

exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY valid-name invalid-entity-value > ]>')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove needless spaces?

Suggested change
REXML::Document.new('<!DOCTYPE root [<!ENTITY valid-name invalid-entity-value > ]>')
REXML::Document.new('<!DOCTYPE root [<!ENTITY valid-name invalid-entity-value>]>')

end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 61
Last 80 unconsumed characters:
valid-name invalid-entity-value > ]>
DETAIL
end

def test_invalid_entity_value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use why this is an invalid value for test name instead of test_invalid_entity_value like you did for test_no_quote?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the method name at 98e0d8f

# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EntityValue
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY valid-name "% &" > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 46
Last 80 unconsumed characters:
valid-name \"% &\" > ]>
DETAIL
end

class TestExternalId < self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class TestExternalId < self
class TestExternalID < self

# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-ExternalID
class TestSystemLiteral < self
def test_no_quote_in_system
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-SystemLiteral
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY valid-name SYSTEM invalid-system-literal > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 70
Last 80 unconsumed characters:
valid-name SYSTEM invalid-system-literal > ]>
DETAIL
end

def test_no_quote_in_public
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-SystemLiteral
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY valid-name PUBLIC "valid-pubid-literal" invalid-system-literal > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 92
Last 80 unconsumed characters:
valid-name PUBLIC \"valid-pubid-literal\" invalid-system-literal > ]>
DETAIL
end
end

class TestPubidLiteral < self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class TestPubidLiteral < self
class TestPublicIDLiteral < self

def test_no_quote
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PubidLiteral
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY valid-name PUBLIC invalid-pubid-literal "valid-system-literal" > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 92
Last 80 unconsumed characters:
valid-name PUBLIC invalid-pubid-literal \"valid-system-literal\" > ]>
DETAIL
end

def test_invalid_pubid_char
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PubidChar
exception = assert_raise(REXML::ParseException) do
# U+3042 HIRAGANA LETTER A
REXML::Document.new("<!DOCTYPE root [<!ENTITY valid-name PUBLIC \"\u3042\" \"valid-system-literal\" > ]>")
end
assert_equal(<<-DETAIL.force_encoding('utf-8').chomp, exception.to_s.force_encoding('utf-8'))
Malformed entity declaration
Line: 1
Position: 76
Last 80 unconsumed characters:
valid-name PUBLIC \"\u3042\" \"valid-system-literal\" > ]>
DETAIL
end
end
end

class TestNDataDeclaration < self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class TestNDataDeclaration < self
class TestNotationDataDeclaration < self

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a test for invalid Name in NDataDecl ::= S 'NDATA' S Name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added tests for Name at 9ecad7c

def test_no_quote
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "no quote" a suitable reason?
I think that NDATA is only the valid keyword.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry. It is my mistake.

# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-NDataDecl
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY valid-name PUBLIC "valid-pubid-literal" "valid-system-literal" invalid-ndata valid-ndata-value> ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 123
Last 80 unconsumed characters:
valid-name PUBLIC \"valid-pubid-literal\" \"valid-system-literal\" invalid-ndata val
DETAIL
end
end
end
end
end

class TestParsedEntityDeclaration < self
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PEDecl
class ParsedEntityDefinition < self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class ParsedEntityDefinition < self
class TestParsedEntityDefinition < self

# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PEDef
class TestEntityValue < self
class TestEntityValue < self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we reduce a needless class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry. It is my mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed at 77b23ae

def test_no_quote
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EntityValue
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY % valid-name invalid-entity-value > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 63
Last 80 unconsumed characters:
% valid-name invalid-entity-value > ]>
DETAIL
end

def test_invalid_entity_value
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EntityValue
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY % valid-name "% &" > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 48
Last 80 unconsumed characters:
% valid-name \"% &\" > ]>
DETAIL
end
end
end

class TestExternalId < self
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-ExternalID
class TestSystemLiteral < self
def test_no_quote_in_system
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-SystemLiteral
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY % valid-name SYSTEM invalid-system-literal > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 72
Last 80 unconsumed characters:
% valid-name SYSTEM invalid-system-literal > ]>
DETAIL
end

def test_no_quote_in_public
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-SystemLiteral
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY % valid-name PUBLIC "valid-pubid-literal" invalid-system-literal > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 94
Last 80 unconsumed characters:
% valid-name PUBLIC \"valid-pubid-literal\" invalid-system-literal > ]>
DETAIL
end
end

class TestPubidLiteral < self
def test_no_quote
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PubidLiteral
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY % valid-name PUBLIC invalid-pubid-literal "valid-system-literal" > ]>')
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 94
Last 80 unconsumed characters:
% valid-name PUBLIC invalid-pubid-literal \"valid-system-literal\" > ]>
DETAIL
end

def test_invalid_pubid_char
Watson1978 marked this conversation as resolved.
Show resolved Hide resolved
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PubidChar
exception = assert_raise(REXML::ParseException) do
# U+3042 HIRAGANA LETTER A
REXML::Document.new("<!DOCTYPE root [<!ENTITY % valid-name PUBLIC \"\u3042\" \"valid-system-literal\" > ]>")
end
assert_equal(<<-DETAIL.force_encoding('utf-8').chomp, exception.to_s.force_encoding('utf-8'))
Malformed entity declaration
Line: 1
Position: 78
Last 80 unconsumed characters:
% valid-name PUBLIC \"\u3042\" \"valid-system-literal\" > ]>
DETAIL
end
end
end
end

def test_unnecessary_ndata_declaration
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PEDef
exception = assert_raise(REXML::ParseException) do
REXML::Document.new('<!DOCTYPE root [<!ENTITY % valid-name "valid-entity-value" "NDATA" valid-ndata-value > ]>')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
REXML::Document.new('<!DOCTYPE root [<!ENTITY % valid-name "valid-entity-value" "NDATA" valid-ndata-value > ]>')
REXML::Document.new('<!DOCTYPE root [<!ENTITY % valid-name "valid-entity-value" NDATA valid-ndata-value > ]>')

BTW, should we use the ExternalID NDataDecl? part instead of the EntityValue part in EntityDef ::= EntityValue | ExternalID NDataDecl?)? If we want to use the EntityValue part, we should use "garbage" or something instead of ndata_declaration because all contents (including NDataDecl) are invalid after the EntityValue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sence.
We should make sure to check that any unnecessary NDataDecl after EntityValue will raise an exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test at 4782084

end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed entity declaration
Line: 1
Position: 89
Last 80 unconsumed characters:
% valid-name \"valid-entity-value\" \"NDATA\" valid-ndata-value > ]>
DETAIL
end
end

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