From 280955b8e0067c8a9b04af30dafc5c1e31e42a78 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 6 Jun 2020 04:24:14 -0500 Subject: [PATCH] csv serialization test added --- tests/keyringTest.cs | 5 +++++ tests/profile/testLoad.cs | 31 +++++++++++++++++++++++++++++- treasurechest/identity/private.cs | 16 +++++++++------ treasurechest/identity/public.cs | 3 --- treasurechest/treasurechest.csproj | 1 + 5 files changed, 46 insertions(+), 10 deletions(-) diff --git a/tests/keyringTest.cs b/tests/keyringTest.cs index c9ef54b..636a0ec 100644 --- a/tests/keyringTest.cs +++ b/tests/keyringTest.cs @@ -3,6 +3,7 @@ using System; using chestcrypto; using keyring; using chestcrypto.identity; +using System.Linq; using Sodium; namespace keyringTests @@ -36,6 +37,10 @@ namespace keyringTests PrivateIdentity iden2 = new PrivateIdentity(key2, "alice"); KeyRing ring = new KeyRing(); ring.addPrivateIdentity(iden); + foreach(PrivateIdentity id in ring.privateIdentities){ + Assert.IsTrue(Enumerable.SequenceEqual(id.getPrivateKey().getRawDouble(), iden.getPrivateKey().getRawDouble())); + Assert.IsFalse(Enumerable.SequenceEqual(id.getPrivateKey().getRawDouble(), iden2.getPrivateKey().getRawDouble())); + } Assert.IsTrue(ring.privateIdentities.Contains(iden)); Assert.IsFalse(ring.privateIdentities.Contains(iden2)); diff --git a/tests/profile/testLoad.cs b/tests/profile/testLoad.cs index 43630a5..48a7425 100644 --- a/tests/profile/testLoad.cs +++ b/tests/profile/testLoad.cs @@ -2,9 +2,14 @@ using NUnit.Framework; using chestcrypto.identity; using chestcrypto; using chestcrypto.profile; +using keyring; using System; using System.IO; using System.Linq; +using System.Collections.Generic; +using System.Globalization; +using Sodium; +using CsvHelper; namespace testProfileLoad { @@ -17,7 +22,31 @@ namespace testProfileLoad [Test] public void TestLoad(){ - RestoreKeyring restore = new RestoreKeyring("test.profile"); + 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"); + KeyRing ring = new KeyRing(); + ring.addPrivateIdentity(iden); + + + using (var writer = new StreamWriter(privateProfile)) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + csv.WriteRecords(ring.privateIdentities); + } + using (var writer = new StreamWriter(publicProfile)) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + csv.WriteRecords(ring.publicIdentities); + } + + KeyRing restored = new RestoreKeyring(temp); + + Directory.Delete(privateProfile); } diff --git a/treasurechest/identity/private.cs b/treasurechest/identity/private.cs index 0fb8f1b..16ec36b 100644 --- a/treasurechest/identity/private.cs +++ b/treasurechest/identity/private.cs @@ -1,16 +1,18 @@ -using chestcrypto; +using SimpleBase; namespace chestcrypto.identity { - public class PrivateIdentity{ /* PrivateIdentity is a wrapper around a DoublePrivateKey providing associated metadata such as alias and note */ + private DoublePrivateKey key { get; set; } + public string base85Key { get { + return getEncodedKey(); + }} + public string name { get; set; } + public string comment { get; set; } // human's note - private DoublePrivateKey key; - private string name; - private string comment; // human's note public PrivateIdentity(DoublePrivateKey doublePrivateKey, string alias){ key = doublePrivateKey; @@ -25,10 +27,12 @@ namespace chestcrypto.identity } public DoublePrivateKey getPrivateKey(){return key;} + public string getEncodedKey(){ + return SimpleBase.Base85.Z85.Encode(key.getRawDouble()); + } public string getName(){return name;} public string getNote(){return comment;} - } } \ No newline at end of file diff --git a/treasurechest/identity/public.cs b/treasurechest/identity/public.cs index cb055ef..9c71972 100644 --- a/treasurechest/identity/public.cs +++ b/treasurechest/identity/public.cs @@ -1,5 +1,3 @@ -using chestcrypto; - namespace chestcrypto.identity { @@ -7,7 +5,6 @@ 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 diff --git a/treasurechest/treasurechest.csproj b/treasurechest/treasurechest.csproj index 61f0982..b9c4bbf 100644 --- a/treasurechest/treasurechest.csproj +++ b/treasurechest/treasurechest.csproj @@ -5,6 +5,7 @@ +