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.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
14 import java.net.UnknownHostException;
15 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
24 import com.google.common.collect.Lists;
26 public class IPAddressesAndPrefixesTest {
30 assertTrue("123.123.123.123".equals(new Ipv4Address("123.123.123.123").getValue()));
31 assertTrue("2001::1".equals(new Ipv6Address("2001::1").getValue()));
35 public void test4() throws UnknownHostException {
36 assertTrue(new IpPrefix(new Ipv4Prefix("123.123.123.123")).getIpv4Prefix() != null);
37 assertTrue(new IpPrefix(new Ipv6Prefix("2001::1")).getIpv6Prefix() != null);
42 assertTrue("123.123.123.123/24".equals(new Ipv4Prefix("123.123.123.123/24").getValue()));
43 assertTrue("2001::1/120".equals(new Ipv6Prefix("2001::1/120").getValue()));
47 public void testPrefix4ForBytes() {
48 byte[] bytes = new byte[] { 123, 122, 4, 5 };
49 assertEquals(new Ipv4Prefix("123.122.4.5/32"), Ipv4Util.prefixForBytes(bytes, 32));
53 public void testAddress4ForBytes() {
54 byte[] bytes = new byte[] { (byte) 123, (byte) 122, (byte) 4, (byte) 5 };
55 assertEquals(new Ipv4Address("123.122.4.5"), Ipv4Util.addressForBytes(bytes));
57 Ipv4Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
59 } catch (IllegalArgumentException e) {
60 assertEquals("addr is of illegal length", e.getMessage());
65 public void testPrefixList4ForBytes() {
66 byte[] bytes = new byte[] { 22, (byte) 172, (byte) 168, 3, 8, 12, 32, (byte) 192, (byte) 168, 35, 100 };
67 List<Ipv4Prefix> prefs = Ipv4Util.prefixListForBytes(bytes);
69 Lists.newArrayList(new Ipv4Prefix("172.168.3.0/22"), new Ipv4Prefix("12.0.0.0/8"), new Ipv4Prefix("192.168.35.100/32")),
74 public void testPrefix6ForBytes() {
75 byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
76 assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
80 public void testPrefixList6ForBytes() {
81 List<Ipv6Prefix> prefs = Lists.newArrayList();
82 prefs.add(new Ipv6Prefix("2001:db8:1:2::/64"));
83 prefs.add(new Ipv6Prefix("2001:db8:1:1::/64"));
84 prefs.add(new Ipv6Prefix("2001:db8:1::/64"));