summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_primary_password.html
blob: f8ffec57d05aaec053fefb1c98f7083ba767e02c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for primary password</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="pwmgr_common.js"></script>
  <script type="text/javascript" src="../../../prompts/test/prompt_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
Login Manager test: primary password.

<p id="display"></p>

<div id="content" style="display: none">
</div>

<pre id="test">
<script class="testbody" type="text/javascript">
"use strict";

// Force parent to not look for tab-modal prompts, as they're not used for auth prompts.
modalType = Ci.nsIPrompt.MODAL_TYPE_WINDOW;

const exampleCom = "https://example.com/tests/toolkit/components/passwordmgr/test/mochitest/";
const exampleOrg = "https://example.org/tests/toolkit/components/passwordmgr/test/mochitest/";

gTestDependsOnDeprecatedLogin = true;
const chromeScript = runChecksAfterCommonInit();

const win = window.open("about:blank");
SimpleTest.registerCleanupFunction(() => win.close());

add_setup(async () => {
  await addLoginsInParent(
    ["https://example.com", "https://example.com", null, "user1", "pass1", "uname", "pword"],
    ["https://example.org", "https://example.org", null, "user2", "pass2", "uname", "pword"]
  );
  ok(await isLoggedIn(), "should be initially logged in (no PP)");
  enablePrimaryPassword();
  ok(!await isLoggedIn(), "should be logged out after setting PP");
});

add_task(async function test_1() {
  // Trigger a MP prompt via the API
  const state = {
    msg: "Please enter your Primary Password.",
    title: "the title",
    textValue: "",
    passValue: "",
    iconClass: "authentication-icon question-icon",
    titleHidden: true,
    textHidden: true,
    passHidden: false,
    checkHidden: true,
    checkMsg: "",
    checked: false,
    focused: "passField",
    defButton: "button0",
  };
  const action = {
    buttonClick: "ok",
    passField: LoginTestUtils.primaryPassword.primaryPassword,
  };
  const promptDone = handlePrompt(state, action);

  const logins = await LoginManager.getAllLogins();

  await promptDone;
  is(logins.length, 3, "expected number of logins");

  ok(await isLoggedIn(), "should be logged in after MP prompt");
  logoutPrimaryPassword();
  ok(!await isLoggedIn(), "should be logged out");
});

add_task(async function test_2() {
  // Try again but click cancel.
  const state = {
    msg: "Please enter your Primary Password.",
    title: "the title",
    textValue: "",
    passValue: "",
    iconClass: "authentication-icon question-icon",
    titleHidden: true,
    textHidden: true,
    passHidden: false,
    checkHidden: true,
    checkMsg: "",
    checked: false,
    focused: "passField",
    defButton: "button0",
  };
  const action = {
    buttonClick: "cancel",
  };
  const promptDone = handlePrompt(state, action);

  const logins = await LoginManager.getAllLogins().catch(() => {});
  await promptDone;
  is(logins, undefined, "shouldn't have gotten logins");
  ok(!await isLoggedIn(), "should still be logged out");
});

add_task(async function test_3() {
  const state = {
    msg: "Please enter your Primary Password.",
    title: "the title",
    textValue: "",
    passValue: "",
    iconClass: "authentication-icon question-icon",
    titleHidden: true,
    textHidden: true,
    passHidden: false,
    checkHidden: true,
    checkMsg: "",
    checked: false,
    focused: "passField",
    defButton: "button0",
  };
  const action = {
    buttonClick: "ok",
    passField: LoginTestUtils.primaryPassword.primaryPassword,
  };
  const promptDone = handlePrompt(state, action);

  const fillPromise = promiseFormsProcessed();

  info("Load a single window to trigger a MP");
  await SimpleTest.promiseFocus(win, true);
  win.location = exampleCom + "subtst_primary_pass.html";

  await promptDone;
  info("promptDone");
  await fillPromise;
  info("filled");

  // check contents of win fields

  await SpecialPowers.spawn(win, [], function() {
    const u = this.content.document.getElementById("userfield");
    const p = this.content.document.getElementById("passfield");
    Assert.equal(u.value, "user1", "checking expected user to have been filled in");
    Assert.equal(p.value, "pass1", "checking expected pass to have been filled in");
    u.value = "";
    p.value = "";
  });

  ok(await isLoggedIn(), "should be logged in");
  logoutPrimaryPassword();
  ok(!await isLoggedIn(), "should be logged out");
});

add_task(async function test_4() {
  const state = {
    msg: "Please enter your Primary Password.",
    title: "the title",
    textValue: "",
    passValue: "",
    iconClass: "authentication-icon question-icon",
    titleHidden: true,
    textHidden: true,
    passHidden: false,
    checkHidden: true,
    checkMsg: "",
    checked: false,
    focused: "passField",
    defButton: "button0",
  };
  let action = {
    buttonClick: "none",
  };
  const promptDone = handlePrompt(state, action);

  // first part of loading 2 MP-triggering windows
  await SimpleTest.promiseFocus(win);
  win.location = exampleOrg + "subtst_primary_pass.html";
  // The MP prompt is open but don't take any action yet.
  await promptDone;

  // check contents of win fields
  await SpecialPowers.spawn(win, [], function() {
    const u = this.content.document.getElementById("userfield");
    const p = this.content.document.getElementById("passfield");
    Assert.equal(u.value, "", "checking expected empty user");
    Assert.equal(p.value, "", "checking expected empty pass");
  });

  ok(!await isLoggedIn(), "should be logged out");

  // XXX check that there's 1 MP window open

  // Load a second login form in an iframe
  // This should detect that there's already a pending MP prompt, and not
  // put up a second one.

  // Since the Primary Password prompt is open, we can't focus another tab
  // to load the second form. Instead, we load the same form into an iframe.
  const url = exampleOrg + "subtst_primary_pass.html";
  await SpecialPowers.spawn(win, [url], async function(urlF) {
    const iframe = this.content.document.querySelector("iframe");
    const loadPromise = new Promise(resolve => {
      iframe.addEventListener("load", function onload() {
        resolve();
      }, { once: true });
    });
    // Use the same origin as the top level to ensure we would autofill
    // if we could (we don't fill in cross-origin iframes).
    iframe.src = urlF;
    await loadPromise;
  });

  // We can't use promiseFormsProcessed* here, because _fillForm doesn't
  // run if Primary Password is locked.
  await new Promise(resolve => {
    // Testing a negative, wait a little to give the login manager a chance to
    // (incorrectly) fill in the form.  Note, we cannot use setTimeout()
    // here because the modal window suspends all window timers.  Instead we
    // must use a chrome script to use nsITimer directly.
    const chromeURL = SimpleTest.getTestFileURL("chrome_timeout.js");
    const script = SpecialPowers.loadChromeScript(chromeURL);
    script.addMessageListener("ready", _ => {
      script.sendAsyncMessage("setTimeout", { delay: 500 });
    });
    script.addMessageListener("timeout", resolve);
  });

  // iframe should load without having triggered a MP prompt (because one
  // is already waiting)

  // check contents of iframe fields
  await SpecialPowers.spawn(win, [], function() {
    const iframe = this.content.document.querySelector("iframe");
    const frameDoc = iframe.contentDocument;
    const u = frameDoc.getElementById("userfield");
    const p = frameDoc.getElementById("passfield");
    Assert.equal(u.value, "", "checking expected empty user");
    Assert.equal(p.value, "", "checking expected empty pass");
  });

  // XXX check that there's 1 MP window open
  ok(!await isLoggedIn(), "should be logged out");

  // Ok, now enter the MP. The MP prompt is already up.
  const fillPromise = promiseFormsProcessed(2);

  // fill existing MP dialog with MP.
  action = {
    buttonClick: "ok",
    passField: LoginTestUtils.primaryPassword.primaryPassword,
  };
  await handlePrompt(state, action);
  await fillPromise;

  // We shouldn't have to worry about win's load event racing with
  // filling of the iframe's data. We notify observers synchronously, so
  // the iframe's observer will process the iframe before win even finishes
  // processing the form.
  ok(await isLoggedIn(), "should be logged in");

  // check contents of win fields
  await SpecialPowers.spawn(win, [], function() {
    const u = this.content.document.getElementById("userfield");
    const p = this.content.document.getElementById("passfield");
    Assert.equal(u.value, "user2", "checking expected user to have been filled in");
    Assert.equal(p.value, "pass2", "checking expected pass to have been filled in");

    // clearing fields to not cause a submission when the next document is loaded
    u.value = "";
    p.value = "";
  });

  // check contents of iframe fields
  await SpecialPowers.spawn(win, [], function() {
    const iframe = this.content.document.querySelector("iframe");
    const frameDoc = iframe.contentDocument;
    const u = frameDoc.getElementById("userfield");
    const p = frameDoc.getElementById("passfield");
    Assert.equal(u.value, "user2", "checking expected user to have been filled in");
    Assert.equal(p.value, "pass2", "checking expected pass to have been filled in");

    // clearing fields to not cause a submission when the next document is loaded
    u.value = "";
    p.value = "";
  });
});

// XXX do a test5ABC with clicking cancel?

SimpleTest.registerCleanupFunction(function finishTest() {
  disablePrimaryPassword();
});

</script>
</pre>
</body>
</html>