@@ -6,24 +6,28 @@ import (
66 "time"
77
88 "github.com/btcsuite/btcd/btcec/v2"
9+ "github.com/lightningnetwork/lnd/clock"
910 "github.com/stretchr/testify/require"
1011)
1112
13+ var testTime = time .Date (2020 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )
14+
1215// TestBasicSessionStore tests the basic getters and setters of the session
1316// store.
1417func TestBasicSessionStore (t * testing.T ) {
1518 // 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 )
1721 require .NoError (t , err )
1822 t .Cleanup (func () {
1923 _ = db .Close ()
2024 })
2125
2226 // 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 )
2731
2832 // Persist session 1. This should now succeed.
2933 require .NoError (t , db .CreateSession (s1 ))
@@ -51,11 +55,11 @@ func TestBasicSessionStore(t *testing.T) {
5155 for _ , s := range []* Session {s1 , s2 , s3 } {
5256 session , err := db .GetSession (s .LocalPublicKey )
5357 require .NoError (t , err )
54- require . Equal (t , s . Label , session . Label )
58+ assertEqualSessions (t , s , session )
5559
5660 session , err = db .GetSessionByID (s .ID )
5761 require .NoError (t , err )
58- require . Equal (t , s . Label , session . Label )
62+ assertEqualSessions (t , s , session )
5963 }
6064
6165 // Fetch session 1 and assert that it currently has no remote pub key.
@@ -89,17 +93,18 @@ func TestBasicSessionStore(t *testing.T) {
8993// TestLinkingSessions tests that session linking works as expected.
9094func TestLinkingSessions (t * testing.T ) {
9195 // 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 )
9398 require .NoError (t , err )
9499 t .Cleanup (func () {
95100 _ = db .Close ()
96101 })
97102
98103 // 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 )
100105
101106 // 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 )
103108
104109 // Try to persist the second session and assert that it fails due to the
105110 // linked session not existing in the DB yet.
@@ -125,7 +130,8 @@ func TestLinkingSessions(t *testing.T) {
125130// of the GetGroupID and GetSessionIDs methods.
126131func TestLinkedSessions (t * testing.T ) {
127132 // 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 )
129135 require .NoError (t , err )
130136 t .Cleanup (func () {
131137 _ = db .Close ()
@@ -135,9 +141,9 @@ func TestLinkedSessions(t *testing.T) {
135141 // after are all linked to the prior one. All these sessions belong to
136142 // the same group. The group ID is equivalent to the session ID of the
137143 // 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 )
141147
142148 // Persist the sessions.
143149 require .NoError (t , db .CreateSession (s1 ))
@@ -163,8 +169,8 @@ func TestLinkedSessions(t *testing.T) {
163169
164170 // To ensure that different groups don't interfere with each other,
165171 // 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 )
168174
169175 require .NotEqual (t , s4 .GroupID , s1 .GroupID )
170176
@@ -192,7 +198,8 @@ func TestLinkedSessions(t *testing.T) {
192198// method correctly checks if each session in a group passes a predicate.
193199func TestCheckSessionGroupPredicate (t * testing.T ) {
194200 // 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 )
196203 require .NoError (t , err )
197204 t .Cleanup (func () {
198205 _ = db .Close ()
@@ -202,7 +209,7 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
202209 // function is checked correctly.
203210
204211 // Add a new session to the DB.
205- s1 := newSession (t , db , "label 1" , nil )
212+ s1 := newSession (t , db , clock , "label 1" , nil )
206213 require .NoError (t , db .CreateSession (s1 ))
207214
208215 // Check that the group passes against an appropriate predicate.
@@ -227,7 +234,7 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
227234 require .NoError (t , db .RevokeSession (s1 .LocalPublicKey ))
228235
229236 // 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 )
231238 require .NoError (t , db .CreateSession (s2 ))
232239
233240 // Check that the group passes against an appropriate predicate.
@@ -249,7 +256,7 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
249256 require .False (t , ok )
250257
251258 // 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 )
253260 require .NoError (t , db .CreateSession (s3 ))
254261
255262 // Ensure that the first group is unaffected.
@@ -279,14 +286,15 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
279286 require .True (t , ok )
280287}
281288
282- func newSession (t * testing.T , db Store , label string ,
289+ func newSession (t * testing.T , db Store , clock clock. Clock , label string ,
283290 linkedGroupID * ID ) * Session {
284291
285292 id , priv , err := db .GetUnusedIDAndKeyPair ()
286293 require .NoError (t , err )
287294
288- session , err := NewSession (
295+ session , err := buildSession (
289296 id , priv , label , TypeMacaroonAdmin ,
297+ clock .Now (),
290298 time .Date (99999 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ),
291299 "foo.bar.baz:1234" , true , nil , nil , nil , true , linkedGroupID ,
292300 []PrivacyFlag {ClearPubkeys },
@@ -295,3 +303,32 @@ func newSession(t *testing.T, db Store, label string,
295303
296304 return session
297305}
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