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