--- a/toolkit/components/passwordmgr/test/mochitest/test_master_password.html
+++ b/toolkit/components/passwordmgr/test/mochitest/test_master_password.html
@@ -12,57 +12,57 @@
<body>
Login Manager test: master password.
<script>
"use strict";
// Force parent to not look for tab-modal prompts, as they're not used for auth prompts.
isTabModal = false;
-commonInit();
+var chromeScript = runChecksAfterCommonInit();
SimpleTest.requestFlakyTimeout("untriaged");
-var pwmgr = SpecialPowers.Cc["@mozilla.org/login-manager;1"]
- .getService(SpecialPowers.Ci.nsILoginManager);
-var pwcrypt = SpecialPowers.Cc["@mozilla.org/login-manager/crypto/SDR;1"]
- .getService(Ci.nsILoginManagerCrypto);
-
-var nsLoginInfo = new SpecialPowers.wrap(SpecialPowers.Components).Constructor("@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo);
-
var exampleCom = "http://example.com/tests/toolkit/components/passwordmgr/test/mochitest/";
var exampleOrg = "http://example.org/tests/toolkit/components/passwordmgr/test/mochitest/";
-var login1 = new nsLoginInfo();
-var login2 = new nsLoginInfo();
+runInParent(() => {
+ const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
+ var pwmgr = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
+
+ var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo);
+
+ var login1 = new nsLoginInfo();
+ var login2 = new nsLoginInfo();
-login1.init("http://example.com", "http://example.com", null,
- "user1", "pass1", "uname", "pword");
-login2.init("http://example.org", "http://example.org", null,
- "user2", "pass2", "uname", "pword");
+ login1.init("http://example.com", "http://example.com", null,
+ "user1", "pass1", "uname", "pword");
+ login2.init("http://example.org", "http://example.org", null,
+ "user2", "pass2", "uname", "pword");
-pwmgr.addLogin(login1);
-pwmgr.addLogin(login2);
+ pwmgr.addLogin(login1);
+ pwmgr.addLogin(login2);
+});
</script>
<p id="display"></p>
<div id="content" style="display: none">
<iframe id="iframe1"></iframe>
<iframe id="iframe2"></iframe>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
var iframe1 = document.getElementById("iframe1");
var iframe2 = document.getElementById("iframe2");
add_task(function* test_1() {
- ok(pwcrypt.isLoggedIn, "should be initially logged in (no MP)");
+ ok(isLoggedIn(), "should be initially logged in (no MP)");
enableMasterPassword();
- ok(!pwcrypt.isLoggedIn, "should be logged out after setting MP");
+ ok(!isLoggedIn(), "should be logged out after setting MP");
// Trigger a MP prompt via the API
var state = {
msg : "Please enter the master password for the Software Security Device.",
title : "the title",
textValue : "",
passValue : "",
iconClass : "authentication-icon question-icon",
@@ -76,24 +76,24 @@ add_task(function* test_1() {
defButton : "button0",
};
var action = {
buttonClick : "ok",
passField : masterPassword,
};
var promptDone = handlePrompt(state, action);
- var logins = pwmgr.getAllLogins();
+ var logins = chromeScript.sendSyncMessage("getAllLogins")[0][0];
yield promptDone;
is(logins.length, 3, "expected number of logins");
- ok(pwcrypt.isLoggedIn, "should be logged in after MP prompt");
+ ok(isLoggedIn(), "should be logged in after MP prompt");
logoutMasterPassword();
- ok(!pwcrypt.isLoggedIn, "should be logged out");
+ ok(!isLoggedIn(), "should be logged out");
});
add_task(function* test_2() {
// Try again but click cancel.
var state = {
msg : "Please enter the master password for the Software Security Device.",
title : "the title",
textValue : "",
@@ -111,22 +111,22 @@ add_task(function* test_2() {
var action = {
buttonClick : "cancel",
};
var promptDone = handlePrompt(state, action);
var failedAsExpected = false;
var logins = null;
try {
- logins = pwmgr.getAllLogins();
+ logins = chromeScript.sendSyncMessage("getAllLogins")[0][0];
} catch (e) { failedAsExpected = true; }
yield promptDone;
ok(failedAsExpected, "getAllLogins should have thrown");
is(logins, null, "shouldn't have gotten logins");
- ok(!pwcrypt.isLoggedIn, "should still be logged out");
+ ok(!isLoggedIn(), "should still be logged out");
});
add_task(function* test_3() {
var state = {
msg : "Please enter the master password for the Software Security Device.",
title : "the title",
textValue : "",
passValue : "",
@@ -159,19 +159,19 @@ add_task(function* test_3() {
info("filled");
// check contents of iframe1 fields
var u = SpecialPowers.wrap(iframe1).contentDocument.getElementById("userfield");
var p = SpecialPowers.wrap(iframe1).contentDocument.getElementById("passfield");
is(u.value, "user1", "checking expected user to have been filled in");
is(p.value, "pass1", "checking expected pass to have been filled in");
- ok(pwcrypt.isLoggedIn, "should be logged in");
+ ok(isLoggedIn(), "should be logged in");
logoutMasterPassword();
- ok(!pwcrypt.isLoggedIn, "should be logged out");
+ ok(!isLoggedIn(), "should be logged out");
});
add_task(function* test_4() {
var state = {
msg : "Please enter the master password for the Software Security Device.",
title : "the title",
textValue : "",
passValue : "",
@@ -196,17 +196,17 @@ add_task(function* test_4() {
yield promptDone;
// check contents of iframe1 fields
var u = SpecialPowers.wrap(iframe1).contentDocument.getElementById("userfield");
var p = SpecialPowers.wrap(iframe1).contentDocument.getElementById("passfield");
is(u.value, "", "checking expected empty user");
is(p.value, "", "checking expected empty pass");
- ok(!pwcrypt.isLoggedIn, "should be logged out");
+ ok(!isLoggedIn(), "should be logged out");
// XXX check that there's 1 MP window open
// Load another iframe with a login form
// This should detect that there's already a pending MP prompt, and not
// put up a second one.
var loadPromise = new Promise(resolve => {
iframe2.addEventListener("load", function onload() {
@@ -227,17 +227,17 @@ add_task(function* test_4() {
// check contents of iframe2 fields
u = SpecialPowers.wrap(iframe2).contentDocument.getElementById("userfield");
p = SpecialPowers.wrap(iframe2).contentDocument.getElementById("passfield");
is(u.value, "", "checking expected empty user");
is(p.value, "", "checking expected empty pass");
// XXX check that there's 1 MP window open
- ok(!pwcrypt.isLoggedIn, "should be logged out");
+ ok(!isLoggedIn(), "should be logged out");
// Ok, now enter the MP. The MP prompt is already up.
var fillPromise = new Promise(resolve => {
addEventListener("message", resolve);
});
// fill existing MP dialog with MP.
@@ -247,17 +247,17 @@ add_task(function* test_4() {
};
yield handlePrompt(state, action);
yield fillPromise;
// We shouldn't have to worry about iframe1's load event racing with
// filling of iframe2's data. We notify observers synchronously, so
// iframe2's observer will process iframe2 before iframe1 even finishes
// processing the form.
- ok(pwcrypt.isLoggedIn, "should be logged in");
+ ok(isLoggedIn(), "should be logged in");
// check contents of iframe1 fields
u = SpecialPowers.wrap(iframe1).contentDocument.getElementById("userfield");
p = SpecialPowers.wrap(iframe1).contentDocument.getElementById("passfield");
is(u.value, "user2", "checking expected user to have been filled in");
is(p.value, "pass2", "checking expected pass to have been filled in");
// check contents of iframe2 fields
@@ -266,17 +266,14 @@ add_task(function* test_4() {
is(u.value, "user1", "checking expected user to have been filled in");
is(p.value, "pass1", "checking expected pass to have been filled in");
});
// XXX do a test5ABC with clicking cancel?
SimpleTest.registerCleanupFunction(function finishTest() {
disableMasterPassword();
-
- pwmgr.removeLogin(login1);
- pwmgr.removeLogin(login2);
});
</script>
</pre>
</body>
</html>
--- a/toolkit/components/passwordmgr/test/pwmgr_common.js
+++ b/toolkit/components/passwordmgr/test/pwmgr_common.js
@@ -202,36 +202,29 @@ function enableMasterPassword() {
setMasterPassword(true);
}
function disableMasterPassword() {
setMasterPassword(false);
}
function setMasterPassword(enable) {
- var oldPW, newPW;
- if (enable) {
- oldPW = "";
- newPW = masterPassword;
- } else {
- oldPW = masterPassword;
- newPW = "";
- }
- // Set master password. Note that this does not log you in, so the next
- // invocation of pwmgr can trigger a MP prompt.
-
- var pk11db = Cc["@mozilla.org/security/pk11tokendb;1"].getService(Ci.nsIPK11TokenDB);
- var token = pk11db.findTokenByName("");
- info("MP change from " + oldPW + " to " + newPW);
- token.changePassword(oldPW, newPW);
+ chromeScript.sendSyncMessage("setMasterPassword", { enable });
}
function logoutMasterPassword() {
- var sdr = Cc["@mozilla.org/security/sdr;1"].getService(Ci.nsISecretDecoderRing);
- sdr.logoutAndTeardown();
+ runInParent(function parent_logoutMasterPassword() {
+ const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
+ var sdr = Cc["@mozilla.org/security/sdr;1"].getService(Ci.nsISecretDecoderRing);
+ sdr.logoutAndTeardown();
+ });
+}
+
+function isLoggedIn() {
+ return chromeScript.sendSyncMessage("isLoggedIn")[0][0];
}
function dumpLogins(pwmgr) {
var logins = pwmgr.getAllLogins();
ok(true, "----- dumpLogins: have " + logins.length + " logins. -----");
for (var i = 0; i < logins.length; i++)
dumpLogin("login #" + i + " --- ", logins[i]);
}
@@ -356,16 +349,17 @@ function runChecksAfterCommonInit(aFunct
// Code to run when loaded as a chrome script in tests via loadChromeScript
if (this.addMessageListener) {
const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
var SpecialPowers = { Cc, Ci, Cr, Cu, };
var ok, is;
// Ignore ok/is in commonInit since they aren't defined in a chrome script.
ok = is = () => {}; // eslint-disable-line no-native-reassign
+ Cu.import("resource://gre/modules/LoginHelper.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Task.jsm");
function onStorageChanged(subject, topic, data) {
sendAsyncMessage("storageChanged", {
topic,
data,
});
@@ -390,16 +384,42 @@ if (this.addMessageListener) {
yield recipeParent.reset();
sendAsyncMessage("recipesReset");
}));
addMessageListener("countLogins", ({formOrigin, submitOrigin, httpRealm}) => {
return Services.logins.countLogins(formOrigin, submitOrigin, httpRealm);
});
+ addMessageListener("getAllLogins", () => {
+ return LoginHelper.loginsToVanillaObjects(Services.logins.getAllLogins());
+ });
+
+ addMessageListener("isLoggedIn", () => {
+ return Services.logins.isLoggedIn;
+ });
+
+ addMessageListener("setMasterPassword", ({ enable }) => {
+ var oldPW, newPW;
+ if (enable) {
+ oldPW = "";
+ newPW = masterPassword;
+ } else {
+ oldPW = masterPassword;
+ newPW = "";
+ }
+ // Set master password. Note that this does not log you in, so the next
+ // invocation of pwmgr can trigger a MP prompt.
+
+ var pk11db = Cc["@mozilla.org/security/pk11tokendb;1"].getService(Ci.nsIPK11TokenDB);
+ var token = pk11db.findTokenByName("");
+ dump("MP change from " + oldPW + " to " + newPW + "\n");
+ token.changePassword(oldPW, newPW);
+ });
+
var globalMM = Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageListenerManager);
globalMM.addMessageListener("RemoteLogins:onFormSubmit", function onFormSubmit(message) {
sendAsyncMessage("formSubmissionProcessed", message.data, message.objects);
});
} else {
// Code to only run in the mochitest pages (not in the chrome script).
SimpleTest.registerCleanupFunction(() => {
runInParent(function cleanupParent() {