summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/unit/test_maybeImportLogin.js
blob: fbf494d52b6b4d51a895f79d0d3e61144e468123 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"use strict";

const HOST1 = "https://www.example.com";
const HOST2 = "https://www.mozilla.org";

const USER1 = "myuser";
const USER2 = "anotheruser";

const PASS1 = "mypass";
const PASS2 = "anotherpass";
const PASS3 = "yetanotherpass";

async function maybeImportLogins(logins) {
  let summary = await LoginHelper.maybeImportLogins(logins);
  return summary.filter(ir => ir.result == "added").map(ir => ir.login);
}

add_task(async function test_invalid_logins() {
  let importedLogins = await maybeImportLogins([
    {
      username: USER1,
      password: PASS1,
      origin: "example.com", // Not an origin
      formActionOrigin: HOST1,
    },
    {
      username: USER1,
      // no password
      origin: HOST1,
      formActionOrigin: HOST1,
    },
    {
      username: USER2,
      password: "", // Empty password
      origin: HOST1,
      formActionOrigin: HOST1,
    },
  ]);
  Assert.equal(
    importedLogins.length,
    0,
    `Return value should indicate no imported login: ${JSON.stringify(
      importedLogins,
      null,
      2
    )}`
  );
  let savedLogins = await Services.logins.getAllLogins();
  Assert.equal(
    savedLogins.length,
    0,
    `Should have no logins in storage: ${JSON.stringify(savedLogins, null, 2)}`
  );
  Services.logins.removeAllUserFacingLogins();
});

add_task(async function test_new_logins() {
  let [importedLogin] = await maybeImportLogins([
    {
      username: USER1,
      password: PASS1,
      origin: HOST1 + "/",
      formActionOrigin: HOST1 + "/",
    },
  ]);
  Assert.ok(importedLogin, "Return value should indicate imported login.");
  let matchingLogins = LoginHelper.searchLoginsWithObject({ origin: HOST1 });
  Assert.equal(
    matchingLogins.length,
    1,
    `There should be 1 login for ${HOST1}`
  );

  [importedLogin] = await maybeImportLogins([
    {
      username: USER1,
      password: PASS1,
      origin: HOST2,
      formActionOrigin: HOST2,
    },
  ]);

  Assert.ok(
    importedLogin,
    "Return value should indicate another imported login."
  );
  matchingLogins = LoginHelper.searchLoginsWithObject({ origin: HOST1 });
  Assert.equal(
    matchingLogins.length,
    1,
    `There should still be 1 login for ${HOST1}`
  );

  matchingLogins = LoginHelper.searchLoginsWithObject({ origin: HOST2 });
  Assert.equal(
    matchingLogins.length,
    1,
    `There should also be 1 login for ${HOST2}`
  );
  Assert.equal(
    (await Services.logins.getAllLogins()).length,
    2,
    "There should be 2 logins in total"
  );
  Services.logins.removeAllUserFacingLogins();
});

add_task(async function test_duplicate_logins() {
  let [importedLogin] = await maybeImportLogins([
    {
      username: USER1,
      password: PASS1,
      origin: HOST1,
      formActionOrigin: HOST1,
    },
  ]);
  Assert.ok(importedLogin, "Return value should indicate imported login.");
  let matchingLogins = LoginHelper.searchLoginsWithObject({ origin: HOST1 });
  Assert.equal(
    matchingLogins.length,
    1,
    `There should be 1 login for ${HOST1}`
  );

  [importedLogin] = await maybeImportLogins([
    {
      username: USER1,
      password: PASS1,
      origin: HOST1,
      formActionOrigin: HOST1,
    },
  ]);
  Assert.ok(
    !importedLogin,
    "Return value should indicate no new login was imported."
  );
  matchingLogins = LoginHelper.searchLoginsWithObject({ origin: HOST1 });
  Assert.equal(
    matchingLogins.length,
    1,
    `There should still be 1 login for ${HOST1}`
  );
  Services.logins.removeAllUserFacingLogins();
});

add_task(async function test_different_passwords() {
  let [importedLogin] = await maybeImportLogins([
    {
      username: USER1,
      password: PASS1,
      origin: HOST1,
      formActionOrigin: HOST1,
      timeCreated: new Date(Date.now() - 1000),
    },
  ]);
  Assert.ok(importedLogin, "Return value should indicate imported login.");
  let matchingLogins = LoginHelper.searchLoginsWithObject({ origin: HOST1 });
  Assert.equal(
    matchingLogins.length,
    1,
    `There should be 1 login for ${HOST1}`
  );

  // This item will be newer, so its password should take precedence.
  [importedLogin] = await maybeImportLogins([
    {
      username: USER1,
      password: PASS2,
      origin: HOST1,
      formActionOrigin: HOST1,
      timeCreated: new Date(),
    },
  ]);
  Assert.ok(
    !importedLogin,
    "Return value should not indicate imported login (as we updated an existing one)."
  );
  matchingLogins = LoginHelper.searchLoginsWithObject({ origin: HOST1 });
  Assert.equal(
    matchingLogins.length,
    1,
    `There should still be 1 login for ${HOST1}`
  );
  Assert.equal(
    matchingLogins[0].password,
    PASS2,
    "We should have updated the password for this login."
  );

  // Now try to update with an older password:
  [importedLogin] = await maybeImportLogins([
    {
      username: USER1,
      password: PASS3,
      origin: HOST1,
      formActionOrigin: HOST1,
      timeCreated: new Date(Date.now() - 1000000),
    },
  ]);
  Assert.ok(
    !importedLogin,
    "Return value should not indicate imported login (as we didn't update anything)."
  );
  matchingLogins = LoginHelper.searchLoginsWithObject({ origin: HOST1 });
  Assert.equal(
    matchingLogins.length,
    1,
    `There should still be 1 login for ${HOST1}`
  );
  Assert.equal(
    matchingLogins[0].password,
    PASS2,
    "We should NOT have updated the password for this login."
  );

  Services.logins.removeAllUserFacingLogins();
});

