blob: 1243c614ed0f2134718503bdcc4e3a93a626246b [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import static org.junit.Assert.assertArrayEquals;
4import static org.junit.Assert.assertEquals;
Andreas Wundsame04c86f2013-11-05 17:13:18 -08005import static org.junit.Assert.assertThat;
Yotam Harcholf3f11152013-09-05 16:47:16 -07006import static org.junit.Assert.fail;
7
8import java.net.Inet6Address;
9import java.net.InetAddress;
10import java.net.UnknownHostException;
11
Andreas Wundsame04c86f2013-11-05 17:13:18 -080012import org.hamcrest.CoreMatchers;
Yotam Harcholf3f11152013-09-05 16:47:16 -070013import org.jboss.netty.buffer.ChannelBuffers;
14import org.junit.Test;
15import org.projectfloodlight.openflow.exceptions.OFParseError;
16
Andreas Wundsame04c86f2013-11-05 17:13:18 -080017import com.google.common.io.BaseEncoding;
18
Yotam Harchola289d552013-09-16 10:10:40 -070019public class IPv6AddressTest {
Yotam Harcholf3f11152013-09-05 16:47:16 -070020
21 String[] testStrings = {
22 "::",
23 "::1",
24 "ffe0::",
25 "1:2:3:4:5:6:7:8"
26 };
27
Yotam Harcholf3f11152013-09-05 16:47:16 -070028
Andreas Wundsame04c86f2013-11-05 17:13:18 -080029 private final BaseEncoding hex = BaseEncoding.base16().omitPadding().lowerCase();
Yotam Harcholf3f11152013-09-05 16:47:16 -070030
Andreas Wundsame04c86f2013-11-05 17:13:18 -080031 private class WithMaskTaskCase {
32 final String input;
33 boolean hasMask;
Gregor Maier7f987e62013-12-10 19:34:18 -080034 int expectedMaskLength = 128;
Andreas Wundsame04c86f2013-11-05 17:13:18 -080035 byte[] expectedMask = hex.decode("ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff".replaceAll(" ", ""));
36
37 public WithMaskTaskCase(String input) {
38 super();
39 this.input = input;
40 }
41
Andreas Wundsame04c86f2013-11-05 17:13:18 -080042 public WithMaskTaskCase maskHex(String string) {
43 string = string.replaceAll(" ", "");
44 this.hasMask = true;
45 expectedMask = hex.decode(string);
46 return this;
47 }
48
Gregor Maier7f987e62013-12-10 19:34:18 -080049 public WithMaskTaskCase expectedMaskLength(int expectedLength) {
50 this.expectedMaskLength = expectedLength;
51 return this;
52 }
53
Andreas Wundsame04c86f2013-11-05 17:13:18 -080054 }
55
56 WithMaskTaskCase[] withMasks = new WithMaskTaskCase[] {
57 new WithMaskTaskCase("1::1/80")
Gregor Maier7f987e62013-12-10 19:34:18 -080058 .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00")
59 .expectedMaskLength(80),
Andreas Wundsame04c86f2013-11-05 17:13:18 -080060
61 new WithMaskTaskCase("ffff:ffee:1::/ff00:ff00:ff00:ff00::")
Gregor Maier7f987e62013-12-10 19:34:18 -080062 .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00")
63 .expectedMaskLength(-1),
Andreas Wundsame04c86f2013-11-05 17:13:18 -080064 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
65 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
66 new WithMaskTaskCase("1:2:3:4:5:6:7:8/128"),
67 new WithMaskTaskCase("::/0")
68 .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")
Gregor Maier7f987e62013-12-10 19:34:18 -080069 .expectedMaskLength(0),
Yotam Harcholf3f11152013-09-05 16:47:16 -070070 };
71
72 @Test
73 public void testMasked() throws UnknownHostException {
Andreas Wundsame04c86f2013-11-05 17:13:18 -080074 for(WithMaskTaskCase w: withMasks) {
75 IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input);
76 if (!w.hasMask) {
Yotam Harchola289d552013-09-16 10:10:40 -070077 IPv6Address ip = value.getValue();
Andreas Wundsame04c86f2013-11-05 17:13:18 -080078 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
Yotam Harcholf3f11152013-09-05 16:47:16 -070079
80 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
Andreas Wundsame04c86f2013-11-05 17:13:18 -080081 assertEquals(w.input.split("/")[0], ip.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070082 }
Gregor Maier7f987e62013-12-10 19:34:18 -080083 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
84
85 assertEquals("Input " + w.input, w.expectedMaskLength, value.getMask().asCidrMaskLength());
86
87 byte[] address = inetAddress.getAddress();
88 assertEquals(address.length, value.getValue().getBytes().length);
89
90 for (int j = 0; j < address.length; j++) {
91 address[j] &= w.expectedMask[j];
92 }
93
94 assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address));
95 assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask));
96 }
97 for (int i = 0; i <= 128; i++) {
98 String ipString = String.format("8001:2::1/%d", i);
99 IPv6AddressWithMask value = IPv6AddressWithMask.of(ipString);
100 assertEquals("Input " + ipString, i, value.getMask().asCidrMaskLength());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700101 }
102 }
103
104
105 @Test
106 public void testOfString() throws UnknownHostException {
107 for(int i=0; i < testStrings.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700108 IPv6Address ip = IPv6Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700109 InetAddress inetAddress = InetAddress.getByName(testStrings[i]);
110
111 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
112 assertEquals(testStrings[i], ip.toString());
113 }
114 }
115
116 @Test
117 public void testOfByteArray() throws UnknownHostException {
118 for(int i=0; i < testStrings.length; i++ ) {
119 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700120 IPv6Address ip = IPv6Address.of(bytes);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700121 assertEquals(testStrings[i], ip.toString());
122 assertArrayEquals(bytes, ip.getBytes());
123 }
124 }
125
126 @Test
127 public void testReadFrom() throws OFParseError, UnknownHostException {
128 for(int i=0; i < testStrings.length; i++ ) {
129 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700130 IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700131 assertEquals(testStrings[i], ip.toString());
132 assertArrayEquals(bytes, ip.getBytes());
133 }
134 }
135
136 String[] invalidIPs = {
137 "",
138 ":",
139 "1:2:3:4:5:6:7:8:9",
140 "1:2:3:4:5:6:7:8:",
141 "1:2:3:4:5:6:7:8g",
142 "1:2:3:",
143 "12345::",
144 "1::3::8",
145 "::3::"
146 };
147
148 @Test
149 public void testInvalidIPs() throws OFParseError {
150 for(String invalid : invalidIPs) {
151 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700152 IPv6Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700153 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
154 } catch(IllegalArgumentException e) {
155 // ok
156 }
157 }
158 }
159
160 @Test
161 public void testZeroCompression() throws OFParseError {
Yotam Harchola289d552013-09-16 10:10:40 -0700162 assertEquals("::", IPv6Address.of("::").toString(true, false));
163 assertEquals("0:0:0:0:0:0:0:0", IPv6Address.of("::").toString(false, false));
164 assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6Address.of("::").toString(false, true));
165 assertEquals("1::4:5:6:0:8", IPv6Address.of("1:0:0:4:5:6:0:8").toString(true, false));
166 assertEquals("1:0:0:4::8", IPv6Address.of("1:0:0:4:0:0:0:8").toString(true, false));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700167 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800168
169 @Test
170 public void testSuperclass() throws Exception {
171 for(String ipString: testStrings) {
172 IPAddress<?> superIp = IPAddress.of(ipString);
173 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
174 assertEquals(IPv6Address.of(ipString), superIp);
175 }
176
177 for(WithMaskTaskCase w: withMasks) {
178 String ipMaskedString = w.input;
179 IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
180 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
181 assertEquals(IPv6AddressWithMask.of(ipMaskedString), superIp);
182 }
183 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700184}