DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5b81998bb7ab)

VCS Links

Line Code
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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for Login Manager</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="pwmgr_common.js"></script>
  <script type="text/javascript" src="prompt_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
Login Manager test: master password.
<p id="display"></p>

<div id="content" style="display: none">
<iframe id="iframe1"></iframe>
<iframe id="iframe2"></iframe>
</div>

<pre id="test">
<script class="testbody" type="text/javascript">

commonInit();
var testNum = 1;

/*
 * handleDialog
 *
 * Invoked a short period of time after calling startCallbackTimer(), and
 * allows testing the actual auth dialog while it's being displayed. Tests
 * should call startCallbackTimer() each time the auth dialog is expected (the
 * timer is a one-shot).
 */
function handleDialog(doc, testNum) {
  netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
  ok(true, "handleDialog running for test " + testNum);

  var clickOK   = true;
  var doNothing = false;
  var passfield = doc.getElementById("password1Textbox");
  var dialog    = doc.getElementById("commonDialog");

    switch(testNum) {
      case 1:
        is(passfield.getAttribute("value"), "", "Checking empty prompt");
        passfield.setAttribute("value", masterPassword);
        is(passfield.getAttribute("value"), masterPassword, "Checking filled prompt");
        break;

      case 2:
        clickOK = false;
        break;

      case 3:
        is(passfield.getAttribute("value"), "", "Checking empty prompt");
        passfield.setAttribute("value", masterPassword);
        break;

      case 4:
        doNothing = true;
        break;

      case 5:
        is(passfield.getAttribute("value"), "", "Checking empty prompt");
        passfield.setAttribute("value", masterPassword);
        break;

      default:
        ok(false, "Uhh, unhandled switch for testNum #" + testNum);
        break;
    }

    if (!doNothing) {
        if (clickOK)
            dialog.acceptDialog();
        else
            dialog.cancelDialog();
    }

    ok(true, "handleDialog done");
    didDialog = true;

    if (testNum == 4)
        checkTest4A();
}


function startTest1() {
    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

    ok(pwcrypt.isLoggedIn, "should be initially logged in (no MP)");
    enableMasterPassword();
    ok(!pwcrypt.isLoggedIn, "should be logged out after setting MP");

    // --- Test 1 ---
    // Trigger a MP prompt via the API
    startCallbackTimer();
    var logins = pwmgr.getAllLogins();
    ok(didDialog, "handleDialog was invoked");
    is(logins.length, 3, "expected number of logins");

    ok(pwcrypt.isLoggedIn, "should be logged in after MP prompt");
    logoutMasterPassword();
    ok(!pwcrypt.isLoggedIn, "should be logged out");

    // --- Test 2 ---
    // Try again but click cancel.
    testNum++;
    startCallbackTimer();
    var failedAsExpected = false;
    logins = null;
    try {
        logins = pwmgr.getAllLogins();
    } catch (e) { failedAsExpected = true; }
    ok(didDialog, "handleDialog was invoked");
    ok(failedAsExpected, "getAllLogins should have thrown");
    is(logins, null, "shouldn't have gotten logins");
    ok(!pwcrypt.isLoggedIn, "should still be logged out");

    // --- Test 3 ---
    // Load a single iframe to trigger a MP
    testNum++;

    // Note that because DOMContentLoaded is dispatched synchronously, the
    // document's load event is blocked until after the MP entry (because
    // pwmgr's listener doesn't return until after it processes the form,
    // which is blocked waiting on a MP entry).
    iframe1.onload = checkTest3;
    iframe1.src = exampleCom + "subtst_master_pass.html";
    startCallbackTimer();
}

function checkTest3() {
    ok(true, "checkTest3 starting");
    ok(didDialog, "handleDialog was invoked");

    // check contents of iframe1 fields
    var u = SpecialPowers.wrap(iframe1).contentDocument.getElementById("userfield");
    var p = SpecialPowers.wrap(iframe1).contentDocument.getElementById("passfield");
    is(u.value, "user1", "checking expected user to have been filled in");
    is(p.value, "pass1", "checking expected pass to have been filled in");

    ok(pwcrypt.isLoggedIn, "should be logged in");
    logoutMasterPassword();
    ok(!pwcrypt.isLoggedIn, "should be logged out");


    // --- Test 4 ---
    // first part of loading 2 MP-triggering iframes
    testNum++;
    iframe1.onload = checkTest4C;
    iframe1.src = exampleOrg + "subtst_master_pass.html";
    // start the callback, but we'll not enter the MP, just call checkTest4A
    startCallbackTimer();
}

