apiary/honeypot/ssh/store/cache_test.go

70 lines
2.0 KiB
Go
Raw Normal View History

2021-09-17 00:01:43 +00:00
package store_test
import (
"testing"
2021-10-21 10:36:01 +00:00
"github.uio.no/torjus/apiary/honeypot/ssh/store"
2021-09-17 00:01:43 +00:00
)
func TestCacheStore(t *testing.T) {
backend := &store.MemoryStore{}
s := store.NewCachingStore(backend)
testLoginAttemptStore(s, t)
}
2021-11-03 20:24:23 +00:00
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])
}
}
}