package auth import "testing" func TestHashAndVerifyPassword(t *testing.T) { hash, err := HashPassword("correct horse battery staple") if err != nil { t.Fatalf("HashPassword: %v", err) } ok, err := VerifyPassword("correct horse battery staple", hash) if err != nil { t.Fatalf("VerifyPassword: %v", err) } if !ok { t.Error("VerifyPassword returned false for the correct password") } ok, err = VerifyPassword("wrong password", hash) if err != nil { t.Fatalf("VerifyPassword: %v", err) } if ok { t.Error("VerifyPassword returned true for an incorrect password") } } func TestVerifyPasswordMalformedHash(t *testing.T) { if _, err := VerifyPassword("anything", "not-a-valid-hash"); err == nil { t.Error("expected an error for a malformed hash, got nil") } } func TestNewSessionTokenIsUnpredictableAndHashVerifiable(t *testing.T) { token1, hash1, err := NewSessionToken() if err != nil { t.Fatalf("NewSessionToken: %v", err) } token2, _, err := NewSessionToken() if err != nil { t.Fatalf("NewSessionToken: %v", err) } if token1 == token2 { t.Error("two calls to NewSessionToken produced the same token") } gotHash := HashSessionToken(token1) if string(gotHash) != string(hash1) { t.Error("HashSessionToken(token) does not match the hash returned alongside it") } }