blob: e3d5c81da87a955ba273574b61c41a29d7f55041 [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 };
24
25 @Test
26 public void testOfString() throws UnknownHostException {
27 for(int i=0; i < testStrings.length; i++ ) {
28 IPv6 ip = IPv6.of(testStrings[i]);
29 InetAddress inetAddress = InetAddress.getByName(testStrings[i]);
30
31 assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
32 assertEquals(testStrings[i], ip.toString());
33 }
34 }
35
36 @Test
37 public void testOfByteArray() throws UnknownHostException {
38 for(int i=0; i < testStrings.length; i++ ) {
39 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
40 IPv6 ip = IPv6.of(bytes);
41 assertEquals(testStrings[i], ip.toString());
42 assertArrayEquals(bytes, ip.getBytes());
43 }
44 }
45
46 @Test
47 public void testReadFrom() throws OFParseError, OFShortRead, UnknownHostException {
48 for(int i=0; i < testStrings.length; i++ ) {
49 byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress();
50 IPv6 ip = IPv6.readFrom(ChannelBuffers.copiedBuffer(bytes));
51 assertEquals(testStrings[i], ip.toString());
52 assertArrayEquals(bytes, ip.getBytes());
53 }
54 }
55
56 String[] invalidIPs = {
57 "",
58 ":",
59 "1:2:3:4:5:6:7:8:9",
60 "1:2:3:4:5:6:7:8:",
61 "1:2:3:4:5:6:7:8g",
62 "1:2:3:",
63 "12345::",
64 "1::3::8",
65 "::3::"
66 };
67
68 @Test
69 public void testInvalidIPs() throws OFParseError, OFShortRead {
70 for(String invalid : invalidIPs) {
71 try {
72 IPv6.of(invalid);
73 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
74 } catch(IllegalArgumentException e) {
75 // ok
76 }
77 }
78 }
79
80 @Test
81 public void testZeroCompression() throws OFParseError, OFShortRead {
82 assertEquals("::", IPv6.of("::").toString(true, false));
83 assertEquals("0:0:0:0:0:0:0:0", IPv6.of("::").toString(false, false));
84 assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6.of("::").toString(false, true));
85 assertEquals("1::4:5:6:0:8", IPv6.of("1:0:0:4:5:6:0:8").toString(true, false));
86 assertEquals("1:0:0:4::8", IPv6.of("1:0:0:4:0:0:0:8").toString(true, false));
87 }
88}