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

[JENKINS-28154] Reproduce in unittest #17

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ staticMethod org.codehaus.groovy.runtime.ScriptBytecodeAdapter compareEqual java
staticMethod org.codehaus.groovy.runtime.ScriptBytecodeAdapter compareNotEqual java.lang.Object java.lang.Object
staticMethod org.codehaus.groovy.runtime.ScriptBytecodeAdapter findRegex java.lang.Object java.lang.Object
staticMethod org.codehaus.groovy.runtime.ScriptBytecodeAdapter matchRegex java.lang.Object java.lang.Object
staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods collect java.util.Collection groovy.lang.Closure
staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods each java.lang.Object groovy.lang.Closure
staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods disjoint java.util.Collection java.util.Collection
staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods join java.util.Collection java.lang.String
staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods leftShift java.util.Collection java.lang.Object
staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods or java.lang.Boolean java.lang.Boolean
staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods xor java.lang.Boolean java.lang.Boolean
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

package org.jenkinsci.plugins.scriptsecurity.sandbox.groovy;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import groovy.json.JsonBuilder;
import groovy.json.JsonDelegate;
import groovy.lang.GString;
Expand All @@ -35,6 +38,7 @@
import groovy.text.SimpleTemplateEngine;
import groovy.text.Template;
import hudson.Functions;
import hudson.util.IOUtils;

import java.lang.reflect.Method;
import java.net.URL;
Expand All @@ -56,7 +60,6 @@
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.ProxyWhitelist;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;
import static org.junit.Assert.*;
import org.junit.Ignore;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
Expand Down Expand Up @@ -216,7 +219,7 @@ public void setProp2(String prop2) {
this._prop2 = prop2;
}
}

@Test public void dynamicProperties() throws Exception {
String dynamic = Dynamic.class.getName();
String ctor = "new " + dynamic;
Expand Down Expand Up @@ -367,7 +370,7 @@ private Unsafe() {}
}
}, new ProxyWhitelist(new StaticWhitelist("method java.lang.String toLowerCase"), new GenericWhitelist())));
}

@Test public void selfProperties() throws Exception {
assertEvaluate(new ProxyWhitelist(), true, "BOOL=true; BOOL");
}
Expand Down Expand Up @@ -437,6 +440,11 @@ public static final class Ambiguity {
assertEvaluate(new GenericWhitelist(), Collections.singletonMap("part0", "one\ntwo"), "def list = [['one', 'two']]; def map = [:]; for (int i = 0; i < list.size(); i++) {map[\"part${i}\"] = list.get(i).join(\"\\n\")}; map");
}

@Test public void keywordsAndOperators() throws Exception {
String script = IOUtils.toString(this.getClass().getResourceAsStream("SandboxInterceptorTest/all.groovy"));
assertEvaluate(new GenericWhitelist(), null, script);
}

private static void assertEvaluate(Whitelist whitelist, final Object expected, final String script) {
final GroovyShell shell = new GroovyShell(GroovySandbox.createSecureCompilerConfiguration());
Object actual = GroovySandbox.run(shell.parse(script), whitelist);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// A script that should exercise all keywords and operators.
// TODO:
// ===
// !==
// ||=
// &&=
// \
// \=
// native
// KEYWORD_DEFMACRO
// mixin
// KEYWORD_IMPORT
// KEYWORD_DO

package test

def fun() {
return [];
}

arr = fun();
assert arr.collect { it -> it } == [];
arr << 0;
assert arr == [0];

assert true in [true, false];
(1..3).each {};
[1..3]*.toString();
assert 42 as String == "42"
assert 0 == [1:0][1];

assert "asdf" =~ /sd/
assert "asdf" ==~ /asdf/
assert ~/asdf/ instanceof java.util.regex.Pattern

assert 1 < 2 && 1 <= 2
assert 2 > 1 && 2 >= 1
assert 1 <=> 1 == 0
assert false || !false

assert (6 / 3 + 4) * (2 ** 3 - (3 % 2)) == 42

int a = 1
a += 1
a *= 2
a **= 2
a /= 2
a %= 5
assert a == 3

l = 1;
r = 3;
assert ++l-- == --r++;
assert +0 == -0

assert (2 << 1) + (8 >> 1) == (16 >>> 1);

def b = 8;
b >>= 1;
b >>>= 1;
b <<= 2;
assert b == 8;

assert !(false ? true : false)
assert false?:true;
assert null?.hashCode() == null

assert (1 | 2) == (1 & 3) + (1 ^ 3);

int bin = 15;
bin ^= 5;
bin |= 3;
bin &= 6;
assert bin == 2;

interface I {}

abstract strictfp class A implements I {
final transient int v = 42;
volatile double a;
long l;
float f;
char ch;
{}
static {}
protected static synchronized byte s() throws IOException {}
public abstract void f();
private short p() {};
def d(String... s) { return 0 };
}

class E extends A {
public void f() {
def superP = super.&p;
superP();
def arr = ["Lorem", "ipsum"];
this.d(*arr);
}
private int field;
}

assert new E() {} instanceof I;
new E().@field = 42;

if (false) {
assert false;
} else {
assert true;
}
label: for (i in [1, 2]) { continue label; }

switch (false) {
case null: break;
case true: break;
default: break;
}

try {
throw new Exception();
} catch (Exception _) {
} finally {
}