summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/xpcshell/test_ext_browsingData_passwords.js
blob: d39b5df05f79a08333a85d98a4cdf0d7666fbf2e (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

const REFERENCE_DATE = Date.now();
const LOGIN_USERNAME = "username";
const LOGIN_PASSWORD = "password";
const OLD_HOST = "http://mozilla.org";
const NEW_HOST = "http://mozilla.com";
const FXA_HOST = "chrome://FirefoxAccounts";

function checkLoginExists(host, shouldExist) {
  const logins = Services.logins.findLogins(host, "", null);
  equal(
    logins.length,
    shouldExist ? 1 : 0,
    `Login was ${shouldExist ? "" : "not "} found.`
  );
}

async function addLogin(host, timestamp) {
  checkLoginExists(host, false);
  let login = Cc["@mozilla.org/login-manager/loginInfo;1"].createInstance(
    Ci.nsILoginInfo
  );
  login.init(host, "", null, LOGIN_USERNAME, LOGIN_PASSWORD);
  login.QueryInterface(Ci.nsILoginMetaInfo);
  login.timePasswordChanged = timestamp;
  await Services.logins.addLoginAsync(login);
  checkLoginExists(host, true);
}

async function setupPasswords() {
  Services.logins.removeAllUserFacingLogins();
  await addLogin(FXA_HOST, REFERENCE_DATE);
  await addLogin(NEW_HOST, REFERENCE_DATE);
  await addLogin(OLD_HOST, REFERENCE_DATE - 10000);
}

add_task(async function testPasswords() {
  function background() {
    browser.test.onMessage.addListener(async (msg, options) => {
      if (msg == "removeHistory") {
        await browser.browsingData.removePasswords(options);
      } else {
        await browser.browsingData.remove(options, { passwords: true });
      }
      browser.test.sendMessage("passwordsRemoved");
    });
  }

  const extension = ExtensionTestUtils.loadExtension({
    background,
    manifest: {
      permissions: ["browsingData"],
    },
  });

  async function testRemovalMethod(method) {
    // Clear passwords with no since value.
    await setupPasswords();
    extension.sendMessage(method, {});
    await extension.awaitMessage("passwordsRemoved");

    checkLoginExists(OLD_HOST, false);
    checkLoginExists(NEW_HOST, false);
    checkLoginExists(FXA_HOST, true);

    // Clear passwords with recent since value.
    await setupPasswords();
    extension.sendMessage(method, { since: REFERENCE_DATE - 1000 });
    await extension.awaitMessage("passwordsRemoved");

    checkLoginExists(OLD_HOST, true);
    checkLoginExists(NEW_HOST, false);
    checkLoginExists(FXA_HOST, true);

    // Clear passwords with old since value.
    await setupPasswords();
    extension.sendMessage(method, { since: REFERENCE_DATE - 20000 });
    await extension.awaitMessage("passwordsRemoved");

    checkLoginExists(OLD_HOST, false);
    checkLoginExists(NEW_HOST, false);
    checkLoginExists(FXA_HOST, true);
  }

  await extension.startup();

  await testRemovalMethod("removePasswords");
  await testRemovalMethod("remove");

  await extension.unload();
});