Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 3 | import static org.junit.Assert.*; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 4 | |
| 5 | import java.net.Inet6Address; |
| 6 | import java.net.InetAddress; |
| 7 | import java.net.UnknownHostException; |
| 8 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 9 | import org.hamcrest.CoreMatchers; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 10 | import org.jboss.netty.buffer.ChannelBuffers; |
| 11 | import org.junit.Test; |
| 12 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 13 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 14 | import com.google.common.io.BaseEncoding; |
| 15 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 16 | public class IPv6AddressTest { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 17 | |
| 18 | String[] testStrings = { |
| 19 | "::", |
| 20 | "::1", |
| 21 | "ffe0::", |
| 22 | "1:2:3:4:5:6:7:8" |
| 23 | }; |
| 24 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 25 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 26 | private final BaseEncoding hex = BaseEncoding.base16().omitPadding().lowerCase(); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 27 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 28 | private class WithMaskTaskCase { |
| 29 | final String input; |
| 30 | boolean hasMask; |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 31 | int expectedMaskLength = 128; |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 32 | 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 Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 39 | public WithMaskTaskCase maskHex(String string) { |
| 40 | string = string.replaceAll(" ", ""); |
| 41 | this.hasMask = true; |
| 42 | expectedMask = hex.decode(string); |
| 43 | return this; |
| 44 | } |
| 45 | |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 46 | public WithMaskTaskCase expectedMaskLength(int expectedLength) { |
| 47 | this.expectedMaskLength = expectedLength; |
| 48 | return this; |
| 49 | } |
| 50 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | WithMaskTaskCase[] withMasks = new WithMaskTaskCase[] { |
| 54 | new WithMaskTaskCase("1::1/80") |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 55 | .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00") |
| 56 | .expectedMaskLength(80), |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 57 | |
| 58 | new WithMaskTaskCase("ffff:ffee:1::/ff00:ff00:ff00:ff00::") |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 59 | .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00") |
| 60 | .expectedMaskLength(-1), |
Gregor Maier | cef9e3e | 2013-12-11 22:41:36 -0800 | [diff] [blame] | 61 | 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 Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 70 | 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 Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 75 | .expectedMaskLength(0), |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | @Test |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 79 | 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 Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 113 | public void testMasked() throws UnknownHostException { |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 114 | for(WithMaskTaskCase w: withMasks) { |
| 115 | IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input); |
| 116 | if (!w.hasMask) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 117 | IPv6Address ip = value.getValue(); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 118 | InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 119 | |
| 120 | assertArrayEquals(ip.getBytes(), inetAddress.getAddress()); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 121 | assertEquals(w.input.split("/")[0], ip.toString()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 122 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 123 | InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]); |
| 124 | |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 125 | 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 Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 138 | |
| 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 Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | |
| 157 | @Test |
| 158 | public void testOfString() throws UnknownHostException { |
| 159 | for(int i=0; i < testStrings.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 160 | IPv6Address ip = IPv6Address.of(testStrings[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 161 | 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 Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 172 | IPv6Address ip = IPv6Address.of(bytes); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 173 | 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 Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 182 | IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 183 | 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 Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 204 | IPv6Address.of(invalid); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 205 | 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 Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 214 | 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 Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 219 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 220 | |
| 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 Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 236 | |
| 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 Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 286 | } |