blob: 394a39b5e65552e99679b855e4619c7acca2115e [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;
Yotam Harcholf3f11152013-09-05 16:47:16 -070010import org.jboss.netty.buffer.ChannelBuffers;
11import org.junit.Test;
12import org.projectfloodlight.openflow.exceptions.OFParseError;
13
Andreas Wundsame04c86f2013-11-05 17:13:18 -080014import com.google.common.io.BaseEncoding;
15
Yotam Harchola289d552013-09-16 10:10:40 -070016public class IPv6AddressTest {
Yotam Harcholf3f11152013-09-05 16:47:16 -070017
18 String[] testStrings = {
19 "::",
20 "::1",
21 "ffe0::",
22 "1:2:3:4:5:6:7:8"
23 };
24
Yotam Harcholf3f11152013-09-05 16:47:16 -070025
Andreas Wundsame04c86f2013-11-05 17:13:18 -080026 private final BaseEncoding hex = BaseEncoding.base16().omitPadding().lowerCase();
Yotam Harcholf3f11152013-09-05 16:47:16 -070027
Andreas Wundsame04c86f2013-11-05 17:13:18 -080028 private class WithMaskTaskCase {
29 final String input;
30 boolean hasMask;
Gregor Maier7f987e62013-12-10 19:34:18 -080031 int expectedMaskLength = 128;
Andreas Wundsame04c86f2013-11-05 17:13:18 -080032 byte[] expectedMask = hex.decode("ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff".replaceAll(" ", ""));
33
34 public WithMaskTaskCase(String input) {
35 super();
36 this.input = input;
37 }
38
Andreas Wundsame04c86f2013-11-05 17:13:18 -080039 public WithMaskTaskCase maskHex(String string) {
40 string = string.replaceAll(" ", "");
41 this.hasMask = true;
42 expectedMask = hex.decode(string);
43 return this;
44 }
45
Gregor Maier7f987e62013-12-10 19:34:18 -080046 public WithMaskTaskCase expectedMaskLength(int expectedLength) {
47 this.expectedMaskLength = expectedLength;
48 return this;
49 }
50
Andreas Wundsame04c86f2013-11-05 17:13:18 -080051 }
52
53 WithMaskTaskCase[] withMasks = new WithMaskTaskCase[] {
54 new WithMaskTaskCase("1::1/80")
Gregor Maier7f987e62013-12-10 19:34:18 -080055 .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00")
56 .expectedMaskLength(80),
Andreas Wundsame04c86f2013-11-05 17:13:18 -080057
58 new WithMaskTaskCase("ffff:ffee:1::/ff00:ff00:ff00:ff00::")
Gregor Maier7f987e62013-12-10 19:34:18 -080059 .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00")
60 .expectedMaskLength(-1),
Gregor Maiercef9e3e2013-12-11 22:41:36 -080061 new WithMaskTaskCase("1:2:3:4:5:6:7:8/1::ff00:ff00")
62 .maskHex("00 01 00 00 00 00 00 00 00 00 00 00 ff 00 ff 00")
63 .expectedMaskLength(-1),
64 new WithMaskTaskCase("1:2:3:4:5:6:7:8/::ff00:ff00")
65 .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 ff 00 ff 00")
66 .expectedMaskLength(-1),
67 new WithMaskTaskCase("1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff::ff00:ff00")
68 .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 ff 00 ff 00")
69 .expectedMaskLength(-1),
Andreas Wundsame04c86f2013-11-05 17:13:18 -080070 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
71 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
72 new WithMaskTaskCase("1:2:3:4:5:6:7:8/128"),
73 new WithMaskTaskCase("::/0")
74 .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")
Gregor Maier7f987e62013-12-10 19:34:18 -080075 .expectedMaskLength(0),
Yotam Harcholf3f11152013-09-05 16:47:16 -070076 };
77
78 @Test
Aditya Vaja56b8b182014-03-11 13:13:58 -070079 public void testLogicalOperatorsBroadcast() {
80 assertTrue(IPv6Address.NO_MASK.not().equals(IPv6Address.FULL_MASK));
81 assertTrue(IPv6Address.NO_MASK.or(IPv6Address.FULL_MASK).
82 equals(IPv6Address.NO_MASK));
83 assertTrue(IPv6Address.NO_MASK.and(IPv6Address.FULL_MASK).
84 equals(IPv6Address.FULL_MASK));
85
86 assertTrue(IPv6Address.NO_MASK.isBroadcast());
87 assertTrue(!IPv6Address.FULL_MASK.isBroadcast());
88 }
89
90 @Test
91 public void testMaskedSubnetBroadcast() {
92 assertTrue(IPv6AddressWithMask.of("10:10::1/112")
93 .getSubnetBroadcastAddress()
94 .equals(IPv6Address.of("10:10::ffff")));
95 assertTrue(IPv6AddressWithMask.of("10:10::1/112")
96 .isSubnetBroadcastAddress(IPv6Address.of("10:10::ffff")));
97 assertTrue(!IPv6AddressWithMask.of("10:10::1/112")
98 .isSubnetBroadcastAddress(IPv6Address.of("10:10::fffd")));
99 }
100
101 @Test
Gregor Maier5615b6c2013-12-11 22:29:07 -0800102 public void testConstants() {
103 byte[] zeros = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
104 (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[] ones = { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
108 (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 // Make sure class initializtation and static assignment don't get
112 // messed up. Test everything twice for cached values
113 assertTrue(IPv6Address.NONE.isCidrMask());
114 assertEquals(0, IPv6Address.NONE.asCidrMaskLength());
115 assertArrayEquals(zeros, IPv6Address.NONE.getBytes());
116 assertTrue(IPv6Address.NONE.isCidrMask());
117 assertEquals(0, IPv6Address.NONE.asCidrMaskLength());
118 assertArrayEquals(zeros, IPv6Address.NONE.getBytes());
119
120 assertTrue(IPv6Address.NO_MASK.isCidrMask());
121 assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength());
122 assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes());
123 assertTrue(IPv6Address.NO_MASK.isCidrMask());
124 assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength());
125 assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes());
126
127 assertTrue(IPv6Address.FULL_MASK.isCidrMask());
128 assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength());
129 assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes());
130 assertTrue(IPv6Address.FULL_MASK.isCidrMask());
131 assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength());
132 assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes());
133 }
134
135 @Test
Yotam Harcholf3f11152013-09-05 16:47:16 -0700136 public void testMasked() throws UnknownHostException {
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800137 for(WithMaskTaskCase w: withMasks) {
138 IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input);
139 if (!w.hasMask) {
Yotam Harchola289d552013-09-16 10:10:40 -0700140 IPv6Address ip = value.getValue();
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800141 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700142
143 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800144 assertEquals(w.input.split("/")[0], ip.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700145 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800146 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
147
Gregor Maier5615b6c2013-12-11 22:29:07 -0800148 if (w.expectedMaskLength == -1) {
149 assertFalse(value.getMask().isCidrMask());
150 try {
151 value.getMask().asCidrMaskLength();
152 fail("Expected IllegalStateException not thrown");
153 } catch(IllegalStateException e) {
154 //expected
155 }
156 } else {
157 assertTrue(value.getMask().isCidrMask());
158 assertEquals("Input " + w.input, w.expectedMaskLength,
159 value.getMask().asCidrMaskLength());
160 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800161
162 byte[] address = inetAddress.getAddress();
163 assertEquals(address.length, value.getValue().getBytes().length);
164
165 for (int j = 0; j < address.length; j++) {
166 address[j] &= w.expectedMask[j];
167 }
168
169 assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address));
170 assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask));
171 }
172 for (int i = 0; i <= 128; i++) {
173 String ipString = String.format("8001:2::1/%d", i);
174 IPv6AddressWithMask value = IPv6AddressWithMask.of(ipString);
175 assertEquals("Input " + ipString, i, value.getMask().asCidrMaskLength());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700176 }
177 }
178
179
180 @Test
181 public void testOfString() throws UnknownHostException {
182 for(int i=0; i < testStrings.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700183 IPv6Address ip = IPv6Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700184 InetAddress inetAddress = InetAddress.getByName(testStrings[i]);
185
186 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
187 assertEquals(testStrings[i], ip.toString());
188 }
189 }
190
191 @Test
192 public void testOfByteArray() throws UnknownHostException {
193 for(int i=0; i < testStrings.length; i++ ) {
194 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700195 IPv6Address ip = IPv6Address.of(bytes);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700196 assertEquals(testStrings[i], ip.toString());
197 assertArrayEquals(bytes, ip.getBytes());
198 }
199 }
200
Ronald Lifea17892014-07-03 18:36:06 -0700201 private static void testOfCidrMaskLengthHelper(
202 int cidrMaskLength, String ipStr) throws UnknownHostException {
203 byte[] ba0 = IPv6Address.ofCidrMaskLength(cidrMaskLength).getBytes();
204 byte[] ba1 = Inet6Address.getByName(ipStr).getAddress();
205 assertArrayEquals(ba0, ba1);
206 }
207
208 @Test
209 public void testOfCidrMaskLength() throws UnknownHostException {
210 for (int i = 0; i <= 128; i++) {
211 assertTrue(IPv6Address.ofCidrMaskLength(i).isCidrMask());
212 assertEquals(IPv6Address.ofCidrMaskLength(i).asCidrMaskLength(), i);
213 }
214 testOfCidrMaskLengthHelper(0, "::");
215 testOfCidrMaskLengthHelper(1, "8000::");
216 testOfCidrMaskLengthHelper(2, "c000::");
217 testOfCidrMaskLengthHelper(8, "ff00::");
218 testOfCidrMaskLengthHelper(16, "ffff::");
219 testOfCidrMaskLengthHelper(17, "ffff:8000::");
220 testOfCidrMaskLengthHelper(31, "ffff:fffe::");
221 testOfCidrMaskLengthHelper(32, "ffff:ffff::");
222 testOfCidrMaskLengthHelper(33, "ffff:ffff:8000::");
223 testOfCidrMaskLengthHelper(46, "ffff:ffff:fffc::");
224 testOfCidrMaskLengthHelper(48, "ffff:ffff:ffff::");
225 testOfCidrMaskLengthHelper(55, "ffff:ffff:ffff:fe00::");
226 testOfCidrMaskLengthHelper(56, "ffff:ffff:ffff:ff00::");
227 testOfCidrMaskLengthHelper(59, "ffff:ffff:ffff:ffe0::");
228 testOfCidrMaskLengthHelper(63, "ffff:ffff:ffff:fffe::");
229 testOfCidrMaskLengthHelper(64, "ffff:ffff:ffff:ffff::");
230 testOfCidrMaskLengthHelper(65, "ffff:ffff:ffff:ffff:8000::");
231 testOfCidrMaskLengthHelper(67, "ffff:ffff:ffff:ffff:e000::");
232 testOfCidrMaskLengthHelper(100, "ffff:ffff:ffff:ffff:ffff:ffff:f000::");
233 testOfCidrMaskLengthHelper(101, "ffff:ffff:ffff:ffff:ffff:ffff:f800::");
234 testOfCidrMaskLengthHelper(126, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc");
235 testOfCidrMaskLengthHelper(127, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe");
236 testOfCidrMaskLengthHelper(128, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
237 }
238
Yotam Harcholf3f11152013-09-05 16:47:16 -0700239 @Test
240 public void testReadFrom() throws OFParseError, UnknownHostException {
241 for(int i=0; i < testStrings.length; i++ ) {
242 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700243 IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700244 assertEquals(testStrings[i], ip.toString());
245 assertArrayEquals(bytes, ip.getBytes());
246 }
247 }
248
249 String[] invalidIPs = {
250 "",
251 ":",
252 "1:2:3:4:5:6:7:8:9",
253 "1:2:3:4:5:6:7:8:",
254 "1:2:3:4:5:6:7:8g",
255 "1:2:3:",
256 "12345::",
257 "1::3::8",
258 "::3::"
259 };
260
261 @Test
262 public void testInvalidIPs() throws OFParseError {
263 for(String invalid : invalidIPs) {
264 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700265 IPv6Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700266 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
267 } catch(IllegalArgumentException e) {
268 // ok
269 }
270 }
271 }
272
273 @Test
274 public void testZeroCompression() throws OFParseError {
Yotam Harchola289d552013-09-16 10:10:40 -0700275 assertEquals("::", IPv6Address.of("::").toString(true, false));
276 assertEquals("0:0:0:0:0:0:0:0", IPv6Address.of("::").toString(false, false));
277 assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6Address.of("::").toString(false, true));
278 assertEquals("1::4:5:6:0:8", IPv6Address.of("1:0:0:4:5:6:0:8").toString(true, false));
279 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 -0700280 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800281
282 @Test
283 public void testSuperclass() throws Exception {
284 for(String ipString: testStrings) {
285 IPAddress<?> superIp = IPAddress.of(ipString);
286 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
287 assertEquals(IPv6Address.of(ipString), superIp);
288 }
289
290 for(WithMaskTaskCase w: withMasks) {
291 String ipMaskedString = w.input;
292 IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
293 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
294 assertEquals(IPv6AddressWithMask.of(ipMaskedString), superIp);
295 }
296 }
Gregor Maier1ff55972013-12-11 02:22:56 -0800297
298 @Test
299 public void testOfExceptions() throws Exception {
300 try {
301 IPv6AddressWithMask.of(null);
302 fail("Should have thrown NullPointerException");
303 } catch (NullPointerException e) {
304 assertNotNull(e.getMessage());
305 }
306 try {
307 String s = null;
308 IPv6Address.of(s);
309 fail("Should have thrown NullPointerException");
310 } catch (NullPointerException e) {
311 assertNotNull(e.getMessage());
312 }
313 try {
314 byte[] b = null;
315 IPv6Address.of(b);
316 fail("Should have thrown NullPointerException");
317 } catch (NullPointerException e) {
318 assertNotNull(e.getMessage());
319 }
320 try {
321 byte[] b = new byte[7];
322 IPv6Address.of(b);
323 fail("Should have thrown IllegalArgumentException");
324 } catch (IllegalArgumentException e) {
325 // expected
326 }
327 try {
328 byte[] b = new byte[9];
329 IPv6Address.of(b);
330 fail("Should have thrown IllegalArgumentException");
331 } catch (IllegalArgumentException e) {
332 // expected
333 }
334 try {
335 IPv6AddressWithMask.of(IPv6Address.of("1::"), null);
336 fail("Should have thrown NullPointerException");
337 } catch (NullPointerException e) {
338 assertNotNull(e.getMessage());
339 }
340 try {
341 IPv6AddressWithMask.of(null, IPv6Address.of("255::"));
342 fail("Should have thrown NullPointerException");
343 } catch (NullPointerException e) {
344 assertNotNull(e.getMessage());
345 }
Aditya Vajab21043d2014-03-11 13:34:06 -0700346 try {
347 IPv6AddressWithMask.of(IPv6Address.of("10:10::0"),
348 IPv6Address.of("ffff:0:ffff::"))
349 .getSubnetBroadcastAddress();
350 fail("Should have thrown IllegalArgumentException");
351 } catch (IllegalArgumentException e) {
352 assertNotNull(e.getMessage());
353 }
Ronald Lifea17892014-07-03 18:36:06 -0700354 try {
355 IPv6Address.ofCidrMaskLength(-1);
356 fail("Should have thrown IllegalArgumentException");
357 } catch (IllegalArgumentException e) {
358 assertNotNull(e.getMessage());
359 }
360 try {
361 IPv6Address.ofCidrMaskLength(129);
362 fail("Should have thrown IllegalArgumentException");
363 } catch (IllegalArgumentException e) {
364 assertNotNull(e.getMessage());
365 }
Gregor Maier1ff55972013-12-11 02:22:56 -0800366 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700367}