public void Handling_namevaluesettings_forced_encryption_should_work()
        {
            /* Arrange */
            var crypto = A.Fake<ICryptoProvider>();
            A.CallTo(() => crypto.Encrypt(A<string>.Ignored)).ReturnsLazily(h => Convert.ToBase64String(Encoding.UTF8.GetBytes(h.Arguments[0].ToString())));
            A.CallTo(() => crypto.Decrypt(A<string>.Ignored)).ReturnsLazily(h => Encoding.UTF8.GetString(Convert.FromBase64String(h.Arguments[0].ToString())));
            A.CallTo(() => crypto.IsEncrypted(A<string>.Ignored)).Returns(true);

            var nv = new NameValueSettings();
            nv.SetSetting("Plain", "PlainText");
            nv.SetEncryptedSetting("Encrypted", "EncryptedText");

            var handler = new NameValueExtendedSectionHandler();

            /* Act */
            var node = handler.WriteSection("TestSection", nv, crypto, true);
            var result = handler.ReadSection(node, crypto, true) as NameValueSettings;
            var val1 = result.Get("Plain", null);
            var val2 = result.Get("Encrypted", null);
            var allEncrypted = result.IsEncrypted("Plain") && result.IsEncrypted("Encrypted");

            /* Assert */
            val1.Should().Be("PlainText");
            val2.Should().Be("EncryptedText");
            allEncrypted.Should().BeTrue();
        }
예제 #2
0
        public void Getting_an_invalid_type_should_throw()
        {
            /* Arrange */
            var nv = new NameValueSettings();

            /* Act */
            nv.SetSetting(TestSetting.StringValue, "somestring");

            Action a1 = () => nv.Get<int>(TestSetting.StringValue, 0);
            Action a2 = () => nv.Get<object>(TestSetting.StringValue, null);

            /* Assert */
            a1.ShouldThrow<ConfigException>();
            a2.ShouldThrow<ConfigException>();
        }
        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();
        }
        public void Normal_write_read_handling_with_null_encrypter_should_work()
        {
            /* Arrange */
            var nv = new NameValueSettings();
            nv.SetSetting("Plain", "PlainText");
            nv.SetEncryptedSetting("Encrypted", "EncryptedText");

            var handler = new NameValueExtendedSectionHandler();

            /* Act */
            var node = handler.WriteSection("TestSection", nv, null, false);
            var result = handler.ReadSection(node, null, false) as NameValueSettings;
            var val1 = result.Get("Plain", null);
            var val2 = result.Get("Encrypted", null);
            var isEnc1 = result.IsEncrypted("Plain");
            var isEnc2 = result.IsEncrypted("Encrypted");

            /* Assert */
            val1.Should().Be("PlainText");
            val2.Should().Be("EncryptedText");
            isEnc1.Should().BeFalse();
            isEnc2.Should().BeFalse();
        }
예제 #5
0
        public void Setting_and_getting_name_value_settings_should_work()
        {
            /* Arrange */
            var doc = TestConfigFactory.CreateConfig();

            var p = A.Fake<IConfigProvider>();
            A.CallTo(() => p.LoadConfig()).Returns(new[] { doc });

            var settingsOne = new NameValueCollection();
            settingsOne.Add("A", "ValueA");

            var settingsTwo = new NameValueSettings();
            settingsTwo.SetSetting("B", "ValueB");

            /* Act */
            var mgr = ConfigManager.Create(p);

            mgr.SetSettings(TestSection.SectionOne, settingsOne);
            mgr.SetSettings(TestSection.SectionTwo, settingsTwo);

            var readSectionOne = mgr.GetSettings<NameValueSettings>(TestSection.SectionOne);
            var readSectionTwo = mgr.GetSettings<NameValueSettings>(TestSection.SectionTwo);

            mgr.Reload();

            var readSectionOneAfterReload = mgr.GetSettings<NameValueSettings>(TestSection.SectionOne);
            var readSectionTwoAfterReload = mgr.GetSettings<NameValueSettings>(TestSection.SectionTwo);

            int sectionCount = mgr.Sections.Count();

            /* Assert */
            A.CallTo(() => p.SaveConfig(doc)).MustNotHaveHappened();

            readSectionOne.Should().NotBeNull();
            readSectionOne.Get("A", null).Should().Be("ValueA");

            readSectionTwo.Should().NotBeNull();
            readSectionTwo.Should().NotBeSameAs(settingsTwo);
            readSectionTwo.Get("B", null).Should().Be("ValueB");

            readSectionOneAfterReload.Should().NotBeNull();
            readSectionOneAfterReload.Get("A", null).Should().Be("ValueA");

            readSectionTwoAfterReload.Should().NotBeNull();
            readSectionTwoAfterReload.Get("B", null).Should().Be("ValueB");

            sectionCount.Should().Be(2);
        }
