/** * Upgrade the port to SSL. If it is already secured, this is a no-op. * If the data port type doesn't support SSL it should throw an exception. */ public void upgradeToSecure() throws IOException { Options options = this.connection.getOptions(); SSLContext context = options.getSslContext(); SSLSocketFactory factory = context.getSocketFactory(); Duration timeout = options.getConnectionTimeout(); this.sslSocket = (SSLSocket) factory.createSocket(socket, null, true); this.sslSocket.setUseClientMode(true); final CompletableFuture<Void> waitForHandshake = new CompletableFuture<>(); this.sslSocket.addHandshakeCompletedListener((evt) -> { waitForHandshake.complete(null); }); this.sslSocket.startHandshake(); try { waitForHandshake.get(timeout.toNanos(), TimeUnit.NANOSECONDS); } catch (Exception ex) { this.connection.handleCommunicationIssue(ex); } in = sslSocket.getInputStream(); out = sslSocket.getOutputStream(); }
/** * Upgrade the port to SSL. If it is already secured, this is a no-op. * If the data port type doesn't support SSL it should throw an exception. */ public void upgradeToSecure() throws IOException { Options options = this.connection.getOptions(); SSLContext context = options.getSslContext(); SSLSocketFactory factory = context.getSocketFactory(); Duration timeout = options.getConnectionTimeout(); this.sslSocket = (SSLSocket) factory.createSocket(socket, null, true); this.sslSocket.setUseClientMode(true); final CompletableFuture<Void> waitForHandshake = new CompletableFuture<>(); this.sslSocket.addHandshakeCompletedListener((evt) -> { waitForHandshake.complete(null); }); this.sslSocket.startHandshake(); try { waitForHandshake.get(timeout.toNanos(), TimeUnit.NANOSECONDS); } catch (Exception ex) { this.connection.handleCommunicationIssue(ex); } in = sslSocket.getInputStream(); out = sslSocket.getOutputStream(); }
@Test public void testPropertiesSSLOptions() throws Exception { // don't use default for tests, issues with forcing algorithm exception in other tests break it SSLContext.setDefault(TestSSLUtils.createTestSSLContext()); Properties props = new Properties(); props.setProperty(Options.PROP_SECURE, "true"); Options o = new Options.Builder(props).build(); assertEquals("default verbose", false, o.isVerbose()); // One from a different type assertNotNull("property context", o.getSslContext()); }
@Test public void testPropertiesBooleanBuilder() { Properties props = new Properties(); props.setProperty(Options.PROP_VERBOSE, "true"); props.setProperty(Options.PROP_PEDANTIC, "true"); props.setProperty(Options.PROP_NORANDOMIZE, "true"); props.setProperty(Options.PROP_USE_OLD_REQUEST_STYLE, "true"); props.setProperty(Options.PROP_OPENTLS, "true"); props.setProperty(Options.PROP_NO_ECHO, "true"); props.setProperty(Options.PROP_UTF8_SUBJECTS, "true"); Options o = new Options.Builder(props).build(); assertNull("default username", o.getUsername()); assertEquals("property verbose", true, o.isVerbose()); assertEquals("property pedantic", true, o.isPedantic()); assertEquals("property norandomize", true, o.isNoRandomize()); assertEquals("property oldstyle", true, o.isOldRequestStyle()); assertEquals("property noecho", true, o.isNoEcho()); assertEquals("property utf8", true, o.supportUTF8Subjects()); assertNotNull("property opentls", o.getSslContext()); }
@Test public void testChainedSecure() throws Exception { SSLContext ctx = TestSSLUtils.createTestSSLContext(); SSLContext.setDefault(ctx); Options o = new Options.Builder().secure().build(); assertEquals("chained context", ctx, o.getSslContext()); }
@Test public void testChainedSSLOptions() throws Exception { SSLContext ctx = TestSSLUtils.createTestSSLContext(); Options o = new Options.Builder().sslContext(ctx).build(); assertEquals("default verbose", false, o.isVerbose()); // One from a different type assertEquals("chained context", ctx, o.getSslContext()); }
@Test public void testDefaultOptions() { Options o = new Options.Builder().build(); assertEquals("default one server", 1, o.getServers().size()); assertEquals("default url", Options.DEFAULT_URL, o.getServers().toArray()[0].toString()); assertEquals("default data port type", Options.DEFAULT_DATA_PORT_TYPE, o.getDataPortType()); assertEquals("default verbose", false, o.isVerbose()); assertEquals("default pedantic", false, o.isPedantic()); assertEquals("default norandomize", false, o.isNoRandomize()); assertEquals("default oldstyle", false, o.isOldRequestStyle()); assertEquals("default noEcho", false, o.isNoEcho()); assertEquals("default UTF8 Support", false, o.supportUTF8Subjects()); assertNull("default username", o.getUsername()); assertNull("default password", o.getPassword()); assertNull("default token", o.getToken()); assertNull("default connection name", o.getConnectionName()); assertNull("default ssl context", o.getSslContext()); assertEquals("default max reconnect", Options.DEFAULT_MAX_RECONNECT, o.getMaxReconnect()); assertEquals("default ping max", Options.DEFAULT_MAX_PINGS_OUT, o.getMaxPingsOut()); assertEquals("default reconnect buffer size", Options.DEFAULT_RECONNECT_BUF_SIZE, o.getReconnectBufferSize()); assertEquals("default reconnect wait", Options.DEFAULT_RECONNECT_WAIT, o.getReconnectWait()); assertEquals("default connection timeout", Options.DEFAULT_CONNECTION_TIMEOUT, o.getConnectionTimeout()); assertEquals("default ping interval", Options.DEFAULT_PING_INTERVAL, o.getPingInterval()); assertEquals("default cleanup interval", Options.DEFAULT_REQUEST_CLEANUP_INTERVAL, o.getRequestCleanupInterval()); assertNull("error handler", o.getErrorListener()); assertNull("disconnect handler", o.getConnectionListener()); }