intl/uconv/tests/unit/test_bug317216.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Test case for bug 317216
michael@0 2 *
michael@0 3 * Uses nsIConverterInputStream to decode UTF-16 text with valid surrogate
michael@0 4 * pairs and lone surrogate characters
michael@0 5 *
michael@0 6 * Sample text is: "A" in Mathematical Bold Capitals (U+1D400)
michael@0 7 *
michael@0 8 * The test uses buffers of 4 different lengths to test end of buffer in mid-
michael@0 9 * UTF16 character and mid-surrogate pair
michael@0 10 */
michael@0 11
michael@0 12 const test = [
michael@0 13 // 0: Valid surrogate pair
michael@0 14 ["%D8%35%DC%20%00%2D%00%2D",
michael@0 15 // expected: surrogate pair
michael@0 16 "\uD835\uDC20--"],
michael@0 17 // 1: Lone high surrogate
michael@0 18 ["%D8%35%00%2D%00%2D",
michael@0 19 // expected: one replacement char
michael@0 20 "\uFFFD--"],
michael@0 21 // 2: Lone low surrogate
michael@0 22 ["%DC%20%00%2D%00%2D",
michael@0 23 // expected: one replacement char
michael@0 24 "\uFFFD--"],
michael@0 25 // 3: Two high surrogates
michael@0 26 ["%D8%35%D8%35%00%2D%00%2D",
michael@0 27 // expected: two replacement chars
michael@0 28 "\uFFFD\uFFFD--"],
michael@0 29 // 4: Two low surrogates
michael@0 30 ["%DC%20%DC%20%00%2D%00%2D",
michael@0 31 // expected: two replacement chars
michael@0 32 "\uFFFD\uFFFD--"],
michael@0 33 // 5: Low surrogate followed by high surrogate
michael@0 34 ["%DC%20%D8%35%00%2D%00%2D",
michael@0 35 // expected: two replacement chars
michael@0 36 "\uFFFD\uFFFD--"],
michael@0 37 // 6: Lone high surrogate followed by valid surrogate pair
michael@0 38 ["%D8%35%D8%35%DC%20%00%2D%00%2D",
michael@0 39 // expected: replacement char followed by surrogate pair
michael@0 40 "\uFFFD\uD835\uDC20--"],
michael@0 41 // 7: Lone low surrogate followed by valid surrogate pair
michael@0 42 ["%DC%20%D8%35%DC%20%00%2D%00%2D",
michael@0 43 // expected: replacement char followed by surrogate pair
michael@0 44 "\uFFFD\uD835\uDC20--"],
michael@0 45 // 8: Valid surrogate pair followed by lone high surrogate
michael@0 46 ["%D8%35%DC%20%D8%35%00%2D%00%2D",
michael@0 47 // expected: surrogate pair followed by replacement char
michael@0 48 "\uD835\uDC20\uFFFD--"],
michael@0 49 // 9: Valid surrogate pair followed by lone low surrogate
michael@0 50 ["%D8%35%DC%20%DC%20%00%2D%00%2D",
michael@0 51 // expected: surrogate pair followed by replacement char
michael@0 52 "\uD835\uDC20\uFFFD--"],
michael@0 53 // 10: Lone high surrogate at the end of the input
michael@0 54 ["%D8%35%",
michael@0 55 // expected: nothing
michael@0 56 ""],
michael@0 57 // 11: Half code unit at the end of the input
michael@0 58 ["%D8",
michael@0 59 // expected: nothing
michael@0 60 ""]];
michael@0 61
michael@0 62 const IOService = Components.Constructor("@mozilla.org/network/io-service;1",
michael@0 63 "nsIIOService");
michael@0 64 const ConverterInputStream =
michael@0 65 Components.Constructor("@mozilla.org/intl/converter-input-stream;1",
michael@0 66 "nsIConverterInputStream",
michael@0 67 "init");
michael@0 68 const ios = new IOService();
michael@0 69
michael@0 70 function testCase(testText, expectedText, bufferLength, charset)
michael@0 71 {
michael@0 72 var dataURI = "data:text/plain;charset=" + charset + "," + testText;
michael@0 73
michael@0 74 var channel = ios.newChannel(dataURI, "", null);
michael@0 75 var testInputStream = channel.open();
michael@0 76 var testConverter = new ConverterInputStream(testInputStream,
michael@0 77 charset,
michael@0 78 bufferLength,
michael@0 79 0xFFFD);
michael@0 80
michael@0 81 if (!(testConverter instanceof
michael@0 82 Components.interfaces.nsIUnicharLineInputStream))
michael@0 83 throw "not line input stream";
michael@0 84
michael@0 85 var outStr = "";
michael@0 86 var more;
michael@0 87 do {
michael@0 88 // read the line and check for eof
michael@0 89 var line = {};
michael@0 90 more = testConverter.readLine(line);
michael@0 91 outStr += line.value;
michael@0 92 } while (more);
michael@0 93
michael@0 94 // escape the strings before comparing for better readability
michael@0 95 do_check_eq(escape(outStr), escape(expectedText));
michael@0 96 }
michael@0 97
michael@0 98 // Add 32 dummy characters to the test text to work around the minimum buffer
michael@0 99 // size of an ns*Buffer
michael@0 100 const MINIMUM_BUFFER_SIZE=32;
michael@0 101 function padBytes(str)
michael@0 102 {
michael@0 103 var padding = "";
michael@0 104 for (var i = 0; i < MINIMUM_BUFFER_SIZE; ++i) {
michael@0 105 padding += "%00%2D";
michael@0 106 }
michael@0 107 return padding + str;
michael@0 108 }
michael@0 109
michael@0 110 function padUnichars(str)
michael@0 111 {
michael@0 112 var padding = "";
michael@0 113 for (var i = 0; i < MINIMUM_BUFFER_SIZE; ++i) {
michael@0 114 padding += "-";
michael@0 115 }
michael@0 116 return padding + str;
michael@0 117 }
michael@0 118
michael@0 119 // Byte-swap %-encoded utf-16
michael@0 120 function flip(str) { return str.replace(/(%..)(%..)/g, "$2$1"); }
michael@0 121
michael@0 122 function run_test()
michael@0 123 {
michael@0 124 for (var i = 0; i < 12; ++i) {
michael@0 125 for (var bufferLength = MINIMUM_BUFFER_SIZE;
michael@0 126 bufferLength < MINIMUM_BUFFER_SIZE + 4;
michael@0 127 ++ bufferLength) {
michael@0 128 var testText = padBytes(test[i][0]);
michael@0 129 var expectedText = padUnichars(test[i][1]);
michael@0 130 testCase(testText, expectedText, bufferLength, "UTF-16BE");
michael@0 131 testCase(flip(testText), expectedText, bufferLength, "UTF-16LE");
michael@0 132 }
michael@0 133 }
michael@0 134 }

mercurial