BUG-49 : fixed tests for bgp-parser.
[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.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13
14 import java.net.UnknownHostException;
15 import java.util.List;
16
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;
23
24 import com.google.common.collect.Lists;
25
26 public class IPAddressesAndPrefixesTest {
27
28         @Test
29         public void test3() {
30                 assertTrue("123.123.123.123".equals(new Ipv4Address("123.123.123.123").getValue()));
31                 assertTrue("2001::1".equals(new Ipv6Address("2001::1").getValue()));
32         }
33
34         @Test
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);
38         }
39
40         @Test
41         public void test5() {
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()));
44         }
45
46         @Test
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));
50         }
51
52         @Test
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));
56                 try {
57                         Ipv4Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
58                         fail();
59                 } catch (IllegalArgumentException e) {
60                         assertEquals("addr is of illegal length", e.getMessage());
61                 }
62         }
63
64         @Test
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);
68                 assertEquals(
69                                 Lists.newArrayList(new Ipv4Prefix("172.168.3.0/22"), new Ipv4Prefix("12.0.0.0/8"), new Ipv4Prefix("192.168.35.100/32")),
70                                 prefs);
71         }
72
73         @Test
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));
77         }
78
79         @Test
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"));
85
86         }
87 }