summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_connection_coalescing.js
blob: a61098e73eb8b690db7e29bf69eb84c1ee801a0c (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* 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/. */

"use strict";

const override = Cc["@mozilla.org/network/native-dns-override;1"].getService(
  Ci.nsINativeDNSResolverOverride
);

let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
  Ci.nsIX509CertDB
);
addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");

async function createServer() {
  let server = new NodeHTTP2Server();
  await server.start();
  registerCleanupFunction(async () => {
    await server.stop();
  });
  await server.registerPathHandler("/", (req, resp) => {
    let content = `hello from ${req.authority} | ${req.socket.remotePort}`;
    resp.writeHead(200, {
      "Content-Type": "text/plain",
      "Content-Length": `${content.length}`,
    });
    resp.end(content);
  });
  return server;
}

let IP1 = "127.0.0.1";
let IP2 = "127.0.0.2";
if (AppConstants.platform == "macosx") {
  // OSX doesn't use 127.0.0.2 as a local interface
  IP2 = "::1";
} else if (AppConstants.platform == "android") {
  IP2 = "10.0.2.2";
}

async function openChan(uri) {
  let chan = NetUtil.newChannel({
    uri,
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);
  chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;

  let { req, buffer } = await new Promise(resolve => {
    function finish(r, b) {
      resolve({ req: r, buffer: b });
    }
    chan.asyncOpen(new ChannelListener(finish, null, CL_ALLOW_UNKNOWN_CL));
  });

  return {
    buffer,
    port: buffer.split("|")[1],
    addr: req.QueryInterface(Ci.nsIHttpChannelInternal).remoteAddress,
    status: req.QueryInterface(Ci.nsIHttpChannel).responseStatus,
  };
}

add_task(async function test_dontCoalesce() {
  let server = await createServer();
  Services.prefs.setBoolPref("network.http.http2.aggressive_coalescing", false);
  override.clearOverrides();
  Services.dns.clearCache(true);

  override.addIPOverride("foo.example.com", IP1);
  override.addIPOverride("foo.example.com", IP2);
  override.addIPOverride("alt1.example.com", IP2);

  let { addr: addr1 } = await openChan(
    `https://foo.example.com:${server.port()}/`
  );
  let { addr: addr2 } = await openChan(
    `https://alt1.example.com:${server.port()}/`
  );

  Assert.notEqual(addr1, addr2);
  await server.stop();
});

add_task(async function test_doCoalesce() {
  let server = await createServer();
  Services.prefs.setBoolPref("network.http.http2.aggressive_coalescing", false);
  override.clearOverrides();
  Services.dns.clearCache(true);

  override.addIPOverride("foo.example.com", IP1);
  override.addIPOverride("foo.example.com", IP2);
  override.addIPOverride("alt2.example.com", IP1);
  override.addIPOverride("alt2.example.com", IP2);

  let { port: port1, addr: addr1 } = await openChan(
    `https://foo.example.com:${server.port()}/`
  );
  let { port: port2, addr: addr2 } = await openChan(
    `https://alt2.example.com:${server.port()}/`
  );

  Assert.equal(addr1, addr2);
  Assert.equal(port1, port2);
  await server.stop();
});

add_task(async function test_doCoalesceAggresive() {
  let server = await createServer();

  Services.prefs.setBoolPref("network.http.http2.aggressive_coalescing", true);
  override.clearOverrides();
  Services.dns.clearCache(true);

  override.addIPOverride("foo.example.com", IP1);
  override.addIPOverride("foo.example.com", IP2);
  override.addIPOverride("alt1.example.com", IP2);

  let { port: port1, addr: addr1 } = await openChan(
    `https://foo.example.com:${server.port()}/`
  );
  let { port: port2, addr: addr2 } = await openChan(
    `https://alt1.example.com:${server.port()}/`
  );

  Assert.equal(addr1, addr2);
  Assert.equal(port1, port2);
  await server.stop();
});

// On android because of the way networking is set up the
// localAddress is always ::ffff:127.0.0.1 so it can't be
// used to make a decision.
add_task(
  { skip_if: () => AppConstants.platform == "android" },
  async function test_doCoalesceAggresive421() {
    let server = await createServer();

    await server.execute(`global.rightIP = "${IP2}"`);

    await server.registerPathHandler("/", (req, resp) => {
      let content = `hello from ${req.authority} | ${req.socket.remotePort}`;
      // Check that returning 421 when aggresively coalescing
      // makes Firefox not coalesce the connections.
      if (
        req.authority.startsWith("alt1.example.com") &&
        req.socket.localAddress != global.rightIP &&
        req.socket.localAddress != `::ffff:${global.rightIP}`
      ) {
        resp.writeHead(421, {
          "Content-Type": "text/plain",
          "Content-Length": `${content.length}`,
        });
        resp.end(content);
        return;
      }
      resp.writeHead(200, {
        "Content-Type": "text/plain",
        "Content-Length": `${content.length}`,
      });
      resp.end(content);
    });

    Services.prefs.setBoolPref(
      "network.http.http2.aggressive_coalescing",
      true
    );
    override.clearOverrides();
    Services.dns.clearCache(true);

    override.addIPOverride("foo.example.com", IP1);
    override.addIPOverride("foo.example.com", IP2);
    override.addIPOverride("alt1.example.com", IP2);

    let {
      addr: addr1,
      status: status1,
      port: port1,
    } = await openChan(`https://foo.example.com:${server.port()}/`);
    Assert.equal(status1, 200);
    Assert.equal(addr1, IP1);
    let {
      addr: addr2,
      status: status2,
      port: port2,
    } = await openChan(`https://alt1.example.com:${server.port()}/`);

    Assert.equal(status2, 200);
    Assert.equal(addr2, IP2);
    Assert.notEqual(port1, port2);
    await server.stop();
  }
);