2017-11-06 19:01:01 +01:00
|
|
|
|
using System;
|
2017-09-25 18:34:27 +02:00
|
|
|
|
using System.IO;
|
2017-11-06 19:01:01 +01:00
|
|
|
|
using Windows.Storage;
|
|
|
|
|
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
2017-09-26 15:38:58 +02:00
|
|
|
|
using ModernKeePassLib.Keys;
|
2017-11-06 19:01:01 +01:00
|
|
|
|
using ModernKeePassLib.Utility;
|
2017-09-25 18:34:27 +02:00
|
|
|
|
|
2017-11-02 18:45:08 +01:00
|
|
|
|
namespace ModernKeePassLib.Test.Keys
|
2017-09-25 18:34:27 +02:00
|
|
|
|
{
|
2017-11-06 19:01:01 +01:00
|
|
|
|
[TestClass]
|
|
|
|
|
public class KcpKeyFileTests
|
|
|
|
|
{
|
|
|
|
|
private const string TestCreateFile = "TestCreate.xml";
|
|
|
|
|
private const string TestKey = "0123456789";
|
2017-09-25 18:34:27 +02:00
|
|
|
|
|
2017-11-06 19:01:01 +01:00
|
|
|
|
private const string ExpectedFileStart =
|
|
|
|
|
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
|
|
|
|
|
"<KeyFile>\r\n" +
|
|
|
|
|
"\t<Meta>\r\n" +
|
|
|
|
|
"\t\t<Version>1.00</Version>\r\n" +
|
|
|
|
|
"\t</Meta>\r\n" +
|
|
|
|
|
"\t<Key>\r\n" +
|
|
|
|
|
"\t\t<Data>";
|
2017-09-25 18:34:27 +02:00
|
|
|
|
|
2017-11-06 19:01:01 +01:00
|
|
|
|
private const string ExpectedFileEnd = "\t</Key>\r\n" +
|
2018-09-12 12:58:32 +02:00
|
|
|
|
"</KeyFile>";
|
2017-09-25 18:34:27 +02:00
|
|
|
|
|
2017-11-06 19:01:01 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void TestConstruct()
|
|
|
|
|
{
|
|
|
|
|
var expectedKeyData = new byte[32]
|
|
|
|
|
{
|
|
|
|
|
0xC1, 0xB1, 0x12, 0x77, 0x23, 0xB8, 0x99, 0xB8,
|
|
|
|
|
0xB9, 0x3B, 0x1B, 0xFF, 0x6C, 0xBE, 0xA1, 0x5B,
|
|
|
|
|
0x8B, 0x99, 0xAC, 0xBD, 0x99, 0x51, 0x85, 0x95,
|
|
|
|
|
0x31, 0xAA, 0x14, 0x3D, 0x95, 0xBF, 0x63, 0xFF
|
|
|
|
|
};
|
|
|
|
|
|
2017-11-15 17:56:31 +01:00
|
|
|
|
//var fullPath = Path.Combine(ApplicationData.Current.TemporaryFolder.Path, TestCreateFile);
|
2017-11-06 19:01:01 +01:00
|
|
|
|
var file = ApplicationData.Current.TemporaryFolder.CreateFileAsync(TestCreateFile).GetAwaiter().GetResult();
|
|
|
|
|
using (var fs = file.OpenStreamForWriteAsync().GetAwaiter().GetResult())
|
|
|
|
|
{
|
|
|
|
|
using (var sw = new StreamWriter(fs))
|
|
|
|
|
{
|
|
|
|
|
sw.Write(ExpectedFileStart);
|
|
|
|
|
sw.Write(TestKey);
|
|
|
|
|
sw.Write(ExpectedFileEnd);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-25 18:34:27 +02:00
|
|
|
|
|
2017-11-06 19:01:01 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-11-15 17:56:31 +01:00
|
|
|
|
var keyFile = new KcpKeyFile(file);
|
2017-11-06 19:01:01 +01:00
|
|
|
|
var keyData = keyFile.KeyData.ReadData();
|
|
|
|
|
Assert.IsTrue(MemUtil.ArraysEqual(keyData, expectedKeyData));
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
file.DeleteAsync().GetAwaiter().GetResult();
|
|
|
|
|
}
|
2017-09-25 18:34:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-06 19:01:01 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void TestCreate()
|
|
|
|
|
{
|
2017-11-15 17:56:31 +01:00
|
|
|
|
//var fullPath = Path.Combine(ApplicationData.Current.TemporaryFolder.Path, TestCreateFile);
|
2017-11-06 19:01:01 +01:00
|
|
|
|
var file = ApplicationData.Current.TemporaryFolder.CreateFileAsync(TestCreateFile).GetAwaiter().GetResult();
|
2017-11-15 17:56:31 +01:00
|
|
|
|
KcpKeyFile.Create(file, null);
|
2017-11-06 19:01:01 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var fileContents = FileIO.ReadTextAsync(file).GetAwaiter().GetResult();
|
2017-09-25 18:34:27 +02:00
|
|
|
|
|
2018-09-12 12:58:32 +02:00
|
|
|
|
Assert.AreEqual(185, fileContents.Length);
|
2017-11-06 19:01:01 +01:00
|
|
|
|
Assert.IsTrue(fileContents.StartsWith(ExpectedFileStart));
|
|
|
|
|
Assert.IsTrue(fileContents.EndsWith(ExpectedFileEnd));
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
file.DeleteAsync().GetAwaiter().GetResult();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-25 18:34:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|