Source file
src/mime/mediatype_test.go
1
2
3
4
5 package mime
6
7 import (
8 "maps"
9 "strings"
10 "testing"
11 )
12
13 func TestConsumeToken(t *testing.T) {
14 tests := [...][3]string{
15 {"foo bar", "foo", " bar"},
16 {"bar", "bar", ""},
17 {"", "", ""},
18 {" foo", "", " foo"},
19 }
20 for _, test := range tests {
21 token, rest := consumeToken(test[0])
22 expectedToken := test[1]
23 expectedRest := test[2]
24 if token != expectedToken {
25 t.Errorf("expected to consume token '%s', not '%s' from '%s'",
26 expectedToken, token, test[0])
27 } else if rest != expectedRest {
28 t.Errorf("expected to have left '%s', not '%s' after reading token '%s' from '%s'",
29 expectedRest, rest, token, test[0])
30 }
31 }
32 }
33
34 func TestConsumeValue(t *testing.T) {
35 tests := [...][3]string{
36 {"foo bar", "foo", " bar"},
37 {"bar", "bar", ""},
38 {" bar ", "", " bar "},
39 {`"My value"end`, "My value", "end"},
40 {`"My value" end`, "My value", " end"},
41 {`"\\" rest`, "\\", " rest"},
42 {`"My \" value"end`, "My \" value", "end"},
43 {`"\" rest`, "", `"\" rest`},
44 {`"C:\dev\go\robots.txt"`, `C:\dev\go\robots.txt`, ""},
45 {`"C:\新建文件夹\中文第二次测试.mp4"`, `C:\新建文件夹\中文第二次测试.mp4`, ""},
46 }
47 for _, test := range tests {
48 value, rest := consumeValue(test[0])
49 expectedValue := test[1]
50 expectedRest := test[2]
51 if value != expectedValue {
52 t.Errorf("expected to consume value [%s], not [%s] from [%s]",
53 expectedValue, value, test[0])
54 } else if rest != expectedRest {
55 t.Errorf("expected to have left [%s], not [%s] after reading value [%s] from [%s]",
56 expectedRest, rest, value, test[0])
57 }
58 }
59 }
60
61 func TestConsumeMediaParam(t *testing.T) {
62 tests := [...][4]string{
63 {" ; foo=bar", "foo", "bar", ""},
64 {"; foo=bar", "foo", "bar", ""},
65 {";foo=bar", "foo", "bar", ""},
66 {";FOO=bar", "foo", "bar", ""},
67 {`;foo="bar"`, "foo", "bar", ""},
68 {`;foo="bar"; `, "foo", "bar", "; "},
69 {`;foo="bar"; foo=baz`, "foo", "bar", "; foo=baz"},
70 {` ; boundary=----CUT;`, "boundary", "----CUT", ";"},
71 {` ; key=value; blah="value";name="foo" `, "key", "value", `; blah="value";name="foo" `},
72 {`; blah="value";name="foo" `, "blah", "value", `;name="foo" `},
73 {`;name="foo" `, "name", "foo", ` `},
74 }
75 for _, test := range tests {
76 param, value, rest := consumeMediaParam(test[0])
77 expectedParam := test[1]
78 expectedValue := test[2]
79 expectedRest := test[3]
80 if param != expectedParam {
81 t.Errorf("expected to consume param [%s], not [%s] from [%s]",
82 expectedParam, param, test[0])
83 } else if value != expectedValue {
84 t.Errorf("expected to consume value [%s], not [%s] from [%s]",
85 expectedValue, value, test[0])
86 } else if rest != expectedRest {
87 t.Errorf("expected to have left [%s], not [%s] after reading [%s/%s] from [%s]",
88 expectedRest, rest, param, value, test[0])
89 }
90 }
91 }
92
93 type mediaTypeTest struct {
94 in string
95 t string
96 p map[string]string
97 }
98
99 var parseMediaTypeTests []mediaTypeTest
100
101 func init() {
102
103 m := func(s ...string) map[string]string {
104 sm := make(map[string]string)
105 for i := 0; i < len(s); i += 2 {
106 sm[s[i]] = s[i+1]
107 }
108 return sm
109 }
110
111 nameFoo := map[string]string{"name": "foo"}
112 parseMediaTypeTests = []mediaTypeTest{
113 {`form-data; name="foo"`, "form-data", nameFoo},
114 {` form-data ; name=foo`, "form-data", nameFoo},
115 {`FORM-DATA;name="foo"`, "form-data", nameFoo},
116 {` FORM-DATA ; name="foo"`, "form-data", nameFoo},
117 {` FORM-DATA ; name="foo"`, "form-data", nameFoo},
118
119 {`form-data; key=value; blah="value";name="foo" `,
120 "form-data",
121 m("key", "value", "blah", "value", "name", "foo")},
122
123 {`foo; key=val1; key=the-key-appears-again-which-is-bogus`,
124 "", m()},
125
126
127 {`application/x-stuff; title*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A`,
128 "application/x-stuff",
129 m("title", "This is ***fun***")},
130
131 {`message/external-body; access-type=URL; ` +
132 `URL*0="ftp://";` +
133 `URL*1="cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar"`,
134 "message/external-body",
135 m("access-type", "URL",
136 "url", "ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar")},
137
138 {`application/x-stuff; ` +
139 `title*0*=us-ascii'en'This%20is%20even%20more%20; ` +
140 `title*1*=%2A%2A%2Afun%2A%2A%2A%20; ` +
141 `title*2="isn't it!"`,
142 "application/x-stuff",
143 m("title", "This is even more ***fun*** isn't it!")},
144
145
146
147
148
149 {`attachment`,
150 "attachment",
151 m()},
152
153 {`ATTACHMENT`,
154 "attachment",
155 m()},
156
157 {`attachment; filename="foo.html"`,
158 "attachment",
159 m("filename", "foo.html")},
160
161 {`attachment; filename="0000000000111111111122222"`,
162 "attachment",
163 m("filename", "0000000000111111111122222")},
164
165 {`attachment; filename="00000000001111111111222222222233333"`,
166 "attachment",
167 m("filename", "00000000001111111111222222222233333")},
168
169 {`attachment; filename="f\oo.html"`,
170 "attachment",
171 m("filename", "f\\oo.html")},
172
173 {`attachment; filename="\"quoting\" tested.html"`,
174 "attachment",
175 m("filename", `"quoting" tested.html`)},
176
177 {`attachment; filename="Here's a semicolon;.html"`,
178 "attachment",
179 m("filename", "Here's a semicolon;.html")},
180
181 {`attachment; foo="bar"; filename="foo.html"`,
182 "attachment",
183 m("foo", "bar", "filename", "foo.html")},
184
185 {`attachment; foo="\"\\";filename="foo.html"`,
186 "attachment",
187 m("foo", "\"\\", "filename", "foo.html")},
188
189 {`attachment; FILENAME="foo.html"`,
190 "attachment",
191 m("filename", "foo.html")},
192
193 {`attachment; filename=foo.html`,
194 "attachment",
195 m("filename", "foo.html")},
196
197 {`attachment; filename=foo.html ;`,
198 "attachment",
199 m("filename", "foo.html")},
200
201 {`attachment; filename='foo.html'`,
202 "attachment",
203 m("filename", "'foo.html'")},
204
205 {`attachment; filename="foo-ä.html"`,
206 "attachment",
207 m("filename", "foo-ä.html")},
208
209 {`attachment; filename="foo-ä.html"`,
210 "attachment",
211 m("filename", "foo-ä.html")},
212
213 {`attachment; filename="foo-%41.html"`,
214 "attachment",
215 m("filename", "foo-%41.html")},
216
217 {`attachment; filename="50%.html"`,
218 "attachment",
219 m("filename", "50%.html")},
220
221 {`attachment; filename="foo-%\41.html"`,
222 "attachment",
223 m("filename", "foo-%\\41.html")},
224
225 {`attachment; name="foo-%41.html"`,
226 "attachment",
227 m("name", "foo-%41.html")},
228
229 {`attachment; name="ä-%41.html"`,
230 "attachment",
231 m("name", "ä-%41.html")},
232
233 {`attachment; filename="foo-%c3%a4-%e2%82%ac.html"`,
234 "attachment",
235 m("filename", "foo-%c3%a4-%e2%82%ac.html")},
236
237 {`attachment; filename ="foo.html"`,
238 "attachment",
239 m("filename", "foo.html")},
240
241 {`filename=foo.html`,
242 "", m()},
243
244 {`x=y; filename=foo.html`,
245 "", m()},
246
247 {`"foo; filename=bar;baz"; filename=qux`,
248 "", m()},
249
250 {`filename=foo.html, filename=bar.html`,
251 "", m()},
252
253 {`; filename=foo.html`,
254 "", m()},
255
256 {`: inline; attachment; filename=foo.html`,
257 "", m()},
258
259 {`inline; attachment; filename=foo.html`,
260 "", m()},
261
262 {`attachment; inline; filename=foo.html`,
263 "", m()},
264
265 {`attachment; filename="foo.html".txt`,
266 "", m()},
267
268 {`attachment; filename="bar`,
269 "", m()},
270
271 {`attachment; filename=foo"bar;baz"qux`,
272 "", m()},
273
274 {`attachment; filename=foo.html, attachment; filename=bar.html`,
275 "", m()},
276
277 {`attachment; foo=foo filename=bar`,
278 "", m()},
279
280 {`attachment; filename=bar foo=foo`,
281 "", m()},
282
283 {`attachment filename=bar`,
284 "", m()},
285
286 {`filename=foo.html; attachment`,
287 "", m()},
288
289 {`attachment; xfilename=foo.html`,
290 "attachment",
291 m("xfilename", "foo.html")},
292
293 {`attachment; creation-date="Wed, 12 Feb 1997 16:29:51 -0500"`,
294 "attachment",
295 m("creation-date", "Wed, 12 Feb 1997 16:29:51 -0500")},
296
297 {`attachment; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"`,
298 "attachment",
299 m("modification-date", "Wed, 12 Feb 1997 16:29:51 -0500")},
300
301 {`foobar`, "foobar", m()},
302
303 {`attachment; example="filename=example.txt"`,
304 "attachment",
305 m("example", "filename=example.txt")},
306
307 {`attachment; filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html`,
308 "attachment",
309 m("filename", "foo-ä-€.html")},
310
311 {`attachment; filename*=''foo-%c3%a4-%e2%82%ac.html`,
312 "attachment",
313 m()},
314
315 {`attachment; filename*=UTF-8''foo-a%cc%88.html`,
316 "attachment",
317 m("filename", "foo-ä.html")},
318
319 {`attachment; filename*= UTF-8''foo-%c3%a4.html`,
320 "attachment",
321 m("filename", "foo-ä.html")},
322
323 {`attachment; filename* =UTF-8''foo-%c3%a4.html`,
324 "attachment",
325 m("filename", "foo-ä.html")},
326
327 {`attachment; filename*="UTF-8''foo-%c3%a4.html"`,
328 "attachment",
329 m("filename", "foo-ä.html")},
330
331 {`attachment; filename*="foo%20bar.html"`,
332 "attachment",
333 m()},
334
335 {`attachment; filename*=UTF-8'foo-%c3%a4.html`,
336 "attachment",
337 m()},
338
339 {`attachment; filename*=UTF-8''foo%`,
340 "attachment",
341 m()},
342
343 {`attachment; filename*=UTF-8''f%oo.html`,
344 "attachment",
345 m()},
346
347 {`attachment; filename*=UTF-8''A-%2541.html`,
348 "attachment",
349 m("filename", "A-%41.html")},
350
351 {`attachment; filename*0="foo."; filename*1="html"`,
352 "attachment",
353 m("filename", "foo.html")},
354
355 {`attachment; filename*0*=UTF-8''foo-%c3%a4; filename*1=".html"`,
356 "attachment",
357 m("filename", "foo-ä.html")},
358
359 {`attachment; filename*0="foo"; filename*01="bar"`,
360 "attachment",
361 m("filename", "foo")},
362
363 {`attachment; filename*0="foo"; filename*2="bar"`,
364 "attachment",
365 m("filename", "foo")},
366
367 {`attachment; filename*1="foo."; filename*2="html"`,
368 "attachment", m()},
369
370 {`attachment; filename*1="bar"; filename*0="foo"`,
371 "attachment",
372 m("filename", "foobar")},
373
374 {`attachment; filename="foo-ae.html"; filename*=UTF-8''foo-%c3%a4.html`,
375 "attachment",
376 m("filename", "foo-ä.html")},
377
378 {`attachment; filename*=UTF-8''foo-%c3%a4.html; filename="foo-ae.html"`,
379 "attachment",
380 m("filename", "foo-ä.html")},
381
382 {`attachment; filename*0*=ISO-8859-15''euro-sign%3d%a4; filename*=ISO-8859-1''currency-sign%3d%a4`,
383 "attachment",
384 m()},
385
386 {`attachment; foobar=x; filename="foo.html"`,
387 "attachment",
388 m("foobar", "x", "filename", "foo.html")},
389
390
391
392 {`form-data; firstname="Брэд"; lastname="Фицпатрик"`,
393 "form-data",
394 m("firstname", "Брэд", "lastname", "Фицпатрик")},
395
396
397 {`foo; bar=""`, "foo", m("bar", "")},
398
399
400 {`form-data; name="file"; filename="C:\dev\go\robots.txt"`, "form-data", m("name", "file", "filename", `C:\dev\go\robots.txt`)},
401 {`form-data; name="file"; filename="C:\新建文件夹\中文第二次测试.mp4"`, "form-data", m("name", "file", "filename", `C:\新建文件夹\中文第二次测试.mp4`)},
402
403
404 {
405
406 `message/external-body; access-type=URL;
407 URL*0="ftp://";
408 URL*1="cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar";`,
409 `message/external-body`,
410 m("access-type", "URL", "url", "ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar"),
411 },
412
413
414 {`text; charset=utf-8; charset=utf-8; format=fixed`, "text", m("charset", "utf-8", "format", "fixed")},
415 {`text; charset=utf-8; format=flowed; charset=utf-8`, "text", m("charset", "utf-8", "format", "flowed")},
416 }
417 }
418
419 func TestParseMediaType(t *testing.T) {
420 for _, test := range parseMediaTypeTests {
421 mt, params, err := ParseMediaType(test.in)
422 if err != nil {
423 if test.t != "" {
424 t.Errorf("for input %#q, unexpected error: %v", test.in, err)
425 continue
426 }
427 continue
428 }
429 if g, e := mt, test.t; g != e {
430 t.Errorf("for input %#q, expected type %q, got %q",
431 test.in, e, g)
432 continue
433 }
434 if len(params) == 0 && len(test.p) == 0 {
435 continue
436 }
437 if !maps.Equal(params, test.p) {
438 t.Errorf("for input %#q, wrong params.\n"+
439 "expected: %#v\n"+
440 " got: %#v",
441 test.in, test.p, params)
442 }
443 }
444 }
445
446 func BenchmarkParseMediaType(b *testing.B) {
447 for range b.N {
448 for _, test := range parseMediaTypeTests {
449 ParseMediaType(test.in)
450 }
451 }
452 }
453
454 type badMediaTypeTest struct {
455 in string
456 mt string
457 err string
458 }
459
460 var badMediaTypeTests = []badMediaTypeTest{
461 {"bogus ;=========", "bogus", "mime: invalid media parameter"},
462
463
464 {"application/pdf; x-mac-type=\"3F3F3F3F\"; x-mac-creator=\"3F3F3F3F\" name=\"a.pdf\";",
465 "application/pdf", "mime: invalid media parameter"},
466 {"bogus/<script>alert</script>", "", "mime: expected token after slash"},
467 {"bogus/bogus<script>alert</script>", "", "mime: unexpected content after media subtype"},
468
469 {`"attachment"`, "attachment", "mime: no media type"},
470 {"attachment; filename=foo,bar.html", "attachment", "mime: invalid media parameter"},
471 {"attachment; ;filename=foo", "attachment", "mime: invalid media parameter"},
472 {"attachment; filename=foo bar.html", "attachment", "mime: invalid media parameter"},
473 {`attachment; filename="foo.html"; filename="bar.html"`, "attachment", "mime: duplicate parameter name"},
474 {"attachment; filename=foo[1](2).html", "attachment", "mime: invalid media parameter"},
475 {"attachment; filename=foo-ä.html", "attachment", "mime: invalid media parameter"},
476 {"attachment; filename=foo-ä.html", "attachment", "mime: invalid media parameter"},
477 {`attachment; filename *=UTF-8''foo-%c3%a4.html`, "attachment", "mime: invalid media parameter"},
478 }
479
480 func TestParseMediaTypeBogus(t *testing.T) {
481 for _, tt := range badMediaTypeTests {
482 mt, params, err := ParseMediaType(tt.in)
483 if err == nil {
484 t.Errorf("ParseMediaType(%q) = nil error; want parse error", tt.in)
485 continue
486 }
487 if err.Error() != tt.err {
488 t.Errorf("ParseMediaType(%q) = err %q; want %q", tt.in, err.Error(), tt.err)
489 }
490 if params != nil {
491 t.Errorf("ParseMediaType(%q): got non-nil params on error", tt.in)
492 }
493 if err != ErrInvalidMediaParameter && mt != "" {
494 t.Errorf("ParseMediaType(%q): got unexpected non-empty media type string", tt.in)
495 }
496 if err == ErrInvalidMediaParameter && mt != tt.mt {
497 t.Errorf("ParseMediaType(%q): in case of invalid parameters: expected type %q, got %q", tt.in, tt.mt, mt)
498 }
499 }
500 }
501
502 func BenchmarkParseMediaTypeBogus(b *testing.B) {
503 for range b.N {
504 for _, test := range badMediaTypeTests {
505 ParseMediaType(test.in)
506 }
507 }
508 }
509
510 type formatTest struct {
511 typ string
512 params map[string]string
513 want string
514 }
515
516 var formatTests = []formatTest{
517 {"noslash", map[string]string{"X": "Y"}, "noslash; x=Y"},
518 {"foo bar/baz", nil, ""},
519 {"foo/bar baz", nil, ""},
520 {"attachment", map[string]string{"filename": "ĄĄŽŽČČŠŠ"}, "attachment; filename*=utf-8''%C4%84%C4%84%C5%BD%C5%BD%C4%8C%C4%8C%C5%A0%C5%A0"},
521 {"attachment", map[string]string{"filename": "ÁÁÊÊÇÇÎÎ"}, "attachment; filename*=utf-8''%C3%81%C3%81%C3%8A%C3%8A%C3%87%C3%87%C3%8E%C3%8E"},
522 {"attachment", map[string]string{"filename": "数据统计.png"}, "attachment; filename*=utf-8''%E6%95%B0%E6%8D%AE%E7%BB%9F%E8%AE%A1.png"},
523 {"foo/BAR", nil, "foo/bar"},
524 {"foo/BAR", map[string]string{"X": "Y"}, "foo/bar; x=Y"},
525 {"foo/BAR", map[string]string{"space": "With space"}, `foo/bar; space="With space"`},
526 {"foo/BAR", map[string]string{"quote": `With "quote`}, `foo/bar; quote="With \"quote"`},
527 {"foo/BAR", map[string]string{"bslash": `With \backslash`}, `foo/bar; bslash="With \\backslash"`},
528 {"foo/BAR", map[string]string{"both": `With \backslash and "quote`}, `foo/bar; both="With \\backslash and \"quote"`},
529 {"foo/BAR", map[string]string{"": "empty attribute"}, ""},
530 {"foo/BAR", map[string]string{"bad attribute": "baz"}, ""},
531 {"foo/BAR", map[string]string{"nonascii": "not an ascii character: ä"}, "foo/bar; nonascii*=utf-8''not%20an%20ascii%20character%3A%20%C3%A4"},
532 {"foo/BAR", map[string]string{"ctl": "newline: \n nil: \000"}, "foo/bar; ctl*=utf-8''newline%3A%20%0A%20nil%3A%20%00"},
533 {"foo/bar", map[string]string{"a": "av", "b": "bv", "c": "cv"}, "foo/bar; a=av; b=bv; c=cv"},
534 {"foo/bar", map[string]string{"0": "'", "9": "'"}, "foo/bar; 0='; 9='"},
535 {"foo", map[string]string{"bar": ""}, `foo; bar=""`},
536 }
537
538 func TestFormatMediaType(t *testing.T) {
539 for i, tt := range formatTests {
540 got := FormatMediaType(tt.typ, tt.params)
541 if got != tt.want {
542 t.Errorf("%d. FormatMediaType(%q, %v) = %q; want %q", i, tt.typ, tt.params, got, tt.want)
543 }
544 if got == "" {
545 continue
546 }
547 typ, params, err := ParseMediaType(got)
548 if err != nil {
549 t.Errorf("%d. ParseMediaType(%q) err: %v", i, got, err)
550 }
551 if typ != strings.ToLower(tt.typ) {
552 t.Errorf("%d. ParseMediaType(%q) typ = %q; want %q", i, got, typ, tt.typ)
553 }
554 for k, v := range tt.params {
555 k = strings.ToLower(k)
556 if params[k] != v {
557 t.Errorf("%d. ParseMediaType(%q) params[%s] = %q; want %q", i, got, k, params[k], v)
558 }
559 }
560 }
561 }
562
View as plain text