Search in sources :

Example 11 with SymbolicExecutionVisitor

use of org.sonar.java.se.SymbolicExecutionVisitor in project sonar-java by SonarSource.

the class MethodYieldTest method test_creation_of_flows.

@Test
public void test_creation_of_flows() throws Exception {
    SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/XProcYieldsFlows.java");
    MethodBehavior mb = getMethodBehavior(sev, "foo");
    MethodYield methodYield = mb.happyPathYields().filter(y -> y.resultConstraint() != null && y.resultConstraint().get(ObjectConstraint.class) != ObjectConstraint.NULL).findFirst().get();
    Set<Flow> flowReturnValue = methodYield.flow(ImmutableList.of(-1), Lists.newArrayList(ObjectConstraint.class));
    assertThat(flowReturnValue.iterator().next().isEmpty()).isFalse();
    Set<Flow> flowFirstParam = methodYield.flow(ImmutableList.of(0), Lists.newArrayList(ObjectConstraint.class, BooleanConstraint.class));
    assertThat(flowFirstParam.iterator().next().isEmpty()).isFalse();
}
Also used : SETestUtils.mockMethodBehavior(org.sonar.java.se.SETestUtils.mockMethodBehavior) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) SymbolicExecutionVisitor(org.sonar.java.se.SymbolicExecutionVisitor) Flow(org.sonar.java.se.Flow) Test(org.junit.Test)

Example 12 with SymbolicExecutionVisitor

use of org.sonar.java.se.SymbolicExecutionVisitor in project sonar-java by SonarSource.

the class MethodYieldTest method test_creation_of_states.

@Test
public void test_creation_of_states() throws Exception {
    SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/XProcYields.java");
    MethodBehavior mb = getMethodBehavior(sev, "foo");
    ProgramState ps = ProgramState.EMPTY_STATE;
    SymbolicValue sv1 = new SymbolicValue();
    SymbolicValue sv2 = new SymbolicValue();
    SymbolicValue sv3 = new SymbolicValue();
    Symbol sym = new JavaSymbol.VariableJavaSymbol(0, "myVar", new JavaSymbol.MethodJavaSymbol(0, "dummy", null));
    ps = ps.put(sym, sv1);
    MethodYield methodYield = mb.happyPathYields().findFirst().get();
    Stream<ProgramState> generatedStatesFromFirstYield = methodYield.statesAfterInvocation(Lists.newArrayList(sv1, sv2), Lists.newArrayList(), ps, () -> sv3);
    assertThat(generatedStatesFromFirstYield).hasSize(1);
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) JavaSymbol(org.sonar.java.resolve.JavaSymbol) MethodSymbol(org.sonar.plugins.java.api.semantic.Symbol.MethodSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) SETestUtils.mockMethodBehavior(org.sonar.java.se.SETestUtils.mockMethodBehavior) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) ProgramState(org.sonar.java.se.ProgramState) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) SymbolicExecutionVisitor(org.sonar.java.se.SymbolicExecutionVisitor) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Test(org.junit.Test)

Example 13 with SymbolicExecutionVisitor

use of org.sonar.java.se.SymbolicExecutionVisitor in project sonar-java by SonarSource.

the class DebugCheckTestUtils method verifyIssuesWithMaxSteps.

static void verifyIssuesWithMaxSteps(String sourcefile, SECheck check, int maxSteps) {
    BehaviorCache behaviorCache = new BehaviorCache(new SquidClassLoader(new ArrayList<>()));
    SymbolicExecutionVisitor sev = new SymbolicExecutionVisitor(Collections.singletonList(check), behaviorCache) {

        @Override
        protected ExplodedGraphWalker getWalker() {
            return new ExplodedGraphWalker(Collections.singletonList(check), behaviorCache, (SemanticModel) context.getSemanticModel()) {

                @Override
                protected int maxSteps() {
                    return maxSteps;
                }
            };
        }

        @Override
        public void scanFile(JavaFileScannerContext context) {
            super.scanFile(context);
            // the check has been executed, but we still need to call the scan manually to report the issues
            check.scanFile(context);
        }
    };
    JavaCheckVerifier.verify(sourcefile, sev);
}
Also used : ExplodedGraphWalker(org.sonar.java.se.ExplodedGraphWalker) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ArrayList(java.util.ArrayList) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) SymbolicExecutionVisitor(org.sonar.java.se.SymbolicExecutionVisitor) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 14 with SymbolicExecutionVisitor

