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 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0 |
michael@0 | 3 | */ |
michael@0 | 4 | |
michael@0 | 5 | function BinaryComparer(file, callback) { |
michael@0 | 6 | var fstream = Cc["@mozilla.org/network/file-input-stream;1"]. |
michael@0 | 7 | createInstance(Ci.nsIFileInputStream); |
michael@0 | 8 | fstream.init(file, -1, 0, 0); |
michael@0 | 9 | this.length = file.fileSize; |
michael@0 | 10 | this.fileStream = Cc["@mozilla.org/binaryinputstream;1"]. |
michael@0 | 11 | createInstance(Ci.nsIBinaryInputStream); |
michael@0 | 12 | this.fileStream.setInputStream(fstream); |
michael@0 | 13 | this.offset = 0; |
michael@0 | 14 | this.callback = callback; |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | BinaryComparer.prototype = { |
michael@0 | 18 | fileStream: null, |
michael@0 | 19 | offset: null, |
michael@0 | 20 | length: null, |
michael@0 | 21 | callback: null, |
michael@0 | 22 | |
michael@0 | 23 | onStartRequest: function(aRequest, aContext) { |
michael@0 | 24 | }, |
michael@0 | 25 | |
michael@0 | 26 | onStopRequest: function(aRequest, aContext, aStatusCode) { |
michael@0 | 27 | this.fileStream.close(); |
michael@0 | 28 | do_check_eq(aStatusCode, Components.results.NS_OK); |
michael@0 | 29 | do_check_eq(this.offset, this.length); |
michael@0 | 30 | this.callback(); |
michael@0 | 31 | }, |
michael@0 | 32 | |
michael@0 | 33 | onDataAvailable: function(aRequest, aContext, aInputStream, aOffset, aCount) { |
michael@0 | 34 | var stream = Cc["@mozilla.org/binaryinputstream;1"]. |
michael@0 | 35 | createInstance(Ci.nsIBinaryInputStream); |
michael@0 | 36 | stream.setInputStream(aInputStream); |
michael@0 | 37 | var source, actual; |
michael@0 | 38 | for (var i = 0; i < aCount; i++) { |
michael@0 | 39 | try { |
michael@0 | 40 | source = this.fileStream.read8(); |
michael@0 | 41 | } |
michael@0 | 42 | catch (e) { |
michael@0 | 43 | do_throw("Unable to read from file at offset " + this.offset + " " + e); |
michael@0 | 44 | } |
michael@0 | 45 | try { |
michael@0 | 46 | actual = stream.read8(); |
michael@0 | 47 | } |
michael@0 | 48 | catch (e) { |
michael@0 | 49 | do_throw("Unable to read from converted stream at offset " + this.offset + " " + e); |
michael@0 | 50 | } |
michael@0 | 51 | // The byte at offset 9 is the OS byte (see RFC 1952, section 2.3), which |
michael@0 | 52 | // can legitimately differ when the source is compressed on different |
michael@0 | 53 | // operating systems. The actual .gz for this test was created on a Unix |
michael@0 | 54 | // system, but we want the test to work correctly everywhere. So ignore |
michael@0 | 55 | // the byte at offset 9. |
michael@0 | 56 | if (this.offset == 9) |
michael@0 | 57 | ; |
michael@0 | 58 | else if (source != actual) |
michael@0 | 59 | do_throw("Invalid value " + actual + " at offset " + this.offset + ", should have been " + source); |
michael@0 | 60 | this.offset++; |
michael@0 | 61 | } |
michael@0 | 62 | } |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | function comparer_callback() |
michael@0 | 66 | { |
michael@0 | 67 | do_test_finished(); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | function run_test() |
michael@0 | 71 | { |
michael@0 | 72 | var source = do_get_file(DATA_DIR + "test_bug717061.html"); |
michael@0 | 73 | var comparer = new BinaryComparer(do_get_file(DATA_DIR + "test_bug717061.gz"), |
michael@0 | 74 | comparer_callback); |
michael@0 | 75 | |
michael@0 | 76 | // Prepare the stream converter |
michael@0 | 77 | var scs = Cc["@mozilla.org/streamConverters;1"]. |
michael@0 | 78 | getService(Ci.nsIStreamConverterService); |
michael@0 | 79 | var converter = scs.asyncConvertData("uncompressed", "gzip", comparer, null); |
michael@0 | 80 | |
michael@0 | 81 | // Open the expected output file |
michael@0 | 82 | var fstream = Cc["@mozilla.org/network/file-input-stream;1"]. |
michael@0 | 83 | createInstance(Ci.nsIFileInputStream); |
michael@0 | 84 | fstream.init(source, -1, 0, 0); |
michael@0 | 85 | |
michael@0 | 86 | // Set up a pump to push data from the file to the stream converter |
michael@0 | 87 | var pump = Cc["@mozilla.org/network/input-stream-pump;1"]. |
michael@0 | 88 | createInstance(Ci.nsIInputStreamPump); |
michael@0 | 89 | pump.init(fstream, -1, -1, 0, 0, true); |
michael@0 | 90 | pump.asyncRead(converter, null); |
michael@0 | 91 | do_test_pending(); |
michael@0 | 92 | } |