Initial code drop
[bgpcep.git] / concepts / src / test / java / org / opendaylight / protocol / concepts / IPv6AddressTest.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.hamcrest.core.IsNot.not;
11 import static org.junit.Assert.assertThat;
12
13 import java.net.InetAddress;
14 import java.util.HashSet;
15 import java.util.Set;
16 import java.util.TreeSet;
17
18 import org.junit.Before;
19 import org.junit.Test;
20 import static org.junit.Assert.*;
21
22 public class IPv6AddressTest {
23         private IPv6Address a1, a2, a3, a4, a5;
24
25         @Before
26         public void setUp() throws Exception {
27                 this.a1 = new IPv6Address(
28                                 InetAddress.getByName("2001:db8:85a3:0:0:8a2e:370:7331"));
29                 this.a2 = new IPv6Address(
30                                 InetAddress.getByName("2001:db8:85a3:0:0:8a2e:370:7332"));
31                 this.a3 = new IPv6Address(
32                                 InetAddress.getByName("2001:db8:85a3:0:0:8a2e:370:7332"));
33                 this.a4 = new IPv6Address(
34                                 InetAddress.getByName("2001:db8:85a3:0:0:8a2e:370:0000"));
35                 this.a5 = this.a4.applyMask(112);
36         }
37
38         @Test(expected = IllegalArgumentException.class)
39         public void testFailingFactory() {
40                 final byte[] fail_bytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
41                                 14, 15, 16, 17 };
42
43                 new IPv6Address(fail_bytes);
44         }
45
46         @Test
47         public void testFactory() {
48                 final byte[] succ_bytes = { 32, 1, 13, -72, -123, -93, 0, 0, 0, 0,
49                                 -118, 46, 3, 112, 115, 49 };
50
51                 final IPv6Address a = new IPv6Address(succ_bytes);
52                 assertEquals(this.a1, a);
53                 assertEquals(succ_bytes, a.getAddress());
54         }
55
56         @Test
57         public void testEquals() {
58                 assertTrue(this.a2.equals(this.a3));
59                 assertFalse(this.a1.equals(this.a2));
60                 assertFalse(this.a1.equals(new Object()));
61         }
62
63         @Test
64         public void testHashCode() {
65                 final Set<IPv6Address> set = new HashSet<IPv6Address>();
66
67                 set.add(this.a1);
68                 assertEquals(1, set.size());
69
70                 set.add(this.a2);
71                 assertEquals(2, set.size());
72
73                 set.add(this.a3);
74                 assertEquals(2, set.size());
75
76                 set.add(this.a4);
77                 assertEquals(3, set.size());
78         }
79
80         @Test
81         public void testCompareTo() {
82                 final Set<IPv6Address> set = new TreeSet<IPv6Address>();
83
84                 set.add(this.a1);
85                 assertEquals(1, set.size());
86
87                 set.add(this.a2);
88                 assertEquals(2, set.size());
89
90                 set.add(this.a3);
91                 assertEquals(2, set.size());
92
93                 set.add(this.a4);
94                 assertEquals(3, set.size());
95         }
96
97         @Test
98         public void testCompareToExtended() throws Exception {
99                 IPv6Prefix an1 = new IPv6Prefix(new IPv6Address(
100                                 InetAddress.getByName("8:0:0:0:0:0:0:0")), 128);
101                 IPv6Prefix an2 = new IPv6Prefix(new IPv6Address(
102                                 InetAddress.getByName("1:0:0:0:0:0:0:0")), 128);
103
104                 assertEquals(7, an1.compareTo(an2));
105                 assertThat(an1, not(an2));
106
107                 assertEquals(-7, an2.compareTo(an1));
108                 assertThat(an2, not(an1));
109
110                 an1 = new IPv6Prefix(new IPv6Address(
111                                 InetAddress.getByName("aa:0:0:0:0:0:0:0")), 128);
112                 an2 = new IPv6Prefix(new IPv6Address(
113                                 InetAddress.getByName("1:0:0:0:0:0:0:0")), 128);
114
115                 assertEquals(169, an1.compareTo(an2));
116                 assertThat(an1, not(an2));
117
118                 assertEquals(-169, an2.compareTo(an1));
119                 assertThat(an2, not(an1));
120
121                 an1 = new IPv6Prefix(new IPv6Address(
122                                 InetAddress.getByName("ff:0:0:0:0:0:0:0")), 128);
123                 an2 = new IPv6Prefix(new IPv6Address(
124                                 InetAddress.getByName("0:0:0:0:0:0:0:0")), 128);
125
126                 assertEquals(255, an1.compareTo(an2));
127                 assertThat(an1, not(an2));
128
129                 assertEquals(-255, an2.compareTo(an1));
130                 assertThat(an2, not(an1));
131         }
132
133         @Test(expected = IllegalArgumentException.class)
134         public void testIllegalArgument() throws Exception {
135                 new IPv6Address(InetAddress.getByName("120.20.20.20"));
136         }
137
138         @Test
139         public void testToString() {
140                 assertEquals("2001:db8:85a3:0:0:8a2e:370:7331", this.a1.toString());
141                 assertEquals("2001:db8:85a3:0:0:8a2e:370:7332", this.a2.toString());
142                 assertEquals("2001:db8:85a3:0:0:8a2e:370:7332", this.a3.toString());
143                 assertEquals("2001:db8:85a3:0:0:8a2e:370:0", this.a4.toString());
144                 assertEquals("2001:db8:85a3:0:0:8a2e:370:0", this.a5.toString());
145         }
146 }