|
42 | 42 |
|
43 | 43 | import static org.junit.Assert.assertEquals; |
44 | 44 | import static org.junit.Assert.assertTrue; |
| 45 | +import static org.junit.Assert.fail; |
45 | 46 |
|
46 | 47 | import java.io.ByteArrayOutputStream; |
47 | 48 | import java.io.IOException; |
48 | 49 | import java.util.Map; |
49 | 50 |
|
50 | 51 | import org.graalvm.polyglot.Context; |
| 52 | +import org.graalvm.polyglot.PolyglotException; |
51 | 53 | import org.graalvm.polyglot.Source; |
52 | 54 | import org.graalvm.polyglot.Value; |
53 | 55 | import org.graalvm.polyglot.io.IOAccess; |
@@ -129,4 +131,31 @@ public void testLoadFromVirtualFileSystem() { |
129 | 131 | assertEquals(42, result.asInt()); |
130 | 132 | } |
131 | 133 | } |
| 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 | + } |
132 | 161 | } |
0 commit comments