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