Skip to content

Commit

Permalink
Adding some tests of allowBufferAccess flag of HostAccess.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamstolis committed Jun 5, 2021
1 parent 5d4de72 commit b866cb1
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.lang.reflect.Proxy;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -372,6 +373,41 @@ private static void assertArrayAccessDisabled(Context context) {
ValueAssert.assertValue(value, false, Trait.MEMBERS, Trait.HOST_OBJECT);
}

@Test
public void testBufferAccessEnabled() {
setupEnv(HostAccess.newBuilder().allowBufferAccess(true));
assertBufferAccessEnabled(context);
}

@Test
public void testBufferAccessEnabledHostAccessCloned() {
HostAccess hostAccess = HostAccess.newBuilder().allowBufferAccess(true).build();
setupEnv(HostAccess.newBuilder(hostAccess));
assertBufferAccessEnabled(context);
}

private static void assertBufferAccessEnabled(Context context) {
ByteBuffer buffer = ByteBuffer.allocate(2);
buffer.put((byte) 42);
Value value = context.asValue(buffer);
assertTrue(value.hasBufferElements());
assertTrue(value.isBufferWritable());
assertEquals(2, value.getBufferSize());
assertEquals(42, value.readBufferByte(0));
value.writeBufferByte(1, (byte) 24);
assertEquals(24, value.readBufferByte(1));
ValueAssert.assertValue(value, false, Trait.BUFFER_ELEMENTS, Trait.MEMBERS, Trait.HOST_OBJECT);
}

@Test
public void testBufferAccessDisabled() {
setupEnv(HostAccess.newBuilder().allowBufferAccess(false));
ByteBuffer buffer = ByteBuffer.allocate(2);
Value value = context.asValue(buffer);
assertSame(buffer, value.asHostObject());
ValueAssert.assertValue(value, false, Trait.MEMBERS, Trait.HOST_OBJECT);
}

@Test
public void testListAccessEnabled() {
setupEnv(HostAccess.newBuilder().allowListAccess(true));
Expand Down

0 comments on commit b866cb1

Please sign in to comment.