Files
fn_registry/functions/infra/bcrypt_htpasswd_test.go
T
egutierrez aca2348a20 chore: auto-commit (97 archivos)
- .claude/CLAUDE.md
- .claude/agents/fn-recopilador/SKILL.md
- .claude/rules/INDEX.md
- .claude/rules/cpp_apps.md
- bash/functions/infra/build_cpp_windows.sh
- cpp/CMakeLists.txt
- cpp/PATTERNS.md
- cpp/framework/app_base.cpp
- cpp/framework/app_base.h
- dev/issues/README.md
- ...

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-09 18:11:24 +02:00

79 lines
2.0 KiB
Go

package infra
import (
"strings"
"testing"
"golang.org/x/crypto/bcrypt"
)
func TestBcryptHtpasswd(t *testing.T) {
t.Run("hash valido pasa CompareHashAndPassword", func(t *testing.T) {
line, err := BcryptHtpasswd("lucas", "s3cr3t", 4)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
parts := strings.SplitN(line, ":", 2)
if len(parts) != 2 {
t.Fatalf("expected user:hash, got %q", line)
}
if err := bcrypt.CompareHashAndPassword([]byte(parts[1]), []byte("s3cr3t")); err != nil {
t.Errorf("hash does not match password: %v", err)
}
})
t.Run("formato es user:hash", func(t *testing.T) {
line, err := BcryptHtpasswd("admin", "pass", 4)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.HasPrefix(line, "admin:") {
t.Errorf("expected line to start with 'admin:', got %q", line)
}
parts := strings.SplitN(line, ":", 2)
if len(parts) != 2 || parts[1] == "" {
t.Errorf("expected non-empty hash after colon, got %q", line)
}
})
t.Run("cost cero usa default 10", func(t *testing.T) {
line, err := BcryptHtpasswd("user", "password", 0)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
parts := strings.SplitN(line, ":", 2)
cost, err := bcrypt.Cost([]byte(parts[1]))
if err != nil {
t.Fatalf("could not extract cost: %v", err)
}
if cost != 10 {
t.Errorf("expected cost 10, got %d", cost)
}
})
t.Run("error si user vacio", func(t *testing.T) {
_, err := BcryptHtpasswd("", "pass", 4)
if err == nil {
t.Error("expected error for empty user, got nil")
}
})
t.Run("error si password vacio", func(t *testing.T) {
_, err := BcryptHtpasswd("user", "", 4)
if err == nil {
t.Error("expected error for empty password, got nil")
}
})
t.Run("error si cost fuera de rango", func(t *testing.T) {
_, err := BcryptHtpasswd("user", "pass", 32)
if err == nil {
t.Error("expected error for cost=32, got nil")
}
_, err = BcryptHtpasswd("user", "pass", 3)
if err == nil {
t.Error("expected error for cost=3, got nil")
}
})
}