summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_anonymous-coalescing.js
blob: c46dbfe52bcdea1ac2d6ae665db486f2e68217d5 (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
/*
- test to check we use only a single connection for both onymous and anonymous requests over an existing h2 session
- request from a domain w/o LOAD_ANONYMOUS flag
- request again from the same domain, but different URI, with LOAD_ANONYMOUS flag, check the client is using the same conn
- close all and do it in the opposite way (do an anonymous req first)
*/

"use strict";

var h2Port;
var prefs;
var http2pref;
var extpref;

function run_test() {
  h2Port = Services.env.get("MOZHTTP2_PORT");
  Assert.notEqual(h2Port, null);
  Assert.notEqual(h2Port, "");

  // Set to allow the cert presented by our H2 server
  do_get_profile();
  prefs = Services.prefs;

  http2pref = prefs.getBoolPref("network.http.http2.enabled");
  extpref = prefs.getBoolPref("network.http.originextension");

  prefs.setBoolPref("network.http.http2.enabled", true);
  prefs.setBoolPref("network.http.originextension", true);
  prefs.setCharPref(
    "network.dns.localDomains",
    "foo.example.com, alt1.example.com"
  );

  // The moz-http2 cert is for {foo, alt1, alt2}.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");

  doTest1();
}

function resetPrefs() {
  prefs.setBoolPref("network.http.http2.enabled", http2pref);
  prefs.setBoolPref("network.http.originextension", extpref);
  prefs.clearUserPref("network.dns.localDomains");
}

function makeChan(origin) {
  return NetUtil.newChannel({
    uri: origin,
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);
}

var nextTest;
var origin;
var nextPortExpectedToBeSame = false;
var currentPort = 0;
var forceReload = false;
var anonymous = false;

var Listener = function () {};
Listener.prototype.clientPort = 0;
Listener.prototype = {
  onStartRequest: function testOnStartRequest(request) {
    Assert.ok(request instanceof Ci.nsIHttpChannel);

    if (!Components.isSuccessCode(request.status)) {
      do_throw("Channel should have a success code! (" + request.status + ")");
    }
    Assert.equal(request.responseStatus, 200);
    this.clientPort = parseInt(request.getResponseHeader("x-client-port"));
  },

  onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
    read_stream(stream, cnt);
  },

  onStopRequest: function testOnStopRequest(request, status) {
    Assert.ok(Components.isSuccessCode(status));
    if (nextPortExpectedToBeSame) {
      Assert.equal(currentPort, this.clientPort);
    } else {
      Assert.notEqual(currentPort, this.clientPort);
    }
    currentPort = this.clientPort;
    nextTest();
    do_test_finished();
  },
};

function testsDone() {
  dump("testsDone\n");
  resetPrefs();
}

function doTest() {
  dump("execute doTest " + origin + "\n");

  var loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
  if (anonymous) {
    loadFlags |= Ci.nsIRequest.LOAD_ANONYMOUS;
  }
  anonymous = false;
  if (forceReload) {
    loadFlags |= Ci.nsIRequest.LOAD_FRESH_CONNECTION;
  }
  forceReload = false;

  var chan = makeChan(origin);
  chan.loadFlags = loadFlags;

  var listener = new Listener();
  chan.asyncOpen(listener);
}

function doTest1() {
  dump("doTest1()\n");
  origin = "https://foo.example.com:" + h2Port + "/origin-1";
  nextTest = doTest2;
  nextPortExpectedToBeSame = false;
  do_test_pending();
  doTest();
}

function doTest2() {
  // Run the same test as above to make sure connection is marked experienced.
  dump("doTest2()\n");
  origin = "https://foo.example.com:" + h2Port + "/origin-1";
  nextTest = doTest3;
  nextPortExpectedToBeSame = true;
  do_test_pending();
  doTest();
}

function doTest3() {
  // connection expected to be reused for an anonymous request
  dump("doTest3()\n");
  origin = "https://foo.example.com:" + h2Port + "/origin-2";
  nextTest = doTest4;
  nextPortExpectedToBeSame = true;
  anonymous = true;
  do_test_pending();
  doTest();
}

function doTest4() {
  dump("doTest4()\n");
  origin = "https://foo.example.com:" + h2Port + "/origin-3";
  nextTest = doTest5;
  nextPortExpectedToBeSame = false;
  forceReload = true;
  anonymous = true;
  do_test_pending();
  doTest();
}

function doTest5() {
  // Run the same test as above just without forceReload to make sure connection
  // is marked experienced.
  dump("doTest5()\n");
  origin = "https://foo.example.com:" + h2Port + "/origin-3";
  nextTest = doTest6;
  nextPortExpectedToBeSame = true;
  anonymous = true;
  do_test_pending();
  doTest();
}

function doTest6() {
  dump("doTest6()\n");
  origin = "https://foo.example.com:" + h2Port + "/origin-4";
  nextTest = testsDone;
  nextPortExpectedToBeSame = true;
  do_test_pending();
  doTest();
}