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(); }
public void Reading_a_value_that_where_decryption_fails_must_fail_gracefully() { /* 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)).Throws(new Exception()); A.CallTo(() => crypto.IsEncrypted(A<string>.Ignored)).Returns(true); var nv = new NameValueSettings(); 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; /* Assert */ handler.Errors.Should().ContainSingle(e => e.Code == ConfigErrorCode.InvalidConfigValue); }
public void Reading_a_section_with_one_encryption_and_writing_it_with_another_must_work() { /* Arrange */ var cryptoOne = A.Fake<ICryptoProvider>(); A.CallTo(() => cryptoOne.Encrypt(A<string>.Ignored)).ReturnsLazily(h => Convert.ToBase64String(Encoding.UTF8.GetBytes(h.Arguments[0].ToString()))); A.CallTo(() => cryptoOne.Decrypt(A<string>.Ignored)).ReturnsLazily(h => Encoding.UTF8.GetString(Convert.FromBase64String(h.Arguments[0].ToString()))); A.CallTo(() => cryptoOne.IsEncrypted(A<string>.Ignored)).Returns(true); var cryptoTwo = A.Fake<ICryptoProvider>(); A.CallTo(() => cryptoTwo.Encrypt(A<string>.Ignored)).ReturnsLazily(h => new string(h.Arguments[0].ToString().Reverse().ToArray())); A.CallTo(() => cryptoTwo.Decrypt(A<string>.Ignored)).ReturnsLazily(h => new string(h.Arguments[0].ToString().Reverse().ToArray())); A.CallTo(() => cryptoTwo.IsEncrypted(A<string>.Ignored)).Returns(true); var source = new NameValueSettings(); source.SetEncryptedSetting("Setting1", "SomeText"); var handler1 = new NameValueExtendedSectionHandler(); var handler2 = new NameValueExtendedSectionHandler(); /* Act */ var node = handler1.WriteSection("TestSection", source, cryptoOne, false); var midResult = handler1.ReadSection(node, cryptoOne, false) as NameValueSettings; node = handler2.WriteSection("TestSection", midResult, cryptoTwo, false); var result = handler2.ReadSection(node, cryptoTwo, false) as NameValueSettings; var val = result.Get("Setting1", null); /* Assert */ A.CallTo(() => cryptoOne.Encrypt(A<string>.Ignored)).MustHaveHappened(Repeated.Exactly.Once); A.CallTo(() => cryptoOne.Decrypt(A<string>.Ignored)).MustHaveHappened(Repeated.Exactly.Once); A.CallTo(() => cryptoTwo.Encrypt(A<string>.Ignored)).MustHaveHappened(Repeated.Exactly.Once); A.CallTo(() => cryptoTwo.Decrypt(A<string>.Ignored)).MustHaveHappened(Repeated.Exactly.Once); val.Should().Be("SomeText"); }
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(); }
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(); }