intl/uconv/tests/unit/test_bug563618.js

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* Test case for bug 563618
     2  *
     3  * Uses nsIConverterInputStream to decode invalid EUC-JP text
     4  *
     5  */
     7 const test = [
     8 // 0: 0x8e followed by hi byte, not valid JIS X 0201
     9 	      ["abcdefghijklmnopqrstuvwxyz12test00%8e%80foobar",
    10 //    expected: one replacement character, invalid byte eaten
    11                "abcdefghijklmnopqrstuvwxyz12test00\uFFFDfoobar"],
    12 // 1: 0x8e followed by ASCII
    13 	      ["abcdefghijklmnopqrstuvwxyz12test01%8efoobar",
    14 //    expected: one replacement character, invalid byte not eaten
    15                "abcdefghijklmnopqrstuvwxyz12test01\uFFFDfoobar"],
    16 // 2: JIS X 0208 lead byte followed by invalid hi byte
    17               ["abcdefghijklmnopqrstuvwxyz12test02%bf%80foobar",
    18 //    expected: one replacement character, invalid byte eaten
    19 	       "abcdefghijklmnopqrstuvwxyz12test02\uFFFDfoobar"],
    20 // 3: JIS X 0208 lead byte followed by ASCII
    21               ["abcdefghijklmnopqrstuvwxyz12test03%bffoobar",
    22 //    expected: one replacement character, invalid byte not eaten
    23 	       "abcdefghijklmnopqrstuvwxyz12test03\uFFFDfoobar"]];
    25 const IOService = Components.Constructor("@mozilla.org/network/io-service;1",
    26                                          "nsIIOService");
    27 const ConverterInputStream =
    28       Components.Constructor("@mozilla.org/intl/converter-input-stream;1",
    29                              "nsIConverterInputStream",
    30                              "init");
    31 const ios = new IOService();
    33 function testCase(testText, expectedText, bufferLength, charset)
    34 {
    35   var dataURI = "data:text/plain;charset=" + charset + "," + testText;
    37   var channel = ios.newChannel(dataURI, "", null);
    38   var testInputStream = channel.open();
    39   var testConverter = new ConverterInputStream(testInputStream,
    40                                                charset,
    41                                                bufferLength,
    42                                                0xFFFD);
    44   if (!(testConverter instanceof
    45         Components.interfaces.nsIUnicharLineInputStream))
    46     throw "not line input stream";
    48   var outStr = "";
    49   var more;
    50   do {
    51     // read the line and check for eof
    52     var line = {};
    53     more = testConverter.readLine(line);
    54     outStr += line.value;
    55   } while (more);
    57   if (outStr != expectedText) {
    58     dump("Failed with bufferLength = " + bufferLength + "\n");
    59     if (outStr.length == expectedText.length) {
    60       for (i = 0; i < outStr.length; ++i) {
    61 	if (outStr.charCodeAt(i) != expectedText.charCodeAt(i)) {
    62 	  dump(i + ": " + outStr.charCodeAt(i).toString(16) + " != " + expectedText.charCodeAt(i).toString(16) + "\n");
    63 	}
    64       }
    65     }
    66   }
    68   // escape the strings before comparing for better readability
    69   do_check_eq(escape(outStr), escape(expectedText));
    70 }
    72 function run_test()
    73 {
    74   for (var i = 0; i < test.length; ++i) {
    75     for (var bufferLength = 32; bufferLength < 40; ++ bufferLength) {
    76       testCase(test[i][0], test[i][1], bufferLength, "EUC-JP");
    77     }
    78   }
    79 }

mercurial