Skip to content

Commit 07e0a08

Browse files
Add more tests
1 parent 117cf06 commit 07e0a08

File tree

5 files changed

+168
-6
lines changed

5 files changed

+168
-6
lines changed

multiapps-controller-process/src/main/java/org/cloudfoundry/multiapps/controller/process/Messages.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class Messages {
1313
public static final String ERROR_RETRIEVING_MTA_REQUIRED_DEPENDENCY_CONTENT = "Error retrieving content of MTA required dependency \"{0}\"";
1414
public static final String ERROR_RETRIEVING_MTA_RESOURCE_CONTENT = "Error retrieving content of MTA resource \"{0}\"";
1515
public static final String ERROR_SERVICE_NEEDS_TO_BE_RECREATED_BUT_FLAG_NOT_SET = "Service described by MTA resource \"{0}\" of type [{1}] does not match already existing service \"{2}\" of type [{3}] and needs to be recreated. Use command line option \"--delete-services\" to enable the deletion of the existing one.";
16-
public static final String SIZE_OF_APP_EXCEEDS_MAX_SIZE_LIMIT = "The size of the application exceeds max size limit \"{0}\"";
1716
public static final String ERROR_RETRIEVING_REQUIRED_SERVICE_KEY_ELEMENT = "Unable to retrieve required service key element \"{0}\" for service \"{1}\"";
1817
public static final String ERROR_PARAMETER_1_MUST_NOT_BE_NEGATIVE = "Value \"{0}\" of parameter \"{1}\" must not be negative";
1918
public static final String ERROR_PARAMETER_1_MUST_BE_LESS_THAN_2 = "Value \"{0}\" of parameter \"{1}\" must be less than {2}";

multiapps-controller-process/src/main/java/org/cloudfoundry/multiapps/controller/process/util/ApplicationDigestCalculator.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private void calculateDigestFromDirectory(ApplicationArchiveContext applicationA
8484
}
8585
}
8686

