summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_deleteLoginsBackup.js
blob: 608329f4827d1e2bcdd99caa0905cbf52a08b041 (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
/**
 * Test that logins backup is deleted as expected when logins are deleted.
 */

XPCOMUtils.defineLazyModuleGetters(this, {
  FXA_PWDMGR_HOST: "resource://gre/modules/FxAccountsCommon.js",
  FXA_PWDMGR_REALM: "resource://gre/modules/FxAccountsCommon.js",
});

const nsLoginInfo = new Components.Constructor(
  "@mozilla.org/login-manager/loginInfo;1",
  Ci.nsILoginInfo,
  "init"
);

const login1 = new nsLoginInfo(
  "https://example.com",
  "https://example.com",
  null,
  "notifyu1",
  "notifyp1",
  "user",
  "pass"
);
const login2 = new nsLoginInfo(
  "https://example.com",
  "https://example.com",
  null,
  "",
  "notifyp1",
  "",
  "pass"
);

const fxaKey = new nsLoginInfo(
  FXA_PWDMGR_HOST,
  null,
  FXA_PWDMGR_REALM,
  "foo@bar.com",
  "pass2",
  "",
  ""
);

const loginStorePath = PathUtils.join(PathUtils.profileDir, "logins.json");
const loginBackupPath = PathUtils.join(
  PathUtils.profileDir,
  "logins-backup.json"
);

async function waitForBackupUpdate() {
  return new Promise(resolve => {
    Services.obs.addObserver(function observer(subject, topic) {
      Services.obs.removeObserver(observer, "logins-backup-updated");
      resolve();
    }, "logins-backup-updated");
  });
}

async function loginStoreExists() {
  return TestUtils.waitForCondition(() => IOUtils.exists(loginStorePath));
}

async function loginBackupExists() {
  return TestUtils.waitForCondition(() => IOUtils.exists(loginBackupPath));
}

async function loginBackupDeleted() {
  return TestUtils.waitForCondition(
    async () => !(await IOUtils.exists(loginBackupPath))
  );
}

// If a fxa key is stored as a login, test that logins backup is updated to only store
// the fxa key when the last user facing login is deleted.
add_task(
  async function test_deleteLoginsBackup_removeAllUserFacingLogins_fxaKey() {
    info(
      "Testing removeAllUserFacingLogins() case when there is a saved fxa key"
    );
    info("Adding two logins: fxa key and one user facing login");
    let storageUpdatePromise = TestUtils.topicObserved(
      "password-storage-updated"
    );
    await Services.logins.addLoginAsync(login1);
    Assert.ok(true, "added login1");
    await loginStoreExists();
    await Services.logins.addLoginAsync(fxaKey);
    Assert.ok(true, "added fxaKey");
    await loginBackupExists();
    Assert.ok(true, "logins-backup.json now exists");
    await storageUpdatePromise;
    info("Writes to storage are complete for addLogin calls");

    storageUpdatePromise = TestUtils.topicObserved("password-storage-updated");
    info("Removing all user facing logins");
    Services.logins.removeAllUserFacingLogins();
    await storageUpdatePromise;
    info("Writes to storage are complete after removeAllUserFacingLogins call");
    await waitForBackupUpdate();
    Assert.ok(
      true,
      "logins-backup.json was updated to only store the fxa key, as expected"
    );

    // Clean up.
    // Since there is a fxa key left, we need to call removeAllLogins() or removeLogin(fxaKey)
    // to remove the fxa key. Otherwise the test will fail in verify mode when trying to add login1
    Services.logins.removeAllLogins();
    await IOUtils.remove(loginStorePath);
  }
);

// Test that logins backup is deleted when Services.logins.removeAllUserFacingLogins() is called.
add_task(async function test_deleteLoginsBackup_removeAllUserFacingLogins() {
  // Remove logins.json and logins-backup.json before starting.
  info("Testing the removeAllUserFacingLogins() case");

  await IOUtils.remove(loginStorePath, { ignoreAbsent: true });
  await IOUtils.remove(loginBackupPath, { ignoreAbsent: true });

  let storageUpdatePromise = TestUtils.topicObserved(
    "password-storage-updated"
  );
  info("Add a login to create logins.json");
  await Services.logins.addLoginAsync(login1);
  await loginStoreExists();
  Assert.ok(true, "logins.json now exists");

  info("Add a second login to create logins-backup.json");
  await Services.logins.addLoginAsync(login2);
  await loginBackupExists();
  info("logins-backup.json now exists");

  await storageUpdatePromise;
  info("Writes to storage are complete for addLogin calls");

  storageUpdatePromise = TestUtils.topicObserved("password-storage-updated");
  info("Removing all user facing logins");
  Services.logins.removeAllUserFacingLogins();

  await storageUpdatePromise;
  info(
    "Writes to storage are complete when removeAllUserFacingLogins() is called"
  );
  await loginBackupDeleted();
  info(
    "logins-backup.json was deleted as expected when all logins were removed"
  );

  // Clean up.
  await IOUtils.remove(loginStorePath);
});

// 1. Test that logins backup is deleted when Services.logins.removeAllLogins() is called
// 2. If a FxA key is stored as a login, test that logins backup is deleted when
//   Services.logins.removeAllLogins() is called
add_task(async function test_deleteLoginsBackup_removeAllLogins() {
  // Remove logins.json and logins-backup.json before starting.
  info("Testing the removeAllLogins() case");

  await IOUtils.remove(loginStorePath, { ignoreAbsent: true });
  await IOUtils.remove(loginBackupPath, { ignoreAbsent: true });

  let storageUpdatePromise = TestUtils.topicObserved(
    "password-storage-updated"
  );
  info("Add a login to create logins.json");
  await Services.logins.addLoginAsync(login1);
  Assert.ok(true, "added login1");
  await loginStoreExists();
  Assert.ok(true, "logins.json now exists");
  await Services.logins.addLoginAsync(login2);
  Assert.ok(true, "added login2");
  await loginBackupExists();
  info("logins-backup.json now exists");

  await storageUpdatePromise;
  info("Writes to storage are complete for addLogin calls");

  storageUpdatePromise = TestUtils.topicObserved("password-storage-updated");
  info("Removing all logins");
  Services.logins.removeAllLogins();

  await storageUpdatePromise;
  info("Writes to storage are complete when removeAllLogins() is called");

  await loginBackupDeleted();
  info(
    "logins-backup.json was deleted as expected when all logins were removed"
  );
  await IOUtils.remove(loginStorePath);

  info("Testing the removeAllLogins() case when FxA key is present");
  storageUpdatePromise = TestUtils.topicObserved("password-storage-updated");
  await Services.logins.addLoginAsync(login1);
  await loginStoreExists();
  await Services.logins.addLoginAsync(fxaKey);
  await loginBackupExists();
  info("logins-backup.json now exists");
  await storageUpdatePromise;
  info("Write to storage are complete for addLogin calls");

  storageUpdatePromise = TestUtils.topicObserved("password-storage-updated");
  info("Removing all logins, including FxA key");
  Services.logins.removeAllLogins();
  await storageUpdatePromise;
  info("Writes to storage are complete after the last removeAllLogins call");
  await loginBackupDeleted();
  info(
    "logins-backup.json was deleted when the last logins were removed, as expected"
  );

  // Clean up.
  await IOUtils.remove(loginStorePath);
});

// 1. Test that logins backup is deleted when the last saved login is removed using
//    Services.logins.removeLogin() when no fxa key is saved.
// 2. Test that logins backup is updated when the last saved login is removed using
//    Services.logins.removeLogin() when a fxa key is present.
add_task(async function test_deleteLoginsBackup_removeLogin() {
  info("Testing the removeLogin() case when there is no saved fxa key");
  info("Adding two logins");
  let storageUpdatePromise = TestUtils.topicObserved(
    "password-storage-updated"
  );
  await Services.logins.addLoginAsync(login1);
  await loginStoreExists();
  await Services.logins.addLoginAsync(login2);
  await loginBackupExists();
  info("logins-backup.json now exists");

  await storageUpdatePromise;
  info("Writes to storage are complete for addLogin calls");

  storageUpdatePromise = TestUtils.topicObserved("password-storage-updated");
  info("Removing one login");
  Services.logins.removeLogin(login1);
  await storageUpdatePromise;
  info("Writes to storage are complete after one removeLogin call");
  await loginBackupExists();

  storageUpdatePromise = TestUtils.topicObserved("password-storage-updated");
  info("Removing the last login");
  Services.logins.removeLogin(login2);
  await storageUpdatePromise;
  info("Writes to storage are complete after the last removeLogin call");
  await loginBackupDeleted();
  info(
    "logins-backup.json was deleted as expected when the last saved login was removed"
  );
  await IOUtils.remove(loginStorePath);

  info("Testing the removeLogin() case when there is a saved fxa key");
  info("Adding two logins: one user facing and the fxa key");
  storageUpdatePromise = TestUtils.topicObserved("password-storage-updated");
  await Services.logins.addLoginAsync(login1);
  await loginStoreExists();
  await Services.logins.addLoginAsync(fxaKey);
  await loginBackupExists();
  info("logins-backup.json now exists");

  await storageUpdatePromise;
  info("Writes to storage are complete for addLogin calls");

  storageUpdatePromise = TestUtils.topicObserved("password-storage-updated");
  let backupUpdate = waitForBackupUpdate();
  Services.logins.removeLogin(login1);
  await storageUpdatePromise;
  info("Writes to storage are complete after one removeLogin call");
  await backupUpdate;

  await loginBackupExists();
  info("logins-backup.json was updated to contain only the fxa key");

  // Clean up.
  // Since there is a fxa key left, we need to call removeAllLogins() or removeLogin(fxaKey)
  // to remove the fxa key. Otherwise the test will fail in verify mode when trying to add login1
  Services.logins.removeAllLogins();
  await IOUtils.remove(loginStorePath);
});