Search in sources :

Example 61 with Context

use of org.graalvm.polyglot.Context in project graal by oracle.

the class VirtualizedFileSystemTest method testSize.

@Test
public void testSize() {
    final Context ctx = cfg.getContext();
    final Path path = cfg.getPath();
    final boolean canRead = cfg.canRead();
    languageAction = (Env env) -> {
        final TruffleFile root = cfg.needsURI() ? env.getTruffleFile(path.toUri()) : env.getTruffleFile(path.toString());
        try {
            final TruffleFile file = root.resolve(FOLDER_EXISTING).resolve(FILE_EXISTING);
            final long size = file.size();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
            Assert.assertEquals(cfg.formatErrorMessage("File size"), FILE_EXISTING_CONTENT.getBytes(StandardCharsets.UTF_8).length, size);
        } catch (SecurityException se) {
            Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), canRead);
        } catch (IOException ioe) {
            throw new AssertionError(cfg.formatErrorMessage(ioe.getMessage()), ioe);
        }
    };
    ctx.eval(LANGAUGE_ID, "");
}
Also used : Context(org.graalvm.polyglot.Context) Path(java.nio.file.Path) TruffleFile(com.oracle.truffle.api.TruffleFile) IOException(java.io.IOException) Env(com.oracle.truffle.api.TruffleLanguage.Env) Test(org.junit.Test)

Example 62 with Context

use of org.graalvm.polyglot.Context in project graal by oracle.

the class VirtualizedFileSystemTest method testWriteUsingChannel.

@Test
public void testWriteUsingChannel() {
    final Context ctx = cfg.getContext();
    final Path path = cfg.getPath();
    final boolean canRead = cfg.canRead();
    final boolean canWrite = cfg.canWrite();
    languageAction = (Env env) -> {
        final TruffleFile root = cfg.needsURI() ? env.getTruffleFile(path.toUri()) : env.getTruffleFile(path.toString());
        try {
            final String expectedContent = "0123456789";
            final TruffleFile file = root.resolve(FILE_NEW_WRITE_CHANNEL);
            try (ByteChannel bc = file.newByteChannel(EnumSet.of(StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW));
                OutputStream out = Channels.newOutputStream(bc)) {
                out.write(expectedContent.getBytes(StandardCharsets.UTF_8));
            }
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canWrite);
            if (canRead) {
                Assert.assertEquals(cfg.formatErrorMessage("Expected file size"), 10, file.size());
            }
        } catch (SecurityException se) {
            Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), canWrite);
        } catch (IOException ioe) {
            throw new AssertionError(cfg.formatErrorMessage(ioe.getMessage()), ioe);
        }
    };
    ctx.eval(LANGAUGE_ID, "");
}
Also used : Context(org.graalvm.polyglot.Context) Path(java.nio.file.Path) ByteChannel(java.nio.channels.ByteChannel) SeekableByteChannel(java.nio.channels.SeekableByteChannel) OutputStream(java.io.OutputStream) TruffleFile(com.oracle.truffle.api.TruffleFile) IOException(java.io.IOException) Env(com.oracle.truffle.api.TruffleLanguage.Env) Test(org.junit.Test)

Example 63 with Context

use of org.graalvm.polyglot.Context in project graal by oracle.

the class VirtualizedFileSystemTest method testGetCanonicalFile.

@Test
public void testGetCanonicalFile() {
    final Context ctx = cfg.getContext();
    final Path path = cfg.getPath();
    final boolean canRead = cfg.canRead();
    languageAction = (Env env) -> {
        final TruffleFile root = cfg.needsURI() ? env.getTruffleFile(path.toUri()) : env.getTruffleFile(path.toString());
        try {
            final TruffleFile canonical = root.resolve(FOLDER_EXISTING).resolve(FILE_EXISTING).getCanonicalFile();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
            Assert.assertNotNull(cfg.formatErrorMessage("Canonical file"), canonical);
        } catch (SecurityException se) {
            Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), canRead);
        } catch (IOException ioe) {
            throw new AssertionError(cfg.formatErrorMessage(ioe.getMessage()), ioe);
        }
    };
    ctx.eval(LANGAUGE_ID, "");
}
Also used : Context(org.graalvm.polyglot.Context) Path(java.nio.file.Path) TruffleFile(com.oracle.truffle.api.TruffleFile) IOException(java.io.IOException) Env(com.oracle.truffle.api.TruffleLanguage.Env) Test(org.junit.Test)

Example 64 with Context

use of org.graalvm.polyglot.Context in project graal by oracle.

the class VirtualizedFileSystemTest method createParameters.

