use of javax.net.ssl.SSLEngineResult in project netty by netty.
the class AbstractSslEngineBenchmark method doHandshake.
protected final boolean doHandshake() throws SSLException {
clientEngine.beginHandshake();
serverEngine.beginHandshake();
SSLEngineResult clientResult = null;
SSLEngineResult serverResult = null;
boolean clientHandshakeFinished = false;
boolean serverHandshakeFinished = false;
do {
int cTOsPos = cTOs.position();
int sTOcPos = sTOc.position();
if (!clientHandshakeFinished) {
clientResult = clientEngine.wrap(empty, cTOs);
runDelegatedTasks(clientResult, clientEngine);
assert empty.remaining() == clientResult.bytesConsumed();
assert cTOs.position() - cTOsPos == clientResult.bytesProduced();
clientHandshakeFinished = isHandshakeFinished(clientResult);
}
if (!serverHandshakeFinished) {
serverResult = serverEngine.wrap(empty, sTOc);
runDelegatedTasks(serverResult, serverEngine);
assert empty.remaining() == serverResult.bytesConsumed();
assert sTOc.position() - sTOcPos == serverResult.bytesProduced();
serverHandshakeFinished = isHandshakeFinished(serverResult);
}
cTOs.flip();
sTOc.flip();
cTOsPos = cTOs.position();
sTOcPos = sTOc.position();
if (!clientHandshakeFinished) {
int clientAppReadBufferPos = clientAppReadBuffer.position();
clientResult = clientEngine.unwrap(sTOc, clientAppReadBuffer);
runDelegatedTasks(clientResult, clientEngine);
assert sTOc.position() - sTOcPos == clientResult.bytesConsumed();
assert clientAppReadBuffer.position() - clientAppReadBufferPos == clientResult.bytesProduced();
clientHandshakeFinished = isHandshakeFinished(clientResult);
} else {
assert !sTOc.hasRemaining();
}
if (!serverHandshakeFinished) {
int serverAppReadBufferPos = serverAppReadBuffer.position();
serverResult = serverEngine.unwrap(cTOs, serverAppReadBuffer);
runDelegatedTasks(serverResult, serverEngine);
assert cTOs.position() - cTOsPos == serverResult.bytesConsumed();
assert serverAppReadBuffer.position() - serverAppReadBufferPos == serverResult.bytesProduced();
serverHandshakeFinished = isHandshakeFinished(serverResult);
} else {
assert !cTOs.hasRemaining();
}
sTOc.compact();
cTOs.compact();
} while (!clientHandshakeFinished || !serverHandshakeFinished);
return clientResult.getStatus() == SSLEngineResult.Status.OK && serverResult.getStatus() == SSLEngineResult.Status.OK;
}
use of javax.net.ssl.SSLEngineResult in project netty by netty.
the class OpenSslEngineTest method testNeededDstCapacityIsCorrectlyCalculated.
@Test
public void testNeededDstCapacityIsCorrectlyCalculated() throws Exception {
clientSslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).sslProvider(sslClientProvider()).build();
SelfSignedCertificate ssc = new SelfSignedCertificate();
serverSslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).sslProvider(sslServerProvider()).build();
SSLEngine clientEngine = null;
SSLEngine serverEngine = null;
try {
clientEngine = clientSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);
serverEngine = serverSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);
handshake(clientEngine, serverEngine);
ByteBuffer src = allocateBuffer(1024);
ByteBuffer src2 = src.duplicate();
ByteBuffer dst = allocateBuffer(src.capacity() + MAX_TLS_RECORD_OVERHEAD_LENGTH);
SSLEngineResult result = clientEngine.wrap(new ByteBuffer[] { src, src2 }, dst);
assertEquals(SSLEngineResult.Status.BUFFER_OVERFLOW, result.getStatus());
assertEquals(0, src.position());
assertEquals(0, src2.position());
assertEquals(0, dst.position());
assertEquals(0, result.bytesConsumed());
assertEquals(0, result.bytesProduced());
} finally {
cleanupClientSslEngine(clientEngine);
cleanupServerSslEngine(serverEngine);
}
}
use of javax.net.ssl.SSLEngineResult in project robovm by robovm.
the class SSLEngineResultTest method test_bytesProduced.
/**
* Test for <code>bytesProduced()</code> method
*/
public void test_bytesProduced() {
int[] pos = { 0, 1, 1000, Integer.MAX_VALUE, (Integer.MAX_VALUE - 1) };
SSLEngineResult.Status[] enS = SSLEngineResult.Status.values();
SSLEngineResult.HandshakeStatus[] enHS = SSLEngineResult.HandshakeStatus.values();
for (int i = 0; i < enS.length; i++) {
for (int j = 0; j < enHS.length; j++) {
for (int n = 0; n < pos.length; n++) {
for (int l = 0; l < pos.length; ++l) {
SSLEngineResult res = new SSLEngineResult(enS[i], enHS[j], pos[n], pos[l]);
assertEquals("Incorrect bytesProduced", pos[l], res.bytesProduced());
}
}
}
}
}
use of javax.net.ssl.SSLEngineResult in project robovm by robovm.
the class SSLEngineResultTest method test_bytesConsumed.
/**
* Test for <code>bytesConsumed()</code> method
*/
public void test_bytesConsumed() {
int[] pos = { 0, 1, 1000, Integer.MAX_VALUE, (Integer.MAX_VALUE - 1) };
SSLEngineResult.Status[] enS = SSLEngineResult.Status.values();
SSLEngineResult.HandshakeStatus[] enHS = SSLEngineResult.HandshakeStatus.values();
for (int i = 0; i < enS.length; i++) {
for (int j = 0; j < enHS.length; j++) {
for (int n = 0; n < pos.length; n++) {
for (int l = 0; l < pos.length; l++) {
SSLEngineResult res = new SSLEngineResult(enS[i], enHS[j], pos[n], pos[l]);
assertEquals("Incorrect bytesConsumed", pos[n], res.bytesConsumed());
}
}
}
}
}
use of javax.net.ssl.SSLEngineResult in project robovm by robovm.
the class SSLEngineResultTest method test_ConstructorLjavax_net_ssl_SSLEngineResult_StatusLjavax_net_ssl_SSLEngineResult_HandshakeStatusII.
/**
* Test for <code>SSLEngineResult(SSLEngineResult.Status status,
* SSLEngineResult.HandshakeStatus handshakeStatus,
* int bytesConsumed,
* int bytesProduced) </code> constructor and
* <code>getHandshakeStatus()</code>
* <code>getStatus()</code>
* <code>bytesConsumed()</code>
* <code>bytesProduced()</code>
* <code>toString()</code>
* methods
* Assertions:
* constructor throws IllegalArgumentException when bytesConsumed
* or bytesProduced is negative or when status or handshakeStatus
* is null
*
*/
public void test_ConstructorLjavax_net_ssl_SSLEngineResult_StatusLjavax_net_ssl_SSLEngineResult_HandshakeStatusII() {
int[] neg = { -1, -10, -1000, Integer.MIN_VALUE, (Integer.MIN_VALUE + 1) };
try {
new SSLEngineResult(null, SSLEngineResult.HandshakeStatus.FINISHED, 1, 1);
fail("IllegalArgumentException must be thrown");
} catch (IllegalArgumentException e) {
}
try {
new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW, null, 1, 1);
fail("IllegalArgumentException must be thrown");
} catch (IllegalArgumentException e) {
}
for (int i = 0; i < neg.length; i++) {
try {
new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW, SSLEngineResult.HandshakeStatus.FINISHED, neg[i], 1);
fail("IllegalArgumentException must be thrown");
} catch (IllegalArgumentException e) {
}
}
for (int i = 0; i < neg.length; i++) {
try {
new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW, SSLEngineResult.HandshakeStatus.FINISHED, 1, neg[i]);
fail("IllegalArgumentException must be thrown");
} catch (IllegalArgumentException e) {
}
}
try {
SSLEngineResult res = new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW, SSLEngineResult.HandshakeStatus.FINISHED, 1, 2);
assertNotNull("Null object", res);
assertEquals(1, res.bytesConsumed());
assertEquals(2, res.bytesProduced());
} catch (Exception e) {
fail("Unexpected exception: " + e);
}
}
Aggregations