Bug 5947: additional tests for mdsal project #10
[mdsal.git] / model / ietf / ietf-type-util / src / test / java / org / opendaylight / mdsal / model / ietf / util / Ipv6UtilsTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems Inc and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.mdsal.model.ietf.util;
10
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;
16
17 import java.lang.reflect.Constructor;
18 import org.junit.Test;
19
20 public class Ipv6UtilsTest {
21
22     @Test
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("::");
29     }
30
31     @Test
32     public void testFullQuads() {
33         assertEqualResult("0000:0000:0000:0000:0000:0000:0000:0001");
34     }
35
36     @Test
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("::");
47
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"));
51
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"));
56     }
57
58     @Test
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");
64     }
65
66     @Test
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");
76     }
77
78     @Test
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");
83     }
84
85     @Test
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"));
92     }
93
94     @Test
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");
102
103         assertEqualResult("64:ff9b::192.0.2.33");
104     }
105
106     private static byte[] bytesForString(final String str) {
107         final byte[] bytes = new byte[16];
108         fillIpv6Bytes(bytes, str, str.length());
109         return bytes;
110     }
111
112     // Utility for quick comparison with Guava
113     private static void assertEqualResult(final String str) {
114         assertArrayEquals(forString(str).getAddress(), bytesForString(str));
115     }
116
117     @Test(expected = UnsupportedOperationException.class)
118     public void privateConstructTest() throws Throwable {
119         final Constructor constructor = Ipv6Utils.class.getDeclaredConstructor();
120         assertFalse(constructor.isAccessible());
121         constructor.setAccessible(true);
122         try {
123             constructor.newInstance();
124             fail("Exception should be thrown");
125         } catch (Exception e) {
126             throw e.getCause();
127         }
128     }
129 }