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 | |
Ronald Li | 5d0ca65 | 2014-07-05 02:14:11 -0700 | [diff] [blame] | 12 | import java.net.Inet4Address; |
| 13 | import java.net.InetAddress; |
| 14 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 15 | import org.hamcrest.CoreMatchers; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 16 | import org.jboss.netty.buffer.ChannelBuffers; |
| 17 | import org.junit.Test; |
| 18 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 19 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 20 | public class IPv4AddressTest { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 21 | byte[][] testAddresses = new byte[][] { |
| 22 | {0x01, 0x02, 0x03, 0x04 }, |
| 23 | {127, 0, 0, 1}, |
| 24 | {(byte) 192, (byte) 168, 0, 100 }, |
| 25 | {(byte) 255, (byte) 255, (byte) 255, (byte) 255 } |
| 26 | }; |
| 27 | |
| 28 | String[] testStrings = { |
| 29 | "1.2.3.4", |
| 30 | "127.0.0.1", |
| 31 | "192.168.0.100", |
| 32 | "255.255.255.255" |
| 33 | }; |
| 34 | |
| 35 | int[] testInts = { |
| 36 | 0x01020304, |
| 37 | 0x7f000001, |
| 38 | (192 << 24) | (168 << 16) | 100, |
| 39 | 0xffffffff |
| 40 | }; |
| 41 | |
| 42 | String[] invalidIPs = { |
| 43 | "", |
| 44 | ".", |
| 45 | "1.2..3.4", |
| 46 | "1.2.3.4.", |
| 47 | "257.11.225.1", |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 48 | "256.11.225.1", |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 49 | "-1.2.3.4", |
| 50 | "1.2.3.4.5", |
| 51 | "1.x.3.4", |
| 52 | "1.2x.3.4" |
| 53 | }; |
| 54 | |
| 55 | String[] ipsWithMask = { |
| 56 | "1.2.3.4/24", |
| 57 | "192.168.130.140/255.255.192.0", |
| 58 | "127.0.0.1/8", |
| 59 | "8.8.8.8", |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 60 | "8.8.8.8/32", |
| 61 | "0.0.0.0/0", |
| 62 | "192.168.130.140/255.0.255.0", |
| 63 | "1.2.3.4/0.127.0.255" |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | boolean[] hasMask = { |
| 67 | true, |
| 68 | true, |
| 69 | true, |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 70 | false, |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 71 | false, |
| 72 | true, |
| 73 | true, |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 74 | true |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | byte[][][] ipsWithMaskValues = { |
| 78 | new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00 } }, |
| 79 | new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xC0, (byte)0x00 } }, |
| 80 | 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] | 81 | new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } }, |
| 82 | new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } }, |
| 83 | new byte[][] { new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 } }, |
| 84 | new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0xFF, (byte)0x00 } }, |
| 85 | new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0x00, (byte)0x7F, (byte)0x00, (byte)0xFF } } |
| 86 | }; |
| 87 | |
| 88 | int[] ipsWithMaskLengths = { |
| 89 | 24, |
| 90 | 18, |
| 91 | 8, |
| 92 | 32, |
| 93 | 32, |
| 94 | 0, |
| 95 | -1, |
| 96 | -1 |
| 97 | }; |
| 98 | |
| 99 | String[] invalidIpsWithMask = { |
| 100 | "asdf", |
| 101 | "1.2.3.4/33", |
| 102 | "1.2.3.4/34", |
| 103 | "1.2.3.4/-1", |
| 104 | "1.2.3.4/256.0.0.0", |
| 105 | "1.256.3.4/255.255.0.0", |
| 106 | "1.2.3.4/255.255.0.0.0", |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Andreas Wundsam | 12cea88 | 2013-12-15 15:05:50 -0800 | [diff] [blame] | 109 | @Test |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 110 | public void testLogicalOperatorsBroadcast() { |
| 111 | assertTrue(IPv4Address.NO_MASK.not().equals(IPv4Address.FULL_MASK)); |
| 112 | assertTrue(IPv4Address.NO_MASK.or(IPv4Address.FULL_MASK). |
| 113 | equals(IPv4Address.NO_MASK)); |
| 114 | assertTrue(IPv4Address.NO_MASK.and(IPv4Address.FULL_MASK). |
| 115 | equals(IPv4Address.FULL_MASK)); |
| 116 | |
| 117 | assertTrue(IPv4Address.NO_MASK.isBroadcast()); |
| 118 | assertTrue(!IPv4Address.FULL_MASK.isBroadcast()); |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | public void testMaskedSubnetBroadcast() { |
| 123 | assertTrue(IPv4AddressWithMask.of("10.10.10.1/24") |
| 124 | .getSubnetBroadcastAddress() |
| 125 | .equals(IPv4Address.of("10.10.10.255"))); |
| 126 | assertTrue(IPv4AddressWithMask.of("10.10.10.1/24") |
| 127 | .isSubnetBroadcastAddress(IPv4Address.of("10.10.10.255"))); |
| 128 | assertTrue(!IPv4AddressWithMask.of("10.10.10.1/24") |
| 129 | .isSubnetBroadcastAddress(IPv4Address.of("10.10.10.254"))); |
| 130 | } |
| 131 | |
| 132 | @Test |
Andreas Wundsam | 12cea88 | 2013-12-15 15:05:50 -0800 | [diff] [blame] | 133 | public void testMaskedMatchesCidr() { |
| 134 | IPv4AddressWithMask slash28 = IPv4AddressWithMask.of("10.0.42.16/28"); |
| 135 | |
| 136 | String[] notContained = {"0.0.0.0", "11.0.42.16", "10.0.41.1", "10.0.42.0", "10.0.42.15", |
| 137 | "10.0.42.32", "255.255.255.255" }; |
| 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(int i=16; i < 32; i++) { |
| 145 | IPv4Address c = IPv4Address.of(String.format("10.0.42.%d", i)); |
| 146 | assertThat(String.format("slash 28 %s should contain address %s", |
| 147 | slash28, c), |
| 148 | slash28.matches(c), equalTo(true)); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | @Test |
| 153 | public void testMaskedMatchesArbitrary() { |
| 154 | // irregular octect on the 3rd bitmask requires '1'bit to be set |
| 155 | // 4 bit unset, all others arbitrary |
| 156 | IPv4AddressWithMask slash28 = IPv4AddressWithMask.of("1.2.1.4/255.255.5.255"); |
| 157 | |
| 158 | String[] notContained = {"0.0.0.0", "1.2.3.5", "1.2.3.3", |
| 159 | "1.2.0.4", "1.2.2.4", "1.2.4.4", "1.2.5.4", "1.2.6.4", "1.2.7.4", |
| 160 | "1.2.8.4", "1.2.12.4", "1.2.13.4" |
| 161 | }; |
| 162 | String[] contained = {"1.2.1.4", "1.2.3.4", "1.2.9.4", "1.2.11.4", "1.2.251.4", |
| 163 | }; |
| 164 | |
| 165 | for(String n: notContained) { |
| 166 | assertThat(String.format("slash 28 %s should not contain address %s", |
| 167 | slash28, n), |
| 168 | slash28.matches(IPv4Address.of(n)), equalTo(false)); |
| 169 | } |
| 170 | for(String c: contained) { |
| 171 | IPv4Address addr = IPv4Address.of(c); |
| 172 | assertThat(String.format("slash 28 %s should contain address %s", |
| 173 | slash28, addr), |
| 174 | slash28.matches(addr), equalTo(true)); |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 179 | |
| 180 | @Test |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 181 | public void testConstants() { |
| 182 | byte[] zeros = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }; |
| 183 | byte[] ones = { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF }; |
| 184 | // Make sure class initializtation and static assignment don't get |
| 185 | // messed up. Test everything twice for cached values |
| 186 | assertTrue(IPv4Address.NONE.isCidrMask()); |
| 187 | assertEquals(0, IPv4Address.NONE.asCidrMaskLength()); |
| 188 | assertArrayEquals(zeros, IPv4Address.NONE.getBytes()); |
| 189 | assertTrue(IPv4Address.NONE.isCidrMask()); |
| 190 | assertEquals(0, IPv4Address.NONE.asCidrMaskLength()); |
| 191 | assertArrayEquals(zeros, IPv4Address.NONE.getBytes()); |
| 192 | |
| 193 | assertTrue(IPv4Address.NO_MASK.isCidrMask()); |
| 194 | assertEquals(32, IPv4Address.NO_MASK.asCidrMaskLength()); |
| 195 | assertArrayEquals(ones, IPv4Address.NO_MASK.getBytes()); |
| 196 | assertTrue(IPv4Address.NO_MASK.isCidrMask()); |
| 197 | assertEquals(32, IPv4Address.NO_MASK.asCidrMaskLength()); |
| 198 | assertArrayEquals(ones, IPv4Address.NO_MASK.getBytes()); |
| 199 | |
| 200 | assertTrue(IPv4Address.FULL_MASK.isCidrMask()); |
| 201 | assertEquals(0, IPv4Address.FULL_MASK.asCidrMaskLength()); |
| 202 | assertArrayEquals(zeros, IPv4Address.FULL_MASK.getBytes()); |
| 203 | assertTrue(IPv4Address.FULL_MASK.isCidrMask()); |
| 204 | assertEquals(0, IPv4Address.FULL_MASK.asCidrMaskLength()); |
| 205 | assertArrayEquals(zeros, IPv4Address.FULL_MASK.getBytes()); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | @Test |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 210 | public void testOfString() { |
| 211 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 212 | IPv4Address ip = IPv4Address.of(testStrings[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 213 | assertEquals(testInts[i], ip.getInt()); |
| 214 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 215 | assertEquals(testStrings[i], ip.toString()); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | @Test |
| 220 | public void testOfByteArray() { |
| 221 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 222 | IPv4Address ip = IPv4Address.of(testAddresses[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 223 | assertEquals(testInts[i], ip.getInt()); |
| 224 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 225 | assertEquals(testStrings[i], ip.toString()); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | @Test |
Ronald Li | a748422 | 2014-07-03 17:14:12 -0700 | [diff] [blame] | 230 | public void testOfCidrMaskLength() { |
| 231 | for (int i = 0; i <= 32; i++) { |
| 232 | assertEquals(IPv4Address.ofCidrMaskLength(i).asCidrMaskLength(), i); |
| 233 | } |
| 234 | |
| 235 | assertEquals(IPv4Address.ofCidrMaskLength(0).getInt(), 0x0000_0000); |
| 236 | |
| 237 | assertEquals(IPv4Address.ofCidrMaskLength(1).getInt(), 0x8000_0000); |
| 238 | assertEquals(IPv4Address.ofCidrMaskLength(2).getInt(), 0xC000_0000); |
| 239 | assertEquals(IPv4Address.ofCidrMaskLength(3).getInt(), 0xE000_0000); |
| 240 | assertEquals(IPv4Address.ofCidrMaskLength(4).getInt(), 0xF000_0000); |
| 241 | assertEquals(IPv4Address.ofCidrMaskLength(5).getInt(), 0xF800_0000); |
| 242 | assertEquals(IPv4Address.ofCidrMaskLength(6).getInt(), 0xFC00_0000); |
| 243 | assertEquals(IPv4Address.ofCidrMaskLength(7).getInt(), 0xFE00_0000); |
| 244 | assertEquals(IPv4Address.ofCidrMaskLength(8).getInt(), 0xFF00_0000); |
| 245 | |
| 246 | assertEquals(IPv4Address.ofCidrMaskLength(9).getInt(), 0xFF80_0000); |
| 247 | assertEquals(IPv4Address.ofCidrMaskLength(10).getInt(), 0xFFC0_0000); |
| 248 | assertEquals(IPv4Address.ofCidrMaskLength(11).getInt(), 0xFFE0_0000); |
| 249 | assertEquals(IPv4Address.ofCidrMaskLength(12).getInt(), 0xFFF0_0000); |
| 250 | assertEquals(IPv4Address.ofCidrMaskLength(13).getInt(), 0xFFF8_0000); |
| 251 | assertEquals(IPv4Address.ofCidrMaskLength(14).getInt(), 0xFFFC_0000); |
| 252 | assertEquals(IPv4Address.ofCidrMaskLength(15).getInt(), 0xFFFE_0000); |
| 253 | assertEquals(IPv4Address.ofCidrMaskLength(16).getInt(), 0xFFFF_0000); |
| 254 | |
| 255 | assertEquals(IPv4Address.ofCidrMaskLength(17).getInt(), 0xFFFF_8000); |
| 256 | assertEquals(IPv4Address.ofCidrMaskLength(18).getInt(), 0xFFFF_C000); |
| 257 | assertEquals(IPv4Address.ofCidrMaskLength(19).getInt(), 0xFFFF_E000); |
| 258 | assertEquals(IPv4Address.ofCidrMaskLength(20).getInt(), 0xFFFF_F000); |
| 259 | assertEquals(IPv4Address.ofCidrMaskLength(21).getInt(), 0xFFFF_F800); |
| 260 | assertEquals(IPv4Address.ofCidrMaskLength(22).getInt(), 0xFFFF_FC00); |
| 261 | assertEquals(IPv4Address.ofCidrMaskLength(23).getInt(), 0xFFFF_FE00); |
| 262 | assertEquals(IPv4Address.ofCidrMaskLength(24).getInt(), 0xFFFF_FF00); |
| 263 | |
| 264 | assertEquals(IPv4Address.ofCidrMaskLength(25).getInt(), 0xFFFF_FF80); |
| 265 | assertEquals(IPv4Address.ofCidrMaskLength(26).getInt(), 0xFFFF_FFC0); |
| 266 | assertEquals(IPv4Address.ofCidrMaskLength(27).getInt(), 0xFFFF_FFE0); |
| 267 | assertEquals(IPv4Address.ofCidrMaskLength(28).getInt(), 0xFFFF_FFF0); |
| 268 | assertEquals(IPv4Address.ofCidrMaskLength(29).getInt(), 0xFFFF_FFF8); |
| 269 | assertEquals(IPv4Address.ofCidrMaskLength(30).getInt(), 0xFFFF_FFFC); |
| 270 | assertEquals(IPv4Address.ofCidrMaskLength(31).getInt(), 0xFFFF_FFFE); |
| 271 | assertEquals(IPv4Address.ofCidrMaskLength(32).getInt(), 0xFFFF_FFFF); |
| 272 | } |
| 273 | |
| 274 | @Test |
Ronald Li | 5d0ca65 | 2014-07-05 02:14:11 -0700 | [diff] [blame] | 275 | public void testWithMask() throws Exception { |
| 276 | // Sanity tests for the withMask*() syntactic sugars |
| 277 | |
| 278 | IPv4Address original = IPv4Address.of("192.168.1.101"); |
| 279 | IPv4Address expectedValue = IPv4Address.of("192.168.1.0"); |
| 280 | IPv4Address expectedMask = IPv4Address.of("255.255.255.0"); |
| 281 | |
| 282 | IPv4AddressWithMask v; |
| 283 | |
Ronald Li | ca08d5f | 2014-07-05 02:43:18 -0700 | [diff] [blame] | 284 | v = original.withMask(expectedMask); |
| 285 | assertEquals(v.getValue(), expectedValue); |
| 286 | assertEquals(v.getMask(), expectedMask); |
| 287 | |
Ronald Li | 5d0ca65 | 2014-07-05 02:14:11 -0700 | [diff] [blame] | 288 | v = original.withMask(new byte[] {-1, -1, -1, 0}); |
| 289 | assertEquals(v.getValue(), expectedValue); |
| 290 | assertEquals(v.getMask(), expectedMask); |
| 291 | |
| 292 | v = original.withMask(0xFFFF_FF00); |
| 293 | assertEquals(v.getValue(), expectedValue); |
| 294 | assertEquals(v.getMask(), expectedMask); |
| 295 | |
| 296 | v = original.withMask("255.255.255.0"); |
| 297 | assertEquals(v.getValue(), expectedValue); |
| 298 | assertEquals(v.getMask(), expectedMask); |
| 299 | |
| 300 | Inet4Address i4a = (Inet4Address) InetAddress.getByName("255.255.255.0"); |
| 301 | v = original.withMask(i4a); |
| 302 | assertEquals(v.getValue(), expectedValue); |
| 303 | assertEquals(v.getMask(), expectedMask); |
| 304 | |
| 305 | v = original.withMaskOfLength(24); |
| 306 | assertEquals(v.getValue(), expectedValue); |
| 307 | assertEquals(v.getMask(), expectedMask); |
| 308 | } |
| 309 | |
| 310 | @Test |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 311 | public void testReadFrom() throws OFParseError { |
| 312 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 313 | IPv4Address ip = IPv4Address.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i])); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 314 | assertEquals(testInts[i], ip.getInt()); |
| 315 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 316 | assertEquals(testStrings[i], ip.toString()); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | |
| 321 | @Test |
| 322 | public void testInvalidIPs() throws OFParseError { |
| 323 | for(String invalid : invalidIPs) { |
| 324 | try { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 325 | IPv4Address.of(invalid); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 326 | fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException"); |
| 327 | } catch(IllegalArgumentException e) { |
| 328 | // ok |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | @Test |
| 334 | public void testOfMasked() throws OFParseError { |
| 335 | for (int i = 0; i < ipsWithMask.length; i++) { |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 336 | IPv4AddressWithMask value = IPv4AddressWithMask.of(ipsWithMask[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 337 | if (!hasMask[i]) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 338 | IPv4Address ip = value.getValue(); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 339 | assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 340 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 341 | IPv4Address mask = value.getMask(); |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 342 | if (ipsWithMaskLengths[i] == -1) { |
| 343 | assertFalse(mask.isCidrMask()); |
| 344 | try { |
| 345 | mask.asCidrMaskLength(); |
| 346 | fail("Expected IllegalStateException not thrown"); |
| 347 | } catch(IllegalStateException e) { |
| 348 | //expected |
| 349 | } |
| 350 | } else { |
| 351 | assertTrue(mask.isCidrMask()); |
| 352 | assertEquals(ipsWithMaskLengths[i], mask.asCidrMaskLength()); |
| 353 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 354 | assertArrayEquals(ipsWithMaskValues[i][1], mask.getBytes()); |
| 355 | byte[] ipBytes = new byte[4]; |
| 356 | System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4); |
| 357 | assertEquals(ipBytes.length, value.getValue().getBytes().length); |
| 358 | for (int j = 0; j < ipBytes.length; j++) { |
| 359 | ipBytes[j] &= ipsWithMaskValues[i][1][j]; |
| 360 | } |
| 361 | |
| 362 | assertArrayEquals(ipBytes, value.getValue().getBytes()); |
| 363 | assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value), |
| 364 | value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1])); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | @Test |
| 369 | public void testOfMaskedInvalid() throws Exception { |
| 370 | for(String invalid : invalidIpsWithMask) { |
| 371 | try { |
| 372 | IPv4Address.of(invalid); |
| 373 | fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException"); |
| 374 | } catch(IllegalArgumentException e) { |
| 375 | // ok |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | @Test |
| 381 | public void testSuperclass() throws Exception { |
| 382 | for(String ipString: testStrings) { |
| 383 | IPAddress<?> superIp = IPAddress.of(ipString); |
| 384 | assertEquals(IPVersion.IPv4, superIp.getIpVersion()); |
| 385 | assertEquals(IPv4Address.of(ipString), superIp); |
| 386 | } |
| 387 | |
| 388 | for(String ipMaskedString: ipsWithMask) { |
| 389 | IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString); |
| 390 | assertEquals(IPVersion.IPv4, superIp.getIpVersion()); |
| 391 | assertEquals(IPv4AddressWithMask.of(ipMaskedString), superIp); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 394 | |
| 395 | @Test |
| 396 | public void testOfExceptions() { |
| 397 | // We check if the message of a caught NPE is set to a useful message |
| 398 | // as a hacky way of verifying that we got an NPE thrown by use rather |
| 399 | // than one the JVM created for a null access. |
| 400 | try { |
| 401 | String s = null; |
| 402 | IPv4Address.of(s); |
| 403 | fail("Should have thrown NullPointerException"); |
| 404 | } catch (NullPointerException e) { |
| 405 | assertNotNull(e.getMessage()); |
| 406 | } |
| 407 | try { |
| 408 | byte[] b = null; |
| 409 | IPv4Address.of(b); |
| 410 | fail("Should have thrown NullPointerException"); |
| 411 | } catch (NullPointerException e) { |
| 412 | assertNotNull(e.getMessage()); |
| 413 | } |
| 414 | try { |
| 415 | byte[] b = new byte[3]; |
| 416 | IPv4Address.of(b); |
| 417 | fail("Should have thrown IllegalArgumentException"); |
| 418 | } catch (IllegalArgumentException e) { |
| 419 | // expected |
| 420 | } |
| 421 | try { |
| 422 | byte[] b = new byte[5]; |
| 423 | IPv4Address.of(b); |
| 424 | fail("Should have thrown IllegalArgumentException"); |
| 425 | } catch (IllegalArgumentException e) { |
| 426 | // expected |
| 427 | } |
| 428 | try { |
| 429 | IPv4AddressWithMask.of(null); |
| 430 | fail("Should have thrown NullPointerException"); |
| 431 | } catch (NullPointerException e) { |
| 432 | assertNotNull(e.getMessage()); |
| 433 | } |
| 434 | try { |
| 435 | IPv4AddressWithMask.of(IPv4Address.of("1.2.3.4"), null); |
| 436 | fail("Should have thrown NullPointerException"); |
| 437 | } catch (NullPointerException e) { |
| 438 | assertNotNull(e.getMessage()); |
| 439 | } |
| 440 | try { |
| 441 | IPv4AddressWithMask.of(null, IPv4Address.of("255.0.0.0")); |
| 442 | fail("Should have thrown NullPointerException"); |
| 443 | } catch (NullPointerException e) { |
| 444 | assertNotNull(e.getMessage()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 445 | } |
Aditya Vaja | b21043d | 2014-03-11 13:34:06 -0700 | [diff] [blame] | 446 | try { |
| 447 | IPv4AddressWithMask.of(IPv4Address.of("10.10.10.0"), |
| 448 | IPv4Address.of("255.0.255.0")) |
| 449 | .getSubnetBroadcastAddress(); |
| 450 | fail("Should have thrown IllegalArgumentException"); |
| 451 | } catch (IllegalArgumentException e) { |
| 452 | assertNotNull(e.getMessage()); |
| 453 | } |
Ronald Li | a748422 | 2014-07-03 17:14:12 -0700 | [diff] [blame] | 454 | try { |
| 455 | IPv4Address.ofCidrMaskLength(-1); |
| 456 | fail("Should have thrown IllegalArgumentException"); |
| 457 | } catch (IllegalArgumentException e) { |
| 458 | assertNotNull(e.getMessage()); |
| 459 | } |
| 460 | try { |
| 461 | IPv4Address.ofCidrMaskLength(33); |
| 462 | fail("Should have thrown IllegalArgumentException"); |
| 463 | } catch (IllegalArgumentException e) { |
| 464 | assertNotNull(e.getMessage()); |
| 465 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 466 | } |
| 467 | } |