From 38ff8949b95c7efd34ce300714df67742be5eaad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Thu, 1 Sep 2022 03:59:18 +0200 Subject: [PATCH] Tryout go test fuzzing --- honeypot/ssh/store/bbolt_test.go | 15 +++++++++++++++ honeypot/ssh/store/memory_test.go | 6 ++++++ honeypot/ssh/store/store_test.go | 19 +++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/honeypot/ssh/store/bbolt_test.go b/honeypot/ssh/store/bbolt_test.go index ee951dd..791f849 100644 --- a/honeypot/ssh/store/bbolt_test.go +++ b/honeypot/ssh/store/bbolt_test.go @@ -21,3 +21,18 @@ func TestBBoltStore(t *testing.T) { } testLoginAttemptStore(s, t) } + +func FuzzBBoltStore(f *testing.F) { + dir := f.TempDir() + file, err := os.CreateTemp(dir, "apiary-test-bbolt") + if err != nil { + f.Fatal(err) + } + fname := file.Name() + file.Close() + s, err := store.NewBBoltStore(fname) + if err != nil { + f.Fatal(err) + } + fuzzLoginAttemptStore(s, f) +} diff --git a/honeypot/ssh/store/memory_test.go b/honeypot/ssh/store/memory_test.go index b50a37e..6c214d7 100644 --- a/honeypot/ssh/store/memory_test.go +++ b/honeypot/ssh/store/memory_test.go @@ -10,6 +10,7 @@ func TestMemoryStore(t *testing.T) { s := &store.MemoryStore{} testLoginAttemptStore(s, t) } + func TestMemoryStoreWithCache(t *testing.T) { backend := &store.MemoryStore{} s := store.NewCachingStore(backend) @@ -22,3 +23,8 @@ func BenchmarkMemoryStore(b *testing.B) { } benchmarkLoginAttemptStore(setupFunc, b) } + +func FuzzMemoryStore(f *testing.F) { + s := &store.MemoryStore{} + fuzzLoginAttemptStore(s, f) +} diff --git a/honeypot/ssh/store/store_test.go b/honeypot/ssh/store/store_test.go index 2f140eb..12c8cd7 100644 --- a/honeypot/ssh/store/store_test.go +++ b/honeypot/ssh/store/store_test.go @@ -323,3 +323,22 @@ func equalAttempts(a, b []models.LoginAttempt) bool { return true } + +func fuzzLoginAttemptStore(s store.LoginAttemptStore, f *testing.F) { + usernames := []string{"username", "root", "ubnt", "pi", "admin"} + for _, username := range usernames { + f.Add(username) + } + f.Fuzz(func(t *testing.T, orig string) { + attempt := models.LoginAttempt{ + Date: time.Now(), + Username: orig, + Password: randomString(8), + Country: "NO", + } + + if err := s.AddAttempt(&attempt); err != nil { + t.Fatalf("error adding: %s", err) + } + }) +}