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 | bbf0194 | 2014-07-07 17:00:13 -0700 | [diff] [blame] | 284 | v = original.withMask(IPv4Address.of(new byte[] {-1, -1, -1, 0})); |
Ronald Li | ca08d5f | 2014-07-05 02:43:18 -0700 | [diff] [blame] | 285 | assertEquals(v.getValue(), expectedValue); |
| 286 | assertEquals(v.getMask(), expectedMask); |
| 287 | |
Ronald Li | bbf0194 | 2014-07-07 17:00:13 -0700 | [diff] [blame] | 288 | v = original.withMask(IPv4Address.of(0xFFFF_FF00)); |
Ronald Li | 5d0ca65 | 2014-07-05 02:14:11 -0700 | [diff] [blame] | 289 | assertEquals(v.getValue(), expectedValue); |
| 290 | assertEquals(v.getMask(), expectedMask); |
| 291 | |
Ronald Li | bbf0194 | 2014-07-07 17:00:13 -0700 | [diff] [blame] | 292 | v = original.withMask(IPv4Address.of("255.255.255.0")); |
Ronald Li | 5d0ca65 | 2014-07-05 02:14:11 -0700 | [diff] [blame] | 293 | assertEquals(v.getValue(), expectedValue); |
| 294 | assertEquals(v.getMask(), expectedMask); |
| 295 | |
| 296 | Inet4Address i4a = (Inet4Address) InetAddress.getByName("255.255.255.0"); |
Ronald Li | bbf0194 | 2014-07-07 17:00:13 -0700 | [diff] [blame] | 297 | v = original.withMask(IPv4Address.of(i4a)); |
Ronald Li | 5d0ca65 | 2014-07-05 02:14:11 -0700 | [diff] [blame] | 298 | assertEquals(v.getValue(), expectedValue); |
| 299 | assertEquals(v.getMask(), expectedMask); |
| 300 | |
| 301 | v = original.withMaskOfLength(24); |
| 302 | assertEquals(v.getValue(), expectedValue); |
| 303 | assertEquals(v.getMask(), expectedMask); |
| 304 | } |
| 305 | |
| 306 | @Test |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 307 | public void testReadFrom() throws OFParseError { |
| 308 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 309 | IPv4Address ip = IPv4Address.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i])); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 310 | assertEquals(testInts[i], ip.getInt()); |
| 311 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 312 | assertEquals(testStrings[i], ip.toString()); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | |
| 317 | @Test |
| 318 | public void testInvalidIPs() throws OFParseError { |
| 319 | for(String invalid : invalidIPs) { |
| 320 | try { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 321 | IPv4Address.of(invalid); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 322 | fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException"); |
| 323 | } catch(IllegalArgumentException e) { |
| 324 | // ok |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | @Test |
| 330 | public void testOfMasked() throws OFParseError { |
| 331 | for (int i = 0; i < ipsWithMask.length; i++) { |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 332 | IPv4AddressWithMask value = IPv4AddressWithMask.of(ipsWithMask[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 333 | if (!hasMask[i]) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 334 | IPv4Address ip = value.getValue(); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 335 | assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 336 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 337 | IPv4Address mask = value.getMask(); |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 338 | if (ipsWithMaskLengths[i] == -1) { |
| 339 | assertFalse(mask.isCidrMask()); |
| 340 | try { |
| 341 | mask.asCidrMaskLength(); |
| 342 | fail("Expected IllegalStateException not thrown"); |
| 343 | } catch(IllegalStateException e) { |
| 344 | //expected |
| 345 | } |
| 346 | } else { |
| 347 | assertTrue(mask.isCidrMask()); |
| 348 | assertEquals(ipsWithMaskLengths[i], mask.asCidrMaskLength()); |
| 349 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 350 | assertArrayEquals(ipsWithMaskValues[i][1], mask.getBytes()); |
| 351 | byte[] ipBytes = new byte[4]; |
| 352 | System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4); |
| 353 | assertEquals(ipBytes.length, value.getValue().getBytes().length); |
| 354 | for (int j = 0; j < ipBytes.length; j++) { |
| 355 | ipBytes[j] &= ipsWithMaskValues[i][1][j]; |
| 356 | } |
| 357 | |
| 358 | assertArrayEquals(ipBytes, value.getValue().getBytes()); |
| 359 | assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value), |
| 360 | value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1])); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | @Test |
| 365 | public void testOfMaskedInvalid() throws Exception { |
| 366 | for(String invalid : invalidIpsWithMask) { |
| 367 | try { |
| 368 | IPv4Address.of(invalid); |
| 369 | fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException"); |
| 370 | } catch(IllegalArgumentException e) { |
| 371 | // ok |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | @Test |
| 377 | public void testSuperclass() throws Exception { |
| 378 | for(String ipString: testStrings) { |
| 379 | IPAddress<?> superIp = IPAddress.of(ipString); |
| 380 | assertEquals(IPVersion.IPv4, superIp.getIpVersion()); |
| 381 | assertEquals(IPv4Address.of(ipString), superIp); |
| 382 | } |
| 383 | |
| 384 | for(String ipMaskedString: ipsWithMask) { |
| 385 | IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString); |
| 386 | assertEquals(IPVersion.IPv4, superIp.getIpVersion()); |
| 387 | assertEquals(IPv4AddressWithMask.of(ipMaskedString), superIp); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 390 | |
| 391 | @Test |
| 392 | public void testOfExceptions() { |
| 393 | // We check if the message of a caught NPE is set to a useful message |
| 394 | // as a hacky way of verifying that we got an NPE thrown by use rather |
| 395 | // than one the JVM created for a null access. |
| 396 | try { |
| 397 | String s = null; |
| 398 | IPv4Address.of(s); |
| 399 | fail("Should have thrown NullPointerException"); |
| 400 | } catch (NullPointerException e) { |
| 401 | assertNotNull(e.getMessage()); |
| 402 | } |
| 403 | try { |
| 404 | byte[] b = null; |
| 405 | IPv4Address.of(b); |
| 406 | fail("Should have thrown NullPointerException"); |
| 407 | } catch (NullPointerException e) { |
| 408 | assertNotNull(e.getMessage()); |
| 409 | } |
| 410 | try { |
| 411 | byte[] b = new byte[3]; |
| 412 | IPv4Address.of(b); |
| 413 | fail("Should have thrown IllegalArgumentException"); |
| 414 | } catch (IllegalArgumentException e) { |
| 415 | // expected |
| 416 | } |
| 417 | try { |
| 418 | byte[] b = new byte[5]; |
| 419 | IPv4Address.of(b); |
| 420 | fail("Should have thrown IllegalArgumentException"); |
| 421 | } catch (IllegalArgumentException e) { |
| 422 | // expected |
| 423 | } |
| 424 | try { |
| 425 | IPv4AddressWithMask.of(null); |
| 426 | fail("Should have thrown NullPointerException"); |
| 427 | } catch (NullPointerException e) { |
| 428 | assertNotNull(e.getMessage()); |
| 429 | } |
| 430 | try { |
| 431 | IPv4AddressWithMask.of(IPv4Address.of("1.2.3.4"), null); |
| 432 | fail("Should have thrown NullPointerException"); |
| 433 | } catch (NullPointerException e) { |
| 434 | assertNotNull(e.getMessage()); |
| 435 | } |
| 436 | try { |
| 437 | IPv4AddressWithMask.of(null, IPv4Address.of("255.0.0.0")); |
| 438 | fail("Should have thrown NullPointerException"); |
| 439 | } catch (NullPointerException e) { |
| 440 | assertNotNull(e.getMessage()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 441 | } |
Aditya Vaja | b21043d | 2014-03-11 13:34:06 -0700 | [diff] [blame] | 442 | try { |
| 443 | IPv4AddressWithMask.of(IPv4Address.of("10.10.10.0"), |
| 444 | IPv4Address.of("255.0.255.0")) |
| 445 | .getSubnetBroadcastAddress(); |
| 446 | fail("Should have thrown IllegalArgumentException"); |
| 447 | } catch (IllegalArgumentException e) { |
| 448 | assertNotNull(e.getMessage()); |
| 449 | } |
Ronald Li | a748422 | 2014-07-03 17:14:12 -0700 | [diff] [blame] | 450 | try { |
| 451 | IPv4Address.ofCidrMaskLength(-1); |
| 452 | fail("Should have thrown IllegalArgumentException"); |
| 453 | } catch (IllegalArgumentException e) { |
| 454 | assertNotNull(e.getMessage()); |
| 455 | } |
| 456 | try { |
| 457 | IPv4Address.ofCidrMaskLength(33); |
| 458 | fail("Should have thrown IllegalArgumentException"); |
| 459 | } catch (IllegalArgumentException e) { |
| 460 | assertNotNull(e.getMessage()); |
| 461 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 462 | } |
| 463 | } |