Merge "Bug 611 - NlriReg supports serialization"
[bgpcep.git] / concepts / src / test / java / org / opendaylight / protocol / concepts / 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.concepts;
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
17 import java.net.UnknownHostException;
18 import java.util.List;
19
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;
26
27 public class IPAddressesAndPrefixesTest {
28
29     @Test
30     public void test3() {
31         assertEquals("123.123.123.123", new Ipv4Address("123.123.123.123").getValue());
32         assertEquals("2001::1", new Ipv6Address("2001::1").getValue());
33     }
34
35     @Test
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());
39     }
40
41     @Test
42     public void test5() {
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());
45     }
46
47     @Test
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")));
52
53         bytes = new byte[] { (byte) 255, (byte) 255, 0, 0 };
54         assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
55
56         assertArrayEquals(new byte[] { (byte) 255, (byte) 255, 0, 0, 16 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("255.255.0.0/16")));
57     }
58
59     @Test
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));
63         try {
64             Ipv4Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
65             fail();
66         } catch (final IllegalArgumentException e) {
67             assertEquals("Failed to construct IPv4 address", e.getMessage());
68         }
69     }
70
71     @Test
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,
74             0x01 };
75         assertEquals(new Ipv6Address("2001::1"), Ipv6Util.addressForBytes(bytes));
76         try {
77             Ipv6Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
78             fail();
79         } catch (final IllegalArgumentException e) {
80             assertEquals("Failed to construct IPv6 address", e.getMessage());
81         }
82     }
83
84     @Test
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);
88         assertEquals(
89                 Lists.newArrayList(new Ipv4Prefix("172.168.3.0/22"), new Ipv4Prefix("12.0.0.0/8"), new Ipv4Prefix("192.168.35.100/32")),
90                 prefs);
91     }
92
93     @Test
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")));
98     }
99
100     @Test
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")));
106     }
107
108     @Test
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")));
114     }
115
116     @Test
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"))));
120     }
121 }