mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 23:50:18 -04:00
Added unit tests (all passing unfortunately)
UI improvements Write mode still doesn't work
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
#if KeePassLib
|
||||
using KeePassLib.Cryptography.Cipher;
|
||||
#else
|
||||
using ModernKeePassLibPCL.Cryptography.Cipher;
|
||||
#endif
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace ModernKeePassLib.Test.Shared.Cryptography.Cipher
|
||||
{
|
||||
[TestFixture ()]
|
||||
public class StandardAesEngineTests
|
||||
{
|
||||
[Test ()]
|
||||
public void TestEncryptStream ()
|
||||
{
|
||||
// Test vector (official ECB test vector #356)
|
||||
var pbIV = new byte[16];
|
||||
var pbTestKey = new byte[32];
|
||||
var pbTestData = new byte[16];
|
||||
var pbReferenceCT = new byte[16] {
|
||||
0x75, 0xD1, 0x1B, 0x0E, 0x3A, 0x68, 0xC4, 0x22,
|
||||
0x3D, 0x88, 0xDB, 0xF0, 0x17, 0x97, 0x7D, 0xD7
|
||||
};
|
||||
|
||||
pbTestData[0] = 0x04;
|
||||
|
||||
var outStream = new MemoryStream (new byte[16]);
|
||||
var aes = new StandardAesEngine ();
|
||||
var inStream = aes.EncryptStream (outStream, pbTestKey, pbIV);
|
||||
new BinaryWriter (inStream).Write (pbTestData);
|
||||
Assert.That (outStream.Position, Is.EqualTo (16));
|
||||
outStream.Position = 0;
|
||||
var outBytes = new BinaryReader (outStream).ReadBytes (16);
|
||||
Assert.That(outBytes, Is.EqualTo (pbReferenceCT));
|
||||
}
|
||||
|
||||
[Test ()]
|
||||
public void TestDecryptStream ()
|
||||
{
|
||||
// Test vector (official ECB test vector #356)
|
||||
var pbIV = new byte[16];
|
||||
var pbTestKey = new byte[32];
|
||||
var pbTestData = new byte[16];
|
||||
var pbReferenceCT = new byte[16] {
|
||||
0x75, 0xD1, 0x1B, 0x0E, 0x3A, 0x68, 0xC4, 0x22,
|
||||
0x3D, 0x88, 0xDB, 0xF0, 0x17, 0x97, 0x7D, 0xD7
|
||||
};
|
||||
|
||||
pbTestData[0] = 0x04;
|
||||
|
||||
// Possible Mono Bug? This only works with size >= 48
|
||||
var inStream = new MemoryStream (new byte[48]);
|
||||
inStream.Write (pbReferenceCT, 0, pbReferenceCT.Length);
|
||||
inStream.Position = 0;
|
||||
var aes = new StandardAesEngine ();
|
||||
var outStream = aes.DecryptStream (inStream, pbTestKey, pbIV);
|
||||
var outBytes = new BinaryReader (outStream).ReadBytes (16);
|
||||
Assert.That(outBytes, Is.EqualTo (pbTestData));
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,60 @@
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
|
||||
#if KeePassLib
|
||||
using KeePassLib.Cryptography;
|
||||
#else
|
||||
using ModernKeePassLibPCL.Cryptography;
|
||||
#endif
|
||||
|
||||
namespace ModernKeePassLib.Test.Shared.Cryptography
|
||||
{
|
||||
[TestFixture ()]
|
||||
public class CryptoRandomStreamTests
|
||||
{
|
||||
void TestGetRandomBytes(CryptoRandomStream stream)
|
||||
{
|
||||
const uint length = 16;
|
||||
var bytes1 = stream.GetRandomBytes (length);
|
||||
Assert.That (bytes1.Length, Is.EqualTo (length));
|
||||
var bytes2 = stream.GetRandomBytes (length);
|
||||
Assert.That (bytes2, Is.Not.EqualTo (bytes1));
|
||||
}
|
||||
|
||||
[Test ()]
|
||||
public void TestGetRandomBytesCrsAlgorithmSalsa20 ()
|
||||
{
|
||||
var stream = new CryptoRandomStream (CrsAlgorithm.Salsa20, new byte[16]);
|
||||
TestGetRandomBytes (stream);
|
||||
}
|
||||
|
||||
[Test ()]
|
||||
public void TestGetRandomBytesCrsAlgorithmArcFourVariant ()
|
||||
{
|
||||
var stream = new CryptoRandomStream (CrsAlgorithm.ArcFourVariant, new byte[16]);
|
||||
TestGetRandomBytes (stream);
|
||||
}
|
||||
|
||||
void TestGetRandomInt64 (CryptoRandomStream stream)
|
||||
{
|
||||
var value1 = stream.GetRandomUInt64 ();
|
||||
var value2 = stream.GetRandomUInt64 ();
|
||||
Assert.That (value2, Is.Not.EqualTo (value1));
|
||||
}
|
||||
|
||||
[Test ()]
|
||||
public void TestGetRandomInt64AlgorithmSalsa20 ()
|
||||
{
|
||||
var stream = new CryptoRandomStream (CrsAlgorithm.Salsa20, new byte[16]);
|
||||
TestGetRandomInt64 (stream);
|
||||
}
|
||||
|
||||
[Test ()]
|
||||
public void TestGetRandomInt64AlgorithmArcFourVariant ()
|
||||
{
|
||||
var stream = new CryptoRandomStream (CrsAlgorithm.ArcFourVariant, new byte[16]);
|
||||
TestGetRandomInt64 (stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
44
ModernKeePassLib.Test/Cryptography/CryptoRandomTests.cs
Normal file
44
ModernKeePassLib.Test/Cryptography/CryptoRandomTests.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
|
||||
#if KeePassLib
|
||||
using KeePassLib.Cryptography;
|
||||
#else
|
||||
using ModernKeePassLibPCL.Cryptography;
|
||||
#endif
|
||||
|
||||
namespace ModernKeePassLib.Test.Shared.Cryptography
|
||||
{
|
||||
[TestFixture ()]
|
||||
public class CryptoRandomTests
|
||||
{
|
||||
[Test ()]
|
||||
public void TestAddEntropy ()
|
||||
{
|
||||
// just making sure it does not throw an exception
|
||||
CryptoRandom.Instance.AddEntropy (new byte[1]);
|
||||
}
|
||||
|
||||
[Test ()]
|
||||
public void TestGetRandomBytes ()
|
||||
{
|
||||
const int length = 32;
|
||||
var bytes1 = CryptoRandom.Instance.GetRandomBytes (length);
|
||||
Assert.That (bytes1.Length, Is.EqualTo (length));
|
||||
var bytes2 = CryptoRandom.Instance.GetRandomBytes (length);
|
||||
Assert.That (bytes2, Is.Not.EqualTo (bytes1));
|
||||
}
|
||||
|
||||
[Test ()]
|
||||
public void TestGeneratedBytesCount ()
|
||||
{
|
||||
const int length = 1;
|
||||
CryptoRandom.Instance.GetRandomBytes (length);
|
||||
var count1 = CryptoRandom.Instance.GeneratedBytesCount;
|
||||
CryptoRandom.Instance.GetRandomBytes (length);
|
||||
var count2 = CryptoRandom.Instance.GeneratedBytesCount;
|
||||
Assert.That (count2, Is.GreaterThan (count1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
77
ModernKeePassLib.Test/Cryptography/HashingStreamExTests.cs
Normal file
77
ModernKeePassLib.Test/Cryptography/HashingStreamExTests.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
#if KeePassLib
|
||||
using KeePassLib.Cryptography;
|
||||
#else
|
||||
using ModernKeePassLibPCL.Cryptography;
|
||||
#endif
|
||||
|
||||
namespace ModernKeePassLib.Test.Shared.Cryptography
|
||||
{
|
||||
[TestFixture ()]
|
||||
public class HashingStreamExTests
|
||||
{
|
||||
const string data = "test";
|
||||
|
||||
// The expected hash includes the \n added by WriteLine
|
||||
static readonly byte[] sha256HashOfData = {
|
||||
0xf2, 0xca, 0x1b, 0xb6, 0xc7, 0xe9, 0x07, 0xd0,
|
||||
0x6d, 0xaf, 0xe4, 0x68, 0x7e, 0x57, 0x9f, 0xce,
|
||||
0x76, 0xb3, 0x7e, 0x4e, 0x93, 0xb7, 0x60, 0x50,
|
||||
0x22, 0xda, 0x52, 0xe6, 0xcc, 0xc2, 0x6f, 0xd2
|
||||
};
|
||||
|
||||
[Test ()]
|
||||
public void TestRead ()
|
||||
{
|
||||
// if we use larger size, StreamReader will read past newline and cause bad hash
|
||||
var bytes = new byte[data.Length + 1];
|
||||
using (var ms = new MemoryStream (bytes)) {
|
||||
using (var sw = new StreamWriter (ms)) {
|
||||
// set NewLine to ensure we don't run into cross-platform issues on Windows
|
||||
sw.NewLine = "\n";
|
||||
sw.WriteLine (data);
|
||||
}
|
||||
}
|
||||
using (var ms = new MemoryStream (bytes)) {
|
||||
using (var hs = new HashingStreamEx (ms, false, null)) {
|
||||
using (var sr = new StreamReader (hs)) {
|
||||
var read = sr.ReadLine ();
|
||||
Assert.That (read, Is.EqualTo (data));
|
||||
}
|
||||
// When the StreamReader is disposed, it calls Dispose on the
|
||||
//HasingStreamEx, which computes the hash.
|
||||
Assert.That (hs.Hash, Is.EqualTo (sha256HashOfData));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test ()]
|
||||
public void TestWrite ()
|
||||
{
|
||||
var bytes = new byte[16];
|
||||
using (var ms = new MemoryStream (bytes)) {
|
||||
using (var hs = new HashingStreamEx (ms, true, null)) {
|
||||
using (var sw = new StreamWriter (hs)) {
|
||||
// set NewLine to ensure we don't run into cross-platform issues on Windows
|
||||
sw.NewLine = "\n";
|
||||
sw.WriteLine (data);
|
||||
}
|
||||
// When the StreamWriter is disposed, it calls Dispose on the
|
||||
//HasingStreamEx, which computes the hash.
|
||||
Assert.That (hs.Hash, Is.EqualTo (sha256HashOfData));
|
||||
}
|
||||
}
|
||||
using (var ms = new MemoryStream (bytes)) {
|
||||
using (var sr = new StreamReader (ms)) {
|
||||
var read = sr.ReadLine ();
|
||||
Assert.That (read, Is.EqualTo (data));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
36
ModernKeePassLib.Test/Cryptography/HmacOtpTests.cs
Normal file
36
ModernKeePassLib.Test/Cryptography/HmacOtpTests.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
#if KeePassLib
|
||||
using KeePassLib.Cryptography;
|
||||
#else
|
||||
using ModernKeePassLibPCL.Cryptography;
|
||||
#endif
|
||||
|
||||
namespace ModernKeePassLib.Test.Shared.Cryptography
|
||||
{
|
||||
[TestFixture ()]
|
||||
public class HmacOtpTests
|
||||
{
|
||||
// Using the test case from Appendix D of RFC 4226
|
||||
|
||||
const string secret = "12345678901234567890";
|
||||
static readonly string[] expectedHOTP = new string[] {
|
||||
"755224", "287082", "359152", "969429", "338314",
|
||||
"254676", "287922", "162583", "399871", "520489"
|
||||
};
|
||||
|
||||
[Test ()]
|
||||
public void TestGenerate ()
|
||||
{
|
||||
var secretBytes = Encoding.UTF8.GetBytes (secret);
|
||||
|
||||
for (ulong i = 0; i < 10; i++) {
|
||||
var hotp = HmacOtp.Generate (secretBytes, i, 6, false, -1);
|
||||
Assert.That (hotp, Is.EqualTo (expectedHOTP[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user