Search in sources :

Example 6 with HappyPathYield

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

the class BytecodeEGWalkerTest method test_int_comparator.

@Test
public void test_int_comparator() throws Exception {
    MethodBehavior methodBehavior = getMethodBehavior("int_comparison(II)Ljava/lang/Object;");
    assertThat(methodBehavior.yields()).hasSize(1);
    HappyPathYield methodYield = ((HappyPathYield) methodBehavior.yields().get(0));
    assertThat(methodYield.resultConstraint().get(ObjectConstraint.class)).isSameAs(ObjectConstraint.NULL);
}
Also used : HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 7 with HappyPathYield

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

the class BytecodeEGWalkerTest method test_enqueueing_of_exit_block4.

@Test
public void test_enqueueing_of_exit_block4() {
    MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "enqueueExitBlock4()Z");
    List<HappyPathYield> happyPathYields = mb.happyPathYields().collect(Collectors.toList());
    assertThat(happyPathYields.get(0).resultConstraint().hasConstraint(BooleanConstraint.TRUE)).isTrue();
    assertThat(mb.exceptionalPathYields().findFirst().isPresent()).isFalse();
}
Also used : HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 8 with HappyPathYield

use of org.sonar.java.se.xproc.HappyPathYield 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();
}
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) DivisionByZeroCheck(org.sonar.java.se.checks.DivisionByZeroCheck) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) 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 9 with HappyPathYield

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

the class BytecodeEGWalkerExecuteTest method exceptional_paths_should_be_enqueued.

@Test
public void exceptional_paths_should_be_enqueued() {
    MethodBehavior mb = walker.getMethodBehavior(BytecodeEGWalkerExecuteTest.class.getCanonicalName() + "#enqueue_exceptional_paths(Lorg/sonar/java/bytecode/se/BytecodeEGWalkerExecuteTest;)Ljava/lang/Object;", squidClassLoader);
    assertThat(mb.yields()).hasSize(2);
    List<Constraint> resultConstraints = mb.yields().stream().map(y -> ((HappyPathYield) y).resultConstraint()).map(c -> c.get(ObjectConstraint.class)).collect(Collectors.toList());
    assertThat(resultConstraints).contains(ObjectConstraint.NOT_NULL, ObjectConstraint.NULL);
}
Also used : ASTORE(org.objectweb.asm.Opcodes.ASTORE) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SETestUtils(org.sonar.java.se.SETestUtils) DMUL(org.objectweb.asm.Opcodes.DMUL) LSUB(org.objectweb.asm.Opcodes.LSUB) MethodYield(org.sonar.java.se.xproc.MethodYield) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) LSHR(org.objectweb.asm.Opcodes.LSHR) DSUB(org.objectweb.asm.Opcodes.DSUB) LMUL(org.objectweb.asm.Opcodes.LMUL) Mockito.doThrow(org.mockito.Mockito.doThrow) LSHL(org.objectweb.asm.Opcodes.LSHL) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Collectors(java.util.stream.Collectors) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) LAND(org.objectweb.asm.Opcodes.LAND) List(java.util.List) LXOR(org.objectweb.asm.Opcodes.LXOR) Instruction(org.sonar.java.bytecode.cfg.Instruction) GOTO(org.objectweb.asm.Opcodes.GOTO) Constraint(org.sonar.java.se.constraint.Constraint) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) LUSHR(org.objectweb.asm.Opcodes.LUSHR) BeforeClass(org.junit.BeforeClass) ProgramState(org.sonar.java.se.ProgramState) Label(org.objectweb.asm.Label) Mockito.spy(org.mockito.Mockito.spy) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ArrayList(java.util.ArrayList) Instructions(org.sonar.java.bytecode.cfg.Instructions) DivisionByZeroCheck(org.sonar.java.se.checks.DivisionByZeroCheck) ISTORE(org.objectweb.asm.Opcodes.ISTORE) INVOKESTATIC(org.objectweb.asm.Opcodes.INVOKESTATIC) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) Before(org.junit.Before) JavaParser(org.sonar.java.ast.parser.JavaParser) LOR(org.objectweb.asm.Opcodes.LOR) ICONST_4(org.objectweb.asm.Opcodes.ICONST_4) Opcodes(org.objectweb.asm.Opcodes) DREM(org.objectweb.asm.Opcodes.DREM) ICONST_3(org.objectweb.asm.Opcodes.ICONST_3) ICONST_2(org.objectweb.asm.Opcodes.ICONST_2) ICONST_1(org.objectweb.asm.Opcodes.ICONST_1) ICONST_0(org.objectweb.asm.Opcodes.ICONST_0) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DADD(org.objectweb.asm.Opcodes.DADD) Type(org.sonar.plugins.java.api.semantic.Type) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) File(java.io.File) LADD(org.objectweb.asm.Opcodes.LADD) CFG(org.sonar.java.cfg.CFG) Printer(org.objectweb.asm.util.Printer) RETURN(org.objectweb.asm.Opcodes.RETURN) SemanticModel(org.sonar.java.resolve.SemanticModel) LREM(org.objectweb.asm.Opcodes.LREM) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) ILOAD(org.objectweb.asm.Opcodes.ILOAD) ProgramPoint(org.sonar.java.se.ProgramPoint) Preconditions(com.google.common.base.Preconditions) DDIV(org.objectweb.asm.Opcodes.DDIV) Collections(java.util.Collections) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) LDIV(org.objectweb.asm.Opcodes.LDIV) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) Constraint(org.sonar.java.se.constraint.Constraint) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) 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