Files
urutau/internal/app/token_test.go
T
Lucas RayanandClaude Opus 5.5 b4a2c2c9ec
Release / Plan (reuse the published image?) (push) Successful in 4s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 1m29s
CI / Tests (Linux, race detector) (push) Failing after 1m48s
CI / Dependency audit (push) Successful in 21s
Release / ci (push) Failing after 1m50s
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Skipped
Release / Gitea release (push) Skipped
feat: Urutau 1.0.0, lightweight threat intelligence and web analytics
Always-on Go service (stdlib only) that follows the Traefik JSON access
log, the sshd log and the fail2ban log of a host, classifies attacks,
scores sources, keeps bounded aggregates with snapshots, and serves an
embedded dashboard with live updates, offline geolocation (DB-IP Lite),
privacy-friendly traffic analytics, an exportable blocklist and
token-authenticated federation between instances.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 16:13:34 -03:00

53 lines
1.3 KiB
Go

package app
import (
"crypto/rand"
"encoding/base64"
"encoding/hex"
"strings"
"testing"
)
// Every token produced by the documented generators must be accepted:
// a quality rule that rejects random output breaks deploys (crash loop).
func TestCheckTokenAcceptsRandomTokens(t *testing.T) {
gens := map[string]func() string{
"hex32": func() string { return hex.EncodeToString(randBytes(32)) },
"hex24": func() string { return hex.EncodeToString(randBytes(24)) },
"b64-32": func() string { return base64.StdEncoding.EncodeToString(randBytes(32)) },
"b64-48": func() string { return base64.StdEncoding.EncodeToString(randBytes(48)) },
}
for name, gen := range gens {
for i := 0; i < 20000; i++ {
tok := gen()
if err := checkToken("T", tok); err != nil {
t.Fatalf("%s token %q rejected: %v", name, tok, err)
}
}
}
}
func TestCheckTokenRejectsWeakTokens(t *testing.T) {
for _, tok := range []string{
strings.Repeat("a", 40),
strings.Repeat("abcdefghijklmnop", 2),
strings.Repeat("ab", 20),
strings.Repeat("0123456789", 4),
"abcdefghijklmnopqrstuvwxyzABCDEF",
"zyxwvutsrqponmlkjihgfedcbaZYXWVU",
"short",
} {
if err := checkToken("T", tok); err == nil {
t.Errorf("weak token %q accepted", tok)
}
}
}
func randBytes(n int) []byte {
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
panic(err)
}
return b
}