From 3b690dfc6ee447ece594cb915c512c9c5abddd04 Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Tue, 21 Apr 2020 12:53:36 +0200 Subject: [PATCH] More resilient loading of settings.json --- .../common/settings/SettingsProvider.java | 15 ++++++++++----- .../common/settings/SettingsJsonAdapterTest.java | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/main/commons/src/main/java/org/cryptomator/common/settings/SettingsProvider.java b/main/commons/src/main/java/org/cryptomator/common/settings/SettingsProvider.java index 7bb7aefa0..390e40f5a 100644 --- a/main/commons/src/main/java/org/cryptomator/common/settings/SettingsProvider.java +++ b/main/commons/src/main/java/org/cryptomator/common/settings/SettingsProvider.java @@ -10,7 +10,9 @@ package org.cryptomator.common.settings; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; import com.google.gson.JsonParseException; +import com.google.gson.JsonParser; import org.cryptomator.common.Environment; import org.cryptomator.common.LazyInitializer; import org.slf4j.Logger; @@ -77,12 +79,15 @@ public class SettingsProvider implements Supplier { LOG.debug("Attempting to load settings from {}", path); try (InputStream in = Files.newInputStream(path, StandardOpenOption.READ); // Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) { - Settings settings = gson.fromJson(reader, Settings.class); - if (settings == null) { - throw new IOException("Unexpected EOF"); + JsonElement json = JsonParser.parseReader(reader); + if (json.isJsonObject()) { + Settings settings = gson.fromJson(json, Settings.class); + LOG.info("Settings loaded from {}", path); + return Stream.of(settings); + } else { + LOG.warn("Invalid json file {}", path); + return Stream.empty(); } - LOG.info("Settings loaded from {}", path); - return Stream.of(settings); } catch (NoSuchFileException e) { return Stream.empty(); } catch (IOException e) { diff --git a/main/commons/src/test/java/org/cryptomator/common/settings/SettingsJsonAdapterTest.java b/main/commons/src/test/java/org/cryptomator/common/settings/SettingsJsonAdapterTest.java index 1d31e9cea..347ec1e45 100644 --- a/main/commons/src/test/java/org/cryptomator/common/settings/SettingsJsonAdapterTest.java +++ b/main/commons/src/test/java/org/cryptomator/common/settings/SettingsJsonAdapterTest.java @@ -7,6 +7,8 @@ package org.cryptomator.common.settings; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.io.IOException; @@ -34,4 +36,16 @@ public class SettingsJsonAdapterTest { Assertions.assertEquals(VolumeImpl.FUSE, settings.preferredVolumeImpl().get()); } + @ParameterizedTest(name = "fromJson() should throw IOException for input: {0}") + @ValueSource(strings = { + "", + "", + "{invalidjson}" + }) + public void testDeserializeMalformed(String input) { + Assertions.assertThrows(IOException.class, () -> { + adapter.fromJson(input); + }); + } + }