use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_new.
@Test
public void test_new() throws Exception {
ProgramState programState = execute(new Instruction(Opcodes.NEW, "java.lang.Object"));
assertStack(programState, new Constraint[][] { { ObjectConstraint.NOT_NULL, new TypedConstraint("java.lang.Object") } });
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_getfield.
@Test
public void test_getfield() throws Exception {
SymbolicValue objectRef = new SymbolicValue();
ProgramState programState = execute(new Instruction(Opcodes.GETFIELD, new Instruction.FieldOrMethod("", "", "D", false)), ProgramState.EMPTY_STATE.stackValue(objectRef));
SymbolicValue fieldValue = programState.peekValue();
assertThat(fieldValue).isNotNull();
assertThat(isDoubleOrLong(programState, fieldValue)).isTrue();
assertThat(fieldValue).isNotEqualTo(objectRef);
programState = execute(new Instruction(Opcodes.GETFIELD, new Instruction.FieldOrMethod("", "", "I", false)), ProgramState.EMPTY_STATE.stackValue(objectRef));
fieldValue = programState.peekValue();
assertThat(fieldValue).isNotNull();
assertThat(isDoubleOrLong(programState, fieldValue)).isFalse();
assertThatThrownBy(() -> execute(new Instruction(Opcodes.GETFIELD))).hasMessage("GETFIELD needs 1 values on stack");
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_compare_instructions.
@Test
public void test_compare_instructions() {
int[] opcodes = { Opcodes.IF_ICMPEQ, Opcodes.IF_ICMPNE, Opcodes.IF_ICMPLT, Opcodes.IF_ICMPGE, Opcodes.IF_ACMPEQ, Opcodes.IF_ACMPNE };
SymbolicValue left = new SymbolicValue();
SymbolicValue right = new SymbolicValue();
for (int opcode : opcodes) {
ProgramState programState = walker.branchingState(new Instruction(opcode), ProgramState.EMPTY_STATE.stackValue(left).stackValue(right));
RelationalSymbolicValue relSV = (RelationalSymbolicValue) programState.peekValue();
assertThat(relSV.getLeftOp()).isSameAs(left);
assertThat(relSV.getRightOp()).isSameAs(right);
}
// these opcodes inverse operator and swap operands
int[] swapOperandsOpcodes = { Opcodes.IF_ICMPLE, Opcodes.IF_ICMPGT };
for (int opcode : swapOperandsOpcodes) {
ProgramState programState = walker.branchingState(new Instruction(opcode), ProgramState.EMPTY_STATE.stackValue(left).stackValue(right));
RelationalSymbolicValue relSV = (RelationalSymbolicValue) programState.peekValue();
assertThat(relSV.getRightOp()).isSameAs(left);
assertThat(relSV.getLeftOp()).isSameAs(right);
}
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_athrow.
@Test
public void test_athrow() throws Exception {
SymbolicValue sv = new SymbolicValue();
Type exceptionType = semanticModel.getClassType("java.lang.RuntimeException");
ProgramState initialState = ProgramState.EMPTY_STATE.stackValue(sv).addConstraint(sv, new TypedConstraint("java.lang.RuntimeException"));
ProgramState programState = execute(new Instruction(Opcodes.ATHROW), initialState);
SymbolicValue exception = programState.peekValue();
assertThat(exception).isInstanceOf(SymbolicValue.ExceptionalSymbolicValue.class);
assertThat(((SymbolicValue.ExceptionalSymbolicValue) exception).exceptionType()).isEqualTo(exceptionType);
assertThat(programState.exitValue()).isEqualTo(exception);
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class DivisionByZeroCheckTest method copyConstraint.
private DivisionByZeroCheck.ZeroConstraint copyConstraint(SymbolicValue a, SymbolicValue b, RelationalSymbolicValue.Kind relation, @Nullable DivisionByZeroCheck.ZeroConstraint expected) {
ProgramState ps = Iterables.getOnlyElement(a.setConstraint(ProgramState.EMPTY_STATE, ZERO));
RelationalSymbolicValue rel = new RelationalSymbolicValue(relation);
SymbolicValueTestUtil.computedFrom(rel, b, a);
ps = Iterables.getOnlyElement(rel.setConstraint(ps, BooleanConstraint.TRUE));
return ps.getConstraint(b, DivisionByZeroCheck.ZeroConstraint.class);
}
Aggregations