Bug 5947: Increasing code coverage - ietf-type-util
[mdsal.git] / model / ietf / ietf-type-util / src / test / java / org / opendaylight / mdsal / model / ietf / util / AbstractIetfInetUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco 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 package org.opendaylight.mdsal.model.ietf.util;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16
17 import com.google.common.net.InetAddresses;
18 import java.net.Inet4Address;
19 import java.net.Inet6Address;
20 import java.net.InetAddress;
21 import org.junit.Test;
22
23 public class AbstractIetfInetUtilTest {
24     private static final IpUtil UTIL = new IpUtil();
25
26     private static void assertV4Equals(final String literal, final String append) {
27         final byte[] expected = InetAddresses.forString(literal).getAddress();
28         final byte[] actual = UTIL.ipv4AddressBytes(new IpClass(literal + append));
29         assertArrayEquals(expected, actual);
30     }
31
32     private static void assertV4Equals(final String literal) {
33         assertV4Equals(literal, "");
34     }
35
36     @Test
37     public void testIpToBytesAndBack() throws Exception {
38         assertV4Equals("1.2.3.4");
39         assertV4Equals("12.23.34.45");
40         assertV4Equals("255.254.253.252");
41         assertV4Equals("128.16.0.127");
42
43         assertV4Equals("1.2.3.4", "%5");
44         assertV4Equals("12.23.34.45", "%5");
45         assertV4Equals("255.254.253.252", "%5");
46         assertV4Equals("128.16.0.127", "%5");
47
48         assertEquals(new IpClass("1.2.3.4")._value.toLowerCase(),
49                 UTIL.ipAddressFor(UTIL.ipv4AddressBytes(new IpClass("1.2.3.4")))._value.toLowerCase());
50         assertNotEquals(new IpClass("2.3.4.5")._value.toLowerCase(),
51                 UTIL.ipAddressFor(UTIL.ipv4AddressBytes(new IpClass("1.2.3.4")))._value.toLowerCase());
52
53         assertEquals(new IpClass("FE80::2002:B3FF:FE1E:8329")._value.toLowerCase(),
54                 UTIL.ipAddressFor(UTIL.ipv6AddressBytes(new IpClass("FE80::2002:B3FF:FE1E:8329")))._value.toLowerCase());
55         assertNotEquals(new IpClass("FEFF::2002:B3FF:FE1E:8329")._value.toLowerCase(),
56                 UTIL.ipAddressFor(UTIL.ipv6AddressBytes(new IpClass("FE80::2002:B3FF:FE1E:8329")))._value.toLowerCase());
57
58         assertEquals(new IpClass("1.2.3.4")._value.toLowerCase(),
59                 UTIL.ipAddressFor(UTIL.inetAddressFor(new IpClass("1.2.3.4")))._value.toLowerCase());
60         assertNotEquals(new IpClass("2.3.4.5")._value.toLowerCase(),
61                 UTIL.ipAddressFor(UTIL.inetAddressFor(new IpClass("1.2.3.4")))._value.toLowerCase());
62
63         assertEquals(new IpClass("FE80::2002:B3FF:FE1E:8329")._value.toLowerCase(),
64                 UTIL.ipAddressFor(UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329")))._value.toLowerCase());
65         assertNotEquals(new IpClass("FEFF::2002:B3FF:FE1E:8329")._value.toLowerCase(),
66                 UTIL.ipAddressFor(UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329")))._value.toLowerCase());
67     }
68
69     @Test(expected = IllegalArgumentException.class)
70     public void illegalArrayLengthForAddressTest() throws Exception {
71         UTIL.ipAddressFor(new byte[] { 0, 0, 0 });
72     }
73
74     @Test(expected = IllegalArgumentException.class)
75     public void unhandledAddressTest() throws Exception {
76         final InetAddress adr = mock(InetAddress.class);
77         doReturn("testAddress").when(adr).toString();
78         UTIL.ipAddressFor(adr);
79     }
80
81     @Test(expected = IllegalArgumentException.class)
82     public void illegalArrayLengthforPrefixTest() throws Exception {
83         UTIL.ipPrefixFor(new byte[] { 0, 0, 0 }, 0);
84     }
85
86     @Test(expected = IllegalArgumentException.class)
87     public void illegalAddressforPrefixTest() throws Exception {
88         final InetAddress adr = mock(InetAddress.class);
89         doReturn("testAddress").when(adr).toString();
90         UTIL.ipPrefixFor(adr, 0);
91     }
92
93     @Test
94     public void ipv4Tests() throws Exception {
95         IpClass ipClass = new IpClass("1.2.3.4/16");
96         assertEquals("1.2.3.4", UTIL.ipv4AddressFrom(ipClass)._value);
97         assertTrue(UTIL.ipv4PrefixFor(UTIL.ipv4AddressBytes(ipClass))._value.contains("/32"));
98         ipClass = new IpClass("1.2.3.4");
99         assertTrue(UTIL.ipv4PrefixFor(UTIL.inetAddressFor(ipClass))._value.contains("/32"));
100         assertTrue(UTIL.ipv4PrefixFor(ipClass)._value.contains("/32"));
101         assertTrue(UTIL.ipv4PrefixFor(ipClass, 16)._value.contains("/16"));
102
103         assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0)._value.equals("0.0.0.0/0"));
104         assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 32)._value.equals("1.2.3.4/32"));
105         assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0, 0)._value.equals("0.0.0.0/0"));
106         assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0, 32)._value.equals("1.2.3.4/32"));
107         assertTrue(UTIL.ipv4PrefixForShort(new byte[] { 1, 2, 3, 4, 5 }, 1, 32)._value.equals("2.3.4.5/32"));
108
109         assertTrue(UTIL.splitIpv4Prefix(new IpClass("1.2.3.4/16")).getKey()._value.equals("1.2.3.4"));
110         assertTrue(UTIL.splitIpv4Prefix(new IpClass("1.2.3.4/16")).getValue().equals(16));
111         assertArrayEquals(new byte[] { 1,2,3,4,16 }, UTIL.ipv4PrefixToBytes(new IpClass("1.2.3.4/16")));
112     }
113
114     @Test
115     public void ipv6Tests() throws Exception {
116         IpClass ipClass = new IpClass("::0/128");
117         assertEquals("::0", UTIL.ipv6AddressFrom(ipClass)._value);
118         ipClass = new IpClass("::0");
119         assertTrue(UTIL.ipv6PrefixFor(UTIL.ipv6AddressBytes(ipClass))._value.contains("/128"));
120         assertTrue(UTIL.ipv6PrefixFor(UTIL.inetAddressFor(ipClass))._value.contains("/128"));
121         assertTrue(UTIL.ipv6PrefixFor(ipClass)._value.contains("/128"));
122         assertTrue(UTIL.ipv6PrefixFor(ipClass, 16)._value.contains("/16"));
123
124         assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0)._value.equals("::0/0"));
125         assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 64)._value.equals("::/64"));
126         assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0, 0)._value.equals("::0/0"));
127         assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0, 32)._value.equals("::/32"));
128
129         assertTrue(UTIL.splitIpv6Prefix(new IpClass("::/32")).getKey()._value.equals("::"));
130         assertTrue(UTIL.splitIpv6Prefix(new IpClass("::/32")).getValue().equals(32));
131         assertArrayEquals(new byte[] { 0, 10, 0, 0, 0, 0, 0, 0, 0, 11, 0, 12, 0, 13, 0, 14, 64 },
132                 UTIL.ipv6PrefixToBytes(new IpClass("A::B:C:D:E/64")));
133     }
134
135     @Test
136     public void prefixTest() throws Exception {
137         assertTrue(UTIL.ipPrefixFor(UTIL.inetAddressFor(new IpClass("0.0.0.0")), 16)._value.equals("0.0.0.0/16"));
138         assertTrue(UTIL.ipPrefixFor(UTIL.inetAddressFor(new IpClass("::")), 64)
139                 ._value.equals("::/64"));
140
141         assertTrue(UTIL.ipPrefixFor(new byte[] { 0, 0, 0, 0 }, 16)._value.equals("0.0.0.0/16"));
142         assertTrue(UTIL.ipPrefixFor(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 64)
143                 ._value.equals("::/64"));
144     }
145
146     @Test
147     public void inetAddressTest() throws Exception {
148         assertTrue(UTIL.inetAddressFor(new IpClass("1.2.3.4")) instanceof Inet4Address);
149         assertTrue(UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329")) instanceof Inet6Address);
150     }
151 }