summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/unit/test_logins_metainfo.js
blob: 8f31fba02b823f06e2b00cef52cd8921ddbad0c3 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests the handling of nsILoginMetaInfo by methods that add, remove, modify,
 * and find logins.
 */

"use strict";

// Globals

const gLooksLikeUUIDRegex = /^\{\w{8}-\w{4}-\w{4}-\w{4}-\w{12}\}$/;

/**
 * Retrieves the only login among the current data that matches the origin of
 * the given nsILoginInfo.  In case there is more than one login for the
 * origin, the test fails.
 */
function retrieveLoginMatching(aLoginInfo) {
  let logins = Services.logins.findLogins(aLoginInfo.origin, "", "");
  Assert.equal(logins.length, 1);
  return logins[0].QueryInterface(Ci.nsILoginMetaInfo);
}

/**
 * Checks that the nsILoginInfo and nsILoginMetaInfo properties of two different
 * login instances are equal.
 */
function assertMetaInfoEqual(aActual, aExpected) {
  Assert.notEqual(aActual, aExpected);

  // Check the nsILoginInfo properties.
  Assert.ok(aActual.equals(aExpected));

  // Check the nsILoginMetaInfo properties.
  Assert.equal(aActual.guid, aExpected.guid);
  Assert.equal(aActual.timeCreated, aExpected.timeCreated);
  Assert.equal(aActual.timeLastUsed, aExpected.timeLastUsed);
  Assert.equal(aActual.timePasswordChanged, aExpected.timePasswordChanged);
  Assert.equal(aActual.timesUsed, aExpected.timesUsed);
}

/**
 * nsILoginInfo instances with or without nsILoginMetaInfo properties.
 */
let gLoginInfo1;
let gLoginInfo2;
let gLoginInfo3;

/**
 * nsILoginInfo instances reloaded with all the nsILoginMetaInfo properties.
 * These are often used to provide the reference values to test against.
 */
let gLoginMetaInfo1;
let gLoginMetaInfo2;
let gLoginMetaInfo3;

// Tests

/**
 * Prepare the test objects that will be used by the following tests.
 */
add_task(function test_initialize() {
  // Use a reference time from ten minutes ago to initialize one instance of
  // nsILoginMetaInfo, to test that reference times are updated when needed.
  let baseTimeMs = Date.now() - 600000;

  gLoginInfo1 = TestData.formLogin();
  gLoginInfo2 = TestData.formLogin({
    origin: "http://other.example.com",
    guid: Services.uuid.generateUUID().toString(),
    timeCreated: baseTimeMs,
    timeLastUsed: baseTimeMs + 2,
    timePasswordChanged: baseTimeMs + 1,
    timesUsed: 2,
  });
  gLoginInfo3 = TestData.authLogin();
});

/**
 * Tests the behavior of addLogin with regard to metadata.  The logins added
 * here are also used by the following tests.
 */
add_task(async function test_addLogin_metainfo() {
  // Add a login without metadata to the database.
  await Services.logins.addLoginAsync(gLoginInfo1);

  // The object provided to addLogin should not have been modified.
  Assert.equal(gLoginInfo1.guid, null);
  Assert.equal(gLoginInfo1.timeCreated, 0);
  Assert.equal(gLoginInfo1.timeLastUsed, 0);
  Assert.equal(gLoginInfo1.timePasswordChanged, 0);
  Assert.equal(gLoginInfo1.timesUsed, 0);

  // A login with valid metadata should have been stored.
  gLoginMetaInfo1 = retrieveLoginMatching(gLoginInfo1);
  Assert.ok(gLooksLikeUUIDRegex.test(gLoginMetaInfo1.guid));
  let creationTime = gLoginMetaInfo1.timeCreated;
  LoginTestUtils.assertTimeIsAboutNow(creationTime);
  Assert.equal(gLoginMetaInfo1.timeLastUsed, creationTime);
  Assert.equal(gLoginMetaInfo1.timePasswordChanged, creationTime);
  Assert.equal(gLoginMetaInfo1.timesUsed, 1);

  // Add a login without metadata to the database.
  let originalLogin = gLoginInfo2.clone().QueryInterface(Ci.nsILoginMetaInfo);
  await Services.logins.addLoginAsync(gLoginInfo2);

  // The object provided to addLogin should not have been modified.
  assertMetaInfoEqual(gLoginInfo2, originalLogin);

  // A login with the provided metadata should have been stored.
  gLoginMetaInfo2 = retrieveLoginMatching(gLoginInfo2);
  assertMetaInfoEqual(gLoginMetaInfo2, gLoginInfo2);

  // Add an authentication login to the database before continuing.
  await Services.logins.addLoginAsync(gLoginInfo3);
  gLoginMetaInfo3 = retrieveLoginMatching(gLoginInfo3);
  LoginTestUtils.checkLogins([gLoginInfo1, gLoginInfo2, gLoginInfo3]);
});

