Tryout go test fuzzing

This commit is contained in:
Torjus Håkestad 2022-09-01 03:59:18 +02:00
parent fce78d234d
commit 38ff8949b9
No known key found for this signature in database
GPG Key ID: C6FD38B820D295FC
3 changed files with 40 additions and 0 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}
})
}