Skip to content
Permalink
Browse files

Merge pull request #23 from jglick/olivergondza-excercise-operators

[FIXED JENKINS-28154] Confirm fix of problem with operators
  • Loading branch information...
jglick committed Aug 21, 2015
2 parents 7f3e2a7 + 1f2f0b0 commit b8b421f6836e72bfaf865971e9fa2734700f2574
@@ -43,8 +43,12 @@ staticMethod org.codehaus.groovy.runtime.ScriptBytecodeAdapter compareNotEqual j
staticMethod org.codehaus.groovy.runtime.ScriptBytecodeAdapter compareLessThan 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
staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods asBoolean java.lang.Object
staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods isCase java.util.Collection java.lang.Object
@@ -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;
@@ -36,6 +39,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;
@@ -58,7 +62,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;
@@ -224,7 +227,7 @@ public void setProp2(String prop2) {
this._prop2 = prop2;
}
}

@Test public void dynamicProperties() throws Exception {
String dynamic = Dynamic.class.getName();
String ctor = "new " + dynamic;
@@ -379,7 +382,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");
}
@@ -477,6 +480,11 @@ public Object invokeMethod(String name, Object args) {
);
}

@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);
@@ -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 {
}

0 comments on commit b8b421f

Please sign in to comment.
You can’t perform that action at this time.