netwerk/test/unit/test_MIME_params.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_MIME_params.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,560 @@
     1.4 +/**
     1.5 + * Tests for parsing header fields using the syntax used in
     1.6 + * Content-Disposition and Content-Type
     1.7 + *
     1.8 + * See also https://bugzilla.mozilla.org/show_bug.cgi?id=609667
     1.9 + */
    1.10 +
    1.11 +var BS = '\\';
    1.12 +var DQUOTE = '"'; 
    1.13 +
    1.14 +// Test array:
    1.15 +//  - element 0: "Content-Disposition" header to test
    1.16 +//  under MIME (email):
    1.17 +//  - element 1: correct value returned for disposition-type (empty param name)
    1.18 +//  - element 2: correct value for filename returned
    1.19 +//  under HTTP:
    1.20 +// (currently supports continuations; expected results without continuations
    1.21 +// are commented out for now)
    1.22 +//  - element 3: correct value returned for disposition-type (empty param name)
    1.23 +//  - element 4: correct value for filename returned 
    1.24 +//
    1.25 +// 3 and 4 may be left out if they are identical
    1.26 +
    1.27 +var tests = [
    1.28 +  // No filename parameter: return nothing
    1.29 +  ["attachment;",
    1.30 +   "attachment", Cr.NS_ERROR_INVALID_ARG],
    1.31 +
    1.32 +  // basic
    1.33 +  ["attachment; filename=basic",
    1.34 +   "attachment", "basic"],
    1.35 +
    1.36 +  // extended
    1.37 +  ["attachment; filename*=UTF-8''extended",
    1.38 +   "attachment", "extended"],
    1.39 +
    1.40 +  // prefer extended to basic (bug 588781)
    1.41 +  ["attachment; filename=basic; filename*=UTF-8''extended",
    1.42 +   "attachment", "extended"],
    1.43 +
    1.44 +  // prefer extended to basic (bug 588781)
    1.45 +  ["attachment; filename*=UTF-8''extended; filename=basic",
    1.46 +   "attachment", "extended"],
    1.47 +
    1.48 +  // use first basic value (invalid; error recovery)
    1.49 +  ["attachment; filename=first; filename=wrong",
    1.50 +   "attachment", "first"],
    1.51 +
    1.52 +  // old school bad HTTP servers: missing 'attachment' or 'inline'
    1.53 +  // (invalid; error recovery)
    1.54 +  ["filename=old",
    1.55 +   "filename=old", "old"],
    1.56 +
    1.57 +  ["attachment; filename*=UTF-8''extended",
    1.58 +   "attachment", "extended"],
    1.59 +
    1.60 +  // continuations not part of RFC 5987 (bug 610054)
    1.61 +  ["attachment; filename*0=foo; filename*1=bar",
    1.62 +   "attachment", "foobar",
    1.63 +   /* "attachment", Cr.NS_ERROR_INVALID_ARG */],
    1.64 +
    1.65 +  // Return first continuation (invalid; error recovery)
    1.66 +  ["attachment; filename*0=first; filename*0=wrong; filename=basic",
    1.67 +   "attachment", "first",
    1.68 +   /* "attachment", "basic" */],
    1.69 +
    1.70 +  // Only use correctly ordered continuations  (invalid; error recovery)
    1.71 +  ["attachment; filename*0=first; filename*1=second; filename*0=wrong",
    1.72 +   "attachment", "firstsecond",
    1.73 +   /* "attachment", Cr.NS_ERROR_INVALID_ARG */],
    1.74 +
    1.75 +  // prefer continuation to basic (unless RFC 5987)
    1.76 +  ["attachment; filename=basic; filename*0=foo; filename*1=bar",
    1.77 +   "attachment", "foobar",
    1.78 +   /* "attachment", "basic" */],
    1.79 +
    1.80 +  // Prefer extended to basic and/or (broken or not) continuation
    1.81 +  // (invalid; error recovery)
    1.82 +  ["attachment; filename=basic; filename*0=first; filename*0=wrong; filename*=UTF-8''extended",
    1.83 +   "attachment", "extended"],
    1.84 +
    1.85 +  // RFC 2231 not clear on correct outcome: we prefer non-continued extended
    1.86 +  // (invalid; error recovery)
    1.87 +  ["attachment; filename=basic; filename*=UTF-8''extended; filename*0=foo; filename*1=bar",
    1.88 +   "attachment", "extended"],
    1.89 +
    1.90 +  // Gaps should result in returning only value until gap hit
    1.91 +  // (invalid; error recovery)
    1.92 +  ["attachment; filename*0=foo; filename*2=bar",
    1.93 +   "attachment", "foo",
    1.94 +   /* "attachment", Cr.NS_ERROR_INVALID_ARG */],
    1.95 +
    1.96 +  // Don't allow leading 0's (*01) (invalid; error recovery)
    1.97 +  ["attachment; filename*0=foo; filename*01=bar",
    1.98 +   "attachment", "foo",
    1.99 +   /* "attachment", Cr.NS_ERROR_INVALID_ARG */],
   1.100 +
   1.101 +  // continuations should prevail over non-extended (unless RFC 5987)
   1.102 +  ["attachment; filename=basic; filename*0*=UTF-8''multi;\r\n"
   1.103 +    + " filename*1=line;\r\n" 
   1.104 +    + " filename*2*=%20extended",
   1.105 +   "attachment", "multiline extended",
   1.106 +   /* "attachment", "basic" */],
   1.107 +
   1.108 +  // Gaps should result in returning only value until gap hit
   1.109 +  // (invalid; error recovery)
   1.110 +  ["attachment; filename=basic; filename*0*=UTF-8''multi;\r\n"
   1.111 +    + " filename*1=line;\r\n" 
   1.112 +    + " filename*3*=%20extended",
   1.113 +   "attachment", "multiline",
   1.114 +   /* "attachment", "basic" */],
   1.115 +
   1.116 +  // First series, only please, and don't slurp up higher elements (*2 in this
   1.117 +  // case) from later series into earlier one (invalid; error recovery)
   1.118 +  ["attachment; filename=basic; filename*0*=UTF-8''multi;\r\n"
   1.119 +    + " filename*1=line;\r\n" 
   1.120 +    + " filename*0*=UTF-8''wrong;\r\n"
   1.121 +    + " filename*1=bad;\r\n"
   1.122 +    + " filename*2=evil",
   1.123 +   "attachment", "multiline",
   1.124 +   /* "attachment", "basic" */],
   1.125 +
   1.126 +  // RFC 2231 not clear on correct outcome: we prefer non-continued extended
   1.127 +  // (invalid; error recovery)
   1.128 +  ["attachment; filename=basic; filename*0=UTF-8''multi\r\n;"
   1.129 +    + " filename*=UTF-8''extended;\r\n"
   1.130 +    + " filename*1=line;\r\n" 
   1.131 +    + " filename*2*=%20extended",
   1.132 +   "attachment", "extended"],
   1.133 +
   1.134 +  // sneaky: if unescaped, make sure we leave UTF-8'' in value
   1.135 +  ["attachment; filename*0=UTF-8''unescaped;\r\n"
   1.136 +    + " filename*1*=%20so%20includes%20UTF-8''%20in%20value", 
   1.137 +   "attachment", "UTF-8''unescaped so includes UTF-8'' in value",
   1.138 +   /* "attachment", Cr.NS_ERROR_INVALID_ARG */],
   1.139 +
   1.140 +  // sneaky: if unescaped, make sure we leave UTF-8'' in value
   1.141 +  ["attachment; filename=basic; filename*0=UTF-8''unescaped;\r\n"
   1.142 +    + " filename*1*=%20so%20includes%20UTF-8''%20in%20value", 
   1.143 +   "attachment", "UTF-8''unescaped so includes UTF-8'' in value",
   1.144 +   /* "attachment", "basic" */],
   1.145 +
   1.146 +  // Prefer basic over invalid continuation
   1.147 +  // (invalid; error recovery)
   1.148 +  ["attachment; filename=basic; filename*1=multi;\r\n"
   1.149 +    + " filename*2=line;\r\n" 
   1.150 +    + " filename*3*=%20extended",
   1.151 +   "attachment", "basic"],
   1.152 +
   1.153 +  // support digits over 10
   1.154 +  ["attachment; filename=basic; filename*0*=UTF-8''0;\r\n"
   1.155 +    + " filename*1=1; filename*2=2;filename*3=3;filename*4=4;filename*5=5;\r\n" 
   1.156 +    + " filename*6=6; filename*7=7;filename*8=8;filename*9=9;filename*10=a;\r\n"
   1.157 +    + " filename*11=b; filename*12=c;filename*13=d;filename*14=e;filename*15=f\r\n",
   1.158 +   "attachment", "0123456789abcdef",
   1.159 +   /* "attachment", "basic" */],
   1.160 +
   1.161 +  // support digits over 10 (detect gaps)
   1.162 +  ["attachment; filename=basic; filename*0*=UTF-8''0;\r\n"
   1.163 +    + " filename*1=1; filename*2=2;filename*3=3;filename*4=4;filename*5=5;\r\n" 
   1.164 +    + " filename*6=6; filename*7=7;filename*8=8;filename*9=9;filename*10=a;\r\n"
   1.165 +    + " filename*11=b; filename*12=c;filename*14=e\r\n",
   1.166 +   "attachment", "0123456789abc",
   1.167 +   /* "attachment", "basic" */],
   1.168 +
   1.169 +  // return nothing: invalid
   1.170 +  // (invalid; error recovery)
   1.171 +  ["attachment; filename*1=multi;\r\n"
   1.172 +    + " filename*2=line;\r\n" 
   1.173 +    + " filename*3*=%20extended",
   1.174 +   "attachment", Cr.NS_ERROR_INVALID_ARG],
   1.175 +   
   1.176 +  // Bug 272541: Empty disposition type treated as "attachment" 
   1.177 +
   1.178 +  // sanity check
   1.179 +  ["attachment; filename=foo.html", 
   1.180 +   "attachment", "foo.html",
   1.181 +   "attachment", "foo.html"],
   1.182 +
   1.183 +  // the actual bug
   1.184 +  ["; filename=foo.html", 
   1.185 +   Cr.NS_ERROR_FIRST_HEADER_FIELD_COMPONENT_EMPTY, "foo.html",
   1.186 +   Cr.NS_ERROR_FIRST_HEADER_FIELD_COMPONENT_EMPTY, "foo.html"],
   1.187 +  
   1.188 +  // regression check, but see bug 671204
   1.189 +  ["filename=foo.html", 
   1.190 +   "filename=foo.html", "foo.html",
   1.191 +   "filename=foo.html", "foo.html"],
   1.192 +   
   1.193 +  // Bug 384571: RFC 2231 parameters not decoded when appearing in reversed order
   1.194 +
   1.195 +  // check ordering
   1.196 +  ["attachment; filename=basic; filename*0*=UTF-8''0;\r\n"
   1.197 +    + " filename*1=1; filename*2=2;filename*3=3;filename*4=4;filename*5=5;\r\n" 
   1.198 +    + " filename*6=6; filename*7=7;filename*8=8;filename*9=9;filename*10=a;\r\n"
   1.199 +    + " filename*11=b; filename*12=c;filename*13=d;filename*15=f;filename*14=e;\r\n",
   1.200 +   "attachment", "0123456789abcdef",
   1.201 +   /* "attachment", "basic" */],
   1.202 +
   1.203 +  // check non-digits in sequence numbers
   1.204 +  ["attachment; filename=basic; filename*0*=UTF-8''0;\r\n"
   1.205 +    + " filename*1a=1\r\n",
   1.206 +   "attachment", "0",
   1.207 +   /* "attachment", "basic" */],
   1.208 +
   1.209 +  // check duplicate sequence numbers
   1.210 +  ["attachment; filename=basic; filename*0*=UTF-8''0;\r\n"
   1.211 +    + " filename*0=bad; filename*1=1;\r\n",
   1.212 +   "attachment", "0",
   1.213 +   /* "attachment", "basic" */],
   1.214 +
   1.215 +  // check overflow
   1.216 +  ["attachment; filename=basic; filename*0*=UTF-8''0;\r\n"
   1.217 +    + " filename*11111111111111111111111111111111111111111111111111111111111=1",
   1.218 +   "attachment", "0",
   1.219 +   /* "attachment", "basic" */],
   1.220 +
   1.221 +  // check underflow
   1.222 +  ["attachment; filename=basic; filename*0*=UTF-8''0;\r\n"
   1.223 +    + " filename*-1=1",
   1.224 +   "attachment", "0",
   1.225 +   /* "attachment", "basic" */],
   1.226 +
   1.227 +  // check mixed token/quoted-string
   1.228 +  ["attachment; filename=basic; filename*0=\"0\";\r\n"
   1.229 +    + " filename*1=1;\r\n"
   1.230 +    + " filename*2*=%32",
   1.231 +   "attachment", "012",
   1.232 +   /* "attachment", "basic" */],
   1.233 +
   1.234 +  // check empty sequence number
   1.235 +  ["attachment; filename=basic; filename**=UTF-8''0\r\n",
   1.236 +   "attachment", "basic",
   1.237 +   "attachment", "basic"],
   1.238 +
   1.239 +
   1.240 +  // Bug 419157: ensure that a MIME parameter with no charset information
   1.241 +  // fallbacks to Latin-1
   1.242 +
   1.243 +  ["attachment;filename=IT839\x04\xB5(m8)2.pdf;",
   1.244 +   "attachment", "IT839\u0004\u00b5(m8)2.pdf"],
   1.245 +
   1.246 +  // Bug 588389: unescaping backslashes in quoted string parameters
   1.247 +   
   1.248 +  // '\"', should be parsed as '"'  
   1.249 +  ["attachment; filename=" + DQUOTE + (BS + DQUOTE) + DQUOTE, 
   1.250 +   "attachment", DQUOTE],
   1.251 +  
   1.252 +  // 'a\"b', should be parsed as 'a"b'
   1.253 +  ["attachment; filename=" + DQUOTE + 'a' + (BS + DQUOTE) + 'b' + DQUOTE, 
   1.254 +   "attachment", "a" + DQUOTE + "b"],
   1.255 +  
   1.256 +  // '\x', should be parsed as 'x'
   1.257 +  ["attachment; filename=" + DQUOTE + (BS + "x") + DQUOTE, 
   1.258 +   "attachment", "x"],
   1.259 +   
   1.260 +  // test empty param (quoted-string)
   1.261 +  ["attachment; filename=" + DQUOTE + DQUOTE, 
   1.262 +   "attachment", ""],
   1.263 +  
   1.264 +  // test empty param 
   1.265 +  ["attachment; filename=", 
   1.266 +   "attachment", ""],    
   1.267 +
   1.268 +  // Bug 601933: RFC 2047 does not apply to parameters (at least in HTTP)
   1.269 +  ["attachment; filename==?ISO-8859-1?Q?foo-=E4.html?=",
   1.270 +   "attachment", "foo-\u00e4.html",
   1.271 +   /* "attachment", "=?ISO-8859-1?Q?foo-=E4.html?=" */],
   1.272 +
   1.273 +  ["attachment; filename=\"=?ISO-8859-1?Q?foo-=E4.html?=\"",
   1.274 +   "attachment", "foo-\u00e4.html",
   1.275 +   /* "attachment", "=?ISO-8859-1?Q?foo-=E4.html?=" */],
   1.276 +
   1.277 +  // format sent by GMail as of 2012-07-23 (5987 overrides 2047)
   1.278 +  ["attachment; filename=\"=?ISO-8859-1?Q?foo-=E4.html?=\"; filename*=UTF-8''5987",
   1.279 +   "attachment", "5987"],
   1.280 +
   1.281 +  // Bug 651185: double quotes around 2231/5987 encoded param
   1.282 +  // Change reverted to backwards compat issues with various web services,
   1.283 +  // such as OWA (Bug 703015), plus similar problems in Thunderbird. If this
   1.284 +  // is tried again in the future, email probably needs to be special-cased.
   1.285 +  
   1.286 +  // sanity check
   1.287 +  ["attachment; filename*=utf-8''%41", 
   1.288 +   "attachment", "A"],
   1.289 +
   1.290 +  // the actual bug   
   1.291 +  ["attachment; filename*=" + DQUOTE + "utf-8''%41" + DQUOTE, 
   1.292 +   "attachment", "A"],
   1.293 +  // previously with the fix for 651185:
   1.294 +  // "attachment", Cr.NS_ERROR_INVALID_ARG],
   1.295 +
   1.296 +  // Bug 670333: Content-Disposition parser does not require presence of "="
   1.297 +  // in params
   1.298 +  
   1.299 +  // sanity check
   1.300 +  ["attachment; filename*=UTF-8''foo-%41.html", 
   1.301 +   "attachment", "foo-A.html"],
   1.302 +
   1.303 +  // the actual bug
   1.304 +  ["attachment; filename *=UTF-8''foo-%41.html", 
   1.305 +   "attachment", Cr.NS_ERROR_INVALID_ARG],
   1.306 +   
   1.307 +  // the actual bug, without 2231/5987 encoding
   1.308 +  ["attachment; filename X", 
   1.309 +   "attachment", Cr.NS_ERROR_INVALID_ARG],
   1.310 +
   1.311 +  // sanity check with WS on both sides
   1.312 +  ["attachment; filename = foo-A.html", 
   1.313 +   "attachment", "foo-A.html"],   
   1.314 +
   1.315 +  // Bug 685192: in RFC2231/5987 encoding, a missing charset field should be
   1.316 +  // treated as error 
   1.317 +
   1.318 +  // the actual bug
   1.319 +  ["attachment; filename*=''foo", 
   1.320 +   "attachment", "foo"],      
   1.321 +  // previously with the fix for 692574:
   1.322 +  // "attachment", Cr.NS_ERROR_INVALID_ARG],      
   1.323 +
   1.324 +  // sanity check
   1.325 +  ["attachment; filename*=a''foo", 
   1.326 +   "attachment", "foo"],      
   1.327 +
   1.328 +  // Bug 692574: RFC2231/5987 decoding should not tolerate missing single
   1.329 +  // quotes
   1.330 +
   1.331 +  // one missing
   1.332 +  ["attachment; filename*=UTF-8'foo-%41.html", 
   1.333 +   "attachment", "foo-A.html"],
   1.334 +  // previously with the fix for 692574:
   1.335 +  // "attachment", Cr.NS_ERROR_INVALID_ARG],
   1.336 +
   1.337 +  // both missing
   1.338 +  ["attachment; filename*=foo-%41.html", 
   1.339 +   "attachment","foo-A.html"],
   1.340 +  // previously with the fix for 692574:
   1.341 +  // "attachment", Cr.NS_ERROR_INVALID_ARG],
   1.342 +
   1.343 +  // make sure fallback works
   1.344 +  ["attachment; filename*=UTF-8'foo-%41.html; filename=bar.html", 
   1.345 +   "attachment", "foo-A.html"],
   1.346 +  // previously with the fix for 692574:
   1.347 +  // "attachment", "bar.html"],
   1.348 +
   1.349 +  // Bug 693806: RFC2231/5987 encoding: charset information should be treated
   1.350 +  // as authoritative
   1.351 +
   1.352 +  // UTF-8 labeled ISO-8859-1
   1.353 +  ["attachment; filename*=ISO-8859-1''%c3%a4", 
   1.354 +   "attachment", "\u00c3\u00a4"],
   1.355 +
   1.356 +  // UTF-8 labeled ISO-8859-1, but with octets not allowed in ISO-8859-1
   1.357 +  // accepts x82, understands it as Win1252, maps it to Unicode \u20a1
   1.358 +  ["attachment; filename*=ISO-8859-1''%e2%82%ac", 
   1.359 +   "attachment", "\u00e2\u201a\u00ac"],
   1.360 +
   1.361 +  // defective UTF-8
   1.362 +  ["attachment; filename*=UTF-8''A%e4B", 
   1.363 +   "attachment", Cr.NS_ERROR_INVALID_ARG],
   1.364 +
   1.365 +  // defective UTF-8, with fallback
   1.366 +  ["attachment; filename*=UTF-8''A%e4B; filename=fallback", 
   1.367 +   "attachment", "fallback"],
   1.368 +
   1.369 +  // defective UTF-8 (continuations), with fallback
   1.370 +  ["attachment; filename*0*=UTF-8''A%e4B; filename=fallback", 
   1.371 +   "attachment", "fallback"],
   1.372 +
   1.373 +  // check that charsets aren't mixed up
   1.374 +  ["attachment; filename*0*=ISO-8859-15''euro-sign%3d%a4; filename*=ISO-8859-1''currency-sign%3d%a4", 
   1.375 +   "attachment", "currency-sign=\u00a4"],
   1.376 +
   1.377 +  // same as above, except reversed
   1.378 +  ["attachment; filename*=ISO-8859-1''currency-sign%3d%a4; filename*0*=ISO-8859-15''euro-sign%3d%a4", 
   1.379 +   "attachment", "currency-sign=\u00a4"],
   1.380 +
   1.381 +  // Bug 704989: add workaround for broken Outlook Web App (OWA)
   1.382 +  // attachment handling
   1.383 +
   1.384 +  ["attachment; filename*=\"a%20b\"", 
   1.385 +   "attachment", "a b"],
   1.386 +
   1.387 +  // Bug 717121: crash nsMIMEHeaderParamImpl::DoParameterInternal
   1.388 +
   1.389 +  ["attachment; filename=\"", 
   1.390 +   "attachment", ""], 
   1.391 +
   1.392 +  // We used to read past string if last param w/o = and ;
   1.393 +  // Note: was only detected on windows PGO builds
   1.394 +  ["attachment; filename=foo; trouble", 
   1.395 +   "attachment", "foo"], 
   1.396 +
   1.397 +  // Same, followed by space, hits another case
   1.398 +  ["attachment; filename=foo; trouble ", 
   1.399 +   "attachment", "foo"], 
   1.400 +
   1.401 +  ["attachment", 
   1.402 +   "attachment", Cr.NS_ERROR_INVALID_ARG], 
   1.403 +
   1.404 +  // Bug 730574: quoted-string in RFC2231-continuations not handled
   1.405 +
   1.406 +  ['attachment; filename=basic; filename*0="foo"; filename*1="\\b\\a\\r.html"', 
   1.407 +   "attachment", "foobar.html",
   1.408 +   /* "attachment", "basic" */],
   1.409 +
   1.410 +  // unmatched escape char
   1.411 +  ['attachment; filename=basic; filename*0="foo"; filename*1="\\b\\a\\', 
   1.412 +   "attachment", "fooba\\",
   1.413 +   /* "attachment", "basic" */],
   1.414 +
   1.415 +  // Bug 732369: Content-Disposition parser does not require presence of ";" between params
   1.416 +  // optimally, this would not even return the disposition type "attachment"  
   1.417 +
   1.418 +  ["attachment; extension=bla filename=foo", 
   1.419 +   "attachment", Cr.NS_ERROR_INVALID_ARG], 
   1.420 +
   1.421 +  ["attachment; filename=foo extension=bla", 
   1.422 +   "attachment", "foo"], 
   1.423 +
   1.424 +  ["attachment filename=foo", 
   1.425 +   "attachment", Cr.NS_ERROR_INVALID_ARG], 
   1.426 +
   1.427 +  // Bug 777687: handling of broken %escapes
   1.428 +
   1.429 +  ["attachment; filename*=UTF-8''f%oo; filename=bar", 
   1.430 +   "attachment", "bar"], 
   1.431 +
   1.432 +  ["attachment; filename*=UTF-8''foo%; filename=bar", 
   1.433 +   "attachment", "bar"], 
   1.434 +
   1.435 +  // Bug 783502 - xpcshell test netwerk/test/unit/test_MIME_params.js fails on AddressSanitizer
   1.436 +  ['attachment; filename="\\b\\a\\', 
   1.437 +   "attachment", "ba\\"], 
   1.438 +];
   1.439 +
   1.440 +var rfc5987paramtests = [
   1.441 +  [ // basic test
   1.442 +    "UTF-8'language'value", "value", "language", Cr.NS_OK ],
   1.443 +  [ // percent decoding
   1.444 +    "UTF-8''1%202", "1 2", "", Cr.NS_OK ],
   1.445 +  [ // UTF-8
   1.446 +    "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", "\u00a3 and \u20ac rates", "", Cr.NS_OK ],
   1.447 +  [ // missing charset
   1.448 +    "''abc", "", "", Cr.NS_ERROR_INVALID_ARG ],
   1.449 +  [ // ISO-8859-1: unsupported
   1.450 +    "ISO-8859-1''%A3%20rates", "", "", Cr.NS_ERROR_INVALID_ARG ],
   1.451 +  [ // unknown charset
   1.452 +    "foo''abc", "", "", Cr.NS_ERROR_INVALID_ARG ],
   1.453 +  [ // missing component
   1.454 +    "abc", "", "", Cr.NS_ERROR_INVALID_ARG ],
   1.455 +  [ // missing component
   1.456 +    "'abc", "", "", Cr.NS_ERROR_INVALID_ARG ],
   1.457 +  [ // illegal chars
   1.458 +    "UTF-8''a b", "", "", Cr.NS_ERROR_INVALID_ARG ],
   1.459 +  [ // broken % escapes
   1.460 +    "UTF-8''a%zz", "", "", Cr.NS_ERROR_INVALID_ARG ],
   1.461 +  [ // broken % escapes
   1.462 +    "UTF-8''a%b", "", "", Cr.NS_ERROR_INVALID_ARG ],
   1.463 +  [ // broken % escapes
   1.464 +    "UTF-8''a%", "", "", Cr.NS_ERROR_INVALID_ARG ],
   1.465 +  [ // broken UTF-8
   1.466 +    "UTF-8''%A3%20rates", "", "", 0x8050000E /* NS_ERROR_UDEC_ILLEGALINPUT */  ],
   1.467 +];
   1.468 +
   1.469 +function do_tests(whichRFC)
   1.470 +{
   1.471 +  var mhp = Components.classes["@mozilla.org/network/mime-hdrparam;1"]
   1.472 +                      .getService(Components.interfaces.nsIMIMEHeaderParam);
   1.473 +
   1.474 +  var unused = { value : null };
   1.475 +
   1.476 +  for (var i = 0; i < tests.length; ++i) {
   1.477 +    dump("Testing #" + i + ": " + tests[i] + "\n");
   1.478 +
   1.479 +    // check disposition type
   1.480 +    var expectedDt = tests[i].length == 3 || whichRFC == 0 ? tests[i][1] : tests[i][3];
   1.481 +
   1.482 +    try {
   1.483 +      var result;
   1.484 +      
   1.485 +      if (whichRFC == 0)
   1.486 +        result = mhp.getParameter(tests[i][0], "", "UTF-8", true, unused);
   1.487 +      else 
   1.488 +        result = mhp.getParameterHTTP(tests[i][0], "", "UTF-8", true, unused);
   1.489 +
   1.490 +      do_check_eq(result, expectedDt);
   1.491 +    } 
   1.492 +    catch (e) {
   1.493 +      // Tests can also succeed by expecting to fail with given error code
   1.494 +      if (e.result) {
   1.495 +        // Allow following tests to run by catching exception from do_check_eq()
   1.496 +        try { 
   1.497 +          do_check_eq(e.result, expectedDt); 
   1.498 +        } catch(e) {}  
   1.499 +      }
   1.500 +      continue;
   1.501 +    }
   1.502 +
   1.503 +    // check filename parameter
   1.504 +    var expectedFn = tests[i].length == 3 || whichRFC == 0 ? tests[i][2] : tests[i][4];
   1.505 +
   1.506 +    try {
   1.507 +      var result;
   1.508 +      
   1.509 +      if (whichRFC == 0)
   1.510 +        result = mhp.getParameter(tests[i][0], "filename", "UTF-8", true, unused);
   1.511 +      else 
   1.512 +        result = mhp.getParameterHTTP(tests[i][0], "filename", "UTF-8", true, unused);
   1.513 +
   1.514 +      do_check_eq(result, expectedFn);
   1.515 +    } 
   1.516 +    catch (e) {
   1.517 +      // Tests can also succeed by expecting to fail with given error code
   1.518 +      if (e.result) {
   1.519 +        // Allow following tests to run by catching exception from do_check_eq()
   1.520 +        try { 
   1.521 +          do_check_eq(e.result, expectedFn); 
   1.522 +        } catch(e) {}  
   1.523 +      }
   1.524 +      continue;
   1.525 +    }
   1.526 +  }
   1.527 +}
   1.528 +
   1.529 +function test_decode5987Param() {
   1.530 +  var mhp = Components.classes["@mozilla.org/network/mime-hdrparam;1"]
   1.531 +                      .getService(Components.interfaces.nsIMIMEHeaderParam);
   1.532 +
   1.533 +  for (var i = 0; i < rfc5987paramtests.length; ++i) {
   1.534 +    dump("Testing #" + i + ": " + rfc5987paramtests[i] + "\n");
   1.535 +
   1.536 +    var lang = {};
   1.537 +    try {
   1.538 +      var decoded = mhp.decodeRFC5987Param(rfc5987paramtests[i][0], lang);
   1.539 +      if (rfc5987paramtests[i][3] == Cr.NS_OK) {
   1.540 +        do_check_eq(rfc5987paramtests[i][1], decoded);
   1.541 +        do_check_eq(rfc5987paramtests[i][2], lang.value);
   1.542 +      }
   1.543 +      else {
   1.544 +        do_check_eq(rfc5987paramtests[i][3], "instead got: " + decoded);
   1.545 +      }
   1.546 +    }
   1.547 +    catch (e) {
   1.548 +      do_check_eq(rfc5987paramtests[i][3], e.result);
   1.549 +    }
   1.550 +  }
   1.551 +}
   1.552 +
   1.553 +function run_test() {
   1.554 +
   1.555 +  // Test RFC 2231 (complete header field values)
   1.556 +  do_tests(0);
   1.557 +
   1.558 +  // Test RFC 5987 (complete header field values)
   1.559 +  do_tests(1);
   1.560 +
   1.561 +  // tests for RFC5987 parameter parsing
   1.562 +  test_decode5987Param();
   1.563 +}

mercurial