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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/**
* Test for LoginManagerChild._getFormFields.
*/
"use strict";
XPCOMUtils.defineLazyGlobalGetters(this, ["URL"]);
const { LoginFormFactory } = ChromeUtils.import(
"resource://gre/modules/LoginFormFactory.jsm"
);
const LMCBackstagePass = ChromeUtils.import(
"resource://gre/modules/LoginManagerChild.jsm",
null
);
const { LoginManagerChild } = LMCBackstagePass;
const TESTENVIRONMENTS = {
filledPW1WithGeneratedPassword: {
generatedPWFieldSelectors: ["#pw1"],
},
};
const TESTCASES = [
{
description: "1 password field outside of a <form>",
document: `<input id="pw1" type=password>`,
returnedFieldIDs: {
usernameField: null,
newPasswordField: "pw1",
oldPasswordField: null,
},
skipEmptyFields: undefined,
extraTestEnvironments: [TESTENVIRONMENTS.filledPW1WithGeneratedPassword],
},
{
description: "1 text field outside of a <form> without a password field",
document: `<input id="un1">`,
returnedFieldIDs: {
usernameField: null,
newPasswordField: null,
oldPasswordField: null,
},
skipEmptyFields: undefined,
// there is no password field to fill, so no sense testing with gen. passwords
extraTestEnvironments: [],
},
{
description: "1 username & password field outside of a <form>",
document: `<input id="un1">
<input id="pw1" type=password>`,
returnedFieldIDs: {
usernameField: "un1",
newPasswordField: "pw1",
oldPasswordField: null,
},
skipEmptyFields: undefined,
extraTestEnvironments: [TESTENVIRONMENTS.filledPW1WithGeneratedPassword],
},
{
beforeGetFunction(doc, formLike) {
// Access the formLike.elements lazy getter to have it cached.
Assert.equal(
formLike.elements.length,
2,
"Check initial elements length"
);
doc.getElementById("un1").remove();
},
description: "1 username & password field outside of a <form>, un1 removed",
document: `<input id="un1">
<input id="pw1" type=password>`,
returnedFieldIDs: {
usernameField: null,
newPasswordField: "pw1",
oldPasswordField: null,
},
skipEmptyFields: undefined,
extraTestEnvironments: [TESTENVIRONMENTS.filledPW1WithGeneratedPassword],
},
{
description: "1 username & password field in a <form>",
document: `<form>
<input id="un1">
<input id="pw1" type=password>
</form>`,
returnedFieldIDs: {
usernameField: "un1",
newPasswordField: "pw1",
oldPasswordField: null,
},
skipEmptyFields: undefined,
extraTestEnvironments: [TESTENVIRONMENTS.filledPW1WithGeneratedPassword],
},
{
description: "5 empty password fields outside of a <form>",
document: `<input id="pw1" type=password>
<input id="pw2" type=password>
<input id="pw3" type=password>
<input id="pw4" type=password>
<input id="pw5" type=password>`,
returnedFieldIDs: {
usernameField: null,
newPasswordField: "pw1",
oldPasswordField: null,
},
skipEmptyFields: undefined,
extraTestEnvironments: [TESTENVIRONMENTS.filledPW1WithGeneratedPassword],
},
{
description: "6 empty password fields outside of a <form>",
document: `<input id="pw1" type=password>
<input id="pw2" type=password>
<input id="pw3" type=password>
<input id="pw4" type=password>
<input id="pw5" type=password>
<input id="pw6" type=password>`,
returnedFieldIDs: {
usernameField: null,
newPasswordField: null,
oldPasswordField: null,
},
skipEmptyFields: undefined,
extraTestEnvironments: [TESTENVIRONMENTS.filledPW1WithGeneratedPassword],
},
{
description:
"4 password fields outside of a <form> (1 empty, 3 full) with skipEmpty",
document: `<input id="pw1" type=password>
<input id="pw2" type=password value="pass2">
<input id="pw3" type=password value="pass3">
<input id="pw4" type=password value="pass4">`,
returnedFieldIDs: {
usernameField: null,
newPasswordField: null,
oldPasswordField: null,
},
skipEmptyFields: true,
// This test assumes that pw1 has not been filled, so don't test prefilling it
extraTestEnvironments: [],
},
{
description: "Form with 1 password field",
document: `<form><input id="pw1" type=password></form>`,
returnedFieldIDs: {
usernameField: null,
newPasswordField: "pw1",
oldPasswordField: null,
},
skipEmptyFields: undefined,
extraTestEnvironments: [TESTENVIRONMENTS.filledPW1WithGeneratedPassword],
},
{
description: "Form with 2 password fields",
document: `<form><input id="pw1" type=password><input id='pw2' type=password></form>`,
returnedFieldIDs: {
usernameField: null,
newPasswordField: "pw1",
oldPasswordField: null,
},
skipEmptyFields: undefined,
extraTestEnvironments: [TESTENVIRONMENTS.filledPW1WithGeneratedPassword],
},
{
description: "1 password field in a form, 1 outside (not processed)",
document: `<form><input id="pw1" type=password></form><input id="pw2" type=password>`,
returnedFieldIDs: {
usernameField: null,
newPasswordField: "pw1",
oldPasswordField: null,
},
skipEmptyFields: undefined,
extraTestEnvironments: [TESTENVIRONMENTS.filledPW1WithGeneratedPassword],
},
{
description:
"1 password field in a form, 1 text field outside (not processed)",
document: `<form><input id="pw1" type=password></form><input>`,
returnedFieldIDs: {
usernameField: null,
newPasswordField: "pw1",
oldPasswordField: null,
},
skipEmptyFields: undefined,
extraTestEnvironments: [TESTENVIRONMENTS.filledPW1WithGeneratedPassword],
},
{
description:
"1 text field in a form, 1 password field outside (not processed)",
document: `<form><input></form><input id="pw1" type=password>`,
returnedFieldIDs: {
usernameField: null,
newPasswordField: null,
oldPasswordField: null,
},
skipEmptyFields: undefined,
extraTestEnvironments: [TESTENVIRONMENTS.filledPW1WithGeneratedPassword],
},
{
description:
"2 password fields outside of a <form> with 1 linked via @form",
document: `<input id="pw1" type=password><input id="pw2" type=password form='form1'>
<form id="form1"></form>`,
returnedFieldIDs: {
usernameField: null,
newPasswordField: "pw1",
oldPasswordField: null,
},
skipEmptyFields: undefined,
extraTestEnvironments: [TESTENVIRONMENTS.filledPW1WithGeneratedPassword],
},
{
description:
"2 password fields outside of a <form> with 1 linked via @form + skipEmpty",
document: `<input id="pw1" type=password><input id="pw2" type=password form="form1">
<form id="form1"></form>`,
returnedFieldIDs: {
usernameField: null,
newPasswordField: null,
oldPasswordField: null,
},
skipEmptyFields: true,
extraTestEnvironments: [TESTENVIRONMENTS.filledPW1WithGeneratedPassword],
},
{
description:
"2 password fields outside of a <form> with 1 linked via @form + skipEmpty with 1 empty",
document: `<input id="pw1" type=password value="pass1"><input id="pw2" type=password form="form1">
<form id="form1"></form>`,
returnedFieldIDs: {
usernameField: null,
newPasswordField: "pw1",
oldPasswordField: null,
},
skipEmptyFields: true,
extraTestEnvironments: [TESTENVIRONMENTS.filledPW1WithGeneratedPassword],
},
{
description:
"3 password fields, 2nd and 3rd are filled with generated passwords",
document: `<input id="pw1" type=password>
<input id="pw2" type=password value="pass2">
<input id="pw3" type=password value="pass3">`,
returnedFieldIDs: {
usernameField: null,
newPasswordField: "pw2",
confirmPasswordField: "pw3",
oldPasswordField: "pw1",
},
skipEmptyFields: undefined,
generatedPWFieldSelectors: ["#pw2", "#pw3"],
// this test doesn't make sense to run with different filled generated password values
extraTestEnvironments: [],
},
];
const TEST_ENVIRONMENT_CASES = TESTCASES.flatMap(tc => {
let arr = [tc];
// also run this test case with this different state
for (let env of tc.extraTestEnvironments) {
arr.push({
...tc,
...env,
});
}
return arr;
});
for (let tc of TEST_ENVIRONMENT_CASES) {
info("Sanity checking the testcase: " + tc.description);
(function() {
let testcase = tc;
add_task(async function() {
info("Starting testcase: " + testcase.description);
info("Document string: " + testcase.document);
let document = MockDocument.createTestDocument(
"http://localhost:8080/test/",
testcase.document
);
let input = document.querySelector("input");
MockDocument.mockOwnerDocumentProperty(
input,
document,
"http://localhost:8080/test/"
);
let formLike = LoginFormFactory.createFromField(input);
if (testcase.beforeGetFunction) {
await testcase.beforeGetFunction(document, formLike);
}
let lmc = new LoginManagerChild();
let loginFormState = lmc.stateForDocument(formLike.ownerDocument);
loginFormState.generatedPasswordFields = _generateDocStateFromTestCase(
testcase,
document
);
let actual = lmc._getFormFields(
formLike,
testcase.skipEmptyFields,
new Set()
);
[
"usernameField",
"newPasswordField",
"oldPasswordField",
"confirmPasswordField",
].forEach(fieldName => {
Assert.ok(
fieldName in actual,
"_getFormFields return value includes " + fieldName
);
});
for (let key of Object.keys(testcase.returnedFieldIDs)) {
let expectedID = testcase.returnedFieldIDs[key];
if (expectedID === null) {
Assert.strictEqual(
actual[key],
expectedID,
"Check returned field " + key + " is null"
);
} else {
Assert.strictEqual(
actual[key].id,
expectedID,
"Check returned field " + key + " ID"
);
}
}
});
})();
}
function _generateDocStateFromTestCase(stateProperties, document) {
// prepopulate the document form state LMC holds with
// any generated password fields defined in this testcase
let generatedPasswordFields = new Set();
info(
"stateProperties has generatedPWFieldSelectors: " +
stateProperties.generatedPWFieldSelectors?.join(", ")
);
if (stateProperties.generatedPWFieldSelectors?.length) {
stateProperties.generatedPWFieldSelectors.forEach(sel => {
let field = document.querySelector(sel);
if (field) {
generatedPasswordFields.add(field);
} else {
info(`No password field: ${sel} found in this document`);
}
});
}
return generatedPasswordFields;
}
|