Bug 613785 - Updated http auth prompt tests to support tab prompts. r=johannh

Differential Revision: https://phabricator.services.mozilla.com/D75567
This commit is contained in:
pbz 2020-05-28 11:11:52 +00:00
Родитель 3aa6f1346f
Коммит d8204e01b9
12 изменённых файлов: 143 добавлений и 118 удалений

Просмотреть файл

@ -1,5 +1,9 @@
let { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
let tabPromptEnabled =
Services.prefs.getIntPref("prompts.modalType.httpAuth") !==
Services.prompt.MODAL_TYPE_WINDOW;
let server = new HttpServer();
server.registerPathHandler("/file.html", fileHandler);
server.start(-1);
@ -27,17 +31,27 @@ function fileHandler(metadata, response) {
}
function onCommonDialogLoaded(subject) {
let dialog;
if (tabPromptEnabled) {
let promptBox =
subject.ownerGlobal.gBrowser.selectedBrowser.tabModalPromptBox;
dialog = promptBox.getPrompt(subject).Dialog;
} else {
dialog = subject.Dialog;
}
// Submit random account and password
let dialog = subject.Dialog;
dialog.ui.loginTextbox.setAttribute("value", Math.random());
dialog.ui.password1Textbox.setAttribute("value", Math.random());
dialog.ui.button0.click();
}
Services.obs.addObserver(onCommonDialogLoaded, "common-dialog-loaded");
let authPromptTopic = tabPromptEnabled
? "tabmodal-dialog-loaded"
: "common-dialog-loaded";
Services.obs.addObserver(onCommonDialogLoaded, authPromptTopic);
registerCleanupFunction(() => {
Services.obs.removeObserver(onCommonDialogLoaded, "common-dialog-loaded");
Services.obs.removeObserver(onCommonDialogLoaded, authPromptTopic);
server.stop(() => {
server = null;
});

Просмотреть файл

@ -5,34 +5,14 @@
// This tests that the basic auth dialog can not be used for DOS attacks
// and that the protections are reset on user-initiated navigation/reload.
const PROMPT_URL = "chrome://global/content/commonDialog.xhtml";
let promptModalType = Services.prefs.getIntPref("prompts.modalType.httpAuth");
function promiseAuthWindowShown() {
return new Promise(resolve => {
let listener = {
onOpenWindow(xulWin) {
let domwindow = xulWin.docShell.domWindow;
waitForFocus(() => {
is(
domwindow.document.location.href,
PROMPT_URL,
"Should have seen a prompt window"
);
is(
domwindow.args.promptType,
"promptUserAndPass",
"Should be an authenticate prompt"
);
domwindow.document.getElementById("commonDialog").cancelDialog();
Services.wm.removeListener(listener);
resolve();
}, domwindow);
},
onCloseWindow() {},
};
Services.wm.addListener(listener);
});
return PromptTestUtils.handleNextPrompt(
window,
{ modalType: promptModalType, promptType: "promptUserAndPass" },
{ buttonNumClick: 1 }
);
}
add_task(async function test() {

Просмотреть файл

@ -2,40 +2,18 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const PROMPT_URL = "chrome://global/content/commonDialog.xhtml";
let modalType = Services.prefs.getIntPref("prompts.modalType.httpAuth");
add_task(async function test() {
await new Promise(resolve => {
let tab = BrowserTestUtils.addTab(gBrowser);
isnot(tab, gBrowser.selectedTab, "New tab shouldn't be selected");
let listener = {
onOpenWindow(xulWin) {
var domwindow = xulWin.docShell.domWindow;
waitForFocus(() => {
is(
domwindow.document.location.href,
PROMPT_URL,
"Should have seen a prompt window"
);
is(
domwindow.args.promptType,
"promptUserAndPass",
"Should be an authenticate prompt"
);
is(gBrowser.selectedTab, tab, "Should have selected the new tab");
domwindow.document.getElementById("commonDialog").cancelDialog();
}, domwindow);
},
onCloseWindow() {},
};
Services.wm.addListener(listener);
let authPromptShown = PromptTestUtils.waitForPrompt(tab.linkedBrowser, {
modalType,
promptType: "promptUserAndPass",
});
registerCleanupFunction(() => {
Services.wm.removeListener(listener);
gBrowser.removeTab(tab);
});
@ -44,5 +22,10 @@ add_task(async function test() {
tab.linkedBrowser,
"http://example.com/browser/toolkit/components/passwordmgr/test/browser/authenticate.sjs"
);
authPromptShown.then(dialog => {
is(gBrowser.selectedTab, tab, "Should have selected the new tab");
PromptTestUtils.handlePrompt(dialog, { buttonNumClick: 1 }); // Cancel
});
});
});

Просмотреть файл

@ -74,14 +74,13 @@ async function loadAccessRestrictedURL(browser, url, username, password) {
let browserLoaded = BrowserTestUtils.browserLoaded(browser);
BrowserTestUtils.loadURI(browser, url);
let promptDoc = await waitForAuthPrompt();
let dialogUI = promptDoc.defaultView.Dialog.ui;
ok(dialogUI, "Got expected HTTP auth dialog Dialog.ui");
// Wait for the auth prompt, enter the login details and close the prompt
await PromptTestUtils.handleNextPrompt(
browser,
{ modalType: authPromptModalType, promptType: "promptUserAndPass" },
{ buttonNumClick: 0, loginInput: username, passwordInput: password }
);
// fill and submit the dialog form
dialogUI.loginTextbox.value = username;
dialogUI.password1Textbox.value = password;
promptDoc.getElementById("commonDialog").acceptDialog();
await SimpleTest.promiseFocus(browser.ownerGlobal);
await browserLoaded;
}
@ -107,11 +106,13 @@ const authUrl = `https://example.com/${DIRECTORY_PATH}authenticate.sjs`;
let normalWin;
let privateWin;
let authPromptModalType;
// XXX: Note that tasks are currently run in sequence. Some tests may assume the state
// resulting from successful or unsuccessful logins in previous tasks
add_task(async function test_setup() {
authPromptModalType = Services.prefs.getIntPref("prompts.modalType.httpAuth");
normalWin = await BrowserTestUtils.openNewBrowserWindow({ private: false });
privateWin = await BrowserTestUtils.openNewBrowserWindow({ private: true });
Services.logins.removeAllLogins();

Просмотреть файл

@ -7,6 +7,7 @@ const { LoginManagerParent } = ChromeUtils.import(
ChromeUtils.import("resource://testing-common/LoginTestUtils.jsm", this);
ChromeUtils.import("resource://testing-common/ContentTaskUtils.jsm", this);
ChromeUtils.import("resource://testing-common/TelemetryTestUtils.jsm", this);
ChromeUtils.import("resource://testing-common/PromptTestUtils.jsm", this);
add_task(async function common_initialize() {
await SpecialPowers.pushPrefEnv({

Просмотреть файл

@ -85,6 +85,10 @@
parentCu.import("resource://gre/modules/Timer.jsm");
parentCu.import("resource://gre/modules/XPCOMUtils.jsm");
let tabPromptEnabled =
Services.prefs.getIntPref("prompts.modalType.httpAuth")
!== Services.prompt.MODAL_TYPE_WINDOW;
let channel = NetUtil.newChannel({
uri: "http://example.com",
loadUsingSystemPrincipal: true,
@ -119,12 +123,19 @@
sendAsyncMessage("prepareForNextTestDone");
});
let dialogObserverTopic = "common-dialog-loaded";
let dialogObserverTopic = tabPromptEnabled
? "tabmodal-dialog-loaded" : "common-dialog-loaded";
function dialogObserver(subj, topic, data) {
subj.Dialog.ui.prompt.document
if(tabPromptEnabled) {
let prompt = subj.ownerGlobal.gBrowser.selectedBrowser
.tabModalPromptBox.getPrompt(subj);
prompt.Dialog.ui.button0.click(); // Accept button
} else {
subj.Dialog.ui.prompt.document
.getElementById("commonDialog")
.acceptDialog();
}
sendAsyncMessage("promptAccepted");
}

Просмотреть файл

@ -16,9 +16,11 @@
const EXAMPLE_ORG = "http://example.org/tests/toolkit/components/passwordmgr/test/mochitest/";
let mozproxyOrigin;
// Force parent to not look for tab-modal prompts, as they're not used for auth prompts.
// Used by prompt_common.js.
isTabModal = false;
// Let parent check for tab modal prompts if they are enabled for auth
// prompts. Used by prompt_common.js.
isTabModal = SpecialPowers.Services.prefs.getBoolPref("prompts.tab_modal.enabled")
&& SpecialPowers.Services.prefs.getIntPref("prompts.modalType.httpAuth")
!== SpecialPowers.Services.prompt.MODAL_TYPE_WINDOW;
// These are magically defined on the window due to the iframe IDs
/* global iframe1, iframe2a, iframe2b */

Просмотреть файл

@ -19,8 +19,11 @@
<script class="testbody" type="text/javascript">
var iframe = document.getElementById("iframe");
// Force parent to not look for tab-modal prompts, as they're not used for auth prompts.
isTabModal = false;
// Let parent check for tab modal prompts if they are enabled for auth
// prompts. Used by prompt_common.js.
isTabModal = SpecialPowers.Services.prefs.getBoolPref("prompts.tab_modal.enabled")
&& SpecialPowers.Services.prefs.getIntPref("prompts.modalType.httpAuth")
!== SpecialPowers.Services.prompt.MODAL_TYPE_WINDOW;
const AUTHENTICATE_PATH = new URL("authenticate.sjs", window.location.href).pathname;

Просмотреть файл

@ -17,8 +17,11 @@
<pre id="test">
<script class="testbody" type="text/javascript">
// Force parent to not look for tab-modal prompts, as they're not used for auth prompts.
isTabModal = false;
// Let parent check for tab modal prompts if they are enabled for auth
// prompts. Used by prompt_common.js.
isTabModal = SpecialPowers.Services.prefs.getBoolPref("prompts.tab_modal.enabled")
&& SpecialPowers.Services.prefs.getIntPref("prompts.modalType.httpAuth")
!== SpecialPowers.Services.prompt.MODAL_TYPE_WINDOW;
add_task(async function setup() {
let loginAddedPromise = promiseStorageChanged(["addLogin"]);

Просмотреть файл

@ -40,8 +40,11 @@ function xhrLoad(xmlDoc) {
return {username, password, authok};
}
// Force parent to not look for tab-modal prompts, as they're not used for auth prompts.
isTabModal = false;
// Let parent check for tab modal prompts if they are enabled for auth
// prompts. Used by prompt_common.js.
isTabModal = SpecialPowers.Services.prefs.getBoolPref("prompts.tab_modal.enabled")
&& SpecialPowers.Services.prefs.getIntPref("prompts.modalType.httpAuth")
!== SpecialPowers.Services.prompt.MODAL_TYPE_WINDOW;
let prompterParent = runInParent(() => {
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
@ -131,15 +134,19 @@ add_task(async function test2() {
checked: false,
focused: "textField",
defButton: "button0",
// Check that the dialog is modal, chrome and dependent;
// We can't just check window.opener because that'll be
// a content window, which therefore isn't exposed (it'll lie and
// be null).
chrome: true,
dialog: true,
chromeDependent: true,
isWindowModal: true,
};
// For window prompts check that the dialog is modal, chrome and dependent;
// We can't just check window.opener because that'll be
// a content window, which therefore isn't exposed (it'll lie and
// be null).
if(!isTabModal) {
state.chrome = true;
state.dialog = true;
state.chromeDependent = true;
state.isWindowModal = true;
}
let action = {
buttonClick: "ok",
};

Просмотреть файл

@ -33,6 +33,12 @@ var iframe1Loaded = onloadPromiseFor("iframe_diff_origin");
var iframe2Loaded = onloadPromiseFor("iframe_same_origin");
var iframe_prompt = document.getElementById("iframe_prompt");
add_task(function setup() {
isTabModal = SpecialPowers.Services.prefs.getBoolPref("prompts.tab_modal.enabled")
&& SpecialPowers.Services.prefs.getIntPref("prompts.modalType.httpAuth")
!== SpecialPowers.Services.prompt.MODAL_TYPE_WINDOW;
})
add_task(async function runTest() {
// This test depends on tab modal prompts being enabled.
if (!isTabModal) {
@ -99,12 +105,9 @@ add_task(async function runTest() {
});
add_task(async function runTestAuth() {
// Following tests chack prompt message for a cross-origin and not
// Following tests check prompt message for a cross-origin and not
// cross-origin subresources load
// Force parent to not look for tab-modal prompts, as they're not
// used for auth prompts.
isTabModal = false;
let state, action;
state = {
@ -142,8 +145,6 @@ add_task(async function runTestAuth() {
// Cross-origin subresourse test.
// Force parent to not look for tab-modal prompts, as they're not used for auth prompts.
isTabModal = false;
state = {
msg: "http://example.com is requesting your username and password. " +
"WARNING: Your password will not be sent to the website you are currently visiting!",

Просмотреть файл

@ -3,6 +3,9 @@
const { PermissionTestUtils } = ChromeUtils.import(
"resource://testing-common/PermissionTestUtils.jsm"
);
const { PromptTestUtils } = ChromeUtils.import(
"resource://testing-common/PromptTestUtils.jsm"
);
const RELATIVE_DIR = "toolkit/mozapps/extensions/test/xpinstall/";
@ -118,6 +121,9 @@ var Harness = {
Services.obs.addObserver(this, "addon-install-failed");
Services.obs.addObserver(this, "addon-install-complete");
// For browser_auth tests which trigger auth dialogs.
Services.obs.addObserver(this, "tabmodal-dialog-loaded");
this._boundWin = Cu.getWeakReference(win); // need this so our addon manager listener knows which window to use.
AddonManager.addInstallListener(this);
AddonManager.addAddonListener(this);
@ -143,6 +149,8 @@ var Harness = {
Services.obs.removeObserver(self, "addon-install-failed");
Services.obs.removeObserver(self, "addon-install-complete");
Services.obs.removeObserver(self, "tabmodal-dialog-loaded");
AddonManager.removeInstallListener(self);
AddonManager.removeAddonListener(self);
@ -223,40 +231,46 @@ var Harness = {
}
},
// This affects all browser_auth tests
// Window open handling
windowReady(window) {
if (window.document.location.href == PROMPT_URL) {
var promptType = window.args.promptType;
let dialog = window.document.getElementById("commonDialog");
switch (promptType) {
case "alert":
case "alertCheck":
case "confirmCheck":
case "confirm":
case "confirmEx":
dialog.acceptDialog();
break;
case "promptUserAndPass":
// This is a login dialog, hopefully an authentication prompt
// for the xpi.
if (this.authenticationCallback) {
var auth = this.authenticationCallback();
if (auth && auth.length == 2) {
window.document.getElementById("loginTextbox").value = auth[0];
window.document.getElementById("password1Textbox").value =
auth[1];
dialog.acceptDialog();
} else {
dialog.cancelDialog();
}
this.promptReady(window.Dialog, Services.prompt.MODAL_TYPE_WINDOW);
}
},
promptReady(dialog) {
let promptType = dialog.args.promptType;
switch (promptType) {
case "alert":
case "alertCheck":
case "confirmCheck":
case "confirm":
case "confirmEx":
PromptTestUtils.handlePrompt(dialog, { buttonNumClick: 0 });
break;
case "promptUserAndPass":
// This is a login dialog, hopefully an authentication prompt
// for the xpi.
if (this.authenticationCallback) {
var auth = this.authenticationCallback();
if (auth && auth.length == 2) {
PromptTestUtils.handlePrompt(dialog, {
loginInput: auth[0],
passwordInput: auth[1],
buttonNumClick: 0,
});
} else {
dialog.cancelDialog();
PromptTestUtils.handlePrompt(dialog, { buttonNumClick: 1 });
}
break;
default:
ok(false, "prompt type " + promptType + " not handled in test.");
break;
}
} else {
PromptTestUtils.handlePrompt(dialog, { buttonNumClick: 1 });
}
break;
default:
ok(false, "prompt type " + promptType + " not handled in test.");
break;
}
},
@ -556,6 +570,11 @@ var Harness = {
);
}, this);
break;
case "tabmodal-dialog-loaded":
let browser = subject.ownerGlobal.gBrowser.selectedBrowser;
let prompt = browser.tabModalPromptBox.getPrompt(subject);
this.promptReady(prompt.Dialog);
break;
}
},