瀏覽代碼

add tests for masking in limiter/throttler

tags/v1.0.0-rc1
Shivaram Lingamneni 5 年之前
父節點
當前提交
e094c2a9c5
共有 1 個檔案被更改,包括 38 行新增4 行删除
  1. 38
    4
      irc/connection_limits/throttler_test.go

+ 38
- 4
irc/connection_limits/throttler_test.go 查看文件

@@ -62,25 +62,59 @@ func TestGenericThrottleDisabled(t *testing.T) {
62 62
 	}
63 63
 }
64 64
 
65
-func TestConnectionThrottle(t *testing.T) {
65
+func makeTestThrottler(v4len, v6len int) *Throttler {
66 66
 	minute, _ := time.ParseDuration("1m")
67 67
 	maxConnections := 3
68 68
 	config := ThrottlerConfig{
69 69
 		Enabled:            true,
70
-		CidrLenIPv4:        32,
71
-		CidrLenIPv6:        64,
70
+		CidrLenIPv4:        v4len,
71
+		CidrLenIPv6:        v6len,
72 72
 		ConnectionsPerCidr: maxConnections,
73 73
 		Duration:           minute,
74 74
 	}
75 75
 	throttler := NewThrottler()
76 76
 	throttler.ApplyConfig(config)
77
+	return throttler
78
+}
77 79
 
80
+func TestConnectionThrottle(t *testing.T) {
81
+	throttler := makeTestThrottler(32, 64)
78 82
 	addr := net.ParseIP("8.8.8.8")
79 83
 
80
-	for i := 0; i < maxConnections; i += 1 {
84
+	for i := 0; i < 3; i += 1 {
81 85
 		err := throttler.AddClient(addr)
82 86
 		assertEqual(err, nil, t)
83 87
 	}
84 88
 	err := throttler.AddClient(addr)
85 89
 	assertEqual(err, errTooManyClients, t)
86 90
 }
91
+
92
+func TestConnectionThrottleIPv6(t *testing.T) {
93
+	throttler := makeTestThrottler(32, 64)
94
+
95
+	var err error
96
+	err = throttler.AddClient(net.ParseIP("2001:0db8::1"))
97
+	assertEqual(err, nil, t)
98
+	err = throttler.AddClient(net.ParseIP("2001:0db8::2"))
99
+	assertEqual(err, nil, t)
100
+	err = throttler.AddClient(net.ParseIP("2001:0db8::3"))
101
+	assertEqual(err, nil, t)
102
+
103
+	err = throttler.AddClient(net.ParseIP("2001:0db8::4"))
104
+	assertEqual(err, errTooManyClients, t)
105
+}
106
+
107
+func TestConnectionThrottleIPv4(t *testing.T) {
108
+	throttler := makeTestThrottler(24, 64)
109
+
110
+	var err error
111
+	err = throttler.AddClient(net.ParseIP("192.168.1.101"))
112
+	assertEqual(err, nil, t)
113
+	err = throttler.AddClient(net.ParseIP("192.168.1.102"))
114
+	assertEqual(err, nil, t)
115
+	err = throttler.AddClient(net.ParseIP("192.168.1.103"))
116
+	assertEqual(err, nil, t)
117
+
118
+	err = throttler.AddClient(net.ParseIP("192.168.1.104"))
119
+	assertEqual(err, errTooManyClients, t)
120
+}

Loading…
取消
儲存