Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Claude.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ ast-eclipse-plugin/
| IDE Framework | Eclipse OSGi / RCP | 4.11+ |
| UI Toolkit | SWT / JFace | Bundled with Eclipse |
| Build System | Maven + Eclipse Tycho | Tycho 4.0.11 |
| Platform API | ast-cli-java-wrapper | 2.4.23 |
| Platform API | ast-cli-java-wrapper | dev (built from the wrapper `main` branch; has the severity-threshold overloads) |
| Event Bus | Google Guava | Bundled with Eclipse |
| Git Integration | JGit | Bundled with Eclipse |
| JSON | Jackson | 2.21.1 |
Expand Down
2 changes: 1 addition & 1 deletion ast-cli-java-wrapper.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.4.33
dev
Comment thread
cx-aniket-shinde marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package checkmarx.ast.eclipse.plugin.tests.unit.devassist.backend;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;

import com.checkmarx.eclipse.common.preferences.SeverityThreshold;
import com.checkmarx.eclipse.devassist.backend.SeverityThresholdRescanner;
import com.checkmarx.eclipse.devassist.model.ScanIssue;

/**
* Tests the pure decisions of the severity threshold change: which files are remembered for the rescan
* (taken BEFORE everything is cleared) and which earlier findings are put back when a rescan fails.
*/
class SeverityThresholdRescannerTest {

private static ScanIssue issue(String id, String severity) {
ScanIssue issue = new ScanIssue();
issue.setScanIssueId(id);
issue.setSeverity(severity);
return issue;
}

private static List<String> ids(List<ScanIssue> issues) {
List<String> ids = new ArrayList<>();
for (ScanIssue issue : issues) {
ids.add(issue.getScanIssueId());
}
return ids;
}

// ---- snapshotOfFilesWithFindings ----

@Test
void snapshotKeepsOnlyFilesThatHaveFindings() {
Map<String, List<ScanIssue>> all = new LinkedHashMap<>();
all.put("a.java", List.of(issue("1", "HIGH")));
all.put("empty.java", new ArrayList<>());
all.put("null.java", null);
all.put(null, List.of(issue("2", "HIGH")));

Map<String, List<ScanIssue>> snapshot = SeverityThresholdRescanner.snapshotOfFilesWithFindings(all);

assertEquals(List.of("a.java"), new ArrayList<>(snapshot.keySet()));
assertEquals(List.of("1"), ids(snapshot.get("a.java")));
}

@Test
void snapshotOfNullOrEmptyIsEmpty() {
assertTrue(SeverityThresholdRescanner.snapshotOfFilesWithFindings(null).isEmpty());
assertTrue(SeverityThresholdRescanner.snapshotOfFilesWithFindings(new LinkedHashMap<>()).isEmpty());
}

@Test
void snapshotIsNotAffectedWhenTheSourceIsClearedAfterwards() {
// The rescan list must survive clearing the results store.
List<ScanIssue> source = new ArrayList<>(List.of(issue("1", "HIGH"), issue("2", "LOW")));
Map<String, List<ScanIssue>> all = new LinkedHashMap<>();
all.put("a.java", source);

Map<String, List<ScanIssue>> snapshot = SeverityThresholdRescanner.snapshotOfFilesWithFindings(all);
source.clear();
all.clear();

assertEquals(List.of("a.java"), new ArrayList<>(snapshot.keySet()));
assertEquals(List.of("1", "2"), ids(snapshot.get("a.java")));
assertNotSame(source, snapshot.get("a.java"));
}

@Test
void snapshotKeepsFileOrder() {
Map<String, List<ScanIssue>> all = new LinkedHashMap<>();
all.put("c.java", List.of(issue("3", "HIGH")));
all.put("a.java", List.of(issue("1", "HIGH")));
all.put("b.java", List.of(issue("2", "HIGH")));

assertEquals(Arrays.asList("c.java", "a.java", "b.java"),
new ArrayList<>(SeverityThresholdRescanner.snapshotOfFilesWithFindings(all).keySet()));
}

// ---- findingsStillShownAt ----

@Test
void highThresholdKeepsCriticalAndHighOnly() {
List<ScanIssue> previous = List.of(issue("c", "CRITICAL"), issue("h", "HIGH"), issue("m", "MEDIUM"),
issue("l", "LOW"));

assertEquals(List.of("c", "h"), ids(SeverityThresholdRescanner.findingsStillShownAt(previous,
SeverityThreshold.HIGH)));
}

@Test
void criticalThresholdKeepsCriticalOnly() {
List<ScanIssue> previous = List.of(issue("c", "CRITICAL"), issue("h", "HIGH"));

assertEquals(List.of("c"), ids(SeverityThresholdRescanner.findingsStillShownAt(previous,
SeverityThreshold.CRITICAL)));
}

@Test
void lowThresholdKeepsEverythingExceptInfo() {
List<ScanIssue> previous = List.of(issue("c", "CRITICAL"), issue("l", "LOW"), issue("i", "INFO"));

assertEquals(List.of("c", "l"), ids(SeverityThresholdRescanner.findingsStillShownAt(previous,
SeverityThreshold.LOW)));
}

@Test
void maliciousOkAndUnknownAreAlwaysKept() {
List<ScanIssue> previous = List.of(issue("m", "MALICIOUS"), issue("o", "OK"), issue("u", "UNKNOWN"),
issue("l", "LOW"));

assertEquals(List.of("m", "o", "u"), ids(SeverityThresholdRescanner.findingsStillShownAt(previous,
SeverityThreshold.CRITICAL)));
}

@Test
void severityMatchIsCaseInsensitive() {
List<ScanIssue> previous = List.of(issue("h", "high"), issue("m", "Medium"));

assertEquals(List.of("h"), ids(SeverityThresholdRescanner.findingsStillShownAt(previous,
SeverityThreshold.HIGH)));
}

@Test
void missingSeverityIsKept() {
List<ScanIssue> previous = List.of(issue("n", null), issue("b", " "));

assertEquals(List.of("n", "b"), ids(SeverityThresholdRescanner.findingsStillShownAt(previous,
SeverityThreshold.CRITICAL)));
}

@Test
void nullInputsGiveNothing() {
assertTrue(SeverityThresholdRescanner.findingsStillShownAt(null, SeverityThreshold.HIGH).isEmpty());
assertTrue(SeverityThresholdRescanner.findingsStillShownAt(List.of(issue("h", "HIGH")), null).isEmpty());
}

@Test
void nullEntriesAreSkipped() {
List<ScanIssue> previous = new ArrayList<>();
previous.add(null);
previous.add(issue("h", "HIGH"));

assertEquals(List.of("h"), ids(SeverityThresholdRescanner.findingsStillShownAt(previous,
SeverityThreshold.HIGH)));
}

@Test
void resultIsACopy() {
List<ScanIssue> previous = new ArrayList<>(List.of(issue("h", "HIGH")));

List<ScanIssue> kept = SeverityThresholdRescanner.findingsStillShownAt(previous, SeverityThreshold.HIGH);
previous.clear();

assertEquals(List.of("h"), ids(kept));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package checkmarx.ast.eclipse.plugin.tests.unit.properties;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;

import org.junit.jupiter.api.Test;

import com.checkmarx.eclipse.common.preferences.SeverityThreshold;

class SeverityThresholdTest {

@Test
void defaultIsHigh() {
assertSame(SeverityThreshold.HIGH, SeverityThreshold.DEFAULT);
}

@Test
void levelsAreOrderedFromCriticalToLow() {
assertEquals(List.of(SeverityThreshold.CRITICAL, SeverityThreshold.HIGH, SeverityThreshold.MEDIUM,
SeverityThreshold.LOW), List.of(SeverityThreshold.values()));
}

@Test
void cliValueIsLowerCase() {
assertEquals("critical", SeverityThreshold.CRITICAL.getCliValue());
assertEquals("high", SeverityThreshold.HIGH.getCliValue());
assertEquals("medium", SeverityThreshold.MEDIUM.getCliValue());
assertEquals("low", SeverityThreshold.LOW.getCliValue());
}

@Test
void labelsAreDisplayNames() {
assertEquals("Critical", SeverityThreshold.CRITICAL.getLabel());
assertEquals("High", SeverityThreshold.HIGH.toString());
}

@Test
void fromValueIsCaseInsensitiveAndTrims() {
assertSame(SeverityThreshold.MEDIUM, SeverityThreshold.fromValue("medium"));
assertSame(SeverityThreshold.MEDIUM, SeverityThreshold.fromValue("MEDIUM"));
assertSame(SeverityThreshold.LOW, SeverityThreshold.fromValue(" Low "));
}

@Test
void fromValueFallsBackToDefaultForMissingOrInvalid() {
assertSame(SeverityThreshold.DEFAULT, SeverityThreshold.fromValue(null));
assertSame(SeverityThreshold.DEFAULT, SeverityThreshold.fromValue(""));
assertSame(SeverityThreshold.DEFAULT, SeverityThreshold.fromValue(" "));
assertSame(SeverityThreshold.DEFAULT, SeverityThreshold.fromValue("not-a-level"));
assertSame(SeverityThreshold.DEFAULT, SeverityThreshold.fromValue("malicious"));
}

@Test
void fromLabelMatchesDropDownEntries() {
for (SeverityThreshold level : SeverityThreshold.values()) {
assertSame(level, SeverityThreshold.fromLabel(level.getLabel()));
}
assertSame(SeverityThreshold.DEFAULT, SeverityThreshold.fromLabel(""));
}

@Test
void includedLabelsListThisLevelAndEverythingMoreSevere() {
assertEquals(List.of("Critical"), SeverityThreshold.CRITICAL.getIncludedLabels());
assertEquals(List.of("Critical", "High"), SeverityThreshold.HIGH.getIncludedLabels());
assertEquals(List.of("Critical", "High", "Medium", "Low"), SeverityThreshold.LOW.getIncludedLabels());
}

@Test
void highReportsCriticalAndHighOnly() {
SeverityThreshold high = SeverityThreshold.HIGH;
assertTrue(high.includes("CRITICAL"));
assertTrue(high.includes("High"));
assertFalse(high.includes("medium"));
assertFalse(high.includes("LOW"));
assertFalse(high.includes("Info"));
}

@Test
void lowReportsEverythingExceptInfo() {
for (String severity : new String[] { "critical", "high", "medium", "low" }) {
assertTrue(SeverityThreshold.LOW.includes(severity), severity);
}
assertFalse(SeverityThreshold.LOW.includes("info"));
}

@Test
void criticalReportsOnlyCritical() {
assertTrue(SeverityThreshold.CRITICAL.includes("critical"));
assertFalse(SeverityThreshold.CRITICAL.includes("high"));
}

@Test
void missingOrUnrecognisedSeverityIsAlwaysIncluded() {
// Malicious, OK and Unknown are not threshold levels, so the threshold never hides them.
for (SeverityThreshold level : SeverityThreshold.values()) {
assertTrue(level.includes(null), level + " null");
assertTrue(level.includes(""), level + " empty");
assertTrue(level.includes(" "), level + " blank");
assertTrue(level.includes("MALICIOUS"), level + " malicious");
assertTrue(level.includes("OK"), level + " ok");
assertTrue(level.includes("UNKNOWN"), level + " unknown");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;

import com.checkmarx.ast.asca.ScanResult;
import com.checkmarx.ast.containersrealtime.ContainersRealtimeResults;
import com.checkmarx.ast.iacrealtime.IacRealtimeResults;
import com.checkmarx.ast.ossrealtime.OssRealtimeResults;
import com.checkmarx.ast.project.Project;
import com.checkmarx.ast.secretsrealtime.SecretsRealtimeResults;
import com.checkmarx.ast.wrapper.CxWrapper;
import com.checkmarx.eclipse.common.wrapper.WrapperProvider;

Expand Down Expand Up @@ -60,4 +65,71 @@ void testTriageGetStates_propagatesExceptionFromWrapper() throws Exception {
assertThrows(RuntimeException.class, () -> wrapperProvider.triageGetStates(false));
}
}

// ---- Severity threshold overloads: the threshold must reach the CxWrapper call --------------

@Test
void testOssRealtimeScan_withThreshold_forwardsThresholdToWrapper() throws Exception {
OssRealtimeResults expected = mock(OssRealtimeResults.class);
try (MockedConstruction<CxWrapper> mocked = mockConstruction(CxWrapper.class,
(mock, context) -> when(mock.ossRealtimeScan("pom.xml", "ignore.json", "high")).thenReturn(expected))) {

assertSame(expected, wrapperProvider.ossRealtimeScan("pom.xml", "ignore.json", "high"));
}
}

@Test
void testSecretsRealtimeScan_withThreshold_forwardsThresholdToWrapper() throws Exception {
SecretsRealtimeResults expected = mock(SecretsRealtimeResults.class);
try (MockedConstruction<CxWrapper> mocked = mockConstruction(CxWrapper.class,
(mock, context) -> when(mock.secretsRealtimeScan("a.txt", "ignore.json", "medium")).thenReturn(expected))) {

assertSame(expected, wrapperProvider.secretsRealtimeScan("a.txt", "ignore.json", "medium"));
}
}

@Test
void testContainersRealtimeScan_withThreshold_forwardsThresholdToWrapper() throws Exception {
ContainersRealtimeResults expected = mock(ContainersRealtimeResults.class);
try (MockedConstruction<CxWrapper> mocked = mockConstruction(CxWrapper.class,
(mock, context) -> when(mock.containersRealtimeScan("Dockerfile", "ignore.json", "low")).thenReturn(expected))) {

assertSame(expected, wrapperProvider.containersRealtimeScan("Dockerfile", "ignore.json", "low"));
}
}

@Test
void testIacRealtimeScan_withThreshold_forwardsThresholdToWrapper() throws Exception {
IacRealtimeResults expected = mock(IacRealtimeResults.class);
try (MockedConstruction<CxWrapper> mocked = mockConstruction(CxWrapper.class,
(mock, context) -> when(mock.iacRealtimeScan("main.tf", "docker", "ignore.json", "info")).thenReturn(expected))) {

assertSame(expected, wrapperProvider.iacRealtimeScan("main.tf", "docker", "ignore.json", "info"));
}
}

@Test
void testScanAsca_withThreshold_forwardsThresholdToWrapper() throws Exception {
ScanResult expected = mock(ScanResult.class);
try (MockedConstruction<CxWrapper> mocked = mockConstruction(CxWrapper.class,
(mock, context) -> when(mock.ScanAsca("A.java", true, "Eclipse", null, "critical")).thenReturn(expected))) {

assertSame(expected, wrapperProvider.scanAsca("A.java", true, "Eclipse", null, "critical"));
}
}

@Test
void testRealtimeScans_withoutThreshold_keepCallingTheOriginalWrapperMethods() throws Exception {
// Existing callers (for example the ignore-upkeep scans) must keep using the unfiltered methods.
OssRealtimeResults oss = mock(OssRealtimeResults.class);
ScanResult asca = mock(ScanResult.class);
try (MockedConstruction<CxWrapper> mocked = mockConstruction(CxWrapper.class, (mock, context) -> {
when(mock.ossRealtimeScan("pom.xml", "")).thenReturn(oss);
when(mock.ScanAsca("A.java", true, "Eclipse", null)).thenReturn(asca);
})) {

assertSame(oss, wrapperProvider.ossRealtimeScan("pom.xml", ""));
assertSame(asca, wrapperProvider.scanAsca("A.java", true, "Eclipse", null));
}
}
}
2 changes: 1 addition & 1 deletion checkmarx-ast-eclipse-plugin/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="lib" path="../common-lib/lib/ast-cli-java-wrapper-2.4.33.jar"/>
<classpathentry exported="true" kind="lib" path="../common-lib/lib/ast-cli-java-wrapper-dev.jar"/>
<classpathentry kind="lib" path="../common-lib/lib/commons-lang3-3.18.0.jar"/>
<classpathentry kind="lib" path="../common-lib/lib/jackson-core-2.22.3.jar"/>
<classpathentry kind="lib" path="../common-lib/lib/jackson-databind-2.22.3.jar"/>
Expand Down
2 changes: 1 addition & 1 deletion common-lib/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</classpathentry>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="lib" path="lib/ast-cli-java-wrapper-2.4.33.jar"/>
<classpathentry exported="true" kind="lib" path="lib/ast-cli-java-wrapper-dev.jar"/>
<classpathentry kind="lib" path="lib/commons-lang3-3.18.0.jar"/>
<classpathentry kind="lib" path="lib/jackson-annotations-2.21.jar"/>
<classpathentry kind="lib" path="lib/slf4j-api-2.0.17.jar"/>
Expand Down
2 changes: 1 addition & 1 deletion common-lib/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Bundle-SymbolicName: com.checkmarx.eclipse.common
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: Checkmarx
Bundle-ClassPath: .,
lib/ast-cli-java-wrapper-2.4.33.jar,
lib/ast-cli-java-wrapper-dev.jar,
lib/jackson-core-2.22.3.jar,
lib/jackson-databind-2.22.3.jar,
lib/jackson-annotations-2.21.jar,
Expand Down
Loading
Loading