summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_http3.js
blob: 4fd4c0be22d8a56086ea5af8e3c2d44d557c0211 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
"use strict";

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");

// Generate a post with known pre-calculated md5 sum.
function generateContent(size) {
  let content = "";
  for (let i = 0; i < size; i++) {
    content += "0";
  }
  return content;
}

let post = generateContent(10);

// Max concurent stream number in neqo is 100.
// Openning 120 streams will test queuing of streams.
let number_of_parallel_requests = 120;
let h1Server = null;
let h3Route;
let httpsOrigin;
let httpOrigin;
let h3AltSvc;

let prefs;

let tests = [
  // This test must be the first because it setsup alt-svc connection, that
  // other tests use.
  test_https_alt_svc,
  test_multiple_requests,
  test_request_cancelled_by_server,
  test_stream_cancelled_by_necko,
  test_multiple_request_one_is_cancelled,
  test_multiple_request_one_is_cancelled_by_necko,
  test_post,
  test_patch,
  test_http_alt_svc,
  test_slow_receiver,
  // This test should be at the end, because it will close http3
  // connection and the transaction will switch to already existing http2
  // connection.
  // TODO: Bug 1582667 should try to fix issue with connection being closed.
  test_version_fallback,
  testsDone,
];

let current_test = 0;

function run_next_test() {
  if (current_test < tests.length) {
    dump("starting test number " + current_test + "\n");
    tests[current_test]();
    current_test++;
  }
}

function run_test() {
  let h2Port = Services.env.get("MOZHTTP2_PORT");
  Assert.notEqual(h2Port, null);
  Assert.notEqual(h2Port, "");
  let h3Port = Services.env.get("MOZHTTP3_PORT");
  Assert.notEqual(h3Port, null);
  Assert.notEqual(h3Port, "");
  h3AltSvc = ":" + h3Port;

  h3Route = "foo.example.com:" + h3Port;
  do_get_profile();
  prefs = Services.prefs;

  prefs.setBoolPref("network.http.http3.enable", true);
  prefs.setCharPref("network.dns.localDomains", "foo.example.com");
  // We always resolve elements of localDomains as it's hardcoded without the
  // following pref:
  prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true);
  prefs.setBoolPref("network.http.altsvc.oe", true);

  // The certificate for the http3server server 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");
  httpsOrigin = "https://foo.example.com:" + h2Port + "/";

  h1Server = new HttpServer();
  h1Server.registerPathHandler("/http3-test", h1Response);
  h1Server.registerPathHandler("/.well-known/http-opportunistic", h1ServerWK);
  h1Server.registerPathHandler("/VersionFallback", h1Response);
  h1Server.start(-1);
  h1Server.identity.setPrimary(
    "http",
    "foo.example.com",
    h1Server.identity.primaryPort
  );
  httpOrigin = "http://foo.example.com:" + h1Server.identity.primaryPort + "/";

  run_next_test();
}

function h1Response(metadata, response) {
  response.setStatusLine(metadata.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "text/plain", false);
  response.setHeader("Connection", "close", false);
  response.setHeader("Cache-Control", "no-cache", false);
  response.setHeader("Access-Control-Allow-Origin", "*", false);
  response.setHeader("Access-Control-Allow-Method", "GET", false);
  response.setHeader("Access-Control-Allow-Headers", "x-altsvc", false);

  try {
    let hval = "h3-29=" + metadata.getHeader("x-altsvc");
    response.setHeader("Alt-Svc", hval, false);
  } catch (e) {}

  let body = "Q: What did 0 say to 8? A: Nice Belt!\n";
  response.bodyOutputStream.write(body, body.length);
}

function h1ServerWK(metadata, response) {
  response.setStatusLine(metadata.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "application/json", false);
  response.setHeader("Connection", "close", false);
  response.setHeader("Cache-Control", "no-cache", false);
  response.setHeader("Access-Control-Allow-Origin", "*", false);
  response.setHeader("Access-Control-Allow-Method", "GET", false);
  response.setHeader("Access-Control-Allow-Headers", "x-altsvc", false);

  let body = '["http://foo.example.com:' + h1Server.identity.primaryPort + '"]';
  response.bodyOutputStream.write(body, body.length);
}

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

let Http3CheckListener = function () {};

