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