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 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | function BinaryComparer(file, callback) { |
michael@0 | 7 | var fstream = Cc["@mozilla.org/network/file-input-stream;1"]. |
michael@0 | 8 | createInstance(Ci.nsIFileInputStream); |
michael@0 | 9 | fstream.init(file, -1, 0, 0); |
michael@0 | 10 | this.length = file.fileSize; |
michael@0 | 11 | this.fileStream = Cc["@mozilla.org/binaryinputstream;1"]. |
michael@0 | 12 | createInstance(Ci.nsIBinaryInputStream); |
michael@0 | 13 | this.fileStream.setInputStream(fstream); |
michael@0 | 14 | this.offset = 0; |
michael@0 | 15 | this.callback = callback; |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | BinaryComparer.prototype = { |
michael@0 | 19 | fileStream: null, |
michael@0 | 20 | offset: null, |
michael@0 | 21 | length: null, |
michael@0 | 22 | callback: null, |
michael@0 | 23 | |
michael@0 | 24 | onStartRequest: function(aRequest, aContext) { |
michael@0 | 25 | }, |
michael@0 | 26 | |
michael@0 | 27 | onStopRequest: function(aRequest, aContext, aStatusCode) { |
michael@0 | 28 | this.fileStream.close(); |
michael@0 | 29 | do_check_eq(aStatusCode, Components.results.NS_OK); |
michael@0 | 30 | do_check_eq(this.offset, this.length); |
michael@0 | 31 | this.callback(); |
michael@0 | 32 | }, |
michael@0 | 33 | |
michael@0 | 34 | onDataAvailable: function(aRequest, aContext, aInputStream, aOffset, aCount) { |
michael@0 | 35 | var stream = Cc["@mozilla.org/binaryinputstream;1"]. |
michael@0 | 36 | createInstance(Ci.nsIBinaryInputStream); |
michael@0 | 37 | stream.setInputStream(aInputStream); |
michael@0 | 38 | var source, actual; |
michael@0 | 39 | for (var i = 0; i < aCount; i++) { |
michael@0 | 40 | try { |
michael@0 | 41 | source = this.fileStream.read8(); |
michael@0 | 42 | } |
michael@0 | 43 | catch (e) { |
michael@0 | 44 | do_throw("Unable to read from file at offset " + this.offset + " " + e); |
michael@0 | 45 | } |
michael@0 | 46 | try { |
michael@0 | 47 | actual = stream.read8(); |
michael@0 | 48 | } |
michael@0 | 49 | catch (e) { |
michael@0 | 50 | do_throw("Unable to read from converted stream at offset " + this.offset + " " + e); |
michael@0 | 51 | } |
michael@0 | 52 | if (source != actual) |
michael@0 | 53 | do_throw("Invalid value " + actual + " at offset " + this.offset + ", should have been " + source); |
michael@0 | 54 | this.offset++; |
michael@0 | 55 | } |
michael@0 | 56 | } |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | function comparer_callback() |
michael@0 | 60 | { |
michael@0 | 61 | do_test_finished(); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | function run_test() |
michael@0 | 65 | { |
michael@0 | 66 | var source = do_get_file(DATA_DIR + "test_bug399727.html"); |
michael@0 | 67 | var comparer = new BinaryComparer(do_get_file(DATA_DIR + "test_bug399727.zlib"), |
michael@0 | 68 | comparer_callback); |
michael@0 | 69 | |
michael@0 | 70 | // Prepare the stream converter |
michael@0 | 71 | var scs = Cc["@mozilla.org/streamConverters;1"]. |
michael@0 | 72 | getService(Ci.nsIStreamConverterService); |
michael@0 | 73 | var converter = scs.asyncConvertData("uncompressed", "deflate", comparer, null); |
michael@0 | 74 | |
michael@0 | 75 | // Open the expected output file |
michael@0 | 76 | var fstream = Cc["@mozilla.org/network/file-input-stream;1"]. |
michael@0 | 77 | createInstance(Ci.nsIFileInputStream); |
michael@0 | 78 | fstream.init(source, -1, 0, 0); |
michael@0 | 79 | |
michael@0 | 80 | // Set up a pump to push data from the file to the stream converter |
michael@0 | 81 | var pump = Cc["@mozilla.org/network/input-stream-pump;1"]. |
michael@0 | 82 | createInstance(Ci.nsIInputStreamPump); |
michael@0 | 83 | pump.init(fstream, -1, -1, 0, 0, true); |
michael@0 | 84 | pump.asyncRead(converter, null); |
michael@0 | 85 | do_test_pending(); |
michael@0 | 86 | } |