From afea152677d1663b0ff37e879fb6b44548b0e789 Mon Sep 17 00:00:00 2001 From: Tony Germano Date: Sun, 15 Nov 2026 16:58:59 -0500 Subject: [PATCH 1/3] Smoke test message export to ZIP and TAR archives Nothing exercises message export to an archive against a running server or the packaged distribution. The unit tests run on the build classpath, so a library missing from the distribution, or one that misbehaves in it, would go unnoticed until someone exported messages. Add a smoke test that sends a few messages through a channel and exports them the way the administrator's export dialog does: - Locally, in the harness JVM, which runs with the engine's own jars: an unencrypted ZIP, a gzipped TAR, and a ZIP for each encryption type. The archives are checked with the JDK and commons-compress, not the ZIP library under test, so the test holds when that library is upgraded. For the encrypted ones it checks that the JDK refuses to open the archive, that every entry name is in the directory, and that the WinZip AES record is present exactly when AES was asked for. - On the server, a password protected ZIP. The harness cannot read the server's file system, so this checks that the export completes and counts the messages, which fails if the library cannot be loaded or the archive cannot be finished. The test passes against the current zip4j 1.3.3. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Tony Germano --- .../admin/MessageExportArchiveTest.java | 266 ++++++++++++++++++ 1 file changed, 266 insertions(+) create mode 100644 smoketest/src/test/java/org/openintegrationengine/smoketest/admin/MessageExportArchiveTest.java diff --git a/smoketest/src/test/java/org/openintegrationengine/smoketest/admin/MessageExportArchiveTest.java b/smoketest/src/test/java/org/openintegrationengine/smoketest/admin/MessageExportArchiveTest.java new file mode 100644 index 0000000000..0032663600 --- /dev/null +++ b/smoketest/src/test/java/org/openintegrationengine/smoketest/admin/MessageExportArchiveTest.java @@ -0,0 +1,266 @@ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: 2026 Tony Germano + +package org.openintegrationengine.smoketest.admin; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.zip.GZIPInputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipException; +import java.util.zip.ZipFile; + +import org.apache.commons.compress.archivers.ArchiveStreamFactory; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; +import org.apache.commons.compress.compressors.CompressorStreamFactory; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.openintegrationengine.smoketest.Harness; +import org.openintegrationengine.smoketest.SharedServer; + +import com.mirth.connect.client.core.Client; +import com.mirth.connect.client.core.PaginatedMessageList; +import com.mirth.connect.donkey.model.message.ContentType; +import com.mirth.connect.donkey.model.message.Message; +import com.mirth.connect.model.Channel; +import com.mirth.connect.model.filters.MessageFilter; +import com.mirth.connect.util.MessageExporter; +import com.mirth.connect.util.messagewriter.EncryptionType; +import com.mirth.connect.util.messagewriter.MessageWriter; +import com.mirth.connect.util.messagewriter.MessageWriterFactory; +import com.mirth.connect.util.messagewriter.MessageWriterOptions; + +/** + * Exports messages into archives, as the administrator's export dialog does, with the engine's own + * jars on the classpath. The client-side tests run the dialog's code (message writer, archive + * creation and the zip library) in this JVM; the server-side test runs the same code in the server. + * They check the archives with the JDK and commons-compress rather than the zip library under + * test, so they hold across upgrades of that library. + */ +@DisplayName("Message export archives") +class MessageExportArchiveTest { + + private static final String TOKEN = UUID.randomUUID().toString().substring(0, 8).toUpperCase(); + private static final String PASSWORD = "smoke-" + TOKEN; + private static final int MESSAGES = 3; + + /** + * Where server-side exports go: in the server's temp directory (dir.tempdata, appdata/temp by + * default), relative to its working directory. The harness cannot delete files on the server, + * so the export is left there, in a folder whose name says it can be deleted. + */ + private static final String SERVER_EXPORT_PREFIX = "appdata/temp/safe-to-delete-smoketest-export"; + + /** + * What WinZip AES encryption adds to the extra field of each entry: header id 0x9901, data + * size 7, version (1 for AE-1, 2 for AE-2) and vendor "AE". The key strength and the entry's + * compression method follow. Which version is written depends on the zip library. + */ + private static final byte[] AES_EXTRA_AE1 = {0x01, (byte) 0x99, 0x07, 0x00, 0x01, 0x00, 'A', 'E'}; + private static final byte[] AES_EXTRA_AE2 = {0x01, (byte) 0x99, 0x07, 0x00, 0x02, 0x00, 'A', 'E'}; + + @TempDir + static Path exportDirectory; + + private static String channelId; + private static Map contentByMessageId = new HashMap<>(); + + @BeforeAll + static void deployAndSend() throws Exception { + Channel channel = Harness.newChannel("admin/channel.xml", "Smoke message export"); + channelId = SharedServer.get().deployChannel(channel, channel.getName(), ""); + + for (int i = 1; i <= MESSAGES; i++) { + String content = "export " + TOKEN + " message " + i; + List ids = SharedServer.get().submitMessage(channelId, content, new HashMap<>()); + assertEquals(1, ids.size(), "messages produced by " + content); + contentByMessageId.put(ids.get(0) + ".txt", content); + } + + Harness.awaitAsserted(() -> { + List messages = admin().getMessages(channelId, new MessageFilter(), false, 0, MESSAGES + 1); + assertEquals(MESSAGES, messages.size(), "messages in the channel"); + assertTrue(messages.stream().allMatch(Message::isProcessed), "messages not all processed"); + }); + } + + @AfterAll + static void remove() { + Harness.undeploy(channelId); + } + + @Test + @DisplayName("writes an unencrypted ZIP the JDK can read") + void zip() throws Exception { + Path archive = exportLocally("zip", ArchiveStreamFactory.ZIP, null, null, null); + + try (ZipFile zip = new ZipFile(archive.toFile())) { + assertMessagesInZip(zip); + } + } + + @ParameterizedTest(name = "{0}") + @EnumSource(EncryptionType.class) + @DisplayName("writes a password protected ZIP the JDK refuses to open") + void encryptedZip(EncryptionType type) throws Exception { + Path archive = exportLocally("encrypted-" + type.name(), ArchiveStreamFactory.ZIP, null, PASSWORD, type); + + // The JDK cannot decrypt, and rejects an archive whose entries are marked as encrypted + ZipException rejected = assertThrows(ZipException.class, () -> new ZipFile(archive.toFile()).close()); + assertTrue(rejected.getMessage().contains("encrypted"), rejected.getMessage()); + + // The entry names are in the archive's bytes, as the headers hold them unencrypted. This + // shows the archive was written out in full; the JDK's rejection above shows it is encrypted. + byte[] bytes = Files.readAllBytes(archive); + for (String name : contentByMessageId.keySet()) { + assertTrue(indexOf(bytes, name.getBytes(StandardCharsets.UTF_8)) >= 0, "entry " + name + " in the archive"); + } + + // Entries encrypted with AES, and only those, carry the AES extra field + boolean aes = indexOf(bytes, AES_EXTRA_AE1) >= 0 || indexOf(bytes, AES_EXTRA_AE2) >= 0; + assertEquals(type != EncryptionType.STANDARD, aes, "AES extra record in the archive"); + } + + @Test + @DisplayName("writes a gzipped TAR") + void tarGz() throws Exception { + Path archive = exportLocally("tar", ArchiveStreamFactory.TAR, CompressorStreamFactory.GZIP, null, null); + + Map found = new HashMap<>(); + try (TarArchiveInputStream tar = new TarArchiveInputStream(new GZIPInputStream(Files.newInputStream(archive)))) { + TarArchiveEntry entry; + while ((entry = tar.getNextEntry()) != null) { + if (!entry.isDirectory()) { + found.put(entry.getName(), new String(tar.readAllBytes(), StandardCharsets.UTF_8)); + } + } + } + + assertContains(found); + } + + /** + * The server writes the archive to its own file system, which this harness cannot read, so + * this checks that the export completes and counts the messages. A missing library or a + * failure to finish the archive fails the request. + */ + @Test + @DisplayName("exports a password protected ZIP on the server") + void serverSideEncryptedZip() throws Exception { + MessageWriterOptions options = options("server-side", ArchiveStreamFactory.ZIP, null, PASSWORD, EncryptionType.AES256); + options.setBaseFolder(null); + options.setRootFolder(serverFolder("")); + + assertEquals(MESSAGES, admin().exportMessagesServer(channelId, new MessageFilter(), 2, options)); + } + + private static String serverFolder(String suffix) { + return SERVER_EXPORT_PREFIX + suffix + "-" + TOKEN; + } + + /** Exports the channel's messages to a file in a new directory, as the export dialog does locally. */ + private static Path exportLocally(String name, String archiver, String compressor, String password, EncryptionType type) throws Exception { + Path root = Files.createDirectories(exportDirectory.resolve(name)); + MessageWriterOptions options = options(name, archiver, compressor, password, type); + options.setBaseFolder(root.toString()); + options.setRootFolder(root.toString()); + + PaginatedMessageList messages = new PaginatedMessageList(); + messages.setClient(admin()); + messages.setChannelId(channelId); + messages.setMessageFilter(new MessageFilter()); + messages.setPageSize(2); + messages.setIncludeContent(true); + messages.setItemCount(admin().getMessageCount(channelId, new MessageFilter())); + + MessageWriter writer = MessageWriterFactory.getInstance().getMessageWriter(options, null); + int exported; + try { + exported = new MessageExporter().exportMessages(messages, writer, null, options); + writer.finishWrite(); + } finally { + writer.close(); + } + assertEquals(MESSAGES, exported, "messages exported"); + + String extension = compressor == null ? archiver : archiver + "." + compressor; + Path archive = root.resolve(name + "." + extension); + + List files; + try (var list = Files.list(root)) { + files = list.map(file -> file.getFileName().toString()).sorted().toList(); + } + assertTrue(Files.isRegularFile(archive), "archive " + archive.getFileName() + " in " + files); + // Next to the archive: the export's readme, and none of the working folder it was built in + assertEquals(List.of("EXPORTREADME.txt", archive.getFileName().toString()), files, "files in the export directory"); + return archive; + } + + private static MessageWriterOptions options(String archiveName, String archiver, String compressor, String password, EncryptionType type) { + MessageWriterOptions options = new MessageWriterOptions(); + options.setContentType(ContentType.RAW); + options.setFilePattern("${message.messageId}.txt"); + options.setArchiveFileName(archiveName); + options.setArchiveFormat(archiver); + options.setCompressFormat(compressor); + options.setPasswordEnabled(password != null); + options.setPassword(password); + options.setEncryptionType(type); + return options; + } + + private static void assertMessagesInZip(ZipFile zip) throws IOException { + Map found = new HashMap<>(); + for (ZipEntry entry : java.util.Collections.list(zip.entries())) { + if (!entry.isDirectory()) { + try (InputStream in = zip.getInputStream(entry)) { + found.put(entry.getName(), new String(in.readAllBytes(), StandardCharsets.UTF_8)); + } + } + } + + assertContains(found); + } + + /** The archive holds each message's raw content under its file name (and may hold other files). */ + private static void assertContains(Map found) { + for (Map.Entry expected : contentByMessageId.entrySet()) { + // The writer ends each message with blank lines + assertEquals(expected.getValue(), found.get(expected.getKey()) == null ? null : found.get(expected.getKey()).strip(), "content of " + expected.getKey() + " in the archive of " + new ArrayList<>(found.keySet())); + } + } + + private static int indexOf(byte[] bytes, byte[] target) { + outer: for (int i = 0; i + target.length <= bytes.length; i++) { + for (int j = 0; j < target.length; j++) { + if (bytes[i + j] != target[j]) { + continue outer; + } + } + return i; + } + return -1; + } + + private static Client admin() { + return SharedServer.get().client(); + } +} From f8f6bd7e39f4858ecd5595637fa81e34aac78de4 Mon Sep 17 00:00:00 2001 From: Tony Germano Date: Sun, 15 Nov 2026 13:39:47 -0500 Subject: [PATCH 2/3] Use temporary folders in tests that wrote to the working directory and /tmp Several tests created files outside a managed directory: - DefaultExtensionControllerTest and ArchiveUtilsTest extracted into tests/zipextraction relative to the working directory, with an @After that only deleted top-level files, and created their zip fixtures in the system temp directory without removing them. - XsltStepTest left its resource-library directory behind, because deleteOnExit cannot remove a directory that is not empty. - FileConnectionTests used a TEST_FileConnection folder in the working directory, and Log4jMigrationsTest, Log4jBootstrapTest and XMLBatchAdaptorTest relied on deleteOnExit for files in the system temp directory. Use a JUnit TemporaryFolder in all of them so everything is removed after each test, and use java.nio.file for the files the tests create, read and write themselves. Calls that need a java.io.File, such as the production APIs under test and File.setReadable, keep it. The zip extraction directory is a subfolder of the temporary folder so that the "../" zip-slip entries still resolve inside it. FileConnectionTests stays on java.io.File to keep its diff small. Besides moving its folder to the rule, it restores the read and write permissions that some tests remove so the rule can delete the folder, and testReadFile now closes its stream with try-with-resources and lets a FileConnectorException fail the test instead of catching it. ZipTestUtils gains an overload that creates the zip in a given directory, and now also closes the stream when it writes an empty zip. The one-argument method delegates to it and still uses the system temp directory for UnzipEngineTest, which is removed separately. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Tony Germano --- .../file/filesystems/FileConnectionTests.java | 39 ++++------ .../datatypes/xml/XMLBatchAdaptorTest.java | 16 +++-- .../plugins/xsltstep/XsltStepTest.java | 28 ++++---- .../DefaultExtensionControllerTest.java | 54 ++++++-------- .../server/launcher/Log4jBootstrapTest.java | 26 +++---- .../server/launcher/Log4jMigrationsTest.java | 18 ++--- .../mirth/connect/util/ArchiveUtilsTest.java | 72 +++++++++---------- .../com/mirth/connect/util/ZipTestUtils.java | 25 ++++--- 8 files changed, 126 insertions(+), 152 deletions(-) diff --git a/server/src/test/java/com/mirth/connect/connectors/file/filesystems/FileConnectionTests.java b/server/src/test/java/com/mirth/connect/connectors/file/filesystems/FileConnectionTests.java index fc42fdaaa3..8f980efeed 100644 --- a/server/src/test/java/com/mirth/connect/connectors/file/filesystems/FileConnectionTests.java +++ b/server/src/test/java/com/mirth/connect/connectors/file/filesystems/FileConnectionTests.java @@ -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 { + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + private FileConnection fc; private File someFolder; @@ -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()); @@ -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); @@ -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); } } diff --git a/server/src/test/java/com/mirth/connect/plugins/datatypes/xml/XMLBatchAdaptorTest.java b/server/src/test/java/com/mirth/connect/plugins/datatypes/xml/XMLBatchAdaptorTest.java index 5932f080dd..1e6e6a38b8 100644 --- a/server/src/test/java/com/mirth/connect/plugins/datatypes/xml/XMLBatchAdaptorTest.java +++ b/server/src/test/java/com/mirth/connect/plugins/datatypes/xml/XMLBatchAdaptorTest.java @@ -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; @@ -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(" ]>&xxe;"); + XMLBatchAdaptor adaptor = adaptor(" ]>&xxe;"); try { fail("Expected the external entity to be rejected, but got: " + adaptor.getMessage()); diff --git a/server/src/test/java/com/mirth/connect/plugins/xsltstep/XsltStepTest.java b/server/src/test/java/com/mirth/connect/plugins/xsltstep/XsltStepTest.java index 0e987ee436..4d8bea3cb6 100644 --- a/server/src/test/java/com/mirth/connect/plugins/xsltstep/XsltStepTest.java +++ b/server/src/test/java/com/mirth/connect/plugins/xsltstep/XsltStepTest.java @@ -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; @@ -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; @@ -42,21 +42,23 @@ public class XsltStepTest { private static final String TEXT_XSLT = "" + ""; - 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 = " ]>&xxe;"; + String xml = " ]>&xxe;"; String outcome; try { @@ -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, "" + Path imported = tempFolder.newFile("imported.xsl").toPath(); + Files.writeString(imported, "" + "imported", UTF_8); String xslt = "" - + ""; + + ""; assertEquals("imported", transform("hello", xslt)); } @@ -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 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()); } diff --git a/server/src/test/java/com/mirth/connect/server/controllers/DefaultExtensionControllerTest.java b/server/src/test/java/com/mirth/connect/server/controllers/DefaultExtensionControllerTest.java index 984607dcdf..b7949f9f8e 100644 --- a/server/src/test/java/com/mirth/connect/server/controllers/DefaultExtensionControllerTest.java +++ b/server/src/test/java/com/mirth/connect/server/controllers/DefaultExtensionControllerTest.java @@ -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)); } -} \ No newline at end of file +} diff --git a/server/src/test/java/com/mirth/connect/server/launcher/Log4jBootstrapTest.java b/server/src/test/java/com/mirth/connect/server/launcher/Log4jBootstrapTest.java index 1a27103e93..ed6a45de1f 100644 --- a/server/src/test/java/com/mirth/connect/server/launcher/Log4jBootstrapTest.java +++ b/server/src/test/java/com/mirth/connect/server/launcher/Log4jBootstrapTest.java @@ -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 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 classpathUrls = Log4jBootstrap.getClasspathUrls(log4jLibDir); + List 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); } } \ No newline at end of file diff --git a/server/src/test/java/com/mirth/connect/server/launcher/Log4jMigrationsTest.java b/server/src/test/java/com/mirth/connect/server/launcher/Log4jMigrationsTest.java index 7687563c42..3caa073856 100644 --- a/server/src/test/java/com/mirth/connect/server/launcher/Log4jMigrationsTest.java +++ b/server/src/test/java/com/mirth/connect/server/launcher/Log4jMigrationsTest.java @@ -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", @@ -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", @@ -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", @@ -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()); diff --git a/server/src/test/java/com/mirth/connect/util/ArchiveUtilsTest.java b/server/src/test/java/com/mirth/connect/util/ArchiveUtilsTest.java index b45f0f6aa0..e9ada7d582 100644 --- a/server/src/test/java/com/mirth/connect/util/ArchiveUtilsTest.java +++ b/server/src/test/java/com/mirth/connect/util/ArchiveUtilsTest.java @@ -14,69 +14,61 @@ import java.io.BufferedInputStream; import java.io.File; -import java.io.FileInputStream; +import java.io.IOException; import java.net.URL; +import java.nio.file.Files; import java.util.List; import java.util.zip.ZipException; import java.util.zip.ZipInputStream; -import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; public class ArchiveUtilsTest { + @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 { - File installTempDir = new File("tests/zipextraction"); - File file = ZipTestUtils.createTempZipFile("../ZipSlip.txt"); - FileInputStream fis = new FileInputStream(file); - BufferedInputStream bis = new BufferedInputStream(fis); - ZipInputStream zis = new ZipInputStream(bis); - ArchiveUtils.extractArchive(installTempDir, zis); + File file = ZipTestUtils.createTempZipFile(tempFolder.getRoot(), "../ZipSlip.txt"); + try (ZipInputStream zis = openZip(file)) { + ArchiveUtils.extractArchive(installTempDir, zis); + } } @Test public void testExtractZipNoZipSlip() throws Exception { - File installTempDir = new File("tests/zipextraction"); - File file = ZipTestUtils.createTempZipFile("ZipSlip.txt"); - FileInputStream fis = new FileInputStream(file); - BufferedInputStream bis = new BufferedInputStream(fis); - ZipInputStream zis = new ZipInputStream(bis); - List fileUrls = ArchiveUtils.extractArchive(installTempDir, zis); + File file = ZipTestUtils.createTempZipFile(tempFolder.getRoot(), "ZipSlip.txt"); + List fileUrls; + try (ZipInputStream zis = openZip(file)) { + fileUrls = ArchiveUtils.extractArchive(installTempDir, zis); + } assertFalse(fileUrls.isEmpty()); - File extractedFile = new File("tests/zipextraction", "ZipSlip.txt"); - assertTrue(extractedFile.exists()); + assertTrue(Files.exists(installTempDir.toPath().resolve("ZipSlip.txt"))); } @Test public void testExtractEmptyZipFile() throws Exception { - File installTempDir = new File("tests/zipextraction"); - File file = ZipTestUtils.createTempZipFile(null); - FileInputStream fis = new FileInputStream(file); - BufferedInputStream bis = new BufferedInputStream(fis); - ZipInputStream zis = new ZipInputStream(bis); - List fileUrls = ArchiveUtils.extractArchive(installTempDir, zis); - assertTrue(fileUrls.isEmpty()); - } - - @Before - public void createTestFolder() { - File installTempDir = new File("tests/zipextraction"); - if (!installTempDir.exists()) { - installTempDir.mkdir(); - } else { - cleanupTestFolder(); + File file = ZipTestUtils.createTempZipFile(tempFolder.getRoot(), null); + List fileUrls; + try (ZipInputStream zis = openZip(file)) { + fileUrls = ArchiveUtils.extractArchive(installTempDir, zis); } + assertTrue(fileUrls.isEmpty()); } - @After - public void cleanupTestFolder() { - File tempDir = new File("tests/zipextraction"); - if (tempDir.exists()) { - for (File file : tempDir.listFiles()) { - file.delete(); - } - } + private static ZipInputStream openZip(File file) throws IOException { + return new ZipInputStream(new BufferedInputStream(Files.newInputStream(file.toPath()))); } } diff --git a/server/src/test/java/com/mirth/connect/util/ZipTestUtils.java b/server/src/test/java/com/mirth/connect/util/ZipTestUtils.java index ee552ab9cd..c72771624d 100644 --- a/server/src/test/java/com/mirth/connect/util/ZipTestUtils.java +++ b/server/src/test/java/com/mirth/connect/util/ZipTestUtils.java @@ -11,7 +11,8 @@ import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileOutputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -20,22 +21,20 @@ public class ZipTestUtils { public static File createTempZipFile(String fileName) throws Exception { - File tempFile = File.createTempFile("temp_zip", ".zip"); //write to system defined temp - FileOutputStream fos = new FileOutputStream(tempFile); - BufferedOutputStream bos = new BufferedOutputStream(fos); - ZipOutputStream zos = new ZipOutputStream(bos); - - if (!Strings.isNullOrEmpty(fileName)) { - try { - ZipEntry entry = new ZipEntry(fileName); - zos.putNextEntry(entry); + return createTempZipFile(null, fileName); // system defined temp + } + + public static File createTempZipFile(File directory, String fileName) throws Exception { + Path tempFile = directory == null ? Files.createTempFile("temp_zip", ".zip") : Files.createTempFile(directory.toPath(), "temp_zip", ".zip"); + + try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(tempFile)))) { + if (!Strings.isNullOrEmpty(fileName)) { + zos.putNextEntry(new ZipEntry(fileName)); zos.write("file contents".getBytes()); zos.closeEntry(); - } finally { - zos.close(); } } - return tempFile; + return tempFile.toFile(); } } From 1e014f03aff92b019d8222ee098dd1271ca739d3 Mon Sep 17 00:00:00 2001 From: Tony Germano Date: Sun, 15 Nov 2026 15:44:48 -0500 Subject: [PATCH 3/3] Rename FileConnectionTests so the Gradle test task runs it The test task only includes classes matching **/*Test.class, so FileConnectionTests was never run by ./gradlew test or in CI. It needs no database or other infrastructure and passes, so rename it to FileConnectionTest. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Tony Germano --- .../{FileConnectionTests.java => FileConnectionTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename server/src/test/java/com/mirth/connect/connectors/file/filesystems/{FileConnectionTests.java => FileConnectionTest.java} (96%) diff --git a/server/src/test/java/com/mirth/connect/connectors/file/filesystems/FileConnectionTests.java b/server/src/test/java/com/mirth/connect/connectors/file/filesystems/FileConnectionTest.java similarity index 96% rename from server/src/test/java/com/mirth/connect/connectors/file/filesystems/FileConnectionTests.java rename to server/src/test/java/com/mirth/connect/connectors/file/filesystems/FileConnectionTest.java index 8f980efeed..7334bcdf8b 100644 --- a/server/src/test/java/com/mirth/connect/connectors/file/filesystems/FileConnectionTests.java +++ b/server/src/test/java/com/mirth/connect/connectors/file/filesystems/FileConnectionTest.java @@ -32,7 +32,7 @@ import com.mirth.connect.connectors.file.FileConnectorException; -public class FileConnectionTests { +public class FileConnectionTest { @Rule public TemporaryFolder tempFolder = new TemporaryFolder();