tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_bug340714.js (5354B)


      1 /* Test case for bug 340714
      2 *
      3 * Uses nsIConverterInputStream to decode UTF-16 text with all combinations
      4 * of UTF-16BE and UTF-16LE with and without BOM.
      5 *
      6 * Sample text is: "Все счастливые семьи похожи друг на друга, каждая несчастливая семья несчастлива по-своему."
      7 *
      8 * The enclosing quotation marks are included in the sample text to test that
      9 * UTF-16LE is recognized even when there is no BOM and the UTF-16LE decoder is
     10 * not explicitly called. This only works when the first character of the text
     11 * is an eight-bit character.
     12 */
     13 
     14 const { NetUtil } = ChromeUtils.importESModule(
     15  "resource://gre/modules/NetUtil.sys.mjs"
     16 );
     17 
     18 const beBOM = "%FE%FF";
     19 const leBOM = "%FF%FE";
     20 const sampleUTF16BE =
     21  "%00%22%04%12%04%41%04%35%00%20%04%41%04%47%04%30%04%41%04%42%04%3B%04%38%04%32%04%4B%04%35%00%20%04%41%04%35%04%3C%04%4C%04%38%00%20%04%3F%04%3E%04%45%04%3E%04%36%04%38%00%20%04%34%04%40%04%43%04%33%00%20%04%3D%04%30%00%20%04%34%04%40%04%43%04%33%04%30%00%2C%00%20%04%3A%04%30%04%36%04%34%04%30%04%4F%00%20%04%3D%04%35%04%41%04%47%04%30%04%41%04%42%04%3B%04%38%04%32%04%30%04%4F%00%20%04%41%04%35%04%3C%04%4C%04%4F%00%20%04%3D%04%35%04%41%04%47%04%30%04%41%04%42%04%3B%04%38%04%32%04%30%00%20%04%3F%04%3E%00%2D%04%41%04%32%04%3E%04%35%04%3C%04%43%00%2E%00%22";
     22 const sampleUTF16LE =
     23  "%22%00%12%04%41%04%35%04%20%00%41%04%47%04%30%04%41%04%42%04%3B%04%38%04%32%04%4B%04%35%04%20%00%41%04%35%04%3C%04%4C%04%38%04%20%00%3F%04%3E%04%45%04%3E%04%36%04%38%04%20%00%34%04%40%04%43%04%33%04%20%00%3D%04%30%04%20%00%34%04%40%04%43%04%33%04%30%04%2C%00%20%00%3A%04%30%04%36%04%34%04%30%04%4F%04%20%00%3D%04%35%04%41%04%47%04%30%04%41%04%42%04%3B%04%38%04%32%04%30%04%4F%04%20%00%41%04%35%04%3C%04%4C%04%4F%04%20%00%3D%04%35%04%41%04%47%04%30%04%41%04%42%04%3B%04%38%04%32%04%30%04%20%00%3F%04%3E%04%2D%00%41%04%32%04%3E%04%35%04%3C%04%43%04%2E%00%22%00";
     24 const expected =
     25  '"\u0412\u0441\u0435 \u0441\u0447\u0430\u0441\u0442\u043B\u0438\u0432\u044B\u0435 \u0441\u0435\u043C\u044C\u0438 \u043F\u043E\u0445\u043E\u0436\u0438 \u0434\u0440\u0443\u0433 \u043D\u0430 \u0434\u0440\u0443\u0433\u0430, \u043A\u0430\u0436\u0434\u0430\u044F \u043D\u0435\u0441\u0447\u0430\u0441\u0442\u043B\u0438\u0432\u0430\u044F \u0441\u0435\u043C\u044C\u044F \u043D\u0435\u0441\u0447\u0430\u0441\u0442\u043B\u0438\u0432\u0430 \u043F\u043E-\u0441\u0432\u043E\u0435\u043C\u0443."';
     26 
     27 Services.prefs.setBoolPref("security.allow_eval_with_system_principal", true);
     28 registerCleanupFunction(() => {
     29  Services.prefs.clearUserPref("security.allow_eval_with_system_principal");
     30 });
     31 
     32 function makeText(withBOM, charset) {
     33  const isBE = charset === "UTF16BE";
     34  const sampleText = isBE ? sampleUTF16BE : sampleUTF16LE;
     35  const bom = isBE ? beBOM : leBOM;
     36  return withBOM ? bom + sampleText : sampleText;
     37 }
     38 
     39 function testCase(withBOM, charset, charsetDec, decoder, bufferLength) {
     40  var dataURI =
     41    "data:text/plain;charset=" + charsetDec + "," + makeText(withBOM, charset);
     42 
     43  var ConverterInputStream = Components.Constructor(
     44    "@mozilla.org/intl/converter-input-stream;1",
     45    "nsIConverterInputStream",
     46    "init"
     47  );
     48 
     49  var channel = NetUtil.newChannel({
     50    uri: dataURI,
     51    loadUsingSystemPrincipal: true,
     52  });
     53  var testInputStream = channel.open();
     54  var testConverter = new ConverterInputStream(
     55    testInputStream,
     56    decoder,
     57    bufferLength,
     58    0xfffd
     59  );
     60 
     61  if (!(testConverter instanceof Ci.nsIUnicharLineInputStream)) {
     62    throw new Error("not line input stream");
     63  }
     64 
     65  var outStr = "";
     66  var more;
     67  do {
     68    // read the line and check for eof
     69    var line = {};
     70    more = testConverter.readLine(line);
     71    outStr += line.value;
     72  } while (more);
     73 
     74  if (outStr != expected) {
     75    dump(
     76      "Failed with BOM = " +
     77        withBOM +
     78        "; charset = " +
     79        charset +
     80        "; charset declaration = " +
     81        charsetDec +
     82        "; decoder = " +
     83        decoder +
     84        "; bufferLength = " +
     85        bufferLength +
     86        "\n"
     87    );
     88    if (outStr.length == expected.length) {
     89      for (let i = 0; i < outStr.length; ++i) {
     90        if (outStr.charCodeAt(i) != expected.charCodeAt(i)) {
     91          dump(
     92            i +
     93              ": " +
     94              outStr.charCodeAt(i).toString(16) +
     95              " != " +
     96              expected.charCodeAt(i).toString(16) +
     97              "\n"
     98          );
     99        }
    100      }
    101    }
    102  }
    103 
    104  // escape the strings before comparing for better readability
    105  Assert.equal(escape(outStr), escape(expected));
    106 }
    107 
    108 function run_test() {
    109  /*       BOM    charset    charset   decoder     buffer
    110                               declaration           length */
    111  testCase(true, "UTF16LE", "UTF-16", "UTF-16BE", 64);
    112  testCase(true, "UTF16BE", "UTF-16", "UTF-16LE", 64);
    113  testCase(true, "UTF16LE", "UTF-16", "UTF-16LE", 64);
    114  testCase(true, "UTF16BE", "UTF-16", "UTF-16BE", 64);
    115  testCase(false, "UTF16LE", "UTF-16", "UTF-16LE", 64);
    116  testCase(false, "UTF16BE", "UTF-16", "UTF-16BE", 64);
    117  testCase(true, "UTF16LE", "UTF-16", "UTF-16BE", 65);
    118  testCase(true, "UTF16BE", "UTF-16", "UTF-16LE", 65);
    119  testCase(true, "UTF16LE", "UTF-16", "UTF-16LE", 65);
    120  testCase(true, "UTF16BE", "UTF-16", "UTF-16BE", 65);
    121  testCase(false, "UTF16LE", "UTF-16", "UTF-16LE", 65);
    122  testCase(false, "UTF16BE", "UTF-16", "UTF-16BE", 65);
    123 }