Search in sources :

Example 1 with HappyPathYield

use of org.sonar.java.se.xproc.HappyPathYield in project sonar-java by SonarSource.

the class ExplodedGraphWalkerTest method test_enqueueing_of_catch_blocks.

@Test
public void test_enqueueing_of_catch_blocks() {
    SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/java/org/sonar/java/bytecode/se/testdata/ExceptionEnqueue.java");
    MethodBehavior mb = sev.behaviorCache.behaviors.get("org.sonar.java.bytecode.se.testdata.ExceptionEnqueue#testCatchBlockEnqueue(Lorg/sonar/java/bytecode/se/testdata/ExceptionEnqueue;)Z");
    List<HappyPathYield> happyPathYields = mb.happyPathYields().collect(Collectors.toList());
    assertThat(happyPathYields).hasSize(1);
    assertThat(happyPathYields.get(0).resultConstraint()).isNull();
    mb = sev.behaviorCache.behaviors.get("org.sonar.java.bytecode.se.testdata.ExceptionEnqueue#testCatchBlockEnqueue2()Z");
    happyPathYields = mb.happyPathYields().collect(Collectors.toList());
    assertThat(happyPathYields).hasSize(1);
    // correctly result constraint should be TRUE, but we enqueue also unreachable catch block which creates yield with FALSE result
    // and yields are reduced consequently
    assertThat(happyPathYields.get(0).resultConstraint()).isNull();
}
Also used : HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) Test(org.junit.Test)

Example 2 with HappyPathYield

use of org.sonar.java.se.xproc.HappyPathYield in project sonar-java by SonarSource.

the class BytecodeEGWalkerTest method generateMethodBehavior.

@Test
public void generateMethodBehavior() throws Exception {
    MethodBehavior methodBehavior = getMethodBehavior("fun(ZLjava/lang/Object;)Ljava/lang/Object;");
    assertThat(methodBehavior.yields()).hasSize(2);
    SymbolicValue svFirstArg = new SymbolicValue();
    SymbolicValue svsecondArg = new SymbolicValue();
    SymbolicValue svResult = new SymbolicValue();
    List<SymbolicValue> invocationArguments = Lists.newArrayList(svFirstArg, svsecondArg);
    List<ObjectConstraint> collect = methodBehavior.yields().stream().map(my -> {
        Collection<ProgramState> ps = my.statesAfterInvocation(invocationArguments, Lists.newArrayList(), ProgramState.EMPTY_STATE, () -> svResult).collect(Collectors.toList());
        assertThat(ps).hasSize(1);
        ProgramState next = ps.iterator().next();
        return next.getConstraint(svResult, ObjectConstraint.class);
    }).collect(Collectors.toList());
    assertThat(collect).hasSize(2).containsOnly(ObjectConstraint.NOT_NULL, ObjectConstraint.NULL);
    List<HappyPathYield> nullConstraintOnResult = methodBehavior.happyPathYields().filter(my -> ObjectConstraint.NULL.equals(my.resultConstraint().get(ObjectConstraint.class))).collect(Collectors.toList());
    assertThat(nullConstraintOnResult).hasSize(1);
    HappyPathYield nullConstraintResult = nullConstraintOnResult.get(0);
    Collection<ProgramState> ps = nullConstraintResult.statesAfterInvocation(invocationArguments, Lists.newArrayList(), ProgramState.EMPTY_STATE, () -> svResult).collect(Collectors.toList());
    assertThat(ps).hasSize(1);
    ObjectConstraint constraint = ps.iterator().next().getConstraint(svsecondArg, ObjectConstraint.class);
    assertThat(constraint).isSameAs(ObjectConstraint.NULL);
}
Also used : BytecodeTestClass(org.sonar.java.bytecode.se.testdata.BytecodeTestClass) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) Iterables(com.google.common.collect.Iterables) BeforeClass(org.junit.BeforeClass) ProgramState(org.sonar.java.se.ProgramState) MethodYield(org.sonar.java.se.xproc.MethodYield) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExceptionEnqueue(org.sonar.java.bytecode.se.testdata.ExceptionEnqueue) DivisionByZeroCheck(org.sonar.java.se.checks.DivisionByZeroCheck) Lists(com.google.common.collect.Lists) Assertions.assertThat(org.fest.assertions.Assertions.assertThat) FinalBytecodeTestClass(org.sonar.java.bytecode.se.testdata.FinalBytecodeTestClass) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) JavaParser(org.sonar.java.ast.parser.JavaParser) Collection(java.util.Collection) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) MaxRelationBytecode(org.sonar.java.bytecode.se.testdata.MaxRelationBytecode) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Type(org.sonar.plugins.java.api.semantic.Type) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) SemanticModel(org.sonar.java.resolve.SemanticModel) ByteStreams(com.google.common.io.ByteStreams) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) Constraint(org.sonar.java.se.constraint.Constraint) ExceptionalYield(org.sonar.java.se.xproc.ExceptionalYield) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Collection(java.util.Collection) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Test(org.junit.Test)

