summaryrefslogtreecommitdiffstats
path: root/dom/webtransport/test/xpcshell
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /dom/webtransport/test/xpcshell
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/webtransport/test/xpcshell')
-rw-r--r--dom/webtransport/test/xpcshell/moz.build9
-rw-r--r--dom/webtransport/test/xpcshell/test_close.js77
-rw-r--r--dom/webtransport/test/xpcshell/test_simple_conn.js134
-rw-r--r--dom/webtransport/test/xpcshell/test_simple_stream.js170
-rw-r--r--dom/webtransport/test/xpcshell/xpcshell.toml29
5 files changed, 419 insertions, 0 deletions
diff --git a/dom/webtransport/test/xpcshell/moz.build b/dom/webtransport/test/xpcshell/moz.build
new file mode 100644
index 0000000000..0eb0836320
--- /dev/null
+++ b/dom/webtransport/test/xpcshell/moz.build
@@ -0,0 +1,9 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# 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/.
+
+XPCSHELL_TESTS_MANIFESTS += [
+ "xpcshell.toml",
+]
diff --git a/dom/webtransport/test/xpcshell/test_close.js b/dom/webtransport/test/xpcshell/test_close.js
new file mode 100644
index 0000000000..5e3e9c5941
--- /dev/null
+++ b/dom/webtransport/test/xpcshell/test_close.js
@@ -0,0 +1,77 @@
+//
+// Simple WebTransport test
+//
+// keep eslint happy until it knows about WebTransport
+/* global WebTransport:false */
+
+"use strict";
+
+var h3Port;
+var host;
+
+registerCleanupFunction(async () => {
+ Services.prefs.clearUserPref("network.dns.localDomains");
+});
+
+var { NetUtil } = ChromeUtils.importESModule(
+ "resource://gre/modules/NetUtil.sys.mjs"
+);
+
+function readFile(file) {
+ let fstream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
+ Ci.nsIFileInputStream
+ );
+ fstream.init(file, -1, 0, 0);
+ let data = NetUtil.readInputStreamToString(fstream, fstream.available());
+ fstream.close();
+ return data;
+}
+
+function addCertFromFile(certdb, filename, trustString) {
+ let certFile = do_get_file(filename, false);
+ let pem = readFile(certFile)
+ .replace(/-----BEGIN CERTIFICATE-----/, "")
+ .replace(/-----END CERTIFICATE-----/, "")
+ .replace(/[\r\n]/g, "");
+ certdb.addCertFromBase64(pem, trustString);
+}
+
+add_task(async function setup() {
+ Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com");
+
+ h3Port = Services.env.get("MOZHTTP3_PORT");
+ Assert.notEqual(h3Port, null);
+ Assert.notEqual(h3Port, "");
+ host = "foo.example.com:" + h3Port;
+ do_get_profile();
+
+ let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
+ Ci.nsIX509CertDB
+ );
+ // `../unit/` so that unit_ipc tests can use as well
+ addCertFromFile(
+ certdb,
+ "../../../../netwerk/test/unit/http2-ca.pem",
+ "CTu,u,u"
+ );
+});
+
+add_task(async function test_webtransport_create() {
+ Services.prefs.setBoolPref("network.webtransport.enabled", true);
+
+ const wt = new WebTransport("https://" + host + "/success");
+ await wt.ready;
+ dump("**** ready\n");
+
+ wt.close();
+});
+
+// bug 1840626 - cancel and then close
+add_task(async function test_wt_stream_create_bidi_cancel_close() {
+ let wt = new WebTransport("https://" + host + "/success");
+ await wt.ready;
+
+ await wt.createBidirectionalStream();
+ await wt.incomingBidirectionalStreams.cancel(undefined);
+ wt.close();
+});
diff --git a/dom/webtransport/test/xpcshell/test_simple_conn.js b/dom/webtransport/test/xpcshell/test_simple_conn.js
new file mode 100644
index 0000000000..aa4a4131e1
--- /dev/null
+++ b/dom/webtransport/test/xpcshell/test_simple_conn.js
@@ -0,0 +1,134 @@
+/* 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/. */
+
+// Some basic WebTransport tests for:
+// * session rejection and redirection
+// * session and stream creation
+// * reading from incoming streams (uni)
+//
+// keep eslint happy until it knows about WebTransport
+/* global WebTransport:false */
+
+"use strict";
+
+var h3Port;
+var host;
+
+/* eslint no-unused-vars: 0 */
+const dns = Services.dns;
+
+registerCleanupFunction(async () => {
+ Services.prefs.clearUserPref("network.dns.localDomains");
+ Services.prefs.clearUserPref("network.webtransport.enabled");
+ Services.prefs.clearUserPref("network.webtransport.datagrams.enabled");
+ Services.prefs.clearUserPref("network.webtransport.redirect.enabled");
+});
+
+var { NetUtil } = ChromeUtils.importESModule(
+ "resource://gre/modules/NetUtil.sys.mjs"
+);
+
+function readFile(file) {
+ let fstream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
+ Ci.nsIFileInputStream
+ );
+ fstream.init(file, -1, 0, 0);
+ let data = NetUtil.readInputStreamToString(fstream, fstream.available());
+ fstream.close();
+ return data;
+}
+
+function addCertFromFile(certdb, filename, trustString) {
+ let certFile = do_get_file(filename, false);
+ let pem = readFile(certFile)
+ .replace(/-----BEGIN CERTIFICATE-----/, "")
+ .replace(/-----END CERTIFICATE-----/, "")
+ .replace(/[\r\n]/g, "");
+ certdb.addCertFromBase64(pem, trustString);
+}
+
+add_setup(async function setup() {
+ Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com");
+ Services.prefs.setBoolPref("network.webtransport.enabled", true);
+ Services.prefs.setBoolPref("network.webtransport.datagrams.enabled", true);
+ Services.prefs.setBoolPref("network.webtransport.redirect.enabled", true);
+
+ h3Port = Services.env.get("MOZHTTP3_PORT");
+ Assert.notEqual(h3Port, null);
+ Assert.notEqual(h3Port, "");
+ host = "foo.example.com:" + h3Port;
+ do_get_profile();
+
+ let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
+ Ci.nsIX509CertDB
+ );
+ // `../unit/` so that unit_ipc tests can use as well
+ addCertFromFile(
+ certdb,
+ "../../../../netwerk/test/unit/http2-ca.pem",
+ "CTu,u,u"
+ );
+});
+
+add_task(async function test_webtransport_create() {
+ const wt = new WebTransport("https://" + host + "/success");
+ await wt.ready;
+ wt.close();
+});
+
+add_task(async function test_redirect_wt() {
+ let wt = new WebTransport("https://" + host + "/redirect");
+ const e1 = await wt.ready.catch(e => e);
+ const e2 = await wt.closed.catch(e => e);
+
+ Assert.equal(e1, "WebTransportError: WebTransport connection rejected");
+ Assert.equal(e2, "WebTransportError: WebTransport connection rejected");
+});
+
+add_task(async function test_reject_wt() {
+ let wt = new WebTransport("https://" + host + "/reject");
+ const e1 = await wt.ready.catch(e => e);
+ const e2 = await wt.closed.catch(e => e);
+ Assert.equal(e1, "WebTransportError: WebTransport connection rejected");
+ Assert.equal(e2, "WebTransportError: WebTransport connection rejected");
+});
+
+add_task(async function test_immediate_server_close() {
+ let wt = new WebTransport("https://" + host + "/closeafter0ms");
+ await wt.ready;
+ await wt.closed;
+ Assert.ok(true);
+});
+
+add_task(async function test_delayed_server_close() {
+ let wt = new WebTransport("https://" + host + "/closeafter100ms");
+ await wt.ready;
+ await wt.closed;
+ Assert.ok(true);
+});
+
+add_task(async function test_wt_stream_create_bidi() {
+ let wt = new WebTransport("https://" + host + "/success");
+ await wt.ready;
+
+ let bds = await wt.createBidirectionalStream();
+ await bds.writable.close();
+ await bds.readable.cancel();
+ Assert.notEqual(bds, null);
+ wt.close();
+});
+
+add_task(async function test_wt_stream_create_uni() {
+ let wt = new WebTransport("https://" + host + "/success");
+ await wt.ready;
+
+ let uds = await wt.createUnidirectionalStream();
+ Assert.notEqual(uds, null);
+ await uds.close();
+ wt.close();
+});
+
+// TODO: datagram test
+// TODO: getStats tests
+// TODO: fix the crash discussed in bug 1822154
diff --git a/dom/webtransport/test/xpcshell/test_simple_stream.js b/dom/webtransport/test/xpcshell/test_simple_stream.js
new file mode 100644
index 0000000000..49fad973a4
--- /dev/null
+++ b/dom/webtransport/test/xpcshell/test_simple_stream.js
@@ -0,0 +1,170 @@
+/* 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/. */
+
+// keep eslint happy until it knows about WebTransport
+/* global WebTransport:false */
+/* global TextDecoderStream:false */
+
+// Using multiple files to reduce racing
+// This file tests reading/writing to incoming/outgoing streams (uni & bidi)
+//
+"use strict";
+
+var h3Port;
+var host;
+
+registerCleanupFunction(async () => {
+ Services.prefs.clearUserPref("network.dns.localDomains");
+ Services.prefs.clearUserPref("network.webtransport.enabled");
+ Services.prefs.clearUserPref("network.webtransport.datagrams.enabled");
+ Services.prefs.clearUserPref("network.webtransport.redirect.enabled");
+});
+
+var { NetUtil } = ChromeUtils.importESModule(
+ "resource://gre/modules/NetUtil.sys.mjs"
+);
+
+function readFile(file) {
+ let fstream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
+ Ci.nsIFileInputStream
+ );
+ fstream.init(file, -1, 0, 0);
+ let data = NetUtil.readInputStreamToString(fstream, fstream.available());
+ fstream.close();
+ return data;
+}
+
+function addCertFromFile(certdb, filename, trustString) {
+ let certFile = do_get_file(filename, false);
+ let pem = readFile(certFile)
+ .replace(/-----BEGIN CERTIFICATE-----/, "")
+ .replace(/-----END CERTIFICATE-----/, "")
+ .replace(/[\r\n]/g, "");
+ certdb.addCertFromBase64(pem, trustString);
+}
+
+add_setup(async function setup() {
+ Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com");
+ Services.prefs.setBoolPref("network.webtransport.enabled", true);
+ Services.prefs.setBoolPref("network.webtransport.datagrams.enabled", true);
+ Services.prefs.setBoolPref("network.webtransport.redirect.enabled", true);
+
+ h3Port = Services.env.get("MOZHTTP3_PORT");
+ Assert.notEqual(h3Port, null);
+ Assert.notEqual(h3Port, "");
+ host = "foo.example.com:" + h3Port;
+ do_get_profile();
+
+ let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
+ Ci.nsIX509CertDB
+ );
+ // `../unit/` so that unit_ipc tests can use as well
+ addCertFromFile(
+ certdb,
+ "../../../../netwerk/test/unit/http2-ca.pem",
+ "CTu,u,u"
+ );
+});
+
+// Read all chunks from |readable_stream|, decode chunks to a utf-8 string, then
+// return the string. (borrowed from wpt tests)
+async function read_stream_as_string(readable_stream) {
+ const decoder = new TextDecoderStream();
+ const decode_stream = readable_stream.pipeThrough(decoder);
+ const reader = decode_stream.getReader();
+
+ let chunks = "";
+ while (true) {
+ const { value: chunk, done } = await reader.read();
+ if (done) {
+ break;
+ }
+ chunks += chunk;
+ }
+ reader.releaseLock();
+ return chunks;
+}
+
+add_task(async function test_wt_incoming_unidi_stream() {
+ // trigger stream creation server side and default echo
+ let wt = new WebTransport(
+ "https://" + host + "/create_unidi_stream_and_hello"
+ );
+ await wt.ready;
+
+ const streams = await wt.incomingUnidirectionalStreams;
+ const stream_reader = streams.getReader();
+ const { value: recv_stream } = await stream_reader.read();
+ let str = await read_stream_as_string(recv_stream);
+ stream_reader.releaseLock();
+ Assert.equal(str, "qwerty");
+
+ wt.close();
+});
+
+add_task(async function test_wt_incoming_and_outgoing_unidi_stream() {
+ // create the client's incoming stream from the server side
+ // we need it to listen to the echo back
+ let wt = new WebTransport("https://" + host + "/create_unidi_stream");
+ await wt.ready;
+
+ // send hello to server
+ let expected = "uni_hello";
+ let writableStream = await wt.createUnidirectionalStream(); // only triggers NewStream OnWrite
+ let wsDefaultWriter = writableStream.getWriter();
+ await wsDefaultWriter.ready;
+ let data = new TextEncoder().encode(expected);
+ await wsDefaultWriter.write(data); // triggers Http3ServerEvent::Data
+ await wsDefaultWriter.close();
+ wsDefaultWriter.releaseLock();
+
+ // read the echo
+ const streams = await wt.incomingUnidirectionalStreams;
+ const stream_reader = streams.getReader();
+ const { value: recv_stream } = await stream_reader.read();
+ let str = await read_stream_as_string(recv_stream);
+ Assert.equal(str, expected);
+ stream_reader.releaseLock();
+ await recv_stream.closed;
+
+ wt.close();
+});
+
+add_task(async function test_wt_outgoing_bidi_stream() {
+ let wt = new WebTransport("https://" + host + "/success");
+ await wt.ready;
+
+ // write to server
+ let wtbds = await wt.createBidirectionalStream();
+ let writableStream = wtbds.writable;
+ let wsDefaultWriter = writableStream.getWriter();
+ await wsDefaultWriter.ready;
+ let expected = "xyzhello";
+ let data = new TextEncoder().encode(expected);
+ await wsDefaultWriter.write(data);
+ await wsDefaultWriter.close();
+ wsDefaultWriter.releaseLock();
+
+ // string goes through server and is echoed back here
+ const str = await read_stream_as_string(wtbds.readable);
+ Assert.equal(str, expected);
+
+ wt.close();
+});
+
+add_task(async function test_wt_incoming_bidi_stream() {
+ let wt = new WebTransport(
+ "https://" + host + "/create_bidi_stream_and_hello"
+ );
+ // await wt.ready; // causes occasional hang on release --verify
+
+ const stream_reader = wt.incomingBidirectionalStreams.getReader();
+ const { value: bidi_stream } = await stream_reader.read();
+ stream_reader.releaseLock();
+
+ const str = await read_stream_as_string(bidi_stream.readable);
+ Assert.equal(str, "asdfg");
+
+ wt.close();
+});
diff --git a/dom/webtransport/test/xpcshell/xpcshell.toml b/dom/webtransport/test/xpcshell/xpcshell.toml
new file mode 100644
index 0000000000..0695d452af
--- /dev/null
+++ b/dom/webtransport/test/xpcshell/xpcshell.toml
@@ -0,0 +1,29 @@
+# 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/.
+
+[DEFAULT]
+# XXX figure out why android doesn't get MOZHTTP3_PORT
+skip-if = [
+ "os == 'android'",
+ "socketprocess_networking",
+ "os == 'win' && msix", # https://bugzilla.mozilla.org/show_bug.cgi?id=1807925
+]
+
+["test_close.js"]
+
+["test_simple_conn.js"]
+skip-if = [
+ "!fission && debug",
+ "verify && tsan",
+ "verify && debug",
+]
+run-sequentially = "http3server"
+
+["test_simple_stream.js"]
+skip-if = [
+ "!fission && debug",
+ "verify && tsan",
+ "verify && debug",
+]
+run-sequentially = "http3server"