Skip to content

Commit

Permalink
Share Vaas instance between tests. Make sure to close after the tests!
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Stadermann committed Sep 13, 2024
1 parent 6f0f48a commit ed30e1a
Showing 1 changed file with 44 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -19,6 +18,8 @@
import java.util.concurrent.TimeoutException;

import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
Expand All @@ -43,6 +44,18 @@ public class RealApiIntegrationTests {
private static final Dotenv dotenv = Dotenv.configure()
.ignoreIfMissing()
.load();
private static Vaas vaas;

@BeforeAll
public static void setUpAll() throws URISyntaxException, InterruptedException, IOException, ExecutionException,
TimeoutException, VaasAuthenticationException {
vaas = getVaasWithCredentials();
}

@AfterAll
public static void tearDownAll() throws Exception {
vaas.close();
}

@Test
public void clientCredentialsGrantAuthenticatorGetToken() throws Exception {
Expand All @@ -69,11 +82,9 @@ public void resourceOwnerPasswordAuthenticatorGetToken() throws Exception {

@Test
public void forSha256SingleMaliciousHash() throws Exception {
var vaas = this.getVaasWithCredentials();
var sha256 = new Sha256("ab5788279033b0a96f2d342e5f35159f103f69e0191dd391e036a1cd711791a2");

var verdict = vaas.forSha256(sha256);
vaas.disconnect();

assertEquals(Verdict.MALICIOUS, verdict.getVerdict());
assertTrue("ab5788279033b0a96f2d342e5f35159f103f69e0191dd391e036a1cd711791a2"
Expand All @@ -82,11 +93,9 @@ public void forSha256SingleMaliciousHash() throws Exception {

@Test
public void fromSha256SinglePupHash() throws Exception {
var vaas = this.getVaasWithCredentials();
var sha256 = new Sha256("d6f6c6b9fde37694e12b12009ad11ab9ec8dd0f193e7319c523933bdad8a50ad");

var verdict = vaas.forSha256(sha256);
vaas.disconnect();

assertEquals(Verdict.PUP, verdict.getVerdict());
assertTrue("d6f6c6b9fde37694e12b12009ad11ab9ec8dd0f193e7319c523933bdad8a50ad"
Expand All @@ -95,20 +104,21 @@ public void fromSha256SinglePupHash() throws Exception {

@Test
@Tag("ErrorLogProducer")
public void illegalCredentials() throws URISyntaxException {
public void illegalCredentials() throws Exception {
var clientId = "NON_EXISTING_CLIENT_ID";
var clientSecret = "A wizard is never late, Frodo Baggins. He arrives precisely when he means to!";
var tokenUrl = new URI(getEnvironmentKey("TOKEN_URL"));
var vaasUrl = getEnvironmentKey("VAAS_URL");
var config = new VaasConfig(new URI(vaasUrl));
var authenticator = new ClientCredentialsGrantAuthenticator(clientId, clientSecret, tokenUrl);
var client = new Vaas(config, authenticator);
assertThrows(Exception.class, client::connect);
try (var client = new Vaas(config, authenticator)) {
assertThrows(Exception.class, client::connect);
}
}

@Test
@Tag("ErrorLogProducer")
public void wrongTokenUsedToAuthenticateWebsocket() throws URISyntaxException {
public void wrongTokenUsedToAuthenticateWebsocket() throws Exception {
class MockAuthenticator implements IAuthenticator {

@Override
Expand All @@ -121,22 +131,20 @@ public String getToken() {
var config = new VaasConfig(new URI(vaasUrl));
var authenticator = new MockAuthenticator();

var client = new Vaas(config, authenticator);

assertThrows(VaasAuthenticationException.class, client::connect);
try (var client = new Vaas(config, authenticator)) {
assertThrows(VaasAuthenticationException.class, client::connect);
}
}

@Test
public void forSha256MultipleHashes() throws Exception {
var vaas = this.getVaasWithCredentials();
var sha256_1 = new Sha256("ab5788279033b0a96f2d342e5f35159f103f69e0191dd391e036a1cd711791a2");
var sha256_2 = new Sha256("cd617c5c1b1ff1c94a52ab8cf07192654f271a3f8bad49490288131ccb9efc1e");
var sha256_3 = new Sha256("1f72c1111111111111f912e40b7323a0192a300b376186c10f6803dc5efe28df");

var verdict_1 = vaas.forSha256(sha256_1);
var verdict_2 = vaas.forSha256(sha256_2);
var verdict_3 = vaas.forSha256(sha256_3);
vaas.disconnect();

assertEquals(Verdict.MALICIOUS, verdict_1.getVerdict());
assertEquals(Verdict.CLEAN, verdict_2.getVerdict());
Expand All @@ -152,15 +160,13 @@ public void forSha256MultipleHashes() throws Exception {

@Test
public void forSha256MultipleUnknownHash() throws Exception {
var vaas = this.getVaasWithCredentials();
var sha256_1 = new Sha256("110005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8");
var sha256_2 = new Sha256("11000b68934493af2f5954593fe8127b9dda6d4b520e78265aa5875623b58c9c");
var sha256_3 = new Sha256("11000f83e3120f79a21b7b395dd3dd6a9c31ce00857f78d7cf487476ca75fd1a");

var verdict_1 = vaas.forSha256(sha256_1);
var verdict_2 = vaas.forSha256(sha256_2);
var verdict_3 = vaas.forSha256(sha256_3);
vaas.disconnect();

assertEquals(Verdict.UNKNOWN, verdict_1.getVerdict());
assertEquals(Verdict.UNKNOWN, verdict_2.getVerdict());
Expand All @@ -178,11 +184,9 @@ public void forSha256MultipleUnknownHash() throws Exception {
public void forFileSingleMaliciousFile()
throws Exception {
var tmpFile = Sha256Test.writeEicar();
var vaas = this.getVaasWithCredentials();

var sha256 = new Sha256(tmpFile);
var verdict = vaas.forFile(tmpFile);
vaas.disconnect();

Files.deleteIfExists(tmpFile);
assertEquals(Verdict.MALICIOUS, verdict.getVerdict());
Expand All @@ -193,15 +197,13 @@ public void forFileSingleMaliciousFile()
public void forFileSingleMaliciousFileWithVerdictRequestAttributes()
throws Exception {
var tmpFile = Sha256Test.writeEicar();
var vaas = this.getVaasWithCredentials();

var sha256 = new Sha256(tmpFile);
var verdict = vaas.forFile(tmpFile, new VerdictRequestAttributes() {
{
setTenantId("JavaSDK");
}
});
vaas.disconnect();

Files.deleteIfExists(tmpFile);
assertEquals(Verdict.MALICIOUS, verdict.getVerdict());
Expand All @@ -214,11 +216,9 @@ public void forFileSingleCleanFile()
byte[] clean = { 0x65, 0x0a, 0x67, 0x0a, 0x65, 0x0a, 0x62, 0x0a };
var tmpFile = Path.of(System.getProperty("java.io.tmpdir"), "clean.txt");
Files.write(tmpFile, clean);
var vaas = this.getVaasWithCredentials();

var sha256 = new Sha256(tmpFile);
var verdict = vaas.forFile(tmpFile);
vaas.disconnect();

Files.deleteIfExists(tmpFile);
assertEquals(Verdict.CLEAN, verdict.getVerdict());
Expand All @@ -231,11 +231,9 @@ public void forFileSingleUnknownFile()
var unknown = getRandomString(50);
var tmpFile = Path.of(System.getProperty("java.io.tmpdir"), "unknown.txt");
Files.writeString(tmpFile, unknown);
var vaas = this.getVaasWithCredentials();

var sha256 = new Sha256(tmpFile);
var verdict = vaas.forFile(tmpFile);
vaas.disconnect();

Files.deleteIfExists(tmpFile);
assert (verdict != null);
Expand All @@ -250,11 +248,9 @@ public void forFileEmptyFile()
var tmpFile = Path.of(System.getProperty("java.io.tmpdir"), "empty.txt");

Files.write(tmpFile, clean);
var vaas = this.getVaasWithCredentials();

var sha256 = new Sha256(tmpFile);
var verdict = vaas.forFile(tmpFile);
vaas.disconnect();

Files.deleteIfExists(tmpFile);
assertEquals(Verdict.CLEAN, verdict.getVerdict());
Expand All @@ -264,7 +260,6 @@ public void forFileEmptyFile()
@Test
@Disabled("Enable to test keep-alive")
public void forFile_WorksWithBigSample() throws Exception {
var vaas = this.getVaasWithCredentials();
var verdict = vaas.forFile(Path.of("/home/vscode/big.zip"));
assert (verdict != null);
assertEquals(Verdict.MALICIOUS, verdict.getVerdict());
Expand All @@ -273,7 +268,6 @@ public void forFile_WorksWithBigSample() throws Exception {
@Test
@Disabled("Enable to test keep-alive")
public void forSha256_WorksAfter40s() throws Exception {
var vaas = this.getVaasWithCredentials();
var sha256 = new Sha256("3A78F382E8E2968EC201B33178102E06DB72E4F2D1505E058A4613C1E977825C");
var verdict = vaas.forSha256(sha256);
assert (verdict != null);
Expand All @@ -286,22 +280,24 @@ public void forSha256_WorksAfter40s() throws Exception {

@Test
public void forSha256_ThrowsConnectionClosed() throws Exception {
var vaas = this.getVaasWithCredentials();
vaas.disconnect();
var sha256 = new Sha256("3A78F382E8E2968EC201B33178102E06DB72E4F2D1505E058A4613C1E977825C");
assertThrows(VaasConnectionClosedException.class, () -> {
vaas.forSha256(sha256);
});
try (var vaas = getVaasWithCredentials()) {
vaas.disconnect();
var sha256 = new Sha256("3A78F382E8E2968EC201B33178102E06DB72E4F2D1505E058A4613C1E977825C");
assertThrows(VaasConnectionClosedException.class, () -> {
vaas.forSha256(sha256);
});
}
}

@Test
public void forSha256_VaasCloses() throws Exception {
var vaas = this.getVaasWithCredentials();
vaas.close();
var sha256 = new Sha256("3A78F382E8E2968EC201B33178102E06DB72E4F2D1505E058A4613C1E977825C");
assertThrows(VaasConnectionClosedException.class, () -> {
vaas.forSha256(sha256);
});
try (var vaas = getVaasWithCredentials()) {
vaas.close();
var sha256 = new Sha256("3A78F382E8E2968EC201B33178102E06DB72E4F2D1505E058A4613C1E977825C");
assertThrows(VaasConnectionClosedException.class, () -> {
vaas.forSha256(sha256);
});
}
}

@Test
Expand All @@ -313,24 +309,23 @@ public void forSha256_ConnectHasntBeCalled() throws Exception {

var authenticator = new ClientCredentialsGrantAuthenticator(clientId, clientSecret, tokenUrl);
var config = new VaasConfig(new URI(vaasUrl));
var vaas = new Vaas(config, authenticator);
var sha256 = new Sha256("3A78F382E8E2968EC201B33178102E06DB72E4F2D1505E058A4613C1E977825C");
assertThrows(VaasInvalidStateException.class, () -> {
vaas.forSha256(sha256);
});
try (var vaas = new Vaas(config, authenticator)) {
var sha256 = new Sha256("3A78F382E8E2968EC201B33178102E06DB72E4F2D1505E058A4613C1E977825C");
assertThrows(VaasInvalidStateException.class, () -> {
vaas.forSha256(sha256);
});
}
}

@Test
public void forUrlMultipleMaliciousUrls() throws Exception {
var vaas = this.getVaasWithCredentials();
var url_1 = new URL("https://secure.eicar.org/eicar.com");
var url_2 = new URL("https://secure.eicar.org/eicar.com.txt");
var url_3 = new URL("https://secure.eicar.org/eicar_com.zip");

var verdict_1 = vaas.forUrl(url_1);
var verdict_2 = vaas.forUrl(url_2);
var verdict_3 = vaas.forUrl(url_3);
vaas.disconnect();

assertEquals(Verdict.MALICIOUS, verdict_1.getVerdict());
assertEquals(Verdict.MALICIOUS, verdict_2.getVerdict());
Expand All @@ -340,7 +335,6 @@ public void forUrlMultipleMaliciousUrls() throws Exception {
@Test
@Tag("ErrorLogProducer")
public void forUrl_WithoutAuthority_ThrowsURISyntaxException() throws Exception {
var vaas = this.getVaasWithCredentials();
var url_1 = new URL("https://");
var e = assertThrows(URISyntaxException.class, () -> vaas.forUrl(url_1));
assertEquals(
Expand All @@ -350,7 +344,6 @@ public void forUrl_WithoutAuthority_ThrowsURISyntaxException() throws Exception

@Test
public void forUrl_WithUrlNull_ThrowsNullPointerException() throws Exception {
var vaas = this.getVaasWithCredentials();
@SuppressWarnings("DataFlowIssue")
var e = assertThrows(NullPointerException.class, () -> vaas.forUrl(null));
assertEquals("url is marked non-null but is null", e.getMessage());
Expand All @@ -359,7 +352,6 @@ public void forUrl_WithUrlNull_ThrowsNullPointerException() throws Exception {
@Test
@Tag("ErrorLogProducer")
public void forUrl_WithUrlWithStatusCode4xx_ThrowsVaasClientException() throws Exception {
var vaas = this.getVaasWithCredentials();
var url_1 = new URL("https://upload.production.vaas.gdatasecurity.de/nocontenthere");
var e = assertThrows(VaasClientException.class, () -> vaas.forUrl(url_1));
assertEquals(
Expand All @@ -374,7 +366,6 @@ public void forUrlInALoop() throws Exception {

while (true) {
try {
var vaas = this.getVaasWithCredentials();
while (true) {
var verdict_1 = vaas.forUrl(url_1);
assertEquals(Verdict.CLEAN, verdict_1.getVerdict());
Expand All @@ -395,7 +386,6 @@ public void forUrlInALoop() throws Exception {
@Test
@Tag("ErrorLogProducer")
public void forUrlMultipleCleanUrls() throws Exception {
var vaas = this.getVaasWithCredentials();
var url_1 = new URL("https://github.com/GDATASoftwareAG/vaas");
var url_2 = new URL("https://github.com/GDATASoftwareAG/vaas");
var url_3 = new URL("https://github.com/GDATASoftwareAG/vaas");
Expand All @@ -406,7 +396,6 @@ public void forUrlMultipleCleanUrls() throws Exception {
var verdict_1 = vaas.forUrl(url_1, verdictRequestAttributes);
var verdict_2 = vaas.forUrl(url_2, verdictRequestAttributes);
var verdict_3 = vaas.forUrl(url_3, verdictRequestAttributes);
vaas.disconnect();

assertEquals(Verdict.CLEAN, verdict_1.getVerdict());
assertEquals(Verdict.CLEAN, verdict_2.getVerdict());
Expand All @@ -427,15 +416,13 @@ public void serializationTest() {

@Test
public void forSha256_WithSha256Null_ThrowsNullPointerException() throws Exception {
var vaas = this.getVaasWithCredentials();
@SuppressWarnings("DataFlowIssue")
var e = assertThrows(NullPointerException.class, () -> vaas.forSha256(null));
assertEquals("sha256 is marked non-null but is null", e.getMessage());
}

@Test
public void forStream_WithCleanString_ReturnsCleanVerdict() throws Exception {
var vaas = this.getVaasWithCredentials();
var targetStream = new ByteArrayInputStream("I am clean".getBytes());
var contentLength = targetStream.available();
var verdict = vaas.forStream(targetStream, contentLength);
Expand All @@ -445,7 +432,6 @@ public void forStream_WithCleanString_ReturnsCleanVerdict() throws Exception {

@Test
public void forStream_WithEicarString_ReturnsMaliciousVerdict() throws Exception {
var vaas = this.getVaasWithCredentials();
var targetStream = new ByteArrayInputStream(
"X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*".getBytes());
var contentLength = targetStream.available();
Expand All @@ -461,7 +447,6 @@ public void forStream_WithCleanUrl_ReturnsCleanVerdict() throws Exception {
var inputStream = conn.getInputStream();
var contentLength = conn.getContentLengthLong();

var vaas = this.getVaasWithCredentials();
var verdict = vaas.forStream(inputStream, contentLength);

assertEquals(Verdict.CLEAN, verdict.getVerdict());
Expand All @@ -474,7 +459,6 @@ public void forStream_WithEicarUrl_ReturnsMaliciousVerdict() throws Exception {
var inputStream = conn.getInputStream();
var contentLength = conn.getContentLengthLong();

var vaas = this.getVaasWithCredentials();
var verdict = vaas.forStream(inputStream, contentLength);

assertEquals(Verdict.MALICIOUS, verdict.getVerdict());
Expand All @@ -492,7 +476,7 @@ public void forStream_WithEicarUrl_ReturnsMaliciousVerdict() throws Exception {
return sb.toString();
}

private Vaas getVaasWithCredentials()
private static Vaas getVaasWithCredentials()
throws URISyntaxException, InterruptedException, IOException, ExecutionException, TimeoutException,
VaasAuthenticationException {
var clientId = getEnvironmentKey("CLIENT_ID");
Expand All @@ -507,7 +491,7 @@ private Vaas getVaasWithCredentials()
return client;
}

private String getEnvironmentKey(String key) {
private static String getEnvironmentKey(String key) {
var value = dotenv.get(key);
if (value == null) {
throw new IllegalArgumentException("Environment variable " + key + " must be set.");
Expand All @@ -522,7 +506,6 @@ public void forStream_WithEicarFile_ReturnsMaliciousVerdictWithDetections() thro
var inputStream = conn.getInputStream();
var contentLength = conn.getContentLengthLong();

var vaas = this.getVaasWithCredentials();
var verdict = vaas.forStream(inputStream, contentLength);

assertNotNull(verdict.getDetection());
Expand Down

0 comments on commit ed30e1a

Please sign in to comment.