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

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 const charset = "EUC-JP";
     2 const ScriptableUnicodeConverter =
     3 	Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter",
     4 			       "nsIScriptableUnicodeConverter");
     5 var gConverter;
     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 }
    25 function IsASCII(charCode) {
    26     return (charCode <= 0x7e);
    27 }
    29 function IsNotGR(charCode) {
    30     return (charCode < 0xa1 || charCode > 0xfe);
    31 }
    33 function test(inString) {
    34     var outString = gConverter.ConvertToUnicode(inString) +
    35 	            gConverter.Finish();
    37     var outLen = outString.length;
    38     if (IsASCII(inString.charCodeAt(1)) && 
    39 	inString.charCodeAt(1) != outString.charCodeAt(outLen - 5))  {
    40 	error(inString, outString, "ASCII second byte eaten");
    41     } else if (IsASCII(inString.charCodeAt(2)) && 
    42 	       inString.charCodeAt(2) != outString.charCodeAt(outLen - 4)) {
    43 	error(inString, outString, "ASCII third byte eaten");
    44     } else if (inString.charCodeAt(0) == 0x8f &&
    45 	       inString.charCodeAt(1) > 0x7f &&
    46 	       IsNotGR(inString.charCodeAt(2)) &&
    47 	       (!(outString.charCodeAt(outLen - 4) == 0xFFFD ||
    48 		  outString.charCodeAt(outLen - 4) == inString.charCodeAt(2)))) {
    49 	error(inString, outString, "non-GR third byte eaten");
    50     }
    51 }
    53 function run_test() {
    54     gConverter = new ScriptableUnicodeConverter();
    55     gConverter.charset = charset;
    57     var byte1, byte2, byte3;
    58     for (byte1 = 1; byte1 < 0x100; ++byte1) {
    59 	for (byte2 = 1; byte2 < 0x100; ++byte2) {
    60 	    if (byte1 == 0x8f) {
    61 		for (byte3 = 1; byte3 < 0x100; ++byte3) {
    62 		    test(String.fromCharCode(byte1, byte2, byte3) + "foo");
    63 		}
    64 	    } else {
    65 		test(String.fromCharCode(byte1, byte2) + " foo");
    66 	    }
    67 	}
    68     }
    69 }

mercurial