Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .csharpierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# .csharpierignore
!**/*.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="8.8.0" />
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="xunit" Version="2.9.3" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using EncryptedConfigValue.AspNetCore.Test.Util;
using EncryptedConfigValue.Crypto;
using EncryptedConfigValue.Crypto.Util;
using FluentAssertions;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;
using Shouldly;
using System.Text.RegularExpressions;

namespace EncryptedConfigValue.AspNetCore.Test
{
Expand All @@ -25,25 +26,25 @@ public void TestDecryptionSucceeds()
.AddEncryptedConfigValueProvider()
.Build();

configuration["Unencrypted"].Should().Be("value");
configuration["Encrypted"].Should().Be("value");
configuration["EncryptedWithSingleQuote"].Should().Be("don't use quotes");
configuration["EncryptedWithDoubleQuote"].Should().Be("double quote is \"");
configuration["EncryptedMalformedYaml"].Should().Be("[oh dear");
configuration["Unencrypted"].ShouldBe("value");
configuration["Encrypted"].ShouldBe("value");
configuration["EncryptedWithSingleQuote"].ShouldBe("don't use quotes");
configuration["EncryptedWithDoubleQuote"].ShouldBe("double quote is \"");
configuration["EncryptedMalformedYaml"].ShouldBe("[oh dear");

configuration
.GetSection("ArrayWithSomeEncryptedValues")
.GetChildren()
.Select(x => x.Value)
.Should()
.BeEquivalentTo(new List<string> { "value", "value", "other value", "[oh dear" });
.ToList()
.ShouldBeEquivalentTo(new List<string> { "value", "value", "other value", "[oh dear" });

var person = new Person();
configuration
.GetSection("PocoWithEncryptedValues")
.Bind(person);
person.Username.Should().BeEquivalentTo("some-user");
person.Password.Should().BeEquivalentTo("value");
person.Username.ShouldBeEquivalentTo("some-user");
person.Password.ShouldBeEquivalentTo("value");
}

[Fact]
Expand All @@ -53,11 +54,10 @@ public void TestDecryptionFailsWithNiceMessage()
.AddJsonFile(Path.Combine("Resources", "testConfigWithError.json"), optional: false, reloadOnChange: false)
.AddEncryptedConfigValueProvider()
.Build();
act.Should()
.Throw<ConfigurationDecryptionException>()
.WithMessage($"Configuration decryption error at {Path.Combine("Resources", "testConfigWithError.json")} (*)")
.WithInnerExceptionExactly<StringSubstitutionException>()
.WithMessage("The value 'enc:ERROR' for field 'arrayWithSomeEncryptedValues:3' could not be replaced");
var ex = Should.Throw<ConfigurationDecryptionException>(act);
ex.Message.ShouldMatch($@"Configuration decryption error at {Regex.Escape(Path.Combine("Resources", "testConfigWithError.json"))} \(.*\)");
ex.InnerException.ShouldBeOfType<StringSubstitutionException>();
ex.InnerException.Message.ShouldBe("The value 'enc:ERROR' for field 'ArrayWithSomeEncryptedValues:3' could not be replaced");
}
}
}
20 changes: 10 additions & 10 deletions EncryptedConfigValue.AspNetCore.Test/VariableSubstitutionTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using EncryptedConfigValue.AspNetCore.Test.Util;
using EncryptedConfigValue.Crypto;
using FluentAssertions;
using Shouldly;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using System;
Expand Down Expand Up @@ -31,25 +31,25 @@ private static WebApplication CreateWebApplication()
[Fact]
public void TestCanDecryptValueInConfig()
{
aspnet.Configuration["Unencrypted"].Should().Be("value");
aspnet.Configuration["Encrypted"].Should().Be("value");
aspnet.Configuration["EncryptedWithSingleQuote"].Should().Be("don't use quotes");
aspnet.Configuration["EncryptedWithDoubleQuote"].Should().Be("double quote is \"");
aspnet.Configuration["EncryptedMalformedYaml"].Should().Be("[oh dear");
aspnet.Configuration["Unencrypted"].ShouldBe("value");
aspnet.Configuration["Encrypted"].ShouldBe("value");
aspnet.Configuration["EncryptedWithSingleQuote"].ShouldBe("don't use quotes");
aspnet.Configuration["EncryptedWithDoubleQuote"].ShouldBe("double quote is \"");
aspnet.Configuration["EncryptedMalformedYaml"].ShouldBe("[oh dear");

aspnet.Configuration
.GetSection("ArrayWithSomeEncryptedValues")
.GetChildren()
.Select(x => x.Value)
.Should()
.BeEquivalentTo(new List<string> { "value", "value", "other value", "[oh dear" });
.ToList()
.ShouldBeEquivalentTo(new List<string> { "value", "value", "other value", "[oh dear" });

var person = new Person();
aspnet.Configuration
.GetSection("PocoWithEncryptedValues")
.Bind(person);
person.Username.Should().BeEquivalentTo("some-user");
person.Password.Should().BeEquivalentTo("value");
person.Username.ShouldBeEquivalentTo("some-user");
person.Password.ShouldBeEquivalentTo("value");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using EncryptedConfigValue.Crypto;
using EncryptedConfigValue.Crypto.Algorithm;
using FluentAssertions;
using Shouldly;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -69,7 +69,7 @@ internal void WeEncryptAndPrintAValue(Algorithm algorithm)
var decryptionKey = keyPair.DecryptionKey;
var decryptedValue = configValue.Decrypt(decryptionKey);

decryptedValue.Should().BeEquivalentTo(plaintext);
decryptedValue.ShouldBeEquivalentTo(plaintext);
}

[Fact]
Expand Down Expand Up @@ -100,7 +100,7 @@ internal void WeFailIfTheKeyfileDoesNotExist()
command.Execute("--keyfile", tempFilePath, "--value", plaintext);
};

act.Should().Throw<FileNotFoundException>();
act.ShouldThrow<FileNotFoundException>();
}

public static IEnumerable<object[]> Data() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="8.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
8 changes: 4 additions & 4 deletions EncryptedConfigValue.Cli.Test/GenerateKeyCommandTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using EncryptedConfigValue.Crypto;
using EncryptedConfigValue.Crypto.Algorithm;
using FluentAssertions;
using Shouldly;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -42,7 +42,7 @@ public void WeGenerateAValidKey(Algorithm algorithm)
command.Execute("--algorithm", algorithm.ToString(), "--file", tempFilePath);

var keyPair = KeyFileUtils.KeyPairFromPath(tempFilePath);
keyPair.EncryptionKey.Type.Algorithm.Should().BeEquivalentTo(algorithm);
keyPair.EncryptionKey.Type.Algorithm.ShouldBeEquivalentTo(algorithm);
}

[Fact]
Expand All @@ -62,8 +62,8 @@ public void WeDoNotOverwriteAnExistingKeyfile()
{
command.Execute("--algorithm", algorithm.ToString(), "--file", tempFilePath);
};

act.Should().Throw<IOException>().WithMessage("*already exists*");
var ex = Should.Throw<IOException>(act);
ex.Message.ShouldMatch("(.*)already exists*");
}

public static IEnumerable<object[]> Data() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using EncryptedConfigValue.Crypto.Algorithm;
using EncryptedConfigValue.Crypto.Util;
using EncryptedConfigValue.Module;
using FluentAssertions;
using Shouldly;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -63,7 +63,7 @@ public void Dispose()
public void ConstantsAreNotModified()
{
substitutor.TryReplace("abc", out var output);
output.Should().BeEquivalentTo("abc");
output.ShouldBeEquivalentTo("abc");
}

[Fact]
Expand All @@ -76,30 +76,30 @@ public void InvalidEncryptedVariablesThrowStringSubstitutionException()
}
catch (StringSubstitutionException e)
{
e.Value.Should().BeEquivalentTo("enc:invalid-contents");
e.Field.Should().BeEmpty();
e.Value.ShouldBeEquivalentTo("enc:invalid-contents");
e.Field.ShouldBeEmpty();
}
}

[Fact]
public void NonEncryptedVariablesAreNotModified()
{
substitutor.TryReplace("${abc}", out var output);
output.Should().BeEquivalentTo("${abc}");
output.ShouldBeEquivalentTo("${abc}");
}

[Fact]
public void VariableIsDecrypted()
{
substitutor.TryReplace("${" + Encrypt("abc") + "}", out var output);
output.Should().BeEquivalentTo("abc");
output.ShouldBeEquivalentTo("abc");
}

[Fact]
public void VariableIsDecryptedWithRegex()
{
substitutor.TryReplace("${" + Encrypt("$5") + "}", out var output);
output.Should().Be("$5");
output.ShouldBe("$5");
}

[Fact]
Expand All @@ -110,7 +110,7 @@ public void DecryptsMultiple()
var hello = "${" + Encrypt("enc:hello") + "}";
var source = abc + ":" + def + '.' + hello;
substitutor.TryReplace(source, out var output);
output.Should().Be("abc:def.enc:hello");
output.ShouldBe("abc:def.enc:hello");
}

[Fact]
Expand All @@ -120,7 +120,7 @@ public void DecryptsWithPlaceholders()
var def = "${" + Encrypt("${enc:test}") + "}";
var source = abc + ":" + def;
substitutor.TryReplace(source, out var output);
output.Should().BeEquivalentTo("abc:${enc:test}");
output.ShouldBeEquivalentTo("abc:${enc:test}");
}

[SkippableTheory]
Expand All @@ -131,7 +131,7 @@ public void PropertyTestValues(string plaintext)
Skip.If(Encoding.UTF8.GetBytes(plaintext).Length > 190);
EnsureTestKeysExist();
substitutor.TryReplace("${" + Encrypt(plaintext) + "}", out var output);
output.Should().BeEquivalentTo(plaintext);
output.ShouldBeEquivalentTo(plaintext);
}

public static IEnumerable<object[]> PropertyTestData(int tries)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="8.8.0" />
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
Expand Down
4 changes: 2 additions & 2 deletions EncryptedConfigValue.Test/Aes/AesKeyTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using EncryptedConfigValue.Crypto.Algorithm.Aes;
using FluentAssertions;
using Shouldly;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Xunit;
Expand All @@ -19,7 +19,7 @@ public static byte[] NewSecretKey()
public void TestEqualityFromSameSecretKey()
{
var secretKey = NewSecretKey();
new AesKey(secretKey).Should().BeEquivalentTo(new AesKey(secretKey));
new AesKey(secretKey).ShouldBeEquivalentTo(new AesKey(secretKey));
}
}
}
16 changes: 9 additions & 7 deletions EncryptedConfigValue.Test/AlgorithmTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using EncryptedConfigValue.Crypto.Algorithm;
using FluentAssertions;
using Shouldly;
using System.Collections.Generic;
using Xunit;

Expand All @@ -15,7 +15,8 @@ internal void WeGenerateRandomKeys(Algorithm algorithm)
{
var keyPair1 = algorithm.NewKeyPair();
var keyPair2 = algorithm.NewKeyPair();
keyPair1.Should().NotBe(keyPair2).And.NotBeSameAs(keyPair2);
keyPair1.ShouldNotBe(keyPair2);
keyPair1.ShouldNotBeSameAs(keyPair2);
}

[Theory]
Expand All @@ -26,7 +27,7 @@ internal void WeCanEncryptAndDecrypt(Algorithm algorithm)
var encryptedValue = algorithm.NewEncrypter().Encrypt(keyPair.EncryptionKey, plaintext);
var decryptionKey = keyPair.DecryptionKey;
var decrypted = encryptedValue.Decrypt(decryptionKey);
decrypted.Should().Be(plaintext);
decrypted.ShouldBe(plaintext);
}

[Theory]
Expand All @@ -38,17 +39,18 @@ internal void TheSameStringEncryptsToDifferentCiphertexts(Algorithm algorithm)
var encrypted2 = algorithm.NewEncrypter().Encrypt(keyPair.EncryptionKey, plaintext);

// we don't want to leak that certain values are the same
encrypted1.Should().NotBe(encrypted2).And.NotBeSameAs(encrypted2);
encrypted1.ShouldNotBe(encrypted2);
encrypted1.ShouldNotBeSameAs(encrypted2);
// paranoia, let's say the equals method is badly behaved
encrypted1.GetHashCode().Should().NotBe(encrypted2.GetHashCode());
encrypted1.GetHashCode().ShouldNotBe(encrypted2.GetHashCode());

// we should naturally decrypt back to the same thing - the plaintext
var decryptionKey = keyPair.DecryptionKey;
var decryptedString1 = encrypted1.Decrypt(decryptionKey);
var decryptedString2 = encrypted2.Decrypt(decryptionKey);

decryptedString1.Should().Be(plaintext);
decryptedString2.Should().Be(plaintext);
decryptedString1.ShouldBe(plaintext);
decryptedString2.ShouldBe(plaintext);
}

public static IEnumerable<object[]> Data() => new[]
Expand Down
2 changes: 1 addition & 1 deletion EncryptedConfigValue.Test/EncryptedConfigValue.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="8.8.0" />
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="xunit" Version="2.9.3" />
Expand Down
10 changes: 5 additions & 5 deletions EncryptedConfigValue.Test/EncryptedValueCompatTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using EncryptedConfigValue.Crypto;
using FluentAssertions;
using Shouldly;
using Xunit;

namespace EncryptedConfigValue.Test
Expand All @@ -13,7 +13,7 @@ public void TestAesLegacyValue()
EncryptedValue ev = EncryptedValue.FromString(
"enc:QjR4AHIYoIzvjEHf53XETM3QYnCl1mgFYC51Q7x4ebwM+h3PHVqSt/1un/+KvpJ2mZfMH0tifu+htRVxEPyXmt88lyKB83Npe"
+ "sNJEoLFLL+wBWCkppaLRuc/1w==");
ev.Decrypt(kwa).Should().BeEquivalentTo("my secret. I don't want anyone to know this");
ev.Decrypt(kwa).ShouldBeEquivalentTo("my secret. I don't want anyone to know this");
}

[Fact]
Expand All @@ -23,7 +23,7 @@ public void TestAesNewValue()
EncryptedValue ev = EncryptedValue.FromString(
"enc:eyJ0eXBlIjoiQUVTIiwibW9kZSI6IkdDTSIsImNpcGhlcnRleHQiOiJNOTRrSXlvYTUrMloiLCJpdiI6InVBR3FSbFA5d2l6c"
+ "GRCMHoiLCJ0YWciOiJBQ1N1ekR3VFVMb21zanhwRk1rWUtBPT0ifQ==");
ev.Decrypt(kwa).Should().BeEquivalentTo("plaintext");
ev.Decrypt(kwa).ShouldBeEquivalentTo("plaintext");
}

[Fact]
Expand Down Expand Up @@ -51,7 +51,7 @@ public void TestRsaLegacyValue()
+ "qrb08s46hodTPDLU76JNrtaxlCssXYxFN/Ni8k95pKauwPxRfvTP0SUf7o9rsZrY6LdV9+M3y6mNrEIKevAZQZtNmvX"
+ "riclQGV1CwRzV/0sNVuTfNqNw0lDsI4hcvC26DhLrXla8jCUiKEYDFAqVr2DaTwtV3htxtCB36Jk6Lg5abdcc9B/ZqV"
+ "7lfUIddGEuXFzhz8KIIGtwVVXqis15Dw1ECSNJhicHZp43vSYN9y9NJTnvTAhCQ==");
ev.Decrypt(kwa).Should().BeEquivalentTo("my secret. I don't want anyone to know this");
ev.Decrypt(kwa).ShouldBeEquivalentTo("my secret. I don't want anyone to know this");
}

[Fact]
Expand Down Expand Up @@ -83,7 +83,7 @@ public void TestRsaNewValue()
+ "xROEVMNUVBWjJoNytscVY5Rmp0emxBdWtxbUp4OEVwMGhJbGhPUjIzNndJcWQwZHpBVzZnODYybENLb1Nob0dvS04y"
+ "dHR5alU2TURRUWo0a0hBNFdCbTBOdm9XREZHN1p3T1ozbDIwVnY3R1ZRPT0iLCJvYWVwLWFsZyI6IlNIQS0yNTYiLC"
+ "JtZGYxLWFsZyI6IlNIQS0yNTYifQ==");
ev.Decrypt(kwa).Should().BeEquivalentTo("plaintext");
ev.Decrypt(kwa).ShouldBeEquivalentTo("plaintext");
}
}
}
Loading
Loading