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