finished restore test
This commit is contained in:
parent
280955b8e0
commit
a0617da1f5
@ -9,7 +9,7 @@ using System.Linq;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using Sodium;
|
using Sodium;
|
||||||
using CsvHelper;
|
using SimpleBase;
|
||||||
|
|
||||||
namespace testProfileLoad
|
namespace testProfileLoad
|
||||||
{
|
{
|
||||||
@ -21,32 +21,90 @@ namespace testProfileLoad
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[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 temp = Path.GetTempPath();
|
||||||
var publicProfile = temp + "/public.keyring.csv";
|
var publicProfile = temp + "/public.keyring.csv";
|
||||||
var privateProfile = temp + "/private.keyring.csv";
|
var privateProfile = temp + "/private.keyring.csv";
|
||||||
DoublePrivateKey key = new DoublePrivateKey(PublicKeyAuth.GenerateKeyPair().PrivateKey, PublicKeyBox.GenerateKeyPair().PrivateKey);
|
DoublePrivateKey key = new DoublePrivateKey(PublicKeyAuth.GenerateKeyPair().PrivateKey, PublicKeyBox.GenerateKeyPair().PrivateKey);
|
||||||
PrivateIdentity iden = new PrivateIdentity(key, "bob");
|
PrivateIdentity iden = new PrivateIdentity(key, "bob");
|
||||||
DoublePrivateKey key2 = new DoublePrivateKey(PublicKeyAuth.GenerateKeyPair().PrivateKey, PublicKeyBox.GenerateKeyPair().PrivateKey);
|
DoublePublicKey key2 = new DoublePublicKey(PublicKeyAuth.GenerateKeyPair().PublicKey, PublicKeyBox.GenerateKeyPair().PublicKey);
|
||||||
PrivateIdentity iden2 = new PrivateIdentity(key2, "alice");
|
PublicIdentity iden2 = new PublicIdentity(key2, "alice");
|
||||||
KeyRing ring = new KeyRing();
|
KeyRing ring = new KeyRing();
|
||||||
ring.addPrivateIdentity(iden);
|
ring.addPrivateIdentity(iden);
|
||||||
|
ring.addPublicIdentity(iden2);
|
||||||
|
|
||||||
|
string header = "base85Key,name,note";
|
||||||
|
|
||||||
using (var writer = new StreamWriter(privateProfile))
|
using (System.IO.StreamWriter file =
|
||||||
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
|
new System.IO.StreamWriter(privateProfile, false))
|
||||||
{
|
{
|
||||||
csv.WriteRecords(ring.privateIdentities);
|
file.Write(header + "\r\n" + iden.getEncodedKey() + "," + iden.name + "," + iden.getNote() + "\r\n");
|
||||||
}
|
}
|
||||||
using (var writer = new StreamWriter(publicProfile))
|
|
||||||
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
|
using (System.IO.StreamWriter file =
|
||||||
|
new System.IO.StreamWriter(publicProfile, false))
|
||||||
{
|
{
|
||||||
csv.WriteRecords(ring.publicIdentities);
|
file.Write(header + "\r\n" + iden2.getEncodedKey() + "," + iden2.name + "," + iden2.getNote() + "\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
KeyRing restored = new RestoreKeyring(temp);
|
|
||||||
|
|
||||||
Directory.Delete(privateProfile);
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,14 +6,13 @@ namespace chestcrypto.identity
|
|||||||
/*
|
/*
|
||||||
PrivateIdentity is a wrapper around a DoublePrivateKey providing associated metadata such as alias and note
|
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 {
|
public string base85Key { get {
|
||||||
return getEncodedKey();
|
return getEncodedKey();
|
||||||
}}
|
}}
|
||||||
public string name { get; set; }
|
public string name { get; set; }
|
||||||
public string comment { get; set; } // human's note
|
public string comment { get; set; } // human's note
|
||||||
|
|
||||||
|
|
||||||
public PrivateIdentity(DoublePrivateKey doublePrivateKey, string alias){
|
public PrivateIdentity(DoublePrivateKey doublePrivateKey, string alias){
|
||||||
key = doublePrivateKey;
|
key = doublePrivateKey;
|
||||||
name = alias;
|
name = alias;
|
||||||
|
@ -6,8 +6,11 @@ namespace chestcrypto.identity
|
|||||||
PublicIdentity is a wrapper around a DoublePublicKey providing associated metadata such as alias and note
|
PublicIdentity is a wrapper around a DoublePublicKey providing associated metadata such as alias and note
|
||||||
*/
|
*/
|
||||||
private DoublePublicKey key;
|
private DoublePublicKey key;
|
||||||
private string name;
|
public string base85Key { get {
|
||||||
private string comment; // human's note
|
return getEncodedKey();
|
||||||
|
}}
|
||||||
|
public string name { get; set; }
|
||||||
|
public string comment { get; set; } // human's note
|
||||||
|
|
||||||
public PublicIdentity(DoublePublicKey doublePublicKey, string alias){
|
public PublicIdentity(DoublePublicKey doublePublicKey, string alias){
|
||||||
key = doublePublicKey;
|
key = doublePublicKey;
|
||||||
@ -24,7 +27,9 @@ namespace chestcrypto.identity
|
|||||||
public DoublePublicKey getPublicKey(){return key;}
|
public DoublePublicKey getPublicKey(){return key;}
|
||||||
public string getName(){return name;}
|
public string getName(){return name;}
|
||||||
public string getNote(){return comment;}
|
public string getNote(){return comment;}
|
||||||
|
public string getEncodedKey(){
|
||||||
|
return SimpleBase.Base85.Z85.Encode(key.getRawDouble());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ namespace keyring{
|
|||||||
|
|
||||||
public void addPublicIdentity(PublicIdentity newIden){
|
public void addPublicIdentity(PublicIdentity newIden){
|
||||||
foreach(PublicIdentity iden in publicIdentities){
|
foreach(PublicIdentity iden in publicIdentities){
|
||||||
if (Enumerable.ReferenceEquals(iden.getPublicKey(), iden)){
|
if (Enumerable.SequenceEqual(iden.getPublicKey().getRawDouble(), newIden.getPublicKey().getRawDouble())){
|
||||||
throw new DuplicateIdentityException();
|
throw new DuplicateIdentityException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -25,7 +25,7 @@ namespace keyring{
|
|||||||
}
|
}
|
||||||
public void addPrivateIdentity(PrivateIdentity newIden){
|
public void addPrivateIdentity(PrivateIdentity newIden){
|
||||||
foreach(PrivateIdentity iden in privateIdentities){
|
foreach(PrivateIdentity iden in privateIdentities){
|
||||||
if (Enumerable.ReferenceEquals(iden.getPrivateKey(), iden)){
|
if (Enumerable.SequenceEqual(iden.getPrivateKey().getRawDouble(), newIden.getPrivateKey().getRawDouble())){
|
||||||
throw new DuplicateIdentityException();
|
throw new DuplicateIdentityException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System;
|
||||||
using keyring;
|
using keyring;
|
||||||
|
using chestcrypto.identity;
|
||||||
|
|
||||||
namespace chestcrypto.profile{
|
namespace chestcrypto.profile{
|
||||||
|
|
||||||
@ -14,10 +16,97 @@ namespace chestcrypto.profile{
|
|||||||
if (! Directory.Exists(profileDir)){
|
if (! Directory.Exists(profileDir)){
|
||||||
Directory.CreateDirectory(profileDir); // Does not error if it exists already
|
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();
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user