Use new Uint* constants
[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.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16
17 import com.google.common.collect.Lists;
18 import com.google.common.net.InetAddresses;
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.Unpooled;
21 import java.net.InetSocketAddress;
22 import java.net.UnknownHostException;
23 import java.util.List;
24 import org.junit.Assert;
25 import org.junit.Test;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
33 import org.opendaylight.yangtools.yang.common.Uint16;
34
35 public class IPAddressesAndPrefixesTest {
36
37     @Test
38     public void test3() {
39         assertEquals("123.123.123.123", new Ipv4Address("123.123.123.123").getValue());
40         assertEquals("2001::1", new Ipv6Address("2001::1").getValue());
41     }
42
43     @Test
44     public void test4() throws UnknownHostException {
45         assertNotNull(new IpPrefix(new Ipv4Prefix("123.123.123.123/32")).getIpv4Prefix());
46         assertNotNull(new IpPrefix(new Ipv6Prefix("2001::1/120")).getIpv6Prefix());
47     }
48
49     @Test
50     public void test5() {
51         assertEquals("123.123.123.123/24", new Ipv4Prefix("123.123.123.123/24").getValue());
52         assertEquals("2001::1/120", new Ipv6Prefix("2001::1/120").getValue());
53     }
54
55     @Test
56     public void testPrefix4ForBytes() {
57         byte[] bytes = new byte[]{123, 122, 4, 5};
58         assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8));
59         assertArrayEquals(new byte[]{102, 0, 0, 0, 8}, Ipv4Util.bytesForPrefix(new Ipv4Prefix("102.0.0.0/8")));
60
61         bytes = new byte[]{(byte) 255, (byte) 255, 0, 0};
62         assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
63
64         assertArrayEquals(new byte[]{(byte) 255, (byte) 255, 0, 0, 16},
65                 Ipv4Util.bytesForPrefix(new Ipv4Prefix("255.255.0.0/16")));
66
67         try {
68             Ipv4Util.prefixForBytes(bytes, 200);
69             fail();
70         } catch (final IllegalArgumentException exc) {
71             assertNull(exc.getMessage());
72         }
73     }
74
75     @Test
76     public void testBytesForPrefix4Begin() {
77         byte[] bytes = new byte[]{123, 122, 4, 5};
78         assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8));
79
80         bytes = new byte[]{(byte) 255, (byte) 255, 0, 0};
81         assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
82
83         bytes = new byte[]{(byte) 0, (byte) 0, (byte) 0, (byte) 0};
84         assertEquals(new Ipv4Prefix("0.0.0.0/0"), Ipv4Util.prefixForBytes(bytes, 0));
85     }
86
87     @Test
88     public void testPrefixForByteBuf() {
89         final ByteBuf bb = Unpooled.wrappedBuffer(new byte[]{
90             0x0e, 123, 122,
91             0x40, 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92             0x00,
93             0x00,
94             (byte) 0x80, 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
95             (byte) 0x88, (byte) 0x89
96         });
97         assertEquals(new Ipv4Prefix("123.122.0.0/14"), Ipv4Util.prefixForByteBuf(bb));
98         assertEquals(new Ipv6Prefix("2001::/64"), Ipv6Util.prefixForByteBuf(bb));
99         assertEquals(new Ipv4Prefix("0.0.0.0/0"), Ipv4Util.prefixForByteBuf(bb));
100         assertEquals(new Ipv6Prefix("::/0"), Ipv6Util.prefixForByteBuf(bb));
101         // test prefix length 128, as its signed byte value is -128
102         assertEquals(new Ipv6Prefix("2001:4860:4860:1:20::8889/128"), Ipv6Util.prefixForByteBuf(bb));
103     }
104
105     @Test
106     public void testAddressForByteBuf() {
107         final ByteBuf bb = Unpooled.wrappedBuffer(new byte[]{123, 122, 4, 5, 0x20, (byte) 0x01, 0x00, 0x00, 0x00,
108             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01});
109         assertEquals(new Ipv4Address("123.122.4.5"), Ipv4Util.addressForByteBuf(bb));
110         assertEquals(new Ipv6Address("2001::1"), Ipv6Util.addressForByteBuf(bb));
111     }
112
113     @Test
114     public void testByteBufForAddress() {
115         final ByteBuf bb4 = Unpooled.wrappedBuffer(new byte[]{123, 122, 4, 5});
116         final ByteBuf bb6 = Unpooled.wrappedBuffer(new byte[]{0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
117             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01});
118         assertEquals(bb4, Ipv4Util.byteBufForAddress(new Ipv4Address("123.122.4.5")));
119         assertEquals(bb6, Ipv6Util.byteBufForAddress(new Ipv6Address("2001::1")));
120     }
121
122     @Test
123     public void testBytesForAddress() {
124         assertArrayEquals(new byte[]{12, 58, (byte) 201, 99},
125                 Ipv4Util.bytesForAddress(new Ipv4Address("12.58.201.99")));
126         assertArrayEquals(new byte[]{0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127             0x00, 0x00, 0x00, 0x01}, Ipv6Util.bytesForAddress(new Ipv6Address("2001::1")));
128     }
129
130     @Test
131     public void testPrefix6ForBytes() {
132         final byte[] bytes = new byte[]{0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02};
133         assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
134         assertArrayEquals(new byte[]{0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00,
135             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40},
136                 Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/64")));
137         assertArrayEquals(new byte[]{0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00,
138             0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x80},
139                 Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/128")));
140
141         try {
142             Ipv6Util.prefixForBytes(bytes, 200);
143             fail();
144         } catch (final IllegalArgumentException exc) {
145             assertNull(exc.getMessage());
146         }
147     }
148
149     @Test
150     public void testBytesForPrefix6Begin() {
151         byte[] bytes = new byte[]{0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02};
152         assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
153
154         bytes = new byte[]{(byte) 0};
155         assertEquals(new Ipv6Prefix("::/0"), Ipv6Util.prefixForBytes(bytes, 0));
156     }
157
158     @Test
159     public void testPrefixLength() {
160         assertEquals(8, Ipv4Util.getPrefixLengthBytes("2001:db8:1:2::/64"));
161         assertEquals(3, Ipv4Util.getPrefixLengthBytes("172.168.3.0/22"));
162     }
163
164     @Test
165     public void testPrefixList4ForBytes() {
166         final byte[] bytes = new byte[]{22, (byte) 172, (byte) 168, 3, 8, 12, 32, (byte) 192, (byte) 168, 35, 100};
167         List<Ipv4Prefix> prefs = Ipv4Util.prefixListForBytes(bytes);
168         assertEquals(
169                 Lists.newArrayList(new Ipv4Prefix("172.168.3.0/22"), new Ipv4Prefix("12.0.0.0/8"),
170                         new Ipv4Prefix("192.168.35.100/32")), prefs);
171
172         prefs = Ipv4Util.prefixListForBytes(new byte[]{});
173         assertTrue(prefs.isEmpty());
174     }
175
176     @Test
177     public void testPrefixList6ForBytes() {
178         final byte[] bytes = new byte[]{0x40, 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x40, 0x20,
179             0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x01,};
180         List<Ipv6Prefix> prefs = Ipv6Util.prefixListForBytes(bytes);
181         assertEquals(prefs, Lists.newArrayList(new Ipv6Prefix("2001:db8:1:2::/64"),
182             new Ipv6Prefix("2001:db8:1:1::/64")));
183
184         prefs = Ipv6Util.prefixListForBytes(new byte[]{});
185         assertTrue(prefs.isEmpty());
186     }
187
188     @Test
189     public void testFullFormOfIpv6() {
190         assertEquals(new Ipv6Address("0:0:0:0:0:0:0:1"), Ipv6Util.getFullForm(new Ipv6Address("::1")));
191     }
192
193     @Test
194     public void testInetAddressToIpAddress() {
195         final IpAddress ipAddress = Ipv4Util.getIpAddress(InetAddresses.forString("123.42.13.8"));
196         Assert.assertNotNull(ipAddress.getIpv4Address());
197         Assert.assertEquals(new Ipv4Address("123.42.13.8"), ipAddress.getIpv4Address());
198
199         final IpAddress ipAddress2 = Ipv4Util.getIpAddress(InetAddresses.forString("2001:db8:1:2::"));
200         Assert.assertNotNull(ipAddress2.getIpv6Address());
201         Assert.assertEquals(new Ipv6Address("2001:db8:1:2::"), ipAddress2.getIpv6Address());
202     }
203
204     @Test
205     public void testToInetSocketAddress() {
206         final InetSocketAddress isa = Ipv4Util.toInetSocketAddress(new IpAddress(new Ipv4Address("123.42.13.8")),
207                 new PortNumber(Uint16.TEN));
208         Assert.assertEquals(10, isa.getPort());
209         Assert.assertEquals("123.42.13.8", InetAddresses.toAddrString(isa.getAddress()));
210
211         final InetSocketAddress isa2 = Ipv4Util.toInetSocketAddress(new IpAddress(new Ipv6Address("2001:db8:1:2::")),
212                 new PortNumber(Uint16.TEN));
213         Assert.assertEquals(10, isa2.getPort());
214         Assert.assertEquals("2001:db8:1:2::", InetAddresses.toAddrString(isa2.getAddress()));
215     }
216 }