diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /netwerk/test/unit/test_webtransport_simple.js | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'netwerk/test/unit/test_webtransport_simple.js')
-rw-r--r-- | netwerk/test/unit/test_webtransport_simple.js | 393 |
1 files changed, 393 insertions, 0 deletions
diff --git a/netwerk/test/unit/test_webtransport_simple.js b/netwerk/test/unit/test_webtransport_simple.js new file mode 100644 index 0000000000..01d070b497 --- /dev/null +++ b/netwerk/test/unit/test_webtransport_simple.js @@ -0,0 +1,393 @@ +// +// Simple WebTransport test +// + +/* import-globals-from head_webtransport.js */ + +"use strict"; + +var h3Port; +var host; + +var { setTimeout } = ChromeUtils.importESModule( + "resource://gre/modules/Timer.sys.mjs" +); + +registerCleanupFunction(async () => { + Services.prefs.clearUserPref("network.dns.localDomains"); + Services.prefs.clearUserPref("network.webtransport.datagrams.enabled"); +}); + +add_task(async function setup() { + Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com"); + Services.prefs.setBoolPref("network.webtransport.datagrams.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, "../unit/http2-ca.pem", "CTu,u,u"); +}); + +add_task(async function test_wt_datagram() { + let webTransport = NetUtil.newWebTransport(); + let listener = new WebTransportListener().QueryInterface( + Ci.WebTransportSessionEventListener + ); + + let pReady = new Promise(resolve => { + listener.ready = resolve; + }); + let pData = new Promise(resolve => { + listener.onDatagram = resolve; + }); + let pSize = new Promise(resolve => { + listener.onMaxDatagramSize = resolve; + }); + let pOutcome = new Promise(resolve => { + listener.onDatagramOutcome = resolve; + }); + + webTransport.asyncConnect( + NetUtil.newURI(`https://${host}/success`), + Services.scriptSecurityManager.getSystemPrincipal(), + Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL, + listener + ); + + await pReady; + + webTransport.getMaxDatagramSize(); + let size = await pSize; + info("max size:" + size); + + let rawData = new Uint8Array(size); + rawData.fill(42); + + webTransport.sendDatagram(rawData, 1); + let { id, outcome } = await pOutcome; + Assert.equal(id, 1); + Assert.equal(outcome, Ci.WebTransportSessionEventListener.SENT); + + let received = await pData; + Assert.deepEqual(received, rawData); + + webTransport.getMaxDatagramSize(); + size = await pSize; + info("max size:" + size); + + rawData = new Uint8Array(size + 1); + webTransport.sendDatagram(rawData, 2); + + pOutcome = new Promise(resolve => { + listener.onDatagramOutcome = resolve; + }); + ({ id, outcome } = await pOutcome); + Assert.equal(id, 2); + Assert.equal( + outcome, + Ci.WebTransportSessionEventListener.DROPPED_TOO_MUCH_DATA + ); + + webTransport.closeSession(0, ""); +}); + +add_task(async function test_connect_wt() { + let webTransport = NetUtil.newWebTransport(); + + await new Promise(resolve => { + let listener = new WebTransportListener().QueryInterface( + Ci.WebTransportSessionEventListener + ); + listener.ready = resolve; + + webTransport.asyncConnect( + NetUtil.newURI(`https://${host}/success`), + Services.scriptSecurityManager.getSystemPrincipal(), + Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL, + listener + ); + }); + + webTransport.closeSession(0, ""); +}); + +add_task(async function test_redirect_wt() { + let webTransport = NetUtil.newWebTransport(); + + await new Promise(resolve => { + let listener = new WebTransportListener().QueryInterface( + Ci.WebTransportSessionEventListener + ); + + listener.closed = resolve; + + webTransport.asyncConnect( + NetUtil.newURI(`https://${host}/redirect`), + Services.scriptSecurityManager.getSystemPrincipal(), + Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL, + listener + ); + }); +}); + +add_task(async function test_reject() { + let webTransport = NetUtil.newWebTransport(); + + await new Promise(resolve => { + let listener = new WebTransportListener().QueryInterface( + Ci.WebTransportSessionEventListener + ); + listener.closed = resolve; + + webTransport.asyncConnect( + NetUtil.newURI(`https://${host}/reject`), + Services.scriptSecurityManager.getSystemPrincipal(), + Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL, + listener + ); + }); +}); + +async function test_closed(path) { + let webTransport = NetUtil.newWebTransport(); + + let listener = new WebTransportListener().QueryInterface( + Ci.WebTransportSessionEventListener + ); + + let pReady = new Promise(resolve => { + listener.ready = resolve; + }); + let pClose = new Promise(resolve => { + listener.closed = resolve; + }); + webTransport.asyncConnect( + NetUtil.newURI(`https://${host}${path}`), + Services.scriptSecurityManager.getSystemPrincipal(), + Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL, + listener + ); + + await pReady; + await pClose; +} + +add_task(async function test_closed_0ms() { + test_closed("/closeafter0ms"); +}); + +add_task(async function test_closed_100ms() { + test_closed("/closeafter100ms"); +}); + +add_task(async function test_wt_stream_create() { + let webTransport = NetUtil.newWebTransport().QueryInterface( + Ci.nsIWebTransport + ); + + let error = await streamCreatePromise(webTransport, true); + Assert.equal(error, Ci.nsIWebTransport.INVALID_STATE_ERROR); + + await new Promise(resolve => { + let listener = new WebTransportListener().QueryInterface( + Ci.WebTransportSessionEventListener + ); + listener.ready = resolve; + + webTransport.asyncConnect( + NetUtil.newURI(`https://${host}/success`), + Services.scriptSecurityManager.getSystemPrincipal(), + Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL, + listener + ); + }); + + await streamCreatePromise(webTransport, true); + await streamCreatePromise(webTransport, false); + + webTransport.closeSession(0, ""); +}); + +add_task(async function test_wt_stream_send_and_stats() { + let webTransport = NetUtil.newWebTransport().QueryInterface( + Ci.nsIWebTransport + ); + + await new Promise(resolve => { + let listener = new WebTransportListener().QueryInterface( + Ci.WebTransportSessionEventListener + ); + listener.ready = resolve; + + webTransport.asyncConnect( + NetUtil.newURI(`https://${host}/success`), + Services.scriptSecurityManager.getSystemPrincipal(), + Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL, + listener + ); + }); + + let stream = await streamCreatePromise(webTransport, false); + stream.QueryInterface(Ci.nsIAsyncOutputStream); + + let data = "123456"; + stream.write(data, data.length); + + // We need some time to send the packet out. + // eslint-disable-next-line mozilla/no-arbitrary-setTimeout + await new Promise(resolve => setTimeout(resolve, 2000)); + + let stats = await sendStreamStatsPromise(stream); + Assert.equal(stats.bytesSent, data.length); + + webTransport.closeSession(0, ""); +}); + +add_task(async function test_wt_receive_stream_and_stats() { + let webTransport = NetUtil.newWebTransport().QueryInterface( + Ci.nsIWebTransport + ); + + let listener = new WebTransportListener().QueryInterface( + Ci.WebTransportSessionEventListener + ); + + let pReady = new Promise(resolve => { + listener.ready = resolve; + }); + let pStreamReady = new Promise(resolve => { + listener.streamAvailable = resolve; + }); + webTransport.asyncConnect( + NetUtil.newURI(`https://${host}/create_unidi_stream`), + Services.scriptSecurityManager.getSystemPrincipal(), + Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL, + listener + ); + + await pReady; + let stream = await pStreamReady; + + let data = await new Promise(resolve => { + let handler = new inputStreamReader().QueryInterface( + Ci.nsIInputStreamCallback + ); + handler.finish = resolve; + stream.QueryInterface(Ci.nsIAsyncInputStream); + stream.asyncWait(handler, 0, 0, Services.tm.currentThread); + }); + + info("data: " + data); + Assert.equal(data, "0123456789"); + + let stats = await receiveStreamStatsPromise(stream); + Assert.equal(stats.bytesReceived, data.length); + + stream.sendStopSending(0); + + webTransport.closeSession(0, ""); +}); + +add_task(async function test_wt_outgoing_bidi_stream() { + let webTransport = NetUtil.newWebTransport().QueryInterface( + Ci.nsIWebTransport + ); + + await new Promise(resolve => { + let listener = new WebTransportListener().QueryInterface( + Ci.WebTransportSessionEventListener + ); + listener.ready = resolve; + + webTransport.asyncConnect( + NetUtil.newURI(`https://${host}/success`), + Services.scriptSecurityManager.getSystemPrincipal(), + Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL, + listener + ); + }); + + let stream = await streamCreatePromise(webTransport, true); + stream.QueryInterface(Ci.nsIAsyncOutputStream); + + let data = "1234567"; + stream.write(data, data.length); + + let received = await new Promise(resolve => { + let handler = new inputStreamReader().QueryInterface( + Ci.nsIInputStreamCallback + ); + handler.finish = resolve; + stream.QueryInterface(Ci.nsIAsyncInputStream); + stream.asyncWaitForRead(handler, 0, 0, Services.tm.currentThread); + }); + + info("received: " + received); + Assert.equal(received, data); + + let stats = await sendStreamStatsPromise(stream); + Assert.equal(stats.bytesSent, data.length); + + stats = await receiveStreamStatsPromise(stream); + Assert.equal(stats.bytesReceived, data.length); + + webTransport.closeSession(0, ""); +}); + +add_task(async function test_wt_incoming_bidi_stream() { + let webTransport = NetUtil.newWebTransport().QueryInterface( + Ci.nsIWebTransport + ); + + let listener = new WebTransportListener().QueryInterface( + Ci.WebTransportSessionEventListener + ); + + let pReady = new Promise(resolve => { + listener.ready = resolve; + }); + let pStreamReady = new Promise(resolve => { + listener.streamAvailable = resolve; + }); + webTransport.asyncConnect( + NetUtil.newURI(`https://${host}/create_bidi_stream`), + Services.scriptSecurityManager.getSystemPrincipal(), + Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL, + listener + ); + + await pReady; + let stream = await pStreamReady; + + stream.QueryInterface(Ci.nsIAsyncOutputStream); + + let data = "12345678"; + stream.write(data, data.length); + + let received = await new Promise(resolve => { + let handler = new inputStreamReader().QueryInterface( + Ci.nsIInputStreamCallback + ); + handler.finish = resolve; + stream.QueryInterface(Ci.nsIAsyncInputStream); + stream.asyncWaitForRead(handler, 0, 0, Services.tm.currentThread); + }); + + info("received: " + received); + Assert.equal(received, data); + + let stats = await sendStreamStatsPromise(stream); + Assert.equal(stats.bytesSent, data.length); + + stats = await receiveStreamStatsPromise(stream); + Assert.equal(stats.bytesReceived, data.length); + + webTransport.closeSession(0, ""); +}); |