toolkit/components/passwordmgr/test/test_master_password.html

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 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <title>Test for Login Manager</title>
michael@0 5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 6 <script type="text/javascript" src="pwmgr_common.js"></script>
michael@0 7 <script type="text/javascript" src="prompt_common.js"></script>
michael@0 8 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 9 </head>
michael@0 10 <body>
michael@0 11 Login Manager test: master password.
michael@0 12 <script>
michael@0 13 "use strict";
michael@0 14
michael@0 15 commonInit();
michael@0 16 SimpleTest.waitForExplicitFinish();
michael@0 17
michael@0 18 var pwmgr = SpecialPowers.Cc["@mozilla.org/login-manager;1"]
michael@0 19 .getService(SpecialPowers.Ci.nsILoginManager);
michael@0 20 var pwcrypt = SpecialPowers.Cc["@mozilla.org/login-manager/crypto/SDR;1"]
michael@0 21 .getService(Ci.nsILoginManagerCrypto);
michael@0 22
michael@0 23 var nsLoginInfo = new SpecialPowers.wrap(SpecialPowers.Components).Constructor("@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo);
michael@0 24
michael@0 25 var exampleCom = "http://example.com/tests/toolkit/components/passwordmgr/test/";
michael@0 26 var exampleOrg = "http://example.org/tests/toolkit/components/passwordmgr/test/";
michael@0 27
michael@0 28 var login1 = new nsLoginInfo();
michael@0 29 var login2 = new nsLoginInfo();
michael@0 30
michael@0 31 login1.init("http://example.com", "http://example.com", null,
michael@0 32 "user1", "pass1", "uname", "pword");
michael@0 33 login2.init("http://example.org", "http://example.org", null,
michael@0 34 "user2", "pass2", "uname", "pword");
michael@0 35
michael@0 36 pwmgr.addLogin(login1);
michael@0 37 pwmgr.addLogin(login2);
michael@0 38 </script>
michael@0 39
michael@0 40 <p id="display"></p>
michael@0 41
michael@0 42 <div id="content" style="display: none">
michael@0 43 <iframe id="iframe1"></iframe>
michael@0 44 <iframe id="iframe2"></iframe>
michael@0 45 </div>
michael@0 46
michael@0 47 <pre id="test">
michael@0 48 <script class="testbody" type="text/javascript">
michael@0 49 var testNum = 1;
michael@0 50 var iframe1 = document.getElementById("iframe1");
michael@0 51 var iframe2 = document.getElementById("iframe2");
michael@0 52
michael@0 53 /*
michael@0 54 * handleDialog
michael@0 55 *
michael@0 56 * Invoked a short period of time after calling startCallbackTimer(), and
michael@0 57 * allows testing the actual auth dialog while it's being displayed. Tests
michael@0 58 * should call startCallbackTimer() each time the auth dialog is expected (the
michael@0 59 * timer is a one-shot).
michael@0 60 */
michael@0 61 function handleDialog(doc, testNum) {
michael@0 62 ok(true, "handleDialog running for test " + testNum);
michael@0 63
michael@0 64 var clickOK = true;
michael@0 65 var doNothing = false;
michael@0 66 var passfield = doc.getElementById("password1Textbox");
michael@0 67 var dialog = doc.getElementById("commonDialog");
michael@0 68
michael@0 69 switch(testNum) {
michael@0 70 case 1:
michael@0 71 is(passfield.getAttribute("value"), "", "Checking empty prompt");
michael@0 72 passfield.setAttribute("value", masterPassword);
michael@0 73 is(passfield.getAttribute("value"), masterPassword, "Checking filled prompt");
michael@0 74 break;
michael@0 75
michael@0 76 case 2:
michael@0 77 clickOK = false;
michael@0 78 break;
michael@0 79
michael@0 80 case 3:
michael@0 81 is(passfield.getAttribute("value"), "", "Checking empty prompt");
michael@0 82 passfield.setAttribute("value", masterPassword);
michael@0 83 break;
michael@0 84
michael@0 85 case 4:
michael@0 86 doNothing = true;
michael@0 87 break;
michael@0 88
michael@0 89 case 5:
michael@0 90 is(passfield.getAttribute("value"), "", "Checking empty prompt");
michael@0 91 passfield.setAttribute("value", masterPassword);
michael@0 92 break;
michael@0 93
michael@0 94 default:
michael@0 95 ok(false, "Uhh, unhandled switch for testNum #" + testNum);
michael@0 96 break;
michael@0 97 }
michael@0 98
michael@0 99 didDialog = true;
michael@0 100
michael@0 101 if (!doNothing) {
michael@0 102 SpecialPowers.addObserver(outerWindowObserver, "outer-window-destroyed", false);
michael@0 103 if (clickOK)
michael@0 104 dialog.acceptDialog();
michael@0 105 else
michael@0 106 dialog.cancelDialog();
michael@0 107 }
michael@0 108
michael@0 109 ok(true, "handleDialog done for test " + testNum);
michael@0 110
michael@0 111 if (testNum == 4)
michael@0 112 checkTest4A();
michael@0 113 }
michael@0 114
michael@0 115 var outerWindowObserver = {
michael@0 116 observe: function(id) {
michael@0 117 SpecialPowers.removeObserver(outerWindowObserver, "outer-window-destroyed");
michael@0 118 if (testNum == 1)
michael@0 119 startTest2();
michael@0 120 else if (testNum == 2)
michael@0 121 startTest3();
michael@0 122 else if (testNum == 3)
michael@0 123 checkTest3();
michael@0 124 else if (testNum == 5)
michael@0 125 checkTest4C();
michael@0 126 }
michael@0 127 };
michael@0 128
michael@0 129
michael@0 130 function startTest1() {
michael@0 131 ok(pwcrypt.isLoggedIn, "should be initially logged in (no MP)");
michael@0 132 enableMasterPassword();
michael@0 133 ok(!pwcrypt.isLoggedIn, "should be logged out after setting MP");
michael@0 134
michael@0 135 // --- Test 1 ---
michael@0 136 // Trigger a MP prompt via the API
michael@0 137 startCallbackTimer();
michael@0 138 var logins = pwmgr.getAllLogins();
michael@0 139 ok(didDialog, "handleDialog was invoked");
michael@0 140 is(logins.length, 3, "expected number of logins");
michael@0 141
michael@0 142 ok(pwcrypt.isLoggedIn, "should be logged in after MP prompt");
michael@0 143 logoutMasterPassword();
michael@0 144 ok(!pwcrypt.isLoggedIn, "should be logged out");
michael@0 145 }
michael@0 146
michael@0 147 function startTest2() {
michael@0 148 // Try again but click cancel.
michael@0 149 testNum++;
michael@0 150 startCallbackTimer();
michael@0 151 var failedAsExpected = false;
michael@0 152 logins = null;
michael@0 153 try {
michael@0 154 logins = pwmgr.getAllLogins();
michael@0 155 } catch (e) { failedAsExpected = true; }
michael@0 156 ok(didDialog, "handleDialog was invoked");
michael@0 157 ok(failedAsExpected, "getAllLogins should have thrown");
michael@0 158 is(logins, null, "shouldn't have gotten logins");
michael@0 159 ok(!pwcrypt.isLoggedIn, "should still be logged out");
michael@0 160 }
michael@0 161
michael@0 162 function startTest3() {
michael@0 163 // Load a single iframe to trigger a MP
michael@0 164 testNum++;
michael@0 165 iframe1.src = exampleCom + "subtst_master_pass.html";
michael@0 166 startCallbackTimer();
michael@0 167 }
michael@0 168
michael@0 169 function checkTest3() {
michael@0 170 ok(true, "checkTest3 starting");
michael@0 171 ok(didDialog, "handleDialog was invoked");
michael@0 172
michael@0 173 // check contents of iframe1 fields
michael@0 174 var u = SpecialPowers.wrap(iframe1).contentDocument.getElementById("userfield");
michael@0 175 var p = SpecialPowers.wrap(iframe1).contentDocument.getElementById("passfield");
michael@0 176 is(u.value, "user1", "checking expected user to have been filled in");
michael@0 177 is(p.value, "pass1", "checking expected pass to have been filled in");
michael@0 178
michael@0 179 ok(pwcrypt.isLoggedIn, "should be logged in");
michael@0 180 logoutMasterPassword();
michael@0 181 ok(!pwcrypt.isLoggedIn, "should be logged out");
michael@0 182
michael@0 183
michael@0 184 // --- Test 4 ---
michael@0 185 // first part of loading 2 MP-triggering iframes
michael@0 186 testNum++;
michael@0 187 iframe1.src = exampleOrg + "subtst_master_pass.html";
michael@0 188 // start the callback, but we'll not enter the MP, just call checkTest4A
michael@0 189 startCallbackTimer();
michael@0 190 }
michael@0 191
michael@0 192 function checkTest4A() {
michael@0 193 ok(true, "checkTest4A starting");
michael@0 194 ok(didDialog, "handleDialog was invoked");
michael@0 195
michael@0 196 // check contents of iframe1 fields
michael@0 197 var u = SpecialPowers.wrap(iframe1).contentDocument.getElementById("userfield");
michael@0 198 var p = SpecialPowers.wrap(iframe1).contentDocument.getElementById("passfield");
michael@0 199 is(u.value, "", "checking expected empty user");
michael@0 200 is(p.value, "", "checking expected empty pass");
michael@0 201
michael@0 202
michael@0 203 ok(!pwcrypt.isLoggedIn, "should be logged out");
michael@0 204
michael@0 205 // XXX check that there's 1 MP window open
michael@0 206
michael@0 207 // Load another iframe with a login form
michael@0 208 // This should detect that there's already a pending MP prompt, and not
michael@0 209 // put up a second one. The load event will fire (note that when pwmgr is
michael@0 210 // driven from DOMContentLoaded, if that blocks due to prompting for a MP,
michael@0 211 // the load even will also be blocked until the prompt is dismissed).
michael@0 212 iframe2.onload = checkTest4B;
michael@0 213 iframe2.src = exampleCom + "subtst_master_pass.html";
michael@0 214 }
michael@0 215
michael@0 216 function checkTest4B() {
michael@0 217 ok(true, "checkTest4B starting");
michael@0 218 // iframe2 should load without having triggered a MP prompt (because one
michael@0 219 // is already waiting)
michael@0 220
michael@0 221 // check contents of iframe2 fields
michael@0 222 var u = SpecialPowers.wrap(iframe2).contentDocument.getElementById("userfield");
michael@0 223 var p = SpecialPowers.wrap(iframe2).contentDocument.getElementById("passfield");
michael@0 224 is(u.value, "", "checking expected empty user");
michael@0 225 is(p.value, "", "checking expected empty pass");
michael@0 226
michael@0 227 // XXX check that there's 1 MP window open
michael@0 228 ok(!pwcrypt.isLoggedIn, "should be logged out");
michael@0 229
michael@0 230 // Ok, now enter the MP. The MP prompt is already up, but we'll just reuse startCallBackTimer.
michael@0 231 // --- Test 5 ---
michael@0 232 testNum++;
michael@0 233 startCallbackTimer();
michael@0 234 }
michael@0 235
michael@0 236 function checkTest4C() {
michael@0 237 ok(true, "checkTest4C starting");
michael@0 238 ok(didDialog, "handleDialog was invoked");
michael@0 239
michael@0 240 // We shouldn't have to worry about iframe1's load event racing with
michael@0 241 // filling of iframe2's data. We notify observers synchronously, so
michael@0 242 // iframe2's observer will process iframe2 before iframe1 even finishes
michael@0 243 // processing the form (which is blocking its load event).
michael@0 244 ok(pwcrypt.isLoggedIn, "should be logged in");
michael@0 245
michael@0 246 // check contents of iframe1 fields
michael@0 247 var u = SpecialPowers.wrap(iframe1).contentDocument.getElementById("userfield");
michael@0 248 var p = SpecialPowers.wrap(iframe1).contentDocument.getElementById("passfield");
michael@0 249 is(u.value, "user2", "checking expected user to have been filled in");
michael@0 250 is(p.value, "pass2", "checking expected pass to have been filled in");
michael@0 251
michael@0 252 // check contents of iframe2 fields
michael@0 253 u = SpecialPowers.wrap(iframe2).contentDocument.getElementById("userfield");
michael@0 254 p = SpecialPowers.wrap(iframe2).contentDocument.getElementById("passfield");
michael@0 255 is(u.value, "user1", "checking expected user to have been filled in");
michael@0 256 is(p.value, "pass1", "checking expected pass to have been filled in");
michael@0 257
michael@0 258 finishTest();
michael@0 259 }
michael@0 260
michael@0 261 // XXX do a test5ABC with clicking cancel?
michael@0 262
michael@0 263 function finishTest() {
michael@0 264 disableMasterPassword();
michael@0 265 ok(pwcrypt.isLoggedIn, "should be logged in");
michael@0 266
michael@0 267 pwmgr.removeLogin(login1);
michael@0 268 pwmgr.removeLogin(login2);
michael@0 269 SimpleTest.finish();
michael@0 270 }
michael@0 271
michael@0 272 window.onload = startTest1;
michael@0 273 </script>
michael@0 274 </pre>
michael@0 275 </body>
michael@0 276 </html>
michael@0 277

mercurial