Http3CheckListener.prototype = {
  onDataAvailableFired: false,
  expectedStatus: Cr.NS_OK,
  expectedRoute: "",

  onStartRequest: function testOnStartRequest(request) {
    Assert.ok(request instanceof Ci.nsIHttpChannel);

    Assert.equal(request.status, this.expectedStatus);
    if (Components.isSuccessCode(this.expectedStatus)) {
      Assert.equal(request.responseStatus, 200);
    }
  },

  onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
    this.onDataAvailableFired = true;
    read_stream(stream, cnt);
  },

  onStopRequest: function testOnStopRequest(request, status) {
    Assert.equal(status, this.expectedStatus);
    let routed = "NA";
    try {
      routed = request.getRequestHeader("Alt-Used");
    } catch (e) {}
    dump("routed is " + routed + "\n");

    Assert.equal(routed, this.expectedRoute);

    if (Components.isSuccessCode(this.expectedStatus)) {
      let httpVersion = "";
      try {
        httpVersion = request.protocolVersion;
      } catch (e) {}
      Assert.equal(httpVersion, "h3-29");
      Assert.equal(this.onDataAvailableFired, true);
      Assert.equal(request.getResponseHeader("X-Firefox-Http3"), "h3-29");
    }
    run_next_test();
    do_test_finished();
  },
};

let WaitForHttp3Listener = function () {};

WaitForHttp3Listener.prototype = new Http3CheckListener();

WaitForHttp3Listener.prototype.uri = "";
WaitForHttp3Listener.prototype.h3AltSvc = "";

WaitForHttp3Listener.prototype.onStopRequest = function testOnStopRequest(
  request,
  status
) {
  Assert.equal(status, this.expectedStatus);

  let routed = "NA";
  try {
    routed = request.getRequestHeader("Alt-Used");
  } catch (e) {}
  dump("routed is " + routed + "\n");

  let httpVersion = "";
  try {
    httpVersion = request.protocolVersion;
  } catch (e) {}

  if (routed == this.expectedRoute) {
    Assert.equal(routed, this.expectedRoute); // always true, but a useful log
    Assert.equal(httpVersion, "h3-29");
    run_next_test();
  } else {
    dump("poll later for alt svc mapping\n");
    if (httpVersion == "h2") {
      request.QueryInterface(Ci.nsIHttpChannelInternal);
      Assert.ok(request.supportsHTTP3);
    }
    do_test_pending();
    do_timeout(500, () => {
      doTest(this.uri, this.expectedRoute, this.h3AltSvc);
    });
  }

  do_test_finished();
};

function doTest(uri, expectedRoute, altSvc) {
  let chan = makeChan(uri);
  let listener = new WaitForHttp3Listener();
  listener.uri = uri;
  listener.expectedRoute = expectedRoute;
  listener.h3AltSvc = altSvc;
  chan.setRequestHeader("x-altsvc", altSvc, false);
  chan.asyncOpen(listener);
}

// Test Alt-Svc for http3.
// H2 server returns alt-svc=h3-29=:h3port
function test_https_alt_svc() {
  dump("test_https_alt_svc()\n");
  do_test_pending();
  doTest(httpsOrigin + "http3-test", h3Route, h3AltSvc);
}

// Listener for a number of parallel requests. if with_error is set, one of
// the channels will be cancelled (by the server or in onStartRequest).
let MultipleListener = function () {};

