Search in sources :

Example 31 with Env

use of com.oracle.truffle.api.TruffleLanguage.Env in project graal by oracle.

the class VirtualizedFileSystemTest method testIsDirectory.

@Test
public void testIsDirectory() {
    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 isDir = file.isDirectory();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
            Assert.assertFalse(cfg.formatErrorMessage("Not directory"), isDir);
        } 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 32 with Env

use of com.oracle.truffle.api.TruffleLanguage.Env in project graal by oracle.

the class VirtualizedFileSystemTest method testReadUsingStream.

@Test
public void testReadUsingStream() {
    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 StringBuilder content = new StringBuilder();
            try (final BufferedReader in = file.newBufferedReader()) {
                final char[] buffer = new char[512];
                while (true) {
                    int len = in.read(buffer);
                    if (len < 0) {
                        break;
                    } else {
                        content.append(buffer, 0, len);
                    }
                }
            }
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
            Assert.assertEquals(cfg.formatErrorMessage("Expected file content"), FILE_EXISTING_CONTENT, content.toString());
        } 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) BufferedReader(java.io.BufferedReader) TruffleFile(com.oracle.truffle.api.TruffleFile) IOException(java.io.IOException) Env(com.oracle.truffle.api.TruffleLanguage.Env) Test(org.junit.Test)

Example 33 with Env

use of com.oracle.truffle.api.TruffleLanguage.Env in project graal by oracle.

the class VirtualizedFileSystemTest method testRename.

@Test
public void testRename() {
    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 TruffleFile file = root.resolve(FILE_EXISTING_RENAME);
            final TruffleFile target = root.resolve(FILE_NEW_RENAME);
            file.move(target);
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canWrite);
            if (canRead) {
                final Collection<? extends TruffleFile> children = root.list();
                final boolean hasRenameSource = children.stream().filter((TruffleFile truffleFile) -> FILE_EXISTING_RENAME.equals(truffleFile.getName())).findAny().isPresent();
                final boolean hasRenameTarget = children.stream().filter((TruffleFile truffleFile) -> FILE_NEW_RENAME.equals(truffleFile.getName())).findAny().isPresent();
                Assert.assertFalse(cfg.formatErrorMessage("Renamed file should not exist"), hasRenameSource);
                Assert.assertTrue(cfg.formatErrorMessage("Rename target file should exist"), hasRenameTarget);
            }
        } 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) Test(org.junit.Test)

Example 34 with Env

use of com.oracle.truffle.api.TruffleLanguage.Env in project graal by oracle.

the class VirtualizedFileSystemTest method testCreateFileTest.

@Test
public void testCreateFileTest() {
    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 TruffleFile toCreate = root.resolve(FILE_NEW_CREATE_FILE);
            toCreate.createFile();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canWrite);
            if (canRead) {
                Assert.assertTrue(cfg.formatErrorMessage("Expected file exists"), toCreate.exists());
            }
        } 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) Test(org.junit.Test)

Example 35 with Env

use of com.oracle.truffle.api.TruffleLanguage.Env in project graal by oracle.

the class MultiThreadedLanguageTest method testInterruptPolyglotThread.

@Test
public void testInterruptPolyglotThread() throws Throwable {
    MultiThreadedLanguage.isThreadAccessAllowed = (req) -> {
        return true;
    };
    AtomicBoolean seenInterrupt = new AtomicBoolean(false);
    AtomicReference<Throwable> seenError = new AtomicReference<>();
    Engine engine = Engine.create();
    Context context = Context.newBuilder().allowCreateThread(true).engine(engine).build();
    Semaphore wait = new Semaphore(0);
    eval(context, new Function<Env, Object>() {

        public Object apply(Env env) {
            Semaphore waitForEnter = new Semaphore(0);
            Thread t = env.createThread(() -> {
                try {
                    waitForEnter.release();
                    wait.acquire();
                } catch (InterruptedException e) {
                    seenInterrupt.set(true);
                }
            });
            t.setUncaughtExceptionHandler((thread, e) -> seenError.set(e));
            t.start();
            try {
                waitForEnter.acquire();
            } catch (InterruptedException e) {
            }
            return t;
        }
    }).asHostObject();
    engine.close(true);
    if (seenError.get() != null) {
        throw seenError.get();
    }
    Assert.assertTrue(seenInterrupt.get());
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.MultiThreadedLanguage.LanguageContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function(java.util.function.Function) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) Env(com.oracle.truffle.api.TruffleLanguage.Env) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Aggregations

Env (com.oracle.truffle.api.TruffleLanguage.Env)48 Context (org.graalvm.polyglot.Context)41 Test (org.junit.Test)41 TruffleContext (com.oracle.truffle.api.TruffleContext)21 TruffleFile (com.oracle.truffle.api.TruffleFile)20 Path (java.nio.file.Path)19 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)16 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)13 IOException (java.io.IOException)12 Function (java.util.function.Function)10 Engine (org.graalvm.polyglot.Engine)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 PolyglotException (org.graalvm.polyglot.PolyglotException)7 Value (org.graalvm.polyglot.Value)6 LanguageContext (com.oracle.truffle.api.test.polyglot.MultiThreadedLanguage.LanguageContext)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ExecutionException (java.util.concurrent.ExecutionException)4 ExecutorService (java.util.concurrent.ExecutorService)4