1 // -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
2 // Any copyright is dedicated to the Public Domain.
3 // http://creativecommons.org/publicdomain/zero/1.0/
6 // Tests that PSM can successfully ask for a password from the user and relay it
7 // back to NSS. Does so by mocking out the actual dialog and "filling in" the
8 // password. Also tests that providing an incorrect password will fail (well,
9 // technically the user will just get prompted again, but if they then cancel
10 // the dialog the overall operation will fail).
16 // This intentionally does not use arrow function syntax to avoid an issue
17 // where in the context of the arrow function, |this != gMockPrompter| due to
18 // how objects get wrapped when going across xpcom boundaries.
19 promptPassword(dialogTitle, text, password, checkMsg) {
21 if (this.numPrompts > 1) {
22 // don't keep retrying a bad password
27 "Please enter your Primary Password.",
28 "password prompt text should be as expected"
30 equal(checkMsg, null, "checkMsg should be null");
31 ok(this.passwordToTry, "passwordToTry should be non-null");
32 password.value = this.passwordToTry;
36 QueryInterface: ChromeUtils.generateQI(["nsIPrompt"]),
39 // Mock nsIWindowWatcher. PSM calls getNewPrompter on this to get an nsIPrompt
40 // to call promptPassword. We return the mock one, above.
41 var gWindowWatcher = {
42 getNewPrompter: () => gMockPrompter,
43 QueryInterface: ChromeUtils.generateQI(["nsIWindowWatcher"]),
49 let windowWatcherCID = MockRegistrar.register(
50 "@mozilla.org/embedcomp/window-watcher;1",
53 registerCleanupFunction(() => {
54 MockRegistrar.unregister(windowWatcherCID);
57 // Set an initial password.
58 let tokenDB = Cc["@mozilla.org/security/pk11tokendb;1"].getService(
61 let token = tokenDB.getInternalKeyToken();
62 token.initPassword("hunter2");
65 // Try with the correct password.
66 gMockPrompter.passwordToTry = "hunter2";
67 // Using nsISecretDecoderRing will cause the password prompt to come up if the
68 // token has a password and is logged out.
69 let sdr = Cc["@mozilla.org/security/sdr;1"].getService(
70 Ci.nsISecretDecoderRing
72 sdr.encryptString("poke");
73 equal(gMockPrompter.numPrompts, 1, "should have prompted for password once");
76 gMockPrompter.numPrompts = 0;
79 // Try with an incorrect password.
80 gMockPrompter.passwordToTry = "*******";
82 () => sdr.encryptString("poke2"),
84 "logging in with the wrong password should fail"
86 equal(gMockPrompter.numPrompts, 2, "should have prompted for password twice");