Bug 1667316 - Unit test for veto-ing redirects r=necko-reviewers,kershaw

Differential Revision: https://phabricator.services.mozilla.com/D140567
This commit is contained in:
Valentin Gosu 2022-11-16 08:49:44 +00:00
parent 3e7554fe4d
commit 0c7d02f3a6
7 changed files with 231 additions and 4 deletions

View File

@ -126,10 +126,7 @@ window.ChannelEventSink = {
},
// nsIFactory implementation
createInstance(outer, iid) {
if (outer) {
throw Components.Exception("", Cr.NS_ERROR_NO_AGGREGATION);
}
createInstance(iid) {
return this.QueryInterface(iid);
},
};

View File

@ -0,0 +1,100 @@
"use strict";
const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
XPCOMUtils.defineLazyGetter(this, "URL", function() {
return "http://localhost:" + httpServer.identity.primaryPort;
});
var httpServer = null;
// Need to randomize, because apparently no one clears our cache
var randomPath = "/redirect/" + Math.random();
XPCOMUtils.defineLazyGetter(this, "randomURI", function() {
return URL + randomPath;
});
function make_channel(url, callback, ctx) {
return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
}
const responseBody = "response body";
function redirectHandler(metadata, response) {
response.setStatusLine(metadata.httpVersion, 301, "Moved");
response.setHeader("Location", URL + "/content", false);
}
function contentHandler(metadata, response) {
response.setHeader("Content-Type", "text/plain");
response.bodyOutputStream.write(responseBody, responseBody.length);
}
let ChannelEventSink2 = {
_classDescription: "WebRequest channel event sink",
_classID: Components.ID("115062f8-92f1-11e5-8b7f-08001110f7ec"),
_contractID: "@mozilla.org/webrequest/channel-event-sink;1",
QueryInterface: ChromeUtils.generateQI(["nsIChannelEventSink", "nsIFactory"]),
init() {
Components.manager
.QueryInterface(Ci.nsIComponentRegistrar)
.registerFactory(
this._classID,
this._classDescription,
this._contractID,
this
);
},
register() {
Services.catMan.addCategoryEntry(
"net-channel-event-sinks",
this._contractID,
this._contractID,
false,
true
);
},
unregister() {
Services.catMan.deleteCategoryEntry(
"net-channel-event-sinks",
this._contractID,
false
);
},
// nsIChannelEventSink implementation
asyncOnChannelRedirect(oldChannel, newChannel, flags, redirectCallback) {
// Abort the redirection
redirectCallback.onRedirectVerifyCallback(Cr.NS_ERROR_NO_INTERFACE);
},
// nsIFactory implementation
createInstance(iid) {
return this.QueryInterface(iid);
},
};
add_task(async function test_redirect_veto() {
httpServer = new HttpServer();
httpServer.registerPathHandler(randomPath, redirectHandler);
httpServer.registerPathHandler("/content", contentHandler);
httpServer.start(-1);
ChannelEventSink2.init();
ChannelEventSink2.register();
let chan = make_channel(randomURI);
let [req, buff] = await new Promise(resolve =>
chan.asyncOpen(
new ChannelListener((aReq, aBuff) => resolve([aReq, aBuff], null))
)
);
Assert.equal(buff, "");
Assert.equal(req.status, Cr.NS_OK);
await httpServer.stop();
ChannelEventSink2.unregister();
});

View File

@ -289,6 +289,7 @@ skip-if =
[test_psl.js]
[test_range_requests.js]
[test_readline.js]
[test_redirect_veto.js]
[test_redirect-caching_canceled.js]
[test_redirect-caching_failure.js]
[test_redirect-caching_passing.js]

View File

