forked from mtusi2026/mtusi2026lab3
127 lines
4.0 KiB
Go
127 lines
4.0 KiB
Go
package auth_tests
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"highload/internal/security"
|
|
)
|
|
|
|
// These are tests of the selected Argon2id implementation, not a requirement
|
|
// that lab2 use particular parameters or a particular encoded hash format.
|
|
func TestPasswordRoundTripAndRandomSalt(t *testing.T) {
|
|
password := "Синтетический:пароль:😀"
|
|
first, err := security.HashPassword(password)
|
|
if err != nil {
|
|
t.Fatal("password hashing failed")
|
|
}
|
|
second, err := security.HashPassword(password)
|
|
if err != nil {
|
|
t.Fatal("password hashing failed")
|
|
}
|
|
if first == password || second == password || first == second {
|
|
t.Fatal("hash must differ from plaintext and have a fresh random salt")
|
|
}
|
|
if !strings.HasPrefix(first, "$argon2id$") {
|
|
t.Fatal("selected password implementation did not encode Argon2id")
|
|
}
|
|
for _, encoded := range []string{first, second} {
|
|
ok, err := security.VerifyPassword(password, encoded)
|
|
if err != nil || !ok {
|
|
t.Fatal("stored hash does not verify the original password")
|
|
}
|
|
ok, err = security.VerifyPassword(password+"x", encoded)
|
|
if err != nil || ok {
|
|
t.Fatal("wrong password was accepted or verifier failed")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPasswordDoesNotTruncateLongUTF8(t *testing.T) {
|
|
// Both passwords share much more than bcrypt's 72-byte limit; each is
|
|
// within the lab's 128-code-point maximum.
|
|
password := strings.Repeat("😀", 127) + "A"
|
|
different := strings.Repeat("😀", 127) + "B"
|
|
encoded, err := security.HashPassword(password)
|
|
if err != nil {
|
|
t.Fatal("maximum-length UTF-8 password cannot be hashed")
|
|
}
|
|
ok, err := security.VerifyPassword(password, encoded)
|
|
if err != nil || !ok {
|
|
t.Fatal("maximum-length UTF-8 password cannot be verified")
|
|
}
|
|
ok, err = security.VerifyPassword(different, encoded)
|
|
if err != nil || ok {
|
|
t.Fatal("password suffix was truncated")
|
|
}
|
|
}
|
|
|
|
func TestPasswordWhitespaceIsSignificant(t *testing.T) {
|
|
password := " synthetic-password "
|
|
encoded, err := security.HashPassword(password)
|
|
if err != nil {
|
|
t.Fatal("password hashing failed")
|
|
}
|
|
ok, err := security.VerifyPassword(strings.TrimSpace(password), encoded)
|
|
if err != nil || ok {
|
|
t.Fatal("password whitespace was discarded")
|
|
}
|
|
}
|
|
|
|
func TestMalformedPasswordHashRejectedWithoutPanic(t *testing.T) {
|
|
valid, err := security.HashPassword("synthetic-password")
|
|
if err != nil {
|
|
t.Fatal("password hashing failed")
|
|
}
|
|
parts := strings.Split(valid, "$")
|
|
if len(parts) != 6 {
|
|
t.Fatal("unexpected encoded hash format")
|
|
}
|
|
cases := map[string]string{
|
|
"empty": "",
|
|
"plaintext": "synthetic-password",
|
|
"wrong_algorithm": strings.Replace(valid, "argon2id", "argon2i", 1),
|
|
"wrong_version": strings.Replace(valid, "$v=19$", "$v=999$", 1),
|
|
"missing_version": strings.Replace(valid, "$v=19$", "$19$", 1),
|
|
"extra_field": valid + "$extra",
|
|
}
|
|
for name, params := range map[string]string{
|
|
"zero_iterations": "m=65536,t=0,p=2",
|
|
"zero_threads": "m=65536,t=3,p=0",
|
|
"tiny_memory": "m=1,t=3,p=2",
|
|
"huge_memory": "m=4294967295,t=3,p=2",
|
|
"huge_iterations": "m=65536,t=4294967295,p=2",
|
|
"huge_threads": "m=65536,t=3,p=255",
|
|
"negative": "m=-1,t=3,p=2",
|
|
"non_numeric": "m=no,t=3,p=2",
|
|
"missing_param": "m=65536,t=3",
|
|
} {
|
|
copyParts := append([]string(nil), parts...)
|
|
copyParts[3] = params
|
|
cases[name] = strings.Join(copyParts, "$")
|
|
}
|
|
for name, field := range map[string]int{"salt": 4, "hash": 5} {
|
|
for label, value := range map[string]string{
|
|
"empty": "", "bad_base64": "!!!", "wrong_size": "YQ",
|
|
} {
|
|
copyParts := append([]string(nil), parts...)
|
|
copyParts[field] = value
|
|
cases[name+"_"+label] = strings.Join(copyParts, "$")
|
|
}
|
|
}
|
|
for name, encoded := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
defer func() {
|
|
if recover() != nil {
|
|
t.Error("malformed stored hash caused a panic")
|
|
}
|
|
}()
|
|
ok, err := security.VerifyPassword("synthetic-password", encoded)
|
|
if ok || !errors.Is(err, security.ErrInvalidHash) {
|
|
t.Error("malformed stored hash was not rejected")
|
|
}
|
|
})
|
|
}
|
|
}
|