Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onlab.packet; |
| 17 | |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 18 | import java.net.InetAddress; |
| 19 | |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 20 | import org.junit.Test; |
| 21 | |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 22 | import com.google.common.net.InetAddresses; |
| 23 | import com.google.common.testing.EqualsTester; |
Thomas Vachuska | 9d8f72f | 2014-11-03 10:14:05 -0800 | [diff] [blame] | 24 | |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 25 | import static org.hamcrest.Matchers.is; |
Pavlin Radoslavov | 34c8164 | 2014-11-04 16:21:38 -0800 | [diff] [blame] | 26 | import static org.junit.Assert.assertNull; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 27 | import static org.junit.Assert.assertThat; |
| 28 | import static org.junit.Assert.assertTrue; |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 29 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Tests for class {@link IpAddress}. |
| 33 | */ |
| 34 | public class IpAddressTest { |
| 35 | /** |
| 36 | * Tests the immutability of {@link IpAddress}. |
| 37 | */ |
| 38 | @Test |
| 39 | public void testImmutable() { |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 40 | assertThatClassIsImmutableBaseClass(IpAddress.class); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Tests the length of the address in bytes (octets). |
| 45 | */ |
| 46 | @Test |
| 47 | public void testAddrByteLength() { |
| 48 | assertThat(IpAddress.INET_BYTE_LENGTH, is(4)); |
| 49 | assertThat(IpAddress.INET6_BYTE_LENGTH, is(16)); |
| 50 | assertThat(IpAddress.byteLength(IpAddress.Version.INET), is(4)); |
| 51 | assertThat(IpAddress.byteLength(IpAddress.Version.INET6), is(16)); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Tests the length of the address in bits. |
| 56 | */ |
| 57 | @Test |
| 58 | public void testAddrBitLength() { |
| 59 | assertThat(IpAddress.INET_BIT_LENGTH, is(32)); |
| 60 | assertThat(IpAddress.INET6_BIT_LENGTH, is(128)); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Tests returning the IP address version. |
| 65 | */ |
| 66 | @Test |
| 67 | public void testVersion() { |
| 68 | IpAddress ipAddress; |
| 69 | |
| 70 | // IPv4 |
| 71 | ipAddress = IpAddress.valueOf("0.0.0.0"); |
| 72 | assertThat(ipAddress.version(), is(IpAddress.Version.INET)); |
| 73 | |
| 74 | // IPv6 |
| 75 | ipAddress = IpAddress.valueOf("::"); |
| 76 | assertThat(ipAddress.version(), is(IpAddress.Version.INET6)); |
| 77 | } |
| 78 | |
| 79 | /** |
Pavlin Radoslavov | 34c8164 | 2014-11-04 16:21:38 -0800 | [diff] [blame] | 80 | * Tests getting the Ip4Address and Ip6Address view of the IP address. |
| 81 | */ |
| 82 | @Test |
| 83 | public void testGetIp4AndIp6AddressView() { |
| 84 | IpAddress ipAddress; |
| 85 | Ip4Address ip4Address; |
| 86 | Ip6Address ip6Address; |
| 87 | |
| 88 | // Pure IPv4 IpAddress |
| 89 | ipAddress = IpAddress.valueOf("1.2.3.4"); |
| 90 | ip4Address = ipAddress.getIp4Address(); |
| 91 | ip6Address = ipAddress.getIp6Address(); |
| 92 | assertThat(ip4Address.toString(), is("1.2.3.4")); |
| 93 | assertNull(ip6Address); |
| 94 | |
| 95 | // IPv4 IpAddress that is Ip4Address |
| 96 | ipAddress = Ip4Address.valueOf("1.2.3.4"); |
| 97 | ip4Address = ipAddress.getIp4Address(); |
| 98 | ip6Address = ipAddress.getIp6Address(); |
| 99 | assertThat(ip4Address.toString(), is("1.2.3.4")); |
| 100 | assertNull(ip6Address); |
| 101 | |
| 102 | // Pure IPv6 IpAddress |
| 103 | ipAddress = IpAddress.valueOf("1111:2222::"); |
| 104 | ip4Address = ipAddress.getIp4Address(); |
| 105 | ip6Address = ipAddress.getIp6Address(); |
| 106 | assertNull(ip4Address); |
| 107 | assertThat(ip6Address.toString(), is("1111:2222::")); |
| 108 | |
| 109 | // IPv6 IpAddress that is Ip6Address |
| 110 | ipAddress = Ip6Address.valueOf("1111:2222::"); |
| 111 | ip4Address = ipAddress.getIp4Address(); |
| 112 | ip6Address = ipAddress.getIp6Address(); |
| 113 | assertNull(ip4Address); |
| 114 | assertThat(ip6Address.toString(), is("1111:2222::")); |
| 115 | } |
| 116 | |
| 117 | /** |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 118 | * Tests returning an IPv4 address as a byte array. |
| 119 | */ |
| 120 | @Test |
| 121 | public void testAddressToOctetsIPv4() { |
| 122 | IpAddress ipAddress; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 123 | byte[] value; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 124 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 125 | value = new byte[] {1, 2, 3, 4}; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 126 | ipAddress = IpAddress.valueOf("1.2.3.4"); |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 127 | assertThat(ipAddress.toOctets(), is(value)); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 128 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 129 | value = new byte[] {0, 0, 0, 0}; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 130 | ipAddress = IpAddress.valueOf("0.0.0.0"); |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 131 | assertThat(ipAddress.toOctets(), is(value)); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 132 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 133 | value = new byte[] {(byte) 0xff, (byte) 0xff, |
| 134 | (byte) 0xff, (byte) 0xff}; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 135 | ipAddress = IpAddress.valueOf("255.255.255.255"); |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 136 | assertThat(ipAddress.toOctets(), is(value)); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Tests returning an IPv6 address as a byte array. |
| 141 | */ |
| 142 | @Test |
| 143 | public void testAddressToOctetsIPv6() { |
| 144 | IpAddress ipAddress; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 145 | byte[] value; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 146 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 147 | value = new byte[] {0x11, 0x11, 0x22, 0x22, |
| 148 | 0x33, 0x33, 0x44, 0x44, |
| 149 | 0x55, 0x55, 0x66, 0x66, |
| 150 | 0x77, 0x77, |
| 151 | (byte) 0x88, (byte) 0x88}; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 152 | ipAddress = |
| 153 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 154 | assertThat(ipAddress.toOctets(), is(value)); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 155 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 156 | value = new byte[] {0x00, 0x00, 0x00, 0x00, |
| 157 | 0x00, 0x00, 0x00, 0x00, |
| 158 | 0x00, 0x00, 0x00, 0x00, |
| 159 | 0x00, 0x00, 0x00, 0x00}; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 160 | ipAddress = IpAddress.valueOf("::"); |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 161 | assertThat(ipAddress.toOctets(), is(value)); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 162 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 163 | value = new byte[] {(byte) 0xff, (byte) 0xff, |
| 164 | (byte) 0xff, (byte) 0xff, |
| 165 | (byte) 0xff, (byte) 0xff, |
| 166 | (byte) 0xff, (byte) 0xff, |
| 167 | (byte) 0xff, (byte) 0xff, |
| 168 | (byte) 0xff, (byte) 0xff, |
| 169 | (byte) 0xff, (byte) 0xff, |
| 170 | (byte) 0xff, (byte) 0xff}; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 171 | ipAddress = |
| 172 | IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 173 | assertThat(ipAddress.toOctets(), is(value)); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 177 | * Tests valueOf() converter for IPv4 integer value. |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 178 | */ |
| 179 | @Test |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 180 | public void testValueOfForIntegerIPv4() { |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 181 | IpAddress ipAddress; |
| 182 | |
| 183 | ipAddress = IpAddress.valueOf(0x01020304); |
| 184 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
| 185 | |
| 186 | ipAddress = IpAddress.valueOf(0); |
| 187 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 188 | |
| 189 | ipAddress = IpAddress.valueOf(0xffffffff); |
| 190 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Tests valueOf() converter for IPv4 byte array. |
| 195 | */ |
| 196 | @Test |
| 197 | public void testValueOfByteArrayIPv4() { |
| 198 | IpAddress ipAddress; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 199 | byte[] value; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 200 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 201 | value = new byte[] {1, 2, 3, 4}; |
| 202 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 203 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
| 204 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 205 | value = new byte[] {0, 0, 0, 0}; |
| 206 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 207 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 208 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 209 | value = new byte[] {(byte) 0xff, (byte) 0xff, |
| 210 | (byte) 0xff, (byte) 0xff}; |
| 211 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 212 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Tests valueOf() converter for IPv6 byte array. |
| 217 | */ |
| 218 | @Test |
| 219 | public void testValueOfByteArrayIPv6() { |
| 220 | IpAddress ipAddress; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 221 | byte[] value; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 222 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 223 | value = new byte[] {0x11, 0x11, 0x22, 0x22, |
| 224 | 0x33, 0x33, 0x44, 0x44, |
| 225 | 0x55, 0x55, 0x66, 0x66, |
| 226 | 0x77, 0x77, |
| 227 | (byte) 0x88, (byte) 0x88}; |
| 228 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 229 | assertThat(ipAddress.toString(), |
| 230 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 231 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 232 | value = new byte[] {0x00, 0x00, 0x00, 0x00, |
| 233 | 0x00, 0x00, 0x00, 0x00, |
| 234 | 0x00, 0x00, 0x00, 0x00, |
| 235 | 0x00, 0x00, 0x00, 0x00}; |
| 236 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 237 | assertThat(ipAddress.toString(), is("::")); |
| 238 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 239 | value = new byte[] {(byte) 0xff, (byte) 0xff, |
| 240 | (byte) 0xff, (byte) 0xff, |
| 241 | (byte) 0xff, (byte) 0xff, |
| 242 | (byte) 0xff, (byte) 0xff, |
| 243 | (byte) 0xff, (byte) 0xff, |
| 244 | (byte) 0xff, (byte) 0xff, |
| 245 | (byte) 0xff, (byte) 0xff, |
| 246 | (byte) 0xff, (byte) 0xff}; |
| 247 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 248 | assertThat(ipAddress.toString(), |
| 249 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Tests invalid valueOf() converter for a null array for IPv4. |
| 254 | */ |
| 255 | @Test(expected = NullPointerException.class) |
| 256 | public void testInvalidValueOfNullArrayIPv4() { |
| 257 | IpAddress ipAddress; |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 258 | byte[] value; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 259 | |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 260 | value = null; |
| 261 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Tests invalid valueOf() converter for a null array for IPv6. |
| 266 | */ |
| 267 | @Test(expected = NullPointerException.class) |
| 268 | public void testInvalidValueOfNullArrayIPv6() { |
| 269 | IpAddress ipAddress; |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 270 | byte[] value; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 271 | |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 272 | value = null; |
| 273 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Tests invalid valueOf() converger for an array that is too short for |
| 278 | * IPv4. |
| 279 | */ |
| 280 | @Test(expected = IllegalArgumentException.class) |
| 281 | public void testInvalidValueOfShortArrayIPv4() { |
| 282 | IpAddress ipAddress; |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 283 | byte[] value; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 284 | |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 285 | value = new byte[] {1, 2, 3}; |
| 286 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Tests invalid valueOf() converger for an array that is too short for |
| 291 | * IPv6. |
| 292 | */ |
| 293 | @Test(expected = IllegalArgumentException.class) |
| 294 | public void testInvalidValueOfShortArrayIPv6() { |
| 295 | IpAddress ipAddress; |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 296 | byte[] value; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 297 | |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 298 | value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 299 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Tests valueOf() converter for IPv4 byte array and an offset. |
| 304 | */ |
| 305 | @Test |
| 306 | public void testValueOfByteArrayOffsetIPv4() { |
| 307 | IpAddress ipAddress; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 308 | byte[] value; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 309 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 310 | value = new byte[] {11, 22, 33, // Preamble |
| 311 | 1, 2, 3, 4, |
| 312 | 44, 55}; // Extra bytes |
| 313 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 3); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 314 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
| 315 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 316 | value = new byte[] {11, 22, // Preamble |
| 317 | 0, 0, 0, 0, |
| 318 | 33}; // Extra bytes |
| 319 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 320 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 321 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 322 | value = new byte[] {11, 22, // Preamble |
| 323 | (byte) 0xff, (byte) 0xff, |
| 324 | (byte) 0xff, (byte) 0xff, |
| 325 | 33}; // Extra bytes |
| 326 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 327 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Tests valueOf() converter for IPv6 byte array and an offset. |
| 332 | */ |
| 333 | @Test |
| 334 | public void testValueOfByteArrayOffsetIPv6() { |
| 335 | IpAddress ipAddress; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 336 | byte[] value; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 337 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 338 | value = new byte[] {11, 22, 33, // Preamble |
| 339 | 0x11, 0x11, 0x22, 0x22, |
| 340 | 0x33, 0x33, 0x44, 0x44, |
| 341 | 0x55, 0x55, 0x66, 0x66, |
| 342 | 0x77, 0x77, |
| 343 | (byte) 0x88, (byte) 0x88, |
| 344 | 44, 55}; // Extra bytes |
| 345 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 3); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 346 | assertThat(ipAddress.toString(), |
| 347 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 348 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 349 | value = new byte[] {11, 22, // Preamble |
| 350 | 0x00, 0x00, 0x00, 0x00, |
| 351 | 0x00, 0x00, 0x00, 0x00, |
| 352 | 0x00, 0x00, 0x00, 0x00, |
| 353 | 0x00, 0x00, 0x00, 0x00, |
| 354 | 33}; // Extra bytes |
| 355 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 356 | assertThat(ipAddress.toString(), is("::")); |
| 357 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 358 | value = new byte[] {11, 22, // Preamble |
| 359 | (byte) 0xff, (byte) 0xff, |
| 360 | (byte) 0xff, (byte) 0xff, |
| 361 | (byte) 0xff, (byte) 0xff, |
| 362 | (byte) 0xff, (byte) 0xff, |
| 363 | (byte) 0xff, (byte) 0xff, |
| 364 | (byte) 0xff, (byte) 0xff, |
| 365 | (byte) 0xff, (byte) 0xff, |
| 366 | (byte) 0xff, (byte) 0xff, |
| 367 | 33}; // Extra bytes |
| 368 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 369 | assertThat(ipAddress.toString(), |
| 370 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Tests invalid valueOf() converger for an array and an invalid offset |
| 375 | * for IPv4. |
| 376 | */ |
| 377 | @Test(expected = IllegalArgumentException.class) |
| 378 | public void testInvalidValueOfArrayInvalidOffsetIPv4() { |
| 379 | IpAddress ipAddress; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 380 | byte[] value; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 381 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 382 | value = new byte[] {11, 22, 33, // Preamble |
| 383 | 1, 2, 3, 4, |
| 384 | 44, 55}; // Extra bytes |
| 385 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 6); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Tests invalid valueOf() converger for an array and an invalid offset |
| 390 | * for IPv6. |
| 391 | */ |
| 392 | @Test(expected = IllegalArgumentException.class) |
| 393 | public void testInvalidValueOfArrayInvalidOffsetIPv6() { |
| 394 | IpAddress ipAddress; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 395 | byte[] value; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 396 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 397 | value = new byte[] {11, 22, 33, // Preamble |
| 398 | 0x11, 0x11, 0x22, 0x22, |
| 399 | 0x33, 0x33, 0x44, 0x44, |
| 400 | 0x55, 0x55, 0x66, 0x66, |
| 401 | 0x77, 0x77, |
| 402 | (byte) 0x88, (byte) 0x88, |
| 403 | 44, 55}; // Extra bytes |
| 404 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 6); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /** |
Pavlin Radoslavov | af5ff79 | 2014-10-31 20:51:47 -0700 | [diff] [blame] | 408 | * Tests valueOf() converter for IPv4 InetAddress. |
| 409 | */ |
| 410 | @Test |
| 411 | public void testValueOfInetAddressIPv4() { |
| 412 | IpAddress ipAddress; |
| 413 | InetAddress inetAddress; |
| 414 | |
| 415 | inetAddress = InetAddresses.forString("1.2.3.4"); |
| 416 | ipAddress = IpAddress.valueOf(inetAddress); |
| 417 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
| 418 | |
| 419 | inetAddress = InetAddresses.forString("0.0.0.0"); |
| 420 | ipAddress = IpAddress.valueOf(inetAddress); |
| 421 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 422 | |
| 423 | inetAddress = InetAddresses.forString("255.255.255.255"); |
| 424 | ipAddress = IpAddress.valueOf(inetAddress); |
| 425 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Tests valueOf() converter for IPv6 InetAddress. |
| 430 | */ |
| 431 | @Test |
| 432 | public void testValueOfInetAddressIPv6() { |
| 433 | IpAddress ipAddress; |
| 434 | InetAddress inetAddress; |
| 435 | |
| 436 | inetAddress = |
| 437 | InetAddresses.forString("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 438 | ipAddress = IpAddress.valueOf(inetAddress); |
| 439 | assertThat(ipAddress.toString(), |
| 440 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 441 | |
| 442 | inetAddress = InetAddresses.forString("::"); |
| 443 | ipAddress = IpAddress.valueOf(inetAddress); |
| 444 | assertThat(ipAddress.toString(), is("::")); |
| 445 | |
| 446 | inetAddress = |
| 447 | InetAddresses.forString("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 448 | ipAddress = IpAddress.valueOf(inetAddress); |
| 449 | assertThat(ipAddress.toString(), |
| 450 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 451 | } |
| 452 | |
| 453 | /** |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 454 | * Tests valueOf() converter for IPv4 string. |
| 455 | */ |
| 456 | @Test |
| 457 | public void testValueOfStringIPv4() { |
| 458 | IpAddress ipAddress; |
| 459 | |
| 460 | ipAddress = IpAddress.valueOf("1.2.3.4"); |
| 461 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
| 462 | |
| 463 | ipAddress = IpAddress.valueOf("0.0.0.0"); |
| 464 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 465 | |
| 466 | ipAddress = IpAddress.valueOf("255.255.255.255"); |
| 467 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Tests valueOf() converter for IPv6 string. |
| 472 | */ |
| 473 | @Test |
| 474 | public void testValueOfStringIPv6() { |
| 475 | IpAddress ipAddress; |
| 476 | |
| 477 | ipAddress = |
| 478 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 479 | assertThat(ipAddress.toString(), |
| 480 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 481 | |
| 482 | ipAddress = IpAddress.valueOf("::"); |
| 483 | assertThat(ipAddress.toString(), is("::")); |
| 484 | |
| 485 | ipAddress = |
| 486 | IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 487 | assertThat(ipAddress.toString(), |
| 488 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Tests invalid valueOf() converter for a null string. |
| 493 | */ |
| 494 | @Test(expected = NullPointerException.class) |
| 495 | public void testInvalidValueOfNullString() { |
| 496 | IpAddress ipAddress; |
| 497 | |
| 498 | String fromString = null; |
| 499 | ipAddress = IpAddress.valueOf(fromString); |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Tests invalid valueOf() converter for an empty string. |
| 504 | */ |
| 505 | @Test(expected = IllegalArgumentException.class) |
| 506 | public void testInvalidValueOfEmptyString() { |
| 507 | IpAddress ipAddress; |
| 508 | |
| 509 | String fromString = ""; |
| 510 | ipAddress = IpAddress.valueOf(fromString); |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Tests invalid valueOf() converter for an incorrect string. |
| 515 | */ |
| 516 | @Test(expected = IllegalArgumentException.class) |
| 517 | public void testInvalidValueOfIncorrectString() { |
| 518 | IpAddress ipAddress; |
| 519 | |
| 520 | String fromString = "NoSuchIpAddress"; |
| 521 | ipAddress = IpAddress.valueOf(fromString); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Tests making a mask prefix for a given prefix length for IPv4. |
| 526 | */ |
| 527 | @Test |
| 528 | public void testMakeMaskPrefixIPv4() { |
| 529 | IpAddress ipAddress; |
| 530 | |
| 531 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 25); |
| 532 | assertThat(ipAddress.toString(), is("255.255.255.128")); |
| 533 | |
| 534 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 0); |
| 535 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 536 | |
| 537 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 32); |
| 538 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Tests making a mask prefix for a given prefix length for IPv6. |
| 543 | */ |
| 544 | @Test |
| 545 | public void testMakeMaskPrefixIPv6() { |
| 546 | IpAddress ipAddress; |
| 547 | |
| 548 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 8); |
| 549 | assertThat(ipAddress.toString(), is("ff00::")); |
| 550 | |
| 551 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 120); |
| 552 | assertThat(ipAddress.toString(), |
| 553 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00")); |
| 554 | |
| 555 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 0); |
| 556 | assertThat(ipAddress.toString(), is("::")); |
| 557 | |
| 558 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 128); |
| 559 | assertThat(ipAddress.toString(), |
| 560 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 561 | |
| 562 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 64); |
| 563 | assertThat(ipAddress.toString(), is("ffff:ffff:ffff:ffff::")); |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * Tests making a mask prefix for an invalid prefix length for IPv4: |
| 568 | * negative prefix length. |
| 569 | */ |
| 570 | @Test(expected = IllegalArgumentException.class) |
| 571 | public void testInvalidMakeNegativeMaskPrefixIPv4() { |
| 572 | IpAddress ipAddress; |
| 573 | |
| 574 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, -1); |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Tests making a mask prefix for an invalid prefix length for IPv6: |
| 579 | * negative prefix length. |
| 580 | */ |
| 581 | @Test(expected = IllegalArgumentException.class) |
| 582 | public void testInvalidMakeNegativeMaskPrefixIPv6() { |
| 583 | IpAddress ipAddress; |
| 584 | |
| 585 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, -1); |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Tests making a mask prefix for an invalid prefix length for IPv4: |
| 590 | * too long prefix length. |
| 591 | */ |
| 592 | @Test(expected = IllegalArgumentException.class) |
| 593 | public void testInvalidMakeTooLongMaskPrefixIPv4() { |
| 594 | IpAddress ipAddress; |
| 595 | |
| 596 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 33); |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Tests making a mask prefix for an invalid prefix length for IPv6: |
| 601 | * too long prefix length. |
| 602 | */ |
| 603 | @Test(expected = IllegalArgumentException.class) |
| 604 | public void testInvalidMakeTooLongMaskPrefixIPv6() { |
| 605 | IpAddress ipAddress; |
| 606 | |
| 607 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 129); |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Tests making of a masked address for IPv4. |
| 612 | */ |
| 613 | @Test |
| 614 | public void testMakeMaskedAddressIPv4() { |
| 615 | IpAddress ipAddress = IpAddress.valueOf("1.2.3.5"); |
| 616 | IpAddress ipAddressMasked; |
| 617 | |
| 618 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 24); |
| 619 | assertThat(ipAddressMasked.toString(), is("1.2.3.0")); |
| 620 | |
| 621 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0); |
| 622 | assertThat(ipAddressMasked.toString(), is("0.0.0.0")); |
| 623 | |
| 624 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 32); |
| 625 | assertThat(ipAddressMasked.toString(), is("1.2.3.5")); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Tests making of a masked address for IPv6. |
| 630 | */ |
| 631 | @Test |
| 632 | public void testMakeMaskedAddressIPv6() { |
| 633 | IpAddress ipAddress = |
| 634 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"); |
| 635 | IpAddress ipAddressMasked; |
| 636 | |
| 637 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 8); |
| 638 | assertThat(ipAddressMasked.toString(), is("1100::")); |
| 639 | |
| 640 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 120); |
| 641 | assertThat(ipAddressMasked.toString(), |
| 642 | is("1111:2222:3333:4444:5555:6666:7777:8800")); |
| 643 | |
| 644 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0); |
| 645 | assertThat(ipAddressMasked.toString(), is("::")); |
| 646 | |
| 647 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 128); |
| 648 | assertThat(ipAddressMasked.toString(), |
| 649 | is("1111:2222:3333:4444:5555:6666:7777:8885")); |
| 650 | |
| 651 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 64); |
| 652 | assertThat(ipAddressMasked.toString(), is("1111:2222:3333:4444::")); |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Tests making of a masked address for invalid prefix length for IPv4: |
| 657 | * negative prefix length. |
| 658 | */ |
| 659 | @Test(expected = IllegalArgumentException.class) |
| 660 | public void testInvalidMakeNegativeMaskedAddressIPv4() { |
| 661 | IpAddress ipAddress = IpAddress.valueOf("1.2.3.5"); |
| 662 | IpAddress ipAddressMasked; |
| 663 | |
| 664 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1); |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Tests making of a masked address for invalid prefix length for IPv6: |
| 669 | * negative prefix length. |
| 670 | */ |
| 671 | @Test(expected = IllegalArgumentException.class) |
| 672 | public void testInvalidMakeNegativeMaskedAddressIPv6() { |
| 673 | IpAddress ipAddress = |
| 674 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"); |
| 675 | IpAddress ipAddressMasked; |
| 676 | |
| 677 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1); |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Tests making of a masked address for an invalid prefix length for IPv4: |
| 682 | * too long prefix length. |
| 683 | */ |
| 684 | @Test(expected = IllegalArgumentException.class) |
| 685 | public void testInvalidMakeTooLongMaskedAddressIPv4() { |
| 686 | IpAddress ipAddress = IpAddress.valueOf("1.2.3.5"); |
| 687 | IpAddress ipAddressMasked; |
| 688 | |
| 689 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 33); |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * Tests making of a masked address for an invalid prefix length for IPv6: |
| 694 | * too long prefix length. |
| 695 | */ |
| 696 | @Test(expected = IllegalArgumentException.class) |
| 697 | public void testInvalidMakeTooLongMaskedAddressIPv6() { |
| 698 | IpAddress ipAddress = |
| 699 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"); |
| 700 | IpAddress ipAddressMasked; |
| 701 | |
| 702 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 129); |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Tests comparison of {@link IpAddress} for IPv4. |
| 707 | */ |
| 708 | @Test |
| 709 | public void testComparisonIPv4() { |
| 710 | IpAddress addr1, addr2, addr3, addr4; |
| 711 | |
| 712 | addr1 = IpAddress.valueOf("1.2.3.4"); |
| 713 | addr2 = IpAddress.valueOf("1.2.3.4"); |
| 714 | addr3 = IpAddress.valueOf("1.2.3.3"); |
| 715 | addr4 = IpAddress.valueOf("1.2.3.5"); |
| 716 | assertTrue(addr1.compareTo(addr2) == 0); |
| 717 | assertTrue(addr1.compareTo(addr3) > 0); |
| 718 | assertTrue(addr1.compareTo(addr4) < 0); |
| 719 | |
| 720 | addr1 = IpAddress.valueOf("255.2.3.4"); |
| 721 | addr2 = IpAddress.valueOf("255.2.3.4"); |
| 722 | addr3 = IpAddress.valueOf("255.2.3.3"); |
| 723 | addr4 = IpAddress.valueOf("255.2.3.5"); |
| 724 | assertTrue(addr1.compareTo(addr2) == 0); |
| 725 | assertTrue(addr1.compareTo(addr3) > 0); |
| 726 | assertTrue(addr1.compareTo(addr4) < 0); |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * Tests comparison of {@link IpAddress} for IPv6. |
| 731 | */ |
| 732 | @Test |
| 733 | public void testComparisonIPv6() { |
| 734 | IpAddress addr1, addr2, addr3, addr4; |
| 735 | |
| 736 | addr1 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 737 | addr2 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 738 | addr3 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8887"); |
| 739 | addr4 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8889"); |
| 740 | assertTrue(addr1.compareTo(addr2) == 0); |
| 741 | assertTrue(addr1.compareTo(addr3) > 0); |
| 742 | assertTrue(addr1.compareTo(addr4) < 0); |
| 743 | |
| 744 | addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 745 | addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 746 | addr3 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8887"); |
| 747 | addr4 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8889"); |
| 748 | assertTrue(addr1.compareTo(addr2) == 0); |
| 749 | assertTrue(addr1.compareTo(addr3) > 0); |
| 750 | assertTrue(addr1.compareTo(addr4) < 0); |
| 751 | |
| 752 | addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 753 | addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 754 | addr3 = IpAddress.valueOf("ffff:2222:3333:4443:5555:6666:7777:8888"); |
| 755 | addr4 = IpAddress.valueOf("ffff:2222:3333:4445:5555:6666:7777:8888"); |
| 756 | assertTrue(addr1.compareTo(addr2) == 0); |
| 757 | assertTrue(addr1.compareTo(addr3) > 0); |
| 758 | assertTrue(addr1.compareTo(addr4) < 0); |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * Tests equality of {@link IpAddress} for IPv4. |
| 763 | */ |
| 764 | @Test |
| 765 | public void testEqualityIPv4() { |
Pavlin Radoslavov | 854ecab | 2014-11-03 09:43:25 -0800 | [diff] [blame] | 766 | new EqualsTester() |
| 767 | .addEqualityGroup(IpAddress.valueOf("1.2.3.4"), |
| 768 | IpAddress.valueOf("1.2.3.4")) |
| 769 | .addEqualityGroup(IpAddress.valueOf("1.2.3.5"), |
| 770 | IpAddress.valueOf("1.2.3.5")) |
| 771 | .addEqualityGroup(IpAddress.valueOf("0.0.0.0"), |
| 772 | IpAddress.valueOf("0.0.0.0")) |
| 773 | .addEqualityGroup(IpAddress.valueOf("255.255.255.255"), |
| 774 | IpAddress.valueOf("255.255.255.255")) |
| 775 | .testEquals(); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /** |
| 779 | * Tests equality of {@link IpAddress} for IPv6. |
| 780 | */ |
| 781 | @Test |
| 782 | public void testEqualityIPv6() { |
Pavlin Radoslavov | 854ecab | 2014-11-03 09:43:25 -0800 | [diff] [blame] | 783 | new EqualsTester() |
| 784 | .addEqualityGroup( |
| 785 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"), |
| 786 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888")) |
| 787 | .addEqualityGroup( |
| 788 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"), |
| 789 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a")) |
| 790 | .addEqualityGroup( |
| 791 | IpAddress.valueOf("::"), |
| 792 | IpAddress.valueOf("::")) |
| 793 | .addEqualityGroup( |
| 794 | IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), |
| 795 | IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) |
| 796 | .testEquals(); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Tests object string representation for IPv4. |
| 801 | */ |
| 802 | @Test |
| 803 | public void testToStringIPv4() { |
| 804 | IpAddress ipAddress; |
| 805 | |
| 806 | ipAddress = IpAddress.valueOf("1.2.3.4"); |
| 807 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
| 808 | |
| 809 | ipAddress = IpAddress.valueOf("0.0.0.0"); |
| 810 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 811 | |
| 812 | ipAddress = IpAddress.valueOf("255.255.255.255"); |
| 813 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * Tests object string representation for IPv6. |
| 818 | */ |
| 819 | @Test |
| 820 | public void testToStringIPv6() { |
| 821 | IpAddress ipAddress; |
| 822 | |
| 823 | ipAddress = |
| 824 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 825 | assertThat(ipAddress.toString(), |
| 826 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 827 | |
| 828 | ipAddress = IpAddress.valueOf("1111::8888"); |
| 829 | assertThat(ipAddress.toString(), is("1111::8888")); |
| 830 | |
| 831 | ipAddress = IpAddress.valueOf("1111::"); |
| 832 | assertThat(ipAddress.toString(), is("1111::")); |
| 833 | |
| 834 | ipAddress = IpAddress.valueOf("::8888"); |
| 835 | assertThat(ipAddress.toString(), is("::8888")); |
| 836 | |
| 837 | ipAddress = IpAddress.valueOf("::"); |
| 838 | assertThat(ipAddress.toString(), is("::")); |
| 839 | |
| 840 | ipAddress = |
| 841 | IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 842 | assertThat(ipAddress.toString(), |
| 843 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 844 | } |
| 845 | } |