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;
17 import com.google.common.collect.Lists;
18 import com.google.common.net.InetAddresses;
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.Unpooled;
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.InvocationTargetException;
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.rev100924.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
34 public class IPAddressesAndPrefixesTest {
38 assertEquals("123.123.123.123", new Ipv4Address("123.123.123.123").getValue());
39 assertEquals("2001::1", new Ipv6Address("2001::1").getValue());
43 public void test4() throws UnknownHostException {
44 assertNotNull(new IpPrefix(new Ipv4Prefix("123.123.123.123/32")).getIpv4Prefix());
45 assertNotNull(new IpPrefix(new Ipv6Prefix("2001::1/120")).getIpv6Prefix());
50 assertEquals("123.123.123.123/24", new Ipv4Prefix("123.123.123.123/24").getValue());
51 assertEquals("2001::1/120", new Ipv6Prefix("2001::1/120").getValue());
55 public void testPrefix4ForBytes() {
56 byte[] bytes = new byte[] { 123, 122, 4, 5 };
57 assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8));
58 assertArrayEquals(new byte[] { 102, 0, 0, 0, 8 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("102.0.0.0/8")));
60 bytes = new byte[] { (byte) 255, (byte) 255, 0, 0 };
61 assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
63 assertArrayEquals(new byte[] { (byte) 255, (byte) 255, 0, 0, 16 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("255.255.0.0/16")));
66 Ipv4Util.prefixForBytes(bytes, 200);
68 } catch (final IllegalArgumentException e) {
69 assertNull(e.getMessage());
74 public void testBytesForPrefix4Begin() {
75 byte[] bytes = new byte[] { 123, 122, 4, 5 };
76 assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8));
77 assertArrayEquals(new byte[] { 8, 102 }, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("102.0.0.0/8")));
79 bytes = new byte[] { (byte) 255, (byte) 255, 0, 0 };
80 assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
82 assertArrayEquals(new byte[] { 16, (byte) 255, (byte) 255 }, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("255.255.0.0/16")));
84 bytes = new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
85 assertEquals(new Ipv4Prefix("0.0.0.0/0"), Ipv4Util.prefixForBytes(bytes, 0));
87 bytes = new byte[] { (byte) 0 };
88 assertArrayEquals(bytes, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("0.0.0.0/0")));
92 public void testPrefixForByteBuf() {
93 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 } );
94 assertEquals(new Ipv4Prefix("123.122.0.0/14"), Ipv4Util.prefixForByteBuf(bb));
95 assertEquals(new Ipv6Prefix("2001::/64"), Ipv6Util.prefixForByteBuf(bb));
99 public void testAddressForByteBuf() {
100 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 } );
101 assertEquals(new Ipv4Address("123.122.4.5"), Ipv4Util.addressForByteBuf(bb));
102 assertEquals(new Ipv6Address("2001::1"), Ipv6Util.addressForByteBuf(bb));
106 public void testByteBufForAddress() {
107 final ByteBuf bb4 = Unpooled.wrappedBuffer(new byte[] { 123, 122, 4, 5} );
108 final ByteBuf bb6 = Unpooled.wrappedBuffer(new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } );
109 assertEquals(bb4, Ipv4Util.byteBufForAddress(new Ipv4Address("123.122.4.5")));
110 assertEquals(bb6, Ipv6Util.byteBufForAddress(new Ipv6Address("2001::1")));
114 public void testBytesForAddress() {
115 assertArrayEquals(new byte[] { 12, 58, (byte) 201, 99 }, Ipv4Util.bytesForAddress(new Ipv4Address("12.58.201.99")));
116 assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
117 0x01 }, Ipv6Util.bytesForAddress(new Ipv6Address("2001::1")));
121 public void testPrefix6ForBytes() {
122 final byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
123 assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
124 assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
125 0x00, 0x00, 0x40 }, Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/64")));
128 Ipv6Util.prefixForBytes(bytes, 200);
130 } catch (final IllegalArgumentException e) {
131 assertNull(e.getMessage());
136 public void testBytesForPrefix6Begin() {
137 byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
138 assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
139 assertArrayEquals(new byte[] { 0x40, 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 }, Ipv6Util.bytesForPrefixBegin(new Ipv6Prefix("2001:db8:1:2::/64")));
141 bytes = new byte[] { (byte) 0 };
142 assertEquals(new Ipv6Prefix("::/0"), Ipv6Util.prefixForBytes(bytes, 0));
143 assertArrayEquals(bytes, Ipv6Util.bytesForPrefixBegin(new Ipv6Prefix("::/0")));
147 public void testPrefixLength() {
148 assertEquals(8, Ipv4Util.getPrefixLengthBytes("2001:db8:1:2::/64"));
149 assertEquals(3, Ipv4Util.getPrefixLengthBytes("172.168.3.0/22"));
153 public void testPrefixList4ForBytes() {
154 final byte[] bytes = new byte[] { 22, (byte) 172, (byte) 168, 3, 8, 12, 32, (byte) 192, (byte) 168, 35, 100 };
155 List<Ipv4Prefix> prefs = Ipv4Util.prefixListForBytes(bytes);
157 Lists.newArrayList(new Ipv4Prefix("172.168.3.0/22"), new Ipv4Prefix("12.0.0.0/8"), new Ipv4Prefix("192.168.35.100/32")),
160 prefs = Ipv4Util.prefixListForBytes(new byte[] {});
161 assertTrue(prefs.isEmpty());
165 public void testPrefixList6ForBytes() {
166 final byte[] bytes = new byte[] { 0x40, 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x40, 0x20, 0x01, 0x0d, (byte) 0xb8,
167 0x00, 0x01, 0x00, 0x01, };
168 List<Ipv6Prefix> prefs = Ipv6Util.prefixListForBytes(bytes);
169 assertEquals(prefs, Lists.newArrayList(new Ipv6Prefix("2001:db8:1:2::/64"), new Ipv6Prefix("2001:db8:1:1::/64")));
171 prefs = Ipv6Util.prefixListForBytes(new byte[] {});
172 assertTrue(prefs.isEmpty());
175 @Test(expected=UnsupportedOperationException.class)
176 public void testIpv4UtilPrivateConstructor() throws Throwable {
177 final Constructor<Ipv4Util> c = Ipv4Util.class.getDeclaredConstructor();
178 c.setAccessible(true);
181 } catch (final InvocationTargetException e) {
186 @Test(expected=UnsupportedOperationException.class)
187 public void testIpv6UtilPrivateConstructor() throws Throwable {
188 final Constructor<Ipv6Util> c = Ipv6Util.class.getDeclaredConstructor();
189 c.setAccessible(true);
192 } catch (final InvocationTargetException e) {
198 public void testInetAddressToIpAddress() {
199 final IpAddress ipAddress = Ipv4Util.getIpAddress(InetAddresses.forString("123.42.13.8"));
200 Assert.assertNotNull(ipAddress.getIpv4Address());
201 Assert.assertEquals(new Ipv4Address("123.42.13.8"), ipAddress.getIpv4Address());
203 final IpAddress ipAddress2 = Ipv4Util.getIpAddress(InetAddresses.forString("2001:db8:1:2::"));
204 Assert.assertNotNull(ipAddress2.getIpv6Address());
205 Assert.assertEquals(new Ipv6Address("2001:db8:1:2::"), ipAddress2.getIpv6Address());