Files
OwnCord/Server/auth/session_test.go
T
jevb ce4326766a fix: resolve all golangci-lint issues blocking CI server build
Fix 70+ errcheck violations by adding explicit error discards (`_ =`)
for unchecked return values across test helpers and deferred Close()
calls. Remove unused `senderID` field from broadcastMsg and unused
`defaultCleanupMaxWindow` const. Apply De Morgan's law, remove empty
branch, and simplify redundant type declaration per staticcheck.
2026-03-17 08:09:52 +01:00

78 lines
1.8 KiB
Go

package auth_test
import (
"testing"
"github.com/owncord/server/auth"
)
func TestGenerateToken_Length(t *testing.T) {
token, err := auth.GenerateToken()
if err != nil {
t.Fatalf("GenerateToken() error = %v", err)
}
if len(token) != 64 {
t.Errorf("GenerateToken() len = %d, want 64", len(token))
}
}
func TestGenerateToken_HexCharacters(t *testing.T) {
token, err := auth.GenerateToken()
if err != nil {
t.Fatalf("GenerateToken() error = %v", err)
}
for i, c := range token {
if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
t.Errorf("GenerateToken() char[%d] = %q, not lowercase hex", i, c)
}
}
}
func TestGenerateToken_Uniqueness(t *testing.T) {
const n = 1000
seen := make(map[string]struct{}, n)
for i := range n {
tok, err := auth.GenerateToken()
if err != nil {
t.Fatalf("GenerateToken() iteration %d error = %v", i, err)
}
if _, dup := seen[tok]; dup {
t.Fatalf("GenerateToken() produced duplicate token at iteration %d", i)
}
seen[tok] = struct{}{}
}
}
func TestHashToken_Deterministic(t *testing.T) {
token := "abc123"
h1 := auth.HashToken(token)
h2 := auth.HashToken(token)
if h1 != h2 {
t.Errorf("HashToken() not deterministic: %q != %q", h1, h2)
}
}
func TestHashToken_DiffersFromPlaintext(t *testing.T) {
token := "abc123"
hash := auth.HashToken(token)
if hash == token {
t.Errorf("HashToken() hash equals plaintext token")
}
}
func TestHashToken_Length(t *testing.T) {
// SHA-256 hex = 64 chars
hash := auth.HashToken("any-token")
if len(hash) != 64 {
t.Errorf("HashToken() len = %d, want 64", len(hash))
}
}
func TestHashToken_DifferentInputsDifferentHashes(t *testing.T) {
h1 := auth.HashToken("token-one")
h2 := auth.HashToken("token-two")
if h1 == h2 {
t.Errorf("HashToken() same hash for different inputs")
}
}