gecko-dev/netwerk/test/unit/test_reopen.js
Ehsan Akhgari 868c13b5e5 Bug 1547397 - Part 2: Move some xpcshell tests for cookies out of extensions/cookies; r=baku
Differential Revision: https://phabricator.services.mozilla.com/D29115

--HG--
rename : extensions/cookie/test/unit/head_cookies.js => netwerk/test/unit/head_cookies.js
rename : extensions/cookie/test/unit/test_bug526789.js => netwerk/test/unit/test_bug526789.js
rename : extensions/cookie/test/unit/test_bug650522.js => netwerk/test/unit/test_bug650522.js
rename : extensions/cookie/test/unit/test_bug667087.js => netwerk/test/unit/test_bug667087.js
rename : extensions/cookie/test/unit/test_cookies_async_failure.js => netwerk/test/unit/test_cookies_async_failure.js
rename : extensions/cookie/test/unit/test_cookies_persistence.js => netwerk/test/unit/test_cookies_persistence.js
rename : extensions/cookie/test/unit/test_cookies_privatebrowsing.js => netwerk/test/unit/test_cookies_privatebrowsing.js
rename : extensions/cookie/test/unit/test_cookies_profile_close.js => netwerk/test/unit/test_cookies_profile_close.js
rename : extensions/cookie/test/unit/test_cookies_read.js => netwerk/test/unit/test_cookies_read.js
rename : extensions/cookie/test/unit/test_cookies_sync_failure.js => netwerk/test/unit/test_cookies_sync_failure.js
rename : extensions/cookie/test/unit/test_cookies_thirdparty.js => netwerk/test/unit/test_cookies_thirdparty.js
rename : extensions/cookie/test/unit/test_cookies_thirdparty_nonsecure_session.js => netwerk/test/unit/test_cookies_thirdparty_nonsecure_session.js
rename : extensions/cookie/test/unit/test_cookies_thirdparty_session.js => netwerk/test/unit/test_cookies_thirdparty_session.js
rename : extensions/cookie/test/unit/test_domain_eviction.js => netwerk/test/unit/test_domain_eviction.js
rename : extensions/cookie/test/unit/test_eviction.js => netwerk/test/unit/test_eviction.js
rename : extensions/cookie/test/unit/test_schema_2_migration.js => netwerk/test/unit/test_schema_2_migration.js
rename : extensions/cookie/test/unit/test_schema_3_migration.js => netwerk/test/unit/test_schema_3_migration.js
extra : moz-landing-system : lando
2019-04-30 14:06:27 +00:00

146 lines
3.5 KiB
JavaScript

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