/**
 * Tests that adding a login with a duplicate GUID throws an exception.
 */
add_task(async function test_addLogin_metainfo_duplicate() {
  let loginInfo = TestData.formLogin({
    origin: "http://duplicate.example.com",
    guid: gLoginMetaInfo2.guid,
  });
  await Assert.rejects(
    Services.logins.addLoginAsync(loginInfo),
    /specified GUID already exists/
  );

  // Verify that no data was stored by the previous call.
  LoginTestUtils.checkLogins([gLoginInfo1, gLoginInfo2, gLoginInfo3]);
});

/**
 * Tests that the existing metadata is not changed when modifyLogin is called
 * with an nsILoginInfo argument.
 */
add_task(function test_modifyLogin_nsILoginInfo_metainfo_ignored() {
  let newLoginInfo = gLoginInfo1.clone().QueryInterface(Ci.nsILoginMetaInfo);
  newLoginInfo.guid = Services.uuid.generateUUID().toString();
  newLoginInfo.timeCreated = Date.now();
  newLoginInfo.timeLastUsed = Date.now();
  newLoginInfo.timePasswordChanged = Date.now();
  newLoginInfo.timesUsed = 12;
  Services.logins.modifyLogin(gLoginInfo1, newLoginInfo);

  newLoginInfo = retrieveLoginMatching(gLoginInfo1);
  assertMetaInfoEqual(newLoginInfo, gLoginMetaInfo1);
});

/**
 * Tests the modifyLogin function with an nsIProperyBag argument.
 */
add_task(function test_modifyLogin_nsIProperyBag_metainfo() {
  // Use a new reference time that is two minutes from now.
  let newTimeMs = Date.now() + 120000;
  let newUUIDValue = Services.uuid.generateUUID().toString();

  // Check that properties are changed as requested.
  Services.logins.modifyLogin(
    gLoginInfo1,
    newPropertyBag({
      guid: newUUIDValue,
      timeCreated: newTimeMs,
      timeLastUsed: newTimeMs + 2,
      timePasswordChanged: newTimeMs + 1,
      timesUsed: 2,
    })
  );

  gLoginMetaInfo1 = retrieveLoginMatching(gLoginInfo1);
  Assert.equal(gLoginMetaInfo1.guid, newUUIDValue);
  Assert.equal(gLoginMetaInfo1.timeCreated, newTimeMs);
  Assert.equal(gLoginMetaInfo1.timeLastUsed, newTimeMs + 2);
  Assert.equal(gLoginMetaInfo1.timePasswordChanged, newTimeMs + 1);
  Assert.equal(gLoginMetaInfo1.timesUsed, 2);

  // Check that timePasswordChanged is updated when changing the password.
  let originalLogin = gLoginInfo2.clone().QueryInterface(Ci.nsILoginMetaInfo);
  Services.logins.modifyLogin(
    gLoginInfo2,
    newPropertyBag({
      password: "new password",
    })
  );
  gLoginInfo2.password = "new password";

  gLoginMetaInfo2 = retrieveLoginMatching(gLoginInfo2);
  Assert.equal(gLoginMetaInfo2.password, gLoginInfo2.password);
  Assert.equal(gLoginMetaInfo2.timeCreated, originalLogin.timeCreated);
  Assert.equal(gLoginMetaInfo2.timeLastUsed, originalLogin.timeLastUsed);
  LoginTestUtils.assertTimeIsAboutNow(gLoginMetaInfo2.timePasswordChanged);

  // Check that timePasswordChanged is not set to the current time when changing
  // the password and specifying a new value for the property at the same time.
  Services.logins.modifyLogin(
    gLoginInfo2,
    newPropertyBag({
      password: "other password",
      timePasswordChanged: newTimeMs,
    })
  );
  gLoginInfo2.password = "other password";

  gLoginMetaInfo2 = retrieveLoginMatching(gLoginInfo2);
  Assert.equal(gLoginMetaInfo2.password, gLoginInfo2.password);
  Assert.equal(gLoginMetaInfo2.timeCreated, originalLogin.timeCreated);
  Assert.equal(gLoginMetaInfo2.timeLastUsed, originalLogin.timeLastUsed);
  Assert.equal(gLoginMetaInfo2.timePasswordChanged, newTimeMs);

  // Check the special timesUsedIncrement property.
  Services.logins.modifyLogin(
    gLoginInfo2,
    newPropertyBag({
      timesUsedIncrement: 2,
    })
  );

  gLoginMetaInfo2 = retrieveLoginMatching(gLoginInfo2);
  Assert.equal(gLoginMetaInfo2.timeCreated, originalLogin.timeCreated);
  Assert.equal(gLoginMetaInfo2.timeLastUsed, originalLogin.timeLastUsed);
  Assert.equal(gLoginMetaInfo2.timePasswordChanged, newTimeMs);
  Assert.equal(gLoginMetaInfo2.timesUsed, 4);
});

