package store import ( "bytes" "encoding/json" "os" "path/filepath" "testing" "time" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core" ) func visit(ip, dom, path, ref string, status int, ago time.Duration) core.Event { return core.Event{Time: t0.Add(-ago), IP: ip, Cat: core.OK, Source: "traefik", Domain: dom, Method: "GET", Path: path, UA: "Mozilla/5.0 test", Status: status, DurationNS: 40e6, Bytes: 1000, Referer: ref, E: core.Enrich{CC: "BR", City: "Recife", Browser: "Firefox", OS: "Linux", Device: "desktop"}} } func TestTrafficAggregatesAndPrivacy(t *testing.T) { s := newStore("oracle", 100) for i := 0; i < 5; i++ { s.Traffic(visit("200.100.50.25", "a.example", "/blog", "https://www.google.com/search?q=x", 200, time.Duration(i)*time.Minute)) } s.Traffic(visit("200.100.50.26", "a.example", "/", "https://a.example/blog", 200, time.Minute)) s.Traffic(visit("200.100.50.26", "a.example", "/style.css", "", 200, time.Minute)) s.Traffic(visit("200.100.50.27", "b.example", "/s.css", "", 200, time.Minute)) s.Traffic(visit("200.100.50.27", "b.example", "/x", "", 404, time.Minute)) bot := visit("66.249.66.1", "a.example", "/", "", 200, time.Minute) bot.E = core.Enrich{Bot: true, BotName: "Googlebot", Device: "bot"} s.Traffic(bot) all := s.TrafficSummary("24h", "all") if all.Requests != 10 || all.Pages != 6 || all.Visitors != 2 || all.Bots != 1 { t.Fatalf("all: %+v", all) } if all.Status[0] != 9 || all.Status[2] != 1 || all.Bytes != 10000 { t.Fatalf("status/bytes %+v %d", all.Status, all.Bytes) } if all.P50 < 25 || all.P50 > 50 || all.P95 < 25 || all.P95 > 50 { t.Fatalf("latency p50=%v p95=%v", all.P50, all.P95) } if len(all.Domains) != 2 || all.Domains[0].Key != "a.example" { t.Fatalf("domains %+v", all.Domains) } if len(all.Referrers) != 1 || all.Referrers[0].Key != "google.com" || all.Referrers[0].N != 5 { t.Fatalf("referrers (self referrals are not external): %+v", all.Referrers) } if all.Countries[0].Key != "BR" || all.Cities[0].Key != "Recife, BR" || all.Browsers[0].Key != "Firefox" || all.BotNames[0].Key != "Googlebot" { t.Fatalf("breakdowns %+v", all) } for _, p := range all.TopPages { if p.Key == "/style.css" { t.Fatal("assets are not pages") } } a := s.TrafficSummary("24h", "a.example") if a.Requests != 8 || a.Visitors != 2 { t.Fatalf("per domain %+v", a) } var tl uint32 for _, v := range a.Timeline.Req { tl += v } if tl != 8 { t.Fatalf("timeline %d", tl) } if w := s.TrafficSummary("7d", "a.example"); w.Requests != 8 || w.Visitors != 2 { t.Fatalf("7d %+v", w) } // privacy: the visitor IPs reach neither the snapshot nor any API payload p := filepath.Join(t.TempDir(), "state.gob") if err := s.Save(p, nil); err != nil { t.Fatal(err) } raw, _ := os.ReadFile(p) api, _ := json.Marshal([]any{all, a, s.Summary("24h"), s.AllIPs(ParseRange("7d"), true)}) for _, ip := range []string{"200.100.50.25", "200.100.50.26", "200.100.50.27", "66.249.66.1"} { if bytes.Contains(raw, []byte(ip)) || bytes.Contains(api, []byte(ip)) { t.Fatalf("visitor IP %s leaked", ip) } if _, ok := s.IP(ip); ok { t.Fatalf("visitor IP %s is in the threat table", ip) } } if bytes.Contains(raw, s.tr.salt[:]) { t.Fatal("the daily salt must never reach the disk") } if s.tr.saltDay == 0 { t.Fatal("test precondition: a salt was generated") } // the snapshot restores the aggregates s2 := newStore("oracle", 100) if _, err := s2.Load(p); err != nil { t.Fatal(err) } if r := s2.TrafficSummary("24h", "all"); r.Requests != 10 || r.Visitors != 2 { t.Fatalf("restored %+v", r) } } func TestMergeTrafficAndDomainCap(t *testing.T) { a, b := newStore("oracle", 10), newStore("hostinger", 10) a.Traffic(visit("1.1.1.1", "a.example", "/", "", 200, time.Minute)) b.Traffic(visit("2.2.2.2", "a.example", "/", "", 200, time.Minute)) b.Traffic(visit("2.2.2.3", "c.example", "/", "", 200, time.Minute)) m := MergeTraffic("all", []TrafficData{a.TrafficSummary("1h", "all"), b.TrafficSummary("1h", "all")}) if m.Requests != 3 || m.Visitors != 3 || len(m.Domains) != 2 || m.Domains[0].N != 2 || m.Countries[0].N != 3 { t.Fatalf("%+v", m) } c := newStore("x", 10) for i := 0; i < 60; i++ { c.Traffic(visit("1.1.1.1", "d"+string(rune('a'+i%26))+string(rune('a'+i/26))+".example", "/", "", 200, time.Minute)) } if len(c.tr.dom) > maxDomains+1 { t.Fatalf("domain cap: %d", len(c.tr.dom)) } } func TestToolBonusAndCountries(t *testing.T) { s := newStore("x", 100) e := ev("9.9.9.9", core.Scanner, time.Minute) e.E = core.Enrich{CC: "RU", Tool: true} s.Ingest(e) n := ev("9.9.9.8", core.NotFound, time.Minute) s.Ingest(n) if _, ok := s.IP("9.9.9.8"); ok { t.Fatal("404 without tool must not be tracked") } n.E.Tool = true n.IP = "9.9.9.7" s.Ingest(n) if r, ok := s.IP("9.9.9.9"); !ok || r.Score != 5 { t.Fatalf("tool bonus: %+v", r) } if r, ok := s.IP("9.9.9.7"); !ok || r.Score != 2.2 { t.Fatalf("tool 404: %+v", r) } if c := s.Summary("1h").Countries; len(c) != 1 || c[0].Key != "RU" || c[0].N != 1 { t.Fatalf("countries %+v", c) } }