finished restore test

This commit is contained in:
Kevin Froman 2020-06-08 02:59:43 -05:00
parent 280955b8e0
commit a0617da1f5
5 changed files with 174 additions and 23 deletions

View File

@ -9,7 +9,7 @@ using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using Sodium;
using CsvHelper;
using SimpleBase;
namespace testProfileLoad
{
@ -21,32 +21,90 @@ namespace testProfileLoad
}
[Test]
public void TestLoad(){
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(){
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");
DoublePrivateKey key2 = new DoublePrivateKey(PublicKeyAuth.GenerateKeyPair().PrivateKey, PublicKeyBox.GenerateKeyPair().PrivateKey);
PrivateIdentity iden2 = new PrivateIdentity(key2, "alice");
DoublePublicKey key2 = new DoublePublicKey(PublicKeyAuth.GenerateKeyPair().PublicKey, PublicKeyBox.GenerateKeyPair().PublicKey);
PublicIdentity iden2 = new PublicIdentity(key2, "alice");
KeyRing ring = new KeyRing();
ring.addPrivateIdentity(iden);
ring.addPublicIdentity(iden2);
string header = "base85Key,name,note";
using (var writer = new StreamWriter(privateProfile))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(privateProfile, false))
{
csv.WriteRecords(ring.privateIdentities);
}
using (var writer = new StreamWriter(publicProfile))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(ring.publicIdentities);
file.Write(header + "\r\n" + iden.getEncodedKey() + "," + iden.name + "," + iden.getNote() + "\r\n");
}
KeyRing restored = new RestoreKeyring(temp);
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(publicProfile, false))
{
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()));
}
File.Delete(privateProfile);
File.Delete(publicProfile);
Directory.Delete(privateProfile);
}

View File

@ -6,14 +6,13 @@ namespace chestcrypto.identity
/*
PrivateIdentity is a wrapper around a DoublePrivateKey providing associated metadata such as alias and note
*/
private DoublePrivateKey key { get; set; }
private DoublePrivateKey key;
public string base85Key { get {
return getEncodedKey();
}}
public string name { get; set; }
public string comment { get; set; } // human's note
public PrivateIdentity(DoublePrivateKey doublePrivateKey, string alias){
key = doublePrivateKey;
name = alias;

View File

@ -6,8 +6,11 @@ namespace chestcrypto.identity
PublicIdentity is a wrapper around a DoublePublicKey providing associated metadata such as alias and note
*/
private DoublePublicKey key;
private string name;
private string comment; // human's note
public string base85Key { get {
return getEncodedKey();
}}
public string name { get; set; }
public string comment { get; set; } // human's note
public PublicIdentity(DoublePublicKey doublePublicKey, string alias){
key = doublePublicKey;
@ -24,7 +27,9 @@ namespace chestcrypto.identity
public DoublePublicKey getPublicKey(){return key;}
public string getName(){return name;}
public string getNote(){return comment;}
public string getEncodedKey(){
return SimpleBase.Base85.Z85.Encode(key.getRawDouble());
}
}

View File

@ -17,7 +17,7 @@ namespace keyring{
public void addPublicIdentity(PublicIdentity newIden){
foreach(PublicIdentity iden in publicIdentities){
if (Enumerable.ReferenceEquals(iden.getPublicKey(), iden)){
if (Enumerable.SequenceEqual(iden.getPublicKey().getRawDouble(), newIden.getPublicKey().getRawDouble())){
throw new DuplicateIdentityException();
}
}
@ -25,7 +25,7 @@ namespace keyring{
}
public void addPrivateIdentity(PrivateIdentity newIden){
foreach(PrivateIdentity iden in privateIdentities){
if (Enumerable.ReferenceEquals(iden.getPrivateKey(), iden)){
if (Enumerable.SequenceEqual(iden.getPrivateKey().getRawDouble(), newIden.getPrivateKey().getRawDouble())){
throw new DuplicateIdentityException();
}
}

View File

@ -1,6 +1,8 @@
using System.IO;
using System.Collections.Generic;
using System;
using keyring;
using chestcrypto.identity;
namespace chestcrypto.profile{
@ -14,10 +16,97 @@ namespace chestcrypto.profile{
if (! Directory.Exists(profileDir)){
Directory.CreateDirectory(profileDir); // Does not error if it exists already
}
}
private void getKeyring(){
private PrivateIdentity getPrivateIdentityFromLine(string data){
var parts = data.Split(',');
int counter = 0;
DoublePrivateKey key = null;
string alias = "";
string note = "";
foreach (string part in parts){
switch(counter){
case 0:
key = new DoublePrivateKey(SimpleBase.Base85.Z85.Decode(part).ToArray());
break;
case 1:
alias = part;
break;
case 2:
note = part;
break;
default:
throw new InvalidDataException();
}
counter += 1;
}
return new PrivateIdentity(key, alias, note);
}
private PublicIdentity getPublicIdentityFromLine(string data){
var parts = data.Split(',');
int counter = 0;
DoublePublicKey key = null;
string alias = "";
string note = "";
foreach (string part in parts){
switch(counter){
case 0:
key = new DoublePublicKey(SimpleBase.Base85.Z85.Decode(part).ToArray());
break;
case 1:
alias = part;
break;
case 2:
note = part;
break;
default:
throw new InvalidDataException();
}
counter += 1;
}
return new PublicIdentity(key, alias, note);
}
public KeyRing getKeyring(){
KeyRing keyRing = new KeyRing();
string[] lines;
bool first;
try{
lines = System.IO.File.ReadAllLines(profileDir + "/private.keyring.csv");
first = true;
foreach (string line in lines){
if (first){
first = false;
continue;
}
if (line.Length <= 1){
continue;
}
keyRing.addPrivateIdentity(getPrivateIdentityFromLine(line));
}
}
catch(System.IO.FileNotFoundException){}
try{
lines = System.IO.File.ReadAllLines(profileDir + "/public.keyring.csv");
first = true;
foreach (string line in lines){
if (first){
first = false;
continue;
}
if (line.Length <= 1){
continue;
}
keyRing.addPublicIdentity(getPublicIdentityFromLine(line));
}
}
catch(System.IO.FileNotFoundException){}
return keyRing;
}
}