Bug 581175: Make NetUtil's asyncFetch suppress SSL error messages by default, and pass relevant nsIRequest object to its callback, r+a=sdwilsh

--HG--
rename : testing/mochitest/tests/browser/Makefile.in => netwerk/test/browser/Makefile.in
This commit is contained in:
Gavin Sharp 2010-07-23 17:59:07 -04:00
parent 73c20d6589
commit e3be2e9894
4 changed files with 172 additions and 2 deletions

View File

@ -52,6 +52,7 @@ let EXPORTED_SYMBOLS = [
const Ci = Components.interfaces;
const Cc = Components.classes;
const Cr = Components.results;
const Cu = Components.utils;
const PR_UINT32_MAX = 0xffffffff;
@ -130,15 +131,20 @@ const NetUtil = {
/**
* Asynchronously opens a source and fetches the response. A source can be
* an nsIURI, nsIFile, string spec, or nsIChannel. The provided callback
* will get an input stream containing the response, and the result code.
* will get an input stream containing the response, the result code, and a
* reference to the request.
*
* @param aSource
* The nsIURI, nsIFile, string spec, or nsIChannel to open.
* Note: If passing an nsIChannel whose notificationCallbacks is
* already set, callers are responsible for implementations
* of nsIBadCertListener/nsISSLErrorListener.
* @param aCallback
* The callback function that will be notified upon completion. It
* will get two arguments:
* 1) An nsIInputStream containing the data from the channel, if any.
* 2) The status code from opening the source.
* 3) Reference to the channel (as an nsIRequest).
*/
asyncFetch: function NetUtil_asyncOpen(aSource, aCallback)
{
@ -164,7 +170,7 @@ const NetUtil = {
onStartRequest: function(aRequest, aContext) {},
onStopRequest: function(aRequest, aContext, aStatusCode) {
pipe.outputStream.close();
aCallback(pipe.inputStream, aStatusCode);
aCallback(pipe.inputStream, aStatusCode, aRequest);
}
});
@ -173,6 +179,13 @@ const NetUtil = {
channel = this.newChannel(aSource);
}
// Add a BadCertHandler to suppress SSL/cert error dialogs, but only if
// the channel doesn't already have a notificationCallbacks.
if (!channel.notificationCallbacks) {
// Pass true to avoid optional redirect-cert-checking behavior.
channel.notificationCallbacks = new BadCertHandler(true);
}
channel.asyncOpen(listener, null);
},
@ -265,3 +278,9 @@ Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
// Define our lazy getters.
XPCOMUtils.defineLazyServiceGetter(this, "ioUtil", "@mozilla.org/io-util;1",
"nsIIOUtil");
XPCOMUtils.defineLazyGetter(this, "BadCertHandler", "@mozilla.org/io-util;1", function () {
var obj = {};
Cu.import("resource://gre/modules/CertUtils.jsm", obj);
return obj.BadCertHandler;
});

View File

@ -47,6 +47,7 @@ MODULE = test_necko
DIRS = \
httpserver \
browser \
$(NULL)

View File

@ -0,0 +1,52 @@
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is NetUtil test code.
#
# The Initial Developer of the Original Code is
# the Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2010
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Gavin Sharp <gavin@gavinsharp.com> (Original author)
#
# Alternatively, the contents of this file may be used under the terms of
# either of the GNU General Public License Version 2 or later (the "GPL"),
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
DEPTH = ../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
relativesrcdir = netwerk/test/browser
include $(DEPTH)/config/autoconf.mk
include $(topsrcdir)/config/rules.mk
_BROWSER_TEST_FILES = \
browser_NetUtil.js \
$(NULL)
libs:: $(_BROWSER_TEST_FILES)
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/browser/$(relativesrcdir)

View File

@ -0,0 +1,98 @@
/*
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
*/
Cu.import("resource://gre/modules/NetUtil.jsm");
function test() {
waitForExplicitFinish();
nextTest();
}
function nextTest() {
if (tests.length)
executeSoon(tests.shift());
else
executeSoon(finish);
}
var tests = [
test_asyncFetchBadCert,
];
var gCertErrorDialogShown = 0;
function test_asyncFetchBadCert() {
let listener = new WindowListener("chrome://pippki/content/certerror.xul", function (domwindow) {
gCertErrorDialogShown++;
// Close the dialog
domwindow.document.documentElement.cancelDialog();
});
Services.wm.addListener(listener);
// Try a load from an untrusted cert, with errors supressed
NetUtil.asyncFetch("https://untrusted.example.com", function (aInputStream, aStatusCode, aRequest) {
ok(!Components.isSuccessCode(aStatusCode), "request failed");
ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
is(gCertErrorDialogShown, 0, "cert error was suppressed");
// Now try again with a channel whose notificationCallbacks doesn't suprress errors
let channel = NetUtil.newChannel("https://untrusted.example.com");
channel.notificationCallbacks = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIProgressEventSink,
Ci.nsIInterfaceRequestor]),
getInterface: function (aIID) this.QueryInterface(aIID),
onProgress: function () {},
onStatus: function () {}
};
NetUtil.asyncFetch(channel, function (aInputStream, aStatusCode, aRequest) {
ok(!Components.isSuccessCode(aStatusCode), "request failed");
ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
is(gCertErrorDialogShown, 1, "cert error was not suppressed");
// Now try a valid request
NetUtil.asyncFetch("https://example.com", function (aInputStream, aStatusCode, aRequest) {
ok(Components.isSuccessCode(aStatusCode), "request succeeded");
ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
ok(aRequest.requestSucceeded, "HTTP request succeeded");
is(gCertErrorDialogShown, 1, "cert error was not shown");
Services.wm.removeListener(listener);
nextTest();
});
});
});
}
function WindowListener(aURL, aCallback) {
this.callback = aCallback;
this.url = aURL;
}
WindowListener.prototype = {
onOpenWindow: function(aXULWindow) {
var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowInternal);
var self = this;
domwindow.addEventListener("load", function() {
domwindow.removeEventListener("load", arguments.callee, false);
if (domwindow.document.location.href != self.url)
return;
// Allow other window load listeners to execute before passing to callback
executeSoon(function() {
self.callback(domwindow);
});
}, false);
},
onCloseWindow: function(aXULWindow) {},
onWindowTitleChange: function(aXULWindow, aNewTitle) {}
}