forked from jpillora/ipfilter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipfilter_test.go
More file actions
78 lines (70 loc) · 2.03 KB
/
ipfilter_test.go
File metadata and controls
78 lines (70 loc) · 2.03 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
package ipfilter
import (
"net"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSingleIP(t *testing.T) {
f, err := New(Options{
AllowedIPs: []string{"222.25.118.1"},
BlockByDefault: true,
})
if err != nil {
t.Fatal(err)
}
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, err := New(Options{
AllowedIPs: []string{"10.0.0.0/16"},
BlockByDefault: true,
})
if err != nil {
t.Fatal(err)
}
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")
}
func TestManualCountryCode(t *testing.T) {
f, err := New(Options{})
if err != nil {
t.Fatal(err)
}
assert.Equal(t, f.IPToCountry("203.25.111.68"), "AU")
assert.Equal(t, f.IPToCountry("216.58.199.67"), "US")
}
func TestCountryCodeWhiteList(t *testing.T) {
f, err := New(Options{
AllowedCountries: []string{"AU"},
BlockByDefault: true,
})
if err != nil {
t.Fatal(err)
}
assert.True(t, f.Allowed("203.25.111.68"), "[1] should be allowed")
assert.True(t, f.Blocked("216.58.199.67"), "[2] should be blocked")
}
func TestCountryCodeBlackList(t *testing.T) {
f, err := New(Options{
BlockedCountries: []string{"RU", "CN"},
})
if err != nil {
t.Fatal(err)
}
assert.True(t, f.Allowed("203.25.111.68"), "[1] AU should be allowed")
assert.True(t, f.Allowed("216.58.199.67"), "[2] US should be allowed")
assert.True(t, f.Blocked("116.31.116.51"), "[3] CN should be blocked")
}
func TestDynamicList(t *testing.T) {
f, err := New(Options{})
if err != nil {
t.Fatal(err)
}
assert.True(t, f.Allowed("116.31.116.51"), "[1] CN should be allowed")
f.BlockCountry("CN")
assert.True(t, f.Blocked("116.31.116.51"), "[1] CN should be blocked")
}