Example usage for javax.net.ssl SSLEngine setUseClientMode

List of usage examples for javax.net.ssl SSLEngine setUseClientMode

Introduction

In this page you can find the example usage for javax.net.ssl SSLEngine setUseClientMode.

Prototype

public abstract void setUseClientMode(boolean mode);

Source Link

Document

Configures the engine to use client (or server) mode when handshaking.

Usage

From source file:org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayerTest.java

@Theory
public void expiredClientDoesNotConnect(NetworkLayerFactory serverFactory, NetworkLayerFactory clientFactory)
        throws Exception {
    SSLEngine serverEngine = serverCtx.createSSLEngine();
    serverEngine.setUseClientMode(false);
    serverEngine.setNeedClientAuth(true);
    SSLEngine clientEngine = expiredClientCtx.createSSLEngine();
    clientEngine.setUseClientMode(true);

    ProtocolStack<IOBufferMatcher> client = ProtocolStack
            .on(clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink()))
            .filter(new SSLEngineFilterLayer(clientEngine, null)).build(new IOBufferMatcherLayer());

    ProtocolStack<IOBufferMatcher> server = ProtocolStack
            .on(serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink()))
            .filter(new SSLEngineFilterLayer(serverEngine, null)).build(new IOBufferMatcherLayer());

    IOBufferMatcher clientMatcher = client.get();
    IOBufferMatcher serverMatcher = server.get();

    clientMatcher.awaitClose();/*w  ww .  jav  a2  s . co  m*/
    serverMatcher.awaitClose();
    assertThat(clientMatcher.getCloseCause(), instanceOf(ClosedChannelException.class));
    assertThat(serverMatcher.getCloseCause(), instanceOf(SSLHandshakeException.class));
}

From source file:org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayerTest.java

@Theory
public void clientDoesNotConnectToNotYetValidServer(NetworkLayerFactory serverFactory,
        NetworkLayerFactory clientFactory) throws Exception {
    SSLEngine serverEngine = notYetValidServerCtx.createSSLEngine();
    serverEngine.setUseClientMode(false);
    serverEngine.setNeedClientAuth(true);
    SSLEngine clientEngine = expiredClientCtx.createSSLEngine();
    clientEngine.setUseClientMode(true);

    ProtocolStack<IOBufferMatcher> client = ProtocolStack
            .on(clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink()))
            .filter(new SSLEngineFilterLayer(clientEngine, null)).build(new IOBufferMatcherLayer());

    ProtocolStack<IOBufferMatcher> server = ProtocolStack
            .on(serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink()))
            .filter(new SSLEngineFilterLayer(serverEngine, null)).build(new IOBufferMatcherLayer());

    IOBufferMatcher clientMatcher = client.get();
    IOBufferMatcher serverMatcher = server.get();

    clientMatcher.awaitClose();/*w  w  w .  j a  va2s . c  om*/
    serverMatcher.awaitClose();
    assertThat(clientMatcher.getCloseCause(), instanceOf(SSLHandshakeException.class));
    assertThat(serverMatcher.getCloseCause(), instanceOf(ClosedChannelException.class));
}

From source file:org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayerTest.java