예제 #6
0
        public void Get_and_set_should_work_regardless_of_culture()
        {
            /* Arrange */
            var nv = new NameValueSettings();

            var expectedDate = DateTime.UtcNow;
            var expectedTime = new TimeSpan(5, 4, 3, 2, 1);
            var expectedGuid = Guid.NewGuid();

            /* Act */
            using (new CultureContext())
            {
                nv.SetSetting("StringValue", "SomeString");
                nv.SetEncryptedSetting("StringValueTwo", "SomeOtherString");
                nv.SetSetting(TestSetting.IntegerValue, 10);
                nv.SetSetting(TestSetting.BooleanValue, true);
                nv.SetSetting(TestSetting.DateTimeValue, expectedDate);
                nv.SetSetting(TestSetting.EnumValue, TestSetting.EnumValue);
                nv.SetSetting(TestSetting.TimeSpanValue, expectedTime);
                nv.SetEncryptedSetting(TestSetting.GuidValue, expectedGuid);
            }

            bool encryptedDate = nv.IsEncrypted(TestSetting.DateTimeValue);
            bool encryptedGuid = nv.IsEncrypted(TestSetting.GuidValue);
            bool valueExists = nv.HasSetting(TestSetting.BooleanValue);
            bool valueNoExists = nv.HasSetting("SomeOtherVal");

            string stringVal = nv.Get("StringValue", null);
            string stringVal2 = nv.Get<string>("StringValueTwo", null);
            int intVal = nv.Get<int>(TestSetting.IntegerValue, 0);
            bool boolVal = nv.Get<bool>(TestSetting.BooleanValue, false);
            DateTime dateVal = nv.Get<DateTime>(TestSetting.DateTimeValue, DateTime.MaxValue);
            TestSetting enumVal = nv.Get<TestSetting>(TestSetting.EnumValue, TestSetting.BooleanValue);
            TimeSpan timeVal = nv.Get<TimeSpan>(TestSetting.TimeSpanValue, TimeSpan.Zero);
            Guid guidVal = nv.Get<Guid>(TestSetting.GuidValue, Guid.Empty);

            /* Assert */
            stringVal.Should().Be("SomeString");
            stringVal2.Should().Be("SomeOtherString");
            intVal.Should().Be(10);
            boolVal.Should().Be(true);
            dateVal.Should().Be(expectedDate);
            enumVal.Should().Be(TestSetting.EnumValue);
            timeVal.Should().Be(expectedTime);
            guidVal.Should().Be(expectedGuid);

            encryptedDate.Should().BeFalse();
            encryptedGuid.Should().BeTrue();
            valueExists.Should().BeTrue();
            valueNoExists.Should().BeFalse();
        }
예제 #7
0
        public void Setting_empty_values_should_work()
        {
            /* Arrange */
            var nv = new NameValueSettings();

            /* Act */
            nv.SetSetting(TestSetting.StringValue, "SomeVal");
            nv.SetSetting(TestSetting.BooleanValue, true);

            nv.SetSetting(TestSetting.StringValue, null);
            nv.SetSetting(TestSetting.BooleanValue, string.Empty);

            var s1 = nv.Get(TestSetting.StringValue, "Def1");
            var s2 = nv.Get<bool>(TestSetting.BooleanValue, false);

            /* Assert */
            s1.Should().Be("Def1");
            s2.Should().BeFalse();
        }
예제 #8
0
        public void Removing_a_setting_should_work()
        {
            /* Arrange */
            var nv = new NameValueSettings();

            /* Act */
            nv.SetSetting(TestSetting.StringValue, "SomeVal");
            nv.SetSetting(TestSetting.BooleanValue, true);

            nv.RemoveSetting(TestSetting.StringValue);
            nv.RemoveSetting((Enum)null);
            nv.RemoveSetting((string)null);

            var s1 = nv.Get(TestSetting.StringValue, "Def1");
            var s2 = nv.Get<bool>(TestSetting.BooleanValue, false);

            /* Assert */
            s1.Should().Be("Def1");
            s2.Should().BeTrue();
        }
예제 #9
0
        public void Props_should_return_expected_values()
        {
            /* Arrange */
            var nv = new NameValueSettings();

            /* Act */
            nv.SetSetting(TestSetting.StringValue, "somestring");
            nv.SetSetting(TestSetting.BooleanValue, false);

            /* Assert */
            nv.Count.Should().Be(2);
            nv.RawSettings.Count.Should().Be(2);
            nv.SettingNames.Count.Should().Be(2);
        }
        /// <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;
        }