blob: 706fb259dea3b4d7ec100bb2988f5776c725341d [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import static org.junit.Assert.assertArrayEquals;
4import static org.junit.Assert.assertEquals;
Gregor Maier1ff55972013-12-11 02:22:56 -08005import static org.junit.Assert.assertNotNull;
Andreas Wundsame04c86f2013-11-05 17:13:18 -08006import static org.junit.Assert.assertThat;
Yotam Harcholf3f11152013-09-05 16:47:16 -07007import static org.junit.Assert.fail;
8
9import java.net.Inet6Address;
10import java.net.InetAddress;
11import java.net.UnknownHostException;
12
Andreas Wundsame04c86f2013-11-05 17:13:18 -080013import org.hamcrest.CoreMatchers;
Yotam Harcholf3f11152013-09-05 16:47:16 -070014import org.jboss.netty.buffer.ChannelBuffers;
15import org.junit.Test;
16import org.projectfloodlight.openflow.exceptions.OFParseError;
17
Andreas Wundsame04c86f2013-11-05 17:13:18 -080018import com.google.common.io.BaseEncoding;
19
Yotam Harchola289d552013-09-16 10:10:40 -070020public class IPv6AddressTest {
Yotam Harcholf3f11152013-09-05 16:47:16 -070021
22 String[] testStrings = {
23 "::",
24 "::1",
25 "ffe0::",
26 "1:2:3:4:5:6:7:8"
27 };
28
Yotam Harcholf3f11152013-09-05 16:47:16 -070029
Andreas Wundsame04c86f2013-11-05 17:13:18 -080030 private final BaseEncoding hex = BaseEncoding.base16().omitPadding().lowerCase();
Yotam Harcholf3f11152013-09-05 16:47:16 -070031
Andreas Wundsame04c86f2013-11-05 17:13:18 -080032 private class WithMaskTaskCase {
33 final String input;
34 boolean hasMask;
Gregor Maier7f987e62013-12-10 19:34:18 -080035 int expectedMaskLength = 128;
Andreas Wundsame04c86f2013-11-05 17:13:18 -080036 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 Wundsame04c86f2013-11-05 17:13:18 -080043 public WithMaskTaskCase maskHex(String string) {
44 string = string.replaceAll(" ", "");
45 this.hasMask = true;
46 expectedMask = hex.decode(string);
47 return this;
48 }
49
Gregor Maier7f987e62013-12-10 19:34:18 -080050 public WithMaskTaskCase expectedMaskLength(int expectedLength) {
51 this.expectedMaskLength = expectedLength;
52 return this;
53 }
54
Andreas Wundsame04c86f2013-11-05 17:13:18 -080055 }
56
57 WithMaskTaskCase[] withMasks = new WithMaskTaskCase[] {
58 new WithMaskTaskCase("1::1/80")
Gregor Maier7f987e62013-12-10 19:34:18 -080059 .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00")
60 .expectedMaskLength(80),
Andreas Wundsame04c86f2013-11-05 17:13:18 -080061
62 new WithMaskTaskCase("ffff:ffee:1::/ff00:ff00:ff00:ff00::")
Gregor Maier7f987e62013-12-10 19:34:18 -080063 .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00")
64 .expectedMaskLength(-1),
Andreas Wundsame04c86f2013-11-05 17:13:18 -080065 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 Maier7f987e62013-12-10 19:34:18 -080070 .expectedMaskLength(0),
Yotam Harcholf3f11152013-09-05 16:47:16 -070071 };
72
73 @Test
74 public void testMasked() throws UnknownHostException {
Andreas Wundsame04c86f2013-11-05 17:13:18 -080075 for(WithMaskTaskCase w: withMasks) {
76 IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input);
77 if (!w.hasMask) {
Yotam Harchola289d552013-09-16 10:10:40 -070078 IPv6Address ip = value.getValue();
Andreas Wundsame04c86f2013-11-05 17:13:18 -080079 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
Yotam Harcholf3f11152013-09-05 16:47:16 -070080
81 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
Andreas Wundsame04c86f2013-11-05 17:13:18 -080082 assertEquals(w.input.split("/")[0], ip.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070083 }
Gregor Maier7f987e62013-12-10 19:34:18 -080084 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 Harcholf3f11152013-09-05 16:47:16 -0700102 }
103 }
104
105
106 @Test
107 public void testOfString() throws UnknownHostException {
108 for(int i=0; i < testStrings.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700109 IPv6Address ip = IPv6Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700110 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 Harchola289d552013-09-16 10:10:40 -0700121 IPv6Address ip = IPv6Address.of(bytes);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700122 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 Harchola289d552013-09-16 10:10:40 -0700131 IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700132 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 Harchola289d552013-09-16 10:10:40 -0700153 IPv6Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700154 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 Harchola289d552013-09-16 10:10:40 -0700163 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 Harcholf3f11152013-09-05 16:47:16 -0700168 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800169
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 Maier1ff55972013-12-11 02:22:56 -0800185
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 Harcholf3f11152013-09-05 16:47:16 -0700235}