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