android: Change format of address ranges and print sets
[strongswan.git] / src / frontends / android / app / src / test / java / org / strongswan / android / test / IPRangeTest.java
1 /*
2 * Copyright (C) 2017 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 package org.strongswan.android.test;
17
18 import org.junit.Test;
19 import org.strongswan.android.utils.IPRange;
20
21 import java.net.InetAddress;
22 import java.net.UnknownHostException;
23 import java.util.List;
24
25 import static org.junit.Assert.assertEquals;
26
27 public class IPRangeTest
28 {
29 @Test
30 public void testRangeReversed() throws UnknownHostException
31 {
32 IPRange test = new IPRange("192.168.0.10", "192.168.0.1");
33 assertEquals("from", "192.168.0.1", test.getFrom().getHostAddress());
34 assertEquals("to", "192.168.0.10", test.getTo().getHostAddress());
35 }
36
37 @Test(expected = IllegalArgumentException.class)
38 public void testRangeInvalid() throws UnknownHostException
39 {
40 IPRange test = new IPRange("192.168.0.1", "fec1::1");
41 assertEquals("from", "192.168.0.1", test.getFrom().getHostAddress());
42 }
43
44 @Test(expected = UnknownHostException.class)
45 public void testPrefixAddrInvalid() throws UnknownHostException
46 {
47 IPRange test = new IPRange("a.b.c.d", 24);
48 assertEquals("from", "192.168.0.1", test.getFrom().getHostAddress());
49 }
50
51 @Test(expected = IllegalArgumentException.class)
52 public void testPrefixNegative() throws UnknownHostException
53 {
54 IPRange test = new IPRange("192.168.0.1", -5);
55 assertEquals("from", "192.168.0.1", test.getFrom().getHostAddress());
56 }
57
58 @Test(expected = IllegalArgumentException.class)
59 public void testPrefixLarge() throws UnknownHostException
60 {
61 IPRange test = new IPRange("192.168.0.1", 42);
62 assertEquals("from", "192.168.0.1", test.getFrom().getHostAddress());
63 }
64
65 private void testPrefix(String from, String to, Integer prefix) throws UnknownHostException
66 {
67 IPRange test = new IPRange(from, to);
68 assertEquals("prefix", prefix, test.getPrefix());
69 }
70
71 @Test
72 public void testPrefix() throws UnknownHostException
73 {
74 testPrefix("192.168.0.0", "192.168.0.255", 24);
75 testPrefix("192.168.0.8", "192.168.0.15", 29);
76 testPrefix("192.168.0.1", "192.168.0.255", null);
77 testPrefix("192.168.0.1", "192.168.0.1", 32);
78 testPrefix("fec1::0", "fec1::ffff", 112);
79 testPrefix("fec1::1", "fec1::ffff", null);
80 testPrefix("fec1::1", "fec1::1", 128);
81 }
82
83 private void testPrefixRange(String base, int prefix, String from, String to) throws UnknownHostException
84 {
85 IPRange test = new IPRange(InetAddress.getByName(base), prefix);
86 assertEquals("from", from, test.getFrom().getHostAddress());
87 assertEquals("to", to, test.getTo().getHostAddress());
88 }
89
90 @Test
91 public void testPrefixRange() throws UnknownHostException
92 {
93 testPrefixRange("0.0.0.0", 0, "0.0.0.0", "255.255.255.255");
94 testPrefixRange("0.0.0.0", 32, "0.0.0.0", "0.0.0.0");
95 testPrefixRange("192.168.1.0", 24, "192.168.1.0", "192.168.1.255");
96 testPrefixRange("192.168.1.10", 24, "192.168.1.0", "192.168.1.255");
97 testPrefixRange("192.168.1.64", 26, "192.168.1.64", "192.168.1.127");
98 testPrefixRange("192.168.1.64", 27, "192.168.1.64", "192.168.1.95");
99 testPrefixRange("192.168.1.64", 28, "192.168.1.64", "192.168.1.79");
100 testPrefixRange("192.168.1.255", 29, "192.168.1.248", "192.168.1.255");
101 testPrefixRange("192.168.1.255", 30, "192.168.1.252", "192.168.1.255");
102 testPrefixRange("192.168.1.255", 31, "192.168.1.254", "192.168.1.255");
103 testPrefixRange("192.168.1.255", 32, "192.168.1.255", "192.168.1.255");
104
105 testPrefixRange("::", 0, "0:0:0:0:0:0:0:0", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
106 testPrefixRange("fec1::", 64, "fec1:0:0:0:0:0:0:0", "fec1:0:0:0:ffff:ffff:ffff:ffff");
107 testPrefixRange("fec1::1", 128, "fec1:0:0:0:0:0:0:1", "fec1:0:0:0:0:0:0:1");
108 testPrefixRange("fec1::10:0", 108, "fec1:0:0:0:0:0:10:0", "fec1:0:0:0:0:0:1f:ffff");
109 testPrefixRange("fec1::10:0", 112, "fec1:0:0:0:0:0:10:0", "fec1:0:0:0:0:0:10:ffff");
110 testPrefixRange("fec1::10:0", 113, "fec1:0:0:0:0:0:10:0", "fec1:0:0:0:0:0:10:7fff");
111 testPrefixRange("fec1::10:0", 116, "fec1:0:0:0:0:0:10:0", "fec1:0:0:0:0:0:10:fff");
112 testPrefixRange("fec1::1:ffff", 112, "fec1:0:0:0:0:0:1:0", "fec1:0:0:0:0:0:1:ffff");
113 testPrefixRange("fec1::1:ffff", 113, "fec1:0:0:0:0:0:1:8000", "fec1:0:0:0:0:0:1:ffff");
114 testPrefixRange("fec1::1:ffff", 114, "fec1:0:0:0:0:0:1:c000", "fec1:0:0:0:0:0:1:ffff");
115 testPrefixRange("fec1::1:ffff", 115, "fec1:0:0:0:0:0:1:e000", "fec1:0:0:0:0:0:1:ffff");
116 testPrefixRange("fec1::1:ffff", 116, "fec1:0:0:0:0:0:1:f000", "fec1:0:0:0:0:0:1:ffff");
117 testPrefixRange("fec1::1:ffff", 117, "fec1:0:0:0:0:0:1:f800", "fec1:0:0:0:0:0:1:ffff");
118 }
119
120 @Test(expected = IllegalArgumentException.class)
121 public void testCIDRAddressInvalid() throws UnknownHostException
122 {
123 IPRange test = new IPRange("asdf");
124 assertEquals("not reached", null, test);
125 }
126
127 @Test(expected = IllegalArgumentException.class)
128 public void testCIDRIncomplete() throws UnknownHostException
129 {
130 IPRange test = new IPRange("/32");
131 assertEquals("not reached", null, test);
132 }
133
134 @Test(expected = IllegalArgumentException.class)
135 public void testCIDRIncompletePrefix() throws UnknownHostException
136 {
137 IPRange test = new IPRange("192.168.0.1/");
138 assertEquals("not reached", null, test);
139 }
140
141 @Test(expected = IllegalArgumentException.class)
142 public void testCIDRPrefixNegative() throws UnknownHostException
143 {
144 IPRange test = new IPRange("192.168.0.1/-5");
145 assertEquals("not reached", null, test);
146 }
147
148 @Test(expected = IllegalArgumentException.class)
149 public void testCIDRPrefixLarge() throws UnknownHostException
150 {
151 IPRange test = new IPRange("192.168.0.1/33");
152 assertEquals("not reached", null, test);
153 }
154
155 @Test(expected = IllegalArgumentException.class)
156 public void testCIDRPrefixLargev6() throws UnknownHostException
157 {
158 IPRange test = new IPRange("fec1::1/129");
159 assertEquals("not reached", null, test);
160 }
161
162 @Test(expected = IllegalArgumentException.class)
163 public void testRangeMixed() throws UnknownHostException
164 {
165 IPRange test = new IPRange("192.168.1.1-fec1::1");
166 assertEquals("not reached", null, test);
167 }
168
169 private void testCIDR(String cidr, IPRange exp) throws UnknownHostException
170 {
171 IPRange test = new IPRange(cidr);
172 assertEquals("cidr", exp, test);
173 }
174
175 @Test
176 public void testCIDR() throws UnknownHostException
177 {
178 testCIDR("0.0.0.0/0", new IPRange("0.0.0.0", 0));
179 testCIDR("192.168.1.0/24", new IPRange("192.168.1.0", 24));
180 testCIDR("192.168.1.10/24", new IPRange("192.168.1.0", 24));
181 testCIDR("192.168.1.1/32", new IPRange("192.168.1.1", 32));
182 testCIDR("192.168.1.1", new IPRange("192.168.1.1", 32));
183 testCIDR("192.168.1.1-192.168.1.10", new IPRange("192.168.1.1", "192.168.1.10"));
184 testCIDR("::/0", new IPRange("::", 0));
185 testCIDR("fec1::/64", new IPRange("fec1::", 64));
186 testCIDR("fec1::10/64", new IPRange("fec1::", 64));
187 testCIDR("fec1::1/128", new IPRange("fec1::1", 128));
188 testCIDR("fec1::1", new IPRange("fec1::1", 128));
189 testCIDR("fec1::1-fec1::5", new IPRange("fec1::1", "fec1::5"));
190 }
191
192 private void testToString(String f, String t, String exp) throws UnknownHostException
193 {
194 IPRange a = new IPRange(f, t);
195 assertEquals("string", exp, a.toString());
196 }
197
198 @Test
199 public void testToString() throws UnknownHostException
200 {
201 testToString("192.168.1.1", "192.168.1.1", "192.168.1.1/32");
202 testToString("192.168.1.0", "192.168.1.255", "192.168.1.0/24");
203 testToString("192.168.1.1", "192.168.1.255", "192.168.1.1-192.168.1.255");
204 testToString("0.0.0.0", "255.255.255.255", "0.0.0.0/0");
205 testToString("fec1::1", "fec1::1", "fec1:0:0:0:0:0:0:1/128");
206 testToString("fec1::", "fec1::ffff", "fec1:0:0:0:0:0:0:0/112");
207 testToString("::", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "0:0:0:0:0:0:0:0/0");
208 }
209
210 private void compare(String af, String at, String bf, String bt, int exp) throws UnknownHostException
211 {
212 IPRange a = new IPRange(af, at);
213 IPRange b = new IPRange(bf, bt);
214 assertEquals("compare", exp, a.compareTo(b));
215 }
216
217 private void compare(String a, int pa, String b, int pb, int exp) throws UnknownHostException
218 {
219 IPRange ar = new IPRange(a, pa);
220 IPRange br = new IPRange(b, pb);
221 assertEquals("compare", exp, ar.compareTo(br));
222 }
223
224 @Test
225 public void testCompareTo() throws UnknownHostException
226 {
227 compare("192.168.0.0", "192.168.0.10", "192.168.0.0", "192.168.0.10", 0);
228 compare("192.168.0.1", "192.168.0.10", "192.168.0.0", "192.168.0.10", 1);
229 compare("192.168.0.0", "192.168.0.9", "192.168.0.0", "192.168.0.10", -1);
230
231 compare("192.168.0.0", 24, "192.168.0.0", 24, 0);
232 compare("192.168.0.0", 24, "192.168.0.0", 28, 1);
233 compare("192.168.0.0", 28, "192.168.0.0", 24, -1);
234 compare("192.168.0.0", 32, "192.168.0.255", 32, -1);
235 compare("10.0.1.0", 24, "192.168.1.0", 24, -1);
236 compare("10.0.1.0", 24, "fec1::", 64, -1);
237 compare("fec1::1", 128, "fec1::2", 128, -1);
238 compare("fec1::1", 126, "fec1::2", 126, 0);
239 }
240
241 @Test
242 public void testEquals() throws UnknownHostException
243 {
244 IPRange a = new IPRange("192.168.1.0/24");
245 IPRange b = new IPRange("192.168.1.0/24");
246 assertEquals("same", true, a.equals(a));
247 assertEquals("equals", true, a.equals(b));
248 InetAddress c = InetAddress.getByName("192.168.1.0");
249 assertEquals("null", false, a.equals(c));
250 assertEquals("null", false, a.equals(null));
251 }
252
253 private void contains(String af, String at, String bf, String bt, boolean exp) throws UnknownHostException
254 {
255 IPRange a = new IPRange(af, at);
256 IPRange b = new IPRange(bf, bt);
257 assertEquals("contains", exp, a.contains(b));
258 }
259
260 @Test
261 public void testContains() throws UnknownHostException
262 {
263 contains("192.168.1.0", "192.168.1.10", "192.168.1.0", "192.168.1.10", true);
264 contains("192.168.1.0", "192.168.1.10", "192.168.1.1", "192.168.1.10", true);
265 contains("192.168.1.0", "192.168.1.10", "192.168.1.0", "192.168.1.9", true);
266 contains("192.168.1.0", "192.168.1.10", "192.168.1.1", "192.168.1.9", true);
267 contains("192.168.1.0", "192.168.1.10", "192.168.1.2", "192.168.1.2", true);
268 contains("192.168.1.0", "192.168.1.10", "192.168.1.1", "192.168.1.11", false);
269 contains("192.168.1.0", "192.168.1.10", "192.168.0.255", "192.168.1.10", false);
270 contains("192.168.1.0", "192.168.1.10", "192.168.0.255", "192.168.1.11", false);
271 contains("192.168.1.1", "192.168.1.1", "192.168.1.0", "192.168.1.0", false);
272 contains("192.168.1.1", "192.168.1.1", "192.168.1.2", "192.168.1.2", false);
273 contains("192.168.1.0", "192.168.1.10", "fec1::", "fec1::10", false);
274 contains("fec1::", "fec1::10", "192.168.1.0", "192.168.1.10", false);
275 }
276
277 private void overlaps(String af, String at, String bf, String bt, boolean exp) throws UnknownHostException
278 {
279 IPRange a = new IPRange(af, at);
280 IPRange b = new IPRange(bf, bt);
281 assertEquals("b overlaps with a", exp, a.overlaps(b));
282 assertEquals("a overlaps with b", exp, b.overlaps(a));
283 }
284
285 @Test
286 public void testOverlaps() throws UnknownHostException
287 {
288 overlaps("192.168.1.0", "192.168.1.10", "192.168.1.0", "192.168.1.10", true);
289 overlaps("192.168.1.0", "192.168.1.10", "192.168.1.1", "192.168.1.9", true);
290 overlaps("192.168.1.0", "192.168.1.10", "192.168.1.10", "192.168.1.20", true);
291 overlaps("192.168.1.0", "192.168.1.10", "192.168.1.9", "192.168.1.20", true);
292 overlaps("192.168.1.0", "192.168.1.10", "192.168.1.11", "192.168.1.20", false);
293 overlaps("192.168.1.0", "192.168.1.10", "192.168.0.245", "192.168.1.1", true);
294 overlaps("192.168.1.0", "192.168.1.10", "192.168.0.245", "192.168.1.0", true);
295 overlaps("192.168.1.0", "192.168.1.10", "192.168.0.245", "192.168.0.255", false);
296 overlaps("192.168.1.0", "192.168.1.10", "fec1::", "fec1::10", false);
297 }
298
299 private void remove(String af, String at, String bf, String bt, IPRange...exp) throws UnknownHostException
300 {
301 IPRange a = new IPRange(af, at);
302 IPRange b = new IPRange(bf, bt);
303 List<IPRange> l = a.remove(b);
304 assertEquals("ranges", exp.length, l.size());
305 for (int i = 0; i < exp.length; i++)
306 {
307 assertEquals("range", exp[i], l.get(i));
308 }
309 }
310
311 @Test
312 public void testRemove() throws UnknownHostException
313 {
314 remove("192.168.1.0", "192.168.1.10", "192.168.1.0", "192.168.1.10");
315 remove("192.168.1.0", "192.168.1.10", "192.168.0.0", "192.168.1.255");
316 remove("192.168.1.0", "192.168.1.10", "10.0.1.0", "10.0.1.10",
317 new IPRange("192.168.1.0", "192.168.1.10"));
318 remove("192.168.1.0", "192.168.1.10", "192.168.1.0", "192.168.1.5",
319 new IPRange("192.168.1.6", "192.168.1.10"));
320 remove("192.168.1.0", "192.168.1.10", "192.168.1.6", "192.168.1.10",
321 new IPRange("192.168.1.0", "192.168.1.5"));
322 remove("192.168.1.0", "192.168.1.10", "192.168.0.0", "192.168.1.5",
323 new IPRange("192.168.1.6", "192.168.1.10"));
324 remove("192.168.1.0", "192.168.1.10", "192.168.1.6", "192.168.1.255",
325 new IPRange("192.168.1.0", "192.168.1.5"));
326 remove("192.168.1.0", "192.168.1.10", "192.168.1.1", "192.168.1.9",
327 new IPRange("192.168.1.0", "192.168.1.0"), new IPRange("192.168.1.10", "192.168.1.10"));
328 remove("192.168.1.0", "192.168.1.10", "192.168.1.3", "192.168.1.7",
329 new IPRange("192.168.1.0", "192.168.1.2"), new IPRange("192.168.1.8", "192.168.1.10"));
330 remove("192.168.0.0", "192.168.1.255", "192.168.1.0", "192.168.1.255",
331 new IPRange("192.168.0.0", "192.168.0.255"));
332 remove("192.168.0.0", "192.168.1.255", "192.168.0.0", "192.168.0.255",
333 new IPRange("192.168.1.0", "192.168.1.255"));
334 remove("192.168.1.0", "192.168.1.10", "0.0.0.0", "192.168.1.10");
335 remove("192.168.1.0", "192.168.1.10", "192.168.1.0", "255.255.255.255");
336 }
337
338 private void merge(String af, String at, String bf, String bt, IPRange exp) throws UnknownHostException
339 {
340 IPRange a = new IPRange(af, at);
341 IPRange b = new IPRange(bf, bt);
342 IPRange r = a.merge(b);
343 assertEquals("merge", exp, r);
344 r = b.merge(a);
345 assertEquals("reverse", exp, r);
346 }
347
348 @Test
349 public void testMerge() throws UnknownHostException
350 {
351 merge("192.168.1.0", "192.168.1.10", "192.168.1.0", "192.168.1.10", new IPRange("192.168.1.0", "192.168.1.10"));
352 merge("192.168.1.0", "192.168.1.10", "192.168.0.0", "192.168.1.10", new IPRange("192.168.0.0", "192.168.1.10"));
353 merge("192.168.1.0", "192.168.1.10", "192.168.0.0", "192.168.0.255", new IPRange("192.168.0.0", "192.168.1.10"));
354 merge("192.168.1.0", "192.168.1.10", "192.168.1.0", "192.168.1.255", new IPRange("192.168.1.0", "192.168.1.255"));
355 merge("192.168.1.0", "192.168.1.10", "192.168.1.11", "192.168.1.255", new IPRange("192.168.1.0", "192.168.1.255"));
356 merge("192.168.1.0", "192.168.1.10", "192.168.0.0", "192.168.1.255", new IPRange("192.168.0.0", "192.168.1.255"));
357 merge("255.255.255.0", "255.255.255.255", "0.0.0.0", "0.0.0.255", null);
358 merge("0.0.0.1", "255.255.255.255", "0.0.0.0", "0.0.0.0", new IPRange("0.0.0.0", 0));
359 merge("0.0.0.0", "255.255.255.254", "255.255.255.255", "255.255.255.255", new IPRange("0.0.0.0", 0));
360 merge("192.168.1.0", "192.168.1.10", "192.168.0.0", "192.168.0.254", null);
361 merge("192.168.1.0", "192.168.1.10", "192.168.1.12", "192.168.1.255", null);
362 merge("192.168.1.0", "192.168.1.10", "fec1::0", "fec1::10", null);
363 merge("fec1::1:0", "fec1::1:10", "fec1::1:8", "fec1::1:20", new IPRange("fec1::1:0", "fec1::1:20"));
364 }
365
366 private void toSubnet(String f, String t, IPRange...exp) throws UnknownHostException
367 {
368 IPRange a = new IPRange(f, t);
369 List<IPRange> l = a.toSubnets();
370 assertEquals("ranges", exp.length, l.size());
371 for (int i = 0; i < exp.length; i++)
372 {
373 assertEquals("range", exp[i], l.get(i));
374 }
375 }
376
377 @Test
378 public void testToSubnet() throws UnknownHostException
379 {
380 toSubnet("0.0.0.0", "255.255.255.255", new IPRange("0.0.0.0", 0));
381 toSubnet("192.168.1.1", "192.168.1.1", new IPRange("192.168.1.1", 32));
382 toSubnet("192.168.1.0", "192.168.1.255", new IPRange("192.168.1.0", 24));
383 toSubnet("192.168.1.0", "192.168.1.10", new IPRange("192.168.1.0", 29),
384 new IPRange("192.168.1.8", 31), new IPRange("192.168.1.10", 32));
385 toSubnet("192.168.1.1", "192.168.1.10", new IPRange("192.168.1.1", 32),
386 new IPRange("192.168.1.2", 31), new IPRange("192.168.1.4", 30),
387 new IPRange("192.168.1.8", 31), new IPRange("192.168.1.10", 32));
388 toSubnet("192.168.1.241", "192.168.1.255", new IPRange("192.168.1.241", 32),
389 new IPRange("192.168.1.242", 31), new IPRange("192.168.1.244", 30),
390 new IPRange("192.168.1.248", 29));
391 toSubnet("192.168.254.255", "192.168.255.1", new IPRange("192.168.254.255", 32),
392 new IPRange("192.168.255.0", 31));
393 toSubnet("::", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", new IPRange("::", 0));
394 toSubnet("fec1::0", "fec1::a", new IPRange("fec1::0", 125), new IPRange("fec1::8", 127),
395 new IPRange("fec1::a", 128));
396 }
397 }