1
using System;
2
using System.Linq;
3
using Libplanet.Crypto;
4
using Libplanet.KeyStore;
5
using Xunit;
6

7
namespace Libplanet.Tests.KeyStore
8
{
9
    public abstract class KeyStoreTest<T>
10
        where T : IKeyStore
11
    {
12
        public abstract T KeyStore { get; }
13

14
        [Fact]
15
        public void List()
16 1
        {
17 1
            Assert.Empty(KeyStore.List());
18 1
            Assert.Empty(KeyStore.ListIds());
19

20 1
            var key = new PrivateKey();
21 1
            ProtectedPrivateKey ppk = ProtectedPrivateKey.Protect(key, "pass");
22 1
            Guid id = KeyStore.Add(ppk);
23

24 1
            Assert.Single(KeyStore.List());
25 1
            Tuple<Guid, ProtectedPrivateKey> pair = KeyStore.List().First();
26 1
            Assert.Equal(id, pair.Item1);
27 1
            Assert.Equal(ppk.Address, pair.Item2.Address);
28 1
            Assert.Equal(new[] { id }, KeyStore.ListIds());
29

30 1
            var key2 = new PrivateKey();
31 1
            ProtectedPrivateKey ppk2 = ProtectedPrivateKey.Protect(key2, "pass");
32 1
            Guid id2 = KeyStore.Add(ppk2);
33

34 1
            Assert.Equal(
35 1
                new[] { (id, ppk.Address), (id2, ppk2.Address) }.ToHashSet(),
36 1
                KeyStore.List().Select(tuple => (tuple.Item1, tuple.Item2.Address)).ToHashSet()
37 1
            );
38 1
            Assert.Equal(
39 1
                new[] { id, id2 }.ToHashSet(),
40 1
                KeyStore.ListIds().ToHashSet()
41 1
            );
42 1
        }
43

44
        [Fact]
45
        public void Get()
46 1
        {
47 1
            var key = new PrivateKey();
48 1
            ProtectedPrivateKey ppk = ProtectedPrivateKey.Protect(key, "pass");
49 1
            Guid id = KeyStore.Add(ppk);
50

51 1
            Assert.Equal(ppk.Address, KeyStore.Get(id).Address);
52 1
            Assert.Equal(key, KeyStore.Get(id).Unprotect("pass"));
53 1
            Assert.Throws<NoKeyException>(() => KeyStore.Get(Guid.NewGuid()));
54 1
        }
55

56
        [Fact]
57
        public void Add()
58 1
        {
59 1
            var key = new PrivateKey();
60 1
            ProtectedPrivateKey ppk = ProtectedPrivateKey.Protect(key, "pass");
61 1
            Guid id = KeyStore.Add(ppk);
62 1
            var key2 = new PrivateKey();
63 1
            ProtectedPrivateKey ppk2 = ProtectedPrivateKey.Protect(key2, "pass");
64 1
            Guid id2 = KeyStore.Add(ppk2);
65

66
            // Key ids should be unique.
67 1
            Assert.NotEqual(id, id2);
68

69 1
            Assert.Equal(ppk.Address, KeyStore.Get(id).Address);
70 1
            Assert.Equal(ppk2.Address, KeyStore.Get(id2).Address);
71

72
            (Guid, Address Address) ToSimplePair(Tuple<Guid, ProtectedPrivateKey> pair) =>
73 1
                (pair.Item1, pair.Item2.Address);
74

75 1
            Assert.Contains((id, ppk.Address), KeyStore.List().Select(ToSimplePair));
76 1
            Assert.Contains((id2, ppk2.Address), KeyStore.List().Select(ToSimplePair));
77 1
        }
78

79
        [Fact]
80
        public void Remove()
81 1
        {
82 1
            var key = new PrivateKey();
83 1
            Guid id = KeyStore.Add(ProtectedPrivateKey.Protect(key, "pass"));
84

85 1
            Assert.Throws<NoKeyException>(() => KeyStore.Remove(Guid.NewGuid()));
86 1
            Assert.Equal(new[] { id }, KeyStore.ListIds());
87

88 1
            KeyStore.Remove(id);
89 1
            Assert.Throws<NoKeyException>(() => KeyStore.Get(id));
90 1
            Assert.Empty(KeyStore.ListIds());
91 1
        }
92
    }
93
}

Read our documentation on viewing source code .

Loading