-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkeystore_test.go
67 lines (56 loc) · 1.55 KB
/
keystore_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package encrepo
import (
"context"
secrand "crypto/rand"
"io"
"path/filepath"
"sort"
"testing"
"github.com/ipfs/go-datastore"
ci "github.com/libp2p/go-libp2p/core/crypto"
"github.com/stretchr/testify/require"
)
func requireClose(t *testing.T, closer io.Closer) {
t.Helper()
require.NoError(t, closer.Close())
}
func TestKeystoreFromSQLiteDatastore(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Instantiate datastore
ds, err := NewSQLiteDatastore("sqlite3", filepath.Join(t.TempDir(), "db.sqlite"), "keys")
require.NoError(t, err)
defer requireClose(t, ds)
// Generate keys
keysIDs := []string{"a", "b", "c"}
keys := map[string]ci.PrivKey{}
for _, id := range keysIDs {
sk, _, err := ci.GenerateEd25519Key(secrand.Reader)
require.NoError(t, err)
keys[id] = sk
}
// Create keystore
const prefix = "keys"
ks := KeystoreFromDatastore(NewNamespacedDatastore(ds, datastore.NewKey(prefix)))
// Put keys
for id, val := range keys {
require.NoError(t, ks.Put(id, val))
}
// Put data with same prefix in ds
require.NoError(t, ds.Put(ctx, datastore.NewKey(prefix+"_foo"), []byte("42")))
// Check that the key list contains the correct keys and not the data with same prefix
l, err := ks.List()
require.NoError(t, err)
l2 := make([]string, len(l))
for i, k := range l {
l2[i] = datastore.NewKey(k).Name()
}
sort.Strings(l2)
require.Equal(t, keysIDs, l2)
// Check that key data matches
for id, val := range keys {
v, err := ks.Get(id)
require.NoError(t, err)
require.Equal(t, val, v)
}
}