package intel import ( "context" "fmt" "net/netip" "strings" "testing" "time" ) // letters encodes i with letters g-z only (digits and hex runs are normalised away). func letters(i int) string { s := "x" for ; i > 0; i /= 20 { s += string(rune('g' + i%20)) } return s } func synthSessions(n int, mode string) ([]Session, map[string]Signature) { ss := make([]Session, n) sigs := make(map[string]Signature, n) for i := 0; i < n; i++ { id := fmt.Sprintf("s%d", i) ss[i] = Session{ID: id, IP: fmt.Sprintf("2001:db8:%x::%x", i%65536, i), Start: at(i), End: at(i)} var paths []string switch mode { case "distinct": // every session unique, all share "/" paths = []string{"/", "/a" + letters(i), "/b" + letters(i), "/c" + letters(i), "/d" + letters(i)} case "pool": // random-ish 5-subsets of a 40-path pool for k := 0; k < 5; k++ { paths = append(paths, "/p"+letters((i*7+k*k*13+k*i)%40)) } default: // same signature paths = []string{"/.env", "/admin", "/wp-login.php", "/.git/config", "/x"} } sigs[id] = SignatureFrom(paths, "nuclei") } return ss, sigs } func TestCampaignsScale(t *testing.T) { for _, mode := range []string{"same", "distinct", "pool"} { ss, sigs := synthSessions(50000, mode) start := time.Now() cs := Campaigns(ss, sigs, 0) el := time.Since(start) t.Logf("%s: 50000 sessions -> %d campaigns in %v", mode, len(cs), el) if el > 8*time.Second { t.Errorf("%s too slow: %v", mode, el) } if mode == "distinct" && len(cs) != 0 { t.Errorf("distinct sessions must not cluster: %d", len(cs)) } if mode == "same" && len(cs) == 0 { t.Error("same signature must form campaigns") } } } func TestCampaignsCtxPartial(t *testing.T) { ss, sigs := synthSessions(5000, "same") ctx, cancel := context.WithCancel(context.Background()) cancel() if cs := CampaignsCtx(ctx, ss, sigs, 0); len(cs) != 0 { t.Errorf("cancelled before start should give nothing, got %d", len(cs)) } ctx2, cancel2 := context.WithTimeout(context.Background(), time.Nanosecond) defer cancel2() time.Sleep(time.Millisecond) CampaignsCtx(ctx2, ss, sigs, 0) // must return, partial } func TestJaccardNoAlloc(t *testing.T) { a := []string{"/a", "/b", "/c", "/d", "/e"} b := []string{"/a", "/b", "/c", "/d", "/f"} if n := testing.AllocsPerRun(100, func() { Jaccard(a, b) }); n != 0 { t.Errorf("allocs: %v", n) } if j := Jaccard(a, b); j < 0.666 || j > 0.667 { t.Errorf("%v", j) } if Jaccard([]string{"a", "a"}, []string{"a"}) != 1 { t.Error("duplicates") } big := strings.Split("a b c d e f g h i j", " ") if Jaccard(big, big[:5]) != 0.5 { t.Error("map path") } } func TestSTIXPrefix(t *testing.T) { now := time.Date(2026, 11, 14, 0, 0, 0, 0, time.UTC) for _, in := range []string{"2001:db8::/64", "2001:db8::1234/64"} { b, err := STIXBundle(in, at(0), at(10), 3, []string{"sqli"}, nil, "", now) if err != nil || ValidateSTIX(b) != nil { t.Fatalf("%s: %v", in, err) } if !strings.Contains(string(b), "[ipv6-addr:value = '2001:db8::/64']") { t.Errorf("%s: %s", in, b) } } p4, err := STIXBundlePrefix(netip.MustParsePrefix("203.0.113.77/24"), at(0), at(1), 1, nil, nil, "", now) if err != nil || !strings.Contains(string(p4), "[ipv4-addr:value = '203.0.113.0/24']") { t.Errorf("%v %s", err, p4) } one, _ := STIXBundlePrefix(netip.MustParsePrefix("1.2.3.4/32"), at(0), at(1), 1, nil, nil, "", now) if !strings.Contains(string(one), "[ipv4-addr:value = '1.2.3.4']") { t.Error("/32 is an address") } if _, err := STIXBundlePrefix(netip.Prefix{}, at(0), at(1), 1, nil, nil, "", now); err == nil { t.Error("invalid prefix") } for _, bad := range []string{"1.2.3.4/99", "x/24", "/", "1.2.3.4/"} { if _, err := STIXBundle(bad, at(0), at(1), 1, nil, nil, "", now); err == nil { t.Errorf("accepted %q", bad) } } } func BenchmarkCampaigns50k(b *testing.B) { ss, sigs := synthSessions(50000, "same") b.ResetTimer() for i := 0; i < b.N; i++ { Campaigns(ss, sigs, 0) } } func BenchmarkJaccard(b *testing.B) { x := []string{"/a", "/b", "/c", "/d", "/e"} y := []string{"/a", "/b", "/c", "/d", "/f"} b.ReportAllocs() for i := 0; i < b.N; i++ { Jaccard(x, y) } }