From 9e3c08db40b8916968b9f30096c7be3f00ce9647 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 21 Apr 2024 13:44:51 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- netwerk/test/unit/socks_client_subprocess.js | 101 +++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 netwerk/test/unit/socks_client_subprocess.js (limited to 'netwerk/test/unit/socks_client_subprocess.js') diff --git a/netwerk/test/unit/socks_client_subprocess.js b/netwerk/test/unit/socks_client_subprocess.js new file mode 100644 index 0000000000..3fb64dbc78 --- /dev/null +++ b/netwerk/test/unit/socks_client_subprocess.js @@ -0,0 +1,101 @@ +/* global arguments */ + +"use strict"; + +var CC = Components.Constructor; + +const BinaryInputStream = CC( + "@mozilla.org/binaryinputstream;1", + "nsIBinaryInputStream", + "setInputStream" +); +const ProtocolProxyService = CC( + "@mozilla.org/network/protocol-proxy-service;1", + "nsIProtocolProxyService" +); +var sts = Cc["@mozilla.org/network/socket-transport-service;1"].getService( + Ci.nsISocketTransportService +); + +function waitForStream(stream, streamType) { + return new Promise((resolve, reject) => { + stream = stream.QueryInterface(streamType); + if (!stream) { + reject("stream didn't implement given stream type"); + } + let currentThread = + Cc["@mozilla.org/thread-manager;1"].getService().currentThread; + stream.asyncWait( + stream => { + resolve(stream); + }, + 0, + 0, + currentThread + ); + }); +} + +async function launchConnection( + socks_vers, + socks_port, + dest_host, + dest_port, + dns +) { + let pi_flags = 0; + if (dns == "remote") { + pi_flags = Ci.nsIProxyInfo.TRANSPARENT_PROXY_RESOLVES_HOST; + } + + let pps = new ProtocolProxyService(); + let pi = pps.newProxyInfo( + socks_vers, + "localhost", + socks_port, + "", + "", + pi_flags, + -1, + null + ); + let trans = sts.createTransport([], dest_host, dest_port, pi, null); + let input = trans.openInputStream(0, 0, 0); + let output = trans.openOutputStream(0, 0, 0); + input = await waitForStream(input, Ci.nsIAsyncInputStream); + let bin = new BinaryInputStream(input); + let data = bin.readBytes(5); + let response; + if (data == "PING!") { + print("client: got ping, sending pong."); + response = "PONG!"; + } else { + print("client: wrong data from server:", data); + response = "Error: wrong data received."; + } + output = await waitForStream(output, Ci.nsIAsyncOutputStream); + output.write(response, response.length); + output.close(); + input.close(); +} + +async function run(args) { + for (let arg of args) { + print("client: running test", arg); + let test = arg.split("|"); + await launchConnection( + test[0], + parseInt(test[1]), + test[2], + parseInt(test[3]), + test[4] + ); + } +} + +var satisfied = false; +run(arguments).then(() => (satisfied = true)); +var mainThread = Cc["@mozilla.org/thread-manager;1"].getService().mainThread; +while (!satisfied) { + mainThread.processNextEvent(true); +} -- cgit v1.2.3