From 1101adfa5dadf6ae8c041564f3e3bc2f818ecff3 Mon Sep 17 00:00:00 2001 From: Mirjan Hoffmann Date: Mon, 12 Oct 2020 08:01:33 +0200 Subject: [PATCH 1/2] Added test for null content reader --- .../services/AntivirusServiceImplTest.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 alfviral/src/test/java/com/fegor/alfresco/services/AntivirusServiceImplTest.java diff --git a/alfviral/src/test/java/com/fegor/alfresco/services/AntivirusServiceImplTest.java b/alfviral/src/test/java/com/fegor/alfresco/services/AntivirusServiceImplTest.java new file mode 100644 index 0000000..c4c3a07 --- /dev/null +++ b/alfviral/src/test/java/com/fegor/alfresco/services/AntivirusServiceImplTest.java @@ -0,0 +1,54 @@ +package com.fegor.alfresco.services; + +import static org.junit.Assert.fail; +import static org.mockito.Mockito.when; + +import org.alfresco.service.ServiceRegistry; +import org.alfresco.service.cmr.repository.ContentService; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.NodeService; +import org.alfresco.service.namespace.QName; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class AntivirusServiceImplTest { + + private AntivirusServiceImpl service; + private NodeRef testNode; + + @Mock + private ServiceRegistry serviceRegistry; + @Mock + private NodeService nodeService; + @Mock + private ContentService contentService; + + @Before + public void setup() { + service = new AntivirusServiceImpl(); + service.setServiceRegistry(serviceRegistry); + testNode = new NodeRef("workspace://TestStore/test"); + service.setFileExceptions("text/html|text/xml|application/pdf|image/jpeg|image/png|image/giftext/plain"); + service.setFileOnly("application/octet-stream|application/x-dosexec|application/bat|application/x-bat|application/x-msdos-program|application/textedit|application/cmd|application/x-ms-dos-executable"); + service.setFileOnlyOrExceptions("exceptions"); + } + + @Test + public void testNullContentReader() { + when(serviceRegistry.getNodeService()).thenReturn(nodeService); + when(serviceRegistry.getContentService()).thenReturn(contentService); + when(nodeService.exists(testNode)).thenReturn(true); + when(contentService.getReader(Mockito.any(NodeRef.class), Mockito.any(QName.class))).thenReturn(null); + + try { + service.scanFile(testNode); + } catch (Exception e) { + fail("There should be no exception when the content reader is null"); + } + } +} From 452e209605a85e508b5e5a41c4b6a7534072d328 Mon Sep 17 00:00:00 2001 From: Mirjan Hoffmann Date: Mon, 12 Oct 2020 11:19:18 +0200 Subject: [PATCH 2/2] Fixed access to nullable ContentReader --- .../services/AntivirusServiceImpl.java | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/alfviral/src/main/java/com/fegor/alfresco/services/AntivirusServiceImpl.java b/alfviral/src/main/java/com/fegor/alfresco/services/AntivirusServiceImpl.java index 456ae2b..2122bb6 100644 --- a/alfviral/src/main/java/com/fegor/alfresco/services/AntivirusServiceImpl.java +++ b/alfviral/src/main/java/com/fegor/alfresco/services/AntivirusServiceImpl.java @@ -144,11 +144,7 @@ public void scanFile(NodeRef nodeRef) throws InvalidNodeRefException { if (nodeService.exists(nodeRef)) { contentReader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT); - if ((contentReader != null) - && (fileExceptions.indexOf(contentReader.getMimetype()) == -1 - && (fileOnlyOrExceptions.toLowerCase()).equals(FILE_EXCEPTIONS)) - || (fileOnly.indexOf(contentReader.getMimetype()) != -1 - && (fileOnlyOrExceptions.toLowerCase()).equals(FILE_ONLY))) { + if (shouldScan(contentReader)) { String contentUrl = contentReader.getContentUrl(); String contentPath = contentUrl.replaceFirst("store:/", this.store); @@ -249,13 +245,24 @@ else if (mode.toUpperCase().equals(VirusScanMode.ScanModeICap)) { logger.debug("[File: " + contentReader.getContentUrl() + " is clean"); } } + } + } - else if ((!(fileOnlyOrExceptions.toLowerCase()).equals(FILE_ONLY)) - || (!(fileOnlyOrExceptions.toLowerCase()).equals(FILE_EXCEPTIONS))) { - logger.error("Property alfviral.file.only_or_exceptions not is '" + FILE_ONLY + "' or '" - + FILE_EXCEPTIONS + "'"); - } + private boolean shouldScan(final ContentReader contentReader) { + if ((!(fileOnlyOrExceptions.toLowerCase()).equals(FILE_ONLY)) + && (!(fileOnlyOrExceptions.toLowerCase()).equals(FILE_EXCEPTIONS))) { + logger.error("Property alfviral.file.only_or_exceptions not is '" + FILE_ONLY + "' or '" + FILE_EXCEPTIONS + + "'"); + return false; + } + if (contentReader != null) { + final boolean fileExceptionsMatches = fileExceptions.indexOf(contentReader.getMimetype()) == -1 + && (fileOnlyOrExceptions.toLowerCase()).equals(FILE_EXCEPTIONS); + final boolean fileOnlyMatches = fileOnly.indexOf(contentReader.getMimetype()) != -1 + && (fileOnlyOrExceptions.toLowerCase()).equals(FILE_ONLY); + return fileExceptionsMatches || fileOnlyMatches; } + return false; } /**