added tests for HashPIN and VerifyPIN
This commit is contained in:
parent
ca8df5989a
commit
29ce4b83bd
@ -1,6 +1,10 @@
|
||||
package common
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func TestIsValidPIN(t *testing.T) {
|
||||
tests := []struct {
|
||||
@ -54,3 +58,95 @@ func TestIsValidPIN(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashPIN(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
pin string
|
||||
}{
|
||||
{
|
||||
name: "Valid PIN with 4 digits",
|
||||
pin: "1234",
|
||||
},
|
||||
{
|
||||
name: "Valid PIN with leading zeros",
|
||||
pin: "0001",
|
||||
},
|
||||
{
|
||||
name: "Empty PIN",
|
||||
pin: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
hashedPIN, err := HashPIN(tt.pin)
|
||||
if err != nil {
|
||||
t.Errorf("HashPIN(%q) returned an error: %v", tt.pin, err)
|
||||
return
|
||||
}
|
||||
|
||||
if hashedPIN == "" {
|
||||
t.Errorf("HashPIN(%q) returned an empty hash", tt.pin)
|
||||
}
|
||||
|
||||
// Ensure the hash can be verified with bcrypt
|
||||
err = bcrypt.CompareHashAndPassword([]byte(hashedPIN), []byte(tt.pin))
|
||||
if tt.pin != "" && err != nil {
|
||||
t.Errorf("HashPIN(%q) produced a hash that does not match: %v", tt.pin, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyPIN(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
pin string
|
||||
hashedPIN string
|
||||
shouldPass bool
|
||||
}{
|
||||
{
|
||||
name: "Valid PIN verification",
|
||||
pin: "1234",
|
||||
hashedPIN: hashPINHelper("1234"),
|
||||
shouldPass: true,
|
||||
},
|
||||
{
|
||||
name: "Invalid PIN verification with incorrect PIN",
|
||||
pin: "5678",
|
||||
hashedPIN: hashPINHelper("1234"),
|
||||
shouldPass: false,
|
||||
},
|
||||
{
|
||||
name: "Invalid PIN verification with empty PIN",
|
||||
pin: "",
|
||||
hashedPIN: hashPINHelper("1234"),
|
||||
shouldPass: false,
|
||||
},
|
||||
{
|
||||
name: "Invalid PIN verification with invalid hash",
|
||||
pin: "1234",
|
||||
hashedPIN: "invalidhash",
|
||||
shouldPass: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := VerifyPIN(tt.hashedPIN, tt.pin)
|
||||
if result != tt.shouldPass {
|
||||
t.Errorf("VerifyPIN(%q, %q) = %v; expected %v", tt.hashedPIN, tt.pin, result, tt.shouldPass)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to hash a PIN for testing purposes
|
||||
func hashPINHelper(pin string) string {
|
||||
hashedPIN, err := HashPIN(pin)
|
||||
if err != nil {
|
||||
panic("Failed to hash PIN for test setup: " + err.Error())
|
||||
}
|
||||
return hashedPIN
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user