-
Notifications
You must be signed in to change notification settings - Fork 58
/
onion_store_test.go
63 lines (54 loc) · 1.74 KB
/
onion_store_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
package aperture
import (
"bytes"
"testing"
"github.com/lightningnetwork/lnd/tor"
)
// assertPrivateKeyExists is a helper to determine if the private key for an
// onion service exists in the store. If it does, it's compared against what's
// expected.
func assertPrivateKeyExists(t *testing.T, store *onionStore,
expPrivateKey *[]byte) {
t.Helper()
exists := expPrivateKey != nil
privateKey, err := store.PrivateKey()
switch {
case exists && err != nil:
t.Fatalf("unable to retrieve private key: %v", err)
case !exists && err != tor.ErrNoPrivateKey:
t.Fatalf("expected error ErrNoPrivateKey, got \"%v\"", err)
case exists:
if !bytes.Equal(privateKey, *expPrivateKey) {
t.Fatalf("expected private key %v, got %v",
string(*expPrivateKey), string(privateKey))
}
default:
return
}
}
// TestOnionStore ensures the different operations of the onionStore behave
// as expected.
func TestOnionStore(t *testing.T) {
etcdClient, serverCleanup := etcdSetup(t)
defer etcdClient.Close()
defer serverCleanup()
// Upon a fresh initialization of the store, no private keys should
// exist for any onion service type.
store := newOnionStore(etcdClient)
assertPrivateKeyExists(t, store, nil)
// Store a private key for an onion service and check it was stored
// correctly.
privateKey := []byte("hide_me_plz")
if err := store.StorePrivateKey(privateKey); err != nil {
t.Fatalf("unable to store private key for onion service: %v",
err)
}
assertPrivateKeyExists(t, store, &privateKey)
// Delete the private key for the onion service and check that it was
// indeed successful.
if err := store.DeletePrivateKey(); err != nil {
t.Fatalf("unable to remove private key for onion service: %v",
err)
}
assertPrivateKeyExists(t, store, nil)
}