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.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;
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;
25 public class AbstractIetfInetUtilTest {
26 private static final IpUtil UTIL = new IpUtil();
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);
34 private static void assertV4Equals(final String literal) {
35 assertV4Equals(literal, "");
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");
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");
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());
55 assertEquals(new IpClass("FE80::2002:B3FF:FE1E:8329").getValue().toLowerCase(),
57 UTIL.ipv6AddressBytes(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase());
58 assertNotEquals(new IpClass("FEFF::2002:B3FF:FE1E:8329").getValue().toLowerCase(),
60 UTIL.ipv6AddressBytes(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase());
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());
67 assertEquals(new IpClass("FE80::2002:B3FF:FE1E:8329").getValue().toLowerCase(),
69 UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase());
70 assertNotEquals(new IpClass("FEFF::2002:B3FF:FE1E:8329").getValue().toLowerCase(),
72 UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase());
75 @Test(expected = IllegalArgumentException.class)
76 public void illegalArrayLengthForAddressTest() throws Exception {
77 UTIL.ipAddressFor(new byte[] { 0, 0, 0 });
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);
87 @Test(expected = IllegalArgumentException.class)
88 public void illegalArrayLengthforPrefixTest() throws Exception {
89 UTIL.ipPrefixFor(new byte[] { 0, 0, 0 }, 0);
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);
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"));
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"));
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")));
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"));
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"));
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")));
141 // verify that an IPv4-mapped IPv6 address gets parsed as an IPv6 address
142 assertEquals("::ffff:ab0:eb", UTIL.ipv6AddressFor(
143 new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xff, (byte) 0xff, 0x0a, (byte) 0xb0, 0, (byte) 0xeb})
148 public void prefixTest() throws Exception {
149 assertTrue(UTIL.ipPrefixFor(UTIL.inetAddressFor(new IpClass("0.0.0.0")), 16).getValue().equals("0.0.0.0/16"));
150 assertTrue(UTIL.ipPrefixFor(UTIL.inetAddressFor(new IpClass("::")), 64)
151 .getValue().equals("::/64"));
153 assertTrue(UTIL.ipPrefixFor(new byte[] { 0, 0, 0, 0 }, 16).getValue().equals("0.0.0.0/16"));
154 assertTrue(UTIL.ipPrefixFor(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 64)
155 .getValue().equals("::/64"));
159 public void inetAddressTest() throws Exception {
160 assertTrue(UTIL.inetAddressFor(new IpClass("1.2.3.4")) instanceof Inet4Address);
161 assertTrue(UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329")) instanceof Inet6Address);
164 @Test(expected = IllegalArgumentException.class)
165 public void inet4AddressForWithExceptionTest() throws Exception {
166 final IpClass ipClass = mock(IpClass.class);
167 doReturn("testClass").when(ipClass).toString();
169 throw new UnknownHostException();
170 }).when(ipClass).getValue();
171 UTIL.inet4AddressFor(ipClass);
174 @Test(expected = IllegalArgumentException.class)
175 public void inet6AddressForWithExceptionTest() throws Exception {
176 final IpClass ipClass = mock(IpClass.class);
177 doReturn("testClass").when(ipClass).toString();
179 throw new UnknownHostException();
180 }).when(ipClass).getValue();
181 UTIL.inet6AddressFor(ipClass);
185 public void testIpv4AddressForBits() {
186 assertEquals("1.2.3.4", UTIL.ipv4AddressFor(0x01020304).getValue());
187 assertEquals("255.255.255.255", UTIL.ipv4AddressFor(0xFFFFFFFF).getValue());
191 public void testIpv4AddressNoZoneForBits() {
192 assertEquals("1.2.3.4", UTIL.ipv4AddressNoZoneFor(0x01020304).getValue());
193 assertEquals("255.255.255.255", UTIL.ipv4AddressNoZoneFor(0xFFFFFFFF).getValue());
197 public void testIpv4AddressBits() {
198 assertEquals(0x01020304, UTIL.ipv4AddressBits(new IpClass("1.2.3.4")));
199 assertEquals(0xFFFFFFFF, UTIL.ipv4AddressBits(new IpClass("255.255.255.255")));
203 public void testIpv4AddressNoZoneBits() {
204 assertEquals(0x01020304, UTIL.ipv4AddressNoZoneBits(new IpClass("1.2.3.4")));
205 assertEquals(0xFFFFFFFF, UTIL.ipv4AddressNoZoneBits(new IpClass("255.255.255.255")));