blob: 6963c21eea1aeb16c92154a62272edb3d7b28b88 [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),
Gregor Maiercef9e3e2013-12-11 22:41:36 -080061 new WithMaskTaskCase("1:2:3:4:5:6:7:8/1::ff00:ff00")
62 .maskHex("00 01 00 00 00 00 00 00 00 00 00 00 ff 00 ff 00")
63 .expectedMaskLength(-1),
64 new WithMaskTaskCase("1:2:3:4:5:6:7:8/::ff00:ff00")
65 .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 ff 00 ff 00")
66 .expectedMaskLength(-1),
67 new WithMaskTaskCase("1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff::ff00:ff00")
68 .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 ff 00 ff 00")
69 .expectedMaskLength(-1),
Andreas Wundsame04c86f2013-11-05 17:13:18 -080070 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
71 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
72 new WithMaskTaskCase("1:2:3:4:5:6:7:8/128"),
73 new WithMaskTaskCase("::/0")
74 .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")
Gregor Maier7f987e62013-12-10 19:34:18 -080075 .expectedMaskLength(0),
Yotam Harcholf3f11152013-09-05 16:47:16 -070076 };
77
78 @Test
Aditya Vaja56b8b182014-03-11 13:13:58 -070079 public void testLogicalOperatorsBroadcast() {
80 assertTrue(IPv6Address.NO_MASK.not().equals(IPv6Address.FULL_MASK));
81 assertTrue(IPv6Address.NO_MASK.or(IPv6Address.FULL_MASK).
82 equals(IPv6Address.NO_MASK));
83 assertTrue(IPv6Address.NO_MASK.and(IPv6Address.FULL_MASK).
84 equals(IPv6Address.FULL_MASK));
85
86 assertTrue(IPv6Address.NO_MASK.isBroadcast());
87 assertTrue(!IPv6Address.FULL_MASK.isBroadcast());
88 }
89
90 @Test
91 public void testMaskedSubnetBroadcast() {
92 assertTrue(IPv6AddressWithMask.of("10:10::1/112")
93 .getSubnetBroadcastAddress()
94 .equals(IPv6Address.of("10:10::ffff")));
95 assertTrue(IPv6AddressWithMask.of("10:10::1/112")
96 .isSubnetBroadcastAddress(IPv6Address.of("10:10::ffff")));
97 assertTrue(!IPv6AddressWithMask.of("10:10::1/112")
98 .isSubnetBroadcastAddress(IPv6Address.of("10:10::fffd")));
99 }
100
101 @Test
Gregor Maier5615b6c2013-12-11 22:29:07 -0800102 public void testConstants() {
103 byte[] zeros = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
104 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
105 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
106 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
107 byte[] ones = { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
108 (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
109 (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
110 (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF };
111 // Make sure class initializtation and static assignment don't get
112 // messed up. Test everything twice for cached values
113 assertTrue(IPv6Address.NONE.isCidrMask());
114 assertEquals(0, IPv6Address.NONE.asCidrMaskLength());
115 assertArrayEquals(zeros, IPv6Address.NONE.getBytes());
116 assertTrue(IPv6Address.NONE.isCidrMask());
117 assertEquals(0, IPv6Address.NONE.asCidrMaskLength());
118 assertArrayEquals(zeros, IPv6Address.NONE.getBytes());
119
120 assertTrue(IPv6Address.NO_MASK.isCidrMask());
121 assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength());
122 assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes());
123 assertTrue(IPv6Address.NO_MASK.isCidrMask());
124 assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength());
125 assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes());
126
127 assertTrue(IPv6Address.FULL_MASK.isCidrMask());
128 assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength());
129 assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes());
130 assertTrue(IPv6Address.FULL_MASK.isCidrMask());
131 assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength());
132 assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes());
133 }
134
135 @Test
Yotam Harcholf3f11152013-09-05 16:47:16 -0700136 public void testMasked() throws UnknownHostException {
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800137 for(WithMaskTaskCase w: withMasks) {
138 IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input);
139 if (!w.hasMask) {
Yotam Harchola289d552013-09-16 10:10:40 -0700140 IPv6Address ip = value.getValue();
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800141 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700142
143 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800144 assertEquals(w.input.split("/")[0], ip.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700145 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800146 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
147
Gregor Maier5615b6c2013-12-11 22:29:07 -0800148 if (w.expectedMaskLength == -1) {
149 assertFalse(value.getMask().isCidrMask());
150 try {
151 value.getMask().asCidrMaskLength();
152 fail("Expected IllegalStateException not thrown");
153 } catch(IllegalStateException e) {
154 //expected
155 }
156 } else {
157 assertTrue(value.getMask().isCidrMask());
158 assertEquals("Input " + w.input, w.expectedMaskLength,
159 value.getMask().asCidrMaskLength());
160 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800161
162 byte[] address = inetAddress.getAddress();
163 assertEquals(address.length, value.getValue().getBytes().length);
164
165 for (int j = 0; j < address.length; j++) {
166 address[j] &= w.expectedMask[j];
167 }
168
169 assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address));
170 assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask));
171 }
172 for (int i = 0; i <= 128; i++) {
173 String ipString = String.format("8001:2::1/%d", i);
174 IPv6AddressWithMask value = IPv6AddressWithMask.of(ipString);
175 assertEquals("Input " + ipString, i, value.getMask().asCidrMaskLength());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700176 }
177 }
178
179
180 @Test
181 public void testOfString() throws UnknownHostException {
182 for(int i=0; i < testStrings.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700183 IPv6Address ip = IPv6Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700184 InetAddress inetAddress = InetAddress.getByName(testStrings[i]);
185
186 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
187 assertEquals(testStrings[i], ip.toString());
188 }
189 }
190
191 @Test
192 public void testOfByteArray() throws UnknownHostException {
193 for(int i=0; i < testStrings.length; i++ ) {
194 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700195 IPv6Address ip = IPv6Address.of(bytes);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700196 assertEquals(testStrings[i], ip.toString());
197 assertArrayEquals(bytes, ip.getBytes());
198 }
199 }
200
201 @Test
202 public void testReadFrom() throws OFParseError, UnknownHostException {
203 for(int i=0; i < testStrings.length; i++ ) {
204 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700205 IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700206 assertEquals(testStrings[i], ip.toString());
207 assertArrayEquals(bytes, ip.getBytes());
208 }
209 }
210
211 String[] invalidIPs = {
212 "",
213 ":",
214 "1:2:3:4:5:6:7:8:9",
215 "1:2:3:4:5:6:7:8:",
216 "1:2:3:4:5:6:7:8g",
217 "1:2:3:",
218 "12345::",
219 "1::3::8",
220 "::3::"
221 };
222
223 @Test
224 public void testInvalidIPs() throws OFParseError {
225 for(String invalid : invalidIPs) {
226 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700227 IPv6Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700228 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
229 } catch(IllegalArgumentException e) {
230 // ok
231 }
232 }
233 }
234
235 @Test
236 public void testZeroCompression() throws OFParseError {
Yotam Harchola289d552013-09-16 10:10:40 -0700237 assertEquals("::", IPv6Address.of("::").toString(true, false));
238 assertEquals("0:0:0:0:0:0:0:0", IPv6Address.of("::").toString(false, false));
239 assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6Address.of("::").toString(false, true));
240 assertEquals("1::4:5:6:0:8", IPv6Address.of("1:0:0:4:5:6:0:8").toString(true, false));
241 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 -0700242 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800243
244 @Test
245 public void testSuperclass() throws Exception {
246 for(String ipString: testStrings) {
247 IPAddress<?> superIp = IPAddress.of(ipString);
248 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
249 assertEquals(IPv6Address.of(ipString), superIp);
250 }
251
252 for(WithMaskTaskCase w: withMasks) {
253 String ipMaskedString = w.input;
254 IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
255 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
256 assertEquals(IPv6AddressWithMask.of(ipMaskedString), superIp);
257 }
258 }
Gregor Maier1ff55972013-12-11 02:22:56 -0800259
260 @Test
261 public void testOfExceptions() throws Exception {
262 try {
263 IPv6AddressWithMask.of(null);
264 fail("Should have thrown NullPointerException");
265 } catch (NullPointerException e) {
266 assertNotNull(e.getMessage());
267 }
268 try {
269 String s = null;
270 IPv6Address.of(s);
271 fail("Should have thrown NullPointerException");
272 } catch (NullPointerException e) {
273 assertNotNull(e.getMessage());
274 }
275 try {
276 byte[] b = null;
277 IPv6Address.of(b);
278 fail("Should have thrown NullPointerException");
279 } catch (NullPointerException e) {
280 assertNotNull(e.getMessage());
281 }
282 try {
283 byte[] b = new byte[7];
284 IPv6Address.of(b);
285 fail("Should have thrown IllegalArgumentException");
286 } catch (IllegalArgumentException e) {
287 // expected
288 }
289 try {
290 byte[] b = new byte[9];
291 IPv6Address.of(b);
292 fail("Should have thrown IllegalArgumentException");
293 } catch (IllegalArgumentException e) {
294 // expected
295 }
296 try {
297 IPv6AddressWithMask.of(IPv6Address.of("1::"), null);
298 fail("Should have thrown NullPointerException");
299 } catch (NullPointerException e) {
300 assertNotNull(e.getMessage());
301 }
302 try {
303 IPv6AddressWithMask.of(null, IPv6Address.of("255::"));
304 fail("Should have thrown NullPointerException");
305 } catch (NullPointerException e) {
306 assertNotNull(e.getMessage());
307 }
Aditya Vajab21043d2014-03-11 13:34:06 -0700308 try {
309 IPv6AddressWithMask.of(IPv6Address.of("10:10::0"),
310 IPv6Address.of("ffff:0:ffff::"))
311 .getSubnetBroadcastAddress();
312 fail("Should have thrown IllegalArgumentException");
313 } catch (IllegalArgumentException e) {
314 assertNotNull(e.getMessage());
315 }
Gregor Maier1ff55972013-12-11 02:22:56 -0800316 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700317}