|
1 | 1 | package fi.solita.clamav; |
2 | 2 |
|
3 | | -import static org.junit.Assert.assertFalse; |
4 | | -import static org.junit.Assert.assertTrue; |
| 3 | +import org.junit.jupiter.api.Assertions; |
| 4 | +import org.junit.jupiter.api.Disabled; |
| 5 | +import org.junit.jupiter.params.ParameterizedTest; |
| 6 | +import org.junit.jupiter.params.provider.MethodSource; |
| 7 | +import org.testcontainers.containers.GenericContainer; |
5 | 8 |
|
6 | 9 | import java.io.IOException; |
7 | | -import java.io.InputStream; |
8 | | -import java.net.UnknownHostException; |
| 10 | +import java.nio.charset.StandardCharsets; |
9 | 11 |
|
10 | | -import org.junit.Test; |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
11 | 15 |
|
12 | | -/** |
13 | | - * These tests assume clamd is running and responding in the virtual machine. |
14 | | - */ |
15 | | -public class InstreamTest { |
| 16 | +public class InstreamTest extends BaseTestcontainers { |
| 17 | + @ParameterizedTest(autoCloseArguments = false) |
| 18 | + @MethodSource("provideClamdContainers") |
| 19 | + public void testRandomBytes(String clamdVersion, GenericContainer<?> container) throws IOException { |
| 20 | + byte[] r = client(container).scan("alsdklaksdla".getBytes(StandardCharsets.US_ASCII)); |
| 21 | + assertTrue(ClamAVClient.isCleanReply(r)); |
| 22 | + } |
16 | 23 |
|
17 | | - private static String CLAMAV_HOST = "localhost"; |
18 | | - |
19 | | - private byte[] scan(byte[] input) throws UnknownHostException, IOException { |
20 | | - ClamAVClient cl = new ClamAVClient(CLAMAV_HOST, 3310); |
21 | | - return cl.scan(input); |
22 | | - } |
23 | | - |
24 | | - private byte[] scan(InputStream input) throws UnknownHostException, IOException { |
25 | | - ClamAVClient cl = new ClamAVClient(CLAMAV_HOST, 3310); |
26 | | - return cl.scan(input); |
27 | | - } |
28 | | - @Test |
29 | | - public void testRandomBytes() throws UnknownHostException, IOException { |
30 | | - byte[] r = scan("alsdklaksdla".getBytes("ASCII")); |
31 | | - assertTrue(ClamAVClient.isCleanReply(r)); |
32 | | - } |
33 | | - |
34 | | - @Test |
35 | | - public void testPositive() throws UnknownHostException, IOException { |
36 | | - // http://www.eicar.org/86-0-Intended-use.html |
37 | | - byte[] EICAR = "X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*".getBytes("ASCII"); |
38 | | - byte[] r = scan(EICAR); |
39 | | - assertFalse(ClamAVClient.isCleanReply(r)); |
40 | | - } |
| 24 | + @ParameterizedTest(autoCloseArguments = false) |
| 25 | + @MethodSource("provideClamdContainers") |
| 26 | + public void testPositive(String clamdVersion, GenericContainer<?> container) throws IOException { |
| 27 | + // http://www.eicar.org/86-0-Intended-use.html |
| 28 | + byte[] EICAR = "X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*".getBytes(StandardCharsets.US_ASCII); |
| 29 | + byte[] r = client(container).scan(EICAR); |
| 30 | + assertFalse(ClamAVClient.isCleanReply(r)); |
| 31 | + } |
41 | 32 |
|
42 | | - @Test |
43 | | - public void testStreamChunkingWorks() throws UnknownHostException, IOException { |
44 | | - byte[] multipleChunks = new byte[50000]; |
45 | | - byte[] r = scan(multipleChunks); |
46 | | - assertTrue(ClamAVClient.isCleanReply(r)); |
47 | | - } |
48 | | - |
49 | | - @Test |
50 | | - public void testChunkLimit() throws UnknownHostException, IOException { |
51 | | - byte[] maximumChunk = new byte[2048]; |
52 | | - byte[] r = scan(maximumChunk); |
53 | | - assertTrue(ClamAVClient.isCleanReply(r)); |
54 | | - } |
55 | | - |
56 | | - @Test |
57 | | - public void testZeroBytes() throws UnknownHostException, IOException { |
58 | | - byte[] r = scan(new byte[]{}); |
59 | | - assertTrue(ClamAVClient.isCleanReply(r)); |
60 | | - } |
| 33 | + @ParameterizedTest(autoCloseArguments = false) |
| 34 | + @MethodSource("provideClamdContainers") |
| 35 | + public void testStreamChunkingWorks(String clamdVersion, GenericContainer<?> container) throws IOException { |
| 36 | + byte[] multipleChunks = new byte[50000]; |
| 37 | + byte[] r = client(container).scan(multipleChunks); |
| 38 | + assertTrue(ClamAVClient.isCleanReply(r)); |
| 39 | + } |
61 | 40 |
|
62 | | - @Test(expected = ClamAVSizeLimitException.class) |
63 | | - public void testSizeLimit() throws UnknownHostException, IOException { |
64 | | - scan(new SlowInputStream()); |
65 | | - } |
| 41 | + @ParameterizedTest(autoCloseArguments = false) |
| 42 | + @MethodSource("provideClamdContainers") |
| 43 | + public void testChunkLimit(String clamdVersion, GenericContainer<?> container) throws IOException { |
| 44 | + byte[] maximumChunk = new byte[2048]; |
| 45 | + byte[] r = client(container).scan(maximumChunk); |
| 46 | + assertTrue(ClamAVClient.isCleanReply(r)); |
| 47 | + } |
| 48 | + |
| 49 | + @ParameterizedTest(autoCloseArguments = false) |
| 50 | + @MethodSource("provideClamdContainers") |
| 51 | + public void testZeroBytes(String clamdVersion, GenericContainer<?> container) throws IOException { |
| 52 | + byte[] r = client(container).scan(new byte[]{}); |
| 53 | + assertTrue(ClamAVClient.isCleanReply(r)); |
| 54 | + } |
| 55 | + |
| 56 | + @ParameterizedTest(autoCloseArguments = false) |
| 57 | + @MethodSource("provideClamdContainers") |
| 58 | + public void testSizeLimit(String clamdVersion, GenericContainer<?> container) { |
| 59 | + ClamAVSizeLimitException exception = Assertions.assertThrows(ClamAVSizeLimitException.class, |
| 60 | + () -> client(container).scan(new SlowInputStream(false))); |
| 61 | + assertEquals("Clamd size limit exceeded. Full reply from server: INSTREAM size limit exceeded. ERROR", exception.getMessage()); |
| 62 | + } |
| 63 | + |
| 64 | + @Disabled |
| 65 | + @ParameterizedTest(autoCloseArguments = false) |
| 66 | + @MethodSource("provideClamdContainers") |
| 67 | + public void testSizeLimitSuperSlow(String clamdVersion, GenericContainer<?> container) { |
| 68 | + ClamAVSizeLimitException exception = Assertions.assertThrows(ClamAVSizeLimitException.class, |
| 69 | + () -> client(container).scan(new SlowInputStream(true))); |
| 70 | + assertEquals("Clamd size limit exceeded. Full reply from server: INSTREAM size limit exceeded. ERROR", exception.getMessage()); |
| 71 | + } |
66 | 72 | } |
0 commit comments