Skip to content

Commit 2bd5fec

Browse files
committed
Only give the needed permissions for PolyglotInteropTest
1 parent 9d77ca1 commit 2bd5fec

File tree

2 files changed

+19
-52
lines changed

2 files changed

+19
-52
lines changed

src/test/java/org/truffleruby/PolyglotInteropTest.java

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.truffleruby;
1111

1212
import org.graalvm.polyglot.Context;
13+
import org.graalvm.polyglot.HostAccess;
1314
import org.graalvm.polyglot.Source;
1415
import org.graalvm.polyglot.Value;
1516
import org.junit.Test;
@@ -28,46 +29,28 @@ public class PolyglotInteropTest {
2829

2930
@Test
3031
public void testCreateContext() {
31-
try (Context polyglot = Context.newBuilder()
32-
.allowAllAccess(true)
33-
.build()) {
34-
assertEquals(14, polyglot.eval(TruffleRuby.LANGUAGE_ID, "14").asInt());
35-
}
36-
}
37-
38-
@Test
39-
public void testCreateContextNoAccess() {
40-
try (Context polyglot = Context.newBuilder()
41-
.allowExperimentalOptions(true)
42-
.option(OptionsCatalog.NATIVE_PLATFORM.getName(), Boolean.FALSE.toString())
43-
.build()) {
32+
try (Context polyglot = Context.create()) {
4433
assertEquals(14, polyglot.eval(TruffleRuby.LANGUAGE_ID, "14").asInt());
4534
}
4635
}
4736

4837
@Test
4938
public void testParameters() {
50-
try (Context polyglot = Context.newBuilder()
51-
.allowAllAccess(true)
52-
.build()) {
39+
try (Context polyglot = Context.create()) {
5340
assertEquals(16, polyglot.eval("ruby", "lambda { |a, b| a + b }").execute(14, 2).asInt());
5441
}
5542
}
5643

5744
@Test
5845
public void testCallingMethods() {
59-
try (Context polyglot = Context.newBuilder()
60-
.allowAllAccess(true)
61-
.build()) {
46+
try (Context polyglot = Context.create()) {
6247
assertEquals(0.909, polyglot.eval("ruby", "Math").getMember("sin").execute(2).asDouble(), 0.01);
6348
}
6449
}
6550

6651
@Test
6752
public void testPassingBlocks() {
68-
try (Context polyglot = Context.newBuilder()
69-
.allowAllAccess(true)
70-
.build()) {
53+
try (Context polyglot = Context.newBuilder().allowHostAccess(HostAccess.ALL).build()) {
7154
final int[] counter = new int[]{0};
7255

7356
polyglot.eval("ruby", "lambda { |block| (1..3).each { |n| block.call n } }")
@@ -79,28 +62,23 @@ public void testPassingBlocks() {
7962

8063
@Test
8164
public void testCreatingObjects() {
82-
try (Context polyglot = Context.newBuilder()
83-
.allowAllAccess(true)
84-
.build()) {
65+
// Native access needed for ENV['TZ']
66+
try (Context polyglot = Context.newBuilder().allowNativeAccess(true).build()) {
8567
assertEquals(2021, polyglot.eval("ruby", "Time").newInstance(2021, 3, 18).getMember("year").execute().asInt());
8668
}
8769
}
8870

8971
@Test
9072
public void testAccessingArrays() {
91-
try (Context polyglot = Context.newBuilder()
92-
.allowAllAccess(true)
93-
.build()) {
73+
try (Context polyglot = Context.create()) {
9474
assertEquals(4, polyglot.eval("ruby", "[3, 4, 5]").getArrayElement(1).asInt());
9575
assertEquals(4, polyglot.eval("ruby", "[3, 4, 5]").as(List.class).get(1));
9676
}
9777
}
9878

9979
@Test
10080
public void testAccessingHashes() {
101-
try (Context polyglot = Context.newBuilder()
102-
.allowAllAccess(true)
103-
.build()) {
81+
try (Context polyglot = Context.create()) {
10482
final Value access = polyglot.eval("ruby", "->(hash, key) { hash[key] }");
10583
final Value hash = polyglot.eval("ruby", "{'a' => 3, 'b' => 4, 'c' => 5}");
10684
assertEquals(4, access.execute(hash, "b").asInt());
@@ -109,9 +87,7 @@ public void testAccessingHashes() {
10987

11088
@Test
11189
public void testImplementInterface() {
112-
try (Context polyglot = Context.newBuilder()
113-
.allowAllAccess(true)
114-
.build()) {
90+
try (Context polyglot = Context.newBuilder().allowHostAccess(HostAccess.EXPLICIT).build()) {
11591
final FluidForce fluidForce = polyglot.eval("ruby", FluidForce.RUBY_SOURCE).as(FluidForce.class);
11692
assertEquals(5587.008375144088, fluidForce.getFluidForce(2.0, 3.0, 6.0), 0.01);
11793
}
@@ -120,39 +96,31 @@ public void testImplementInterface() {
12096
@SuppressWarnings({"rawtypes", "unchecked"})
12197
@Test
12298
public void testImplementLambda() {
123-
try (Context polyglot = Context.newBuilder()
124-
.allowAllAccess(true)
125-
.build()) {
99+
try (Context polyglot = Context.create()) {
126100
final BiFunction adder = polyglot.eval("ruby", "lambda { |a, b| a + b }").as(BiFunction.class);
127101
assertEquals(16, (int) adder.apply(14, 2));
128102
}
129103
}
130104

131105
@Test
132106
public void testParseOnceRunMany() {
133-
try (Context polyglot = Context.newBuilder()
134-
.allowAllAccess(true)
135-
.build()) {
107+
try (Context polyglot = Context.create()) {
136108
final Value parsedOnce = polyglot.eval("ruby", "lambda { 14 }");
137109
assertEquals(14, parsedOnce.execute().asInt());
138110
}
139111
}
140112

141113
@Test
142114
public void testLocalVariablesNotSharedBetweenNonInteractiveEval() {
143-
try (Context polyglot = Context.newBuilder()
144-
.allowAllAccess(true)
145-
.build()) {
115+
try (Context polyglot = Context.create()) {
146116
polyglot.eval("ruby", "a = 14");
147117
assertTrue(polyglot.eval("ruby", "defined?(a).nil?").asBoolean());
148118
}
149119
}
150120

151121
@Test
152122
public void testLocalVariablesSharedBetweenInteractiveEval() {
153-
try (Context polyglot = Context.newBuilder()
154-
.allowAllAccess(true)
155-
.build()) {
123+
try (Context polyglot = Context.create()) {
156124
polyglot.eval(Source.newBuilder("ruby", "a = 14", "test").interactive(true).buildLiteral());
157125
assertFalse(polyglot.eval(Source.newBuilder("ruby", "defined?(a).nil?", "test").interactive(true).buildLiteral()).asBoolean());
158126
polyglot.eval(Source.newBuilder("ruby", "b = 2", "test").interactive(true).buildLiteral());
@@ -162,9 +130,7 @@ public void testLocalVariablesSharedBetweenInteractiveEval() {
162130

163131
@Test
164132
public void testLocalVariablesSharedBetweenInteractiveEvalChangesParsing() {
165-
try (Context polyglot = Context.newBuilder()
166-
.allowAllAccess(true)
167-
.build()) {
133+
try (Context polyglot = Context.create()) {
168134
polyglot.eval(Source.newBuilder("ruby", "def foo; 12; end", "test").interactive(true).buildLiteral());
169135
assertEquals(12, polyglot.eval(Source.newBuilder("ruby", "foo", "test").interactive(true).buildLiteral()).asInt());
170136
polyglot.eval(Source.newBuilder("ruby", "foo = 42", "test").interactive(true).buildLiteral());
@@ -174,9 +140,7 @@ public void testLocalVariablesSharedBetweenInteractiveEvalChangesParsing() {
174140

175141
@Test
176142
public void testLocalVariablesAreNotSharedBetweenInteractiveAndNonInteractive() {
177-
try (Context polyglot = Context.newBuilder()
178-
.allowAllAccess(true)
179-
.build()) {
143+
try (Context polyglot = Context.create()) {
180144
polyglot.eval(Source.newBuilder("ruby", "a = 14", "test").interactive(false).buildLiteral());
181145
polyglot.eval(Source.newBuilder("ruby", "b = 2", "test").interactive(true).buildLiteral());
182146
assertTrue(polyglot.eval(Source.newBuilder("ruby", "defined?(a).nil?", "test").interactive(true).buildLiteral()).asBoolean());

src/test/java/org/truffleruby/fixtures/FluidForce.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*/
1010
package org.truffleruby.fixtures;
1111

12+
import org.graalvm.polyglot.HostAccess;
13+
14+
@HostAccess.Implementable
1215
public interface FluidForce {
1316

1417
// Example copied from the JRuby wiki

0 commit comments

Comments
 (0)