blob: c5212920a4e6965102bfa9aff40ceebbb1789874 [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
Gregor Maier5615b6c2013-12-11 22:29:07 -080079 public void testConstants() {
80 byte[] zeros = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
81 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
82 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
83 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
84 byte[] ones = { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
85 (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
86 (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
87 (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF };
88 // Make sure class initializtation and static assignment don't get
89 // messed up. Test everything twice for cached values
90 assertTrue(IPv6Address.NONE.isCidrMask());
91 assertEquals(0, IPv6Address.NONE.asCidrMaskLength());
92 assertArrayEquals(zeros, IPv6Address.NONE.getBytes());
93 assertTrue(IPv6Address.NONE.isCidrMask());
94 assertEquals(0, IPv6Address.NONE.asCidrMaskLength());
95 assertArrayEquals(zeros, IPv6Address.NONE.getBytes());
96
97 assertTrue(IPv6Address.NO_MASK.isCidrMask());
98 assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength());
99 assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes());
100 assertTrue(IPv6Address.NO_MASK.isCidrMask());
101 assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength());
102 assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes());
103
104 assertTrue(IPv6Address.FULL_MASK.isCidrMask());
105 assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength());
106 assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes());
107 assertTrue(IPv6Address.FULL_MASK.isCidrMask());
108 assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength());
109 assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes());
110 }
111
112 @Test
Yotam Harcholf3f11152013-09-05 16:47:16 -0700113 public void testMasked() throws UnknownHostException {
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800114 for(WithMaskTaskCase w: withMasks) {
115 IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input);
116 if (!w.hasMask) {
Yotam Harchola289d552013-09-16 10:10:40 -0700117 IPv6Address ip = value.getValue();
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800118 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700119
120 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800121 assertEquals(w.input.split("/")[0], ip.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700122 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800123 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
124
Gregor Maier5615b6c2013-12-11 22:29:07 -0800125 if (w.expectedMaskLength == -1) {
126 assertFalse(value.getMask().isCidrMask());
127 try {
128 value.getMask().asCidrMaskLength();
129 fail("Expected IllegalStateException not thrown");
130 } catch(IllegalStateException e) {
131 //expected
132 }
133 } else {
134 assertTrue(value.getMask().isCidrMask());
135 assertEquals("Input " + w.input, w.expectedMaskLength,
136 value.getMask().asCidrMaskLength());
137 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800138
139 byte[] address = inetAddress.getAddress();
140 assertEquals(address.length, value.getValue().getBytes().length);
141
142 for (int j = 0; j < address.length; j++) {
143 address[j] &= w.expectedMask[j];
144 }
145
146 assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address));
147 assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask));
148 }
149 for (int i = 0; i <= 128; i++) {
150 String ipString = String.format("8001:2::1/%d", i);
151 IPv6AddressWithMask value = IPv6AddressWithMask.of(ipString);
152 assertEquals("Input " + ipString, i, value.getMask().asCidrMaskLength());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700153 }
154 }
155
156
157 @Test
158 public void testOfString() throws UnknownHostException {
159 for(int i=0; i < testStrings.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700160 IPv6Address ip = IPv6Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700161 InetAddress inetAddress = InetAddress.getByName(testStrings[i]);
162
163 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
164 assertEquals(testStrings[i], ip.toString());
165 }
166 }
167
168 @Test
169 public void testOfByteArray() throws UnknownHostException {
170 for(int i=0; i < testStrings.length; i++ ) {
171 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700172 IPv6Address ip = IPv6Address.of(bytes);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700173 assertEquals(testStrings[i], ip.toString());
174 assertArrayEquals(bytes, ip.getBytes());
175 }
176 }
177
178 @Test
179 public void testReadFrom() throws OFParseError, UnknownHostException {
180 for(int i=0; i < testStrings.length; i++ ) {
181 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700182 IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700183 assertEquals(testStrings[i], ip.toString());
184 assertArrayEquals(bytes, ip.getBytes());
185 }
186 }
187
188 String[] invalidIPs = {
189 "",
190 ":",
191 "1:2:3:4:5:6:7:8:9",
192 "1:2:3:4:5:6:7:8:",
193 "1:2:3:4:5:6:7:8g",
194 "1:2:3:",
195 "12345::",
196 "1::3::8",
197 "::3::"
198 };
199
200 @Test
201 public void testInvalidIPs() throws OFParseError {
202 for(String invalid : invalidIPs) {
203 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700204 IPv6Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700205 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
206 } catch(IllegalArgumentException e) {
207 // ok
208 }
209 }
210 }
211
212 @Test
213 public void testZeroCompression() throws OFParseError {
Yotam Harchola289d552013-09-16 10:10:40 -0700214 assertEquals("::", IPv6Address.of("::").toString(true, false));
215 assertEquals("0:0:0:0:0:0:0:0", IPv6Address.of("::").toString(false, false));
216 assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6Address.of("::").toString(false, true));
217 assertEquals("1::4:5:6:0:8", IPv6Address.of("1:0:0:4:5:6:0:8").toString(true, false));
218 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 -0700219 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800220
221 @Test
222 public void testSuperclass() throws Exception {
223 for(String ipString: testStrings) {
224 IPAddress<?> superIp = IPAddress.of(ipString);
225 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
226 assertEquals(IPv6Address.of(ipString), superIp);
227 }
228
229 for(WithMaskTaskCase w: withMasks) {
230 String ipMaskedString = w.input;
231 IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
232 assertEquals(IPVersion.IPv6, superIp.getIpVersion());
233 assertEquals(IPv6AddressWithMask.of(ipMaskedString), superIp);
234 }
235 }
Gregor Maier1ff55972013-12-11 02:22:56 -0800236
237 @Test
238 public void testOfExceptions() throws Exception {
239 try {
240 IPv6AddressWithMask.of(null);
241 fail("Should have thrown NullPointerException");
242 } catch (NullPointerException e) {
243 assertNotNull(e.getMessage());
244 }
245 try {
246 String s = null;
247 IPv6Address.of(s);
248 fail("Should have thrown NullPointerException");
249 } catch (NullPointerException e) {
250 assertNotNull(e.getMessage());
251 }
252 try {
253 byte[] b = null;
254 IPv6Address.of(b);
255 fail("Should have thrown NullPointerException");
256 } catch (NullPointerException e) {
257 assertNotNull(e.getMessage());
258 }
259 try {
260 byte[] b = new byte[7];
261 IPv6Address.of(b);
262 fail("Should have thrown IllegalArgumentException");
263 } catch (IllegalArgumentException e) {
264 // expected
265 }
266 try {
267 byte[] b = new byte[9];
268 IPv6Address.of(b);
269 fail("Should have thrown IllegalArgumentException");
270 } catch (IllegalArgumentException e) {
271 // expected
272 }
273 try {
274 IPv6AddressWithMask.of(IPv6Address.of("1::"), null);
275 fail("Should have thrown NullPointerException");
276 } catch (NullPointerException e) {
277 assertNotNull(e.getMessage());
278 }
279 try {
280 IPv6AddressWithMask.of(null, IPv6Address.of("255::"));
281 fail("Should have thrown NullPointerException");
282 } catch (NullPointerException e) {
283 assertNotNull(e.getMessage());
284 }
285 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700286}