forked from jpillora/ipfilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipfilter_test.go
30 lines (26 loc) · 904 Bytes
/
ipfilter_test.go
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
package ipfilter_test
import (
"net"
"testing"
"github.com/jpillora/ipfilter"
"github.com/stretchr/testify/assert"
)
func TestSingleIP(t *testing.T) {
f := ipfilter.New(ipfilter.Options{
AllowedIPs: []string{"222.25.118.1"},
BlockByDefault: true,
})
assert.True(t, f.Allowed("222.25.118.1"), "[1] should be allowed")
assert.True(t, f.Blocked("222.25.118.2"), "[2] should be blocked")
assert.True(t, f.NetAllowed(net.IP{222, 25, 118, 1}), "[3] should be allowed")
assert.True(t, f.NetBlocked(net.IP{222, 25, 118, 2}), "[4] should be blocked")
}
func TestSubnetIP(t *testing.T) {
f := ipfilter.New(ipfilter.Options{
AllowedIPs: []string{"10.0.0.0/16"},
BlockByDefault: true,
})
assert.True(t, f.Allowed("10.0.0.1"), "[1] should be allowed")
assert.True(t, f.Allowed("10.0.42.1"), "[2] should be allowed")
assert.True(t, f.Blocked("10.42.0.1"), "[3] should be blocked")
}