summaryrefslogtreecommitdiffstats
path: root/browser/components/migration/tests/unit/test_Chrome_passwords.js
blob: 16f98e7038e0ab1d05f93b58f9522ff169fb53cb (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
369
370
371
372
373
"use strict";

const PROFILE = {
  id: "Default",
  name: "Person 1",
};

const TEST_LOGINS = [
  {
    id: 1, // id of the row in the chrome login db
    username: "username",
    password: "password",
    origin: "https://c9.io",
    formActionOrigin: "https://c9.io",
    httpRealm: null,
    usernameField: "inputEmail",
    passwordField: "inputPassword",
    timeCreated: 1437418416037,
    timePasswordChanged: 1437418416037,
    timesUsed: 1,
  },
  {
    id: 2,
    username: "username@gmail.com",
    password: "password2",
    origin: "https://accounts.google.com",
    formActionOrigin: "https://accounts.google.com",
    httpRealm: null,
    usernameField: "Email",
    passwordField: "Passwd",
    timeCreated: 1437418446598,
    timePasswordChanged: 1437418446598,
    timesUsed: 6,
  },
  {
    id: 3,
    username: "username",
    password: "password3",
    origin: "https://www.facebook.com",
    formActionOrigin: "https://www.facebook.com",
    httpRealm: null,
    usernameField: "email",
    passwordField: "pass",
    timeCreated: 1437418478851,
    timePasswordChanged: 1437418478851,
    timesUsed: 1,
  },
  {
    id: 4,
    username: "user",
    password: "اقرأPÀßwörd",
    origin: "http://httpbin.org",
    formActionOrigin: null,
    httpRealm: "me@kennethreitz.com", // Digest auth.
    usernameField: "",
    passwordField: "",
    timeCreated: 1437787462368,
    timePasswordChanged: 1437787462368,
    timesUsed: 1,
  },
  {
    id: 5,
    username: "buser",
    password: "bpassword",
    origin: "http://httpbin.org",
    formActionOrigin: null,
    httpRealm: "Fake Realm", // Basic auth.
    usernameField: "",
    passwordField: "",
    timeCreated: 1437787539233,
    timePasswordChanged: 1437787539233,
    timesUsed: 1,
  },
  {
    id: 6,
    username: "username",
    password: "password6",
    origin: "https://www.example.com",
    formActionOrigin: "", // NULL `action_url`
    httpRealm: null,
    usernameField: "",
    passwordField: "pass",
    timeCreated: 1557291348878,
    timePasswordChanged: 1557291348878,
    timesUsed: 1,
  },
  {
    id: 7,
    version: "v10",
    username: "username",
    password: "password",
    origin: "https://v10.io",
    formActionOrigin: "https://v10.io",
    httpRealm: null,
    usernameField: "inputEmail",
    passwordField: "inputPassword",
    timeCreated: 1437418416037,
    timePasswordChanged: 1437418416037,
    timesUsed: 1,
  },
];

var loginCrypto;
var dbConn;

async function promiseSetPassword(login) {
  const encryptedString = await loginCrypto.encryptData(
    login.password,
    login.version
  );
  info(`promiseSetPassword: ${encryptedString}`);
  const passwordValue = new Uint8Array(
    loginCrypto.stringToArray(encryptedString)
  );
  return dbConn.execute(
    `UPDATE logins
                         SET password_value = :password_value
                         WHERE rowid = :rowid
                        `,
    { password_value: passwordValue, rowid: login.id }
  );
}

function checkLoginsAreEqual(passwordManagerLogin, chromeLogin, id) {
  passwordManagerLogin.QueryInterface(Ci.nsILoginMetaInfo);

  Assert.equal(
    passwordManagerLogin.username,
    chromeLogin.username,
    "The two logins ID " + id + " have the same username"
  );
  Assert.equal(
    passwordManagerLogin.password,
    chromeLogin.password,
    "The two logins ID " + id + " have the same password"
  );
  Assert.equal(
    passwordManagerLogin.origin,
    chromeLogin.origin,
    "The two logins ID " + id + " have the same origin"
  );
  Assert.equal(
    passwordManagerLogin.formActionOrigin,
    chromeLogin.formActionOrigin,
    "The two logins ID " + id + " have the same formActionOrigin"
  );
  Assert.equal(
    passwordManagerLogin.httpRealm,
    chromeLogin.httpRealm,
    "The two logins ID " + id + " have the same httpRealm"
  );
  Assert.equal(
    passwordManagerLogin.usernameField,
    chromeLogin.usernameField,
    "The two logins ID " + id + " have the same usernameElement"
  );
  Assert.equal(
    passwordManagerLogin.passwordField,
    chromeLogin.passwordField,
    "The two logins ID " + id + " have the same passwordElement"
  );
  Assert.equal(
    passwordManagerLogin.timeCreated,
    chromeLogin.timeCreated,
    "The two logins ID " + id + " have the same timeCreated"
  );
  Assert.equal(
    passwordManagerLogin.timePasswordChanged,
    chromeLogin.timePasswordChanged,
    "The two logins ID " + id + " have the same timePasswordChanged"
  );
  Assert.equal(
    passwordManagerLogin.timesUsed,
    chromeLogin.timesUsed,
    "The two logins ID " + id + " have the same timesUsed"
  );
}

function generateDifferentLogin(login) {
  const newLogin = Cc["@mozilla.org/login-manager/loginInfo;1"].createInstance(
    Ci.nsILoginInfo
  );

  newLogin.init(
    login.origin,
    login.formActionOrigin,
    null,
    login.username,
    login.password + 1,
    login.usernameField + 1,
    login.passwordField + 1
  );
  newLogin.QueryInterface(Ci.nsILoginMetaInfo);
  newLogin.timeCreated = login.timeCreated + 1;
  newLogin.timePasswordChanged = login.timePasswordChanged + 1;
  newLogin.timesUsed = login.timesUsed + 1;
  return newLogin;
}

add_task(async function setup() {
  let dirSvcPath;
  let pathId;
  let profilePathSegments;

  // Use a mock service and account name to avoid a Keychain auth. prompt that
  // would block the test from finishing if Chrome has already created a matching
  // Keychain entry. This allows us to still exercise the keychain lookup code.
  // The mock encryption passphrase is used when the Keychain item isn't found.
  const mockMacOSKeychain = {
    passphrase: "bW96aWxsYWZpcmVmb3g=",
    serviceName: "TESTING Chrome Safe Storage",
    accountName: "TESTING Chrome",
  };
  if (AppConstants.platform == "macosx") {
    const { ChromeMacOSLoginCrypto } = ChromeUtils.importESModule(
      "resource:///modules/ChromeMacOSLoginCrypto.sys.mjs"
    );
    loginCrypto = new ChromeMacOSLoginCrypto(
      mockMacOSKeychain.serviceName,
      mockMacOSKeychain.accountName,
      mockMacOSKeychain.passphrase
    );
    dirSvcPath = "Library/";
    pathId = "ULibDir";
    profilePathSegments = [
      "Application Support",
      "Google",
      "Chrome",
      "Default",
      "Login Data",
    ];
  } else if (AppConstants.platform == "win") {
    const { ChromeWindowsLoginCrypto } = ChromeUtils.importESModule(
      "resource:///modules/ChromeWindowsLoginCrypto.sys.mjs"
    );
    loginCrypto = new ChromeWindowsLoginCrypto("Chrome");
    dirSvcPath = "AppData/Local/";
    pathId = "LocalAppData";
    profilePathSegments = [
      "Google",
      "Chrome",
      "User Data",
      "Default",
      "Login Data",
    ];
  } else {
    throw new Error("Not implemented");
  }
  const dirSvcFile = do_get_file(dirSvcPath);
  registerFakePath(pathId, dirSvcFile);

  info(PathUtils.join(dirSvcFile.path, ...profilePathSegments));
  const loginDataFilePath = PathUtils.join(
    dirSvcFile.path,
    ...profilePathSegments
  );
  dbConn = await Sqlite.openConnection({ path: loginDataFilePath });

  if (AppConstants.platform == "macosx") {
    const migrator = await MigrationUtils.getMigrator("chrome");
    Object.assign(migrator, {
      _keychainServiceName: mockMacOSKeychain.serviceName,
      _keychainAccountName: mockMacOSKeychain.accountName,
      _keychainMockPassphrase: mockMacOSKeychain.passphrase,
    });
  }

  registerCleanupFunction(() => {
    Services.logins.removeAllUserFacingLogins();
    if (loginCrypto.finalize) {
      loginCrypto.finalize();
    }
    return dbConn.close();
  });
});

add_task(async function test_importIntoEmptyDB() {
  for (const login of TEST_LOGINS) {
    await promiseSetPassword(login);
  }

  const migrator = await MigrationUtils.getMigrator("chrome");
  Assert.ok(
    await migrator.isSourceAvailable(),
    "Sanity check the source exists"
  );

  let logins = Services.logins.getAllLogins();
  Assert.equal(logins.length, 0, "There are no logins initially");

  // Migrate the logins.
  await promiseMigration(
    migrator,
    MigrationUtils.resourceTypes.PASSWORDS,
    PROFILE,
    true
  );

  logins = Services.logins.getAllLogins();
  Assert.equal(
    logins.length,
    TEST_LOGINS.length,
    "Check login count after importing the data"
  );
  Assert.equal(
    logins.length,
    MigrationUtils._importQuantities.logins,
    "Check telemetry matches the actual import."
  );

  for (let i = 0; i < TEST_LOGINS.length; i++) {
    checkLoginsAreEqual(logins[i], TEST_LOGINS[i], i + 1);
  }
});

// Test that existing logins for the same primary key don't get overwritten
add_task(async function test_importExistingLogins() {
  const migrator = await MigrationUtils.getMigrator("chrome");
  Assert.ok(
    await migrator.isSourceAvailable(),
    "Sanity check the source exists"
  );

  Services.logins.removeAllUserFacingLogins();
  let logins = Services.logins.getAllLogins();
  Assert.equal(
    logins.length,
    0,
    "There are no logins after removing all of them"
  );

  const newLogins = [];

  // Create 3 new logins that are different but where the key properties are still the same.
  for (let i = 0; i < 3; i++) {
    newLogins.push(generateDifferentLogin(TEST_LOGINS[i]));
    await Services.logins.addLoginAsync(newLogins[i]);
  }

  logins = Services.logins.getAllLogins();
  Assert.equal(
    logins.length,
    newLogins.length,
    "Check login count after the insertion"
  );

  for (let i = 0; i < newLogins.length; i++) {
    checkLoginsAreEqual(logins[i], newLogins[i], i + 1);
  }
  // Migrate the logins.
  await promiseMigration(
    migrator,
    MigrationUtils.resourceTypes.PASSWORDS,
    PROFILE,
    true
  );

  logins = Services.logins.getAllLogins();
  Assert.equal(
    logins.length,
    TEST_LOGINS.length,
    "Check there are still the same number of logins after re-importing the data"
  );
  Assert.equal(
    logins.length,
    MigrationUtils._importQuantities.logins,
    "Check telemetry matches the actual import."
  );

  for (let i = 0; i < newLogins.length; i++) {
    checkLoginsAreEqual(logins[i], newLogins[i], i + 1);
  }
});