-
Notifications
You must be signed in to change notification settings - Fork 44
/
element_test.go
231 lines (223 loc) · 7.71 KB
/
element_test.go
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
package terminal
import (
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
)
var errorCases = []struct {
name string
input string
expected string
}{
{
`1337: sequence does not have a content part and a arguments part`,
"1337;File=foobar",
`expected sequence to have one arguments part and one content part, got 1 part(s)`,
}, {
`1337: sequence has too many parts`,
"1337;File=foobar:baz:foo",
`expected sequence to have one arguments part and one content part, got 3 part(s)`,
}, {
`1337: content part is not valid Base64`,
"1337;File=foobar:!!!!!",
`expected content part to be valid Base64`,
}, {
`1337: image name is missing`,
"1337;File=foobar:AA==",
`name= argument not supplied, required to determine content type`,
}, {
`1337: invalid base64 encoding`,
"1337;File=name=foo.baz:AA==",
`name= value of "foo.baz" is not valid base64`,
}, {
`1337: errors for unclosed quotes`,
`1337;File=name=` + base64Encode(`foo".gif`) + `;width="100%;height='50px>;inline=1:AA==`,
`invalid syntax: unclosed quotation marks`,
}, {
`1337: can't determine content type`,
"1337;File=name=" + base64Encode("foo.baz") + ":AA==",
`can't determine content type for "foo.baz"`,
}, {
`1337: no image content`,
"1337;File=name=foo.jpg:",
`image content missing`,
}, {
`1338: url missing`,
"1338;",
`url= argument not supplied`,
},
}
func TestErrorCases(t *testing.T) {
for _, c := range errorCases {
t.Run(c.name, func(t *testing.T) {
elem, err := parseElementSequence(c.input)
if elem != nil {
t.Fatalf("%s\ninput\t\t%q\nexpected no image, received %+v", c.name, c.input, elem)
}
if diff := cmp.Diff(err.Error(), c.expected); diff != "" {
t.Errorf("%v.Error() diff (-got +want):\n%s", err, diff)
}
})
}
}
var validCases = []struct {
name string
input string
expected *element
}{
{
`unsupported escape sequence`,
"9999",
nil,
}, {
`1337: image with name, content & inline`,
`1337;File=name=Zm9vLmdpZg==;inline=1:AA==`,
&element{url: "foo.gif", content: "AA==", contentType: "image/gif", elementType: elementITermImage},
}, {
`1337: image without inline=1 does not render`,
`1337;File=name=Zm9vLmdpZg==:AA==`,
nil,
}, {
`1337: adapts content type based on image name`,
`1337;File=name=` + base64Encode("foo.jpg") + `;inline=1:AA==`,
&element{url: "foo.jpg", content: "AA==", contentType: "image/jpeg", elementType: elementITermImage},
}, {
`1337: handles width & height`,
`1337;File=name=Zm9vLmdpZg==;width=100%;height=50px;inline=1:AA==`,
&element{url: "foo.gif", content: "AA==", contentType: "image/gif", width: "100%", height: "50px", elementType: elementITermImage},
}, {
`1337: parsing is NOT concerned with XSS in image name, width & height by stripping brackets, because that's protected at render time`,
`1337;File=name=` + base64Encode(`foo".gif`) + `;width="100%";height='50px'>;inline=1:AA==`,
&element{url: `foo".gif`, content: "AA==", contentType: "image/gif", width: "100%", height: "50px>em", elementType: elementITermImage},
}, {
`1337: converts width & height without percent or px to em`,
`1337;File=name=Zm9vLmdpZg==;width=1;height=5;inline=1:AA==`,
&element{url: "foo.gif", content: "AA==", contentType: "image/gif", width: "1em", height: "5em", elementType: elementITermImage},
}, {
`1337: malfored arguments are silently ignored`,
`1337;File=name=Zm9vLmdpZg==;inline=1;sdfsdfs;====ddd;herp=derps:AA==`,
&element{url: "foo.gif", content: "AA==", contentType: "image/gif", elementType: elementITermImage},
}, {
`1338: image with filename`,
"1338;url=tmp/foo.gif",
&element{url: "tmp/foo.gif", elementType: elementImage},
}, {
`1338: image with filename containing an escaped ;`,
"1338;url=tmp/foo\\;bar.gif",
&element{url: "tmp/foo;bar.gif", elementType: elementImage},
}, {
`1338: image with filename, width, height & alt tag`,
"1338;url=foo.gif;width=50px;height=50px;alt=foo gif",
&element{url: "foo.gif", width: "50px", height: "50px", alt: "foo gif", elementType: elementImage},
}, {
`1339: link with url only`,
"1339;url=foo.gif",
&element{url: "foo.gif", elementType: elementLink},
}, {
`1339: link with url and content`,
"1339;url=foo.gif;content=bar",
&element{url: "foo.gif", content: "bar", elementType: elementLink},
}, {
`1339: link in quotes with url only`,
"1339;url='foo.gif'",
&element{url: "foo.gif", elementType: elementLink},
}, {
`1339: link in quotes with url and content`,
"1339;url='foo.gif';content=bar",
&element{url: "foo.gif", content: "bar", elementType: elementLink},
}, {
`1339: link with url and content in quotes`,
"1339;url='foo.gif';content='bar'",
&element{url: "foo.gif", content: "bar", elementType: elementLink},
}, {
`1339: link in quotes with semicolon in url`,
"1339;url='foo.gif?weirdparams=something;somethingelse'",
&element{url: "foo.gif?weirdparams=something;somethingelse", elementType: elementLink},
}, {
`1339: link with HTML special characters in attributes`,
`1339;url=https://example.com/a?b=<c>&d=e#f;height="<hello>";width=<world%>;alt=&;content=<h1>heading</h1>`,
&element{
url: "https://example.com/a?b=<c>&d=e#f",
alt: "&",
content: "<h1>heading</h1>",
height: "<hello>em",
width: "<world%>em",
elementType: elementLink,
},
},
}
func TestElementCases(t *testing.T) {
for _, c := range validCases {
t.Run(c.name, func(t *testing.T) {
elem, err := parseElementSequence(c.input)
if err != nil {
t.Errorf("%s\ninput\t\t%q\nexpected no error, received %s", c.name, c.input, err.Error())
} else if !reflect.DeepEqual(elem, c.expected) {
t.Errorf("%s\ninput\t\t%q\nreceived\t%+v\nexpected\t%+v", c.name, c.input, elem, c.expected)
}
})
}
}
var asHTMLCases = []struct {
name string
element element
expected string
}{
{
"inline image (simple)",
element{
elementType: elementITermImage,
url: "test.png",
contentType: "image/png",
content: "AA==",
},
`<img alt="test.png" src="data:image/png;base64,AA==">`,
}, {
"inline image (HTML minefield)",
element{
elementType: elementITermImage,
url: "<script>.pdf",
contentType: "application/pdf",
content: "<script>",
width: "<'&'>%",
height: "<'&'>px",
},
`<img alt="<script>.pdf" src="data:application/pdf;base64,<script>" width="<'&'>%" height="<'&'>px">`,
}, {
"external image (simple)",
element{elementType: elementImage, url: "https://example.com/a.png"},
`<img alt="https://example.com/a.png" src="https://example.com/a.png">`,
}, {
"external image (HTML minefield)",
element{
elementType: elementImage,
url: "https://example.com/?tag=<script>&a=b",
alt: "<script>'hello & world'</script>",
width: "<'&'>%",
height: "<'&'>px",
},
`<img alt="<script>'hello & world'</script>" src="https://example.com/?tag=<script>&a=b" width="<'&'>%" height="<'&'>px">`,
}, {
"link (simple)",
element{elementType: elementLink, url: "https://example.com/"},
`<a href="https://example.com/">https://example.com/</a>`,
}, {
"link (HTML minefield)",
element{
elementType: elementLink,
url: "https://example.com/?tag=<script>&a=b",
content: "<script>'hello & world'</script>",
},
`<a href="https://example.com/?tag=<script>&a=b"><script>'hello & world'</script></a>`,
},
}
func TestAsHTMLCases(t *testing.T) {
for _, c := range asHTMLCases {
t.Run(c.name, func(t *testing.T) {
html := c.element.asHTML()
if diff := cmp.Diff(html, c.expected); diff != "" {
t.Errorf("%v.asHTML() diff (-got +want):\n%s", c.element, diff)
}
})
}
}