-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathparser_charref_test.go
More file actions
244 lines (213 loc) · 8.08 KB
/
Copy pathparser_charref_test.go
File metadata and controls
244 lines (213 loc) · 8.08 KB
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
package helium_test
import (
"testing"
"github.com/lestrrat-go/helium"
"github.com/stretchr/testify/require"
)
func TestCharRef(t *testing.T) {
t.Parallel()
// parses documents with valid hex/decimal char refs to
// drive the success branches of parseCharRef.
t.Run("valid forms", func(t *testing.T) {
doc, err := helium.NewParser().Parse(t.Context(), []byte(`<root>ABJ</root>`))
require.NoError(t, err)
root := doc.DocumentElement()
require.NotNil(t, root)
require.Equal(t, "ABJ", string(root.Content()))
})
t.Run("a valid reference is accepted", func(t *testing.T) {
for _, tc := range []struct {
input string
want string
}{
{`<root>A</root>`, "A"},
{`<root>A</root>`, "A"},
} {
doc, err := helium.NewParser().Parse(t.Context(), []byte(tc.input))
require.NoError(t, err, "valid char ref must parse: %s", tc.input)
require.NotNil(t, doc)
root := doc.DocumentElement()
require.NotNil(t, root)
require.Equal(t, tc.want, string(root.Content()))
}
})
t.Run("missing digits are rejected", func(t *testing.T) {
for _, input := range []string{
`<root>&#;</root>`,
`<root>&#x;</root>`,
} {
_, err := helium.NewParser().Parse(t.Context(), []byte(input))
require.Error(t, err, "char ref without digits must be rejected: %s", input)
}
})
t.Run("a missing semicolon is rejected", func(t *testing.T) {
for _, input := range []string{
`<root>A</root>`,
`<root>A</root>`,
} {
_, err := helium.NewParser().Parse(t.Context(), []byte(input))
require.Error(t, err, "char ref without terminating ';' must be rejected: %s", input)
}
})
// drives parseCharRef / parseEntityRef error
// branches with a table of malformed-reference documents.
t.Run("malformed character and entity references", func(t *testing.T) {
bad := []struct {
name string
src string
}{
{"hex-missing-digits", `<root>&#x;</root>`},
{"dec-missing-digits", `<root>&#;</root>`},
{"hex-invalid-digit", `<root>&#xZZ;</root>`},
{"dec-invalid-digit", `<root>A3;</root>`},
{"charref-out-of-range", `<root>�</root>`},
{"charref-control", `<root>�</root>`},
{"undeclared-entity-standalone", `<?xml version="1.0" standalone="yes"?><root>&undeclared;</root>`},
{"entity-empty-name", `<root>&;</root>`},
}
for _, tc := range bad {
t.Run(tc.name, func(t *testing.T) {
_, err := helium.NewParser().Parse(t.Context(), []byte(tc.src))
require.Error(t, err, "expected parse error for %q", tc.src)
})
}
})
// indirectly exercises resolveCharRefs by
// round-tripping a document whose internal entity content contains char refs.
t.Run("resolved through Parse", func(t *testing.T) {
const src = `<?xml version="1.0"?>
<!DOCTYPE doc [
<!ENTITY e "ABCDE">
]>
<doc>&e;</doc>`
doc, err := helium.NewParser().Parse(t.Context(), []byte(src))
require.NoError(t, err)
out, err := helium.WriteString(doc)
require.NoError(t, err)
require.Contains(t, out, "doc")
})
// against the index-out-of-range panic in
// parseStringCharRef when malformed character references appear inside entity
// declarations. Each malformed input must yield a structured
// helium.ErrParseError, never a panic.
t.Run("a malformed reference does not panic", func(t *testing.T) {
malformed := []string{
`<!DOCTYPE root [<!ENTITY e "&#">]><root>&e;</root>`,
`<!DOCTYPE root [<!ENTITY e "&#x">]><root>&e;</root>`,
`<!DOCTYPE root [<!ENTITY e "">]><root>&e;</root>`,
`<!DOCTYPE root [<!ENTITY e "&#xZ">]><root>&e;</root>`,
`<!DOCTYPE root [<!ENTITY e "&#;">]><root>&e;</root>`,
}
for _, input := range malformed {
t.Run(input, func(t *testing.T) {
require.NotPanics(t, func() {
_, err := helium.NewParser().SubstituteEntities(true).Parse(t.Context(), []byte(input))
require.Error(t, err, "malformed char ref must return an error")
var pe helium.ErrParseError
require.ErrorAs(t, err, &pe, "malformed char ref must return a helium.ErrParseError")
})
})
}
})
// against an out-of-range character
// reference wrapping int32 into a valid-looking rune. These exercise the
// content-path parser (parseCharRef) and the parameter-entity declaration
// branch, both of which must report an error and produce no
// bogus character.
t.Run("an out-of-range reference is rejected", func(t *testing.T) {
cases := []string{
// Content path: 4294967337 = 0x100000029 wraps to 0x29 (')').
`<root>�</root>`,
`<root>�</root>`,
// Parameter-entity value must propagate the same error.
`<!DOCTYPE root [<!ENTITY % p "�"> %p;]><root/>`,
}
for _, input := range cases {
t.Run(input, func(t *testing.T) {
require.NotPanics(t, func() {
_, err := helium.NewParser().SubstituteEntities(true).Parse(t.Context(), []byte(input))
require.Error(t, err, "out-of-range char ref must return an error")
var pe helium.ErrParseError
require.ErrorAs(t, err, &pe, "out-of-range char ref must return a helium.ErrParseError")
})
})
}
})
// against over-rejection: a valid decimal
// character reference entity must still parse and expand correctly.
t.Run("a valid reference still works", func(t *testing.T) {
input := `<!DOCTYPE root [<!ENTITY e "A">]><root>&e;</root>`
doc, err := helium.NewParser().SubstituteEntities(true).Parse(t.Context(), []byte(input))
require.NoError(t, err, "valid char ref must parse")
require.NotNil(t, doc)
root := doc.DocumentElement()
require.NotNil(t, root)
child := root.FirstChild()
require.NotNil(t, child, "expanded entity must produce a text child")
require.Equal(t, "A", string(child.Content()), "A must expand to 'A'")
})
// numeric and hex character references.
t.Run("character references", func(t *testing.T) {
const src = `<root>dec=A hex=B high=😀</root>`
doc, err := helium.NewParser().Parse(t.Context(), []byte(src))
require.NoError(t, err)
require.Equal(t, "dec=A hex=B high=\U0001F600", string(doc.DocumentElement().Content()))
})
t.Run("the replacement character", func(t *testing.T) {
doc, err := helium.NewParser().Parse(t.Context(), []byte("<root>�</root>"))
require.NoError(t, err)
root := doc.DocumentElement()
require.NotNil(t, root)
require.Equal(t, "�", string(root.Content()))
})
}
func TestCreateCharRef(t *testing.T) {
t.Parallel()
// the CreateCharRef name-stripping branches.
t.Run("forms", func(t *testing.T) {
doc := helium.NewDocument("1.0", "UTF-8", helium.StandaloneImplicitNo)
plain, err := doc.CreateCharRef("foo")
require.NoError(t, err)
require.Equal(t, "foo", plain.Name())
// "&foo;" -> "foo"
full, err := doc.CreateCharRef("&foo;")
require.NoError(t, err)
require.Equal(t, "foo", full.Name())
// "&foo" (no trailing semicolon) -> "foo"
noSemi, err := doc.CreateCharRef("&foo")
require.NoError(t, err)
require.Equal(t, "foo", noSemi.Name())
// Empty name and a name that decodes to empty are rejected.
_, err = doc.CreateCharRef("")
require.Error(t, err)
_, err = doc.CreateCharRef("&;")
require.Error(t, err)
})
// locks the create->serialize round-trip: an
// EntityRefNode built by CreateCharRef with a "#NNN"/"#xHH" name serializes as
// a character reference, and a plain name serializes as a named entity
// reference, mirroring libxml2's xmlNewCharRef (both are XML_ENTITY_REF_NODE).
t.Run("serializes", func(t *testing.T) {
for _, tc := range []struct {
name string
want string
}{
{name: "#123", want: "{"},
{name: "#xAB", want: "«"},
{name: "amp", want: "&"},
} {
doc := helium.NewDocument("1.0", "UTF-8", helium.StandaloneImplicitNo)
root, err := doc.CreateElement("root")
require.NoError(t, err)
require.NoError(t, doc.SetDocumentElement(root))
ref, err := doc.CreateCharRef(tc.name)
require.NoError(t, err)
require.Equal(t, helium.EntityRefNode, ref.Type(), "CreateCharRef yields an EntityRefNode (libxml2 xmlNewCharRef)")
require.Equal(t, tc.name, ref.Name())
require.NoError(t, root.AddChild(ref))
str, err := helium.WriteString(root)
require.NoError(t, err)
require.Equal(t, "<root>"+tc.want+"</root>", str)
}
})
}