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.concepts;
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;
17 import java.net.UnknownHostException;
18 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
27 public class IPAddressesAndPrefixesTest {
31 assertEquals("123.123.123.123", new Ipv4Address("123.123.123.123").getValue());
32 assertEquals("2001::1", new Ipv6Address("2001::1").getValue());
36 public void test4() throws UnknownHostException {
37 assertNotNull(new IpPrefix(new Ipv4Prefix("123.123.123.123")).getIpv4Prefix());
38 assertNotNull(new IpPrefix(new Ipv6Prefix("2001::1")).getIpv6Prefix());
43 assertEquals("123.123.123.123/24", new Ipv4Prefix("123.123.123.123/24").getValue());
44 assertEquals("2001::1/120", new Ipv6Prefix("2001::1/120").getValue());
48 public void testPrefix4ForBytes() {
49 byte[] bytes = new byte[] { 123, 122, 4, 5 };
50 assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8));
51 assertArrayEquals(new byte[] { 102, 0, 0, 0, 8 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("102.0.0.0/8")));
53 bytes = new byte[] { (byte) 255, (byte) 255, 0, 0 };
54 assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
56 assertArrayEquals(new byte[] { (byte) 255, (byte) 255, 0, 0, 16 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("255.255.0.0/16")));
60 public void testAddress4ForBytes() {
61 final byte[] bytes = new byte[] { (byte) 123, (byte) 122, (byte) 4, (byte) 5 };
62 assertEquals(new Ipv4Address("123.122.4.5"), Ipv4Util.addressForBytes(bytes));
64 Ipv4Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
66 } catch (final IllegalArgumentException e) {
67 assertEquals("Failed to construct IPv4 address", e.getMessage());
72 public void testAddress6ForBytes() {
73 final byte[] bytes = new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 assertEquals(new Ipv6Address("2001::1"), Ipv6Util.addressForBytes(bytes));
77 Ipv6Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
79 } catch (final IllegalArgumentException e) {
80 assertEquals("Failed to construct IPv6 address", e.getMessage());
85 public void testPrefixList4ForBytes() {
86 final byte[] bytes = new byte[] { 22, (byte) 172, (byte) 168, 3, 8, 12, 32, (byte) 192, (byte) 168, 35, 100 };
87 final List<Ipv4Prefix> prefs = Ipv4Util.prefixListForBytes(bytes);
89 Lists.newArrayList(new Ipv4Prefix("172.168.3.0/22"), new Ipv4Prefix("12.0.0.0/8"), new Ipv4Prefix("192.168.35.100/32")),
94 public void testBytesForAddress() {
95 assertArrayEquals(new byte[] { 12, 58, (byte) 201, 99 }, Ipv4Util.bytesForAddress(new Ipv4Address("12.58.201.99")));
96 assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97 0x01 }, Ipv6Util.bytesForAddress(new Ipv6Address("2001::1")));
101 public void testPrefix6ForBytes() {
102 final byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
103 assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
104 assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x40 }, Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/64")));
109 public void testPrefixList6ForBytes() {
110 final byte[] bytes = new byte[] { 0x40, 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x40, 0x20, 0x01, 0x0d, (byte) 0xb8,
111 0x00, 0x01, 0x00, 0x01, };
112 final List<Ipv6Prefix> prefs = Ipv6Util.prefixListForBytes(bytes);
113 assertEquals(prefs, Lists.newArrayList(new Ipv6Prefix("2001:db8:1:2::/64"), new Ipv6Prefix("2001:db8:1:1::/64")));
117 public void testPrefixLength() {
118 assertEquals(22, Ipv4Util.getPrefixLength(new IpPrefix(new Ipv4Prefix("172.168.3.0/22"))));
119 assertEquals(64, Ipv4Util.getPrefixLength(new IpPrefix(new Ipv6Prefix("2001:db8:1:2::/64"))));