mirror of
https://github.com/ergochat/ergo.git
synced 2024-12-22 18:52:41 +01:00
add tests for masking in limiter/throttler
This commit is contained in:
parent
eb8f0e50df
commit
e094c2a9c5
@ -62,25 +62,59 @@ func TestGenericThrottleDisabled(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConnectionThrottle(t *testing.T) {
|
func makeTestThrottler(v4len, v6len int) *Throttler {
|
||||||
minute, _ := time.ParseDuration("1m")
|
minute, _ := time.ParseDuration("1m")
|
||||||
maxConnections := 3
|
maxConnections := 3
|
||||||
config := ThrottlerConfig{
|
config := ThrottlerConfig{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
CidrLenIPv4: 32,
|
CidrLenIPv4: v4len,
|
||||||
CidrLenIPv6: 64,
|
CidrLenIPv6: v6len,
|
||||||
ConnectionsPerCidr: maxConnections,
|
ConnectionsPerCidr: maxConnections,
|
||||||
Duration: minute,
|
Duration: minute,
|
||||||
}
|
}
|
||||||
throttler := NewThrottler()
|
throttler := NewThrottler()
|
||||||
throttler.ApplyConfig(config)
|
throttler.ApplyConfig(config)
|
||||||
|
return throttler
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConnectionThrottle(t *testing.T) {
|
||||||
|
throttler := makeTestThrottler(32, 64)
|
||||||
addr := net.ParseIP("8.8.8.8")
|
addr := net.ParseIP("8.8.8.8")
|
||||||
|
|
||||||
for i := 0; i < maxConnections; i += 1 {
|
for i := 0; i < 3; i += 1 {
|
||||||
err := throttler.AddClient(addr)
|
err := throttler.AddClient(addr)
|
||||||
assertEqual(err, nil, t)
|
assertEqual(err, nil, t)
|
||||||
}
|
}
|
||||||
err := throttler.AddClient(addr)
|
err := throttler.AddClient(addr)
|
||||||
assertEqual(err, errTooManyClients, t)
|
assertEqual(err, errTooManyClients, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConnectionThrottleIPv6(t *testing.T) {
|
||||||
|
throttler := makeTestThrottler(32, 64)
|
||||||
|
|
||||||
|
var err error
|
||||||
|
err = throttler.AddClient(net.ParseIP("2001:0db8::1"))
|
||||||
|
assertEqual(err, nil, t)
|
||||||
|
err = throttler.AddClient(net.ParseIP("2001:0db8::2"))
|
||||||
|
assertEqual(err, nil, t)
|
||||||
|
err = throttler.AddClient(net.ParseIP("2001:0db8::3"))
|
||||||
|
assertEqual(err, nil, t)
|
||||||
|
|
||||||
|
err = throttler.AddClient(net.ParseIP("2001:0db8::4"))
|
||||||
|
assertEqual(err, errTooManyClients, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConnectionThrottleIPv4(t *testing.T) {
|
||||||
|
throttler := makeTestThrottler(24, 64)
|
||||||
|
|
||||||
|
var err error
|
||||||
|
err = throttler.AddClient(net.ParseIP("192.168.1.101"))
|
||||||
|
assertEqual(err, nil, t)
|
||||||
|
err = throttler.AddClient(net.ParseIP("192.168.1.102"))
|
||||||
|
assertEqual(err, nil, t)
|
||||||
|
err = throttler.AddClient(net.ParseIP("192.168.1.103"))
|
||||||
|
assertEqual(err, nil, t)
|
||||||
|
|
||||||
|
err = throttler.AddClient(net.ParseIP("192.168.1.104"))
|
||||||
|
assertEqual(err, errTooManyClients, t)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user