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 | |
| 9 | import java.net.Inet6Address; |
| 10 | import java.net.InetAddress; |
| 11 | import java.net.UnknownHostException; |
| 12 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 13 | import org.hamcrest.CoreMatchers; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 14 | import org.jboss.netty.buffer.ChannelBuffers; |
| 15 | import org.junit.Test; |
| 16 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 17 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 18 | import com.google.common.io.BaseEncoding; |
| 19 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 20 | public class IPv6AddressTest { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 21 | |
| 22 | String[] testStrings = { |
| 23 | "::", |
| 24 | "::1", |
| 25 | "ffe0::", |
| 26 | "1:2:3:4:5:6:7:8" |
| 27 | }; |
| 28 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 29 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 30 | private final BaseEncoding hex = BaseEncoding.base16().omitPadding().lowerCase(); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 31 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 32 | private class WithMaskTaskCase { |
| 33 | final String input; |
| 34 | boolean hasMask; |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 35 | int expectedMaskLength = 128; |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 36 | byte[] expectedMask = hex.decode("ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff".replaceAll(" ", "")); |
| 37 | |
| 38 | public WithMaskTaskCase(String input) { |
| 39 | super(); |
| 40 | this.input = input; |
| 41 | } |
| 42 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 43 | public WithMaskTaskCase maskHex(String string) { |
| 44 | string = string.replaceAll(" ", ""); |
| 45 | this.hasMask = true; |
| 46 | expectedMask = hex.decode(string); |
| 47 | return this; |
| 48 | } |
| 49 | |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 50 | public WithMaskTaskCase expectedMaskLength(int expectedLength) { |
| 51 | this.expectedMaskLength = expectedLength; |
| 52 | return this; |
| 53 | } |
| 54 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | WithMaskTaskCase[] withMasks = new WithMaskTaskCase[] { |
| 58 | new WithMaskTaskCase("1::1/80") |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 59 | .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00") |
| 60 | .expectedMaskLength(80), |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 61 | |
| 62 | new WithMaskTaskCase("ffff:ffee:1::/ff00:ff00:ff00:ff00::") |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 63 | .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00") |
| 64 | .expectedMaskLength(-1), |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 65 | new WithMaskTaskCase("8:8:8:8:8:8:8:8"), |
| 66 | new WithMaskTaskCase("8:8:8:8:8:8:8:8"), |
| 67 | new WithMaskTaskCase("1:2:3:4:5:6:7:8/128"), |
| 68 | new WithMaskTaskCase("::/0") |
| 69 | .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00") |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 70 | .expectedMaskLength(0), |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | @Test |
| 74 | public void testMasked() throws UnknownHostException { |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 75 | for(WithMaskTaskCase w: withMasks) { |
| 76 | IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input); |
| 77 | if (!w.hasMask) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 78 | IPv6Address ip = value.getValue(); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 79 | InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 80 | |
| 81 | assertArrayEquals(ip.getBytes(), inetAddress.getAddress()); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 82 | assertEquals(w.input.split("/")[0], ip.toString()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 83 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 84 | InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]); |
| 85 | |
| 86 | assertEquals("Input " + w.input, w.expectedMaskLength, value.getMask().asCidrMaskLength()); |
| 87 | |
| 88 | byte[] address = inetAddress.getAddress(); |
| 89 | assertEquals(address.length, value.getValue().getBytes().length); |
| 90 | |
| 91 | for (int j = 0; j < address.length; j++) { |
| 92 | address[j] &= w.expectedMask[j]; |
| 93 | } |
| 94 | |
| 95 | assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address)); |
| 96 | assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask)); |
| 97 | } |
| 98 | for (int i = 0; i <= 128; i++) { |
| 99 | String ipString = String.format("8001:2::1/%d", i); |
| 100 | IPv6AddressWithMask value = IPv6AddressWithMask.of(ipString); |
| 101 | assertEquals("Input " + ipString, i, value.getMask().asCidrMaskLength()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | |
| 106 | @Test |
| 107 | public void testOfString() throws UnknownHostException { |
| 108 | for(int i=0; i < testStrings.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 109 | IPv6Address ip = IPv6Address.of(testStrings[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 110 | InetAddress inetAddress = InetAddress.getByName(testStrings[i]); |
| 111 | |
| 112 | assertArrayEquals(ip.getBytes(), inetAddress.getAddress()); |
| 113 | assertEquals(testStrings[i], ip.toString()); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void testOfByteArray() throws UnknownHostException { |
| 119 | for(int i=0; i < testStrings.length; i++ ) { |
| 120 | byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress(); |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 121 | IPv6Address ip = IPv6Address.of(bytes); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 122 | assertEquals(testStrings[i], ip.toString()); |
| 123 | assertArrayEquals(bytes, ip.getBytes()); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void testReadFrom() throws OFParseError, UnknownHostException { |
| 129 | for(int i=0; i < testStrings.length; i++ ) { |
| 130 | byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress(); |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 131 | IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 132 | assertEquals(testStrings[i], ip.toString()); |
| 133 | assertArrayEquals(bytes, ip.getBytes()); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | String[] invalidIPs = { |
| 138 | "", |
| 139 | ":", |
| 140 | "1:2:3:4:5:6:7:8:9", |
| 141 | "1:2:3:4:5:6:7:8:", |
| 142 | "1:2:3:4:5:6:7:8g", |
| 143 | "1:2:3:", |
| 144 | "12345::", |
| 145 | "1::3::8", |
| 146 | "::3::" |
| 147 | }; |
| 148 | |
| 149 | @Test |
| 150 | public void testInvalidIPs() throws OFParseError { |
| 151 | for(String invalid : invalidIPs) { |
| 152 | try { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 153 | IPv6Address.of(invalid); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 154 | fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException"); |
| 155 | } catch(IllegalArgumentException e) { |
| 156 | // ok |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | @Test |
| 162 | public void testZeroCompression() throws OFParseError { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 163 | assertEquals("::", IPv6Address.of("::").toString(true, false)); |
| 164 | assertEquals("0:0:0:0:0:0:0:0", IPv6Address.of("::").toString(false, false)); |
| 165 | assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6Address.of("::").toString(false, true)); |
| 166 | assertEquals("1::4:5:6:0:8", IPv6Address.of("1:0:0:4:5:6:0:8").toString(true, false)); |
| 167 | assertEquals("1:0:0:4::8", IPv6Address.of("1:0:0:4:0:0:0:8").toString(true, false)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 168 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 169 | |
| 170 | @Test |
| 171 | public void testSuperclass() throws Exception { |
| 172 | for(String ipString: testStrings) { |
| 173 | IPAddress<?> superIp = IPAddress.of(ipString); |
| 174 | assertEquals(IPVersion.IPv6, superIp.getIpVersion()); |
| 175 | assertEquals(IPv6Address.of(ipString), superIp); |
| 176 | } |
| 177 | |
| 178 | for(WithMaskTaskCase w: withMasks) { |
| 179 | String ipMaskedString = w.input; |
| 180 | IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString); |
| 181 | assertEquals(IPVersion.IPv6, superIp.getIpVersion()); |
| 182 | assertEquals(IPv6AddressWithMask.of(ipMaskedString), superIp); |
| 183 | } |
| 184 | } |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame^] | 185 | |
| 186 | @Test |
| 187 | public void testOfExceptions() throws Exception { |
| 188 | try { |
| 189 | IPv6AddressWithMask.of(null); |
| 190 | fail("Should have thrown NullPointerException"); |
| 191 | } catch (NullPointerException e) { |
| 192 | assertNotNull(e.getMessage()); |
| 193 | } |
| 194 | try { |
| 195 | String s = null; |
| 196 | IPv6Address.of(s); |
| 197 | fail("Should have thrown NullPointerException"); |
| 198 | } catch (NullPointerException e) { |
| 199 | assertNotNull(e.getMessage()); |
| 200 | } |
| 201 | try { |
| 202 | byte[] b = null; |
| 203 | IPv6Address.of(b); |
| 204 | fail("Should have thrown NullPointerException"); |
| 205 | } catch (NullPointerException e) { |
| 206 | assertNotNull(e.getMessage()); |
| 207 | } |
| 208 | try { |
| 209 | byte[] b = new byte[7]; |
| 210 | IPv6Address.of(b); |
| 211 | fail("Should have thrown IllegalArgumentException"); |
| 212 | } catch (IllegalArgumentException e) { |
| 213 | // expected |
| 214 | } |
| 215 | try { |
| 216 | byte[] b = new byte[9]; |
| 217 | IPv6Address.of(b); |
| 218 | fail("Should have thrown IllegalArgumentException"); |
| 219 | } catch (IllegalArgumentException e) { |
| 220 | // expected |
| 221 | } |
| 222 | try { |
| 223 | IPv6AddressWithMask.of(IPv6Address.of("1::"), null); |
| 224 | fail("Should have thrown NullPointerException"); |
| 225 | } catch (NullPointerException e) { |
| 226 | assertNotNull(e.getMessage()); |
| 227 | } |
| 228 | try { |
| 229 | IPv6AddressWithMask.of(null, IPv6Address.of("255::")); |
| 230 | fail("Should have thrown NullPointerException"); |
| 231 | } catch (NullPointerException e) { |
| 232 | assertNotNull(e.getMessage()); |
| 233 | } |
| 234 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 235 | } |