use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class SECheckTest method flow_from_exit_node_should_not_lead_to_infinite_recursion.
@Test(timeout = 3000)
public void flow_from_exit_node_should_not_lead_to_infinite_recursion() throws Exception {
CFG cfg = CFGTest.buildCFG("void foo(boolean a) { if(a) {foo(true);} foo(false); }");
ExplodedGraph eg = new ExplodedGraph();
ExplodedGraph.Node node = eg.node(new ProgramPoint(cfg.blocks().get(3)), ProgramState.EMPTY_STATE);
node.addParent(eg.node(new ProgramPoint(cfg.blocks().get(2)).next().next(), ProgramState.EMPTY_STATE), null);
Set<Flow> flows = FlowComputation.flow(node, new SymbolicValue(), Collections.singletonList(ObjectConstraint.class));
assertThat(flows.iterator().next().isEmpty()).isTrue();
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class SymbolicValueFactoryTest method testFactory.
@Test
public void testFactory() {
final IdentifierTree tree = new IdentifierTreeImpl(new InternalSyntaxToken(1, 1, "id", Collections.<SyntaxTrivia>emptyList(), 0, 0, false));
final ConstraintManager manager = new ConstraintManager();
SymbolicValue symbolicValue = manager.createSymbolicValue(tree);
assertThat(symbolicValue.getClass()).as("Created without factory").isSameAs(SymbolicValue.class);
manager.setValueFactory(new TestSymbolicValueFactory());
symbolicValue = manager.createSymbolicValue(tree);
assertThat(symbolicValue.getClass()).as("Created with factory").isSameAs(TestSymbolicValue.class);
assertThat(symbolicValue.references(symbolicValue)).isFalse();
manager.setValueFactory(new TestSymbolicValueFactory());
try {
manager.setValueFactory(new TestSymbolicValueFactory());
fail("Able to add a second factory to the contraints manager");
} catch (IllegalStateException e) {
assertThat(e.getMessage()).as("Exception message").isEqualTo("The symbolic value factory has already been defined by another checker!");
}
symbolicValue = manager.createSymbolicValue(tree);
assertThat(symbolicValue.getClass()).as("Created with first factory").isSameAs(TestSymbolicValue.class);
symbolicValue = manager.createSymbolicValue(tree);
assertThat(symbolicValue.getClass()).as("Created after factory usage").isSameAs(SymbolicValue.class);
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class BytecodeEGWalkerTest method verify_behavior_of_fun2_method.
@Test
public void verify_behavior_of_fun2_method() throws Exception {
MethodBehavior methodBehavior = getMethodBehavior("fun2(Z)Ljava/lang/Object;");
assertThat(methodBehavior.yields()).hasSize(2);
SymbolicValue svFirstArg = new SymbolicValue();
SymbolicValue svResult = new SymbolicValue();
List<SymbolicValue> invocationArguments = Lists.newArrayList(svFirstArg);
List<HappyPathYield> oneYield = methodBehavior.happyPathYields().filter(my -> ObjectConstraint.NULL.equals(my.resultConstraint().get(ObjectConstraint.class))).collect(Collectors.toList());
assertThat(oneYield).hasSize(1);
HappyPathYield yield = oneYield.get(0);
Collection<ProgramState> pss = yield.statesAfterInvocation(invocationArguments, Lists.newArrayList(), ProgramState.EMPTY_STATE, () -> svResult).collect(Collectors.toList());
assertThat(pss).hasSize(1);
ProgramState ps = pss.iterator().next();
assertThat(ps.getConstraint(svFirstArg, ObjectConstraint.class)).isNull();
assertThat(ps.getConstraint(svFirstArg, BooleanConstraint.class)).isSameAs(BooleanConstraint.TRUE);
assertThat(ps.getConstraint(svFirstArg, DivisionByZeroCheck.ZeroConstraint.class)).isNull();
oneYield = methodBehavior.happyPathYields().filter(my -> ObjectConstraint.NOT_NULL.equals(my.resultConstraint().get(ObjectConstraint.class))).collect(Collectors.toList());
assertThat(oneYield).hasSize(1);
yield = oneYield.get(0);
pss = yield.statesAfterInvocation(invocationArguments, Lists.newArrayList(), ProgramState.EMPTY_STATE, () -> svResult).collect(Collectors.toList());
assertThat(pss).hasSize(1);
ps = pss.iterator().next();
assertThat(ps.getConstraint(svFirstArg, ObjectConstraint.class)).isNull();
assertThat(ps.getConstraint(svFirstArg, BooleanConstraint.class)).isSameAs(BooleanConstraint.FALSE);
assertThat(ps.getConstraint(svFirstArg, DivisionByZeroCheck.ZeroConstraint.class)).isNull();
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class ExceptionalCheckBasedYieldTest method test_equals.
@Test
public void test_equals() {
final Class<? extends SECheck> seCheckClass1 = new SECheck() {
}.getClass();
final Class<? extends SECheck> seCheckClass2 = (new SECheck() {
}).getClass();
MethodBehavior mb = mockMethodBehavior();
String mockedExceptionType1 = "SomeException";
ExceptionalCheckBasedYield yield = new ExceptionalCheckBasedYield(SV_CAUSING_EXCEPTION, mockedExceptionType1, seCheckClass1, null, mb);
ExceptionalYield otherYield = new ExceptionalCheckBasedYield(SV_CAUSING_EXCEPTION, mockedExceptionType1, seCheckClass1, null, mb);
assertThat(yield).isNotEqualTo(null);
assertThat(yield).isEqualTo(yield);
assertThat(yield).isEqualTo(otherYield);
// same exception, but simple exceptional yield
otherYield = new ExceptionalYield(null, mb);
otherYield.setExceptionType(mockedExceptionType1);
assertThat(yield).isNotEqualTo(otherYield);
// same exception, different SV
otherYield = new ExceptionalCheckBasedYield(new SymbolicValue(), mockedExceptionType1, seCheckClass2, null, mb);
assertThat(yield).isNotEqualTo(otherYield);
// same exception, different check
otherYield = new ExceptionalCheckBasedYield(SV_CAUSING_EXCEPTION, mockedExceptionType1, seCheckClass2, null, mb);
assertThat(yield).isNotEqualTo(otherYield);
// different exception, same check
otherYield = new ExceptionalCheckBasedYield(SV_CAUSING_EXCEPTION, "SomeOtherException", seCheckClass1, null, mb);
assertThat(yield).isNotEqualTo(otherYield);
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class MethodYieldTest method test_creation_of_states.
@Test
public void test_creation_of_states() throws Exception {
SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/XProcYields.java");
MethodBehavior mb = getMethodBehavior(sev, "foo");
ProgramState ps = ProgramState.EMPTY_STATE;
SymbolicValue sv1 = new SymbolicValue();
SymbolicValue sv2 = new SymbolicValue();
SymbolicValue sv3 = new SymbolicValue();
Symbol sym = new JavaSymbol.VariableJavaSymbol(0, "myVar", new JavaSymbol.MethodJavaSymbol(0, "dummy", null));
ps = ps.put(sym, sv1);
MethodYield methodYield = mb.happyPathYields().findFirst().get();
Stream<ProgramState> generatedStatesFromFirstYield = methodYield.statesAfterInvocation(Lists.newArrayList(sv1, sv2), Lists.newArrayList(), ps, () -> sv3);
assertThat(generatedStatesFromFirstYield).hasSize(1);
}
Aggregations