intl/uconv/tests/unit/test_bug563618.js

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial