3b054ceed22430d44db0e1d61d03a24cc94f76a8
[strongswan.git] / src / frontends / android / app / src / test / java / org / strongswan / android / test / IPRangeSetTest.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 import org.strongswan.android.utils.IPRangeSet;
21
22 import java.net.UnknownHostException;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import static org.junit.Assert.assertEquals;
28
29 public class IPRangeSetTest
30 {
31 private void assertSubnets(IPRangeSet set, IPRange...exp)
32 {
33 Iterator<IPRange> subnets = set.subnets().iterator();
34 if (exp.length == 0)
35 {
36 assertEquals("no subnets", false, subnets.hasNext());
37 return;
38 }
39 for (IPRange e : exp)
40 {
41 assertEquals("has subnet", true, subnets.hasNext());
42 assertEquals("range", e, subnets.next());
43 }
44 assertEquals("done", false, subnets.hasNext());
45 }
46
47 @Test
48 public void testEmpty()
49 {
50 IPRangeSet set = new IPRangeSet();
51 assertEquals("size", 0, set.size());
52 assertSubnets(set);
53 }
54
55 @Test
56 public void testAddDistinct() throws UnknownHostException
57 {
58 IPRangeSet set = new IPRangeSet();
59 set.add(new IPRange("192.168.1.0/24"));
60 assertEquals("size", 1, set.size());
61 assertSubnets(set, new IPRange("192.168.1.0/24"));
62 set.add(new IPRange("10.0.1.0/24"));
63 assertEquals("size", 2, set.size());
64 assertSubnets(set, new IPRange("10.0.1.0/24"), new IPRange("192.168.1.0/24"));
65 }
66
67 @Test
68 public void testAddAdjacent() throws UnknownHostException
69 {
70 IPRangeSet set = new IPRangeSet();
71 set.add(new IPRange("192.168.1.0/24"));
72 assertEquals("size", 1, set.size());
73 assertSubnets(set, new IPRange("192.168.1.0/24"));
74 set.add(new IPRange("192.168.2.0/24"));
75 assertEquals("size", 1, set.size());
76 assertSubnets(set, new IPRange("192.168.1.0/24"), new IPRange("192.168.2.0/24"));
77 }
78
79 @Test
80 public void testAddAdjacentJoin() throws UnknownHostException
81 {
82 IPRangeSet set = new IPRangeSet();
83 set.add(new IPRange("192.168.1.0/24"));
84 set.add(new IPRange("192.168.3.0/24"));
85 assertEquals("size", 2, set.size());
86 set.add(new IPRange("192.168.2.0/24"));
87 assertEquals("size", 1, set.size());
88 assertSubnets(set, new IPRange("192.168.1.0/24"), new IPRange("192.168.2.0/23"));
89 }
90
91 @Test
92 public void testAddSame() throws UnknownHostException
93 {
94 IPRangeSet set = new IPRangeSet();
95 set.add(new IPRange("192.168.1.0/24"));
96 assertEquals("size", 1, set.size());
97 assertSubnets(set, new IPRange("192.168.1.0/24"));
98 set.add(new IPRange("192.168.1.0/24"));
99 assertEquals("size", 1, set.size());
100 assertSubnets(set, new IPRange("192.168.1.0/24"));
101 }
102
103 @Test
104 public void testAddLarger() throws UnknownHostException
105 {
106 IPRangeSet set = new IPRangeSet();
107 set.add(new IPRange("192.168.1.0/24"));
108 assertEquals("size", 1, set.size());
109 assertSubnets(set, new IPRange("192.168.1.0/24"));
110 set.add(new IPRange("192.168.0.0/16"));
111 assertEquals("size", 1, set.size());
112 assertSubnets(set, new IPRange("192.168.0.0/16"));
113 set.add(new IPRange("0.0.0.0/0"));
114 assertEquals("size", 1, set.size());
115 assertSubnets(set, new IPRange("0.0.0.0/0"));
116 }
117
118 @Test
119 public void testAddLargerMulti() throws UnknownHostException
120 {
121 IPRangeSet set = new IPRangeSet();
122 set.add(new IPRange("192.168.1.0/24"));
123 set.add(new IPRange("10.0.1.0/24"));
124 set.add(new IPRange("255.255.255.255/32"));
125 assertEquals("size", 3, set.size());
126 set.add(new IPRange("0.0.0.0/0"));
127 assertEquals("size", 1, set.size());
128 assertSubnets(set, new IPRange("0.0.0.0/0"));
129 }
130
131 @Test
132 public void testAddAll() throws UnknownHostException
133 {
134 IPRangeSet set = new IPRangeSet();
135 List<IPRange> list = new ArrayList<>();
136 list.add(new IPRange("192.168.1.0/24"));
137 list.add(new IPRange("10.0.1.0/24"));
138 list.add(new IPRange("255.255.255.255/32"));
139 set.addAll(list);
140 assertEquals("size", 3, set.size());
141 assertSubnets(set, new IPRange("10.0.1.0/24"), new IPRange("192.168.1.0/24"),
142 new IPRange("255.255.255.255/32"));
143 }
144
145 @Test
146 public void testAddSet() throws UnknownHostException
147 {
148 IPRangeSet set = new IPRangeSet();
149 IPRangeSet other = new IPRangeSet();
150 other.add(new IPRange("192.168.1.0/24"));
151 other.add(new IPRange("10.0.1.0/24"));
152 other.add(new IPRange("255.255.255.255/32"));
153 set.add(other);
154 assertEquals("size", 3, set.size());
155 assertSubnets(set, new IPRange("10.0.1.0/24"), new IPRange("192.168.1.0/24"),
156 new IPRange("255.255.255.255/32"));
157 }
158
159 @Test
160 public void testAddSetIdent() throws UnknownHostException
161 {
162 IPRangeSet set = new IPRangeSet();
163 set.add(new IPRange("192.168.1.0/24"));
164 set.add(new IPRange("10.0.1.0/24"));
165 set.add(new IPRange("255.255.255.255/32"));
166 set.add(set);
167 assertEquals("size", 3, set.size());
168 assertSubnets(set, new IPRange("10.0.1.0/24"), new IPRange("192.168.1.0/24"),
169 new IPRange("255.255.255.255/32"));
170 }
171
172 @Test
173 public void testRemoveNothing() throws UnknownHostException
174 {
175 IPRangeSet set = new IPRangeSet();
176 set.add(new IPRange("192.168.1.0/24"));
177 set.remove(new IPRange("192.168.2.0/24"));
178 assertEquals("size", 1, set.size());
179 assertSubnets(set, new IPRange("192.168.1.0/24"));
180 set.remove(new IPRange("10.0.1.0/24"));
181 assertEquals("size", 1, set.size());
182 assertSubnets(set, new IPRange("192.168.1.0/24"));
183 }
184
185 @Test
186 public void testRemoveAll() throws UnknownHostException
187 {
188 IPRangeSet set = new IPRangeSet();
189 set.add(new IPRange("192.168.1.0/24"));
190 set.remove(new IPRange("192.168.0.0/16"));
191 assertEquals("size", 0, set.size());
192 assertSubnets(set);
193 set.add(new IPRange("192.168.1.0/24"));
194 set.add(new IPRange("10.0.1.0/24"));
195 set.add(new IPRange("255.255.255.255/32"));
196 assertEquals("size", 3, set.size());
197 set.remove(new IPRange("0.0.0.0/0"));
198 assertEquals("size", 0, set.size());
199 assertSubnets(set);
200 }
201
202 @Test
203 public void testRemoveOverlap() throws UnknownHostException
204 {
205 IPRangeSet set = new IPRangeSet();
206 set.add(new IPRange("192.168.1.0/24"));
207 set.add(new IPRange("192.168.2.0/24"));
208 set.remove(new IPRange("192.168.1.128", "192.168.2.127"));
209 assertEquals("size", 2, set.size());
210 assertSubnets(set, new IPRange("192.168.1.0/25"), new IPRange("192.168.2.128/25"));
211 }
212
213 @Test
214 public void testRemoveRangesIdent() throws UnknownHostException
215 {
216 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24 192.168.4.0/24");
217 set.remove(set);
218 assertEquals("size", 0, set.size());
219 assertSubnets(set);
220 }
221
222 @Test
223 public void testRemoveRanges() throws UnknownHostException
224 {
225 IPRangeSet set = IPRangeSet.fromString("192.168.0.0/16");
226 IPRangeSet remove = IPRangeSet.fromString("192.168.1.0/24 192.168.3.0/24 192.168.16.0-192.168.255.255");
227 set.remove(remove);
228 assertEquals("size", 3, set.size());
229 assertSubnets(set, new IPRange("192.168.0.0/24"), new IPRange("192.168.2.0/24"),
230 new IPRange("192.168.4.0/22"), new IPRange("192.168.8.0/21"));
231 }
232
233 @Test
234 public void testFromStringSingle() throws UnknownHostException
235 {
236 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24");
237 assertEquals("size", 1, set.size());
238 assertSubnets(set, new IPRange("192.168.1.0/24"));
239 }
240
241 @Test
242 public void testFromString() throws UnknownHostException
243 {
244 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24 fec1::/64 10.0.1.0/24 255.255.255.255");
245 assertEquals("size", 4, set.size());
246 assertSubnets(set, new IPRange("10.0.1.0/24"), new IPRange("192.168.1.0/24"),
247 new IPRange("255.255.255.255/32"), new IPRange("fec1::/64"));
248 }
249
250 @Test
251 public void testFromStringRange() throws UnknownHostException
252 {
253 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24 10.0.1.0-10.0.1.16");
254 assertEquals("size", 2, set.size());
255 assertSubnets(set, new IPRange("10.0.1.0/28"), new IPRange("10.0.1.16/32"),
256 new IPRange("192.168.1.0/24"));
257 }
258
259 @Test
260 public void testFromStringInvalidPrefix() throws UnknownHostException
261 {
262 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/65");
263 assertEquals("failed", null, set);
264 }
265
266 @Test
267 public void testFromStringInvalidRange() throws UnknownHostException
268 {
269 IPRangeSet set = IPRangeSet.fromString("192.168.1.1 - 192.168.1.10");
270 assertEquals("failed", null, set);
271 }
272
273 @Test
274 public void testIteratorRanges() throws UnknownHostException
275 {
276 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24 192.168.2.0/24");
277 assertSubnets(set, new IPRange("192.168.1.0/24"), new IPRange("192.168.2.0/24"));
278 Iterator<IPRange> iterator = set.iterator();
279 assertEquals("hasNext", true, iterator.hasNext());
280 assertEquals("next", new IPRange("192.168.1.0-192.168.2.255"), iterator.next());
281 assertEquals("hasNext", false, iterator.hasNext());
282 }
283
284 @Test
285 public void testIteratorRemove() throws UnknownHostException
286 {
287 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24");
288 assertSubnets(set, new IPRange("192.168.1.0/24"));
289 Iterator<IPRange> iterator = set.iterator();
290 assertEquals("next", new IPRange("192.168.1.0/24"), iterator.next());
291 iterator.remove();
292 assertEquals("hasNext", false, iterator.hasNext());
293 assertEquals("size", 0, set.size());
294 }
295
296 @Test(expected = UnsupportedOperationException.class)
297 public void testIteratorSubnetRemove() throws UnknownHostException
298 {
299 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24");
300 Iterator<IPRange> iterator = set.subnets().iterator();
301 assertEquals("hasNext", true, iterator.hasNext());
302 assertEquals("next", new IPRange("192.168.1.0/24"), iterator.next());
303 iterator.remove();
304 }
305 }