1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/unit/test_localstreams.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,83 @@ 1.4 +// Tests bug 304414 1.5 + 1.6 +const PR_RDONLY = 0x1; // see prio.h 1.7 + 1.8 +// Does some sanity checks on the stream and returns the number of bytes read 1.9 +// when the checks passed. 1.10 +function test_stream(stream) { 1.11 + // This test only handles blocking streams; that's desired for file streams 1.12 + // anyway. 1.13 + do_check_eq(stream.isNonBlocking(), false); 1.14 + 1.15 + // Check that the stream is not buffered 1.16 + do_check_eq(Components.classes["@mozilla.org/io-util;1"] 1.17 + .getService(Components.interfaces.nsIIOUtil) 1.18 + .inputStreamIsBuffered(stream), 1.19 + false); 1.20 + 1.21 + // Wrap it in a binary stream (to avoid wrong results that 1.22 + // scriptablestream would produce with binary content) 1.23 + var binstream = Components.classes['@mozilla.org/binaryinputstream;1'] 1.24 + .createInstance(Components.interfaces.nsIBinaryInputStream); 1.25 + binstream.setInputStream(stream); 1.26 + 1.27 + var numread = 0; 1.28 + for (;;) { 1.29 + do_check_eq(stream.available(), binstream.available()); 1.30 + var avail = stream.available(); 1.31 + do_check_neq(avail, -1); 1.32 + 1.33 + // PR_UINT32_MAX and PR_INT32_MAX; the files we're testing with aren't that 1.34 + // large. 1.35 + do_check_neq(avail, Math.pow(2, 32) - 1); 1.36 + do_check_neq(avail, Math.pow(2, 31) - 1); 1.37 + 1.38 + if (!avail) { 1.39 + // For blocking streams, available() only returns 0 on EOF 1.40 + // Make sure that there is really no data left 1.41 + var could_read = false; 1.42 + try { 1.43 + binstream.readByteArray(1); 1.44 + could_read = true; 1.45 + } catch (e) { 1.46 + // We expect the exception, so do nothing here 1.47 + } 1.48 + if (could_read) 1.49 + do_throw("Data readable when available indicated EOF!"); 1.50 + return numread; 1.51 + } 1.52 + 1.53 + dump("Trying to read " + avail + " bytes\n"); 1.54 + // Note: Verification that this does return as much bytes as we asked for is 1.55 + // done in the binarystream implementation 1.56 + var data = binstream.readByteArray(avail); 1.57 + 1.58 + numread += avail; 1.59 + } 1.60 + return numread; 1.61 +} 1.62 + 1.63 +function stream_for_file(file) { 1.64 + var str = Components.classes["@mozilla.org/network/file-input-stream;1"] 1.65 + .createInstance(Components.interfaces.nsIFileInputStream); 1.66 + str.init(file, PR_RDONLY, 0, 0); 1.67 + return str; 1.68 +} 1.69 + 1.70 +function stream_from_channel(file) { 1.71 + var ios = Components.classes["@mozilla.org/network/io-service;1"] 1.72 + .getService(Components.interfaces.nsIIOService); 1.73 + var uri = ios.newFileURI(file); 1.74 + return ios.newChannelFromURI(uri).open(); 1.75 +} 1.76 + 1.77 +function run_test() { 1.78 + // Get a file and a directory in order to do some testing 1.79 + var file = do_get_file("../unit/data/test_readline6.txt"); 1.80 + var len = file.fileSize; 1.81 + do_check_eq(test_stream(stream_for_file(file)), len); 1.82 + do_check_eq(test_stream(stream_from_channel(file)), len); 1.83 + var dir = file.parent; 1.84 + test_stream(stream_from_channel(dir)); // Can't do size checking 1.85 +} 1.86 +