[java-property-tests] SourceToSinkPropertyInspectionTest fixed

Likely, the improvements in inspection were not reflected in test:
1. Later assignment to local variable overwrites the previous tainted state
2. Constant conditions like (1 == 1) are evaluated by inspection, so we need a non-constant one in test

GitOrigin-RevId: 62e373f85011b8302186cdf3b951cb8b056a7603
This commit is contained in:
Tagir Valeev
2024-04-19 15:17:10 +02:00
committed by intellij-monorepo-bot
parent 053d59f113
commit 098214398a

View File

@@ -14,6 +14,7 @@ import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import com.intellij.util.containers.ContainerUtil;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jetCheck.Generator;
@@ -271,7 +272,7 @@ public class SourceToSinkPropertyInspectionTest extends LightJavaCodeInsightFixt
@Override
public @NotNull String getText() {
return String.format("((1 == 1) ? %s : %s)", myLhs.getText(), myRhs.getText());
return String.format("((Math.random() > 0.5) ? %s : %s)", myLhs.getText(), myRhs.getText());
}
@Override
@@ -355,16 +356,8 @@ public class SourceToSinkPropertyInspectionTest extends LightJavaCodeInsightFixt
@Override
public @NotNull TaintState taintState() {
TaintState taintState = TaintState.SAFE;
for (Variable variable : myVariables) {
if (variable.isField()) {
taintState = TaintState.UNKNOWN;
}
else {
taintState = taintState.join(variable.taintState());
}
}
return taintState;
return ContainerUtil.exists(myVariables, Variable::isField) ? TaintState.UNKNOWN :
myVariables.stream().findFirst().map(Variable::taintState).orElse(TaintState.SAFE);
}
private static @NotNull MethodBody generate(@NotNull ImperativeCommand.Environment env) {
@@ -392,14 +385,7 @@ public class SourceToSinkPropertyInspectionTest extends LightJavaCodeInsightFixt
@Override
public @NotNull TaintState taintState() {
if (isField()) return TaintState.UNKNOWN;
TaintState taintState = myDeclaration.taintState();
if (taintState == TaintState.TAINTED) return taintState;
for (Assignment assignment : myAssignments) {
TaintState assignmentState = assignment.taintState();
if (assignmentState == TaintState.TAINTED) return assignmentState;
if (taintState == TaintState.SAFE) taintState = assignmentState;
}
return taintState;
return myAssignments.stream().reduce((a, b) -> b).map(Assignment::taintState).orElse(myDeclaration.taintState());
}
public boolean isField() {