2 * Copyright (c) 2013 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.protocol.util;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16 import com.google.common.collect.Lists;
17 import com.google.common.net.InetAddresses;
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.InvocationTargetException;
22 import java.net.InetSocketAddress;
23 import java.net.UnknownHostException;
24 import java.util.List;
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
35 public class IPAddressesAndPrefixesTest {
39 assertEquals("123.123.123.123", new Ipv4Address("123.123.123.123").getValue());
40 assertEquals("2001::1", new Ipv6Address("2001::1").getValue());
44 public void test4() throws UnknownHostException {
45 assertNotNull(new IpPrefix(new Ipv4Prefix("123.123.123.123/32")).getIpv4Prefix());
46 assertNotNull(new IpPrefix(new Ipv6Prefix("2001::1/120")).getIpv6Prefix());
51 assertEquals("123.123.123.123/24", new Ipv4Prefix("123.123.123.123/24").getValue());
52 assertEquals("2001::1/120", new Ipv6Prefix("2001::1/120").getValue());
56 public void testPrefix4ForBytes() {
57 byte[] bytes = new byte[] { 123, 122, 4, 5 };
58 assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8));
59 assertArrayEquals(new byte[] { 102, 0, 0, 0, 8 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("102.0.0.0/8")));
61 bytes = new byte[] { (byte) 255, (byte) 255, 0, 0 };
62 assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
64 assertArrayEquals(new byte[] { (byte) 255, (byte) 255, 0, 0, 16 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("255.255.0.0/16")));
67 Ipv4Util.prefixForBytes(bytes, 200);
69 } catch (final IllegalArgumentException e) {
70 assertNull(e.getMessage());
75 public void testBytesForPrefix4Begin() {
76 byte[] bytes = new byte[] { 123, 122, 4, 5 };
77 assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8));
78 assertArrayEquals(new byte[] { 8, 102 }, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("102.0.0.0/8")));
80 bytes = new byte[] { (byte) 255, (byte) 255, 0, 0 };
81 assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
83 assertArrayEquals(new byte[] { 16, (byte) 255, (byte) 255 }, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("255.255.0.0/16")));
85 bytes = new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
86 assertEquals(new Ipv4Prefix("0.0.0.0/0"), Ipv4Util.prefixForBytes(bytes, 0));
88 bytes = new byte[] { (byte) 0 };
89 assertArrayEquals(bytes, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("0.0.0.0/0")));
93 public void testPrefixForByteBuf() {
94 final ByteBuf bb = Unpooled.wrappedBuffer(new byte[] { 0x0e, 123, 122, 0x40, 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } );
95 assertEquals(new Ipv4Prefix("123.122.0.0/14"), Ipv4Util.prefixForByteBuf(bb));
96 assertEquals(new Ipv6Prefix("2001::/64"), Ipv6Util.prefixForByteBuf(bb));
100 public void testAddressForByteBuf() {
101 final ByteBuf bb = Unpooled.wrappedBuffer(new byte[] { 123, 122, 4, 5, 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } );
102 assertEquals(new Ipv4Address("123.122.4.5"), Ipv4Util.addressForByteBuf(bb));
103 assertEquals(new Ipv6Address("2001::1"), Ipv6Util.addressForByteBuf(bb));
107 public void testByteBufForAddress() {
108 final ByteBuf bb4 = Unpooled.wrappedBuffer(new byte[] { 123, 122, 4, 5} );
109 final ByteBuf bb6 = Unpooled.wrappedBuffer(new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } );
110 assertEquals(bb4, Ipv4Util.byteBufForAddress(new Ipv4Address("123.122.4.5")));
111 assertEquals(bb6, Ipv6Util.byteBufForAddress(new Ipv6Address("2001::1")));
115 public void testBytesForAddress() {
116 assertArrayEquals(new byte[] { 12, 58, (byte) 201, 99 }, Ipv4Util.bytesForAddress(new Ipv4Address("12.58.201.99")));
117 assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
118 0x01 }, Ipv6Util.bytesForAddress(new Ipv6Address("2001::1")));
122 public void testPrefix6ForBytes() {
123 final byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
124 assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
125 assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126 0x00, 0x00, 0x40 }, Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/64")));
127 assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
128 0x00, 0x00, (byte)0x80 }, Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/128")));
131 Ipv6Util.prefixForBytes(bytes, 200);
133 } catch (final IllegalArgumentException e) {
134 assertNull(e.getMessage());
139 public void testBytesForPrefix6Begin() {
140 byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
141 assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
142 assertArrayEquals(new byte[] { 0x40, 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 }, Ipv6Util.bytesForPrefixBegin(new Ipv6Prefix("2001:db8:1:2::/64")));
144 bytes = new byte[] { (byte) 0 };
145 assertEquals(new Ipv6Prefix("::/0"), Ipv6Util.prefixForBytes(bytes, 0));
146 assertArrayEquals(bytes, Ipv6Util.bytesForPrefixBegin(new Ipv6Prefix("::/0")));
150 public void testPrefixLength() {
151 assertEquals(8, Ipv4Util.getPrefixLengthBytes("2001:db8:1:2::/64"));
152 assertEquals(3, Ipv4Util.getPrefixLengthBytes("172.168.3.0/22"));
156 public void testPrefixList4ForBytes() {
157 final byte[] bytes = new byte[] { 22, (byte) 172, (byte) 168, 3, 8, 12, 32, (byte) 192, (byte) 168, 35, 100 };
158 List<Ipv4Prefix> prefs = Ipv4Util.prefixListForBytes(bytes);
160 Lists.newArrayList(new Ipv4Prefix("172.168.3.0/22"), new Ipv4Prefix("12.0.0.0/8"), new Ipv4Prefix("192.168.35.100/32")),
163 prefs = Ipv4Util.prefixListForBytes(new byte[] {});
164 assertTrue(prefs.isEmpty());
168 public void testPrefixList6ForBytes() {
169 final byte[] bytes = new byte[] { 0x40, 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x40, 0x20, 0x01, 0x0d, (byte) 0xb8,
170 0x00, 0x01, 0x00, 0x01, };
171 List<Ipv6Prefix> prefs = Ipv6Util.prefixListForBytes(bytes);
172 assertEquals(prefs, Lists.newArrayList(new Ipv6Prefix("2001:db8:1:2::/64"), new Ipv6Prefix("2001:db8:1:1::/64")));
174 prefs = Ipv6Util.prefixListForBytes(new byte[] {});
175 assertTrue(prefs.isEmpty());
179 public void testFullFormOfIpv6() {
180 assertEquals(new Ipv6Address("0:0:0:0:0:0:0:1"), Ipv6Util.getFullForm(new Ipv6Address("::1")));
183 @Test(expected=UnsupportedOperationException.class)
184 public void testIpv4UtilPrivateConstructor() throws Throwable {
185 final Constructor<Ipv4Util> c = Ipv4Util.class.getDeclaredConstructor();
186 c.setAccessible(true);
189 } catch (final InvocationTargetException e) {
194 @Test(expected=UnsupportedOperationException.class)
195 public void testIpv6UtilPrivateConstructor() throws Throwable {
196 final Constructor<Ipv6Util> c = Ipv6Util.class.getDeclaredConstructor();
197 c.setAccessible(true);
200 } catch (final InvocationTargetException e) {
206 public void testInetAddressToIpAddress() {
207 final IpAddress ipAddress = Ipv4Util.getIpAddress(InetAddresses.forString("123.42.13.8"));
208 Assert.assertNotNull(ipAddress.getIpv4Address());
209 Assert.assertEquals(new Ipv4Address("123.42.13.8"), ipAddress.getIpv4Address());
211 final IpAddress ipAddress2 = Ipv4Util.getIpAddress(InetAddresses.forString("2001:db8:1:2::"));
212 Assert.assertNotNull(ipAddress2.getIpv6Address());
213 Assert.assertEquals(new Ipv6Address("2001:db8:1:2::"), ipAddress2.getIpv6Address());
217 public void testToInetSocketAddress() {
218 final InetSocketAddress isa = Ipv4Util.toInetSocketAddress(new IpAddress(new Ipv4Address("123.42.13.8")), new PortNumber(10));
219 Assert.assertEquals(10, isa.getPort());
220 Assert.assertEquals("123.42.13.8", InetAddresses.toAddrString(isa.getAddress()));
222 final InetSocketAddress isa2 = Ipv4Util.toInetSocketAddress(new IpAddress(new Ipv6Address("2001:db8:1:2::")), new PortNumber(10));
223 Assert.assertEquals(10, isa2.getPort());
224 Assert.assertEquals("2001:db8:1:2::", InetAddresses.toAddrString(isa2.getAddress()));