MultipleListener.prototype = {
  number_of_parallel_requests: 0,
  with_error: Cr.NS_OK,
  count_of_done_requests: 0,
  error_found_onstart: false,
  error_found_onstop: false,
  need_cancel_found: false,

  onStartRequest: function testOnStartRequest(request) {
    Assert.ok(request instanceof Ci.nsIHttpChannel);

    let need_cancel = "";
    try {
      need_cancel = request.getRequestHeader("CancelMe");
    } catch (e) {}
    if (need_cancel != "") {
      this.need_cancel_found = true;
      request.cancel(Cr.NS_ERROR_ABORT);
    } else if (Components.isSuccessCode(request.status)) {
      Assert.equal(request.responseStatus, 200);
    } else if (this.error_found_onstart) {
      do_throw("We should have only one request failing.");
    } else {
      Assert.equal(request.status, this.with_error);
      this.error_found_onstart = true;
    }
  },

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

  onStopRequest: function testOnStopRequest(request, status) {
    let routed = "";
    try {
      routed = request.getRequestHeader("Alt-Used");
    } catch (e) {}
    Assert.equal(routed, this.expectedRoute);

    if (Components.isSuccessCode(request.status)) {
      let httpVersion = "";
      try {
        httpVersion = request.protocolVersion;
      } catch (e) {}
      Assert.equal(httpVersion, "h3-29");
    }

    if (!Components.isSuccessCode(request.status)) {
      if (this.error_found_onstop) {
        do_throw("We should have only one request failing.");
      } else {
        Assert.equal(request.status, this.with_error);
        this.error_found_onstop = true;
      }
    }
    this.count_of_done_requests++;
    if (this.count_of_done_requests == this.number_of_parallel_requests) {
      if (Components.isSuccessCode(this.with_error)) {
        Assert.equal(this.error_found_onstart, false);
        Assert.equal(this.error_found_onstop, false);
      } else {
        Assert.ok(this.error_found_onstart || this.need_cancel_found);
        Assert.equal(this.error_found_onstop, true);
      }
      run_next_test();
    }
    do_test_finished();
  },
};

// Multiple requests
function test_multiple_requests() {
  dump("test_multiple_requests()\n");

  let listener = new MultipleListener();
  listener.number_of_parallel_requests = number_of_parallel_requests;
  listener.expectedRoute = h3Route;

  for (let i = 0; i < number_of_parallel_requests; i++) {
    let chan = makeChan(httpsOrigin + "20000");
    chan.asyncOpen(listener);
    do_test_pending();
  }
}

// A request cancelled by a server.
function test_request_cancelled_by_server() {
  dump("test_request_cancelled_by_server()\n");

  let listener = new Http3CheckListener();
  listener.expectedStatus = Cr.NS_ERROR_NET_INTERRUPT;
  listener.expectedRoute = h3Route;
  let chan = makeChan(httpsOrigin + "RequestCancelled");
  chan.asyncOpen(listener);
  do_test_pending();
}

let CancelRequestListener = function () {};

CancelRequestListener.prototype = new Http3CheckListener();

CancelRequestListener.prototype.expectedStatus = Cr.NS_ERROR_ABORT;

CancelRequestListener.prototype.onStartRequest = function testOnStartRequest(
  request
) {
  Assert.ok(request instanceof Ci.nsIHttpChannel);

  Assert.equal(Components.isSuccessCode(request.status), true);
  request.cancel(Cr.NS_ERROR_ABORT);
};

// Cancel stream after OnStartRequest.
function test_stream_cancelled_by_necko() {
  dump("test_stream_cancelled_by_necko()\n");

  let listener = new CancelRequestListener();
  listener.expectedRoute = h3Route;
  let chan = makeChan(httpsOrigin + "20000");
  chan.asyncOpen(listener);
  do_test_pending();
}

// Multiple requests, one gets cancelled by the server, the other should finish normally.
function test_multiple_request_one_is_cancelled() {
  dump("test_multiple_request_one_is_cancelled()\n");

  let listener = new MultipleListener();
  listener.number_of_parallel_requests = number_of_parallel_requests;
  listener.with_error = Cr.NS_ERROR_NET_INTERRUPT;
  listener.expectedRoute = h3Route;

  for (let i = 0; i < number_of_parallel_requests; i++) {
    let uri = httpsOrigin + "20000";
    if (i == 4) {
      // Add a request that will be cancelled by the server.
      uri = httpsOrigin + "RequestCancelled";
    }
    let chan = makeChan(uri);
    chan.asyncOpen(listener);
    do_test_pending();
  }
}

// Multiple requests, one gets cancelled by us, the other should finish normally.
function test_multiple_request_one_is_cancelled_by_necko() {
  dump("test_multiple_request_one_is_cancelled_by_necko()\n");

  let listener = new MultipleListener();
  listener.number_of_parallel_requests = number_of_parallel_requests;
  listener.with_error = Cr.NS_ERROR_ABORT;
  listener.expectedRoute = h3Route;
  for (let i = 0; i < number_of_parallel_requests; i++) {
    let chan = makeChan(httpsOrigin + "20000");
    if (i == 4) {
      // MultipleListener will cancel request with this header.
      chan.setRequestHeader("CancelMe", "true", false);
    }
    chan.asyncOpen(listener);
    do_test_pending();
  }
}

