From ea0982ecae9559110132b3460fe528545faf4065 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 21 May 2016 00:22:12 -0700 Subject: [PATCH] Fixed: Error messages when config file is empty or contains invalid characters Closes #1104 --- .../ConfigFileProviderTest.cs | 30 +++++++++++++++++-- .../Configuration/ConfigFileProvider.cs | 14 ++++++++- .../InvalidConfigFileException.cs | 4 +++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/NzbDrone.Common.Test/ConfigFileProviderTest.cs b/src/NzbDrone.Common.Test/ConfigFileProviderTest.cs index 76383d88e..31daee47d 100644 --- a/src/NzbDrone.Common.Test/ConfigFileProviderTest.cs +++ b/src/NzbDrone.Common.Test/ConfigFileProviderTest.cs @@ -17,17 +17,18 @@ namespace NzbDrone.Common.Test public class ConfigFileProviderTest : TestBase { private string _configFileContents; + private string _configFilePath; [SetUp] public void SetUp() { WithTempAsAppPath(); - var configFile = Mocker.Resolve().GetConfigPath(); + _configFilePath = Mocker.Resolve().GetConfigPath(); _configFileContents = null; - WithMockConfigFile(configFile); + WithMockConfigFile(_configFilePath); } protected void WithMockConfigFile(string configFile) @@ -187,5 +188,30 @@ public void SaveDictionary_should_only_save_specified_values() Subject.SslPort.Should().Be(sslPort); } + [Test] + public void should_throw_if_config_file_is_empty() + { + Mocker.GetMock() + .Setup(v => v.FileExists(_configFilePath)) + .Returns(true); + + Assert.Throws(() => Subject.GetValue("key", "value")); + } + + [Test] + public void should_throw_if_config_file_contains_only_null_character() + { + _configFileContents = "\0"; + + Assert.Throws(() => Subject.GetValue("key", "value")); + } + + [Test] + public void should_throw_if_config_file_contains_invalid_xml() + { + _configFileContents = "{ \"key\": \"value\" }"; + + Assert.Throws(() => Subject.GetValue("key", "value")); + } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs index b9c509e64..7db6d8508 100644 --- a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs +++ b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs @@ -348,6 +348,18 @@ private XDocument LoadConfigFile() { if (_diskProvider.FileExists(_configFile)) { + var contents = _diskProvider.ReadAllText(_configFile); + + if (contents.IsNullOrWhiteSpace()) + { + throw new InvalidConfigFileException($"{_configFile} is empty. Please delete the config file and Sonarr will recreate it."); + } + + if (contents.All(char.IsControl)) + { + throw new InvalidConfigFileException($"{_configFile} is corrupt. Please delete the config file and Sonarr will recreate it."); + } + return XDocument.Parse(_diskProvider.ReadAllText(_configFile)); } @@ -360,7 +372,7 @@ private XDocument LoadConfigFile() catch (XmlException ex) { - throw new InvalidConfigFileException(_configFile + " is invalid, please see the http://wiki.sonarr.tv for steps to resolve this issue.", ex); + throw new InvalidConfigFileException($"{_configFile} is corrupt is invalid. Please delete the config file and Sonarr will recreate it.", ex); } } diff --git a/src/NzbDrone.Core/Configuration/InvalidConfigFileException.cs b/src/NzbDrone.Core/Configuration/InvalidConfigFileException.cs index 7069749ae..1251cf30a 100644 --- a/src/NzbDrone.Core/Configuration/InvalidConfigFileException.cs +++ b/src/NzbDrone.Core/Configuration/InvalidConfigFileException.cs @@ -5,6 +5,10 @@ namespace NzbDrone.Core.Configuration { public class InvalidConfigFileException : NzbDroneException { + public InvalidConfigFileException(string message) : base(message) + { + } + public InvalidConfigFileException(string message, Exception innerException) : base(message, innerException) { }