Skip to content

Commit

Permalink
Migrate to Java: NettySslEngineTest
Browse files Browse the repository at this point in the history
  • Loading branch information
marianobarrios committed Apr 7, 2024
1 parent 0ecf209 commit d3b471b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 69 deletions.
99 changes: 99 additions & 0 deletions src/test/scala/tlschannel/NettySslEngineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package tlschannel;

import static org.junit.jupiter.api.Assertions.assertThrows;

import io.netty.buffer.ByteBufAllocator;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslProvider;
import io.netty.handler.ssl.util.SimpleTrustManagerFactory;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.security.KeyStore;
import java.security.cert.X509Certificate;
import javax.net.ssl.*;
import javax.net.ssl.ManagerFactoryParameters;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.TrustManager;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

@TestInstance(Lifecycle.PER_CLASS)
public class NettySslEngineTest {

// dummy handshake as minimal sanity test
@Test
void testDummyHandshake() throws SSLException {
io.netty.handler.ssl.SslContext ctx = SslContextBuilder.forClient()
.sslProvider(SslProvider.OPENSSL)
.trustManager(dummyTrustManagerFactory)
.protocols("TLSv1.3")
.build();
SSLEngine sslEngine = ctx.newEngine(ByteBufAllocator.DEFAULT, "test", 0);

ClientTlsChannel channel = ClientTlsChannel.newBuilder(new DummyByteChannel(), sslEngine)
.withEncryptedBufferAllocator(new HeapBufferAllocator())
.build();

assertThrows(NeedsWriteException.class, () -> channel.handshake());
}

private final TrustManagerFactory dummyTrustManagerFactory = new SimpleTrustManagerFactory() {
@Override
public void engineInit(KeyStore keyStore) {}

@Override
public void engineInit(ManagerFactoryParameters params) {}

@Override
public TrustManager[] engineGetTrustManagers() {
return new TrustManager[] {new DummyTrustManager()};
}
};

private static class DummyByteChannel implements ByteChannel {
@Override
public boolean isOpen() {
return false;
}

@Override
public void close() {}

@Override
public int write(ByteBuffer src) {
return 0;
}

@Override
public int read(ByteBuffer dst) {
return 0;
}
}

private static class DummyTrustManager extends X509ExtendedTrustManager {
@Override
public void checkClientTrusted(X509Certificate[] certs, String s) {}

@Override
public void checkServerTrusted(X509Certificate[] certs, String s) {}

@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}

@Override
public void checkClientTrusted(X509Certificate[] certs, String s, Socket socket) {}

@Override
public void checkServerTrusted(X509Certificate[] certs, String s, Socket socket) {}

@Override
public void checkClientTrusted(X509Certificate[] certs, String s, SSLEngine sslEngine) {}

@Override
public void checkServerTrusted(X509Certificate[] certs, String s, SSLEngine sslEngine) {}
}
}
69 changes: 0 additions & 69 deletions src/test/scala/tlschannel/NettySslEngineTest.scala

This file was deleted.

0 comments on commit d3b471b

Please sign in to comment.