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, "");
}
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, "");
}
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, "");
}
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;
}
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, "");
}
Aggregations