forked from mtusi2026/mtusi2026lab7
122 lines
2.9 KiB
Go
122 lines
2.9 KiB
Go
package password
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
type PasswordSuite struct {
|
|
suite.Suite
|
|
hasher *Hasher
|
|
}
|
|
|
|
func TestPasswordSuite(t *testing.T) {
|
|
suite.Run(t, new(PasswordSuite))
|
|
}
|
|
|
|
func (s *PasswordSuite) SetupTest() {
|
|
hasher, err := New(testConfig(), 2)
|
|
s.Require().NoError(err)
|
|
s.hasher = hasher
|
|
}
|
|
|
|
func (s *PasswordSuite) TestHasher() {
|
|
hash, err := s.hasher.Hash("T3st-password-123")
|
|
s.Require().NoError(err)
|
|
|
|
verified, err := s.hasher.Verify("T3st-password-123", hash)
|
|
s.Require().NoError(err)
|
|
s.True(verified)
|
|
|
|
verified, err = s.hasher.Verify("wrong-password", hash)
|
|
s.Require().NoError(err)
|
|
s.False(verified)
|
|
}
|
|
|
|
func (s *PasswordSuite) TestVerifyRejectsMalformedHash() {
|
|
_, err := s.hasher.Verify("password", "invalid")
|
|
s.Error(err)
|
|
}
|
|
|
|
func (s *PasswordSuite) TestNeedsRehashDetectsChangedParameters() {
|
|
currentHash, err := s.hasher.Hash("T3st-password-123")
|
|
s.Require().NoError(err)
|
|
needsRehash, err := s.hasher.NeedsRehash(currentHash)
|
|
s.Require().NoError(err)
|
|
s.False(needsRehash)
|
|
|
|
legacyConfig := testConfig()
|
|
legacyConfig.KeyLength = 16
|
|
legacy, err := New(legacyConfig, 2)
|
|
s.Require().NoError(err)
|
|
legacyHash, err := legacy.Hash("T3st-password-123")
|
|
s.Require().NoError(err)
|
|
needsRehash, err = s.hasher.NeedsRehash(legacyHash)
|
|
s.Require().NoError(err)
|
|
s.True(needsRehash)
|
|
}
|
|
|
|
func (s *PasswordSuite) TestBusyOperationsRecoverAfterCapacityIsReleased() {
|
|
s.hasher.operations <- struct{}{}
|
|
s.hasher.operations <- struct{}{}
|
|
defer func() { <-s.hasher.operations }()
|
|
_, err := s.hasher.Hash("T3st-password-123")
|
|
s.ErrorIs(err, ErrBusy)
|
|
_, err = s.hasher.Verify("T3st-password-123", s.hasher.DummyHash())
|
|
s.ErrorIs(err, ErrBusy)
|
|
|
|
<-s.hasher.operations
|
|
hash, err := s.hasher.Hash("T3st-password-123")
|
|
s.Require().NoError(err)
|
|
verified, err := s.hasher.Verify("T3st-password-123", hash)
|
|
s.Require().NoError(err)
|
|
s.True(verified)
|
|
}
|
|
|
|
func (s *PasswordSuite) TestNewRejectsInvalidOperationLimit() {
|
|
_, err := New(testConfig(), 0)
|
|
s.ErrorIs(err, ErrInvalidConfig)
|
|
}
|
|
|
|
func testConfig() Config {
|
|
return Config{MemoryKiB: 8 * 1024, Iterations: 1, Parallelism: 1, SaltLength: 16, KeyLength: 32}
|
|
}
|
|
|
|
func BenchmarkArgon2idHash(b *testing.B) {
|
|
hasher, err := New(productionConfig(), 2)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for range b.N {
|
|
if _, err := hasher.Hash("T3st-password-123"); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkArgon2idVerify(b *testing.B) {
|
|
hasher, err := New(productionConfig(), 2)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
hash, err := hasher.Hash("T3st-password-123")
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for range b.N {
|
|
verified, err := hasher.Verify("T3st-password-123", hash)
|
|
if err != nil || !verified {
|
|
b.Fatalf("verify: result=%v error=%v", verified, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func productionConfig() Config {
|
|
return Config{MemoryKiB: 64 * 1024, Iterations: 3, Parallelism: 2, SaltLength: 16, KeyLength: 32}
|
|
}
|