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