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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
#include "ip.hpp"
#include <gtest/gtest.h>
#include "cxxstdio.hpp"
#define CB(X) (std::integral_constant<bool, (X)>::value)
TEST(ip4addr, cmp)
{
constexpr static
IP4Address a = IP4_LOCALHOST;
constexpr static
IP4Address b = IP4_BROADCAST;
EXPECT_FALSE(CB(a < a));
EXPECT_TRUE (CB(a < b));
EXPECT_FALSE(CB(b < a));
EXPECT_FALSE(CB(b < b));
EXPECT_FALSE(CB(a > a));
EXPECT_FALSE(CB(a > b));
EXPECT_TRUE (CB(b > a));
EXPECT_FALSE(CB(b > b));
EXPECT_TRUE (CB(a <= a));
EXPECT_TRUE (CB(a <= b));
EXPECT_FALSE(CB(b <= a));
EXPECT_TRUE (CB(b <= b));
EXPECT_TRUE (CB(a >= a));
EXPECT_FALSE(CB(a >= b));
EXPECT_TRUE (CB(b >= a));
EXPECT_TRUE (CB(b >= b));
EXPECT_TRUE (CB(a == a));
EXPECT_FALSE(CB(a == b));
EXPECT_FALSE(CB(b == a));
EXPECT_TRUE (CB(b == b));
EXPECT_FALSE(CB(a != a));
EXPECT_TRUE (CB(a != b));
EXPECT_TRUE (CB(b != a));
EXPECT_FALSE(CB(b != b));
}
TEST(ip4addr, str)
{
IP4Address a;
EXPECT_EQ("0.0.0.0", STRNPRINTF(17, "%s", a));
EXPECT_EQ("127.0.0.1", STRNPRINTF(17, "%s", IP4_LOCALHOST));
EXPECT_EQ("255.255.255.255", STRNPRINTF(17, "%s", IP4_BROADCAST));
}
TEST(ip4addr, extract)
{
IP4Address a;
EXPECT_TRUE(extract("0.0.0.0", &a));
EXPECT_EQ("0.0.0.0", STRNPRINTF(16, "%s", a));
EXPECT_TRUE(extract("127.0.0.1", &a));
EXPECT_EQ("127.0.0.1", STRNPRINTF(16, "%s", a));
EXPECT_TRUE(extract("255.255.255.255", &a));
EXPECT_EQ("255.255.255.255", STRNPRINTF(16, "%s", a));
EXPECT_TRUE(extract("1.2.3.4", &a));
EXPECT_EQ("1.2.3.4", STRNPRINTF(16, "%s", a));
EXPECT_FALSE(extract("1.2.3.4.5", &a));
EXPECT_FALSE(extract("1.2.3.4.", &a));
EXPECT_FALSE(extract("1.2.3.", &a));
EXPECT_FALSE(extract("1.2.3", &a));
EXPECT_FALSE(extract("1.2.", &a));
EXPECT_FALSE(extract("1.2", &a));
EXPECT_FALSE(extract("1.", &a));
EXPECT_FALSE(extract("1", &a));
EXPECT_FALSE(extract("", &a));
}
TEST(ip4mask, body)
{
IP4Mask m;
EXPECT_EQ(IP4Address(), m.addr());
EXPECT_EQ(IP4Address(), m.mask());
m = IP4Mask(IP4_LOCALHOST, IP4_BROADCAST);
EXPECT_EQ(IP4_LOCALHOST, m.addr());
EXPECT_EQ(IP4_BROADCAST, m.mask());
}
TEST(ip4mask, str)
{
IP4Mask m;
EXPECT_EQ("0.0.0.0/0.0.0.0", STRNPRINTF(33, "%s", m));
m = IP4Mask(IP4_LOCALHOST, IP4_BROADCAST);
EXPECT_EQ("127.0.0.1/255.255.255.255", STRNPRINTF(33, "%s", m));
}
TEST(ip4mask, extract)
{
IP4Mask m;
EXPECT_FALSE(extract("9.8.7.6/33", &m));
EXPECT_FALSE(extract("9.8.7.6.5", &m));
EXPECT_FALSE(extract("9.8.7.6/", &m));
EXPECT_FALSE(extract("9.8.7", &m));
EXPECT_FALSE(extract("9.8", &m));
EXPECT_FALSE(extract("9", &m));
EXPECT_TRUE(extract("127.0.0.1", &m));
EXPECT_EQ("127.0.0.1/255.255.255.255", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("127.0.0.1.", &m));
EXPECT_EQ("127.0.0.1/255.255.255.255", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("127.0.0.", &m));
EXPECT_EQ("127.0.0.0/255.255.255.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("127.0.", &m));
EXPECT_EQ("127.0.0.0/255.255.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("127.", &m));
EXPECT_EQ("127.0.0.0/255.0.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("1.2.3.4/255.255.255.255", &m));
EXPECT_EQ("1.2.3.4/255.255.255.255", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("1.2.3.0/255.255.255.0", &m));
EXPECT_EQ("1.2.3.0/255.255.255.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("1.2.0.4/255.255.0.255", &m));
EXPECT_EQ("1.2.0.4/255.255.0.255", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("1.2.0.0/255.255.0.0", &m));
EXPECT_EQ("1.2.0.0/255.255.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("1.0.3.4/255.0.255.255", &m));
EXPECT_EQ("1.0.3.4/255.0.255.255", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("1.0.3.0/255.0.255.0", &m));
EXPECT_EQ("1.0.3.0/255.0.255.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("1.0.0.4/255.0.0.255", &m));
EXPECT_EQ("1.0.0.4/255.0.0.255", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("1.0.0.0/255.0.0.0", &m));
EXPECT_EQ("1.0.0.0/255.0.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.2.3.4/0.255.255.255", &m));
EXPECT_EQ("0.2.3.4/0.255.255.255", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.2.3.0/0.255.255.0", &m));
EXPECT_EQ("0.2.3.0/0.255.255.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.2.0.4/0.255.0.255", &m));
EXPECT_EQ("0.2.0.4/0.255.0.255", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.2.0.0/0.255.0.0", &m));
EXPECT_EQ("0.2.0.0/0.255.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.3.4/0.0.255.255", &m));
EXPECT_EQ("0.0.3.4/0.0.255.255", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.3.0/0.0.255.0", &m));
EXPECT_EQ("0.0.3.0/0.0.255.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.4/0.0.0.255", &m));
EXPECT_EQ("0.0.0.4/0.0.0.255", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/0.0.0.0", &m));
EXPECT_EQ("0.0.0.0/0.0.0.0", STRNPRINTF(32, "%s", m));
// please don't do this
EXPECT_TRUE(extract("120.248.200.217/89.57.126.5", &m));
EXPECT_EQ("88.56.72.1/89.57.126.5", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/32", &m));
EXPECT_EQ("0.0.0.0/255.255.255.255", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/31", &m));
EXPECT_EQ("0.0.0.0/255.255.255.254", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/30", &m));
EXPECT_EQ("0.0.0.0/255.255.255.252", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/29", &m));
EXPECT_EQ("0.0.0.0/255.255.255.248", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/28", &m));
EXPECT_EQ("0.0.0.0/255.255.255.240", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/27", &m));
EXPECT_EQ("0.0.0.0/255.255.255.224", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/26", &m));
EXPECT_EQ("0.0.0.0/255.255.255.192", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/25", &m));
EXPECT_EQ("0.0.0.0/255.255.255.128", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/24", &m));
EXPECT_EQ("0.0.0.0/255.255.255.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/23", &m));
EXPECT_EQ("0.0.0.0/255.255.254.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/22", &m));
EXPECT_EQ("0.0.0.0/255.255.252.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/21", &m));
EXPECT_EQ("0.0.0.0/255.255.248.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/20", &m));
EXPECT_EQ("0.0.0.0/255.255.240.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/19", &m));
EXPECT_EQ("0.0.0.0/255.255.224.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/18", &m));
EXPECT_EQ("0.0.0.0/255.255.192.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/17", &m));
EXPECT_EQ("0.0.0.0/255.255.128.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/16", &m));
EXPECT_EQ("0.0.0.0/255.255.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/15", &m));
EXPECT_EQ("0.0.0.0/255.254.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/14", &m));
EXPECT_EQ("0.0.0.0/255.252.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/13", &m));
EXPECT_EQ("0.0.0.0/255.248.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/12", &m));
EXPECT_EQ("0.0.0.0/255.240.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/11", &m));
EXPECT_EQ("0.0.0.0/255.224.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/10", &m));
EXPECT_EQ("0.0.0.0/255.192.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/9", &m));
EXPECT_EQ("0.0.0.0/255.128.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/8", &m));
EXPECT_EQ("0.0.0.0/255.0.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/7", &m));
EXPECT_EQ("0.0.0.0/254.0.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/6", &m));
EXPECT_EQ("0.0.0.0/252.0.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/5", &m));
EXPECT_EQ("0.0.0.0/248.0.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/4", &m));
EXPECT_EQ("0.0.0.0/240.0.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/3", &m));
EXPECT_EQ("0.0.0.0/224.0.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/2", &m));
EXPECT_EQ("0.0.0.0/192.0.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/1", &m));
EXPECT_EQ("0.0.0.0/128.0.0.0", STRNPRINTF(32, "%s", m));
EXPECT_TRUE(extract("0.0.0.0/0", &m));
EXPECT_EQ("0.0.0.0/0.0.0.0", STRNPRINTF(32, "%s", m));
}
TEST(ip4mask, cover)
{
IP4Address a;
IP4Address b = IP4_BROADCAST;
IP4Address l = IP4_LOCALHOST;
IP4Address h({127, 255, 255, 255});
IP4Address p24l({10, 0, 0, 0});
IP4Address p24h({10, 255, 255, 255});
IP4Address p20l({172, 16, 0, 0});
IP4Address p20h({172, 31, 255, 255});
IP4Address p16l({192, 168, 0, 0});
IP4Address p16h({192, 168, 255, 255});
IP4Mask m;
EXPECT_TRUE(m.covers(a));
EXPECT_TRUE(m.covers(b));
EXPECT_TRUE(m.covers(l));
EXPECT_TRUE(m.covers(h));
EXPECT_TRUE(m.covers(p24l));
EXPECT_TRUE(m.covers(p24h));
EXPECT_TRUE(m.covers(p20l));
EXPECT_TRUE(m.covers(p20h));
EXPECT_TRUE(m.covers(p16l));
EXPECT_TRUE(m.covers(p16h));
m = IP4Mask(l, a);
EXPECT_TRUE(m.covers(a));
EXPECT_TRUE(m.covers(b));
EXPECT_TRUE(m.covers(l));
EXPECT_TRUE(m.covers(h));
EXPECT_TRUE(m.covers(p24l));
EXPECT_TRUE(m.covers(p24h));
EXPECT_TRUE(m.covers(p20l));
EXPECT_TRUE(m.covers(p20h));
EXPECT_TRUE(m.covers(p16l));
EXPECT_TRUE(m.covers(p16h));
m = IP4Mask(l, b);
EXPECT_FALSE(m.covers(a));
EXPECT_FALSE(m.covers(b));
EXPECT_TRUE(m.covers(l));
EXPECT_FALSE(m.covers(h));
EXPECT_FALSE(m.covers(p24l));
EXPECT_FALSE(m.covers(p24h));
EXPECT_FALSE(m.covers(p20l));
EXPECT_FALSE(m.covers(p20h));
EXPECT_FALSE(m.covers(p16l));
EXPECT_FALSE(m.covers(p16h));
// but the really useful ones are with partial masks
m = IP4Mask(IP4Address({10, 0, 0, 0}), IP4Address({255, 0, 0, 0}));
EXPECT_FALSE(m.covers(a));
EXPECT_FALSE(m.covers(b));
EXPECT_FALSE(m.covers(l));
EXPECT_FALSE(m.covers(h));
EXPECT_TRUE(m.covers(p24l));
EXPECT_TRUE(m.covers(p24h));
EXPECT_FALSE(m.covers(p20l));
EXPECT_FALSE(m.covers(p20h));
EXPECT_FALSE(m.covers(p16l));
EXPECT_FALSE(m.covers(p16h));
EXPECT_FALSE(m.covers(IP4Address({9, 255, 255, 255})));
EXPECT_FALSE(m.covers(IP4Address({11, 0, 0, 0})));
m = IP4Mask(IP4Address({127, 0, 0, 0}), IP4Address({255, 0, 0, 0}));
EXPECT_FALSE(m.covers(a));
EXPECT_FALSE(m.covers(b));
EXPECT_TRUE(m.covers(l));
EXPECT_TRUE(m.covers(h));
EXPECT_FALSE(m.covers(p24l));
EXPECT_FALSE(m.covers(p24h));
EXPECT_FALSE(m.covers(p20l));
EXPECT_FALSE(m.covers(p20h));
EXPECT_FALSE(m.covers(p16l));
EXPECT_FALSE(m.covers(p16h));
EXPECT_FALSE(m.covers(IP4Address({126, 255, 255, 255})));
EXPECT_FALSE(m.covers(IP4Address({128, 0, 0, 0})));
m = IP4Mask(IP4Address({172, 16, 0, 0}), IP4Address({255, 240, 0, 0}));
EXPECT_FALSE(m.covers(a));
EXPECT_FALSE(m.covers(b));
EXPECT_FALSE(m.covers(l));
EXPECT_FALSE(m.covers(h));
EXPECT_FALSE(m.covers(p24l));
EXPECT_FALSE(m.covers(p24h));
EXPECT_TRUE(m.covers(p20l));
EXPECT_TRUE(m.covers(p20h));
EXPECT_FALSE(m.covers(p16l));
EXPECT_FALSE(m.covers(p16h));
EXPECT_FALSE(m.covers(IP4Address({172, 15, 255, 255})));
EXPECT_FALSE(m.covers(IP4Address({172, 32, 0, 0})));
m = IP4Mask(IP4Address({192, 168, 0, 0}), IP4Address({255, 255, 0, 0}));
EXPECT_FALSE(m.covers(a));
EXPECT_FALSE(m.covers(b));
EXPECT_FALSE(m.covers(l));
EXPECT_FALSE(m.covers(h));
EXPECT_FALSE(m.covers(p24l));
EXPECT_FALSE(m.covers(p24h));
EXPECT_FALSE(m.covers(p20l));
EXPECT_FALSE(m.covers(p20h));
EXPECT_TRUE(m.covers(p16l));
EXPECT_TRUE(m.covers(p16h));
EXPECT_FALSE(m.covers(IP4Address({192, 167, 255, 255})));
EXPECT_FALSE(m.covers(IP4Address({192, 169, 0, 0})));
// OTOH this is crazy
EXPECT_TRUE(extract("120.248.200.217/89.57.126.5", &m));
EXPECT_TRUE(m.covers(IP4Address({120, 248, 200, 217})));
EXPECT_TRUE(m.covers(IP4Address({88, 56, 72, 1})));
EXPECT_FALSE(m.covers(IP4Address({88, 56, 72, 0})));
EXPECT_FALSE(m.covers(IP4Address({88, 56, 72, 255})));
}
|