let PostListener = function () {};

PostListener.prototype = new Http3CheckListener();

PostListener.prototype.onDataAvailable = function (request, stream, off, cnt) {
  this.onDataAvailableFired = true;
  read_stream(stream, cnt);
};

// Support for doing a POST
function do_post(content, chan, listener, method) {
  let stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
    Ci.nsIStringInputStream
  );
  stream.data = content;

  let uchan = chan.QueryInterface(Ci.nsIUploadChannel);
  uchan.setUploadStream(stream, "text/plain", stream.available());

  chan.requestMethod = method;

  chan.asyncOpen(listener);
}

// Test a simple POST
function test_post() {
  dump("test_post()");
  let chan = makeChan(httpsOrigin + "post");
  let listener = new PostListener();
  listener.expectedRoute = h3Route;
  do_post(post, chan, listener, "POST");
  do_test_pending();
}

// Test a simple PATCH
function test_patch() {
  dump("test_patch()");
  let chan = makeChan(httpsOrigin + "patch");
  let listener = new PostListener();
  listener.expectedRoute = h3Route;
  do_post(post, chan, listener, "PATCH");
  do_test_pending();
}

// Test alt-svc for http (without s)
function test_http_alt_svc() {
  dump("test_http_alt_svc()\n");

  do_test_pending();
  doTest(httpOrigin + "http3-test", h3Route, h3AltSvc);
}

let SlowReceiverListener = function () {};

SlowReceiverListener.prototype = new Http3CheckListener();
SlowReceiverListener.prototype.count = 0;

SlowReceiverListener.prototype.onDataAvailable = function (
  request,
  stream,
  off,
  cnt
) {
  this.onDataAvailableFired = true;
  this.count += cnt;
  read_stream(stream, cnt);
};

SlowReceiverListener.prototype.onStopRequest = function (request, status) {
  Assert.equal(status, this.expectedStatus);
  Assert.equal(this.count, 10000000);
  let routed = "NA";
  try {
    routed = request.getRequestHeader("Alt-Used");
  } catch (e) {}
  dump("routed is " + routed + "\n");

  Assert.equal(routed, this.expectedRoute);

  if (Components.isSuccessCode(this.expectedStatus)) {
    let httpVersion = "";
    try {
      httpVersion = request.protocolVersion;
    } catch (e) {}
    Assert.equal(httpVersion, "h3-29");
    Assert.equal(this.onDataAvailableFired, true);
  }
  run_next_test();
  do_test_finished();
};

function test_slow_receiver() {
  dump("test_slow_receiver()\n");
  let chan = makeChan(httpsOrigin + "10000000");
  let listener = new SlowReceiverListener();
  listener.expectedRoute = h3Route;
  chan.asyncOpen(listener);
  do_test_pending();
  chan.suspend();
  do_timeout(1000, chan.resume);
}

let CheckFallbackListener = function () {};

CheckFallbackListener.prototype = {
  onStartRequest: function testOnStartRequest(request) {
    Assert.ok(request instanceof Ci.nsIHttpChannel);

    Assert.equal(request.status, Cr.NS_OK);
    Assert.equal(request.responseStatus, 200);
  },

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

  onStopRequest: function testOnStopRequest(request, status) {
    Assert.equal(status, Cr.NS_OK);
    let routed = "NA";
    try {
      routed = request.getRequestHeader("Alt-Used");
    } catch (e) {}
    dump("routed is " + routed + "\n");

    Assert.equal(routed, "0");

    let httpVersion = "";
    try {
      httpVersion = request.protocolVersion;
    } catch (e) {}
    Assert.equal(httpVersion, "http/1.1");
    run_next_test();
    do_test_finished();
  },
};

// Server cancels request with VersionFallback.
function test_version_fallback() {
  dump("test_version_fallback()\n");

  let chan = makeChan(httpsOrigin + "VersionFallback");
  let listener = new CheckFallbackListener();
  chan.asyncOpen(listener);
  do_test_pending();
}

function testsDone() {
  prefs.clearUserPref("network.http.http3.enable");
  prefs.clearUserPref("network.dns.localDomains");
  prefs.clearUserPref("network.proxy.allow_hijacking_localhost");
  prefs.clearUserPref("network.http.altsvc.oe");
  dump("testDone\n");
  do_test_pending();
  h1Server.stop(do_test_finished);
}