| 1 |
package mediatype |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
) |
| 6 |
|
| 7 |
func TestFixMangledMediaType(t *testing.T) { |
| 8 |
testCases := []struct { |
| 9 |
input string |
| 10 |
sep rune |
| 11 |
want string |
| 12 |
}{ |
| 13 |
{ |
| 14 |
input: "", |
| 15 |
sep: ';', |
| 16 |
want: "", |
| 17 |
}, |
| 18 |
{ |
| 19 |
input: `text/HTML; charset=UTF-8; format=flowed; content-transfer-encoding: 7bit=`, |
| 20 |
sep: ';', |
| 21 |
want: "text/HTML; charset=UTF-8; format=flowed", |
| 22 |
}, |
| 23 |
{ |
| 24 |
input: "text/html;charset=", |
| 25 |
sep: ';', |
| 26 |
want: "text/html;charset=", |
| 27 |
}, |
| 28 |
{ |
| 29 |
input: "text/;charset=", |
| 30 |
sep: ';', |
| 31 |
want: "text/plain;charset=", |
| 32 |
}, |
| 33 |
{ |
| 34 |
input: "multipart/;charset=", |
| 35 |
sep: ';', |
| 36 |
want: "multipart/mixed;charset=", |
| 37 |
}, |
| 38 |
{ |
| 39 |
input: "text/plain;", |
| 40 |
sep: ';', |
| 41 |
want: "text/plain", |
| 42 |
}, |
| 43 |
{ |
| 44 |
// Removes empty parameters. |
| 45 |
input: `image/png; name="abc.png"; =""`, |
| 46 |
sep: ';', |
| 47 |
want: `image/png; name="abc.png"`, |
| 48 |
}, |
| 49 |
{ |
| 50 |
input: "application/octet-stream;=?UTF-8?B?bmFtZT0iw7DCn8KUwoo=?=You've got a new voice miss call.msg", |
| 51 |
sep: ';', |
| 52 |
want: "application/octet-stream;name=\"ð\u009f\u0094\u008aYou've got a new voice miss call.msg\"", |
| 53 |
}, |
| 54 |
{ |
| 55 |
input: `application/; name="Voice message from =?UTF-8?B?4piOICsxIDI1MS0yNDUtODA0NC5tc2c=?=";`, |
| 56 |
sep: ';', |
| 57 |
want: `application/octet-stream; name="Voice message from ☎ +1 251-245-8044.msg"`, |
| 58 |
}, |
| 59 |
{ |
| 60 |
input: `application/pdf name="file.pdf"`, |
| 61 |
sep: ' ', |
| 62 |
want: `application/pdf;name="file.pdf"`, |
| 63 |
}, |
| 64 |
{ |
| 65 |
// Removes duplicate parameters. |
| 66 |
input: `one/two; name="file.two"; name="file.two"`, |
| 67 |
sep: ';', |
| 68 |
want: `one/two; name="file.two"`, |
| 69 |
}, |
| 70 |
{ |
| 71 |
// Removes duplicate parameters. |
| 72 |
input: `one/nosp;name="file.two"; name="file.two"`, |
| 73 |
sep: ';', |
| 74 |
want: `one/nosp;name="file.two"`, |
| 75 |
}, |
| 76 |
{ |
| 77 |
// Removes duplicate parameters. |
| 78 |
input: `one/; name="file.two"; name="file.two"`, |
| 79 |
sep: ';', |
| 80 |
want: `application/octet-stream; name="file.two"`, |
| 81 |
}, |
| 82 |
{ |
| 83 |
input: `application/octet-stream; =?UTF-8?B?bmFtZT3DsMKfwpTCii5tc2c=?=`, |
| 84 |
sep: ' ', |
| 85 |
want: "application/octet-stream;name=\"ð\u009f\u0094\u008a.msg\"", |
| 86 |
}, |
| 87 |
{ |
| 88 |
// Removes duplicate parameters. |
| 89 |
input: `one/two name="file.two" name="file.two"`, |
| 90 |
sep: ' ', |
| 91 |
want: `one/two;name="file.two"`, |
| 92 |
}, |
| 93 |
{ |
| 94 |
input: `; name="file.two"`, |
| 95 |
sep: ';', |
| 96 |
want: ctPlaceholder + `; name="file.two"`, |
| 97 |
}, |
| 98 |
{ |
| 99 |
input: `charset=binary; name="logoleft.jpg"`, |
| 100 |
sep: ';', |
| 101 |
want: `application/octet-stream; charset=binary; name="logoleft.jpg"`, |
| 102 |
}, |
| 103 |
{ |
| 104 |
input: `one/two;iso-8859-1`, |
| 105 |
sep: ';', |
| 106 |
want: `one/two;iso-8859-1=` + pvPlaceholder, |
| 107 |
}, |
| 108 |
{ |
| 109 |
input: `one/two; name="file.two"; iso-8859-1`, |
| 110 |
sep: ';', |
| 111 |
want: `one/two; name="file.two"; iso-8859-1=` + pvPlaceholder, |
| 112 |
}, |
| 113 |
{ |
| 114 |
input: `one/two; ; name="file.two"`, |
| 115 |
sep: ';', |
| 116 |
want: `one/two; name="file.two"`, |
| 117 |
}, |
| 118 |
|
| 119 |
// remove extra content type parts |
| 120 |
{ |
| 121 |
input: `application/pdf/.pdf; name=1337.pdf`, |
| 122 |
sep: ';', |
| 123 |
want: `application/pdf; name=1337.pdf`, |
| 124 |
}, |
| 125 |
{ |
| 126 |
input: `application/pdf/pdf/pdf; name=1337.pdf`, |
| 127 |
sep: ';', |
| 128 |
want: `application/pdf; name=1337.pdf`, |
| 129 |
}, |
| 130 |
} |
| 131 |
for _, tc := range testCases { |
| 132 |
t.Run(tc.input, func(t *testing.T) { |
| 133 |
got := fixMangledMediaType(tc.input, tc.sep) |
| 134 |
if got != tc.want { |
| 135 |
t.Errorf("got %q, want %q", got, tc.want) |
| 136 |
} |
| 137 |
}) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
func TestFixUnquotedSpecials(t *testing.T) { |
| 142 |
testCases := []struct { |
| 143 |
input, want string |
| 144 |
}{ |
| 145 |
{ |
| 146 |
input: "", |
| 147 |
want: "", |
| 148 |
}, |
| 149 |
{ |
| 150 |
input: "application/octet-stream", |
| 151 |
want: "application/octet-stream", |
| 152 |
}, |
| 153 |
{ |
| 154 |
input: "application/octet-stream;", |
| 155 |
want: "application/octet-stream;", |
| 156 |
}, |
| 157 |
{ |
| 158 |
input: `application/octet-stream; param1="value1"`, |
| 159 |
want: `application/octet-stream; param1="value1"`, |
| 160 |
}, |
| 161 |
{ |
| 162 |
input: `application/octet-stream; param1="value1"\`, |
| 163 |
want: `application/octet-stream; param1="value1"\`, |
| 164 |
}, |
| 165 |
{ |
| 166 |
input: "application/octet-stream; param1=value1", |
| 167 |
want: "application/octet-stream; param1=value1", |
| 168 |
}, |
| 169 |
{ |
| 170 |
input: `application/octet-stream; param1=value1\`, |
| 171 |
want: "application/octet-stream; param1=value1", |
| 172 |
}, |
| 173 |
{ |
| 174 |
input: `application/octet-stream; param1=value1\"`, |
| 175 |
want: `application/octet-stream; param1="value1\""`, |
| 176 |
}, |
| 177 |
{ |
| 178 |
input: `application/octet-stream; param1=value"1"`, |
| 179 |
want: `application/octet-stream; param1="value\"1\""`, |
| 180 |
}, |
| 181 |
{ |
| 182 |
input: `application/octet-stream; param1="value\"1\""`, |
| 183 |
want: `application/octet-stream; param1="value\"1\""`, |
| 184 |
}, |
| 185 |
{ |
| 186 |
// Do not preserve unqoted whitespace. |
| 187 |
input: "application/octet-stream; param1= value1", |
| 188 |
want: "application/octet-stream; param1=value1", |
| 189 |
}, |
| 190 |
{ |
| 191 |
// Do not preserve unqoted whitespace. |
| 192 |
input: "application/octet-stream; param1=\tvalue1", |
| 193 |
want: "application/octet-stream; param1=value1", |
| 194 |
}, |
| 195 |
{ |
| 196 |
input: `application/octet-stream; param1="value1;"`, |
| 197 |
want: `application/octet-stream; param1="value1;"`, |
| 198 |
}, |
| 199 |
{ |
| 200 |
input: `application/octet-stream; param1="value1;2.txt"`, |
| 201 |
want: `application/octet-stream; param1="value1;2.txt"`, |
| 202 |
}, |
| 203 |
{ |
| 204 |
input: `application/octet-stream; param1="value 1"`, |
| 205 |
want: `application/octet-stream; param1="value 1"`, |
| 206 |
}, |
| 207 |
{ |
| 208 |
// Preserve quoted whitespace. |
| 209 |
input: `application/octet-stream; param1=" value 1"`, |
| 210 |
want: `application/octet-stream; param1=" value 1"`, |
| 211 |
}, |
| 212 |
{ |
| 213 |
// Preserve quoted whitespace. |
| 214 |
input: "application/octet-stream; param1=\"\tvalue 1\"", |
| 215 |
want: "application/octet-stream; param1=\"\tvalue 1\"", |
| 216 |
}, |
| 217 |
{ |
| 218 |
// Preserve quoted whitespace. |
| 219 |
input: "application/octet-stream; param1=\"value\t1\"", |
| 220 |
want: "application/octet-stream; param1=\"value\t1\"", |
| 221 |
}, |
| 222 |
{ |
| 223 |
input: `application/octet-stream; param1="value(1).pdf"`, |
| 224 |
want: `application/octet-stream; param1="value(1).pdf"`, |
| 225 |
}, |
| 226 |
{ |
| 227 |
input: `application/octet-stream; param1=value(1).pdf`, |
| 228 |
want: `application/octet-stream; param1="value(1).pdf"`, |
| 229 |
}, |
| 230 |
{ |
| 231 |
input: `application/octet-stream; param1=value(1).pdf; param2=value(2).pdf`, |
| 232 |
want: `application/octet-stream; param1="value(1).pdf"; param2="value(2).pdf"`, |
| 233 |
}, |
| 234 |
{ |
| 235 |
input: "application/octet-stream; param1=value(1).pdf;\tparam2=value2.pdf;", |
| 236 |
want: "application/octet-stream; param1=\"value(1).pdf\";\tparam2=value2.pdf;", |
| 237 |
}, |
| 238 |
{ |
| 239 |
input: `application/octet-stream; param1=value(1).pdf;param2=value2.pdf;`, |
| 240 |
want: `application/octet-stream; param1="value(1).pdf";param2=value2.pdf;`, |
| 241 |
}, |
| 242 |
{ |
| 243 |
input: `application/octet-stream; param1=value/1`, |
| 244 |
want: `application/octet-stream; param1="value/1"`, |
| 245 |
}, |
| 246 |
{ |
| 247 |
input: `multipart/alternative; boundary=?UOAwFjScLp1is-162467503201177404728935166502-`, |
| 248 |
want: `multipart/alternative; boundary="?UOAwFjScLp1is-162467503201177404728935166502-"`, |
| 249 |
}, |
| 250 |
{ |
| 251 |
input: `text/HTML; charset="UTF-8Return-Path: [email protected]`, |
| 252 |
want: `text/HTML; charset="UTF-8Return-Path: [email protected]"`, |
| 253 |
}, |
| 254 |
{ |
| 255 |
input: `text/html;charset=`, |
| 256 |
want: `text/html;charset=""`, |
| 257 |
}, |
| 258 |
{ |
| 259 |
input: `text/html; charset=; format=flowed`, |
| 260 |
want: `text/html; charset=""; format=flowed`, |
| 261 |
}, |
| 262 |
{ |
| 263 |
input: `text/html;charset="`, |
| 264 |
want: `text/html;charset=""`, |
| 265 |
}, |
| 266 |
{ |
| 267 |
// Check unquoted 8bit is encoded |
| 268 |
input: `application/msword;name=管理.doc`, |
| 269 |
want: `application/msword;name="=?utf-8?b?566h55CGLmRvYw==?="`, |
| 270 |
}, |
| 271 |
{ |
| 272 |
// Check mix of ascii and unquoted 8bit is encoded |
| 273 |
input: `application/msword;name=15管理.doc`, |
| 274 |
want: `application/msword;name="=?utf-8?b?MTXnrqHnkIYuZG9j?="`, |
| 275 |
}, |
| 276 |
{ |
| 277 |
// Check quoted 8bit is encoded |
| 278 |
input: `application/msword;name="15管理.doc"`, |
| 279 |
want: `application/msword;name="=?utf-8?b?MTXnrqHnkIYuZG9j?="`, |
| 280 |
}, |
| 281 |
{ |
| 282 |
// Check quoted 8bit with missing closing quote is encoded |
| 283 |
input: `application/msword;name="15管理.doc`, |
| 284 |
want: `application/msword;name="=?utf-8?b?MTXnrqHnkIYuZG9j?="`, |
| 285 |
}, |
| 286 |
{ |
| 287 |
// Trailing quote without starting quote is considered as part of param text for simplicity |
| 288 |
input: `application/msword;name=15管理.doc"`, |
| 289 |
want: `application/msword;name="=?utf-8?b?MTXnrqHnkIYuZG9jXCI=?="`, |
| 290 |
}, |
| 291 |
{ |
| 292 |
// Invalid UTF-8 sequence does not cause any fatal error |
| 293 |
input: "application/msword;name=\xe2\x28\xa1.doc", |
| 294 |
want: `application/msword;name="=?utf-8?b?77+9KO+/vS5kb2M=?="`, |
| 295 |
}, |
| 296 |
{ |
| 297 |
// Value with spaces is surrounded with quotes. |
| 298 |
input: `text/plain; name=Untitled document.txt`, |
| 299 |
want: `text/plain; name="Untitled document.txt"`, |
| 300 |
}, |
| 301 |
{ |
| 302 |
// Value with spaces is surrounded with quotes. |
| 303 |
input: `text/plain; name=Untitled document.txt; disposition=inline`, |
| 304 |
want: `text/plain; name="Untitled document.txt"; disposition=inline`, |
| 305 |
}, |
| 306 |
} |
| 307 |
for _, tc := range testCases { |
| 308 |
t.Run(tc.input, func(t *testing.T) { |
| 309 |
got := fixUnquotedSpecials(tc.input) |
| 310 |
if got != tc.want { |
| 311 |
t.Errorf("\ngot : %s\nwant : %s\ninput: %s", got, tc.want, tc.input) |
| 312 |
} |
| 313 |
}) |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
func TestFixUnEscapedQuotes(t *testing.T) { |
| 318 |
testCases := []struct { |
| 319 |
input, want string |
| 320 |
}{ |
| 321 |
{ |
| 322 |
input: `application/rtf; charset=iso-8859-1; name=""V047411.rtf".rtf"`, |
| 323 |
want: `application/rtf; charset=iso-8859-1; name="\"V047411.rtf\".rtf"`, |
| 324 |
}, |
| 325 |
{ |
| 326 |
input: `application/octet-stream; param1="`, |
| 327 |
want: `application/octet-stream; param1=""`, |
| 328 |
}, |
| 329 |
{ |
| 330 |
input: `application/octet-stream; param1="\""`, |
| 331 |
want: `application/octet-stream; param1="\""`, |
| 332 |
}, |
| 333 |
{ |
| 334 |
input: `application/rtf; charset=iso-8859-1; name=b"V047411.rtf".rtf`, |
| 335 |
want: `application/rtf; charset=iso-8859-1; name="b\"V047411.rtf\".rtf"`, |
| 336 |
}, |
| 337 |
{ |
| 338 |
input: `application/rtf; charset=iso-8859-1; name="V047411.rtf".rtf`, |
| 339 |
want: `application/rtf; charset=iso-8859-1; name="\"V047411.rtf\".rtf"`, |
| 340 |
}, |
| 341 |
{ |
| 342 |
input: `application/rtf; charset=iso-8859-1; name="V047411.rtf;".rtf`, |
| 343 |
want: `application/rtf; charset=iso-8859-1; name="\"V047411.rtf;\".rtf"`, |
| 344 |
}, |
| 345 |
{ |
| 346 |
input: `application/rtf; charset=utf-8; name="žába.jpg"`, |
| 347 |
want: `application/rtf; charset=utf-8; name="žába.jpg"`, |
| 348 |
}, |
| 349 |
{ |
| 350 |
input: `application/rtf; charset=utf-8; name=""žába".jpg"`, |
| 351 |
want: `application/rtf; charset=utf-8; name="\"žába\".jpg"`, |
| 352 |
}, |
| 353 |
} |
| 354 |
for _, tc := range testCases { |
| 355 |
t.Run(tc.input, func(t *testing.T) { |
| 356 |
got := fixUnescapedQuotes(tc.input) |
| 357 |
if got != tc.want { |
| 358 |
t.Errorf("\ngot: %s\nwant: %s", got, tc.want) |
| 359 |
} |
| 360 |
}) |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
func TestParseMediaType(t *testing.T) { |
| 365 |
testCases := []struct { |
| 366 |
label string // Test case label. |
| 367 |
input string // Content type to parse. |
| 368 |
mtype string // Expected media type returned. |
| 369 |
params map[string]string // Expected params returned. |
| 370 |
}{ |
| 371 |
{ |
| 372 |
label: "basic filename", |
| 373 |
input: "text/html; name=index.html", |
| 374 |
mtype: "text/html", |
| 375 |
params: map[string]string{"name": "index.html"}, |
| 376 |
}, |
| 377 |
{ |
| 378 |
label: "quoted filename", |
| 379 |
input: `text/html; name="index.html"`, |
| 380 |
mtype: "text/html", |
| 381 |
params: map[string]string{"name": "index.html"}, |
| 382 |
}, |
| 383 |
{ |
| 384 |
label: "basic filename trailing separator", |
| 385 |
input: "text/html; name=index.html;", |
| 386 |
mtype: "text/html", |
| 387 |
params: map[string]string{"name": "index.html"}, |
| 388 |
}, |
| 389 |
{ |
| 390 |
label: "quoted filename trailing separator", |
| 391 |
input: `text/html; name="index.html";`, |
| 392 |
mtype: "text/html", |
| 393 |
params: map[string]string{"name": "index.html"}, |
| 394 |
}, |
| 395 |
{ |
| 396 |
label: "unclosed quoted filename", |
| 397 |
input: `text/html; name="index.html`, |
| 398 |
mtype: "text/html", |
| 399 |
params: map[string]string{"name": "index.html"}, |
| 400 |
}, |
| 401 |
{ |
| 402 |
label: "quoted filename with separator", |
| 403 |
input: `text/html; name="index;a.html"`, |
| 404 |
mtype: "text/html", |
| 405 |
params: map[string]string{"name": "index;a.html"}, |
| 406 |
}, |
| 407 |
{ |
| 408 |
label: "quoted separator mid-string", |
| 409 |
input: `text/html; name="index;a.html"; hash=8675309`, |
| 410 |
mtype: "text/html", |
| 411 |
params: map[string]string{"name": "index;a.html", "hash": "8675309"}, |
| 412 |
}, |
| 413 |
{ |
| 414 |
label: "with prefix whitespace", |
| 415 |
input: `text/plain; charset= "UTF-8"; format=flowed`, |
| 416 |
mtype: "text/plain", |
| 417 |
params: map[string]string{"charset": "UTF-8", "format": "flowed"}, |
| 418 |
}, |
| 419 |
{ |
| 420 |
label: "with double prefix whitespace", |
| 421 |
input: `text/plain; charset = "UTF-8"; format=flowed`, |
| 422 |
mtype: "text/plain", |
| 423 |
params: map[string]string{"charset": "UTF-8", "format": "flowed"}, |
| 424 |
}, |
| 425 |
{ |
| 426 |
label: "with postfix whitespace", |
| 427 |
input: `text/plain; charset="UTF-8" ; format=flowed`, |
| 428 |
mtype: "text/plain", |
| 429 |
params: map[string]string{"charset": "UTF-8", "format": "flowed"}, |
| 430 |
}, |
| 431 |
{ |
| 432 |
label: "with whitespace tab", |
| 433 |
input: "text/plain; charset=\"UTF-8\"\t; format=flowed", |
| 434 |
mtype: "text/plain", |
| 435 |
params: map[string]string{"charset": "UTF-8", "format": "flowed"}, |
| 436 |
}, |
| 437 |
{ |
| 438 |
label: "with newline and tab", |
| 439 |
input: "text/plain; charset=\"UTF-8\"\n\t; format=flowed", |
| 440 |
mtype: "text/plain", |
| 441 |
params: map[string]string{"charset": "UTF-8", "format": "flowed"}, |
| 442 |
}, |
| 443 |
{ |
| 444 |
label: "with newline and space", |
| 445 |
input: "application/pdf; name=foo\n ; format=flowed", |
| 446 |
mtype: "application/pdf", |
| 447 |
params: map[string]string{"name": "foo", "format": "flowed"}, |
| 448 |
}, |
| 449 |
{ |
| 450 |
label: "with more spaces", |
| 451 |
input: "application/pdf; name=foo ; format=flowed", |
| 452 |
mtype: "application/pdf", |
| 453 |
params: map[string]string{"name": "foo", "format": "flowed"}, |
| 454 |
}, |
| 455 |
{ |
| 456 |
label: "with more tabs", |
| 457 |
input: "application/pdf; name=foo \t\t; format=flowed", |
| 458 |
mtype: "application/pdf", |
| 459 |
params: map[string]string{"name": "foo", "format": "flowed"}, |
| 460 |
}, |
| 461 |
{ |
| 462 |
label: "with more newlines", |
| 463 |
input: "application/pdf; name=foo \n\n; format=flowed", |
| 464 |
mtype: "application/pdf", |
| 465 |
params: map[string]string{"name": "foo", "format": "flowed"}, |
| 466 |
}, |
| 467 |
{ |
| 468 |
label: "unquoted with spaces", |
| 469 |
input: "application/pdf; x-unix-mode=0644; name=File name with spaces.pdf", |
| 470 |
mtype: "application/pdf", |
| 471 |
params: map[string]string{"x-unix-mode": "0644", "name": "File name with spaces.pdf"}, |
| 472 |
}, |
| 473 |
{ |
| 474 |
label: "Outlook-Logo with newlines", |
| 475 |
input: `application/octet-stream; name="=?utf-8?B?T3V0bG9vay1Mb2dvCgpEZXNj?="`, |
| 476 |
mtype: "application/octet-stream", |
| 477 |
params: map[string]string{"name": "Outlook-Logo Desc"}, |
| 478 |
}, |
| 479 |
} |
| 480 |
for _, tc := range testCases { |
| 481 |
t.Run(tc.label, func(t *testing.T) { |
| 482 |
mtype, params, _, err := Parse(tc.input) |
| 483 |
|
| 484 |
if err != nil { |
| 485 |
t.Errorf("got err %v, want nil", err) |
| 486 |
return |
| 487 |
} |
| 488 |
|
| 489 |
if mtype != tc.mtype { |
| 490 |
t.Errorf("mtype got %q, want %q", mtype, tc.mtype) |
| 491 |
} |
| 492 |
|
| 493 |
for k, v := range tc.params { |
| 494 |
if params[k] != v { |
| 495 |
t.Errorf("params[%q] got %q, want %q", k, params[k], v) |
| 496 |
} |
| 497 |
// Delete param to allow check for unexpected below. |
| 498 |
delete(params, k) |
| 499 |
} |
| 500 |
for pname := range params { |
| 501 |
t.Errorf("Found unexpected param: %q=%q", pname, params[pname]) |
| 502 |
} |
| 503 |
}) |
| 504 |
} |
| 505 |
} |