summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/unit/test_search_schemeUpgrades.js
blob: 9428d3f897826b5d9721284ee768a4e797855455 (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
/**
 * Test Services.logins.searchLogins with the `schemeUpgrades` property.
 */

const HTTP3_ORIGIN = "http://www3.example.com";
const HTTPS_ORIGIN = "https://www.example.com";
const HTTP_ORIGIN = "http://www.example.com";

/**
 * Returns a list of new nsILoginInfo objects that are a subset of the test
 * data, built to match the specified query.
 *
 * @param {Object} aQuery
 *        Each property and value of this object restricts the search to those
 *        entries from the test data that match the property exactly.
 */
function buildExpectedLogins(aQuery) {
  return TestData.loginList().filter(entry =>
    Object.keys(aQuery).every(name => {
      if (name == "schemeUpgrades") {
        return true;
      }
      if (["origin", "formActionOrigin"].includes(name)) {
        return LoginHelper.isOriginMatching(entry[name], aQuery[name], {
          schemeUpgrades: aQuery.schemeUpgrades,
        });
      }
      return entry[name] === aQuery[name];
    })
  );
}

/**
 * Tests the searchLogins function.
 *
 * @param {Object} aQuery
 *        Each property and value of this object is translated to an entry in
 *        the nsIPropertyBag parameter of searchLogins.
 * @param {Number} aExpectedCount
 *        Number of logins from the test data that should be found.  The actual
 *        list of logins is obtained using the buildExpectedLogins helper, and
 *        this value is just used to verify that modifications to the test data
 *        don't make the current test meaningless.
 */
function checkSearch(aQuery, aExpectedCount) {
  info("Testing searchLogins for " + JSON.stringify(aQuery));

  let expectedLogins = buildExpectedLogins(aQuery);
  Assert.equal(expectedLogins.length, aExpectedCount);

  let logins = Services.logins.searchLogins(newPropertyBag(aQuery));
  LoginTestUtils.assertLoginListsEqual(logins, expectedLogins);
}

/**
 * Prepare data for the following tests.
 */
add_setup(async () => {
  await Services.logins.addLogins(TestData.loginList());
});

/**
 * Tests searchLogins with the `schemeUpgrades` property
 */
add_task(function test_search_schemeUpgrades_origin() {
  // Origin-only
  checkSearch(
    {
      origin: HTTPS_ORIGIN,
    },
    1
  );
  checkSearch(
    {
      origin: HTTPS_ORIGIN,
      schemeUpgrades: false,
    },
    1
  );
  checkSearch(
    {
      origin: HTTPS_ORIGIN,
      schemeUpgrades: undefined,
    },
    1
  );
  checkSearch(
    {
      origin: HTTPS_ORIGIN,
      schemeUpgrades: true,
    },
    2
  );
});

/**
 * Same as above but replacing origin with formActionOrigin.
 */
add_task(function test_search_schemeUpgrades_formActionOrigin() {
  checkSearch(
    {
      formActionOrigin: HTTPS_ORIGIN,
    },
    2
  );
  checkSearch(
    {
      formActionOrigin: HTTPS_ORIGIN,
      schemeUpgrades: false,
    },
    2
  );
  checkSearch(
    {
      formActionOrigin: HTTPS_ORIGIN,
      schemeUpgrades: undefined,
    },
    2
  );
  checkSearch(
    {
      formActionOrigin: HTTPS_ORIGIN,
      schemeUpgrades: true,
    },
    4
  );
});

add_task(function test_search_schemeUpgrades_origin_formActionOrigin() {
  checkSearch(
    {
      formActionOrigin: HTTPS_ORIGIN,
      origin: HTTPS_ORIGIN,
    },
    1
  );
  checkSearch(
    {
      formActionOrigin: HTTPS_ORIGIN,
      origin: HTTPS_ORIGIN,
      schemeUpgrades: false,
    },
    1
  );
  checkSearch(
    {
      formActionOrigin: HTTPS_ORIGIN,
      origin: HTTPS_ORIGIN,
      schemeUpgrades: undefined,
    },
    1
  );
  checkSearch(
    {
      formActionOrigin: HTTPS_ORIGIN,
      origin: HTTPS_ORIGIN,
      schemeUpgrades: true,
    },
    2
  );
  checkSearch(
    {
      formActionOrigin: HTTPS_ORIGIN,
      origin: HTTPS_ORIGIN,
      schemeUpgrades: true,
      usernameField: "form_field_username",
    },
    2
  );
  checkSearch(
    {
      formActionOrigin: HTTPS_ORIGIN,
      origin: HTTPS_ORIGIN,
      passwordField: "form_field_password",
      schemeUpgrades: true,
      usernameField: "form_field_username",
    },
    2
  );
  checkSearch(
    {
      formActionOrigin: HTTPS_ORIGIN,
      origin: HTTPS_ORIGIN,
      httpRealm: null,
      passwordField: "form_field_password",
      schemeUpgrades: true,
      usernameField: "form_field_username",
    },
    2
  );
});

/**
 * HTTP submitting to HTTPS
 */
add_task(function test_http_to_https() {
  checkSearch(
    {
      formActionOrigin: HTTPS_ORIGIN,
      origin: HTTP3_ORIGIN,
      httpRealm: null,
      schemeUpgrades: false,
    },
    1
  );
  checkSearch(
    {
      formActionOrigin: HTTPS_ORIGIN,
      origin: HTTP3_ORIGIN,
      httpRealm: null,
      schemeUpgrades: true,
    },
    2
  );
});

/**
 * schemeUpgrades shouldn't cause downgrades
 */
add_task(function test_search_schemeUpgrades_downgrade() {
  checkSearch(
    {
      formActionOrigin: HTTP_ORIGIN,
      origin: HTTP_ORIGIN,
    },
    1
  );
  info(
    "The same number should be found with schemeUpgrades since we're searching for HTTP"
  );
  checkSearch(
    {
      formActionOrigin: HTTP_ORIGIN,
      origin: HTTP_ORIGIN,
      schemeUpgrades: true,
    },
    1
  );
});