1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/passwordmgr/test/test_master_password.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,277 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<head> 1.7 + <title>Test for Login Manager</title> 1.8 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.9 + <script type="text/javascript" src="pwmgr_common.js"></script> 1.10 + <script type="text/javascript" src="prompt_common.js"></script> 1.11 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.12 +</head> 1.13 +<body> 1.14 +Login Manager test: master password. 1.15 +<script> 1.16 +"use strict"; 1.17 + 1.18 +commonInit(); 1.19 +SimpleTest.waitForExplicitFinish(); 1.20 + 1.21 +var pwmgr = SpecialPowers.Cc["@mozilla.org/login-manager;1"] 1.22 + .getService(SpecialPowers.Ci.nsILoginManager); 1.23 +var pwcrypt = SpecialPowers.Cc["@mozilla.org/login-manager/crypto/SDR;1"] 1.24 + .getService(Ci.nsILoginManagerCrypto); 1.25 + 1.26 +var nsLoginInfo = new SpecialPowers.wrap(SpecialPowers.Components).Constructor("@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo); 1.27 + 1.28 +var exampleCom = "http://example.com/tests/toolkit/components/passwordmgr/test/"; 1.29 +var exampleOrg = "http://example.org/tests/toolkit/components/passwordmgr/test/"; 1.30 + 1.31 +var login1 = new nsLoginInfo(); 1.32 +var login2 = new nsLoginInfo(); 1.33 + 1.34 +login1.init("http://example.com", "http://example.com", null, 1.35 + "user1", "pass1", "uname", "pword"); 1.36 +login2.init("http://example.org", "http://example.org", null, 1.37 + "user2", "pass2", "uname", "pword"); 1.38 + 1.39 +pwmgr.addLogin(login1); 1.40 +pwmgr.addLogin(login2); 1.41 +</script> 1.42 + 1.43 +<p id="display"></p> 1.44 + 1.45 +<div id="content" style="display: none"> 1.46 +<iframe id="iframe1"></iframe> 1.47 +<iframe id="iframe2"></iframe> 1.48 +</div> 1.49 + 1.50 +<pre id="test"> 1.51 +<script class="testbody" type="text/javascript"> 1.52 +var testNum = 1; 1.53 +var iframe1 = document.getElementById("iframe1"); 1.54 +var iframe2 = document.getElementById("iframe2"); 1.55 + 1.56 +/* 1.57 + * handleDialog 1.58 + * 1.59 + * Invoked a short period of time after calling startCallbackTimer(), and 1.60 + * allows testing the actual auth dialog while it's being displayed. Tests 1.61 + * should call startCallbackTimer() each time the auth dialog is expected (the 1.62 + * timer is a one-shot). 1.63 + */ 1.64 +function handleDialog(doc, testNum) { 1.65 + ok(true, "handleDialog running for test " + testNum); 1.66 + 1.67 + var clickOK = true; 1.68 + var doNothing = false; 1.69 + var passfield = doc.getElementById("password1Textbox"); 1.70 + var dialog = doc.getElementById("commonDialog"); 1.71 + 1.72 + switch(testNum) { 1.73 + case 1: 1.74 + is(passfield.getAttribute("value"), "", "Checking empty prompt"); 1.75 + passfield.setAttribute("value", masterPassword); 1.76 + is(passfield.getAttribute("value"), masterPassword, "Checking filled prompt"); 1.77 + break; 1.78 + 1.79 + case 2: 1.80 + clickOK = false; 1.81 + break; 1.82 + 1.83 + case 3: 1.84 + is(passfield.getAttribute("value"), "", "Checking empty prompt"); 1.85 + passfield.setAttribute("value", masterPassword); 1.86 + break; 1.87 + 1.88 + case 4: 1.89 + doNothing = true; 1.90 + break; 1.91 + 1.92 + case 5: 1.93 + is(passfield.getAttribute("value"), "", "Checking empty prompt"); 1.94 + passfield.setAttribute("value", masterPassword); 1.95 + break; 1.96 + 1.97 + default: 1.98 + ok(false, "Uhh, unhandled switch for testNum #" + testNum); 1.99 + break; 1.100 + } 1.101 + 1.102 + didDialog = true; 1.103 + 1.104 + if (!doNothing) { 1.105 + SpecialPowers.addObserver(outerWindowObserver, "outer-window-destroyed", false); 1.106 + if (clickOK) 1.107 + dialog.acceptDialog(); 1.108 + else 1.109 + dialog.cancelDialog(); 1.110 + } 1.111 + 1.112 + ok(true, "handleDialog done for test " + testNum); 1.113 + 1.114 + if (testNum == 4) 1.115 + checkTest4A(); 1.116 +} 1.117 + 1.118 +var outerWindowObserver = { 1.119 + observe: function(id) { 1.120 + SpecialPowers.removeObserver(outerWindowObserver, "outer-window-destroyed"); 1.121 + if (testNum == 1) 1.122 + startTest2(); 1.123 + else if (testNum == 2) 1.124 + startTest3(); 1.125 + else if (testNum == 3) 1.126 + checkTest3(); 1.127 + else if (testNum == 5) 1.128 + checkTest4C(); 1.129 + } 1.130 +}; 1.131 + 1.132 + 1.133 +function startTest1() { 1.134 + ok(pwcrypt.isLoggedIn, "should be initially logged in (no MP)"); 1.135 + enableMasterPassword(); 1.136 + ok(!pwcrypt.isLoggedIn, "should be logged out after setting MP"); 1.137 + 1.138 + // --- Test 1 --- 1.139 + // Trigger a MP prompt via the API 1.140 + startCallbackTimer(); 1.141 + var logins = pwmgr.getAllLogins(); 1.142 + ok(didDialog, "handleDialog was invoked"); 1.143 + is(logins.length, 3, "expected number of logins"); 1.144 + 1.145 + ok(pwcrypt.isLoggedIn, "should be logged in after MP prompt"); 1.146 + logoutMasterPassword(); 1.147 + ok(!pwcrypt.isLoggedIn, "should be logged out"); 1.148 +} 1.149 + 1.150 +function startTest2() { 1.151 + // Try again but click cancel. 1.152 + testNum++; 1.153 + startCallbackTimer(); 1.154 + var failedAsExpected = false; 1.155 + logins = null; 1.156 + try { 1.157 + logins = pwmgr.getAllLogins(); 1.158 + } catch (e) { failedAsExpected = true; } 1.159 + ok(didDialog, "handleDialog was invoked"); 1.160 + ok(failedAsExpected, "getAllLogins should have thrown"); 1.161 + is(logins, null, "shouldn't have gotten logins"); 1.162 + ok(!pwcrypt.isLoggedIn, "should still be logged out"); 1.163 +} 1.164 + 1.165 +function startTest3() { 1.166 + // Load a single iframe to trigger a MP 1.167 + testNum++; 1.168 + iframe1.src = exampleCom + "subtst_master_pass.html"; 1.169 + startCallbackTimer(); 1.170 +} 1.171 + 1.172 +function checkTest3() { 1.173 + ok(true, "checkTest3 starting"); 1.174 + ok(didDialog, "handleDialog was invoked"); 1.175 + 1.176 + // check contents of iframe1 fields 1.177 + var u = SpecialPowers.wrap(iframe1).contentDocument.getElementById("userfield"); 1.178 + var p = SpecialPowers.wrap(iframe1).contentDocument.getElementById("passfield"); 1.179 + is(u.value, "user1", "checking expected user to have been filled in"); 1.180 + is(p.value, "pass1", "checking expected pass to have been filled in"); 1.181 + 1.182 + ok(pwcrypt.isLoggedIn, "should be logged in"); 1.183 + logoutMasterPassword(); 1.184 + ok(!pwcrypt.isLoggedIn, "should be logged out"); 1.185 + 1.186 + 1.187 + // --- Test 4 --- 1.188 + // first part of loading 2 MP-triggering iframes 1.189 + testNum++; 1.190 + iframe1.src = exampleOrg + "subtst_master_pass.html"; 1.191 + // start the callback, but we'll not enter the MP, just call checkTest4A 1.192 + startCallbackTimer(); 1.193 +} 1.194 + 1.195 +function checkTest4A() { 1.196 + ok(true, "checkTest4A starting"); 1.197 + ok(didDialog, "handleDialog was invoked"); 1.198 + 1.199 + // check contents of iframe1 fields 1.200 + var u = SpecialPowers.wrap(iframe1).contentDocument.getElementById("userfield"); 1.201 + var p = SpecialPowers.wrap(iframe1).contentDocument.getElementById("passfield"); 1.202 + is(u.value, "", "checking expected empty user"); 1.203 + is(p.value, "", "checking expected empty pass"); 1.204 + 1.205 + 1.206 + ok(!pwcrypt.isLoggedIn, "should be logged out"); 1.207 + 1.208 + // XXX check that there's 1 MP window open 1.209 + 1.210 + // Load another iframe with a login form 1.211 + // This should detect that there's already a pending MP prompt, and not 1.212 + // put up a second one. The load event will fire (note that when pwmgr is 1.213 + // driven from DOMContentLoaded, if that blocks due to prompting for a MP, 1.214 + // the load even will also be blocked until the prompt is dismissed). 1.215 + iframe2.onload = checkTest4B; 1.216 + iframe2.src = exampleCom + "subtst_master_pass.html"; 1.217 +} 1.218 + 1.219 +function checkTest4B() { 1.220 + ok(true, "checkTest4B starting"); 1.221 + // iframe2 should load without having triggered a MP prompt (because one 1.222 + // is already waiting) 1.223 + 1.224 + // check contents of iframe2 fields 1.225 + var u = SpecialPowers.wrap(iframe2).contentDocument.getElementById("userfield"); 1.226 + var p = SpecialPowers.wrap(iframe2).contentDocument.getElementById("passfield"); 1.227 + is(u.value, "", "checking expected empty user"); 1.228 + is(p.value, "", "checking expected empty pass"); 1.229 + 1.230 + // XXX check that there's 1 MP window open 1.231 + ok(!pwcrypt.isLoggedIn, "should be logged out"); 1.232 + 1.233 + // Ok, now enter the MP. The MP prompt is already up, but we'll just reuse startCallBackTimer. 1.234 + // --- Test 5 --- 1.235 + testNum++; 1.236 + startCallbackTimer(); 1.237 +} 1.238 + 1.239 +function checkTest4C() { 1.240 + ok(true, "checkTest4C starting"); 1.241 + ok(didDialog, "handleDialog was invoked"); 1.242 + 1.243 + // We shouldn't have to worry about iframe1's load event racing with 1.244 + // filling of iframe2's data. We notify observers synchronously, so 1.245 + // iframe2's observer will process iframe2 before iframe1 even finishes 1.246 + // processing the form (which is blocking its load event). 1.247 + ok(pwcrypt.isLoggedIn, "should be logged in"); 1.248 + 1.249 + // check contents of iframe1 fields 1.250 + var u = SpecialPowers.wrap(iframe1).contentDocument.getElementById("userfield"); 1.251 + var p = SpecialPowers.wrap(iframe1).contentDocument.getElementById("passfield"); 1.252 + is(u.value, "user2", "checking expected user to have been filled in"); 1.253 + is(p.value, "pass2", "checking expected pass to have been filled in"); 1.254 + 1.255 + // check contents of iframe2 fields 1.256 + u = SpecialPowers.wrap(iframe2).contentDocument.getElementById("userfield"); 1.257 + p = SpecialPowers.wrap(iframe2).contentDocument.getElementById("passfield"); 1.258 + is(u.value, "user1", "checking expected user to have been filled in"); 1.259 + is(p.value, "pass1", "checking expected pass to have been filled in"); 1.260 + 1.261 + finishTest(); 1.262 +} 1.263 + 1.264 +// XXX do a test5ABC with clicking cancel? 1.265 + 1.266 +function finishTest() { 1.267 + disableMasterPassword(); 1.268 + ok(pwcrypt.isLoggedIn, "should be logged in"); 1.269 + 1.270 + pwmgr.removeLogin(login1); 1.271 + pwmgr.removeLogin(login2); 1.272 + SimpleTest.finish(); 1.273 +} 1.274 + 1.275 +window.onload = startTest1; 1.276 +</script> 1.277 +</pre> 1.278 +</body> 1.279 +</html> 1.280 +