Search in sources :

Example 6 with Constraint

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

the class BytecodeEGWalkerExecuteTest method assertStack.

private void assertStack(ProgramState ps, Constraint[]... constraints) {
    ProgramState.Pop pop = ps.unstackValue(constraints.length);
    assertEmptyStack(pop.state);
    assertThat(pop.valuesAndSymbols).hasSize(constraints.length);
    List<SymbolicValue> symbolicValues = pop.values;
    int idx = 0;
    for (SymbolicValue sv : symbolicValues) {
        ConstraintsByDomain constraintsByDomain = ps.getConstraints(sv);
        if (constraintsByDomain != null) {
            constraintsByDomain = constraintsByDomain.remove(BytecodeEGWalker.StackValueCategoryConstraint.class);
        }
        for (Constraint expectedConstraint : constraints[idx]) {
            if (expectedConstraint != null) {
                Class<? extends Constraint> expectedConstraintDomain = expectedConstraint.getClass();
                Constraint constraint = constraintsByDomain.get(expectedConstraintDomain);
                assertThat(constraint).isEqualTo(expectedConstraint);
                constraintsByDomain = constraintsByDomain.remove(expectedConstraintDomain);
            } else {
                assertThat(constraintsByDomain).isNull();
            }
        }
        if (constraintsByDomain != null) {
            assertThat(constraintsByDomain.isEmpty()).isTrue();
        }
        idx++;
    }
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) Constraint(org.sonar.java.se.constraint.Constraint) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) ProgramState(org.sonar.java.se.ProgramState) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) Constraint(org.sonar.java.se.constraint.Constraint) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) ProgramPoint(org.sonar.java.se.ProgramPoint)

Example 7 with Constraint

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

the class BytecodeEGWalkerExecuteTest method assertStack.

private void assertStack(ProgramState ps, Constraint... constraints) {
    Constraint[][] cs = new Constraint[constraints.length][1];
    int i = 0;
    for (Constraint constraint : constraints) {
        cs[i] = new Constraint[] { constraint };
        i++;
    }
    assertStack(ps, cs);
}
Also used : BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) Constraint(org.sonar.java.se.constraint.Constraint) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) Constraint(org.sonar.java.se.constraint.Constraint) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) ProgramPoint(org.sonar.java.se.ProgramPoint)

Example 8 with Constraint

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

the class MethodBehaviorTest method result_with_unreducible_constraint_should_not_be_reduced.

@Test
public void result_with_unreducible_constraint_should_not_be_reduced() {
    MethodBehavior mb = new MethodBehavior("foo()Ljava/lang/Object;");
    addYield(mb, ObjectConstraint.NOT_NULL);
    addYield(mb, ObjectConstraint.NULL);
    mb.completed();
    assertThat(mb.yields()).hasSize(2);
    List<Constraint> resultConstraints = mb.yields().stream().map(y -> ((HappyPathYield) y).resultConstraint().get(ObjectConstraint.class)).collect(Collectors.toList());
    assertThat(resultConstraints).contains(ObjectConstraint.NULL, ObjectConstraint.NOT_NULL);
}
Also used : ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Collectors(java.util.stream.Collectors) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) List(java.util.List) Pair(org.sonar.java.se.Pair) SemanticModel(org.sonar.java.resolve.SemanticModel) SymbolicExecutionVisitor(org.sonar.java.se.SymbolicExecutionVisitor) SETestUtils.createSymbolicExecutionVisitorAndSemantic(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitorAndSemantic) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) Constraint(org.sonar.java.se.constraint.Constraint) Nullable(javax.annotation.Nullable) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) Constraint(org.sonar.java.se.constraint.Constraint) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) Test(org.junit.Test)

Example 9 with Constraint

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

the class MethodBehaviorTest method addYield.

private void addYield(MethodBehavior mb, @Nullable Constraint result, Constraint... constraints) {
    HappyPathYield yield = new HappyPathYield(mb);
    for (Constraint constraint : constraints) {
        yield.parametersConstraints.add(ConstraintsByDomain.empty().put(constraint));
    }
    if (result != null) {
        yield.setResult(-1, ConstraintsByDomain.empty().put(result));
    }
    mb.yields.add(yield);
}
Also used : ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) Constraint(org.sonar.java.se.constraint.Constraint)

Example 10 with Constraint

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

the class MethodBehaviorTest method method_behavior_yields.

@Test
public void method_behavior_yields() {
    SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/resources/se/MethodYields.java");
    MethodBehavior mb = getMethodBehavior(sev, "method");
    List<MethodYield> yields = mb.yields();
    assertThat(yields).hasSize(3);
    List<HappyPathYield> trueResults = mb.happyPathYields().filter(my -> BooleanConstraint.TRUE.equals(my.resultConstraint().get(BooleanConstraint.class))).collect(Collectors.toList());
    assertThat(trueResults).hasSize(1);
    HappyPathYield trueResult = trueResults.get(0);
    // 'a' has constraint "null"
    assertThat(((ObjectConstraint) trueResult.parametersConstraints.get(0).get(ObjectConstraint.class)).isNull()).isTrue();
    // no constraint on 'b'
    assertThat(((ObjectConstraint) trueResult.parametersConstraints.get(0).get(ObjectConstraint.class)).isNull()).isTrue();
    // result SV is a different SV than 'a' and 'b'
    assertThat(trueResult.resultIndex()).isEqualTo(-1);
    List<HappyPathYield> falseResults = mb.happyPathYields().filter(my -> BooleanConstraint.FALSE.equals(my.resultConstraint().get(BooleanConstraint.class))).collect(Collectors.toList());
    assertThat(falseResults).hasSize(2);
    // for both "False" results, 'a' has the constraint "not null"
    assertThat(falseResults.stream().filter(my -> !((ObjectConstraint) my.parametersConstraints.get(0).get(ObjectConstraint.class)).isNull()).count()).isEqualTo(2);
    // 1) 'b' has constraint "false", result is 'b'
    assertThat(falseResults.stream().filter(my -> BooleanConstraint.FALSE.equals(my.parametersConstraints.get(1).get(BooleanConstraint.class)) && my.resultIndex() == 1).count()).isEqualTo(1);
    // 2) 'b' is "true", result is a different SV than 'a' and 'b'
    assertThat(falseResults.stream().filter(my -> BooleanConstraint.TRUE.equals(my.parametersConstraints.get(1).get(BooleanConstraint.class)) && my.resultIndex() == -1).count()).isEqualTo(1);
}
Also used : ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Collectors(java.util.stream.Collectors) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) List(java.util.List) Pair(org.sonar.java.se.Pair) SemanticModel(org.sonar.java.resolve.SemanticModel) SymbolicExecutionVisitor(org.sonar.java.se.SymbolicExecutionVisitor) SETestUtils.createSymbolicExecutionVisitorAndSemantic(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitorAndSemantic) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) Constraint(org.sonar.java.se.constraint.Constraint) Nullable(javax.annotation.Nullable) 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) Test(org.junit.Test)

Aggregations

Constraint (org.sonar.java.se.constraint.Constraint)20 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)19 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)18 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)12 ProgramState (org.sonar.java.se.ProgramState)11 Test (org.junit.Test)10 ConstraintsByDomain (org.sonar.java.se.constraint.ConstraintsByDomain)9 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)9 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 ProgramPoint (org.sonar.java.se.ProgramPoint)8 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)8 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)8 SemanticModel (org.sonar.java.resolve.SemanticModel)6 Instruction (org.sonar.java.bytecode.cfg.Instruction)5 Lists (com.google.common.collect.Lists)4 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 Nullable (javax.annotation.Nullable)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4