michael@0: // This testcase verifies that channels can't be reopened michael@0: // See https://bugzilla.mozilla.org/show_bug.cgi?id=372486 michael@0: michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: const NS_ERROR_IN_PROGRESS = 0x804b000f; michael@0: const NS_ERROR_ALREADY_OPENED = 0x804b0049; michael@0: michael@0: var chan = null; michael@0: var httpserv = null; michael@0: michael@0: [ michael@0: test_data_channel, michael@0: test_http_channel, michael@0: test_file_channel, michael@0: // Commented by default as it relies on external ressources michael@0: //test_ftp_channel, michael@0: end michael@0: ].forEach(add_test); michael@0: michael@0: // Utility functions michael@0: michael@0: function makeChan(url) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"] michael@0: .getService(Ci.nsIIOService); michael@0: return chan = ios.newChannel(url, null, null) michael@0: .QueryInterface(Ci.nsIChannel); michael@0: } michael@0: michael@0: function new_file_channel(file) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"] michael@0: .getService(Ci.nsIIOService); michael@0: return ios.newChannelFromURI(ios.newFileURI(file)); michael@0: } michael@0: michael@0: michael@0: function check_throws(closure, error) { michael@0: var thrown = false; michael@0: try { michael@0: closure(); michael@0: } catch (e) { michael@0: if (error instanceof Array) { michael@0: do_check_neq(error.indexOf(e.result), -1); michael@0: } else { michael@0: do_check_eq(e.result, error); michael@0: } michael@0: thrown = true; michael@0: } michael@0: do_check_true(thrown); michael@0: } michael@0: michael@0: function check_open_throws(error) { michael@0: check_throws(function() { michael@0: chan.open(listener, null); michael@0: }, error); michael@0: } michael@0: michael@0: function check_async_open_throws(error) { michael@0: check_throws(function() { michael@0: chan.asyncOpen(listener, null); michael@0: }, error); michael@0: } michael@0: michael@0: var listener = { michael@0: onStartRequest: function test_onStartR(request, ctx) { michael@0: check_async_open_throws(NS_ERROR_IN_PROGRESS); michael@0: }, michael@0: michael@0: onDataAvailable: function test_ODA(request, cx, inputStream, michael@0: offset, count) { michael@0: new BinaryInputStream(inputStream).readByteArray(count); // required by API michael@0: check_async_open_throws(NS_ERROR_IN_PROGRESS); michael@0: }, michael@0: michael@0: onStopRequest: function test_onStopR(request, ctx, status) { michael@0: // Once onStopRequest is reached, the channel is marked as having been michael@0: // opened michael@0: check_async_open_throws(NS_ERROR_ALREADY_OPENED); michael@0: do_timeout(0, after_channel_closed); michael@0: } michael@0: }; michael@0: michael@0: function after_channel_closed() { michael@0: check_async_open_throws(NS_ERROR_ALREADY_OPENED); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_channel(createChanClosure) { michael@0: // First, synchronous reopening test michael@0: chan = createChanClosure(); michael@0: var inputStream = chan.open(); michael@0: check_open_throws(NS_ERROR_IN_PROGRESS); michael@0: check_async_open_throws([NS_ERROR_IN_PROGRESS, NS_ERROR_ALREADY_OPENED]); michael@0: michael@0: // Then, asynchronous one michael@0: chan = createChanClosure(); michael@0: chan.asyncOpen(listener, null); michael@0: check_open_throws(NS_ERROR_IN_PROGRESS); michael@0: check_async_open_throws(NS_ERROR_IN_PROGRESS); michael@0: } michael@0: michael@0: function test_data_channel() { michael@0: test_channel(function() { michael@0: return makeChan("data:text/plain,foo"); michael@0: }); michael@0: } michael@0: michael@0: function test_http_channel() { michael@0: test_channel(function() { michael@0: return makeChan("http://localhost:" + httpserv.identity.primaryPort + "/"); michael@0: }); michael@0: } michael@0: michael@0: function test_file_channel() { michael@0: var file = do_get_file("data/test_readline1.txt"); michael@0: test_channel(function() { michael@0: return new_file_channel(file); michael@0: }); michael@0: } michael@0: michael@0: // Uncomment test_ftp_channel in test_array to test this michael@0: function test_ftp_channel() { michael@0: test_channel(function() { michael@0: return makeChan("ftp://ftp.mozilla.org/pub/mozilla.org/README"); michael@0: }); michael@0: } michael@0: michael@0: function end() { michael@0: httpserv.stop(do_test_finished); michael@0: } michael@0: michael@0: function run_test() { michael@0: // start server michael@0: httpserv = new HttpServer(); michael@0: httpserv.start(-1); michael@0: michael@0: run_next_test(); michael@0: }