Skip to content

Commit

Permalink
Migrate some test utilities to Java
Browse files Browse the repository at this point in the history
  • Loading branch information
marianobarrios committed Oct 29, 2023
1 parent 0ef6f13 commit 71589b4
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 156 deletions.
60 changes: 60 additions & 0 deletions src/test/scala/tlschannel/helpers/BlockerByteChannel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package tlschannel.helpers;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousByteChannel;
import java.nio.channels.ByteChannel;
import java.util.concurrent.ExecutionException;

import tlschannel.WouldBlockException;

/**
* Transforms a non-blocking {@link ByteChannel} into a blocking one.
*/
class BlockerByteChannel implements ByteChannel {

private final AsynchronousByteChannel impl;

public BlockerByteChannel(AsynchronousByteChannel impl) {
this.impl = impl;
}

@Override
public int write(ByteBuffer src)throws IOException {
try {
return impl.write(src).get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}

@Override
public int read(ByteBuffer dst) throws IOException {
try {
return impl.read(dst).get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}

@Override
public boolean isOpen() {
return impl.isOpen();
}

@Override
public void close() throws IOException {
try {
impl.close();
} catch (WouldBlockException e) {
// OK
}
}

}
25 changes: 0 additions & 25 deletions src/test/scala/tlschannel/helpers/BlockerByteChannel.scala

This file was deleted.

50 changes: 50 additions & 0 deletions src/test/scala/tlschannel/helpers/NullSslContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tlschannel.helpers;

import javax.net.ssl.*;
import java.security.SecureRandom;


public class NullSslContext extends SSLContext {
public NullSslContext() {
super(new NullSslContextSpi(), null, null);
}

}

class NullSslContextSpi extends SSLContextSpi {

@Override
public SSLEngine engineCreateSSLEngine() {
return new NullSslEngine();
}

@Override
public SSLEngine engineCreateSSLEngine(String s, int i) {
throw new UnsupportedOperationException();
}

@Override
public SSLSessionContext engineGetClientSessionContext() {
throw new UnsupportedOperationException();
}

@Override
public SSLSessionContext engineGetServerSessionContext() {
throw new UnsupportedOperationException();
}
@Override
public SSLServerSocketFactory engineGetServerSocketFactory() {
throw new UnsupportedOperationException();
}

@Override
public SSLSocketFactory engineGetSocketFactory() {
throw new UnsupportedOperationException();
}

@Override
public void engineInit(KeyManager[] km, TrustManager[] tm, SecureRandom sc) {
throw new UnsupportedOperationException();
}

}
20 changes: 0 additions & 20 deletions src/test/scala/tlschannel/helpers/NullSslContext.scala

This file was deleted.

151 changes: 151 additions & 0 deletions src/test/scala/tlschannel/helpers/NullSslEngine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package tlschannel.helpers;

import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
import java.nio.ByteBuffer;
import javax.net.ssl.SSLEngineResult;
import javax.net.ssl.SSLEngineResult.Status;
import javax.net.ssl.SSLSession;

import tlschannel.impl.ByteBufferSet;
import tlschannel.impl.ByteBufferUtil;

/**
* "Null" {@link SSLEngine} that does nothing to the bytesProduced.
*/
class NullSslEngine extends SSLEngine {

/** Internal buffers are still used to prevent any underlying optimization of the wrap/unwrap.
*/
private final int bufferSize = 16000;

@Override
public void beginHandshake() {}

@Override
public void closeInbound() {}

@Override
public void closeOutbound() {}

@Override
public Runnable getDelegatedTask() {
return null;
}

@Override
public boolean getEnableSessionCreation() {
return true;
}

@Override
public String[] getEnabledCipherSuites() {
return new String[]{};
}

@Override
public String[] getEnabledProtocols() {
return new String[]{};
}

@Override
public HandshakeStatus getHandshakeStatus() {
return HandshakeStatus.NOT_HANDSHAKING;
}

@Override
public boolean getNeedClientAuth() {
return false;
}

@Override
public SSLSession getSession() {
return new NullSslSession(bufferSize);
}

@Override
public String[] getSupportedCipherSuites() {
return new String[]{};
}

@Override
public String[] getSupportedProtocols(){
return new String[]{};
}

@Override
public boolean getUseClientMode() {
return true;
}

@Override
public boolean getWantClientAuth() {
return false;
}

@Override
public boolean isInboundDone() {
return false;
}

@Override
public boolean isOutboundDone() {
return false;
}

@Override
public void setEnableSessionCreation(boolean b) {}

@Override
public void setEnabledCipherSuites(String[] a) {}

@Override
public void setEnabledProtocols(String[] a) {}

@Override
public void setNeedClientAuth(boolean b) {}

@Override
public void setUseClientMode(boolean b) {}

@Override
public void setWantClientAuth(boolean b) {}

private final ByteBuffer unwrapBuffer = ByteBuffer.allocate(bufferSize);
private final ByteBuffer wrapBuffer = ByteBuffer.allocate(bufferSize);

@Override
public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts, int offset, int length) {
var dstSet = new ByteBufferSet(dsts, offset, length);
if (!src.hasRemaining()) {
return new SSLEngineResult(Status.BUFFER_UNDERFLOW, HandshakeStatus.NOT_HANDSHAKING, 0, 0);
}
int unwrapSize = Math.min(unwrapBuffer.capacity(), src.remaining());
if (dstSet.remaining() < unwrapSize) {
return new SSLEngineResult(Status.BUFFER_OVERFLOW, HandshakeStatus.NOT_HANDSHAKING, 0, 0);
}
unwrapBuffer.clear();
ByteBufferUtil.copy(src, unwrapBuffer, unwrapSize);
unwrapBuffer.flip();
dstSet.putRemaining(unwrapBuffer);
return new SSLEngineResult(Status.OK, HandshakeStatus.NOT_HANDSHAKING, unwrapSize, unwrapSize);
}

@Override
public SSLEngineResult wrap(ByteBuffer[] srcs, int offset, int length, ByteBuffer dst) {
var srcSet = new ByteBufferSet(srcs, offset, length);
if (!srcSet.hasRemaining()) {
return new SSLEngineResult(Status.OK, HandshakeStatus.NOT_HANDSHAKING, 0, 0);
}
int wrapSize = (int) Math.min(wrapBuffer.capacity(), srcSet.remaining());
if (dst.remaining() < wrapSize) {
return new SSLEngineResult(Status.BUFFER_OVERFLOW, HandshakeStatus.NOT_HANDSHAKING, 0, 0);
}
wrapBuffer.clear();
srcSet.get(wrapBuffer, wrapSize);
wrapBuffer.flip();
dst.put(wrapBuffer);
return new SSLEngineResult(Status.OK, HandshakeStatus.NOT_HANDSHAKING, wrapSize, wrapSize);
}

}
74 changes: 0 additions & 74 deletions src/test/scala/tlschannel/helpers/NullSslEngine.scala

This file was deleted.

Loading

0 comments on commit 71589b4

Please sign in to comment.