test_bug381412.euc_jp.js (2530B)
1 const charset = "EUC-JP"; 2 const ScriptableUnicodeConverter = Components.Constructor( 3 "@mozilla.org/intl/scriptableunicodeconverter", 4 "nsIScriptableUnicodeConverter" 5 ); 6 var gConverter; 7 8 function error(inString, outString, msg) { 9 var dispIn = ""; 10 var dispOut = ""; 11 var i; 12 for (i = 0; i < inString.length; ++i) { 13 dispIn += " x" + inString.charCodeAt(i).toString(16); 14 } 15 if (!outString.length) { 16 dispOut = "<empty>"; 17 } else { 18 for (i = 0; i < outString.length; ++i) { 19 dispOut += " x" + outString.charCodeAt(i).toString(16); 20 } 21 } 22 dump('"' + dispIn + '" ==> "' + dispOut + '"\n'); 23 do_throw("security risk: " + msg); 24 } 25 26 function test(inString) { 27 var outString = gConverter.ConvertToUnicode(inString) + gConverter.Finish(); 28 29 switch (outString.length) { 30 case 0: 31 case 1: 32 case 2: 33 error(inString, outString, "Unexpected error"); 34 break; 35 case 3: 36 error(inString, outString, "3 byte sequence eaten"); 37 break; 38 case 4: 39 if ( 40 outString.charCodeAt(0) < 0x80 && 41 outString.charCodeAt(1) < 0x80 && 42 outString.charCodeAt(2) < 0x80 && 43 outString.charCodeAt(3) < 0x80 44 ) { 45 error(inString, outString, "3 byte sequence converted to 1 ASCII"); 46 } 47 break; 48 case 5: 49 if ( 50 outString != inString && 51 outString.charCodeAt(0) < 0x80 && 52 outString.charCodeAt(1) < 0x80 && 53 outString.charCodeAt(2) < 0x80 && 54 outString.charCodeAt(3) < 0x80 && 55 outString.charCodeAt(4) < 0x80 56 ) { 57 error(inString, outString, "3 byte sequence converted to 2 ASCII"); 58 } 59 break; 60 case 6: 61 if ( 62 outString != inString && 63 outString.charCodeAt(0) < 0x80 && 64 outString.charCodeAt(1) < 0x80 && 65 outString.charCodeAt(2) < 0x80 && 66 outString.charCodeAt(3) < 0x80 && 67 outString.charCodeAt(4) < 0x80 && 68 outString.charCodeAt(5) < 0x80 69 ) { 70 error(inString, outString, "3 byte sequence converted to 3 ASCII"); 71 } 72 break; 73 } 74 } 75 76 function run_test() { 77 gConverter = new ScriptableUnicodeConverter(); 78 gConverter.charset = charset; 79 80 var byte1, byte2, byte3; 81 for (byte1 = 1; byte1 < 0x100; ++byte1) { 82 for (byte2 = 1; byte2 < 0x100; ++byte2) { 83 if (byte1 == 0x8f) { 84 for (byte3 = 1; byte3 < 0x100; ++byte3) { 85 test(String.fromCharCode(byte1, byte2, byte3) + "foo"); 86 } 87 } else { 88 test(String.fromCharCode(byte1, byte2) + " foo"); 89 } 90 } 91 } 92 }