Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Java: NettySslEngineTest #202

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.