Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Tests illegal UTF-8 sequences |
michael@0 | 2 | |
michael@0 | 3 | const Cc = Components.Constructor; |
michael@0 | 4 | const Ci = Components.interfaces; |
michael@0 | 5 | |
michael@0 | 6 | const tests = [ |
michael@0 | 7 | { inStrings: ["%80", // Illegal or incomplete sequences |
michael@0 | 8 | "%8f", |
michael@0 | 9 | "%90", |
michael@0 | 10 | "%9f", |
michael@0 | 11 | "%a0", |
michael@0 | 12 | "%bf", |
michael@0 | 13 | "%c0", |
michael@0 | 14 | "%c1", |
michael@0 | 15 | "%c2", |
michael@0 | 16 | "%df", |
michael@0 | 17 | "%e0", |
michael@0 | 18 | "%e0%a0", |
michael@0 | 19 | "%e0%bf", |
michael@0 | 20 | "%ed%80", |
michael@0 | 21 | "%ed%9f", |
michael@0 | 22 | "%ef", |
michael@0 | 23 | "%ef%bf", |
michael@0 | 24 | "%f0", |
michael@0 | 25 | "%f0%90", |
michael@0 | 26 | "%f0%90%80", |
michael@0 | 27 | "%f0%90%bf", |
michael@0 | 28 | "%f0%bf", |
michael@0 | 29 | "%f0%bf%80", |
michael@0 | 30 | "%f0%bf%bf", |
michael@0 | 31 | "%f4", |
michael@0 | 32 | "%f4%80", |
michael@0 | 33 | "%f4%80%80", |
michael@0 | 34 | "%f4%80%bf", |
michael@0 | 35 | "%f4%8f", |
michael@0 | 36 | "%f4%8f%80", |
michael@0 | 37 | "%f4%8f%bf", |
michael@0 | 38 | "%f5", |
michael@0 | 39 | "%f7", |
michael@0 | 40 | "%f8", |
michael@0 | 41 | "%fb", |
michael@0 | 42 | "%fc", |
michael@0 | 43 | "%fd"], |
michael@0 | 44 | expected: "ABC\ufffdXYZ" }, |
michael@0 | 45 | |
michael@0 | 46 | { inStrings: ["%c0%af", // Illegal bytes in 2-octet |
michael@0 | 47 | "%c1%af"], // sequences |
michael@0 | 48 | expected: "ABC\ufffd\ufffdXYZ" }, |
michael@0 | 49 | |
michael@0 | 50 | { inStrings: ["%e0%80%80", // Illegal bytes in 3-octet |
michael@0 | 51 | "%e0%80%af", // sequences |
michael@0 | 52 | "%e0%9f%bf", |
michael@0 | 53 | // long surrogates |
michael@0 | 54 | "%ed%a0%80", // D800 |
michael@0 | 55 | "%ed%ad%bf", // DB7F |
michael@0 | 56 | "%ed%ae%80", // DB80 |
michael@0 | 57 | "%ed%af%bf", // DBFF |
michael@0 | 58 | "%ed%b0%80", // DC00 |
michael@0 | 59 | "%ed%be%80", // DF80 |
michael@0 | 60 | "%ed%bf%bf"], // DFFF |
michael@0 | 61 | expected: "ABC\ufffd\ufffd\ufffdXYZ" }, |
michael@0 | 62 | |
michael@0 | 63 | { inStrings: ["%f0%80%80%80", // Illegal bytes in 4-octet |
michael@0 | 64 | "%f0%80%80%af", // sequences |
michael@0 | 65 | "%f0%8f%bf%bf", |
michael@0 | 66 | "%f4%90%80%80", |
michael@0 | 67 | "%f4%bf%bf%bf", |
michael@0 | 68 | "%f5%80%80%80", |
michael@0 | 69 | "%f7%bf%bf%bf"], |
michael@0 | 70 | expected: "ABC\ufffd\ufffd\ufffd\ufffdXYZ" }, |
michael@0 | 71 | |
michael@0 | 72 | { inStrings: ["%f8%80%80%80%80", // Illegal bytes in 5-octet |
michael@0 | 73 | "%f8%80%80%80%af", // sequences |
michael@0 | 74 | "%fb%bf%bf%bf%bf"], |
michael@0 | 75 | expected: "ABC\ufffd\ufffd\ufffd\ufffd\ufffdXYZ" }, |
michael@0 | 76 | |
michael@0 | 77 | // Surrogate pairs |
michael@0 | 78 | { inStrings: ["%ed%a0%80%ed%b0%80", // D800 DC00 |
michael@0 | 79 | "%ed%a0%80%ed%bf%bf", // D800 DFFF |
michael@0 | 80 | "%ed%ad%bf%ed%b0%80", // DB7F DC00 |
michael@0 | 81 | "%ed%ad%bf%ed%bf%bf", // DB7F DFFF |
michael@0 | 82 | "%ed%ae%80%ed%b0%80", // DB80 DC00 |
michael@0 | 83 | "%ed%ae%80%ed%bf%bf", // DB80 DFFF |
michael@0 | 84 | "%ed%af%bf%ed%b0%80", // DBFF DC00 |
michael@0 | 85 | "%ed%ad%bf%ed%bf%bf", // DBFF DFFF |
michael@0 | 86 | "%fc%80%80%80%80%80", // Illegal bytes in 6-octet |
michael@0 | 87 | "%fc%80%80%80%80%af", // sequences |
michael@0 | 88 | "%fd%bf%bf%bf%bf%bf"], |
michael@0 | 89 | expected: "ABC\ufffd\ufffd\ufffd\ufffd\ufffd\ufffdXYZ" }, |
michael@0 | 90 | ]; |
michael@0 | 91 | |
michael@0 | 92 | |
michael@0 | 93 | function testCaseInputStream(inStr, expected) |
michael@0 | 94 | { |
michael@0 | 95 | var dataURI = "data:text/plain; charset=UTF-8,ABC" + inStr + "XYZ" |
michael@0 | 96 | dump(inStr + "==>"); |
michael@0 | 97 | |
michael@0 | 98 | var IOService = Cc("@mozilla.org/network/io-service;1", |
michael@0 | 99 | "nsIIOService"); |
michael@0 | 100 | var ConverterInputStream = |
michael@0 | 101 | Cc("@mozilla.org/intl/converter-input-stream;1", |
michael@0 | 102 | "nsIConverterInputStream", |
michael@0 | 103 | "init"); |
michael@0 | 104 | |
michael@0 | 105 | var ios = new IOService(); |
michael@0 | 106 | var channel = ios.newChannel(dataURI, "", null); |
michael@0 | 107 | var testInputStream = channel.open(); |
michael@0 | 108 | var testConverter = new ConverterInputStream(testInputStream, |
michael@0 | 109 | "UTF-8", |
michael@0 | 110 | 16, |
michael@0 | 111 | 0xFFFD); |
michael@0 | 112 | |
michael@0 | 113 | if (!(testConverter instanceof Ci.nsIUnicharLineInputStream)) |
michael@0 | 114 | throw "not line input stream"; |
michael@0 | 115 | |
michael@0 | 116 | var outStr = ""; |
michael@0 | 117 | var more; |
michael@0 | 118 | do { |
michael@0 | 119 | // read the line and check for eof |
michael@0 | 120 | var line = {}; |
michael@0 | 121 | more = testConverter.readLine(line); |
michael@0 | 122 | outStr += line.value; |
michael@0 | 123 | } while (more); |
michael@0 | 124 | |
michael@0 | 125 | dump(outStr + "; expected=" + expected + "\n"); |
michael@0 | 126 | do_check_eq(outStr, expected); |
michael@0 | 127 | do_check_eq(outStr.length, expected.length); |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | function run_test() { |
michael@0 | 131 | for (var t of tests) { |
michael@0 | 132 | for (var inStr of t.inStrings) { |
michael@0 | 133 | testCaseInputStream(inStr, t.expected); |
michael@0 | 134 | } |
michael@0 | 135 | } |
michael@0 | 136 | } |