private void concurrentStress(NetworkLayerFactory serverFactory, NetworkLayerFactory clientFactory,
        int serverLimit, int clientLimit) throws java.io.IOException, InterruptedException,
        java.util.concurrent.ExecutionException, TimeoutException {
    Logger.getLogger(name.getMethodName()).log(Level.INFO,
            "Starting test with server {0} client {1} serverLimit {2} clientLimit {3}",
            new Object[] { serverFactory.getClass().getSimpleName(), clientFactory.getClass().getSimpleName(),
                    serverLimit, clientLimit });
    SSLEngine serverEngine = serverCtx.createSSLEngine();
    serverEngine.setUseClientMode(false);
    serverEngine.setNeedClientAuth(true);
    SSLEngine clientEngine = clientCtx.createSSLEngine();
    clientEngine.setUseClientMode(true);

    ProtocolStack<IOBufferMatcher> clientStack = ProtocolStack
            .on(clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink()))
            .filter(new SSLEngineFilterLayer(clientEngine, null)).build(new IOBufferMatcherLayer());

    ProtocolStack<IOBufferMatcher> serverStack = ProtocolStack
            .on(serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink()))
            .filter(new SSLEngineFilterLayer(serverEngine, null)).build(new IOBufferMatcherLayer());

    final IOBufferMatcher client = clientStack.get();
    final IOBufferMatcher server = serverStack.get();
    Future<Void> clientWork = selector.executorService().submit(new SequentialSender(client, clientLimit, 11));
    Future<Void> serverWork = selector.executorService().submit(new SequentialSender(server, serverLimit, 13));

    clientWork.get();/*from w ww. j  a  va  2  s  . c om*/
    serverWork.get();

    client.awaitByteContent(SequentialSender.matcher(serverLimit));
    server.awaitByteContent(SequentialSender.matcher(clientLimit));

    client.close();
    server.close();

    client.awaitClose();
    server.awaitClose();

    assertThat(client.asByteArray(), SequentialSender.matcher(serverLimit));
    assertThat(server.asByteArray(), SequentialSender.matcher(clientLimit));
}

From source file:org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayerTest.java

@Theory
public void sendingBiggerAndBiggerBatches(NetworkLayerFactory serverFactory, NetworkLayerFactory clientFactory,
        BatchSendBufferingFilterLayer batch)
        throws java.io.IOException, InterruptedException, java.util.concurrent.ExecutionException {
    Logger.getLogger(name.getMethodName()).log(Level.INFO, "Starting test with server {0} client {1} batch {2}",
            new Object[] { serverFactory.getClass().getSimpleName(), clientFactory.getClass().getSimpleName(),
                    batch });/* ww w. j  a v a  2 s. c om*/
    SSLEngine serverEngine = serverCtx.createSSLEngine();
    serverEngine.setUseClientMode(false);
    serverEngine.setNeedClientAuth(true);
    SSLEngine clientEngine = clientCtx.createSSLEngine();
    clientEngine.setUseClientMode(true);

    ProtocolStack<IOBufferMatcher> clientStack = ProtocolStack
            .on(clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink()))
            .filter(new SSLEngineFilterLayer(clientEngine, null)).build(new IOBufferMatcherLayer());

    ProtocolStack<IOBufferMatcher> serverStack = ProtocolStack
            .on(serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink()))
            .filter(new SSLEngineFilterLayer(serverEngine, null)).filter(batch)
            .build(new IOBufferMatcherLayer());

    final IOBufferMatcher client = clientStack.get();
    final IOBufferMatcher server = serverStack.get();
    int amount = fullTests ? 65536 * 4 : 16384;
    Future<Void> serverWork = selector.executorService().submit(new SequentialSender(server, amount, 13));

    serverWork.get();
    batch.flush();

    client.awaitByteContent(SequentialSender.matcher(amount));

    client.close();
    server.close();

    client.awaitClose();
    server.awaitClose();

    assertThat(client.asByteArray(), SequentialSender.matcher(amount));
}

From source file:org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayerTest.java

