From 4002c927f50a460f5d5e29d1d82b94e7e75c42e5 Mon Sep 17 00:00:00 2001 From: Diogo Pimenta Date: Fri, 5 Nov 2021 15:47:51 +0000 Subject: [PATCH 1/2] AST-6032 add a thin wrapper for usage in light plugins e.g. maven --- .../checkmarx/ast/wrapper/CxThinWrapper.java | 36 +++++++++++++++++++ .../com/checkmarx/ast/ThinWrapperTest.java | 31 ++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/main/java/com/checkmarx/ast/wrapper/CxThinWrapper.java create mode 100644 src/test/java/com/checkmarx/ast/ThinWrapperTest.java diff --git a/src/main/java/com/checkmarx/ast/wrapper/CxThinWrapper.java b/src/main/java/com/checkmarx/ast/wrapper/CxThinWrapper.java new file mode 100644 index 00000000..e6eb7521 --- /dev/null +++ b/src/main/java/com/checkmarx/ast/wrapper/CxThinWrapper.java @@ -0,0 +1,36 @@ +package com.checkmarx.ast.wrapper; + +import lombok.NonNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class CxThinWrapper { + + @NonNull + private final Logger logger; + @NonNull + private final String executable; + + public CxThinWrapper() throws IOException { + this(LoggerFactory.getLogger(CxWrapper.class)); + } + + public CxThinWrapper(@NonNull Logger logger) throws IOException { + this.logger = logger; + this.executable = Execution.getTempBinary(); + this.logger.info("using executable: " + executable); + } + + public String run(@NonNull String arguments) throws CxException, IOException, InterruptedException { + this.logger.info("executing thin wrapper command"); + List argv = new ArrayList<>(); + argv.add(executable); + argv.addAll(Arrays.asList(arguments.split(" "))); + return Execution.executeCommand(argv, logger, (line) -> line); + } +} diff --git a/src/test/java/com/checkmarx/ast/ThinWrapperTest.java b/src/test/java/com/checkmarx/ast/ThinWrapperTest.java new file mode 100644 index 00000000..4d6fefe2 --- /dev/null +++ b/src/test/java/com/checkmarx/ast/ThinWrapperTest.java @@ -0,0 +1,31 @@ +package com.checkmarx.ast; + +import com.checkmarx.ast.scan.Scan; +import com.checkmarx.ast.wrapper.CxException; +import com.checkmarx.ast.wrapper.CxThinWrapper; +import lombok.SneakyThrows; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class ThinWrapperTest extends BaseTest { + + @SneakyThrows + @Test + public void testThinWrapper() { + CxThinWrapper wrapper = new CxThinWrapper(getLogger()); + String result = wrapper.run("scan list --format json --filter limit=10"); + List scanList = Scan.listFromLine(result); + Assert.assertTrue(scanList.size() <= 10); + } + + @SneakyThrows + @Test + public void testThinWrapperFail() { + CxThinWrapper wrapper = new CxThinWrapper(getLogger()); + String arguments = "scan create -s . --project-name thin-wrapper-test --sast-preset invalid-preset"; + CxException e = Assert.assertThrows(CxException.class, () -> wrapper.run(arguments)); + Assert.assertTrue(e.getMessage().contains("--sast-preset")); + } +} \ No newline at end of file From 50a1e456527407e40f0996b2338cd100a1349984 Mon Sep 17 00:00:00 2001 From: Diogo Pimenta Date: Fri, 5 Nov 2021 16:12:25 +0000 Subject: [PATCH 2/2] AST-6032 add newline --- src/test/java/com/checkmarx/ast/ThinWrapperTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/checkmarx/ast/ThinWrapperTest.java b/src/test/java/com/checkmarx/ast/ThinWrapperTest.java index 4d6fefe2..bda68f53 100644 --- a/src/test/java/com/checkmarx/ast/ThinWrapperTest.java +++ b/src/test/java/com/checkmarx/ast/ThinWrapperTest.java @@ -28,4 +28,4 @@ public void testThinWrapperFail() { CxException e = Assert.assertThrows(CxException.class, () -> wrapper.run(arguments)); Assert.assertTrue(e.getMessage().contains("--sast-preset")); } -} \ No newline at end of file +}