blob: a397c2a9f5dc7942bbdec7a78b5dbf3be229dab3 [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.projectfloodlight.openflow.types;
2
3import static org.junit.Assert.*;
4
5import java.net.Inet6Address;
6import java.net.InetAddress;
7import java.net.UnknownHostException;
8
9import org.hamcrest.CoreMatchers;
10import org.jboss.netty.buffer.ChannelBuffers;
11import org.junit.Test;
12import org.projectfloodlight.openflow.exceptions.OFParseError;
13
14import com.google.common.io.BaseEncoding;
15
16public class IPv6AddressTest {
17
18 String[] testStrings = {
19 "::",
20 "::1",
21 "ffe0::",
22 "1:2:3:4:5:6:7:8"
23 };
24
25
26 private final BaseEncoding hex = BaseEncoding.base16().omitPadding().lowerCase();
27
28 private class WithMaskTaskCase {
29 final String input;
30 boolean hasMask;
31 int expectedMaskLength = 128;
32 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
39 public WithMaskTaskCase maskHex(String string) {
40 string = string.replaceAll(" ", "");
41 this.hasMask = true;
42 expectedMask = hex.decode(string);
43 return this;
44 }
45
46 public WithMaskTaskCase expectedMaskLength(int expectedLength) {
47 this.expectedMaskLength = expectedLength;
48 return this;
49 }
50
51 }
52
53 WithMaskTaskCase[] withMasks = new WithMaskTaskCase[] {
54 new WithMaskTaskCase("1::1/80")
55 .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00")
56 .expectedMaskLength(80),
57
58 new WithMaskTaskCase("ffff:ffee:1::/ff00:ff00:ff00:ff00::")
59 .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00")
60 .expectedMaskLength(-1),
61 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),
70 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")
75 .expectedMaskLength(0),
76 };
77
78 @Test
79 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
102 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
136 public void testMasked() throws UnknownHostException {
137 for(WithMaskTaskCase w: withMasks) {
138 IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input);
139 if (!w.hasMask) {
140 IPv6Address ip = value.getValue();
141 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
142
143 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
144 assertEquals(w.input.split("/")[0], ip.toString());
145 }
146 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
147
148 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 }
161
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());
176 }
177 }
178
179
180 @Test
181 public void testOfString() throws UnknownHostException {
182 for(int i=0; i < testStrings.length; i++ ) {
183 IPv6Address ip = IPv6Address.of(testStrings[i]);
184 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();
195 IPv6Address ip = IPv6Address.of(bytes);
196 assertEquals(testStrings[i], ip.toString());
197 assertArrayEquals(bytes, ip.getBytes());
198 }
199 }
200
201 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
239 @Test
240 public void testWithMask() throws Exception {
241 // Sanity tests for the withMask*() syntactic sugars
242
243 IPv6Address original = IPv6Address.of("fd12:3456:ABCD:7890::1");
244 IPv6Address expectedValue = IPv6Address.of("fd12:3456:ABCD::");
245 IPv6Address expectedMask = IPv6Address.of("ffff:ffff:ffff::");
246
247 IPv6AddressWithMask v;
248
249 v = original.withMask(IPv6Address.of(new byte[] {
250 -1, -1, -1, -1, -1, -1, 0, 0,
251 0, 0, 0, 0, 0, 0, 0, 0 }));
252 assertEquals(v.getValue(), expectedValue);
253 assertEquals(v.getMask(), expectedMask);
254
255 v = original.withMask(IPv6Address.of(
256 0xFFFF_FFFF_FFFF_0000L, 0x0000_0000_0000_0000L));
257 assertEquals(v.getValue(), expectedValue);
258 assertEquals(v.getMask(), expectedMask);
259
260 v = original.withMask(IPv6Address.of("ffff:ffff:ffff::"));
261 assertEquals(v.getValue(), expectedValue);
262 assertEquals(v.getMask(), expectedMask);
263
264 Inet6Address i6a = (Inet6Address) InetAddress.getByName("ffff:ffff:ffff::");
265 v = original.withMask(IPv6Address.of(i6a));
266 assertEquals(v.getValue(), expectedValue);
267 assertEquals(v.getMask(), expectedMask);
268
269 v = original.withMaskOfLength(48);
270 assertEquals(v.getValue(), expectedValue);
271 assertEquals(v.getMask(), expectedMask);
272 }
273
274 @Test
275 public void testReadFrom() throws OFParseError, UnknownHostException {
276 for(int i=0; i < testStrings.length; i++ ) {
277 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
278 IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes));
279 assertEquals(testStrings[i], ip.toString());
280 assertArrayEquals(bytes, ip.getBytes());
281 }
282 }
283
284 String[] invalidIPs = {
285 "",
286 ":",
287 "1:2:3:4:5:6:7:8:9",
288 "1:2:3:4:5:6:7:8:",
289 "1:2:3:4:5:6:7:8g",
290 "1:2:3:",
291 "12345::",
292 "1::3::8",
293 "::3::"
294 };
295
296 @Test
297 public void testInvalidIPs() throws OFParseError {
298 for(String invalid : invalidIPs) {
299 try {
300 IPv6Address.of(invalid);
301 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
302 } catch(IllegalArgumentException e) {
303 // ok
304 }
305 }
306 }
307
308 @Test
309 public void testZeroCompression() throws OFParseError {
310 assertEquals("::", IPv6Address.of("::").toString(true, false));
311 assertEquals("0:0:0:0:0:0:0:0", IPv6Address.of("::").toString(false, false));
312 assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6Address.of("::").toString(false, true));
313 assertEquals("1::4:5:6:0:8", IPv6Address.of("1:0:0:4:5:6:0:8").toString(true, false));
314 assertEquals("1:0:0:4::8", IPv6Address.of("1:0:0:4:0:0:0:8").toString(true, false));
315 }
316
317 @Test
318 public void testSuperclass() throws Exception {
319 for(String ipString: testStrings) {
320 IPAddress<?> superIp = IPAddress.of(ipString);
321 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
322 assertEquals(IPv6Address.of(ipString), superIp);
323 }
324
325 for(WithMaskTaskCase w: withMasks) {
326 String ipMaskedString = w.input;
327 IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
328 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
329 assertEquals(IPv6AddressWithMask.of(ipMaskedString), superIp);
330 }
331 }
332
333 @Test
334 public void testOfExceptions() throws Exception {
335 try {
336 IPv6AddressWithMask.of(null);
337 fail("Should have thrown NullPointerException");
338 } catch (NullPointerException e) {
339 assertNotNull(e.getMessage());
340 }
341 try {
342 String s = null;
343 IPv6Address.of(s);
344 fail("Should have thrown NullPointerException");
345 } catch (NullPointerException e) {
346 assertNotNull(e.getMessage());
347 }
348 try {
349 byte[] b = null;
350 IPv6Address.of(b);
351 fail("Should have thrown NullPointerException");
352 } catch (NullPointerException e) {
353 assertNotNull(e.getMessage());
354 }
355 try {
356 byte[] b = new byte[7];
357 IPv6Address.of(b);
358 fail("Should have thrown IllegalArgumentException");
359 } catch (IllegalArgumentException e) {
360 // expected
361 }
362 try {
363 byte[] b = new byte[9];
364 IPv6Address.of(b);
365 fail("Should have thrown IllegalArgumentException");
366 } catch (IllegalArgumentException e) {
367 // expected
368 }
369 try {
370 IPv6AddressWithMask.of(IPv6Address.of("1::"), null);
371 fail("Should have thrown NullPointerException");
372 } catch (NullPointerException e) {
373 assertNotNull(e.getMessage());
374 }
375 try {
376 IPv6AddressWithMask.of(null, IPv6Address.of("255::"));
377 fail("Should have thrown NullPointerException");
378 } catch (NullPointerException e) {
379 assertNotNull(e.getMessage());
380 }
381 try {
382 IPv6AddressWithMask.of(IPv6Address.of("10:10::0"),
383 IPv6Address.of("ffff:0:ffff::"))
384 .getSubnetBroadcastAddress();
385 fail("Should have thrown IllegalArgumentException");
386 } catch (IllegalArgumentException e) {
387 assertNotNull(e.getMessage());
388 }
389 try {
390 IPv6Address.ofCidrMaskLength(-1);
391 fail("Should have thrown IllegalArgumentException");
392 } catch (IllegalArgumentException e) {
393 assertNotNull(e.getMessage());
394 }
395 try {
396 IPv6Address.ofCidrMaskLength(129);
397 fail("Should have thrown IllegalArgumentException");
398 } catch (IllegalArgumentException e) {
399 assertNotNull(e.getMessage());
400 }
401 }
402}