Skip to content

Commit

Permalink
test: add more test to increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
josestg committed Mar 11, 2023
1 parent 70ac757 commit 5c6a960
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
15 changes: 5 additions & 10 deletions bcrypt/bcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package bcrypt

import (
"fmt"
"golang.org/x/crypto/bcrypt"
)

Expand All @@ -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.
Expand All @@ -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.
28 changes: 28 additions & 0 deletions bcrypt/bcrypt_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
5 changes: 1 addition & 4 deletions passwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion passwd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 5c6a960

Please sign in to comment.