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