toolkit/components/passwordmgr/test/pwmgr_common.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * $_
michael@0 3 *
michael@0 4 * Returns the element with the specified |name| attribute.
michael@0 5 */
michael@0 6 function $_(formNum, name) {
michael@0 7 var form = document.getElementById("form" + formNum);
michael@0 8 if (!form) {
michael@0 9 logWarning("$_ couldn't find requested form " + formNum);
michael@0 10 return null;
michael@0 11 }
michael@0 12
michael@0 13 var element = form.elements.namedItem(name);
michael@0 14 if (!element) {
michael@0 15 logWarning("$_ couldn't find requested element " + name);
michael@0 16 return null;
michael@0 17 }
michael@0 18
michael@0 19 // Note that namedItem is a bit stupid, and will prefer an
michael@0 20 // |id| attribute over a |name| attribute when looking for
michael@0 21 // the element. Login Mananger happens to use .namedItem
michael@0 22 // anyway, but let's rigorously check it here anyway so
michael@0 23 // that we don't end up with tests that mistakenly pass.
michael@0 24
michael@0 25 if (element.getAttribute("name") != name) {
michael@0 26 logWarning("$_ got confused.");
michael@0 27 return null;
michael@0 28 }
michael@0 29
michael@0 30 return element;
michael@0 31 }
michael@0 32
michael@0 33
michael@0 34 /*
michael@0 35 * checkForm
michael@0 36 *
michael@0 37 * Check a form for expected values. If an argument is null, a field's
michael@0 38 * expected value will be the default value.
michael@0 39 *
michael@0 40 * <form id="form#">
michael@0 41 * checkForm(#, "foo");
michael@0 42 */
michael@0 43 function checkForm(formNum, val1, val2, val3) {
michael@0 44 var e, form = document.getElementById("form" + formNum);
michael@0 45 ok(form, "Locating form " + formNum);
michael@0 46
michael@0 47 var numToCheck = arguments.length - 1;
michael@0 48
michael@0 49 if (!numToCheck--)
michael@0 50 return;
michael@0 51 e = form.elements[0];
michael@0 52 if (val1 == null)
michael@0 53 is(e.value, e.defaultValue, "Test default value of field " + e.name +
michael@0 54 " in form " + formNum);
michael@0 55 else
michael@0 56 is(e.value, val1, "Test value of field " + e.name +
michael@0 57 " in form " + formNum);
michael@0 58
michael@0 59
michael@0 60 if (!numToCheck--)
michael@0 61 return;
michael@0 62 e = form.elements[1];
michael@0 63 if (val2 == null)
michael@0 64 is(e.value, e.defaultValue, "Test default value of field " + e.name +
michael@0 65 " in form " + formNum);
michael@0 66 else
michael@0 67 is(e.value, val2, "Test value of field " + e.name +
michael@0 68 " in form " + formNum);
michael@0 69
michael@0 70
michael@0 71 if (!numToCheck--)
michael@0 72 return;
michael@0 73 e = form.elements[2];
michael@0 74 if (val3 == null)
michael@0 75 is(e.value, e.defaultValue, "Test default value of field " + e.name +
michael@0 76 " in form " + formNum);
michael@0 77 else
michael@0 78 is(e.value, val3, "Test value of field " + e.name +
michael@0 79 " in form " + formNum);
michael@0 80
michael@0 81 }
michael@0 82
michael@0 83
michael@0 84 /*
michael@0 85 * checkUnmodifiedForm
michael@0 86 *
michael@0 87 * Check a form for unmodified values from when page was loaded.
michael@0 88 *
michael@0 89 * <form id="form#">
michael@0 90 * checkUnmodifiedForm(#);
michael@0 91 */
michael@0 92 function checkUnmodifiedForm(formNum) {
michael@0 93 var form = document.getElementById("form" + formNum);
michael@0 94 ok(form, "Locating form " + formNum);
michael@0 95
michael@0 96 for (var i = 0; i < form.elements.length; i++) {
michael@0 97 var ele = form.elements[i];
michael@0 98
michael@0 99 // No point in checking form submit/reset buttons.
michael@0 100 if (ele.type == "submit" || ele.type == "reset")
michael@0 101 continue;
michael@0 102
michael@0 103 is(ele.value, ele.defaultValue, "Test to default value of field " +
michael@0 104 ele.name + " in form " + formNum);
michael@0 105 }
michael@0 106 }
michael@0 107
michael@0 108
michael@0 109 // Mochitest gives us a sendKey(), but it's targeted to a specific element.
michael@0 110 // This basically sends an untargeted key event, to whatever's focused.
michael@0 111 function doKey(aKey, modifier) {
michael@0 112 var keyName = "DOM_VK_" + aKey.toUpperCase();
michael@0 113 var key = KeyEvent[keyName];
michael@0 114
michael@0 115 // undefined --> null
michael@0 116 if (!modifier)
michael@0 117 modifier = null;
michael@0 118
michael@0 119 // Window utils for sending fake sey events.
michael@0 120 var wutils = SpecialPowers.wrap(window).
michael@0 121 QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor).
michael@0 122 getInterface(SpecialPowers.Ci.nsIDOMWindowUtils);
michael@0 123
michael@0 124 if (wutils.sendKeyEvent("keydown", key, 0, modifier)) {
michael@0 125 wutils.sendKeyEvent("keypress", key, 0, modifier);
michael@0 126 }
michael@0 127 wutils.sendKeyEvent("keyup", key, 0, modifier);
michael@0 128 }
michael@0 129
michael@0 130 // Init with a common login
michael@0 131 function commonInit() {
michael@0 132 var pwmgr = SpecialPowers.Cc["@mozilla.org/login-manager;1"].
michael@0 133 getService(SpecialPowers.Ci.nsILoginManager);
michael@0 134 ok(pwmgr != null, "Access LoginManager");
michael@0 135
michael@0 136
michael@0 137 // Check that initial state has no logins
michael@0 138 var logins = pwmgr.getAllLogins();
michael@0 139 if (logins.length) {
michael@0 140 //todo(false, "Warning: wasn't expecting logins to be present.");
michael@0 141 pwmgr.removeAllLogins();
michael@0 142 }
michael@0 143 var disabledHosts = pwmgr.getAllDisabledHosts();
michael@0 144 if (disabledHosts.length) {
michael@0 145 //todo(false, "Warning: wasn't expecting disabled hosts to be present.");
michael@0 146 for (var host of disabledHosts)
michael@0 147 pwmgr.setLoginSavingEnabled(host, true);
michael@0 148 }
michael@0 149
michael@0 150 // Add a login that's used in multiple tests
michael@0 151 var login = SpecialPowers.Cc["@mozilla.org/login-manager/loginInfo;1"].
michael@0 152 createInstance(SpecialPowers.Ci.nsILoginInfo);
michael@0 153 login.init("http://mochi.test:8888", "http://mochi.test:8888", null,
michael@0 154 "testuser", "testpass", "uname", "pword");
michael@0 155 pwmgr.addLogin(login);
michael@0 156
michael@0 157 // Last sanity check
michael@0 158 logins = pwmgr.getAllLogins();
michael@0 159 is(logins.length, 1, "Checking for successful init login");
michael@0 160 disabledHosts = pwmgr.getAllDisabledHosts();
michael@0 161 is(disabledHosts.length, 0, "Checking for no disabled hosts");
michael@0 162 }
michael@0 163
michael@0 164 const masterPassword = "omgsecret!";
michael@0 165
michael@0 166 function enableMasterPassword() {
michael@0 167 setMasterPassword(true);
michael@0 168 }
michael@0 169
michael@0 170 function disableMasterPassword() {
michael@0 171 setMasterPassword(false);
michael@0 172 }
michael@0 173
michael@0 174 function setMasterPassword(enable) {
michael@0 175 var oldPW, newPW;
michael@0 176 if (enable) {
michael@0 177 oldPW = "";
michael@0 178 newPW = masterPassword;
michael@0 179 } else {
michael@0 180 oldPW = masterPassword;
michael@0 181 newPW = "";
michael@0 182 }
michael@0 183 // Set master password. Note that this does not log you in, so the next
michael@0 184 // invocation of pwmgr can trigger a MP prompt.
michael@0 185
michael@0 186 var pk11db = Cc["@mozilla.org/security/pk11tokendb;1"].
michael@0 187 getService(Ci.nsIPK11TokenDB)
michael@0 188 var token = pk11db.findTokenByName("");
michael@0 189 ok(true, "change from " + oldPW + " to " + newPW);
michael@0 190 token.changePassword(oldPW, newPW);
michael@0 191 }
michael@0 192
michael@0 193 function logoutMasterPassword() {
michael@0 194 var sdr = Cc["@mozilla.org/security/sdr;1"].
michael@0 195 getService(Ci.nsISecretDecoderRing);
michael@0 196 sdr.logoutAndTeardown();
michael@0 197 }
michael@0 198
michael@0 199 function dumpLogins(pwmgr) {
michael@0 200 var logins = pwmgr.getAllLogins();
michael@0 201 ok(true, "----- dumpLogins: have " + logins.length + " logins. -----");
michael@0 202 for (var i = 0; i < logins.length; i++)
michael@0 203 dumpLogin("login #" + i + " --- ", logins[i]);
michael@0 204 }
michael@0 205
michael@0 206 function dumpLogin(label, login) {
michael@0 207 loginText = "";
michael@0 208 loginText += "host: ";
michael@0 209 loginText += login.hostname;
michael@0 210 loginText += " / formURL: ";
michael@0 211 loginText += login.formSubmitURL;
michael@0 212 loginText += " / realm: ";
michael@0 213 loginText += login.httpRealm;
michael@0 214 loginText += " / user: ";
michael@0 215 loginText += login.username;
michael@0 216 loginText += " / pass: ";
michael@0 217 loginText += login.password;
michael@0 218 loginText += " / ufield: ";
michael@0 219 loginText += login.usernameField;
michael@0 220 loginText += " / pfield: ";
michael@0 221 loginText += login.passwordField;
michael@0 222 ok(true, label + loginText);
michael@0 223 }

mercurial