forked from lightninglabs/aperture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore_test.go
More file actions
130 lines (119 loc) · 3.36 KB
/
store_test.go
File metadata and controls
130 lines (119 loc) · 3.36 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package lsat
import (
"os"
"path/filepath"
"testing"
"github.com/lightningnetwork/lnd/lntypes"
)
// TestStore tests the basic functionality of the file based store.
func TestFileStore(t *testing.T) {
t.Parallel()
tempDirName, err := os.MkdirTemp("", "lsatstore")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDirName)
var (
paidPreimage = lntypes.Preimage{1, 2, 3, 4, 5}
paidToken = &Token{
Preimage: paidPreimage,
baseMac: makeMac(),
}
pendingToken = &Token{
Preimage: zeroPreimage,
baseMac: makeMac(),
}
)
store, err := NewFileStore(tempDirName)
if err != nil {
t.Fatalf("could not create test store: %v", err)
}
// Make sure the current store is empty.
_, err = store.CurrentToken()
if err != ErrNoToken {
t.Fatalf("expected store to be empty but error was: %v", err)
}
tokens, err := store.AllTokens()
if err != nil {
t.Fatalf("unexpected error listing all tokens: %v", err)
}
if len(tokens) != 0 {
t.Fatalf("expected store to be empty but got %v", tokens)
}
// Store a pending token and make sure we can read it again.
err = store.StoreToken(pendingToken)
if err != nil {
t.Fatalf("could not save pending token: %v", err)
}
if !fileExists(filepath.Join(tempDirName, storeFileNamePending)) {
t.Fatalf("expected file %s/%s to exist but it didn't",
tempDirName, storeFileNamePending)
}
token, err := store.CurrentToken()
if err != nil {
t.Fatalf("could not read pending token: %v", err)
}
if !token.baseMac.Equal(pendingToken.baseMac) {
t.Fatalf("expected macaroon to match")
}
tokens, err = store.AllTokens()
if err != nil {
t.Fatalf("unexpected error listing all tokens: %v", err)
}
if len(tokens) != 1 {
t.Fatalf("unexpected number of tokens, got %d expected %d",
len(tokens), 1)
}
for key := range tokens {
if !tokens[key].baseMac.Equal(pendingToken.baseMac) {
t.Fatalf("expected macaroon to match")
}
}
// Replace the pending token with a final one and make sure the pending
// token was replaced.
err = store.StoreToken(paidToken)
if err != nil {
t.Fatalf("could not save pending token: %v", err)
}
if !fileExists(filepath.Join(tempDirName, storeFileName)) {
t.Fatalf("expected file %s/%s to exist but it didn't",
tempDirName, storeFileName)
}
if fileExists(filepath.Join(tempDirName, storeFileNamePending)) {
t.Fatalf("expected file %s/%s to be removed but it wasn't",
tempDirName, storeFileNamePending)
}
token, err = store.CurrentToken()
if err != nil {
t.Fatalf("could not read pending token: %v", err)
}
if !token.baseMac.Equal(paidToken.baseMac) {
t.Fatalf("expected macaroon to match")
}
tokens, err = store.AllTokens()
if err != nil {
t.Fatalf("unexpected error listing all tokens: %v", err)
}
if len(tokens) != 1 {
t.Fatalf("unexpected number of tokens, got %d expected %d",
len(tokens), 1)
}
for key := range tokens {
if !tokens[key].baseMac.Equal(paidToken.baseMac) {
t.Fatalf("expected macaroon to match")
}
}
// Make sure we can't replace the existing paid token with a pending.
err = store.StoreToken(pendingToken)
if err != errNoReplace {
t.Fatalf("unexpected error. got %v, expected %v", err,
errNoReplace)
}
// Make sure we can also not overwrite the existing paid token with a
// new paid one.
err = store.StoreToken(paidToken)
if err != errNoReplace {
t.Fatalf("unexpected error. got %v, expected %v", err,
errNoReplace)
}
}