package server
import (
"os"
"strings"
"testing"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/egress"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/enforce"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/intel"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
)
func richReportInput(lang string) reportInput {
var in reportInput
in.Lang = lang
o := &in.Out
o.Range = store.RangeInfo{Name: "7d", From: 1763000000, To: 1763604800, Step: 3600}
o.Hosts = []string{"web-01.example.com", "web-02.example.com"}
o.Summary.reportFigures = reportFigures{Requests: 1843210, Attacks: 48211, UniqueIPs: 912, HostileIPs: 143, Bans: 97, Visitors: 5120, NonHuman: 733, PageViews: 18430}
o.Summary.Prev = reportFigures{Requests: 1500000, Attacks: 30100, UniqueIPs: 700, HostileIPs: 110, Bans: 90, Visitors: 4800, PageViews: 17000}
o.Summary.HasPrev = true
o.Summary.DeltaPct = map[string]float64{"requests": 22.9, "attacks": 60.2, "hostile_ips": 30, "bans": 7.7, "visitors": 6.7}
o.Timeline.Start, o.Timeline.Step = o.Range.From, 3600
for i := 0; i < 168; i++ {
o.Timeline.Values = append(o.Timeline.Values, uint32(200+(i*37)%400+(i%24)*10))
}
o.Families = []store.Count{{Key: "scanner", N: 21000}, {Key: "exploit", N: 14000}, {Key: "bruteforce", N: 8000}, {Key: "ssh-fail", N: 4000}, {Key: "ssh-probe", N: 1211}}
o.Techniques = []store.Count{{Key: "T1190", N: 9000, Label: "Exploit Public-Facing Application"}, {Key: "T1110", N: 4000, Label: "Brute Force"}, {Key: "T1595", N: 3000, Label: "Active Scanning"}}
o.Countries = []store.Count{{Key: "NL", N: 9000}, {Key: "US", N: 8000}, {Key: "DE", N: 5000}, {Key: "RU", N: 3000}, {Key: "BR", N: 900}}
o.ASNs = []store.Count{{Key: "AS64496", N: 9000, Label: "Example Hosting Ltd"}, {Key: "AS64497", N: 5000, Label: "Documentation Net"}}
for i := 0; i < 10; i++ {
o.TopIPs = append(o.TopIPs, reportTopIP{IP: "198.51.100." + string(rune('1'+i)), Score: 92.5 - float64(i), Level: "hostile", CC: "NL", Country: "Netherlands", ASOrg: "Example Hosting", Events: uint32(4000 - i*300), Banned: i%2 == 0})
}
o.Campaigns = []intel.Campaign{{ID: "camp-1", IPsN: 12, First: time.Unix(1763100000, 0), Last: time.Unix(1763500000, 0), Techs: []string{"T1190", "T1595"}}}
o.Escalations = []store.EscalationOut{{IP: "203.0.113.7", Host: "web-01.example.com", Stage: "exploit"}}
o.Exposures.Total = 3
for i := 0; i < 3; i++ {
o.Exposures.Items = append(o.Exposures.Items, reportExposure{T: 1763200000 + int64(i)*600, IP: "203.0.113.7", Domain: "app.example.com", Path: "/?x=" + strings.Repeat("a", 120), Status: 200, Fam: "exploit"})
}
o.Defense.MTTBN, o.Defense.MTTBMedianS, o.Defense.MTTBP90S, o.Defense.BanCoverage = 40, 95, 700, 67.8
o.Defense.BlocklistCover, o.Defense.ExposureRate, o.Defense.ExploitsRefused = 31, 0.4, 99
in.Traffic = store.TrafficData{Visitors: 5120, Pages: 18430, NonHuman: 733, P50: 42, P95: 380, Status: [4]uint64{1500000, 200000, 140000, 3210},
TopPages: []store.Count{{Key: "/", N: 9000}, {Key: "/docs", N: 3000}}, Browsers: []store.Count{{Key: "Firefox", N: 3000}, {Key: "Chrome", N: 2000}},
Devices: []store.Count{{Key: "Desktop", N: 4000}, {Key: "Mobile", N: 1100}}, Referrers: []store.Count{{Key: "example.org", N: 400}}}
in.HasHealth = true
in.Classes = [store.NumCls]uint32{store.ClsApp: 120, store.ClsProxy: 12, store.ClsBlocked: 30000, store.ClsAttack: 9000, store.ClsBot: 4000, store.ClsClientBroken: 900, store.ClsClientAuth: 300, store.ClsClientOther: 50}
in.Enforce = &enforceView{Mode: enforce.ModeOn, Coverage: enforce.Coverage{AttackIPs: 143, BlockedByFirewall: 80, BlockedByUrutau: 20, Unblocked: 43},
Counts: enforce.Counts{Decided: 400, Written: 380, AlreadyBlocked: 20},
Recent: []enforce.Recent{{T: 1763300000, IP: "198.51.100.4", Reason: "exploit x12", Action: "ban", CC: "NL"}}}
in.Egress = &egressView{Source: egress.SourceInfo{State: "ok"}, Report: egress.Report{
Workloads: []egress.WorkloadOut{{Name: "app-1", Role: "web", Conns: 12000, Dsts: 14}, {Name: "db-1", Role: "db", Conns: 800, Dsts: 2}},
TopDst: []egress.DstOut{{IP: "192.0.2.10", CC: "US", Conns: 5000}},
Findings: []egress.Finding{{Severity: "high", Kind: "beacon", Workload: "app-1", Dst: "192.0.2.99", DPort: 4444, Proto: "tcp", Detail: "
periodic"}}}}
return in
}
func TestReportHTMLRenders(t *testing.T) {
for _, lang := range []string{"pt-BR", "en"} {
b, err := renderReportHTML(richReportInput(lang), 1763604800)
if err != nil {
t.Fatal(err)
}
s := string(b)
if strings.Contains(s, "