From 5c6a9607555828127c420a351b42ca2bbcbc73ec Mon Sep 17 00:00:00 2001 From: Jose Sitanggang Date: Sat, 11 Mar 2023 16:46:21 +0700 Subject: [PATCH] test: add more test to increase test coverage --- bcrypt/bcrypt.go | 15 +++++---------- bcrypt/bcrypt_test.go | 28 ++++++++++++++++++++++++++++ passwd.go | 5 +---- passwd_test.go | 4 +++- 4 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 bcrypt/bcrypt_test.go diff --git a/bcrypt/bcrypt.go b/bcrypt/bcrypt.go index f67b3d3..cce2224 100644 --- a/bcrypt/bcrypt.go +++ b/bcrypt/bcrypt.go @@ -2,7 +2,6 @@ package bcrypt import ( - "fmt" "golang.org/x/crypto/bcrypt" ) @@ -12,13 +11,8 @@ type bcryptImpl int // Hash generates a bcrypt hash from the specified plaintext password using the configured cost. // It returns the resulting hash as a string and any errors that occur during the hash generation. func (b bcryptImpl) Hash(plain string) (string, error) { - switch b { - default: - return "", fmt.Errorf("passwd: bcryptImpl: hash: unsuported cost") - case DefaultCost: - b, err := bcrypt.GenerateFromPassword([]byte(plain), bcrypt.DefaultCost) - return string(b), err - } + hash, err := bcrypt.GenerateFromPassword([]byte(plain), int(b)) + return string(hash), err } // Compare compares the specified plaintext password with the specified bcrypt hash. @@ -27,5 +21,6 @@ func (b bcryptImpl) Compare(hash string, plain string) error { return bcrypt.CompareHashAndPassword([]byte(hash), []byte(plain)) } -// DefaultCost is a bcrypt algorithm with default cost. -const DefaultCost = bcryptImpl(1) +const DefaultCost = bcryptImpl(bcrypt.DefaultCost) // DefaultCost is a bcrypt algorithm with default cost. +const MaxCost = bcryptImpl(bcrypt.MaxCost) // MaxCost is a bcrypt algorithm with max cost. +const MinCost = bcryptImpl(bcrypt.MinCost) // MinCost is a bcrypt algorithm with min cost. diff --git a/bcrypt/bcrypt_test.go b/bcrypt/bcrypt_test.go new file mode 100644 index 0000000..43384f1 --- /dev/null +++ b/bcrypt/bcrypt_test.go @@ -0,0 +1,28 @@ +package bcrypt_test + +import ( + "github.com/pkg-id/passwd" + "github.com/pkg-id/passwd/bcrypt" + "testing" +) + +func TestBcryptImpl(t *testing.T) { + impls := []passwd.HashComparer{ + bcrypt.MinCost, + bcrypt.DefaultCost, + bcrypt.MinCost, + } + + const plain = "abc123" + + for _, impl := range impls { + hash, err := impl.Hash(plain) + if err != nil { + t.Fatalf("expect no error; got an error: %v", err) + } + + if err := impl.Compare(hash, plain); err != nil { + t.Errorf("expect password is match") + } + } +} diff --git a/passwd.go b/passwd.go index 0bd120a..fb8133e 100644 --- a/passwd.go +++ b/passwd.go @@ -44,10 +44,7 @@ type Password string // It returns an error if the hash generation fails. func (p Password) Value() (driver.Value, error) { hash, err := hashComparer.Hash(string(p)) - if err != nil { - return nil, fmt.Errorf("passwd: Password.Value: generate hash: %w", err) - } - return driver.Value(hash), nil + return driver.Value(hash), err } // Scan implements the sql.Scanner interface. It sets the password value to an empty string if the source value is nil. diff --git a/passwd_test.go b/passwd_test.go index 625bc45..96269b8 100644 --- a/passwd_test.go +++ b/passwd_test.go @@ -3,12 +3,14 @@ package passwd_test import ( "encoding/json" "github.com/pkg-id/passwd" + passwdBcrypt "github.com/pkg-id/passwd/bcrypt" "golang.org/x/crypto/bcrypt" "strings" "testing" ) func TestPassword_Value(t *testing.T) { + passwd.SetHashComparer(passwdBcrypt.DefaultCost) pwd := passwd.Password("pass1234") value, err := pwd.Value() if err != nil { @@ -34,7 +36,7 @@ func TestPassword_Scan(t *testing.T) { t.Errorf("value: expected no error, but got an error: %v", err) } - src := value.(string) + src := []byte(value.(string)) var scanned passwd.Password if err := scanned.Scan(src); err != nil { t.Errorf("scan: expected no error, but got an error: %v", err)