Tests for bug 715319
authorSimon Montagu <smontagu@smontagu.org>
Sun, 20 May 2012 02:09:14 -0700
changeset 96727 16c6de083029e73644cbc55b0aeec8f6928f51f6
parent 96726 58d38bc9de23b37ac5457d494ff4f07e5bdbb939
child 96728 443c0c79e54f31c5887da1baf0ece0b2ee39a4bc
push id1439
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 20:19:22 +0000
treeherdermozilla-aurora@ea74834dccd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs715319
milestone15.0a1
Tests for bug 715319
intl/uconv/tests/unit/test_bug715319.dbcs.js
intl/uconv/tests/unit/test_bug715319.euc_jp.js
intl/uconv/tests/unit/test_bug715319.gb2312.js
intl/uconv/tests/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/intl/uconv/tests/unit/test_bug715319.dbcs.js
@@ -0,0 +1,54 @@
+// 2-byte charsets:
+const charsets = [ "Big5", "EUC-KR", "x-euc-tw", "x-johab" ]
+const ScriptableUnicodeConverter =
+	Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter",
+			       "nsIScriptableUnicodeConverter");
+var gConverter;
+
+function error(inString, outString, msg) {
+    var dispIn = "";
+    var dispOut = "";
+    var i;
+    for (i = 0; i < inString.length; ++i) {
+	dispIn += " x" + inString.charCodeAt(i).toString(16);
+    }
+    if (outString.length == 0) {
+	dispOut = "<empty>";
+    } else {
+	for (i = 0; i < outString.length; ++i) {
+	    dispOut += " x" + outString.charCodeAt(i).toString(16);
+	}
+    }
+    dump("\"" + dispIn + "\" ==> \"" + dispOut + "\"\n");
+    do_throw("security risk: " + msg);
+}
+
+function IsASCII(charCode) {
+    return (charCode <= 0x7e);
+}
+
+function test(inString) {
+    var outString = gConverter.ConvertToUnicode(inString) +
+	            gConverter.Finish();
+
+    var outLen = outString.length;
+
+    if (IsASCII(inString.charCodeAt(1)) && 
+	(outLen < 4 || outString.charCodeAt(outLen - 4) == 0xFFFD)) {
+	error(inString, outString, "ASCII input eaten in " + gConverter.charset);
+    }
+}
+
+function run_test() {
+    gConverter = new ScriptableUnicodeConverter();
+    for (var i = 0; i < charsets.length; ++i) {
+	gConverter.charset = charsets[i];
+
+	var byte1, byte2;
+	for (byte1 = 1; byte1 < 0x100; ++byte1) {
+	    for (byte2 = 1; byte2 < 0x100; ++byte2) {
+		test(String.fromCharCode(byte1, byte2) + "foo");
+	    }
+	}
+    }
+}
new file mode 100644
--- /dev/null
+++ b/intl/uconv/tests/unit/test_bug715319.euc_jp.js
@@ -0,0 +1,69 @@
+const charset = "EUC-JP";
+const ScriptableUnicodeConverter =
+	Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter",
+			       "nsIScriptableUnicodeConverter");
+var gConverter;
+
+function error(inString, outString, msg) {
+    var dispIn = "";
+    var dispOut = "";
+    var i;
+    for (i = 0; i < inString.length; ++i) {
+	dispIn += " x" + inString.charCodeAt(i).toString(16);
+    }
+    if (outString.length == 0) {
+	dispOut = "<empty>";
+    } else {
+	for (i = 0; i < outString.length; ++i) {
+	    dispOut += " x" + outString.charCodeAt(i).toString(16);
+	}
+    }
+    dump("\"" + dispIn + "\" ==> \"" + dispOut + "\"\n");
+    do_throw("security risk: " + msg);
+}
+
+function IsASCII(charCode) {
+    return (charCode <= 0x7e);
+}
+
+function IsNotGR(charCode) {
+    return (charCode < 0xa1 || charCode > 0xfe);
+}
+
+function test(inString) {
+    var outString = gConverter.ConvertToUnicode(inString) +
+	            gConverter.Finish();
+
+    var outLen = outString.length;
+    if (IsASCII(inString.charCodeAt(1)) && 
+	inString.charCodeAt(1) != outString.charCodeAt(outLen - 5))  {
+	error(inString, outString, "ASCII second byte eaten");
+    } else if (IsASCII(inString.charCodeAt(2)) && 
+	       inString.charCodeAt(2) != outString.charCodeAt(outLen - 4)) {
+	error(inString, outString, "ASCII third byte eaten");
+    } else if (inString.charCodeAt(0) == 0x8f &&
+	       inString.charCodeAt(1) > 0x7f &&
+	       IsNotGR(inString.charCodeAt(2)) &&
+	       (!(outString.charCodeAt(outLen - 4) == 0xFFFD ||
+		  outString.charCodeAt(outLen - 4) == inString.charCodeAt(2)))) {
+	error(inString, outString, "non-GR third byte eaten");
+    }
+}
+
+function run_test() {
+    gConverter = new ScriptableUnicodeConverter();
+    gConverter.charset = charset;
+
+    var byte1, byte2, byte3;
+    for (byte1 = 1; byte1 < 0x100; ++byte1) {
+	for (byte2 = 1; byte2 < 0x100; ++byte2) {
+	    if (byte1 == 0x8f) {
+		for (byte3 = 1; byte3 < 0x100; ++byte3) {
+		    test(String.fromCharCode(byte1, byte2, byte3) + "foo");
+		}
+	    } else {
+		test(String.fromCharCode(byte1, byte2) + " foo");
+	    }
+	}
+    }
+}
new file mode 100644
--- /dev/null
+++ b/intl/uconv/tests/unit/test_bug715319.gb2312.js
@@ -0,0 +1,79 @@
+const charset = "GB2312";
+const ScriptableUnicodeConverter =
+	Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter",
+			       "nsIScriptableUnicodeConverter");
+var gConverter;
+
+function error(inString, outString, msg) {
+    var dispIn = "";
+    var dispOut = "";
+    var i;
+    for (i = 0; i < inString.length; ++i) {
+	dispIn += " x" + inString.charCodeAt(i).toString(16);
+    }
+    if (outString.length == 0) {
+	dispOut = "<empty>";
+    } else {
+	for (i = 0; i < outString.length; ++i) {
+	    dispOut += " x" + outString.charCodeAt(i).toString(16);
+	}
+    }
+    dump("\"" + dispIn + "\" ==> \"" + dispOut + "\"\n");
+    do_throw("security risk: " + msg);
+}
+
+function IsASCII(charCode) {
+    return (charCode <= 0x7e);
+}
+
+function test(inString) {
+    var outString = gConverter.ConvertToUnicode(inString) +
+	            gConverter.Finish();
+
+    var outLen = outString.length;
+    for (var pos = 1; pos < 3; ++pos) {
+	outPos = outLen - (9 - pos);
+	if (outPos < 0) {
+	    outPos = 0;
+	}
+	c0 = inString.charCodeAt(0);
+	c1 = inString.charCodeAt(1);
+	c2 = inString.charCodeAt(2);
+	c3 = inString.charCodeAt(3);
+	if (IsASCII(inString.charCodeAt(pos)) && 
+	    !(outString.charCodeAt(outPos) == inString.charCodeAt(pos) ||
+	      (outString.charCodeAt(outPos) != 0xFFFD) ||
+	      // legal 4 byte range
+	      (0x81 <= c0 && c0 <= 0xfe &&
+	       0x30 <= c1 && c1 <= 0x39 &&
+	       0x81 <= c2 && c2 <= 0xfe &&
+	       0x30 <= c3 && c3 <= 0x39))) {
+	    dump("pos = " + pos + "; outPos = " + outPos + "\n");
+	    error(inString, outString, "ASCII input eaten");
+	}
+    }
+}
+
+function run_test() {
+    gConverter = new ScriptableUnicodeConverter();
+    gConverter.charset = charset;
+
+    var byte1, byte2, byte3, byte4;
+
+    // 2-byte
+    for (byte1 = 1; byte1 < 0x100; ++byte1) {
+	for (byte2 = 1; byte2 < 0x100; ++byte2) {
+	    test(String.fromCharCode(byte1, byte2) + "    foo");
+	}
+    }
+    // 4-byte (limited)
+    for (byte1 = 0x80; byte1 < 0x90; ++byte1) {
+	for (byte2 = 0x20; byte2 < 0x40; ++byte2) {
+	    for (byte3 = 0x80; byte3 < 0x90; ++byte3) {
+		for (byte4 = 0x20; byte4 < 0x40; ++byte4) {
+		    test(String.fromCharCode(byte1, byte2, byte3, byte4) + "  foo");
+		}
+	    }
+	}
+    }
+}
--- a/intl/uconv/tests/unit/xpcshell.ini
+++ b/intl/uconv/tests/unit/xpcshell.ini
@@ -24,16 +24,19 @@ tail =
 [test_bug457886.js]
 [test_bug522931.js]
 [test_bug563283.js]
 [test_bug563618.js]
 [test_bug601429.js]
 [test_bug699673.js]
 [test_bug90411.js]
 [test_bug713519.js]
+[test_bug715319.euc_jp.js]
+[test_bug715319.gb2312.js]
+[test_bug715319.dbcs.js]
 [test_bug718500.js]
 [test_charset_conversion.js]
 [test_decode_8859-1.js]
 [test_decode_8859-10.js]
 [test_decode_8859-11.js]
 [test_decode_8859-13.js]
 [test_decode_8859-14.js]
 [test_decode_8859-15.js]