Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Andreas Wundsam | 12cea88 | 2013-12-15 15:05:50 -0800 | [diff] [blame] | 3 | import static org.hamcrest.CoreMatchers.equalTo; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 4 | import static org.junit.Assert.assertArrayEquals; |
| 5 | import static org.junit.Assert.assertEquals; |
Andreas Wundsam | acaf650 | 2013-12-15 15:08:21 -0800 | [diff] [blame] | 6 | import static org.junit.Assert.assertFalse; |
| 7 | import static org.junit.Assert.assertNotNull; |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 8 | import static org.junit.Assert.assertThat; |
Andreas Wundsam | acaf650 | 2013-12-15 15:08:21 -0800 | [diff] [blame] | 9 | import static org.junit.Assert.assertTrue; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 10 | import static org.junit.Assert.fail; |
| 11 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 12 | import org.hamcrest.CoreMatchers; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 13 | import org.jboss.netty.buffer.ChannelBuffers; |
| 14 | import org.junit.Test; |
| 15 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 16 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 17 | public class IPv4AddressTest { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 18 | byte[][] testAddresses = new byte[][] { |
| 19 | {0x01, 0x02, 0x03, 0x04 }, |
| 20 | {127, 0, 0, 1}, |
| 21 | {(byte) 192, (byte) 168, 0, 100 }, |
| 22 | {(byte) 255, (byte) 255, (byte) 255, (byte) 255 } |
| 23 | }; |
| 24 | |
| 25 | String[] testStrings = { |
| 26 | "1.2.3.4", |
| 27 | "127.0.0.1", |
| 28 | "192.168.0.100", |
| 29 | "255.255.255.255" |
| 30 | }; |
| 31 | |
| 32 | int[] testInts = { |
| 33 | 0x01020304, |
| 34 | 0x7f000001, |
| 35 | (192 << 24) | (168 << 16) | 100, |
| 36 | 0xffffffff |
| 37 | }; |
| 38 | |
| 39 | String[] invalidIPs = { |
| 40 | "", |
| 41 | ".", |
| 42 | "1.2..3.4", |
| 43 | "1.2.3.4.", |
| 44 | "257.11.225.1", |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 45 | "256.11.225.1", |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 46 | "-1.2.3.4", |
| 47 | "1.2.3.4.5", |
| 48 | "1.x.3.4", |
| 49 | "1.2x.3.4" |
| 50 | }; |
| 51 | |
| 52 | String[] ipsWithMask = { |
| 53 | "1.2.3.4/24", |
| 54 | "192.168.130.140/255.255.192.0", |
| 55 | "127.0.0.1/8", |
| 56 | "8.8.8.8", |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 57 | "8.8.8.8/32", |
| 58 | "0.0.0.0/0", |
| 59 | "192.168.130.140/255.0.255.0", |
| 60 | "1.2.3.4/0.127.0.255" |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | boolean[] hasMask = { |
| 64 | true, |
| 65 | true, |
| 66 | true, |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 67 | false, |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 68 | false, |
| 69 | true, |
| 70 | true, |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 71 | true |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | byte[][][] ipsWithMaskValues = { |
| 75 | new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00 } }, |
| 76 | new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xC0, (byte)0x00 } }, |
| 77 | new byte[][] { new byte[] { (byte)0x7F, (byte)0x00, (byte)0x00, (byte)0x01 }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0x00 } }, |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 78 | new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } }, |
| 79 | new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } }, |
| 80 | new byte[][] { new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 } }, |
| 81 | new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0xFF, (byte)0x00 } }, |
| 82 | new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0x00, (byte)0x7F, (byte)0x00, (byte)0xFF } } |
| 83 | }; |
| 84 | |
| 85 | int[] ipsWithMaskLengths = { |
| 86 | 24, |
| 87 | 18, |
| 88 | 8, |
| 89 | 32, |
| 90 | 32, |
| 91 | 0, |
| 92 | -1, |
| 93 | -1 |
| 94 | }; |
| 95 | |
| 96 | String[] invalidIpsWithMask = { |
| 97 | "asdf", |
| 98 | "1.2.3.4/33", |
| 99 | "1.2.3.4/34", |
| 100 | "1.2.3.4/-1", |
| 101 | "1.2.3.4/256.0.0.0", |
| 102 | "1.256.3.4/255.255.0.0", |
| 103 | "1.2.3.4/255.255.0.0.0", |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
Andreas Wundsam | 12cea88 | 2013-12-15 15:05:50 -0800 | [diff] [blame] | 106 | @Test |
| 107 | public void testMaskedMatchesCidr() { |
| 108 | IPv4AddressWithMask slash28 = IPv4AddressWithMask.of("10.0.42.16/28"); |
| 109 | |
| 110 | String[] notContained = {"0.0.0.0", "11.0.42.16", "10.0.41.1", "10.0.42.0", "10.0.42.15", |
| 111 | "10.0.42.32", "255.255.255.255" }; |
| 112 | |
| 113 | for(String n: notContained) { |
| 114 | assertThat(String.format("slash 28 %s should not contain address %s", |
| 115 | slash28, n), |
| 116 | slash28.matches(IPv4Address.of(n)), equalTo(false)); |
| 117 | } |
| 118 | for(int i=16; i < 32; i++) { |
| 119 | IPv4Address c = IPv4Address.of(String.format("10.0.42.%d", i)); |
| 120 | assertThat(String.format("slash 28 %s should contain address %s", |
| 121 | slash28, c), |
| 122 | slash28.matches(c), equalTo(true)); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void testMaskedMatchesArbitrary() { |
| 128 | // irregular octect on the 3rd bitmask requires '1'bit to be set |
| 129 | // 4 bit unset, all others arbitrary |
| 130 | IPv4AddressWithMask slash28 = IPv4AddressWithMask.of("1.2.1.4/255.255.5.255"); |
| 131 | |
| 132 | String[] notContained = {"0.0.0.0", "1.2.3.5", "1.2.3.3", |
| 133 | "1.2.0.4", "1.2.2.4", "1.2.4.4", "1.2.5.4", "1.2.6.4", "1.2.7.4", |
| 134 | "1.2.8.4", "1.2.12.4", "1.2.13.4" |
| 135 | }; |
| 136 | String[] contained = {"1.2.1.4", "1.2.3.4", "1.2.9.4", "1.2.11.4", "1.2.251.4", |
| 137 | }; |
| 138 | |
| 139 | for(String n: notContained) { |
| 140 | assertThat(String.format("slash 28 %s should not contain address %s", |
| 141 | slash28, n), |
| 142 | slash28.matches(IPv4Address.of(n)), equalTo(false)); |
| 143 | } |
| 144 | for(String c: contained) { |
| 145 | IPv4Address addr = IPv4Address.of(c); |
| 146 | assertThat(String.format("slash 28 %s should contain address %s", |
| 147 | slash28, addr), |
| 148 | slash28.matches(addr), equalTo(true)); |
| 149 | } |
| 150 | |
| 151 | } |
| 152 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 153 | |
| 154 | @Test |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 155 | public void testConstants() { |
| 156 | byte[] zeros = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }; |
| 157 | byte[] ones = { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF }; |
| 158 | // Make sure class initializtation and static assignment don't get |
| 159 | // messed up. Test everything twice for cached values |
| 160 | assertTrue(IPv4Address.NONE.isCidrMask()); |
| 161 | assertEquals(0, IPv4Address.NONE.asCidrMaskLength()); |
| 162 | assertArrayEquals(zeros, IPv4Address.NONE.getBytes()); |
| 163 | assertTrue(IPv4Address.NONE.isCidrMask()); |
| 164 | assertEquals(0, IPv4Address.NONE.asCidrMaskLength()); |
| 165 | assertArrayEquals(zeros, IPv4Address.NONE.getBytes()); |
| 166 | |
| 167 | assertTrue(IPv4Address.NO_MASK.isCidrMask()); |
| 168 | assertEquals(32, IPv4Address.NO_MASK.asCidrMaskLength()); |
| 169 | assertArrayEquals(ones, IPv4Address.NO_MASK.getBytes()); |
| 170 | assertTrue(IPv4Address.NO_MASK.isCidrMask()); |
| 171 | assertEquals(32, IPv4Address.NO_MASK.asCidrMaskLength()); |
| 172 | assertArrayEquals(ones, IPv4Address.NO_MASK.getBytes()); |
| 173 | |
| 174 | assertTrue(IPv4Address.FULL_MASK.isCidrMask()); |
| 175 | assertEquals(0, IPv4Address.FULL_MASK.asCidrMaskLength()); |
| 176 | assertArrayEquals(zeros, IPv4Address.FULL_MASK.getBytes()); |
| 177 | assertTrue(IPv4Address.FULL_MASK.isCidrMask()); |
| 178 | assertEquals(0, IPv4Address.FULL_MASK.asCidrMaskLength()); |
| 179 | assertArrayEquals(zeros, IPv4Address.FULL_MASK.getBytes()); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | @Test |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 184 | public void testOfString() { |
| 185 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 186 | IPv4Address ip = IPv4Address.of(testStrings[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 187 | assertEquals(testInts[i], ip.getInt()); |
| 188 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 189 | assertEquals(testStrings[i], ip.toString()); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | @Test |
| 194 | public void testOfByteArray() { |
| 195 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 196 | IPv4Address ip = IPv4Address.of(testAddresses[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 197 | assertEquals(testInts[i], ip.getInt()); |
| 198 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 199 | assertEquals(testStrings[i], ip.toString()); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | @Test |
| 204 | public void testReadFrom() throws OFParseError { |
| 205 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 206 | IPv4Address ip = IPv4Address.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i])); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 207 | assertEquals(testInts[i], ip.getInt()); |
| 208 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 209 | assertEquals(testStrings[i], ip.toString()); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | |
| 214 | @Test |
| 215 | public void testInvalidIPs() throws OFParseError { |
| 216 | for(String invalid : invalidIPs) { |
| 217 | try { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 218 | IPv4Address.of(invalid); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 219 | fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException"); |
| 220 | } catch(IllegalArgumentException e) { |
| 221 | // ok |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | @Test |
| 227 | public void testOfMasked() throws OFParseError { |
| 228 | for (int i = 0; i < ipsWithMask.length; i++) { |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 229 | IPv4AddressWithMask value = IPv4AddressWithMask.of(ipsWithMask[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 230 | if (!hasMask[i]) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 231 | IPv4Address ip = value.getValue(); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 232 | assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 233 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 234 | IPv4Address mask = value.getMask(); |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 235 | if (ipsWithMaskLengths[i] == -1) { |
| 236 | assertFalse(mask.isCidrMask()); |
| 237 | try { |
| 238 | mask.asCidrMaskLength(); |
| 239 | fail("Expected IllegalStateException not thrown"); |
| 240 | } catch(IllegalStateException e) { |
| 241 | //expected |
| 242 | } |
| 243 | } else { |
| 244 | assertTrue(mask.isCidrMask()); |
| 245 | assertEquals(ipsWithMaskLengths[i], mask.asCidrMaskLength()); |
| 246 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 247 | assertArrayEquals(ipsWithMaskValues[i][1], mask.getBytes()); |
| 248 | byte[] ipBytes = new byte[4]; |
| 249 | System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4); |
| 250 | assertEquals(ipBytes.length, value.getValue().getBytes().length); |
| 251 | for (int j = 0; j < ipBytes.length; j++) { |
| 252 | ipBytes[j] &= ipsWithMaskValues[i][1][j]; |
| 253 | } |
| 254 | |
| 255 | assertArrayEquals(ipBytes, value.getValue().getBytes()); |
| 256 | assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value), |
| 257 | value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1])); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | @Test |
| 262 | public void testOfMaskedInvalid() throws Exception { |
| 263 | for(String invalid : invalidIpsWithMask) { |
| 264 | try { |
| 265 | IPv4Address.of(invalid); |
| 266 | fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException"); |
| 267 | } catch(IllegalArgumentException e) { |
| 268 | // ok |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | @Test |
| 274 | public void testSuperclass() throws Exception { |
| 275 | for(String ipString: testStrings) { |
| 276 | IPAddress<?> superIp = IPAddress.of(ipString); |
| 277 | assertEquals(IPVersion.IPv4, superIp.getIpVersion()); |
| 278 | assertEquals(IPv4Address.of(ipString), superIp); |
| 279 | } |
| 280 | |
| 281 | for(String ipMaskedString: ipsWithMask) { |
| 282 | IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString); |
| 283 | assertEquals(IPVersion.IPv4, superIp.getIpVersion()); |
| 284 | assertEquals(IPv4AddressWithMask.of(ipMaskedString), superIp); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 287 | |
| 288 | @Test |
| 289 | public void testOfExceptions() { |
| 290 | // We check if the message of a caught NPE is set to a useful message |
| 291 | // as a hacky way of verifying that we got an NPE thrown by use rather |
| 292 | // than one the JVM created for a null access. |
| 293 | try { |
| 294 | String s = null; |
| 295 | IPv4Address.of(s); |
| 296 | fail("Should have thrown NullPointerException"); |
| 297 | } catch (NullPointerException e) { |
| 298 | assertNotNull(e.getMessage()); |
| 299 | } |
| 300 | try { |
| 301 | byte[] b = null; |
| 302 | IPv4Address.of(b); |
| 303 | fail("Should have thrown NullPointerException"); |
| 304 | } catch (NullPointerException e) { |
| 305 | assertNotNull(e.getMessage()); |
| 306 | } |
| 307 | try { |
| 308 | byte[] b = new byte[3]; |
| 309 | IPv4Address.of(b); |
| 310 | fail("Should have thrown IllegalArgumentException"); |
| 311 | } catch (IllegalArgumentException e) { |
| 312 | // expected |
| 313 | } |
| 314 | try { |
| 315 | byte[] b = new byte[5]; |
| 316 | IPv4Address.of(b); |
| 317 | fail("Should have thrown IllegalArgumentException"); |
| 318 | } catch (IllegalArgumentException e) { |
| 319 | // expected |
| 320 | } |
| 321 | try { |
| 322 | IPv4AddressWithMask.of(null); |
| 323 | fail("Should have thrown NullPointerException"); |
| 324 | } catch (NullPointerException e) { |
| 325 | assertNotNull(e.getMessage()); |
| 326 | } |
| 327 | try { |
| 328 | IPv4AddressWithMask.of(IPv4Address.of("1.2.3.4"), null); |
| 329 | fail("Should have thrown NullPointerException"); |
| 330 | } catch (NullPointerException e) { |
| 331 | assertNotNull(e.getMessage()); |
| 332 | } |
| 333 | try { |
| 334 | IPv4AddressWithMask.of(null, IPv4Address.of("255.0.0.0")); |
| 335 | fail("Should have thrown NullPointerException"); |
| 336 | } catch (NullPointerException e) { |
| 337 | assertNotNull(e.getMessage()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | } |