Removed useless Bouncy Castle package and test

Migrated to Nunit
Refactor
This commit is contained in:
Geoffroy BONNEVILLE
2019-07-29 17:30:15 +02:00
parent 26e8e5c223
commit c4de2dd594
23 changed files with 381 additions and 423 deletions

View File

@@ -1,16 +1,15 @@
using System.IO; using System.IO;
using System.Security.Cryptography;
using System.Text; using System.Text;
using NUnit.Framework;
using ModernKeePassLib.Serialization; using ModernKeePassLib.Serialization;
using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Cryptography.Cipher; using ModernKeePassLib.Cryptography.Cipher;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
using Xunit;
using System.Security.Cryptography;
using ModernKeePassLib.Cryptography;
namespace ModernKeePassLib.Test.Cryptography.Cipher namespace ModernKeePassLib.Test.Cryptography.Cipher
{ {
[TestFixture]
public class AesTests public class AesTests
{ {
// Test vector (official ECB test vector #356) // Test vector (official ECB test vector #356)
@@ -27,7 +26,7 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}; };
[Fact] [Test]
public void TestEncryptStream() public void TestEncryptStream()
{ {
var a = CryptoUtil.CreateAes(); var a = CryptoUtil.CreateAes();
@@ -45,35 +44,20 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
iCrypt.TransformBlock(_pbTestData, 0, 16, _pbTestData, 0); iCrypt.TransformBlock(_pbTestData, 0, 16, _pbTestData, 0);
Assert.True(MemUtil.ArraysEqual(_pbTestData, _pbReferenceCt)); Assert.That(MemUtil.ArraysEqual(_pbTestData, _pbReferenceCt), Is.True);
} }
[Fact] [Test]
public void TestDecryptStream() public void TestDecryptStream()
{ {
// Possible Mono Bug? This only works with size >= 48 // Possible Mono Bug? This only works with size >= 48
using (var inStream = new MemoryStream(new byte[32])) using var inStream = new MemoryStream(new byte[32]);
{
inStream.Write(_pbReferenceCt, 0, _pbReferenceCt.Length); inStream.Write(_pbReferenceCt, 0, _pbReferenceCt.Length);
inStream.Position = 0; inStream.Position = 0;
var aes = new StandardAesEngine(); var aes = new StandardAesEngine();
using (var outStream = aes.DecryptStream(inStream, _pbTestKey, _pbIv)) using var outStream = aes.DecryptStream(inStream, _pbTestKey, _pbIv);
{
var outBytes = new BinaryReaderEx(outStream, Encoding.UTF8, string.Empty).ReadBytes(16); var outBytes = new BinaryReaderEx(outStream, Encoding.UTF8, string.Empty).ReadBytes(16);
Assert.True(MemUtil.ArraysEqual(outBytes, _pbTestData)); Assert.That(MemUtil.ArraysEqual(outBytes, _pbTestData), Is.True);
}
}
}
[Fact]
public void TestBouncyCastleAes()
{
var aesEngine = new AesEngine();
//var parametersWithIv = new ParametersWithIV(new KeyParameter(pbTestKey), pbIV);
aesEngine.Init(true, new KeyParameter(_pbTestKey));
Assert.Equal(_pbTestData.Length, aesEngine.GetBlockSize());
aesEngine.ProcessBlock(_pbTestData, 0, _pbTestData, 0);
Assert.True(MemUtil.ArraysEqual(_pbReferenceCt,_pbTestData));
} }
} }
} }

View File

@@ -4,13 +4,14 @@ using System.IO;
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Cryptography.Cipher; using ModernKeePassLib.Cryptography.Cipher;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography.Cipher namespace ModernKeePassLib.Test.Cryptography.Cipher
{ {
[TestFixture]
public class Chacha20Tests public class Chacha20Tests
{ {
[Fact] [Test]
public void TestChacha20Cipher() public void TestChacha20Cipher()
{ {
// ====================================================== // ======================================================
@@ -23,7 +24,7 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
pbIV[3] = 0x09; pbIV[3] = 0x09;
pbIV[7] = 0x4A; pbIV[7] = 0x4A;
var pbExpc = new byte[64] { var pbExpc = new byte[] {
0x10, 0xF1, 0xE7, 0xE4, 0xD1, 0x3B, 0x59, 0x15, 0x10, 0xF1, 0xE7, 0xE4, 0xD1, 0x3B, 0x59, 0x15,
0x50, 0x0F, 0xDD, 0x1F, 0xA3, 0x20, 0x71, 0xC4, 0x50, 0x0F, 0xDD, 0x1F, 0xA3, 0x20, 0x71, 0xC4,
0xC7, 0xD1, 0xF4, 0xC7, 0x33, 0xC0, 0x68, 0x03, 0xC7, 0xD1, 0xF4, 0xC7, 0x33, 0xC0, 0x68, 0x03,
@@ -36,12 +37,12 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
var pb = new byte[64]; var pb = new byte[64];
using (var c = new ChaCha20Cipher(pbKey, pbIV)) using (var chaCha20Cipher1 = new ChaCha20Cipher(pbKey, pbIV))
{ {
c.Seek(64, SeekOrigin.Begin); // Skip first block chaCha20Cipher1.Seek(64, SeekOrigin.Begin); // Skip first block
c.Encrypt(pb, 0, pb.Length); chaCha20Cipher1.Encrypt(pb, 0, pb.Length);
Assert.True(MemUtil.ArraysEqual(pb, pbExpc)); Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
} }
#if DEBUG #if DEBUG
@@ -74,12 +75,12 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
var pb64 = new byte[64]; var pb64 = new byte[64];
using (var c = new ChaCha20Cipher(pbKey, pbIV)) using (var chaCha20Cipher2 = new ChaCha20Cipher(pbKey, pbIV))
{ {
c.Encrypt(pb64, 0, pb64.Length); // Skip first block chaCha20Cipher2.Encrypt(pb64, 0, pb64.Length); // Skip first block
c.Encrypt(pb, 0, pb.Length); chaCha20Cipher2.Encrypt(pb, 0, pb.Length);
Assert.True(MemUtil.ArraysEqual(pb, pbExpc)); Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
} }
// ====================================================== // ======================================================
@@ -116,17 +117,17 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
using (var msEnc = new MemoryStream()) using (var msEnc = new MemoryStream())
{ {
using (var c = new ChaCha20Stream(msEnc, true, pbKey, pbIV)) using (var chaCha20Stream = new ChaCha20Stream(msEnc, true, pbKey, pbIV))
{ {
var r = CryptoRandom.NewWeakRandom(); var r = CryptoRandom.NewWeakRandom();
r.NextBytes(pb64); r.NextBytes(pb64);
c.Write(pb64, 0, pb64.Length); // Skip first block chaCha20Stream.Write(pb64, 0, pb64.Length); // Skip first block
var p = 0; var p = 0;
while (p < pb.Length) while (p < pb.Length)
{ {
var cb = r.Next(1, pb.Length - p + 1); var cb = r.Next(1, pb.Length - p + 1);
c.Write(pb, p, cb); chaCha20Stream.Write(pb, p, cb);
p += cb; p += cb;
} }
Debug.Assert(p == pb.Length); Debug.Assert(p == pb.Length);
@@ -134,27 +135,22 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
var pbEnc0 = msEnc.ToArray(); var pbEnc0 = msEnc.ToArray();
var pbEnc = MemUtil.Mid(pbEnc0, 64, pbEnc0.Length - 64); var pbEnc = MemUtil.Mid(pbEnc0, 64, pbEnc0.Length - 64);
Assert.True(MemUtil.ArraysEqual(pbEnc, pbExpc)); Assert.That(MemUtil.ArraysEqual(pbEnc, pbExpc), Is.True);
using (var msCT = new MemoryStream(pbEnc0, false)) using var msCT = new MemoryStream(pbEnc0, false);
{ using var cDec = new ChaCha20Stream(msCT, false, pbKey, pbIV);
using (var cDec = new ChaCha20Stream(msCT, false,
pbKey, pbIV))
{
var pbPT = MemUtil.Read(cDec, pbEnc0.Length); var pbPT = MemUtil.Read(cDec, pbEnc0.Length);
Assert.True(cDec.ReadByte() < 0); Assert.That(cDec.ReadByte(), Is.LessThan(0));
Assert.True(MemUtil.ArraysEqual(MemUtil.Mid(pbPT, 0, 64), pb64)); Assert.That(MemUtil.ArraysEqual(MemUtil.Mid(pbPT, 0, 64), pb64), Is.True);
Assert.True(MemUtil.ArraysEqual(MemUtil.Mid(pbPT, 64, pbEnc.Length), pb)); Assert.That(MemUtil.ArraysEqual(MemUtil.Mid(pbPT, 64, pbEnc.Length), pb), Is.True);
}
}
} }
// ====================================================== // ======================================================
// Test vector TC8 from RFC draft by J. Strombergson: // Test vector TC8 from RFC draft by J. Strombergson:
// https://tools.ietf.org/html/draft-strombergson-chacha-test-vectors-01 // https://tools.ietf.org/html/draft-strombergson-chacha-test-vectors-01
pbKey = new byte[32] { pbKey = new byte[] {
0xC4, 0x6E, 0xC1, 0xB1, 0x8C, 0xE8, 0xA8, 0x78, 0xC4, 0x6E, 0xC1, 0xB1, 0x8C, 0xE8, 0xA8, 0x78,
0x72, 0x5A, 0x37, 0xE7, 0x80, 0xDF, 0xB7, 0x35, 0x72, 0x5A, 0x37, 0xE7, 0x80, 0xDF, 0xB7, 0x35,
0x1F, 0x68, 0xED, 0x2E, 0x19, 0x4C, 0x79, 0xFB, 0x1F, 0x68, 0xED, 0x2E, 0x19, 0x4C, 0x79, 0xFB,
@@ -165,13 +161,13 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
// is used; this makes the RFC 7539 version of ChaCha20 // is used; this makes the RFC 7539 version of ChaCha20
// compatible with the original specification by // compatible with the original specification by
// D. J. Bernstein. // D. J. Bernstein.
pbIV = new byte[12] { 0x00, 0x00, 0x00, 0x00, pbIV = new byte[] { 0x00, 0x00, 0x00, 0x00,
0x1A, 0xDA, 0x31, 0xD5, 0xCF, 0x68, 0x82, 0x21 0x1A, 0xDA, 0x31, 0xD5, 0xCF, 0x68, 0x82, 0x21
}; };
pb = new byte[128]; pb = new byte[128];
pbExpc = new byte[128] { pbExpc = new byte[] {
0xF6, 0x3A, 0x89, 0xB7, 0x5C, 0x22, 0x71, 0xF9, 0xF6, 0x3A, 0x89, 0xB7, 0x5C, 0x22, 0x71, 0xF9,
0x36, 0x88, 0x16, 0x54, 0x2B, 0xA5, 0x2F, 0x06, 0x36, 0x88, 0x16, 0x54, 0x2B, 0xA5, 0x2F, 0x06,
0xED, 0x49, 0x24, 0x17, 0x92, 0x30, 0x2B, 0x00, 0xED, 0x49, 0x24, 0x17, 0x92, 0x30, 0x2B, 0x00,
@@ -191,12 +187,10 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
0x05, 0x3C, 0x84, 0xE4, 0x9A, 0x4A, 0x33, 0x32 0x05, 0x3C, 0x84, 0xE4, 0x9A, 0x4A, 0x33, 0x32
}; };
using (var c = new ChaCha20Cipher(pbKey, pbIV, true)) using var c = new ChaCha20Cipher(pbKey, pbIV, true);
{
c.Decrypt(pb, 0, pb.Length); c.Decrypt(pb, 0, pb.Length);
Assert.True(MemUtil.ArraysEqual(pb, pbExpc)); Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
}
#endif #endif
} }
} }

View File

@@ -3,13 +3,14 @@ using System.Collections.Generic;
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Cryptography.Cipher; using ModernKeePassLib.Cryptography.Cipher;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography.Cipher namespace ModernKeePassLib.Test.Cryptography.Cipher
{ {
[TestFixture]
public class Salsa20Tests public class Salsa20Tests
{ {
[Fact] [Test]
public void TestSalsa20Cipher() public void TestSalsa20Cipher()
{ {
var r = CryptoRandom.NewWeakRandom(); var r = CryptoRandom.NewWeakRandom();
@@ -31,7 +32,7 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
var pb = new byte[16]; var pb = new byte[16];
var c = new Salsa20Cipher(pbKey, pbIv); var c = new Salsa20Cipher(pbKey, pbIv);
c.Encrypt(pb, 0, pb.Length); c.Encrypt(pb, 0, pb.Length);
Assert.True(MemUtil.ArraysEqual(pb, pbExpected)); Assert.That(MemUtil.ArraysEqual(pb, pbExpected), Is.True);
// Extended test // Extended test
var pbExpected2 = new byte[] { var pbExpected2 = new byte[] {
@@ -46,12 +47,12 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
var nPos = Salsa20ToPos(c, r, pb.Length, 65536); var nPos = Salsa20ToPos(c, r, pb.Length, 65536);
Array.Clear(pb, 0, pb.Length); Array.Clear(pb, 0, pb.Length);
c.Encrypt(pb, 0, pb.Length); c.Encrypt(pb, 0, pb.Length);
Assert.True(MemUtil.ArraysEqual(pb, pbExpected2)); Assert.That(MemUtil.ArraysEqual(pb, pbExpected2), Is.True);
Salsa20ToPos(c, r, nPos + pb.Length, 131008); Salsa20ToPos(c, r, nPos + pb.Length, 131008);
Array.Clear(pb, 0, pb.Length); Array.Clear(pb, 0, pb.Length);
c.Encrypt(pb, 0, pb.Length); c.Encrypt(pb, 0, pb.Length);
Assert.True(MemUtil.ArraysEqual(pb, pbExpected3)); Assert.That(MemUtil.ArraysEqual(pb, pbExpected3), Is.True);
var d = new Dictionary<string, bool>(); var d = new Dictionary<string, bool>();
const int nRounds = 100; const int nRounds = 100;
@@ -62,7 +63,7 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
c.Encrypt(z, 0, z.Length); c.Encrypt(z, 0, z.Length);
d[MemUtil.ByteArrayToHexString(z)] = true; d[MemUtil.ByteArrayToHexString(z)] = true;
} }
Assert.Equal(nRounds, d.Count); Assert.That(d.Count, Is.EqualTo(nRounds));
} }
private static int Salsa20ToPos(Salsa20Cipher c, Random r, int nPos, private static int Salsa20ToPos(Salsa20Cipher c, Random r, int nPos,

View File

@@ -1,28 +1,29 @@
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography namespace ModernKeePassLib.Test.Cryptography
{ {
[TestFixture]
public class CryptoRandomStreamTests public class CryptoRandomStreamTests
{ {
private void TestGetRandomBytes(CryptoRandomStream stream) private void TestGetRandomBytes(CryptoRandomStream stream)
{ {
const uint length = 16; const uint length = 16;
var bytes1 = stream.GetRandomBytes(length); var bytes1 = stream.GetRandomBytes(length);
Assert.Equal(bytes1.Length, (int)length); Assert.That((int)length, Is.EqualTo(bytes1.Length));
var bytes2 = stream.GetRandomBytes(length); var bytes2 = stream.GetRandomBytes(length);
Assert.False(MemUtil.ArraysEqual(bytes2, bytes1)); Assert.That(MemUtil.ArraysEqual(bytes2, bytes1), Is.False);
} }
[Fact] [Test]
public void TestGetRandomBytesCrsAlgorithmSalsa20() public void TestGetRandomBytesCrsAlgorithmSalsa20()
{ {
var stream = new CryptoRandomStream(CrsAlgorithm.Salsa20, new byte[16]); var stream = new CryptoRandomStream(CrsAlgorithm.Salsa20, new byte[16]);
TestGetRandomBytes(stream); TestGetRandomBytes(stream);
} }
[Fact] [Test]
public void TestGetRandomBytesCrsAlgorithmArcFourVariant() public void TestGetRandomBytesCrsAlgorithmArcFourVariant()
{ {
var stream = new CryptoRandomStream(CrsAlgorithm.ArcFourVariant, new byte[16]); var stream = new CryptoRandomStream(CrsAlgorithm.ArcFourVariant, new byte[16]);
@@ -33,17 +34,17 @@ namespace ModernKeePassLib.Test.Cryptography
{ {
var value1 = stream.GetRandomUInt64(); var value1 = stream.GetRandomUInt64();
var value2 = stream.GetRandomUInt64(); var value2 = stream.GetRandomUInt64();
Assert.NotEqual(value2, value1); Assert.That(value2, Is.Not.EqualTo(value1));
} }
[Fact] [Test]
public void TestGetRandomInt64AlgorithmSalsa20() public void TestGetRandomInt64AlgorithmSalsa20()
{ {
var stream = new CryptoRandomStream(CrsAlgorithm.Salsa20, new byte[16]); var stream = new CryptoRandomStream(CrsAlgorithm.Salsa20, new byte[16]);
TestGetRandomInt64(stream); TestGetRandomInt64(stream);
} }
[Fact] [Test]
public void TestGetRandomInt64AlgorithmArcFourVariant() public void TestGetRandomInt64AlgorithmArcFourVariant()
{ {
var stream = new CryptoRandomStream(CrsAlgorithm.ArcFourVariant, new byte[16]); var stream = new CryptoRandomStream(CrsAlgorithm.ArcFourVariant, new byte[16]);

View File

@@ -1,28 +1,30 @@
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography namespace ModernKeePassLib.Test.Cryptography
{ {
[TestFixture]
public class CryptoRandomTests public class CryptoRandomTests
{ {
[Fact] [Test]
public void TestAddEntropy() public void TestAddEntropy()
{ {
// just making sure it does not throw an exception // just making sure it does not throw an exception
CryptoRandom.Instance.AddEntropy(new byte[1]); Assert.DoesNotThrow(() => CryptoRandom.Instance.AddEntropy(new byte[1]));
} }
[Fact] [Test]
public void TestGetRandomBytes() public void TestGetRandomBytes()
{ {
const int length = 32; const int length = 32;
var bytes1 = CryptoRandom.Instance.GetRandomBytes(length); var bytes1 = CryptoRandom.Instance.GetRandomBytes(length);
Assert.Equal(bytes1.Length, length);
var bytes2 = CryptoRandom.Instance.GetRandomBytes(length); var bytes2 = CryptoRandom.Instance.GetRandomBytes(length);
Assert.NotEqual(bytes2, bytes1);
Assert.That(length, Is.EqualTo(bytes1.Length));
Assert.That(bytes1, Is.Not.EqualTo(bytes2));
} }
[Fact] [Test]
public void TestGeneratedBytesCount() public void TestGeneratedBytesCount()
{ {
const int length = 1; const int length = 1;
@@ -30,7 +32,8 @@ namespace ModernKeePassLib.Test.Cryptography
var count1 = CryptoRandom.Instance.GeneratedBytesCount; var count1 = CryptoRandom.Instance.GeneratedBytesCount;
CryptoRandom.Instance.GetRandomBytes(length); CryptoRandom.Instance.GetRandomBytes(length);
var count2 = CryptoRandom.Instance.GeneratedBytesCount; var count2 = CryptoRandom.Instance.GeneratedBytesCount;
Assert.True(count2 > count1);
Assert.That(count2, Is.GreaterThan(count1));
} }
} }
} }

View File

@@ -1,24 +1,36 @@
using System; using System.Text;
using System.Text;
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Cryptography.Hash; using ModernKeePassLib.Cryptography.Hash;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography.Hash namespace ModernKeePassLib.Test.Cryptography.Hash
{ {
[TestFixture]
public class Blake2bTests public class Blake2bTests
{ {
[Fact] private Blake2b _blake2bHash;
[SetUp]
public void SetUp()
{
_blake2bHash = new Blake2b();
}
[TearDown]
public void TearDown()
{
_blake2bHash.Clear();
}
[Test]
public void TestBlake2bUtf8() public void TestBlake2bUtf8()
{ {
Blake2b h = new Blake2b();
// ====================================================== // ======================================================
// From https://tools.ietf.org/html/rfc7693 // From https://tools.ietf.org/html/rfc7693
byte[] pbData = StrUtil.Utf8.GetBytes("abc"); var pbData = StrUtil.Utf8.GetBytes("abc");
byte[] pbExpc = new byte[64] var pbExpc = new byte[]
{ {
0xBA, 0x80, 0xA5, 0x3F, 0x98, 0x1C, 0x4D, 0x0D, 0xBA, 0x80, 0xA5, 0x3F, 0x98, 0x1C, 0x4D, 0x0D,
0x6A, 0x27, 0x97, 0xB6, 0x9F, 0x12, 0xF6, 0xE9, 0x6A, 0x27, 0x97, 0xB6, 0x9F, 0x12, 0xF6, 0xE9,
@@ -30,18 +42,17 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
0xB9, 0x23, 0x86, 0xED, 0xD4, 0x00, 0x99, 0x23 0xB9, 0x23, 0x86, 0xED, 0xD4, 0x00, 0x99, 0x23
}; };
byte[] pbC = h.ComputeHash(pbData); var pbC = _blake2bHash.ComputeHash(pbData);
Assert.True(MemUtil.ArraysEqual(pbC, pbExpc)); Assert.That(MemUtil.ArraysEqual(pbC, pbExpc), Is.True);
} }
[Fact] [Test]
public void TestBlake2bEmpty() public void TestBlake2bEmpty()
{ {
// ====================================================== // ======================================================
// Computed using the official b2sum tool // Computed using the official b2sum tool
Blake2b h = new Blake2b();
var pbExpc = new byte[64] var pbExpc = new byte[]
{ {
0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03, 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72, 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
@@ -53,23 +64,22 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
}; };
var pbC = h.ComputeHash(new byte[0]); var pbC = _blake2bHash.ComputeHash(new byte[0]);
Assert.True(MemUtil.ArraysEqual(pbC, pbExpc)); Assert.That(MemUtil.ArraysEqual(pbC, pbExpc), Is.True);
} }
[Fact] [Test]
public void TestBlake2bString() public void TestBlake2bString()
{ {
// ====================================================== // ======================================================
// Computed using the official b2sum tool // Computed using the official b2sum tool
Blake2b h = new Blake2b();
string strS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:,;_-\r\n"; var strS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:,;_-\r\n";
StringBuilder sb = new StringBuilder(); var sb = new StringBuilder();
for (int i = 0; i < 1000; ++i) sb.Append(strS); for (var i = 0; i < 1000; ++i) sb.Append(strS);
var pbData = StrUtil.Utf8.GetBytes(sb.ToString()); var pbData = StrUtil.Utf8.GetBytes(sb.ToString());
var pbExpc = new byte[64] { var pbExpc = new byte[] {
0x59, 0x69, 0x8D, 0x3B, 0x83, 0xF4, 0x02, 0x4E, 0x59, 0x69, 0x8D, 0x3B, 0x83, 0xF4, 0x02, 0x4E,
0xD8, 0x99, 0x26, 0x0E, 0xF4, 0xE5, 0x9F, 0x20, 0xD8, 0x99, 0x26, 0x0E, 0xF4, 0xE5, 0x9F, 0x20,
0xDC, 0x31, 0xEE, 0x5B, 0x45, 0xEA, 0xBB, 0xFC, 0xDC, 0x31, 0xEE, 0x5B, 0x45, 0xEA, 0xBB, 0xFC,
@@ -80,21 +90,19 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
0x3F, 0x08, 0x8A, 0x93, 0xF8, 0x75, 0x91, 0xB0 0x3F, 0x08, 0x8A, 0x93, 0xF8, 0x75, 0x91, 0xB0
}; };
Random r = CryptoRandom.NewWeakRandom(); var r = CryptoRandom.NewWeakRandom();
int p = 0; var p = 0;
while (p < pbData.Length) while (p < pbData.Length)
{ {
int cb = r.Next(1, pbData.Length - p + 1); var cb = r.Next(1, pbData.Length - p + 1);
h.TransformBlock(pbData, p, cb, pbData, p); _blake2bHash.TransformBlock(pbData, p, cb, pbData, p);
p += cb; p += cb;
} }
Assert.Equal(p, pbData.Length); Assert.That(p, Is.EqualTo(pbData.Length));
h.TransformFinalBlock(new byte[0], 0, 0); _blake2bHash.TransformFinalBlock(new byte[0], 0, 0);
Assert.True(MemUtil.ArraysEqual(h.Hash, pbExpc)); Assert.That(MemUtil.ArraysEqual(_blake2bHash.Hash, pbExpc), Is.True);
h.Clear();
} }
} }
} }

View File

@@ -1,13 +1,15 @@
using ModernKeePassLib.Cryptography; using System.Linq;
using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using System.Security.Cryptography; using System.Security.Cryptography;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography.Hash namespace ModernKeePassLib.Test.Cryptography.Hash
{ {
[TestFixture]
public class HmacTests public class HmacTests
{ {
[Fact] [Test]
public void TestHmac1() public void TestHmac1()
{ {
// Test vectors from RFC 4231 // Test vectors from RFC 4231
@@ -25,7 +27,7 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
HmacEval(pbKey, pbMsg, pbExpc); HmacEval(pbKey, pbMsg, pbExpc);
} }
[Fact] [Test]
public void TestHmac2() public void TestHmac2()
{ {
var pbKey = new byte[131]; var pbKey = new byte[131];
@@ -43,65 +45,57 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
HmacEval(pbKey, pbMsg, pbExpc); HmacEval(pbKey, pbMsg, pbExpc);
} }
[Fact] [Test]
public void TestHmacSha1ComputeHash() public void TestHmacSha1ComputeHash()
{ {
var expectedHash = "AC2C2E614882CE7158F69B7E3B12114465945D01"; var expectedHash = "AC2C2E614882CE7158F69B7E3B12114465945D01";
var message = StrUtil.Utf8.GetBytes("testing123"); var message = StrUtil.Utf8.GetBytes("testing123");
var key = StrUtil.Utf8.GetBytes("hello"); var key = StrUtil.Utf8.GetBytes("hello");
using (var result = new HMACSHA1(key)) using var result = new HMACSHA1(key);
{ Assert.That(expectedHash, Is.EqualTo(ByteToString(result.ComputeHash(message))));
Assert.Equal(ByteToString(result.ComputeHash(message)), expectedHash);
}
} }
[Fact] [Test]
public void TestHmacSha256ComputeHash() public void TestHmacSha256ComputeHash()
{ {
var expectedHash = "09C1BD2DE4E5659C0EFAF9E6AE4723E9CF96B69609B4E562F6AFF1745D7BF4E0"; var expectedHash = "09C1BD2DE4E5659C0EFAF9E6AE4723E9CF96B69609B4E562F6AFF1745D7BF4E0";
var message = StrUtil.Utf8.GetBytes("testing123"); var message = StrUtil.Utf8.GetBytes("testing123");
var key = StrUtil.Utf8.GetBytes("hello"); var key = StrUtil.Utf8.GetBytes("hello");
using (var result = new HMACSHA256(key)) using var result = new HMACSHA256(key);
{ Assert.That(expectedHash, Is.EqualTo(ByteToString(result.ComputeHash(message))));
Assert.Equal(ByteToString(result.ComputeHash(message)), expectedHash);
}
} }
private static string ByteToString(byte[] buff) private static string ByteToString(byte[] buff)
{ {
string sbinary = ""; return buff.Aggregate("", (current, t) => current + t.ToString("X2"));
for (int i = 0; i < buff.Length; i++)
{
sbinary += buff[i].ToString("X2"); // hex format
}
return (sbinary);
} }
[Fact] [Test]
public void TestHmacOtp() [TestCase(0, ExpectedResult = "755224")]
[TestCase(1, ExpectedResult = "287082")]
[TestCase(2, ExpectedResult = "359152")]
[TestCase(3, ExpectedResult = "969429")]
[TestCase(4, ExpectedResult = "338314")]
[TestCase(5, ExpectedResult = "254676")]
[TestCase(6, ExpectedResult = "287922")]
[TestCase(7, ExpectedResult = "162583")]
[TestCase(8, ExpectedResult = "399871")]
[TestCase(9, ExpectedResult = "520489")]
public string TestHmacOtp(int factor)
{ {
var pbSecret = StrUtil.Utf8.GetBytes("12345678901234567890"); var pbSecret = StrUtil.Utf8.GetBytes("12345678901234567890");
var vExp = new []{ "755224", "287082", "359152", return HmacOtp.Generate(pbSecret, (ulong)factor, 6, false, -1);
"969429", "338314", "254676", "287922", "162583", "399871",
"520489" };
for (var i = 0; i < vExp.Length; ++i)
{
Assert.Equal(HmacOtp.Generate(pbSecret, (ulong)i, 6, false, -1), vExp[i]);
}
} }
private static void HmacEval(byte[] pbKey, byte[] pbMsg, private static void HmacEval(byte[] pbKey, byte[] pbMsg,
byte[] pbExpc) byte[] pbExpc)
{ {
using (var h = new HMACSHA256(pbKey)) using var h = new HMACSHA256(pbKey);
{
h.TransformBlock(pbMsg, 0, pbMsg.Length, pbMsg, 0); h.TransformBlock(pbMsg, 0, pbMsg.Length, pbMsg, 0);
h.TransformFinalBlock(new byte[0], 0, 0); h.TransformFinalBlock(new byte[0], 0, 0);
byte[] pbHash = h.Hash; var pbHash = h.Hash;
Assert.True(MemUtil.ArraysEqual(pbHash, pbExpc)); Assert.That(MemUtil.ArraysEqual(pbHash, pbExpc), Is.True);
// Reuse the object // Reuse the object
h.Initialize(); h.Initialize();
@@ -109,8 +103,7 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
h.TransformFinalBlock(new byte[0], 0, 0); h.TransformFinalBlock(new byte[0], 0, 0);
pbHash = h.Hash; pbHash = h.Hash;
Assert.True(MemUtil.ArraysEqual(pbHash, pbExpc)); Assert.That(MemUtil.ArraysEqual(pbHash, pbExpc), Is.True);
}
} }
} }
} }

View File

@@ -1,13 +1,15 @@
using ModernKeePassLib.Utility; using System.Linq;
using ModernKeePassLib.Utility;
using System.Security.Cryptography; using System.Security.Cryptography;
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography.Hash namespace ModernKeePassLib.Test.Cryptography.Hash
{ {
[TestFixture]
public class ShaManagedTests public class ShaManagedTests
{ {
[Fact] [Test]
public void TestSha256() public void TestSha256()
{ {
var r = CryptoRandom.NewWeakRandom(); var r = CryptoRandom.NewWeakRandom();
@@ -34,39 +36,31 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
pbH2 = h2.ComputeHash(pbData); pbH2 = h2.ComputeHash(pbData);
} }
Assert.True(MemUtil.ArraysEqual(pbH1, pbH2)); Assert.That(MemUtil.ArraysEqual(pbH1, pbH2), Is.True);
} }
[Fact] [Test]
public void TestSha256ComputeHash() public void TestSha256ComputeHash()
{ {
var expectedHash = "B822F1CD2DCFC685B47E83E3980289FD5D8E3FF3A82DEF24D7D1D68BB272EB32"; var expectedHash = "B822F1CD2DCFC685B47E83E3980289FD5D8E3FF3A82DEF24D7D1D68BB272EB32";
var message = StrUtil.Utf8.GetBytes("testing123"); var message = StrUtil.Utf8.GetBytes("testing123");
using (var result = new SHA256Managed()) using var result = new SHA256Managed();
{ Assert.That(expectedHash, Is.EqualTo(ByteToString(result.ComputeHash(message))));
Assert.Equal(ByteToString(result.ComputeHash(message)), expectedHash);
}
} }
[Fact] [Test]
public void TestSha512ComputeHash() public void TestSha512ComputeHash()
{ {
var expectedHash = "4120117B3190BA5E24044732B0B09AA9ED50EB1567705ABCBFA78431A4E0A96B1152ED7F4925966B1C82325E186A8100E692E6D2FCB6702572765820D25C7E9E"; var expectedHash = "4120117B3190BA5E24044732B0B09AA9ED50EB1567705ABCBFA78431A4E0A96B1152ED7F4925966B1C82325E186A8100E692E6D2FCB6702572765820D25C7E9E";
var message = StrUtil.Utf8.GetBytes("testing123"); var message = StrUtil.Utf8.GetBytes("testing123");
using (var result = new SHA512Managed()) using var result = new SHA512Managed();
{ Assert.That(expectedHash, Is.EqualTo(ByteToString(result.ComputeHash(message))));
Assert.Equal(ByteToString(result.ComputeHash(message)), expectedHash);
}
} }
private static string ByteToString(byte[] buff) private static string ByteToString(byte[] buff)
{ {
var sbinary = ""; var sbinary = buff.Aggregate("", (current, t) => current + t.ToString("X2"));
for (var i = 0; i < buff.Length; i++)
{
sbinary += buff[i].ToString("X2"); // hex format
}
return (sbinary); return (sbinary);
} }
} }

View File

@@ -1,16 +1,17 @@
using System.IO; using System.IO;
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography namespace ModernKeePassLib.Test.Cryptography
{ {
[TestFixture]
public class HashingStreamExTests public class HashingStreamExTests
{ {
const string data = "test"; private const string Data = "test";
// The expected hash includes the \n added by WriteLine // The expected hash includes the \n added by WriteLine
static readonly byte[] sha256HashOfData = private static readonly byte[] Sha256HashOfData =
{ {
0xf2, 0xca, 0x1b, 0xb6, 0xc7, 0xe9, 0x07, 0xd0, 0xf2, 0xca, 0x1b, 0xb6, 0xc7, 0xe9, 0x07, 0xd0,
0x6d, 0xaf, 0xe4, 0x68, 0x7e, 0x57, 0x9f, 0xce, 0x6d, 0xaf, 0xe4, 0x68, 0x7e, 0x57, 0x9f, 0xce,
@@ -18,63 +19,52 @@ namespace ModernKeePassLib.Test.Cryptography
0x22, 0xda, 0x52, 0xe6, 0xcc, 0xc2, 0x6f, 0xd2 0x22, 0xda, 0x52, 0xe6, 0xcc, 0xc2, 0x6f, 0xd2
}; };
[Fact] [Test]
public void TestRead() public void TestRead()
{ {
// if we use larger size, StreamReader will read past newline and cause bad hash // if we use larger size, StreamReader will read past newline and cause bad hash
var bytes = new byte[data.Length + 1]; var bytes = new byte[Data.Length + 1];
using (var ms = new MemoryStream(bytes)) using (var memoryStream1 = new MemoryStream(bytes))
{
using (var sw = new StreamWriter(ms))
{ {
using var sw = new StreamWriter(memoryStream1);
// set NewLine to ensure we don't run into cross-platform issues on Windows // set NewLine to ensure we don't run into cross-platform issues on Windows
sw.NewLine = "\n"; sw.NewLine = "\n";
sw.WriteLine(data); sw.WriteLine(Data);
} }
}
using (var ms = new MemoryStream(bytes)) using var memoryStream2 = new MemoryStream(bytes);
{ using var hs = new HashingStreamEx(memoryStream2, false, null);
using (var hs = new HashingStreamEx(ms, false, null))
{
using (var sr = new StreamReader(hs)) using (var sr = new StreamReader(hs))
{ {
var read = sr.ReadLine(); var read = sr.ReadLine();
Assert.Equal(read, data); Assert.That(read, Is.EqualTo(Data));
}
// When the StreamReader is disposed, it calls Dispose on the
//HasingStreamEx, which computes the hash.
Assert.True(MemUtil.ArraysEqual(hs.Hash, sha256HashOfData));
}
} }
// When the StreamReader is disposed, it calls Dispose on the HasingStreamEx, which computes the hash.
Assert.That(MemUtil.ArraysEqual(hs.Hash, Sha256HashOfData), Is.True);
} }
[Fact] [Test]
public void TestWrite() public void TestWrite()
{ {
var bytes = new byte[16]; var bytes = new byte[16];
using (var ms = new MemoryStream(bytes)) using (var memoryStream1 = new MemoryStream(bytes))
{
using (var hs = new HashingStreamEx(ms, true, null))
{ {
using var hs = new HashingStreamEx(memoryStream1, true, null);
using (var sw = new StreamWriter(hs)) using (var sw = new StreamWriter(hs))
{ {
// set NewLine to ensure we don't run into cross-platform issues on Windows // set NewLine to ensure we don't run into cross-platform issues on Windows
sw.NewLine = "\n"; sw.NewLine = "\n";
sw.WriteLine(data); sw.WriteLine(Data);
} }
// When the StreamWriter is disposed, it calls Dispose on the // When the StreamWriter is disposed, it calls Dispose on the HasingStreamEx, which computes the hash.
//HasingStreamEx, which computes the hash. Assert.True(MemUtil.ArraysEqual(hs.Hash, Sha256HashOfData));
Assert.True(MemUtil.ArraysEqual(hs.Hash, sha256HashOfData));
} }
}
using (var ms = new MemoryStream(bytes)) using var memoryStream2 = new MemoryStream(bytes);
{ using var sr = new StreamReader(memoryStream2);
using (var sr = new StreamReader(ms))
{
var read = sr.ReadLine(); var read = sr.ReadLine();
Assert.Equal(read, data);
} Assert.That(read, Is.EqualTo(Data));
}
} }
} }
} }

View File

@@ -1,30 +1,30 @@
using System.Text; using System.Text;
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography namespace ModernKeePassLib.Test.Cryptography
{ {
[TestFixture]
public class HmacOtpTests public class HmacOtpTests
{ {
// Using the test case from Appendix D of RFC 4226 // Using the test case from Appendix D of RFC 4226
const string secret = "12345678901234567890"; private const string Secret = "12345678901234567890";
static readonly string[] expectedHOTP = new string[] private static readonly string[] ExpectedHotp = {
{
"755224", "287082", "359152", "969429", "338314", "755224", "287082", "359152", "969429", "338314",
"254676", "287922", "162583", "399871", "520489" "254676", "287922", "162583", "399871", "520489"
}; };
[Fact] [Test]
public void TestGenerate() public void TestGenerate()
{ {
var secretBytes = Encoding.UTF8.GetBytes(secret); var secretBytes = Encoding.UTF8.GetBytes(Secret);
for (ulong i = 0; i < 10; i++) for (ulong i = 0; i < 10; i++)
{ {
var hotp = HmacOtp.Generate(secretBytes, i, 6, false, -1); var hotp = HmacOtp.Generate(secretBytes, i, 6, false, -1);
Assert.Equal(hotp, expectedHOTP[i]); Assert.That(ExpectedHotp[i], Is.EqualTo(hotp));
} }
} }
} }

View File

@@ -2,13 +2,14 @@
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Cryptography.KeyDerivation; using ModernKeePassLib.Cryptography.KeyDerivation;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography.KeyDerivation namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
{ {
[TestFixture]
public class AesKdfTests public class AesKdfTests
{ {
[Fact] [Test]
public void TestAesKdf() public void TestAesKdf()
{ {
// Up to KeePass 2.34, the OtpKeyProv plugin used the public // Up to KeePass 2.34, the OtpKeyProv plugin used the public
@@ -26,7 +27,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
var pbMan = new byte[pbKey.Length]; var pbMan = new byte[pbKey.Length];
Array.Copy(pbKey, pbMan, pbKey.Length); Array.Copy(pbKey, pbMan, pbKey.Length);
Assert.True(AesKdf.TransformKeyManaged(pbMan, pbSeed, uRounds)); Assert.That(AesKdf.TransformKeyManaged(pbMan, pbSeed, uRounds), Is.True);
pbMan = CryptoUtil.HashSha256(pbMan); pbMan = CryptoUtil.HashSha256(pbMan);
var kdf = new AesKdf(); var kdf = new AesKdf();
@@ -35,7 +36,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
p.SetByteArray(AesKdf.ParamSeed, pbSeed); p.SetByteArray(AesKdf.ParamSeed, pbSeed);
var pbKdf = kdf.Transform(pbKey, p); var pbKdf = kdf.Transform(pbKey, p);
Assert.True(MemUtil.ArraysEqual(pbMan, pbKdf)); Assert.That(MemUtil.ArraysEqual(pbMan, pbKdf), Is.True);
} }
} }
} }

View File

@@ -1,15 +1,16 @@
using ModernKeePassLib.Cryptography.KeyDerivation; using ModernKeePassLib.Cryptography.KeyDerivation;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography.KeyDerivation namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
{ {
[TestFixture]
public class Argon2Tests public class Argon2Tests
{ {
[Fact] [Test]
public void TestArgon2() public void TestArgon2()
{ {
Argon2Kdf kdf = new Argon2Kdf(); var kdf = new Argon2Kdf();
// ====================================================== // ======================================================
// From the official Argon2 1.3 reference code package // From the official Argon2 1.3 reference code package
@@ -19,37 +20,37 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
var p = kdf.GetDefaultParameters(); var p = kdf.GetDefaultParameters();
kdf.Randomize(p); kdf.Randomize(p);
Assert.Equal(0x13U, p.GetUInt32(Argon2Kdf.ParamVersion, 0)); Assert.That(p.GetUInt32(Argon2Kdf.ParamVersion, 0), Is.EqualTo(0x13U));
byte[] pbMsg = new byte[32]; var pbMsg = new byte[32];
for (int i = 0; i < pbMsg.Length; ++i) pbMsg[i] = 1; for (var i = 0; i < pbMsg.Length; ++i) pbMsg[i] = 1;
p.SetUInt64(Argon2Kdf.ParamMemory, 32 * 1024); p.SetUInt64(Argon2Kdf.ParamMemory, 32 * 1024);
p.SetUInt64(Argon2Kdf.ParamIterations, 3); p.SetUInt64(Argon2Kdf.ParamIterations, 3);
p.SetUInt32(Argon2Kdf.ParamParallelism, 4); p.SetUInt32(Argon2Kdf.ParamParallelism, 4);
byte[] pbSalt = new byte[16]; var pbSalt = new byte[16];
for (int i = 0; i < pbSalt.Length; ++i) pbSalt[i] = 2; for (var i = 0; i < pbSalt.Length; ++i) pbSalt[i] = 2;
p.SetByteArray(Argon2Kdf.ParamSalt, pbSalt); p.SetByteArray(Argon2Kdf.ParamSalt, pbSalt);
byte[] pbKey = new byte[8]; var pbKey = new byte[8];
for (int i = 0; i < pbKey.Length; ++i) pbKey[i] = 3; for (var i = 0; i < pbKey.Length; ++i) pbKey[i] = 3;
p.SetByteArray(Argon2Kdf.ParamSecretKey, pbKey); p.SetByteArray(Argon2Kdf.ParamSecretKey, pbKey);
byte[] pbAssoc = new byte[12]; var pbAssoc = new byte[12];
for (int i = 0; i < pbAssoc.Length; ++i) pbAssoc[i] = 4; for (var i = 0; i < pbAssoc.Length; ++i) pbAssoc[i] = 4;
p.SetByteArray(Argon2Kdf.ParamAssocData, pbAssoc); p.SetByteArray(Argon2Kdf.ParamAssocData, pbAssoc);
byte[] pbExpc = new byte[32] { var pbExpc = new byte[] {
0x51, 0x2B, 0x39, 0x1B, 0x6F, 0x11, 0x62, 0x97, 0x51, 0x2B, 0x39, 0x1B, 0x6F, 0x11, 0x62, 0x97,
0x53, 0x71, 0xD3, 0x09, 0x19, 0x73, 0x42, 0x94, 0x53, 0x71, 0xD3, 0x09, 0x19, 0x73, 0x42, 0x94,
0xF8, 0x68, 0xE3, 0xBE, 0x39, 0x84, 0xF3, 0xC1, 0xF8, 0x68, 0xE3, 0xBE, 0x39, 0x84, 0xF3, 0xC1,
0xA1, 0x3A, 0x4D, 0xB9, 0xFA, 0xBE, 0x4A, 0xCB 0xA1, 0x3A, 0x4D, 0xB9, 0xFA, 0xBE, 0x4A, 0xCB
}; };
byte[] pb = kdf.Transform(pbMsg, p); var pb = kdf.Transform(pbMsg, p);
Assert.True(MemUtil.ArraysEqual(pb, pbExpc)); Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
// ====================================================== // ======================================================
// From the official Argon2 1.3 reference code package // From the official Argon2 1.3 reference code package
@@ -57,7 +58,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
p.SetUInt32(Argon2Kdf.ParamVersion, 0x10); p.SetUInt32(Argon2Kdf.ParamVersion, 0x10);
pbExpc = new byte[32] { pbExpc = new byte[] {
0x96, 0xA9, 0xD4, 0xE5, 0xA1, 0x73, 0x40, 0x92, 0x96, 0xA9, 0xD4, 0xE5, 0xA1, 0x73, 0x40, 0x92,
0xC8, 0x5E, 0x29, 0xF4, 0x10, 0xA4, 0x59, 0x14, 0xC8, 0x5E, 0x29, 0xF4, 0x10, 0xA4, 0x59, 0x14,
0xA5, 0xDD, 0x1F, 0x5C, 0xBF, 0x08, 0xB2, 0x67, 0xA5, 0xDD, 0x1F, 0x5C, 0xBF, 0x08, 0xB2, 0x67,
@@ -66,7 +67,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
pb = kdf.Transform(pbMsg, p); pb = kdf.Transform(pbMsg, p);
Assert.True(MemUtil.ArraysEqual(pb, pbExpc)); Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
// ====================================================== // ======================================================
// From the official 'phc-winner-argon2-20151206.zip' // From the official 'phc-winner-argon2-20151206.zip'
@@ -74,7 +75,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
p.SetUInt64(Argon2Kdf.ParamMemory, 16 * 1024); p.SetUInt64(Argon2Kdf.ParamMemory, 16 * 1024);
pbExpc = new byte[32] { pbExpc = new byte[] {
0x57, 0xB0, 0x61, 0x3B, 0xFD, 0xD4, 0x13, 0x1A, 0x57, 0xB0, 0x61, 0x3B, 0xFD, 0xD4, 0x13, 0x1A,
0x0C, 0x34, 0x88, 0x34, 0xC6, 0x72, 0x9C, 0x2C, 0x0C, 0x34, 0x88, 0x34, 0xC6, 0x72, 0x9C, 0x2C,
0x72, 0x29, 0x92, 0x1E, 0x6B, 0xBA, 0x37, 0x66, 0x72, 0x29, 0x92, 0x1E, 0x6B, 0xBA, 0x37, 0x66,
@@ -83,7 +84,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
pb = kdf.Transform(pbMsg, p); pb = kdf.Transform(pbMsg, p);
Assert.True(MemUtil.ArraysEqual(pb, pbExpc)); Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
// ====================================================== // ======================================================
// Computed using the official 'argon2' application // Computed using the official 'argon2' application
@@ -100,7 +101,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
pbSalt = StrUtil.Utf8.GetBytes("somesalt"); pbSalt = StrUtil.Utf8.GetBytes("somesalt");
p.SetByteArray(Argon2Kdf.ParamSalt, pbSalt); p.SetByteArray(Argon2Kdf.ParamSalt, pbSalt);
pbExpc = new byte[32] { pbExpc = new byte[] {
0x29, 0xCB, 0xD3, 0xA1, 0x93, 0x76, 0xF7, 0xA2, 0x29, 0xCB, 0xD3, 0xA1, 0x93, 0x76, 0xF7, 0xA2,
0xFC, 0xDF, 0xB0, 0x68, 0xAC, 0x0B, 0x99, 0xBA, 0xFC, 0xDF, 0xB0, 0x68, 0xAC, 0x0B, 0x99, 0xBA,
0x40, 0xAC, 0x09, 0x01, 0x73, 0x42, 0xCE, 0xF1, 0x40, 0xAC, 0x09, 0x01, 0x73, 0x42, 0xCE, 0xF1,
@@ -109,12 +110,12 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
pb = kdf.Transform(pbMsg, p); pb = kdf.Transform(pbMsg, p);
Assert.True(MemUtil.ArraysEqual(pb, pbExpc)); Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
p.SetUInt64(Argon2Kdf.ParamMemory, (1 << 10) * 1024); // 1 MB p.SetUInt64(Argon2Kdf.ParamMemory, (1 << 10) * 1024); // 1 MB
p.SetUInt64(Argon2Kdf.ParamIterations, 3); p.SetUInt64(Argon2Kdf.ParamIterations, 3);
pbExpc = new byte[32] { pbExpc = new byte[] {
0x7A, 0xBE, 0x1C, 0x1C, 0x8D, 0x7F, 0xD6, 0xDC, 0x7A, 0xBE, 0x1C, 0x1C, 0x8D, 0x7F, 0xD6, 0xDC,
0x7C, 0x94, 0x06, 0x3E, 0xD8, 0xBC, 0xD8, 0x1C, 0x7C, 0x94, 0x06, 0x3E, 0xD8, 0xBC, 0xD8, 0x1C,
0x2F, 0x87, 0x84, 0x99, 0x12, 0x83, 0xFE, 0x76, 0x2F, 0x87, 0x84, 0x99, 0x12, 0x83, 0xFE, 0x76,
@@ -123,14 +124,14 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
pb = kdf.Transform(pbMsg, p); pb = kdf.Transform(pbMsg, p);
Assert.True(MemUtil.ArraysEqual(pb, pbExpc)); Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
// TODO: Out of memory exception // TODO: Out of memory exception
/*p.SetUInt64(Argon2Kdf.ParamMemory, (1 << 20) * 1024); // 1 GB p.SetUInt64(Argon2Kdf.ParamMemory, (1 << 20) * 1024); // 1 GB
p.SetUInt64(Argon2Kdf.ParamIterations, 2); p.SetUInt64(Argon2Kdf.ParamIterations, 2);
p.SetUInt32(Argon2Kdf.ParamParallelism, 3); p.SetUInt32(Argon2Kdf.ParamParallelism, 3);
pbExpc = new byte[32] { pbExpc = new byte[] {
0xE6, 0xE7, 0xCB, 0xF5, 0x5A, 0x06, 0x93, 0x05, 0xE6, 0xE7, 0xCB, 0xF5, 0x5A, 0x06, 0x93, 0x05,
0x32, 0xBA, 0x86, 0xC6, 0x1F, 0x45, 0x17, 0x99, 0x32, 0xBA, 0x86, 0xC6, 0x1F, 0x45, 0x17, 0x99,
0x65, 0x41, 0x77, 0xF9, 0x30, 0x55, 0x9A, 0xE8, 0x65, 0x41, 0x77, 0xF9, 0x30, 0x55, 0x9A, 0xE8,
@@ -139,7 +140,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
pb = kdf.Transform(pbMsg, p); pb = kdf.Transform(pbMsg, p);
Assert.IsTrue(MemUtil.ArraysEqual(pb, pbExpc));*/ Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
} }
} }
} }

View File

@@ -1,13 +1,14 @@
using ModernKeePassLib.Cryptography.KeyDerivation; using ModernKeePassLib.Cryptography.KeyDerivation;
using ModernKeePassLib.Keys; using ModernKeePassLib.Keys;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Keys namespace ModernKeePassLib.Test.Keys
{ {
[TestFixture]
public class CompositeKeyTests public class CompositeKeyTests
{ {
[Fact] [Test]
public void TestGenerateKey32() public void TestGenerateKey32()
{ {
var originalKey = new byte[32]; var originalKey = new byte[32];
@@ -21,14 +22,14 @@ namespace ModernKeePassLib.Test.Keys
const ulong rounds = 1; const ulong rounds = 1;
var composite = new CompositeKey(); var composite = new CompositeKey();
AesKdf kdf = new AesKdf(); var kdf = new AesKdf();
KdfParameters p = kdf.GetDefaultParameters(); var p = kdf.GetDefaultParameters();
p.SetUInt64(AesKdf.ParamRounds, rounds); p.SetUInt64(AesKdf.ParamRounds, rounds);
p.SetByteArray(AesKdf.ParamSeed, originalKey); p.SetByteArray(AesKdf.ParamSeed, originalKey);
var key = composite.GenerateKey32(p); var key = composite.GenerateKey32(p);
Assert.NotNull(key); Assert.That(key, Is.Not.Null);
var keyData = key.ReadData(); var keyData = key.ReadData();
Assert.True(MemUtil.ArraysEqual(keyData, expectedKey)); Assert.That(MemUtil.ArraysEqual(keyData, expectedKey), Is.True);
} }
} }
} }

View File

@@ -1,17 +1,18 @@
using ModernKeePassLib.Keys; using ModernKeePassLib.Keys;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Keys namespace ModernKeePassLib.Test.Keys
{ {
[TestFixture]
public class KcpCustomKeyTests public class KcpCustomKeyTests
{ {
static readonly byte[] testData = private static readonly byte[] TestData =
{ {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}; };
[Fact] [Test]
public void TestConstruct() public void TestConstruct()
{ {
var expectedHash = new byte[32] var expectedHash = new byte[32]
@@ -22,13 +23,13 @@ namespace ModernKeePassLib.Test.Keys
0xE5, 0xB2, 0x32, 0x8D, 0xE0, 0xE8, 0x3D, 0xFC 0xE5, 0xB2, 0x32, 0x8D, 0xE0, 0xE8, 0x3D, 0xFC
}; };
var key = new KcpCustomKey("test1", testData, false); var key = new KcpCustomKey("test1", TestData, false);
var keyData = key.KeyData.ReadData(); var keyData = key.KeyData.ReadData();
Assert.True(MemUtil.ArraysEqual(keyData, testData)); Assert.That(MemUtil.ArraysEqual(keyData, TestData), Is.True);
key = new KcpCustomKey("test2", testData, true); key = new KcpCustomKey("test2", TestData, true);
keyData = key.KeyData.ReadData(); keyData = key.KeyData.ReadData();
Assert.True(MemUtil.ArraysEqual(keyData, expectedHash)); Assert.That(MemUtil.ArraysEqual(keyData, expectedHash), Is.True);
} }
} }
} }

View File

@@ -4,12 +4,14 @@ using System.Threading.Tasks;
using ModernKeePassLib.Keys; using ModernKeePassLib.Keys;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Windows.Storage; using Windows.Storage;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Keys namespace ModernKeePassLib.Test.Keys
{ {
[TestFixture]
public class KcpKeyFileTests public class KcpKeyFileTests
{ {
private StorageFile _file;
private const string TestCreateFile = "TestCreate.xml"; private const string TestCreateFile = "TestCreate.xml";
private const string TestKey = "0123456789"; private const string TestKey = "0123456789";
@@ -24,7 +26,20 @@ namespace ModernKeePassLib.Test.Keys
private const string ExpectedFileEnd = "</Data>\r\n\t</Key>\r\n</KeyFile>"; private const string ExpectedFileEnd = "</Data>\r\n\t</Key>\r\n</KeyFile>";
[Fact] [SetUp]
public async Task SetUp()
{
var folder = await StorageFolder.GetFolderFromPathAsync(Path.GetTempPath());
_file = await folder.CreateFileAsync(TestCreateFile, CreationCollisionOption.ReplaceExisting);
}
[TearDown]
public async Task TearDown()
{
await _file.DeleteAsync();
}
[Test]
public async Task TestConstruct() public async Task TestConstruct()
{ {
var expectedKeyData = new byte[] var expectedKeyData = new byte[]
@@ -35,9 +50,7 @@ namespace ModernKeePassLib.Test.Keys
0x45, 0xfc, 0xc8, 0x92, 0xbd, 0xeb, 0xaf, 0xc3 0x45, 0xfc, 0xc8, 0x92, 0xbd, 0xeb, 0xaf, 0xc3
}; };
var folder = await StorageFolder.GetFolderFromPathAsync(Path.GetTempPath()); await using (var fs = await _file.OpenStreamForWriteAsync())
var file = await folder.CreateFileAsync(TestCreateFile, CreationCollisionOption.ReplaceExisting);
await using (var fs = await file.OpenStreamForWriteAsync())
{ {
await using var sw = new StreamWriter(fs); await using var sw = new StreamWriter(fs);
sw.Write(ExpectedFileStart); sw.Write(ExpectedFileStart);
@@ -45,36 +58,20 @@ namespace ModernKeePassLib.Test.Keys
sw.Write(ExpectedFileEnd); sw.Write(ExpectedFileEnd);
} }
try var keyFile = new KcpKeyFile(_file);
{
var keyFile = new KcpKeyFile(file);
var keyData = keyFile.KeyData.ReadData(); var keyData = keyFile.KeyData.ReadData();
Assert.True(MemUtil.ArraysEqual(keyData, expectedKeyData)); Assert.That(MemUtil.ArraysEqual(keyData, expectedKeyData), Is.True);
}
finally
{
await file.DeleteAsync();
}
} }
[Fact] [Test]
public async Task TestCreate() public async Task TestCreate()
{ {
var folder = await StorageFolder.GetFolderFromPathAsync(Path.GetTempPath()); KcpKeyFile.Create(_file, null);
var file = await folder.CreateFileAsync(TestCreateFile, CreationCollisionOption.ReplaceExisting); var fileContents = await FileIO.ReadTextAsync(_file);
KcpKeyFile.Create(file, null);
try
{
var fileContents = await FileIO.ReadTextAsync(file);
Assert.Equal(185, fileContents.Length); Assert.That(fileContents.Length, Is.EqualTo(185));
Assert.StartsWith(ExpectedFileStart, fileContents); Assert.That(fileContents.StartsWith(ExpectedFileStart), Is.True);
Assert.EndsWith(ExpectedFileEnd, fileContents); Assert.That(fileContents.EndsWith(ExpectedFileEnd), Is.True);
}
finally
{
await file.DeleteAsync();
}
} }
} }
} }

View File

@@ -1,14 +1,15 @@
using ModernKeePassLib.Keys; using ModernKeePassLib.Keys;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Keys namespace ModernKeePassLib.Test.Keys
{ {
[TestFixture]
public class KcpPasswordTests public class KcpPasswordTests
{ {
const string testPassword = "password"; private const string TestPassword = "password";
[Fact] [Test]
public void TestConstruct() public void TestConstruct()
{ {
var expectedHash = new byte[32] var expectedHash = new byte[32]
@@ -19,9 +20,9 @@ namespace ModernKeePassLib.Test.Keys
0x2A, 0x11, 0xEF, 0x72, 0x1D, 0x15, 0x42, 0xD8 0x2A, 0x11, 0xEF, 0x72, 0x1D, 0x15, 0x42, 0xD8
}; };
var key = new KcpPassword(testPassword); var key = new KcpPassword(TestPassword);
var keyData = key.KeyData.ReadData(); var keyData = key.KeyData.ReadData();
Assert.True(MemUtil.ArraysEqual(keyData, expectedHash)); Assert.That(MemUtil.ArraysEqual(keyData, expectedHash), Is.True);
} }
} }
} }

View File

@@ -8,13 +8,9 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5" /> <PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="System.Runtime.WindowsRuntime" Version="4.3.0" /> <PackageReference Include="System.Runtime.WindowsRuntime" Version="4.3.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -2,67 +2,68 @@
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Security; using ModernKeePassLib.Security;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Security namespace ModernKeePassLib.Test.Security
{ {
[TestFixture]
public class ProtectedObjectsTests public class ProtectedObjectsTests
{ {
private readonly Encoding _enc = StrUtil.Utf8; private readonly Encoding _enc = StrUtil.Utf8;
[Fact] [Test]
public void TestAreBinaryObjectsProtected() public void TestAreBinaryObjectsProtected()
{ {
var pbData = _enc.GetBytes("Test Test Test Test"); var pbData = _enc.GetBytes("Test Test Test Test");
var pb = new ProtectedBinary(true, pbData); var pb = new ProtectedBinary(true, pbData);
Assert.True(pb.IsProtected); Assert.That(pb.IsProtected, Is.True);
var pbDec = pb.ReadData(); var pbDec = pb.ReadData();
Assert.True(MemUtil.ArraysEqual(pbData, pbDec)); Assert.That(MemUtil.ArraysEqual(pbData, pbDec), Is.True);
Assert.True(pb.IsProtected); Assert.That(pb.IsProtected, Is.True);
var pbData2 = _enc.GetBytes("Test Test Test Test"); var pbData2 = _enc.GetBytes("Test Test Test Test");
var pbData3 = _enc.GetBytes("Test Test Test Test Test"); var pbData3 = _enc.GetBytes("Test Test Test Test Test");
var pb2 = new ProtectedBinary(true, pbData2); var pb2 = new ProtectedBinary(true, pbData2);
var pb3 = new ProtectedBinary(true, pbData3); var pb3 = new ProtectedBinary(true, pbData3);
Assert.True(pb.Equals(pb2)); Assert.That(pb.Equals(pb2), Is.True);
Assert.False(pb.Equals(pb3)); Assert.That(pb.Equals(pb3), Is.False);
Assert.False(pb2.Equals(pb3)); Assert.That(pb2.Equals(pb3), Is.False);
Assert.Equal(pb.GetHashCode(), pb2.GetHashCode()); Assert.That(pb.GetHashCode(), Is.EqualTo(pb2.GetHashCode()));
Assert.True(pb.Equals((object) pb2)); Assert.That(pb.Equals((object) pb2), Is.True);
Assert.False(pb.Equals((object) pb3)); Assert.That(pb.Equals((object) pb3), Is.False);
Assert.False(pb2.Equals((object) pb3)); Assert.That(pb2.Equals((object) pb3), Is.False);
} }
[Fact] [Test]
public void TestIsEmptyProtectedStringEmpty() public void TestIsEmptyProtectedStringEmpty()
{ {
var ps = new ProtectedString(); var ps = new ProtectedString();
Assert.Equal(0, ps.Length); Assert.That(ps.Length, Is.EqualTo(0));
Assert.True(ps.IsEmpty); Assert.That(ps.IsEmpty, Is.True);
Assert.Equal(0, ps.ReadString().Length); Assert.That(ps.ReadString().Length, Is.EqualTo(0));
} }
[Fact] [Test]
public void TestAreEqualStringsProtected() public void TestAreEqualStringsProtected()
{ {
var ps = new ProtectedString(true, "Test"); var ps = new ProtectedString(true, "Test");
var ps2 = new ProtectedString(true, _enc.GetBytes("Test")); var ps2 = new ProtectedString(true, _enc.GetBytes("Test"));
Assert.False(ps.IsEmpty); Assert.That(ps.IsEmpty, Is.False);
var pbData = ps.ReadUtf8(); var pbData = ps.ReadUtf8();
var pbData2 = ps2.ReadUtf8(); var pbData2 = ps2.ReadUtf8();
Assert.True(MemUtil.ArraysEqual(pbData, pbData2)); Assert.That(MemUtil.ArraysEqual(pbData, pbData2), Is.True);
Assert.Equal(4, pbData.Length); Assert.That(pbData.Length, Is.EqualTo(4));
Assert.Equal(ps.ReadString(), ps2.ReadString()); Assert.That(ps.ReadString(), Is.EqualTo(ps2.ReadString()));
pbData = ps.ReadUtf8(); pbData = ps.ReadUtf8();
pbData2 = ps2.ReadUtf8(); pbData2 = ps2.ReadUtf8();
Assert.True(MemUtil.ArraysEqual(pbData, pbData2)); Assert.That(MemUtil.ArraysEqual(pbData, pbData2), Is.True);
Assert.True(ps.IsProtected); Assert.That(ps.IsProtected, Is.True);
Assert.True(ps2.IsProtected); Assert.That(ps2.IsProtected, Is.True);
} }
[Fact] [Test]
public void TestIsRandomStringProtected() public void TestIsRandomStringProtected()
{ {
var r = CryptoRandom.NewWeakRandom(); var r = CryptoRandom.NewWeakRandom();
@@ -81,8 +82,8 @@ namespace ModernKeePassLib.Test.Security
str = str.Insert(x, strIns); str = str.Insert(x, strIns);
ps = ps.Insert(x, strIns); ps = ps.Insert(x, strIns);
Assert.Equal(bProt, ps.IsProtected); Assert.That(ps.IsProtected, Is.EqualTo(bProt));
Assert.Equal(str, ps.ReadString()); Assert.That(ps.ReadString(), Is.EqualTo(str));
ps = ps.WithProtection(bProt); ps = ps.WithProtection(bProt);
@@ -92,19 +93,19 @@ namespace ModernKeePassLib.Test.Security
str = str.Remove(x, c); str = str.Remove(x, c);
ps = ps.Remove(x, c); ps = ps.Remove(x, c);
Assert.Equal(bProt, ps.IsProtected); Assert.That(ps.IsProtected, Is.EqualTo(bProt));
Assert.Equal(str, ps.ReadString()); Assert.That(ps.ReadString(), Is.EqualTo(str));
} }
} }
[Fact] [Test]
public void TestAreConcatenatedStringsProtected() public void TestAreConcatenatedStringsProtected()
{ {
var ps = new ProtectedString(false, "ABCD"); var ps = new ProtectedString(false, "ABCD");
var ps2 = new ProtectedString(true, "EFG"); var ps2 = new ProtectedString(true, "EFG");
ps += (ps2 + "HI"); ps += (ps2 + "HI");
Assert.True(ps.Equals(new ProtectedString(true, "ABCDEFGHI"), true)); Assert.That(ps.Equals(new ProtectedString(true, "ABCDEFGHI"), true), Is.True);
Assert.True(ps.Equals(new ProtectedString(false, "ABCDEFGHI"), false)); Assert.That(ps.Equals(new ProtectedString(false, "ABCDEFGHI"), false), Is.True);
} }
} }
} }

View File

@@ -1,16 +1,16 @@
using System.IO; using System.IO;
using ModernKeePassLib.Serialization; using ModernKeePassLib.Serialization;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Serialization namespace ModernKeePassLib.Test.Serialization
{ {
[TestFixture]
public class HashedBlockStreamTests public class HashedBlockStreamTests
{ {
static readonly byte[] data = new byte[16]; private static readonly byte[] Data = new byte[16];
static readonly byte[] hashStreamData = new byte[] private static readonly byte[] HashStreamData = {
{
// The first 4 bytes are an integer indicating the block index // The first 4 bytes are an integer indicating the block index
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// Then the SHA-256 hash of the data // Then the SHA-256 hash of the data
@@ -34,38 +34,28 @@ namespace ModernKeePassLib.Test.Serialization
0x00, 0x00, 0x00, 0x00 0x00, 0x00, 0x00, 0x00
}; };
[Fact] [Test]
public void TestRead() public void TestRead()
{ {
using (var ms = new MemoryStream(hashStreamData)) using var ms = new MemoryStream(HashStreamData);
{ using var hbs = new HashedBlockStream(ms, false);
using (var hbs = new HashedBlockStream(ms, false)) using var br = new BinaryReader(hbs);
{ var bytes = br.ReadBytes(Data.Length);
using (var br = new BinaryReader(hbs)) Assert.That(MemUtil.ArraysEqual(bytes, Data), Is.True);
{
var bytes = br.ReadBytes(data.Length);
Assert.True(MemUtil.ArraysEqual(bytes, data));
Assert.Throws<EndOfStreamException>(() => br.ReadByte()); Assert.Throws<EndOfStreamException>(() => br.ReadByte());
} }
}
}
}
[Fact] [Test]
public void TestWrite() public void TestWrite()
{ {
var buffer = new byte[hashStreamData.Length]; var buffer = new byte[HashStreamData.Length];
using (var ms = new MemoryStream(buffer)) using var ms = new MemoryStream(buffer);
{
using (var hbs = new HashedBlockStream(ms, true)) using (var hbs = new HashedBlockStream(ms, true))
{ {
using (var bw = new BinaryWriter(hbs)) using var bw = new BinaryWriter(hbs);
{ bw.Write(Data);
bw.Write(data);
}
}
Assert.True(MemUtil.ArraysEqual(buffer, hashStreamData));
} }
Assert.That(MemUtil.ArraysEqual(buffer, HashStreamData), Is.True);
} }
} }
} }

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Drawing;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Text; using System.Text;
@@ -6,24 +7,25 @@ using ModernKeePassLib.Keys;
using ModernKeePassLib.Security; using ModernKeePassLib.Security;
using ModernKeePassLib.Serialization; using ModernKeePassLib.Serialization;
using ModernKeePassLib.Collections; using ModernKeePassLib.Collections;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Serialization namespace ModernKeePassLib.Test.Serialization
{ {
[TestFixture]
public class KdbxFileTests public class KdbxFileTests
{ {
const string TestLocalizedAppName = "My Localized App Name"; private const string TestLocalizedAppName = "My Localized App Name";
const string TestDatabaseName = "My Database Name"; private const string TestDatabaseName = "My Database Name";
const string TestDatabaseDescription = "My Database Description"; private const string TestDatabaseDescription = "My Database Description";
const string TestDefaultUserName = "My Default User Name"; private const string TestDefaultUserName = "My Default User Name";
const string TestColor = "#FF0000"; // Red private const string TestColor = "#FF0000"; // Red
const string TestRootGroupName = "My Root Group Name"; private const string TestRootGroupName = "My Root Group Name";
const string TestRootGroupNotes = "My Root Group Notes"; private const string TestRootGroupNotes = "My Root Group Notes";
const string TestRootGroupDefaultAutoTypeSequence = "My Root Group Default Auto Type Sequence"; private const string TestRootGroupDefaultAutoTypeSequence = "My Root Group Default Auto Type Sequence";
const string TestDatabase = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n" + private const string TestDatabase = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n" +
"<KeePassFile>\r\n" + "<KeePassFile>\r\n" +
"\t<Meta>\r\n" + "\t<Meta>\r\n" +
"\t\t<Generator>" + TestLocalizedAppName + "</Generator>\r\n" + "\t\t<Generator>" + TestLocalizedAppName + "</Generator>\r\n" +
@@ -34,8 +36,7 @@ namespace ModernKeePassLib.Test.Serialization
"\t\t<DefaultUserName>" + TestDefaultUserName + "</DefaultUserName>\r\n" + "\t\t<DefaultUserName>" + TestDefaultUserName + "</DefaultUserName>\r\n" +
"\t\t<DefaultUserNameChanged>2017-10-23T08:03:55Z</DefaultUserNameChanged>\r\n" + "\t\t<DefaultUserNameChanged>2017-10-23T08:03:55Z</DefaultUserNameChanged>\r\n" +
"\t\t<MaintenanceHistoryDays>365</MaintenanceHistoryDays>\r\n" + "\t\t<MaintenanceHistoryDays>365</MaintenanceHistoryDays>\r\n" +
//"\t\t<Color>" + testColor + "</Color>\r\n" + "\t\t<Color>" + TestColor + "</Color>\r\n" +
"\t\t<Color></Color>\r\n" +
"\t\t<MasterKeyChanged>2017-10-23T08:03:55Z</MasterKeyChanged>\r\n" + "\t\t<MasterKeyChanged>2017-10-23T08:03:55Z</MasterKeyChanged>\r\n" +
"\t\t<MasterKeyChangeRec>-1</MasterKeyChangeRec>\r\n" + "\t\t<MasterKeyChangeRec>-1</MasterKeyChangeRec>\r\n" +
"\t\t<MasterKeyChangeForce>-1</MasterKeyChangeForce>\r\n" + "\t\t<MasterKeyChangeForce>-1</MasterKeyChangeForce>\r\n" +
@@ -83,9 +84,9 @@ namespace ModernKeePassLib.Test.Serialization
"\t</Root>\r\n" + "\t</Root>\r\n" +
"</KeePassFile>"; "</KeePassFile>";
const string TestDate = "2017-10-23T08:03:55Z"; private const string TestDate = "2017-10-23T08:03:55Z";
[Fact] [Test]
public void TestLoad() public void TestLoad()
{ {
var database = new PwDatabase(); var database = new PwDatabase();
@@ -94,13 +95,13 @@ namespace ModernKeePassLib.Test.Serialization
var file = new KdbxFile(database); var file = new KdbxFile(database);
file.Load(ms, KdbxFormat.PlainXml, null); file.Load(ms, KdbxFormat.PlainXml, null);
} }
//Assert.That(database.Color.ToArgb(), Is.EqualTo(Color.Red.ToArgb())); Assert.That(database.Color.ToArgb(), Is.EqualTo(Color.Red.ToArgb()));
Assert.Equal(PwCompressionAlgorithm.GZip, database.Compression); Assert.That(database.Compression, Is.EqualTo(PwCompressionAlgorithm.GZip));
//Assert.That (database.CustomData, Is.EqualTo ()); //Assert.That (database.CustomData, Is.EqualTo ());
Assert.True(database.CustomIcons.Count == 0); Assert.True(database.CustomIcons.Count == 0);
} }
[Fact] [Test]
public void TestSave() public void TestSave()
{ {
var buffer = new byte[4096]; var buffer = new byte[4096];
@@ -117,7 +118,7 @@ namespace ModernKeePassLib.Test.Serialization
database.DescriptionChanged = date; database.DescriptionChanged = date;
database.DefaultUserName = TestDefaultUserName; database.DefaultUserName = TestDefaultUserName;
database.DefaultUserNameChanged = date; database.DefaultUserNameChanged = date;
//database.Color = Color.Red; database.Color = Color.Red;
database.MasterKeyChanged = date; database.MasterKeyChanged = date;
database.RecycleBinChanged = date; database.RecycleBinChanged = date;
database.EntryTemplatesGroupChanged = date; database.EntryTemplatesGroupChanged = date;
@@ -140,10 +141,10 @@ namespace ModernKeePassLib.Test.Serialization
// so it uses native line endings. // so it uses native line endings.
fileContents = fileContents.Replace("\n", "\r\n"); fileContents = fileContents.Replace("\n", "\r\n");
} }
Assert.Equal(fileContents, TestDatabase); Assert.That(TestDatabase, Is.EqualTo(fileContents));
} }
[Fact] [Test]
public void TestSearch() public void TestSearch()
{ {
var database = new PwDatabase(); var database = new PwDatabase();
@@ -158,13 +159,13 @@ namespace ModernKeePassLib.Test.Serialization
}; };
var listStorage = new PwObjectList<PwEntry>(); var listStorage = new PwObjectList<PwEntry>();
database.RootGroup.SearchEntries(sp, listStorage); database.RootGroup.SearchEntries(sp, listStorage);
Assert.Equal(0U, listStorage.UCount); Assert.That(listStorage.UCount, Is.EqualTo(0U));
var entry = new PwEntry(true, true); var entry = new PwEntry(true, true);
entry.Strings.Set("Title", new ProtectedString(false, "NaMe")); entry.Strings.Set("Title", new ProtectedString(false, "NaMe"));
database.RootGroup.AddEntry(entry, true); database.RootGroup.AddEntry(entry, true);
sp.SearchString = "name"; sp.SearchString = "name";
database.RootGroup.SearchEntries(sp, listStorage); database.RootGroup.SearchEntries(sp, listStorage);
Assert.Equal(1U, listStorage.UCount); Assert.That(listStorage.UCount, Is.EqualTo(1U));
} }
} }
} }

View File

@@ -1,9 +1,8 @@
using System; using NUnit.Framework;
using ModernKeePassLib.Utility;
using Xunit;
namespace ModernKeePassLib.Test.Utility namespace ModernKeePassLib.Test.Utility
{ {
[TestFixture]
public class GfxUtilTests public class GfxUtilTests
{ {
// 16x16 all white PNG file, base64 encoded // 16x16 all white PNG file, base64 encoded
@@ -13,7 +12,7 @@ namespace ModernKeePassLib.Test.Utility
"VkIHdpdGggR0lNUFeBDhcAAAAaSURBVCjPY/z//z8DKYCJgUQwqmFUw9DRAABVb" + "VkIHdpdGggR0lNUFeBDhcAAAAaSURBVCjPY/z//z8DKYCJgUQwqmFUw9DRAABVb" +
"QMdny4VogAAAABJRU5ErkJggg=="; "QMdny4VogAAAABJRU5ErkJggg==";
//[Fact] //[Test]
//public void TestLoadImage () //public void TestLoadImage ()
//{ //{
// var testData = Convert.FromBase64String (testImageData); // var testData = Convert.FromBase64String (testImageData);

View File

@@ -1,88 +1,89 @@
using System.Text; using System.Text;
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Xunit; using NUnit.Framework;
namespace ModernKeePassLib.Test.Utility namespace ModernKeePassLib.Test.Utility
{ {
[TestFixture]
public class MemUtilTests public class MemUtilTests
{ {
private byte[] _pb = CryptoRandom.Instance.GetRandomBytes((uint)CryptoRandom.NewWeakRandom().Next(0, 0x2FFFF)); private byte[] _pb = CryptoRandom.Instance.GetRandomBytes((uint)CryptoRandom.NewWeakRandom().Next(0, 0x2FFFF));
[Fact] [Test]
public void TestGzip() public void TestGzip()
{ {
var pbCompressed = MemUtil.Compress(_pb); var pbCompressed = MemUtil.Compress(_pb);
Assert.True(MemUtil.ArraysEqual(MemUtil.Decompress(pbCompressed), _pb)); Assert.True(MemUtil.ArraysEqual(MemUtil.Decompress(pbCompressed), _pb));
} }
[Fact] [Test]
public void TestMemUtil() public void TestMemUtil()
{ {
var enc = StrUtil.Utf8; var enc = StrUtil.Utf8;
_pb = enc.GetBytes("012345678901234567890a"); _pb = enc.GetBytes("012345678901234567890a");
var pbN = enc.GetBytes("9012"); var pbN = enc.GetBytes("9012");
Assert.Equal(9, MemUtil.IndexOf(_pb, pbN)); Assert.That(MemUtil.IndexOf(_pb, pbN), Is.EqualTo(9));
pbN = enc.GetBytes("01234567890123"); pbN = enc.GetBytes("01234567890123");
Assert.Equal(0, MemUtil.IndexOf(_pb, pbN)); Assert.That(MemUtil.IndexOf(_pb, pbN), Is.EqualTo(0));
pbN = enc.GetBytes("a"); pbN = enc.GetBytes("a");
Assert.Equal(21, MemUtil.IndexOf(_pb, pbN)); Assert.That(MemUtil.IndexOf(_pb, pbN), Is.EqualTo(21));
pbN = enc.GetBytes("0a"); pbN = enc.GetBytes("0a");
Assert.Equal(20, MemUtil.IndexOf(_pb, pbN)); Assert.That(MemUtil.IndexOf(_pb, pbN), Is.EqualTo(20));
pbN = enc.GetBytes("1"); pbN = enc.GetBytes("1");
Assert.Equal(1, MemUtil.IndexOf(_pb, pbN)); Assert.That(MemUtil.IndexOf(_pb, pbN), Is.EqualTo(1));
pbN = enc.GetBytes("b"); pbN = enc.GetBytes("b");
Assert.True(MemUtil.IndexOf(_pb, pbN) < 0); Assert.That(MemUtil.IndexOf(_pb, pbN), Is.LessThan(0));
pbN = enc.GetBytes("012b"); pbN = enc.GetBytes("012b");
Assert.True(MemUtil.IndexOf(_pb, pbN) < 0); Assert.That(MemUtil.IndexOf(_pb, pbN), Is.LessThan(0));
} }
[Fact] [Test]
public void TestBase32() public void TestBase32()
{ {
var pbRes = MemUtil.ParseBase32("MY======"); var pbRes = MemUtil.ParseBase32("MY======");
var pbExp = Encoding.UTF8.GetBytes("f"); var pbExp = Encoding.UTF8.GetBytes("f");
Assert.True(MemUtil.ArraysEqual(pbRes, pbExp)); Assert.That(MemUtil.ArraysEqual(pbRes, pbExp), Is.True);
pbRes = MemUtil.ParseBase32("MZXQ===="); pbRes = MemUtil.ParseBase32("MZXQ====");
pbExp = Encoding.UTF8.GetBytes("fo"); pbExp = Encoding.UTF8.GetBytes("fo");
Assert.True(MemUtil.ArraysEqual(pbRes, pbExp)); Assert.That(MemUtil.ArraysEqual(pbRes, pbExp), Is.True);
pbRes = MemUtil.ParseBase32("MZXW6==="); pbRes = MemUtil.ParseBase32("MZXW6===");
pbExp = Encoding.UTF8.GetBytes("foo"); pbExp = Encoding.UTF8.GetBytes("foo");
Assert.True(MemUtil.ArraysEqual(pbRes, pbExp)); Assert.That(MemUtil.ArraysEqual(pbRes, pbExp), Is.True);
pbRes = MemUtil.ParseBase32("MZXW6YQ="); pbRes = MemUtil.ParseBase32("MZXW6YQ=");
pbExp = Encoding.UTF8.GetBytes("foob"); pbExp = Encoding.UTF8.GetBytes("foob");
Assert.True(MemUtil.ArraysEqual(pbRes, pbExp)); Assert.That(MemUtil.ArraysEqual(pbRes, pbExp), Is.True);
pbRes = MemUtil.ParseBase32("MZXW6YTB"); pbRes = MemUtil.ParseBase32("MZXW6YTB");
pbExp = Encoding.UTF8.GetBytes("fooba"); pbExp = Encoding.UTF8.GetBytes("fooba");
Assert.True(MemUtil.ArraysEqual(pbRes, pbExp)); Assert.That(MemUtil.ArraysEqual(pbRes, pbExp), Is.True);
pbRes = MemUtil.ParseBase32("MZXW6YTBOI======"); pbRes = MemUtil.ParseBase32("MZXW6YTBOI======");
pbExp = Encoding.UTF8.GetBytes("foobar"); pbExp = Encoding.UTF8.GetBytes("foobar");
Assert.True(MemUtil.ArraysEqual(pbRes, pbExp)); Assert.That(MemUtil.ArraysEqual(pbRes, pbExp), Is.True);
pbRes = MemUtil.ParseBase32("JNSXSIDQOJXXM2LEMVZCAYTBONSWIIDPNYQG63TFFV2GS3LFEBYGC43TO5XXEZDTFY======"); pbRes = MemUtil.ParseBase32("JNSXSIDQOJXXM2LEMVZCAYTBONSWIIDPNYQG63TFFV2GS3LFEBYGC43TO5XXEZDTFY======");
pbExp = Encoding.UTF8.GetBytes("Key provider based on one-time passwords."); pbExp = Encoding.UTF8.GetBytes("Key provider based on one-time passwords.");
Assert.True(MemUtil.ArraysEqual(pbRes, pbExp)); Assert.That(MemUtil.ArraysEqual(pbRes, pbExp), Is.True);
} }
[Fact] [Test]
public void TestMemUtil2() public void TestMemUtil2()
{ {
var i = 0 - 0x10203040; var i = 0 - 0x10203040;
var pbRes = MemUtil.Int32ToBytes(i); var pbRes = MemUtil.Int32ToBytes(i);
Assert.Equal("C0CFDFEF", MemUtil.ByteArrayToHexString(pbRes)); Assert.That(MemUtil.ByteArrayToHexString(pbRes), Is.EqualTo("C0CFDFEF"));
Assert.Equal(MemUtil.BytesToUInt32(pbRes), (uint)i); Assert.That(MemUtil.BytesToUInt32(pbRes), Is.EqualTo((uint)i));
Assert.Equal(MemUtil.BytesToInt32(pbRes), i); Assert.That(MemUtil.BytesToInt32(pbRes), Is.EqualTo(i));
} }
} }
} }

View File

@@ -69,7 +69,7 @@ namespace ModernKeePassLib.Utility
if(!l.Contains(UrlUtil.LocalDirSepChar)) if(!l.Contains(UrlUtil.LocalDirSepChar))
{ {
Debug.Assert(false); //Debug.Assert(false);
l.Add(UrlUtil.LocalDirSepChar); l.Add(UrlUtil.LocalDirSepChar);
} }