@ -0,0 +1,51 @@
/* import-globals-from ../unit/head_trr.js */
"use strict";
const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
XPCOMUtils.defineLazyGetter(this, "URL", function() {
return "http://localhost:" + httpServer.identity.primaryPort;
});
var httpServer = null;
// Need to randomize, because apparently no one clears our cache
var randomPath = "/redirect/" + Math.random();
XPCOMUtils.defineLazyGetter(this, "randomURI", function() {
return URL + randomPath;
});
function make_channel(url, callback, ctx) {
return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
}
const responseBody = "response body";
function redirectHandler(metadata, response) {
response.setStatusLine(metadata.httpVersion, 301, "Moved");
response.setHeader("Location", URL + "/content", false);
}
function contentHandler(metadata, response) {
response.setHeader("Content-Type", "text/plain");
response.bodyOutputStream.write(responseBody, responseBody.length);
}
add_task(async function doStuff() {
httpServer = new HttpServer();
httpServer.registerPathHandler(randomPath, redirectHandler);
httpServer.registerPathHandler("/content", contentHandler);
httpServer.start(-1);
let chan = make_channel(randomURI);
let [req, buff] = await new Promise(resolve =>
chan.asyncOpen(
new ChannelListener((aReq, aBuff) => resolve([aReq, aBuff]), null)
)
);
Assert.equal(buff, "");
Assert.equal(req.status, Cr.NS_OK);
await httpServer.stop();
await do_send_remote_message("child-test-done");
});

View File

@ -0,0 +1,64 @@
//
// Run test script in content process instead of chrome (xpcshell's default)
//
//
let ChannelEventSink2 = {
_classDescription: "WebRequest channel event sink",
_classID: Components.ID("115062f8-92f1-11e5-8b7f-08001110f7ec"),
_contractID: "@mozilla.org/webrequest/channel-event-sink;1",
QueryInterface: ChromeUtils.generateQI(["nsIChannelEventSink", "nsIFactory"]),
init() {
Components.manager
.QueryInterface(Ci.nsIComponentRegistrar)
.registerFactory(
this._classID,
this._classDescription,
this._contractID,
this
);
},
register() {
Services.catMan.addCategoryEntry(
"net-channel-event-sinks",
this._contractID,
this._contractID,
false,
true
);
},
unregister() {
Components.manager
.QueryInterface(Ci.nsIComponentRegistrar)
.unregisterFactory(this._classID, ChannelEventSink2);
Services.catMan.deleteCategoryEntry(
"net-channel-event-sinks",
this._contractID,
false
);
},
// nsIChannelEventSink implementation
asyncOnChannelRedirect(oldChannel, newChannel, flags, redirectCallback) {
// Abort the redirection
redirectCallback.onRedirectVerifyCallback(Cr.NS_ERROR_NO_INTERFACE);
},
// nsIFactory implementation
createInstance(iid) {
return this.QueryInterface(iid);
},
};
add_task(async function run_test() {
ChannelEventSink2.init();
ChannelEventSink2.register();
run_test_in_child("child_veto_in_parent.js");
await do_await_remote_message("child-test-done");
ChannelEventSink2.unregister();
});

View File

@ -0,0 +1,7 @@
//
// Run test script in content process instead of chrome (xpcshell's default)
//
//
function run_test() {
run_test_in_child("../unit/test_redirect_veto.js");
}

View File

@ -22,6 +22,7 @@ support-files =
!/netwerk/test/unit/test_post.js
!/netwerk/test/unit/test_predictor.js
!/netwerk/test/unit/test_progress.js
!/netwerk/test/unit/test_redirect_veto.js
!/netwerk/test/unit/test_redirect-caching_canceled.js
!/netwerk/test/unit/test_redirect-caching_failure.js
!/netwerk/test/unit/test_redirect-caching_passing.js
@ -68,6 +69,7 @@ support-files =
!/netwerk/test/unit/test_orb_empty_header.js
child_cookie_header.js
child_dns_by_type_resolve.js
child_veto_in_parent.js
[test_cookie_header_stripped.js]
@ -117,6 +119,11 @@ prefs = network.allow_raw_sockets_in_content_processes=true
# Do not test the channel.redirectTo() API under e10s until 827269 is resolved
[test_redirect_from_script_wrap.js]
skip-if = true
[test_redirect_veto_wrap.js]
prefs = network.allow_raw_sockets_in_content_processes=true
[test_redirect_veto_parent.js]
prefs = network.allow_raw_sockets_in_content_processes=true
run-sequentially = doesn't play nice with others.
[test_redirect_passing_wrap.js]
prefs = network.allow_raw_sockets_in_content_processes=true
[test_redirect_different-protocol_wrap.js]