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 | 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", |
| 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", |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 53 | "0.0.0.0/0" |
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, |
| 61 | true |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | byte[][][] ipsWithMaskValues = { |
| 65 | new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00 } }, |
| 66 | new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xC0, (byte)0x00 } }, |
| 67 | new byte[][] { new byte[] { (byte)0x7F, (byte)0x00, (byte)0x00, (byte)0x01 }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0x00 } }, |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 68 | new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, null }, |
| 69 | new byte[][] { new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 } } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
Andreas Wundsam | 12cea88 | 2013-12-15 15:05:50 -0800 | [diff] [blame^] | 72 | @Test |
| 73 | public void testMaskedMatchesCidr() { |
| 74 | IPv4AddressWithMask slash28 = IPv4AddressWithMask.of("10.0.42.16/28"); |
| 75 | |
| 76 | String[] notContained = {"0.0.0.0", "11.0.42.16", "10.0.41.1", "10.0.42.0", "10.0.42.15", |
| 77 | "10.0.42.32", "255.255.255.255" }; |
| 78 | |
| 79 | for(String n: notContained) { |
| 80 | assertThat(String.format("slash 28 %s should not contain address %s", |
| 81 | slash28, n), |
| 82 | slash28.matches(IPv4Address.of(n)), equalTo(false)); |
| 83 | } |
| 84 | for(int i=16; i < 32; i++) { |
| 85 | IPv4Address c = IPv4Address.of(String.format("10.0.42.%d", i)); |
| 86 | assertThat(String.format("slash 28 %s should contain address %s", |
| 87 | slash28, c), |
| 88 | slash28.matches(c), equalTo(true)); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testMaskedMatchesArbitrary() { |
| 94 | // irregular octect on the 3rd bitmask requires '1'bit to be set |
| 95 | // 4 bit unset, all others arbitrary |
| 96 | IPv4AddressWithMask slash28 = IPv4AddressWithMask.of("1.2.1.4/255.255.5.255"); |
| 97 | |
| 98 | String[] notContained = {"0.0.0.0", "1.2.3.5", "1.2.3.3", |
| 99 | "1.2.0.4", "1.2.2.4", "1.2.4.4", "1.2.5.4", "1.2.6.4", "1.2.7.4", |
| 100 | "1.2.8.4", "1.2.12.4", "1.2.13.4" |
| 101 | }; |
| 102 | String[] contained = {"1.2.1.4", "1.2.3.4", "1.2.9.4", "1.2.11.4", "1.2.251.4", |
| 103 | }; |
| 104 | |
| 105 | for(String n: notContained) { |
| 106 | assertThat(String.format("slash 28 %s should not contain address %s", |
| 107 | slash28, n), |
| 108 | slash28.matches(IPv4Address.of(n)), equalTo(false)); |
| 109 | } |
| 110 | for(String c: contained) { |
| 111 | IPv4Address addr = IPv4Address.of(c); |
| 112 | assertThat(String.format("slash 28 %s should contain address %s", |
| 113 | slash28, addr), |
| 114 | slash28.matches(addr), equalTo(true)); |
| 115 | } |
| 116 | |
| 117 | } |
| 118 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 119 | |
| 120 | @Test |
| 121 | public void testOfString() { |
| 122 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 123 | IPv4Address ip = IPv4Address.of(testStrings[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 124 | assertEquals(testInts[i], ip.getInt()); |
| 125 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 126 | assertEquals(testStrings[i], ip.toString()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void testOfByteArray() { |
| 132 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 133 | IPv4Address ip = IPv4Address.of(testAddresses[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 134 | assertEquals(testInts[i], ip.getInt()); |
| 135 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 136 | assertEquals(testStrings[i], ip.toString()); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void testReadFrom() throws OFParseError { |
| 142 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 143 | IPv4Address ip = IPv4Address.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i])); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 144 | assertEquals(testInts[i], ip.getInt()); |
| 145 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 146 | assertEquals(testStrings[i], ip.toString()); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | |
| 151 | @Test |
| 152 | public void testInvalidIPs() throws OFParseError { |
| 153 | for(String invalid : invalidIPs) { |
| 154 | try { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 155 | IPv4Address.of(invalid); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 156 | fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException"); |
| 157 | } catch(IllegalArgumentException e) { |
| 158 | // ok |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | @Test |
| 164 | public void testOfMasked() throws OFParseError { |
| 165 | for (int i = 0; i < ipsWithMask.length; i++) { |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 166 | IPv4AddressWithMask value = IPv4AddressWithMask.of(ipsWithMask[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 167 | if (!hasMask[i]) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 168 | IPv4Address ip = value.getValue(); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 169 | assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes()); |
| 170 | } else if (hasMask[i]) { |
| 171 | byte[] ipBytes = new byte[4]; |
| 172 | System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4); |
| 173 | assertEquals(ipBytes.length, value.getValue().getBytes().length); |
| 174 | for (int j = 0; j < ipBytes.length; j++) { |
| 175 | ipBytes[j] &= ipsWithMaskValues[i][1][j]; |
| 176 | } |
| 177 | |
| 178 | assertArrayEquals(ipBytes, value.getValue().getBytes()); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 179 | assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value), |
| 180 | value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1])); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |