treasurechest/tests/doublekeyPublicTest.cs

74 lines
2.4 KiB
C#
Raw Normal View History

using NUnit.Framework;
using chestcrypto;
using System;
using Sodium;
2020-05-12 08:06:13 +00:00
namespace DoubleKeyPublicTests
{
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
2020-05-12 08:06:13 +00:00
public void TestDoublePublicKeyThrowsOnBadLoad()
{
byte[] invalid = {0};
byte[] invalid2 = new byte[33];
for (int i = 0; i < invalid2.Length; i++){
invalid2[i] = 1;
}
bool success = false;
try{
new chestcrypto.DoublePublicKey(invalid);
success = true;
}
2020-05-17 07:49:36 +00:00
catch (chestcrypto.exceptions.InvalidDoubleKeyException){
2020-05-12 08:06:13 +00:00
}
if (success){
Assert.Fail();
}
try{
new chestcrypto.DoublePublicKey(invalid2);
success = true;
}
2020-05-17 07:49:36 +00:00
catch (chestcrypto.exceptions.InvalidDoubleKeyException){
2020-05-12 08:06:13 +00:00
}
if (success){
Assert.Fail();
}
}
[Test]
public void TestDoublePublicKeyLoad()
{
// Test that the combined key loader loads both constructors with the same results
byte[] signingKey = PublicKeyAuth.GenerateKeyPair().PublicKey;
byte[] encryptionKey = PublicKeyBox.GenerateKeyPair().PublicKey;
byte[] combinedKey = new byte[signingKey.Length + encryptionKey.Length];
Buffer.BlockCopy(signingKey, 0, combinedKey, 0, signingKey.Length);
Buffer.BlockCopy(encryptionKey, 0, combinedKey, signingKey.Length, encryptionKey.Length);
DoublePublicKey combinedLoad = new chestcrypto.DoublePublicKey(combinedKey);
DoublePublicKey parameterLoad = new chestcrypto.DoublePublicKey(signingKey, encryptionKey);
if (combinedLoad.getRawDouble().Length != parameterLoad.getRawDouble().Length){
Assert.Fail();
}
if (combinedLoad.getRawDouble().Length != 64){
Assert.Fail();
}
for (int i = 0; i < combinedLoad.getRawDouble().Length; i++){
if (combinedLoad.getRawDouble()[i] != parameterLoad.getRawDouble()[i]){
Assert.Fail();
}
if (combinedLoad.getRawDouble()[i] != combinedKey[i]){
Assert.Fail();
}
}
}
}
}