-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathIPMatcher.test.ts
More file actions
206 lines (189 loc) · 6.89 KB
/
IPMatcher.test.ts
File metadata and controls
206 lines (189 loc) · 6.89 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import * as t from "tap";
import { IPMatcher } from "./IPMatcher";
t.test("check with single Ipv4s", async (t) => {
const input = [
"192.168.0.0/32",
"192.168.0.3/32",
"192.168.0.24/32",
"192.168.0.52/32",
"192.168.0.123/32",
"192.168.0.124/32",
"192.168.0.125/32",
"192.168.0.170/32",
"192.168.0.171/32",
"192.168.0.222/32",
"192.168.0.234/32",
"192.168.0.255/32",
];
const matcher = new IPMatcher(input);
t.same(matcher.has("192.168.0.254"), false);
t.same(matcher.has("192.168.0.1"), false);
t.same(matcher.has("192.168.0.255"), true);
t.same(matcher.has("192.168.0.24"), true);
});
t.test("it works with ranges", async (t) => {
const input = [
"192.168.0.0/24",
"192.168.0.3/32",
"192.168.0.24/32",
"192.168.0.52/32",
"192.168.0.123/32",
"192.168.0.124/32",
"192.168.0.125/32",
"192.168.0.170/32",
"192.168.0.171/32",
"192.168.0.222/32",
"192.168.0.234/32",
"192.168.0.255/32",
];
const matcher = new IPMatcher(input);
t.same(matcher.has("192.168.0.254"), true);
t.same(matcher.has("10.0.0.1"), false);
t.same(matcher.has("192.168.0.234"), true);
});
t.test("it works with invalid ranges", async (t) => {
const input = [
"192.168.0.0/24",
"192.168.0.3/32",
"192.168.0.24/32",
"192.168.0.52/32",
"foobar",
"0.a.0.0/32",
"123.123.123.123/1999",
"",
",,,",
"192.168.0.124/32",
"192.168.0.125/32",
"192.168.0.170/32",
"192.168.0.171/32",
"192.168.0.222/32",
"192.168.0.234/32",
"192.168.0.255",
];
const matcher = new IPMatcher(input);
t.same(matcher.has("192.168.0.254"), true);
t.same(matcher.has("foobar"), false);
t.same(matcher.has("192.168.0.222"), true);
t.same(matcher.has("192.168.0.1"), true);
t.same(matcher.has("10.0.0.1"), false);
t.same(matcher.has("192.168.0.255"), true);
t.same(matcher.has(""), false);
t.same(matcher.has("1"), false);
t.same(matcher.has("192.168.0.1/32"), true);
});
t.test("it works with empty ranges", async (t) => {
const input: string[] = [];
const matcher = new IPMatcher(input);
t.same(matcher.has("192.168.2.1"), false);
t.same(matcher.has("foobar"), false);
});
t.test("it works with ipv6 ranges", async (t) => {
const input = [
"2002:db8::/32",
"2001:db8::1/128",
"2001:db8::2/128",
"2001:db8::3/128",
"2001:db8::4/128",
"2001:db8::5/128",
"2001:db8::6/128",
"2001:db8::7/128",
"2001:db8::8/128",
"2001:db8::9/128",
"2001:db8::a/128",
"2001:db8::b/128",
"2001:db8::c/128",
"2001:db8::d/128",
"2001:db8::e/128",
"[2001:db8::f]",
"2001:db9::abc",
];
const matcher = new IPMatcher(input);
t.same(matcher.has("2001:db8::1"), true);
t.same(matcher.has("2001:db8::0"), false);
t.same(matcher.has("2001:db8::f"), true);
t.same(matcher.has("[2001:db8::f]"), true);
t.same(matcher.has("2001:db8::10"), false);
t.same(matcher.has("2002:db8::1"), true);
t.same(matcher.has("2002:db8::2f:2"), true);
t.same(matcher.has("2001:db9::abc"), true);
});
t.test("mix ipv4 and ipv6", async (t) => {
const input = ["2002:db8::/32", "10.0.0.0/8"];
const matcher = new IPMatcher(input);
t.same(matcher.has("2001:db8::1"), false);
t.same(matcher.has("2001:db8::0"), false);
t.same(matcher.has("2002:db8::1"), true);
t.same(matcher.has("10.0.0.1"), true);
t.same(matcher.has("10.0.0.255"), true);
t.same(matcher.has("192.168.1.1"), false);
});
t.test("add ips later", async (t) => {
const input = ["2002:db8::/32", "10.0.0.0/8"];
const matcher = new IPMatcher();
t.same(matcher.has("2001:db8::0"), false);
t.same(matcher.has("2002:db8::1"), false);
for (const ip of input) {
matcher.add(ip);
}
t.same(matcher.has("2001:db8::1"), false);
t.same(matcher.has("2001:db8::0"), false);
t.same(matcher.has("2002:db8::1"), true);
t.same(matcher.has("10.0.0.1"), true);
t.same(matcher.has("10.0.0.255"), true);
t.same(matcher.has("192.168.1.1"), false);
});
t.test("strange ips", async (t) => {
const input = ["::ffff:0.0.0.0", "::ffff:0:0:0:0", "::ffff:127.0.0.1"];
const matcher = new IPMatcher(input);
t.same(matcher.has("::ffff:0.0.0.0"), true);
t.same(matcher.has("::ffff:127.0.0.1"), true);
t.same(matcher.has("::ffff:123"), false);
t.same(matcher.has("2001:db8::1"), false);
t.same(matcher.has("[::ffff:0.0.0.0]"), true);
t.same(matcher.has("::ffff:0:0:0:0"), true);
});
t.test("Different cidr ranges", async (t) => {
t.same(new IPMatcher(["123.2.0.2/0"]).has("1.1.1.1"), true);
t.same(new IPMatcher(["123.2.0.2/1"]).has("1.1.1.1"), true);
t.same(new IPMatcher(["123.2.0.2/2"]).has("1.1.1.1"), false);
t.same(new IPMatcher(["123.2.0.2/3"]).has("123.3.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/4"]).has("123.3.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/5"]).has("123.3.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/6"]).has("123.3.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/7"]).has("123.3.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/8"]).has("123.3.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/9"]).has("123.3.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/10"]).has("123.3.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/11"]).has("123.3.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/12"]).has("123.3.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/13"]).has("123.3.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/14"]).has("123.3.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/15"]).has("123.3.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/16"]).has("123.3.0.1"), false);
t.same(new IPMatcher(["123.2.0.2/17"]).has("123.2.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/18"]).has("123.2.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/19"]).has("123.2.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/20"]).has("123.2.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/21"]).has("123.2.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/22"]).has("123.2.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/23"]).has("123.2.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/24"]).has("123.2.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/25"]).has("123.2.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/26"]).has("123.2.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/27"]).has("123.2.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/29"]).has("123.2.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/30"]).has("123.2.0.1"), true);
t.same(new IPMatcher(["123.2.0.2/31"]).has("123.2.0.1"), false);
t.same(new IPMatcher(["123.2.0.2/32"]).has("123.2.0.2"), true);
});
t.test("allow all ips", async (t) => {
const matcher = new IPMatcher(["0.0.0.0/0", "::/0"]);
t.same(matcher.has("1.2.3.4"), true);
t.same(matcher.has("::1"), true);
t.same(matcher.has("::ffff:1234"), true);
t.same(matcher.has("1.1.1.1"), true);
t.same(matcher.has("2002:db8::1"), true);
t.same(matcher.has("10.0.0.1"), true);
t.same(matcher.has("10.0.0.255"), true);
t.same(matcher.has("192.168.1.1"), true);
});