michael@0: /* Test case for bug 563618 michael@0: * michael@0: * Uses nsIConverterInputStream to decode invalid EUC-JP text michael@0: * michael@0: */ michael@0: michael@0: const test = [ michael@0: // 0: 0x8e followed by hi byte, not valid JIS X 0201 michael@0: ["abcdefghijklmnopqrstuvwxyz12test00%8e%80foobar", michael@0: // expected: one replacement character, invalid byte eaten michael@0: "abcdefghijklmnopqrstuvwxyz12test00\uFFFDfoobar"], michael@0: // 1: 0x8e followed by ASCII michael@0: ["abcdefghijklmnopqrstuvwxyz12test01%8efoobar", michael@0: // expected: one replacement character, invalid byte not eaten michael@0: "abcdefghijklmnopqrstuvwxyz12test01\uFFFDfoobar"], michael@0: // 2: JIS X 0208 lead byte followed by invalid hi byte michael@0: ["abcdefghijklmnopqrstuvwxyz12test02%bf%80foobar", michael@0: // expected: one replacement character, invalid byte eaten michael@0: "abcdefghijklmnopqrstuvwxyz12test02\uFFFDfoobar"], michael@0: // 3: JIS X 0208 lead byte followed by ASCII michael@0: ["abcdefghijklmnopqrstuvwxyz12test03%bffoobar", michael@0: // expected: one replacement character, invalid byte not eaten michael@0: "abcdefghijklmnopqrstuvwxyz12test03\uFFFDfoobar"]]; michael@0: michael@0: const IOService = Components.Constructor("@mozilla.org/network/io-service;1", michael@0: "nsIIOService"); michael@0: const ConverterInputStream = michael@0: Components.Constructor("@mozilla.org/intl/converter-input-stream;1", michael@0: "nsIConverterInputStream", michael@0: "init"); michael@0: const ios = new IOService(); michael@0: michael@0: function testCase(testText, expectedText, bufferLength, charset) michael@0: { michael@0: var dataURI = "data:text/plain;charset=" + charset + "," + testText; michael@0: michael@0: var channel = ios.newChannel(dataURI, "", null); michael@0: var testInputStream = channel.open(); michael@0: var testConverter = new ConverterInputStream(testInputStream, michael@0: charset, michael@0: bufferLength, michael@0: 0xFFFD); michael@0: michael@0: if (!(testConverter instanceof michael@0: Components.interfaces.nsIUnicharLineInputStream)) michael@0: throw "not line input stream"; michael@0: michael@0: var outStr = ""; michael@0: var more; michael@0: do { michael@0: // read the line and check for eof michael@0: var line = {}; michael@0: more = testConverter.readLine(line); michael@0: outStr += line.value; michael@0: } while (more); michael@0: michael@0: if (outStr != expectedText) { michael@0: dump("Failed with bufferLength = " + bufferLength + "\n"); michael@0: if (outStr.length == expectedText.length) { michael@0: for (i = 0; i < outStr.length; ++i) { michael@0: if (outStr.charCodeAt(i) != expectedText.charCodeAt(i)) { michael@0: dump(i + ": " + outStr.charCodeAt(i).toString(16) + " != " + expectedText.charCodeAt(i).toString(16) + "\n"); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // escape the strings before comparing for better readability michael@0: do_check_eq(escape(outStr), escape(expectedText)); michael@0: } michael@0: michael@0: function run_test() michael@0: { michael@0: for (var i = 0; i < test.length; ++i) { michael@0: for (var bufferLength = 32; bufferLength < 40; ++ bufferLength) { michael@0: testCase(test[i][0], test[i][1], bufferLength, "EUC-JP"); michael@0: } michael@0: } michael@0: }