Skip to content

Commit 68a38fe

Browse files
committed
firewalldb: pass accountStore & rootKeyStore to mig tests
1 parent 26cd843 commit 68a38fe

File tree

1 file changed

+106
-18
lines changed

1 file changed

+106
-18
lines changed

firewalldb/sql_migration_test.go

Lines changed: 106 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,61 @@ var (
3535
testEntryValue = []byte{1, 2, 3}
3636
)
3737

38+
// rootKeyMockStore is a mock implementation of a macaroon service store that
39+
// can be used to generate mock root keys for testing.
40+
type rootKeyMockStore struct {
41+
// rootKeys is a slice of all root keys that have been added to the
42+
// store.
43+
rootKeys [][]byte
44+
}
45+
46+
// addRootKeyFromIDPrefix adds a new root key to the store, using the passed
47+
// 4 byte prefix. The function generates a root key that starts with the 4 byte
48+
// prefix, followed by a random 4 byte suffix.
49+
func (r *rootKeyMockStore) addRootKeyFromIDPrefix(prefix [4]byte) [4]byte {
50+
// As a real root key is 8 bytes, we need to generate a random 4 byte
51+
// suffix to append to the passed 4 byte prefix.
52+
rootKey := append(prefix[:], randomBytes(4)...)
53+
r.rootKeys = append(r.rootKeys, rootKey)
54+
55+
return prefix
56+
}
57+
58+
// addRootKeyFromAcctID adds a new root key to the store, using the first 4
59+
// bytes of the passed account ID as the prefix for the root key, followed by a
60+
// random 4 byte suffix.
61+
func (r *rootKeyMockStore) addRootKeyFromAcctID(id accounts.AccountID) [4]byte {
62+
var acctPrefix [4]byte
63+
copy(acctPrefix[:], id[:4])
64+
65+
return r.addRootKeyFromIDPrefix(acctPrefix)
66+
}
67+
68+
// addRandomRootKey adds a new random root key to the store, and returns the
69+
// first 4 bytes of the root key as the root key ID.
70+
func (r *rootKeyMockStore) addRandomRootKey() [4]byte {
71+
rootKey := randomBytes(8)
72+
r.rootKeys = append(r.rootKeys, rootKey)
73+
74+
// As we only return the first 4 bytes as the root key ID, we copy
75+
// those into a fixed size array.
76+
var shortID [4]byte
77+
copy(shortID[:], rootKey[:4])
78+
79+
return shortID
80+
}
81+
82+
// getAllRootKeys returns all root keys that have been added to the store.
83+
func (r *rootKeyMockStore) getAllRootKeys() [][]byte {
84+
return r.rootKeys
85+
}
86+
87+
type expectedAction struct {
88+
action *Action
89+
sessionSqlID fn.Option[int64]
90+
accountSqlID fn.Option[int64]
91+
}
92+
3893
// expectedResult represents the expected result of a migration test.
3994
type expectedResult struct {
4095
kvEntries []*kvEntry
@@ -294,13 +349,16 @@ func TestFirewallDBMigration(t *testing.T) {
294349
tests := []struct {
295350
name string
296351
populateDB func(t *testing.T, ctx context.Context,
297-
boltDB *BoltDB, sessionStore session.Store) *expectedResult
352+
boltDB *BoltDB, sessionStore session.Store,
353+
accountsStore accounts.Store,
354+
rKeyStore *rootKeyMockStore) *expectedResult
298355
}{
299356
{
300357
name: "empty",
301358
populateDB: func(t *testing.T, ctx context.Context,
302-
boltDB *BoltDB,
303-
sessionStore session.Store) *expectedResult {
359+
boltDB *BoltDB, sessionStore session.Store,
360+
accountsStore accounts.Store,
361+
rKeyStore *rootKeyMockStore) *expectedResult {
304362

305363
// Don't populate the DB, and return empty kv
306364
// records and privacy pairs.
@@ -384,9 +442,12 @@ func TestFirewallDBMigration(t *testing.T) {
384442
require.NoError(t, firewallStore.Close())
385443
})
386444

445+
rootKeyStore := &rootKeyMockStore{}
446+
387447
// Populate the kv store.
388448
entries := test.populateDB(
389449
t, ctx, firewallStore, sessionsStore,
450+
accountStore, rootKeyStore,
390451
)
391452

392453
// Create the SQL store that we will migrate the data
@@ -412,7 +473,8 @@ func TestFirewallDBMigration(t *testing.T) {
412473
// globalEntries populates the kv store with one global entry for the temp
413474
// store, and one for the perm store.
414475
func globalEntries(t *testing.T, ctx context.Context, boltDB *BoltDB,
415-
_ session.Store) *expectedResult {
476+
_ session.Store, _ accounts.Store,
477+
_ *rootKeyMockStore) *expectedResult {
416478

417479
return insertTempAndPermEntry(
418480
t, ctx, boltDB, testRuleName, fn.None[[]byte](),
@@ -424,7 +486,8 @@ func globalEntries(t *testing.T, ctx context.Context, boltDB *BoltDB,
424486
// entry for the local temp store, and one session specific entry for the perm
425487
// local store.
426488
func sessionSpecificEntries(t *testing.T, ctx context.Context, boltDB *BoltDB,
427-
sessionStore session.Store) *expectedResult {
489+
sessionStore session.Store, _ accounts.Store,
490+
_ *rootKeyMockStore) *expectedResult {
428491

429492
groupAlias := getNewSessionAlias(t, ctx, sessionStore)
430493

@@ -438,7 +501,8 @@ func sessionSpecificEntries(t *testing.T, ctx context.Context, boltDB *BoltDB,
438501
// entry for the local temp store, and one feature specific entry for the perm
439502
// local store.
440503
func featureSpecificEntries(t *testing.T, ctx context.Context, boltDB *BoltDB,
441-
sessionStore session.Store) *expectedResult {
504+
sessionStore session.Store, _ accounts.Store,
505+
_ *rootKeyMockStore) *expectedResult {
442506

443507
groupAlias := getNewSessionAlias(t, ctx, sessionStore)
444508

@@ -456,7 +520,8 @@ func featureSpecificEntries(t *testing.T, ctx context.Context, boltDB *BoltDB,
456520
// any entries when the entry set is more complex than just a single entry at
457521
// each level.
458522
func allEntryCombinations(t *testing.T, ctx context.Context, boltDB *BoltDB,
459-
sessionStore session.Store) *expectedResult {
523+
sessionStore session.Store, acctStore accounts.Store,
524+
rStore *rootKeyMockStore) *expectedResult {
460525

461526
var result []*kvEntry
462527
add := func(entry *expectedResult) {
@@ -465,9 +530,13 @@ func allEntryCombinations(t *testing.T, ctx context.Context, boltDB *BoltDB,
465530

466531
// First lets create standard entries at all levels, which represents
467532
// the entries added by other tests.
468-
add(globalEntries(t, ctx, boltDB, sessionStore))
469-
add(sessionSpecificEntries(t, ctx, boltDB, sessionStore))
470-
add(featureSpecificEntries(t, ctx, boltDB, sessionStore))
533+
add(globalEntries(t, ctx, boltDB, sessionStore, acctStore, rStore))
534+
add(sessionSpecificEntries(
535+
t, ctx, boltDB, sessionStore, acctStore, rStore,
536+
))
537+
add(featureSpecificEntries(
538+
t, ctx, boltDB, sessionStore, acctStore, rStore,
539+
))
471540

472541
groupAlias := getNewSessionAlias(t, ctx, sessionStore)
473542

@@ -647,7 +716,8 @@ func insertKvEntry(t *testing.T, ctx context.Context,
647716
// across all possible combinations of different levels of entries in the kv
648717
// store. All values and different bucket names are randomly generated.
649718
func randomKVEntries(t *testing.T, ctx context.Context,
650-
boltDB *BoltDB, sessionStore session.Store) *expectedResult {
719+
boltDB *BoltDB, sessionStore session.Store, _ accounts.Store,
720+
_ *rootKeyMockStore) *expectedResult {
651721

652722
var (
653723
// We set the number of entries to insert to 1000, as that
@@ -769,23 +839,26 @@ func randomKVEntries(t *testing.T, ctx context.Context,
769839
// oneSessionAndPrivPair inserts 1 session with 1 privacy pair into the
770840
// boltDB.
771841
func oneSessionAndPrivPair(t *testing.T, ctx context.Context,
772-
boltDB *BoltDB, sessionStore session.Store) *expectedResult {
842+
boltDB *BoltDB, sessionStore session.Store, _ accounts.Store,
843+
_ *rootKeyMockStore) *expectedResult {
773844

774845
return createPrivacyPairs(t, ctx, boltDB, sessionStore, 1, 1)
775846
}
776847

777848
// oneSessionsMultiplePrivPairs inserts 1 session with 10 privacy pairs into the
778849
// boltDB.
779850
func oneSessionsMultiplePrivPairs(t *testing.T, ctx context.Context,
780-
boltDB *BoltDB, sessionStore session.Store) *expectedResult {
851+
boltDB *BoltDB, sessionStore session.Store, _ accounts.Store,
852+
_ *rootKeyMockStore) *expectedResult {
781853

782854
return createPrivacyPairs(t, ctx, boltDB, sessionStore, 1, 10)
783855
}
784856

785857
// multipleSessionsAndPrivacyPairs inserts 5 sessions with 10 privacy pairs
786858
// per session into the boltDB.
787859
func multipleSessionsAndPrivacyPairs(t *testing.T, ctx context.Context,
788-
boltDB *BoltDB, sessionStore session.Store) *expectedResult {
860+
boltDB *BoltDB, sessionStore session.Store, _ accounts.Store,
861+
_ *rootKeyMockStore) *expectedResult {
789862

790863
return createPrivacyPairs(t, ctx, boltDB, sessionStore, 5, 10)
791864
}
@@ -847,7 +920,8 @@ func createPrivacyPairs(t *testing.T, ctx context.Context,
847920

848921
// randomPrivacyPairs creates a random number of privacy pairs to 10 sessions.
849922
func randomPrivacyPairs(t *testing.T, ctx context.Context,
850-
boltDB *BoltDB, sessionStore session.Store) *expectedResult {
923+
boltDB *BoltDB, sessionStore session.Store, _ accounts.Store,
924+
_ *rootKeyMockStore) *expectedResult {
851925

852926
numSessions := 10
853927
maxPairsPerSession := 20
@@ -905,10 +979,15 @@ func randomPrivacyPairs(t *testing.T, ctx context.Context,
905979
// TODO(viktor): Extend this function to also populate it with random action
906980
// entries, once the actions migration has been implemented.
907981
func randomFirewallDBEntries(t *testing.T, ctx context.Context,
908-
boltDB *BoltDB, sessionStore session.Store) *expectedResult {
982+
boltDB *BoltDB, sessionStore session.Store, acctStore accounts.Store,
983+
rStore *rootKeyMockStore) *expectedResult {
909984

910-
kvEntries := randomKVEntries(t, ctx, boltDB, sessionStore)
911-
privPairs := randomPrivacyPairs(t, ctx, boltDB, sessionStore)
985+
kvEntries := randomKVEntries(
986+
t, ctx, boltDB, sessionStore, acctStore, rStore,
987+
)
988+
privPairs := randomPrivacyPairs(
989+
t, ctx, boltDB, sessionStore, acctStore, rStore,
990+
)
912991

913992
return &expectedResult{
914993
kvEntries: kvEntries.kvEntries,
@@ -927,3 +1006,12 @@ func randomString(n int) string {
9271006
}
9281007
return string(b)
9291008
}
1009+
1010+
// randomBytes generates a random byte array of the passed length n.
1011+
func randomBytes(n int) []byte {
1012+
b := make([]byte, n)
1013+
for i := range b {
1014+
b[i] = byte(rand.Intn(256)) // Random int between 0-255, then cast to byte
1015+
}
1016+
return b
1017+
}

0 commit comments

Comments
 (0)