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

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

mercurial