Skip to content

Commit 6306a81

Browse files
authored
Remove apache commons io library, security update (#261)
* Remove apache commons io library, security update * Should reset tempdirectory to null after a purge * Speed up file assertion
1 parent c9f569a commit 6306a81

File tree

4 files changed

+48
-41
lines changed

4 files changed

+48
-41
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ dependencies {
3636
compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion"
3737
compile "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:$jacksonVersion"
3838
compile 'org.apache.commons:commons-lang3:3.8.1'
39-
compile 'org.zeroturnaround:zt-exec:1.10'
39+
compile 'org.zeroturnaround:zt-exec:1.11'
4040
compile 'org.slf4j:slf4j-api:1.7.25'
4141
compile 'ch.qos.logback:logback-classic:1.2.3'
4242
compileOnly 'junit:junit:4.12'
4343
testCompile 'com.sun.jersey:jersey-client:1.19.4'
4444
testCompile 'com.google.guava:guava:20.0'
4545
testCompile 'org.springframework:spring-web:4.3.20.RELEASE'
4646
testCompile 'org.apache.httpcomponents:httpclient:4.5.6'
47-
testCompile 'org.assertj:assertj-core:3.11.1'
47+
testCompile 'org.assertj:assertj-core:3.19.0'
4848
testCompile 'net.javacrumbs.json-unit:json-unit:1.31.1'
4949
testCompile 'net.javacrumbs.json-unit:json-unit-fluent:1.31.1'
5050
testCompile 'org.eclipse.jetty:jetty-server:9.3.11.v20160721'

src/main/java/io/specto/hoverfly/junit/core/TempFileManager.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
package io.specto.hoverfly.junit.core;
22

3-
import org.apache.commons.io.FileUtils;
4-
import org.slf4j.Logger;
5-
import org.slf4j.LoggerFactory;
3+
import static io.specto.hoverfly.junit.core.SystemConfigFactory.OsName.WINDOWS;
4+
import static java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE;
5+
import static java.nio.file.attribute.PosixFilePermission.OWNER_READ;
6+
import static java.util.Arrays.asList;
67

78
import java.io.File;
89
import java.io.IOException;
9-
import java.net.URL;
10+
import java.io.InputStream;
1011
import java.nio.file.Files;
1112
import java.nio.file.Path;
1213
import java.nio.file.Paths;
14+
import java.nio.file.StandardCopyOption;
15+
import java.util.Comparator;
1316
import java.util.HashSet;
14-
15-
import static io.specto.hoverfly.junit.core.HoverflyUtils.findResourceOnClasspath;
16-
import static io.specto.hoverfly.junit.core.SystemConfigFactory.OsName.WINDOWS;
17-
import static java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE;
18-
import static java.nio.file.attribute.PosixFilePermission.OWNER_READ;
19-
import static java.util.Arrays.asList;
17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
2019

2120
/**
2221
* Manage temporary files for running hoverfly
@@ -36,22 +35,24 @@ void purge() {
3635
return;
3736
}
3837
try {
39-
FileUtils.deleteDirectory(tempDirectory.toFile());
38+
Files.walk(tempDirectory)
39+
.sorted(Comparator.reverseOrder())
40+
.map(Path::toFile)
41+
.forEach(File::delete);
42+
tempDirectory = null;
4043
} catch (IOException e) {
4144
LOGGER.warn("Failed to delete hoverfly binary, will try again on JVM shutdown.", e);
4245
}
43-
4446
}
4547

4648
/**
4749
* Copy classpath resource to hoverfly temporary directory
4850
*/
4951
Path copyClassPathResource(String resourcePath, String targetName) {
50-
URL sourceUrl = HoverflyUtils.findResourceOnClasspath(resourcePath);
5152

5253
Path targetPath = getOrCreateTempDirectory().resolve(targetName);
53-
try {
54-
FileUtils.copyURLToFile(sourceUrl, targetPath.toFile());
54+
try (InputStream resourceAsStream = HoverflyUtils.getClasspathResourceAsStream(resourcePath)) {
55+
Files.copy(resourceAsStream, targetPath, StandardCopyOption.REPLACE_EXISTING);
5556
} catch (IOException e) {
5657
throw new IllegalStateException("Failed to copy classpath resource " + resourcePath, e);
5758
}
@@ -66,13 +67,13 @@ Path copyClassPathResource(String resourcePath, String targetName) {
6667
Path copyHoverflyBinary(SystemConfig systemConfig) {
6768
String binaryName = systemConfig.getHoverflyBinaryName();
6869
LOGGER.info("Selecting the following binary based on the current operating system: {}", binaryName);
69-
final URL sourceUrl = findResourceOnClasspath(HOVERFLY_BINARIES_ROOT_PATH + binaryName);
70-
final Path targetPath = getOrCreateTempDirectory().resolve(binaryName);
70+
Path targetPath = getOrCreateTempDirectory().resolve(binaryName);
7171
LOGGER.info("Storing binary in temporary directory {}", targetPath);
72-
final File targetFile = targetPath.toFile();
73-
try {
74-
FileUtils.copyURLToFile(sourceUrl, targetFile);
72+
73+
try (InputStream resourceAsStream = HoverflyUtils.getClasspathResourceAsStream(HOVERFLY_BINARIES_ROOT_PATH + binaryName)) {
74+
Files.copy(resourceAsStream, targetPath, StandardCopyOption.REPLACE_EXISTING);
7575
if (systemConfig.getOsName() == WINDOWS) {
76+
final File targetFile = targetPath.toFile();
7677
targetFile.setExecutable(true);
7778
targetFile.setReadable(true);
7879
targetFile.setWritable(true);

src/test/java/io/specto/hoverfly/junit/core/MultiCaptureTest.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.google.common.io.Resources;
44
import io.specto.hoverfly.junit.rule.HoverflyRule;
55
import io.specto.hoverfly.webserver.CaptureModeTestWebServer;
6-
import org.apache.commons.io.FileUtils;
6+
import java.util.Comparator;
77
import org.json.JSONException;
88
import org.junit.AfterClass;
99
import org.junit.Before;
@@ -42,20 +42,16 @@ public class MultiCaptureTest {
4242
public void setUp() throws Exception {
4343

4444
// Delete directory and contents
45-
final File firstSimulationDirectory = FIRST_RECORDED_SIMULATION_FILE.getParent().toFile();
46-
if(firstSimulationDirectory.exists()) {
47-
FileUtils.forceDelete(firstSimulationDirectory);
48-
}
45+
Path firstSimulationDirectory = FIRST_RECORDED_SIMULATION_FILE.getParent();
46+
recursiveDeleteIfExists(firstSimulationDirectory);
4947

5048
// Delete individual file
5149
Files.deleteIfExists(SECOND_RECORDED_SIMULATION_FILE);
5250
Files.deleteIfExists(FORTH_RECORDED_SIMULATION_FILE);
5351

5452
// Delete directory and contents
55-
final File thirdSimulationDirectory = THIRD_RECORDED_SIMULATION_FILE.getParent().toFile();
56-
if(thirdSimulationDirectory.exists()) {
57-
FileUtils.forceDelete(thirdSimulationDirectory);
58-
}
53+
Path thirdSimulationDirectory = THIRD_RECORDED_SIMULATION_FILE.getParent();
54+
recursiveDeleteIfExists(thirdSimulationDirectory);
5955

6056
webServerBaseUrl = CaptureModeTestWebServer.run();
6157
}
@@ -106,5 +102,14 @@ public static void after() throws IOException, JSONException {
106102
CaptureModeTestWebServer.terminate();
107103
}
108104

105+
private void recursiveDeleteIfExists(Path directory) throws IOException {
106+
if (directory.toFile().exists()) {
107+
Files.walk(directory)
108+
.sorted(Comparator.reverseOrder())
109+
.map(Path::toFile)
110+
.forEach(File::delete);
111+
}
112+
}
113+
109114

110115
}

src/test/java/io/specto/hoverfly/junit/core/TempFileManagerTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package io.specto.hoverfly.junit.core;
22

33

4-
import com.google.common.io.Resources;
5-
import org.apache.commons.io.FileUtils;
6-
import org.junit.After;
7-
import org.junit.Before;
8-
import org.junit.Test;
4+
import static org.assertj.core.api.Assertions.assertThat;
95

6+
import com.google.common.io.Resources;
7+
import java.io.FileInputStream;
108
import java.net.URL;
119
import java.nio.file.Files;
1210
import java.nio.file.Path;
1311
import java.nio.file.Paths;
14-
15-
import static org.assertj.core.api.Assertions.assertThat;
12+
import org.junit.After;
13+
import org.junit.Before;
14+
import org.junit.Test;
1615

1716
public class TempFileManagerTest {
1817

@@ -45,6 +44,7 @@ public void shouldPurgeAllCreatedTempFiles() {
4544

4645
assertThat(Files.exists(tempResourcePath)).isFalse();
4746
assertThat(Files.exists(tempResourcePath.getParent())).isFalse();
47+
assertThat(tempFileManager.getTempDirectory()).isNull();
4848
}
4949

5050
@Test
@@ -58,7 +58,7 @@ public void shouldCopyClassPathResourceToCurrentTempDirectory() throws Exception
5858
assertThat(Files.isRegularFile(targetFile)).isTrue();
5959
assertThat(Files.isReadable(targetFile)).isTrue();
6060
assertThat(targetFile.getParent()).isEqualTo(tempFileManager.getTempDirectory());
61-
assertThat(FileUtils.contentEquals(sourceFile.toFile(), targetFile.toFile())).isTrue();
61+
assertThat(targetFile).hasSameContentAs(sourceFile);
6262
}
6363

6464
@Test
@@ -78,11 +78,12 @@ public void shouldCopyHoverflyBinary() throws Exception {
7878
assertThat(Files.isReadable(targetFile)).isTrue();
7979
assertThat(Files.isExecutable(targetFile)).isTrue();
8080
assertThat(targetFile.getParent()).isEqualTo(tempFileManager.getTempDirectory());
81-
assertThat(FileUtils.contentEquals(sourceFile.toFile(), targetFile.toFile())).isTrue();
81+
assertThat(new FileInputStream(targetFile.toFile())).hasSameContentAs(new FileInputStream(sourceFile.toFile()));
82+
8283
}
8384

8485
@After
8586
public void tearDown() {
8687
tempFileManager.purge();
8788
}
88-
}
89+
}

0 commit comments

Comments
 (0)