1 // This testcase verifies that channels can't be reopened
2 // See https://bugzilla.mozilla.org/show_bug.cgi?id=372486
6 const { HttpServer
} = ChromeUtils
.importESModule(
7 "resource://testing-common/httpd.sys.mjs"
10 const BinaryInputStream
= Components
.Constructor(
11 "@mozilla.org/binaryinputstream;1",
12 "nsIBinaryInputStream",
16 const NS_ERROR_IN_PROGRESS
= 0x804b000f;
17 const NS_ERROR_ALREADY_OPENED
= 0x804b0049;
22 [test_data_channel
, test_http_channel
, test_file_channel
, end
].forEach(f
=>
28 function makeChan(url
) {
29 return (chan
= NetUtil
.newChannel({
31 loadUsingSystemPrincipal
: true,
32 }).QueryInterface(Ci
.nsIChannel
));
35 function new_file_channel(file
) {
36 return NetUtil
.newChannel({
37 uri
: Services
.io
.newFileURI(file
),
38 loadUsingSystemPrincipal
: true,
42 function check_throws(closure
, error
) {
47 if (error
instanceof Array
) {
48 Assert
.notEqual(error
.indexOf(e
.result
), -1);
50 Assert
.equal(e
.result
, error
);
57 function check_open_throws(error
) {
58 check_throws(function () {
63 function check_async_open_throws(error
) {
64 check_throws(function () {
65 chan
.asyncOpen(listener
);
70 onStartRequest
: function test_onStartR() {
71 check_async_open_throws(NS_ERROR_IN_PROGRESS
);
74 onDataAvailable
: function test_ODA(request
, inputStream
, offset
, count
) {
75 new BinaryInputStream(inputStream
).readByteArray(count
); // required by API
76 check_async_open_throws(NS_ERROR_IN_PROGRESS
);
79 onStopRequest
: function test_onStopR() {
80 // Once onStopRequest is reached, the channel is marked as having been
82 check_async_open_throws(NS_ERROR_ALREADY_OPENED
);
83 do_timeout(0, after_channel_closed
);
87 function after_channel_closed() {
88 check_async_open_throws(NS_ERROR_ALREADY_OPENED
);
93 function test_channel(createChanClosure
) {
94 // First, synchronous reopening test
95 chan
= createChanClosure();
97 check_open_throws(NS_ERROR_IN_PROGRESS
);
98 check_async_open_throws([NS_ERROR_IN_PROGRESS
, NS_ERROR_ALREADY_OPENED
]);
100 // Then, asynchronous one
101 chan
= createChanClosure();
102 chan
.asyncOpen(listener
);
103 check_open_throws(NS_ERROR_IN_PROGRESS
);
104 check_async_open_throws(NS_ERROR_IN_PROGRESS
);
107 function test_data_channel() {
108 test_channel(function () {
109 return makeChan("data:text/plain,foo");
113 function test_http_channel() {
114 test_channel(function () {
115 return makeChan("http://localhost:" + httpserv
.identity
.primaryPort
+ "/");
119 function test_file_channel() {
120 var file
= do_get_file("data/test_readline1.txt");
121 test_channel(function () {
122 return new_file_channel(file
);
127 httpserv
.stop(do_test_finished
);
130 function run_test() {
132 httpserv
= new HttpServer();