github.com/argoproj/argo-cd@v1.8.7/util/password/password_test.go (about)

     1  package password
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func testPasswordHasher(t *testing.T, h PasswordHasher) {
     8  	// Use the default work factor
     9  	const (
    10  		defaultPassword = "Hello, world!"
    11  		pollution       = "extradata12345"
    12  	)
    13  	hashedPassword, _ := h.HashPassword(defaultPassword)
    14  	if !h.VerifyPassword(defaultPassword, hashedPassword) {
    15  		t.Errorf("Password %q should have validated against hash %q", defaultPassword, hashedPassword)
    16  	}
    17  	if h.VerifyPassword(defaultPassword, pollution+hashedPassword) {
    18  		t.Errorf("Password %q should NOT have validated against hash %q", defaultPassword, pollution+hashedPassword)
    19  	}
    20  }
    21  
    22  func TestBcryptPasswordHasher(t *testing.T) {
    23  	// Use the default work factor
    24  	h := BcryptPasswordHasher{0}
    25  	testPasswordHasher(t, h)
    26  }
    27  
    28  func TestDummyPasswordHasher(t *testing.T) {
    29  	h := DummyPasswordHasher{}
    30  	testPasswordHasher(t, h)
    31  }
    32  
    33  func TestPasswordHashing(t *testing.T) {
    34  	const (
    35  		defaultPassword = "Hello, world!"
    36  		blankPassword   = ""
    37  	)
    38  	hashers := []PasswordHasher{
    39  		BcryptPasswordHasher{0},
    40  		DummyPasswordHasher{},
    41  	}
    42  
    43  	hashedPassword, _ := hashPasswordWithHashers(defaultPassword, hashers)
    44  	valid, stale := verifyPasswordWithHashers(defaultPassword, hashedPassword, hashers)
    45  	if !valid {
    46  		t.Errorf("Password %q should have validated against hash %q", defaultPassword, hashedPassword)
    47  	}
    48  	if stale {
    49  		t.Errorf("Password %q should not have been marked stale against hash %q", defaultPassword, hashedPassword)
    50  	}
    51  	valid, stale = verifyPasswordWithHashers(defaultPassword, defaultPassword, hashers)
    52  	if !valid {
    53  		t.Errorf("Password %q should have validated against itself with dummy hasher", defaultPassword)
    54  	}
    55  	if !stale {
    56  		t.Errorf("Password %q should have been acknowledged stale against itself with dummy hasher", defaultPassword)
    57  	}
    58  
    59  	hashedPassword, err := hashPasswordWithHashers(blankPassword, hashers)
    60  	if err == nil {
    61  		t.Errorf("Blank password should have produced error, rather than hash %q", hashedPassword)
    62  	}
    63  
    64  	valid, _ = verifyPasswordWithHashers(blankPassword, "", hashers)
    65  	if valid != false {
    66  		t.Errorf("Blank password should have failed verification")
    67  	}
    68  }