1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/passwordmgr/test/pwmgr_common.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,223 @@ 1.4 +/* 1.5 + * $_ 1.6 + * 1.7 + * Returns the element with the specified |name| attribute. 1.8 + */ 1.9 +function $_(formNum, name) { 1.10 + var form = document.getElementById("form" + formNum); 1.11 + if (!form) { 1.12 + logWarning("$_ couldn't find requested form " + formNum); 1.13 + return null; 1.14 + } 1.15 + 1.16 + var element = form.elements.namedItem(name); 1.17 + if (!element) { 1.18 + logWarning("$_ couldn't find requested element " + name); 1.19 + return null; 1.20 + } 1.21 + 1.22 + // Note that namedItem is a bit stupid, and will prefer an 1.23 + // |id| attribute over a |name| attribute when looking for 1.24 + // the element. Login Mananger happens to use .namedItem 1.25 + // anyway, but let's rigorously check it here anyway so 1.26 + // that we don't end up with tests that mistakenly pass. 1.27 + 1.28 + if (element.getAttribute("name") != name) { 1.29 + logWarning("$_ got confused."); 1.30 + return null; 1.31 + } 1.32 + 1.33 + return element; 1.34 +} 1.35 + 1.36 + 1.37 +/* 1.38 + * checkForm 1.39 + * 1.40 + * Check a form for expected values. If an argument is null, a field's 1.41 + * expected value will be the default value. 1.42 + * 1.43 + * <form id="form#"> 1.44 + * checkForm(#, "foo"); 1.45 + */ 1.46 +function checkForm(formNum, val1, val2, val3) { 1.47 + var e, form = document.getElementById("form" + formNum); 1.48 + ok(form, "Locating form " + formNum); 1.49 + 1.50 + var numToCheck = arguments.length - 1; 1.51 + 1.52 + if (!numToCheck--) 1.53 + return; 1.54 + e = form.elements[0]; 1.55 + if (val1 == null) 1.56 + is(e.value, e.defaultValue, "Test default value of field " + e.name + 1.57 + " in form " + formNum); 1.58 + else 1.59 + is(e.value, val1, "Test value of field " + e.name + 1.60 + " in form " + formNum); 1.61 + 1.62 + 1.63 + if (!numToCheck--) 1.64 + return; 1.65 + e = form.elements[1]; 1.66 + if (val2 == null) 1.67 + is(e.value, e.defaultValue, "Test default value of field " + e.name + 1.68 + " in form " + formNum); 1.69 + else 1.70 + is(e.value, val2, "Test value of field " + e.name + 1.71 + " in form " + formNum); 1.72 + 1.73 + 1.74 + if (!numToCheck--) 1.75 + return; 1.76 + e = form.elements[2]; 1.77 + if (val3 == null) 1.78 + is(e.value, e.defaultValue, "Test default value of field " + e.name + 1.79 + " in form " + formNum); 1.80 + else 1.81 + is(e.value, val3, "Test value of field " + e.name + 1.82 + " in form " + formNum); 1.83 + 1.84 +} 1.85 + 1.86 + 1.87 +/* 1.88 + * checkUnmodifiedForm 1.89 + * 1.90 + * Check a form for unmodified values from when page was loaded. 1.91 + * 1.92 + * <form id="form#"> 1.93 + * checkUnmodifiedForm(#); 1.94 + */ 1.95 +function checkUnmodifiedForm(formNum) { 1.96 + var form = document.getElementById("form" + formNum); 1.97 + ok(form, "Locating form " + formNum); 1.98 + 1.99 + for (var i = 0; i < form.elements.length; i++) { 1.100 + var ele = form.elements[i]; 1.101 + 1.102 + // No point in checking form submit/reset buttons. 1.103 + if (ele.type == "submit" || ele.type == "reset") 1.104 + continue; 1.105 + 1.106 + is(ele.value, ele.defaultValue, "Test to default value of field " + 1.107 + ele.name + " in form " + formNum); 1.108 + } 1.109 +} 1.110 + 1.111 + 1.112 +// Mochitest gives us a sendKey(), but it's targeted to a specific element. 1.113 +// This basically sends an untargeted key event, to whatever's focused. 1.114 +function doKey(aKey, modifier) { 1.115 + var keyName = "DOM_VK_" + aKey.toUpperCase(); 1.116 + var key = KeyEvent[keyName]; 1.117 + 1.118 + // undefined --> null 1.119 + if (!modifier) 1.120 + modifier = null; 1.121 + 1.122 + // Window utils for sending fake sey events. 1.123 + var wutils = SpecialPowers.wrap(window). 1.124 + QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor). 1.125 + getInterface(SpecialPowers.Ci.nsIDOMWindowUtils); 1.126 + 1.127 + if (wutils.sendKeyEvent("keydown", key, 0, modifier)) { 1.128 + wutils.sendKeyEvent("keypress", key, 0, modifier); 1.129 + } 1.130 + wutils.sendKeyEvent("keyup", key, 0, modifier); 1.131 +} 1.132 + 1.133 +// Init with a common login 1.134 +function commonInit() { 1.135 + var pwmgr = SpecialPowers.Cc["@mozilla.org/login-manager;1"]. 1.136 + getService(SpecialPowers.Ci.nsILoginManager); 1.137 + ok(pwmgr != null, "Access LoginManager"); 1.138 + 1.139 + 1.140 + // Check that initial state has no logins 1.141 + var logins = pwmgr.getAllLogins(); 1.142 + if (logins.length) { 1.143 + //todo(false, "Warning: wasn't expecting logins to be present."); 1.144 + pwmgr.removeAllLogins(); 1.145 + } 1.146 + var disabledHosts = pwmgr.getAllDisabledHosts(); 1.147 + if (disabledHosts.length) { 1.148 + //todo(false, "Warning: wasn't expecting disabled hosts to be present."); 1.149 + for (var host of disabledHosts) 1.150 + pwmgr.setLoginSavingEnabled(host, true); 1.151 + } 1.152 + 1.153 + // Add a login that's used in multiple tests 1.154 + var login = SpecialPowers.Cc["@mozilla.org/login-manager/loginInfo;1"]. 1.155 + createInstance(SpecialPowers.Ci.nsILoginInfo); 1.156 + login.init("http://mochi.test:8888", "http://mochi.test:8888", null, 1.157 + "testuser", "testpass", "uname", "pword"); 1.158 + pwmgr.addLogin(login); 1.159 + 1.160 + // Last sanity check 1.161 + logins = pwmgr.getAllLogins(); 1.162 + is(logins.length, 1, "Checking for successful init login"); 1.163 + disabledHosts = pwmgr.getAllDisabledHosts(); 1.164 + is(disabledHosts.length, 0, "Checking for no disabled hosts"); 1.165 +} 1.166 + 1.167 +const masterPassword = "omgsecret!"; 1.168 + 1.169 +function enableMasterPassword() { 1.170 + setMasterPassword(true); 1.171 +} 1.172 + 1.173 +function disableMasterPassword() { 1.174 + setMasterPassword(false); 1.175 +} 1.176 + 1.177 +function setMasterPassword(enable) { 1.178 + var oldPW, newPW; 1.179 + if (enable) { 1.180 + oldPW = ""; 1.181 + newPW = masterPassword; 1.182 + } else { 1.183 + oldPW = masterPassword; 1.184 + newPW = ""; 1.185 + } 1.186 + // Set master password. Note that this does not log you in, so the next 1.187 + // invocation of pwmgr can trigger a MP prompt. 1.188 + 1.189 + var pk11db = Cc["@mozilla.org/security/pk11tokendb;1"]. 1.190 + getService(Ci.nsIPK11TokenDB) 1.191 + var token = pk11db.findTokenByName(""); 1.192 + ok(true, "change from " + oldPW + " to " + newPW); 1.193 + token.changePassword(oldPW, newPW); 1.194 +} 1.195 + 1.196 +function logoutMasterPassword() { 1.197 + var sdr = Cc["@mozilla.org/security/sdr;1"]. 1.198 + getService(Ci.nsISecretDecoderRing); 1.199 + sdr.logoutAndTeardown(); 1.200 +} 1.201 + 1.202 +function dumpLogins(pwmgr) { 1.203 + var logins = pwmgr.getAllLogins(); 1.204 + ok(true, "----- dumpLogins: have " + logins.length + " logins. -----"); 1.205 + for (var i = 0; i < logins.length; i++) 1.206 + dumpLogin("login #" + i + " --- ", logins[i]); 1.207 +} 1.208 + 1.209 +function dumpLogin(label, login) { 1.210 + loginText = ""; 1.211 + loginText += "host: "; 1.212 + loginText += login.hostname; 1.213 + loginText += " / formURL: "; 1.214 + loginText += login.formSubmitURL; 1.215 + loginText += " / realm: "; 1.216 + loginText += login.httpRealm; 1.217 + loginText += " / user: "; 1.218 + loginText += login.username; 1.219 + loginText += " / pass: "; 1.220 + loginText += login.password; 1.221 + loginText += " / ufield: "; 1.222 + loginText += login.usernameField; 1.223 + loginText += " / pfield: "; 1.224 + loginText += login.passwordField; 1.225 + ok(true, label + loginText); 1.226 +}