Moved Ipv4Util and Ipv6Util from concepts to util
[bgpcep.git] / util / src / test / java / org / opendaylight / protocol / util / IPAddressesAndPrefixesTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.util;
9
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;
14
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;
24
25 public class IPAddressesAndPrefixesTest {
26
27     @Test
28     public void test3() {
29         assertEquals("123.123.123.123", new Ipv4Address("123.123.123.123").getValue());
30         assertEquals("2001::1", new Ipv6Address("2001::1").getValue());
31     }
32
33     @Test
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());
37     }
38
39     @Test
40     public void test5() {
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());
43     }
44
45     @Test
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")));
50
51         bytes = new byte[] { (byte) 255, (byte) 255, 0, 0 };
52         assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
53
54         assertArrayEquals(new byte[] { (byte) 255, (byte) 255, 0, 0, 16 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("255.255.0.0/16")));
55     }
56
57     @Test
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));
61         try {
62             Ipv4Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
63             fail();
64         } catch (final IllegalArgumentException e) {
65             assertEquals("Failed to construct IPv4 address", e.getMessage());
66         }
67     }
68
69     @Test
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,
72             0x01 };
73         assertEquals(new Ipv6Address("2001::1"), Ipv6Util.addressForBytes(bytes));
74         try {
75             Ipv6Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
76             fail();
77         } catch (final IllegalArgumentException e) {
78             assertEquals("Failed to construct IPv6 address", e.getMessage());
79         }
80     }
81
82     @Test
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);
86         assertEquals(
87                 Lists.newArrayList(new Ipv4Prefix("172.168.3.0/22"), new Ipv4Prefix("12.0.0.0/8"), new Ipv4Prefix("192.168.35.100/32")),
88                 prefs);
89     }
90
91     @Test
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")));
96     }
97
98     @Test
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")));
104     }
105
106     @Test
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")));
112     }
113
114     @Test
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"))));
118     }
119 }