blob: 092dacb454564c940bfc4c3f0c8f9bc12f1f0b17 [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;
5import static org.junit.Assert.fail;
6
7import java.net.Inet6Address;
8import java.net.InetAddress;
9import java.net.UnknownHostException;
10
11import org.jboss.netty.buffer.ChannelBuffers;
12import org.junit.Test;
13import org.projectfloodlight.openflow.exceptions.OFParseError;
14
15public class IPv6Test {
16
17 String[] testStrings = {
18 "::",
19 "::1",
20 "ffe0::",
21 "1:2:3:4:5:6:7:8"
22 };
23
24 String[] ipsWithMask = {
25 "1::1/80",
26 "1:2:3:4::/ffff:ffff:ffff:ff00::",
27 "ffff:ffee:1::/ff00:ff00:ff00:ff00::",
28 "8:8:8:8:8:8:8:8",
29 };
30
31 byte[][] masks = {
32 new byte[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
33 (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
34 (byte)0xff, (byte)0xff, (byte)0x00, (byte)0x00,
35 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
36 new byte[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
37 (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x00,
38 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
39 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
40 new byte[] { (byte)0xff, (byte)0x00, (byte)0xff, (byte)0x00,
41 (byte)0xff, (byte)0x00, (byte)0xff, (byte)0x00,
42 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
43 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
44 new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
45 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
46 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
47 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }
48 };
49
50 boolean[] hasMask = {
51 true,
52 true,
53 true,
54 false
55 };
56
57 @Test
58 public void testMasked() throws UnknownHostException {
59 for(int i=0; i < ipsWithMask.length; i++ ) {
60 IPv6WithMask value = IPv6WithMask.of(ipsWithMask[i]);
61 if (!hasMask[i]) {
62 IPv6 ip = value.getValue();
63 InetAddress inetAddress = InetAddress.getByName(ipsWithMask[i]);
64
65 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
66 assertEquals(ipsWithMask[i], ip.toString());
67 } else if (value instanceof IPv6WithMask && hasMask[i]) {
68 InetAddress inetAddress = InetAddress.getByName(ipsWithMask[i].substring(0, ipsWithMask[i].indexOf('/')));
69
70 byte[] address = inetAddress.getAddress();
71 assertEquals(address.length, value.getValue().getBytes().length);
72
73 for (int j = 0; j < address.length; j++) {
74 address[j] &= masks[i][j];
75 }
76
77 assertArrayEquals(value.getValue().getBytes(), address);
78 assertArrayEquals(masks[i], value.getMask().getBytes());
79 }
80 }
81 }
82
83
84 @Test
85 public void testOfString() throws UnknownHostException {
86 for(int i=0; i < testStrings.length; i++ ) {
87 IPv6 ip = IPv6.of(testStrings[i]);
88 InetAddress inetAddress = InetAddress.getByName(testStrings[i]);
89
90 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
91 assertEquals(testStrings[i], ip.toString());
92 }
93 }
94
95 @Test
96 public void testOfByteArray() throws UnknownHostException {
97 for(int i=0; i < testStrings.length; i++ ) {
98 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
99 IPv6 ip = IPv6.of(bytes);
100 assertEquals(testStrings[i], ip.toString());
101 assertArrayEquals(bytes, ip.getBytes());
102 }
103 }
104
105 @Test
106 public void testReadFrom() throws OFParseError, UnknownHostException {
107 for(int i=0; i < testStrings.length; i++ ) {
108 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
109 IPv6 ip = IPv6.read16Bytes(ChannelBuffers.copiedBuffer(bytes));
110 assertEquals(testStrings[i], ip.toString());
111 assertArrayEquals(bytes, ip.getBytes());
112 }
113 }
114
115 String[] invalidIPs = {
116 "",
117 ":",
118 "1:2:3:4:5:6:7:8:9",
119 "1:2:3:4:5:6:7:8:",
120 "1:2:3:4:5:6:7:8g",
121 "1:2:3:",
122 "12345::",
123 "1::3::8",
124 "::3::"
125 };
126
127 @Test
128 public void testInvalidIPs() throws OFParseError {
129 for(String invalid : invalidIPs) {
130 try {
131 IPv6.of(invalid);
132 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
133 } catch(IllegalArgumentException e) {
134 // ok
135 }
136 }
137 }
138
139 @Test
140 public void testZeroCompression() throws OFParseError {
141 assertEquals("::", IPv6.of("::").toString(true, false));
142 assertEquals("0:0:0:0:0:0:0:0", IPv6.of("::").toString(false, false));
143 assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6.of("::").toString(false, true));
144 assertEquals("1::4:5:6:0:8", IPv6.of("1:0:0:4:5:6:0:8").toString(true, false));
145 assertEquals("1:0:0:4::8", IPv6.of("1:0:0:4:0:0:0:8").toString(true, false));
146 }
147}