intl/uconv/tests/unit/test_bug715319.euc_jp.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/uconv/tests/unit/test_bug715319.euc_jp.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,69 @@
     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 IsASCII(charCode) {
    1.29 +    return (charCode <= 0x7e);
    1.30 +}
    1.31 +
    1.32 +function IsNotGR(charCode) {
    1.33 +    return (charCode < 0xa1 || charCode > 0xfe);
    1.34 +}
    1.35 +
    1.36 +function test(inString) {
    1.37 +    var outString = gConverter.ConvertToUnicode(inString) +
    1.38 +	            gConverter.Finish();
    1.39 +
    1.40 +    var outLen = outString.length;
    1.41 +    if (IsASCII(inString.charCodeAt(1)) && 
    1.42 +	inString.charCodeAt(1) != outString.charCodeAt(outLen - 5))  {
    1.43 +	error(inString, outString, "ASCII second byte eaten");
    1.44 +    } else if (IsASCII(inString.charCodeAt(2)) && 
    1.45 +	       inString.charCodeAt(2) != outString.charCodeAt(outLen - 4)) {
    1.46 +	error(inString, outString, "ASCII third byte eaten");
    1.47 +    } else if (inString.charCodeAt(0) == 0x8f &&
    1.48 +	       inString.charCodeAt(1) > 0x7f &&
    1.49 +	       IsNotGR(inString.charCodeAt(2)) &&
    1.50 +	       (!(outString.charCodeAt(outLen - 4) == 0xFFFD ||
    1.51 +		  outString.charCodeAt(outLen - 4) == inString.charCodeAt(2)))) {
    1.52 +	error(inString, outString, "non-GR third byte eaten");
    1.53 +    }
    1.54 +}
    1.55 +
    1.56 +function run_test() {
    1.57 +    gConverter = new ScriptableUnicodeConverter();
    1.58 +    gConverter.charset = charset;
    1.59 +
    1.60 +    var byte1, byte2, byte3;
    1.61 +    for (byte1 = 1; byte1 < 0x100; ++byte1) {
    1.62 +	for (byte2 = 1; byte2 < 0x100; ++byte2) {
    1.63 +	    if (byte1 == 0x8f) {
    1.64 +		for (byte3 = 1; byte3 < 0x100; ++byte3) {
    1.65 +		    test(String.fromCharCode(byte1, byte2, byte3) + "foo");
    1.66 +		}
    1.67 +	    } else {
    1.68 +		test(String.fromCharCode(byte1, byte2) + " foo");
    1.69 +	    }
    1.70 +	}
    1.71 +    }
    1.72 +}

mercurial