Search in sources :

Example 1 with ConnectionOpen

use of org.webpieces.nio.api.mocks.MockAsyncListener.ConnectionOpen in project webpieces by deanhiller.

the class TestSslBasicSvr method setup.

@Before
public void setup() throws GeneralSecurityException, IOException, InterruptedException, ExecutionException, TimeoutException {
    System.setProperty("jdk.tls.server.protocols", "TLSv1.2");
    System.setProperty("jdk.tls.client.protocols", "TLSv1.2");
    SSLEngineFactoryForTestOld sslFactory = new SSLEngineFactoryForTestOld();
    MeterRegistry meters = Metrics.globalRegistry;
    ChannelManagerFactory factory = ChannelManagerFactory.createFactory(mockJdk, meters);
    ChannelManager mgr = factory.createMultiThreadedChanMgr("test'n", new TwoPools("pl", new SimpleMeterRegistry()), new BackpressureConfig(), new DirectExecutor());
    AsyncServerManager svrMgr = AsyncServerMgrFactory.createAsyncServer(mgr, meters);
    server = svrMgr.createTcpServer(new AsyncConfig(), listener, sslFactory);
    XFuture<Void> future = server.start(new InetSocketAddress(8443));
    Assert.assertFalse(future.isDone());
    mockJdk.setThread(Thread.currentThread());
    mockJdk.fireSelector();
    future.get(2, TimeUnit.SECONDS);
    BufferPool pool = new TwoPools("p1", new SimpleMeterRegistry());
    SSLEngine clientSsl = sslFactory.createEngineForSocket();
    SSLMetrics sslMetrics = new SSLMetrics("", meters);
    clientSslParser = AsyncSSLFactory.create("svr", clientSsl, pool, sslMetrics);
    SslAction result = clientSslParser.beginHandshake();
    // simulate the jdk firing the selector with a new channel...
    mockSvrChannel.addNewChannel(mockChannel);
    mockJdk.setThread(Thread.currentThread());
    mockJdk.fireSelector();
    // assert connectionOpened was called with value of isReadyForWrites=false
    // (This feature is specifically so clients can start a time and timeout the connection if they do not
    // receive a valid payload in a certain amount of time).
    ConnectionOpen connectionOpenedInfo = listener.getConnectionOpenedInfo();
    channel = connectionOpenedInfo.channel;
    Assert.assertEquals(false, connectionOpenedInfo.isReadyForWrites);
    mockChannel.setNumBytesToConsume(100000);
    mockChannel.forceDataRead(mockJdk, result.getEncryptedData());
}
Also used : TwoPools(org.webpieces.data.api.TwoPools) InetSocketAddress(java.net.InetSocketAddress) SSLEngine(javax.net.ssl.SSLEngine) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) AsyncConfig(org.webpieces.asyncserver.api.AsyncConfig) AsyncServerManager(org.webpieces.asyncserver.api.AsyncServerManager) SslAction(org.webpieces.ssl.api.dto.SslAction) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) ConnectionOpen(org.webpieces.nio.api.mocks.MockAsyncListener.ConnectionOpen) BufferPool(org.webpieces.data.api.BufferPool) DirectExecutor(org.webpieces.util.threading.DirectExecutor) SSLMetrics(org.webpieces.ssl.api.SSLMetrics) Before(org.junit.Before)

Example 2 with ConnectionOpen

use of org.webpieces.nio.api.mocks.MockAsyncListener.ConnectionOpen in project webpieces by deanhiller.

the class TestSslCloseSvr method setup.

@Before
public void setup() throws GeneralSecurityException, IOException, InterruptedException, ExecutionException, TimeoutException {
    System.setProperty("jdk.tls.server.protocols", "TLSv1.2");
    System.setProperty("jdk.tls.client.protocols", "TLSv1.2");
    server = createServer();
    clientSslParser = createClientParser();
    XFuture<Void> future = server.start(new InetSocketAddress(8443));
    Assert.assertFalse(future.isDone());
    mockJdk.setThread(Thread.currentThread());
    mockJdk.fireSelector();
    future.get(2, TimeUnit.SECONDS);
    SslAction result = clientSslParser.beginHandshake();
    // simulate the jdk firing the selector with a new channel...
    mockSvrChannel.addNewChannel(mockChannel);
    mockJdk.setThread(Thread.currentThread());
    mockJdk.fireSelector();
    // assert connectionOpened was called with value of isReadyForWrites=false
    // (This feature is specifically so clients can start a time and timeout the connection if they do not
    // receive a valid payload in a certain amount of time).
    ConnectionOpen connectionOpenedInfo = listener.getConnectionOpenedInfo();
    channel = connectionOpenedInfo.channel;
    Assert.assertEquals(false, connectionOpenedInfo.isReadyForWrites);
    mockChannel.setNumBytesToConsume(100000);
    mockChannel.forceDataRead(mockJdk, result.getEncryptedData());
    // 3 encrypted packets sent here
    SslAction action = parseIncoming();
    Assert.assertEquals(SslActionEnum.SEND_TO_SOCKET, action.getSslAction());
    mockChannel.forceDataRead(mockJdk, action.getEncryptedData());
    Assert.assertEquals(SslActionEnum.WAIT_FOR_MORE_DATA_FROM_REMOTE_END, parseIncoming().getSslAction());
    Assert.assertEquals(SslActionEnum.SEND_LINK_ESTABLISHED_TO_APP, parseIncoming().getSslAction());
    ConnectionOpen openedInfo = listener.getConnectionOpenedInfo();
    Assert.assertEquals(true, openedInfo.isReadyForWrites);
    Assert.assertEquals(channel, openedInfo.channel);
    transferBigData();
}
Also used : ConnectionOpen(org.webpieces.nio.api.mocks.MockAsyncListener.ConnectionOpen) InetSocketAddress(java.net.InetSocketAddress) SslAction(org.webpieces.ssl.api.dto.SslAction) Before(org.junit.Before)

