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; |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 5 | import static org.junit.Assert.assertThat; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 6 | import static org.junit.Assert.fail; |
| 7 | |
| 8 | import java.net.Inet6Address; |
| 9 | import java.net.InetAddress; |
| 10 | import java.net.UnknownHostException; |
| 11 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 12 | import org.hamcrest.CoreMatchers; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 13 | import org.jboss.netty.buffer.ChannelBuffers; |
| 14 | import org.junit.Test; |
| 15 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 16 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 17 | import com.google.common.io.BaseEncoding; |
| 18 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 19 | public class IPv6AddressTest { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 20 | |
| 21 | String[] testStrings = { |
| 22 | "::", |
| 23 | "::1", |
| 24 | "ffe0::", |
| 25 | "1:2:3:4:5:6:7:8" |
| 26 | }; |
| 27 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 28 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 29 | private final BaseEncoding hex = BaseEncoding.base16().omitPadding().lowerCase(); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 30 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 31 | private class WithMaskTaskCase { |
| 32 | final String input; |
| 33 | boolean hasMask; |
| 34 | byte[] expectedMask = hex.decode("ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff".replaceAll(" ", "")); |
| 35 | |
| 36 | public WithMaskTaskCase(String input) { |
| 37 | super(); |
| 38 | this.input = input; |
| 39 | } |
| 40 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 41 | public WithMaskTaskCase maskHex(String string) { |
| 42 | string = string.replaceAll(" ", ""); |
| 43 | this.hasMask = true; |
| 44 | expectedMask = hex.decode(string); |
| 45 | return this; |
| 46 | } |
| 47 | |
| 48 | } |
| 49 | |
| 50 | WithMaskTaskCase[] withMasks = new WithMaskTaskCase[] { |
| 51 | new WithMaskTaskCase("1::1/80") |
| 52 | .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00"), |
| 53 | |
| 54 | new WithMaskTaskCase("ffff:ffee:1::/ff00:ff00:ff00:ff00::") |
| 55 | .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00"), |
| 56 | new WithMaskTaskCase("8:8:8:8:8:8:8:8"), |
| 57 | new WithMaskTaskCase("8:8:8:8:8:8:8:8"), |
| 58 | new WithMaskTaskCase("1:2:3:4:5:6:7:8/128"), |
| 59 | new WithMaskTaskCase("::/0") |
| 60 | .maskHex("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00") |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | @Test |
| 64 | public void testMasked() throws UnknownHostException { |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 65 | for(WithMaskTaskCase w: withMasks) { |
| 66 | IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input); |
| 67 | if (!w.hasMask) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 68 | IPv6Address ip = value.getValue(); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 69 | InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 70 | |
| 71 | assertArrayEquals(ip.getBytes(), inetAddress.getAddress()); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 72 | assertEquals(w.input.split("/")[0], ip.toString()); |
| 73 | } else { |
| 74 | InetAddress inetAddress = InetAddress.getByName(w.input.substring(0, w.input.indexOf('/'))); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 75 | |
| 76 | byte[] address = inetAddress.getAddress(); |
| 77 | assertEquals(address.length, value.getValue().getBytes().length); |
| 78 | |
| 79 | for (int j = 0; j < address.length; j++) { |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 80 | address[j] &= w.expectedMask[j]; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 83 | assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address)); |
| 84 | assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 90 | @Test |
| 91 | public void testOfString() throws UnknownHostException { |
| 92 | for(int i=0; i < testStrings.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 93 | IPv6Address ip = IPv6Address.of(testStrings[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 94 | InetAddress inetAddress = InetAddress.getByName(testStrings[i]); |
| 95 | |
| 96 | assertArrayEquals(ip.getBytes(), inetAddress.getAddress()); |
| 97 | assertEquals(testStrings[i], ip.toString()); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testOfByteArray() throws UnknownHostException { |
| 103 | for(int i=0; i < testStrings.length; i++ ) { |
| 104 | byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress(); |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 105 | IPv6Address ip = IPv6Address.of(bytes); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 106 | assertEquals(testStrings[i], ip.toString()); |
| 107 | assertArrayEquals(bytes, ip.getBytes()); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void testReadFrom() throws OFParseError, UnknownHostException { |
| 113 | for(int i=0; i < testStrings.length; i++ ) { |
| 114 | byte[] bytes = Inet6Address.getByName(testStrings[i]).getAddress(); |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 115 | IPv6Address ip = IPv6Address.read16Bytes(ChannelBuffers.copiedBuffer(bytes)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 116 | assertEquals(testStrings[i], ip.toString()); |
| 117 | assertArrayEquals(bytes, ip.getBytes()); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | String[] invalidIPs = { |
| 122 | "", |
| 123 | ":", |
| 124 | "1:2:3:4:5:6:7:8:9", |
| 125 | "1:2:3:4:5:6:7:8:", |
| 126 | "1:2:3:4:5:6:7:8g", |
| 127 | "1:2:3:", |
| 128 | "12345::", |
| 129 | "1::3::8", |
| 130 | "::3::" |
| 131 | }; |
| 132 | |
| 133 | @Test |
| 134 | public void testInvalidIPs() throws OFParseError { |
| 135 | for(String invalid : invalidIPs) { |
| 136 | try { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 137 | IPv6Address.of(invalid); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 138 | fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException"); |
| 139 | } catch(IllegalArgumentException e) { |
| 140 | // ok |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void testZeroCompression() throws OFParseError { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 147 | assertEquals("::", IPv6Address.of("::").toString(true, false)); |
| 148 | assertEquals("0:0:0:0:0:0:0:0", IPv6Address.of("::").toString(false, false)); |
| 149 | assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", IPv6Address.of("::").toString(false, true)); |
| 150 | assertEquals("1::4:5:6:0:8", IPv6Address.of("1:0:0:4:5:6:0:8").toString(true, false)); |
| 151 | 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] | 152 | } |
| 153 | } |