summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_bug_627616.html
blob: ad4a41cdb25cb696b64bd6e1bf9c0fcdf3380317 (plain)
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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test bug 627616 related to proxy authentication</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="pwmgr_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script class="testbody" type="text/javascript">
    SimpleTest.waitForExplicitFinish();

    var Ci = SpecialPowers.Ci;

    function makeXHR(expectedStatus, expectedText, extra) {
      var xhr =  new XMLHttpRequest();
      xhr.open("GET", "authenticate.sjs?" +
                      "proxy_user=proxy_user&" +
                      "proxy_pass=proxy_pass&" +
                      "proxy_realm=proxy_realm&" +
                      "user=user1name&" +
                      "pass=user1pass&" +
                      "realm=mochirealm&" +
                      extra || "");
      xhr.onloadend = function() {
        is(xhr.status, expectedStatus, "xhr.status");
        is(xhr.statusText, expectedText, "xhr.statusText");
        runNextTest();
      };
      return xhr;
    }

    function testNonAnonymousCredentials() {
      var xhr = makeXHR(200, "OK");
      xhr.send();
    }

    function testAnonymousCredentials() {
      // Test that an anonymous request correctly performs proxy authentication
      var xhr = makeXHR(401, "Authentication required");
      SpecialPowers.wrap(xhr).channel.loadFlags |= Ci.nsIChannel.LOAD_ANONYMOUS;
      xhr.send();
    }

    function testAnonymousNoAuth() {
      // Next, test that an anonymous request still does not include any non-proxy
      // authentication headers.
      var xhr = makeXHR(200, "Authorization header not found", "anonymous=1");
      SpecialPowers.wrap(xhr).channel.loadFlags |= Ci.nsIChannel.LOAD_ANONYMOUS;
      xhr.send();
    }

    var gExpectedDialogs = 0;
    var gCurrentTest;
    function runNextTest() {
      is(gExpectedDialogs, 0, "received expected number of auth dialogs");
      mm.sendAsyncMessage("prepareForNextTest");
      mm.addMessageListener("prepareForNextTestDone", function prepared(msg) {
        mm.removeMessageListener("prepareForNextTestDone", prepared);
        if (pendingTests.length > 0) {
          ({expectedDialogs: gExpectedDialogs,
            test: gCurrentTest} = pendingTests.shift());
          gCurrentTest.call(this);
        } else {
          mm.sendAsyncMessage("cleanup");
          mm.addMessageListener("cleanupDone", () => {
            // mm.destroy() is called as a cleanup function by runInParent(), no
            // need to do it here.
            SimpleTest.finish();
          });
        }
      });
    }

    var pendingTests = [{expectedDialogs: 2, test: testNonAnonymousCredentials},
                        {expectedDialogs: 1, test: testAnonymousCredentials},
                        {expectedDialogs: 0, test: testAnonymousNoAuth}];

    let mm = runInParent(() => {
      const { classes: parentCc, interfaces: parentCi, utils: parentCu } = Components;

      parentCu.import("resource://gre/modules/Services.jsm");
      parentCu.import("resource://gre/modules/NetUtil.jsm");
      parentCu.import("resource://gre/modules/Timer.jsm");
      parentCu.import("resource://gre/modules/XPCOMUtils.jsm");

      let channel = NetUtil.newChannel({
        uri: "http://example.com",
        loadUsingSystemPrincipal: true
      });

      let pps = parentCc["@mozilla.org/network/protocol-proxy-service;1"].
                getService(parentCi.nsIProtocolProxyService);
      pps.asyncResolve(channel, 0, {
        onProxyAvailable(req, uri, pi, status) {
          let mozproxy = "moz-proxy://" + pi.host + ":" + pi.port;
          let login = parentCc["@mozilla.org/login-manager/loginInfo;1"].
                      createInstance(parentCi.nsILoginInfo);
          login.init(mozproxy, null, "proxy_realm", "proxy_user", "proxy_pass",
                     "", "");
          Services.logins.addLogin(login);

          let login2 = parentCc["@mozilla.org/login-manager/loginInfo;1"].
                       createInstance(parentCi.nsILoginInfo);
          login2.init("http://mochi.test:8888", null, "mochirealm", "user1name",
                       "user1pass", "", "");
          Services.logins.addLogin(login2);

          sendAsyncMessage("setupDone");
        },
        QueryInterface: XPCOMUtils.generateQI([parentCi.nsIProtocolProxyCallback]),
      });

      addMessageListener("prepareForNextTest", message => {
        parentCc["@mozilla.org/network/http-auth-manager;1"].
          getService(parentCi.nsIHttpAuthManager).
          clearAll();
        sendAsyncMessage("prepareForNextTestDone");
      });

      let dialogObserverTopic = "common-dialog-loaded";

      function dialogObserver(subj, topic, data) {
        subj.Dialog.ui.prompt.document.documentElement.acceptDialog();
        sendAsyncMessage("promptAccepted");
      }

      Services.obs.addObserver(dialogObserver, dialogObserverTopic, false);

      addMessageListener("cleanup", message => {
        Services.obs.removeObserver(dialogObserver, dialogObserverTopic);
        sendAsyncMessage("cleanupDone");
      });
    });

    mm.addMessageListener("promptAccepted", msg => {
      gExpectedDialogs--;
    });
    mm.addMessageListener("setupDone", msg => {
      runNextTest();
    });
</script>
</body>
</html>