|
| 1 | +package migration2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/btcsuite/btcd/btcec/v2" |
| 7 | + "github.com/lightninglabs/lightning-terminal/session/migtest" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "go.etcd.io/bbolt" |
| 10 | +) |
| 11 | + |
| 12 | +// ID represents the id of a session. |
| 13 | +type ID [4]byte |
| 14 | + |
| 15 | +// TestMigrateSessionIDToGroupIDIndex tests that the |
| 16 | +// MigrateSessionIDToGroupIDIndex migration correctly back-fills the session ID |
| 17 | +// to group ID index along with the group ID to session ID index. |
| 18 | +func TestMigrateSessionIDToGroupIDIndex(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + // Make a few session IDs. |
| 22 | + sess1ID, sess1Key := newSessionID(t) |
| 23 | + sess2ID, sess2Key := newSessionID(t) |
| 24 | + sess3ID, sess3Key := newSessionID(t) |
| 25 | + |
| 26 | + // Put together a sample session ID index DB based on the above. |
| 27 | + idIndexBefore := map[string]interface{}{ |
| 28 | + string(sess1ID[:]): map[string]interface{}{ |
| 29 | + string(sessionKeyKey): string(sess1Key), |
| 30 | + }, |
| 31 | + string(sess2ID[:]): map[string]interface{}{ |
| 32 | + string(sessionKeyKey): string(sess2Key), |
| 33 | + }, |
| 34 | + string(sess3ID[:]): map[string]interface{}{ |
| 35 | + string(sessionKeyKey): string(sess3Key), |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + // sessionDBBefore is what our session DB will look like before the |
| 40 | + // migration. |
| 41 | + sessionDBBefore := map[string]interface{}{ |
| 42 | + string(idIndexKey): idIndexBefore, |
| 43 | + string(groupIDIndexKey): map[string]interface{}{}, |
| 44 | + } |
| 45 | + |
| 46 | + before := func(tx *bbolt.Tx) error { |
| 47 | + return migtest.RestoreDB(tx, sessionBucketKey, sessionDBBefore) |
| 48 | + } |
| 49 | + |
| 50 | + // Put together what we expect the resulting id-index bucket to look |
| 51 | + // like after the migration. |
| 52 | + idIndexAfter := map[string]interface{}{ |
| 53 | + string(sess1ID[:]): map[string]interface{}{ |
| 54 | + string(sessionKeyKey): string(sess1Key), |
| 55 | + string(groupIDKey): string(sess1ID[:]), |
| 56 | + }, |
| 57 | + string(sess2ID[:]): map[string]interface{}{ |
| 58 | + string(sessionKeyKey): string(sess2Key), |
| 59 | + string(groupIDKey): string(sess2ID[:]), |
| 60 | + }, |
| 61 | + string(sess3ID[:]): map[string]interface{}{ |
| 62 | + string(sessionKeyKey): string(sess3Key), |
| 63 | + string(groupIDKey): string(sess3ID[:]), |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + // Put together what we expect the resulting group-ID-index bucket to |
| 68 | + // look like after the migration. |
| 69 | + groupIDIndexAfter := map[string]interface{}{ |
| 70 | + string(sess1ID[:]): map[string]interface{}{ |
| 71 | + string(sessionIDKey): map[string]interface{}{ |
| 72 | + sequenceString(1): string(sess1ID[:]), |
| 73 | + }, |
| 74 | + }, |
| 75 | + string(sess2ID[:]): map[string]interface{}{ |
| 76 | + string(sessionIDKey): map[string]interface{}{ |
| 77 | + sequenceString(1): string(sess2ID[:]), |
| 78 | + }, |
| 79 | + }, |
| 80 | + string(sess3ID[:]): map[string]interface{}{ |
| 81 | + string(sessionIDKey): map[string]interface{}{ |
| 82 | + sequenceString(1): string(sess3ID[:]), |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + // sessionDBAfter is what our session DB will look like after the |
| 88 | + // migration. |
| 89 | + sessionDBAfter := map[string]interface{}{ |
| 90 | + string(idIndexKey): idIndexAfter, |
| 91 | + string(groupIDIndexKey): groupIDIndexAfter, |
| 92 | + } |
| 93 | + |
| 94 | + after := func(tx *bbolt.Tx) error { |
| 95 | + return migtest.VerifyDB(tx, sessionBucketKey, sessionDBAfter) |
| 96 | + } |
| 97 | + |
| 98 | + migtest.ApplyMigration( |
| 99 | + t, before, after, MigrateSessionIDToGroupIndex, false, |
| 100 | + ) |
| 101 | +} |
| 102 | + |
| 103 | +// newSessionID is a helper function that can be used to generate a new session |
| 104 | +// ID and key. |
| 105 | +func newSessionID(t *testing.T) (ID, []byte) { |
| 106 | + privateKey, err := btcec.NewPrivateKey() |
| 107 | + require.NoError(t, err) |
| 108 | + |
| 109 | + key := privateKey.PubKey().SerializeCompressed() |
| 110 | + |
| 111 | + var id ID |
| 112 | + copy(id[:], key) |
| 113 | + |
| 114 | + return id, key |
| 115 | +} |
| 116 | + |
| 117 | +func sequenceString(id uint64) string { |
| 118 | + var seqNoBytes [8]byte |
| 119 | + byteOrder.PutUint64(seqNoBytes[:], id) |
| 120 | + |
| 121 | + return string(seqNoBytes[:]) |
| 122 | +} |
0 commit comments