Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / netwerk / test / unit / test_localstreams.js
blobf7cf4b9b5d4a225324e16a4c0d192b4f9041902b
1 // Tests bug 304414
3 const PR_RDONLY = 0x1; // see prio.h
5 function getDir(key) {
6 var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"]
7 .getService(Components.interfaces.nsIProperties);
8 return dirSvc.get(key, Components.interfaces.nsILocalFile);
11 // Does some sanity checks on the stream and returns the number of bytes read
12 // when the checks passed.
13 function test_stream(stream) {
14 // This test only handles blocking streams; that's desired for file streams
15 // anyway.
16 do_check_eq(stream.isNonBlocking(), 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
45 if (could_read)
46 do_throw("Data readable when available indicated EOF!");
47 return numread;
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;
57 return numread;
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;
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();
74 function run_test() {
75 // Get a file and a directory in order to do some testing
76 var file = getDir("XpcomLib");
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