michael@0: // Tests bug 304414 michael@0: michael@0: const PR_RDONLY = 0x1; // see prio.h michael@0: michael@0: // Does some sanity checks on the stream and returns the number of bytes read michael@0: // when the checks passed. michael@0: function test_stream(stream) { michael@0: // This test only handles blocking streams; that's desired for file streams michael@0: // anyway. michael@0: do_check_eq(stream.isNonBlocking(), false); michael@0: michael@0: // Check that the stream is not buffered michael@0: do_check_eq(Components.classes["@mozilla.org/io-util;1"] michael@0: .getService(Components.interfaces.nsIIOUtil) michael@0: .inputStreamIsBuffered(stream), michael@0: false); michael@0: michael@0: // Wrap it in a binary stream (to avoid wrong results that michael@0: // scriptablestream would produce with binary content) michael@0: var binstream = Components.classes['@mozilla.org/binaryinputstream;1'] michael@0: .createInstance(Components.interfaces.nsIBinaryInputStream); michael@0: binstream.setInputStream(stream); michael@0: michael@0: var numread = 0; michael@0: for (;;) { michael@0: do_check_eq(stream.available(), binstream.available()); michael@0: var avail = stream.available(); michael@0: do_check_neq(avail, -1); michael@0: michael@0: // PR_UINT32_MAX and PR_INT32_MAX; the files we're testing with aren't that michael@0: // large. michael@0: do_check_neq(avail, Math.pow(2, 32) - 1); michael@0: do_check_neq(avail, Math.pow(2, 31) - 1); michael@0: michael@0: if (!avail) { michael@0: // For blocking streams, available() only returns 0 on EOF michael@0: // Make sure that there is really no data left michael@0: var could_read = false; michael@0: try { michael@0: binstream.readByteArray(1); michael@0: could_read = true; michael@0: } catch (e) { michael@0: // We expect the exception, so do nothing here michael@0: } michael@0: if (could_read) michael@0: do_throw("Data readable when available indicated EOF!"); michael@0: return numread; michael@0: } michael@0: michael@0: dump("Trying to read " + avail + " bytes\n"); michael@0: // Note: Verification that this does return as much bytes as we asked for is michael@0: // done in the binarystream implementation michael@0: var data = binstream.readByteArray(avail); michael@0: michael@0: numread += avail; michael@0: } michael@0: return numread; michael@0: } michael@0: michael@0: function stream_for_file(file) { michael@0: var str = Components.classes["@mozilla.org/network/file-input-stream;1"] michael@0: .createInstance(Components.interfaces.nsIFileInputStream); michael@0: str.init(file, PR_RDONLY, 0, 0); michael@0: return str; michael@0: } michael@0: michael@0: function stream_from_channel(file) { michael@0: var ios = Components.classes["@mozilla.org/network/io-service;1"] michael@0: .getService(Components.interfaces.nsIIOService); michael@0: var uri = ios.newFileURI(file); michael@0: return ios.newChannelFromURI(uri).open(); michael@0: } michael@0: michael@0: function run_test() { michael@0: // Get a file and a directory in order to do some testing michael@0: var file = do_get_file("../unit/data/test_readline6.txt"); michael@0: var len = file.fileSize; michael@0: do_check_eq(test_stream(stream_for_file(file)), len); michael@0: do_check_eq(test_stream(stream_from_channel(file)), len); michael@0: var dir = file.parent; michael@0: test_stream(stream_from_channel(dir)); // Can't do size checking michael@0: } michael@0: