blob: a94f443826b56b81a4e477c40e5cdcfac7ab1707 [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::",
Ronald Lib90552c2015-05-15 16:03:32 -070023 "1:2:3:4:5:6:7:8",
24 "8091:a2b3:c4d5:e6f7:8495:a6b7:c1d2:e3d4",
Yotam Harcholf3f11152013-09-05 16:47:16 -070025 };
26
Yotam Harcholf3f11152013-09-05 16:47:16 -070027
Andreas Wundsame04c86f2013-11-05 17:13:18 -080028 private final BaseEncoding hex = BaseEncoding.base16().omitPadding().lowerCase();
Yotam Harcholf3f11152013-09-05 16:47:16 -070029
Andreas Wundsame04c86f2013-11-05 17:13:18 -080030 private class WithMaskTaskCase {
31 final String input;
32 boolean hasMask;
Gregor Maier7f987e62013-12-10 19:34:18 -080033 int expectedMaskLength = 128;
Andreas Wundsame04c86f2013-11-05 17:13:18 -080034 byte[] expectedMask = hex.decode("ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff".replaceAll(" ", ""));
35
36 public WithMaskTaskCase(String input) {
37 super();
38 this.input = input;
39 }
40
Andreas Wundsame04c86f2013-11-05 17:13:18 -080041 public WithMaskTaskCase maskHex(String string) {
42 string = string.replaceAll(" ", "");
43 this.hasMask = true;
44 expectedMask = hex.decode(string);
45 return this;
46 }
47
Gregor Maier7f987e62013-12-10 19:34:18 -080048 public WithMaskTaskCase expectedMaskLength(int expectedLength) {
49 this.expectedMaskLength = expectedLength;
50 return this;
51 }
52
Andreas Wundsame04c86f2013-11-05 17:13:18 -080053 }
54
55 WithMaskTaskCase[] withMasks = new WithMaskTaskCase[] {
56 new WithMaskTaskCase("1::1/80")
Gregor Maier7f987e62013-12-10 19:34:18 -080057 .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00")
58 .expectedMaskLength(80),
Andreas Wundsame04c86f2013-11-05 17:13:18 -080059
60 new WithMaskTaskCase("ffff:ffee:1::/ff00:ff00:ff00:ff00::")
Gregor Maier7f987e62013-12-10 19:34:18 -080061 .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00")
62 .expectedMaskLength(-1),
Gregor Maiercef9e3e2013-12-11 22:41:36 -080063 new WithMaskTaskCase("1:2:3:4:5:6:7:8/1::ff00:ff00")
64 .maskHex("00 01 00 00 00 00 00 00 00 00 00 00 ff 00 ff 00")
65 .expectedMaskLength(-1),
66 new WithMaskTaskCase("1:2:3:4:5:6:7:8/::ff00:ff00")
67 .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 ff 00 ff 00")
68 .expectedMaskLength(-1),
69 new WithMaskTaskCase("1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff::ff00:ff00")
70 .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 ff 00 ff 00")
71 .expectedMaskLength(-1),
Andreas Wundsame04c86f2013-11-05 17:13:18 -080072 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
73 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
74 new WithMaskTaskCase("1:2:3:4:5:6:7:8/128"),
75 new WithMaskTaskCase("::/0")
76 .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")
Gregor Maier7f987e62013-12-10 19:34:18 -080077 .expectedMaskLength(0),
Yotam Harcholf3f11152013-09-05 16:47:16 -070078 };
79
80 @Test
Aditya Vaja56b8b182014-03-11 13:13:58 -070081 public void testLogicalOperatorsBroadcast() {
82 assertTrue(IPv6Address.NO_MASK.not().equals(IPv6Address.FULL_MASK));
83 assertTrue(IPv6Address.NO_MASK.or(IPv6Address.FULL_MASK).
84 equals(IPv6Address.NO_MASK));
85 assertTrue(IPv6Address.NO_MASK.and(IPv6Address.FULL_MASK).
86 equals(IPv6Address.FULL_MASK));
87
88 assertTrue(IPv6Address.NO_MASK.isBroadcast());
89 assertTrue(!IPv6Address.FULL_MASK.isBroadcast());
90 }
91
92 @Test
93 public void testMaskedSubnetBroadcast() {
94 assertTrue(IPv6AddressWithMask.of("10:10::1/112")
95 .getSubnetBroadcastAddress()
96 .equals(IPv6Address.of("10:10::ffff")));
97 assertTrue(IPv6AddressWithMask.of("10:10::1/112")
98 .isSubnetBroadcastAddress(IPv6Address.of("10:10::ffff")));
99 assertTrue(!IPv6AddressWithMask.of("10:10::1/112")
100 .isSubnetBroadcastAddress(IPv6Address.of("10:10::fffd")));
101 }
102
103 @Test
Gregor Maier5615b6c2013-12-11 22:29:07 -0800104 public void testConstants() {
105 byte[] zeros = { (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)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
109 byte[] ones = { (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 (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF };
113 // Make sure class initializtation and static assignment don't get
114 // messed up. Test everything twice for cached values
115 assertTrue(IPv6Address.NONE.isCidrMask());
116 assertEquals(0, IPv6Address.NONE.asCidrMaskLength());
117 assertArrayEquals(zeros, IPv6Address.NONE.getBytes());
118 assertTrue(IPv6Address.NONE.isCidrMask());
119 assertEquals(0, IPv6Address.NONE.asCidrMaskLength());
120 assertArrayEquals(zeros, IPv6Address.NONE.getBytes());
121
122 assertTrue(IPv6Address.NO_MASK.isCidrMask());
123 assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength());
124 assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes());
125 assertTrue(IPv6Address.NO_MASK.isCidrMask());
126 assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength());
127 assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes());
128
129 assertTrue(IPv6Address.FULL_MASK.isCidrMask());
130 assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength());
131 assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes());
132 assertTrue(IPv6Address.FULL_MASK.isCidrMask());
133 assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength());
134 assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes());
135 }
136
137 @Test
Yotam Harcholf3f11152013-09-05 16:47:16 -0700138 public void testMasked() throws UnknownHostException {
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800139 for(WithMaskTaskCase w: withMasks) {
140 IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input);
141 if (!w.hasMask) {
Yotam Harchola289d552013-09-16 10:10:40 -0700142 IPv6Address ip = value.getValue();
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800143 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700144
145 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800146 assertEquals(w.input.split("/")[0], ip.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700147 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800148 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
149
Gregor Maier5615b6c2013-12-11 22:29:07 -0800150 if (w.expectedMaskLength == -1) {
151 assertFalse(value.getMask().isCidrMask());
152 try {
153 value.getMask().asCidrMaskLength();
154 fail("Expected IllegalStateException not thrown");
155 } catch(IllegalStateException e) {
156 //expected
157 }
158 } else {
159 assertTrue(value.getMask().isCidrMask());
160 assertEquals("Input " + w.input, w.expectedMaskLength,
161 value.getMask().asCidrMaskLength());
162 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800163
164 byte[] address = inetAddress.getAddress();
165 assertEquals(address.length, value.getValue().getBytes().length);
166
167 for (int j = 0; j < address.length; j++) {
168 address[j] &= w.expectedMask[j];
169 }
170
171 assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address));
172 assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask));
173 }
174 for (int i = 0; i <= 128; i++) {
175 String ipString = String.format("8001:2::1/%d", i);
176 IPv6AddressWithMask value = IPv6AddressWithMask.of(ipString);
177 assertEquals("Input " + ipString, i, value.getMask().asCidrMaskLength());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700178 }
179 }
180
181
182 @Test
183 public void testOfString() throws UnknownHostException {
184 for(int i=0; i < testStrings.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700185 IPv6Address ip = IPv6Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700186 InetAddress inetAddress = InetAddress.getByName(testStrings[i]);
187
188 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
189 assertEquals(testStrings[i], ip.toString());
190 }
191 }
192
193 @Test
194 public void testOfByteArray() throws UnknownHostException {
195 for(int i=0; i < testStrings.length; i++ ) {
196 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700197 IPv6Address ip = IPv6Address.of(bytes);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700198 assertEquals(testStrings[i], ip.toString());
199 assertArrayEquals(bytes, ip.getBytes());
200 }
201 }
202
Ronald Lifea17892014-07-03 18:36:06 -0700203 private static void testOfCidrMaskLengthHelper(
204 int cidrMaskLength, String ipStr) throws UnknownHostException {
205 byte[] ba0 = IPv6Address.ofCidrMaskLength(cidrMaskLength).getBytes();
206 byte[] ba1 = Inet6Address.getByName(ipStr).getAddress();
207 assertArrayEquals(ba0, ba1);
208 }
209
210 @Test
211 public void testOfCidrMaskLength() throws UnknownHostException {
212 for (int i = 0; i <= 128; i++) {
213 assertTrue(IPv6Address.ofCidrMaskLength(i).isCidrMask());
214 assertEquals(IPv6Address.ofCidrMaskLength(i).asCidrMaskLength(), i);
215 }
216 testOfCidrMaskLengthHelper(0, "::");
217 testOfCidrMaskLengthHelper(1, "8000::");
218 testOfCidrMaskLengthHelper(2, "c000::");
219 testOfCidrMaskLengthHelper(8, "ff00::");
220 testOfCidrMaskLengthHelper(16, "ffff::");
221 testOfCidrMaskLengthHelper(17, "ffff:8000::");
222 testOfCidrMaskLengthHelper(31, "ffff:fffe::");
223 testOfCidrMaskLengthHelper(32, "ffff:ffff::");
224 testOfCidrMaskLengthHelper(33, "ffff:ffff:8000::");
225 testOfCidrMaskLengthHelper(46, "ffff:ffff:fffc::");
226 testOfCidrMaskLengthHelper(48, "ffff:ffff:ffff::");
227 testOfCidrMaskLengthHelper(55, "ffff:ffff:ffff:fe00::");
228 testOfCidrMaskLengthHelper(56, "ffff:ffff:ffff:ff00::");
229 testOfCidrMaskLengthHelper(59, "ffff:ffff:ffff:ffe0::");
230 testOfCidrMaskLengthHelper(63, "ffff:ffff:ffff:fffe::");
231 testOfCidrMaskLengthHelper(64, "ffff:ffff:ffff:ffff::");
232 testOfCidrMaskLengthHelper(65, "ffff:ffff:ffff:ffff:8000::");
233 testOfCidrMaskLengthHelper(67, "ffff:ffff:ffff:ffff:e000::");
234 testOfCidrMaskLengthHelper(100, "ffff:ffff:ffff:ffff:ffff:ffff:f000::");
235 testOfCidrMaskLengthHelper(101, "ffff:ffff:ffff:ffff:ffff:ffff:f800::");
236 testOfCidrMaskLengthHelper(126, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc");
237 testOfCidrMaskLengthHelper(127, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe");
238 testOfCidrMaskLengthHelper(128, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
239 }
240
Yotam Harcholf3f11152013-09-05 16:47:16 -0700241 @Test
Ronald Libbf01942014-07-07 17:00:13 -0700242 public void testWithMask() throws Exception {
243 // Sanity tests for the withMask*() syntactic sugars
244
245 IPv6Address original = IPv6Address.of("fd12:3456:ABCD:7890::1");
246 IPv6Address expectedValue = IPv6Address.of("fd12:3456:ABCD::");
247 IPv6Address expectedMask = IPv6Address.of("ffff:ffff:ffff::");
248
249 IPv6AddressWithMask v;
250
251 v = original.withMask(IPv6Address.of(new byte[] {
252 -1, -1, -1, -1, -1, -1, 0, 0,
253 0, 0, 0, 0, 0, 0, 0, 0 }));
254 assertEquals(v.getValue(), expectedValue);
255 assertEquals(v.getMask(), expectedMask);
256
257 v = original.withMask(IPv6Address.of(
258 0xFFFF_FFFF_FFFF_0000L, 0x0000_0000_0000_0000L));
259 assertEquals(v.getValue(), expectedValue);
260 assertEquals(v.getMask(), expectedMask);
261
262 v = original.withMask(IPv6Address.of("ffff:ffff:ffff::"));
263 assertEquals(v.getValue(), expectedValue);
264 assertEquals(v.getMask(), expectedMask);
265
266 Inet6Address i6a = (Inet6Address) InetAddress.getByName("ffff:ffff:ffff::");
267 v = original.withMask(IPv6Address.of(i6a));
268 assertEquals(v.getValue(), expectedValue);
269 assertEquals(v.getMask(), expectedMask);
270
271 v = original.withMaskOfLength(48);
272 assertEquals(v.getValue(), expectedValue);
273 assertEquals(v.getMask(), expectedMask);
274 }
275
276 @Test
Yotam Harcholf3f11152013-09-05 16:47:16 -0700277 public void testReadFrom() throws OFParseError, UnknownHostException {
278 for(int i=0; i < testStrings.length; i++ ) {
279 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700280 IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700281 assertEquals(testStrings[i], ip.toString());
282 assertArrayEquals(bytes, ip.getBytes());
283 }
284 }
285
286 String[] invalidIPs = {
287 "",
288 ":",
289 "1:2:3:4:5:6:7:8:9",
290 "1:2:3:4:5:6:7:8:",
291 "1:2:3:4:5:6:7:8g",
292 "1:2:3:",
293 "12345::",
294 "1::3::8",
295 "::3::"
296 };
297
298 @Test
299 public void testInvalidIPs() throws OFParseError {
300 for(String invalid : invalidIPs) {
301 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700302 IPv6Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700303 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
304 } catch(IllegalArgumentException e) {
305 // ok
306 }
307 }
308 }
309
310 @Test
311 public void testZeroCompression() throws OFParseError {
Yotam Harchola289d552013-09-16 10:10:40 -0700312 assertEquals("::", IPv6Address.of("::").toString(true, false));
313 assertEquals("0:0:0:0:0:0:0:0", IPv6Address.of("::").toString(false, false));
314 assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6Address.of("::").toString(false, true));
315 assertEquals("1::4:5:6:0:8", IPv6Address.of("1:0:0:4:5:6:0:8").toString(true, false));
316 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 -0700317 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800318
319 @Test
320 public void testSuperclass() throws Exception {
321 for(String ipString: testStrings) {
322 IPAddress<?> superIp = IPAddress.of(ipString);
323 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
324 assertEquals(IPv6Address.of(ipString), superIp);
325 }
326
327 for(WithMaskTaskCase w: withMasks) {
328 String ipMaskedString = w.input;
329 IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
330 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
331 assertEquals(IPv6AddressWithMask.of(ipMaskedString), superIp);
332 }
333 }
Gregor Maier1ff55972013-12-11 02:22:56 -0800334
335 @Test
Ronald Li9d777a52015-02-07 06:19:29 -0800336 public void testCompareTo() {
337 assertThat(
338 IPv6Address.of("fc00::1").compareTo(IPv6Address.of("fc00::2")),
339 Matchers.lessThan(0));
340 assertThat(
341 IPv6Address.of("::1").compareTo(IPv6Address.of("fc00::")),
342 Matchers.lessThan(0));
343
344 // Make sure that unsigned comparison is used on the first 64 bits
345 assertThat(
346 IPv6Address.of("fc00::1").compareTo(IPv6Address.of("1234::3")),
347 Matchers.greaterThan(0));
348
349 // Make sure that unsigned comparison is used on the next 64 bits
350 assertThat(
351 IPv6Address.of("::8000:0:0:1").compareTo(IPv6Address.of("::1")),
352 Matchers.greaterThan(0));
353 }
354
355 @Test
Gregor Maier1ff55972013-12-11 02:22:56 -0800356 public void testOfExceptions() throws Exception {
357 try {
358 IPv6AddressWithMask.of(null);
359 fail("Should have thrown NullPointerException");
360 } catch (NullPointerException e) {
361 assertNotNull(e.getMessage());
362 }
363 try {
364 String s = null;
365 IPv6Address.of(s);
366 fail("Should have thrown NullPointerException");
367 } catch (NullPointerException e) {
368 assertNotNull(e.getMessage());
369 }
370 try {
371 byte[] b = null;
372 IPv6Address.of(b);
373 fail("Should have thrown NullPointerException");
374 } catch (NullPointerException e) {
375 assertNotNull(e.getMessage());
376 }
377 try {
378 byte[] b = new byte[7];
379 IPv6Address.of(b);
380 fail("Should have thrown IllegalArgumentException");
381 } catch (IllegalArgumentException e) {
382 // expected
383 }
384 try {
385 byte[] b = new byte[9];
386 IPv6Address.of(b);
387 fail("Should have thrown IllegalArgumentException");
388 } catch (IllegalArgumentException e) {
389 // expected
390 }
391 try {
392 IPv6AddressWithMask.of(IPv6Address.of("1::"), null);
393 fail("Should have thrown NullPointerException");
394 } catch (NullPointerException e) {
395 assertNotNull(e.getMessage());
396 }
397 try {
398 IPv6AddressWithMask.of(null, IPv6Address.of("255::"));
399 fail("Should have thrown NullPointerException");
400 } catch (NullPointerException e) {
401 assertNotNull(e.getMessage());
402 }
Aditya Vajab21043d2014-03-11 13:34:06 -0700403 try {
404 IPv6AddressWithMask.of(IPv6Address.of("10:10::0"),
405 IPv6Address.of("ffff:0:ffff::"))
406 .getSubnetBroadcastAddress();
407 fail("Should have thrown IllegalArgumentException");
408 } catch (IllegalArgumentException e) {
409 assertNotNull(e.getMessage());
410 }
Ronald Lifea17892014-07-03 18:36:06 -0700411 try {
412 IPv6Address.ofCidrMaskLength(-1);
413 fail("Should have thrown IllegalArgumentException");
414 } catch (IllegalArgumentException e) {
415 assertNotNull(e.getMessage());
416 }
417 try {
418 IPv6Address.ofCidrMaskLength(129);
419 fail("Should have thrown IllegalArgumentException");
420 } catch (IllegalArgumentException e) {
421 assertNotNull(e.getMessage());
422 }
Gregor Maier1ff55972013-12-11 02:22:56 -0800423 }
Rich Lanebae30f92014-08-27 12:13:20 -0700424
425 @Test
426 public void testZoneId() throws OFParseError {
427 assertEquals("::", IPv6Address.of("::%eth0").toString(true, false));
428 assertEquals("1:0:0:4::8", IPv6Address.of("1:0:0:4:0:0:0:8%2").toString(true, false));
429 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700430}