use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_dup2_x2_form4.
@Test
public void test_dup2_x2_form4() throws Exception {
SymbolicValue sv1 = new SymbolicValue();
SymbolicValue sv2 = new SymbolicValue();
SymbolicValue sv3 = new SymbolicValue();
SymbolicValue sv4 = new SymbolicValue();
ProgramState startingState = ProgramState.EMPTY_STATE.stackValue(sv4).stackValue(sv3).stackValue(sv2).stackValue(sv1);
startingState = setDoubleOrLong(startingState, sv1, true);
startingState = setDoubleOrLong(startingState, sv2, true);
ProgramState programState = execute(new Instruction(Opcodes.DUP2_X2), startingState);
ProgramState.Pop pop = programState.unstackValue(6);
assertThat(pop.values).containsExactly(sv1, sv2, sv1, sv3, sv4);
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_sipush.
@Test
public void test_sipush() throws Exception {
ProgramState programState = execute(new Instruction(Opcodes.SIPUSH, 42));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, ObjectConstraint.NOT_NULL } });
programState = execute(new Instruction(Opcodes.SIPUSH, 1));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, ObjectConstraint.NOT_NULL, BooleanConstraint.TRUE } });
programState = execute(new Instruction(Opcodes.SIPUSH, 0));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.ZERO, ObjectConstraint.NOT_NULL, BooleanConstraint.FALSE } });
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_invoke_static.
@Test
public void test_invoke_static() throws Exception {
ProgramState programState = execute(invokeStatic("staticMethod", "()V"));
assertEmptyStack(programState);
programState = execute(invokeStatic("staticBooleanMethod", "()Z"));
assertStack(programState, new Constraint[][] { { ObjectConstraint.NOT_NULL, BooleanConstraint.FALSE } });
assertThat(isDoubleOrLong(programState, programState.peekValue())).isFalse();
SymbolicValue arg = new SymbolicValue();
programState = execute(invokeStatic("staticIntMethodWithIntArgument", "(I)I"), ProgramState.EMPTY_STATE.stackValue(arg));
assertStack(programState, new Constraint[][] { { ObjectConstraint.NOT_NULL, DivisionByZeroCheck.ZeroConstraint.ZERO } });
assertThat(programState.peekValue()).isNotEqualTo(arg);
assertThat(isDoubleOrLong(programState, programState.peekValue())).isFalse();
programState = execute(invokeStatic("staticMethodWithIntIntArgument", "(II)V"), ProgramState.EMPTY_STATE.stackValue(arg).stackValue(arg));
assertEmptyStack(programState);
programState = execute(invokeStatic("staticMethodWithIntIntArgument", "(II)V"), ProgramState.EMPTY_STATE.stackValue(arg).stackValue(arg));
assertEmptyStack(programState);
programState = execute(invokeStatic("staticReturningLong", "()J"), ProgramState.EMPTY_STATE);
assertThat(isDoubleOrLong(programState, programState.peekValue())).isTrue();
programState = execute(invokeStatic("staticReturningDouble", "()D"), ProgramState.EMPTY_STATE);
assertThat(isDoubleOrLong(programState, programState.peekValue())).isTrue();
assertThatThrownBy(() -> execute(invokeStatic("staticBooleanMethodWithIntArgument", "(I)V"))).hasMessage("Arguments mismatch for INVOKE");
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method execute.
private ProgramState execute(Instruction instruction, ProgramState startingState) {
CFG.IBlock block = mock(CFG.IBlock.class);
when(block.successors()).thenReturn(Collections.emptySet());
walker.programPosition = new ProgramPoint(block);
walker.programState = startingState;
walker.workList.clear();
walker.executeInstruction(instruction);
ProgramState programState = walker.programState;
// X-PROC will enqueue new nodes
if (instruction.isInvoke() && !walker.workList.isEmpty()) {
programState = walker.workList.getFirst().programState;
}
return programState;
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_nullness_check.
@Test
public void test_nullness_check() throws Exception {
SymbolicValue thisSv = new SymbolicValue();
ProgramState startingState = ProgramState.EMPTY_STATE.stackValue(thisSv);
ProgramState programState = execute(invokeMethod(Opcodes.INVOKESPECIAL, "methodWithoutArgument", "()V"), startingState);
assertThat(hasConstraint(thisSv, programState, ObjectConstraint.NOT_NULL)).isTrue();
programState = execute(invokeStatic("staticBooleanMethod", "()Z"), startingState);
assertStack(programState, new Constraint[][] { { ObjectConstraint.NOT_NULL, BooleanConstraint.FALSE }, { null } });
programState = execute(invokeMethod(Opcodes.INVOKESPECIAL, "methodWithIntArgument", "(I)V"), startingState.stackValue(new SymbolicValue()));
assertThat(hasConstraint(thisSv, programState, ObjectConstraint.NOT_NULL)).isTrue();
}
Aggregations