treasurechest/tests/session/testSessionPublic.cs

113 lines
3.9 KiB
C#
Raw Normal View History

2020-05-29 01:38:45 +00:00
using NUnit.Framework;
using System;
using System.Linq;
using chestcrypto.session;
using chestcrypto.exceptions;
using Sodium;
namespace sessionTests
{
public class Tests
{
[SetUp]
public void Setup()
{
}
2020-05-30 08:06:08 +00:00
public long getFutureTime(int seconds){return DateTimeOffset.UtcNow.ToUnixTimeSeconds() + (long) seconds;}
[Test]
public void TestSessionGetLatestPublic(){
byte[] publicK = PublicKeyBox.GenerateKeyPair().PublicKey;
byte[] privateK = PublicKeyBox.GenerateKeyPair().PrivateKey;
byte[] newK = PublicKeyBox.GenerateKeyPair().PublicKey;
Session session = new Session(privateK, publicK, true, 5);
for (int i = 0; i < 5; i++){
session.addPublic(PublicKeyBox.GenerateKeyPair().PublicKey, getFutureTime(630));
}
session.addPublic(newK, getFutureTime(650));
Assert.IsTrue(Enumerable.SequenceEqual(newK, session.getLatestPublicKey()));
2020-05-29 01:38:45 +00:00
}
[Test]
public void TestSessionAddValidPublic(){
byte[] publicK = PublicKeyBox.GenerateKeyPair().PublicKey;
byte[] privateK = PublicKeyBox.GenerateKeyPair().PrivateKey;
byte[] newK = PublicKeyBox.GenerateKeyPair().PublicKey;
2020-05-30 08:06:08 +00:00
Session session = new Session(privateK, publicK, true, 5);
session.addPublic(newK, getFutureTime(610));
2020-05-29 01:38:45 +00:00
Assert.IsTrue(Enumerable.SequenceEqual(newK, session.getLatestPublicKey()));
}
2020-05-29 03:35:58 +00:00
[Test]
public void TestSessionNoPublicDupes(){
byte[] publicK = PublicKeyBox.GenerateKeyPair().PublicKey;
byte[] privateK = PublicKeyBox.GenerateKeyPair().PrivateKey;
byte[] newK = PublicKeyBox.GenerateKeyPair().PublicKey;
2020-05-30 08:06:08 +00:00
Session session = new Session(privateK, publicK, true, 5);
session.addPublic(newK, getFutureTime(615));
2020-05-29 03:35:58 +00:00
try{
2020-05-30 08:06:08 +00:00
session.addPublic(newK, getFutureTime(615));
2020-05-29 03:35:58 +00:00
}
catch(DuplicatePublicKey){return;}
Assert.Fail();
}
[Test]
public void TestSessionAddPublicInvalidKey(){
byte[] publicK = PublicKeyBox.GenerateKeyPair().PublicKey;
byte[] privateK = PublicKeyBox.GenerateKeyPair().PrivateKey;
byte[] newK = {3, 5};
2020-05-30 08:06:08 +00:00
Session session = new Session(privateK, publicK, true, 5);
2020-05-29 03:35:58 +00:00
try{
session.addPublic(newK, getFutureTime(61));
}
catch(InvalidKeyLength){
return;
}
Assert.Fail();
}
2020-05-29 01:38:45 +00:00
[Test]
public void TestSessionAddPublicInvalidTime(){
byte[] publicK = PublicKeyBox.GenerateKeyPair().PublicKey;
byte[] privateK = PublicKeyBox.GenerateKeyPair().PrivateKey;
byte[] newK = PublicKeyBox.GenerateKeyPair().PublicKey;
2020-05-30 08:06:08 +00:00
Session session = new Session(privateK, publicK, true, 5);
2020-05-29 01:38:45 +00:00
try{
session.addPublic(newK, getFutureTime(-1));
}
catch(System.ArgumentOutOfRangeException){
return;
}
Assert.Fail();
}
[Test]
public void TestSessionConstructor()
{
byte[] publicK = PublicKeyBox.GenerateKeyPair().PublicKey;
byte[] privateK = PublicKeyBox.GenerateKeyPair().PrivateKey;
2020-05-30 08:06:08 +00:00
Session session = new Session(privateK, publicK, true, 5);
2020-05-29 01:38:45 +00:00
byte[] invalid = {0, 0, 0};
try{
2020-05-30 08:06:08 +00:00
new Session(invalid, publicK, true, 5);
2020-05-29 01:38:45 +00:00
}
catch(InvalidKeyLength){
goto secondAssert;
}
Assert.Fail();
secondAssert:
try{
2020-05-30 08:06:08 +00:00
new Session(privateK, invalid, true, 5);
2020-05-29 01:38:45 +00:00
}
catch(InvalidKeyLength){
return;
}
Assert.Fail();
}
}
}