blob: a00807096fbe0276cbe048d43e4a64260518a361 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
Gregor Maier5615b6c2013-12-11 22:29:07 -08003import static org.junit.Assert.*;
Yotam Harcholf3f11152013-09-05 16:47:16 -07004
5import java.net.Inet6Address;
6import java.net.InetAddress;
7import java.net.UnknownHostException;
8
Andreas Wundsame04c86f2013-11-05 17:13:18 -08009import org.hamcrest.CoreMatchers;
Yotam Harcholf3f11152013-09-05 16:47:16 -070010import org.jboss.netty.buffer.ChannelBuffers;
11import org.junit.Test;
12import org.projectfloodlight.openflow.exceptions.OFParseError;
13
Andreas Wundsame04c86f2013-11-05 17:13:18 -080014import com.google.common.io.BaseEncoding;
15
Yotam Harchola289d552013-09-16 10:10:40 -070016public class IPv6AddressTest {
Yotam Harcholf3f11152013-09-05 16:47:16 -070017
18 String[] testStrings = {
19 "::",
20 "::1",
21 "ffe0::",
22 "1:2:3:4:5:6:7:8"
23 };
24
Yotam Harcholf3f11152013-09-05 16:47:16 -070025
Andreas Wundsame04c86f2013-11-05 17:13:18 -080026 private final BaseEncoding hex = BaseEncoding.base16().omitPadding().lowerCase();
Yotam Harcholf3f11152013-09-05 16:47:16 -070027
Andreas Wundsame04c86f2013-11-05 17:13:18 -080028 private class WithMaskTaskCase {
29 final String input;
30 boolean hasMask;
Gregor Maier7f987e62013-12-10 19:34:18 -080031 int expectedMaskLength = 128;
Andreas Wundsame04c86f2013-11-05 17:13:18 -080032 byte[] expectedMask = hex.decode("ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff".replaceAll(" ", ""));
33
34 public WithMaskTaskCase(String input) {
35 super();
36 this.input = input;
37 }
38
Andreas Wundsame04c86f2013-11-05 17:13:18 -080039 public WithMaskTaskCase maskHex(String string) {
40 string = string.replaceAll(" ", "");
41 this.hasMask = true;
42 expectedMask = hex.decode(string);
43 return this;
44 }
45
Gregor Maier7f987e62013-12-10 19:34:18 -080046 public WithMaskTaskCase expectedMaskLength(int expectedLength) {
47 this.expectedMaskLength = expectedLength;
48 return this;
49 }
50
Andreas Wundsame04c86f2013-11-05 17:13:18 -080051 }
52
53 WithMaskTaskCase[] withMasks = new WithMaskTaskCase[] {
54 new WithMaskTaskCase("1::1/80")
Gregor Maier7f987e62013-12-10 19:34:18 -080055 .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00")
56 .expectedMaskLength(80),
Andreas Wundsame04c86f2013-11-05 17:13:18 -080057
58 new WithMaskTaskCase("ffff:ffee:1::/ff00:ff00:ff00:ff00::")
Gregor Maier7f987e62013-12-10 19:34:18 -080059 .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00")
60 .expectedMaskLength(-1),
Andreas Wundsame04c86f2013-11-05 17:13:18 -080061 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
62 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
63 new WithMaskTaskCase("1:2:3:4:5:6:7:8/128"),
64 new WithMaskTaskCase("::/0")
65 .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")
Gregor Maier7f987e62013-12-10 19:34:18 -080066 .expectedMaskLength(0),
Yotam Harcholf3f11152013-09-05 16:47:16 -070067 };
68
69 @Test
Gregor Maier5615b6c2013-12-11 22:29:07 -080070 public void testConstants() {
71 byte[] zeros = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
72 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
73 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
74 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
75 byte[] ones = { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
76 (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
77 (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
78 (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF };
79 // Make sure class initializtation and static assignment don't get
80 // messed up. Test everything twice for cached values
81 assertTrue(IPv6Address.NONE.isCidrMask());
82 assertEquals(0, IPv6Address.NONE.asCidrMaskLength());
83 assertArrayEquals(zeros, IPv6Address.NONE.getBytes());
84 assertTrue(IPv6Address.NONE.isCidrMask());
85 assertEquals(0, IPv6Address.NONE.asCidrMaskLength());
86 assertArrayEquals(zeros, IPv6Address.NONE.getBytes());
87
88 assertTrue(IPv6Address.NO_MASK.isCidrMask());
89 assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength());
90 assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes());
91 assertTrue(IPv6Address.NO_MASK.isCidrMask());
92 assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength());
93 assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes());
94
95 assertTrue(IPv6Address.FULL_MASK.isCidrMask());
96 assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength());
97 assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes());
98 assertTrue(IPv6Address.FULL_MASK.isCidrMask());
99 assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength());
100 assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes());
101 }
102
103 @Test
Yotam Harcholf3f11152013-09-05 16:47:16 -0700104 public void testMasked() throws UnknownHostException {
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800105 for(WithMaskTaskCase w: withMasks) {
106 IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input);
107 if (!w.hasMask) {
Yotam Harchola289d552013-09-16 10:10:40 -0700108 IPv6Address ip = value.getValue();
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800109 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700110
111 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800112 assertEquals(w.input.split("/")[0], ip.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700113 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800114 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
115
Gregor Maier5615b6c2013-12-11 22:29:07 -0800116 if (w.expectedMaskLength == -1) {
117 assertFalse(value.getMask().isCidrMask());
118 try {
119 value.getMask().asCidrMaskLength();
120 fail("Expected IllegalStateException not thrown");
121 } catch(IllegalStateException e) {
122 //expected
123 }
124 } else {
125 assertTrue(value.getMask().isCidrMask());
126 assertEquals("Input " + w.input, w.expectedMaskLength,
127 value.getMask().asCidrMaskLength());
128 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800129
130 byte[] address = inetAddress.getAddress();
131 assertEquals(address.length, value.getValue().getBytes().length);
132
133 for (int j = 0; j < address.length; j++) {
134 address[j] &= w.expectedMask[j];
135 }
136
137 assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address));
138 assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask));
139 }
140 for (int i = 0; i <= 128; i++) {
141 String ipString = String.format("8001:2::1/%d", i);
142 IPv6AddressWithMask value = IPv6AddressWithMask.of(ipString);
143 assertEquals("Input " + ipString, i, value.getMask().asCidrMaskLength());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700144 }
145 }
146
147
148 @Test
149 public void testOfString() throws UnknownHostException {
150 for(int i=0; i < testStrings.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700151 IPv6Address ip = IPv6Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700152 InetAddress inetAddress = InetAddress.getByName(testStrings[i]);
153
154 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
155 assertEquals(testStrings[i], ip.toString());
156 }
157 }
158
159 @Test
160 public void testOfByteArray() throws UnknownHostException {
161 for(int i=0; i < testStrings.length; i++ ) {
162 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700163 IPv6Address ip = IPv6Address.of(bytes);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700164 assertEquals(testStrings[i], ip.toString());
165 assertArrayEquals(bytes, ip.getBytes());
166 }
167 }
168
169 @Test
170 public void testReadFrom() throws OFParseError, UnknownHostException {
171 for(int i=0; i < testStrings.length; i++ ) {
172 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700173 IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700174 assertEquals(testStrings[i], ip.toString());
175 assertArrayEquals(bytes, ip.getBytes());
176 }
177 }
178
179 String[] invalidIPs = {
180 "",
181 ":",
182 "1:2:3:4:5:6:7:8:9",
183 "1:2:3:4:5:6:7:8:",
184 "1:2:3:4:5:6:7:8g",
185 "1:2:3:",
186 "12345::",
187 "1::3::8",
188 "::3::"
189 };
190
191 @Test
192 public void testInvalidIPs() throws OFParseError {
193 for(String invalid : invalidIPs) {
194 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700195 IPv6Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700196 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
197 } catch(IllegalArgumentException e) {
198 // ok
199 }
200 }
201 }
202
203 @Test
204 public void testZeroCompression() throws OFParseError {
Yotam Harchola289d552013-09-16 10:10:40 -0700205 assertEquals("::", IPv6Address.of("::").toString(true, false));
206 assertEquals("0:0:0:0:0:0:0:0", IPv6Address.of("::").toString(false, false));
207 assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6Address.of("::").toString(false, true));
208 assertEquals("1::4:5:6:0:8", IPv6Address.of("1:0:0:4:5:6:0:8").toString(true, false));
209 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 -0700210 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800211
212 @Test
213 public void testSuperclass() throws Exception {
214 for(String ipString: testStrings) {
215 IPAddress<?> superIp = IPAddress.of(ipString);
216 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
217 assertEquals(IPv6Address.of(ipString), superIp);
218 }
219
220 for(WithMaskTaskCase w: withMasks) {
221 String ipMaskedString = w.input;
222 IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
223 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
224 assertEquals(IPv6AddressWithMask.of(ipMaskedString), superIp);
225 }
226 }
Gregor Maier1ff55972013-12-11 02:22:56 -0800227
228 @Test
229 public void testOfExceptions() throws Exception {
230 try {
231 IPv6AddressWithMask.of(null);
232 fail("Should have thrown NullPointerException");
233 } catch (NullPointerException e) {
234 assertNotNull(e.getMessage());
235 }
236 try {
237 String s = null;
238 IPv6Address.of(s);
239 fail("Should have thrown NullPointerException");
240 } catch (NullPointerException e) {
241 assertNotNull(e.getMessage());
242 }
243 try {
244 byte[] b = null;
245 IPv6Address.of(b);
246 fail("Should have thrown NullPointerException");
247 } catch (NullPointerException e) {
248 assertNotNull(e.getMessage());
249 }
250 try {
251 byte[] b = new byte[7];
252 IPv6Address.of(b);
253 fail("Should have thrown IllegalArgumentException");
254 } catch (IllegalArgumentException e) {
255 // expected
256 }
257 try {
258 byte[] b = new byte[9];
259 IPv6Address.of(b);
260 fail("Should have thrown IllegalArgumentException");
261 } catch (IllegalArgumentException e) {
262 // expected
263 }
264 try {
265 IPv6AddressWithMask.of(IPv6Address.of("1::"), null);
266 fail("Should have thrown NullPointerException");
267 } catch (NullPointerException e) {
268 assertNotNull(e.getMessage());
269 }
270 try {
271 IPv6AddressWithMask.of(null, IPv6Address.of("255::"));
272 fail("Should have thrown NullPointerException");
273 } catch (NullPointerException e) {
274 assertNotNull(e.getMessage());
275 }
276 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700277}