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.net.InetSocketAddress;
22 import java.net.UnknownHostException;
23 import java.util.List;
24 import org.junit.Test;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
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;
34 import org.opendaylight.yangtools.yang.common.Uint16;
36 public class IPAddressesAndPrefixesTest {
40 assertEquals("123.123.123.123", new Ipv4Address("123.123.123.123").getValue());
41 assertEquals("2001::1", new Ipv6Address("2001::1").getValue());
45 public void test4() throws UnknownHostException {
46 assertNotNull(new IpPrefix(new Ipv4Prefix("123.123.123.123/32")).getIpv4Prefix());
47 assertNotNull(new IpPrefix(new Ipv6Prefix("2001::1/120")).getIpv6Prefix());
52 assertEquals("123.123.123.123/24", new Ipv4Prefix("123.123.123.123/24").getValue());
53 assertEquals("2001::1/120", new Ipv6Prefix("2001::1/120").getValue());
57 public void testPrefix4ForBytes() {
58 byte[] bytes = new byte[]{123, 122, 4, 5};
59 assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8));
60 assertArrayEquals(new byte[]{102, 0, 0, 0, 8}, Ipv4Util.bytesForPrefix(new Ipv4Prefix("102.0.0.0/8")));
62 bytes = new byte[]{(byte) 255, (byte) 255, 0, 0};
63 assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
65 assertArrayEquals(new byte[]{(byte) 255, (byte) 255, 0, 0, 16},
66 Ipv4Util.bytesForPrefix(new Ipv4Prefix("255.255.0.0/16")));
69 Ipv4Util.prefixForBytes(bytes, 200);
71 } catch (final IllegalArgumentException exc) {
72 assertNull(exc.getMessage());
77 public void testBytesForPrefix4Begin() {
78 byte[] bytes = new byte[]{123, 122, 4, 5};
79 assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8));
81 bytes = new byte[]{(byte) 255, (byte) 255, 0, 0};
82 assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 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));
89 public void testPrefixForByteBuf() {
90 final ByteBuf bb = Unpooled.wrappedBuffer(new byte[]{
92 0x40, 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95 (byte) 0x80, 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
96 (byte) 0x88, (byte) 0x89
98 assertEquals(new Ipv4Prefix("123.122.0.0/14"), Ipv4Util.prefixForByteBuf(bb));
99 assertEquals(new Ipv6Prefix("2001::/64"), Ipv6Util.prefixForByteBuf(bb));
100 assertEquals(new Ipv4Prefix("0.0.0.0/0"), Ipv4Util.prefixForByteBuf(bb));
101 assertEquals(new Ipv6Prefix("::/0"), Ipv6Util.prefixForByteBuf(bb));
102 // test prefix length 128, as its signed byte value is -128
103 assertEquals(new Ipv6Prefix("2001:4860:4860:1:20::8889/128"), Ipv6Util.prefixForByteBuf(bb));
107 public void testAddressForByteBuf() {
108 final ByteBuf bb = Unpooled.wrappedBuffer(new byte[]{123, 122, 4, 5, 0x20, (byte) 0x01, 0x00, 0x00, 0x00,
109 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01});
110 assertEquals(new Ipv4Address("123.122.4.5"), Ipv4Util.addressForByteBuf(bb));
111 assertEquals(new Ipv6Address("2001::1"), Ipv6Util.addressForByteBuf(bb));
115 public void testByteBufForAddress() {
116 final ByteBuf bb4 = Unpooled.wrappedBuffer(new byte[]{123, 122, 4, 5});
117 final ByteBuf bb6 = Unpooled.wrappedBuffer(new byte[]{0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
118 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01});
119 assertEquals(bb4, Ipv4Util.byteBufForAddress(new Ipv4AddressNoZone("123.122.4.5")));
120 assertEquals(bb6, Ipv6Util.byteBufForAddress(new Ipv6AddressNoZone("2001::1")));
124 public void testBytesForAddress() {
125 assertArrayEquals(new byte[]{12, 58, (byte) 201, 99},
126 Ipv4Util.bytesForAddress(new Ipv4AddressNoZone("12.58.201.99")));
127 assertArrayEquals(new byte[]{0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
128 0x00, 0x00, 0x00, 0x01}, Ipv6Util.bytesForAddress(new Ipv6AddressNoZone("2001::1")));
132 public void testPrefix6ForBytes() {
133 final byte[] bytes = new byte[]{0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02};
134 assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
135 assertArrayEquals(new byte[]{0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00,
136 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40},
137 Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/64")));
138 assertArrayEquals(new byte[]{0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00,
139 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x80},
140 Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/128")));
143 Ipv6Util.prefixForBytes(bytes, 200);
145 } catch (final IllegalArgumentException exc) {
146 assertNull(exc.getMessage());
151 public void testBytesForPrefix6Begin() {
152 byte[] bytes = new byte[]{0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02};
153 assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
155 bytes = new byte[]{(byte) 0};
156 assertEquals(new Ipv6Prefix("::/0"), Ipv6Util.prefixForBytes(bytes, 0));
160 public void testPrefixLength() {
161 assertEquals(8, Ipv4Util.getPrefixLengthBytes("2001:db8:1:2::/64"));
162 assertEquals(3, Ipv4Util.getPrefixLengthBytes("172.168.3.0/22"));
166 public void testPrefixList4ForBytes() {
167 final byte[] bytes = new byte[]{22, (byte) 172, (byte) 168, 3, 8, 12, 32, (byte) 192, (byte) 168, 35, 100};
168 List<Ipv4Prefix> prefs = Ipv4Util.prefixListForBytes(bytes);
170 Lists.newArrayList(new Ipv4Prefix("172.168.3.0/22"), new Ipv4Prefix("12.0.0.0/8"),
171 new Ipv4Prefix("192.168.35.100/32")), prefs);
173 prefs = Ipv4Util.prefixListForBytes(new byte[]{});
174 assertTrue(prefs.isEmpty());
178 public void testPrefixList6ForBytes() {
179 final byte[] bytes = new byte[]{0x40, 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x40, 0x20,
180 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x01,};
181 List<Ipv6Prefix> prefs = Ipv6Util.prefixListForBytes(bytes);
182 assertEquals(prefs, Lists.newArrayList(new Ipv6Prefix("2001:db8:1:2::/64"),
183 new Ipv6Prefix("2001:db8:1:1::/64")));
185 prefs = Ipv6Util.prefixListForBytes(new byte[]{});
186 assertTrue(prefs.isEmpty());
190 public void testFullFormOfIpv6() {
191 assertEquals(new Ipv6Address("0:0:0:0:0:0:0:1"), Ipv6Util.getFullForm(new Ipv6AddressNoZone("::1")));
195 public void testInetAddressToIpAddress() {
196 final IpAddressNoZone ipAddress = Ipv4Util.getIpAddress(InetAddresses.forString("123.42.13.8"));
197 assertNotNull(ipAddress.getIpv4AddressNoZone());
198 assertEquals(new Ipv4Address("123.42.13.8"), ipAddress.getIpv4AddressNoZone());
200 final IpAddressNoZone ipAddress2 = Ipv4Util.getIpAddress(InetAddresses.forString("2001:db8:1:2::"));
201 assertNotNull(ipAddress2.getIpv6AddressNoZone());
202 assertEquals(new Ipv6Address("2001:db8:1:2::"), ipAddress2.getIpv6AddressNoZone());
206 public void testToInetSocketAddress() {
207 final InetSocketAddress isa = Ipv4Util.toInetSocketAddress(
208 new IpAddressNoZone(new Ipv4AddressNoZone("123.42.13.8")), new PortNumber(Uint16.TEN));
209 assertEquals(10, isa.getPort());
210 assertEquals("123.42.13.8", InetAddresses.toAddrString(isa.getAddress()));
212 final InetSocketAddress isa2 = Ipv4Util.toInetSocketAddress(
213 new IpAddressNoZone(new Ipv6AddressNoZone("2001:db8:1:2::")), new PortNumber(Uint16.TEN));
214 assertEquals(10, isa2.getPort());
215 assertEquals("2001:db8:1:2::", InetAddresses.toAddrString(isa2.getAddress()));