toolkit/components/passwordmgr/test/test_bug_627616.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/passwordmgr/test/test_bug_627616.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,131 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Test bug 627616</title>
     1.8 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
     1.9 +  <script type="text/javascript" src="prompt_common.js"></script>
    1.10 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.11 +</head>
    1.12 +<body>
    1.13 +<script class="testbody" type="text/javascript">
    1.14 +    SimpleTest.waitForExplicitFinish();
    1.15 +
    1.16 +    var Cc = SpecialPowers.Cc;
    1.17 +
    1.18 +    testNum = 1;
    1.19 +
    1.20 +    var login, login2;
    1.21 +
    1.22 +    var resolveCallback = SpecialPowers.wrapCallbackObject({
    1.23 +
    1.24 +      QueryInterface : function (iid) {
    1.25 +        const interfaces = [Ci.nsIProtocolProxyCallback, Ci.nsISupports];
    1.26 +
    1.27 +        if (!interfaces.some( function(v) { return iid.equals(v) } ))
    1.28 +          throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE;
    1.29 +        return this;
    1.30 +      },
    1.31 +
    1.32 +      onProxyAvailable : function (req, uri, pi, status) {
    1.33 +         init2(SpecialPowers.wrap(pi).host, SpecialPowers.wrap(pi).port);
    1.34 +      }
    1.35 +    });
    1.36 +
    1.37 +    function init1() {
    1.38 +        var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    1.39 +        var pps = Cc["@mozilla.org/network/protocol-proxy-service;1"].getService();
    1.40 +
    1.41 +        var uri = ios.newURI("http://example.com", null, null);
    1.42 +        pps.asyncResolve(uri, 0, resolveCallback);
    1.43 +    }
    1.44 +
    1.45 +    function init2(proxyHost, proxyPort) {
    1.46 +
    1.47 +        var mozproxy = "moz-proxy://" + proxyHost + ":" + proxyPort;
    1.48 +
    1.49 +        var pwmgr = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
    1.50 +        login = Cc["@mozilla.org/login-manager/loginInfo;1"].createInstance(Ci.nsILoginInfo);
    1.51 +        login.init(mozproxy, null, "proxy_realm", "proxy_user", "proxy_pass", "", "");
    1.52 +        pwmgr.addLogin(login);
    1.53 +
    1.54 +        login2 = Cc["@mozilla.org/login-manager/loginInfo;1"].createInstance(Ci.nsILoginInfo);
    1.55 +        login2.init("http://mochi.test:8888", null, "mochirealm", "user1name", "user1pass", "", "");
    1.56 +        pwmgr.addLogin(login2);
    1.57 +        startCallbackTimer();
    1.58 +    }
    1.59 +    function cleanup() {
    1.60 +        var pwmgr = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
    1.61 +        pwmgr.removeLogin(login);        
    1.62 +        pwmgr.removeLogin(login2);
    1.63 +        timer.cancel();
    1.64 +    }
    1.65 +
    1.66 +    function makeXHR(expectedStatus, expectedText, extra) {
    1.67 +      var xhr =  new XMLHttpRequest();
    1.68 +      xhr.open("GET", "authenticate.sjs?" +
    1.69 +                      "proxy_user=proxy_user&" +
    1.70 +                      "proxy_pass=proxy_pass&" +
    1.71 +                      "proxy_realm=proxy_realm&" +
    1.72 +                      "user=user1name&" +
    1.73 +                      "pass=user1pass&" +
    1.74 +                      "realm=mochirealm&" +
    1.75 +                      extra || "");
    1.76 +      xhr.onloadend = function() {
    1.77 +        is(xhr.status, expectedStatus, "xhr.status");
    1.78 +        is(xhr.statusText, expectedText, "xhr.statusText");
    1.79 +        runNextTest();
    1.80 +      }
    1.81 +      return xhr;
    1.82 +    }
    1.83 +
    1.84 +    function testNonAnonymousCredentials() {
    1.85 +      var xhr = makeXHR(200, "OK");
    1.86 +      xhr.send();
    1.87 +    }
    1.88 +
    1.89 +    function testAnonymousCredentials() {
    1.90 +      // Test that an anonymous request correctly performs proxy authentication
    1.91 +      var xhr = makeXHR(401, "Authentication required");
    1.92 +      SpecialPowers.wrap(xhr).channel.loadFlags |= Ci.nsIChannel.LOAD_ANONYMOUS;
    1.93 +      xhr.send();
    1.94 +    }
    1.95 +
    1.96 +    function testAnonymousNoAuth() {
    1.97 +      // Next, test that an anonymous request still does not include any non-proxy
    1.98 +      // authentication headers.
    1.99 +      var xhr = makeXHR(200, "Authorization header not found", "anonymous=1");
   1.100 +      SpecialPowers.wrap(xhr).channel.loadFlags |= Ci.nsIChannel.LOAD_ANONYMOUS;
   1.101 +      xhr.send();
   1.102 +    }
   1.103 +
   1.104 +    var gExpectedDialogs = 0;
   1.105 +    var gCurrentTest;
   1.106 +    function runNextTest() {
   1.107 +      is(gExpectedDialogs, 0, "received expected number of auth dialogs");
   1.108 +      Cc["@mozilla.org/network/http-auth-manager;1"].getService(SpecialPowers.Ci.nsIHttpAuthManager).clearAll();
   1.109 +      if (pendingTests.length > 0) {
   1.110 +        ({expectedDialogs: gExpectedDialogs,
   1.111 +          test: gCurrentTest}) = pendingTests.shift();
   1.112 +        gCurrentTest.call(this);
   1.113 +      } else {
   1.114 +        cleanup();
   1.115 +        SimpleTest.finish();
   1.116 +      }
   1.117 +    }
   1.118 +
   1.119 +    var pendingTests = [{expectedDialogs: 2, test: testNonAnonymousCredentials},
   1.120 +                        {expectedDialogs: 1, test: testAnonymousCredentials},
   1.121 +                        {expectedDialogs: 0, test: testAnonymousNoAuth}];
   1.122 +    init1();
   1.123 +    runNextTest();
   1.124 +
   1.125 +    function handleDialog(doc, testNum)
   1.126 +    {
   1.127 +        var dialog = doc.getElementById("commonDialog");
   1.128 +        dialog.acceptDialog();
   1.129 +        gExpectedDialogs--;
   1.130 +        startCallbackTimer();
   1.131 +    }
   1.132 +</script>
   1.133 +</body>
   1.134 +</html>

mercurial