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
28 changes: 28 additions & 0 deletions src/test/java/com/checkmarx/ast/auth/AuthTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.checkmarx.ast.auth;

import com.checkmarx.ast.BaseTest;
import com.checkmarx.ast.wrapper.CxConfig;
import com.checkmarx.ast.wrapper.CxConstants;
import com.checkmarx.ast.wrapper.CxException;
import com.checkmarx.ast.wrapper.CxWrapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;

class AuthTest extends BaseTest {
@Test
void testAuthValidate() throws CxException, IOException, InterruptedException {
Assertions.assertNotNull(wrapper.authValidate());
}
//
@Test
void testAuthFailure() {
CxConfig cxConfig = getConfig();
cxConfig.setBaseAuthUri("wrongAuth");
cxConfig.setApiKey("InvalidApiKey");
Assertions.assertThrows(CxException.class, () -> new CxWrapper(cxConfig, getLogger()).authValidate());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package com.checkmarx.ast.containersrealtime;

import com.checkmarx.ast.realtime.RealtimeLocation;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@DisplayName("ContainersRealtimeImage")
class ContainersRealtimeImageTest {

@Test
@DisplayName("ContainersRealtimeImage is a POJO with Lombok @Value")
void testContainersRealtimeImageExists() {
// ContainersRealtimeImage uses @Value with complex constructor
// Test class existence and basic contract
assertNotNull(ContainersRealtimeImage.class);
assertTrue(ContainersRealtimeImage.class.getSimpleName().contains("ContainersRealtimeImage"));
}

@Test
@DisplayName("Constructor with all parameters creates valid instance")
void testConstructor_WithAllParameters() {
List<RealtimeLocation> locations = Arrays.asList(new RealtimeLocation(1, 0, 10));
List<ContainersRealtimeVulnerability> vulns = Arrays.asList(
new ContainersRealtimeVulnerability("CVE-2021-1", "high")
);

ContainersRealtimeImage img = new ContainersRealtimeImage(
"nginx:latest", "1.0", "/app/Dockerfile", locations, "VULNERABLE", vulns
);

assertNotNull(img);
assertEquals("nginx:latest", img.getImageName());
assertEquals("1.0", img.getImageTag());
assertEquals("/app/Dockerfile", img.getFilePath());
assertEquals("VULNERABLE", img.getStatus());
assertEquals(1, img.getLocations().size());
assertEquals(1, img.getVulnerabilities().size());
}

@Test
@DisplayName("Constructor with null locations converts to empty list")
void testConstructor_NullLocations_CreatesEmptyList() {
ContainersRealtimeImage img = new ContainersRealtimeImage(
"ubuntu:20.04", "2.0", "Dockerfile", null, "SAFE", new ArrayList<>()
);

assertNotNull(img.getLocations());
assertEquals(0, img.getLocations().size());
}

@Test
@DisplayName("Constructor with null vulnerabilities converts to empty list")
void testConstructor_NullVulnerabilities_CreatesEmptyList() {
ContainersRealtimeImage img = new ContainersRealtimeImage(
"alpine:3.12", "3.0", "Dockerfile", new ArrayList<>(), "SAFE", null
);

assertNotNull(img.getVulnerabilities());
assertEquals(0, img.getVulnerabilities().size());
}

@Test
@DisplayName("Constructor with both null collections")
void testConstructor_BothNull_CreatesEmptyCollections() {
ContainersRealtimeImage img = new ContainersRealtimeImage(
"centos:8", "4.0", "Dockerfile", null, "SAFE", null
);

assertEquals(0, img.getLocations().size());
assertEquals(0, img.getVulnerabilities().size());
}

@Test
@DisplayName("equals returns true for identical images")
void testEquals_IdenticalImages_ReturnsTrue() {
ContainersRealtimeImage img1 = new ContainersRealtimeImage(
"nginx:latest", "1.0", "/app/Dockerfile", new ArrayList<>(), "VULNERABLE", new ArrayList<>()
);
ContainersRealtimeImage img2 = new ContainersRealtimeImage(
"nginx:latest", "1.0", "/app/Dockerfile", new ArrayList<>(), "VULNERABLE", new ArrayList<>()
);

assertEquals(img1, img2);
}

@Test
@DisplayName("equals returns false when imageName differs")
void testEquals_DifferentImageName_ReturnsFalse() {
ContainersRealtimeImage img1 = new ContainersRealtimeImage(
"nginx:latest", "1.0", "/app/Dockerfile", null, "VULNERABLE", null
);
ContainersRealtimeImage img2 = new ContainersRealtimeImage(
"apache:latest", "1.0", "/app/Dockerfile", null, "VULNERABLE", null
);

assertNotEquals(img1, img2);
}

@Test
@DisplayName("equals returns false when imageTag differs")
void testEquals_DifferentImageTag_ReturnsFalse() {
ContainersRealtimeImage img1 = new ContainersRealtimeImage(
"nginx:latest", "1.0", "/app/Dockerfile", null, "VULNERABLE", null
);
ContainersRealtimeImage img2 = new ContainersRealtimeImage(
"nginx:latest", "2.0", "/app/Dockerfile", null, "VULNERABLE", null
);

assertNotEquals(img1, img2);
}

@Test
@DisplayName("equals returns false when filePath differs")
void testEquals_DifferentFilePath_ReturnsFalse() {
ContainersRealtimeImage img1 = new ContainersRealtimeImage(
"nginx:latest", "1.0", "/app/Dockerfile", null, "VULNERABLE", null
);
ContainersRealtimeImage img2 = new ContainersRealtimeImage(
"nginx:latest", "1.0", "/docker/Dockerfile", null, "VULNERABLE", null
);

assertNotEquals(img1, img2);
}

@Test
@DisplayName("equals returns false when status differs")
void testEquals_DifferentStatus_ReturnsFalse() {
ContainersRealtimeImage img1 = new ContainersRealtimeImage(
"nginx:latest", "1.0", "/app/Dockerfile", null, "VULNERABLE", null
);
ContainersRealtimeImage img2 = new ContainersRealtimeImage(
"nginx:latest", "1.0", "/app/Dockerfile", null, "SAFE", null
);

assertNotEquals(img1, img2);
}

@Test
@DisplayName("hashCode is consistent for equal images")
void testHashCode_EqualImages_SameHash() {
ContainersRealtimeImage img1 = new ContainersRealtimeImage(
"nginx:latest", "1.0", "/app/Dockerfile", new ArrayList<>(), "VULNERABLE", new ArrayList<>()
);
ContainersRealtimeImage img2 = new ContainersRealtimeImage(
"nginx:latest", "1.0", "/app/Dockerfile", new ArrayList<>(), "VULNERABLE", new ArrayList<>()
);

assertEquals(img1.hashCode(), img2.hashCode());
}

@Test
@DisplayName("toString produces non-null string")
void testToString_ProducesNonNull() {
ContainersRealtimeImage img = new ContainersRealtimeImage(
"nginx:latest", "1.0", "/app/Dockerfile", null, "VULNERABLE", null
);

assertNotNull(img.toString());
assertFalse(img.toString().isEmpty());
}

@Test
@DisplayName("equals with null returns false")
void testEquals_WithNull_ReturnsFalse() {
ContainersRealtimeImage img = new ContainersRealtimeImage(
"nginx:latest", "1.0", "/app/Dockerfile", null, "VULNERABLE", null
);

assertFalse(img.equals(null));
}

@Test
@DisplayName("equals with different type returns false")
void testEquals_DifferentType_ReturnsFalse() {
ContainersRealtimeImage img = new ContainersRealtimeImage(
"nginx:latest", "1.0", "/app/Dockerfile", null, "VULNERABLE", null
);

assertFalse(img.equals("not an image"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package com.checkmarx.ast.containersrealtime;

import com.checkmarx.ast.realtime.RealtimeLocation;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static org.junit.jupiter.api.Assertions.*;

class ContainersRealtimeParsingUnitTest {

// --- ContainersRealtimeResults.fromLine ---

@Test
void testFromLineWithValidImagesJson() {
String json = "{\"Images\": [{" +
" \"ImageName\": \"nginx\"," +
" \"ImageTag\": \"1.21\"," +
" \"FilePath\": \"/Dockerfile\"," +
" \"Status\": \"vulnerable\"" +
"}]}";
ContainersRealtimeResults results = ContainersRealtimeResults.fromLine(json);
assertNotNull(results);
assertNotNull(results.getImages());
assertEquals(1, results.getImages().size());
ContainersRealtimeImage img = results.getImages().get(0);
assertEquals("nginx", img.getImageName());
assertEquals("1.21", img.getImageTag());
assertEquals("/Dockerfile", img.getFilePath());
assertEquals("vulnerable", img.getStatus());
}

@Test
void testFromLineWithJsonWithoutImagesKey() {
// Valid JSON but no "Images" key — contains check fails → null
assertNull(ContainersRealtimeResults.fromLine("{\"Other\": []}"));
assertNull(ContainersRealtimeResults.fromLine("[{\"ImageName\": \"x\"}]"));
}

@Test
void testFromLineWithBlankAndNull() {
assertNull(ContainersRealtimeResults.fromLine(""));
assertNull(ContainersRealtimeResults.fromLine(" "));
assertNull(ContainersRealtimeResults.fromLine(null));
}

@Test
void testFromLineWithInvalidJson() {
assertNull(ContainersRealtimeResults.fromLine("{bad"));
assertNull(ContainersRealtimeResults.fromLine("[{]"));
}

@Test
void testFromLineWithImagesKeyButMalformedJson() {
// Contains "Images" (with quotes) so isValidJSON is called, but JSON is malformed →
// isValidJSON catches IOException and returns false → fromLine returns null.
// This covers the isValidJSON catch block (+3 instructions).
assertNull(ContainersRealtimeResults.fromLine("{\"Images\": not valid json}"));
}

@Test
void testFromLineWithEmptyImagesArray() {
ContainersRealtimeResults results = ContainersRealtimeResults.fromLine("{\"Images\": []}");
assertNotNull(results);
assertNotNull(results.getImages());
assertTrue(results.getImages().isEmpty());
}

@Test
void testFromLineWithMultipleImages() {
String json = "{\"Images\": [" +
" {\"ImageName\": \"img-a\", \"ImageTag\": \"1.0\"}," +
" {\"ImageName\": \"img-b\", \"ImageTag\": \"2.0\"}" +
"]}";
ContainersRealtimeResults results = ContainersRealtimeResults.fromLine(json);
assertNotNull(results);
assertEquals(2, results.getImages().size());
assertEquals("img-a", results.getImages().get(0).getImageName());
assertEquals("img-b", results.getImages().get(1).getImageName());
}

// --- ContainersRealtimeImage constructor ---

@Test
void testImageConstructorWithNullCollections() {
ContainersRealtimeImage img = new ContainersRealtimeImage(
"ubuntu", "20.04", "/Dockerfile", null, "ok", null);
assertEquals("ubuntu", img.getImageName());
assertEquals("20.04", img.getImageTag());
assertEquals("/Dockerfile", img.getFilePath());
assertEquals("ok", img.getStatus());
assertTrue(img.getLocations().isEmpty());
assertTrue(img.getVulnerabilities().isEmpty());
}

@Test
void testImageConstructorWithNonNullCollections() {
RealtimeLocation loc = new RealtimeLocation(1, 0, 5);
ContainersRealtimeVulnerability vuln = new ContainersRealtimeVulnerability(
"CVE-2023-1234", "High");
ContainersRealtimeImage img = new ContainersRealtimeImage(
"alpine", "3.14", "/Dockerfile",
Collections.singletonList(loc),
"vulnerable",
Collections.singletonList(vuln));
assertEquals(1, img.getLocations().size());
assertEquals(1, img.getLocations().get(0).getLine());
assertEquals(1, img.getVulnerabilities().size());
assertEquals("CVE-2023-1234", img.getVulnerabilities().get(0).getCve());
}

// --- ContainersRealtimeVulnerability constructor ---

@Test
void testVulnerabilityConstructorStoresAllFields() {
ContainersRealtimeVulnerability vuln = new ContainersRealtimeVulnerability(
"CVE-2022-5678", "Critical");
assertEquals("CVE-2022-5678", vuln.getCve());
assertEquals("Critical", vuln.getSeverity());
}

@Test
void testFromLineWithVulnerabilitiesInImage() {
String json = "{\"Images\": [{" +
" \"ImageName\": \"vuln-img\"," +
" \"ImageTag\": \"latest\"," +
" \"Vulnerabilities\": [{" +
" \"CVE\": \"CVE-2021-9876\"," +
" \"Severity\": \"Medium\"" +
" }]" +
"}]}";
ContainersRealtimeResults results = ContainersRealtimeResults.fromLine(json);
assertNotNull(results);
assertEquals(1, results.getImages().size());
assertEquals(1, results.getImages().get(0).getVulnerabilities().size());
ContainersRealtimeVulnerability vuln = results.getImages().get(0).getVulnerabilities().get(0);
assertEquals("CVE-2021-9876", vuln.getCve());
assertEquals("Medium", vuln.getSeverity());
}
}
Loading
Loading