forked from mtusi2026/mtusi2026lab7
45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package messenger
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestPasswordHash(t *testing.T) {
|
|
const password = "long-test-password"
|
|
first, err := hashPassword(password)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
second, err := hashPassword(password)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if first == password || strings.Contains(first, password) {
|
|
t.Fatal("hash contains the original password")
|
|
}
|
|
if first == second {
|
|
t.Fatal("independent hashes must use different salts")
|
|
}
|
|
if !verifyPassword(first, password) {
|
|
t.Fatal("correct password was rejected")
|
|
}
|
|
if verifyPassword(first, "wrong-test-password") {
|
|
t.Fatal("wrong password was accepted")
|
|
}
|
|
if verifyPassword("damaged-hash", password) {
|
|
t.Fatal("malformed hash was accepted")
|
|
}
|
|
}
|
|
|
|
func TestArgonParametersAndLegacyBounds(t *testing.T) {
|
|
encoded,err:=hashPassword("legacy-password");if err!=nil{t.Fatal(err)}
|
|
if !strings.HasPrefix(encoded,"$argon2id$v=19$m=19456,t=2,p=1$"){t.Fatal("wrong Argon2id parameters")}
|
|
if needsRehash(encoded){t.Fatal("new hash marked obsolete")}
|
|
if !needsRehash("scrypt$32768$8$1$invalid$invalid"){t.Fatal("legacy not marked for upgrade")}
|
|
if verifyPassword("$argon2id$v=19$m=4294967295,t=2,p=1$bad$bad","x"){t.Fatal("unbounded cost accepted")}
|
|
}
|
|
func BenchmarkArgon2id(b *testing.B) {
|
|
for i:=0;i<b.N;i++ {if _,e:=hashPassword("synthetic-benchmark-password");e!=nil{b.Fatal(e)}}
|
|
}
|