Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_gio_protocol.js
blob2d3250c20353810046b9ac6455f36bcf49fbfafb
1 /* run some tests on the gvfs/gio protocol handler */
3 "use strict";
5 function inChildProcess() {
6 return Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
9 const PR_RDONLY = 0x1; // see prio.h
12 do_test_read_data_dir,
13 do_test_read_recent,
14 test_read_file,
15 do_test_finished,
16 ].forEach(f => add_test(f));
18 function setup() {
19 // Allowing some protocols to get a channel
20 if (!inChildProcess()) {
21 Services.prefs.setCharPref(
22 "network.gio.supported-protocols",
23 "localtest:,recent:"
25 } else {
26 do_send_remote_message("gio-allow-test-protocols");
27 do_await_remote_message("gio-allow-test-protocols-done");
31 setup();
33 registerCleanupFunction(() => {
34 // Resetting the protocols to None
35 if (!inChildProcess()) {
36 Services.prefs.clearUserPref("network.gio.supported-protocols");
38 });
40 function new_file_input_stream(file, buffered) {
41 var stream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
42 Ci.nsIFileInputStream
44 stream.init(file, PR_RDONLY, 0, 0);
45 if (!buffered) {
46 return stream;
49 var buffer = Cc[
50 "@mozilla.org/network/buffered-input-stream;1"
51 ].createInstance(Ci.nsIBufferedInputStream);
52 buffer.init(stream, 4096);
53 return buffer;
56 function new_file_channel(file) {
57 var chan = NetUtil.newChannel({
58 uri: file,
59 loadUsingSystemPrincipal: true,
60 });
62 return chan;
66 * stream listener
67 * this listener has some additional file-specific tests, so we can't just use
68 * ChannelListener here.
70 function FileStreamListener(closure) {
71 this._closure = closure;
73 FileStreamListener.prototype = {
74 _closure: null,
75 _buffer: "",
76 _got_onstartrequest: false,
77 _got_onstoprequest: false,
78 _contentLen: -1,
80 QueryInterface: ChromeUtils.generateQI([
81 "nsIStreamListener",
82 "nsIRequestObserver",
83 ]),
85 onStartRequest() {
86 if (this._got_onstartrequest) {
87 do_throw("Got second onStartRequest event!");
89 this._got_onstartrequest = true;
92 onDataAvailable(request, stream, offset, count) {
93 if (!this._got_onstartrequest) {
94 do_throw("onDataAvailable without onStartRequest event!");
96 if (this._got_onstoprequest) {
97 do_throw("onDataAvailable after onStopRequest event!");
99 if (!request.isPending()) {
100 do_throw("request reports itself as not pending from onStartRequest!");
103 this._buffer = this._buffer.concat(read_stream(stream, count));
106 onStopRequest(request, status) {
107 if (!this._got_onstartrequest) {
108 do_throw("onStopRequest without onStartRequest event!");
110 if (this._got_onstoprequest) {
111 do_throw("Got second onStopRequest event!");
113 this._got_onstoprequest = true;
114 if (!Components.isSuccessCode(status)) {
115 do_throw("Failed to load file: " + status.toString(16));
117 if (status != request.status) {
118 do_throw("request.status does not match status arg to onStopRequest!");
120 if (request.isPending()) {
121 do_throw("request reports itself as pending from onStopRequest!");
123 if (this._contentLen != -1 && this._buffer.length != this._contentLen) {
124 do_throw("did not read nsIChannel.contentLength number of bytes!");
127 this._closure(this._buffer);
131 function test_read_file() {
132 dump("*** test_read_file\n");
133 // Going via parent path, because this is opended from test/unit/ and test/unit_ipc/
134 var file = do_get_file("../unit/data/test_readline4.txt");
135 var chan = new_file_channel("localtest://" + file.path);
137 function on_read_complete(data) {
138 dump("*** test_read_file.on_read_complete()\n");
139 /* read completed successfully. now read data directly from file,
140 and compare the result. */
141 var stream = new_file_input_stream(file, false);
142 var result = read_stream(stream, stream.available());
143 if (result != data) {
144 do_throw("Stream contents do not match with direct read!");
146 run_next_test();
149 chan.asyncOpen(new FileStreamListener(on_read_complete));
152 function do_test_read_data_dir() {
153 dump('*** test_read_data_dir("../data/")\n');
155 var dir = do_get_file("../unit/data/");
156 var chan = new_file_channel("localtest://" + dir.path);
158 function on_read_complete(data) {
159 dump("*** test_read_data_dir.on_read_complete()\n");
161 // The data-directory should be listed, containing a header-line and the files therein
162 if (
164 data.includes("200: filename content-length last-modified file-type") &&
165 data.includes("201: test_readline1.txt") &&
166 data.includes("201: test_readline2.txt")
169 do_throw(
170 "test_read_data_dir() - Bad data! Does not contain needles! Is <" +
171 data +
175 run_next_test();
177 chan.asyncOpen(new FileStreamListener(on_read_complete));
180 function do_test_read_recent() {
181 dump('*** test_read_recent("recent://")\n');
183 var chan = new_file_channel("recent:///");
185 function on_read_complete(data) {
186 dump("*** test_read_recent.on_read_complete()\n");
188 // The data-directory should be listed, containing a header-line and the files therein
189 if (
190 !data.includes("200: filename content-length last-modified file-type")
192 do_throw(
193 "do_test_read_recent() - Bad data! Does not contain header! Is <" +
194 data +
198 run_next_test();
200 chan.asyncOpen(new FileStreamListener(on_read_complete));