dee4d2d45dcdca56153abd6777ec6027d909f35c
[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.doAnswer;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17
18 import com.google.common.net.InetAddresses;
19 import java.net.Inet4Address;
20 import java.net.Inet6Address;
21 import java.net.InetAddress;
22 import java.net.UnknownHostException;
23 import org.junit.Test;
24
25 public class AbstractIetfInetUtilTest {
26     private static final IpUtil UTIL = new IpUtil();
27
28     private static void assertV4Equals(final String literal, final String append) {
29         final byte[] expected = InetAddresses.forString(literal).getAddress();
30         final byte[] actual = UTIL.ipv4AddressBytes(new IpClass(literal + append));
31         assertArrayEquals(expected, actual);
32     }
33
34     private static void assertV4Equals(final String literal) {
35         assertV4Equals(literal, "");
36     }
37
38     @Test
39     public void testIpToBytesAndBack() throws Exception {
40         assertV4Equals("1.2.3.4");
41         assertV4Equals("12.23.34.45");
42         assertV4Equals("255.254.253.252");
43         assertV4Equals("128.16.0.127");
44
45         assertV4Equals("1.2.3.4", "%5");
46         assertV4Equals("12.23.34.45", "%5");
47         assertV4Equals("255.254.253.252", "%5");
48         assertV4Equals("128.16.0.127", "%5");
49
50         assertEquals(new IpClass("1.2.3.4").getValue().toLowerCase(),
51                 UTIL.ipAddressFor(UTIL.ipv4AddressBytes(new IpClass("1.2.3.4"))).getValue().toLowerCase());
52         assertNotEquals(new IpClass("2.3.4.5").getValue().toLowerCase(),
53                 UTIL.ipAddressFor(UTIL.ipv4AddressBytes(new IpClass("1.2.3.4"))).getValue().toLowerCase());
54
55         assertEquals(new IpClass("FE80::2002:B3FF:FE1E:8329").getValue().toLowerCase(),
56                 UTIL.ipAddressFor(
57                         UTIL.ipv6AddressBytes(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase());
58         assertNotEquals(new IpClass("FEFF::2002:B3FF:FE1E:8329").getValue().toLowerCase(),
59                 UTIL.ipAddressFor(
60                         UTIL.ipv6AddressBytes(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase());
61
62         assertEquals(new IpClass("1.2.3.4").getValue().toLowerCase(),
63                 UTIL.ipAddressFor(UTIL.inetAddressFor(new IpClass("1.2.3.4"))).getValue().toLowerCase());
64         assertNotEquals(new IpClass("2.3.4.5").getValue().toLowerCase(),
65                 UTIL.ipAddressFor(UTIL.inetAddressFor(new IpClass("1.2.3.4"))).getValue().toLowerCase());
66
67         assertEquals(new IpClass("FE80::2002:B3FF:FE1E:8329").getValue().toLowerCase(),
68                 UTIL.ipAddressFor(
69                         UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase());
70         assertNotEquals(new IpClass("FEFF::2002:B3FF:FE1E:8329").getValue().toLowerCase(),
71                 UTIL.ipAddressFor(
72                         UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase());
73     }
74
75     @Test(expected = IllegalArgumentException.class)
76     public void illegalArrayLengthForAddressTest() throws Exception {
77         UTIL.ipAddressFor(new byte[] { 0, 0, 0 });
78     }
79
80     @Test(expected = IllegalArgumentException.class)
81     public void unhandledAddressTest() throws Exception {
82         final InetAddress adr = mock(InetAddress.class);
83         doReturn("testAddress").when(adr).toString();
84         UTIL.ipAddressFor(adr);
85     }
86
87     @Test(expected = IllegalArgumentException.class)
88     public void illegalArrayLengthforPrefixTest() throws Exception {
89         UTIL.ipPrefixFor(new byte[] { 0, 0, 0 }, 0);
90     }
91
92     @Test(expected = IllegalArgumentException.class)
93     public void illegalAddressforPrefixTest() throws Exception {
94         final InetAddress adr = mock(InetAddress.class);
95         doReturn("testAddress").when(adr).toString();
96         UTIL.ipPrefixFor(adr, 0);
97     }
98
99     @Test
100     public void ipv4Tests() throws Exception {
101         IpClass ipClass = new IpClass("1.2.3.4/16");
102         assertEquals("1.2.3.4", UTIL.ipv4AddressFrom(ipClass).getValue());
103         assertTrue(UTIL.ipv4PrefixFor(UTIL.ipv4AddressBytes(ipClass)).getValue().contains("/32"));
104         ipClass = new IpClass("1.2.3.4");
105         assertTrue(UTIL.ipv4PrefixFor(UTIL.inetAddressFor(ipClass)).getValue().contains("/32"));
106         assertTrue(UTIL.ipv4PrefixFor(ipClass).getValue().contains("/32"));
107         assertTrue(UTIL.ipv4PrefixFor(ipClass, 16).getValue().contains("/16"));
108
109         assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0).getValue().equals("0.0.0.0/0"));
110         assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 32).getValue().equals("1.2.3.4/32"));
111         assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0, 0).getValue().equals("0.0.0.0/0"));
112         assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0, 32).getValue().equals("1.2.3.4/32"));
113         assertTrue(UTIL.ipv4PrefixForShort(new byte[] { 1, 2, 3, 4, 5 }, 1, 32).getValue().equals("2.3.4.5/32"));
114         assertTrue(UTIL.ipv4PrefixForShort(new byte[] { 1, 2, 3, 4, 5 }, 0, 1).getValue().equals("1.0.0.0/1"));
115
116         assertTrue(UTIL.splitIpv4Prefix(new IpClass("1.2.3.4/16")).getKey().getValue().equals("1.2.3.4"));
117         assertTrue(UTIL.splitIpv4Prefix(new IpClass("1.2.3.4/16")).getValue().equals(16));
118         assertArrayEquals(new byte[] { 1,2,3,4,16 }, UTIL.ipv4PrefixToBytes(new IpClass("1.2.3.4/16")));
119     }
120
121     @Test
122     public void ipv6Tests() throws Exception {
123         IpClass ipClass = new IpClass("::0/128");
124         assertEquals("::0", UTIL.ipv6AddressFrom(ipClass).getValue());
125         ipClass = new IpClass("::0");
126         assertTrue(UTIL.ipv6PrefixFor(UTIL.ipv6AddressBytes(ipClass)).getValue().contains("/128"));
127         assertTrue(UTIL.ipv6PrefixFor(UTIL.inetAddressFor(ipClass)).getValue().contains("/128"));
128         assertTrue(UTIL.ipv6PrefixFor(ipClass).getValue().contains("/128"));
129         assertTrue(UTIL.ipv6PrefixFor(ipClass, 16).getValue().contains("/16"));
130
131         assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0).getValue().equals("::0/0"));
132         assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 64).getValue().equals("::/64"));
133         assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0, 0).getValue().equals("::0/0"));
134         assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0, 32).getValue().equals("::/32"));
135
136         assertTrue(UTIL.splitIpv6Prefix(new IpClass("::/32")).getKey().getValue().equals("::"));
137         assertTrue(UTIL.splitIpv6Prefix(new IpClass("::/32")).getValue().equals(32));
138         assertArrayEquals(new byte[] { 0, 10, 0, 0, 0, 0, 0, 0, 0, 11, 0, 12, 0, 13, 0, 14, 64 },
139                 UTIL.ipv6PrefixToBytes(new IpClass("A::B:C:D:E/64")));
140     }
141
142     @Test
143     public void prefixTest() throws Exception {
144         assertTrue(UTIL.ipPrefixFor(UTIL.inetAddressFor(new IpClass("0.0.0.0")), 16).getValue().equals("0.0.0.0/16"));
145         assertTrue(UTIL.ipPrefixFor(UTIL.inetAddressFor(new IpClass("::")), 64)
146                 .getValue().equals("::/64"));
147
148         assertTrue(UTIL.ipPrefixFor(new byte[] { 0, 0, 0, 0 }, 16).getValue().equals("0.0.0.0/16"));
149         assertTrue(UTIL.ipPrefixFor(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 64)
150                 .getValue().equals("::/64"));
151     }
152
153     @Test
154     public void inetAddressTest() throws Exception {
155         assertTrue(UTIL.inetAddressFor(new IpClass("1.2.3.4")) instanceof Inet4Address);
156         assertTrue(UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329")) instanceof Inet6Address);
157     }
158
159     @Test(expected = IllegalArgumentException.class)
160     public void inet4AddressForWithExceptionTest() throws Exception {
161         final IpClass ipClass = mock(IpClass.class);
162         doReturn("testClass").when(ipClass).toString();
163         doAnswer(inv -> {
164             throw new UnknownHostException();
165         }).when(ipClass).getValue();
166         UTIL.inet4AddressFor(ipClass);
167     }
168
169     @Test(expected = IllegalArgumentException.class)
170     public void inet6AddressForWithExceptionTest() throws Exception {
171         final IpClass ipClass = mock(IpClass.class);
172         doReturn("testClass").when(ipClass).toString();
173         doAnswer(inv -> {
174             throw new UnknownHostException();
175         }).when(ipClass).getValue();
176         UTIL.inet6AddressFor(ipClass);
177     }
178
179     @Test
180     public void testIpv4AddressForBits() {
181         assertEquals("1.2.3.4", UTIL.ipv4AddressFor(0x01020304).getValue());
182         assertEquals("255.255.255.255", UTIL.ipv4AddressFor(0xFFFFFFFF).getValue());
183     }
184
185     @Test
186     public void testIpv4AddressNoZoneForBits() {
187         assertEquals("1.2.3.4", UTIL.ipv4AddressNoZoneFor(0x01020304).getValue());
188         assertEquals("255.255.255.255", UTIL.ipv4AddressNoZoneFor(0xFFFFFFFF).getValue());
189     }
190
191     @Test
192     public void testIpv4AddressBits() {
193         assertEquals(0x01020304, UTIL.ipv4AddressBits(new IpClass("1.2.3.4")));
194         assertEquals(0xFFFFFFFF, UTIL.ipv4AddressBits(new IpClass("255.255.255.255")));
195     }
196
197     @Test
198     public void testIpv4AddressNoZoneBits() {
199         assertEquals(0x01020304, UTIL.ipv4AddressNoZoneBits(new IpClass("1.2.3.4")));
200         assertEquals(0xFFFFFFFF, UTIL.ipv4AddressNoZoneBits(new IpClass("255.255.255.255")));
201     }
202 }