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), |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 61 | 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 Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 66 | .expectedMaskLength(0), |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | @Test |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 70 | 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 Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 104 | public void testMasked() throws UnknownHostException { |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 105 | for(WithMaskTaskCase w: withMasks) { |
| 106 | IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input); |
| 107 | if (!w.hasMask) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 108 | IPv6Address ip = value.getValue(); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 109 | InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 110 | |
| 111 | assertArrayEquals(ip.getBytes(), inetAddress.getAddress()); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 112 | assertEquals(w.input.split("/")[0], ip.toString()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 113 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 114 | InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]); |
| 115 | |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 116 | 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 Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 129 | |
| 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 Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | |
| 148 | @Test |
| 149 | public void testOfString() throws UnknownHostException { |
| 150 | for(int i=0; i < testStrings.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 151 | IPv6Address ip = IPv6Address.of(testStrings[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 152 | 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 Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 163 | IPv6Address ip = IPv6Address.of(bytes); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 164 | 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 Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 173 | IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 174 | 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 Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 195 | IPv6Address.of(invalid); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 196 | 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 Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 205 | 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 Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 210 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 211 | |
| 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 Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 227 | |
| 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 Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 277 | } |