use of org.sonar.java.se.constraint.ConstraintsByDomain 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++;
}
}
use of org.sonar.java.se.constraint.ConstraintsByDomain in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method method_returning_new_should_have_not_null_result.
@Test
public void method_returning_new_should_have_not_null_result() {
MethodBehavior mb = walker.getMethodBehavior(BytecodeEGWalkerExecuteTest.class.getCanonicalName() + "#newObject()Ljava/lang/Object;", squidClassLoader);
List<MethodYield> yields = mb.yields();
assertThat(yields).hasSize(1);
MethodYield yield = yields.get(0);
assertThat(yield).isInstanceOf(HappyPathYield.class);
ConstraintsByDomain resultConstraint = ((HappyPathYield) yield).resultConstraint();
assertThat(resultConstraint).isNotNull();
assertThat(resultConstraint.get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
TypedConstraint typeConstraint = (TypedConstraint) resultConstraint.get(TypedConstraint.class);
assertThat(typeConstraint.type.equals("java.lang.String")).isTrue();
}
use of org.sonar.java.se.constraint.ConstraintsByDomain in project sonar-java by SonarSource.
the class BytecodeSECheckTest method zeroness_check_mul.
@Test
public void zeroness_check_mul() {
int[] opCodes = { Opcodes.DMUL, Opcodes.FMUL, Opcodes.IMUL, Opcodes.LMUL };
for (int mulOpCode : opCodes) {
Instruction instruction = new Instruction(mulOpCode);
ProgramState ps = execute(instruction, zeroZeroPs);
SymbolicValue peekValue = ps.peekValue();
assertThat(peekValue).isEqualTo(sv2);
ConstraintsByDomain constraints = ps.getConstraints(peekValue);
assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.ZERO);
assertThat(constraints.get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.FALSE);
ps = execute(instruction, zeroNonZeroPs);
peekValue = ps.peekValue();
assertThat(peekValue).isEqualTo(sv1);
constraints = ps.getConstraints(peekValue);
assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.ZERO);
assertThat(constraints.get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.FALSE);
ps = execute(instruction, nonZeroZeroPs);
peekValue = ps.peekValue();
assertThat(peekValue).isEqualTo(sv2);
constraints = ps.getConstraints(peekValue);
assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.ZERO);
assertThat(constraints.get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.FALSE);
ps = execute(instruction, nonZeroNonZeroPs);
peekValue = ps.peekValue();
assertThat(peekValue).isNotIn(sv1, sv2);
constraints = ps.getConstraints(peekValue);
assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.NON_ZERO);
assertThat(constraints.get(BooleanConstraint.class)).isNull();
ps = execute(instruction, noConstraints);
peekValue = ps.peekValue();
assertThat(peekValue).isNotIn(sv1, sv2);
constraints = ps.getConstraints(peekValue);
assertThat(constraints.get(ZeroConstraint.class)).isNull();
assertThat(constraints.get(BooleanConstraint.class)).isNull();
}
}
use of org.sonar.java.se.constraint.ConstraintsByDomain in project sonar-java by SonarSource.
the class MethodYieldTest method test_yield_equality.
@Test
public void test_yield_equality() {
MethodBehavior methodBehavior = mockMethodBehavior(1, false);
MethodYield yield = newMethodYield(methodBehavior);
MethodYield otherYield;
assertThat(yield).isNotEqualTo(null);
assertThat(yield).isNotEqualTo(new Object());
// same instance
assertThat(yield).isEqualTo(yield);
// method behavior not taken into account
MethodYield myYield = newMethodYield(null);
assertThat(yield).isEqualTo(myYield);
// node not taken into account
otherYield = newMethodYield(mockNode(), methodBehavior);
assertThat(yield).isEqualTo(otherYield);
// same arity and constraints on parameters but exceptional path
otherYield = new ExceptionalYield(methodBehavior);
assertThat(yield).isNotEqualTo(otherYield);
// same arity and constraints on parameters but happy path path
otherYield = new HappyPathYield(methodBehavior);
assertThat(yield).isNotEqualTo(otherYield);
ConstraintsByDomain nullConstraint = ConstraintsByDomain.empty().put(ObjectConstraint.NULL);
MethodYield yield1 = newMethodYield(methodBehavior);
yield1.parametersConstraints.add(nullConstraint);
yield1.parametersConstraints.add(ConstraintsByDomain.empty());
yield1.parametersConstraints.add(nullConstraint);
MethodYield yield2 = newMethodYield(methodBehavior);
yield2.parametersConstraints.add(nullConstraint);
yield2.parametersConstraints.add(nullConstraint);
yield2.parametersConstraints.add(ConstraintsByDomain.empty());
assertThat(yield1).isNotEqualTo(yield2);
}
use of org.sonar.java.se.constraint.ConstraintsByDomain in project sonar-java by SonarSource.
the class ProgramState method addConstraint.
public ProgramState addConstraint(SymbolicValue symbolicValue, Constraint constraint) {
Preconditions.checkState(!(symbolicValue instanceof RelationalSymbolicValue && constraint == BooleanConstraint.FALSE), "Relations stored in PS should always use TRUE constraint. SV: %s", symbolicValue);
ConstraintsByDomain constraintsForSV = constraints.get(symbolicValue);
if (constraintsForSV == null) {
constraintsForSV = ConstraintsByDomain.empty();
}
return addConstraints(symbolicValue, constraintsForSV.put(constraint));
}
Aggregations