Search in sources :

Example 6 with SymbolicValue

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

the class ProgramStateTest method test_adding_constraint_transitively.

@Test
public void test_adding_constraint_transitively() throws Exception {
    ProgramState ps = ProgramState.EMPTY_STATE;
    SymbolicValue sv1 = new SymbolicValue();
    SymbolicValue sv2 = new SymbolicValue();
    RelationalSymbolicValue relSV = new RelationalSymbolicValue(RelationalSymbolicValue.Kind.EQUAL);
    SymbolicValueTestUtil.computedFrom(relSV, sv1, sv2);
    ps = ps.addConstraint(relSV, BooleanConstraint.TRUE);
    UnclosedResourcesCheck.ResourceConstraint constraint = UnclosedResourcesCheck.ResourceConstraint.OPEN;
    ps = ps.addConstraintTransitively(sv1, constraint);
    assertThat(ps.getConstraint(sv2, constraint.getClass())).isEqualTo(constraint);
}
Also used : UnclosedResourcesCheck(org.sonar.java.se.checks.UnclosedResourcesCheck) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Test(org.junit.Test)

Example 7 with SymbolicValue

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

the class ProgramStateTest method test_peek_nth_value.

@Test
public void test_peek_nth_value() {
    ProgramState state = ProgramState.EMPTY_STATE;
    ProgramState finalState = state;
    assertThatThrownBy(() -> finalState.peekValue(0)).isInstanceOf(IllegalStateException.class);
    SymbolicValue sv1 = new SymbolicValue();
    state = state.stackValue(sv1);
    assertThat(state.peekValue(0)).isEqualTo(sv1);
    SymbolicValue sv2 = new SymbolicValue();
    state = state.stackValue(sv2);
    assertThat(state.peekValue(1)).isEqualTo(sv1);
}
Also used : RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Test(org.junit.Test)

Example 8 with SymbolicValue

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

the class ProgramStateTest method test_symbol_should_not_change_equals.

@Test
public void test_symbol_should_not_change_equals() throws Exception {
    ProgramState ps1 = ProgramState.EMPTY_STATE;
    ProgramState ps2 = ProgramState.EMPTY_STATE;
    SymbolicValue sv = new SymbolicValue();
    JavaSymbol.VariableJavaSymbol symbol = variable("a");
    ps1 = ps1.stackValue(sv);
    ps2 = ps2.stackValue(sv, symbol);
    assertThat(ps1).isEqualTo(ps2);
    assertThat(ImmutableSet.of(ps1, ps2)).hasSize(1);
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Test(org.junit.Test)

Example 9 with SymbolicValue

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

the class ProgramStateTest method testStackUnstack.

@Test
public void testStackUnstack() {
    SymbolicValue sv1 = new SymbolicValue();
    ProgramState state = ProgramState.EMPTY_STATE.stackValue(sv1);
    assertThat(state.peekValue()).isSameAs(sv1);
    SymbolicValue sv2 = new SymbolicValue();
    state = state.stackValue(sv2);
    List<SymbolicValue> values = state.peekValues(2);
    assertThat(values).hasSize(2).containsSequence(sv2, sv1);
    try {
        state.peekValues(3);
        Assert.fail("Able to retrieve more values than there are actually on the stack!");
    } catch (IllegalStateException e) {
    // Expected behavior
    }
    Pop unstack = state.unstackValue(1);
    state = unstack.state;
    values = unstack.values;
    assertThat(values).hasSize(1);
    assertThat(values.get(0)).isSameAs(sv2);
    assertThat(state.peekValue()).isSameAs(sv1);
}
Also used : Pop(org.sonar.java.se.ProgramState.Pop) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Test(org.junit.Test)

Example 10 with SymbolicValue

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

the class DivisionByZeroCheckTest method test_zero_constraint_copy.

@Test
public void test_zero_constraint_copy() throws Exception {
    SymbolicValue a = new SymbolicValue();
    SymbolicValue b = new SymbolicValue();
    DivisionByZeroCheck.ZeroConstraint bConstraint = copyConstraint(a, b, EQUAL, ZERO);
    assertThat(bConstraint).isEqualTo(ZERO);
    bConstraint = copyConstraint(a, b, NOT_EQUAL, NON_ZERO);
    assertThat(bConstraint).isEqualTo(NON_ZERO);
    bConstraint = copyConstraint(a, b, LESS_THAN, NON_ZERO);
    assertThat(bConstraint).isEqualTo(NON_ZERO);
    bConstraint = copyConstraint(a, b, GREATER_THAN_OR_EQUAL, null);
    assertThat(bConstraint).isNull();
}
Also used : RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Test(org.junit.Test)

Aggregations

SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)132 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)94 Test (org.junit.Test)79 ProgramState (org.sonar.java.se.ProgramState)74 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)55 Instruction (org.sonar.java.bytecode.cfg.Instruction)52 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)38 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)36 ProgramPoint (org.sonar.java.se.ProgramPoint)30 Constraint (org.sonar.java.se.constraint.Constraint)29 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)22 Type (org.sonar.plugins.java.api.semantic.Type)18 Symbol (org.sonar.plugins.java.api.semantic.Symbol)17 JavaSymbol (org.sonar.java.resolve.JavaSymbol)16 ConstraintsByDomain (org.sonar.java.se.constraint.ConstraintsByDomain)16 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)15 List (java.util.List)13 Collectors (java.util.stream.Collectors)11 VisibleForTesting (com.google.common.annotations.VisibleForTesting)10 Lists (com.google.common.collect.Lists)10