Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 1 | package org.openflow.types; |
| 2 | |
| 3 | import static org.junit.Assert.assertArrayEquals; |
| 4 | import static org.junit.Assert.assertEquals; |
| 5 | import static org.junit.Assert.fail; |
| 6 | |
| 7 | import java.net.Inet6Address; |
| 8 | import java.net.InetAddress; |
| 9 | import java.net.UnknownHostException; |
| 10 | |
| 11 | import org.jboss.netty.buffer.ChannelBuffers; |
| 12 | import org.junit.Test; |
| 13 | import org.openflow.exceptions.OFParseError; |
| 14 | import org.openflow.exceptions.OFShortRead; |
| 15 | |
| 16 | public 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(); |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame^] | 50 | IPv6 ip = IPv6.SERIALIZER_V12.readFrom(ChannelBuffers.copiedBuffer(bytes)); |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 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 | } |