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; |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 5 | import static org.junit.Assert.assertThat; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 6 | import static org.junit.Assert.fail; |
| 7 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 8 | import org.hamcrest.CoreMatchers; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 9 | import org.jboss.netty.buffer.ChannelBuffers; |
| 10 | import org.junit.Test; |
| 11 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 12 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 13 | public class IPv4AddressTest { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 14 | byte[][] testAddresses = new byte[][] { |
| 15 | {0x01, 0x02, 0x03, 0x04 }, |
| 16 | {127, 0, 0, 1}, |
| 17 | {(byte) 192, (byte) 168, 0, 100 }, |
| 18 | {(byte) 255, (byte) 255, (byte) 255, (byte) 255 } |
| 19 | }; |
| 20 | |
| 21 | String[] testStrings = { |
| 22 | "1.2.3.4", |
| 23 | "127.0.0.1", |
| 24 | "192.168.0.100", |
| 25 | "255.255.255.255" |
| 26 | }; |
| 27 | |
| 28 | int[] testInts = { |
| 29 | 0x01020304, |
| 30 | 0x7f000001, |
| 31 | (192 << 24) | (168 << 16) | 100, |
| 32 | 0xffffffff |
| 33 | }; |
| 34 | |
| 35 | String[] invalidIPs = { |
| 36 | "", |
| 37 | ".", |
| 38 | "1.2..3.4", |
| 39 | "1.2.3.4.", |
| 40 | "257.11.225.1", |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 41 | "256.11.225.1", |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 42 | "-1.2.3.4", |
| 43 | "1.2.3.4.5", |
| 44 | "1.x.3.4", |
| 45 | "1.2x.3.4" |
| 46 | }; |
| 47 | |
| 48 | String[] ipsWithMask = { |
| 49 | "1.2.3.4/24", |
| 50 | "192.168.130.140/255.255.192.0", |
| 51 | "127.0.0.1/8", |
| 52 | "8.8.8.8", |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 53 | "8.8.8.8/32", |
| 54 | "0.0.0.0/0", |
| 55 | "192.168.130.140/255.0.255.0", |
| 56 | "1.2.3.4/0.127.0.255" |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | boolean[] hasMask = { |
| 60 | true, |
| 61 | true, |
| 62 | true, |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 63 | false, |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 64 | false, |
| 65 | true, |
| 66 | true, |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 67 | true |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | byte[][][] ipsWithMaskValues = { |
| 71 | new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00 } }, |
| 72 | new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xC0, (byte)0x00 } }, |
| 73 | 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] | 74 | new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } }, |
| 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)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 } }, |
| 77 | new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0xFF, (byte)0x00 } }, |
| 78 | new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0x00, (byte)0x7F, (byte)0x00, (byte)0xFF } } |
| 79 | }; |
| 80 | |
| 81 | int[] ipsWithMaskLengths = { |
| 82 | 24, |
| 83 | 18, |
| 84 | 8, |
| 85 | 32, |
| 86 | 32, |
| 87 | 0, |
| 88 | -1, |
| 89 | -1 |
| 90 | }; |
| 91 | |
| 92 | String[] invalidIpsWithMask = { |
| 93 | "asdf", |
| 94 | "1.2.3.4/33", |
| 95 | "1.2.3.4/34", |
| 96 | "1.2.3.4/-1", |
| 97 | "1.2.3.4/256.0.0.0", |
| 98 | "1.256.3.4/255.255.0.0", |
| 99 | "1.2.3.4/255.255.0.0.0", |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | |
| 103 | @Test |
| 104 | public void testOfString() { |
| 105 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 106 | IPv4Address ip = IPv4Address.of(testStrings[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 107 | assertEquals(testInts[i], ip.getInt()); |
| 108 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 109 | assertEquals(testStrings[i], ip.toString()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void testOfByteArray() { |
| 115 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 116 | IPv4Address ip = IPv4Address.of(testAddresses[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 117 | assertEquals(testInts[i], ip.getInt()); |
| 118 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 119 | assertEquals(testStrings[i], ip.toString()); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testReadFrom() throws OFParseError { |
| 125 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 126 | IPv4Address ip = IPv4Address.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i])); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 127 | assertEquals(testInts[i], ip.getInt()); |
| 128 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 129 | assertEquals(testStrings[i], ip.toString()); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | @Test |
| 135 | public void testInvalidIPs() throws OFParseError { |
| 136 | for(String invalid : invalidIPs) { |
| 137 | try { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 138 | IPv4Address.of(invalid); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 139 | fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException"); |
| 140 | } catch(IllegalArgumentException e) { |
| 141 | // ok |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | @Test |
| 147 | public void testOfMasked() throws OFParseError { |
| 148 | for (int i = 0; i < ipsWithMask.length; i++) { |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 149 | IPv4AddressWithMask value = IPv4AddressWithMask.of(ipsWithMask[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 150 | if (!hasMask[i]) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 151 | IPv4Address ip = value.getValue(); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 152 | assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 153 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 154 | IPv4Address mask = value.getMask(); |
| 155 | assertEquals(ipsWithMaskLengths[i], value.getMask().asCidrMaskLength()); |
| 156 | assertArrayEquals(ipsWithMaskValues[i][1], mask.getBytes()); |
| 157 | byte[] ipBytes = new byte[4]; |
| 158 | System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4); |
| 159 | assertEquals(ipBytes.length, value.getValue().getBytes().length); |
| 160 | for (int j = 0; j < ipBytes.length; j++) { |
| 161 | ipBytes[j] &= ipsWithMaskValues[i][1][j]; |
| 162 | } |
| 163 | |
| 164 | assertArrayEquals(ipBytes, value.getValue().getBytes()); |
| 165 | assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value), |
| 166 | value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1])); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | @Test |
| 171 | public void testOfMaskedInvalid() throws Exception { |
| 172 | for(String invalid : invalidIpsWithMask) { |
| 173 | try { |
| 174 | IPv4Address.of(invalid); |
| 175 | fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException"); |
| 176 | } catch(IllegalArgumentException e) { |
| 177 | // ok |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | @Test |
| 183 | public void testSuperclass() throws Exception { |
| 184 | for(String ipString: testStrings) { |
| 185 | IPAddress<?> superIp = IPAddress.of(ipString); |
| 186 | assertEquals(IPVersion.IPv4, superIp.getIpVersion()); |
| 187 | assertEquals(IPv4Address.of(ipString), superIp); |
| 188 | } |
| 189 | |
| 190 | for(String ipMaskedString: ipsWithMask) { |
| 191 | IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString); |
| 192 | assertEquals(IPVersion.IPv4, superIp.getIpVersion()); |
| 193 | assertEquals(IPv4AddressWithMask.of(ipMaskedString), superIp); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |