From b866cb1ee3d0f9c67c7990d46710662aba8d806a Mon Sep 17 00:00:00 2001 From: Jan Stola Date: Sat, 5 Jun 2021 08:18:24 +0200 Subject: [PATCH] Adding some tests of allowBufferAccess flag of HostAccess. --- .../api/test/polyglot/HostAccessTest.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/truffle/src/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/polyglot/HostAccessTest.java b/truffle/src/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/polyglot/HostAccessTest.java index d15ef0d6b5f0..c0905361032e 100644 --- a/truffle/src/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/polyglot/HostAccessTest.java +++ b/truffle/src/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/polyglot/HostAccessTest.java @@ -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; @@ -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));