netwerk/test/unit/test_localstreams.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     1 // Tests bug 304414
     3 const PR_RDONLY = 0x1;  // see prio.h
     5 // Does some sanity checks on the stream and returns the number of bytes read
     6 // when the checks passed.
     7 function test_stream(stream) {
     8   // This test only handles blocking streams; that's desired for file streams
     9   // anyway.
    10   do_check_eq(stream.isNonBlocking(), false);
    12   // Check that the stream is not buffered
    13   do_check_eq(Components.classes["@mozilla.org/io-util;1"]
    14                          .getService(Components.interfaces.nsIIOUtil)
    15                          .inputStreamIsBuffered(stream),
    16               false);
    18   // Wrap it in a binary stream (to avoid wrong results that
    19   // scriptablestream would produce with binary content)
    20   var binstream = Components.classes['@mozilla.org/binaryinputstream;1']
    21                             .createInstance(Components.interfaces.nsIBinaryInputStream);
    22   binstream.setInputStream(stream);
    24   var numread = 0;
    25   for (;;) {
    26     do_check_eq(stream.available(), binstream.available());
    27     var avail = stream.available();
    28     do_check_neq(avail, -1);
    30     // PR_UINT32_MAX and PR_INT32_MAX; the files we're testing with aren't that
    31     // large.
    32     do_check_neq(avail, Math.pow(2, 32) - 1);
    33     do_check_neq(avail, Math.pow(2, 31) - 1);
    35     if (!avail) {
    36       // For blocking streams, available() only returns 0 on EOF
    37       // Make sure that there is really no data left
    38       var could_read = false;
    39       try {
    40         binstream.readByteArray(1);
    41         could_read = true;
    42       } catch (e) {
    43         // We expect the exception, so do nothing here
    44       }
    45       if (could_read)
    46         do_throw("Data readable when available indicated EOF!");
    47       return numread;
    48     }
    50     dump("Trying to read " + avail + " bytes\n");
    51     // Note: Verification that this does return as much bytes as we asked for is
    52     // done in the binarystream implementation
    53     var data = binstream.readByteArray(avail);
    55     numread += avail;
    56   }
    57   return numread;
    58 }
    60 function stream_for_file(file) {
    61   var str = Components.classes["@mozilla.org/network/file-input-stream;1"]
    62                       .createInstance(Components.interfaces.nsIFileInputStream);
    63   str.init(file, PR_RDONLY, 0, 0);
    64   return str;
    65 }
    67 function stream_from_channel(file) {
    68   var ios = Components.classes["@mozilla.org/network/io-service;1"]
    69                       .getService(Components.interfaces.nsIIOService);
    70   var uri = ios.newFileURI(file);
    71   return ios.newChannelFromURI(uri).open();
    72 }
    74 function run_test() {
    75   // Get a file and a directory in order to do some testing
    76   var file = do_get_file("../unit/data/test_readline6.txt");
    77   var len = file.fileSize;
    78   do_check_eq(test_stream(stream_for_file(file)), len);
    79   do_check_eq(test_stream(stream_from_channel(file)), len);
    80   var dir = file.parent;
    81   test_stream(stream_from_channel(dir)); // Can't do size checking
    82 }

mercurial