blob: 672a0e1d74b98240519cf11f4f066e828379a228 [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
41 public WithMaskTaskCase hasMask() {
42 this.hasMask = true;
43 return this;
44 }
45
46 public WithMaskTaskCase maskLength(int l) {
47 this.hasMask = l < 128;
48 for(int j=0; j < 8; j++) {
49 int pos = j * 8;
50 int index = Math.max(0, Math.min(7, pos - l));
51
52 if(index == 0) {
53 expectedMask[j] = 0;
54 } else {
55 expectedMask[j] = (byte) (-1 << index);
56 }
57 }
58 return this;
59 }
60
61 public WithMaskTaskCase maskHex(String string) {
62 string = string.replaceAll(" ", "");
63 this.hasMask = true;
64 expectedMask = hex.decode(string);
65 return this;
66 }
67
68 }
69
70 WithMaskTaskCase[] withMasks = new WithMaskTaskCase[] {
71 new WithMaskTaskCase("1::1/80")
72 .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00"),
73
74 new WithMaskTaskCase("ffff:ffee:1::/ff00:ff00:ff00:ff00::")
75 .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00"),
76 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
77 new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
78 new WithMaskTaskCase("1:2:3:4:5:6:7:8/128"),
79 new WithMaskTaskCase("::/0")
80 .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")
Yotam Harcholf3f11152013-09-05 16:47:16 -070081 };
82
83 @Test
84 public void testMasked() throws UnknownHostException {
Andreas Wundsame04c86f2013-11-05 17:13:18 -080085 for(WithMaskTaskCase w: withMasks) {
86 IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input);
87 if (!w.hasMask) {
Yotam Harchola289d552013-09-16 10:10:40 -070088 IPv6Address ip = value.getValue();
Andreas Wundsame04c86f2013-11-05 17:13:18 -080089 InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
Yotam Harcholf3f11152013-09-05 16:47:16 -070090
91 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
Andreas Wundsame04c86f2013-11-05 17:13:18 -080092 assertEquals(w.input.split("/")[0], ip.toString());
93 } else {
94 InetAddress inetAddress = InetAddress.getByName(w.input.substring(0, w.input.indexOf('/')));
Yotam Harcholf3f11152013-09-05 16:47:16 -070095
96 byte[] address = inetAddress.getAddress();
97 assertEquals(address.length, value.getValue().getBytes().length);
98
99 for (int j = 0; j < address.length; j++) {
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800100 address[j] &= w.expectedMask[j];
Yotam Harcholf3f11152013-09-05 16:47:16 -0700101 }
102
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800103 assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address));
104 assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700105 }
106 }
107 }
108
109
110 @Test
111 public void testOfString() throws UnknownHostException {
112 for(int i=0; i < testStrings.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700113 IPv6Address ip = IPv6Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700114 InetAddress inetAddress = InetAddress.getByName(testStrings[i]);
115
116 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
117 assertEquals(testStrings[i], ip.toString());
118 }
119 }
120
121 @Test
122 public void testOfByteArray() throws UnknownHostException {
123 for(int i=0; i < testStrings.length; i++ ) {
124 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700125 IPv6Address ip = IPv6Address.of(bytes);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700126 assertEquals(testStrings[i], ip.toString());
127 assertArrayEquals(bytes, ip.getBytes());
128 }
129 }
130
131 @Test
132 public void testReadFrom() throws OFParseError, UnknownHostException {
133 for(int i=0; i < testStrings.length; i++ ) {
134 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchola289d552013-09-16 10:10:40 -0700135 IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700136 assertEquals(testStrings[i], ip.toString());
137 assertArrayEquals(bytes, ip.getBytes());
138 }
139 }
140
141 String[] invalidIPs = {
142 "",
143 ":",
144 "1:2:3:4:5:6:7:8:9",
145 "1:2:3:4:5:6:7:8:",
146 "1:2:3:4:5:6:7:8g",
147 "1:2:3:",
148 "12345::",
149 "1::3::8",
150 "::3::"
151 };
152
153 @Test
154 public void testInvalidIPs() throws OFParseError {
155 for(String invalid : invalidIPs) {
156 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700157 IPv6Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700158 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
159 } catch(IllegalArgumentException e) {
160 // ok
161 }
162 }
163 }
164
165 @Test
166 public void testZeroCompression() throws OFParseError {
Yotam Harchola289d552013-09-16 10:10:40 -0700167 assertEquals("::", IPv6Address.of("::").toString(true, false));
168 assertEquals("0:0:0:0:0:0:0:0", IPv6Address.of("::").toString(false, false));
169 assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6Address.of("::").toString(false, true));
170 assertEquals("1::4:5:6:0:8", IPv6Address.of("1:0:0:4:5:6:0:8").toString(true, false));
171 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 -0700172 }
173}