|
| 1 | +package abuse |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "sync" |
| 8 | + "sync/atomic" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// TestConfigureTogglesCategories: a disabled category must stop matching, and match |
| 14 | +// again once re-enabled — the settings toggle is only real if the live matcher |
| 15 | +// follows it. |
| 16 | +func TestConfigureTogglesCategories(t *testing.T) { |
| 17 | + s := NewStore(t.TempDir()) |
| 18 | + s.Matcher().SetIP(CatBadIP, []string{"203.0.113.0/24"}) |
| 19 | + |
| 20 | + // nil config == all on. |
| 21 | + if _, ok := s.Matcher().Match("203.0.113.5"); !ok { |
| 22 | + t.Fatal("baseline match failed") |
| 23 | + } |
| 24 | + |
| 25 | + // Disable the feed: it stops matching, the custom list still works. |
| 26 | + s.Configure(map[Category]bool{CatCustom: true}, "198.51.100.0/24") |
| 27 | + if cat, ok := s.Matcher().Match("203.0.113.5"); ok { |
| 28 | + t.Fatalf("disabled feed still matched as %q", cat) |
| 29 | + } |
| 30 | + if cat, ok := s.Matcher().Match("198.51.100.5"); !ok || cat != CatCustom { |
| 31 | + t.Fatalf("custom list should still match: %q,%v", cat, ok) |
| 32 | + } |
| 33 | + |
| 34 | + // Master off (nothing enabled): nothing matches. |
| 35 | + s.Configure(map[Category]bool{}, "198.51.100.0/24") |
| 36 | + if _, ok := s.Matcher().Match("198.51.100.5"); ok { |
| 37 | + t.Fatal("master-off still matched") |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// TestConfigureCustomList: the operator's list is parsed and takes priority, and |
| 42 | +// clears when emptied. |
| 43 | +func TestConfigureCustomList(t *testing.T) { |
| 44 | + s := NewStore(t.TempDir()) |
| 45 | + s.Configure(nil, "198.51.100.0/24\n2001:db8::1\n# comment\n\n") |
| 46 | + |
| 47 | + if cat, ok := s.Matcher().Match("198.51.100.7"); !ok || cat != CatCustom { |
| 48 | + t.Fatalf("custom CIDR: got %q,%v", cat, ok) |
| 49 | + } |
| 50 | + if cat, ok := s.Matcher().Match("2001:db8::1"); !ok || cat != CatCustom { |
| 51 | + t.Fatalf("custom v6: got %q,%v", cat, ok) |
| 52 | + } |
| 53 | + if _, ok := s.Matcher().Match("198.51.101.7"); ok { |
| 54 | + t.Fatal("address outside the custom CIDR matched") |
| 55 | + } |
| 56 | + |
| 57 | + s.Configure(nil, "") |
| 58 | + if _, ok := s.Matcher().Match("198.51.100.7"); ok { |
| 59 | + t.Fatal("custom entry survived an empty list") |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// TestConfigureDisablingCustom: custom off must stop matching even with content. |
| 64 | +func TestConfigureDisablingCustom(t *testing.T) { |
| 65 | + s := NewStore(t.TempDir()) |
| 66 | + s.Configure(map[Category]bool{CatCustom: false}, "198.51.100.0/24") |
| 67 | + if _, ok := s.Matcher().Match("198.51.100.7"); ok { |
| 68 | + t.Fatal("custom matched while disabled") |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// TestParseCustom: addresses and CIDRs survive; comments, blanks and anything that |
| 73 | +// is not an address are skipped rather than poisoning the list. |
| 74 | +func TestParseCustom(t *testing.T) { |
| 75 | + got := ParseCustom("198.51.100.0/24\n# skip\n\nevil.example\n2001:db8::1\n203.0.113.7 trailing comment\nnot an address") |
| 76 | + want := []string{"198.51.100.0/24", "2001:db8::1", "203.0.113.7"} |
| 77 | + if len(got) != len(want) { |
| 78 | + t.Fatalf("got %v, want %v", got, want) |
| 79 | + } |
| 80 | + for i := range want { |
| 81 | + if got[i] != want[i] { |
| 82 | + t.Fatalf("got %v, want %v", got, want) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// TestRefreshIsSingleFlight: the operator's "refresh now" button spawns a goroutine |
| 88 | +// per click and the route has no rate limit, so overlapping passes must be dropped. |
| 89 | +// Two concurrent passes would fight over the temp files — sweepTempFiles removes |
| 90 | +// every .dl-*, including the other pass's in-flight download. |
| 91 | +func TestRefreshIsSingleFlight(t *testing.T) { |
| 92 | + s := NewStore(t.TempDir()) |
| 93 | + // Point the feed at a server that blocks until we let it go, so the first pass is |
| 94 | + // still running while the second one arrives. |
| 95 | + release := make(chan struct{}) |
| 96 | + var hits atomic.Int32 |
| 97 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 98 | + hits.Add(1) |
| 99 | + <-release |
| 100 | + _, _ = w.Write([]byte("203.0.113.0/24\n")) |
| 101 | + })) |
| 102 | + defer srv.Close() |
| 103 | + |
| 104 | + orig := Feeds |
| 105 | + Feeds = []Feed{{Category: CatBadIP, URLs: []string{srv.URL}}} |
| 106 | + defer func() { Feeds = orig }() |
| 107 | + |
| 108 | + var wg sync.WaitGroup |
| 109 | + wg.Add(1) |
| 110 | + go func() { defer wg.Done(); s.Refresh(context.Background(), true) }() |
| 111 | + |
| 112 | + // Wait until the first pass is actually inside the download. |
| 113 | + for range 100 { |
| 114 | + if hits.Load() == 1 { |
| 115 | + break |
| 116 | + } |
| 117 | + time.Sleep(10 * time.Millisecond) |
| 118 | + } |
| 119 | + |
| 120 | + // A second pass while the first is in flight must return immediately, not fetch. |
| 121 | + done := make(chan struct{}) |
| 122 | + go func() { s.Refresh(context.Background(), true); close(done) }() |
| 123 | + select { |
| 124 | + case <-done: |
| 125 | + case <-time.After(2 * time.Second): |
| 126 | + t.Fatal("overlapping Refresh blocked instead of being dropped") |
| 127 | + } |
| 128 | + if n := hits.Load(); n != 1 { |
| 129 | + t.Fatalf("overlapping Refresh fetched too: %d requests, want 1", n) |
| 130 | + } |
| 131 | + |
| 132 | + close(release) |
| 133 | + wg.Wait() |
| 134 | + |
| 135 | + // And once the first finished, a later refresh is allowed again. |
| 136 | + s.Refresh(context.Background(), true) |
| 137 | + if n := hits.Load(); n != 2 { |
| 138 | + t.Fatalf("refresh after completion did not run: %d requests, want 2", n) |
| 139 | + } |
| 140 | +} |
0 commit comments