Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / test_gio_protocol.js
blob37ce37abab84ee202c098ff4410f15e112833211
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:"
24     );
25   } else {
26     do_send_remote_message("gio-allow-test-protocols");
27     do_await_remote_message("gio-allow-test-protocols-done");
28   }
31 setup();
33 registerCleanupFunction(() => {
34   // Resetting the protocols to None
35   if (!inChildProcess()) {
36     Services.prefs.clearUserPref("network.gio.supported-protocols");
37   }
38 });
40 function new_file_input_stream(file, buffered) {
41   var stream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
42     Ci.nsIFileInputStream
43   );
44   stream.init(file, PR_RDONLY, 0, 0);
45   if (!buffered) {
46     return stream;
47   }
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.
69  */
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(request) {
86     if (this._got_onstartrequest) {
87       do_throw("Got second onStartRequest event!");
88     }
89     this._got_onstartrequest = true;
90   },
92   onDataAvailable(request, stream, offset, count) {
93     if (!this._got_onstartrequest) {
94       do_throw("onDataAvailable without onStartRequest event!");
95     }
96     if (this._got_onstoprequest) {
97       do_throw("onDataAvailable after onStopRequest event!");
98     }
99     if (!request.isPending()) {
100       do_throw("request reports itself as not pending from onStartRequest!");
101     }
103     this._buffer = this._buffer.concat(read_stream(stream, count));
104   },
106   onStopRequest(request, status) {
107     if (!this._got_onstartrequest) {
108       do_throw("onStopRequest without onStartRequest event!");
109     }
110     if (this._got_onstoprequest) {
111       do_throw("Got second onStopRequest event!");
112     }
113     this._got_onstoprequest = true;
114     if (!Components.isSuccessCode(status)) {
115       do_throw("Failed to load file: " + status.toString(16));
116     }
117     if (status != request.status) {
118       do_throw("request.status does not match status arg to onStopRequest!");
119     }
120     if (request.isPending()) {
121       do_throw("request reports itself as pending from onStopRequest!");
122     }
123     if (this._contentLen != -1 && this._buffer.length != this._contentLen) {
124       do_throw("did not read nsIChannel.contentLength number of bytes!");
125     }
127     this._closure(this._buffer);
128   },
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!");
145     }
146     run_next_test();
147   }
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 (
163       !(
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")
167       )
168     ) {
169       do_throw(
170         "test_read_data_dir() - Bad data! Does not contain needles! Is <" +
171           data +
172           ">"
173       );
174     }
175     run_next_test();
176   }
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")
191     ) {
192       do_throw(
193         "do_test_read_recent() - Bad data! Does not contain header! Is <" +
194           data +
195           ">"
196       );
197     }
198     run_next_test();
199   }
200   chan.asyncOpen(new FileStreamListener(on_read_complete));