From e8f6c2738194c29c734934c068218e277400a7b3 Mon Sep 17 00:00:00 2001 From: Alex Ross Date: Thu, 21 Jan 2021 16:36:22 +0100 Subject: [PATCH] Use random port if privileged in test resolver --- extensions/vscode-test-resolver/src/extension.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/extensions/vscode-test-resolver/src/extension.ts b/extensions/vscode-test-resolver/src/extension.ts index cec710d5c56..4f3a38980e0 100644 --- a/extensions/vscode-test-resolver/src/extension.ts +++ b/extensions/vscode-test-resolver/src/extension.ts @@ -377,10 +377,15 @@ async function tunnelFactory(tunnelOptions: vscode.TunnelOptions, tunnelCreation proxySocket.pipe(remoteSocket); }); let localPort: number | undefined; - // When the tunnelOptions include a localAddressPort, we should use that. - // However, the test resolver all runs on one machine, so if the localAddressPort is the same as the remote port, - // then we must use a different port number. - if (tunnelOptions.localAddressPort) { + + if (tunnelCreationOptions.elevationRequired) { + // If elevation is required, we can't use the requeseted local port, because this tunnel factory + // only pretends to support elevation for testing purposes + localPort = 0; + } else if (tunnelOptions.localAddressPort) { + // When the tunnelOptions include a localAddressPort, we should use that. + // However, the test resolver all runs on one machine, so if the localAddressPort is the same as the remote port, + // then we must use a different port number. if (tunnelOptions.localAddressPort === tunnelOptions.remoteAddress.port) { localPort = tunnelOptions.localAddressPort + 1; } else { @@ -390,7 +395,7 @@ async function tunnelFactory(tunnelOptions: vscode.TunnelOptions, tunnelCreation // Best practice is to use the same remote port as local port when no local port is provided. // However, everything is running on one machine here, so we can't do that. // In this case, we will just increment the port number by 1. - localPort = tunnelOptions.remoteAddress.port + 1; + localPort = tunnelOptions.remoteAddress.port < 1024 ? 0 : tunnelOptions.remoteAddress.port + 1; } proxyServer.listen(localPort, () => { const localPort = (proxyServer.address()).port;