tomaswolf commented on code in PR #77: URL: https://github.com/apache/mina/pull/77#discussion_r4222241278
########## mina-core/src/test/java/org/apache/mina/filter/ssl/SslEnd2EndTest.java: ########## @@ -0,0 +1,343 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.mina.filter.ssl; + +import org.apache.mina.core.buffer.IoBuffer; +import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; +import org.apache.mina.core.future.ConnectFuture; +import org.apache.mina.core.future.WriteFuture; +import org.apache.mina.core.service.IoAcceptor; +import org.apache.mina.core.service.IoConnector; +import org.apache.mina.core.service.IoHandler; +import org.apache.mina.core.service.IoHandlerAdapter; +import org.apache.mina.core.session.IoSession; +import org.apache.mina.transport.socket.nio.NioSocketAcceptor; +import org.apache.mina.transport.socket.nio.NioSocketConnector; +import org.apache.mina.util.AcceptorBindUtil; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.security.Security; +import java.util.Arrays; +import java.util.Collection; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertEquals; + +@RunWith(Parameterized.class) +public class SslEnd2EndTest { + + private static final String KEY_MANAGER_FACTORY_ALGORITHM; + private static final int MAX_LENGTH = 1024 * 1024 * 8; + + static { + String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm"); + if (algorithm == null) { + algorithm = KeyManagerFactory.getDefaultAlgorithm(); + } + + KEY_MANAGER_FACTORY_ALGORITHM = algorithm; + } + + @Parameterized.Parameters(name = "{index}: {0} / {1} / {2} / {3}") + public static Collection<Object[]> data() { + return Arrays.asList(new Object[][]{ + {"TLSv1.2", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", 2048, 0}, + {"TLSv1.2", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", 2048, 4}, + {"TLSv1.3", "TLS_AES_128_GCM_SHA256", 2048, 0}, + {"TLSv1.3", "TLS_AES_128_GCM_SHA256", 2048, 4}, + {"TLSv1.3", "TLS_AES_256_GCM_SHA384", 4096, 0}, + {"TLSv1.3", "TLS_AES_256_GCM_SHA384", 4096, 4}, + }); + } + + private final String protocol; + private final String[] cipherSuites; + private final int messageCount; + private final int messageLengthDelta; + + public SslEnd2EndTest(String protocol, String cipherSuites, int messageCount, int messageLengthDelta) { + this.protocol = protocol; + this.cipherSuites = cipherSuites.split(","); + this.messageCount = messageCount; + this.messageLengthDelta = messageLengthDelta; + } + + @Test + public void shouldSendLargeMessages() throws Exception { + ByteBuffer acceptorReceiveBuffer = ByteBuffer.allocate(MAX_LENGTH); + AcceptorHandler acceptorHandler = new AcceptorHandler(acceptorReceiveBuffer); + IoAcceptor acceptor = createAcceptor(acceptorHandler); + + try { + AcceptorBindUtil.tryBind(acceptor); + + ByteBuffer connectorReceiveBuffer = ByteBuffer.allocate(MAX_LENGTH); + ConnectorIoHandler connectorHandler = new ConnectorIoHandler(connectorReceiveBuffer); + IoConnector connector = createConnector(connectorHandler); + + try { + ConnectFuture connectFuture = connector.connect(acceptor.getLocalAddress()); + boolean connected = connectFuture.awaitUninterruptibly(4L, TimeUnit.SECONDS); + + if (!connected) { + throw new RuntimeException("Failed to connect"); + } + + IoBuffer connectorSendBuffer = IoBuffer.wrap(ByteBuffer.allocate(MAX_LENGTH)); + + for (int i = 0; i < connectorSendBuffer.limit(); i += 4) { + connectorSendBuffer.putInt(i); + } + + connectorSendBuffer.flip(); + + IoSession session = connectFuture.getSession(); + int messageLength = 65_536; + + for (int i = 0; i < messageCount; i++) { + if (messageLength > MAX_LENGTH) { + throw new RuntimeException("Message length exceeds send buffer capacity. Increase send buffer capacity"); + } + + connectorSendBuffer.limit(messageLength); + connectorSendBuffer.rewind(); + + CountDownLatch acceptorMessageReceivedLatch = new CountDownLatch(1); + acceptorHandler.reset(acceptorMessageReceivedLatch, messageLength); + + CountDownLatch connectorMessageReceivedLatch = new CountDownLatch(1); + connectorHandler.reset(connectorMessageReceivedLatch, messageLength); + + WriteFuture writeFuture = session.write(connectorSendBuffer); + boolean written = writeFuture.awaitUninterruptibly(4L, TimeUnit.SECONDS); + + if (!written) { + throw new RuntimeException("Failed to write data"); + } Review Comment: Ditto here; use `assertTrue`. ########## mina-core/src/test/java/org/apache/mina/filter/ssl/SslEnd2EndTest.java: ########## @@ -0,0 +1,343 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.mina.filter.ssl; + +import org.apache.mina.core.buffer.IoBuffer; +import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; +import org.apache.mina.core.future.ConnectFuture; +import org.apache.mina.core.future.WriteFuture; +import org.apache.mina.core.service.IoAcceptor; +import org.apache.mina.core.service.IoConnector; +import org.apache.mina.core.service.IoHandler; +import org.apache.mina.core.service.IoHandlerAdapter; +import org.apache.mina.core.session.IoSession; +import org.apache.mina.transport.socket.nio.NioSocketAcceptor; +import org.apache.mina.transport.socket.nio.NioSocketConnector; +import org.apache.mina.util.AcceptorBindUtil; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.security.Security; +import java.util.Arrays; +import java.util.Collection; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertEquals; + +@RunWith(Parameterized.class) +public class SslEnd2EndTest { + + private static final String KEY_MANAGER_FACTORY_ALGORITHM; + private static final int MAX_LENGTH = 1024 * 1024 * 8; + + static { + String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm"); + if (algorithm == null) { + algorithm = KeyManagerFactory.getDefaultAlgorithm(); + } + + KEY_MANAGER_FACTORY_ALGORITHM = algorithm; + } + + @Parameterized.Parameters(name = "{index}: {0} / {1} / {2} / {3}") + public static Collection<Object[]> data() { + return Arrays.asList(new Object[][]{ + {"TLSv1.2", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", 2048, 0}, + {"TLSv1.2", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", 2048, 4}, + {"TLSv1.3", "TLS_AES_128_GCM_SHA256", 2048, 0}, + {"TLSv1.3", "TLS_AES_128_GCM_SHA256", 2048, 4}, + {"TLSv1.3", "TLS_AES_256_GCM_SHA384", 4096, 0}, + {"TLSv1.3", "TLS_AES_256_GCM_SHA384", 4096, 4}, + }); + } + + private final String protocol; + private final String[] cipherSuites; + private final int messageCount; + private final int messageLengthDelta; + + public SslEnd2EndTest(String protocol, String cipherSuites, int messageCount, int messageLengthDelta) { + this.protocol = protocol; + this.cipherSuites = cipherSuites.split(","); + this.messageCount = messageCount; + this.messageLengthDelta = messageLengthDelta; + } + + @Test + public void shouldSendLargeMessages() throws Exception { + ByteBuffer acceptorReceiveBuffer = ByteBuffer.allocate(MAX_LENGTH); + AcceptorHandler acceptorHandler = new AcceptorHandler(acceptorReceiveBuffer); + IoAcceptor acceptor = createAcceptor(acceptorHandler); + + try { + AcceptorBindUtil.tryBind(acceptor); + + ByteBuffer connectorReceiveBuffer = ByteBuffer.allocate(MAX_LENGTH); + ConnectorIoHandler connectorHandler = new ConnectorIoHandler(connectorReceiveBuffer); + IoConnector connector = createConnector(connectorHandler); + + try { + ConnectFuture connectFuture = connector.connect(acceptor.getLocalAddress()); + boolean connected = connectFuture.awaitUninterruptibly(4L, TimeUnit.SECONDS); + + if (!connected) { + throw new RuntimeException("Failed to connect"); + } + + IoBuffer connectorSendBuffer = IoBuffer.wrap(ByteBuffer.allocate(MAX_LENGTH)); + + for (int i = 0; i < connectorSendBuffer.limit(); i += 4) { + connectorSendBuffer.putInt(i); + } + + connectorSendBuffer.flip(); + + IoSession session = connectFuture.getSession(); + int messageLength = 65_536; + + for (int i = 0; i < messageCount; i++) { + if (messageLength > MAX_LENGTH) { + throw new RuntimeException("Message length exceeds send buffer capacity. Increase send buffer capacity"); + } + + connectorSendBuffer.limit(messageLength); + connectorSendBuffer.rewind(); + + CountDownLatch acceptorMessageReceivedLatch = new CountDownLatch(1); + acceptorHandler.reset(acceptorMessageReceivedLatch, messageLength); + + CountDownLatch connectorMessageReceivedLatch = new CountDownLatch(1); + connectorHandler.reset(connectorMessageReceivedLatch, messageLength); + + WriteFuture writeFuture = session.write(connectorSendBuffer); + boolean written = writeFuture.awaitUninterruptibly(4L, TimeUnit.SECONDS); + + if (!written) { + throw new RuntimeException("Failed to write data"); + } + + boolean messageReceived = acceptorMessageReceivedLatch.await(4L, TimeUnit.SECONDS); + + if (!messageReceived) { + throw new RuntimeException("Failed to receive from connector"); + } Review Comment: Would be useful to surface a possible exception in the acceptor here. Perhaps using the same mechanism I used in my ad-hoc test, recording the first exception caught, and then doing here ``` if (!messageReceived) { assertNoException(acceptorHandler.getFailure()); fail("Failed to receive from connector"); } ``` ########## mina-core/src/test/java/org/apache/mina/filter/ssl/SslEnd2EndTest.java: ########## @@ -0,0 +1,343 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.mina.filter.ssl; + +import org.apache.mina.core.buffer.IoBuffer; +import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; +import org.apache.mina.core.future.ConnectFuture; +import org.apache.mina.core.future.WriteFuture; +import org.apache.mina.core.service.IoAcceptor; +import org.apache.mina.core.service.IoConnector; +import org.apache.mina.core.service.IoHandler; +import org.apache.mina.core.service.IoHandlerAdapter; +import org.apache.mina.core.session.IoSession; +import org.apache.mina.transport.socket.nio.NioSocketAcceptor; +import org.apache.mina.transport.socket.nio.NioSocketConnector; +import org.apache.mina.util.AcceptorBindUtil; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.security.Security; +import java.util.Arrays; +import java.util.Collection; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertEquals; + +@RunWith(Parameterized.class) +public class SslEnd2EndTest { + + private static final String KEY_MANAGER_FACTORY_ALGORITHM; + private static final int MAX_LENGTH = 1024 * 1024 * 8; + + static { + String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm"); + if (algorithm == null) { + algorithm = KeyManagerFactory.getDefaultAlgorithm(); + } + + KEY_MANAGER_FACTORY_ALGORITHM = algorithm; + } + + @Parameterized.Parameters(name = "{index}: {0} / {1} / {2} / {3}") + public static Collection<Object[]> data() { + return Arrays.asList(new Object[][]{ + {"TLSv1.2", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", 2048, 0}, + {"TLSv1.2", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", 2048, 4}, + {"TLSv1.3", "TLS_AES_128_GCM_SHA256", 2048, 0}, + {"TLSv1.3", "TLS_AES_128_GCM_SHA256", 2048, 4}, + {"TLSv1.3", "TLS_AES_256_GCM_SHA384", 4096, 0}, + {"TLSv1.3", "TLS_AES_256_GCM_SHA384", 4096, 4}, + }); + } + + private final String protocol; + private final String[] cipherSuites; + private final int messageCount; + private final int messageLengthDelta; + + public SslEnd2EndTest(String protocol, String cipherSuites, int messageCount, int messageLengthDelta) { + this.protocol = protocol; + this.cipherSuites = cipherSuites.split(","); + this.messageCount = messageCount; + this.messageLengthDelta = messageLengthDelta; + } + + @Test + public void shouldSendLargeMessages() throws Exception { + ByteBuffer acceptorReceiveBuffer = ByteBuffer.allocate(MAX_LENGTH); + AcceptorHandler acceptorHandler = new AcceptorHandler(acceptorReceiveBuffer); + IoAcceptor acceptor = createAcceptor(acceptorHandler); + + try { + AcceptorBindUtil.tryBind(acceptor); + + ByteBuffer connectorReceiveBuffer = ByteBuffer.allocate(MAX_LENGTH); + ConnectorIoHandler connectorHandler = new ConnectorIoHandler(connectorReceiveBuffer); + IoConnector connector = createConnector(connectorHandler); + + try { + ConnectFuture connectFuture = connector.connect(acceptor.getLocalAddress()); + boolean connected = connectFuture.awaitUninterruptibly(4L, TimeUnit.SECONDS); + + if (!connected) { + throw new RuntimeException("Failed to connect"); + } Review Comment: Use assertions instead of throwing `RuntimeException`. Something like ``` assertTrue("Failed to connect", connectFuture.awaitUninterruptibly(4L, TimeUnit.SECONDS)); ``` ########## mina-core/src/test/java/org/apache/mina/filter/ssl/SslEnd2EndTest.java: ########## @@ -0,0 +1,343 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.mina.filter.ssl; + +import org.apache.mina.core.buffer.IoBuffer; +import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; +import org.apache.mina.core.future.ConnectFuture; +import org.apache.mina.core.future.WriteFuture; +import org.apache.mina.core.service.IoAcceptor; +import org.apache.mina.core.service.IoConnector; +import org.apache.mina.core.service.IoHandler; +import org.apache.mina.core.service.IoHandlerAdapter; +import org.apache.mina.core.session.IoSession; +import org.apache.mina.transport.socket.nio.NioSocketAcceptor; +import org.apache.mina.transport.socket.nio.NioSocketConnector; +import org.apache.mina.util.AcceptorBindUtil; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.security.Security; +import java.util.Arrays; +import java.util.Collection; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertEquals; + +@RunWith(Parameterized.class) +public class SslEnd2EndTest { + + private static final String KEY_MANAGER_FACTORY_ALGORITHM; + private static final int MAX_LENGTH = 1024 * 1024 * 8; + + static { + String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm"); + if (algorithm == null) { + algorithm = KeyManagerFactory.getDefaultAlgorithm(); + } + + KEY_MANAGER_FACTORY_ALGORITHM = algorithm; + } + + @Parameterized.Parameters(name = "{index}: {0} / {1} / {2} / {3}") + public static Collection<Object[]> data() { + return Arrays.asList(new Object[][]{ + {"TLSv1.2", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", 2048, 0}, + {"TLSv1.2", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", 2048, 4}, + {"TLSv1.3", "TLS_AES_128_GCM_SHA256", 2048, 0}, + {"TLSv1.3", "TLS_AES_128_GCM_SHA256", 2048, 4}, + {"TLSv1.3", "TLS_AES_256_GCM_SHA384", 4096, 0}, + {"TLSv1.3", "TLS_AES_256_GCM_SHA384", 4096, 4}, + }); + } + + private final String protocol; + private final String[] cipherSuites; + private final int messageCount; + private final int messageLengthDelta; + + public SslEnd2EndTest(String protocol, String cipherSuites, int messageCount, int messageLengthDelta) { + this.protocol = protocol; + this.cipherSuites = cipherSuites.split(","); + this.messageCount = messageCount; + this.messageLengthDelta = messageLengthDelta; + } + + @Test + public void shouldSendLargeMessages() throws Exception { + ByteBuffer acceptorReceiveBuffer = ByteBuffer.allocate(MAX_LENGTH); + AcceptorHandler acceptorHandler = new AcceptorHandler(acceptorReceiveBuffer); + IoAcceptor acceptor = createAcceptor(acceptorHandler); + + try { + AcceptorBindUtil.tryBind(acceptor); + + ByteBuffer connectorReceiveBuffer = ByteBuffer.allocate(MAX_LENGTH); + ConnectorIoHandler connectorHandler = new ConnectorIoHandler(connectorReceiveBuffer); + IoConnector connector = createConnector(connectorHandler); + + try { + ConnectFuture connectFuture = connector.connect(acceptor.getLocalAddress()); + boolean connected = connectFuture.awaitUninterruptibly(4L, TimeUnit.SECONDS); + + if (!connected) { + throw new RuntimeException("Failed to connect"); + } + + IoBuffer connectorSendBuffer = IoBuffer.wrap(ByteBuffer.allocate(MAX_LENGTH)); + + for (int i = 0; i < connectorSendBuffer.limit(); i += 4) { + connectorSendBuffer.putInt(i); + } + + connectorSendBuffer.flip(); + + IoSession session = connectFuture.getSession(); + int messageLength = 65_536; + + for (int i = 0; i < messageCount; i++) { + if (messageLength > MAX_LENGTH) { + throw new RuntimeException("Message length exceeds send buffer capacity. Increase send buffer capacity"); + } + + connectorSendBuffer.limit(messageLength); + connectorSendBuffer.rewind(); + + CountDownLatch acceptorMessageReceivedLatch = new CountDownLatch(1); + acceptorHandler.reset(acceptorMessageReceivedLatch, messageLength); + + CountDownLatch connectorMessageReceivedLatch = new CountDownLatch(1); + connectorHandler.reset(connectorMessageReceivedLatch, messageLength); + + WriteFuture writeFuture = session.write(connectorSendBuffer); + boolean written = writeFuture.awaitUninterruptibly(4L, TimeUnit.SECONDS); + + if (!written) { + throw new RuntimeException("Failed to write data"); + } + + boolean messageReceived = acceptorMessageReceivedLatch.await(4L, TimeUnit.SECONDS); + + if (!messageReceived) { + throw new RuntimeException("Failed to receive from connector"); + } + + acceptorReceiveBuffer.flip(); + + for (int j = 0; j < acceptorReceiveBuffer.limit(); j += 4) { + assertEquals(j, acceptorReceiveBuffer.getInt()); + } + + boolean connectorMessageReceived = connectorMessageReceivedLatch.await(4L, TimeUnit.SECONDS); + + if (!connectorMessageReceived) { + throw new RuntimeException("Failed to receive from acceptor"); + } Review Comment: Same here: surfacing an exception caught in the connectorHandler would be useful. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
