intl/uconv/tests/unit/test_bug340714.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* Test case for bug 340714
michael@0 2 *
michael@0 3 * Uses nsIConverterInputStream to decode UTF-16 text with all combinations
michael@0 4 * of UTF-16BE and UTF-16LE with and without BOM.
michael@0 5 *
michael@0 6 * Sample text is: "Все счастливые семьи похожи друг на друга, каждая несчастливая семья несчастлива по-своему."
michael@0 7 *
michael@0 8 * The enclosing quotation marks are included in the sample text to test that
michael@0 9 * UTF-16LE is recognized even when there is no BOM and the UTF-16LE decoder is
michael@0 10 * not explicitly called. This only works when the first character of the text
michael@0 11 * is an eight-bit character.
michael@0 12 */
michael@0 13
michael@0 14 const beBOM="%FE%FF";
michael@0 15 const leBOM="%FF%FE";
michael@0 16 const sampleUTF16BE="%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";
michael@0 17 const sampleUTF16LE="%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";
michael@0 18 const expected = "\"\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.\"";
michael@0 19
michael@0 20 function makeText(withBOM, charset)
michael@0 21 {
michael@0 22 var theText = eval("sample" + charset);
michael@0 23 if (withBOM) {
michael@0 24 if (charset == "UTF16BE") {
michael@0 25 theText = beBOM + theText;
michael@0 26 } else {
michael@0 27 theText = leBOM + theText;
michael@0 28 }
michael@0 29 }
michael@0 30 return theText;
michael@0 31 }
michael@0 32
michael@0 33 function testCase(withBOM, charset, charsetDec, decoder, bufferLength)
michael@0 34 {
michael@0 35 var dataURI = "data:text/plain;charset=" + charsetDec + "," +
michael@0 36 makeText(withBOM, charset);
michael@0 37
michael@0 38 var IOService = Components.Constructor("@mozilla.org/network/io-service;1",
michael@0 39 "nsIIOService");
michael@0 40 var ConverterInputStream =
michael@0 41 Components.Constructor("@mozilla.org/intl/converter-input-stream;1",
michael@0 42 "nsIConverterInputStream",
michael@0 43 "init");
michael@0 44
michael@0 45 var ios = new IOService();
michael@0 46 var channel = ios.newChannel(dataURI, "", null);
michael@0 47 var testInputStream = channel.open();
michael@0 48 var testConverter = new ConverterInputStream(testInputStream,
michael@0 49 decoder,
michael@0 50 bufferLength,
michael@0 51 0xFFFD);
michael@0 52
michael@0 53 if (!(testConverter instanceof
michael@0 54 Components.interfaces.nsIUnicharLineInputStream))
michael@0 55 throw "not line input stream";
michael@0 56
michael@0 57 var outStr = "";
michael@0 58 var more;
michael@0 59 do {
michael@0 60 // read the line and check for eof
michael@0 61 var line = {};
michael@0 62 more = testConverter.readLine(line);
michael@0 63 outStr += line.value;
michael@0 64 } while (more);
michael@0 65
michael@0 66 if (outStr != expected) {
michael@0 67 dump("Failed with BOM = " + withBOM + "; charset = " + charset +
michael@0 68 "; charset declaration = " + charsetDec + "; decoder = " + decoder +
michael@0 69 "; bufferLength = " + bufferLength + "\n");
michael@0 70 if (outStr.length == expected.length) {
michael@0 71 for (i = 0; i < outStr.length; ++i) {
michael@0 72 if (outStr.charCodeAt(i) != expected.charCodeAt(i)) {
michael@0 73 dump(i + ": " + outStr.charCodeAt(i).toString(16) + " != " + expected.charCodeAt(i).toString(16) + "\n");
michael@0 74 }
michael@0 75 }
michael@0 76 }
michael@0 77 }
michael@0 78
michael@0 79 // escape the strings before comparing for better readability
michael@0 80 do_check_eq(escape(outStr), escape(expected));
michael@0 81 }
michael@0 82
michael@0 83 function run_test()
michael@0 84 {
michael@0 85 /* BOM charset charset decoder buffer
michael@0 86 declaration length */
michael@0 87 testCase(true, "UTF16LE", "UTF-16", "UTF-16", 64);
michael@0 88 testCase(true, "UTF16BE", "UTF-16", "UTF-16", 64);
michael@0 89 testCase(true, "UTF16LE", "UTF-16", "UTF-16LE", 64);
michael@0 90 testCase(true, "UTF16BE", "UTF-16", "UTF-16BE", 64);
michael@0 91 testCase(false, "UTF16LE", "UTF-16", "UTF-16", 64);
michael@0 92 testCase(false, "UTF16BE", "UTF-16", "UTF-16", 64);
michael@0 93 testCase(false, "UTF16LE", "UTF-16", "UTF-16LE", 64);
michael@0 94 testCase(false, "UTF16BE", "UTF-16", "UTF-16BE", 64);
michael@0 95 testCase(true, "UTF16LE", "UTF-16", "UTF-16", 65);
michael@0 96 testCase(true, "UTF16BE", "UTF-16", "UTF-16", 65);
michael@0 97 testCase(true, "UTF16LE", "UTF-16", "UTF-16LE", 65);
michael@0 98 testCase(true, "UTF16BE", "UTF-16", "UTF-16BE", 65);
michael@0 99 testCase(false, "UTF16LE", "UTF-16", "UTF-16", 65);
michael@0 100 testCase(false, "UTF16BE", "UTF-16", "UTF-16", 65);
michael@0 101 testCase(false, "UTF16LE", "UTF-16", "UTF-16LE", 65);
michael@0 102 testCase(false, "UTF16BE", "UTF-16", "UTF-16BE", 65);
michael@0 103 }

mercurial