Example 3 with HappyPathYield

use of org.sonar.java.se.xproc.HappyPathYield in project sonar-java by SonarSource.

the class BytecodeEGWalkerTest method test_enqueueing_of_catch_blocks.

@Test
public void test_enqueueing_of_catch_blocks() {
    MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "testCatchBlockEnqueue(Lorg/sonar/java/bytecode/se/testdata/ExceptionEnqueue;)Z");
    List<HappyPathYield> happyPathYields = mb.happyPathYields().collect(Collectors.toList());
    assertThat(happyPathYields).hasSize(1);
    assertThat(happyPathYields.get(0).resultConstraint()).isNull();
    List<ExceptionalYield> exceptionalYields = mb.exceptionalPathYields().collect(Collectors.toList());
    assertThat(exceptionalYields).hasSize(1);
    assertThat(exceptionalYields.get(0).exceptionType(semanticModel).is("java.lang.RuntimeException")).isTrue();
}
Also used : HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) ExceptionalYield(org.sonar.java.se.xproc.ExceptionalYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 4 with HappyPathYield

use of org.sonar.java.se.xproc.HappyPathYield in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method method_returning_new_should_have_not_null_result.

@Test
public void method_returning_new_should_have_not_null_result() {
    MethodBehavior mb = walker.getMethodBehavior(BytecodeEGWalkerExecuteTest.class.getCanonicalName() + "#newObject()Ljava/lang/Object;", squidClassLoader);
    List<MethodYield> yields = mb.yields();
    assertThat(yields).hasSize(1);
    MethodYield yield = yields.get(0);
    assertThat(yield).isInstanceOf(HappyPathYield.class);
    ConstraintsByDomain resultConstraint = ((HappyPathYield) yield).resultConstraint();
    assertThat(resultConstraint).isNotNull();
    assertThat(resultConstraint.get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    TypedConstraint typeConstraint = (TypedConstraint) resultConstraint.get(TypedConstraint.class);
    assertThat(typeConstraint.type.equals("java.lang.String")).isTrue();
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) MethodYield(org.sonar.java.se.xproc.MethodYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) Test(org.junit.Test)

Example 5 with HappyPathYield

use of org.sonar.java.se.xproc.HappyPathYield in project sonar-java by SonarSource.

the class BytecodeEGWalkerTest method test_enqueueing_of_exit_block2.

@Test
public void test_enqueueing_of_exit_block2() {
    MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "enqueueExitBlock2(Lorg/sonar/java/bytecode/se/testdata/ExceptionEnqueue;)Z");
    List<HappyPathYield> happyPathYields = mb.happyPathYields().collect(Collectors.toList());
    assertThat(happyPathYields).hasSize(1);
    assertThat(happyPathYields.get(0).resultConstraint()).isNull();
    List<ExceptionalYield> exceptionalYields = mb.exceptionalPathYields().collect(Collectors.toList());
    assertThat(exceptionalYields).hasSize(1);
    assertThat(exceptionalYields.get(0).exceptionType(semanticModel).is("java.io.IOException")).isTrue();
}
Also used : HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) ExceptionalYield(org.sonar.java.se.xproc.ExceptionalYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)9 HappyPathYield (org.sonar.java.se.xproc.HappyPathYield)9 MethodBehavior (org.sonar.java.se.xproc.MethodBehavior)9 ExceptionalYield (org.sonar.java.se.xproc.ExceptionalYield)4 MethodYield (org.sonar.java.se.xproc.MethodYield)4 File (java.io.File)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 FileUtils (org.apache.commons.io.FileUtils)3 BeforeClass (org.junit.BeforeClass)3 JavaParser (org.sonar.java.ast.parser.JavaParser)3 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)3 SemanticModel (org.sonar.java.resolve.SemanticModel)3 ProgramState (org.sonar.java.se.ProgramState)3 DivisionByZeroCheck (org.sonar.java.se.checks.DivisionByZeroCheck)3 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)3 Constraint (org.sonar.java.se.constraint.Constraint)3 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)3 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)3 BehaviorCache (org.sonar.java.se.xproc.BehaviorCache)3