2 * Copyright (c) 2014 Brocade Communications Systems Inc and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.mdsal.model.ietf.util;
11 import static com.google.common.net.InetAddresses.forString;
12 import static org.junit.Assert.assertArrayEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.fail;
15 import static org.opendaylight.mdsal.model.ietf.util.Ipv6Utils.fillIpv6Bytes;
17 import java.lang.reflect.Constructor;
18 import org.junit.Test;
20 public class Ipv6UtilsTest {
23 public void testDiscards() {
24 assertEqualResult("2001:0000:3238:DFE1:63:0000:0000:FEFB");
25 assertEqualResult("2001:0000:3238:DFE1:63::FEFB");
26 assertEqualResult("2001:0:3238:DFE1:63::FEFB");
27 assertEqualResult("::1");
28 assertEqualResult("::");
32 public void testFullQuads() {
33 assertEqualResult("0000:0000:0000:0000:0000:0000:0000:0001");
37 public void testRfc4291() {
38 assertEqualResult("ABCD:EF01:2345:6789:ABCD:EF01:2345:6789");
39 assertEqualResult("2001:DB8:0:0:8:800:200C:417A");
40 assertEqualResult("2001:DB8::8:800:200C:417A");
41 assertEqualResult("FF01:0:0:0:0:0:0:101");
42 assertEqualResult("FF01::101");
43 assertEqualResult("0:0:0:0:0:0:0:1");
44 assertEqualResult("::1");
45 assertEqualResult("0:0:0:0:0:0:0:0");
46 assertEqualResult("::");
48 final byte[] test1 = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 1, 68, 3 };
49 assertArrayEquals(test1, bytesForString("0:0:0:0:0:0:13.1.68.3"));
50 assertArrayEquals(test1, bytesForString("::13.1.68.3"));
52 final byte[] test2 = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte)255, (byte)255,
53 (byte)129, (byte)144, 52, 38 };
54 assertArrayEquals(test2, bytesForString("0:0:0:0:0:FFFF:129.144.52.38"));
55 assertArrayEquals(test2, bytesForString("::FFFF:129.144.52.38"));
59 public void testRfc5952leadingZeroes() {
60 assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:eeee:0001");
61 assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:eeee:001");
62 assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:eeee:01");
63 assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:eeee:1");
67 public void testRfc5952zeroCompression() {
68 assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd::1");
69 assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:0:1");
70 assertEqualResult("2001:db8:0:0:0::1");
71 assertEqualResult("2001:db8:0:0::1");
72 assertEqualResult("2001:db8:0::1");
73 assertEqualResult("2001:db8::1");
74 assertEqualResult("2001:db8::aaaa:0:0:1");
75 assertEqualResult("2001:db8:0:0:aaaa::1");
79 public void testRfc5952upperLowerCase() {
80 assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa");
81 assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:eeee:AAAA");
82 assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:eeee:AaAa");
86 public void testRfc5952specials() {
87 // Can't use Guava for these, as it will return an IPv4 address
88 assertArrayEquals(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xff, (byte) 0xff, (byte)192, 0, 2, 1 },
89 bytesForString("::ffff:192.0.2.1"));
90 assertArrayEquals(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xff, (byte) 0xff, (byte)192, 0, 2, 1 },
91 bytesForString("0:0:0:0:0:ffff:192.0.2.1"));
95 public void testRfc6052() {
96 assertEqualResult("2001:db8:c000:221::");
97 assertEqualResult("2001:db8:1c0:2:21::");
98 assertEqualResult("2001:db8:122:c000:2:2100::");
99 assertEqualResult("2001:db8:122:3c0:0:221::");
100 assertEqualResult("2001:db8:122:344:c0:2:2100::");
101 assertEqualResult("2001:db8:122:344::192.0.2.33");
103 assertEqualResult("64:ff9b::192.0.2.33");
106 private static byte[] bytesForString(final String str) {
107 final byte[] bytes = new byte[16];
108 fillIpv6Bytes(bytes, str, str.length());
112 // Utility for quick comparison with Guava
113 private static void assertEqualResult(final String str) {
114 assertArrayEquals(forString(str).getAddress(), bytesForString(str));
117 @Test(expected = UnsupportedOperationException.class)
118 public void privateConstructTest() throws Throwable {
119 final Constructor<Ipv6Utils> constructor = Ipv6Utils.class.getDeclaredConstructor();
120 assertFalse(constructor.isAccessible());
121 constructor.setAccessible(true);
123 constructor.newInstance();
124 fail("Exception should be thrown");
125 } catch (Exception e) {