Example 3 with ConnectionOpen

use of org.webpieces.nio.api.mocks.MockAsyncListener.ConnectionOpen in project webpieces by deanhiller.

the class TestSslBasicSvr method testSplitData.

// @Test
public void testSplitData() throws InterruptedException, ExecutionException, TimeoutException {
    // 3 encrypted packets sent here
    SslAction action = parseIncoming();
    Assert.assertEquals(SslActionEnum.SEND_TO_SOCKET, action.getSslAction());
    List<DataWrapper> split = split(action.getEncryptedData());
    DataWrapper back = dataGen.chainDataWrappers(split.get(0), split.get(1), split.get(2), split.get(3));
    byte[] b1 = action.getEncryptedData().createByteArray();
    byte[] b2 = back.createByteArray();
    Assert.assertEquals(b1.length, b2.length);
    for (int i = 0; i < b1.length; i++) {
        Assert.assertEquals(b1[i], b2[i]);
    }
    mockChannel.forceDataRead(mockJdk, split.get(0));
    Assert.assertEquals(0, mockChannel.getPayloadQueueSize());
    mockChannel.forceDataRead(mockJdk, split.get(1));
    Assert.assertEquals(0, mockChannel.getPayloadQueueSize());
    mockChannel.forceDataRead(mockJdk, split.get(2));
    Assert.assertEquals(0, mockChannel.getPayloadQueueSize());
    mockChannel.forceDataRead(mockJdk, split.get(3));
    Assert.assertEquals(2, mockChannel.getPayloadQueueSize());
    Assert.assertEquals(SslActionEnum.WAIT_FOR_MORE_DATA_FROM_REMOTE_END, parseIncoming().getSslAction());
    Assert.assertEquals(SslActionEnum.SEND_LINK_ESTABLISHED_TO_APP, parseIncoming().getSslAction());
    ConnectionOpen openedInfo = listener.getConnectionOpenedInfo();
    Assert.assertEquals(true, openedInfo.isReadyForWrites);
    Assert.assertEquals(channel, openedInfo.channel);
    transferBigData();
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) ConnectionOpen(org.webpieces.nio.api.mocks.MockAsyncListener.ConnectionOpen) SslAction(org.webpieces.ssl.api.dto.SslAction)

Example 4 with ConnectionOpen

use of org.webpieces.nio.api.mocks.MockAsyncListener.ConnectionOpen in project webpieces by deanhiller.

the class TestSslBasicSvr method testBasic.

// begin handshake results in ONE packet client -> server (server creates runnable, creating ONE
// server creates runnable, runs it creating ONE packet server -> client
// client creates runnable, runs it creating THREE packets client -> server
// all 3 received, server creates TWO packets  client -> server (server is connected here)
// client receives two packets and is connected
// @Test
public void testBasic() throws InterruptedException, ExecutionException, TimeoutException, GeneralSecurityException, IOException {
    // 3 encrypted packets sent here
    SslAction action = parseIncoming();
    Assert.assertEquals(SslActionEnum.SEND_TO_SOCKET, action.getSslAction());
    mockChannel.forceDataRead(mockJdk, action.getEncryptedData());
    Assert.assertEquals(SslActionEnum.WAIT_FOR_MORE_DATA_FROM_REMOTE_END, parseIncoming().getSslAction());
    Assert.assertEquals(SslActionEnum.SEND_LINK_ESTABLISHED_TO_APP, parseIncoming().getSslAction());
    ConnectionOpen openedInfo = listener.getConnectionOpenedInfo();
    Assert.assertEquals(true, openedInfo.isReadyForWrites);
    Assert.assertEquals(channel, openedInfo.channel);
    transferBigData();
}
Also used : ConnectionOpen(org.webpieces.nio.api.mocks.MockAsyncListener.ConnectionOpen) SslAction(org.webpieces.ssl.api.dto.SslAction)

Aggregations

ConnectionOpen (org.webpieces.nio.api.mocks.MockAsyncListener.ConnectionOpen)4 SslAction (org.webpieces.ssl.api.dto.SslAction)4 InetSocketAddress (java.net.InetSocketAddress)2 Before (org.junit.Before)2 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)1 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)1 SSLEngine (javax.net.ssl.SSLEngine)1 AsyncConfig (org.webpieces.asyncserver.api.AsyncConfig)1 AsyncServerManager (org.webpieces.asyncserver.api.AsyncServerManager)1 BufferPool (org.webpieces.data.api.BufferPool)1 DataWrapper (org.webpieces.data.api.DataWrapper)1 TwoPools (org.webpieces.data.api.TwoPools)1 SSLMetrics (org.webpieces.ssl.api.SSLMetrics)1 DirectExecutor (org.webpieces.util.threading.DirectExecutor)1