Skip to content

Commit

Permalink
Fix Java 8 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
marianobarrios committed Oct 29, 2023
1 parent 55cbc63 commit 151c1ba
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 63 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ plugins {
}

compileJava {
if (JavaVersion.current().ordinal() > JavaVersion.VERSION_1_8.ordinal()) {
options.compilerArgs.addAll(['--release', '8', '-Xlint'])
}
sourceCompatibility = '8'
targetCompatibility = '8'
options.compilerArgs.add('-Xlint')
}

repositories {
Expand Down
66 changes: 66 additions & 0 deletions src/test/scala/tlschannel/ApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package tlschannel;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.security.NoSuchAlgorithmException;
import java.util.Optional;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import tlschannel.impl.ByteBufferSet;
import tlschannel.impl.TlsChannelImpl;

@TestInstance(Lifecycle.PER_CLASS)
public class ApiTest {

private static final int arraySize = 1024;

private final ReadableByteChannel readChannel = Channels.newChannel(new ByteArrayInputStream(new byte[arraySize]));
private final WritableByteChannel writeChannel = Channels.newChannel(new ByteArrayOutputStream(arraySize));

private TlsChannelImpl newSocket() {
try {
SSLEngine sslEngine = SSLContext.getDefault().createSSLEngine();
return new TlsChannelImpl(
readChannel,
writeChannel,
sslEngine,
Optional.empty(),
session -> {},
true,
new TrackingAllocator(new HeapBufferAllocator()),
new TrackingAllocator(new HeapBufferAllocator()),
true /* releaseBuffers */,
false /* waitForCloseConfirmation */);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

@Test
public void testReadIntoReadOnlyBuffer() throws IOException {
try (TlsChannelImpl socket = newSocket()) {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
socket.read(new ByteBufferSet(ByteBuffer.allocate(1).asReadOnlyBuffer()));
});
}
}

@Test
public void testReadIntoBufferWithoutCapacity() throws IOException {
try (TlsChannelImpl socket = newSocket()) {
Assertions.assertEquals(
0,
socket.read(new ByteBufferSet(ByteBuffer.allocate(0))),
"read must return zero when the buffer was empty");
}
}
}
58 changes: 0 additions & 58 deletions src/test/scala/tlschannel/ApiTest.scala

This file was deleted.

4 changes: 2 additions & 2 deletions src/test/scala/tlschannel/helpers/NullSslEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void setWantClientAuth(boolean b) {}

@Override
public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts, int offset, int length) {
var dstSet = new ByteBufferSet(dsts, offset, length);
ByteBufferSet dstSet = new ByteBufferSet(dsts, offset, length);
if (!src.hasRemaining()) {
return new SSLEngineResult(Status.BUFFER_UNDERFLOW, HandshakeStatus.NOT_HANDSHAKING, 0, 0);
}
Expand All @@ -132,7 +132,7 @@ public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts, int offset, int

@Override
public SSLEngineResult wrap(ByteBuffer[] srcs, int offset, int length, ByteBuffer dst) {
var srcSet = new ByteBufferSet(srcs, offset, length);
ByteBufferSet srcSet = new ByteBufferSet(srcs, offset, length);
if (!srcSet.hasRemaining()) {
return new SSLEngineResult(Status.OK, HandshakeStatus.NOT_HANDSHAKING, 0, 0);
}
Expand Down

0 comments on commit 151c1ba

Please sign in to comment.