use of org.sonar.java.se.SymbolicExecutionVisitor in project sonar-java by SonarSource.

the class ExceptionalYieldTest method exceptional_yields_void_method.

@Test
public void exceptional_yields_void_method() {
    Pair<SymbolicExecutionVisitor, SemanticModel> sevAndSemantic = createSymbolicExecutionVisitorAndSemantic("src/test/files/se/ExceptionalYieldsVoidMethod.java");
    SymbolicExecutionVisitor sev = sevAndSemantic.a;
    SemanticModel semanticModel = sevAndSemantic.b;
    MethodBehavior mb = getMethodBehavior(sev, "myVoidMethod");
    assertThat(mb.yields()).hasSize(4);
    List<ExceptionalYield> exceptionalYields = mb.exceptionalPathYields().collect(Collectors.toList());
    assertThat(exceptionalYields).hasSize(3);
    assertThat(exceptionalYields.stream().filter(y -> y.exceptionType(semanticModel).isUnknown()).count()).isEqualTo(1);
    MethodYield explicitExceptionYield = exceptionalYields.stream().filter(y -> y.exceptionType(semanticModel).is("org.foo.MyException1")).findAny().get();
    assertThat(explicitExceptionYield.parametersConstraints.get(0).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NULL);
    MethodYield implicitExceptionYield = exceptionalYields.stream().filter(y -> y.exceptionType(semanticModel).is("org.foo.MyException2")).findAny().get();
    assertThat(implicitExceptionYield.parametersConstraints.get(0).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
}
Also used : JavaParser(org.sonar.java.ast.parser.JavaParser) SETestUtils.mockMethodBehavior(org.sonar.java.se.SETestUtils.mockMethodBehavior) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SETestUtils(org.sonar.java.se.SETestUtils) Set(java.util.Set) Test(org.junit.Test) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) List(java.util.List) Pair(org.sonar.java.se.Pair) SemanticModel(org.sonar.java.resolve.SemanticModel) Optional(java.util.Optional) SymbolicExecutionVisitor(org.sonar.java.se.SymbolicExecutionVisitor) SETestUtils.createSymbolicExecutionVisitorAndSemantic(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitorAndSemantic) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) SemanticModel(org.sonar.java.resolve.SemanticModel) SETestUtils.mockMethodBehavior(org.sonar.java.se.SETestUtils.mockMethodBehavior) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) SymbolicExecutionVisitor(org.sonar.java.se.SymbolicExecutionVisitor) Test(org.junit.Test)

Example 15 with SymbolicExecutionVisitor

use of org.sonar.java.se.SymbolicExecutionVisitor in project sonar-java by SonarSource.

the class MethodBehaviorTest method method_behavior_signature.

@Test
public void method_behavior_signature() {
    SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/resources/se/MethodYields.java");
    MethodBehavior mb = getMethodBehavior(sev, "method");
    assertThat(mb.signature()).isEqualTo("MethodYields#method(Ljava/lang/Object;Z)Z");
    assertThat(mb.signature()).isNotNull();
}
Also used : SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) SymbolicExecutionVisitor(org.sonar.java.se.SymbolicExecutionVisitor) Test(org.junit.Test)

Aggregations

SymbolicExecutionVisitor (org.sonar.java.se.SymbolicExecutionVisitor)15 Test (org.junit.Test)13 SETestUtils.createSymbolicExecutionVisitor (org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor)13 SETestUtils.getMethodBehavior (org.sonar.java.se.SETestUtils.getMethodBehavior)11 SETestUtils.mockMethodBehavior (org.sonar.java.se.SETestUtils.mockMethodBehavior)8 SemanticModel (org.sonar.java.resolve.SemanticModel)6 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)6 List (java.util.List)5 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)5 ArrayList (java.util.ArrayList)4 Collectors (java.util.stream.Collectors)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)4 Pair (org.sonar.java.se.Pair)4 SETestUtils.createSymbolicExecutionVisitorAndSemantic (org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitorAndSemantic)4 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)4 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)3 Optional (java.util.Optional)2 Set (java.util.Set)2 Nullable (javax.annotation.Nullable)2