summaryrefslogtreecommitdiffstats
path: root/intl/uconv/tests/unit/test_bug381412.js
blob: 622b7bf7f5b4bd3bd5e303ca99e4b7b39758d470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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 = "<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 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;
	    }
	}
    }
}