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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
|
/**
* Helpers for password manager mochitest-plain tests.
*/
/* import-globals-from ../../../../../toolkit/components/satchel/test/satchel_common.js */
const { LoginTestUtils } = SpecialPowers.ChromeUtils.importESModule(
"resource://testing-common/LoginTestUtils.sys.mjs"
);
const Services = SpecialPowers.Services;
// Setup LoginTestUtils to report assertions to the mochitest harness.
LoginTestUtils.setAssertReporter(
SpecialPowers.wrapCallback((err, message, stack) => {
SimpleTest.record(!err, err ? err.message : message, null, stack);
})
);
const { LoginHelper } = SpecialPowers.ChromeUtils.importESModule(
"resource://gre/modules/LoginHelper.sys.mjs"
);
const { LENGTH: GENERATED_PASSWORD_LENGTH, REGEX: GENERATED_PASSWORD_REGEX } =
LoginTestUtils.generation;
const LOGIN_FIELD_UTILS = LoginTestUtils.loginField;
const TESTS_DIR = "/tests/toolkit/components/passwordmgr/test/";
/**
* Recreate a DOM tree using the outerHTML to ensure that any event listeners
* and internal state for the elements are removed.
*/
function recreateTree(element) {
// eslint-disable-next-line no-self-assign
element.outerHTML = element.outerHTML;
}
function _checkArrayValues(actualValues, expectedValues, msg) {
is(
actualValues.length,
expectedValues.length,
"Checking array values: " + msg
);
for (let i = 0; i < expectedValues.length; i++) {
is(actualValues[i], expectedValues[i], msg + " Checking array entry #" + i);
}
}
/**
* Check autocomplete popup results to ensure that expected
* *labels* are being shown correctly as items in the popup.
*/
function checkAutoCompleteResults(actualValues, expectedValues, hostname, msg) {
if (hostname === null) {
_checkArrayValues(actualValues, expectedValues, msg);
return;
}
isnot(
actualValues.length,
0,
"There should be items in the autocomplete popup: " +
JSON.stringify(actualValues)
);
// Check the footer first.
let footerResult = actualValues[actualValues.length - 1];
is(footerResult, "Manage Passwords", "the footer text is shown correctly");
if (actualValues.length == 1) {
is(
expectedValues.length,
0,
"If only the footer is present then there should be no expectedValues"
);
info("Only the footer is present in the popup");
return;
}
// Check the rest of the autocomplete item values.
_checkArrayValues(actualValues.slice(0, -1), expectedValues, msg);
}
/**
* Wait for autocomplete popup to get closed
* @return {Promise} resolving when the AC popup is closed
*/
async function untilAutocompletePopupClosed() {
return SimpleTest.promiseWaitForCondition(async () => {
const popupState = await getPopupState();
return !popupState.open;
}, "Wait for autocomplete popup to be closed");
}
function getIframeBrowsingContext(window, iframeNumber = 0) {
let bc = SpecialPowers.wrap(window).windowGlobalChild.browsingContext;
return SpecialPowers.unwrap(bc.children[iframeNumber]);
}
/**
* Set input values via setUserInput to emulate user input
* and distinguish them from declarative or script-assigned values
*/
function setUserInputValues(parentNode, selectorValues, userInput = true) {
for (let [selector, newValue] of Object.entries(selectorValues)) {
info(`setUserInputValues, selector: ${selector}`);
try {
let field = SpecialPowers.wrap(parentNode.querySelector(selector));
if (field.value == newValue) {
// we don't get an input event if the new value == the old
field.value += "#";
}
if (userInput) {
field.setUserInput(newValue);
} else {
field.value = newValue;
}
} catch (ex) {
info(ex.message);
info(ex.stack);
ok(
false,
`setUserInputValues: Couldn't set value of field: ${ex.message}`
);
}
}
}
/**
* @param {Function} [aFilterFn = undefined] Function to filter out irrelevant submissions.
* @return {Promise} resolving when a relevant form submission was processed.
*/
function getSubmitMessage(aFilterFn = undefined) {
info("getSubmitMessage");
return new Promise(resolve => {
PWMGR_COMMON_PARENT.addMessageListener(
"formSubmissionProcessed",
function processed(...args) {
if (aFilterFn && !aFilterFn(...args)) {
// This submission isn't the one we're waiting for.
return;
}
info("got formSubmissionProcessed");
PWMGR_COMMON_PARENT.removeMessageListener(
"formSubmissionProcessed",
processed
);
resolve(args[0]);
}
);
});
}
/**
* @return {Promise} resolves when a onPasswordEditedOrGenerated message is received at the parent
*/
function getPasswordEditedMessage() {
info("getPasswordEditedMessage");
return new Promise(resolve => {
PWMGR_COMMON_PARENT.addMessageListener(
"passwordEditedOrGenerated",
function listener(...args) {
info("got passwordEditedOrGenerated");
PWMGR_COMMON_PARENT.removeMessageListener(
"passwordEditedOrGenerated",
listener
);
resolve(args[0]);
}
);
});
}
/**
* Create a login form and insert into contents dom (identified by id
* `content`). If the form (identified by its number) is already present in the
* dom, it gets replaced.
*
* @param {number} [num = 1] - number of the form, used as id, eg `form1`
* @param {string} [action = ""] - action attribute of the form
* @param {string} [autocomplete = null] - forms autocomplete attribute. Default is none
* @param {object} [username = {}] - object describing attributes to the username field:
* @param {string} [username.id = null] - id of the field
* @param {string} [username.name = "uname"] - name attribute
* @param {string} [username.type = "text"] - type of the field
* @param {string} [username.value = null] - initial value of the field
* @param {string} [username.autocomplete = null] - autocomplete attribute
* @param {object} [password = {}] - an object describing attributes to the password field. If falsy, do not create a password field
* @param {string} [password.id = null] - id of the field
* @param {string} [password.name = "pword"] - name attribute
* @param {string} [password.type = "password"] - type of the field
* @param {string} [password.value = null] - initial value of the field
* @param {string} [password.label = null] - if present, wrap field in a label containing its value
* @param {string} [password.autocomplete = null] - autocomplete attribute
*
* @return {HTMLDomElement} the form
*/
function createLoginForm({
num = 1,
action = "",
autocomplete = null,
username = {},
password = {},
} = {}) {
username.name ||= "uname";
username.type ||= "text";
username.id ||= null;
username.value ||= null;
username.autocomplete ||= null;
password.name ||= "pword";
password.type ||= "password";
password.id ||= null;
password.value ||= null;
password.label ||= null;
password.autocomplete ||= null;
password.readonly ||= null;
password.disabled ||= null;
info(
`Creating login form ${JSON.stringify({ num, action, username, password })}`
);
const form = document.createElement("form");
form.id = `form${num}`;
form.action = action;
form.onsubmit = () => false;
if (autocomplete != null) {
form.setAttribute("autocomplete", autocomplete);
}
const usernameInput = document.createElement("input");
usernameInput.type = username.type;
usernameInput.name = username.name;
if (username.id != null) {
usernameInput.id = username.id;
}
if (username.value != null) {
usernameInput.value = username.value;
}
if (username.autocomplete != null) {
usernameInput.setAttribute("autocomplete", username.autocomplete);
}
form.appendChild(usernameInput);
if (password) {
const passwordInput = document.createElement("input");
passwordInput.type = password.type;
passwordInput.name = password.name;
if (password.id != null) {
passwordInput.id = password.id;
}
if (password.value != null) {
passwordInput.value = password.value;
}
if (password.autocomplete != null) {
passwordInput.setAttribute("autocomplete", password.autocomplete);
}
if (password.readonly != null) {
passwordInput.setAttribute("readonly", password.readonly);
}
if (password.disabled != null) {
passwordInput.setAttribute("disabled", password.disabled);
}
if (password.label != null) {
const passwordLabel = document.createElement("label");
passwordLabel.innerText = password.label;
passwordLabel.appendChild(passwordInput);
form.appendChild(passwordLabel);
} else {
form.appendChild(passwordInput);
}
}
const submitButton = document.createElement("button");
submitButton.type = "submit";
submitButton.name = "submit";
submitButton.innerText = "Submit";
form.appendChild(submitButton);
const content = document.getElementById("content");
const oldForm = document.getElementById(form.id);
if (oldForm) {
content.replaceChild(form, oldForm);
} else {
content.appendChild(form);
}
return form;
}
/**
* Check for expected username/password in form.
* @see `checkForm` below for a similar function.
*/
function checkLoginForm(
usernameField,
expectedUsername,
passwordField,
expectedPassword
) {
let formID = usernameField.parentNode.id;
is(
usernameField.value,
expectedUsername,
"Checking " + formID + " username is: " + expectedUsername
);
is(
passwordField.value,
expectedPassword,
"Checking " + formID + " password is: " + expectedPassword
);
}
/**
* Check repeatedly for a while to see if a particular condition still applies.
* This function checks the return value of `condition` repeatedly until either
* the condition has a falsy return value, or `retryTimes` is exceeded.
*/
function ensureCondition(
condition,
errorMsg = "Condition did not last.",
retryTimes = 10
) {
return new Promise((resolve, reject) => {
let tries = 0;
let conditionFailed = false;
let interval = setInterval(async function () {
try {
const conditionPassed = await condition();
conditionFailed ||= !conditionPassed;
} catch (e) {
ok(false, e + "\n" + e.stack);
conditionFailed = true;
}
if (conditionFailed || tries >= retryTimes) {
ok(!conditionFailed, errorMsg);
clearInterval(interval);
if (conditionFailed) {
reject(errorMsg);
} else {
resolve();
}
}
tries++;
}, 100);
});
}
/**
* Wait a while to ensure login form stays filled with username and password
* @see `checkLoginForm` below for a similar function.
* @returns a promise, resolving when done
*
* TODO: eventually get rid of this time based check, and transition to an
* event based approach. See Bug 1811142.
* Filling happens by `_fillForm()` which can report it's decision and we can
* wait for it. One of the options is to have `didFillFormAsync()` from
* https://phabricator.services.mozilla.com/D167214#change-3njWgUgqswws
*/
function ensureLoginFormStaysFilledWith(
usernameField,
expectedUsername,
passwordField,
expectedPassword
) {
return ensureCondition(() => {
return (
Object.is(usernameField.value, expectedUsername) &&
Object.is(passwordField.value, expectedPassword)
);
}, `Ensuring form ${usernameField.parentNode.id} stays filled with "${expectedUsername}:${expectedPassword}"`);
}
function checkLoginFormInFrame(
iframeBC,
usernameFieldId,
expectedUsername,
passwordFieldId,
expectedPassword
) {
return SpecialPowers.spawn(
iframeBC,
[usernameFieldId, expectedUsername, passwordFieldId, expectedPassword],
(
usernameFieldIdF,
expectedUsernameF,
passwordFieldIdF,
expectedPasswordF
) => {
let usernameField =
this.content.document.getElementById(usernameFieldIdF);
let passwordField =
this.content.document.getElementById(passwordFieldIdF);
let formID = usernameField.parentNode.id;
Assert.equal(
usernameField.value,
expectedUsernameF,
"Checking " + formID + " username is: " + expectedUsernameF
);
Assert.equal(
passwordField.value,
expectedPasswordF,
"Checking " + formID + " password is: " + expectedPasswordF
);
}
);
}
async function checkUnmodifiedFormInFrame(bc, formNum) {
return SpecialPowers.spawn(bc, [formNum], formNumF => {
let form = this.content.document.getElementById(`form${formNumF}`);
ok(form, "Locating form " + formNumF);
for (var i = 0; i < form.elements.length; i++) {
var ele = form.elements[i];
// No point in checking form submit/reset buttons.
if (ele.type == "submit" || ele.type == "reset") {
continue;
}
is(
ele.value,
ele.defaultValue,
"Test to default value of field " + ele.name + " in form " + formNumF
);
}
});
}
/**
* Check a form for expected values even if it is in a different top level window
* or process. If an argument is null, a field's expected value will be the default
* value.
*
* Similar to the checkForm helper, but it works across (cross-origin) frames.
*
* <form id="form#">
* checkLoginFormInFrameWithElementValues(#, "foo");
*/
async function checkLoginFormInFrameWithElementValues(
browsingContext,
formNum,
...values
) {
return SpecialPowers.spawn(
browsingContext,
[formNum, values],
function checkFormWithElementValues(formNumF, valuesF) {
let [val1F, val2F, val3F] = valuesF;
let doc = this.content.document;
let e;
let form = doc.getElementById("form" + formNumF);
ok(form, "Locating form " + formNumF);
let numToCheck = arguments.length - 1;
if (!numToCheck--) {
return;
}
e = form.elements[0];
if (val1F == null) {
is(
e.value,
e.defaultValue,
"Test default value of field " + e.name + " in form " + formNumF
);
} else {
is(
e.value,
val1F,
"Test value of field " + e.name + " in form " + formNumF
);
}
if (!numToCheck--) {
return;
}
e = form.elements[1];
if (val2F == null) {
is(
e.value,
e.defaultValue,
"Test default value of field " + e.name + " in form " + formNumF
);
} else {
is(
e.value,
val2F,
"Test value of field " + e.name + " in form " + formNumF
);
}
if (!numToCheck--) {
return;
}
e = form.elements[2];
if (val3F == null) {
is(
e.value,
e.defaultValue,
"Test default value of field " + e.name + " in form " + formNumF
);
} else {
is(
e.value,
val3F,
"Test value of field " + e.name + " in form " + formNumF
);
}
}
);
}
/**
* Check a form for expected values. If an argument is null, a field's
* expected value will be the default value.
*
* <form id="form#">
* checkForm(#, "foo");
*/
function checkForm(formNum, val1, val2, val3) {
var e,
form = document.getElementById("form" + formNum);
ok(form, "Locating form " + formNum);
var numToCheck = arguments.length - 1;
if (!numToCheck--) {
return;
}
e = form.elements[0];
if (val1 == null) {
is(
e.value,
e.defaultValue,
"Test default value of field " + e.name + " in form " + formNum
);
} else {
is(e.value, val1, "Test value of field " + e.name + " in form " + formNum);
}
if (!numToCheck--) {
return;
}
e = form.elements[1];
if (val2 == null) {
is(
e.value,
e.defaultValue,
"Test default value of field " + e.name + " in form " + formNum
);
} else {
is(e.value, val2, "Test value of field " + e.name + " in form " + formNum);
}
if (!numToCheck--) {
return;
}
e = form.elements[2];
if (val3 == null) {
is(
e.value,
e.defaultValue,
"Test default value of field " + e.name + " in form " + formNum
);
} else {
is(e.value, val3, "Test value of field " + e.name + " in form " + formNum);
}
}
/**
* Check a form for unmodified values from when page was loaded.
*
* <form id="form#">
* checkUnmodifiedForm(#);
*/
function checkUnmodifiedForm(formNum) {
var form = document.getElementById("form" + formNum);
ok(form, "Locating form " + formNum);
for (var i = 0; i < form.elements.length; i++) {
var ele = form.elements[i];
// No point in checking form submit/reset buttons.
if (ele.type == "submit" || ele.type == "reset") {
continue;
}
is(
ele.value,
ele.defaultValue,
"Test to default value of field " + ele.name + " in form " + formNum
);
}
}
/**
* Wait for the document to be ready and any existing password fields on
* forms to be processed.
*
* @param existingPasswordFieldsCount the number of password fields
* that begin on the test page.
*/
function registerRunTests(existingPasswordFieldsCount = 0, callback) {
return new Promise(resolve => {
function onDOMContentLoaded() {
var form = document.createElement("form");
form.id = "observerforcer";
var username = document.createElement("input");
username.name = "testuser";
form.appendChild(username);
var password = document.createElement("input");
password.name = "testpass";
password.type = "password";
form.appendChild(password);
let foundForcer = false;
var observer = SpecialPowers.wrapCallback(function (
_subject,
_topic,
data
) {
if (data === "observerforcer") {
foundForcer = true;
} else {
existingPasswordFieldsCount--;
}
if (!foundForcer || existingPasswordFieldsCount > 0) {
return;
}
SpecialPowers.removeObserver(observer, "passwordmgr-processed-form");
form.remove();
SimpleTest.executeSoon(() => {
callback?.();
resolve();
});
});
SpecialPowers.addObserver(observer, "passwordmgr-processed-form");
document.body.appendChild(form);
}
// We provide a general mechanism for our tests to know when they can
// safely run: we add a final form that we know will be filled in, wait
// for the login manager to tell us that it's filled in and then continue
// with the rest of the tests.
if (
document.readyState == "complete" ||
document.readyState == "interactive"
) {
onDOMContentLoaded();
} else {
window.addEventListener("DOMContentLoaded", onDOMContentLoaded);
}
});
}
function enablePrimaryPassword() {
setPrimaryPassword(true);
}
function disablePrimaryPassword() {
setPrimaryPassword(false);
}
function setPrimaryPassword(enable) {
PWMGR_COMMON_PARENT.sendAsyncMessage("setPrimaryPassword", { enable });
}
function isLoggedIn() {
return PWMGR_COMMON_PARENT.sendQuery("isLoggedIn");
}
function logoutPrimaryPassword() {
runInParent(function parent_logoutPrimaryPassword() {
var sdr = Cc["@mozilla.org/security/sdr;1"].getService(
Ci.nsISecretDecoderRing
);
sdr.logoutAndTeardown();
});
}
/**
* Resolves when a specified number of forms have been processed for (potential) filling.
* This relies on the observer service which only notifies observers within the same process.
*/
function promiseFormsProcessedInSameProcess(expectedCount = 1) {
var processedCount = 0;
return new Promise(resolve => {
function onProcessedForm(subject, _topic, data) {
processedCount++;
if (processedCount == expectedCount) {
info(`${processedCount} form(s) processed`);
SpecialPowers.removeObserver(
onProcessedForm,
"passwordmgr-processed-form"
);
resolve(SpecialPowers.Cu.waiveXrays(subject), data);
}
}
SpecialPowers.addObserver(onProcessedForm, "passwordmgr-processed-form");
});
}
/**
* Resolves when a form has been processed for (potential) filling.
* This works across processes.
*/
async function promiseFormsProcessed(expectedCount = 1) {
info(`waiting for ${expectedCount} forms to be processed`);
var processedCount = 0;
return new Promise(resolve => {
PWMGR_COMMON_PARENT.addMessageListener(
"formProcessed",
function formProcessed() {
processedCount++;
info(`processed form ${processedCount} of ${expectedCount}`);
if (processedCount == expectedCount) {
info(`processing of ${expectedCount} forms complete`);
PWMGR_COMMON_PARENT.removeMessageListener(
"formProcessed",
formProcessed
);
resolve();
}
}
);
});
}
async function loadFormIntoWindow(origin, html, win, expectedCount = 1, task) {
let loadedPromise = new Promise(resolve => {
win.addEventListener(
"load",
function (event) {
if (event.target.location.href.endsWith("blank.html")) {
resolve();
}
},
{ once: true }
);
});
let processedPromise = promiseFormsProcessed(expectedCount);
win.location =
origin + "/tests/toolkit/components/passwordmgr/test/mochitest/blank.html";
info(`Waiting for window to load for origin: ${origin}`);
await loadedPromise;
await SpecialPowers.spawn(
win,
[html, task?.toString()],
function (contentHtml, contentTask = null) {
this.content.document.documentElement.innerHTML = contentHtml;
// Similar to the invokeContentTask helper in accessible/tests/browser/shared-head.js
if (contentTask) {
// eslint-disable-next-line no-eval
const runnableTask = eval(`
(() => {
return (${contentTask});
})();`);
runnableTask.call(this);
}
}
);
info("Waiting for the form to be processed");
await processedPromise;
}
async function getTelemetryEvents(options) {
let events = await PWMGR_COMMON_PARENT.sendQuery(
"getTelemetryEvents",
options
);
info("CONTENT: getTelemetryEvents gotResult: " + JSON.stringify(events));
return events;
}
function loadRecipes(recipes) {
info("Loading recipes");
return PWMGR_COMMON_PARENT.sendQuery("loadRecipes", recipes);
}
function resetRecipes() {
info("Resetting recipes");
return PWMGR_COMMON_PARENT.sendQuery("resetRecipes");
}
async function promiseStorageChanged(expectedChangeTypes) {
let result = await PWMGR_COMMON_PARENT.sendQuery("storageChanged", {
expectedChangeTypes,
});
if (result) {
ok(false, result);
}
}
async function promisePromptShown(expectedTopic) {
let topic = await PWMGR_COMMON_PARENT.sendQuery("promptShown");
is(topic, expectedTopic, "Check expected prompt topic");
}
/**
* Run a function synchronously in the parent process and destroy it in the test cleanup function.
* @param {Function|String} aFunctionOrURL - either a function that will be stringified and run
* or the URL to a JS file.
* @return {Object} - the return value of loadChromeScript providing message-related methods.
* @see loadChromeScript in specialpowersAPI.js
*/
function runInParent(aFunctionOrURL) {
let chromeScript = SpecialPowers.loadChromeScript(aFunctionOrURL);
SimpleTest.registerCleanupFunction(() => {
chromeScript.destroy();
});
return chromeScript;
}
/** Manage logins in parent chrome process.
* */
function manageLoginsInParent() {
return runInParent(function addLoginsInParentInner() {
/* eslint-env mozilla/chrome-script */
addMessageListener("removeAllUserFacingLogins", () => {
Services.logins.removeAllUserFacingLogins();
});
/* eslint-env mozilla/chrome-script */
addMessageListener("getLogins", async () => {
const logins = await Services.logins.getAllLogins();
return logins.map(
({
origin,
formActionOrigin,
httpRealm,
username,
password,
usernameField,
passwordField,
}) => [
origin,
formActionOrigin,
httpRealm,
username,
password,
usernameField,
passwordField,
]
);
});
/* eslint-env mozilla/chrome-script */
addMessageListener("addLogins", async logins => {
let nsLoginInfo = Components.Constructor(
"@mozilla.org/login-manager/loginInfo;1",
Ci.nsILoginInfo,
"init"
);
const loginInfos = logins.map(login => new nsLoginInfo(...login));
try {
await Services.logins.addLogins(loginInfos);
} catch (e) {
assert.ok(false, "addLogins threw: " + e);
}
});
});
}
/** Initialize with a list of logins. The logins are added within the parent chrome process.
* @param {array} aLogins - a list of logins to add. Each login is an array of the arguments
* that would be passed to nsLoginInfo.init().
*/
async function addLoginsInParent(...aLogins) {
const script = manageLoginsInParent();
await script.sendQuery("addLogins", aLogins);
return script;
}
/** Initialize with a list of logins, after removing all user facing logins.
* The logins are added within the parent chrome process.
* @param {array} aLogins - a list of logins to add. Each login is an array of the arguments
* that would be passed to nsLoginInfo.init().
*/
async function setStoredLoginsAsync(...aLogins) {
const script = manageLoginsInParent();
script.sendQuery("removeAllUserFacingLogins");
await script.sendQuery("addLogins", aLogins);
return script;
}
/**
* Sets given logins for the duration of the test. Existing logins are first
* removed and finally restored when the test is finished.
* The logins are added within the parent chrome process.
* @param {array} logins - a list of logins to add. Each login is an array of the arguments
* that would be passed to nsLoginInfo.init().
*/
async function setStoredLoginsDuringTest(...logins) {
const script = manageLoginsInParent();
const loginsBefore = await script.sendQuery("getLogins");
await script.sendQuery("removeAllUserFacingLogins");
await script.sendQuery("addLogins", logins);
SimpleTest.registerCleanupFunction(async () => {
await script.sendQuery("removeAllUserFacingLogins");
await script.sendQuery("addLogins", loginsBefore);
});
}
/**
* Sets given logins for the duration of the task. Existing logins are first
* removed and finally restored when the task is finished.
* @param {array} logins - a list of logins to add. Each login is an array of the arguments
* that would be passed to nsLoginInfo.init().
*/
async function setStoredLoginsDuringTask(...logins) {
const script = manageLoginsInParent();
const loginsBefore = await script.sendQuery("getLogins");
await script.sendQuery("removeAllUserFacingLogins");
await script.sendQuery("addLogins", logins);
SimpleTest.registerTaskCleanupFunction(async () => {
await script.sendQuery("removeAllUserFacingLogins");
await script.sendQuery("addLogins", loginsBefore);
});
}
/** Returns a promise which resolves to a list of logins
*/
function getLogins() {
const script = manageLoginsInParent();
return script.sendQuery("getLogins");
}
/*
* gTestDependsOnDeprecatedLogin Set this global to true if your test relies
* on the testuser/testpass login that is created in pwmgr_common.js. New tests
* should not rely on this login.
*/
var gTestDependsOnDeprecatedLogin = false;
/**
* Replace the content innerHTML with the provided form and wait for autofill to fill in the form.
*
* @param {string} form The form to be appended to the #content element.
* @param {string} fieldSelector The CSS selector for the field to-be-filled
* @param {string} fieldValue The value expected to be filled
* @param {string} formId The ID (excluding the # character) of the form
*/
function setFormAndWaitForFieldFilled(
form,
{ fieldSelector, fieldValue, formId }
) {
document.querySelector("#content").innerHTML = form;
return SimpleTest.promiseWaitForCondition(() => {
let ancestor = formId
? document.querySelector("#" + formId)
: document.documentElement;
return ancestor.querySelector(fieldSelector).value == fieldValue;
}, "Wait for password manager to fill form");
}
/**
* Run commonInit synchronously in the parent then run the test function if supplied.
*
* @param {Function} aFunction The test function to run
*/
async function runChecksAfterCommonInit(aFunction = null) {
SimpleTest.waitForExplicitFinish();
await PWMGR_COMMON_PARENT.sendQuery("setupParent", {
testDependsOnDeprecatedLogin: gTestDependsOnDeprecatedLogin,
});
if (aFunction) {
await registerRunTests(0, aFunction);
}
return PWMGR_COMMON_PARENT;
}
// Begin code that runs immediately for all tests that include this file.
const PWMGR_COMMON_PARENT = runInParent(
SimpleTest.getTestFileURL("pwmgr_common_parent.js")
);
SimpleTest.registerCleanupFunction(() => {
SpecialPowers.flushPrefEnv();
PWMGR_COMMON_PARENT.sendAsyncMessage("cleanup");
runInParent(function cleanupParent() {
/* eslint-env mozilla/chrome-script */
// eslint-disable-next-line no-shadow
const { LoginManagerParent } = ChromeUtils.importESModule(
"resource://gre/modules/LoginManagerParent.sys.mjs"
);
// Remove all logins and disabled hosts
Services.logins.removeAllUserFacingLogins();
let disabledHosts = Services.logins.getAllDisabledHosts();
disabledHosts.forEach(host =>
Services.logins.setLoginSavingEnabled(host, true)
);
let authMgr = Cc["@mozilla.org/network/http-auth-manager;1"].getService(
Ci.nsIHttpAuthManager
);
authMgr.clearAll();
// Check that it's not null, instead of truthy to catch it becoming undefined
// in a refactoring.
if (LoginManagerParent._recipeManager !== null) {
LoginManagerParent._recipeManager.reset();
}
// Cleanup PopupNotifications (if on a relevant platform)
let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
if (chromeWin && chromeWin.PopupNotifications) {
let notes = chromeWin.PopupNotifications._currentNotifications;
if (notes.length) {
dump("Removing " + notes.length + " popup notifications.\n");
}
for (let note of notes) {
note.remove();
}
}
// Clear events last in case the above cleanup records events.
Services.telemetry.clearEvents();
});
});
// This is a version of LoginHelper.loginToVanillaObject that is adapted to run
// as content JS instead of chrome JS. This is needed to make it return a
// content JS object because the structured cloning we use to send it over
// JS IPC can't deal with a cross compartment wrapper.
function loginToVanillaObject(login) {
let obj = {};
for (let i in SpecialPowers.do_QueryInterface(
login,
SpecialPowers.Ci.nsILoginMetaInfo
)) {
if (typeof login[i] !== "function") {
obj[i] = login[i];
}
}
return obj;
}
/**
* Proxy for Services.logins (nsILoginManager).
* Only supports arguments which support structured clone plus {nsILoginInfo}
* Assumes properties are methods.
*/
this.LoginManager = new Proxy(
{},
{
get(_target, prop, _receiver) {
return (...args) => {
let loginInfoIndices = [];
let cloneableArgs = args.map((val, index) => {
if (
SpecialPowers.call_Instanceof(val, SpecialPowers.Ci.nsILoginInfo)
) {
loginInfoIndices.push(index);
return loginToVanillaObject(val);
}
return val;
});
return PWMGR_COMMON_PARENT.sendQuery("proxyLoginManager", {
args: cloneableArgs,
loginInfoIndices,
methodName: prop,
});
};
},
}
);
/**
* Set the inner html of the content div and ensure it gets reset after current
* task finishes.
* Returns the first child node of the newly created content div for convenient
* access of the newly created dom node.
*
* @param {String} html
* string of dom content or dom element to be inserted into content element
*/
function setContentForTask(html) {
const content = document.querySelector("#content");
const innerHTMLBefore = content.innerHTML || "";
SimpleTest.registerCurrentTaskCleanupFunction(
() => (content.innerHTML = innerHTMLBefore)
);
if (html.content?.cloneNode) {
const clone = html.content.cloneNode(true);
content.replaceChildren(clone);
} else {
content.innerHTML = html;
}
return content.firstElementChild;
}
/*
* Set preferences via SpecialPowers.pushPrefEnv and reset them after current
* task has finished.
*
* @param {*Object} preferences
* */
async function setPreferencesForTask(...preferences) {
await SpecialPowers.pushPrefEnv({
set: preferences,
});
SimpleTest.registerCurrentTaskCleanupFunction(() => SpecialPowers.popPrefEnv);
}
// capture form autofill results between tasks
let gPwmgrCommonCapturedAutofillResults = {};
PWMGR_COMMON_PARENT.addMessageListener(
"formProcessed",
({ formId, autofillResult }) => {
if (formId === "observerforcer") {
return;
}
gPwmgrCommonCapturedAutofillResults[formId] = autofillResult;
}
);
SimpleTest.registerTaskCleanupFunction(() => {
gPwmgrCommonCapturedAutofillResults = {};
});
/**
* Create a promise that resolves when the form has been processed.
* Works with forms processed in the past since the task started and in the future,
* across parent and child processes.
*
* @param {String} formId / the id of the form of which to expect formautofill events
* @returns promise, resolving with the autofill result.
*/
async function formAutofillResult(formId) {
if (formId in gPwmgrCommonCapturedAutofillResults) {
const autofillResult = gPwmgrCommonCapturedAutofillResults[formId];
delete gPwmgrCommonCapturedAutofillResults[formId];
return autofillResult;
}
return new Promise(resolve => {
PWMGR_COMMON_PARENT.addMessageListener(
"formProcessed",
({ formId: id, autofillResult }) => {
if (id !== formId) {
return;
}
delete gPwmgrCommonCapturedAutofillResults[formId];
resolve(autofillResult);
},
{ once: true }
);
});
}
|