1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/uconv/tests/unit/test_bug715319.gb2312.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,79 @@ 1.4 +const charset = "GB2312"; 1.5 +const ScriptableUnicodeConverter = 1.6 + Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter", 1.7 + "nsIScriptableUnicodeConverter"); 1.8 +var gConverter; 1.9 + 1.10 +function error(inString, outString, msg) { 1.11 + var dispIn = ""; 1.12 + var dispOut = ""; 1.13 + var i; 1.14 + for (i = 0; i < inString.length; ++i) { 1.15 + dispIn += " x" + inString.charCodeAt(i).toString(16); 1.16 + } 1.17 + if (outString.length == 0) { 1.18 + dispOut = "<empty>"; 1.19 + } else { 1.20 + for (i = 0; i < outString.length; ++i) { 1.21 + dispOut += " x" + outString.charCodeAt(i).toString(16); 1.22 + } 1.23 + } 1.24 + dump("\"" + dispIn + "\" ==> \"" + dispOut + "\"\n"); 1.25 + do_throw("security risk: " + msg); 1.26 +} 1.27 + 1.28 +function IsASCII(charCode) { 1.29 + return (charCode <= 0x7e); 1.30 +} 1.31 + 1.32 +function test(inString) { 1.33 + var outString = gConverter.ConvertToUnicode(inString) + 1.34 + gConverter.Finish(); 1.35 + 1.36 + var outLen = outString.length; 1.37 + for (var pos = 1; pos < 3; ++pos) { 1.38 + outPos = outLen - (9 - pos); 1.39 + if (outPos < 0) { 1.40 + outPos = 0; 1.41 + } 1.42 + c0 = inString.charCodeAt(0); 1.43 + c1 = inString.charCodeAt(1); 1.44 + c2 = inString.charCodeAt(2); 1.45 + c3 = inString.charCodeAt(3); 1.46 + if (IsASCII(inString.charCodeAt(pos)) && 1.47 + !(outString.charCodeAt(outPos) == inString.charCodeAt(pos) || 1.48 + (outString.charCodeAt(outPos) != 0xFFFD) || 1.49 + // legal 4 byte range 1.50 + (0x81 <= c0 && c0 <= 0xfe && 1.51 + 0x30 <= c1 && c1 <= 0x39 && 1.52 + 0x81 <= c2 && c2 <= 0xfe && 1.53 + 0x30 <= c3 && c3 <= 0x39))) { 1.54 + dump("pos = " + pos + "; outPos = " + outPos + "\n"); 1.55 + error(inString, outString, "ASCII input eaten"); 1.56 + } 1.57 + } 1.58 +} 1.59 + 1.60 +function run_test() { 1.61 + gConverter = new ScriptableUnicodeConverter(); 1.62 + gConverter.charset = charset; 1.63 + 1.64 + var byte1, byte2, byte3, byte4; 1.65 + 1.66 + // 2-byte 1.67 + for (byte1 = 1; byte1 < 0x100; ++byte1) { 1.68 + for (byte2 = 1; byte2 < 0x100; ++byte2) { 1.69 + test(String.fromCharCode(byte1, byte2) + " foo"); 1.70 + } 1.71 + } 1.72 + // 4-byte (limited) 1.73 + for (byte1 = 0x80; byte1 < 0x90; ++byte1) { 1.74 + for (byte2 = 0x20; byte2 < 0x40; ++byte2) { 1.75 + for (byte3 = 0x80; byte3 < 0x90; ++byte3) { 1.76 + for (byte4 = 0x20; byte4 < 0x40; ++byte4) { 1.77 + test(String.fromCharCode(byte1, byte2, byte3, byte4) + " foo"); 1.78 + } 1.79 + } 1.80 + } 1.81 + } 1.82 +}