blob: f2f6fe8e5101005a726b7b24c8cb6729bded274a [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++ ) {
61 OFValueType value = IPv6WithMask.ofPossiblyMasked(ipsWithMask[i]);
62 if (value instanceof IPv6 && !hasMask[i]) {
63 // Types OK, check values
64 IPv6 ip = (IPv6)value;
65 InetAddress inetAddress = InetAddress.getByName(ipsWithMask[i]);
66
67 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
68 assertEquals(ipsWithMask[i], ip.toString());
69 } else if (value instanceof IPv6WithMask && hasMask[i]) {
70 IPv6WithMask ip = null;
71 try {
72 ip = (IPv6WithMask)value;
73 } catch (ClassCastException e) {
74 fail("Invalid Masked<T> type.");
75 }
76 // Types OK, check values
77 InetAddress inetAddress = InetAddress.getByName(ipsWithMask[i].substring(0, ipsWithMask[i].indexOf('/')));
78
79 assertArrayEquals(ip.value.getBytes(), inetAddress.getAddress());
80 assertEquals(ipsWithMask[i].substring(0, ipsWithMask[i].indexOf('/')), ip.value.toString());
81 assertArrayEquals(masks[i], ip.mask.getBytes());
82 } else if (value instanceof IPv6) {
83 fail("Expected masked IPv6, got unmasked IPv6.");
84 } else {
85 fail("Expected unmasked IPv6, got masked IPv6.");
86 }
87 }
88 }
89
Andreas Wundsam40e14f72013-05-06 14:49:08 -070090
91 @Test
92 public void testOfString() throws UnknownHostException {
93 for(int i=0; i < testStrings.length; i++ ) {
94 IPv6 ip = IPv6.of(testStrings[i]);
95 InetAddress inetAddress = InetAddress.getByName(testStrings[i]);
96
97 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
98 assertEquals(testStrings[i], ip.toString());
99 }
100 }
101
102 @Test
103 public void testOfByteArray() throws UnknownHostException {
104 for(int i=0; i < testStrings.length; i++ ) {
105 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
106 IPv6 ip = IPv6.of(bytes);
107 assertEquals(testStrings[i], ip.toString());
108 assertArrayEquals(bytes, ip.getBytes());
109 }
110 }
111
112 @Test
113 public void testReadFrom() throws OFParseError, OFShortRead, UnknownHostException {
114 for(int i=0; i < testStrings.length; i++ ) {
115 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
Yotam Harchold7b84202013-07-26 16:08:10 -0700116 IPv6 ip = IPv6.read16Bytes(ChannelBuffers.copiedBuffer(bytes));
Andreas Wundsam40e14f72013-05-06 14:49:08 -0700117 assertEquals(testStrings[i], ip.toString());
118 assertArrayEquals(bytes, ip.getBytes());
119 }
120 }
121
122 String[] invalidIPs = {
123 "",
124 ":",
125 "1:2:3:4:5:6:7:8:9",
126 "1:2:3:4:5:6:7:8:",
127 "1:2:3:4:5:6:7:8g",
128 "1:2:3:",
129 "12345::",
130 "1::3::8",
131 "::3::"
132 };
133
134 @Test
135 public void testInvalidIPs() throws OFParseError, OFShortRead {
136 for(String invalid : invalidIPs) {
137 try {
138 IPv6.of(invalid);
139 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
140 } catch(IllegalArgumentException e) {
141 // ok
142 }
143 }
144 }
145
146 @Test
147 public void testZeroCompression() throws OFParseError, OFShortRead {
148 assertEquals("::", IPv6.of("::").toString(true, false));
149 assertEquals("0:0:0:0:0:0:0:0", IPv6.of("::").toString(false, false));
150 assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6.of("::").toString(false, true));
151 assertEquals("1::4:5:6:0:8", IPv6.of("1:0:0:4:5:6:0:8").toString(true, false));
152 assertEquals("1:0:0:4::8", IPv6.of("1:0:0:4:0:0:0:8").toString(true, false));
153 }
154}