Search in sources :

Example 6 with TruffleFile

use of com.oracle.truffle.api.TruffleFile in project graal by oracle.

the class VirtualizedFileSystemTest method isExecutable.

@Test
public void isExecutable() {
    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 boolean executable = file.isExecutable();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
            Assert.assertFalse(cfg.formatErrorMessage("Is executable"), executable);
        } catch (SecurityException se) {
            Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), canRead);
        }
    };
    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) Test(org.junit.Test)

Example 7 with TruffleFile

use of com.oracle.truffle.api.TruffleFile in project graal by oracle.

the class VirtualizedFileSystemTest method testExists.

@Test
public void testExists() {
    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 toCreate = root.resolve(FOLDER_EXISTING).resolve(FILE_EXISTING);
            final boolean exists = toCreate.exists();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
            Assert.assertTrue(cfg.formatErrorMessage("File should exist"), exists);
        } catch (SecurityException se) {
            Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), canRead);
        }
    };
    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) Test(org.junit.Test)

Example 8 with TruffleFile

use of com.oracle.truffle.api.TruffleFile in project graal by oracle.

the class VirtualizedFileSystemTest method testWriteUsingStream.

@Test
public void testWriteUsingStream() {
    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_STREAM);
            try (BufferedWriter out = file.newBufferedWriter(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
                out.write(expectedContent, 0, expectedContent.length());
            }
            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) TruffleFile(com.oracle.truffle.api.TruffleFile) IOException(java.io.IOException) Env(com.oracle.truffle.api.TruffleLanguage.Env) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 9 with TruffleFile

use of com.oracle.truffle.api.TruffleFile 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 10 with TruffleFile

use of com.oracle.truffle.api.TruffleFile 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)

Aggregations

TruffleFile (com.oracle.truffle.api.TruffleFile)20 Env (com.oracle.truffle.api.TruffleLanguage.Env)20 Context (org.graalvm.polyglot.Context)20 Test (org.junit.Test)20 Path (java.nio.file.Path)19 IOException (java.io.IOException)12 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 ByteChannel (java.nio.channels.ByteChannel)1 SeekableByteChannel (java.nio.channels.SeekableByteChannel)1 FileTime (java.nio.file.attribute.FileTime)1