@Parameterized.Parameters(name = "{0}")
public static Collection<Configuration> createParameters() throws IOException {
    final List<Configuration> result = new ArrayList<>();
    final FileSystem fullIO = FileSystemProviderTest.newFullIOFileSystem();
    final Path cwd = Paths.get("").toAbsolutePath();
    // Full IO
    Path accessibleDir = createContent(Files.createTempDirectory(VirtualizedFileSystemTest.class.getSimpleName()), fullIO);
    Context ctx = Context.newBuilder(LANGAUGE_ID).allowIO(true).build();
    result.add(new Configuration("Full IO", ctx, accessibleDir, cwd, fullIO, false, true, true, true));
    // No IO
    ctx = Context.newBuilder(LANGAUGE_ID).allowIO(false).build();
    Path privateDir = createContent(Files.createTempDirectory(VirtualizedFileSystemTest.class.getSimpleName()), fullIO);
    result.add(new Configuration("No IO", ctx, privateDir, cwd, fullIO, false, false, false, true));
    // Checked IO
    accessibleDir = createContent(Files.createTempDirectory(VirtualizedFileSystemTest.class.getSimpleName()), fullIO);
    Path readOnlyDir = createContent(Files.createTempDirectory(VirtualizedFileSystemTest.class.getSimpleName()), fullIO);
    privateDir = createContent(Files.createTempDirectory(VirtualizedFileSystemTest.class.getSimpleName()), fullIO);
    FileSystem fileSystem = new RestrictedFileSystem(FileSystemProviderTest.newFullIOFileSystem(accessibleDir), new AccessPredicate(Arrays.asList(accessibleDir, readOnlyDir)), new AccessPredicate(Collections.singleton(accessibleDir)));
    ctx = Context.newBuilder(LANGAUGE_ID).allowIO(true).fileSystem(fileSystem).build();
    result.add(new Configuration("Conditional IO - read/write part", ctx, accessibleDir, fullIO, false, true, true, true));
    fileSystem = new RestrictedFileSystem(FileSystemProviderTest.newFullIOFileSystem(readOnlyDir), new AccessPredicate(Arrays.asList(accessibleDir, readOnlyDir)), new AccessPredicate(Collections.singleton(accessibleDir)));
    ctx = Context.newBuilder(LANGAUGE_ID).allowIO(true).fileSystem(fileSystem).build();
    result.add(new Configuration("Conditional IO - read only part", ctx, readOnlyDir, fullIO, false, true, false, true));
    fileSystem = new RestrictedFileSystem(FileSystemProviderTest.newFullIOFileSystem(privateDir), new AccessPredicate(Arrays.asList(accessibleDir, readOnlyDir)), new AccessPredicate(Collections.singleton(accessibleDir)));
    ctx = Context.newBuilder(LANGAUGE_ID).allowIO(true).fileSystem(fileSystem).build();
    result.add(new Configuration("Conditional IO - private part", ctx, privateDir, fullIO, false, false, false, true));
    // Memory
    fileSystem = new MemoryFileSystem();
    Path memDir = mkdirs(fileSystem.parsePath(URI.create("file:///work")), fileSystem);
    ((MemoryFileSystem) fileSystem).setUserDir(memDir);
    createContent(memDir, fileSystem);
    ctx = Context.newBuilder(LANGAUGE_ID).allowIO(true).fileSystem(fileSystem).build();
    result.add(new Configuration("Memory FileSystem", ctx, memDir, fileSystem, false, true, true, true));
    return result;
}
Also used : Path(java.nio.file.Path) Context(org.graalvm.polyglot.Context) FileSystem(org.graalvm.polyglot.io.FileSystem) ArrayList(java.util.ArrayList)

Example 65 with Context

use of org.graalvm.polyglot.Context in project graal by oracle.

the class VirtualizedFileSystemTest method testToUri.

@Test
public void testToUri() {
    final Context ctx = cfg.getContext();
    final Path userDir = cfg.getUserDir();
    final boolean allowsUserDir = cfg.allowsUserDir();
    if (cfg.needsURI()) {
        // Nothing to test for URI path
        return;
    }
    languageAction = (Env env) -> {
        final TruffleFile file = env.getTruffleFile(FOLDER_EXISTING).resolve(FILE_EXISTING);
        try {
            final URI uri = file.toUri();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), allowsUserDir);
            Assert.assertEquals(cfg.formatErrorMessage("URI"), userDir.resolve(FOLDER_EXISTING).resolve(FILE_EXISTING).toUri(), uri);
        } catch (SecurityException se) {
            Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), allowsUserDir);
        }
    };
    ctx.eval(LANGAUGE_ID, "");
}
Also used : Context(org.graalvm.polyglot.Context) Path(java.nio.file.Path) TruffleFile(com.oracle.truffle.api.TruffleFile) Env(com.oracle.truffle.api.TruffleLanguage.Env) URI(java.net.URI) Test(org.junit.Test)

Aggregations

Context (org.graalvm.polyglot.Context)185 Test (org.junit.Test)148 Value (org.graalvm.polyglot.Value)58 TruffleContext (com.oracle.truffle.api.TruffleContext)56 Env (com.oracle.truffle.api.TruffleLanguage.Env)41 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)37 Engine (org.graalvm.polyglot.Engine)32 ArrayList (java.util.ArrayList)29 PolyglotException (org.graalvm.polyglot.PolyglotException)24 Source (org.graalvm.polyglot.Source)22 Path (java.nio.file.Path)21 TruffleFile (com.oracle.truffle.api.TruffleFile)20 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)20 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)19 CEntryPointContext (org.graalvm.nativeimage.c.function.CEntryPointContext)19 LanguageContext (com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext)17 IOException (java.io.IOException)14 Debugger (com.oracle.truffle.api.debug.Debugger)13 EventContext (com.oracle.truffle.api.instrumentation.EventContext)13 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)12