void clientCloseHandlers(boolean closeFromClient) { client.connect(testAddress, onSuccess(so -> { AtomicInteger counter = new AtomicInteger(0); so.endHandler(v -> assertEquals(1, counter.incrementAndGet())); so.closeHandler(v -> { assertEquals(2, counter.incrementAndGet()); testComplete(); }); if (closeFromClient) { so.close(); } })); }
void serverCloseHandlers(boolean closeFromServer, Handler<AsyncResult<NetServer>> listenHandler) { server.connectHandler((sock) -> { AtomicInteger counter = new AtomicInteger(0); sock.endHandler(v -> assertEquals(1, counter.incrementAndGet())); sock.closeHandler(v -> { assertEquals(2, counter.incrementAndGet()); testComplete(); }); if (closeFromServer) { sock.close(); } }).listen(testAddress, listenHandler); }
@Test public void testDefaultClientOptionsJson() { NetClientOptions def = new NetClientOptions(); NetClientOptions json = new NetClientOptions(new JsonObject()); assertEquals(def.getReconnectAttempts(), json.getReconnectAttempts()); assertEquals(def.getReconnectInterval(), json.getReconnectInterval()); assertEquals(def.isTrustAll(), json.isTrustAll()); assertEquals(def.getCrlPaths(), json.getCrlPaths()); assertEquals(def.getCrlValues(), json.getCrlValues()); assertEquals(def.getConnectTimeout(), json.getConnectTimeout()); assertEquals(def.isTcpNoDelay(), json.isTcpNoDelay()); assertEquals(def.isTcpKeepAlive(), json.isTcpKeepAlive()); assertEquals(def.getSoLinger(), json.getSoLinger()); assertEquals(def.isUsePooledBuffers(), json.isUsePooledBuffers()); assertEquals(def.isSsl(), json.isSsl()); assertEquals(def.isUseAlpn(), json.isUseAlpn()); assertEquals(def.getSslEngineOptions(), json.getSslEngineOptions()); assertEquals(def.getHostnameVerificationAlgorithm(), json.getHostnameVerificationAlgorithm()); }
void testEchoStringWithEncoding(String encoding) { String sent = TestUtils.randomUnicodeString(100); Buffer buffSent = Buffer.buffer(sent, encoding); testEcho(sock -> sock.write(sent, encoding), buff -> assertEquals(buffSent, buff), buffSent.length()); }
@Test @SuppressWarnings("deprecation") public void testDefaultServerOptionsJson() { NetServerOptions def = new NetServerOptions(); NetServerOptions json = new NetServerOptions(new JsonObject()); assertEquals(def.isClientAuthRequired(), json.isClientAuthRequired()); assertEquals(def.getCrlPaths(), json.getCrlPaths()); assertEquals(def.getCrlValues(), json.getCrlValues()); assertEquals(def.getAcceptBacklog(), json.getAcceptBacklog()); assertEquals(def.getPort(), json.getPort()); assertEquals(def.getHost(), json.getHost()); assertEquals(def.isClientAuthRequired(), json.isClientAuthRequired()); assertEquals(def.getCrlPaths(), json.getCrlPaths()); assertEquals(def.getCrlValues(), json.getCrlValues()); assertEquals(def.getAcceptBacklog(), json.getAcceptBacklog()); assertEquals(def.getPort(), json.getPort()); assertEquals(def.getHost(), json.getHost()); assertEquals(def.isTcpNoDelay(), json.isTcpNoDelay()); assertEquals(def.isTcpKeepAlive(), json.isTcpKeepAlive()); assertEquals(def.getSoLinger(), json.getSoLinger()); assertEquals(def.isUsePooledBuffers(), json.isUsePooledBuffers()); assertEquals(def.isSsl(), json.isSsl()); assertEquals(def.isUseAlpn(), json.isUseAlpn()); assertEquals(def.getSslEngineOptions(), json.getSslEngineOptions()); assertEquals(def.isSni(), json.isSni()); }
@Test public void testEchoString() { String sent = TestUtils.randomUnicodeString(100); Buffer buffSent = Buffer.buffer(sent); testEcho(sock -> sock.write(sent), buff -> assertEquals(buffSent, buff), buffSent.length()); }
@Test public void testEchoBytes() { Buffer sent = TestUtils.randomBuffer(100); testEcho(sock -> sock.write(sent), buff -> assertEquals(sent, buff), sent.length()); }
@Test public void testSniImplicitServerName() throws Exception { TLSTest test = new TLSTest() .clientTrust(Trust.SNI_JKS_HOST2) .address(SocketAddress.inetSocketAddress(4043, "host2.com")) .serverCert(Cert.SNI_JKS).sni(true); test.run(true); await(); assertEquals("host2.com", cnOf(test.clientPeerCert())); assertEquals("host2.com", test.indicatedServerName); }
@Test public void testListenOnWildcardPort() { server.close(); server = vertx.createNetServer(new NetServerOptions().setPort(0)); server.connectHandler((netSocket) -> { }).listen(ar -> { assertFalse(ar.failed()); assertTrue(ar.succeeded()); assertNull(ar.cause()); assertTrue(server.actualPort() > 1024); assertEquals(server, ar.result()); testComplete(); }); await(); }
@Test public void testRemoteAddress() { server.connectHandler(socket -> { SocketAddress addr = socket.remoteAddress(); assertEquals("127.0.0.1", addr.host()); socket.close(); }).listen(1234, "localhost", ar -> { assertTrue(ar.succeeded()); vertx.createNetClient(new NetClientOptions()).connect(1234, "localhost", onSuccess(socket -> { SocketAddress addr = socket.remoteAddress(); assertEquals("127.0.0.1", addr.host()); assertEquals(addr.port(), 1234); socket.closeHandler(v -> testComplete()); })); }); await(); }
@Test public void testMultipleServerClose() { this.server = vertx.createNetServer(); AtomicInteger times = new AtomicInteger(); // We assume the endHandler and the close completion handler are invoked in the same context task ThreadLocal stack = new ThreadLocal(); stack.set(true); server.connectStream().endHandler(v -> { assertNull(stack.get()); assertTrue(Vertx.currentContext().isEventLoopContext()); times.incrementAndGet(); }); server.close(ar1 -> { assertNull(stack.get()); assertTrue(Vertx.currentContext().isEventLoopContext()); server.close(ar2 -> { server.close(ar3 -> { assertEquals(1, times.get()); testComplete(); }); }); }); await(); }
@Test // SNI without server name should use the first keystore entry public void testSniWithoutServerNameUsesTheFirstKeyStoreEntry1() throws Exception { TLSTest test = new TLSTest() .clientTrust(Trust.SERVER_JKS) .serverCert(Cert.SNI_JKS).sni(true); test.run(true); await(); assertEquals("localhost", cnOf(test.clientPeerCert())); }
/** * test socks4a proxy for accessing arbitrary server port. */ @Test public void testWithSocks4aProxy() 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); 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(); }
/** * test socks4a proxy for accessing arbitrary server port using an already resolved address. */ @Test 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).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(); }
@Test // SNI present an unknown server public void testSniWithUnknownServer1() throws Exception { TLSTest test = new TLSTest() .clientTrust(Trust.SERVER_JKS) .serverCert(Cert.SNI_JKS).sni(true).serverName("unknown"); test.run(true); await(); assertEquals("localhost", cnOf(test.clientPeerCert())); }
@Test public void testSniImplicitServerNameDisabledForShortname2() throws Exception { TLSTest test = new TLSTest() .clientTrust(Trust.SERVER_JKS) .address(SocketAddress.inetSocketAddress(4043, "host1")) .serverCert(Cert.SNI_JKS).sni(true); test.run(true); await(); assertEquals("localhost", cnOf(test.clientPeerCert())); }
@Test // SNI returns the certificate for the indicated server name public void testSniWithServerNameStartTLS() throws Exception { TLSTest test = new TLSTest() .clientTrust(Trust.SNI_JKS_HOST1) .startTLS(true) .serverCert(Cert.SNI_JKS).sni(true).serverName("host1"); test.run(true); await(); assertEquals("host1", cnOf(test.clientPeerCert())); }
@Test public void testSniOverrideServerName() throws Exception { TLSTest test = new TLSTest() .clientTrust(Trust.SNI_JKS_HOST2) .address(SocketAddress.inetSocketAddress(4043, "example.com")) .serverName("host2.com") .serverCert(Cert.SNI_JKS).sni(true); test.run(true); await(); assertEquals("host2.com", cnOf(test.clientPeerCert())); }
@Test public void testSniForceShortname() throws Exception { TLSTest test = new TLSTest() .clientTrust(Trust.SNI_JKS_HOST1) .address(SocketAddress.inetSocketAddress(4043, "host1")) .serverName("host1") .serverCert(Cert.SNI_JKS).sni(true); test.run(true); await(); assertEquals("host1", cnOf(test.clientPeerCert())); }
@Test public void testClientLocalAddress() { String expectedAddress = TestUtils.loopbackAddress(); NetClientOptions clientOptions = new NetClientOptions().setLocalAddress(expectedAddress); client.close(); client = vertx.createNetClient(clientOptions); server.connectHandler(sock -> { assertEquals(expectedAddress, sock.remoteAddress().host()); sock.close(); }); server.listen(1234, "localhost", onSuccess(v -> { client.connect(1234, "localhost", onSuccess(socket -> { socket.closeHandler(v2 -> { testComplete(); }); })); })); await(); }