2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.concepts;
10 import static org.hamcrest.core.IsNot.not;
11 import static org.junit.Assert.assertThat;
13 import java.net.InetAddress;
14 import java.util.HashSet;
16 import java.util.TreeSet;
18 import org.junit.Before;
19 import org.junit.Test;
20 import static org.junit.Assert.*;
22 public class IPv6AddressTest {
23 private IPv6Address a1, a2, a3, a4, a5;
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);
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,
43 new IPv6Address(fail_bytes);
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 };
51 final IPv6Address a = new IPv6Address(succ_bytes);
52 assertEquals(this.a1, a);
53 assertEquals(succ_bytes, a.getAddress());
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()));
64 public void testHashCode() {
65 final Set<IPv6Address> set = new HashSet<IPv6Address>();
68 assertEquals(1, set.size());
71 assertEquals(2, set.size());
74 assertEquals(2, set.size());
77 assertEquals(3, set.size());
81 public void testCompareTo() {
82 final Set<IPv6Address> set = new TreeSet<IPv6Address>();
85 assertEquals(1, set.size());
88 assertEquals(2, set.size());
91 assertEquals(2, set.size());
94 assertEquals(3, set.size());
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);
104 assertEquals(7, an1.compareTo(an2));
105 assertThat(an1, not(an2));
107 assertEquals(-7, an2.compareTo(an1));
108 assertThat(an2, not(an1));
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);
115 assertEquals(169, an1.compareTo(an2));
116 assertThat(an1, not(an2));
118 assertEquals(-169, an2.compareTo(an1));
119 assertThat(an2, not(an1));
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);
126 assertEquals(255, an1.compareTo(an2));
127 assertThat(an1, not(an2));
129 assertEquals(-255, an2.compareTo(an1));
130 assertThat(an2, not(an1));
133 @Test(expected = IllegalArgumentException.class)
134 public void testIllegalArgument() throws Exception {
135 new IPv6Address(InetAddress.getByName("120.20.20.20"));
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());