-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip_filter_test.go
More file actions
145 lines (131 loc) · 3.37 KB
/
ip_filter_test.go
File metadata and controls
145 lines (131 loc) · 3.37 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package ipfilter_test
import (
"fmt"
"net"
"testing"
"github.com/Bplotka/go-ipfilter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
trueCond = func(_ net.IP) bool { return true }
falseCond = func(_ net.IP) bool { return false }
)
func TestOR(t *testing.T) {
someIP, err := ipfilter.ParseIP("81.11.43.33")
require.NoError(t, err)
assert.False(t, ipfilter.OR()(someIP), "empty OR should return false.")
assert.False(t, ipfilter.OR(falseCond)(someIP), "false == false")
assert.True(t, ipfilter.OR(trueCond)(someIP), "true == true")
assert.True(t, ipfilter.OR(trueCond, trueCond, trueCond)(someIP), "true OR true OR true == true")
assert.False(t, ipfilter.OR(falseCond, falseCond, falseCond)(someIP), "false OR false OR false == false")
assert.True(t, ipfilter.OR(falseCond, trueCond, falseCond)(someIP), "false OR true OR false == true")
}
func TestAND(t *testing.T) {
someIP, err := ipfilter.ParseIP("81.11.43.33")
require.NoError(t, err)
assert.False(t, ipfilter.AND()(someIP), "empty AND should return false.")
assert.False(t, ipfilter.AND(falseCond)(someIP), "false == false")
assert.True(t, ipfilter.AND(trueCond)(someIP), "true == true")
assert.True(t, ipfilter.AND(trueCond, trueCond, trueCond)(someIP), "true OR true OR true == true")
assert.False(t, ipfilter.AND(falseCond, falseCond, falseCond)(someIP), "false OR false OR false == false")
assert.False(t, ipfilter.AND(falseCond, trueCond, falseCond)(someIP), "false OR true OR false == false")
}
func TestIsWhitelisted(t *testing.T) {
ip, err := ipfilter.ParseIP("81.11.43.33")
require.NoError(t, err)
cond := ipfilter.IsWhitelisted([]net.IPNet{ipfilter.SingleIPNet(ip)})
for _, spec := range []struct {
remote string
expected bool
}{
{
remote: "127.0.2.1:41241",
expected: false,
},
{
remote: "10.2.1.4:9231",
expected: false,
},
{
remote: "11.241.54.24:4124",
expected: false,
},
{
remote: "[::1]:23124",
expected: false,
},
{
remote: "172.31.23.41:9231",
expected: false,
},
{
remote: "192.168.23.14:23123",
expected: false,
},
{
remote: "81.11.43.33:23123",
expected: true,
},
{
remote: "81.11.43.33",
expected: true,
},
{
remote: "81.11.43.34:23123",
expected: false,
},
} {
ip, err := ipfilter.ParseIP(spec.remote)
require.NoError(t, err, fmt.Sprintf("%v should be parsable", spec.remote))
assert.Equal(t, spec.expected, cond(ip), fmt.Sprintf("Should work for %v", spec.remote))
}
}
func TestIsPrivate(t *testing.T) {
cond := ipfilter.IsPrivate()
for _, spec := range []struct {
remote string
expected bool
}{
{
remote: "127.0.2.1:41241",
expected: true,
},
{
remote: "10.2.1.4:9231",
expected: true,
},
{
remote: "11.241.54.32:4124",
expected: false,
},
{
remote: "[::1]:23124",
expected: true,
},
{
remote: "172.31.23.41:9231",
expected: true,
},
{
remote: "192.168.23.14:23123",
expected: true,
},
{
remote: "81.11.43.33:23123",
expected: false,
},
{
remote: "81.11.43.33",
expected: false,
},
{
remote: "81.11.43.34:23123",
expected: false,
},
} {
ip, err := ipfilter.ParseIP(spec.remote)
require.NoError(t, err, fmt.Sprintf("%v should be parsable", spec.remote))
assert.Equal(t, spec.expected, cond(ip), fmt.Sprintf("Should work for %v", spec.remote))
}
}