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