use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method exceptional_paths_should_be_enqueued.
@Test
public void exceptional_paths_should_be_enqueued() {
MethodBehavior mb = walker.getMethodBehavior(BytecodeEGWalkerExecuteTest.class.getCanonicalName() + "#enqueue_exceptional_paths(Lorg/sonar/java/bytecode/se/BytecodeEGWalkerExecuteTest;)Ljava/lang/Object;", squidClassLoader);
assertThat(mb.yields()).hasSize(2);
List<Constraint> resultConstraints = mb.yields().stream().map(y -> ((HappyPathYield) y).resultConstraint()).map(c -> c.get(ObjectConstraint.class)).collect(Collectors.toList());
assertThat(resultConstraints).contains(ObjectConstraint.NOT_NULL, ObjectConstraint.NULL);
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method assertBinarySymbolicValue.
private void assertBinarySymbolicValue(int[] opcodes, Class<? extends BinarySymbolicValue> binarySvClass) {
SymbolicValue sv1 = new SymbolicValue();
SymbolicValue sv2 = new SymbolicValue();
ProgramState initState = ProgramState.EMPTY_STATE.stackValue(sv2).stackValue(sv1);
for (int opcode : opcodes) {
ProgramState programState = execute(new Instruction(opcode), initState);
ProgramState.Pop pop = programState.unstackValue(1);
assertStack(programState, new Constraint[][] { { ObjectConstraint.NOT_NULL } });
SymbolicValue result = pop.values.get(0);
assertThat(result).isNotEqualTo(sv1);
assertThat(result).isNotEqualTo(sv2);
assertThat(result).isInstanceOf(binarySvClass);
assertThat(isDoubleOrLong(programState, result)).isEqualTo(LONG_OPCODE.contains(opcode));
BinarySymbolicValue andSv = (BinarySymbolicValue) result;
assertThat(andSv.getRightOp()).isEqualTo(sv1);
assertThat(andSv.getLeftOp()).isEqualTo(sv2);
}
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_iconst.
@Test
public void test_iconst() throws Exception {
ProgramState programState = execute(new Instruction(Opcodes.ICONST_0));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.ZERO, BooleanConstraint.FALSE, ObjectConstraint.NOT_NULL } });
programState = execute(new Instruction(Opcodes.ICONST_1));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, BooleanConstraint.TRUE, ObjectConstraint.NOT_NULL } });
int[] opCodesConst = new int[] { Opcodes.ICONST_M1, Opcodes.ICONST_2, Opcodes.ICONST_3, Opcodes.ICONST_4, Opcodes.ICONST_5 };
for (int opcode : opCodesConst) {
programState = execute(new Instruction(opcode));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, ObjectConstraint.NOT_NULL } });
}
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method assertConsume2produceNotNull.
private void assertConsume2produceNotNull(int... opcodes) {
SymbolicValue sv1 = new SymbolicValue();
SymbolicValue sv2 = new SymbolicValue();
ProgramState initState = ProgramState.EMPTY_STATE.stackValue(sv2).stackValue(sv1);
for (int opcode : opcodes) {
ProgramState programState = execute(new Instruction(opcode), initState);
ProgramState.Pop pop = programState.unstackValue(1);
assertStack(programState, new Constraint[][] { { ObjectConstraint.NOT_NULL } });
SymbolicValue result = pop.values.get(0);
assertThat(result).isNotEqualTo(sv1);
assertThat(result).isNotEqualTo(sv2);
assertThat(isDoubleOrLong(programState, result)).isEqualTo(LONG_OPCODE.contains(opcode));
}
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class MethodBehaviorTest method result_with_boolean_constraint_should_be_reduced.
@Test
public void result_with_boolean_constraint_should_be_reduced() {
MethodBehavior mb = new MethodBehavior("foo()Z");
addYield(mb, BooleanConstraint.TRUE);
addYield(mb, BooleanConstraint.FALSE);
mb.completed();
assertThat(mb.yields()).hasSize(1);
assertThat(((HappyPathYield) mb.yields().get(0)).resultConstraint()).isNull();
mb = new MethodBehavior("foo()Z");
addYield(mb, BooleanConstraint.TRUE, ObjectConstraint.NULL);
addYield(mb, BooleanConstraint.FALSE, ObjectConstraint.NOT_NULL);
mb.completed();
assertThat(mb.yields()).hasSize(2);
List<Constraint> resultConstraints = mb.yields().stream().map(y -> ((HappyPathYield) y).resultConstraint().get(BooleanConstraint.class)).collect(Collectors.toList());
assertThat(resultConstraints).contains(BooleanConstraint.TRUE, BooleanConstraint.FALSE);
}
Aggregations