Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import com.mirth.connect.connectors.file.FileConnectorException;

public class FileConnectionTests {
public class FileConnectionTest {
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

private FileConnection fc;
private File someFolder;

Expand Down Expand Up @@ -86,7 +91,7 @@ public void testCanWrite() {
}

@Test
public void testReadFile() throws IOException {
public void testReadFile() throws IOException, FileConnectorException {
// The string to write
String testString = new String("This is just a test string");
byte[] byteTest = testString.getBytes(Charset.defaultCharset());
Expand All @@ -96,17 +101,11 @@ public void testReadFile() throws IOException {
// write the file
FileUtils.writeByteArrayToFile(testFile, byteTest);

InputStream in = null;
try {
in = fc.readFile(testFile.getName(), someFolder.getAbsolutePath(), null);
} catch (FileConnectorException e) {
fail("Threw a FileConnectorException");
}

// Read the file data
byte[] tempRead = new byte[byteTest.length];
in.read(tempRead);
in.close();
byte[] tempRead;
try (InputStream in = fc.readFile(testFile.getName(), someFolder.getAbsolutePath(), null)) {
tempRead = in.readAllBytes();
}

// check to make sure it is the same
assertArrayEquals(byteTest, tempRead);
Expand Down Expand Up @@ -231,24 +230,14 @@ public void testMove() {
public void setUp() throws IOException {
fc = new FileConnection();

someFolder = new File("TEST_FileConnection");

// Delete if exists
if (someFolder.exists()) {
someFolder.setWritable(true, false);
// clear it out if there is anything there
FileUtils.deleteDirectory(someFolder);
}

if (!someFolder.mkdir()) {
throw new IOException("Could not create test folder");
}
someFolder = tempFolder.newFolder("TEST_FileConnection");
}

@After
public void tearDown() throws IOException {
public void tearDown() {
// Some tests remove permissions; restore them so the TemporaryFolder rule can delete the folder
someFolder.setReadable(true, false);
someFolder.setWritable(true, false);
FileUtils.deleteDirectory(someFolder);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.xml.sax.SAXParseException;

import com.mirth.connect.donkey.model.message.BatchRawMessage;
Expand All @@ -23,13 +25,15 @@
/** Covered end to end by ci/tests/210-xml-batch-xxe. */
public class XMLBatchAdaptorTest {

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

@Test
public void externalEntityIsNotResolved() throws Exception {
File secret = File.createTempFile("xxe", ".txt");
secret.deleteOnExit();
FileUtils.write(secret, "canary", UTF_8);
Path secret = tempFolder.newFile("xxe.txt").toPath();
Files.writeString(secret, "canary", UTF_8);

XMLBatchAdaptor adaptor = adaptor("<!DOCTYPE Batch [ <!ENTITY xxe SYSTEM \"" + secret.toURI() + "\"> ]><Batch><Message>&xxe;</Message></Batch>");
XMLBatchAdaptor adaptor = adaptor("<!DOCTYPE Batch [ <!ENTITY xxe SYSTEM \"" + secret.toUri() + "\"> ]><Batch><Message>&xxe;</Message></Batch>");

try {
fail("Expected the external entity to be rejected, but got: " + adaptor.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
Expand All @@ -19,10 +18,11 @@
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.io.FileUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
Expand All @@ -42,21 +42,23 @@ public class XsltStepTest {
private static final String TEXT_XSLT = "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
+ "<xsl:output method=\"text\"/><xsl:template match=\"/\"><xsl:value-of select=\".\"/></xsl:template></xsl:stylesheet>";

private static File secret;
@ClassRule
public static final TemporaryFolder tempFolder = new TemporaryFolder();

private static Path secret;

@BeforeClass
public static void setup() throws Exception {
JavaScriptTestUtil.setup();

secret = File.createTempFile("xxe", ".txt");
secret.deleteOnExit();
FileUtils.write(secret, "canary", UTF_8);
secret = tempFolder.newFile("xxe.txt").toPath();
Files.writeString(secret, "canary", UTF_8);
}

/** Dropping the entity and rejecting the message are both fine; leaking the file is not. */
@Test
public void externalEntityInSourceIsNotResolved() throws Exception {
String xml = "<!DOCTYPE r [ <!ENTITY xxe SYSTEM \"" + secret.toURI() + "\"> ]><r>&xxe;</r>";
String xml = "<!DOCTYPE r [ <!ENTITY xxe SYSTEM \"" + secret.toUri() + "\"> ]><r>&xxe;</r>";
String outcome;

try {
Expand Down Expand Up @@ -90,13 +92,12 @@ public void benignDocumentIsTransformed() throws Exception {
/** The step restricts the message, not the stylesheet. */
@Test
public void stylesheetMayImportFromAFileUri() throws Exception {
File imported = File.createTempFile("imported", ".xsl");
imported.deleteOnExit();
FileUtils.write(imported, "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
Path imported = tempFolder.newFile("imported.xsl").toPath();
Files.writeString(imported, "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
+ "<xsl:template match=\"/\">imported</xsl:template></xsl:stylesheet>", UTF_8);

String xslt = "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
+ "<xsl:import href=\"" + imported.toURI() + "\"/><xsl:output method=\"text\"/></xsl:stylesheet>";
+ "<xsl:import href=\"" + imported.toUri() + "\"/><xsl:output method=\"text\"/></xsl:stylesheet>";
assertEquals("imported", transform("<r>hello</r>", xslt));
}

Expand Down Expand Up @@ -154,11 +155,10 @@ public void parserFromChannelResourceLibraryIsNotUsed() throws Exception {

/** Builds a classloader that advertises the factory the way a jar in a library would. */
private static URLClassLoader resourceLibraryProviding(Class<? extends SAXParserFactory> factory) throws Exception {
Path root = Files.createTempDirectory("resource-library");
root.toFile().deleteOnExit();
Path root = tempFolder.newFolder("resource-library").toPath();

Path services = Files.createDirectories(root.resolve("META-INF").resolve("services"));
FileUtils.write(services.resolve(SAXParserFactory.class.getName()).toFile(), factory.getName(), UTF_8);
Files.writeString(services.resolve(SAXParserFactory.class.getName()), factory.getName(), UTF_8);

return new URLClassLoader(new URL[] { root.toUri().toURL() }, XsltStepTest.class.getClassLoader());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,54 @@
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import com.mirth.connect.util.ZipTestUtils;

public class DefaultExtensionControllerTest {

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

private File installTempDir;

@Before
public void createTestFolder() throws Exception {
// Nested so that a "../" entry stays inside the temporary folder
installTempDir = tempFolder.newFolder("zipextraction");
}

@Test(expected = ZipException.class)
public void testExtractZipEntryZipSlipWithRelativePath() throws Exception {
DefaultExtensionController extensionController = new DefaultExtensionController();

File installTempDir = new File("tests/zipextraction");
ZipEntry entry = new ZipEntry("../ZipSlip.txt");
ZipFile zipFile = createTempZipFile("ZipSlip.txt");

extensionController.extractZipEntry(entry, installTempDir, zipFile);
try (ZipFile zipFile = createTempZipFile("ZipSlip.txt")) {
extensionController.extractZipEntry(entry, installTempDir, zipFile);
}
}

@Test
public void testExtractZipEntryValidPath() throws Exception {
File installTempDir = new File("tests/zipextraction/");

DefaultExtensionController extensionController = new DefaultExtensionController();

ZipEntry entry = new ZipEntry("good.txt");
ZipFile zipFile = createTempZipFile("good.txt");
extensionController.extractZipEntry(entry, installTempDir, zipFile);

File extractedFile = new File("tests/zipextraction/", "good.txt");
assertTrue(extractedFile.exists());
}

@Before
public void createTestFolder() {
File installTempDir = new File("tests/zipextraction/");
if (!installTempDir.exists()) {
installTempDir.mkdir();
} else {
cleanupTestFolder();
try (ZipFile zipFile = createTempZipFile("good.txt")) {
extensionController.extractZipEntry(entry, installTempDir, zipFile);
}
}

@After
public void cleanupTestFolder() {
File tempDir = new File("tests/zipextraction/");
if (tempDir.exists()) {
for (File file : tempDir.listFiles()) {
file.delete();
}
}
assertTrue(Files.exists(installTempDir.toPath().resolve("good.txt")));
}

private ZipFile createTempZipFile(String fileName) throws Exception {
return new ZipFile(ZipTestUtils.createTempZipFile(fileName));
return new ZipFile(ZipTestUtils.createTempZipFile(tempFolder.getRoot(), fileName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@
package com.mirth.connect.server.launcher;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/** Unit tests for {@link Log4jBootstrap}. */
public class Log4jBootstrapTest {

/** Tests that {@link Log4jBootstrap#getClasspathUrls(File)} correctly resolves Log4j JARs regardless of version. */
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

/** Tests that {@link Log4jBootstrap#getClasspathUrls(java.io.File)} correctly resolves Log4j JARs regardless of version. */
@Test
public void testGetClasspathUrlsDoesNotDependOnVersions() throws Exception {
File log4jLibDir = Files.createTempDirectory("log4j").toFile();
log4jLibDir.deleteOnExit();
Path log4jLibDir = tempFolder.newFolder("log4j").toPath();

List<URL> expected = new ArrayList<>();
for (String name : Arrays.asList("log4j-core-99.0.0.jar", "log4j-api-99.0.0.jar", "log4j-1.2-api-99.0.0.jar")) {
File jarFile = new File(log4jLibDir, name);
assertTrue(jarFile.createNewFile());
jarFile.deleteOnExit();
expected.add(Files.createFile(log4jLibDir.resolve(name)).toUri().toURL());
}

List<URL> classpathUrls = Log4jBootstrap.getClasspathUrls(log4jLibDir);
List<URL> classpathUrls = Log4jBootstrap.getClasspathUrls(log4jLibDir.toFile());

assertEquals(Arrays.asList(
new File(log4jLibDir, "log4j-core-99.0.0.jar").toURI().toURL(),
new File(log4jLibDir, "log4j-api-99.0.0.jar").toURI().toURL(),
new File(log4jLibDir, "log4j-1.2-api-99.0.0.jar").toURI().toURL()), classpathUrls);
assertEquals(expected, classpathUrls);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
import java.util.Set;

import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class Log4jMigrationsTest {

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

@Test
public void testMigrateLog4jRemovesDirLogs() throws Exception {
File file = File.createTempFile("log4j2", ".properties");
file.deleteOnExit();
File file = tempFolder.newFile("log4j2.properties");
Files.writeString(file.toPath(), String.join(System.lineSeparator(),
"rootLogger = ERROR,stdout,fout",
"dir.logs = logs",
Expand All @@ -43,8 +47,7 @@ public void testMigrateLog4jRemovesDirLogs() throws Exception {

@Test
public void testMigrateLog4jDoesNotRewriteCleanFile() throws Exception {
File file = File.createTempFile("log4j2-clean", ".properties");
file.deleteOnExit();
File file = tempFolder.newFile("log4j2-clean.properties");
String contents = String.join(System.lineSeparator(),
"rootLogger = ERROR,stdout,fout",
"property.log.dir = logs",
Expand All @@ -62,8 +65,7 @@ public void testMigrateLog4jDoesNotRewriteCleanFile() throws Exception {

@Test
public void testMigrateLog4jFailsGracefullyWithReadOnlyFile() throws Exception {
File file = File.createTempFile("log4j2-readonly", ".properties");
file.deleteOnExit();
File file = tempFolder.newFile("log4j2-readonly.properties");
String contents = String.join(System.lineSeparator(),
"rootLogger = ERROR,stdout,fout",
"dir.logs = logs",
Expand Down Expand Up @@ -95,9 +97,7 @@ public void testMigrateLog4jFailsGracefullyWithReadOnlyFile() throws Exception {

@Test
public void testMigrateLog4jIgnoresMissingFile() throws Exception {
File parentDir = Files.createTempDirectory("log4j2-missing").toFile();
parentDir.deleteOnExit();
File file = new File(parentDir, "missing-log4j2.properties");
File file = new File(tempFolder.newFolder("log4j2-missing"), "missing-log4j2.properties");

assertFalse(file.exists());

Expand Down
Loading
Loading