87-
protected void calculateDigestFromArchive(ApplicationArchiveContext applicationArchiveContext, InputStream inputStream)
87+
private void calculateDigestFromArchive(ApplicationArchiveContext applicationArchiveContext, InputStream inputStream)
8888
throws IOException {
8989
byte[] buffer = new byte[BUFFER_SIZE];
9090
int numberOfReadBytes = 0;
@@ -93,7 +93,10 @@ protected void calculateDigestFromArchive(ApplicationArchiveContext applicationA
9393
while ((numberOfReadBytes = inputStream.read(buffer)) != -1) {
9494
long currentSizeInBytes = applicationArchiveContext.getCurrentSizeInBytes();
9595
if (currentSizeInBytes + numberOfReadBytes > maxSizeInBytes) {
96-
throw new ContentException(Messages.SIZE_OF_APP_EXCEEDS_MAX_SIZE_LIMIT, maxSizeInBytes);
96+
throw new ContentException(org.cloudfoundry.multiapps.mta.Messages.ERROR_SIZE_OF_FILE_EXCEEDS_CONFIGURED_MAX_SIZE_LIMIT,
97+
currentSizeInBytes + numberOfReadBytes,
98+
applicationArchiveContext.getModuleFileName(),
99+
maxSizeInBytes);
97100
}
98101
applicationArchiveContext.calculateCurrentSizeInBytes(numberOfReadBytes);
99102
applicationDigestCalculator.updateDigest(buffer, 0, numberOfReadBytes);

multiapps-controller-process/src/main/java/org/cloudfoundry/multiapps/controller/process/util/ApplicationZipBuilder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ protected void copy(InputStream input, OutputStream output, ApplicationArchiveCo
132132
while ((numberOfReadBytes = input.read(buffer)) != -1) {
133133
long currentSizeInBytes = applicationArchiveContext.getCurrentSizeInBytes();
134134
if (currentSizeInBytes + numberOfReadBytes > maxSizeInBytes) {
135-
throw new ContentException(Messages.SIZE_OF_APP_EXCEEDS_MAX_SIZE_LIMIT, maxSizeInBytes);
135+
throw new ContentException(org.cloudfoundry.multiapps.mta.Messages.ERROR_SIZE_OF_FILE_EXCEEDS_CONFIGURED_MAX_SIZE_LIMIT,
136+
currentSizeInBytes + numberOfReadBytes,
137+
applicationArchiveContext.getModuleFileName(),
138+
maxSizeInBytes);
136139
}
137140
output.write(buffer, 0, numberOfReadBytes);
138141
applicationArchiveContext.calculateCurrentSizeInBytes(numberOfReadBytes);

multiapps-controller-process/src/main/java/org/cloudfoundry/multiapps/controller/process/util/ArchiveEntryExtractor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.io.ByteArrayOutputStream;
44
import java.io.IOException;
55
import java.io.InputStream;
6-
import java.util.function.BiConsumer;
6+
import java.util.function.ObjIntConsumer;
77

88
import org.cloudfoundry.multiapps.common.SLException;
99
import org.cloudfoundry.multiapps.controller.persistence.services.FileContentToProcess;
@@ -67,7 +67,7 @@ private byte[] inflateFileContent(FileEntryProperties fileEntryProperties, Input
6767

6868
public void processFileEntryContent(FileEntryProperties fileEntryProperties,
6969
ArchiveEntryWithStreamPositions archiveEntryWithStreamPositions,
70-
BiConsumer<byte[], Integer> decompressedBytesConsumer) {
70+
ObjIntConsumer<byte[]> decompressedBytesConsumer) {
7171
try {
7272
fileService.consumeFileContentWithOffset(toFileContentToProcess(fileEntryProperties, archiveEntryWithStreamPositions),
7373
fileEntryStream -> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package org.cloudfoundry.multiapps.controller.process.util;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.mockito.ArgumentMatchers.any;
6+
import static org.mockito.Mockito.doAnswer;
7+
import static org.mockito.Mockito.doThrow;
8+
9+
import java.io.InputStream;
10+
import java.util.List;
11+
import java.util.function.ObjIntConsumer;
12+
13+
import org.apache.commons.io.input.BoundedInputStream;
14+
import org.cloudfoundry.multiapps.common.SLException;
15+
import org.cloudfoundry.multiapps.controller.persistence.services.FileContentConsumer;
16+
import org.cloudfoundry.multiapps.controller.persistence.services.FileService;
17+
import org.cloudfoundry.multiapps.mta.util.EntryToInflate;
18+
import org.cloudfoundry.multiapps.mta.util.InflatorUtil;
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
import org.mockito.Mock;
22+
import org.mockito.MockitoAnnotations;
23+
24+
class ApplicationDigestCalculatorTest {
25+
26+
private static final String SPACE_GUID = "123";
27+
private static final String APP_ARCHIVE_ID = "132";
28+
private static final String DB_DIRECTORY_MODULE_DIGEST = "71017C6429E2E1FA4ED2AD97ABF321A0";
29+
private static final String WEB_SERVER_MODULE_DIGEST = "4C64A36CDC073B5D07947005F630DACC";
30+
31+
@Mock
32+
private FileService fileService;
33+
@Mock
34+
private ArchiveEntryExtractor archiveEntryExtractor;
35+
36+
private ApplicationDigestCalculator applicationDigestCalculator;
37+
38+
@BeforeEach
39+
void setUp() throws Exception {
40+
MockitoAnnotations.openMocks(this)
41+
.close();
42+
doAnswer(answer -> {
43+
FileContentConsumer fileContentConsumer = answer.getArgument(2);
44+
fileContentConsumer.consume(getClass().getResourceAsStream("com.sap.mta.sample-1.2.1-beta.mtar"));
45+
return null;
46+
}).when(fileService)
47+
.consumeFileContent(any(), any(), any());
48+
doAnswer(answer -> {
49+
FileEntryProperties fileEntryProperties = answer.getArgument(0);
50+
ArchiveEntryWithStreamPositions archiveEntryWithStreamPositions = answer.getArgument(1);
51+
ObjIntConsumer<byte[]> consumer = answer.getArgument(2);
52+
InputStream resourceAsStream = getClass().getResourceAsStream("com.sap.mta.sample-1.2.1-beta.mtar");
53+
resourceAsStream.skip(archiveEntryWithStreamPositions.getStartPosition());
54+
BoundedInputStream boundedInputStream = new BoundedInputStream.Builder().setInputStream(resourceAsStream)
55+
.setCount(archiveEntryWithStreamPositions.getStartPosition())
56+
.setMaxCount(archiveEntryWithStreamPositions.getEndPosition())
57+
.get();
58+
InflatorUtil.inflate(new EntryToInflate("web/web-server.zip", fileEntryProperties.getMaxFileSizeInBytes(), boundedInputStream),
59+
consumer);
60+
return null;
61+
}).when(archiveEntryExtractor)
62+
.processFileEntryContent(any(), any(), any());
63+
applicationDigestCalculator = new ApplicationDigestCalculator(fileService, new ApplicationArchiveIterator(), archiveEntryExtractor);
64+
}
65+
66+
@Test
67+
void testDigestCalculationWhenModuleIsDirectory() {
68+
ArchiveEntryWithStreamPositions directoryModuleEntry = buildDbModule();
69+
List<ArchiveEntryWithStreamPositions> archiveEntriesWithStreamPositions = List.of(directoryModuleEntry);
70+
ApplicationArchiveContext applicationArchiveContext = new ApplicationArchiveContext("db/",
71+
Integer.MAX_VALUE,
72+
archiveEntriesWithStreamPositions,
73+
SPACE_GUID,
74+
APP_ARCHIVE_ID);
75+
String appDigest = applicationDigestCalculator.calculateApplicationDigest(applicationArchiveContext);
76+
assertEquals(DB_DIRECTORY_MODULE_DIGEST, appDigest);
77+
}
78+
79+
private ArchiveEntryWithStreamPositions buildDbModule() {
80+
return ImmutableArchiveEntryWithStreamPositions.builder()
81+
.name("db/")
82+
.startPosition(0)
83+
.endPosition(0)
84+
.compressionMethod(ArchiveEntryWithStreamPositions.CompressionMethod.DEFLATED)
85+
.isDirectory(true)
86+
.build();
87+
}
88+
89+
@Test
90+
void testDigestCalculationWhenModuleIsDirectoryAndExceedsMaxSizeLimit() {
91+
ArchiveEntryWithStreamPositions directoryModuleEntry = buildDbModule();
92+
List<ArchiveEntryWithStreamPositions> archiveEntriesWithStreamPositions = List.of(directoryModuleEntry);
93+
ApplicationArchiveContext applicationArchiveContext = new ApplicationArchiveContext("db/",
94+
1,
95+
archiveEntriesWithStreamPositions,
96+
SPACE_GUID,
97+
APP_ARCHIVE_ID);
98+
Exception exception = assertThrows(Exception.class,
99+
() -> applicationDigestCalculator.calculateApplicationDigest(applicationArchiveContext));
100+
assertEquals("The size \"201\" of mta file \"db/\" exceeds the configured max size limit \"1\"", exception.getMessage());
101+
}
102+
103+
@Test
104+
void testDigestCalculationWhenModuleIsZip() {
105+
ArchiveEntryWithStreamPositions zipModuleEntry = buildWebServerModule();
106+
List<ArchiveEntryWithStreamPositions> archiveEntriesWithStreamPositions = List.of(zipModuleEntry);
107+
ApplicationArchiveContext applicationArchiveContext = new ApplicationArchiveContext("web/web-server.zip",
108+
Integer.MAX_VALUE,
109+
archiveEntriesWithStreamPositions,
110+
SPACE_GUID,
111+
APP_ARCHIVE_ID);
112+
String appDigest = applicationDigestCalculator.calculateApplicationDigest(applicationArchiveContext);
113+
assertEquals(WEB_SERVER_MODULE_DIGEST, appDigest);
114+
}
115+
116+
private ArchiveEntryWithStreamPositions buildWebServerModule() {
117+
return ImmutableArchiveEntryWithStreamPositions.builder()
118+
.name("web/web-server.zip")
119+
.startPosition(531)
120+
.endPosition(678)
121+
.compressionMethod(ArchiveEntryWithStreamPositions.CompressionMethod.DEFLATED)
122+
.isDirectory(false)
123+
.build();
124+
}
125+
126+
@Test
127+
void testDigestCalculationWhenModuleIsZipAndExceedsMaxSizeLimit() {
128+
ArchiveEntryWithStreamPositions zipModuleEntry = buildWebServerModule();
129+
List<ArchiveEntryWithStreamPositions> archiveEntriesWithStreamPositions = List.of(zipModuleEntry);
130+
ApplicationArchiveContext applicationArchiveContext = new ApplicationArchiveContext("web/web-server.zip",
131+
1,
132+
archiveEntriesWithStreamPositions,
133+
SPACE_GUID,
134+
APP_ARCHIVE_ID);
135+
Exception exception = assertThrows(Exception.class,
136+
() -> applicationDigestCalculator.calculateApplicationDigest(applicationArchiveContext));
137+
assertEquals("The size \"203\" of mta file \"web/web-server.zip\" exceeds the configured max size limit \"1\"",
138+
exception.getMessage());
139+
}
140+
141+
@Test
142+
void testDigestCalculationWhenExceptionIsThrown() {
143+
ArchiveEntryWithStreamPositions zipModuleEntry = buildWebServerModule();
144+
List<ArchiveEntryWithStreamPositions> archiveEntriesWithStreamPositions = List.of(zipModuleEntry);
145+
ApplicationArchiveContext applicationArchiveContext = new ApplicationArchiveContext("web/web-server.zip",
146+
1,
147+
archiveEntriesWithStreamPositions,
148+
SPACE_GUID,
149+
APP_ARCHIVE_ID);
150+
doThrow(new SLException("Cannot calculate digest")).when(archiveEntryExtractor)
151+
.processFileEntryContent(any(), any(), any());
152+
Exception exception = assertThrows(Exception.class,
153+
() -> applicationDigestCalculator.calculateApplicationDigest(applicationArchiveContext));
154+
assertEquals("Cannot calculate digest", exception.getMessage());
155+
}
156+
157+
}

0 commit comments

Comments
 (0)