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.fail;
15 import com.google.common.collect.Lists;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.lang.reflect.Constructor;
19 import java.lang.reflect.InvocationTargetException;
20 import java.net.UnknownHostException;
21 import java.util.List;
22 import org.junit.Test;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
29 public class IPAddressesAndPrefixesTest {
33 assertEquals("123.123.123.123", new Ipv4Address("123.123.123.123").getValue());
34 assertEquals("2001::1", new Ipv6Address("2001::1").getValue());
38 public void test4() throws UnknownHostException {
39 assertNotNull(new IpPrefix(new Ipv4Prefix("123.123.123.123")).getIpv4Prefix());
40 assertNotNull(new IpPrefix(new Ipv6Prefix("2001::1")).getIpv6Prefix());
45 assertEquals("123.123.123.123/24", new Ipv4Prefix("123.123.123.123/24").getValue());
46 assertEquals("2001::1/120", new Ipv6Prefix("2001::1/120").getValue());
50 public void testPrefix4ForBytes() {
51 byte[] bytes = new byte[] { 123, 122, 4, 5 };
52 assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8));
53 assertArrayEquals(new byte[] { 102, 0, 0, 0, 8 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("102.0.0.0/8")));
55 bytes = new byte[] { (byte) 255, (byte) 255, 0, 0 };
56 assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
58 assertArrayEquals(new byte[] { (byte) 255, (byte) 255, 0, 0, 16 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("255.255.0.0/16")));
62 public void testBytesForPrefix4Begin() {
63 byte[] bytes = new byte[] { 123, 122, 4, 5 };
64 assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8));
65 assertArrayEquals(new byte[] { 8, 102 }, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("102.0.0.0/8")));
67 bytes = new byte[] { (byte) 255, (byte) 255, 0, 0 };
68 assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
70 assertArrayEquals(new byte[] { 16, (byte) 255, (byte) 255 }, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("255.255.0.0/16")));
74 public void testAddress4ForBytes() {
75 final byte[] bytes = new byte[] { (byte) 123, (byte) 122, (byte) 4, (byte) 5 };
76 assertEquals(new Ipv4Address("123.122.4.5"), Ipv4Util.addressForBytes(bytes));
78 Ipv4Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
80 } catch (final IllegalArgumentException e) {
81 assertEquals("Failed to construct IPv4 address", e.getMessage());
86 public void testAddress6ForBytes() {
87 final byte[] bytes = new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89 assertEquals(new Ipv6Address("2001::1"), Ipv6Util.addressForBytes(bytes));
91 Ipv6Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
93 } catch (final IllegalArgumentException e) {
94 assertEquals("Failed to construct IPv6 address", e.getMessage());
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 testBytesForAddress() {
107 assertArrayEquals(new byte[] { 12, 58, (byte) 201, 99 }, Ipv4Util.bytesForAddress(new Ipv4Address("12.58.201.99")));
108 assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
109 0x01 }, Ipv6Util.bytesForAddress(new Ipv6Address("2001::1")));
113 public void testPrefix6ForBytes() {
114 final byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
115 assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
116 assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
117 0x00, 0x00, 0x40 }, Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/64")));
121 public void testBytesForPrefix6Begin() {
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[] { 0x40, 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 }, Ipv6Util.bytesForPrefixBegin(new Ipv6Prefix("2001:db8:1:2::/64")));
128 public void testPrefixLength() {
129 assertEquals(8, Ipv4Util.getPrefixLengthBytes("2001:db8:1:2::/64"));
130 assertEquals(3, Ipv4Util.getPrefixLengthBytes("172.168.3.0/22"));
134 public void testPrefixList4ForBytes() {
135 final byte[] bytes = new byte[] { 22, (byte) 172, (byte) 168, 3, 8, 12, 32, (byte) 192, (byte) 168, 35, 100 };
136 final List<Ipv4Prefix> prefs = Ipv4Util.prefixListForBytes(bytes);
138 Lists.newArrayList(new Ipv4Prefix("172.168.3.0/22"), new Ipv4Prefix("12.0.0.0/8"), new Ipv4Prefix("192.168.35.100/32")),
143 public void testPrefixList6ForBytes() {
144 final byte[] bytes = new byte[] { 0x40, 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x40, 0x20, 0x01, 0x0d, (byte) 0xb8,
145 0x00, 0x01, 0x00, 0x01, };
146 final List<Ipv6Prefix> prefs = Ipv6Util.prefixListForBytes(bytes);
147 assertEquals(prefs, Lists.newArrayList(new Ipv6Prefix("2001:db8:1:2::/64"), new Ipv6Prefix("2001:db8:1:1::/64")));
150 @Test(expected=UnsupportedOperationException.class)
151 public void testIpv4UtilPrivateConstructor() throws Throwable {
152 final Constructor<Ipv4Util> c = Ipv4Util.class.getDeclaredConstructor();
153 c.setAccessible(true);
156 } catch (InvocationTargetException e) {
161 @Test(expected=UnsupportedOperationException.class)
162 public void testIpv6UtilPrivateConstructor() throws Throwable {
163 final Constructor<Ipv6Util> c = Ipv6Util.class.getDeclaredConstructor();
164 c.setAccessible(true);
167 } catch (InvocationTargetException e) {