summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/unit/test_LoginManagerParent_searchAndDedupeLogins.js
blob: 33ee5bd04c7df36a53019d7340d7c7fcb4c2a839 (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
/**
 * Test LoginManagerParent._searchAndDedupeLogins()
 */

"use strict";

const { LoginManagerParent: LMP } = ChromeUtils.importESModule(
  "resource://gre/modules/LoginManagerParent.sys.mjs"
);

const DOMAIN1_HTTP_ORIGIN = "http://www3.example.com";
const DOMAIN1_HTTPS_ORIGIN = "https://www3.example.com";

const DOMAIN1_HTTP_TO_HTTP_U1_P1 = TestData.formLogin({});
const DOMAIN1_HTTP_TO_HTTP_U2_P1 = TestData.formLogin({
  username: "user2",
});
const DOMAIN1_HTTP_TO_HTTP_U3_P1 = TestData.formLogin({
  username: "user3",
});
const DOMAIN1_HTTPS_TO_HTTPS_U1_P1 = TestData.formLogin({
  origin: DOMAIN1_HTTPS_ORIGIN,
  formActionOrigin: "https://login.example.com",
});
const DOMAIN1_HTTPS_TO_HTTPS_U2_P1 = TestData.formLogin({
  origin: DOMAIN1_HTTPS_ORIGIN,
  formActionOrigin: "https://login.example.com",
  username: "user2",
});
const DOMAIN1_HTTPS_TO_HTTPS_U1_P2 = TestData.formLogin({
  origin: DOMAIN1_HTTPS_ORIGIN,
  formActionOrigin: "https://login.example.com",
  password: "password two",
});
const DOMAIN1_HTTPS_TO_HTTPS_U1_P2_DIFFERENT_PORT = TestData.formLogin({
  origin: "https://www3.example.com:8080",
  password: "password two",
});
const DOMAIN1_HTTP_TO_HTTP_U1_P2 = TestData.formLogin({
  password: "password two",
});
const DOMAIN1_HTTP_TO_HTTP_U1_P1_DIFFERENT_PORT = TestData.formLogin({
  origin: "http://www3.example.com:8080",
});
const DOMAIN2_HTTP_TO_HTTP_U1_P1 = TestData.formLogin({
  origin: "http://different.example.com",
});
const DOMAIN2_HTTPS_TO_HTTPS_U1_P1 = TestData.formLogin({
  origin: "https://different.example.com",
  formActionOrigin: "https://login.example.com",
});

add_task(function setup() {
  // Not enabled by default in all.js:
  Services.prefs.setBoolPref("signon.schemeUpgrades", true);
});

add_task(async function test_searchAndDedupeLogins_acceptDifferentSubdomains() {
  let testcases = [
    {
      description: "HTTPS form, same hostPort, same username, different scheme",
      formActionOrigin: DOMAIN1_HTTPS_ORIGIN,
      logins: [DOMAIN1_HTTPS_TO_HTTPS_U1_P1, DOMAIN1_HTTP_TO_HTTP_U1_P1],
      expected: [DOMAIN1_HTTPS_TO_HTTPS_U1_P1],
    },
    {
      description: "HTTP form, same hostPort, same username, different scheme",
      formActionOrigin: DOMAIN1_HTTP_ORIGIN,
      logins: [DOMAIN1_HTTPS_TO_HTTPS_U1_P1, DOMAIN1_HTTP_TO_HTTP_U1_P1],
      expected: [DOMAIN1_HTTP_TO_HTTP_U1_P1],
    },
    {
      description: "HTTPS form, different passwords, different scheme",
      formActionOrigin: DOMAIN1_HTTPS_ORIGIN,
      logins: [DOMAIN1_HTTPS_TO_HTTPS_U1_P1, DOMAIN1_HTTP_TO_HTTP_U1_P2],
      expected: [DOMAIN1_HTTPS_TO_HTTPS_U1_P1],
    },
    {
      description: "HTTP form, different passwords, different scheme",
      formActionOrigin: DOMAIN1_HTTP_ORIGIN,
      logins: [DOMAIN1_HTTPS_TO_HTTPS_U1_P1, DOMAIN1_HTTP_TO_HTTP_U1_P2],
      expected: [DOMAIN1_HTTP_TO_HTTP_U1_P2],
    },
    {
      description: "HTTPS form, same origin, different port, both schemes",
      formActionOrigin: DOMAIN1_HTTPS_ORIGIN,
      logins: [
        DOMAIN1_HTTPS_TO_HTTPS_U1_P1,
        DOMAIN1_HTTP_TO_HTTP_U1_P1_DIFFERENT_PORT,
        DOMAIN1_HTTPS_TO_HTTPS_U1_P2_DIFFERENT_PORT,
      ],
      expected: [
        DOMAIN1_HTTPS_TO_HTTPS_U1_P1,
        DOMAIN1_HTTPS_TO_HTTPS_U1_P2_DIFFERENT_PORT,
      ],
    },
    {
      description: "HTTP form, same origin, different port, both schemes",
      formActionOrigin: DOMAIN1_HTTP_ORIGIN,
      logins: [
        DOMAIN1_HTTPS_TO_HTTPS_U1_P1,
        DOMAIN1_HTTP_TO_HTTP_U1_P1_DIFFERENT_PORT,
        DOMAIN1_HTTPS_TO_HTTPS_U1_P2_DIFFERENT_PORT,
      ],
      expected: [DOMAIN1_HTTP_TO_HTTP_U1_P1_DIFFERENT_PORT],
    },
    {
      description: "HTTPS form, different origin, different scheme",
      formActionOrigin: DOMAIN1_HTTPS_ORIGIN,
      logins: [DOMAIN1_HTTPS_TO_HTTPS_U1_P1, DOMAIN2_HTTP_TO_HTTP_U1_P1],
      expected: [DOMAIN1_HTTPS_TO_HTTPS_U1_P1],
    },
    {
      description:
        "HTTPS form, different origin, different scheme, same password, same hostPort preferred",
      formActionOrigin: DOMAIN1_HTTPS_ORIGIN,
      logins: [DOMAIN1_HTTP_TO_HTTP_U1_P1, DOMAIN2_HTTPS_TO_HTTPS_U1_P1],
      expected: [DOMAIN1_HTTP_TO_HTTP_U1_P1],
    },
    {
      description: "HTTP form, different origin, different scheme",
      formActionOrigin: DOMAIN1_HTTP_ORIGIN,
      logins: [DOMAIN1_HTTPS_TO_HTTPS_U1_P1, DOMAIN2_HTTP_TO_HTTP_U1_P1],
      expected: [DOMAIN2_HTTP_TO_HTTP_U1_P1],
    },
    {
      description: "HTTPS form, different username, different scheme",
      formActionOrigin: DOMAIN1_HTTPS_ORIGIN,
      logins: [DOMAIN1_HTTPS_TO_HTTPS_U1_P1, DOMAIN1_HTTP_TO_HTTP_U2_P1],
      expected: [DOMAIN1_HTTPS_TO_HTTPS_U1_P1, DOMAIN1_HTTP_TO_HTTP_U2_P1],
    },
    {
      description: "HTTP form, different username, different scheme",
      formActionOrigin: DOMAIN1_HTTP_ORIGIN,
      logins: [DOMAIN1_HTTPS_TO_HTTPS_U1_P1, DOMAIN1_HTTP_TO_HTTP_U2_P1],
      expected: [DOMAIN1_HTTP_TO_HTTP_U2_P1],
    },
    {
      description: "HTTPS form, different usernames, different schemes",
      formActionOrigin: DOMAIN1_HTTPS_ORIGIN,
      logins: [
        DOMAIN1_HTTPS_TO_HTTPS_U1_P2,
        DOMAIN1_HTTPS_TO_HTTPS_U2_P1,
        DOMAIN1_HTTP_TO_HTTP_U1_P1,
        DOMAIN1_HTTP_TO_HTTP_U3_P1,
      ],
      expected: [
        DOMAIN1_HTTPS_TO_HTTPS_U1_P2,
        DOMAIN1_HTTPS_TO_HTTPS_U2_P1,
        DOMAIN1_HTTP_TO_HTTP_U3_P1,
      ],
    },
  ];

  for (let tc of testcases) {
    info(tc.description);

    let guids = await Services.logins.addLogins(tc.logins);
    Assert.strictEqual(
      guids.length,
      tc.logins.length,
      "Check length of added logins"
    );

    let actual = await LMP.searchAndDedupeLogins(tc.formActionOrigin, {
      formActionOrigin: tc.formActionOrigin,
      looseActionOriginMatch: true,
      acceptDifferentSubdomains: true,
    });
    info(`actual:\n ${JSON.stringify(actual, null, 2)}`);
    info(`expected:\n ${JSON.stringify(tc.expected, null, 2)}`);
    Assert.strictEqual(
      actual.length,
      tc.expected.length,
      `Check result length`
    );
    for (let [i, login] of tc.expected.entries()) {
      Assert.ok(actual[i].equals(login), `Check index ${i}`);
    }

    Services.logins.removeAllUserFacingLogins();
  }
});

add_task(async function test_reject_duplicates() {
  const testcases = [
    {
      description: "HTTPS form, both https, same username, different password",
      formActionOrigin: DOMAIN1_HTTPS_ORIGIN,
      logins: [DOMAIN1_HTTPS_TO_HTTPS_U1_P1, DOMAIN1_HTTPS_TO_HTTPS_U1_P2],
    },
    {
      description: "HTTP form, both https, same username, different password",
      formActionOrigin: DOMAIN1_HTTP_ORIGIN,
      logins: [DOMAIN1_HTTPS_TO_HTTPS_U1_P1, DOMAIN1_HTTPS_TO_HTTPS_U1_P2],
    },
  ];

  for (const tc of testcases) {
    info(tc.description);

    const result = await Services.logins.addLogins(tc.logins);
    Assert.equal(result.length, 1, "only single login added");

    Services.logins.removeAllUserFacingLogins();
  }
});