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 java.net.UnknownHostException;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
25 public class IPAddressesAndPrefixesTest {
29 assertEquals("123.123.123.123", new Ipv4Address("123.123.123.123").getValue());
30 assertEquals("2001::1", new Ipv6Address("2001::1").getValue());
34 public void test4() throws UnknownHostException {
35 assertNotNull(new IpPrefix(new Ipv4Prefix("123.123.123.123")).getIpv4Prefix());
36 assertNotNull(new IpPrefix(new Ipv6Prefix("2001::1")).getIpv6Prefix());
41 assertEquals("123.123.123.123/24", new Ipv4Prefix("123.123.123.123/24").getValue());
42 assertEquals("2001::1/120", new Ipv6Prefix("2001::1/120").getValue());
46 public void testPrefix4ForBytes() {
47 byte[] bytes = new byte[] { 123, 122, 4, 5 };
48 assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8));
49 assertArrayEquals(new byte[] { 102, 0, 0, 0, 8 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("102.0.0.0/8")));
51 bytes = new byte[] { (byte) 255, (byte) 255, 0, 0 };
52 assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
54 assertArrayEquals(new byte[] { (byte) 255, (byte) 255, 0, 0, 16 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("255.255.0.0/16")));
58 public void testAddress4ForBytes() {
59 final byte[] bytes = new byte[] { (byte) 123, (byte) 122, (byte) 4, (byte) 5 };
60 assertEquals(new Ipv4Address("123.122.4.5"), Ipv4Util.addressForBytes(bytes));
62 Ipv4Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
64 } catch (final IllegalArgumentException e) {
65 assertEquals("Failed to construct IPv4 address", e.getMessage());
70 public void testAddress6ForBytes() {
71 final byte[] bytes = new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 assertEquals(new Ipv6Address("2001::1"), Ipv6Util.addressForBytes(bytes));
75 Ipv6Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
77 } catch (final IllegalArgumentException e) {
78 assertEquals("Failed to construct IPv6 address", e.getMessage());
83 public void testPrefixList4ForBytes() {
84 final byte[] bytes = new byte[] { 22, (byte) 172, (byte) 168, 3, 8, 12, 32, (byte) 192, (byte) 168, 35, 100 };
85 final List<Ipv4Prefix> prefs = Ipv4Util.prefixListForBytes(bytes);
87 Lists.newArrayList(new Ipv4Prefix("172.168.3.0/22"), new Ipv4Prefix("12.0.0.0/8"), new Ipv4Prefix("192.168.35.100/32")),
92 public void testBytesForAddress() {
93 assertArrayEquals(new byte[] { 12, 58, (byte) 201, 99 }, Ipv4Util.bytesForAddress(new Ipv4Address("12.58.201.99")));
94 assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95 0x01 }, Ipv6Util.bytesForAddress(new Ipv6Address("2001::1")));
99 public void testPrefix6ForBytes() {
100 final byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
101 assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
102 assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103 0x00, 0x00, 0x40 }, Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/64")));
107 public void testPrefixList6ForBytes() {
108 final byte[] bytes = new byte[] { 0x40, 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x40, 0x20, 0x01, 0x0d, (byte) 0xb8,
109 0x00, 0x01, 0x00, 0x01, };
110 final List<Ipv6Prefix> prefs = Ipv6Util.prefixListForBytes(bytes);
111 assertEquals(prefs, Lists.newArrayList(new Ipv6Prefix("2001:db8:1:2::/64"), new Ipv6Prefix("2001:db8:1:1::/64")));
115 public void testPrefixLength() {
116 assertEquals(22, Ipv4Util.getPrefixLength(new IpPrefix(new Ipv4Prefix("172.168.3.0/22"))));
117 assertEquals(64, Ipv4Util.getPrefixLength(new IpPrefix(new Ipv6Prefix("2001:db8:1:2::/64"))));