modules/libjar/zipwriter/test/unit/test_bug399727.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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  */
     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 }
    18 BinaryComparer.prototype = {
    19   fileStream: null,
    20   offset: null,
    21   length: null,
    22   callback: null,
    24   onStartRequest: function(aRequest, aContext) {
    25   },
    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   },
    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 }
    59 function comparer_callback()
    60 {
    61   do_test_finished();
    62 }
    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);
    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);
    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);
    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 }

mercurial