adjust unit tests

This commit is contained in:
Armin Schrenk
2026-01-21 15:34:08 +01:00
parent 046372f95b
commit 7ee0606306
2 changed files with 32 additions and 9 deletions

View File

@@ -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

View File

@@ -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());
}
}