Skip to content

Commit 8881bd5

Browse files
committed
Add more invalid test cases for parsing entitly declaration
This patch will add the test cases to verify that it raises an exception properly when parsing malformed entity declaration. Fix test class names
1 parent 2bca7bd commit 8881bd5

File tree

1 file changed

+233
-0
lines changed

1 file changed

+233
-0
lines changed

test/parse/test_entity_declaration.rb

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,239 @@ def parse(internal_subset)
2323
end
2424

2525
public
26+
27+
class TestGeneralEntityDeclaration < self
28+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-GEDecl
29+
class TestEntityDefinition < self
30+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EntityDef
31+
class TestEntityValue < self
32+
def test_no_quote
33+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EntityValue
34+
exception = assert_raise(REXML::ParseException) do
35+
REXML::Document.new('<!DOCTYPE root [<!ENTITY valid-name invalid-entity-value > ]>')
36+
end
37+
assert_equal(<<-DETAIL.chomp, exception.to_s)
38+
Malformed entity declaration
39+
Line: 1
40+
Position: 61
41+
Last 80 unconsumed characters:
42+
valid-name invalid-entity-value > ]>
43+
DETAIL
44+
end
45+
46+
def test_invalid_entity_value
47+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EntityValue
48+
exception = assert_raise(REXML::ParseException) do
49+
REXML::Document.new('<!DOCTYPE root [<!ENTITY valid-name "% &" > ]>')
50+
end
51+
assert_equal(<<-DETAIL.chomp, exception.to_s)
52+
Malformed entity declaration
53+
Line: 1
54+
Position: 46
55+
Last 80 unconsumed characters:
56+
valid-name \"% &\" > ]>
57+
DETAIL
58+
end
59+
60+
class TestExternalId < self
61+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-ExternalID
62+
class TestSystemLiteral < self
63+
def test_no_quote_in_system
64+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-SystemLiteral
65+
exception = assert_raise(REXML::ParseException) do
66+
REXML::Document.new('<!DOCTYPE root [<!ENTITY valid-name SYSTEM invalid-system-literal > ]>')
67+
end
68+
assert_equal(<<-DETAIL.chomp, exception.to_s)
69+
Malformed entity declaration
70+
Line: 1
71+
Position: 70
72+
Last 80 unconsumed characters:
73+
valid-name SYSTEM invalid-system-literal > ]>
74+
DETAIL
75+
end
76+
77+
def test_no_quote_in_public
78+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-SystemLiteral
79+
exception = assert_raise(REXML::ParseException) do
80+
REXML::Document.new('<!DOCTYPE root [<!ENTITY valid-name PUBLIC "valid-pubid-literal" invalid-system-literal > ]>')
81+
end
82+
assert_equal(<<-DETAIL.chomp, exception.to_s)
83+
Malformed entity declaration
84+
Line: 1
85+
Position: 92
86+
Last 80 unconsumed characters:
87+
valid-name PUBLIC \"valid-pubid-literal\" invalid-system-literal > ]>
88+
DETAIL
89+
end
90+
end
91+
92+
class TestPubidLiteral < self
93+
def test_no_quote
94+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PubidLiteral
95+
exception = assert_raise(REXML::ParseException) do
96+
REXML::Document.new('<!DOCTYPE root [<!ENTITY valid-name PUBLIC invalid-pubid-literal "valid-system-literal" > ]>')
97+
end
98+
assert_equal(<<-DETAIL.chomp, exception.to_s)
99+
Malformed entity declaration
100+
Line: 1
101+
Position: 92
102+
Last 80 unconsumed characters:
103+
valid-name PUBLIC invalid-pubid-literal \"valid-system-literal\" > ]>
104+
DETAIL
105+
end
106+
107+
def test_invalid_pubid_char
108+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PubidChar
109+
exception = assert_raise(REXML::ParseException) do
110+
# U+3042 HIRAGANA LETTER A
111+
REXML::Document.new("<!DOCTYPE root [<!ENTITY valid-name PUBLIC \"\u3042\" \"valid-system-literal\" > ]>")
112+
end
113+
assert_equal(<<-DETAIL.force_encoding('utf-8').chomp, exception.to_s.force_encoding('utf-8'))
114+
Malformed entity declaration
115+
Line: 1
116+
Position: 76
117+
Last 80 unconsumed characters:
118+
valid-name PUBLIC \"\u3042\" \"valid-system-literal\" > ]>
119+
DETAIL
120+
end
121+
end
122+
end
123+
124+
class TestNDataDeclaration < self
125+
def test_no_quote
126+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-NDataDecl
127+
exception = assert_raise(REXML::ParseException) do
128+
REXML::Document.new('<!DOCTYPE root [<!ENTITY valid-name PUBLIC "valid-pubid-literal" "valid-system-literal" invalid-ndata valid-ndata-value> ]>')
129+
end
130+
assert_equal(<<-DETAIL.chomp, exception.to_s)
131+
Malformed entity declaration
132+
Line: 1
133+
Position: 123
134+
Last 80 unconsumed characters:
135+
valid-name PUBLIC \"valid-pubid-literal\" \"valid-system-literal\" invalid-ndata val
136+
DETAIL
137+
end
138+
end
139+
end
140+
end
141+
end
142+
143+
class TestParsedEntityDeclaration < self
144+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PEDecl
145+
class ParsedEntityDefinition < self
146+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PEDef
147+
class TestEntityValue < self
148+
class TestEntityValue < self
149+
def test_no_quote
150+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EntityValue
151+
exception = assert_raise(REXML::ParseException) do
152+
REXML::Document.new('<!DOCTYPE root [<!ENTITY % valid-name invalid-entity-value > ]>')
153+
end
154+
assert_equal(<<-DETAIL.chomp, exception.to_s)
155+
Malformed entity declaration
156+
Line: 1
157+
Position: 63
158+
Last 80 unconsumed characters:
159+
% valid-name invalid-entity-value > ]>
160+
DETAIL
161+
end
162+
163+
def test_invalid_entity_value
164+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EntityValue
165+
exception = assert_raise(REXML::ParseException) do
166+
REXML::Document.new('<!DOCTYPE root [<!ENTITY % valid-name "% &" > ]>')
167+
end
168+
assert_equal(<<-DETAIL.chomp, exception.to_s)
169+
Malformed entity declaration
170+
Line: 1
171+
Position: 48
172+
Last 80 unconsumed characters:
173+
% valid-name \"% &\" > ]>
174+
DETAIL
175+
end
176+
end
177+
end
178+
179+
class TestExternalId < self
180+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-ExternalID
181+
class TestSystemLiteral < self
182+
def test_no_quote_in_system
183+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-SystemLiteral
184+
exception = assert_raise(REXML::ParseException) do
185+
REXML::Document.new('<!DOCTYPE root [<!ENTITY % valid-name SYSTEM invalid-system-literal > ]>')
186+
end
187+
assert_equal(<<-DETAIL.chomp, exception.to_s)
188+
Malformed entity declaration
189+
Line: 1
190+
Position: 72
191+
Last 80 unconsumed characters:
192+
% valid-name SYSTEM invalid-system-literal > ]>
193+
DETAIL
194+
end
195+
196+
def test_no_quote_in_public
197+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-SystemLiteral
198+
exception = assert_raise(REXML::ParseException) do
199+
REXML::Document.new('<!DOCTYPE root [<!ENTITY % valid-name PUBLIC "valid-pubid-literal" invalid-system-literal > ]>')
200+
end
201+
assert_equal(<<-DETAIL.chomp, exception.to_s)
202+
Malformed entity declaration
203+
Line: 1
204+
Position: 94
205+
Last 80 unconsumed characters:
206+
% valid-name PUBLIC \"valid-pubid-literal\" invalid-system-literal > ]>
207+
DETAIL
208+
end
209+
end
210+
211+
class TestPubidLiteral < self
212+
def test_no_quote
213+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PubidLiteral
214+
exception = assert_raise(REXML::ParseException) do
215+
REXML::Document.new('<!DOCTYPE root [<!ENTITY % valid-name PUBLIC invalid-pubid-literal "valid-system-literal" > ]>')
216+
end
217+
assert_equal(<<-DETAIL.chomp, exception.to_s)
218+
Malformed entity declaration
219+
Line: 1
220+
Position: 94
221+
Last 80 unconsumed characters:
222+
% valid-name PUBLIC invalid-pubid-literal \"valid-system-literal\" > ]>
223+
DETAIL
224+
end
225+
226+
def test_invalid_pubid_char
227+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PubidChar
228+
exception = assert_raise(REXML::ParseException) do
229+
# U+3042 HIRAGANA LETTER A
230+
REXML::Document.new("<!DOCTYPE root [<!ENTITY % valid-name PUBLIC \"\u3042\" \"valid-system-literal\" > ]>")
231+
end
232+
assert_equal(<<-DETAIL.force_encoding('utf-8').chomp, exception.to_s.force_encoding('utf-8'))
233+
Malformed entity declaration
234+
Line: 1
235+
Position: 78
236+
Last 80 unconsumed characters:
237+
% valid-name PUBLIC \"\u3042\" \"valid-system-literal\" > ]>
238+
DETAIL
239+
end
240+
end
241+
end
242+
end
243+
244+
def test_unnecessary_ndata_declaration
245+
# https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PEDef
246+
exception = assert_raise(REXML::ParseException) do
247+
REXML::Document.new('<!DOCTYPE root [<!ENTITY % valid-name "valid-entity-value" "NDATA" valid-ndata-value > ]>')
248+
end
249+
assert_equal(<<-DETAIL.chomp, exception.to_s)
250+
Malformed entity declaration
251+
Line: 1
252+
Position: 89
253+
Last 80 unconsumed characters:
254+
% valid-name \"valid-entity-value\" \"NDATA\" valid-ndata-value > ]>
255+
DETAIL
256+
end
257+
end
258+
26259
def test_empty
27260
exception = assert_raise(REXML::ParseException) do
28261
parse(<<-INTERNAL_SUBSET)

0 commit comments

Comments
 (0)