1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/uconv/tests/unit/test_bug381412.euc_jp.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,89 @@ 1.4 +const charset = "EUC-JP"; 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 test(inString) { 1.29 + var outString = gConverter.ConvertToUnicode(inString) + 1.30 + gConverter.Finish(); 1.31 + 1.32 + switch (outString.length) { 1.33 + case 0: 1.34 + case 1: 1.35 + case 2: 1.36 + error(inString, outString, "Unexpected error"); 1.37 + break; 1.38 + case 3: 1.39 + error(inString, outString, "3 byte sequence eaten"); 1.40 + break; 1.41 + case 4: 1.42 + if (outString.charCodeAt(0) < 0x80 && 1.43 + outString.charCodeAt(1) < 0x80 && 1.44 + outString.charCodeAt(2) < 0x80 && 1.45 + outString.charCodeAt(3) < 0x80) { 1.46 + error(inString, outString, 1.47 + "3 byte sequence converted to 1 ASCII"); 1.48 + } 1.49 + break; 1.50 + case 5: 1.51 + if (outString != inString && 1.52 + outString.charCodeAt(0) < 0x80 && 1.53 + outString.charCodeAt(1) < 0x80 && 1.54 + outString.charCodeAt(2) < 0x80 && 1.55 + outString.charCodeAt(3) < 0x80 && 1.56 + outString.charCodeAt(4) < 0x80) { 1.57 + error(inString, outString, 1.58 + "3 byte sequence converted to 2 ASCII"); 1.59 + } 1.60 + break; 1.61 + case 6: 1.62 + if (outString != inString && 1.63 + outString.charCodeAt(0) < 0x80 && 1.64 + outString.charCodeAt(1) < 0x80 && 1.65 + outString.charCodeAt(2) < 0x80 && 1.66 + outString.charCodeAt(3) < 0x80 && 1.67 + outString.charCodeAt(4) < 0x80 && 1.68 + outString.charCodeAt(5) < 0x80) { 1.69 + error(inString, outString, 1.70 + "3 byte sequence converted to 3 ASCII"); 1.71 + } 1.72 + break; 1.73 + } 1.74 +} 1.75 + 1.76 +function run_test() { 1.77 + gConverter = new ScriptableUnicodeConverter(); 1.78 + gConverter.charset = charset; 1.79 + 1.80 + var byte1, byte2, byte3; 1.81 + for (byte1 = 1; byte1 < 0x100; ++byte1) { 1.82 + for (byte2 = 1; byte2 < 0x100; ++byte2) { 1.83 + if (byte1 == 0x8f) { 1.84 + for (byte3 = 1; byte3 < 0x100; ++byte3) { 1.85 + test(String.fromCharCode(byte1, byte2, byte3) + "foo"); 1.86 + } 1.87 + } else { 1.88 + test(String.fromCharCode(byte1, byte2) + " foo"); 1.89 + } 1.90 + } 1.91 + } 1.92 +}