blob: 52f2487231f3d4ba159f1b955963212f67ac64b9 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
Gregor Maier5615b6c2013-12-11 22:29:07 -08003import static org.junit.Assert.*;
Yotam Harcholf3f11152013-09-05 16:47:16 -07004
5import java.net.Inet6Address;
6import java.net.InetAddress;
7import java.net.UnknownHostException;
8
Andreas Wundsame04c86f2013-11-05 17:13:18 -08009import org.hamcrest.CoreMatchers;
Ronald Li9d777a52015-02-07 06:19:29 -080010import org.hamcrest.Matchers;
Yotam Harcholf3f11152013-09-05 16:47:16 -070011import org.jboss.netty.buffer.ChannelBuffers;
12import org.junit.Test;
13import org.projectfloodlight.openflow.exceptions.OFParseError;
14
Andreas Wundsame04c86f2013-11-05 17:13:18 -080015import com.google.common.io.BaseEncoding;
16
Yotam Harchola289d552013-09-16 10:10:40 -070017public class IPv6AddressTest {
Yotam Harcholf3f11152013-09-05 16:47:16 -070018
19 String[] testStrings = {
20 "::",
21 "::1",
22 "ffe0::",
23 "1:2:3:4:5:6:7:8"
24 };
25
Yotam Harcholf3f11152013-09-05 16:47:16 -070026
Andreas Wundsame04c86f2013-11-05 17:13:18 -080027 private final BaseEncoding hex = BaseEncoding.base16().omitPadding().lowerCase();
Yotam Harcholf3f11152013-09-05 16:47:16 -070028
Andreas Wundsame04c86f2013-11-05 17:13:18 -080029 private class WithMaskTaskCase {
30 final String input;
31 boolean hasMask;
Gregor Maier7f987e62013-12-10 19:34:18 -080032 int expectedMaskLength = 128;
Andreas Wundsame04c86f2013-11-05 17:13:18 -080033 byte[] expectedMask = hex.decode("ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff".replaceAll(" ", ""));
34
35 public WithMaskTaskCase(String input) {
36 super();
37 this.input = input;
38 }
39
Andreas Wundsame04c86f2013-11-05 17:13:18 -080040 public WithMaskTaskCase maskHex(String string) {
41 string = string.replaceAll(" ", "");
42 this.hasMask = true;
43 expectedMask = hex.decode(string);
44 return this;
45 }
46
Gregor Maier7f987e62013-12-10 19:34:18 -080047 public WithMaskTaskCase expectedMaskLength(int expectedLength) {
48 this.expectedMaskLength = expectedLength;
49 return this;
50 }
51
Andreas Wundsame04c86f2013-11-05 17:13:18 -080052 }
53
54 WithMaskTaskCase[] withMasks = new WithMaskTaskCase[] {
55 new WithMaskTaskCase("1::1/80")
Gregor Maier7f987e62013-12-10 19:34:18 -080056 .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00")
57 .expectedMaskLength(80),
Andreas Wundsame04c86f2013-11-05 17:13:18 -080058
59 new WithMaskTaskCase("ffff:ffee:1::/ff00:ff00:ff00:ff00::")
Gregor Maier7f987e62013-12-10 19:34:18 -080060 .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00")
61 .expectedMaskLength(-1),
Gregor Maiercef9e3e2013-12-11 22:41:36 -080062 new WithMaskTaskCase("1:2:3:4:5:6:7:8/1::ff00:ff00")
63 .maskHex("00 01 00 00 00 00 00 00 00 00 00 00 ff 00 ff 00")
64 .expectedMaskLength(-1),
65 new WithMaskTaskCase("1:2:3:4:5:6:7:8/::ff00:ff00")
66 .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 ff 00 ff 00")
67 .expectedMaskLength(-1),
68 new WithMaskTaskCase("1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff::ff00:ff00")
69 .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 ff 00 ff 00")
70 .expectedMaskLength(-1),
Andreas Wundsame04c86f2013-11-05 17:13:18 -080071 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
72 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
73 new WithMaskTaskCase("1:2:3:4:5:6:7:8/128"),
74 new WithMaskTaskCase("::/0")
75 .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")
Gregor Maier7f987e62013-12-10 19:34:18 -080076 .expectedMaskLength(0),
Yotam Harcholf3f11152013-09-05 16:47:16 -070077 };
78
79 @Test
Aditya Vaja56b8b182014-03-11 13:13:58 -070080 public void testLogicalOperatorsBroadcast() {
81 assertTrue(IPv6Address.NO_MASK.not().equals(IPv6Address.FULL_MASK));
82 assertTrue(IPv6Address.NO_MASK.or(IPv6Address.FULL_MASK).
83 equals(IPv6Address.NO_MASK));
84 assertTrue(IPv6Address.NO_MASK.and(IPv6Address.FULL_MASK).
85 equals(IPv6Address.FULL_MASK));
86
87 assertTrue(IPv6Address.NO_MASK.isBroadcast());
88 assertTrue(!IPv6Address.FULL_MASK.isBroadcast());
89 }
90
91 @Test
92 public void testMaskedSubnetBroadcast() {
93 assertTrue(IPv6AddressWithMask.of("10:10::1/112")
94 .getSubnetBroadcastAddress()
95 .equals(IPv6Address.of("10:10::ffff")));
96 assertTrue(IPv6AddressWithMask.of("10:10::1/112")
97 .isSubnetBroadcastAddress(IPv6Address.of("10:10::ffff")));
98 assertTrue(!IPv6AddressWithMask.of("10:10::1/112")
99 .isSubnetBroadcastAddress(IPv6Address.of("10:10::fffd")));
100 }
101
102 @Test
Gregor Maier5615b6c2013-12-11 22:29:07 -0800103 public void testConstants() {
104 byte[] zeros = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
105 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
106 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
107 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
108 byte[] ones = { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
109 (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
110 (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
111 (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF };
112 // Make sure class initializtation and static assignment don't get
113 // messed up. Test everything twice for cached values
114 assertTrue(IPv6Address.NONE.isCidrMask());
115 assertEquals(0, IPv6Address.NONE.asCidrMaskLength());
116 assertArrayEquals(zeros, IPv6Address.NONE.getBytes());
117 assertTrue(IPv6Address.NONE.isCidrMask());
118 assertEquals(0, IPv6Address.NONE.asCidrMaskLength());
119 assertArrayEquals(zeros, IPv6Address.NONE.getBytes());
120
121 assertTrue(IPv6Address.NO_MASK.isCidrMask());
122 assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength());
123 assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes());
124 assertTrue(IPv6Address.NO_MASK.isCidrMask());
125 assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength());
126 assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes());
127
128 assertTrue(IPv6Address.FULL_MASK.isCidrMask());
129 assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength());
130 assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes());
131 assertTrue(IPv6Address.FULL_MASK.isCidrMask());
132 assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength());
133 assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes());
134 }
135
136 @Test
Yotam Harcholf3f11152013-09-05 16:47:16 -0700137 public void testMasked() throws UnknownHostException {
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800138 for(WithMaskTaskCase w: withMasks) {
139 IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input);
140 if (!w.hasMask) {
Yotam Harchola289d552013-09-16 10:10:40 -0700141 IPv6Address ip = value.getValue();
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800142 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700143
144 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800145 assertEquals(w.input.split("/")[0], ip.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700146 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800147 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
148
Gregor Maier5615b6c2013-12-11 22:29:07 -0800149 if (w.expectedMaskLength == -1) {
150 assertFalse(value.getMask().isCidrMask());
151 try {
152 value.getMask().asCidrMaskLength();
153 fail("Expected IllegalStateException not thrown");
154 } catch(IllegalStateException e) {
155 //expected
156 }
157 } else {
158 assertTrue(value.getMask().isCidrMask());
159 assertEquals("Input " + w.input, w.expectedMaskLength,
160 value.getMask().asCidrMaskLength());
161 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800162
163 byte[] address = inetAddress.getAddress();
164 assertEquals(address.length, value.getValue().getBytes().length);
165
166 for (int j = 0; j < address.length; j++) {
167 address[j] &= w.expectedMask[j];
168 }
169
170 assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address));
171 assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask));
172 }
173 for (int i = 0; i <= 128; i++) {
174 String ipString = String.format("8001:2::1/%d", i);
175 IPv6AddressWithMask value = IPv6AddressWithMask.of(ipString);
176 assertEquals("Input " + ipString, i, value.getMask().asCidrMaskLength());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700177 }
178 }
179
180
181 @Test
182 public void testOfString() throws UnknownHostException {
183 for(int i=0; i < testStrings.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700184 IPv6Address ip = IPv6Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700185 InetAddress inetAddress = InetAddress.getByName(testStrings[i]);
186
187 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
188 assertEquals(testStrings[i], ip.toString());
189 }
190 }
191
192 @Test
193 public void testOfByteArray() throws UnknownHostException {
194 for(int i=0; i < testStrings.length; i++ ) {
195 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700196 IPv6Address ip = IPv6Address.of(bytes);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700197 assertEquals(testStrings[i], ip.toString());
198 assertArrayEquals(bytes, ip.getBytes());
199 }
200 }
201
Ronald Lifea17892014-07-03 18:36:06 -0700202 private static void testOfCidrMaskLengthHelper(
203 int cidrMaskLength, String ipStr) throws UnknownHostException {
204 byte[] ba0 = IPv6Address.ofCidrMaskLength(cidrMaskLength).getBytes();
205 byte[] ba1 = Inet6Address.getByName(ipStr).getAddress();
206 assertArrayEquals(ba0, ba1);
207 }
208
209 @Test
210 public void testOfCidrMaskLength() throws UnknownHostException {
211 for (int i = 0; i <= 128; i++) {
212 assertTrue(IPv6Address.ofCidrMaskLength(i).isCidrMask());
213 assertEquals(IPv6Address.ofCidrMaskLength(i).asCidrMaskLength(), i);
214 }
215 testOfCidrMaskLengthHelper(0, "::");
216 testOfCidrMaskLengthHelper(1, "8000::");
217 testOfCidrMaskLengthHelper(2, "c000::");
218 testOfCidrMaskLengthHelper(8, "ff00::");
219 testOfCidrMaskLengthHelper(16, "ffff::");
220 testOfCidrMaskLengthHelper(17, "ffff:8000::");
221 testOfCidrMaskLengthHelper(31, "ffff:fffe::");
222 testOfCidrMaskLengthHelper(32, "ffff:ffff::");
223 testOfCidrMaskLengthHelper(33, "ffff:ffff:8000::");
224 testOfCidrMaskLengthHelper(46, "ffff:ffff:fffc::");
225 testOfCidrMaskLengthHelper(48, "ffff:ffff:ffff::");
226 testOfCidrMaskLengthHelper(55, "ffff:ffff:ffff:fe00::");
227 testOfCidrMaskLengthHelper(56, "ffff:ffff:ffff:ff00::");
228 testOfCidrMaskLengthHelper(59, "ffff:ffff:ffff:ffe0::");
229 testOfCidrMaskLengthHelper(63, "ffff:ffff:ffff:fffe::");
230 testOfCidrMaskLengthHelper(64, "ffff:ffff:ffff:ffff::");
231 testOfCidrMaskLengthHelper(65, "ffff:ffff:ffff:ffff:8000::");
232 testOfCidrMaskLengthHelper(67, "ffff:ffff:ffff:ffff:e000::");
233 testOfCidrMaskLengthHelper(100, "ffff:ffff:ffff:ffff:ffff:ffff:f000::");
234 testOfCidrMaskLengthHelper(101, "ffff:ffff:ffff:ffff:ffff:ffff:f800::");
235 testOfCidrMaskLengthHelper(126, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc");
236 testOfCidrMaskLengthHelper(127, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe");
237 testOfCidrMaskLengthHelper(128, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
238 }
239
Yotam Harcholf3f11152013-09-05 16:47:16 -0700240 @Test
Ronald Libbf01942014-07-07 17:00:13 -0700241 public void testWithMask() throws Exception {
242 // Sanity tests for the withMask*() syntactic sugars
243
244 IPv6Address original = IPv6Address.of("fd12:3456:ABCD:7890::1");
245 IPv6Address expectedValue = IPv6Address.of("fd12:3456:ABCD::");
246 IPv6Address expectedMask = IPv6Address.of("ffff:ffff:ffff::");
247
248 IPv6AddressWithMask v;
249
250 v = original.withMask(IPv6Address.of(new byte[] {
251 -1, -1, -1, -1, -1, -1, 0, 0,
252 0, 0, 0, 0, 0, 0, 0, 0 }));
253 assertEquals(v.getValue(), expectedValue);
254 assertEquals(v.getMask(), expectedMask);
255
256 v = original.withMask(IPv6Address.of(
257 0xFFFF_FFFF_FFFF_0000L, 0x0000_0000_0000_0000L));
258 assertEquals(v.getValue(), expectedValue);
259 assertEquals(v.getMask(), expectedMask);
260
261 v = original.withMask(IPv6Address.of("ffff:ffff:ffff::"));
262 assertEquals(v.getValue(), expectedValue);
263 assertEquals(v.getMask(), expectedMask);
264
265 Inet6Address i6a = (Inet6Address) InetAddress.getByName("ffff:ffff:ffff::");
266 v = original.withMask(IPv6Address.of(i6a));
267 assertEquals(v.getValue(), expectedValue);
268 assertEquals(v.getMask(), expectedMask);
269
270 v = original.withMaskOfLength(48);
271 assertEquals(v.getValue(), expectedValue);
272 assertEquals(v.getMask(), expectedMask);
273 }
274
275 @Test
Yotam Harcholf3f11152013-09-05 16:47:16 -0700276 public void testReadFrom() throws OFParseError, UnknownHostException {
277 for(int i=0; i < testStrings.length; i++ ) {
278 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700279 IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700280 assertEquals(testStrings[i], ip.toString());
281 assertArrayEquals(bytes, ip.getBytes());
282 }
283 }
284
285 String[] invalidIPs = {
286 "",
287 ":",
288 "1:2:3:4:5:6:7:8:9",
289 "1:2:3:4:5:6:7:8:",
290 "1:2:3:4:5:6:7:8g",
291 "1:2:3:",
292 "12345::",
293 "1::3::8",
294 "::3::"
295 };
296
297 @Test
298 public void testInvalidIPs() throws OFParseError {
299 for(String invalid : invalidIPs) {
300 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700301 IPv6Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700302 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
303 } catch(IllegalArgumentException e) {
304 // ok
305 }
306 }
307 }
308
309 @Test
310 public void testZeroCompression() throws OFParseError {
Yotam Harchola289d552013-09-16 10:10:40 -0700311 assertEquals("::", IPv6Address.of("::").toString(true, false));
312 assertEquals("0:0:0:0:0:0:0:0", IPv6Address.of("::").toString(false, false));
313 assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6Address.of("::").toString(false, true));
314 assertEquals("1::4:5:6:0:8", IPv6Address.of("1:0:0:4:5:6:0:8").toString(true, false));
315 assertEquals("1:0:0:4::8", IPv6Address.of("1:0:0:4:0:0:0:8").toString(true, false));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700316 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800317
318 @Test
319 public void testSuperclass() throws Exception {
320 for(String ipString: testStrings) {
321 IPAddress<?> superIp = IPAddress.of(ipString);
322 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
323 assertEquals(IPv6Address.of(ipString), superIp);
324 }
325
326 for(WithMaskTaskCase w: withMasks) {
327 String ipMaskedString = w.input;
328 IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
329 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
330 assertEquals(IPv6AddressWithMask.of(ipMaskedString), superIp);
331 }
332 }
Gregor Maier1ff55972013-12-11 02:22:56 -0800333
334 @Test
Ronald Li9d777a52015-02-07 06:19:29 -0800335 public void testCompareTo() {
336 assertThat(
337 IPv6Address.of("fc00::1").compareTo(IPv6Address.of("fc00::2")),
338 Matchers.lessThan(0));
339 assertThat(
340 IPv6Address.of("::1").compareTo(IPv6Address.of("fc00::")),
341 Matchers.lessThan(0));
342
343 // Make sure that unsigned comparison is used on the first 64 bits
344 assertThat(
345 IPv6Address.of("fc00::1").compareTo(IPv6Address.of("1234::3")),
346 Matchers.greaterThan(0));
347
348 // Make sure that unsigned comparison is used on the next 64 bits
349 assertThat(
350 IPv6Address.of("::8000:0:0:1").compareTo(IPv6Address.of("::1")),
351 Matchers.greaterThan(0));
352 }
353
354 @Test
Gregor Maier1ff55972013-12-11 02:22:56 -0800355 public void testOfExceptions() throws Exception {
356 try {
357 IPv6AddressWithMask.of(null);
358 fail("Should have thrown NullPointerException");
359 } catch (NullPointerException e) {
360 assertNotNull(e.getMessage());
361 }
362 try {
363 String s = null;
364 IPv6Address.of(s);
365 fail("Should have thrown NullPointerException");
366 } catch (NullPointerException e) {
367 assertNotNull(e.getMessage());
368 }
369 try {
370 byte[] b = null;
371 IPv6Address.of(b);
372 fail("Should have thrown NullPointerException");
373 } catch (NullPointerException e) {
374 assertNotNull(e.getMessage());
375 }
376 try {
377 byte[] b = new byte[7];
378 IPv6Address.of(b);
379 fail("Should have thrown IllegalArgumentException");
380 } catch (IllegalArgumentException e) {
381 // expected
382 }
383 try {
384 byte[] b = new byte[9];
385 IPv6Address.of(b);
386 fail("Should have thrown IllegalArgumentException");
387 } catch (IllegalArgumentException e) {
388 // expected
389 }
390 try {
391 IPv6AddressWithMask.of(IPv6Address.of("1::"), null);
392 fail("Should have thrown NullPointerException");
393 } catch (NullPointerException e) {
394 assertNotNull(e.getMessage());
395 }
396 try {
397 IPv6AddressWithMask.of(null, IPv6Address.of("255::"));
398 fail("Should have thrown NullPointerException");
399 } catch (NullPointerException e) {
400 assertNotNull(e.getMessage());
401 }
Aditya Vajab21043d2014-03-11 13:34:06 -0700402 try {
403 IPv6AddressWithMask.of(IPv6Address.of("10:10::0"),
404 IPv6Address.of("ffff:0:ffff::"))
405 .getSubnetBroadcastAddress();
406 fail("Should have thrown IllegalArgumentException");
407 } catch (IllegalArgumentException e) {
408 assertNotNull(e.getMessage());
409 }
Ronald Lifea17892014-07-03 18:36:06 -0700410 try {
411 IPv6Address.ofCidrMaskLength(-1);
412 fail("Should have thrown IllegalArgumentException");
413 } catch (IllegalArgumentException e) {
414 assertNotNull(e.getMessage());
415 }
416 try {
417 IPv6Address.ofCidrMaskLength(129);
418 fail("Should have thrown IllegalArgumentException");
419 } catch (IllegalArgumentException e) {
420 assertNotNull(e.getMessage());
421 }
Gregor Maier1ff55972013-12-11 02:22:56 -0800422 }
Rich Lanebae30f92014-08-27 12:13:20 -0700423
424 @Test
425 public void testZoneId() throws OFParseError {
426 assertEquals("::", IPv6Address.of("::%eth0").toString(true, false));
427 assertEquals("1:0:0:4::8", IPv6Address.of("1:0:0:4:0:0:0:8%2").toString(true, false));
428 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700429}