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