function checkTest4A() {
    ok(true, "checkTest4A starting");
    ok(didDialog, "handleDialog was invoked");

    // check contents of iframe1 fields
    var u = SpecialPowers.wrap(iframe1).contentDocument.getElementById("userfield");
    var p = SpecialPowers.wrap(iframe1).contentDocument.getElementById("passfield");
    is(u.value, "", "checking expected empty user");
    is(p.value, "", "checking expected empty pass");


    ok(!pwcrypt.isLoggedIn, "should be logged out");

    // XXX check that there's 1 MP window open

    // Load another iframe with a login form
    // This should detect that there's already a pending MP prompt, and not
    // put up a second one. The load event will fire.
    iframe2.onload = checkTest4B;
    iframe2.src = exampleCom + "subtst_master_pass.html";
}

function checkTest4B() {
    ok(true, "checkTest4B starting");
    // iframe2 should load without having triggered a MP prompt (because one
    // is already waiting)

    // check contents of iframe2 fields
    var u = SpecialPowers.wrap(iframe2).contentDocument.getElementById("userfield");
    var p = SpecialPowers.wrap(iframe2).contentDocument.getElementById("passfield");
    is(u.value, "", "checking expected empty user");
    is(p.value, "", "checking expected empty pass");

    // XXX check that there's 1 MP window open
    ok(!pwcrypt.isLoggedIn, "should be logged out");

    // Ok, now enter the MP. The MP prompt is already up, but we'll just reuse startCallBackTimer.
    // --- Test 5 ---
    testNum++;
    startCallbackTimer();
}

function checkTest4C() {
    // iframe1 finally loads after the MP entry.
    ok(true, "checkTest4C starting");
    ok(didDialog, "handleDialog was invoked");

    // We shouldn't have to worry about iframe1's load event racing with
    // filling of iframe2's data. We notify observers synchronously, so
    // iframe2's observer will process iframe2 before iframe1 even finishes
    // processing the form (which is blocking its load event).
    ok(pwcrypt.isLoggedIn, "should be logged in");

    // check contents of iframe1 fields
    var u = SpecialPowers.wrap(iframe1).contentDocument.getElementById("userfield");
    var p = SpecialPowers.wrap(iframe1).contentDocument.getElementById("passfield");
    is(u.value, "user2", "checking expected user to have been filled in");
    is(p.value, "pass2", "checking expected pass to have been filled in");

    // check contents of iframe2 fields
    u = SpecialPowers.wrap(iframe2).contentDocument.getElementById("userfield");
    p = SpecialPowers.wrap(iframe2).contentDocument.getElementById("passfield");
    is(u.value, "user1", "checking expected user to have been filled in");
    is(p.value, "pass1", "checking expected pass to have been filled in");

    finishTest();
}

// XXX do a test5ABC with clicking cancel?

function finishTest() {
    disableMasterPassword();
    ok(pwcrypt.isLoggedIn, "should be logged in");

    pwmgr.removeLogin(login1);
    pwmgr.removeLogin(login2);
    SimpleTest.finish();
}


netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

// Get the pwmgr service
var pwmgr = Cc["@mozilla.org/login-manager;1"].
            getService(Ci.nsILoginManager);
ok(pwmgr != null, "pwmgr getService()");

var pwcrypt = Cc["@mozilla.org/login-manager/crypto/SDR;1"].
             getService(Ci.nsILoginManagerCrypto);
ok(pwcrypt != null, "pwcrypt getService()");

var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo);
ok(nsLoginInfo != null, "nsLoginInfo constructor");

var exampleCom = "http://example.com/tests/toolkit/components/passwordmgr/test/";
var exampleOrg = "http://example.org/tests/toolkit/components/passwordmgr/test/";

var login1 = new nsLoginInfo();
var login2 = new nsLoginInfo();

login1.init("http://example.com", "http://example.com", null,
            "user1", "pass1", "uname", "pword");
login2.init("http://example.org", "http://example.org", null,
            "user2", "pass2", "uname", "pword");

pwmgr.addLogin(login1);
pwmgr.addLogin(login2);

var iframe1 = document.getElementById("iframe1");
var iframe2 = document.getElementById("iframe2");

window.onload = startTest1;

SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>