summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/unit/test_search_schemeUpgrades.js
blob: 3406becff2e1e7bc829258a6a603cde0d12d164d (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
/*
 * 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 (["hostname", "formSubmitURL"].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) {
  do_print("Testing searchLogins for " + JSON.stringify(aQuery));

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

  let outCount = {};
  let logins = Services.logins.searchLogins(outCount, newPropertyBag(aQuery));
  do_check_eq(outCount.value, expectedLogins.length);
  LoginTestUtils.assertLoginListsEqual(logins, expectedLogins);
}

/**
 * Prepare data for the following tests.
 */
add_task(function test_initialize() {
  for (let login of TestData.loginList()) {
    Services.logins.addLogin(login);
  }
});

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

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


add_task(function test_search_schemeUpgrades_hostname_formSubmitURL() {
  checkSearch({
    formSubmitURL: HTTPS_ORIGIN,
    hostname: HTTPS_ORIGIN,
  }, 1);
  checkSearch({
    formSubmitURL: HTTPS_ORIGIN,
    hostname: HTTPS_ORIGIN,
    schemeUpgrades: false,
  }, 1);
  checkSearch({
    formSubmitURL: HTTPS_ORIGIN,
    hostname: HTTPS_ORIGIN,
    schemeUpgrades: undefined,
  }, 1);
  checkSearch({
    formSubmitURL: HTTPS_ORIGIN,
    hostname: HTTPS_ORIGIN,
    schemeUpgrades: true,
  }, 2);
  checkSearch({
    formSubmitURL: HTTPS_ORIGIN,
    hostname: HTTPS_ORIGIN,
    schemeUpgrades: true,
    usernameField: "form_field_username",
  }, 2);
  checkSearch({
    formSubmitURL: HTTPS_ORIGIN,
    hostname: HTTPS_ORIGIN,
    passwordField: "form_field_password",
    schemeUpgrades: true,
    usernameField: "form_field_username",
  }, 2);
  checkSearch({
    formSubmitURL: HTTPS_ORIGIN,
    hostname: 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({
    formSubmitURL: HTTPS_ORIGIN,
    hostname: HTTP3_ORIGIN,
    httpRealm: null,
    schemeUpgrades: false,
  }, 1);
  checkSearch({
    formSubmitURL: HTTPS_ORIGIN,
    hostname: HTTP3_ORIGIN,
    httpRealm: null,
    schemeUpgrades: true,
  }, 2);
});

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