From 801d4e82651b0d4f68ddbf5e76d3acacbdc77f82 Mon Sep 17 00:00:00 2001 From: "smontagu@smontagu.org" Date: Wed, 5 Sep 2007 22:38:21 -0700 Subject: [PATCH] Tests for bug 381412 --- .../tests/unit/test_bug381412.Big5-HKSCS.js | 60 +++++++++++++ intl/uconv/tests/unit/test_bug381412.Big5.js | 60 +++++++++++++ intl/uconv/tests/unit/test_bug381412.cp936.js | 56 ++++++++++++ .../uconv/tests/unit/test_bug381412.euc-kr.js | 60 +++++++++++++ .../uconv/tests/unit/test_bug381412.euc_jp.js | 89 +++++++++++++++++++ .../uconv/tests/unit/test_bug381412.gb2312.js | 56 ++++++++++++ .../tests/unit/test_bug381412.hk.gb2312.js | 73 +++++++++++++++ intl/uconv/tests/unit/test_bug381412.johab.js | 60 +++++++++++++ intl/uconv/tests/unit/test_bug381412.js | 56 ++++++++++++ 9 files changed, 570 insertions(+) create mode 100644 intl/uconv/tests/unit/test_bug381412.Big5-HKSCS.js create mode 100644 intl/uconv/tests/unit/test_bug381412.Big5.js create mode 100644 intl/uconv/tests/unit/test_bug381412.cp936.js create mode 100644 intl/uconv/tests/unit/test_bug381412.euc-kr.js create mode 100644 intl/uconv/tests/unit/test_bug381412.euc_jp.js create mode 100644 intl/uconv/tests/unit/test_bug381412.gb2312.js create mode 100644 intl/uconv/tests/unit/test_bug381412.hk.gb2312.js create mode 100644 intl/uconv/tests/unit/test_bug381412.johab.js create mode 100644 intl/uconv/tests/unit/test_bug381412.js diff --git a/intl/uconv/tests/unit/test_bug381412.Big5-HKSCS.js b/intl/uconv/tests/unit/test_bug381412.Big5-HKSCS.js new file mode 100644 index 00000000000..14220d847e5 --- /dev/null +++ b/intl/uconv/tests/unit/test_bug381412.Big5-HKSCS.js @@ -0,0 +1,60 @@ +const charset = "Big5-HKSCS"; + +function dumpStrings(inString, outString) { + 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 = ""; + } else { + for (i = 0; i < outString.length; ++i) { + dispOut += " x" + outString.charCodeAt(i).toString(16); + } + } + dump("\"" + dispIn + "\" ==> \"" + dispOut + "\"\n"); +} + +function error(inString, outString, msg){ + dumpStrings(inString, outString); + do_throw("security risk: " + msg); +} + +function run_test() { + var ScriptableUnicodeConverter = + Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter", + "nsIScriptableUnicodeConverter"); + + var converter = new ScriptableUnicodeConverter(); + converter.charset = charset; + + var leadByte, trailByte; + var inString; + for (leadByte = 1; leadByte < 0x100; ++leadByte) { + for (trailByte = 1; trailByte < 0x100; ++trailByte) { + inString = String.fromCharCode(leadByte, trailByte, 65); + var outString = converter.ConvertToUnicode(inString) + converter.Finish(); + switch (outString.length) { + case 1: + error(inString, outString, "2 byte sequence eaten"); + break; + case 2: + if (outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, "2 byte sequence converted to 1 ASCII"); + } + break; + case 3: + if (outString != inString && + outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, + "2 byte sequence converted to 2 ASCII"); + } + break; + } + } + } +} diff --git a/intl/uconv/tests/unit/test_bug381412.Big5.js b/intl/uconv/tests/unit/test_bug381412.Big5.js new file mode 100644 index 00000000000..d42976d5b3d --- /dev/null +++ b/intl/uconv/tests/unit/test_bug381412.Big5.js @@ -0,0 +1,60 @@ +const charset = "Big5"; + +function dumpStrings(inString, outString) { + 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 = ""; + } else { + for (i = 0; i < outString.length; ++i) { + dispOut += " x" + outString.charCodeAt(i).toString(16); + } + } + dump("\"" + dispIn + "\" ==> \"" + dispOut + "\"\n"); +} + +function error(inString, outString, msg){ + dumpStrings(inString, outString); + do_throw("security risk: " + msg); +} + +function run_test() { + var ScriptableUnicodeConverter = + Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter", + "nsIScriptableUnicodeConverter"); + + var converter = new ScriptableUnicodeConverter(); + converter.charset = charset; + + var leadByte, trailByte; + var inString; + for (leadByte = 1; leadByte < 0x100; ++leadByte) { + for (trailByte = 1; trailByte < 0x100; ++trailByte) { + inString = String.fromCharCode(leadByte, trailByte, 65); + var outString = converter.ConvertToUnicode(inString) + converter.Finish(); + switch (outString.length) { + case 1: + error(inString, outString, "2 byte sequence eaten"); + break; + case 2: + if (outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, "2 byte sequence converted to 1 ASCII"); + } + break; + case 3: + if (outString != inString && + outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, + "2 byte sequence converted to 2 ASCII"); + } + break; + } + } + } +} diff --git a/intl/uconv/tests/unit/test_bug381412.cp936.js b/intl/uconv/tests/unit/test_bug381412.cp936.js new file mode 100644 index 00000000000..903fed8ccfe --- /dev/null +++ b/intl/uconv/tests/unit/test_bug381412.cp936.js @@ -0,0 +1,56 @@ +const charset = "windows-936"; + +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 = ""; + } 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 run_test() { + var ScriptableUnicodeConverter = + Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter", + "nsIScriptableUnicodeConverter"); + + var converter = new ScriptableUnicodeConverter(); + converter.charset = charset; + + var leadByte, trailByte; + var inString; + for (leadByte = 1; leadByte < 0x100; ++leadByte) { + for (trailByte = 1; trailByte < 0x100; ++trailByte) { + inString = String.fromCharCode(leadByte, trailByte, 65); + var outString = converter.ConvertToUnicode(inString) + converter.Finish(); + switch (outString.length) { + case 1: + error(inString, outString, "2 byte sequence eaten"); + break; + case 2: + if (outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, "2 byte sequence converted to 1 ASCII"); + } + break; + case 3: + if (outString != inString && + outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, + "2 byte sequence converted to 2 ASCII"); + } + break; + } + } + } +} diff --git a/intl/uconv/tests/unit/test_bug381412.euc-kr.js b/intl/uconv/tests/unit/test_bug381412.euc-kr.js new file mode 100644 index 00000000000..fa69f8772a1 --- /dev/null +++ b/intl/uconv/tests/unit/test_bug381412.euc-kr.js @@ -0,0 +1,60 @@ +const charset = "EUC-KR"; + +function dumpStrings(inString, outString) { + 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 = ""; + } else { + for (i = 0; i < outString.length; ++i) { + dispOut += " x" + outString.charCodeAt(i).toString(16); + } + } + dump("\"" + dispIn + "\" ==> \"" + dispOut + "\"\n"); +} + +function error(inString, outString, msg){ + dumpStrings(inString, outString); + do_throw("security risk: " + msg); +} + +function run_test() { + var ScriptableUnicodeConverter = + Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter", + "nsIScriptableUnicodeConverter"); + + var converter = new ScriptableUnicodeConverter(); + converter.charset = charset; + + var leadByte, trailByte; + var inString; + for (leadByte = 1; leadByte < 0x100; ++leadByte) { + for (trailByte = 1; trailByte < 0x100; ++trailByte) { + inString = String.fromCharCode(leadByte, trailByte, 65); + var outString = converter.ConvertToUnicode(inString) + converter.Finish(); + switch (outString.length) { + case 1: + error(inString, outString, "2 byte sequence eaten"); + break; + case 2: + if (outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, "2 byte sequence converted to 1 ASCII"); + } + break; + case 3: + if (outString != inString && + outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, + "2 byte sequence converted to 2 ASCII"); + } + break; + } + } + } +} diff --git a/intl/uconv/tests/unit/test_bug381412.euc_jp.js b/intl/uconv/tests/unit/test_bug381412.euc_jp.js new file mode 100644 index 00000000000..344a09628af --- /dev/null +++ b/intl/uconv/tests/unit/test_bug381412.euc_jp.js @@ -0,0 +1,89 @@ +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 = ""; + } 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 test(inString) { + var outString = gConverter.ConvertToUnicode(inString) + + gConverter.Finish(); + + switch (outString.length) { + case 0: + case 1: + case 2: + error(inString, outString, "Unexpected error"); + break; + case 3: + error(inString, outString, "3 byte sequence eaten"); + break; + case 4: + if (outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80 && + outString.charCodeAt(2) < 0x80 && + outString.charCodeAt(3) < 0x80) { + error(inString, outString, + "3 byte sequence converted to 1 ASCII"); + } + break; + case 5: + if (outString != inString && + outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80 && + outString.charCodeAt(2) < 0x80 && + outString.charCodeAt(3) < 0x80 && + outString.charCodeAt(4) < 0x80) { + error(inString, outString, + "3 byte sequence converted to 2 ASCII"); + } + break; + case 6: + if (outString != inString && + outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80 && + outString.charCodeAt(2) < 0x80 && + outString.charCodeAt(3) < 0x80 && + outString.charCodeAt(4) < 0x80 && + outString.charCodeAt(5) < 0x80) { + error(inString, outString, + "3 byte sequence converted to 3 ASCII"); + } + break; + } +} + +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"); + } + } + } +} diff --git a/intl/uconv/tests/unit/test_bug381412.gb2312.js b/intl/uconv/tests/unit/test_bug381412.gb2312.js new file mode 100644 index 00000000000..d342bf0b9f9 --- /dev/null +++ b/intl/uconv/tests/unit/test_bug381412.gb2312.js @@ -0,0 +1,56 @@ +const charset = "GB2312"; + +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 = ""; + } 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 run_test() { + var ScriptableUnicodeConverter = + Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter", + "nsIScriptableUnicodeConverter"); + + var converter = new ScriptableUnicodeConverter(); + converter.charset = charset; + + var leadByte, trailByte; + var inString; + for (leadByte = 1; leadByte < 0x100; ++leadByte) { + for (trailByte = 1; trailByte < 0x100; ++trailByte) { + inString = String.fromCharCode(leadByte, trailByte, 65); + var outString = converter.ConvertToUnicode(inString) + converter.Finish(); + switch (outString.length) { + case 1: + error(inString, outString, "2 byte sequence eaten"); + break; + case 2: + if (outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, "2 byte sequence converted to 1 ASCII"); + } + break; + case 3: + if (outString != inString && + outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, + "2 byte sequence converted to 2 ASCII"); + } + break; + } + } + } +} diff --git a/intl/uconv/tests/unit/test_bug381412.hk.gb2312.js b/intl/uconv/tests/unit/test_bug381412.hk.gb2312.js new file mode 100644 index 00000000000..27e58fe470f --- /dev/null +++ b/intl/uconv/tests/unit/test_bug381412.hk.gb2312.js @@ -0,0 +1,73 @@ +const charset = "hz-gb-2312"; + +function dumpStrings(inString, outString) { + 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 = ""; + } else { + for (i = 0; i < outString.length; ++i) { + dispOut += " x" + outString.charCodeAt(i).toString(16); + } + } + dump("\"" + dispIn + "\" ==> \"" + dispOut + "\"\n"); +} + +function error(inString, outString, msg){ + dumpStrings(inString, outString); + do_throw("security risk: " + msg); +} + +function run_test() { + var ScriptableUnicodeConverter = + Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter", + "nsIScriptableUnicodeConverter"); + + var converter = new ScriptableUnicodeConverter(); + converter.charset = charset; + + var leadByte, trailByte; + var inString; + for (leadByte = 1; leadByte < 0x100; ++leadByte) { + for (trailByte = 1; trailByte < 0x100; ++trailByte) { + if (leadByte == 0x7e) { + if (trailByte == 0x7b || + trailByte == 0xa || + trailByte == 0x7e) { + // ignore escape sequences: + // ~{ (sets HZ-GB mode) + // ~\n ( ==> \n) + // ~~ ( ==> ~) + continue; + } + } + inString = String.fromCharCode(leadByte, trailByte, 65); + var outString = converter.ConvertToUnicode(inString) + + converter.Finish(); + switch (outString.length) { + case 1: + error(inString, outString, "2 byte sequence eaten"); + break; + case 2: + if (outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, + "2 byte sequence converted to 1 ASCII"); + } + break; + case 3: + if (outString != inString && + outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, + "2 byte sequence converted to 2 ASCII"); + } + break; + } + } + } +} diff --git a/intl/uconv/tests/unit/test_bug381412.johab.js b/intl/uconv/tests/unit/test_bug381412.johab.js new file mode 100644 index 00000000000..5cf82ed26e7 --- /dev/null +++ b/intl/uconv/tests/unit/test_bug381412.johab.js @@ -0,0 +1,60 @@ +const charset = "x-johab"; + +function dumpStrings(inString, outString) { + 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 = ""; + } else { + for (i = 0; i < outString.length; ++i) { + dispOut += " x" + outString.charCodeAt(i).toString(16); + } + } + dump("\"" + dispIn + "\" ==> \"" + dispOut + "\"\n"); +} + +function error(inString, outString, msg){ + dumpStrings(inString, outString); + do_throw("security risk: " + msg); +} + +function run_test() { + var ScriptableUnicodeConverter = + Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter", + "nsIScriptableUnicodeConverter"); + + var converter = new ScriptableUnicodeConverter(); + converter.charset = charset; + + var leadByte, trailByte; + var inString; + for (leadByte = 1; leadByte < 0x100; ++leadByte) { + for (trailByte = 1; trailByte < 0x100; ++trailByte) { + inString = String.fromCharCode(leadByte, trailByte, 65); + var outString = converter.ConvertToUnicode(inString) + converter.Finish(); + switch (outString.length) { + case 1: + error(inString, outString, "2 byte sequence eaten"); + break; + case 2: + if (outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, "2 byte sequence converted to 1 ASCII"); + } + break; + case 3: + if (outString != inString && + outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, + "2 byte sequence converted to 2 ASCII"); + } + break; + } + } + } +} diff --git a/intl/uconv/tests/unit/test_bug381412.js b/intl/uconv/tests/unit/test_bug381412.js new file mode 100644 index 00000000000..622b7bf7f5b --- /dev/null +++ b/intl/uconv/tests/unit/test_bug381412.js @@ -0,0 +1,56 @@ +const charset = "Shift_JIS"; + +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 = ""; + } 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 run_test() { + var ScriptableUnicodeConverter = + Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter", + "nsIScriptableUnicodeConverter"); + + var converter = new ScriptableUnicodeConverter(); + converter.charset = charset; + + var leadByte, trailByte; + var inString; + for (leadByte = 1; leadByte < 0x100; ++leadByte) { + for (trailByte = 1; trailByte < 0x100; ++trailByte) { + inString = String.fromCharCode(leadByte, trailByte, 65); + var outString = converter.ConvertToUnicode(inString) + converter.Finish(); + switch (outString.length) { + case 1: + error(inString, outString, "2 byte sequence eaten"); + break; + case 2: + if (outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, "2 byte sequence converted to 1 ASCII"); + } + break; + case 3: + if (outString != inString && + outString.charCodeAt(0) < 0x80 && + outString.charCodeAt(1) < 0x80) { + error(inString, outString, + "2 byte sequence converted to 2 ASCII"); + } + break; + } + } + } +}