1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/unit/test_file_protocol.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,247 @@ 1.4 +/* run some tests on the file:// protocol handler */ 1.5 + 1.6 +const PR_RDONLY = 0x1; // see prio.h 1.7 + 1.8 +const special_type = "application/x-our-special-type"; 1.9 + 1.10 +[ 1.11 + test_read_file, 1.12 + test_read_dir_1, 1.13 + test_read_dir_2, 1.14 + test_upload_file, 1.15 + test_load_replace, 1.16 + do_test_finished 1.17 +].forEach(add_test); 1.18 + 1.19 +function getFile(key) { 1.20 + var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"] 1.21 + .getService(Components.interfaces.nsIProperties); 1.22 + return dirSvc.get(key, Components.interfaces.nsILocalFile); 1.23 +} 1.24 + 1.25 +function new_file_input_stream(file, buffered) { 1.26 + var stream = 1.27 + Cc["@mozilla.org/network/file-input-stream;1"]. 1.28 + createInstance(Ci.nsIFileInputStream); 1.29 + stream.init(file, PR_RDONLY, 0, 0); 1.30 + if (!buffered) 1.31 + return stream; 1.32 + 1.33 + var buffer = 1.34 + Cc["@mozilla.org/network/buffered-input-stream;1"]. 1.35 + createInstance(Ci.nsIBufferedInputStream); 1.36 + buffer.init(stream, 4096); 1.37 + return buffer; 1.38 +} 1.39 + 1.40 +function new_file_channel(file) { 1.41 + var ios = 1.42 + Cc["@mozilla.org/network/io-service;1"]. 1.43 + getService(Ci.nsIIOService); 1.44 + return ios.newChannelFromURI(ios.newFileURI(file)); 1.45 +} 1.46 + 1.47 +/* 1.48 + * stream listener 1.49 + * this listener has some additional file-specific tests, so we can't just use 1.50 + * ChannelListener here. 1.51 + */ 1.52 +function FileStreamListener(closure) { 1.53 + this._closure = closure; 1.54 +} 1.55 +FileStreamListener.prototype = { 1.56 + _closure: null, 1.57 + _buffer: "", 1.58 + _got_onstartrequest: false, 1.59 + _got_onstoprequest: false, 1.60 + _contentLen: -1, 1.61 + 1.62 + _isDir: function(request) { 1.63 + request.QueryInterface(Ci.nsIFileChannel); 1.64 + return request.file.isDirectory(); 1.65 + }, 1.66 + 1.67 + QueryInterface: function(iid) { 1.68 + if (iid.equals(Ci.nsIStreamListener) || 1.69 + iid.equals(Ci.nsIRequestObserver) || 1.70 + iid.equals(Ci.nsISupports)) 1.71 + return this; 1.72 + throw Cr.NS_ERROR_NO_INTERFACE; 1.73 + }, 1.74 + 1.75 + onStartRequest: function(request, context) { 1.76 + if (this._got_onstartrequest) 1.77 + do_throw("Got second onStartRequest event!"); 1.78 + this._got_onstartrequest = true; 1.79 + 1.80 + if (!this._isDir(request)) { 1.81 + request.QueryInterface(Ci.nsIChannel); 1.82 + this._contentLen = request.contentLength; 1.83 + if (this._contentLen == -1) 1.84 + do_throw("Content length is unknown in onStartRequest!"); 1.85 + } 1.86 + }, 1.87 + 1.88 + onDataAvailable: function(request, context, stream, offset, count) { 1.89 + if (!this._got_onstartrequest) 1.90 + do_throw("onDataAvailable without onStartRequest event!"); 1.91 + if (this._got_onstoprequest) 1.92 + do_throw("onDataAvailable after onStopRequest event!"); 1.93 + if (!request.isPending()) 1.94 + do_throw("request reports itself as not pending from onStartRequest!"); 1.95 + 1.96 + this._buffer = this._buffer.concat(read_stream(stream, count)); 1.97 + }, 1.98 + 1.99 + onStopRequest: function(request, context, status) { 1.100 + if (!this._got_onstartrequest) 1.101 + do_throw("onStopRequest without onStartRequest event!"); 1.102 + if (this._got_onstoprequest) 1.103 + do_throw("Got second onStopRequest event!"); 1.104 + this._got_onstoprequest = true; 1.105 + if (!Components.isSuccessCode(status)) 1.106 + do_throw("Failed to load file: " + status.toString(16)); 1.107 + if (status != request.status) 1.108 + do_throw("request.status does not match status arg to onStopRequest!"); 1.109 + if (request.isPending()) 1.110 + do_throw("request reports itself as pending from onStopRequest!"); 1.111 + if (this._contentLen != -1 && this._buffer.length != this._contentLen) 1.112 + do_throw("did not read nsIChannel.contentLength number of bytes!"); 1.113 + 1.114 + this._closure(this._buffer); 1.115 + } 1.116 +}; 1.117 + 1.118 +function test_read_file() { 1.119 + dump("*** test_read_file\n"); 1.120 + 1.121 + var file = do_get_file("../unit/data/test_readline6.txt"); 1.122 + var chan = new_file_channel(file); 1.123 + 1.124 + function on_read_complete(data) { 1.125 + dump("*** test_read_file.on_read_complete\n"); 1.126 + 1.127 + // bug 326693 1.128 + if (chan.contentType != special_type) 1.129 + do_throw("Type mismatch! Is <" + chan.contentType + ">, should be <" + 1.130 + special_type + ">") 1.131 + 1.132 + /* read completed successfully. now read data directly from file, 1.133 + and compare the result. */ 1.134 + var stream = new_file_input_stream(file, false); 1.135 + var result = read_stream(stream, stream.available()); 1.136 + if (result != data) 1.137 + do_throw("Stream contents do not match with direct read!"); 1.138 + run_next_test(); 1.139 + } 1.140 + 1.141 + chan.contentType = special_type; 1.142 + chan.asyncOpen(new FileStreamListener(on_read_complete), null); 1.143 +} 1.144 + 1.145 +function do_test_read_dir(set_type, expected_type) { 1.146 + dump("*** test_read_dir(" + set_type + ", " + expected_type + ")\n"); 1.147 + 1.148 + var file = do_get_tempdir(); 1.149 + var chan = new_file_channel(file); 1.150 + 1.151 + function on_read_complete(data) { 1.152 + dump("*** test_read_dir.on_read_complete(" + set_type + ", " + expected_type + ")\n"); 1.153 + 1.154 + // bug 326693 1.155 + if (chan.contentType != expected_type) 1.156 + do_throw("Type mismatch! Is <" + chan.contentType + ">, should be <" + 1.157 + expected_type + ">") 1.158 + 1.159 + run_next_test(); 1.160 + } 1.161 + 1.162 + if (set_type) 1.163 + chan.contentType = expected_type; 1.164 + chan.asyncOpen(new FileStreamListener(on_read_complete), null); 1.165 +} 1.166 + 1.167 +function test_read_dir_1() { 1.168 + return do_test_read_dir(false, "application/http-index-format"); 1.169 +} 1.170 + 1.171 +function test_read_dir_2() { 1.172 + return do_test_read_dir(true, special_type); 1.173 +} 1.174 + 1.175 +function test_upload_file() { 1.176 + dump("*** test_upload_file\n"); 1.177 + 1.178 + var file = do_get_file("../unit/data/test_readline6.txt"); // file to upload 1.179 + var dest = do_get_tempdir(); // file upload destination 1.180 + dest.append("junk.dat"); 1.181 + dest.createUnique(dest.NORMAL_FILE_TYPE, 0600); 1.182 + 1.183 + var uploadstream = new_file_input_stream(file, true); 1.184 + 1.185 + var chan = new_file_channel(dest); 1.186 + chan.QueryInterface(Ci.nsIUploadChannel); 1.187 + chan.setUploadStream(uploadstream, "", file.fileSize); 1.188 + 1.189 + function on_upload_complete(data) { 1.190 + dump("*** test_upload_file.on_upload_complete\n"); 1.191 + 1.192 + // bug 326693 1.193 + if (chan.contentType != special_type) 1.194 + do_throw("Type mismatch! Is <" + chan.contentType + ">, should be <" + 1.195 + special_type + ">") 1.196 + 1.197 + /* upload of file completed successfully. */ 1.198 + if (data.length != 0) 1.199 + do_throw("Upload resulted in data!"); 1.200 + 1.201 + var oldstream = new_file_input_stream(file, false); 1.202 + var newstream = new_file_input_stream(dest, false); 1.203 + var olddata = read_stream(oldstream, oldstream.available()); 1.204 + var newdata = read_stream(newstream, newstream.available()); 1.205 + if (olddata != newdata) 1.206 + do_throw("Stream contents do not match after file copy!"); 1.207 + oldstream.close(); 1.208 + newstream.close(); 1.209 + 1.210 + /* cleanup... also ensures that the destination file is not in 1.211 + use when OnStopRequest is called. */ 1.212 + try { 1.213 + dest.remove(false); 1.214 + } catch (e) { 1.215 + dump(e + "\n"); 1.216 + do_throw("Unable to remove uploaded file!\n"); 1.217 + } 1.218 + 1.219 + run_next_test(); 1.220 + } 1.221 + 1.222 + chan.contentType = special_type; 1.223 + chan.asyncOpen(new FileStreamListener(on_upload_complete), null); 1.224 +} 1.225 + 1.226 +function test_load_replace() { 1.227 + // lnk files should resolve to their targets 1.228 + const isWindows = ("@mozilla.org/windows-registry-key;1" in Cc); 1.229 + if (isWindows) { 1.230 + dump("*** test_load_replace\n"); 1.231 + file = do_get_file("data/system_root.lnk", false); 1.232 + var chan = new_file_channel(file); 1.233 + 1.234 + // The LOAD_REPLACE flag should be set 1.235 + do_check_eq(chan.loadFlags & chan.LOAD_REPLACE, chan.LOAD_REPLACE); 1.236 + 1.237 + // The original URI path should differ from the URI path 1.238 + do_check_neq(chan.URI.path, chan.originalURI.path); 1.239 + 1.240 + // The original URI path should be the same as the lnk file path 1.241 + var ios = Cc["@mozilla.org/network/io-service;1"]. 1.242 + getService(Ci.nsIIOService); 1.243 + do_check_eq(chan.originalURI.path, ios.newFileURI(file).path); 1.244 + } 1.245 + run_next_test(); 1.246 +} 1.247 + 1.248 +function run_test() { 1.249 + run_next_test(); 1.250 +}