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++ ) { |
| 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 Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 90 | |
| 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 Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 116 | IPv6 ip = IPv6.read16Bytes(ChannelBuffers.copiedBuffer(bytes)); |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 117 | 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 | } |