<commit_before>package org.jenkinsci.plugins.workflow.cps.steps

import hudson.Functions
import hudson.model.Result
import org.jenkinsci.plugins.workflow.actions.ErrorAction
import org.jenkinsci.plugins.workflow.cps.AbstractCpsFlowTest
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition
import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker
import org.junit.Test

/**
 *
 *
 * @author <NAME>
 */
class EvaluateStepTest extends AbstractCpsFlowTest {
    /**
     * Test the 'evaluate' method call.
     * The first test case.
     */
    @Test
    void evaluate() {
        def flow = new CpsFlowDefinition("""
assert evaluate('1+2+3')==6
""")

        createExecution(flow)
        exec.start()
        exec.waitForSuspension()

        assert exec.isComplete()
        assert exec.result==Result.SUCCESS;
    }

    /**
     * The code getting evaluated must also get sandbox transformed.
     */
    @Test
    void evaluateShallSandbox() {
        def flow = new CpsFlowDefinition("""
evaluate('Jenkins.getInstance()')
""",true)

        createExecution(flow)
        exec.start()
        exec.waitForSuspension()

        def msg = dumpError()

        // execution should have failed with error, pointing that Jenkins.getInstance() is not allowed from sandbox
        assert exec.isComplete()
        assert exec.result==Result.FAILURE;
        assert msg.contains("org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance") : msg
    }

    private String dumpError() {
        def msg = ""

        def walker = new FlowGraphWalker(exec);
        def n;
        while ((n = walker.next()) != null) {
            def e = n.getAction(ErrorAction.class)
            if (e != null)
                msg += Functions.printThrowable(e.error)
        }
        return msg;
    }
}
<commit_msg>Verifies that evaluated code is still CPS transformed<commit_after>package org.jenkinsci.plugins.workflow.cps.steps

import com.cloudbees.groovy.cps.Outcome
import hudson.Functions
import hudson.model.Result
import org.jenkinsci.plugins.workflow.actions.ErrorAction
import org.jenkinsci.plugins.workflow.cps.AbstractCpsFlowTest
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition
import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker
import org.junit.Test

/**
 *
 *
 * @author <NAME>
 */
class EvaluateStepTest extends AbstractCpsFlowTest {
    /**
     * Test the 'evaluate' method call.
     * The first test case.
     */
    @Test
    void evaluate() {
        def flow = new CpsFlowDefinition("""
assert evaluate('1+2+3')==6
""")

        createExecution(flow)
        exec.start()
        exec.waitForSuspension()

        assert exec.isComplete()
        assert exec.result==Result.SUCCESS : dumpError();
    }

    /**
     * The code getting evaluated must also get sandbox transformed.
     */
    @Test
    void evaluateShallSandbox() {
        def flow = new CpsFlowDefinition("""
evaluate('Jenkins.getInstance()')
""",true)

        createExecution(flow)
        exec.start()
        exec.waitForSuspension()

        def msg = dumpError()

        // execution should have failed with error, pointing that Jenkins.getInstance() is not allowed from sandbox
        assert exec.isComplete()
        assert exec.result==Result.FAILURE;
        assert msg.contains("org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance") : msg
    }

    /**
     * The code getting evaluated must also get CPS transformation.
     */
    @Test
    void evaluateShallBeCpsTransformed() {
        def flow = new CpsFlowDefinition("""
evaluate('1+com.cloudbees.groovy.cps.Continuable.suspend(2+3)')
""")

        createExecution(flow)
        exec.start()
        exec.waitForSuspension()
        // TODO: can't we assert that the suspend() ended with value 5?

        // this should have paused at suspend, so we are going to resume it by having it return a value we control
        assert !exec.isComplete() : dumpError()
        assert exec.programPromise.get().getThread(0).resume(new Outcome(7,null)).get()==8
        exec.waitForSuspension()

        assert exec.isComplete() : dumpError();
        assert exec.result==Result.SUCCESS : dumpError();
    }

    /**
     * Picks up any errors recorded in {@link #exec}.
     */
    private String dumpError() {
        def msg = ""

        def walker = new FlowGraphWalker(exec);
        def n;
        while ((n = walker.next()) != null) {
            def e = n.getAction(ErrorAction.class)
            if (e != null)
                msg += Functions.printThrowable(e.error)
        }
        return msg;
    }
}

{'fraction_non_alphanumeric': 0.10894206549118388, 'fraction_numerical': 0.0031486146095717885, 'mean_word_length': 2.6909372579395816, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3960535', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1352, 'n_words': 333}