summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_http2-proxy-failing.js
blob: 8530f55373e0efc16689cfe651cc6a39c60e2d2f (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
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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* Test stream failure on the session to the proxy:
 *  - Test the case the error closes the affected stream only
 *  - Test the case the error closes the whole session and cancels existing
 *    streams.
 */

/* eslint-env node */

"use strict";

const pps = Cc["@mozilla.org/network/protocol-proxy-service;1"].getService();

let filter;

class ProxyFilter {
  constructor(type, host, port, flags) {
    this._type = type;
    this._host = host;
    this._port = port;
    this._flags = flags;
    this.QueryInterface = ChromeUtils.generateQI(["nsIProtocolProxyFilter"]);
  }
  applyFilter(uri, pi, cb) {
    cb.onProxyFilterResult(
      pps.newProxyInfo(
        this._type,
        this._host,
        this._port,
        null,
        null,
        this._flags,
        1000,
        null
      )
    );
  }
}

function createPrincipal(url) {
  var ssm = Services.scriptSecurityManager;
  try {
    return ssm.createContentPrincipal(Services.io.newURI(url), {});
  } catch (e) {
    return null;
  }
}

function make_channel(url) {
  return Services.io.newChannelFromURIWithProxyFlags(
    Services.io.newURI(url),
    null,
    16,
    null,
    createPrincipal(url),
    createPrincipal(url),
    Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT,
    Ci.nsIContentPolicy.TYPE_OTHER
  );
}

function get_response(channel, flags = CL_ALLOW_UNKNOWN_CL, delay = 0) {
  return new Promise(resolve => {
    var listener = new ChannelListener(
      (request, data) => {
        request.QueryInterface(Ci.nsIHttpChannel);
        const status = request.status;
        const http_code = status ? undefined : request.responseStatus;
        request.QueryInterface(Ci.nsIProxiedChannel);
        const proxy_connect_response_code =
          request.httpProxyConnectResponseCode;
        resolve({ status, http_code, data, proxy_connect_response_code });
      },
      null,
      flags
    );
    if (delay > 0) {
      do_timeout(delay, function () {
        channel.asyncOpen(listener);
      });
    } else {
      channel.asyncOpen(listener);
    }
  });
}

add_task(async function setup() {
  // Set to allow the cert presented by our H2 server
  do_get_profile();

  // The moz-http2 cert is for foo.example.com and is signed by http2-ca.pem
  // so add that cert to the trust list as a signing cert.
  let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
    Ci.nsIX509CertDB
  );
  addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");

  let proxy_port = Services.env.get("MOZHTTP2_PORT");
  Assert.notEqual(proxy_port, null);

  Services.prefs.setBoolPref("network.http.http2.enabled", true);
  // make all native resolve calls "secretly" resolve localhost instead
  Services.prefs.setBoolPref("network.dns.native-is-localhost", true);

  filter = new ProxyFilter("https", "localhost", proxy_port, 16);
  pps.registerFilter(filter, 10);
});

registerCleanupFunction(async () => {
  Services.prefs.clearUserPref("network.http.http2.enabled");
  Services.prefs.clearUserPref("network.dns.native-is-localhost");

  pps.unregisterFilter(filter);
});

add_task(
  async function proxy_server_stream_soft_failure_multiple_streams_not_affected() {
    let should_succeed = get_response(make_channel(`http://750.example.com`));
    const failed = await get_response(
      make_channel(`http://illegalhpacksoft.example.com`),
      CL_EXPECT_FAILURE,
      20
    );

    const succeeded = await should_succeed;

    Assert.equal(failed.status, Cr.NS_ERROR_ILLEGAL_VALUE);
    Assert.equal(failed.proxy_connect_response_code, 0);
    Assert.equal(failed.http_code, undefined);
    Assert.equal(succeeded.status, Cr.NS_OK);
    Assert.equal(succeeded.proxy_connect_response_code, 200);
    Assert.equal(succeeded.http_code, 200);
  }
);

add_task(
  async function proxy_server_stream_hard_failure_multiple_streams_affected() {
    let should_failed = get_response(
      make_channel(`http://750.example.com`),
      CL_EXPECT_FAILURE
    );
    const failed1 = await get_response(
      make_channel(`http://illegalhpackhard.example.com`),
      CL_EXPECT_FAILURE
    );

    const failed2 = await should_failed;

    Assert.equal(failed1.status, 0x804b0053);
    Assert.equal(failed1.proxy_connect_response_code, 0);
    Assert.equal(failed1.http_code, undefined);
    Assert.equal(failed2.status, 0x804b0053);
    Assert.equal(failed2.proxy_connect_response_code, 0);
    Assert.equal(failed2.http_code, undefined);
  }
);

add_task(async function test_http2_h11required_stream() {
  let should_failed = await get_response(
    make_channel(`http://h11required.com`),
    CL_EXPECT_FAILURE
  );

  // See HTTP/1.1 connect handler in moz-http2.js. The handler returns
  // "404 Not Found", so the expected error code is NS_ERROR_UNKNOWN_HOST.
  Assert.equal(should_failed.status, Cr.NS_ERROR_UNKNOWN_HOST);
  Assert.equal(should_failed.proxy_connect_response_code, 404);
  Assert.equal(should_failed.http_code, undefined);
});