Skip to content

Commit

Permalink
Merge pull request #4 from TIBHannover/fix-nullable-contentreader
Browse files Browse the repository at this point in the history
Fix nullable contentreader
  • Loading branch information
mirjan-hoffmann authored Oct 12, 2020
2 parents 63a8dc4 + 452e209 commit cf7d4a3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
}

0 comments on commit cf7d4a3

Please sign in to comment.