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