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; |
Ronald Li | 9d777a5 | 2015-02-07 06:19:29 -0800 | [diff] [blame] | 10 | import org.hamcrest.Matchers; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 11 | import org.jboss.netty.buffer.ChannelBuffers; |
| 12 | import org.junit.Test; |
| 13 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 14 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 15 | import com.google.common.io.BaseEncoding; |
| 16 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 17 | public class IPv6AddressTest { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 18 | |
| 19 | String[] testStrings = { |
| 20 | "::", |
| 21 | "::1", |
| 22 | "ffe0::", |
| 23 | "1:2:3:4:5:6:7:8" |
| 24 | }; |
| 25 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 26 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 27 | private final BaseEncoding hex = BaseEncoding.base16().omitPadding().lowerCase(); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 28 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 29 | private class WithMaskTaskCase { |
| 30 | final String input; |
| 31 | boolean hasMask; |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 32 | int expectedMaskLength = 128; |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 33 | byte[] expectedMask = hex.decode("ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff".replaceAll(" ", "")); |
| 34 | |
| 35 | public WithMaskTaskCase(String input) { |
| 36 | super(); |
| 37 | this.input = input; |
| 38 | } |
| 39 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 40 | public WithMaskTaskCase maskHex(String string) { |
| 41 | string = string.replaceAll(" ", ""); |
| 42 | this.hasMask = true; |
| 43 | expectedMask = hex.decode(string); |
| 44 | return this; |
| 45 | } |
| 46 | |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 47 | public WithMaskTaskCase expectedMaskLength(int expectedLength) { |
| 48 | this.expectedMaskLength = expectedLength; |
| 49 | return this; |
| 50 | } |
| 51 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | WithMaskTaskCase[] withMasks = new WithMaskTaskCase[] { |
| 55 | new WithMaskTaskCase("1::1/80") |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 56 | .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00") |
| 57 | .expectedMaskLength(80), |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 58 | |
| 59 | new WithMaskTaskCase("ffff:ffee:1::/ff00:ff00:ff00:ff00::") |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 60 | .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00") |
| 61 | .expectedMaskLength(-1), |
Gregor Maier | cef9e3e | 2013-12-11 22:41:36 -0800 | [diff] [blame] | 62 | new WithMaskTaskCase("1:2:3:4:5:6:7:8/1::ff00:ff00") |
| 63 | .maskHex("00 01 00 00 00 00 00 00 00 00 00 00 ff 00 ff 00") |
| 64 | .expectedMaskLength(-1), |
| 65 | new WithMaskTaskCase("1:2:3:4:5:6:7:8/::ff00:ff00") |
| 66 | .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 ff 00 ff 00") |
| 67 | .expectedMaskLength(-1), |
| 68 | new WithMaskTaskCase("1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff::ff00:ff00") |
| 69 | .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 ff 00 ff 00") |
| 70 | .expectedMaskLength(-1), |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 71 | new WithMaskTaskCase("8:8:8:8:8:8:8:8"), |
| 72 | new WithMaskTaskCase("8:8:8:8:8:8:8:8"), |
| 73 | new WithMaskTaskCase("1:2:3:4:5:6:7:8/128"), |
| 74 | new WithMaskTaskCase("::/0") |
| 75 | .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] | 76 | .expectedMaskLength(0), |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | @Test |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 80 | public void testLogicalOperatorsBroadcast() { |
| 81 | assertTrue(IPv6Address.NO_MASK.not().equals(IPv6Address.FULL_MASK)); |
| 82 | assertTrue(IPv6Address.NO_MASK.or(IPv6Address.FULL_MASK). |
| 83 | equals(IPv6Address.NO_MASK)); |
| 84 | assertTrue(IPv6Address.NO_MASK.and(IPv6Address.FULL_MASK). |
| 85 | equals(IPv6Address.FULL_MASK)); |
| 86 | |
| 87 | assertTrue(IPv6Address.NO_MASK.isBroadcast()); |
| 88 | assertTrue(!IPv6Address.FULL_MASK.isBroadcast()); |
| 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void testMaskedSubnetBroadcast() { |
| 93 | assertTrue(IPv6AddressWithMask.of("10:10::1/112") |
| 94 | .getSubnetBroadcastAddress() |
| 95 | .equals(IPv6Address.of("10:10::ffff"))); |
| 96 | assertTrue(IPv6AddressWithMask.of("10:10::1/112") |
| 97 | .isSubnetBroadcastAddress(IPv6Address.of("10:10::ffff"))); |
| 98 | assertTrue(!IPv6AddressWithMask.of("10:10::1/112") |
| 99 | .isSubnetBroadcastAddress(IPv6Address.of("10:10::fffd"))); |
| 100 | } |
| 101 | |
| 102 | @Test |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 103 | public void testConstants() { |
| 104 | byte[] zeros = { (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)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }; |
| 108 | byte[] ones = { (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 | (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF }; |
| 112 | // Make sure class initializtation and static assignment don't get |
| 113 | // messed up. Test everything twice for cached values |
| 114 | assertTrue(IPv6Address.NONE.isCidrMask()); |
| 115 | assertEquals(0, IPv6Address.NONE.asCidrMaskLength()); |
| 116 | assertArrayEquals(zeros, IPv6Address.NONE.getBytes()); |
| 117 | assertTrue(IPv6Address.NONE.isCidrMask()); |
| 118 | assertEquals(0, IPv6Address.NONE.asCidrMaskLength()); |
| 119 | assertArrayEquals(zeros, IPv6Address.NONE.getBytes()); |
| 120 | |
| 121 | assertTrue(IPv6Address.NO_MASK.isCidrMask()); |
| 122 | assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength()); |
| 123 | assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes()); |
| 124 | assertTrue(IPv6Address.NO_MASK.isCidrMask()); |
| 125 | assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength()); |
| 126 | assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes()); |
| 127 | |
| 128 | assertTrue(IPv6Address.FULL_MASK.isCidrMask()); |
| 129 | assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength()); |
| 130 | assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes()); |
| 131 | assertTrue(IPv6Address.FULL_MASK.isCidrMask()); |
| 132 | assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength()); |
| 133 | assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes()); |
| 134 | } |
| 135 | |
| 136 | @Test |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 137 | public void testMasked() throws UnknownHostException { |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 138 | for(WithMaskTaskCase w: withMasks) { |
| 139 | IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input); |
| 140 | if (!w.hasMask) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 141 | IPv6Address ip = value.getValue(); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 142 | InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 143 | |
| 144 | assertArrayEquals(ip.getBytes(), inetAddress.getAddress()); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 145 | assertEquals(w.input.split("/")[0], ip.toString()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 146 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 147 | InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]); |
| 148 | |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 149 | if (w.expectedMaskLength == -1) { |
| 150 | assertFalse(value.getMask().isCidrMask()); |
| 151 | try { |
| 152 | value.getMask().asCidrMaskLength(); |
| 153 | fail("Expected IllegalStateException not thrown"); |
| 154 | } catch(IllegalStateException e) { |
| 155 | //expected |
| 156 | } |
| 157 | } else { |
| 158 | assertTrue(value.getMask().isCidrMask()); |
| 159 | assertEquals("Input " + w.input, w.expectedMaskLength, |
| 160 | value.getMask().asCidrMaskLength()); |
| 161 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 162 | |
| 163 | byte[] address = inetAddress.getAddress(); |
| 164 | assertEquals(address.length, value.getValue().getBytes().length); |
| 165 | |
| 166 | for (int j = 0; j < address.length; j++) { |
| 167 | address[j] &= w.expectedMask[j]; |
| 168 | } |
| 169 | |
| 170 | assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address)); |
| 171 | assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask)); |
| 172 | } |
| 173 | for (int i = 0; i <= 128; i++) { |
| 174 | String ipString = String.format("8001:2::1/%d", i); |
| 175 | IPv6AddressWithMask value = IPv6AddressWithMask.of(ipString); |
| 176 | assertEquals("Input " + ipString, i, value.getMask().asCidrMaskLength()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | |
| 181 | @Test |
| 182 | public void testOfString() throws UnknownHostException { |
| 183 | for(int i=0; i < testStrings.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 184 | IPv6Address ip = IPv6Address.of(testStrings[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 185 | InetAddress inetAddress = InetAddress.getByName(testStrings[i]); |
| 186 | |
| 187 | assertArrayEquals(ip.getBytes(), inetAddress.getAddress()); |
| 188 | assertEquals(testStrings[i], ip.toString()); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | @Test |
| 193 | public void testOfByteArray() throws UnknownHostException { |
| 194 | for(int i=0; i < testStrings.length; i++ ) { |
| 195 | byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress(); |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 196 | IPv6Address ip = IPv6Address.of(bytes); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 197 | assertEquals(testStrings[i], ip.toString()); |
| 198 | assertArrayEquals(bytes, ip.getBytes()); |
| 199 | } |
| 200 | } |
| 201 | |
Ronald Li | fea1789 | 2014-07-03 18:36:06 -0700 | [diff] [blame] | 202 | private static void testOfCidrMaskLengthHelper( |
| 203 | int cidrMaskLength, String ipStr) throws UnknownHostException { |
| 204 | byte[] ba0 = IPv6Address.ofCidrMaskLength(cidrMaskLength).getBytes(); |
| 205 | byte[] ba1 = Inet6Address.getByName(ipStr).getAddress(); |
| 206 | assertArrayEquals(ba0, ba1); |
| 207 | } |
| 208 | |
| 209 | @Test |
| 210 | public void testOfCidrMaskLength() throws UnknownHostException { |
| 211 | for (int i = 0; i <= 128; i++) { |
| 212 | assertTrue(IPv6Address.ofCidrMaskLength(i).isCidrMask()); |
| 213 | assertEquals(IPv6Address.ofCidrMaskLength(i).asCidrMaskLength(), i); |
| 214 | } |
| 215 | testOfCidrMaskLengthHelper(0, "::"); |
| 216 | testOfCidrMaskLengthHelper(1, "8000::"); |
| 217 | testOfCidrMaskLengthHelper(2, "c000::"); |
| 218 | testOfCidrMaskLengthHelper(8, "ff00::"); |
| 219 | testOfCidrMaskLengthHelper(16, "ffff::"); |
| 220 | testOfCidrMaskLengthHelper(17, "ffff:8000::"); |
| 221 | testOfCidrMaskLengthHelper(31, "ffff:fffe::"); |
| 222 | testOfCidrMaskLengthHelper(32, "ffff:ffff::"); |
| 223 | testOfCidrMaskLengthHelper(33, "ffff:ffff:8000::"); |
| 224 | testOfCidrMaskLengthHelper(46, "ffff:ffff:fffc::"); |
| 225 | testOfCidrMaskLengthHelper(48, "ffff:ffff:ffff::"); |
| 226 | testOfCidrMaskLengthHelper(55, "ffff:ffff:ffff:fe00::"); |
| 227 | testOfCidrMaskLengthHelper(56, "ffff:ffff:ffff:ff00::"); |
| 228 | testOfCidrMaskLengthHelper(59, "ffff:ffff:ffff:ffe0::"); |
| 229 | testOfCidrMaskLengthHelper(63, "ffff:ffff:ffff:fffe::"); |
| 230 | testOfCidrMaskLengthHelper(64, "ffff:ffff:ffff:ffff::"); |
| 231 | testOfCidrMaskLengthHelper(65, "ffff:ffff:ffff:ffff:8000::"); |
| 232 | testOfCidrMaskLengthHelper(67, "ffff:ffff:ffff:ffff:e000::"); |
| 233 | testOfCidrMaskLengthHelper(100, "ffff:ffff:ffff:ffff:ffff:ffff:f000::"); |
| 234 | testOfCidrMaskLengthHelper(101, "ffff:ffff:ffff:ffff:ffff:ffff:f800::"); |
| 235 | testOfCidrMaskLengthHelper(126, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc"); |
| 236 | testOfCidrMaskLengthHelper(127, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe"); |
| 237 | testOfCidrMaskLengthHelper(128, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 238 | } |
| 239 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 240 | @Test |
Ronald Li | bbf0194 | 2014-07-07 17:00:13 -0700 | [diff] [blame] | 241 | public void testWithMask() throws Exception { |
| 242 | // Sanity tests for the withMask*() syntactic sugars |
| 243 | |
| 244 | IPv6Address original = IPv6Address.of("fd12:3456:ABCD:7890::1"); |
| 245 | IPv6Address expectedValue = IPv6Address.of("fd12:3456:ABCD::"); |
| 246 | IPv6Address expectedMask = IPv6Address.of("ffff:ffff:ffff::"); |
| 247 | |
| 248 | IPv6AddressWithMask v; |
| 249 | |
| 250 | v = original.withMask(IPv6Address.of(new byte[] { |
| 251 | -1, -1, -1, -1, -1, -1, 0, 0, |
| 252 | 0, 0, 0, 0, 0, 0, 0, 0 })); |
| 253 | assertEquals(v.getValue(), expectedValue); |
| 254 | assertEquals(v.getMask(), expectedMask); |
| 255 | |
| 256 | v = original.withMask(IPv6Address.of( |
| 257 | 0xFFFF_FFFF_FFFF_0000L, 0x0000_0000_0000_0000L)); |
| 258 | assertEquals(v.getValue(), expectedValue); |
| 259 | assertEquals(v.getMask(), expectedMask); |
| 260 | |
| 261 | v = original.withMask(IPv6Address.of("ffff:ffff:ffff::")); |
| 262 | assertEquals(v.getValue(), expectedValue); |
| 263 | assertEquals(v.getMask(), expectedMask); |
| 264 | |
| 265 | Inet6Address i6a = (Inet6Address) InetAddress.getByName("ffff:ffff:ffff::"); |
| 266 | v = original.withMask(IPv6Address.of(i6a)); |
| 267 | assertEquals(v.getValue(), expectedValue); |
| 268 | assertEquals(v.getMask(), expectedMask); |
| 269 | |
| 270 | v = original.withMaskOfLength(48); |
| 271 | assertEquals(v.getValue(), expectedValue); |
| 272 | assertEquals(v.getMask(), expectedMask); |
| 273 | } |
| 274 | |
| 275 | @Test |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 276 | public void testReadFrom() throws OFParseError, UnknownHostException { |
| 277 | for(int i=0; i < testStrings.length; i++ ) { |
| 278 | byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress(); |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 279 | IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 280 | assertEquals(testStrings[i], ip.toString()); |
| 281 | assertArrayEquals(bytes, ip.getBytes()); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | String[] invalidIPs = { |
| 286 | "", |
| 287 | ":", |
| 288 | "1:2:3:4:5:6:7:8:9", |
| 289 | "1:2:3:4:5:6:7:8:", |
| 290 | "1:2:3:4:5:6:7:8g", |
| 291 | "1:2:3:", |
| 292 | "12345::", |
| 293 | "1::3::8", |
| 294 | "::3::" |
| 295 | }; |
| 296 | |
| 297 | @Test |
| 298 | public void testInvalidIPs() throws OFParseError { |
| 299 | for(String invalid : invalidIPs) { |
| 300 | try { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 301 | IPv6Address.of(invalid); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 302 | fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException"); |
| 303 | } catch(IllegalArgumentException e) { |
| 304 | // ok |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | @Test |
| 310 | public void testZeroCompression() throws OFParseError { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 311 | assertEquals("::", IPv6Address.of("::").toString(true, false)); |
| 312 | assertEquals("0:0:0:0:0:0:0:0", IPv6Address.of("::").toString(false, false)); |
| 313 | assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6Address.of("::").toString(false, true)); |
| 314 | assertEquals("1::4:5:6:0:8", IPv6Address.of("1:0:0:4:5:6:0:8").toString(true, false)); |
| 315 | 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] | 316 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 317 | |
| 318 | @Test |
| 319 | public void testSuperclass() throws Exception { |
| 320 | for(String ipString: testStrings) { |
| 321 | IPAddress<?> superIp = IPAddress.of(ipString); |
| 322 | assertEquals(IPVersion.IPv6, superIp.getIpVersion()); |
| 323 | assertEquals(IPv6Address.of(ipString), superIp); |
| 324 | } |
| 325 | |
| 326 | for(WithMaskTaskCase w: withMasks) { |
| 327 | String ipMaskedString = w.input; |
| 328 | IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString); |
| 329 | assertEquals(IPVersion.IPv6, superIp.getIpVersion()); |
| 330 | assertEquals(IPv6AddressWithMask.of(ipMaskedString), superIp); |
| 331 | } |
| 332 | } |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 333 | |
| 334 | @Test |
Ronald Li | 9d777a5 | 2015-02-07 06:19:29 -0800 | [diff] [blame] | 335 | public void testCompareTo() { |
| 336 | assertThat( |
| 337 | IPv6Address.of("fc00::1").compareTo(IPv6Address.of("fc00::2")), |
| 338 | Matchers.lessThan(0)); |
| 339 | assertThat( |
| 340 | IPv6Address.of("::1").compareTo(IPv6Address.of("fc00::")), |
| 341 | Matchers.lessThan(0)); |
| 342 | |
| 343 | // Make sure that unsigned comparison is used on the first 64 bits |
| 344 | assertThat( |
| 345 | IPv6Address.of("fc00::1").compareTo(IPv6Address.of("1234::3")), |
| 346 | Matchers.greaterThan(0)); |
| 347 | |
| 348 | // Make sure that unsigned comparison is used on the next 64 bits |
| 349 | assertThat( |
| 350 | IPv6Address.of("::8000:0:0:1").compareTo(IPv6Address.of("::1")), |
| 351 | Matchers.greaterThan(0)); |
| 352 | } |
| 353 | |
| 354 | @Test |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 355 | public void testOfExceptions() throws Exception { |
| 356 | try { |
| 357 | IPv6AddressWithMask.of(null); |
| 358 | fail("Should have thrown NullPointerException"); |
| 359 | } catch (NullPointerException e) { |
| 360 | assertNotNull(e.getMessage()); |
| 361 | } |
| 362 | try { |
| 363 | String s = null; |
| 364 | IPv6Address.of(s); |
| 365 | fail("Should have thrown NullPointerException"); |
| 366 | } catch (NullPointerException e) { |
| 367 | assertNotNull(e.getMessage()); |
| 368 | } |
| 369 | try { |
| 370 | byte[] b = null; |
| 371 | IPv6Address.of(b); |
| 372 | fail("Should have thrown NullPointerException"); |
| 373 | } catch (NullPointerException e) { |
| 374 | assertNotNull(e.getMessage()); |
| 375 | } |
| 376 | try { |
| 377 | byte[] b = new byte[7]; |
| 378 | IPv6Address.of(b); |
| 379 | fail("Should have thrown IllegalArgumentException"); |
| 380 | } catch (IllegalArgumentException e) { |
| 381 | // expected |
| 382 | } |
| 383 | try { |
| 384 | byte[] b = new byte[9]; |
| 385 | IPv6Address.of(b); |
| 386 | fail("Should have thrown IllegalArgumentException"); |
| 387 | } catch (IllegalArgumentException e) { |
| 388 | // expected |
| 389 | } |
| 390 | try { |
| 391 | IPv6AddressWithMask.of(IPv6Address.of("1::"), null); |
| 392 | fail("Should have thrown NullPointerException"); |
| 393 | } catch (NullPointerException e) { |
| 394 | assertNotNull(e.getMessage()); |
| 395 | } |
| 396 | try { |
| 397 | IPv6AddressWithMask.of(null, IPv6Address.of("255::")); |
| 398 | fail("Should have thrown NullPointerException"); |
| 399 | } catch (NullPointerException e) { |
| 400 | assertNotNull(e.getMessage()); |
| 401 | } |
Aditya Vaja | b21043d | 2014-03-11 13:34:06 -0700 | [diff] [blame] | 402 | try { |
| 403 | IPv6AddressWithMask.of(IPv6Address.of("10:10::0"), |
| 404 | IPv6Address.of("ffff:0:ffff::")) |
| 405 | .getSubnetBroadcastAddress(); |
| 406 | fail("Should have thrown IllegalArgumentException"); |
| 407 | } catch (IllegalArgumentException e) { |
| 408 | assertNotNull(e.getMessage()); |
| 409 | } |
Ronald Li | fea1789 | 2014-07-03 18:36:06 -0700 | [diff] [blame] | 410 | try { |
| 411 | IPv6Address.ofCidrMaskLength(-1); |
| 412 | fail("Should have thrown IllegalArgumentException"); |
| 413 | } catch (IllegalArgumentException e) { |
| 414 | assertNotNull(e.getMessage()); |
| 415 | } |
| 416 | try { |
| 417 | IPv6Address.ofCidrMaskLength(129); |
| 418 | fail("Should have thrown IllegalArgumentException"); |
| 419 | } catch (IllegalArgumentException e) { |
| 420 | assertNotNull(e.getMessage()); |
| 421 | } |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 422 | } |
Rich Lane | bae30f9 | 2014-08-27 12:13:20 -0700 | [diff] [blame] | 423 | |
| 424 | @Test |
| 425 | public void testZoneId() throws OFParseError { |
| 426 | assertEquals("::", IPv6Address.of("::%eth0").toString(true, false)); |
| 427 | assertEquals("1:0:0:4::8", IPv6Address.of("1:0:0:4:0:0:0:8%2").toString(true, false)); |
| 428 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 429 | } |