add_task(async function test_different_usernames_without_guid() {
  let [importedLogin] = await maybeImportLogins([
    {
      username: USER1,
      password: PASS1,
      origin: HOST1,
      formActionOrigin: HOST1,
    },
  ]);
  Assert.ok(importedLogin, "Return value should indicate imported login.");
  let matchingLogins = LoginHelper.searchLoginsWithObject({ origin: HOST1 });
  Assert.equal(
    matchingLogins.length,
    1,
    `There should be 1 login for ${HOST1}`
  );

  [importedLogin] = await maybeImportLogins([
    {
      username: USER2,
      password: PASS1,
      origin: HOST1,
      formActionOrigin: HOST1,
    },
  ]);
  Assert.ok(
    importedLogin,
    "Return value should indicate another imported login."
  );
  matchingLogins = LoginHelper.searchLoginsWithObject({ origin: HOST1 });
  Assert.equal(
    matchingLogins.length,
    2,
    `There should now be 2 logins for ${HOST1}`
  );

  Services.logins.removeAllUserFacingLogins();
});

add_task(async function test_different_usernames_with_guid() {
  let [{ login: importedLogin }] = await LoginHelper.maybeImportLogins([
    {
      username: USER1,
      password: PASS1,
      origin: HOST1,
      formActionOrigin: HOST1,
    },
  ]);
  Assert.ok(importedLogin, "Return value should indicate imported login.");
  let matchingLogins = LoginHelper.searchLoginsWithObject({ origin: HOST1 });
  Assert.equal(
    matchingLogins.length,
    1,
    `There should be 1 login for ${HOST1}`
  );

  info("Changing both the origin and username using the GUID");
  let importedLogins = await LoginHelper.maybeImportLogins([
    {
      username: USER2,
      password: PASS1,
      origin: HOST2,
      formActionOrigin: HOST1,
      guid: importedLogin.guid,
    },
  ]);
  Assert.equal(
    importedLogins[0].result,
    "modified",
    "Return value should indicate an update"
  );
  matchingLogins = LoginHelper.searchLoginsWithObject({ origin: HOST2 });
  Assert.equal(
    matchingLogins.length,
    1,
    `The 1 login for ${HOST1} should have been updated`
  );
  let storageLogin = matchingLogins[0];
  Assert.equal(storageLogin.guid, importedLogin.guid, "Check same guid");
  Assert.equal(storageLogin.username, USER2, "Check username updated");
  Assert.equal(storageLogin.origin, HOST2, "Check origin updated");

  Services.logins.removeAllUserFacingLogins();
});

add_task(async function test_different_targets() {
  let [importedLogin] = await maybeImportLogins([
    {
      username: USER1,
      password: PASS1,
      origin: HOST1,
      formActionOrigin: HOST1,
    },
  ]);
  Assert.ok(importedLogin, "Return value should indicate imported login.");
  let matchingLogins = LoginHelper.searchLoginsWithObject({ origin: HOST1 });
  Assert.equal(
    matchingLogins.length,
    1,
    `There should be 1 login for ${HOST1}`
  );

  // Not passing either a formActionOrigin or a httpRealm should be treated as
  // the same as the previous login
  [importedLogin] = await maybeImportLogins([
    {
      username: USER1,
      password: PASS1,
      origin: HOST1,
    },
  ]);
  Assert.ok(
    !importedLogin,
    "Return value should NOT indicate imported login " +
      "(because a missing formActionOrigin and httpRealm should be duped to the existing login)."
  );
  matchingLogins = LoginHelper.searchLoginsWithObject({ origin: HOST1 });
  Assert.equal(
    matchingLogins.length,
    1,
    `There should still be 1 login for ${HOST1}`
  );
  Assert.equal(
    matchingLogins[0].formActionOrigin,
    HOST1,
    "The form submission URL should have been kept."
  );

  [importedLogin] = await maybeImportLogins([
    {
      username: USER1,
      password: PASS1,
      origin: HOST1,
      httpRealm: HOST1,
    },
  ]);
  Assert.ok(
    importedLogin,
    "Return value should indicate another imported login " +
      "as an httpRealm login shouldn't be duped."
  );
  matchingLogins = LoginHelper.searchLoginsWithObject({ origin: HOST1 });
  Assert.equal(
    matchingLogins.length,
    2,
    `There should now be 2 logins for ${HOST1}`
  );

  Services.logins.removeAllUserFacingLogins();
});