95cca2e26d937fc1abc6f4ae544604e211dba7cf
[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 io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.lang.reflect.Constructor;
19 import java.lang.reflect.InvocationTargetException;
20 import java.net.UnknownHostException;
21 import java.util.List;
22 import org.junit.Test;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
28
29 public class IPAddressesAndPrefixesTest {
30
31     @Test
32     public void test3() {
33         assertEquals("123.123.123.123", new Ipv4Address("123.123.123.123").getValue());
34         assertEquals("2001::1", new Ipv6Address("2001::1").getValue());
35     }
36
37     @Test
38     public void test4() throws UnknownHostException {
39         assertNotNull(new IpPrefix(new Ipv4Prefix("123.123.123.123")).getIpv4Prefix());
40         assertNotNull(new IpPrefix(new Ipv6Prefix("2001::1")).getIpv6Prefix());
41     }
42
43     @Test
44     public void test5() {
45         assertEquals("123.123.123.123/24", new Ipv4Prefix("123.123.123.123/24").getValue());
46         assertEquals("2001::1/120", new Ipv6Prefix("2001::1/120").getValue());
47     }
48
49     @Test
50     public void testPrefix4ForBytes() {
51         byte[] bytes = new byte[] { 123, 122, 4, 5 };
52         assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8));
53         assertArrayEquals(new byte[] { 102, 0, 0, 0, 8 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("102.0.0.0/8")));
54
55         bytes = new byte[] { (byte) 255, (byte) 255, 0, 0 };
56         assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
57
58         assertArrayEquals(new byte[] { (byte) 255, (byte) 255, 0, 0, 16 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("255.255.0.0/16")));
59     }
60
61     @Test
62     public void testBytesForPrefix4Begin() {
63         byte[] bytes = new byte[] { 123, 122, 4, 5 };
64         assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8));
65         assertArrayEquals(new byte[] { 8, 102 }, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("102.0.0.0/8")));
66
67         bytes = new byte[] { (byte) 255, (byte) 255, 0, 0 };
68         assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
69
70         assertArrayEquals(new byte[] { 16, (byte) 255, (byte) 255 }, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("255.255.0.0/16")));
71     }
72
73     @Test
74     public void testAddress4ForBytes() {
75         final byte[] bytes = new byte[] { (byte) 123, (byte) 122, (byte) 4, (byte) 5 };
76         assertEquals(new Ipv4Address("123.122.4.5"), Ipv4Util.addressForBytes(bytes));
77         try {
78             Ipv4Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
79             fail();
80         } catch (final IllegalArgumentException e) {
81             assertEquals("Failed to construct IPv4 address", e.getMessage());
82         }
83     }
84
85     @Test
86     public void testAddress6ForBytes() {
87         final byte[] bytes = new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88             0x01 };
89         assertEquals(new Ipv6Address("2001::1"), Ipv6Util.addressForBytes(bytes));
90         try {
91             Ipv6Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
92             fail();
93         } catch (final IllegalArgumentException e) {
94             assertEquals("Failed to construct IPv6 address", e.getMessage());
95         }
96     }
97
98     @Test
99     public void testAddressForByteBuf() {
100         final ByteBuf bb = Unpooled.wrappedBuffer(new byte[] { 123, 122, 4, 5, 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } );
101         assertEquals(new Ipv4Address("123.122.4.5"), Ipv4Util.addressForByteBuf(bb));
102         assertEquals(new Ipv6Address("2001::1"), Ipv6Util.addressForByteBuf(bb));
103     }
104
105     @Test
106     public void testBytesForAddress() {
107         assertArrayEquals(new byte[] { 12, 58, (byte) 201, 99 }, Ipv4Util.bytesForAddress(new Ipv4Address("12.58.201.99")));
108         assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
109             0x01 }, Ipv6Util.bytesForAddress(new Ipv6Address("2001::1")));
110     }
111
112     @Test
113     public void testPrefix6ForBytes() {
114         final byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
115         assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
116         assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
117             0x00, 0x00, 0x40 }, Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/64")));
118     }
119
120     @Test
121     public void testBytesForPrefix6Begin() {
122         final byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
123         assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
124         assertArrayEquals(new byte[] { 0x40, 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 }, Ipv6Util.bytesForPrefixBegin(new Ipv6Prefix("2001:db8:1:2::/64")));
125     }
126
127     @Test
128     public void testPrefixLength() {
129         assertEquals(8, Ipv4Util.getPrefixLengthBytes("2001:db8:1:2::/64"));
130         assertEquals(3, Ipv4Util.getPrefixLengthBytes("172.168.3.0/22"));
131     }
132
133     @Test
134     public void testPrefixList4ForBytes() {
135         final byte[] bytes = new byte[] { 22, (byte) 172, (byte) 168, 3, 8, 12, 32, (byte) 192, (byte) 168, 35, 100 };
136         final List<Ipv4Prefix> prefs = Ipv4Util.prefixListForBytes(bytes);
137         assertEquals(
138             Lists.newArrayList(new Ipv4Prefix("172.168.3.0/22"), new Ipv4Prefix("12.0.0.0/8"), new Ipv4Prefix("192.168.35.100/32")),
139             prefs);
140     }
141
142     @Test
143     public void testPrefixList6ForBytes() {
144         final byte[] bytes = new byte[] { 0x40, 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x40, 0x20, 0x01, 0x0d, (byte) 0xb8,
145             0x00, 0x01, 0x00, 0x01, };
146         final List<Ipv6Prefix> prefs = Ipv6Util.prefixListForBytes(bytes);
147         assertEquals(prefs, Lists.newArrayList(new Ipv6Prefix("2001:db8:1:2::/64"), new Ipv6Prefix("2001:db8:1:1::/64")));
148     }
149
150     @Test(expected=UnsupportedOperationException.class)
151     public void testIpv4UtilPrivateConstructor() throws Throwable {
152         final Constructor<Ipv4Util> c = Ipv4Util.class.getDeclaredConstructor();
153         c.setAccessible(true);
154         try {
155             c.newInstance();
156         } catch (InvocationTargetException e) {
157             throw e.getCause();
158         }
159     }
160
161     @Test(expected=UnsupportedOperationException.class)
162     public void testIpv6UtilPrivateConstructor() throws Throwable {
163         final Constructor<Ipv6Util> c = Ipv6Util.class.getDeclaredConstructor();
164         c.setAccessible(true);
165         try {
166             c.newInstance();
167         } catch (InvocationTargetException e) {
168             throw e.getCause();
169         }
170     }
171 }