1
0

Adding support for .NET 8.0 LTS, renaming private fields to start with underscore

This commit is contained in:
2023-12-29 01:58:40 +01:00
parent 8bd511a936
commit 99d3f7758a
59 changed files with 922 additions and 871 deletions

View File

@@ -10,22 +10,22 @@ namespace UnitTests.Common.Utilities
{
[TestClass]
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class CryptographyHelperTests
public partial class CryptographyHelperTests
{
private string keyFile;
private CryptographyHelper cryptoHelper;
private string _keyFile;
private CryptographyHelper _cryptoHelper;
[TestInitialize]
public void Initialize()
{
keyFile = Path.GetTempFileName();
cryptoHelper = new CryptographyHelper(keyFile);
_keyFile = Path.GetTempFileName();
_cryptoHelper = new CryptographyHelper(_keyFile);
}
[TestCleanup]
public void Cleanup()
{
File.Delete(keyFile);
File.Delete(_keyFile);
}
#region Static
@@ -40,12 +40,12 @@ namespace UnitTests.Common.Utilities
// arrange
using var _ = CryptographyHelperSaltMock.Create(0);
byte[] bytes = new byte[] { 0xaf, 0xfe };
byte[] bytes = [0xaf, 0xfe];
string str = "ABC";
string password1 = "P@ssw0rd!";
string password2 = "P@ssw0rd";
byte[] expectedBytes = new byte[] { 0x7c, 0x7b, 0x77, 0x56, 0x91, 0x1a, 0xd9, 0xc0, 0x72, 0x70, 0x36, 0x88, 0x9f, 0xb4, 0xb5, 0xbc };
byte[] expectedBytes = [0x7c, 0x7b, 0x77, 0x56, 0x91, 0x1a, 0xd9, 0xc0, 0x72, 0x70, 0x36, 0x88, 0x9f, 0xb4, 0xb5, 0xbc];
// act
byte[] cipherBytes1 = CryptographyHelper.AesEncrypt(bytes, password1);
@@ -68,12 +68,12 @@ namespace UnitTests.Common.Utilities
using var _ = CryptographyHelperSaltMock.Create(0);
string cipherStr = "ueLuhFNpCuYmx8v3hczHtg==";
byte[] cipherBytes = new byte[] { 0x7c, 0x7b, 0x77, 0x56, 0x91, 0x1a, 0xd9, 0xc0, 0x72, 0x70, 0x36, 0x88, 0x9f, 0xb4, 0xb5, 0xbc };
byte[] cipherBytes = [0x7c, 0x7b, 0x77, 0x56, 0x91, 0x1a, 0xd9, 0xc0, 0x72, 0x70, 0x36, 0x88, 0x9f, 0xb4, 0xb5, 0xbc];
string password1 = "P@ssw0rd!";
string password2 = "P@ssw0rd";
byte[] expectedBytes = new byte[] { 0xaf, 0xfe };
byte[] expectedBytes = [0xaf, 0xfe];
// act
byte[] plainBytes1 = CryptographyHelper.AesDecrypt(cipherBytes, password1);
@@ -104,7 +104,7 @@ namespace UnitTests.Common.Utilities
public void ShouldEncryptDecryptAesBytes()
{
// arrange
byte[] plain = new byte[] { 0xaf, 0xfe };
byte[] plain = [0xaf, 0xfe];
string password = "P@ssw0rd!";
// act
@@ -154,12 +154,12 @@ namespace UnitTests.Common.Utilities
// arrange
using var _ = CryptographyHelperSaltMock.Create(0);
byte[] bytes = new byte[] { 0xaf, 0xfe };
byte[] bytes = [0xaf, 0xfe];
string str = "ABC";
string password1 = "P@ssw0rd!";
string password2 = "P@ssw0rd";
byte[] expectedBytes = new byte[] { 0xbf, 0x59, 0x1f, 0x48, 0x69, 0xab, 0x18, 0xc7 };
byte[] expectedBytes = [0xbf, 0x59, 0x1f, 0x48, 0x69, 0xab, 0x18, 0xc7];
// act
byte[] cipherBytes1 = CryptographyHelper.TripleDesEncrypt(bytes, password1);
@@ -182,12 +182,12 @@ namespace UnitTests.Common.Utilities
using var _ = CryptographyHelperSaltMock.Create(0);
string cipherStr = "1l74soBuuEI=";
byte[] cipherBytes = new byte[] { 0xbf, 0x59, 0x1f, 0x48, 0x69, 0xab, 0x18, 0xc7 };
byte[] cipherBytes = [0xbf, 0x59, 0x1f, 0x48, 0x69, 0xab, 0x18, 0xc7];
string password1 = "P@ssw0rd!";
string password2 = "P@ssw0rd";
byte[] expectedBytes = new byte[] { 0xaf, 0xfe };
byte[] expectedBytes = [0xaf, 0xfe];
// act
byte[] plainBytes1 = CryptographyHelper.TripleDesDecrypt(cipherBytes, password1);
@@ -218,7 +218,7 @@ namespace UnitTests.Common.Utilities
public void ShouldEncryptDecryptTdesBytes()
{
// arrange
byte[] plain = new byte[] { 0xaf, 0xfe };
byte[] plain = [0xaf, 0xfe];
string password = "P@ssw0rd!";
// act
@@ -269,7 +269,7 @@ namespace UnitTests.Common.Utilities
{
// arrange
string text = "Hello World!";
byte[] bytes = new byte[] { 0xde, 0xad, 0xbe, 0xef };
byte[] bytes = [0xde, 0xad, 0xbe, 0xef];
string fileName = Path.GetTempFileName();
// act
@@ -291,7 +291,7 @@ namespace UnitTests.Common.Utilities
{
// arrange
string text = "Hello World!";
byte[] bytes = new byte[] { 0xde, 0xad, 0xbe, 0xef };
byte[] bytes = [0xde, 0xad, 0xbe, 0xef];
string fileName = Path.GetTempFileName();
// act
@@ -313,7 +313,7 @@ namespace UnitTests.Common.Utilities
{
// arrange
string text = "Hello World!";
byte[] bytes = new byte[] { 0xde, 0xad, 0xbe, 0xef };
byte[] bytes = [0xde, 0xad, 0xbe, 0xef];
string fileName = Path.GetTempFileName();
// act
@@ -335,7 +335,7 @@ namespace UnitTests.Common.Utilities
{
// arrange
string text = "Hello World!";
byte[] bytes = new byte[] { 0xde, 0xad, 0xbe, 0xef };
byte[] bytes = [0xde, 0xad, 0xbe, 0xef];
string fileName = Path.GetTempFileName();
// act
@@ -415,8 +415,8 @@ namespace UnitTests.Common.Utilities
Assert.AreEqual(length, str1.Length);
Assert.AreEqual(length, str2.Length);
Assert.IsFalse(str1 == str2);
Assert.IsFalse(Regex.IsMatch(str1, "[^0-9a-f]"));
Assert.IsFalse(Regex.IsMatch(str2, "[^0-9a-f]"));
Assert.IsFalse(RandomStringWithPoolRegex().IsMatch(str1));
Assert.IsFalse(RandomStringWithPoolRegex().IsMatch(str2));
}
#endregion Random
@@ -537,7 +537,6 @@ namespace UnitTests.Common.Utilities
Assert.IsTrue(!string.IsNullOrWhiteSpace(content));
}
[TestMethod]
public void ShouldEncryptAesUsingKeyFile()
{
@@ -545,11 +544,11 @@ namespace UnitTests.Common.Utilities
string str = "Hello World!";
byte[] bytes = CryptographyHelper.GetRandomBytes(32);
string password = File.ReadAllText(keyFile);
string password = File.ReadAllText(_keyFile);
// act
string cipherStr = cryptoHelper.EncryptAes(str);
byte[] cipherBytes = cryptoHelper.EncryptAes(bytes);
string cipherStr = _cryptoHelper.EncryptAes(str);
byte[] cipherBytes = _cryptoHelper.EncryptAes(bytes);
string plainStr = CryptographyHelper.AesDecrypt(cipherStr, password);
byte[] plainBytes = CryptographyHelper.AesDecrypt(cipherBytes, password);
@@ -568,14 +567,14 @@ namespace UnitTests.Common.Utilities
string str = "Hello World!";
byte[] bytes = CryptographyHelper.GetRandomBytes(32);
string password = File.ReadAllText(keyFile);
string password = File.ReadAllText(_keyFile);
// act
string cipherStr = CryptographyHelper.AesEncrypt(str, password);
byte[] cipherBytes = CryptographyHelper.AesEncrypt(bytes, password);
string plainStr = cryptoHelper.DecryptAes(cipherStr);
byte[] plainBytes = cryptoHelper.DecryptAes(cipherBytes);
string plainStr = _cryptoHelper.DecryptAes(cipherStr);
byte[] plainBytes = _cryptoHelper.DecryptAes(cipherBytes);
// assert
Assert.AreNotEqual(str, cipherStr);
@@ -591,11 +590,11 @@ namespace UnitTests.Common.Utilities
string str = "Hello World!";
byte[] bytes = CryptographyHelper.GetRandomBytes(32);
string password = File.ReadAllText(keyFile);
string password = File.ReadAllText(_keyFile);
// act
string cipherStr = cryptoHelper.EncryptTripleDes(str);
byte[] cipherBytes = cryptoHelper.EncryptTripleDes(bytes);
string cipherStr = _cryptoHelper.EncryptTripleDes(str);
byte[] cipherBytes = _cryptoHelper.EncryptTripleDes(bytes);
string plainStr = CryptographyHelper.TripleDesDecrypt(cipherStr, password);
byte[] plainBytes = CryptographyHelper.TripleDesDecrypt(cipherBytes, password);
@@ -614,14 +613,14 @@ namespace UnitTests.Common.Utilities
string str = "Hello World!";
byte[] bytes = CryptographyHelper.GetRandomBytes(32);
string password = File.ReadAllText(keyFile);
string password = File.ReadAllText(_keyFile);
// act
string cipherStr = CryptographyHelper.TripleDesEncrypt(str, password);
byte[] cipherBytes = CryptographyHelper.TripleDesEncrypt(bytes, password);
string plainStr = cryptoHelper.DecryptTripleDes(cipherStr);
byte[] plainBytes = cryptoHelper.DecryptTripleDes(cipherBytes);
string plainStr = _cryptoHelper.DecryptTripleDes(cipherStr);
byte[] plainBytes = _cryptoHelper.DecryptTripleDes(cipherBytes);
// assert
Assert.AreNotEqual(str, cipherStr);
@@ -630,6 +629,9 @@ namespace UnitTests.Common.Utilities
CollectionAssert.AreEqual(bytes, plainBytes);
}
[GeneratedRegex("[^0-9a-f]")]
private static partial Regex RandomStringWithPoolRegex();
#endregion Instance
}
}