Upgrade and add new testing functions
This commit is contained in:
parent
d12679df65
commit
9330ed8e74
13 changed files with 1867 additions and 130 deletions
109
hashing_test.go
Normal file
109
hashing_test.go
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
// Copyright (c) 2026 Zeni Kim <zenik@smarteching.com>
|
||||
// Use of this source code is governed by MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package core
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.smarteching.com/goffee/core/logger"
|
||||
)
|
||||
|
||||
func TestHashPassword(t *testing.T) {
|
||||
h := &Hashing{}
|
||||
password := "super-secret-password"
|
||||
hashed, err := h.HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatalf("failed testing hash password: %v", err)
|
||||
}
|
||||
if hashed == "" {
|
||||
t.Errorf("expected a non-empty hash")
|
||||
}
|
||||
if hashed == password {
|
||||
t.Errorf("hash must differ from the plaintext password")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashPasswordIsSalted(t *testing.T) {
|
||||
h := &Hashing{}
|
||||
first, err := h.HashPassword("same-password")
|
||||
if err != nil {
|
||||
t.Fatalf("failed testing hash password: %v", err)
|
||||
}
|
||||
second, err := h.HashPassword("same-password")
|
||||
if err != nil {
|
||||
t.Fatalf("failed testing hash password: %v", err)
|
||||
}
|
||||
// bcrypt generates a random salt, so equal inputs must yield different hashes.
|
||||
if first == second {
|
||||
t.Errorf("expected different hashes for the same password (salting)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPasswordHash(t *testing.T) {
|
||||
// The CheckPasswordHash error path logs through the global logger; make sure
|
||||
// it is initialized so the test does not panic on a nil logger.
|
||||
loggr = logger.NewLogger(&logger.LogNullDriver{})
|
||||
|
||||
h := &Hashing{}
|
||||
password := "correct-horse-battery-staple"
|
||||
hashed, err := h.HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatalf("failed testing hash password: %v", err)
|
||||
}
|
||||
|
||||
ok, err := h.CheckPasswordHash(hashed, password)
|
||||
if err != nil {
|
||||
t.Fatalf("failed testing check password hash: %v", err)
|
||||
}
|
||||
if !ok {
|
||||
t.Errorf("expected password check to succeed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPasswordHashMismatch(t *testing.T) {
|
||||
loggr = logger.NewLogger(&logger.LogNullDriver{})
|
||||
|
||||
h := &Hashing{}
|
||||
hashed, err := h.HashPassword("the-right-password")
|
||||
if err != nil {
|
||||
t.Fatalf("failed testing hash password: %v", err)
|
||||
}
|
||||
|
||||
ok, err := h.CheckPasswordHash(hashed, "the-wrong-password")
|
||||
if err != nil {
|
||||
t.Fatalf("mismatched password should not return an error, got: %v", err)
|
||||
}
|
||||
if ok {
|
||||
t.Errorf("expected password check to fail for wrong password")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPasswordHashInvalidHash(t *testing.T) {
|
||||
loggr = logger.NewLogger(&logger.LogNullDriver{})
|
||||
|
||||
h := &Hashing{}
|
||||
// An invalid hash that is not a mismatched-but-valid bcrypt hash should
|
||||
// surface as an error rather than a simple false.
|
||||
ok, err := h.CheckPasswordHash("not-a-valid-bcrypt-hash", "whatever")
|
||||
if err == nil {
|
||||
t.Errorf("expected an error for an invalid hash")
|
||||
}
|
||||
if ok {
|
||||
t.Errorf("expected ok to be false for an invalid hash")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPasswordHashEmptyHash(t *testing.T) {
|
||||
loggr = logger.NewLogger(&logger.LogNullDriver{})
|
||||
|
||||
h := &Hashing{}
|
||||
ok, err := h.CheckPasswordHash("", "password")
|
||||
if err == nil {
|
||||
t.Errorf("expected an error for an empty hash")
|
||||
}
|
||||
if ok {
|
||||
t.Errorf("expected ok to be false for an empty hash")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue