|
| 1 | +package session |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/lightninglabs/lightning-terminal/accounts" |
| 11 | + "github.com/lightninglabs/lightning-terminal/db" |
| 12 | + "github.com/lightningnetwork/lnd/clock" |
| 13 | + "github.com/lightningnetwork/lnd/macaroons" |
| 14 | + "github.com/lightningnetwork/lnd/sqldb" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + "gopkg.in/macaroon-bakery.v2/bakery/checkers" |
| 17 | + "gopkg.in/macaroon.v2" |
| 18 | +) |
| 19 | + |
| 20 | +// TestSessionsStoreMigration tests the migration of session store from a bolt |
| 21 | +// backed to a SQL database. Note that this test does not attempt to be a |
| 22 | +// complete migration test. |
| 23 | +func TestSessionsStoreMigration(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + |
| 26 | + ctx := context.Background() |
| 27 | + clock := clock.NewTestClock(time.Now()) |
| 28 | + |
| 29 | + // When using build tags that creates a kvdb store for NewTestDB, we |
| 30 | + // skip this test as it is only applicable for postgres and sqlite tags. |
| 31 | + store := NewTestDB(t, clock) |
| 32 | + if _, ok := store.(*BoltStore); ok { |
| 33 | + t.Skipf("Skipping session store migration test for kvdb build") |
| 34 | + } |
| 35 | + |
| 36 | + makeSQLDB := func(t *testing.T, acctStore accounts.Store) (*SQLStore, |
| 37 | + *db.TransactionExecutor[SQLQueries]) { |
| 38 | + |
| 39 | + // Create a sql store with a linked account store. |
| 40 | + testDBStore := NewTestDBWithAccounts(t, clock, acctStore) |
| 41 | + t.Cleanup(func() { |
| 42 | + require.NoError(t, testDBStore.Close()) |
| 43 | + }) |
| 44 | + |
| 45 | + store, ok := testDBStore.(*SQLStore) |
| 46 | + require.True(t, ok) |
| 47 | + |
| 48 | + baseDB := store.BaseDB |
| 49 | + |
| 50 | + genericExecutor := db.NewTransactionExecutor( |
| 51 | + baseDB, func(tx *sql.Tx) SQLQueries { |
| 52 | + return baseDB.WithTx(tx) |
| 53 | + }, |
| 54 | + ) |
| 55 | + |
| 56 | + return store, genericExecutor |
| 57 | + } |
| 58 | + |
| 59 | + // assertMigrationResults asserts that the sql store contains the |
| 60 | + // same sessions as the passed kv store sessions. This is intended to be |
| 61 | + // run after the migration. |
| 62 | + assertMigrationResults := func(t *testing.T, sqlStore *SQLStore, |
| 63 | + kvSessions []*Session) { |
| 64 | + |
| 65 | + for _, kvSession := range kvSessions { |
| 66 | + // Fetch the migrated session from the sql store. |
| 67 | + sqlSession, err := sqlStore.GetSession( |
| 68 | + ctx, kvSession.ID, |
| 69 | + ) |
| 70 | + require.NoError(t, err) |
| 71 | + |
| 72 | + assertEqualSessions(t, kvSession, sqlSession) |
| 73 | + } |
| 74 | + |
| 75 | + // Finally we ensure that the sql store doesn't contain more |
| 76 | + // sessions than the kv store. |
| 77 | + sqlSessions, err := sqlStore.ListAllSessions(ctx) |
| 78 | + require.NoError(t, err) |
| 79 | + require.Equal(t, len(kvSessions), len(sqlSessions)) |
| 80 | + } |
| 81 | + |
| 82 | + tests := []struct { |
| 83 | + name string |
| 84 | + populateDB func( |
| 85 | + t *testing.T, kvStore *BoltStore, |
| 86 | + accountStore accounts.Store, |
| 87 | + ) |
| 88 | + }{ |
| 89 | + { |
| 90 | + name: "empty", |
| 91 | + populateDB: func(t *testing.T, store *BoltStore, |
| 92 | + _ accounts.Store) { |
| 93 | + |
| 94 | + // Don't populate the DB. |
| 95 | + }, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "one session no options", |
| 99 | + populateDB: func(t *testing.T, store *BoltStore, |
| 100 | + _ accounts.Store) { |
| 101 | + |
| 102 | + _, err := store.NewSession( |
| 103 | + ctx, "test", TypeMacaroonAdmin, |
| 104 | + time.Unix(1000, 0), "", |
| 105 | + ) |
| 106 | + require.NoError(t, err) |
| 107 | + }, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "multiple sessions no options", |
| 111 | + populateDB: func(t *testing.T, store *BoltStore, |
| 112 | + _ accounts.Store) { |
| 113 | + |
| 114 | + _, err := store.NewSession( |
| 115 | + ctx, "session1", TypeMacaroonAdmin, |
| 116 | + time.Unix(1000, 0), "", |
| 117 | + ) |
| 118 | + require.NoError(t, err) |
| 119 | + |
| 120 | + _, err = store.NewSession( |
| 121 | + ctx, "session2", TypeMacaroonAdmin, |
| 122 | + time.Unix(1000, 0), "", |
| 123 | + ) |
| 124 | + require.NoError(t, err) |
| 125 | + |
| 126 | + _, err = store.NewSession( |
| 127 | + ctx, "session3", TypeMacaroonAdmin, |
| 128 | + time.Unix(1000, 0), "", |
| 129 | + ) |
| 130 | + require.NoError(t, err) |
| 131 | + }, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "one session with one privacy flag", |
| 135 | + populateDB: func(t *testing.T, store *BoltStore, |
| 136 | + _ accounts.Store) { |
| 137 | + |
| 138 | + _, err := store.NewSession( |
| 139 | + ctx, "test", TypeMacaroonAdmin, |
| 140 | + time.Unix(1000, 0), "", |
| 141 | + WithPrivacy(PrivacyFlags{ClearPubkeys}), |
| 142 | + ) |
| 143 | + require.NoError(t, err) |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "one session with multiple privacy flags", |
| 148 | + populateDB: func(t *testing.T, store *BoltStore, |
| 149 | + _ accounts.Store) { |
| 150 | + |
| 151 | + _, err := store.NewSession( |
| 152 | + ctx, "test", TypeMacaroonAdmin, |
| 153 | + time.Unix(1000, 0), "", |
| 154 | + WithPrivacy(PrivacyFlags{ |
| 155 | + ClearChanInitiator, ClearHTLCs, |
| 156 | + ClearClosingTxIds, |
| 157 | + }), |
| 158 | + ) |
| 159 | + require.NoError(t, err) |
| 160 | + }, |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "one session with a feature config", |
| 164 | + populateDB: func(t *testing.T, store *BoltStore, |
| 165 | + _ accounts.Store) { |
| 166 | + |
| 167 | + featureConfig := map[string][]byte{ |
| 168 | + "AutoFees": {1, 2, 3, 4}, |
| 169 | + "AutoSomething": {4, 3, 4, 5, 6, 6}, |
| 170 | + } |
| 171 | + |
| 172 | + _, err := store.NewSession( |
| 173 | + ctx, "test", TypeMacaroonAdmin, |
| 174 | + time.Unix(1000, 0), "", |
| 175 | + WithFeatureConfig(featureConfig), |
| 176 | + ) |
| 177 | + require.NoError(t, err) |
| 178 | + }, |
| 179 | + }, |
| 180 | + { |
| 181 | + name: "one session with dev server", |
| 182 | + populateDB: func(t *testing.T, store *BoltStore, |
| 183 | + _ accounts.Store) { |
| 184 | + |
| 185 | + _, err := store.NewSession( |
| 186 | + ctx, "test", TypeMacaroonAdmin, |
| 187 | + time.Unix(1000, 0), "", |
| 188 | + WithDevServer(), |
| 189 | + ) |
| 190 | + require.NoError(t, err) |
| 191 | + }, |
| 192 | + }, |
| 193 | + { |
| 194 | + name: "one session with macaroon recipe", |
| 195 | + populateDB: func(t *testing.T, store *BoltStore, |
| 196 | + _ accounts.Store) { |
| 197 | + |
| 198 | + // this test uses caveats & perms from the |
| 199 | + // tlv_test.go |
| 200 | + _, err := store.NewSession( |
| 201 | + ctx, "test", TypeMacaroonAdmin, |
| 202 | + time.Unix(1000, 0), "foo.bar.baz:1234", |
| 203 | + WithMacaroonRecipe(caveats, perms), |
| 204 | + ) |
| 205 | + require.NoError(t, err) |
| 206 | + }, |
| 207 | + }, |
| 208 | + { |
| 209 | + name: "one session with macaroon recipe nil caveats", |
| 210 | + populateDB: func(t *testing.T, store *BoltStore, |
| 211 | + _ accounts.Store) { |
| 212 | + |
| 213 | + // this test uses perms from the tlv_test.go |
| 214 | + _, err := store.NewSession( |
| 215 | + ctx, "test", TypeMacaroonAdmin, |
| 216 | + time.Unix(1000, 0), "foo.bar.baz:1234", |
| 217 | + WithMacaroonRecipe(nil, perms), |
| 218 | + ) |
| 219 | + require.NoError(t, err) |
| 220 | + }, |
| 221 | + }, |
| 222 | + { |
| 223 | + name: "one session with macaroon recipe nil perms", |
| 224 | + populateDB: func(t *testing.T, store *BoltStore, |
| 225 | + _ accounts.Store) { |
| 226 | + |
| 227 | + // this test uses caveats from the tlv_test.go |
| 228 | + _, err := store.NewSession( |
| 229 | + ctx, "test", TypeMacaroonAdmin, |
| 230 | + time.Unix(1000, 0), "foo.bar.baz:1234", |
| 231 | + WithMacaroonRecipe(caveats, nil), |
| 232 | + ) |
| 233 | + require.NoError(t, err) |
| 234 | + }, |
| 235 | + }, |
| 236 | + { |
| 237 | + name: "one session with a linked account", |
| 238 | + populateDB: func(t *testing.T, store *BoltStore, |
| 239 | + acctStore accounts.Store) { |
| 240 | + |
| 241 | + // Create an account with balance |
| 242 | + acct, err := acctStore.NewAccount( |
| 243 | + ctx, 1234, time.Now().Add(time.Hour), |
| 244 | + "", |
| 245 | + ) |
| 246 | + require.NoError(t, err) |
| 247 | + require.False(t, acct.HasExpired()) |
| 248 | + |
| 249 | + // For now, we manually add the account caveat |
| 250 | + // for bbolt compatibility. |
| 251 | + accountCaveat := checkers.Condition( |
| 252 | + macaroons.CondLndCustom, |
| 253 | + fmt.Sprintf("%s %x", |
| 254 | + accounts.CondAccount, |
| 255 | + acct.ID[:], |
| 256 | + ), |
| 257 | + ) |
| 258 | + |
| 259 | + sessCaveats := []macaroon.Caveat{ |
| 260 | + { |
| 261 | + Id: []byte(accountCaveat), |
| 262 | + }, |
| 263 | + } |
| 264 | + |
| 265 | + _, err = store.NewSession( |
| 266 | + ctx, "test", TypeMacaroonAccount, |
| 267 | + time.Unix(1000, 0), "", |
| 268 | + WithAccount(acct.ID), |
| 269 | + WithMacaroonRecipe(sessCaveats, nil), |
| 270 | + ) |
| 271 | + require.NoError(t, err) |
| 272 | + }, |
| 273 | + }, |
| 274 | + { |
| 275 | + name: "linked session", |
| 276 | + populateDB: func(t *testing.T, store *BoltStore, |
| 277 | + _ accounts.Store) { |
| 278 | + |
| 279 | + // First create the initial session for the |
| 280 | + // group. |
| 281 | + sess1, err := store.NewSession( |
| 282 | + ctx, "initSession", TypeMacaroonAdmin, |
| 283 | + time.Unix(1000, 0), "", |
| 284 | + ) |
| 285 | + require.NoError(t, err) |
| 286 | + |
| 287 | + // As the store won't allow us to link a |
| 288 | + // session before all sessions in the group have |
| 289 | + // been revoked, we revoke the session before |
| 290 | + // creating a new session that links to the |
| 291 | + // initial session. |
| 292 | + err = store.ShiftState( |
| 293 | + ctx, sess1.ID, StateCreated, |
| 294 | + ) |
| 295 | + require.NoError(t, err) |
| 296 | + |
| 297 | + err = store.ShiftState( |
| 298 | + ctx, sess1.ID, StateRevoked, |
| 299 | + ) |
| 300 | + require.NoError(t, err) |
| 301 | + |
| 302 | + _, err = store.NewSession( |
| 303 | + ctx, "linkedSession", TypeMacaroonAdmin, |
| 304 | + time.Unix(1000, 0), "", |
| 305 | + WithLinkedGroupID(&sess1.ID), |
| 306 | + ) |
| 307 | + require.NoError(t, err) |
| 308 | + }, |
| 309 | + }, |
| 310 | + } |
| 311 | + |
| 312 | + for _, test := range tests { |
| 313 | + t.Run(test.name, func(t *testing.T) { |
| 314 | + t.Parallel() |
| 315 | + |
| 316 | + // First let's create an account store to link to in |
| 317 | + // the sessions store. Note that this is will be a sql |
| 318 | + // store due to the build tags enabled when running this |
| 319 | + // test, which means that we won't need to migrate the |
| 320 | + // account store in this test. |
| 321 | + accountStore := accounts.NewTestDB(t, clock) |
| 322 | + t.Cleanup(func() { |
| 323 | + require.NoError(t, accountStore.Close()) |
| 324 | + }) |
| 325 | + |
| 326 | + kvStore, err := NewDB( |
| 327 | + t.TempDir(), DBFilename, clock, accountStore, |
| 328 | + ) |
| 329 | + require.NoError(t, err) |
| 330 | + |
| 331 | + t.Cleanup(func() { |
| 332 | + require.NoError(t, kvStore.Close()) |
| 333 | + }) |
| 334 | + |
| 335 | + // populate the kvStore with the test data, in |
| 336 | + // preparation for the test. |
| 337 | + test.populateDB(t, kvStore, accountStore) |
| 338 | + |
| 339 | + // Before we migrate the sessions, we fetch all sessions |
| 340 | + // from the kv store, to ensure that the migration |
| 341 | + // function doesn't mutate the bbolt store sessions. |
| 342 | + // We can then compare them to the sql sessions after |
| 343 | + // the migration has been executed. |
| 344 | + kvSessions, err := kvStore.ListAllSessions(ctx) |
| 345 | + require.NoError(t, err) |
| 346 | + |
| 347 | + // Proceed to create the sql store and execute the |
| 348 | + // migration. |
| 349 | + sqlStore, txEx := makeSQLDB(t, accountStore) |
| 350 | + |
| 351 | + var opts sqldb.MigrationTxOptions |
| 352 | + err = txEx.ExecTx( |
| 353 | + ctx, &opts, func(tx SQLQueries) error { |
| 354 | + return MigrateSessionStoreToSQL( |
| 355 | + ctx, kvStore, tx, |
| 356 | + ) |
| 357 | + }, |
| 358 | + ) |
| 359 | + require.NoError(t, err) |
| 360 | + |
| 361 | + // The migration function will check if the inserted |
| 362 | + // sessions equals the migrated ones, but as a sanity |
| 363 | + // check we'll also fetch migrated sessions from the sql |
| 364 | + // store and compare them to the original. |
| 365 | + assertMigrationResults(t, sqlStore, kvSessions) |
| 366 | + }) |
| 367 | + } |
| 368 | +} |
0 commit comments