Lab3 baseline: HAProxy, replicated stateless API, shared PostgreSQL/Valkey, idempotent messages
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHashAndVerify(t *testing.T) {
|
||||
password := "correct horse battery staple"
|
||||
encoded, err := Hash(password)
|
||||
if err != nil {
|
||||
t.Fatalf("Hash: %v", err)
|
||||
}
|
||||
if !strings.HasPrefix(encoded, "$argon2id$v=19$m=19456,t=2,p=1$") {
|
||||
t.Fatalf("unexpected PHC prefix: %q", encoded)
|
||||
}
|
||||
match, err := Verify(password, encoded)
|
||||
if err != nil || !match {
|
||||
t.Fatalf("Verify(correct) = %v, %v", match, err)
|
||||
}
|
||||
match, err = Verify(password+"x", encoded)
|
||||
if err != nil || match {
|
||||
t.Fatalf("Verify(wrong) = %v, %v", match, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashUnicodeAndMaximumLength(t *testing.T) {
|
||||
for _, password := range []string{
|
||||
"pässwörd:12✓",
|
||||
strings.Repeat("界", 128),
|
||||
" leading and trailing spaces ",
|
||||
} {
|
||||
encoded, err := Hash(password)
|
||||
if err != nil {
|
||||
t.Fatalf("Hash(%q): %v", password, err)
|
||||
}
|
||||
if match, err := Verify(password, encoded); err != nil || !match {
|
||||
t.Fatalf("Verify(%q) = %v, %v", password, match, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashUsesRandomSalt(t *testing.T) {
|
||||
first, err := Hash("same-password-twice")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := Hash("same-password-twice")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first == second {
|
||||
t.Fatal("two hashes of the same password are identical")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyRejectsMalformedHashes(t *testing.T) {
|
||||
for _, encoded := range []string{
|
||||
"",
|
||||
"plaintext",
|
||||
"$2a$10$abcdefghijklmnopqrstuv",
|
||||
"$argon2id$v=19$m=19456,t=2,p=1$not-base64$hash",
|
||||
"$argon2id$v=19$m=1,t=3,p=2$c2FsdHNhbHQ$aGFzaGhhc2hoYXNoaGFzaA",
|
||||
"$argon2id$v=19$m=19456,t=2,p=1$c2FsdA$aGFzaA",
|
||||
} {
|
||||
if _, err := Verify("password", encoded); err == nil {
|
||||
t.Fatalf("Verify accepted malformed hash %q", encoded)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseBasic(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
raw string
|
||||
username string
|
||||
password string
|
||||
ok bool
|
||||
}{
|
||||
{
|
||||
name: "ordinary",
|
||||
raw: "Basic " + base64.StdEncoding.EncodeToString([]byte("alice:secret")),
|
||||
username: "alice",
|
||||
password: "secret",
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "password contains colon and UTF-8",
|
||||
raw: "bAsIc " + base64.StdEncoding.EncodeToString([]byte("alice:pässwörd:with:colons")),
|
||||
username: "alice",
|
||||
password: "pässwörd:with:colons",
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "unpadded base64",
|
||||
raw: "Basic " + base64.RawStdEncoding.EncodeToString([]byte("alice:secret")),
|
||||
username: "alice",
|
||||
password: "secret",
|
||||
ok: true,
|
||||
},
|
||||
{name: "no scheme", raw: "alice:secret"},
|
||||
{name: "wrong scheme", raw: "Bearer abc"},
|
||||
{name: "no space", raw: "Basic" + base64.StdEncoding.EncodeToString([]byte("alice:secret"))},
|
||||
{name: "bad base64", raw: "Basic !!!"},
|
||||
{name: "no separator", raw: "Basic " + base64.StdEncoding.EncodeToString([]byte("alice"))},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
username, password, err := ParseBasic(tc.raw)
|
||||
if !tc.ok {
|
||||
if err == nil {
|
||||
t.Fatalf("ParseBasic accepted %q", tc.raw)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("ParseBasic: %v", err)
|
||||
}
|
||||
if username != tc.username || password != tc.password {
|
||||
t.Fatalf("got %q/%q, want %q/%q", username, password, tc.username, tc.password)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokenProperties(t *testing.T) {
|
||||
first, err := NewToken()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := NewToken()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first == second {
|
||||
t.Fatal("tokens are not unique")
|
||||
}
|
||||
if len(first) != 43 {
|
||||
t.Fatalf("token length = %d, want 43 (256 bits)", len(first))
|
||||
}
|
||||
if !ValidTokenShape(first) {
|
||||
t.Fatal("issued token fails its own shape check")
|
||||
}
|
||||
if TokenHash(first) == TokenHash(second) {
|
||||
t.Fatal("token hashes collide")
|
||||
}
|
||||
if len(TokenHash(first)) != 64 {
|
||||
t.Fatalf("token hash length = %d", len(TokenHash(first)))
|
||||
}
|
||||
for _, bad := range []string{"", "short", "has space in it", strings.Repeat("a", 129), "abc+/="} {
|
||||
if ValidTokenShape(bad) {
|
||||
t.Fatalf("ValidTokenShape accepted %q", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user