Debian Codegraph @

common_test.go

raw
1 package parse
2
3 import (
4 "encoding/base64"
5 "mime"
6 "net/url"
7 "regexp"
8 "testing"
9
10 "github.com/tdewolff/test"
11 )
12
13 var entitySlices [][]byte
14 var encodedURLSlices [][]byte
15 var urlSlices [][]byte
16
17 func init() {
18 entitySlices = helperRandStrings(100, 5, []string{""", "'", "'", " ", " ", "test"})
19 encodedURLSlices = helperRandStrings(100, 5, []string{"%20", "%3D", "test"})
20 urlSlices = helperRandStrings(100, 5, []string{"~", "\"", "<", "test"})
21 }
22
23 func TestParseNumber(t *testing.T) {
24 var numberTests = []struct {
25 number string
26 expected int
27 }{
28 {"5", 1},
29 {"0.51", 4},
30 {"0.5e-99", 7},
31 {"0.5e-", 3},
32 {"+50.0", 5},
33 {".0", 2},
34 {"0.", 1},
35 {"", 0},
36 {"+", 0},
37 {".", 0},
38 {"a", 0},
39 }
40 for _, tt := range numberTests {
41 t.Run(tt.number, func(t *testing.T) {
42 n := Number([]byte(tt.number))
43 test.T(t, n, tt.expected)
44 })
45 }
46 }
47
48 func TestParseDimension(t *testing.T) {
49 var dimensionTests = []struct {
50 dimension string
51 expectedNum int
52 expectedUnit int
53 }{
54 {"5px", 1, 2},
55 {"5px ", 1, 2},
56 {"5%", 1, 1},
57 {"5em", 1, 2},
58 {"px", 0, 0},
59 {"1", 1, 0},
60 {"1~", 1, 0},
61 }
62 for _, tt := range dimensionTests {
63 t.Run(tt.dimension, func(t *testing.T) {
64 num, unit := Dimension([]byte(tt.dimension))
65 test.T(t, num, tt.expectedNum, "number")
66 test.T(t, unit, tt.expectedUnit, "unit")
67 })
68 }
69 }
70
71 func TestMediatype(t *testing.T) {
72 var mediatypeTests = []struct {
73 mediatype string
74 expectedMimetype string
75 expectedParams map[string]string
76 }{
77 {"text/plain", "text/plain", nil},
78 {"text/plain;charset=US-ASCII", "text/plain", map[string]string{"charset": "US-ASCII"}},
79 {" text/plain ; charset = US-ASCII ", "text/plain", map[string]string{"charset": "US-ASCII"}},
80 {" text/plain a", "text/plain", nil},
81 {"text/plain;base64", "text/plain", map[string]string{"base64": ""}},
82 {"text/plain;inline=;base64", "text/plain", map[string]string{"inline": "", "base64": ""}},
83 {"ÿ ", "ÿ ", nil}, // OSS-Fuzz; ÿ is two bytes in UTF8
84 {"ÿ ;", "ÿ ", map[string]string{"": ""}},
85 }
86 for _, tt := range mediatypeTests {
87 t.Run(tt.mediatype, func(t *testing.T) {
88 mimetype, params := Mediatype([]byte(tt.mediatype))
89 test.String(t, string(mimetype), tt.expectedMimetype, "mimetype")
90 test.T(t, params, tt.expectedParams, "parameters") // TODO
91 })
92 }
93 }
94
95 func TestParseDataURI(t *testing.T) {
96 var dataURITests = []struct {
97 dataURI string
98 expectedMimetype string
99 expectedData string
100 expectedErr error
101 }{
102 {"www.domain.com", "", "", ErrBadDataURI},
103 {"data:,", "text/plain", "", nil},
104 {"data:text/xml,", "text/xml", "", nil},
105 {"data:,text", "text/plain", "text", nil},
106 {"data:;base64,dGV4dA==", "text/plain", "text", nil},
107 {"data:image/svg+xml,", "image/svg+xml", "", nil},
108 {"data:;base64,()", "", "", base64.CorruptInputError(0)},
109 {"data:image/svg+xml,%3Cpath%20stroke-width='9.38%'/%3E", "image/svg+xml", "<path stroke-width='9.38%'/>", nil},
110 {"data:,%ii", "text/plain", "%ii", nil},
111 {"data:image/svg&#43;xml,%e2%ad%90", "image/svg&#43;xml", "\u2b50", nil},
112 }
113 for _, tt := range dataURITests {
114 t.Run(tt.dataURI, func(t *testing.T) {
115 mimetype, data, err := DataURI([]byte(tt.dataURI))
116 test.T(t, err, tt.expectedErr)
117 test.String(t, string(mimetype), tt.expectedMimetype, "mimetype")
118 test.String(t, string(data), tt.expectedData, "data")
119 })
120 }
121 }
122
123 func TestParseQuoteEntity(t *testing.T) {
124 var quoteEntityTests = []struct {
125 quoteEntity string
126 expectedQuote byte
127 expectedN int
128 }{
129 {"&#34;", '"', 5},
130 {"&#039;", '\'', 6},
131 {"&#x0022;", '"', 8},
132 {"&#x27;", '\'', 6},
133 {"&quot;", '"', 6},
134 {"&apos;", '\'', 6},
135 {"&gt;", 0x00, 0},
136 {"&amp;", 0x00, 0},
137 }
138 for _, tt := range quoteEntityTests {
139 t.Run(tt.quoteEntity, func(t *testing.T) {
140 quote, n := QuoteEntity([]byte(tt.quoteEntity))
141 test.T(t, quote, tt.expectedQuote, "quote")
142 test.T(t, n, tt.expectedN, "quote length")
143 })
144 }
145 }
146
147 func TestReplaceMultipleWhitespace(t *testing.T) {
148 test.Bytes(t, ReplaceMultipleWhitespace([]byte(" a")), []byte(" a"))
149 test.Bytes(t, ReplaceMultipleWhitespace([]byte("a ")), []byte("a "))
150 test.Bytes(t, ReplaceMultipleWhitespace([]byte("a b ")), []byte("a b "))
151 test.Bytes(t, ReplaceMultipleWhitespace([]byte(" a b ")), []byte(" a b "))
152 test.Bytes(t, ReplaceMultipleWhitespace([]byte(" a b ")), []byte(" a b "))
153 test.Bytes(t, ReplaceMultipleWhitespace([]byte(" a b ")), []byte(" a b "))
154 test.Bytes(t, ReplaceMultipleWhitespace([]byte(" a")), []byte(" a"))
155 test.Bytes(t, ReplaceMultipleWhitespace([]byte("a b")), []byte("a b"))
156 }
157
158 func TestReplaceMultipleWhitespaceRandom(t *testing.T) {
159 wsRegexp := regexp.MustCompile("[ \t\f]+")
160 wsNewlinesRegexp := regexp.MustCompile("[ ]*[\r\n][ \r\n]*")
161 for _, e := range wsSlices {
162 reference := wsRegexp.ReplaceAll(e, []byte(" "))
163 reference = wsNewlinesRegexp.ReplaceAll(reference, []byte("\n"))
164 test.Bytes(t, ReplaceMultipleWhitespace(Copy(e)), reference, "in '"+string(e)+"'")
165 }
166 }
167
168 func TestReplaceEntities(t *testing.T) {
169 entitiesMap := map[string][]byte{
170 "varphi": []byte("&phiv;"),
171 "varpi": []byte("&piv;"),
172 "quot": []byte("\""),
173 "apos": []byte("'"),
174 "amp": []byte("&"),
175 }
176 revEntitiesMap := map[byte][]byte{
177 '\'': []byte("&#39;"),
178 }
179 var entityTests = []struct {
180 entity string
181 expected string
182 }{
183 {"&#34;", `"`},
184 {"&#039;", `&#39;`},
185 {"&#x0022;", `"`},
186 {"&#x27;", `&#39;`},
187 {"&#160;", `&#160;`},
188 {"&quot;", `"`},
189 {"&apos;", `&#39;`},
190 {"&#9191;", `&#9191;`},
191 {"&#x23e7;", `&#9191;`},
192 {"&#x23E7;", `&#9191;`},
193 {"&#x23E7;", `&#9191;`},
194 {"&#x270F;", `&#9999;`},
195 {"&#x2710;", `&#x2710;`},
196 {"&apos;&quot;", `&#39;"`},
197 {"&#34", `&#34`},
198 {"&#x22", `&#x22`},
199 {"&apos", `&apos`},
200 {"&amp;", `&`},
201 {"&#39;", `&#39;`},
202 {"&amp;amp;", `&amp;amp;`},
203 {"&amp;#34;", `&amp;#34;`},
204 //{"&amp;a mp;", `&a mp;`},
205 {"&amp;DiacriticalAcute;", `&amp;DiacriticalAcute;`},
206 {"&amp;CounterClockwiseContourIntegral;", `&amp;CounterClockwiseContourIntegral;`},
207 //{"&amp;CounterClockwiseContourIntegralL;", `&CounterClockwiseContourIntegralL;`},
208 {"&amp;parameterize", `&amp;parameterize`},
209 {"&varphi;", "&phiv;"},
210 {"&varpi;", "&piv;"},
211 {"&varnone;", "&varnone;"},
212 }
213 for _, tt := range entityTests {
214 t.Run(tt.entity, func(t *testing.T) {
215 b := ReplaceEntities([]byte(tt.entity), entitiesMap, revEntitiesMap)
216 test.T(t, string(b), tt.expected, "in '"+tt.entity+"'")
217 })
218 }
219 }
220
221 func TestReplaceEntitiesRandom(t *testing.T) {
222 entitiesMap := map[string][]byte{
223 "quot": []byte("\""),
224 "apos": []byte("'"),
225 }
226 revEntitiesMap := map[byte][]byte{
227 '\'': []byte("&#39;"),
228 }
229
230 quotRegexp := regexp.MustCompile("&quot;")
231 aposRegexp := regexp.MustCompile("(&#39;|&#x027;)")
232 for _, e := range entitySlices {
233 reference := quotRegexp.ReplaceAll(e, []byte("\""))
234 reference = aposRegexp.ReplaceAll(reference, []byte("&#39;"))
235 test.Bytes(t, ReplaceEntities(Copy(e), entitiesMap, revEntitiesMap), reference, "in '"+string(e)+"'")
236 }
237 }
238
239 func TestReplaceMultipleWhitespaceAndEntities(t *testing.T) {
240 entitiesMap := map[string][]byte{
241 "varphi": []byte("&phiv;"),
242 }
243 var entityTests = []struct {
244 entity string
245 expected string
246 }{
247 {" ", " "},
248 {" & a ", " & a "},
249 {" &acute a ", " &acute a "},
250 {"\n Lorem & ipsum\n dolor sid amet\n", "\nLorem & ipsum\ndolor sid amet\n"},
251 {" &varphi; &#34; \n ", " &phiv; \"\n"},
252 }
253 for _, tt := range entityTests {
254 t.Run(tt.entity, func(t *testing.T) {
255 b := ReplaceMultipleWhitespaceAndEntities([]byte(tt.entity), entitiesMap, nil)
256 test.T(t, string(b), tt.expected, "in '"+tt.entity+"'")
257 })
258 }
259 }
260
261 func TestReplaceMultipleWhitespaceAndEntitiesRandom(t *testing.T) {
262 entitiesMap := map[string][]byte{
263 "quot": []byte("\""),
264 "apos": []byte("'"),
265 }
266 revEntitiesMap := map[byte][]byte{
267 '\'': []byte("&#39;"),
268 }
269
270 wsRegexp := regexp.MustCompile("[ ]+")
271 quotRegexp := regexp.MustCompile("&quot;")
272 aposRegexp := regexp.MustCompile("(&#39;|&#x027;)")
273 for _, e := range entitySlices {
274 reference := wsRegexp.ReplaceAll(e, []byte(" "))
275 reference = quotRegexp.ReplaceAll(reference, []byte("\""))
276 reference = aposRegexp.ReplaceAll(reference, []byte("&#39;"))
277 test.Bytes(t, ReplaceMultipleWhitespaceAndEntities(Copy(e), entitiesMap, revEntitiesMap), reference, "in '"+string(e)+"'")
278 }
279 }
280
281 func TestDecodeURL(t *testing.T) {
282 var urlTests = []struct {
283 url string
284 expected string
285 }{
286 {"%20%3F%7E", " ?~"},
287 {"%80", "\x80"},
288 {"%2B%2b", "++"},
289 {"%' ", "%' "},
290 {"a+b", "a b"},
291 }
292 for _, tt := range urlTests {
293 t.Run(tt.url, func(t *testing.T) {
294 b := DecodeURL([]byte(tt.url))
295 test.T(t, string(b), tt.expected, "in '"+tt.url+"'")
296 })
297 }
298 }
299
300 func TestDecodeURLRandom(t *testing.T) {
301 for _, e := range encodedURLSlices {
302 reference, _ := url.QueryUnescape(string(e))
303 test.Bytes(t, DecodeURL(Copy(e)), []byte(reference), "in '"+string(e)+"'")
304 }
305 }
306
307 func TestEncodeURL(t *testing.T) {
308 var urlTests = []struct {
309 url string
310 expected string
311 }{
312 {"AZaz09-_.!~*'()", "AZaz09-_.!~*'()"},
313 {"<>", "%3C%3E"},
314 {"\u2318", "%E2%8C%98"},
315 {"a b", "a%20b"},
316 }
317 for _, tt := range urlTests {
318 t.Run(tt.url, func(t *testing.T) {
319 b := EncodeURL([]byte(tt.url), URLEncodingTable)
320 test.T(t, string(b), tt.expected, "in '"+tt.url+"'")
321 })
322 }
323 }
324
325 func TestEncodeDataURI(t *testing.T) {
326 var urlTests = []struct {
327 url string
328 expected string
329 }{
330 {`<svg xmlns="http://www.w3.org/2000/svg"></svg>`, `%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%3E%3C/svg%3E`},
331 }
332 for _, tt := range urlTests {
333 t.Run(tt.url, func(t *testing.T) {
334 b := EncodeURL([]byte(tt.url), DataURIEncodingTable)
335 test.T(t, string(b), tt.expected, "in '"+tt.url+"'")
336 })
337 }
338 }
339
340 func TestEncodeURLRandom(t *testing.T) {
341 for _, e := range urlSlices {
342 reference := url.QueryEscape(string(e))
343 test.Bytes(t, EncodeURL(Copy(e), URLEncodingTable), []byte(reference), "in '"+string(e)+"'")
344 }
345 }
346
347 func TestAppendEscape(t *testing.T) {
348 var tests = []struct {
349 s string
350 expected string
351 }{
352 {`foobar`, `foobar`},
353 {`foo\bar`, `foo\\bar`},
354 {`"foobar"`, `\"foobar\"`},
355 {`\"\\"`, `\\\"\\\\\"`},
356 }
357 for _, tt := range tests {
358 t.Run(tt.s, func(t *testing.T) {
359 b := AppendEscape([]byte(nil), []byte(tt.s), []byte{'"'}, '\\')
360 test.T(t, string(b), tt.expected)
361 })
362 }
363 }
364
365 ////////////////////////////////////////////////////////////////
366
367 func BenchmarkParseMediatypeStd(b *testing.B) {
368 mediatype := "text/plain"
369 for i := 0; i < b.N; i++ {
370 mime.ParseMediaType(mediatype)
371 }
372 }
373
374 func BenchmarkParseMediatypeParamStd(b *testing.B) {
375 mediatype := "text/plain;inline=1"
376 for i := 0; i < b.N; i++ {
377 mime.ParseMediaType(mediatype)
378 }
379 }
380
381 func BenchmarkParseMediatypeParamsStd(b *testing.B) {
382 mediatype := "text/plain;charset=US-ASCII;language=US-EN;compression=gzip;base64"
383 for i := 0; i < b.N; i++ {
384 mime.ParseMediaType(mediatype)
385 }
386 }
387
388 func BenchmarkParseMediatypeParse(b *testing.B) {
389 mediatype := []byte("text/plain")
390 for i := 0; i < b.N; i++ {
391 Mediatype(mediatype)
392 }
393 }
394
395 func BenchmarkParseMediatypeParamParse(b *testing.B) {
396 mediatype := []byte("text/plain;inline=1")
397 for i := 0; i < b.N; i++ {
398 Mediatype(mediatype)
399 }
400 }
401
402 func BenchmarkParseMediatypeParamsParse(b *testing.B) {
403 mediatype := []byte("text/plain;charset=US-ASCII;language=US-EN;compression=gzip;base64")
404 for i := 0; i < b.N; i++ {
405 Mediatype(mediatype)
406 }
407 }
408
409 func BenchmarkReplaceMultipleWhitespace(b *testing.B) {
410 for i := 0; i < b.N; i++ {
411 for _, e := range wsSlices {
412 ReplaceMultipleWhitespace(e)
413 }
414 }
415 }