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 {"attachment; filename={file}.png", "attachment", m("filename", "{file}.png")},
419
420
421 {`multipart/form-data; boundary=a; boundary*=utf-8''b`, "multipart/form-data", m("boundary", "a")},
422 {`multipart/form-data; boundary=a; boundary*0="b"`, "multipart/form-data", m("boundary", "a")},
423 {`multipart/form-data; boundary*0="b"`, "multipart/form-data", m()},
424 {`text; charset=utf-8; charset*=utf-8''utf-7`, "text", m("charset", "utf-8")},
425 {`text; charset=utf-8; charset*0="utf-7"`, "text", m("charset", "utf-8")},
426 {`text; charset*0="utf-7"`, "text", m()},
427 {`text; boundary=a; boundary*0="b"`, "text", m("boundary", "a")},
428 }
429 }
430
431 func TestParseMediaType(t *testing.T) {
432 for _, test := range parseMediaTypeTests {
433 mt, params, err := ParseMediaType(test.in)
434 if err != nil {
435 if test.t != "" {
436 t.Errorf("for input %#q, unexpected error: %v", test.in, err)
437 continue
438 }
439 continue
440 }
441 if g, e := mt, test.t; g != e {
442 t.Errorf("for input %#q, expected type %q, got %q",
443 test.in, e, g)
444 continue
445 }
446 if len(params) == 0 && len(test.p) == 0 {
447 continue
448 }
449 if !maps.Equal(params, test.p) {
450 t.Errorf("for input %#q, wrong params.\n"+
451 "expected: %#v\n"+
452 " got: %#v",
453 test.in, test.p, params)
454 }
455 }
456 }
457
458 func BenchmarkParseMediaType(b *testing.B) {
459 for range b.N {
460 for _, test := range parseMediaTypeTests {
461 ParseMediaType(test.in)
462 }
463 }
464 }
465
466 type badMediaTypeTest struct {
467 in string
468 mt string
469 err string
470 }
471
472 var badMediaTypeTests = []badMediaTypeTest{
473 {"bogus ;=========", "bogus", "mime: invalid media parameter"},
474
475
476 {"application/pdf; x-mac-type=\"3F3F3F3F\"; x-mac-creator=\"3F3F3F3F\" name=\"a.pdf\";",
477 "application/pdf", "mime: invalid media parameter"},
478 {"bogus/<script>alert</script>", "", "mime: expected token after slash"},
479 {"bogus/bogus<script>alert</script>", "", "mime: unexpected content after media subtype"},
480
481 {`"attachment"`, "attachment", "mime: no media type"},
482 {"attachment; filename=foo,bar.html", "attachment", "mime: invalid media parameter"},
483 {"attachment; ;filename=foo", "attachment", "mime: invalid media parameter"},
484 {"attachment; filename=foo bar.html", "attachment", "mime: invalid media parameter"},
485 {`attachment; filename="foo.html"; filename="bar.html"`, "attachment", "mime: duplicate parameter name"},
486 {"attachment; filename=foo[1](2).html", "attachment", "mime: invalid media parameter"},
487 {"attachment; filename=foo-ä.html", "attachment", "mime: invalid media parameter"},
488 {"attachment; filename=foo-ä.html", "attachment", "mime: invalid media parameter"},
489 {`attachment; filename *=UTF-8''foo-%c3%a4.html`, "attachment", "mime: invalid media parameter"},
490 }
491
492 func TestParseMediaTypeBogus(t *testing.T) {
493 for _, tt := range badMediaTypeTests {
494 mt, params, err := ParseMediaType(tt.in)
495 if err == nil {
496 t.Errorf("ParseMediaType(%q) = nil error; want parse error", tt.in)
497 continue
498 }
499 if err.Error() != tt.err {
500 t.Errorf("ParseMediaType(%q) = err %q; want %q", tt.in, err.Error(), tt.err)
501 }
502 if params != nil {
503 t.Errorf("ParseMediaType(%q): got non-nil params on error", tt.in)
504 }
505 if err != ErrInvalidMediaParameter && mt != "" {
506 t.Errorf("ParseMediaType(%q): got unexpected non-empty media type string", tt.in)
507 }
508 if err == ErrInvalidMediaParameter && mt != tt.mt {
509 t.Errorf("ParseMediaType(%q): in case of invalid parameters: expected type %q, got %q", tt.in, tt.mt, mt)
510 }
511 }
512 }
513
514 func BenchmarkParseMediaTypeBogus(b *testing.B) {
515 for range b.N {
516 for _, test := range badMediaTypeTests {
517 ParseMediaType(test.in)
518 }
519 }
520 }
521
522 type formatTest struct {
523 typ string
524 params map[string]string
525 want string
526 }
527
528 var formatTests = []formatTest{
529 {"noslash", map[string]string{"X": "Y"}, "noslash; x=Y"},
530 {"foo bar/baz", nil, ""},
531 {"foo/bar baz", nil, ""},
532 {"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"},
533 {"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"},
534 {"attachment", map[string]string{"filename": "数据统计.png"}, "attachment; filename*=utf-8''%E6%95%B0%E6%8D%AE%E7%BB%9F%E8%AE%A1.png"},
535 {"foo/BAR", nil, "foo/bar"},
536 {"foo/BAR", map[string]string{"X": "Y"}, "foo/bar; x=Y"},
537 {"foo/BAR", map[string]string{"space": "With space"}, `foo/bar; space="With space"`},
538 {"foo/BAR", map[string]string{"quote": `With "quote`}, `foo/bar; quote="With \"quote"`},
539 {"foo/BAR", map[string]string{"bslash": `With \backslash`}, `foo/bar; bslash="With \\backslash"`},
540 {"foo/BAR", map[string]string{"both": `With \backslash and "quote`}, `foo/bar; both="With \\backslash and \"quote"`},
541 {"foo/BAR", map[string]string{"": "empty attribute"}, ""},
542 {"foo/BAR", map[string]string{"bad attribute": "baz"}, ""},
543 {"foo/BAR", map[string]string{"Name": "foo", "name": "bar"}, ""},
544 {"foo/BAR", map[string]string{"nonascii": "not an ascii character: ä"}, "foo/bar; nonascii*=utf-8''not%20an%20ascii%20character%3A%20%C3%A4"},
545 {"foo/BAR", map[string]string{"ctl": "newline: \n nil: \000"}, "foo/bar; ctl*=utf-8''newline%3A%20%0A%20nil%3A%20%00"},
546 {"foo/bar", map[string]string{"a": "av", "b": "bv", "c": "cv"}, "foo/bar; a=av; b=bv; c=cv"},
547 {"foo/bar", map[string]string{"0": "'", "9": "'"}, "foo/bar; 0='; 9='"},
548 {"foo", map[string]string{"bar": ""}, `foo; bar=""`},
549 }
550
551 func TestFormatMediaType(t *testing.T) {
552 for i, tt := range formatTests {
553 got := FormatMediaType(tt.typ, tt.params)
554 if got != tt.want {
555 t.Errorf("%d. FormatMediaType(%q, %v) = %q; want %q", i, tt.typ, tt.params, got, tt.want)
556 }
557 if got == "" {
558 continue
559 }
560 typ, params, err := ParseMediaType(got)
561 if err != nil {
562 t.Errorf("%d. ParseMediaType(%q) err: %v", i, got, err)
563 }
564 if typ != strings.ToLower(tt.typ) {
565 t.Errorf("%d. ParseMediaType(%q) typ = %q; want %q", i, got, typ, tt.typ)
566 }
567 for k, v := range tt.params {
568 k = strings.ToLower(k)
569 if params[k] != v {
570 t.Errorf("%d. ParseMediaType(%q) params[%s] = %q; want %q", i, got, k, params[k], v)
571 }
572 }
573 }
574 }
575
View as plain text