Files
cms-server/internal/cryptography/bcrypt_test.go
Asai Neko 210b8b08ce
All checks were successful
Server Check Build (NixCN CMS) TeamCity build finished
Add test for all components
Signed-off-by: Asai Neko <sugar@sne.moe>
2026-03-26 18:19:26 +08:00

37 lines
874 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cryptography
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBcryptHashAndVerify(t *testing.T) {
input := "super-secret-password"
hash, err := BcryptHash(input)
require.NoError(t, err)
require.NotEmpty(t, hash)
assert.NotEqual(t, input, hash)
assert.True(t, BcryptVerify(input, hash))
}
func TestBcryptVerifyWrongPassword(t *testing.T) {
hash, err := BcryptHash("correct-password")
require.NoError(t, err)
assert.False(t, BcryptVerify("wrong-password", hash))
}
func TestBcryptHashUniqueness(t *testing.T) {
h1, _ := BcryptHash("same")
h2, _ := BcryptHash("same")
// bcrypt uses a random salt hashes must differ
assert.NotEqual(t, h1, h2)
}
func TestBcryptHashEmptyInput(t *testing.T) {
hash, err := BcryptHash("")
require.NoError(t, err)
assert.True(t, BcryptVerify("", hash))
}