blob: 6eb5743c9521d701a0a5d563949acff9d41f198b [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;
Andreas Wundsame04c86f2013-11-05 17:13:18 -08005import static org.junit.Assert.assertThat;
Yotam Harcholf3f11152013-09-05 16:47:16 -07006import static org.junit.Assert.fail;
7
8import java.net.Inet6Address;
9import java.net.InetAddress;
10import java.net.UnknownHostException;
11
Andreas Wundsame04c86f2013-11-05 17:13:18 -080012import org.hamcrest.CoreMatchers;
Yotam Harcholf3f11152013-09-05 16:47:16 -070013import org.jboss.netty.buffer.ChannelBuffers;
14import org.junit.Test;
15import org.projectfloodlight.openflow.exceptions.OFParseError;
16
Andreas Wundsame04c86f2013-11-05 17:13:18 -080017import com.google.common.io.BaseEncoding;
18
Yotam Harchola289d552013-09-16 10:10:40 -070019public class IPv6AddressTest {
Yotam Harcholf3f11152013-09-05 16:47:16 -070020
21 String[] testStrings = {
22 "::",
23 "::1",
24 "ffe0::",
25 "1:2:3:4:5:6:7:8"
26 };
27
Yotam Harcholf3f11152013-09-05 16:47:16 -070028
Andreas Wundsame04c86f2013-11-05 17:13:18 -080029 private final BaseEncoding hex = BaseEncoding.base16().omitPadding().lowerCase();
Yotam Harcholf3f11152013-09-05 16:47:16 -070030
Andreas Wundsame04c86f2013-11-05 17:13:18 -080031 private class WithMaskTaskCase {
32 final String input;
33 boolean hasMask;
34 byte[] expectedMask = hex.decode("ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff".replaceAll(" ", ""));
35
36 public WithMaskTaskCase(String input) {
37 super();
38 this.input = input;
39 }
40
Andreas Wundsame04c86f2013-11-05 17:13:18 -080041 public WithMaskTaskCase maskHex(String string) {
42 string = string.replaceAll(" ", "");
43 this.hasMask = true;
44 expectedMask = hex.decode(string);
45 return this;
46 }
47
48 }
49
50 WithMaskTaskCase[] withMasks = new WithMaskTaskCase[] {
51 new WithMaskTaskCase("1::1/80")
52 .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00"),
53
54 new WithMaskTaskCase("ffff:ffee:1::/ff00:ff00:ff00:ff00::")
55 .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00"),
56 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
57 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
58 new WithMaskTaskCase("1:2:3:4:5:6:7:8/128"),
59 new WithMaskTaskCase("::/0")
60 .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")
Yotam Harcholf3f11152013-09-05 16:47:16 -070061 };
62
63 @Test
64 public void testMasked() throws UnknownHostException {
Andreas Wundsame04c86f2013-11-05 17:13:18 -080065 for(WithMaskTaskCase w: withMasks) {
66 IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input);
67 if (!w.hasMask) {
Yotam Harchola289d552013-09-16 10:10:40 -070068 IPv6Address ip = value.getValue();
Andreas Wundsame04c86f2013-11-05 17:13:18 -080069 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
Yotam Harcholf3f11152013-09-05 16:47:16 -070070
71 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
Andreas Wundsame04c86f2013-11-05 17:13:18 -080072 assertEquals(w.input.split("/")[0], ip.toString());
73 } else {
74 InetAddress inetAddress = InetAddress.getByName(w.input.substring(0, w.input.indexOf('/')));
Yotam Harcholf3f11152013-09-05 16:47:16 -070075
76 byte[] address = inetAddress.getAddress();
77 assertEquals(address.length, value.getValue().getBytes().length);
78
79 for (int j = 0; j < address.length; j++) {
Andreas Wundsame04c86f2013-11-05 17:13:18 -080080 address[j] &= w.expectedMask[j];
Yotam Harcholf3f11152013-09-05 16:47:16 -070081 }
82
Andreas Wundsame04c86f2013-11-05 17:13:18 -080083 assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address));
84 assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask));
Yotam Harcholf3f11152013-09-05 16:47:16 -070085 }
86 }
87 }
88
89
90 @Test
91 public void testOfString() throws UnknownHostException {
92 for(int i=0; i < testStrings.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -070093 IPv6Address ip = IPv6Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -070094 InetAddress inetAddress = InetAddress.getByName(testStrings[i]);
95
96 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
97 assertEquals(testStrings[i], ip.toString());
98 }
99 }
100
101 @Test
102 public void testOfByteArray() throws UnknownHostException {
103 for(int i=0; i < testStrings.length; i++ ) {
104 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700105 IPv6Address ip = IPv6Address.of(bytes);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700106 assertEquals(testStrings[i], ip.toString());
107 assertArrayEquals(bytes, ip.getBytes());
108 }
109 }
110
111 @Test
112 public void testReadFrom() throws OFParseError, UnknownHostException {
113 for(int i=0; i < testStrings.length; i++ ) {
114 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700115 IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700116 assertEquals(testStrings[i], ip.toString());
117 assertArrayEquals(bytes, ip.getBytes());
118 }
119 }
120
121 String[] invalidIPs = {
122 "",
123 ":",
124 "1:2:3:4:5:6:7:8:9",
125 "1:2:3:4:5:6:7:8:",
126 "1:2:3:4:5:6:7:8g",
127 "1:2:3:",
128 "12345::",
129 "1::3::8",
130 "::3::"
131 };
132
133 @Test
134 public void testInvalidIPs() throws OFParseError {
135 for(String invalid : invalidIPs) {
136 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700137 IPv6Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700138 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
139 } catch(IllegalArgumentException e) {
140 // ok
141 }
142 }
143 }
144
145 @Test
146 public void testZeroCompression() throws OFParseError {
Yotam Harchola289d552013-09-16 10:10:40 -0700147 assertEquals("::", IPv6Address.of("::").toString(true, false));
148 assertEquals("0:0:0:0:0:0:0:0", IPv6Address.of("::").toString(false, false));
149 assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6Address.of("::").toString(false, true));
150 assertEquals("1::4:5:6:0:8", IPv6Address.of("1:0:0:4:5:6:0:8").toString(true, false));
151 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 -0700152 }
153}