2020-06-05 04:16:05 +00:00
|
|
|
using NUnit.Framework;
|
|
|
|
using chestcrypto.identity;
|
|
|
|
using chestcrypto;
|
|
|
|
using chestcrypto.profile;
|
2020-06-06 09:24:14 +00:00
|
|
|
using keyring;
|
2020-06-05 04:16:05 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2020-06-06 09:24:14 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using Sodium;
|
2020-06-08 07:59:43 +00:00
|
|
|
using SimpleBase;
|
2020-06-05 04:16:05 +00:00
|
|
|
|
|
|
|
namespace testProfileLoad
|
|
|
|
{
|
|
|
|
public class Tests
|
|
|
|
{
|
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-06-08 07:59:43 +00:00
|
|
|
public void TestLoadMany(){
|
|
|
|
string header = "base85Key,name,note";
|
|
|
|
var temp = Path.GetTempPath();
|
|
|
|
List<PublicIdentity> pub = new List<PublicIdentity>();
|
|
|
|
KeyRing ring = new KeyRing();
|
|
|
|
DoublePublicKey key;
|
|
|
|
PublicIdentity iden;
|
|
|
|
var publicProfile = temp + "/public.keyring.csv";
|
|
|
|
var privateProfile = temp + "/private.keyring.csv";
|
|
|
|
|
|
|
|
using (System.IO.StreamWriter file =
|
|
|
|
new System.IO.StreamWriter(publicProfile, false))
|
|
|
|
{
|
|
|
|
file.Write(header + "\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++){
|
|
|
|
key = new DoublePublicKey(PublicKeyAuth.GenerateKeyPair().PublicKey, PublicKeyBox.GenerateKeyPair().PublicKey);
|
|
|
|
iden = new PublicIdentity(key, "test");
|
|
|
|
ring.addPublicIdentity(iden);
|
|
|
|
pub.Add(iden);
|
|
|
|
using (StreamWriter w = File.AppendText(publicProfile))
|
|
|
|
{
|
|
|
|
w.WriteLine(iden.getEncodedKey() + "," + iden.name + "," + iden.getNote() + "\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
KeyRing restored = new RestoreKeyring(temp).getKeyring();
|
|
|
|
|
|
|
|
Assert.IsTrue(restored.publicIdentities.Count > 0);
|
|
|
|
bool ran = false;
|
|
|
|
foreach(PublicIdentity id in restored.publicIdentities){
|
|
|
|
ran = false;
|
|
|
|
foreach(PublicIdentity existing in pub){
|
|
|
|
if (Enumerable.SequenceEqual(id.getPublicKey().getRawDouble(), existing.getPublicKey().getRawDouble())){
|
|
|
|
ran = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Assert.IsTrue(ran);
|
|
|
|
}
|
|
|
|
File.Delete(publicProfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestLoadSingleEntries(){
|
2020-06-06 09:24:14 +00:00
|
|
|
var temp = Path.GetTempPath();
|
|
|
|
var publicProfile = temp + "/public.keyring.csv";
|
|
|
|
var privateProfile = temp + "/private.keyring.csv";
|
|
|
|
DoublePrivateKey key = new DoublePrivateKey(PublicKeyAuth.GenerateKeyPair().PrivateKey, PublicKeyBox.GenerateKeyPair().PrivateKey);
|
|
|
|
PrivateIdentity iden = new PrivateIdentity(key, "bob");
|
2020-06-08 07:59:43 +00:00
|
|
|
DoublePublicKey key2 = new DoublePublicKey(PublicKeyAuth.GenerateKeyPair().PublicKey, PublicKeyBox.GenerateKeyPair().PublicKey);
|
|
|
|
PublicIdentity iden2 = new PublicIdentity(key2, "alice");
|
2020-06-06 09:24:14 +00:00
|
|
|
KeyRing ring = new KeyRing();
|
|
|
|
ring.addPrivateIdentity(iden);
|
2020-06-08 07:59:43 +00:00
|
|
|
ring.addPublicIdentity(iden2);
|
2020-06-06 09:24:14 +00:00
|
|
|
|
2020-06-08 07:59:43 +00:00
|
|
|
string header = "base85Key,name,note";
|
2020-06-06 09:24:14 +00:00
|
|
|
|
2020-06-08 07:59:43 +00:00
|
|
|
using (System.IO.StreamWriter file =
|
|
|
|
new System.IO.StreamWriter(privateProfile, false))
|
2020-06-06 09:24:14 +00:00
|
|
|
{
|
2020-06-08 07:59:43 +00:00
|
|
|
file.Write(header + "\r\n" + iden.getEncodedKey() + "," + iden.name + "," + iden.getNote() + "\r\n");
|
2020-06-06 09:24:14 +00:00
|
|
|
}
|
2020-06-08 07:59:43 +00:00
|
|
|
|
|
|
|
using (System.IO.StreamWriter file =
|
|
|
|
new System.IO.StreamWriter(publicProfile, false))
|
2020-06-06 09:24:14 +00:00
|
|
|
{
|
2020-06-08 07:59:43 +00:00
|
|
|
file.Write(header + "\r\n" + iden2.getEncodedKey() + "," + iden2.name + "," + iden2.getNote() + "\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
KeyRing restored = new RestoreKeyring(temp).getKeyring();
|
|
|
|
Assert.IsTrue(restored.privateIdentities.Count > 0);
|
|
|
|
Assert.IsTrue(restored.publicIdentities.Count > 0);
|
|
|
|
foreach(PrivateIdentity id in restored.privateIdentities){
|
|
|
|
Assert.IsTrue(Enumerable.SequenceEqual(id.getPrivateKey().getRawDouble(), iden.getPrivateKey().getRawDouble()));
|
|
|
|
}
|
|
|
|
foreach(PublicIdentity id in restored.publicIdentities){
|
|
|
|
Assert.IsTrue(Enumerable.SequenceEqual(id.getPublicKey().getRawDouble(), iden2.getPublicKey().getRawDouble()));
|
2020-06-06 09:24:14 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 07:59:43 +00:00
|
|
|
File.Delete(privateProfile);
|
|
|
|
File.Delete(publicProfile);
|
2020-06-06 09:24:14 +00:00
|
|
|
|
2020-06-05 04:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|