Repository navigation
Expand file tree
/
Copy pathconn_ratelimit_test.go
More file actions
81 lines (74 loc) · 1.91 KB
/
Copy pathconn_ratelimit_test.go
File metadata and controls
81 lines (74 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package zgrab2
import (
"context"
"net"
"net/netip"
"testing"
"time"
"github.com/zmap/zgrab2/ratelimit"
)
// withRateLimitConfig restores the rate limit settings after the test and
// gives it a fresh limiter cache, so limiters created by other tests can't
// leak in.
func withRateLimitConfig(t *testing.T) {
t.Helper()
oldLimit, oldDisabled := config.ServerRateLimit, serverRateLimitDisabled.Load()
oldLimiter := ipRateLimiter
t.Cleanup(func() {
config.ServerRateLimit = oldLimit
serverRateLimitDisabled.Store(oldDisabled)
ipRateLimiter = oldLimiter
})
ipRateLimiter = ratelimit.NewPerObjectRateLimiter[netip.Addr](10, time.Minute)
}
func acceptLoop(t *testing.T) string {
t.Helper()
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = l.Close() })
go func() {
for {
c, err := l.Accept()
if err != nil {
return
}
_ = c.Close()
}
}()
return l.Addr().String()
}
func dialOnce(addr string, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
conn, err := NewDialer(nil).DialContext(ctx, "tcp", addr)
if err == nil {
_ = conn.Close()
}
return err
}
func TestServerRateLimitEnforced(t *testing.T) {
withRateLimitConfig(t)
config.ServerRateLimit = 1
addr := acceptLoop(t)
if err := dialOnce(addr, time.Second); err != nil {
t.Fatalf("first dial: %v", err)
}
// The single token is spent; the next one is a second away.
if err := dialOnce(addr, 100*time.Millisecond); err == nil {
t.Fatal("second dial succeeded, want it rate limited")
}
}
func TestServerRateLimitDisabled(t *testing.T) {
withRateLimitConfig(t)
// A limit of 0 would refuse every dial; disabling must win over it.
config.ServerRateLimit = 0
DisableServerRateLimit()
addr := acceptLoop(t)
for i := 0; i < 20; i++ {
if err := dialOnce(addr, time.Second); err != nil {
t.Fatalf("dial %d: %v", i, err)
}
}
}