2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.model.ietf.util;
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;
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;
27 public class AbstractIetfInetUtilTest {
28 private static final IpUtil UTIL = new IpUtil();
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);
36 private static void assertV4Equals(final String literal) {
37 assertV4Equals(literal, "");
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");
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");
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());
57 assertEquals(new IpClass("FE80::2002:B3FF:FE1E:8329").getValue().toLowerCase(),
59 UTIL.ipv6AddressBytes(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase());
60 assertNotEquals(new IpClass("FEFF::2002:B3FF:FE1E:8329").getValue().toLowerCase(),
62 UTIL.ipv6AddressBytes(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase());
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());
69 assertEquals(new IpClass("FE80::2002:B3FF:FE1E:8329").getValue().toLowerCase(),
71 UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase());
72 assertNotEquals(new IpClass("FEFF::2002:B3FF:FE1E:8329").getValue().toLowerCase(),
74 UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase());
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());
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());
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());
100 public void illegalAddressforPrefixTest() {
101 final InetAddress adr = mock(InetAddress.class);
102 doReturn("testAddress").when(adr).toString();
104 final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
105 () -> UTIL.ipPrefixFor(adr, 0));
106 assertEquals("Unhandled address testAddress", ex.getMessage());
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());
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());
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")));
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());
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());
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")));
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})
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());
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());
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));
172 public void inet4AddressForWithExceptionTest() {
173 final IpClass ipClass = mock(IpClass.class);
174 doReturn("testClass").when(ipClass).toString();
176 throw new UnknownHostException();
177 }).when(ipClass).getValue();
179 final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
180 () -> UTIL.inet4AddressFor(ipClass));
181 assertEquals("Invalid address testClass", ex.getMessage());
185 public void inet6AddressForWithExceptionTest() {
186 final IpClass ipClass = mock(IpClass.class);
187 doReturn("testClass").when(ipClass).toString();
189 throw new UnknownHostException();
190 }).when(ipClass).getValue();
192 final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
193 () -> UTIL.inet6AddressFor(ipClass));
194 assertEquals("Invalid address testClass", ex.getMessage());
198 public void testIpv4AddressForBits() {
199 assertEquals("1.2.3.4", UTIL.ipv4AddressFor(0x01020304).getValue());
200 assertEquals("255.255.255.255", UTIL.ipv4AddressFor(0xFFFFFFFF).getValue());
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")));
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")));