Fix ietf-type-util checkstyle
[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.opendaylight.mdsal.model.ietf.util.Ipv6Utils.fillIpv6Bytes;
14
15 import org.junit.Test;
16
17 public class Ipv6UtilsTest {
18
19     @Test
20     public void testDiscards() {
21         assertEqualResult("2001:0000:3238:DFE1:63:0000:0000:FEFB");
22         assertEqualResult("2001:0000:3238:DFE1:63::FEFB");
23         assertEqualResult("2001:0:3238:DFE1:63::FEFB");
24         assertEqualResult("::1");
25         assertEqualResult("::");
26     }
27
28     @Test
29     public void testFullQuads() {
30         assertEqualResult("0000:0000:0000:0000:0000:0000:0000:0001");
31     }
32
33     @Test
34     public void testRfc4291() {
35         assertEqualResult("ABCD:EF01:2345:6789:ABCD:EF01:2345:6789");
36         assertEqualResult("2001:DB8:0:0:8:800:200C:417A");
37         assertEqualResult("2001:DB8::8:800:200C:417A");
38         assertEqualResult("FF01:0:0:0:0:0:0:101");
39         assertEqualResult("FF01::101");
40         assertEqualResult("0:0:0:0:0:0:0:1");
41         assertEqualResult("::1");
42         assertEqualResult("0:0:0:0:0:0:0:0");
43         assertEqualResult("::");
44
45         final byte[] test1 = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 1, 68, 3 };
46         assertArrayEquals(test1, bytesForString("0:0:0:0:0:0:13.1.68.3"));
47         assertArrayEquals(test1, bytesForString("::13.1.68.3"));
48
49         final byte[] test2 = new byte[] {
50             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte)255, (byte)255, (byte)129, (byte)144, 52, 38
51         };
52         assertArrayEquals(test2, bytesForString("0:0:0:0:0:FFFF:129.144.52.38"));
53         assertArrayEquals(test2, bytesForString("::FFFF:129.144.52.38"));
54     }
55
56     @Test
57     public void testRfc5952leadingZeroes() {
58         assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:eeee:0001");
59         assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:eeee:001");
60         assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:eeee:01");
61         assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:eeee:1");
62     }
63
64     @Test
65     public void testRfc5952zeroCompression() {
66         assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd::1");
67         assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:0:1");
68         assertEqualResult("2001:db8:0:0:0::1");
69         assertEqualResult("2001:db8:0:0::1");
70         assertEqualResult("2001:db8:0::1");
71         assertEqualResult("2001:db8::1");
72         assertEqualResult("2001:db8::aaaa:0:0:1");
73         assertEqualResult("2001:db8:0:0:aaaa::1");
74     }
75
76     @Test
77     public void testRfc5952upperLowerCase() {
78         assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa");
79         assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:eeee:AAAA");
80         assertEqualResult("2001:db8:aaaa:bbbb:cccc:dddd:eeee:AaAa");
81     }
82
83     @Test
84     public void testRfc5952specials() {
85         // Can't use Guava for these, as it will return an IPv4 address
86         assertArrayEquals(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xff, (byte) 0xff, (byte)192, 0, 2, 1 },
87             bytesForString("::ffff:192.0.2.1"));
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("0:0:0:0:0:ffff:192.0.2.1"));
90     }
91
92     @Test
93     public void testRfc6052() {
94         assertEqualResult("2001:db8:c000:221::");
95         assertEqualResult("2001:db8:1c0:2:21::");
96         assertEqualResult("2001:db8:122:c000:2:2100::");
97         assertEqualResult("2001:db8:122:3c0:0:221::");
98         assertEqualResult("2001:db8:122:344:c0:2:2100::");
99         assertEqualResult("2001:db8:122:344::192.0.2.33");
100
101         assertEqualResult("64:ff9b::192.0.2.33");
102     }
103
104     private static byte[] bytesForString(final String str) {
105         final byte[] bytes = new byte[16];
106         fillIpv6Bytes(bytes, str, str.length());
107         return bytes;
108     }
109
110     // Utility for quick comparison with Guava
111     private static void assertEqualResult(final String str) {
112         assertArrayEquals(forString(str).getAddress(), bytesForString(str));
113     }
114 }