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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 const charset = "GB2312";
     3 function error(inString, outString, msg){
     4     var dispIn = "";
     5     var dispOut = "";
     6     var i;
     7     for (i = 0; i < inString.length; ++i) {
     8 	dispIn += " x" + inString.charCodeAt(i).toString(16);
     9     }
    10     if (outString.length == 0) {
    11 	dispOut = "<empty>";
    12     } else {
    13 	for (i = 0; i < outString.length; ++i) {
    14 	    dispOut += " x" + outString.charCodeAt(i).toString(16);
    15 	}
    16     }
    17     dump("\"" + dispIn + "\" ==> \"" + dispOut + "\"\n");
    18     do_throw("security risk: " + msg);
    19 }
    21 function run_test() {
    22     var ScriptableUnicodeConverter =
    23 	Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter",
    24 			       "nsIScriptableUnicodeConverter");
    26     var converter = new ScriptableUnicodeConverter();
    27     converter.charset = charset;
    29     var leadByte, trailByte;
    30     var inString;
    31     for (leadByte = 1; leadByte < 0x100; ++leadByte) {
    32 	for (trailByte = 1; trailByte < 0x100; ++trailByte) {
    33 	    inString = String.fromCharCode(leadByte, trailByte, 65);
    34 	    var outString = converter.ConvertToUnicode(inString) + converter.Finish();
    35 	    switch (outString.length) {
    36 	        case 1:
    37 		    error(inString, outString, "2 byte sequence eaten");
    38 		    break;
    39 	        case 2:
    40 		    if (outString.charCodeAt(0) < 0x80 &&
    41 			outString.charCodeAt(1) < 0x80) {
    42 			error(inString, outString, "2 byte sequence converted to 1 ASCII");
    43 		    }
    44 		    break;
    45 	        case 3:
    46 		    if (outString != inString &&
    47 			outString.charCodeAt(0) < 0x80 &&
    48 			outString.charCodeAt(1) < 0x80) {
    49 			error(inString, outString,
    50 			      "2 byte sequence converted to 2 ASCII");
    51 		    }
    52 		    break;
    53 	    }
    54 	}
    55     }
    56 }

mercurial