| 1 |
// Copyright 2010 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 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 |
// Convenience map initializer |
| 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 |
// From RFC 2231: |
| 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 |
// Tests from http://greenbytes.de/tech/tc2231/ |
| 146 |
// Note: Backslash escape handling is a bit loose, like MSIE. |
| 147 |
|
| 148 |
// #attonly |
| 149 |
{`attachment`, |
| 150 |
"attachment", |
| 151 |
m()}, |
| 152 |
// #attonlyucase |
| 153 |
{`ATTACHMENT`, |
| 154 |
"attachment", |
| 155 |
m()}, |
| 156 |
// #attwithasciifilename |
| 157 |
{`attachment; filename="foo.html"`, |
| 158 |
"attachment", |
| 159 |
m("filename", "foo.html")}, |
| 160 |
// #attwithasciifilename25 |
| 161 |
{`attachment; filename="0000000000111111111122222"`, |
| 162 |
"attachment", |
| 163 |
m("filename", "0000000000111111111122222")}, |
| 164 |
// #attwithasciifilename35 |
| 165 |
{`attachment; filename="00000000001111111111222222222233333"`, |
| 166 |
"attachment", |
| 167 |
m("filename", "00000000001111111111222222222233333")}, |
| 168 |
// #attwithasciifnescapedchar |
| 169 |
{`attachment; filename="f\oo.html"`, |
| 170 |
"attachment", |
| 171 |
m("filename", "f\\oo.html")}, |
| 172 |
// #attwithasciifnescapedquote |
| 173 |
{`attachment; filename="\"quoting\" tested.html"`, |
| 174 |
"attachment", |
| 175 |
m("filename", `"quoting" tested.html`)}, |
| 176 |
// #attwithquotedsemicolon |
| 177 |
{`attachment; filename="Here's a semicolon;.html"`, |
| 178 |
"attachment", |
| 179 |
m("filename", "Here's a semicolon;.html")}, |
| 180 |
// #attwithfilenameandextparam |
| 181 |
{`attachment; foo="bar"; filename="foo.html"`, |
| 182 |
"attachment", |
| 183 |
m("foo", "bar", "filename", "foo.html")}, |
| 184 |
// #attwithfilenameandextparamescaped |
| 185 |
{`attachment; foo="\"\\";filename="foo.html"`, |
| 186 |
"attachment", |
| 187 |
m("foo", "\"\\", "filename", "foo.html")}, |
| 188 |
// #attwithasciifilenameucase |
| 189 |
{`attachment; FILENAME="foo.html"`, |
| 190 |
"attachment", |
| 191 |
m("filename", "foo.html")}, |
| 192 |
// #attwithasciifilenamenq |
| 193 |
{`attachment; filename=foo.html`, |
| 194 |
"attachment", |
| 195 |
m("filename", "foo.html")}, |
| 196 |
// #attwithasciifilenamenqs |
| 197 |
{`attachment; filename=foo.html ;`, |
| 198 |
"attachment", |
| 199 |
m("filename", "foo.html")}, |
| 200 |
// #attwithfntokensq |
| 201 |
{`attachment; filename='foo.html'`, |
| 202 |
"attachment", |
| 203 |
m("filename", "'foo.html'")}, |
| 204 |
// #attwithisofnplain |
| 205 |
{`attachment; filename="foo-ä.html"`, |
| 206 |
"attachment", |
| 207 |
m("filename", "foo-ä.html")}, |
| 208 |
// #attwithutf8fnplain |
| 209 |
{`attachment; filename="foo-ä.html"`, |
| 210 |
"attachment", |
| 211 |
m("filename", "foo-ä.html")}, |
| 212 |
// #attwithfnrawpctenca |
| 213 |
{`attachment; filename="foo-%41.html"`, |
| 214 |
"attachment", |
| 215 |
m("filename", "foo-%41.html")}, |
| 216 |
// #attwithfnusingpct |
| 217 |
{`attachment; filename="50%.html"`, |
| 218 |
"attachment", |
| 219 |
m("filename", "50%.html")}, |
| 220 |
// #attwithfnrawpctencaq |
| 221 |
{`attachment; filename="foo-%\41.html"`, |
| 222 |
"attachment", |
| 223 |
m("filename", "foo-%\\41.html")}, |
| 224 |
// #attwithnamepct |
| 225 |
{`attachment; name="foo-%41.html"`, |
| 226 |
"attachment", |
| 227 |
m("name", "foo-%41.html")}, |
| 228 |
// #attwithfilenamepctandiso |
| 229 |
{`attachment; name="ä-%41.html"`, |
| 230 |
"attachment", |
| 231 |
m("name", "ä-%41.html")}, |
| 232 |
// #attwithfnrawpctenclong |
| 233 |
{`attachment; filename="foo-%c3%a4-%e2%82%ac.html"`, |
| 234 |
"attachment", |
| 235 |
m("filename", "foo-%c3%a4-%e2%82%ac.html")}, |
| 236 |
// #attwithasciifilenamews1 |
| 237 |
{`attachment; filename ="foo.html"`, |
| 238 |
"attachment", |
| 239 |
m("filename", "foo.html")}, |
| 240 |
// #attmissingdisposition |
| 241 |
{`filename=foo.html`, |
| 242 |
"", m()}, |
| 243 |
// #attmissingdisposition2 |
| 244 |
{`x=y; filename=foo.html`, |
| 245 |
"", m()}, |
| 246 |
// #attmissingdisposition3 |
| 247 |
{`"foo; filename=bar;baz"; filename=qux`, |
| 248 |
"", m()}, |
| 249 |
// #attmissingdisposition4 |
| 250 |
{`filename=foo.html, filename=bar.html`, |
| 251 |
"", m()}, |
| 252 |
// #emptydisposition |
| 253 |
{`; filename=foo.html`, |
| 254 |
"", m()}, |
| 255 |
// #doublecolon |
| 256 |
{`: inline; attachment; filename=foo.html`, |
| 257 |
"", m()}, |
| 258 |
// #attandinline |
| 259 |
{`inline; attachment; filename=foo.html`, |
| 260 |
"", m()}, |
| 261 |
// #attandinline2 |
| 262 |
{`attachment; inline; filename=foo.html`, |
| 263 |
"", m()}, |
| 264 |
// #attbrokenquotedfn |
| 265 |
{`attachment; filename="foo.html".txt`, |
| 266 |
"", m()}, |
| 267 |
// #attbrokenquotedfn2 |
| 268 |
{`attachment; filename="bar`, |
| 269 |
"", m()}, |
| 270 |
// #attbrokenquotedfn3 |
| 271 |
{`attachment; filename=foo"bar;baz"qux`, |
| 272 |
"", m()}, |
| 273 |
// #attmultinstances |
| 274 |
{`attachment; filename=foo.html, attachment; filename=bar.html`, |
| 275 |
"", m()}, |
| 276 |
// #attmissingdelim |
| 277 |
{`attachment; foo=foo filename=bar`, |
| 278 |
"", m()}, |
| 279 |
// #attmissingdelim2 |
| 280 |
{`attachment; filename=bar foo=foo`, |
| 281 |
"", m()}, |
| 282 |
// #attmissingdelim3 |
| 283 |
{`attachment filename=bar`, |
| 284 |
"", m()}, |
| 285 |
// #attreversed |
| 286 |
{`filename=foo.html; attachment`, |
| 287 |
"", m()}, |
| 288 |
// #attconfusedparam |
| 289 |
{`attachment; xfilename=foo.html`, |
| 290 |
"attachment", |
| 291 |
m("xfilename", "foo.html")}, |
| 292 |
// #attcdate |
| 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 |
// #attmdate |
| 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 |
// #dispext |
| 301 |
{`foobar`, "foobar", m()}, |
| 302 |
// #dispextbadfn |
| 303 |
{`attachment; example="filename=example.txt"`, |
| 304 |
"attachment", |
| 305 |
m("example", "filename=example.txt")}, |
| 306 |
// #attwithfn2231utf8 |
| 307 |
{`attachment; filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html`, |
| 308 |
"attachment", |
| 309 |
m("filename", "foo-ä-€.html")}, |
| 310 |
// #attwithfn2231noc |
| 311 |
{`attachment; filename*=''foo-%c3%a4-%e2%82%ac.html`, |
| 312 |
"attachment", |
| 313 |
m()}, |
| 314 |
// #attwithfn2231utf8comp |
| 315 |
{`attachment; filename*=UTF-8''foo-a%cc%88.html`, |
| 316 |
"attachment", |
| 317 |
m("filename", "foo-ä.html")}, |
| 318 |
// #attwithfn2231ws2 |
| 319 |
{`attachment; filename*= UTF-8''foo-%c3%a4.html`, |
| 320 |
"attachment", |
| 321 |
m("filename", "foo-ä.html")}, |
| 322 |
// #attwithfn2231ws3 |
| 323 |
{`attachment; filename* =UTF-8''foo-%c3%a4.html`, |
| 324 |
"attachment", |
| 325 |
m("filename", "foo-ä.html")}, |
| 326 |
// #attwithfn2231quot |
| 327 |
{`attachment; filename*="UTF-8''foo-%c3%a4.html"`, |
| 328 |
"attachment", |
| 329 |
m("filename", "foo-ä.html")}, |
| 330 |
// #attwithfn2231quot2 |
| 331 |
{`attachment; filename*="foo%20bar.html"`, |
| 332 |
"attachment", |
| 333 |
m()}, |
| 334 |
// #attwithfn2231singleqmissing |
| 335 |
{`attachment; filename*=UTF-8'foo-%c3%a4.html`, |
| 336 |
"attachment", |
| 337 |
m()}, |
| 338 |
// #attwithfn2231nbadpct1 |
| 339 |
{`attachment; filename*=UTF-8''foo%`, |
| 340 |
"attachment", |
| 341 |
m()}, |
| 342 |
// #attwithfn2231nbadpct2 |
| 343 |
{`attachment; filename*=UTF-8''f%oo.html`, |
| 344 |
"attachment", |
| 345 |
m()}, |
| 346 |
// #attwithfn2231dpct |
| 347 |
{`attachment; filename*=UTF-8''A-%2541.html`, |
| 348 |
"attachment", |
| 349 |
m("filename", "A-%41.html")}, |
| 350 |
// #attfncont |
| 351 |
{`attachment; filename*0="foo."; filename*1="html"`, |
| 352 |
"attachment", |
| 353 |
m("filename", "foo.html")}, |
| 354 |
// #attfncontenc |
| 355 |
{`attachment; filename*0*=UTF-8''foo-%c3%a4; filename*1=".html"`, |
| 356 |
"attachment", |
| 357 |
m("filename", "foo-ä.html")}, |
| 358 |
// #attfncontlz |
| 359 |
{`attachment; filename*0="foo"; filename*01="bar"`, |
| 360 |
"attachment", |
| 361 |
m("filename", "foo")}, |
| 362 |
// #attfncontnc |
| 363 |
{`attachment; filename*0="foo"; filename*2="bar"`, |
| 364 |
"attachment", |
| 365 |
m("filename", "foo")}, |
| 366 |
// #attfnconts1 |
| 367 |
{`attachment; filename*1="foo."; filename*2="html"`, |
| 368 |
"attachment", m()}, |
| 369 |
// #attfncontord |
| 370 |
{`attachment; filename*1="bar"; filename*0="foo"`, |
| 371 |
"attachment", |
| 372 |
m("filename", "foobar")}, |
| 373 |
// #attfnboth |
| 374 |
{`attachment; filename="foo-ae.html"; filename*=UTF-8''foo-%c3%a4.html`, |
| 375 |
"attachment", |
| 376 |
m("filename", "foo-ä.html")}, |
| 377 |
// #attfnboth2 |
| 378 |
{`attachment; filename*=UTF-8''foo-%c3%a4.html; filename="foo-ae.html"`, |
| 379 |
"attachment", |
| 380 |
m("filename", "foo-ä.html")}, |
| 381 |
// #attfnboth3 |
| 382 |
{`attachment; filename*0*=ISO-8859-15''euro-sign%3d%a4; filename*=ISO-8859-1''currency-sign%3d%a4`, |
| 383 |
"attachment", |
| 384 |
m()}, |
| 385 |
// #attnewandfn |
| 386 |
{`attachment; foobar=x; filename="foo.html"`, |
| 387 |
"attachment", |
| 388 |
m("foobar", "x", "filename", "foo.html")}, |
| 389 |
|
| 390 |
// Browsers also just send UTF-8 directly without RFC 2231, |
| 391 |
// at least when the source page is served with UTF-8. |
| 392 |
{`form-data; firstname="Брэд"; lastname="Фицпатрик"`, |
| 393 |
"form-data", |
| 394 |
m("firstname", "Брэд", "lastname", "Фицпатрик")}, |
| 395 |
|
| 396 |
// Empty string used to be mishandled. |
| 397 |
{`foo; bar=""`, "foo", m("bar", "")}, |
| 398 |
|
| 399 |
// Microsoft browsers in intranet mode do not think they need to escape \ in file name. |
| 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 |
// issue #46323 (https://github.com/golang/go/issues/46323) |
| 404 |
{ |
| 405 |
// example from rfc2231-p.3 (https://datatracker.ietf.org/doc/html/rfc2231) |
| 406 |
`message/external-body; access-type=URL; |
| 407 |
URL*0="ftp://"; |
| 408 |
URL*1="cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar";`, // <-- trailing semicolon |
| 409 |
`message/external-body`, |
| 410 |
m("access-type", "URL", "url", "ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar"), |
| 411 |
}, |
| 412 |
|
| 413 |
// Issue #48866: duplicate parameters containing equal values should be allowed |
| 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 |
// Issue #76236: '{' and '}' are token chars. |
| 418 |
{"attachment; filename={file}.png", "attachment", m("filename", "{file}.png")}, |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
func TestParseMediaType(t *testing.T) { |
| 423 |
for _, test := range parseMediaTypeTests { |
| 424 |
mt, params, err := ParseMediaType(test.in) |
| 425 |
if err != nil { |
| 426 |
if test.t != "" { |
| 427 |
t.Errorf("for input %#q, unexpected error: %v", test.in, err) |
| 428 |
continue |
| 429 |
} |
| 430 |
continue |
| 431 |
} |
| 432 |
if g, e := mt, test.t; g != e { |
| 433 |
t.Errorf("for input %#q, expected type %q, got %q", |
| 434 |
test.in, e, g) |
| 435 |
continue |
| 436 |
} |
| 437 |
if len(params) == 0 && len(test.p) == 0 { |
| 438 |
continue |
| 439 |
} |
| 440 |
if !maps.Equal(params, test.p) { |
| 441 |
t.Errorf("for input %#q, wrong params.\n"+ |
| 442 |
"expected: %#v\n"+ |
| 443 |
" got: %#v", |
| 444 |
test.in, test.p, params) |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
func BenchmarkParseMediaType(b *testing.B) { |
| 450 |
for range b.N { |
| 451 |
for _, test := range parseMediaTypeTests { |
| 452 |
ParseMediaType(test.in) |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
type badMediaTypeTest struct { |
| 458 |
in string |
| 459 |
mt string |
| 460 |
err string |
| 461 |
} |
| 462 |
|
| 463 |
var badMediaTypeTests = []badMediaTypeTest{ |
| 464 |
{"bogus ;=========", "bogus", "mime: invalid media parameter"}, |
| 465 |
// The following example is from real email delivered by gmail (error: missing semicolon) |
| 466 |
// and it is there to check behavior described in #19498 |
| 467 |
{"application/pdf; x-mac-type=\"3F3F3F3F\"; x-mac-creator=\"3F3F3F3F\" name=\"a.pdf\";", |
| 468 |
"application/pdf", "mime: invalid media parameter"}, |
| 469 |
{"bogus/<script>alert</script>", "", "mime: expected token after slash"}, |
| 470 |
{"bogus/bogus<script>alert</script>", "", "mime: unexpected content after media subtype"}, |
| 471 |
// Tests from http://greenbytes.de/tech/tc2231/ |
| 472 |
{`"attachment"`, "attachment", "mime: no media type"}, |
| 473 |
{"attachment; filename=foo,bar.html", "attachment", "mime: invalid media parameter"}, |
| 474 |
{"attachment; ;filename=foo", "attachment", "mime: invalid media parameter"}, |
| 475 |
{"attachment; filename=foo bar.html", "attachment", "mime: invalid media parameter"}, |
| 476 |
{`attachment; filename="foo.html"; filename="bar.html"`, "attachment", "mime: duplicate parameter name"}, |
| 477 |
{"attachment; filename=foo[1](2).html", "attachment", "mime: invalid media parameter"}, |
| 478 |
{"attachment; filename=foo-ä.html", "attachment", "mime: invalid media parameter"}, |
| 479 |
{"attachment; filename=foo-ä.html", "attachment", "mime: invalid media parameter"}, |
| 480 |
{`attachment; filename *=UTF-8''foo-%c3%a4.html`, "attachment", "mime: invalid media parameter"}, |
| 481 |
} |
| 482 |
|
| 483 |
func TestParseMediaTypeBogus(t *testing.T) { |
| 484 |
for _, tt := range badMediaTypeTests { |
| 485 |
mt, params, err := ParseMediaType(tt.in) |
| 486 |
if err == nil { |
| 487 |
t.Errorf("ParseMediaType(%q) = nil error; want parse error", tt.in) |
| 488 |
continue |
| 489 |
} |
| 490 |
if err.Error() != tt.err { |
| 491 |
t.Errorf("ParseMediaType(%q) = err %q; want %q", tt.in, err.Error(), tt.err) |
| 492 |
} |
| 493 |
if params != nil { |
| 494 |
t.Errorf("ParseMediaType(%q): got non-nil params on error", tt.in) |
| 495 |
} |
| 496 |
if err != ErrInvalidMediaParameter && mt != "" { |
| 497 |
t.Errorf("ParseMediaType(%q): got unexpected non-empty media type string", tt.in) |
| 498 |
} |
| 499 |
if err == ErrInvalidMediaParameter && mt != tt.mt { |
| 500 |
t.Errorf("ParseMediaType(%q): in case of invalid parameters: expected type %q, got %q", tt.in, tt.mt, mt) |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
func BenchmarkParseMediaTypeBogus(b *testing.B) { |
| 506 |
for range b.N { |
| 507 |
for _, test := range badMediaTypeTests { |
| 508 |
ParseMediaType(test.in) |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
type formatTest struct { |
| 514 |
typ string |
| 515 |
params map[string]string |
| 516 |
want string |
| 517 |
} |
| 518 |
|
| 519 |
var formatTests = []formatTest{ |
| 520 |
{"noslash", map[string]string{"X": "Y"}, "noslash; x=Y"}, // e.g. Content-Disposition values (RFC 2183); issue 11289 |
| 521 |
{"foo bar/baz", nil, ""}, |
| 522 |
{"foo/bar baz", nil, ""}, |
| 523 |
{"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"}, |
| 524 |
{"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"}, |
| 525 |
{"attachment", map[string]string{"filename": "数据统计.png"}, "attachment; filename*=utf-8''%E6%95%B0%E6%8D%AE%E7%BB%9F%E8%AE%A1.png"}, |
| 526 |
{"foo/BAR", nil, "foo/bar"}, |
| 527 |
{"foo/BAR", map[string]string{"X": "Y"}, "foo/bar; x=Y"}, |
| 528 |
{"foo/BAR", map[string]string{"space": "With space"}, `foo/bar; space="With space"`}, |
| 529 |
{"foo/BAR", map[string]string{"quote": `With "quote`}, `foo/bar; quote="With \"quote"`}, |
| 530 |
{"foo/BAR", map[string]string{"bslash": `With \backslash`}, `foo/bar; bslash="With \\backslash"`}, |
| 531 |
{"foo/BAR", map[string]string{"both": `With \backslash and "quote`}, `foo/bar; both="With \\backslash and \"quote"`}, |
| 532 |
{"foo/BAR", map[string]string{"": "empty attribute"}, ""}, |
| 533 |
{"foo/BAR", map[string]string{"bad attribute": "baz"}, ""}, |
| 534 |
{"foo/BAR", map[string]string{"nonascii": "not an ascii character: ä"}, "foo/bar; nonascii*=utf-8''not%20an%20ascii%20character%3A%20%C3%A4"}, |
| 535 |
{"foo/BAR", map[string]string{"ctl": "newline: \n nil: \000"}, "foo/bar; ctl*=utf-8''newline%3A%20%0A%20nil%3A%20%00"}, |
| 536 |
{"foo/bar", map[string]string{"a": "av", "b": "bv", "c": "cv"}, "foo/bar; a=av; b=bv; c=cv"}, |
| 537 |
{"foo/bar", map[string]string{"0": "'", "9": "'"}, "foo/bar; 0='; 9='"}, |
| 538 |
{"foo", map[string]string{"bar": ""}, `foo; bar=""`}, |
| 539 |
} |
| 540 |
|
| 541 |
func TestFormatMediaType(t *testing.T) { |
| 542 |
for i, tt := range formatTests { |
| 543 |
got := FormatMediaType(tt.typ, tt.params) |
| 544 |
if got != tt.want { |
| 545 |
t.Errorf("%d. FormatMediaType(%q, %v) = %q; want %q", i, tt.typ, tt.params, got, tt.want) |
| 546 |
} |
| 547 |
if got == "" { |
| 548 |
continue |
| 549 |
} |
| 550 |
typ, params, err := ParseMediaType(got) |
| 551 |
if err != nil { |
| 552 |
t.Errorf("%d. ParseMediaType(%q) err: %v", i, got, err) |
| 553 |
} |
| 554 |
if typ != strings.ToLower(tt.typ) { |
| 555 |
t.Errorf("%d. ParseMediaType(%q) typ = %q; want %q", i, got, typ, tt.typ) |
| 556 |
} |
| 557 |
for k, v := range tt.params { |
| 558 |
k = strings.ToLower(k) |
| 559 |
if params[k] != v { |
| 560 |
t.Errorf("%d. ParseMediaType(%q) params[%s] = %q; want %q", i, got, k, params[k], v) |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
} |