| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.sling.commons.clam.it.tests; |
| |
| import java.io.ByteArrayInputStream; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.nio.ByteBuffer; |
| import java.nio.charset.StandardCharsets; |
| import java.nio.file.Files; |
| import java.nio.file.Paths; |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| import javax.inject.Inject; |
| |
| import org.apache.commons.io.IOUtils; |
| import org.apache.sling.commons.clam.ClamService; |
| import org.apache.sling.commons.clam.ScanResult; |
| import org.apache.sling.commons.content.analyzing.ContentAnalyzer; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| import org.ops4j.pax.exam.junit.PaxExam; |
| import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; |
| import org.ops4j.pax.exam.spi.reactors.PerClass; |
| import org.ops4j.pax.exam.util.Filter; |
| import org.ops4j.pax.exam.util.PathUtils; |
| |
| import static org.junit.Assert.assertEquals; |
| import static org.junit.Assert.assertNotNull; |
| |
| @RunWith(PaxExam.class) |
| @ExamReactorStrategy(PerClass.class) |
| public class ClamdServiceIT extends ClamTestSupport { |
| |
| @Inject |
| private ClamService clamService; |
| |
| @Inject |
| @Filter(value = "(&(operation=malware detection)(provider=clamd))") |
| private ContentAnalyzer contentAnalyzer; |
| |
| private static final String INSTREAM_SIZE_LIMIT_EXCEEDED_ERROR_MESSAGE = "INSTREAM size limit exceeded. ERROR"; |
| |
| private byte[] readEicarFile() throws IOException { |
| final byte[] xaa = Files.readAllBytes(Paths.get(PathUtils.getBaseDir(), "src/test/resources/eicar/eicarcom2.zip/xaa")); |
| final byte[] xab = Files.readAllBytes(Paths.get(PathUtils.getBaseDir(), "src/test/resources/eicar/eicarcom2.zip/xab")); |
| return ByteBuffer.allocate(xaa.length + xab.length).put(xaa).put(xab).array(); |
| } |
| |
| // ClamService |
| |
| @Test |
| public void testClamService() { |
| assertNotNull(clamService); |
| } |
| |
| @Test |
| public void testScan_ok() throws Exception { |
| final String data = "ok – no malware here"; |
| try (final InputStream inputStream = IOUtils.toInputStream(data, StandardCharsets.UTF_8)) { |
| final ScanResult result = clamService.scan(inputStream); |
| assertEquals(ScanResult.Status.OK, result.getStatus()); |
| } |
| } |
| |
| @Test |
| public void testScan_eicarcom2_zip() throws Exception { |
| final byte[] eicarcom2_zip = readEicarFile(); |
| try (final InputStream fileInputStream = new ByteArrayInputStream(eicarcom2_zip)) { |
| final ScanResult result = clamService.scan(fileInputStream); |
| assertEquals(ScanResult.Status.FOUND, result.getStatus()); |
| } |
| } |
| |
| @Test |
| public void testScan_infiniteStream() throws Exception { |
| try (final InputStream inputStream = new InfiniteInputStream()) { |
| final ScanResult result = clamService.scan(inputStream); |
| assertEquals(ScanResult.Status.ERROR, result.getStatus()); |
| assertEquals(INSTREAM_SIZE_LIMIT_EXCEEDED_ERROR_MESSAGE, result.getMessage()); |
| } |
| } |
| |
| // ContentAnalyzer |
| |
| @Test |
| public void testContentAnalyzer() { |
| assertNotNull(contentAnalyzer); |
| } |
| |
| @Test |
| public void testAnalyze_ok() throws Exception { |
| final String data = "ok – no malware here"; |
| final Map<String, Object> report = new HashMap<>(); |
| try (final InputStream inputStream = IOUtils.toInputStream(data, StandardCharsets.UTF_8)) { |
| contentAnalyzer.analyze(inputStream, null, report).get(); |
| assertEquals(ScanResult.Status.OK, report.get("sling.commons.clam.scanresult.status")); |
| } |
| } |
| |
| @Test |
| public void testAnalyze_eicarcom2_zip() throws Exception { |
| final byte[] eicarcom2_zip = readEicarFile(); |
| final Map<String, Object> report = new HashMap<>(); |
| try (final InputStream inputStream = new ByteArrayInputStream(eicarcom2_zip)) { |
| contentAnalyzer.analyze(inputStream, null, report).get(); |
| assertEquals(ScanResult.Status.FOUND, report.get("sling.commons.clam.scanresult.status")); |
| } |
| } |
| |
| @Test |
| public void testAnalyze_infiniteStream() throws Exception { |
| final Map<String, Object> report = new HashMap<>(); |
| try (final InputStream inputStream = new InfiniteInputStream()) { |
| contentAnalyzer.analyze(inputStream, null, report).get(); |
| assertEquals(INSTREAM_SIZE_LIMIT_EXCEEDED_ERROR_MESSAGE, report.get("sling.commons.clam.scanresult.message")); |
| } |
| } |
| |
| } |