Search in sources :

Example 11 with StringAppender

use of org.mvel2.util.StringAppender in project mvel by mikebrock.

the class AbstractTest method _test.

protected static Object _test(String ex) {
    ExpressionCompiler compiler = new ExpressionCompiler(ex);
    StringAppender failErrors = new StringAppender();
    CompiledExpression compiled = compiler.compile();
    Object first = null, second = null, third = null, fourth = null, fifth = null, sixth = null, seventh = null, eighth = null;
    System.out.println(DebugTools.decompile((Serializable) compiled));
    if (!Boolean.getBoolean("mvel2.disable.jit")) {
        setDefaultOptimizer("ASM");
        try {
            first = executeExpression(compiled, new Base(), createTestMap());
        } catch (Exception e) {
            failErrors.append("\nFIRST TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
            CharArrayWriter writer = new CharArrayWriter();
            e.printStackTrace(new PrintWriter(writer));
            failErrors.append(writer.toCharArray());
        }
        try {
            second = executeExpression(compiled, new Base(), createTestMap());
        } catch (Exception e) {
            failErrors.append("\nSECOND TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
            CharArrayWriter writer = new CharArrayWriter();
            e.printStackTrace(new PrintWriter(writer));
            failErrors.append(writer.toCharArray());
        }
    }
    try {
        third = MVEL.eval(ex, new Base(), createTestMap());
    } catch (Exception e) {
        failErrors.append("\nTHIRD TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    if (first != null && !first.getClass().isArray()) {
        if (!first.equals(second)) {
            System.out.println(failErrors.toString());
            throw new AssertionError("Different result from test 1 and 2 (Compiled Re-Run / JIT) [first: " + valueOf(first) + "; second: " + valueOf(second) + "]");
        }
        if (!first.equals(third)) {
            if (failErrors != null)
                System.out.println(failErrors.toString());
            throw new AssertionError("Different result from test 1 and 3 (Compiled to Interpreted) [first: " + valueOf(first) + " (" + (first != null ? first.getClass().getName() : null) + "); third: " + valueOf(third) + " (" + (third != null ? third.getClass().getName() : "null") + ")]");
        }
    }
    setDefaultOptimizer("reflective");
    Serializable compiled2 = compileExpression(ex);
    try {
        fourth = executeExpression(compiled2, new Base(), createTestMap());
    } catch (Exception e) {
        if (failErrors == null)
            failErrors = new StringAppender();
        failErrors.append("\nFOURTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    try {
        fifth = executeExpression(compiled2, new Base(), createTestMap());
    } catch (Exception e) {
        e.printStackTrace();
        if (failErrors == null)
            failErrors = new StringAppender();
        failErrors.append("\nFIFTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    if (fourth != null && !fourth.getClass().isArray()) {
        if (!fourth.equals(fifth)) {
            throw new AssertionError("Different result from test 4 and 5 (Compiled Re-Run X2) [fourth: " + valueOf(fourth) + "; fifth: " + valueOf(fifth) + "]");
        }
    }
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("unittest");
    ctx.setDebugSymbols(true);
    ExpressionCompiler debuggingCompiler = new ExpressionCompiler(ex);
    //     debuggingCompiler.setDebugSymbols(true);
    CompiledExpression compiledD = debuggingCompiler.compile(ctx);
    try {
        sixth = executeExpression(compiledD, new Base(), createTestMap());
    } catch (Exception e) {
        if (failErrors == null)
            failErrors = new StringAppender();
        failErrors.append("\nSIXTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    if (sixth != null && !sixth.getClass().isArray()) {
        if (!fifth.equals(sixth)) {
            System.out.println("Payload 1 -- No Symbols: ");
            System.out.println(decompile(compiled));
            System.out.println();
            System.out.println("Payload 2 -- With Symbols: ");
            System.out.println(decompile(compiledD));
            System.out.println();
            throw new AssertionError("Different result from test 5 and 6 (Compiled to Compiled+DebuggingSymbols) [first: " + valueOf(fifth) + "; second: " + valueOf(sixth) + "]");
        }
    }
    try {
        seventh = executeExpression(compiledD, new Base(), createTestMap());
    } catch (Exception e) {
        if (failErrors == null)
            failErrors = new StringAppender();
        failErrors.append("\nSEVENTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    if (seventh != null && !seventh.getClass().isArray()) {
        if (!seventh.equals(sixth)) {
            throw new AssertionError("Different result from test 4 and 5 (Compiled Re-Run / Reflective) [first: " + valueOf(first) + "; second: " + valueOf(second) + "]");
        }
    }
    try {
        Serializable xx = serializationTest(compiledD);
        AbstractParser.resetParserContext();
        eighth = executeExpression(xx, new Base(), new MapVariableResolverFactory(createTestMap()));
    } catch (Exception e) {
        if (failErrors == null)
            failErrors = new StringAppender();
        failErrors.append("\nEIGHTH TEST (Serializability): { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    if (eighth != null && !eighth.getClass().isArray()) {
        if (!eighth.equals(seventh)) {
            throw new AssertionError("Different result from test 7 and 8 (Compiled Re-Run / Reflective) [first: " + valueOf(first) + "; second: " + valueOf(second) + "]");
        }
    }
    if (failErrors.length() > 0) {
        System.out.println(decompile(compiledD));
        throw new AssertionError("Detailed Failure Report:\n" + failErrors.toString());
    }
    return fourth;
}
Also used : StringAppender(org.mvel2.util.StringAppender) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression) Base(org.mvel2.tests.core.res.Base)

Example 12 with StringAppender

use of org.mvel2.util.StringAppender in project mvel by mikebrock.

the class Fuzzer method main.

public static void main(String[] args) throws IOException {
    DecimalFormat df = new DecimalFormat("###,###.##");
    StringAppender append = new StringAppender();
    int len;
    long start = currentTimeMillis();
    long time;
    double rate;
    int seed;
    boolean flip = false;
    Random rand = new Random(System.currentTimeMillis());
    Random rand1 = new Random(System.currentTimeMillis() + 1);
    Random rand2 = new Random(rand1.nextInt());
    Random rand3 = new Random(rand.nextInt(SALTS.length - 1));
    Random rand4 = new Random(rand3.nextInt());
    for (int run = 0; run < MAX; run++) {
        len = (int) (random() * 500) + 10;
        append.reset();
        for (int i = 0; i < len; i++) {
            append.append(CHAR_TABLE[((SALTS[((rand.nextInt(1000)) + 1) % SALTS.length]) * ((flip = !flip) ? rand1.nextInt(1000) : rand2.nextInt(1000)) + 1) % CHAR_TABLE.length]);
            SALTS[rand3.nextInt(SALTS.length - 1)] ^= rand4.nextInt(1000) + 1;
        }
        try {
            MVEL.eval(append.toString());
        } catch (UnresolveablePropertyException e) {
        //ignore
        } catch (CompileException e) {
        //ignore
        } catch (ArithmeticException e) {
        //ignore
        } catch (ScriptRuntimeException e) {
        //ignore
        } catch (Exception e) {
            System.out.println("untrapped error!\n---\n" + append.toString() + "\n---\n");
            System.out.flush();
            e.printStackTrace();
            System.err.flush();
        }
        if (run % 25000 == 0 && run != 0) {
            rate = run / (time = (currentTimeMillis() - start) / 1000);
            System.out.println("Run: " + df.format(run) + " times; " + df.format(time) + "secs; " + df.format(rate) + " avg. per second.");
        }
    }
}
Also used : Random(java.util.Random) ScriptRuntimeException(org.mvel2.ScriptRuntimeException) DecimalFormat(java.text.DecimalFormat) StringAppender(org.mvel2.util.StringAppender) CompileException(org.mvel2.CompileException) UnresolveablePropertyException(org.mvel2.UnresolveablePropertyException) UnresolveablePropertyException(org.mvel2.UnresolveablePropertyException) ScriptRuntimeException(org.mvel2.ScriptRuntimeException) IOException(java.io.IOException) CompileException(org.mvel2.CompileException)

Aggregations

StringAppender (org.mvel2.util.StringAppender)8 MethodVisitor (org.mvel2.asm.MethodVisitor)3 Function (org.mvel2.ast.Function)3 ClassWriter (org.mvel2.asm.ClassWriter)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 DecimalFormat (java.text.DecimalFormat)1 LinkedList (java.util.LinkedList)1 Random (java.util.Random)1 CompileException (org.mvel2.CompileException)1 ParserContext (org.mvel2.ParserContext)1 ScriptRuntimeException (org.mvel2.ScriptRuntimeException)1 UnresolveablePropertyException (org.mvel2.UnresolveablePropertyException)1 Proto (org.mvel2.ast.Proto)1 CompiledExpression (org.mvel2.compiler.CompiledExpression)1 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)1 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)1 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)1 Base (org.mvel2.tests.core.res.Base)1 MethodStub (org.mvel2.util.MethodStub)1