summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/unit/test_LoginManagerParent_getGeneratedPassword.js
blob: 8dd8b17dfc0b336fd9f3a06de343dbd9db1ba61f (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
/**
 * Test LoginManagerParent.getGeneratedPassword()
 */

"use strict";

const { sinon } = ChromeUtils.import("resource://testing-common/Sinon.jsm");
const { LoginManagerParent } = ChromeUtils.import(
  "resource://gre/modules/LoginManagerParent.jsm"
);

add_task(async function test_getGeneratedPassword() {
  // Force the feature to be enabled.
  Services.prefs.setBoolPref("signon.generation.available", true);
  Services.prefs.setBoolPref("signon.generation.enabled", true);

  let LMP = new LoginManagerParent();
  LMP.useBrowsingContext(99);

  ok(LMP.getGeneratedPassword, "LMP.getGeneratedPassword exists");
  equal(
    LoginManagerParent.getGeneratedPasswordsByPrincipalOrigin().size,
    0,
    "Empty cache to start"
  );

  equal(LMP.getGeneratedPassword(), null, "Null with no BrowsingContext");

  ok(
    LoginManagerParent._browsingContextGlobal,
    "Check _browsingContextGlobal exists"
  );
  ok(
    !LoginManagerParent._browsingContextGlobal.get(99),
    "BrowsingContext 99 shouldn't exist yet"
  );
  info("Stubbing BrowsingContext.get(99)");
  sinon
    .stub(LoginManagerParent._browsingContextGlobal, "get")
    .withArgs(99)
    .callsFake(() => {
      return {
        currentWindowGlobal: {
          documentPrincipal: Services.scriptSecurityManager.createContentPrincipalFromOrigin(
            "https://www.example.com^userContextId=6"
          ),
        },
      };
    });
  ok(
    LoginManagerParent._browsingContextGlobal.get(99),
    "Checking BrowsingContext.get(99) stub"
  );

  let password1 = LMP.getGeneratedPassword();
  notEqual(password1, null, "Check password was returned");
  equal(
    password1.length,
    LoginTestUtils.generation.LENGTH,
    "Check password length"
  );
  equal(
    LoginManagerParent.getGeneratedPasswordsByPrincipalOrigin().size,
    1,
    "1 added to cache"
  );
  equal(
    LoginManagerParent.getGeneratedPasswordsByPrincipalOrigin().get(
      "https://www.example.com^userContextId=6"
    ).value,
    password1,
    "Cache key and value"
  );
  let password2 = LMP.getGeneratedPassword();
  equal(
    password1,
    password2,
    "Same password should be returned for the same origin"
  );

  info("Changing the documentPrincipal to simulate a navigation in the frame");
  LoginManagerParent._browsingContextGlobal.get.restore();
  sinon
    .stub(LoginManagerParent._browsingContextGlobal, "get")
    .withArgs(99)
    .callsFake(() => {
      return {
        currentWindowGlobal: {
          documentPrincipal: Services.scriptSecurityManager.createContentPrincipalFromOrigin(
            "https://www.mozilla.org^userContextId=2"
          ),
        },
      };
    });
  let password3 = LMP.getGeneratedPassword();
  notEqual(
    password2,
    password3,
    "Different password for a different origin for the same BC"
  );
  equal(
    password3.length,
    LoginTestUtils.generation.LENGTH,
    "Check password3 length"
  );

  info("Now checks cases where null should be returned");

  Services.prefs.setBoolPref("signon.rememberSignons", false);
  equal(LMP.getGeneratedPassword(), null, "Prevented when pwmgr disabled");
  Services.prefs.setBoolPref("signon.rememberSignons", true);

  Services.prefs.setBoolPref("signon.generation.available", false);
  equal(LMP.getGeneratedPassword(), null, "Prevented when unavailable");
  Services.prefs.setBoolPref("signon.generation.available", true);

  Services.prefs.setBoolPref("signon.generation.enabled", false);
  equal(LMP.getGeneratedPassword(), null, "Prevented when disabled");
  Services.prefs.setBoolPref("signon.generation.enabled", true);

  LMP.useBrowsingContext(123);
  equal(
    LMP.getGeneratedPassword(),
    null,
    "Prevented when browsingContext is missing"
  );
});