toolkit/components/passwordmgr/test/test_xhr.html

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 <!DOCTYPE HTML>
     2 <html>
     3 <head>
     4   <title>Test for Login Manager</title>
     5   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>  
     6   <script type="text/javascript" src="pwmgr_common.js"></script>
     7   <script type="text/javascript" src="prompt_common.js"></script>
     8   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     9 </head>
    10 <body>
    11 Login Manager test: XHR prompt
    12 <p id="display"></p>
    14 <div id="content" style="display: none">
    15   <iframe id="iframe"></iframe>
    16 </div>
    18 <pre id="test">
    19 <script class="testbody" type="text/javascript">
    21 /** Test for Login Manager: XHR prompts. **/
    22 var pwmgr, login1, login2;
    24 function initLogins() {
    25   pwmgr = Cc["@mozilla.org/login-manager;1"].
    26           getService(Ci.nsILoginManager);
    28   login1 = Cc["@mozilla.org/login-manager/loginInfo;1"].
    29             createInstance(Ci.nsILoginInfo);
    30   login2 = Cc["@mozilla.org/login-manager/loginInfo;1"].
    31             createInstance(Ci.nsILoginInfo);
    33   login1.init("http://mochi.test:8888", null, "xhr",
    34                "xhruser1", "xhrpass1", "", "");
    35   login2.init("http://mochi.test:8888", null, "xhr2",
    36                "xhruser2", "xhrpass2", "", "");
    38   pwmgr.addLogin(login1);
    39   pwmgr.addLogin(login2);
    40 }
    42 function finishTest() {
    43   ok(true, "finishTest removing testing logins...");
    44   pwmgr.removeLogin(login1);
    45   pwmgr.removeLogin(login2);
    47   SimpleTest.finish();
    48 }
    50 function handleDialog(doc, testNum) {
    51   ok(true, "handleDialog running for test " + testNum);
    53   var clickOK = true;
    54   var userfield = doc.getElementById("loginTextbox");
    55   var passfield = doc.getElementById("password1Textbox");
    56   var username = userfield.getAttribute("value");
    57   var password = passfield.getAttribute("value");
    58   var dialog    = doc.getElementById("commonDialog");
    60   switch(testNum) {
    61     case 1:
    62         is(username, "xhruser1", "Checking provided username");
    63         is(password, "xhrpass1", "Checking provided password");
    64         break;
    66     case 2:
    67         is(username, "xhruser2", "Checking provided username");
    68         is(password, "xhrpass2", "Checking provided password");
    70         // Check that the dialog has the correct parent
    71         ok(doc.defaultView.opener, "dialog has opener");
    72         // Not using is() because its "expected" text doesn't deal
    73         // with window objects very well
    74         // Disabled due to Bug 718543
    75         // ok(doc.defaultView.opener == window, "dialog's opener is correct");
    77         break;
    79     default:
    80         ok(false, "Uhh, unhandled switch for testNum #" + testNum);
    81         break;
    82   }
    84   // Explicitly cancel the dialog and report a fail in this failure
    85   // case, rather than letting the dialog get stuck due to an auth
    86   // failure and having the test timeout.
    87   if (!username && !password) {
    88       ok(false, "No values prefilled");
    89       clickOK = false;
    90   }
    92   if (clickOK)
    93     dialog.acceptDialog();
    94   else
    95     dialog.cancelDialog();
    97   ok(true, "handleDialog done");
    98   didDialog = true;
    99 }
   101 var newWin;
   102 function xhrLoad(xmlDoc) {
   103   ok(true, "xhrLoad running for test " + testNum);
   105   // The server echos back the user/pass it received.
   106   var username = xmlDoc.getElementById("user").textContent;
   107   var password = xmlDoc.getElementById("pass").textContent;
   108   var authok = xmlDoc.getElementById("ok").textContent;
   111   switch(testNum) {
   112     case 1:
   113         is(username, "xhruser1", "Checking provided username");
   114         is(password, "xhrpass1", "Checking provided password");
   115         break;
   117     case 2:
   118         is(username, "xhruser2", "Checking provided username");
   119         is(password, "xhrpass2", "Checking provided password");
   121         newWin.close();
   122         break;
   124     default:
   125         ok(false, "Uhh, unhandled switch for testNum #" + testNum);
   126         break;
   127   }
   129   doTest();
   130 }
   132 function doTest() {
   133   switch(++testNum) {
   134     case 1:
   135         startCallbackTimer();
   136         makeRequest("authenticate.sjs?user=xhruser1&pass=xhrpass1&realm=xhr");
   137         break;
   139     case 2:
   140         // Test correct parenting, by opening another window and
   141         // making sure the prompt's opener is correct
   142         newWin = window.open();
   143         newWin.focus();
   144         startCallbackTimer();
   145         makeRequest("authenticate.sjs?user=xhruser2&pass=xhrpass2&realm=xhr2");
   146         break;
   148     default:
   149         finishTest();
   150   }
   151 }
   153 function makeRequest(uri) {
   154   var request = new XMLHttpRequest();
   155   request.open("GET", uri, true);
   156   request.onreadystatechange = function () {
   157     if (request.readyState == 4)
   158       xhrLoad(request.responseXML);
   159   };
   160   request.send(null);
   161 }
   164 initLogins();
   166 // clear plain HTTP auth sessions before the test, to allow
   167 // running them more than once.
   168 var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1']
   169                         .getService(SpecialPowers.Ci.nsIHttpAuthManager);
   170 authMgr.clearAll();
   172 // start the tests
   173 testNum = 0;
   174 doTest();
   176 SimpleTest.waitForExplicitFinish();
   177 </script>
   178 </pre>
   179 </body>
   180 </html>

mercurial