From 7ee0606306e302b2c66feefc52f8303dd77ec079 Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Wed, 21 Jan 2026 15:34:08 +0100 Subject: [PATCH] adjust unit tests --- .../launcher/AdminPropertiesSetter.java | 7 +++- .../launcher/AdminPropertiesSetterTest.java | 34 ++++++++++++++----- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/cryptomator/launcher/AdminPropertiesSetter.java b/src/main/java/org/cryptomator/launcher/AdminPropertiesSetter.java index 3f35529ec..53863e4e3 100644 --- a/src/main/java/org/cryptomator/launcher/AdminPropertiesSetter.java +++ b/src/main/java/org/cryptomator/launcher/AdminPropertiesSetter.java @@ -37,6 +37,7 @@ import java.util.Set; class AdminPropertiesSetter { private static final Logger LOG = EventualLogger.getInstance(); + private static final long MAX_CONFIG_SIZE_BYTES = 8192; private static final String LINUX_DIR = "/etc/cryptomator"; private static final String MAC_DIR = "/Library/Application Support/Cryptomator"; @@ -88,9 +89,13 @@ class AdminPropertiesSetter { return systemProps; } - private static Properties loadAdminProperties(Path adminPropertiesPath) { + //visible for testing + static Properties loadAdminProperties(Path adminPropertiesPath) { var adminProps = new Properties(); try (var reader = Files.newBufferedReader(adminPropertiesPath, StandardCharsets.UTF_8)) { + if(Files.size(adminPropertiesPath) >= MAX_CONFIG_SIZE_BYTES) { + throw new IOException("Config file %s exceeds maximum size of %d".formatted(adminPropertiesPath, MAX_CONFIG_SIZE_BYTES)); + } adminProps.load(reader); } catch (NoSuchFileException _) { //NO-OP diff --git a/src/test/java/org/cryptomator/launcher/AdminPropertiesSetterTest.java b/src/test/java/org/cryptomator/launcher/AdminPropertiesSetterTest.java index bb2ae97eb..9e8b4962b 100644 --- a/src/test/java/org/cryptomator/launcher/AdminPropertiesSetterTest.java +++ b/src/test/java/org/cryptomator/launcher/AdminPropertiesSetterTest.java @@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import java.io.IOException; +import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -27,7 +28,10 @@ public class AdminPropertiesSetterTest { @DisplayName("UTF-8 is supported") void loadUTF8Properties(@TempDir Path path) throws IOException { var config = path.resolve("config.properties"); - setupValidProperties(config); + try (var out = Files.newOutputStream(config, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) { + var bytes = PROPS.getBytes(StandardCharsets.UTF_8); + out.write(bytes); + } var properties = AdminPropertiesSetter.loadAdminProperties(config); Assertions.assertAll(List.of( // @@ -37,7 +41,7 @@ public class AdminPropertiesSetterTest { } @Test - @DisplayName("Loading not existing file") + @DisplayName("Loading not existing file returns empty properties") void loadNotExistingFile(@TempDir Path path) { var config = path.resolve("config.properties"); var properties = AdminPropertiesSetter.loadAdminProperties(config); @@ -45,15 +49,29 @@ public class AdminPropertiesSetterTest { } @Test - @DisplayName("Loading invalid properties file") + @DisplayName("Loading invalid file returns empty properties") void loadEmptyFile(@TempDir Path path) throws IOException { - } - - void setupValidProperties(Path p) throws IOException { - try (var out = Files.newOutputStream(p, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) { - var bytes = PROPS.getBytes(StandardCharsets.UTF_8); + var config = path.resolve("config.properties"); + try (var out = Files.newOutputStream(config, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) { + var bytes = "method=\\u2u20".getBytes(StandardCharsets.UTF_8); //only one "u" is allowed in a Unicode escape sequence out.write(bytes); } + + var properties = AdminPropertiesSetter.loadAdminProperties(config); + MatcherAssert.assertThat(properties, anEmptyMap()); + } + + @Test + @DisplayName("Loading too big file returns empty properties") + void loadTooBigFile(@TempDir Path path) throws IOException { + var config = path.resolve("config.properties"); + try (var channel = Files.newByteChannel(config, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) { + channel.position(10_000); + channel.write(ByteBuffer.wrap("test=test".getBytes())); + } + + var properties = AdminPropertiesSetter.loadAdminProperties(config); + MatcherAssert.assertThat(properties, anEmptyMap()); } }