package tokenstorage_test import ( "context" "testing" "time" "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/test/testsuite" "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/tokenstorage" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func newInMemory(tb testing.TB, encryptionKey string, expires time.Duration) *tokenstorage.InMemory { tb.Helper() storage, err := tokenstorage.NewInMemory(encryptionKey, expires) require.NoError(tb, err) return storage } func newInMemoryWithGC( tb testing.TB, expires, gcInterval time.Duration, ) *tokenstorage.InMemory { tb.Helper() storage, err := tokenstorage.NewInMemoryWithGC(testsuite.Secret, expires, gcInterval) require.NoError(tb, err) return storage } func TestStorageInMemory(t *testing.T) { t.Parallel() ctx := context.Background() tokenStorage := newInMemory(t, testsuite.Secret, time.Millisecond*400) t.Cleanup(func() { require.NoError(t, tokenStorage.Close()) }) require.NoError(t, tokenStorage.Set(ctx, "0", "TEST0")) require.NoError(t, tokenStorage.Set(ctx, "1", "TEST1")) token, err := tokenStorage.Get(ctx, "0") require.NoError(t, err) assert.Equal(t, "TEST0", token) token, err = tokenStorage.Get(ctx, "1") require.NoError(t, err) assert.Equal(t, "TEST1", token) token, err = tokenStorage.Get(ctx, "1") require.NoError(t, err) assert.Equal(t, "TEST1", token) _, err = tokenStorage.Get(ctx, "2") require.ErrorIs(t, err, tokenstorage.ErrNotExists) err = tokenStorage.Delete(ctx, "1") require.NoError(t, err) _, err = tokenStorage.Get(ctx, "1") require.Error(t, err) require.ErrorIs(t, err, tokenstorage.ErrNotExists) } func TestStorageInMemory_Consume(t *testing.T) { t.Parallel() const consumers = 32 ctx := t.Context() tokenStorage := newInMemory(t, testsuite.Secret, time.Hour) t.Cleanup(func() { require.NoError(t, tokenStorage.Close()) }) require.NoError(t, tokenStorage.Set(ctx, "client", "token")) type result struct { token string err error } start := make(chan struct{}) results := make(chan result, consumers) for range consumers { go func() { <-start token, err := tokenStorage.Consume(ctx, "client") results <- result{token: token, err: err} }() } close(start) consumed := 0 for range consumers { outcome := <-results if outcome.err != nil { require.ErrorIs(t, outcome.err, tokenstorage.ErrNotExists) continue } consumed++ require.Equal(t, "token", outcome.token) } require.Equal(t, 1, consumed) } func TestStorageInMemory_Expire(t *testing.T) { t.Parallel() ctx := context.Background() tokenStorage := newInMemory(t, testsuite.Secret, 0) t.Cleanup(func() { require.NoError(t, tokenStorage.Close()) }) require.NoError(t, tokenStorage.Set(ctx, "0", "TEST0")) require.NoError(t, tokenStorage.Set(ctx, "1", "TEST1")) _, err := tokenStorage.Get(ctx, "0") require.ErrorIs(t, err, tokenstorage.ErrNotExists) _, err = tokenStorage.Get(ctx, "1") require.ErrorIs(t, err, tokenstorage.ErrNotExists) } func TestStorageInMemory_InvalidSecret(t *testing.T) { t.Parallel() ctx := context.Background() key := "invalid" tokenStorage := newInMemory(t, key, time.Second) t.Cleanup(func() { require.NoError(t, tokenStorage.Close()) }) // HKDF derives the fixed-size AEAD key from any input length. // Test that encryption/decryption works even with short keys. require.NoError(t, tokenStorage.Set(ctx, "0", "TEST0")) token, err := tokenStorage.Get(ctx, "0") require.NoError(t, err) require.Equal(t, "TEST0", token) } func TestStorageInMemory_InvalidData(t *testing.T) { t.Parallel() tokenStorage := newInMemory(t, testsuite.Secret, 0) t.Cleanup(func() { require.NoError(t, tokenStorage.Close()) }) require.ErrorIs(t, tokenStorage.SetStorage(nil), tokenstorage.ErrNilData) } func TestStorageInMemory_GarbageCollection(t *testing.T) { t.Parallel() ctx := context.Background() // Use short expiration and GC interval for testing tokenStorage := newInMemoryWithGC(t, 50*time.Millisecond, 100*time.Millisecond) t.Cleanup(func() { require.NoError(t, tokenStorage.Close()) }) // Add tokens require.NoError(t, tokenStorage.Set(ctx, "0", "TEST0")) require.NoError(t, tokenStorage.Set(ctx, "1", "TEST1")) // After expiration and GC, tokens should not be retrievable require.EventuallyWithT(t, func(collect *assert.CollectT) { _, err := tokenStorage.Get(ctx, "0") assert.ErrorIs(collect, err, tokenstorage.ErrNotExists) _, err = tokenStorage.Get(ctx, "1") assert.ErrorIs(collect, err, tokenstorage.ErrNotExists) }, time.Second, 25*time.Millisecond) } func TestStorageInMemory_NoGC(t *testing.T) { t.Parallel() ctx := context.Background() // Disable GC by passing 0 interval tokenStorage := newInMemoryWithGC(t, 50*time.Millisecond, 0) t.Cleanup(func() { require.NoError(t, tokenStorage.Close()) }) require.NoError(t, tokenStorage.Set(ctx, "0", "TEST0")) // Access should trigger removal require.EventuallyWithT(t, func(collect *assert.CollectT) { _, err := tokenStorage.Get(ctx, "0") assert.ErrorIs(collect, err, tokenstorage.ErrNotExists) }, time.Second, 25*time.Millisecond) }