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.assertTrue;
13 import static org.junit.Assert.fail;
15 import java.net.UnknownHostException;
16 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 import com.google.common.collect.Lists;
27 public class IPAddressesAndPrefixesTest {
31 assertTrue("123.123.123.123".equals(new Ipv4Address("123.123.123.123").getValue()));
32 assertTrue("2001::1".equals(new Ipv6Address("2001::1").getValue()));
36 public void test4() throws UnknownHostException {
37 assertTrue(new IpPrefix(new Ipv4Prefix("123.123.123.123")).getIpv4Prefix() != null);
38 assertTrue(new IpPrefix(new Ipv6Prefix("2001::1")).getIpv6Prefix() != null);
43 assertTrue("123.123.123.123/24".equals(new Ipv4Prefix("123.123.123.123/24").getValue()));
44 assertTrue("2001::1/120".equals(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("addr is of illegal length", e.getMessage());
72 public void testPrefixList4ForBytes() {
73 final byte[] bytes = new byte[] { 22, (byte) 172, (byte) 168, 3, 8, 12, 32, (byte) 192, (byte) 168, 35, 100 };
74 final List<Ipv4Prefix> prefs = Ipv4Util.prefixListForBytes(bytes);
76 Lists.newArrayList(new Ipv4Prefix("172.168.3.0/22"), new Ipv4Prefix("12.0.0.0/8"), new Ipv4Prefix("192.168.35.100/32")),
81 public void testPrefix6ForBytes() {
82 final byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
83 assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
87 public void testPrefixList6ForBytes() {
88 final List<Ipv6Prefix> prefs = Lists.newArrayList();
89 prefs.add(new Ipv6Prefix("2001:db8:1:2::/64"));
90 prefs.add(new Ipv6Prefix("2001:db8:1:1::/64"));
91 prefs.add(new Ipv6Prefix("2001:db8:1::/64"));
96 public void testPrefixLength() {
97 assertEquals(22, Ipv4Util.getPrefixLength(new IpPrefix(new Ipv4Prefix("172.168.3.0/22"))));
98 assertEquals(64, Ipv4Util.getPrefixLength(new IpPrefix(new Ipv6Prefix("2001:db8:1:2::/64"))));