use of org.sonar.java.se.SymbolicExecutionVisitor in project sonar-java by SonarSource.
the class MethodYieldTest method test_creation_of_flows.
@Test
public void test_creation_of_flows() throws Exception {
SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/XProcYieldsFlows.java");
MethodBehavior mb = getMethodBehavior(sev, "foo");
MethodYield methodYield = mb.happyPathYields().filter(y -> y.resultConstraint() != null && y.resultConstraint().get(ObjectConstraint.class) != ObjectConstraint.NULL).findFirst().get();
Set<Flow> flowReturnValue = methodYield.flow(ImmutableList.of(-1), Lists.newArrayList(ObjectConstraint.class));
assertThat(flowReturnValue.iterator().next().isEmpty()).isFalse();
Set<Flow> flowFirstParam = methodYield.flow(ImmutableList.of(0), Lists.newArrayList(ObjectConstraint.class, BooleanConstraint.class));
assertThat(flowFirstParam.iterator().next().isEmpty()).isFalse();
}
use of org.sonar.java.se.SymbolicExecutionVisitor 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);
}
use of org.sonar.java.se.SymbolicExecutionVisitor in project sonar-java by SonarSource.
the class DebugCheckTestUtils method verifyIssuesWithMaxSteps.
static void verifyIssuesWithMaxSteps(String sourcefile, SECheck check, int maxSteps) {
BehaviorCache behaviorCache = new BehaviorCache(new SquidClassLoader(new ArrayList<>()));
SymbolicExecutionVisitor sev = new SymbolicExecutionVisitor(Collections.singletonList(check), behaviorCache) {
@Override
protected ExplodedGraphWalker getWalker() {
return new ExplodedGraphWalker(Collections.singletonList(check), behaviorCache, (SemanticModel) context.getSemanticModel()) {
@Override
protected int maxSteps() {
return maxSteps;
}
};
}
@Override
public void scanFile(JavaFileScannerContext context) {
super.scanFile(context);
// the check has been executed, but we still need to call the scan manually to report the issues
check.scanFile(context);
}
};
JavaCheckVerifier.verify(sourcefile, sev);
}
use of org.sonar.java.se.SymbolicExecutionVisitor in project sonar-java by SonarSource.
the class ExceptionalYieldTest method exceptional_yields_void_method.
@Test
public void exceptional_yields_void_method() {
Pair<SymbolicExecutionVisitor, SemanticModel> sevAndSemantic = createSymbolicExecutionVisitorAndSemantic("src/test/files/se/ExceptionalYieldsVoidMethod.java");
SymbolicExecutionVisitor sev = sevAndSemantic.a;
SemanticModel semanticModel = sevAndSemantic.b;
MethodBehavior mb = getMethodBehavior(sev, "myVoidMethod");
assertThat(mb.yields()).hasSize(4);
List<ExceptionalYield> exceptionalYields = mb.exceptionalPathYields().collect(Collectors.toList());
assertThat(exceptionalYields).hasSize(3);
assertThat(exceptionalYields.stream().filter(y -> y.exceptionType(semanticModel).isUnknown()).count()).isEqualTo(1);
MethodYield explicitExceptionYield = exceptionalYields.stream().filter(y -> y.exceptionType(semanticModel).is("org.foo.MyException1")).findAny().get();
assertThat(explicitExceptionYield.parametersConstraints.get(0).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NULL);
MethodYield implicitExceptionYield = exceptionalYields.stream().filter(y -> y.exceptionType(semanticModel).is("org.foo.MyException2")).findAny().get();
assertThat(implicitExceptionYield.parametersConstraints.get(0).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
}
use of org.sonar.java.se.SymbolicExecutionVisitor in project sonar-java by SonarSource.
the class MethodBehaviorTest method method_behavior_signature.
@Test
public void method_behavior_signature() {
SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/resources/se/MethodYields.java");
MethodBehavior mb = getMethodBehavior(sev, "method");
assertThat(mb.signature()).isEqualTo("MethodYields#method(Ljava/lang/Object;Z)Z");
assertThat(mb.signature()).isNotNull();
}
Aggregations