/**
 * Tests that modifying a login to a duplicate GUID throws an exception.
 */
add_task(function test_modifyLogin_nsIProperyBag_metainfo_duplicate() {
  Assert.throws(
    () =>
      Services.logins.modifyLogin(
        gLoginInfo1,
        newPropertyBag({
          guid: gLoginInfo2.guid,
        })
      ),
    /specified GUID already exists/
  );
  LoginTestUtils.checkLogins([gLoginInfo1, gLoginInfo2, gLoginInfo3]);
});

/**
 * Tests searching logins using nsILoginMetaInfo properties.
 */
add_task(function test_searchLogins_metainfo() {
  // Find by GUID.
  let logins = Services.logins.searchLogins(
    newPropertyBag({
      guid: gLoginMetaInfo1.guid,
    })
  );
  Assert.equal(logins.length, 1);
  let foundLogin = logins[0].QueryInterface(Ci.nsILoginMetaInfo);
  assertMetaInfoEqual(foundLogin, gLoginMetaInfo1);

  // Find by timestamp.
  logins = Services.logins.searchLogins(
    newPropertyBag({
      timePasswordChanged: gLoginMetaInfo2.timePasswordChanged,
    })
  );
  Assert.equal(logins.length, 1);
  foundLogin = logins[0].QueryInterface(Ci.nsILoginMetaInfo);
  assertMetaInfoEqual(foundLogin, gLoginMetaInfo2);

  // Find using two properties at the same time.
  logins = Services.logins.searchLogins(
    newPropertyBag({
      guid: gLoginMetaInfo3.guid,
      timePasswordChanged: gLoginMetaInfo3.timePasswordChanged,
    })
  );
  Assert.equal(logins.length, 1);
  foundLogin = logins[0].QueryInterface(Ci.nsILoginMetaInfo);
  assertMetaInfoEqual(foundLogin, gLoginMetaInfo3);
});

/**
 * Tests that the default nsILoginManagerStorage module attached to the Login
 * Manager service is able to save and reload nsILoginMetaInfo properties.
 */
add_task(async function test_storage_metainfo() {
  await LoginTestUtils.reloadData();
  LoginTestUtils.checkLogins([gLoginInfo1, gLoginInfo2, gLoginInfo3]);

  assertMetaInfoEqual(retrieveLoginMatching(gLoginInfo1), gLoginMetaInfo1);
  assertMetaInfoEqual(retrieveLoginMatching(gLoginInfo2), gLoginMetaInfo2);
  assertMetaInfoEqual(retrieveLoginMatching(gLoginInfo3), gLoginMetaInfo3);
});