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 | }; |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 24 | |
| 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 Harchol | 6fccde6 | 2013-08-15 12:04:52 -0700 | [diff] [blame] | 61 | IPv6WithMask value = IPv6WithMask.of(ipsWithMask[i]); |
| 62 | if (!hasMask[i]) { |
| 63 | IPv6 ip = value.getValue(); |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 64 | 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 Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 69 | InetAddress inetAddress = InetAddress.getByName(ipsWithMask[i].substring(0, ipsWithMask[i].indexOf('/'))); |
| 70 | |
Yotam Harchol | 6fccde6 | 2013-08-15 12:04:52 -0700 | [diff] [blame] | 71 | 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 Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 84 | |
| 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 Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 110 | IPv6 ip = IPv6.read16Bytes(ChannelBuffers.copiedBuffer(bytes)); |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 111 | 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 | } |