blob: a57b42af62f1b29d88d3acecf32bd768bf83296e [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
Andreas Wundsam12cea882013-12-15 15:05:50 -08003import static org.hamcrest.CoreMatchers.equalTo;
Yotam Harcholf3f11152013-09-05 16:47:16 -07004import static org.junit.Assert.assertArrayEquals;
5import static org.junit.Assert.assertEquals;
Andreas Wundsamacaf6502013-12-15 15:08:21 -08006import static org.junit.Assert.assertFalse;
7import static org.junit.Assert.assertNotNull;
Andreas Wundsame04c86f2013-11-05 17:13:18 -08008import static org.junit.Assert.assertThat;
Andreas Wundsamacaf6502013-12-15 15:08:21 -08009import static org.junit.Assert.assertTrue;
Yotam Harcholf3f11152013-09-05 16:47:16 -070010import static org.junit.Assert.fail;
11
Andreas Wundsame04c86f2013-11-05 17:13:18 -080012import org.hamcrest.CoreMatchers;
Yotam Harcholf3f11152013-09-05 16:47:16 -070013import org.jboss.netty.buffer.ChannelBuffers;
14import org.junit.Test;
15import org.projectfloodlight.openflow.exceptions.OFParseError;
16
Yotam Harchola289d552013-09-16 10:10:40 -070017public class IPv4AddressTest {
Yotam Harcholf3f11152013-09-05 16:47:16 -070018 byte[][] testAddresses = new byte[][] {
19 {0x01, 0x02, 0x03, 0x04 },
20 {127, 0, 0, 1},
21 {(byte) 192, (byte) 168, 0, 100 },
22 {(byte) 255, (byte) 255, (byte) 255, (byte) 255 }
23 };
24
25 String[] testStrings = {
26 "1.2.3.4",
27 "127.0.0.1",
28 "192.168.0.100",
29 "255.255.255.255"
30 };
31
32 int[] testInts = {
33 0x01020304,
34 0x7f000001,
35 (192 << 24) | (168 << 16) | 100,
36 0xffffffff
37 };
38
39 String[] invalidIPs = {
40 "",
41 ".",
42 "1.2..3.4",
43 "1.2.3.4.",
44 "257.11.225.1",
Gregor Maier7f987e62013-12-10 19:34:18 -080045 "256.11.225.1",
Yotam Harcholf3f11152013-09-05 16:47:16 -070046 "-1.2.3.4",
47 "1.2.3.4.5",
48 "1.x.3.4",
49 "1.2x.3.4"
50 };
51
52 String[] ipsWithMask = {
53 "1.2.3.4/24",
54 "192.168.130.140/255.255.192.0",
55 "127.0.0.1/8",
56 "8.8.8.8",
Gregor Maier7f987e62013-12-10 19:34:18 -080057 "8.8.8.8/32",
58 "0.0.0.0/0",
59 "192.168.130.140/255.0.255.0",
60 "1.2.3.4/0.127.0.255"
Yotam Harcholf3f11152013-09-05 16:47:16 -070061 };
62
63 boolean[] hasMask = {
64 true,
65 true,
66 true,
Andreas Wundsame04c86f2013-11-05 17:13:18 -080067 false,
Gregor Maier7f987e62013-12-10 19:34:18 -080068 false,
69 true,
70 true,
Andreas Wundsame04c86f2013-11-05 17:13:18 -080071 true
Yotam Harcholf3f11152013-09-05 16:47:16 -070072 };
73
74 byte[][][] ipsWithMaskValues = {
75 new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00 } },
76 new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xC0, (byte)0x00 } },
77 new byte[][] { new byte[] { (byte)0x7F, (byte)0x00, (byte)0x00, (byte)0x01 }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0x00 } },
Gregor Maier7f987e62013-12-10 19:34:18 -080078 new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } },
79 new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } },
80 new byte[][] { new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 } },
81 new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0xFF, (byte)0x00 } },
82 new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0x00, (byte)0x7F, (byte)0x00, (byte)0xFF } }
83 };
84
85 int[] ipsWithMaskLengths = {
86 24,
87 18,
88 8,
89 32,
90 32,
91 0,
92 -1,
93 -1
94 };
95
96 String[] invalidIpsWithMask = {
97 "asdf",
98 "1.2.3.4/33",
99 "1.2.3.4/34",
100 "1.2.3.4/-1",
101 "1.2.3.4/256.0.0.0",
102 "1.256.3.4/255.255.0.0",
103 "1.2.3.4/255.255.0.0.0",
Yotam Harcholf3f11152013-09-05 16:47:16 -0700104 };
105
Andreas Wundsam12cea882013-12-15 15:05:50 -0800106 @Test
Aditya Vaja56b8b182014-03-11 13:13:58 -0700107 public void testLogicalOperatorsBroadcast() {
108 assertTrue(IPv4Address.NO_MASK.not().equals(IPv4Address.FULL_MASK));
109 assertTrue(IPv4Address.NO_MASK.or(IPv4Address.FULL_MASK).
110 equals(IPv4Address.NO_MASK));
111 assertTrue(IPv4Address.NO_MASK.and(IPv4Address.FULL_MASK).
112 equals(IPv4Address.FULL_MASK));
113
114 assertTrue(IPv4Address.NO_MASK.isBroadcast());
115 assertTrue(!IPv4Address.FULL_MASK.isBroadcast());
116 }
117
118 @Test
119 public void testMaskedSubnetBroadcast() {
120 assertTrue(IPv4AddressWithMask.of("10.10.10.1/24")
121 .getSubnetBroadcastAddress()
122 .equals(IPv4Address.of("10.10.10.255")));
123 assertTrue(IPv4AddressWithMask.of("10.10.10.1/24")
124 .isSubnetBroadcastAddress(IPv4Address.of("10.10.10.255")));
125 assertTrue(!IPv4AddressWithMask.of("10.10.10.1/24")
126 .isSubnetBroadcastAddress(IPv4Address.of("10.10.10.254")));
127 }
128
129 @Test
Andreas Wundsam12cea882013-12-15 15:05:50 -0800130 public void testMaskedMatchesCidr() {
131 IPv4AddressWithMask slash28 = IPv4AddressWithMask.of("10.0.42.16/28");
132
133 String[] notContained = {"0.0.0.0", "11.0.42.16", "10.0.41.1", "10.0.42.0", "10.0.42.15",
134 "10.0.42.32", "255.255.255.255" };
135
136 for(String n: notContained) {
137 assertThat(String.format("slash 28 %s should not contain address %s",
138 slash28, n),
139 slash28.matches(IPv4Address.of(n)), equalTo(false));
140 }
141 for(int i=16; i < 32; i++) {
142 IPv4Address c = IPv4Address.of(String.format("10.0.42.%d", i));
143 assertThat(String.format("slash 28 %s should contain address %s",
144 slash28, c),
145 slash28.matches(c), equalTo(true));
146 }
147 }
148
149 @Test
150 public void testMaskedMatchesArbitrary() {
151 // irregular octect on the 3rd bitmask requires '1'bit to be set
152 // 4 bit unset, all others arbitrary
153 IPv4AddressWithMask slash28 = IPv4AddressWithMask.of("1.2.1.4/255.255.5.255");
154
155 String[] notContained = {"0.0.0.0", "1.2.3.5", "1.2.3.3",
156 "1.2.0.4", "1.2.2.4", "1.2.4.4", "1.2.5.4", "1.2.6.4", "1.2.7.4",
157 "1.2.8.4", "1.2.12.4", "1.2.13.4"
158 };
159 String[] contained = {"1.2.1.4", "1.2.3.4", "1.2.9.4", "1.2.11.4", "1.2.251.4",
160 };
161
162 for(String n: notContained) {
163 assertThat(String.format("slash 28 %s should not contain address %s",
164 slash28, n),
165 slash28.matches(IPv4Address.of(n)), equalTo(false));
166 }
167 for(String c: contained) {
168 IPv4Address addr = IPv4Address.of(c);
169 assertThat(String.format("slash 28 %s should contain address %s",
170 slash28, addr),
171 slash28.matches(addr), equalTo(true));
172 }
173
174 }
175
Yotam Harcholf3f11152013-09-05 16:47:16 -0700176
177 @Test
Gregor Maier5615b6c2013-12-11 22:29:07 -0800178 public void testConstants() {
179 byte[] zeros = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
180 byte[] ones = { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF };
181 // Make sure class initializtation and static assignment don't get
182 // messed up. Test everything twice for cached values
183 assertTrue(IPv4Address.NONE.isCidrMask());
184 assertEquals(0, IPv4Address.NONE.asCidrMaskLength());
185 assertArrayEquals(zeros, IPv4Address.NONE.getBytes());
186 assertTrue(IPv4Address.NONE.isCidrMask());
187 assertEquals(0, IPv4Address.NONE.asCidrMaskLength());
188 assertArrayEquals(zeros, IPv4Address.NONE.getBytes());
189
190 assertTrue(IPv4Address.NO_MASK.isCidrMask());
191 assertEquals(32, IPv4Address.NO_MASK.asCidrMaskLength());
192 assertArrayEquals(ones, IPv4Address.NO_MASK.getBytes());
193 assertTrue(IPv4Address.NO_MASK.isCidrMask());
194 assertEquals(32, IPv4Address.NO_MASK.asCidrMaskLength());
195 assertArrayEquals(ones, IPv4Address.NO_MASK.getBytes());
196
197 assertTrue(IPv4Address.FULL_MASK.isCidrMask());
198 assertEquals(0, IPv4Address.FULL_MASK.asCidrMaskLength());
199 assertArrayEquals(zeros, IPv4Address.FULL_MASK.getBytes());
200 assertTrue(IPv4Address.FULL_MASK.isCidrMask());
201 assertEquals(0, IPv4Address.FULL_MASK.asCidrMaskLength());
202 assertArrayEquals(zeros, IPv4Address.FULL_MASK.getBytes());
203 }
204
205
206 @Test
Yotam Harcholf3f11152013-09-05 16:47:16 -0700207 public void testOfString() {
208 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700209 IPv4Address ip = IPv4Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700210 assertEquals(testInts[i], ip.getInt());
211 assertArrayEquals(testAddresses[i], ip.getBytes());
212 assertEquals(testStrings[i], ip.toString());
213 }
214 }
215
216 @Test
217 public void testOfByteArray() {
218 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700219 IPv4Address ip = IPv4Address.of(testAddresses[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700220 assertEquals(testInts[i], ip.getInt());
221 assertArrayEquals(testAddresses[i], ip.getBytes());
222 assertEquals(testStrings[i], ip.toString());
223 }
224 }
225
226 @Test
227 public void testReadFrom() throws OFParseError {
228 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700229 IPv4Address ip = IPv4Address.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i]));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700230 assertEquals(testInts[i], ip.getInt());
231 assertArrayEquals(testAddresses[i], ip.getBytes());
232 assertEquals(testStrings[i], ip.toString());
233 }
234 }
235
236
237 @Test
238 public void testInvalidIPs() throws OFParseError {
239 for(String invalid : invalidIPs) {
240 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700241 IPv4Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700242 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
243 } catch(IllegalArgumentException e) {
244 // ok
245 }
246 }
247 }
248
249 @Test
250 public void testOfMasked() throws OFParseError {
251 for (int i = 0; i < ipsWithMask.length; i++) {
Yotam Harchol9a617aa2013-09-16 14:10:17 -0700252 IPv4AddressWithMask value = IPv4AddressWithMask.of(ipsWithMask[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700253 if (!hasMask[i]) {
Yotam Harchola289d552013-09-16 10:10:40 -0700254 IPv4Address ip = value.getValue();
Yotam Harcholf3f11152013-09-05 16:47:16 -0700255 assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700256 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800257 IPv4Address mask = value.getMask();
Gregor Maier5615b6c2013-12-11 22:29:07 -0800258 if (ipsWithMaskLengths[i] == -1) {
259 assertFalse(mask.isCidrMask());
260 try {
261 mask.asCidrMaskLength();
262 fail("Expected IllegalStateException not thrown");
263 } catch(IllegalStateException e) {
264 //expected
265 }
266 } else {
267 assertTrue(mask.isCidrMask());
268 assertEquals(ipsWithMaskLengths[i], mask.asCidrMaskLength());
269 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800270 assertArrayEquals(ipsWithMaskValues[i][1], mask.getBytes());
271 byte[] ipBytes = new byte[4];
272 System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4);
273 assertEquals(ipBytes.length, value.getValue().getBytes().length);
274 for (int j = 0; j < ipBytes.length; j++) {
275 ipBytes[j] &= ipsWithMaskValues[i][1][j];
276 }
277
278 assertArrayEquals(ipBytes, value.getValue().getBytes());
279 assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value),
280 value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1]));
281 }
282 }
283
284 @Test
285 public void testOfMaskedInvalid() throws Exception {
286 for(String invalid : invalidIpsWithMask) {
287 try {
288 IPv4Address.of(invalid);
289 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
290 } catch(IllegalArgumentException e) {
291 // ok
292 }
293 }
294 }
295
296 @Test
297 public void testSuperclass() throws Exception {
298 for(String ipString: testStrings) {
299 IPAddress<?> superIp = IPAddress.of(ipString);
300 assertEquals(IPVersion.IPv4, superIp.getIpVersion());
301 assertEquals(IPv4Address.of(ipString), superIp);
302 }
303
304 for(String ipMaskedString: ipsWithMask) {
305 IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
306 assertEquals(IPVersion.IPv4, superIp.getIpVersion());
307 assertEquals(IPv4AddressWithMask.of(ipMaskedString), superIp);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700308 }
309 }
Gregor Maier1ff55972013-12-11 02:22:56 -0800310
311 @Test
312 public void testOfExceptions() {
313 // We check if the message of a caught NPE is set to a useful message
314 // as a hacky way of verifying that we got an NPE thrown by use rather
315 // than one the JVM created for a null access.
316 try {
317 String s = null;
318 IPv4Address.of(s);
319 fail("Should have thrown NullPointerException");
320 } catch (NullPointerException e) {
321 assertNotNull(e.getMessage());
322 }
323 try {
324 byte[] b = null;
325 IPv4Address.of(b);
326 fail("Should have thrown NullPointerException");
327 } catch (NullPointerException e) {
328 assertNotNull(e.getMessage());
329 }
330 try {
331 byte[] b = new byte[3];
332 IPv4Address.of(b);
333 fail("Should have thrown IllegalArgumentException");
334 } catch (IllegalArgumentException e) {
335 // expected
336 }
337 try {
338 byte[] b = new byte[5];
339 IPv4Address.of(b);
340 fail("Should have thrown IllegalArgumentException");
341 } catch (IllegalArgumentException e) {
342 // expected
343 }
344 try {
345 IPv4AddressWithMask.of(null);
346 fail("Should have thrown NullPointerException");
347 } catch (NullPointerException e) {
348 assertNotNull(e.getMessage());
349 }
350 try {
351 IPv4AddressWithMask.of(IPv4Address.of("1.2.3.4"), null);
352 fail("Should have thrown NullPointerException");
353 } catch (NullPointerException e) {
354 assertNotNull(e.getMessage());
355 }
356 try {
357 IPv4AddressWithMask.of(null, IPv4Address.of("255.0.0.0"));
358 fail("Should have thrown NullPointerException");
359 } catch (NullPointerException e) {
360 assertNotNull(e.getMessage());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700361 }
Aditya Vajab21043d2014-03-11 13:34:06 -0700362 try {
363 IPv4AddressWithMask.of(IPv4Address.of("10.10.10.0"),
364 IPv4Address.of("255.0.255.0"))
365 .getSubnetBroadcastAddress();
366 fail("Should have thrown IllegalArgumentException");
367 } catch (IllegalArgumentException e) {
368 assertNotNull(e.getMessage());
369 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700370 }
371}