mirror of
https://github.com/jlengrand/vert.x.git
synced 2026-03-10 08:51:19 +00:00
Make TestProxyBase start synchronous and blocking
This commit is contained in:
@@ -13,6 +13,8 @@ package io.vertx.test.core;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Base64;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.core.MultiMap;
|
||||
@@ -69,11 +71,9 @@ public class HttpProxy extends TestProxyBase {
|
||||
*
|
||||
* @param vertx
|
||||
* Vertx instance to use for creating the server and client
|
||||
* @param finishedHandler
|
||||
* will be called when the server has started
|
||||
*/
|
||||
@Override
|
||||
public void start(Vertx vertx, Handler<Void> finishedHandler) {
|
||||
public HttpProxy start(Vertx vertx) throws Exception {
|
||||
HttpServerOptions options = new HttpServerOptions();
|
||||
options.setHost("localhost").setPort(PORT);
|
||||
server = vertx.createHttpServer(options);
|
||||
@@ -164,9 +164,16 @@ public class HttpProxy extends TestProxyBase {
|
||||
request.response().setStatusCode(405).end("method not supported");
|
||||
}
|
||||
});
|
||||
server.listen(server -> {
|
||||
finishedHandler.handle(null);
|
||||
CompletableFuture<Void> fut = new CompletableFuture<>();
|
||||
server.listen(ar -> {
|
||||
if (ar.succeeded()) {
|
||||
fut.complete(null);
|
||||
} else {
|
||||
fut.completeExceptionally(ar.cause());
|
||||
}
|
||||
});
|
||||
fut.get(10, TimeUnit.SECONDS);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -92,14 +92,12 @@ public class HttpTestBase extends VertxTestBase {
|
||||
awaitLatch(latch);
|
||||
}
|
||||
|
||||
protected void startProxy(String username, ProxyType proxyType) throws InterruptedException {
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
protected void startProxy(String username, ProxyType proxyType) throws Exception {
|
||||
if (proxyType == ProxyType.HTTP) {
|
||||
proxy = new HttpProxy(username);
|
||||
} else {
|
||||
proxy = new SocksProxy(username);
|
||||
}
|
||||
proxy.start(vertx, v -> latch.countDown());
|
||||
awaitLatch(latch);
|
||||
proxy.start(vertx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2674,7 +2674,7 @@ public class NetTest extends VertxTestBase {
|
||||
* test socks5 proxy for accessing arbitrary server port.
|
||||
*/
|
||||
@Test
|
||||
public void testWithSocks5Proxy() {
|
||||
public void testWithSocks5Proxy() throws Exception {
|
||||
NetClientOptions clientOptions = new NetClientOptions()
|
||||
.setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5).setPort(11080));
|
||||
NetClient client = vertx.createNetClient(clientOptions);
|
||||
@@ -2682,18 +2682,17 @@ public class NetTest extends VertxTestBase {
|
||||
|
||||
});
|
||||
proxy = new SocksProxy(null);
|
||||
proxy.start(vertx, v -> {
|
||||
server.listen(1234, "localhost", ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "localhost", ar2 -> {
|
||||
if (ar2.failed()) {
|
||||
log.warn("failed", ar2.cause());
|
||||
}
|
||||
assertTrue(ar2.succeeded());
|
||||
// make sure we have gone through the proxy
|
||||
assertEquals("localhost:1234", proxy.getLastUri());
|
||||
testComplete();
|
||||
});
|
||||
proxy.start(vertx);
|
||||
server.listen(1234, "localhost", ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "localhost", ar2 -> {
|
||||
if (ar2.failed()) {
|
||||
log.warn("failed", ar2.cause());
|
||||
}
|
||||
assertTrue(ar2.succeeded());
|
||||
// make sure we have gone through the proxy
|
||||
assertEquals("localhost:1234", proxy.getLastUri());
|
||||
testComplete();
|
||||
});
|
||||
});
|
||||
await();
|
||||
@@ -2703,7 +2702,7 @@ public class NetTest extends VertxTestBase {
|
||||
* test socks5 proxy for accessing arbitrary server port with authentication.
|
||||
*/
|
||||
@Test
|
||||
public void testWithSocks5ProxyAuth() {
|
||||
public void testWithSocks5ProxyAuth() throws Exception {
|
||||
NetClientOptions clientOptions = new NetClientOptions()
|
||||
.setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5).setPort(11080)
|
||||
.setUsername("username").setPassword("username"));
|
||||
@@ -2712,13 +2711,12 @@ public class NetTest extends VertxTestBase {
|
||||
|
||||
});
|
||||
proxy = new SocksProxy("username");
|
||||
proxy.start(vertx, v -> {
|
||||
server.listen(1234, "localhost", ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "localhost", ar2 -> {
|
||||
assertTrue(ar2.succeeded());
|
||||
testComplete();
|
||||
});
|
||||
proxy.start(vertx);
|
||||
server.listen(1234, "localhost", ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "localhost", ar2 -> {
|
||||
assertTrue(ar2.succeeded());
|
||||
testComplete();
|
||||
});
|
||||
});
|
||||
await();
|
||||
@@ -2728,7 +2726,7 @@ public class NetTest extends VertxTestBase {
|
||||
* test socks5 proxy when accessing ssl server port with correct cert.
|
||||
*/
|
||||
@Test
|
||||
public void testConnectSSLWithSocks5Proxy() {
|
||||
public void testConnectSSLWithSocks5Proxy() throws Exception {
|
||||
server.close();
|
||||
NetServerOptions options = new NetServerOptions()
|
||||
.setPort(1234)
|
||||
@@ -2747,13 +2745,12 @@ public class NetTest extends VertxTestBase {
|
||||
|
||||
});
|
||||
proxy = new SocksProxy(null);
|
||||
proxy.start(vertx, v -> {
|
||||
server.listen(ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "localhost", ar2 -> {
|
||||
assertTrue(ar2.succeeded());
|
||||
testComplete();
|
||||
});
|
||||
proxy.start(vertx);
|
||||
server.listen(ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "localhost", ar2 -> {
|
||||
assertTrue(ar2.succeeded());
|
||||
testComplete();
|
||||
});
|
||||
});
|
||||
await();
|
||||
@@ -2764,7 +2761,7 @@ public class NetTest extends VertxTestBase {
|
||||
* https://github.com/eclipse/vert.x/issues/1602
|
||||
*/
|
||||
@Test
|
||||
public void testUpgradeSSLWithSocks5Proxy() {
|
||||
public void testUpgradeSSLWithSocks5Proxy() throws Exception {
|
||||
server.close();
|
||||
NetServerOptions options = new NetServerOptions()
|
||||
.setPort(1234)
|
||||
@@ -2782,19 +2779,18 @@ public class NetTest extends VertxTestBase {
|
||||
|
||||
});
|
||||
proxy = new SocksProxy(null);
|
||||
proxy.start(vertx, v -> {
|
||||
server.listen(ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "localhost", ar2 -> {
|
||||
assertTrue(ar2.succeeded());
|
||||
NetSocket ns = ar2.result();
|
||||
ns.exceptionHandler(th -> {
|
||||
fail(th);
|
||||
});
|
||||
ns.upgradeToSsl(v2 -> {
|
||||
// failure would occur before upgradeToSsl completes
|
||||
testComplete();
|
||||
});
|
||||
proxy.start(vertx);
|
||||
server.listen(ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "localhost", ar2 -> {
|
||||
assertTrue(ar2.succeeded());
|
||||
NetSocket ns = ar2.result();
|
||||
ns.exceptionHandler(th -> {
|
||||
fail(th);
|
||||
});
|
||||
ns.upgradeToSsl(v2 -> {
|
||||
// failure would occur before upgradeToSsl completes
|
||||
testComplete();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -2807,7 +2803,7 @@ public class NetTest extends VertxTestBase {
|
||||
* that limit the target host and ports (e.g. connecting to localhost or to port 25 may not be allowed)
|
||||
*/
|
||||
@Test
|
||||
public void testWithHttpConnectProxy() {
|
||||
public void testWithHttpConnectProxy() throws Exception {
|
||||
NetClientOptions clientOptions = new NetClientOptions()
|
||||
.setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setPort(13128));
|
||||
NetClient client = vertx.createNetClient(clientOptions);
|
||||
@@ -2815,18 +2811,17 @@ public class NetTest extends VertxTestBase {
|
||||
|
||||
});
|
||||
proxy = new HttpProxy(null);
|
||||
proxy.start(vertx, v -> {
|
||||
server.listen(1234, "localhost", ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "localhost", ar2 -> {
|
||||
if (ar2.failed()) {
|
||||
log.warn("failed", ar2.cause());
|
||||
}
|
||||
assertTrue(ar2.succeeded());
|
||||
// make sure we have gone through the proxy
|
||||
assertEquals("localhost:1234", proxy.getLastUri());
|
||||
testComplete();
|
||||
});
|
||||
proxy.start(vertx);
|
||||
server.listen(1234, "localhost", ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "localhost", ar2 -> {
|
||||
if (ar2.failed()) {
|
||||
log.warn("failed", ar2.cause());
|
||||
}
|
||||
assertTrue(ar2.succeeded());
|
||||
// make sure we have gone through the proxy
|
||||
assertEquals("localhost:1234", proxy.getLastUri());
|
||||
testComplete();
|
||||
});
|
||||
});
|
||||
await();
|
||||
@@ -2836,7 +2831,7 @@ public class NetTest extends VertxTestBase {
|
||||
* test socks4a proxy for accessing arbitrary server port.
|
||||
*/
|
||||
@Test
|
||||
public void testWithSocks4aProxy() {
|
||||
public void testWithSocks4aProxy() throws Exception {
|
||||
NetClientOptions clientOptions = new NetClientOptions()
|
||||
.setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS4).setPort(11080));
|
||||
NetClient client = vertx.createNetClient(clientOptions);
|
||||
@@ -2844,18 +2839,17 @@ public class NetTest extends VertxTestBase {
|
||||
|
||||
});
|
||||
proxy = new Socks4Proxy(null);
|
||||
proxy.start(vertx, v -> {
|
||||
server.listen(1234, "localhost", ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "localhost", ar2 -> {
|
||||
if (ar2.failed()) {
|
||||
log.warn("failed", ar2.cause());
|
||||
}
|
||||
assertTrue(ar2.succeeded());
|
||||
// make sure we have gone through the proxy
|
||||
assertEquals("localhost:1234", proxy.getLastUri());
|
||||
testComplete();
|
||||
});
|
||||
proxy.start(vertx);
|
||||
server.listen(1234, "localhost", ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "localhost", ar2 -> {
|
||||
if (ar2.failed()) {
|
||||
log.warn("failed", ar2.cause());
|
||||
}
|
||||
assertTrue(ar2.succeeded());
|
||||
// make sure we have gone through the proxy
|
||||
assertEquals("localhost:1234", proxy.getLastUri());
|
||||
testComplete();
|
||||
});
|
||||
});
|
||||
await();
|
||||
@@ -2865,7 +2859,7 @@ public class NetTest extends VertxTestBase {
|
||||
* test socks4a proxy for accessing arbitrary server port using username auth.
|
||||
*/
|
||||
@Test
|
||||
public void testWithSocks4aProxyAuth() {
|
||||
public void testWithSocks4aProxyAuth() throws Exception {
|
||||
NetClientOptions clientOptions = new NetClientOptions()
|
||||
.setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS4).setPort(11080)
|
||||
.setUsername("username"));
|
||||
@@ -2874,18 +2868,17 @@ public class NetTest extends VertxTestBase {
|
||||
|
||||
});
|
||||
proxy = new Socks4Proxy("username");
|
||||
proxy.start(vertx, v -> {
|
||||
server.listen(1234, "localhost", ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "localhost", ar2 -> {
|
||||
if (ar2.failed()) {
|
||||
log.warn("failed", ar2.cause());
|
||||
}
|
||||
assertTrue(ar2.succeeded());
|
||||
// make sure we have gone through the proxy
|
||||
assertEquals("localhost:1234", proxy.getLastUri());
|
||||
testComplete();
|
||||
});
|
||||
proxy.start(vertx);
|
||||
server.listen(1234, "localhost", ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "localhost", ar2 -> {
|
||||
if (ar2.failed()) {
|
||||
log.warn("failed", ar2.cause());
|
||||
}
|
||||
assertTrue(ar2.succeeded());
|
||||
// make sure we have gone through the proxy
|
||||
assertEquals("localhost:1234", proxy.getLastUri());
|
||||
testComplete();
|
||||
});
|
||||
});
|
||||
await();
|
||||
@@ -2895,26 +2888,24 @@ public class NetTest extends VertxTestBase {
|
||||
* test socks4a proxy for accessing arbitrary server port using an already resolved address.
|
||||
*/
|
||||
@Test
|
||||
public void testWithSocks4LocalResolver() {
|
||||
public void testWithSocks4LocalResolver() throws Exception {
|
||||
NetClientOptions clientOptions = new NetClientOptions()
|
||||
.setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS4).setPort(11080));
|
||||
NetClient client = vertx.createNetClient(clientOptions);
|
||||
server.connectHandler(sock -> {
|
||||
|
||||
});
|
||||
proxy = new Socks4Proxy(null);
|
||||
proxy.start(vertx, v -> {
|
||||
server.listen(1234, "localhost", ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "127.0.0.1", ar2 -> {
|
||||
if (ar2.failed()) {
|
||||
log.warn("failed", ar2.cause());
|
||||
}
|
||||
assertTrue(ar2.succeeded());
|
||||
// make sure we have gone through the proxy
|
||||
assertEquals("127.0.0.1:1234", proxy.getLastUri());
|
||||
testComplete();
|
||||
});
|
||||
proxy = new Socks4Proxy(null).start(vertx);
|
||||
server.listen(1234, "localhost", ar -> {
|
||||
assertTrue(ar.succeeded());
|
||||
client.connect(1234, "127.0.0.1", ar2 -> {
|
||||
if (ar2.failed()) {
|
||||
log.warn("failed", ar2.cause());
|
||||
}
|
||||
assertTrue(ar2.succeeded());
|
||||
// make sure we have gone through the proxy
|
||||
assertEquals("127.0.0.1:1234", proxy.getLastUri());
|
||||
testComplete();
|
||||
});
|
||||
});
|
||||
await();
|
||||
|
||||
@@ -71,12 +71,10 @@ public class ProxyErrorTest extends VertxTestBase {
|
||||
|
||||
// we don't start http/https servers, due to the error, they will not be queried
|
||||
|
||||
private void startProxy(int error, String username) throws InterruptedException {
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
private void startProxy(int error, String username) throws Exception {
|
||||
proxy = new HttpProxy(username);
|
||||
proxy.setError(error);
|
||||
proxy.start(vertx, v -> latch.countDown());
|
||||
latch.await();
|
||||
proxy.start(vertx);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -23,6 +23,9 @@ import io.vertx.core.net.NetServerOptions;
|
||||
import io.vertx.core.net.NetSocket;
|
||||
import io.vertx.core.streams.Pump;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* SOCKS4 Proxy
|
||||
* <p>
|
||||
@@ -56,11 +59,9 @@ public class Socks4Proxy extends TestProxyBase {
|
||||
*
|
||||
* @param vertx
|
||||
* Vertx instance to use for creating the server and client
|
||||
* @param finishedHandler
|
||||
* will be called when the start has started
|
||||
*/
|
||||
@Override
|
||||
public void start(Vertx vertx, Handler<Void> finishedHandler) {
|
||||
public Socks4Proxy start(Vertx vertx) throws Exception {
|
||||
NetServerOptions options = new NetServerOptions();
|
||||
options.setHost("localhost").setPort(PORT);
|
||||
server = vertx.createNetServer(options);
|
||||
@@ -120,10 +121,17 @@ public class Socks4Proxy extends TestProxyBase {
|
||||
}
|
||||
});
|
||||
});
|
||||
server.listen(result -> {
|
||||
log.debug("socks4a server started");
|
||||
finishedHandler.handle(null);
|
||||
CompletableFuture<Void> fut = new CompletableFuture<>();
|
||||
server.listen(ar -> {
|
||||
if (ar.succeeded()) {
|
||||
fut.complete(null);
|
||||
} else {
|
||||
fut.completeExceptionally(ar.cause());
|
||||
}
|
||||
});
|
||||
fut.get(10, TimeUnit.SECONDS);
|
||||
log.debug("socks4a server started");
|
||||
return this;
|
||||
}
|
||||
|
||||
private String getString(Buffer buffer) {
|
||||
|
||||
@@ -23,6 +23,9 @@ import io.vertx.core.net.NetServerOptions;
|
||||
import io.vertx.core.net.NetSocket;
|
||||
import io.vertx.core.streams.Pump;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* SOCKS5 Proxy
|
||||
* <p>
|
||||
@@ -63,11 +66,9 @@ public class SocksProxy extends TestProxyBase {
|
||||
*
|
||||
* @param vertx
|
||||
* Vertx instance to use for creating the server and client
|
||||
* @param finishedHandler
|
||||
* will be called when the start has started
|
||||
*/
|
||||
@Override
|
||||
public void start(Vertx vertx, Handler<Void> finishedHandler) {
|
||||
public SocksProxy start(Vertx vertx) throws Exception {
|
||||
NetServerOptions options = new NetServerOptions();
|
||||
options.setHost("localhost").setPort(PORT);
|
||||
server = vertx.createNetServer(options);
|
||||
@@ -92,10 +93,10 @@ public class SocksProxy extends TestProxyBase {
|
||||
throw new IllegalStateException("format error in client request (attribute type ipv4), got " + toHex(buffer2));
|
||||
}
|
||||
host = buffer2.getUnsignedByte(4) + "." +
|
||||
buffer2.getUnsignedByte(5) + "." +
|
||||
buffer2.getUnsignedByte(6) + "." +
|
||||
buffer2.getUnsignedByte(5) + "." +
|
||||
buffer2.getUnsignedByte(6) + "." +
|
||||
buffer2.getUnsignedByte(7);
|
||||
port = buffer2.getUnsignedShort(8);
|
||||
port = buffer2.getUnsignedShort(8);
|
||||
} else if(addressType == 3) {
|
||||
int stringLen = buffer2.getUnsignedByte(4);
|
||||
log.debug("string len " + stringLen);
|
||||
@@ -103,7 +104,7 @@ public class SocksProxy extends TestProxyBase {
|
||||
throw new IllegalStateException("format error in client request (attribute type domain name), got " + toHex(buffer2));
|
||||
}
|
||||
host = buffer2.getString(5, 5 + stringLen);
|
||||
port = buffer2.getUnsignedShort(5 + stringLen);
|
||||
port = buffer2.getUnsignedShort(5 + stringLen);
|
||||
} else {
|
||||
throw new IllegalStateException("expected address type ip (v4) or name, got " + addressType);
|
||||
}
|
||||
@@ -167,10 +168,17 @@ public class SocksProxy extends TestProxyBase {
|
||||
}
|
||||
});
|
||||
});
|
||||
server.listen(result -> {
|
||||
log.debug("socks5 server started");
|
||||
finishedHandler.handle(null);
|
||||
CompletableFuture<Void> fut = new CompletableFuture<>();
|
||||
server.listen(ar -> {
|
||||
if (ar.succeeded()) {
|
||||
fut.complete(null);
|
||||
} else {
|
||||
fut.completeExceptionally(ar.cause());
|
||||
}
|
||||
});
|
||||
fut.get(10, TimeUnit.SECONDS);
|
||||
log.debug("socks5 server started");
|
||||
return this;
|
||||
}
|
||||
|
||||
private String toHex(Buffer buffer) {
|
||||
|
||||
@@ -62,7 +62,7 @@ public abstract class TestProxyBase {
|
||||
|
||||
public abstract int getPort();
|
||||
|
||||
public abstract void start(Vertx vertx, Handler<Void> finishedHandler);
|
||||
public abstract TestProxyBase start(Vertx vertx) throws Exception;
|
||||
public abstract void stop();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user