Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GraalJS to verify the JS scripts #434

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ dependencies {

// The following versions should match the ones of the add-ons.
testImplementation("org.codehaus.groovy:groovy-all:3.0.14")
val graalJsVersion = "22.3.3"
testImplementation("org.graalvm.js:js:$graalJsVersion")
testImplementation("org.graalvm.js:js-scriptengine:$graalJsVersion")
testImplementation("org.jruby:jruby-complete:1.7.4")
testImplementation("org.zaproxy:zest:0.18.0")
testImplementation("org.python:jython-standalone:2.7.2")
Expand Down
32 changes: 20 additions & 12 deletions src/test/java/org/zaproxy/VerifyScripts.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
Expand All @@ -35,23 +36,22 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Stream;
import javax.script.Compilable;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.lang3.mutable.MutableInt;
import org.codehaus.groovy.jsr223.GroovyScriptEngineFactory;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Engine;
import org.jruby.embed.jsr223.JRubyEngineFactory;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -102,15 +102,23 @@ private static Stream<Arguments> scriptsGroovy() {
}

private static Stream<Arguments> scriptsJavaScript() {
if (!EnumSet.range(JRE.JAVA_8, JRE.JAVA_14).contains(JRE.currentVersion())) {
// Nashorn is not bundled in Java 15+
getFilesWithExtension(".js");
return Stream.empty();
}

Compilable engine = (Compilable) new ScriptEngineManager().getEngineByName("ECMAScript");
assertThat(engine).as("ECMAScript script engine exists.").isNotNull();
return testData(".js", engine);
Engine engine =
Engine.newBuilder()
.allowExperimentalOptions(true)
.option("engine.WarnInterpreterOnly", "false")
.build();

Context.Builder contextBuilder =
Context.newBuilder("js")
.allowExperimentalOptions(true)
.option("js.syntax-extensions", "true")
.option("js.load", "true")
.option("js.print", "true")
.option("js.nashorn-compat", "true")
.allowAllAccess(true)
.hostClassLoader(VerifyScripts.class.getClassLoader());

return testData(".js", GraalJSScriptEngine.create(engine, contextBuilder));
}

private static Stream<Arguments> scriptsPython() {
Expand Down
Loading