public void Get_and_set_and_remove_description_Should_work() { /* Arrange */ var nv = new NameValueSettings(); string expectedDescription = "this is a description"; /* Act */ nv.SetDescription(TestSetting.GuidValue, expectedDescription); nv.SetDescription(TestSetting.TimeSpanValue, expectedDescription); nv.RemoveDescription(TestSetting.TimeSpanValue); nv.RemoveDescription((Enum)null); nv.RemoveDescription((string)null); string desc1 = nv.GetDescription(TestSetting.GuidValue); string desc2 = nv.GetDescription(TestSetting.IntegerValue); string desc3 = nv.GetDescription(TestSetting.TimeSpanValue); /* Assert */ desc1.Should().Be(expectedDescription); desc2.Should().BeEmpty(); desc3.Should().BeEmpty(); }
public void Normal_write_read_namevaluesettings_handling_should_work() { /* Arrange */ var crypter = A.Fake<ICryptoProvider>(); A.CallTo(() => crypter.Encrypt(A<string>.Ignored)).ReturnsLazily(h => h.Arguments[0].ToString()); A.CallTo(() => crypter.Decrypt("PlainText")).Throws(new Exception()); A.CallTo(() => crypter.Decrypt("EncryptedText")).Returns("EncryptedText"); A.CallTo(() => crypter.IsEncrypted("PlainText")).Returns(false); A.CallTo(() => crypter.IsEncrypted("EncryptedText")).Returns(true); var nv = new NameValueSettings(); nv.SetSetting("Plain", "PlainText"); nv.SetEncryptedSetting("Encrypted", "EncryptedText"); nv.SetDescription("Encrypted", "This is some encrypted text"); var handler = new NameValueExtendedSectionHandler(); /* Act */ var node = handler.WriteSection("TestSection", nv, crypter, false); var result = handler.ReadSection(node, crypter, false) as NameValueSettings; var val1 = result.Get("Plain", null); var val2 = result.Get("Encrypted", null); var desc = result.GetDescription("Encrypted"); var isEnc1 = result.IsEncrypted("Plain"); var isEnc2 = result.IsEncrypted("Encrypted"); /* Assert */ val1.Should().Be("PlainText"); val2.Should().Be("EncryptedText"); desc.Should().Be("This is some encrypted text"); isEnc1.Should().BeFalse(); isEnc2.Should().BeTrue(); }
/// <summary> /// Creates a <see cref="NameValueSettings"/> from an xml element of a config file. /// <para> /// NOTE: The settings collection is case sensitive. /// </para> /// </summary> /// <param name="section">The XElement that contains the configuration information from the configuration file. Provides direct access to the XML contents of the configuration section</param> /// <param name="cryptoProvider">An <see cref="ICryptoProvider"/> instance that can be used to decrypt settings.</param> /// <param name="decryptAll">If <see langword="true" /> all settings will be decrypted. This is ignored since the <see cref="NameValueSettings"/> handles this by itself.</param> /// <returns>A <see cref="NameValueSettings"/> instance containing the configuration data, or empty if section is null or in fact empty.</returns> public object ReadSection(XElement section, ICryptoProvider cryptoProvider, bool decryptAll) { Ensure.ArgumentNotNull(section, "section"); _errors = null; //Create a case sensitive collection var data = new NameValueSettings(); if (section != null) { var settings = from node in section.Elements(ConfigElement.KeyValueSettingNode) let key = node.Attribute(ConfigElement.SettingKeyAttribute) let val = node.Attribute(ConfigElement.SettingValueAttribute) where key != null && val != null select new { Key = key.Value, Value = val.Value, Comment = node.PreviousNode as XComment }; foreach (var node in settings) { string val = node.Value; bool encrypted = false; if (cryptoProvider != null && cryptoProvider.IsEncrypted(val)) { try { val = cryptoProvider.Decrypt(val); encrypted = true; } catch { if (_errors == null) { _errors = new List<ConfigError>(); } _errors.Add( new ConfigError( ConfigErrorCode.InvalidConfigValue, string.Format("Reading value of setting {0} failed, value appears to be encrypted, but decryption failed.", node.Key))); continue; } } data.SetSetting(node.Key, val, encrypted); if (node.Comment != null) { data.SetDescription(node.Key, node.Comment.Value); } } } return data; }