Search in sources :

Example 1 with SslAction

use of org.webpieces.ssl.api.dto.SslAction in project webpieces by deanhiller.

the class TestSslBasicClient 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 as ONE packet here and is connected
// @Test
public void testBasic() throws InterruptedException, ExecutionException, TimeoutException, GeneralSecurityException, IOException {
    Assert.assertEquals(SslActionEnum.WAIT_FOR_MORE_DATA_FROM_REMOTE_END, parseIncoming().getSslAction());
    Assert.assertEquals(SslActionEnum.WAIT_FOR_MORE_DATA_FROM_REMOTE_END, parseIncoming().getSslAction());
    DataWrapper payload = mockChannel.nextPayload();
    XFuture<List<SslAction>> resultFuture2 = svrSslParser.parseIncoming(payload);
    List<SslAction> result2 = resultFuture2.get(2, TimeUnit.SECONDS);
    Assert.assertEquals(SslActionEnum.SEND_TO_SOCKET, result2.get(0).getSslAction());
    Assert.assertEquals(SslActionEnum.SEND_LINK_ESTABLISHED_TO_APP, result2.get(1).getSslAction());
    // client is still NOT connected yet until the SSL handshake final messages are received
    Assert.assertFalse(connectFuture.isDone());
    mockChannel.forceDataRead(mockJdk, result2.get(0).getEncryptedData());
    connectFuture.get(2, TimeUnit.SECONDS);
    transferBigData();
    transferBigDataOtherWay();
    rehandShake();
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) ArrayList(java.util.ArrayList) List(java.util.List) SslAction(org.webpieces.ssl.api.dto.SslAction)

Example 2 with SslAction

use of org.webpieces.ssl.api.dto.SslAction in project webpieces by deanhiller.

the class TestSslBasicClient method rehandShake.

private void rehandShake() throws InterruptedException, ExecutionException, TimeoutException {
    SslAction action1 = svrSslParser.beginHandshake();
    mockChannel.forceDataRead(mockJdk, action1.getEncryptedData());
    SslAction action2 = parseIncoming();
    Assert.assertEquals(SslActionEnum.SEND_TO_SOCKET, action2.getSslAction());
    mockChannel.forceDataRead(mockJdk, action2.getEncryptedData());
    SslAction action3 = parseIncoming();
    Assert.assertEquals(SslActionEnum.WAIT_FOR_MORE_DATA_FROM_REMOTE_END, action3.getSslAction());
    SslAction action4 = parseIncoming();
    Assert.assertEquals(SslActionEnum.WAIT_FOR_MORE_DATA_FROM_REMOTE_END, action4.getSslAction());
    SslAction action5 = parseIncoming();
    Assert.assertEquals(SslActionEnum.SEND_TO_SOCKET, action5.getSslAction());
    mockChannel.forceDataRead(mockJdk, action5.getEncryptedData());
}
Also used : SslAction(org.webpieces.ssl.api.dto.SslAction)

Example 3 with SslAction

use of org.webpieces.ssl.api.dto.SslAction in project webpieces by deanhiller.

the class TestSslCloseClient method testRaceFarendCloseThenClientCloses.

// @Test
public void testRaceFarendCloseThenClientCloses() throws InterruptedException, ExecutionException, TimeoutException {
    SslAction action = svrSslParser.close();
    Assert.assertEquals(SslActionEnum.SEND_TO_SOCKET, action.getSslAction());
    mockChannel.forceDataRead(mockJdk, action.getEncryptedData());
    Assert.assertTrue(mockClientDataListener.isClosed());
    SslAction action2 = parseIncoming();
    Assert.assertEquals(SslActionEnum.LINK_SUCCESSFULLY_CLOSED, action2.getSslAction());
    // but before the client knew it was closing and was notified, it calls close as well
    XFuture<Void> future = channel.close();
    future.get(2, TimeUnit.SECONDS);
}
Also used : SslAction(org.webpieces.ssl.api.dto.SslAction)

Example 4 with SslAction

use of org.webpieces.ssl.api.dto.SslAction 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 5 with SslAction

use of org.webpieces.ssl.api.dto.SslAction in project webpieces by deanhiller.

the class TestSslBasicSvr method transferBigData.

private void transferBigData() throws InterruptedException, ExecutionException, TimeoutException {
    ByteBuffer b = ByteBuffer.allocate(17000);
    b.put((byte) 1);
    b.put((byte) 2);
    // simulate buffer full of 0's except first 2 and last 2
    b.position(b.limit() - 2);
    b.put((byte) 3);
    b.put((byte) 4);
    b.flip();
    XFuture<Void> future = channel.write(b);
    future.get(2, TimeUnit.SECONDS);
    // results in two ssl packets going out instead of the one that was fed in..
    SslAction action = parseIncoming();
    SslAction action2 = parseIncoming();
    Assert.assertEquals(SslActionEnum.SEND_TO_APP, action.getSslAction());
    Assert.assertEquals(SslActionEnum.SEND_TO_APP, action2.getSslAction());
    Assert.assertEquals(17000, action.getDecryptedData().getReadableSize() + action2.getDecryptedData().getReadableSize());
}
Also used : ByteBuffer(java.nio.ByteBuffer) SslAction(org.webpieces.ssl.api.dto.SslAction)

Aggregations

SslAction (org.webpieces.ssl.api.dto.SslAction)27 DataWrapper (org.webpieces.data.api.DataWrapper)11 List (java.util.List)10 ArrayList (java.util.ArrayList)6 InetSocketAddress (java.net.InetSocketAddress)4 ByteBuffer (java.nio.ByteBuffer)4 ConnectionOpen (org.webpieces.nio.api.mocks.MockAsyncListener.ConnectionOpen)4 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 TwoPools (org.webpieces.data.api.TwoPools)1 Channel (org.webpieces.nio.api.channels.Channel)1 TCPChannel (org.webpieces.nio.api.channels.TCPChannel)1 MockSvrChannel (org.webpieces.nio.api.mocks.MockSvrChannel)1 MockSvrSideJdkChannel (org.webpieces.nio.api.mocks.MockSvrSideJdkChannel)1 SSLMetrics (org.webpieces.ssl.api.SSLMetrics)1