Skip to content

Commit ee0a1d8

Browse files
committed
Test module import from already loaded literal source without I/O access.
1 parent 68ed27a commit ee0a1d8

File tree

1 file changed

+29
-0
lines changed
  • graal-js/src/com.oracle.truffle.js.test/src/com/oracle/truffle/js/test

1 file changed

+29
-0
lines changed

graal-js/src/com.oracle.truffle.js.test/src/com/oracle/truffle/js/test/ModuleTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@
4242

4343
import static org.junit.Assert.assertEquals;
4444
import static org.junit.Assert.assertTrue;
45+
import static org.junit.Assert.fail;
4546

4647
import java.io.ByteArrayOutputStream;
4748
import java.io.IOException;
4849
import java.util.Map;
4950

5051
import org.graalvm.polyglot.Context;
52+
import org.graalvm.polyglot.PolyglotException;
5153
import org.graalvm.polyglot.Source;
5254
import org.graalvm.polyglot.Value;
5355
import org.graalvm.polyglot.io.IOAccess;
@@ -129,4 +131,31 @@ public void testLoadFromVirtualFileSystem() {
129131
assertEquals(42, result.asInt());
130132
}
131133
}
134+
135+
@Test
136+
public void testImportWithoutIOPermission() {
137+
try (Context context = JSTest.newContextBuilder().allowIO(IOAccess.NONE).build()) {
138+
context.eval(Source.newBuilder("js", "export default 42;", "other.mjs").buildLiteral());
139+
Value result = context.eval(Source.newBuilder("js", "import answer from 'other.mjs'; answer;", "main.mjs").buildLiteral());
140+
assertTrue(result.isNumber());
141+
assertEquals(42, result.asInt());
142+
143+
try {
144+
context.eval(Source.newBuilder("js", "import 'non-existent.mjs';", "error.mjs").buildLiteral());
145+
fail("should have thrown");
146+
} catch (PolyglotException expected) {
147+
}
148+
}
149+
}
150+
151+
@Test
152+
public void testDynamicImportWithoutIOPermission() throws IOException {
153+
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
154+
Context context = JSTest.newContextBuilder().allowIO(IOAccess.NONE).out(out).build()) {
155+
context.eval(Source.newBuilder("js", "export const answer = 42;", "other.mjs").buildLiteral());
156+
context.eval(Source.newBuilder("js", "import('other.mjs').then(({answer}) => console.log(answer));", "test1.js").buildLiteral());
157+
context.eval(Source.newBuilder("js", "import('other.mjs').then(({answer}) => console.log(answer + 1));", "test1.mjs").buildLiteral());
158+
assertEquals("42\n43", out.toString().trim());
159+
}
160+
}
132161
}

0 commit comments

Comments
 (0)