@Theory
public void bidiSendingBiggerAndBiggerBatches(NetworkLayerFactory serverFactory,
        NetworkLayerFactory clientFactory, BatchSendBufferingFilterLayer batch)
        throws java.io.IOException, InterruptedException, java.util.concurrent.ExecutionException {
    Logger.getLogger(name.getMethodName()).log(Level.INFO, "Starting test with server {0} client {1} batch {2}",
            new Object[] { serverFactory.getClass().getSimpleName(), clientFactory.getClass().getSimpleName(),
                    batch });/*ww  w  .  j a v a  2  s .co  m*/
    SSLEngine serverEngine = serverCtx.createSSLEngine();
    serverEngine.setUseClientMode(false);
    serverEngine.setNeedClientAuth(true);
    SSLEngine clientEngine = clientCtx.createSSLEngine();
    clientEngine.setUseClientMode(true);

    BatchSendBufferingFilterLayer clientBatch = batch.clone();
    ProtocolStack<IOBufferMatcher> clientStack = ProtocolStack
            .on(clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink()))
            .filter(new NoOpFilterLayer()).filter(new SSLEngineFilterLayer(clientEngine, null))
            .filter(clientBatch).filter(new NoOpFilterLayer()).build(new IOBufferMatcherLayer());

    ProtocolStack<IOBufferMatcher> serverStack = ProtocolStack
            .on(serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink()))
            .filter(new NoOpFilterLayer()).filter(new SSLEngineFilterLayer(serverEngine, null)).filter(batch)
            .filter(new NoOpFilterLayer()).build(new IOBufferMatcherLayer());

    final IOBufferMatcher client = clientStack.get();
    final IOBufferMatcher server = serverStack.get();
    int clientAmount = fullTests ? 65536 * 4 : 16384;
    Future<Void> clientWork = selector.executorService().submit(new SequentialSender(client, clientAmount, 11));
    int serverAmount = fullTests ? 65536 * 4 : 16384;
    Future<Void> serverWork = selector.executorService().submit(new SequentialSender(server, serverAmount, 13));

    clientWork.get();
    serverWork.get();
    clientBatch.flush();
    batch.flush();

    client.awaitByteContent(SequentialSender.matcher(clientAmount));
    server.awaitByteContent(SequentialSender.matcher(serverAmount));
}

From source file:org.jenkinsci.remoting.protocol.ProtocolStackLoopbackLoadStress.java

private void startClient(int n, SocketAddress serverAddress, final int clientIntervalMs, boolean ssl)
        throws IOException, ExecutionException, InterruptedException {
    SocketChannel toServer = SocketChannel.open();
    toServer.connect(serverAddress);//from   www. j a  v  a2  s. c om
    SSLEngine sslEngine = context.createSSLEngine();
    sslEngine.setUseClientMode(true);
    final Channel clientChannel = ProtocolStack.on(new NIONetworkLayer(hub, toServer, toServer))
            .named(String.format("Client %d:  %s -> %s", n, toServer.getLocalAddress(), serverAddress))
            .filter(new AckFilterLayer()).filter(ssl ? new SSLEngineFilterLayer(sslEngine, null) : null)
            .filter(new ConnectionHeadersFilterLayer(Collections.singletonMap("id", "client"),
                    new ConnectionHeadersFilterLayer.Listener() {
                        @Override
                        public void onReceiveHeaders(Map<String, String> headers)
                                throws ConnectionRefusalException {

                        }
                    }))
            .build(new ChannelApplicationLayer(executorService, null)).get().get();
    timer[n % timer.length].scheduleAtFixedRate(new TimerTask() {
        private NoOpCallable callable = new NoOpCallable();
        long start = System.currentTimeMillis();
        int times = 0;

        @Override
        public void run() {
            try {
                long start = System.currentTimeMillis();
                clientChannel.call(callable);
                times++;
                if (times % 1000 == 0) {
                    System.out
                            .println(String.format("  %s has run %d No-op callables. Rate %.1f/s expect %.1f/s",
                                    clientChannel.getName(), times,
                                    times * 1000.0 / (System.currentTimeMillis() - this.start),
                                    1000.0 / clientIntervalMs));
                }
                long duration = System.currentTimeMillis() - start;
                if (duration > 250L) {
                    System.err.println(String.format("  %s took %dms to complete a callable",
                            clientChannel.getName(), duration));
                }
            } catch (Exception e) {
                e.printStackTrace(System.err);
                IOUtils.closeQuietly(clientChannel);
                cancel();
            }
        }
    }, entropy.nextInt(clientIntervalMs), clientIntervalMs);
}