apiary/honeypot/ssh/store/cache_test.go
2022-01-13 09:10:36 +01:00

70 lines
2.0 KiB
Go

package store_test
import (
"testing"
"git.t-juice.club/torjus/apiary/honeypot/ssh/store"
)
func TestCacheStore(t *testing.T) {
backend := &store.MemoryStore{}
s := store.NewCachingStore(backend)
testLoginAttemptStore(s, t)
}
func TestCacheTotalStats(t *testing.T) {
backend := &store.MemoryStore{}
// Add initial attempts, to ensure that the cache is correcly initialized with existing attempts
attempts := randomAttempts(50000)
for _, attempt := range attempts {
err := backend.AddAttempt(attempt)
if err != nil {
t.Fatalf("Error adding attempts: %s", err)
}
}
s := store.NewCachingStore(backend)
cacheTotals, err := s.Stats(store.LoginStatsTotals, 0)
if err != nil {
t.Fatalf("Error getting cached stats: %s", err)
}
backendTotals, err := backend.Stats(store.LoginStatsTotals, 0)
if err != nil {
t.Fatalf("Error getting cached stats: %s", err)
}
for i := range cacheTotals {
if cacheTotals[i].Count != backendTotals[i].Count || cacheTotals[i].Name != backendTotals[i].Name {
t.Fatalf("Mismatched totals: Cache: %+v Backend: %+v", cacheTotals[i], backendTotals[i])
}
}
// Add the same attempts again, to ensure that duplicates are handled correctly
for _, attempt := range attempts {
err := s.AddAttempt(attempt)
if err != nil {
t.Fatalf("Error adding attempts: %s", err)
}
}
// Add some new attempts
attempts = randomAttempts(10000)
for _, attempt := range attempts {
err := s.AddAttempt(attempt)
if err != nil {
t.Fatalf("Error adding attempts: %s", err)
}
}
cacheTotals, err = s.Stats(store.LoginStatsTotals, 0)
if err != nil {
t.Fatalf("Error getting cached stats: %s", err)
}
backendTotals, err = backend.Stats(store.LoginStatsTotals, 0)
if err != nil {
t.Fatalf("Error getting cached stats: %s", err)
}
for i := range cacheTotals {
if cacheTotals[i].Count != backendTotals[i].Count || cacheTotals[i].Name != backendTotals[i].Name {
t.Fatalf("Mismatched totals: Cache: %+v Backend: %+v", cacheTotals[i], backendTotals[i])
}
}
}