android: Add class to manage a set of IP address ranges/subnets
[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.Enumeration;
24
25 import static org.junit.Assert.assertEquals;
26
27 public class IPRangeSetTest
28 {
29 private void assertSubnets(Enumeration<IPRange> subnets, IPRange...exp)
30 {
31 if (exp.length == 0)
32 {
33 assertEquals("no subnets", false, subnets.hasMoreElements());
34 return;
35 }
36 for (int i = 0; i < exp.length; i++)
37 {
38 assertEquals("has subnet", true, subnets.hasMoreElements());
39 assertEquals("range", exp[i], subnets.nextElement());
40 }
41 assertEquals("done", false, subnets.hasMoreElements());
42 }
43
44 @Test
45 public void testEmpty()
46 {
47 IPRangeSet set = new IPRangeSet();
48 assertEquals("size", 0, set.size());
49 assertSubnets(set.getSubnets());
50 }
51
52 @Test
53 public void testAddDistinct() throws UnknownHostException
54 {
55 IPRangeSet set = new IPRangeSet();
56 set.add(new IPRange("192.168.1.0/24"));
57 assertEquals("size", 1, set.size());
58 assertSubnets(set.getSubnets(), new IPRange("192.168.1.0/24"));
59 set.add(new IPRange("10.0.1.0/24"));
60 assertEquals("size", 2, set.size());
61 assertSubnets(set.getSubnets(), new IPRange("10.0.1.0/24"), new IPRange("192.168.1.0/24"));
62 }
63
64 @Test
65 public void testAddAdjacent() throws UnknownHostException
66 {
67 IPRangeSet set = new IPRangeSet();
68 set.add(new IPRange("192.168.1.0/24"));
69 assertEquals("size", 1, set.size());
70 assertSubnets(set.getSubnets(), new IPRange("192.168.1.0/24"));
71 set.add(new IPRange("192.168.2.0/24"));
72 assertEquals("size", 1, set.size());
73 assertSubnets(set.getSubnets(), new IPRange("192.168.1.0/24"), new IPRange("192.168.2.0/24"));
74 }
75
76 @Test
77 public void testAddAdjacentJoin() throws UnknownHostException
78 {
79 IPRangeSet set = new IPRangeSet();
80 set.add(new IPRange("192.168.1.0/24"));
81 set.add(new IPRange("192.168.3.0/24"));
82 assertEquals("size", 2, set.size());
83 set.add(new IPRange("192.168.2.0/24"));
84 assertEquals("size", 1, set.size());
85 assertSubnets(set.getSubnets(), new IPRange("192.168.1.0/24"), new IPRange("192.168.2.0/23"));
86 }
87
88 @Test
89 public void testAddSame() throws UnknownHostException
90 {
91 IPRangeSet set = new IPRangeSet();
92 set.add(new IPRange("192.168.1.0/24"));
93 assertEquals("size", 1, set.size());
94 assertSubnets(set.getSubnets(), new IPRange("192.168.1.0/24"));
95 set.add(new IPRange("192.168.1.0/24"));
96 assertEquals("size", 1, set.size());
97 assertSubnets(set.getSubnets(), new IPRange("192.168.1.0/24"));
98 }
99
100 @Test
101 public void testAddLarger() throws UnknownHostException
102 {
103 IPRangeSet set = new IPRangeSet();
104 set.add(new IPRange("192.168.1.0/24"));
105 assertEquals("size", 1, set.size());
106 assertSubnets(set.getSubnets(), new IPRange("192.168.1.0/24"));
107 set.add(new IPRange("192.168.0.0/16"));
108 assertEquals("size", 1, set.size());
109 assertSubnets(set.getSubnets(), new IPRange("192.168.0.0/16"));
110 set.add(new IPRange("0.0.0.0/0"));
111 assertEquals("size", 1, set.size());
112 assertSubnets(set.getSubnets(), new IPRange("0.0.0.0/0"));
113 }
114
115 @Test
116 public void testAddLargerMulti() throws UnknownHostException
117 {
118 IPRangeSet set = new IPRangeSet();
119 set.add(new IPRange("192.168.1.0/24"));
120 set.add(new IPRange("10.0.1.0/24"));
121 set.add(new IPRange("255.255.255.255/32"));
122 assertEquals("size", 3, set.size());
123 set.add(new IPRange("0.0.0.0/0"));
124 assertEquals("size", 1, set.size());
125 assertSubnets(set.getSubnets(), new IPRange("0.0.0.0/0"));
126 }
127
128 @Test
129 public void testRemoveNothing() throws UnknownHostException
130 {
131 IPRangeSet set = new IPRangeSet();
132 set.add(new IPRange("192.168.1.0/24"));
133 set.remove(new IPRange("192.168.2.0/24"));
134 assertEquals("size", 1, set.size());
135 assertSubnets(set.getSubnets(), new IPRange("192.168.1.0/24"));
136 set.remove(new IPRange("10.0.1.0/24"));
137 assertEquals("size", 1, set.size());
138 assertSubnets(set.getSubnets(), new IPRange("192.168.1.0/24"));
139 }
140
141 @Test
142 public void testRemoveAll() throws UnknownHostException
143 {
144 IPRangeSet set = new IPRangeSet();
145 set.add(new IPRange("192.168.1.0/24"));
146 set.remove(new IPRange("192.168.0.0/16"));
147 assertEquals("size", 0, set.size());
148 assertSubnets(set.getSubnets());
149 set.add(new IPRange("192.168.1.0/24"));
150 set.add(new IPRange("10.0.1.0/24"));
151 set.add(new IPRange("255.255.255.255/32"));
152 assertEquals("size", 3, set.size());
153 set.remove(new IPRange("0.0.0.0/0"));
154 assertEquals("size", 0, set.size());
155 assertSubnets(set.getSubnets());
156 }
157
158 @Test
159 public void testRemoveOverlap() throws UnknownHostException
160 {
161 IPRangeSet set = new IPRangeSet();
162 set.add(new IPRange("192.168.1.0/24"));
163 set.add(new IPRange("192.168.2.0/24"));
164 set.remove(new IPRange("192.168.1.128", "192.168.2.127"));
165 assertEquals("size", 2, set.size());
166 assertSubnets(set.getSubnets(), new IPRange("192.168.1.0/25"), new IPRange("192.168.2.128/25"));
167 }
168
169 @Test
170 public void testFromStringSingle() throws UnknownHostException
171 {
172 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24");
173 assertEquals("size", 1, set.size());
174 assertSubnets(set.getSubnets(), new IPRange("192.168.1.0/24"));
175 }
176
177 @Test
178 public void testFromString() throws UnknownHostException
179 {
180 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24 fec1::/64 10.0.1.0/24 255.255.255.255");
181 assertEquals("size", 4, set.size());
182 assertSubnets(set.getSubnets(), new IPRange("10.0.1.0/24"), new IPRange("192.168.1.0/24"),
183 new IPRange("255.255.255.255/32"), new IPRange("fec1::/64"));
184 }
185
186 @Test
187 public void testFromStringInvalidRange() throws UnknownHostException
188 {
189 IPRangeSet set = IPRangeSet.fromString("192.168.1.0/65");
190 assertEquals("failed", null, set);
191 }
192 }