@@ -6,24 +6,28 @@ import (
6
6
"time"
7
7
8
8
"github.com/btcsuite/btcd/btcec/v2"
9
+ "github.com/lightningnetwork/lnd/clock"
9
10
"github.com/stretchr/testify/require"
10
11
)
11
12
13
+ var testTime = time .Date (2020 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )
14
+
12
15
// TestBasicSessionStore tests the basic getters and setters of the session
13
16
// store.
14
17
func TestBasicSessionStore (t * testing.T ) {
15
18
// Set up a new DB.
16
- db , err := NewDB (t .TempDir (), "test.db" )
19
+ clock := clock .NewTestClock (testTime )
20
+ db , err := NewDB (t .TempDir (), "test.db" , clock )
17
21
require .NoError (t , err )
18
22
t .Cleanup (func () {
19
23
_ = db .Close ()
20
24
})
21
25
22
26
// Create a few sessions.
23
- s1 := newSession (t , db , "session 1" , nil )
24
- s2 := newSession (t , db , "session 2" , nil )
25
- s3 := newSession (t , db , "session 3" , nil )
26
- s4 := newSession (t , db , "session 4" , nil )
27
+ s1 := newSession (t , db , clock , "session 1" , nil )
28
+ s2 := newSession (t , db , clock , "session 2" , nil )
29
+ s3 := newSession (t , db , clock , "session 3" , nil )
30
+ s4 := newSession (t , db , clock , "session 4" , nil )
27
31
28
32
// Persist session 1. This should now succeed.
29
33
require .NoError (t , db .CreateSession (s1 ))
@@ -51,11 +55,11 @@ func TestBasicSessionStore(t *testing.T) {
51
55
for _ , s := range []* Session {s1 , s2 , s3 } {
52
56
session , err := db .GetSession (s .LocalPublicKey )
53
57
require .NoError (t , err )
54
- require . Equal (t , s . Label , session . Label )
58
+ assertEqualSessions (t , s , session )
55
59
56
60
session , err = db .GetSessionByID (s .ID )
57
61
require .NoError (t , err )
58
- require . Equal (t , s . Label , session . Label )
62
+ assertEqualSessions (t , s , session )
59
63
}
60
64
61
65
// Fetch session 1 and assert that it currently has no remote pub key.
@@ -89,17 +93,18 @@ func TestBasicSessionStore(t *testing.T) {
89
93
// TestLinkingSessions tests that session linking works as expected.
90
94
func TestLinkingSessions (t * testing.T ) {
91
95
// Set up a new DB.
92
- db , err := NewDB (t .TempDir (), "test.db" )
96
+ clock := clock .NewTestClock (testTime )
97
+ db , err := NewDB (t .TempDir (), "test.db" , clock )
93
98
require .NoError (t , err )
94
99
t .Cleanup (func () {
95
100
_ = db .Close ()
96
101
})
97
102
98
103
// Create a new session with no previous link.
99
- s1 := newSession (t , db , "session 1" , nil )
104
+ s1 := newSession (t , db , clock , "session 1" , nil )
100
105
101
106
// Create another session and link it to the first.
102
- s2 := newSession (t , db , "session 2" , & s1 .GroupID )
107
+ s2 := newSession (t , db , clock , "session 2" , & s1 .GroupID )
103
108
104
109
// Try to persist the second session and assert that it fails due to the
105
110
// linked session not existing in the DB yet.
@@ -125,7 +130,8 @@ func TestLinkingSessions(t *testing.T) {
125
130
// of the GetGroupID and GetSessionIDs methods.
126
131
func TestLinkedSessions (t * testing.T ) {
127
132
// Set up a new DB.
128
- db , err := NewDB (t .TempDir (), "test.db" )
133
+ clock := clock .NewTestClock (testTime )
134
+ db , err := NewDB (t .TempDir (), "test.db" , clock )
129
135
require .NoError (t , err )
130
136
t .Cleanup (func () {
131
137
_ = db .Close ()
@@ -135,9 +141,9 @@ func TestLinkedSessions(t *testing.T) {
135
141
// after are all linked to the prior one. All these sessions belong to
136
142
// the same group. The group ID is equivalent to the session ID of the
137
143
// first session.
138
- s1 := newSession (t , db , "session 1" , nil )
139
- s2 := newSession (t , db , "session 2" , & s1 .GroupID )
140
- s3 := newSession (t , db , "session 3" , & s2 .GroupID )
144
+ s1 := newSession (t , db , clock , "session 1" , nil )
145
+ s2 := newSession (t , db , clock , "session 2" , & s1 .GroupID )
146
+ s3 := newSession (t , db , clock , "session 3" , & s2 .GroupID )
141
147
142
148
// Persist the sessions.
143
149
require .NoError (t , db .CreateSession (s1 ))
@@ -163,8 +169,8 @@ func TestLinkedSessions(t *testing.T) {
163
169
164
170
// To ensure that different groups don't interfere with each other,
165
171
// let's add another set of linked sessions not linked to the first.
166
- s4 := newSession (t , db , "session 4" , nil )
167
- s5 := newSession (t , db , "session 5" , & s4 .GroupID )
172
+ s4 := newSession (t , db , clock , "session 4" , nil )
173
+ s5 := newSession (t , db , clock , "session 5" , & s4 .GroupID )
168
174
169
175
require .NotEqual (t , s4 .GroupID , s1 .GroupID )
170
176
@@ -192,7 +198,8 @@ func TestLinkedSessions(t *testing.T) {
192
198
// method correctly checks if each session in a group passes a predicate.
193
199
func TestCheckSessionGroupPredicate (t * testing.T ) {
194
200
// Set up a new DB.
195
- db , err := NewDB (t .TempDir (), "test.db" )
201
+ clock := clock .NewTestClock (testTime )
202
+ db , err := NewDB (t .TempDir (), "test.db" , clock )
196
203
require .NoError (t , err )
197
204
t .Cleanup (func () {
198
205
_ = db .Close ()
@@ -202,7 +209,7 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
202
209
// function is checked correctly.
203
210
204
211
// Add a new session to the DB.
205
- s1 := newSession (t , db , "label 1" , nil )
212
+ s1 := newSession (t , db , clock , "label 1" , nil )
206
213
require .NoError (t , db .CreateSession (s1 ))
207
214
208
215
// Check that the group passes against an appropriate predicate.
@@ -227,7 +234,7 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
227
234
require .NoError (t , db .RevokeSession (s1 .LocalPublicKey ))
228
235
229
236
// Add a new session to the same group as the first one.
230
- s2 := newSession (t , db , "label 2" , & s1 .GroupID )
237
+ s2 := newSession (t , db , clock , "label 2" , & s1 .GroupID )
231
238
require .NoError (t , db .CreateSession (s2 ))
232
239
233
240
// Check that the group passes against an appropriate predicate.
@@ -249,7 +256,7 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
249
256
require .False (t , ok )
250
257
251
258
// Add a new session that is not linked to the first one.
252
- s3 := newSession (t , db , "completely different" , nil )
259
+ s3 := newSession (t , db , clock , "completely different" , nil )
253
260
require .NoError (t , db .CreateSession (s3 ))
254
261
255
262
// Ensure that the first group is unaffected.
@@ -279,14 +286,15 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
279
286
require .True (t , ok )
280
287
}
281
288
282
- func newSession (t * testing.T , db Store , label string ,
289
+ func newSession (t * testing.T , db Store , clock clock. Clock , label string ,
283
290
linkedGroupID * ID ) * Session {
284
291
285
292
id , priv , err := db .GetUnusedIDAndKeyPair ()
286
293
require .NoError (t , err )
287
294
288
- session , err := NewSession (
295
+ session , err := buildSession (
289
296
id , priv , label , TypeMacaroonAdmin ,
297
+ clock .Now (),
290
298
time .Date (99999 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ),
291
299
"foo.bar.baz:1234" , true , nil , nil , nil , true , linkedGroupID ,
292
300
[]PrivacyFlag {ClearPubkeys },
@@ -295,3 +303,32 @@ func newSession(t *testing.T, db Store, label string,
295
303
296
304
return session
297
305
}
306
+
307
+ func assertEqualSessions (t * testing.T , expected , actual * Session ) {
308
+ expectedExpiry := expected .Expiry
309
+ actualExpiry := actual .Expiry
310
+ expectedRevoked := expected .RevokedAt
311
+ actualRevoked := actual .RevokedAt
312
+ expectedCreated := expected .CreatedAt
313
+ actualCreated := actual .CreatedAt
314
+
315
+ expected .Expiry = time.Time {}
316
+ expected .RevokedAt = time.Time {}
317
+ expected .CreatedAt = time.Time {}
318
+ actual .Expiry = time.Time {}
319
+ actual .RevokedAt = time.Time {}
320
+ actual .CreatedAt = time.Time {}
321
+
322
+ require .Equal (t , expected , actual )
323
+ require .Equal (t , expectedExpiry .Unix (), actualExpiry .Unix ())
324
+ require .Equal (t , expectedRevoked .Unix (), actualRevoked .Unix ())
325
+ require .Equal (t , expectedCreated .Unix (), actualCreated .Unix ())
326
+
327
+ // Restore the old values to not influence the tests.
328
+ expected .Expiry = expectedExpiry
329
+ expected .RevokedAt = expectedRevoked
330
+ expected .CreatedAt = expectedCreated
331
+ actual .Expiry = actualExpiry
332
+ actual .RevokedAt = actualRevoked
333
+ actual .CreatedAt = actualCreated
334
+ }
0 commit comments