android: Implement Iterable interface and addAll() for range set
[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 testRemoveNothing() throws UnknownHostException
147 {
148 IPRangeSet set = new IPRangeSet();
149 set.add(new IPRange("192.168.1.0/24"));
150 set.remove(new IPRange("192.168.2.0/24"));
151 assertEquals("size", 1, set.size());
152 assertSubnets(set, new IPRange("192.168.1.0/24"));
153 set.remove(new IPRange("10.0.1.0/24"));
154 assertEquals("size", 1, set.size());
155 assertSubnets(set, new IPRange("192.168.1.0/24"));
156 }
157
158 @Test
159 public void testRemoveAll() throws UnknownHostException
160 {
161 IPRangeSet set = new IPRangeSet();
162 set.add(new IPRange("192.168.1.0/24"));
163 set.remove(new IPRange("192.168.0.0/16"));
164 assertEquals("size", 0, set.size());
165 assertSubnets(set);
166 set.add(new IPRange("192.168.1.0/24"));
167 set.add(new IPRange("10.0.1.0/24"));
168 set.add(new IPRange("255.255.255.255/32"));
169 assertEquals("size", 3, set.size());
170 set.remove(new IPRange("0.0.0.0/0"));
171 assertEquals("size", 0, set.size());
172 assertSubnets(set);
173 }
174
175 @Test
176 public void testRemoveOverlap() throws UnknownHostException
177 {
178 IPRangeSet set = new IPRangeSet();
179 set.add(new IPRange("192.168.1.0/24"));
180 set.add(new IPRange("192.168.2.0/24"));
181 set.remove(new IPRange("192.168.1.128", "192.168.2.127"));
182 assertEquals("size", 2, set.size());
183 assertSubnets(set, new IPRange("192.168.1.0/25"), new IPRange("192.168.2.128/25"));
184 }
185
186 @Test
187 public void testRemoveRangesIdent() throws UnknownHostException
188 {
189 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24 192.168.4.0/24");
190 set.remove(set);
191 assertEquals("size", 0, set.size());
192 assertSubnets(set);
193 }
194
195 @Test
196 public void testRemoveRanges() throws UnknownHostException
197 {
198 IPRangeSet set = IPRangeSet.fromString("192.168.0.0/16");
199 IPRangeSet remove = IPRangeSet.fromString("192.168.1.0/24 192.168.3.0/24 192.168.16.0-192.168.255.255");
200 set.remove(remove);
201 assertEquals("size", 3, set.size());
202 assertSubnets(set, new IPRange("192.168.0.0/24"), new IPRange("192.168.2.0/24"),
203 new IPRange("192.168.4.0/22"), new IPRange("192.168.8.0/21"));
204 }
205
206 @Test
207 public void testFromStringSingle() throws UnknownHostException
208 {
209 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24");
210 assertEquals("size", 1, set.size());
211 assertSubnets(set, new IPRange("192.168.1.0/24"));
212 }
213
214 @Test
215 public void testFromString() throws UnknownHostException
216 {
217 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24 fec1::/64 10.0.1.0/24 255.255.255.255");
218 assertEquals("size", 4, set.size());
219 assertSubnets(set, new IPRange("10.0.1.0/24"), new IPRange("192.168.1.0/24"),
220 new IPRange("255.255.255.255/32"), new IPRange("fec1::/64"));
221 }
222
223 @Test
224 public void testFromStringRange() throws UnknownHostException
225 {
226 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24 10.0.1.0-10.0.1.16");
227 assertEquals("size", 2, set.size());
228 assertSubnets(set, new IPRange("10.0.1.0/28"), new IPRange("10.0.1.16/32"),
229 new IPRange("192.168.1.0/24"));
230 }
231
232 @Test
233 public void testFromStringInvalidPrefix() throws UnknownHostException
234 {
235 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/65");
236 assertEquals("failed", null, set);
237 }
238
239 @Test
240 public void testFromStringInvalidRange() throws UnknownHostException
241 {
242 IPRangeSet set = IPRangeSet.fromString("192.168.1.1 - 192.168.1.10");
243 assertEquals("failed", null, set);
244 }
245
246 @Test
247 public void testIteratorRanges() throws UnknownHostException
248 {
249 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24 192.168.2.0/24");
250 assertSubnets(set, new IPRange("192.168.1.0/24"), new IPRange("192.168.2.0/24"));
251 Iterator<IPRange> iterator = set.iterator();
252 assertEquals("hasNext", true, iterator.hasNext());
253 assertEquals("next", new IPRange("192.168.1.0-192.168.2.255"), iterator.next());
254 assertEquals("hasNext", false, iterator.hasNext());
255 }
256
257 @Test
258 public void testIteratorRemove() throws UnknownHostException
259 {
260 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24");
261 assertSubnets(set, new IPRange("192.168.1.0/24"));
262 Iterator<IPRange> iterator = set.iterator();
263 assertEquals("next", new IPRange("192.168.1.0/24"), iterator.next());
264 iterator.remove();
265 assertEquals("hasNext", false, iterator.hasNext());
266 assertEquals("size", 0, set.size());
267 }
268
269 @Test(expected = UnsupportedOperationException.class)
270 public void testIteratorSubnetRemove() throws UnknownHostException
271 {
272 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24");
273 Iterator<IPRange> iterator = set.subnets().iterator();
274 assertEquals("hasNext", true, iterator.hasNext());
275 assertEquals("next", new IPRange("192.168.1.0/24"), iterator.next());
276 iterator.remove();
277 }
278 }