Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 18 | import com.google.common.testing.EqualsTester; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 19 | import org.junit.Test; |
| 20 | |
| 21 | import static org.hamcrest.Matchers.equalTo; |
| 22 | import static org.hamcrest.Matchers.is; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 23 | import static org.junit.Assert.assertThat; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 24 | import static org.junit.Assert.assertFalse; |
| 25 | import static org.junit.Assert.assertTrue; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 26 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 27 | |
| 28 | /** |
| 29 | * Tests for class {@link Ip6Prefix}. |
| 30 | */ |
| 31 | public class Ip6PrefixTest { |
| 32 | /** |
| 33 | * Tests the immutability of {@link Ip6Prefix}. |
| 34 | */ |
| 35 | @Test |
| 36 | public void testImmutable() { |
| 37 | assertThatClassIsImmutable(Ip6Prefix.class); |
| 38 | } |
| 39 | |
| 40 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 41 | * Tests the IPv4 prefix address version constant. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 42 | */ |
| 43 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 44 | public void testAddressVersion() { |
| 45 | assertThat(Ip6Prefix.VERSION, is(IpAddress.Version.INET6)); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 49 | * Tests the maximum mask length. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 50 | */ |
| 51 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 52 | public void testMaxMaskLength() { |
| 53 | assertThat(Ip6Prefix.MAX_MASK_LENGTH, is(128)); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 57 | * Tests returning the IP version of the prefix. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 58 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 59 | @Test |
| 60 | public void testVersion() { |
| 61 | Ip6Prefix ipPrefix; |
| 62 | |
| 63 | // IPv6 |
| 64 | ipPrefix = Ip6Prefix.valueOf("::/0"); |
| 65 | assertThat(ipPrefix.version(), is(IpAddress.Version.INET6)); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 69 | * Tests returning the IP address value and IP address prefix length of |
| 70 | * an IPv6 prefix. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 71 | */ |
| 72 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 73 | public void testAddressAndPrefixLengthIPv6() { |
| 74 | Ip6Prefix ipPrefix; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 75 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 76 | ipPrefix = Ip6Prefix.valueOf("1100::/8"); |
| 77 | assertThat(ipPrefix.address(), equalTo(Ip6Address.valueOf("1100::"))); |
| 78 | assertThat(ipPrefix.prefixLength(), is(8)); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 79 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 80 | ipPrefix = |
| 81 | Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8"); |
| 82 | assertThat(ipPrefix.address(), equalTo(Ip6Address.valueOf("1100::"))); |
| 83 | assertThat(ipPrefix.prefixLength(), is(8)); |
| 84 | |
| 85 | ipPrefix = |
| 86 | Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8800/120"); |
| 87 | assertThat(ipPrefix.address(), |
| 88 | equalTo(Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8800"))); |
| 89 | assertThat(ipPrefix.prefixLength(), is(120)); |
| 90 | |
| 91 | ipPrefix = |
| 92 | Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/128"); |
| 93 | assertThat(ipPrefix.address(), |
| 94 | equalTo(Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"))); |
| 95 | assertThat(ipPrefix.prefixLength(), is(128)); |
| 96 | |
| 97 | ipPrefix = Ip6Prefix.valueOf("::/0"); |
| 98 | assertThat(ipPrefix.address(), equalTo(Ip6Address.valueOf("::"))); |
| 99 | assertThat(ipPrefix.prefixLength(), is(0)); |
| 100 | |
| 101 | ipPrefix = |
| 102 | Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 103 | assertThat(ipPrefix.address(), |
| 104 | equalTo(Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); |
| 105 | assertThat(ipPrefix.prefixLength(), is(128)); |
| 106 | |
| 107 | ipPrefix = |
| 108 | Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/64"); |
| 109 | assertThat(ipPrefix.address(), |
| 110 | equalTo(Ip6Address.valueOf("1111:2222:3333:4444::"))); |
| 111 | assertThat(ipPrefix.prefixLength(), is(64)); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Tests valueOf() converter for IPv6 byte array. |
| 116 | */ |
| 117 | @Test |
| 118 | public void testValueOfByteArrayIPv6() { |
| 119 | Ip6Prefix ipPrefix; |
| 120 | byte[] value; |
| 121 | |
| 122 | value = new byte[] {0x11, 0x11, 0x22, 0x22, |
| 123 | 0x33, 0x33, 0x44, 0x44, |
| 124 | 0x55, 0x55, 0x66, 0x66, |
| 125 | 0x77, 0x77, (byte) 0x88, (byte) 0x88}; |
| 126 | ipPrefix = Ip6Prefix.valueOf(value, 120); |
| 127 | assertThat(ipPrefix.toString(), |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 128 | is("1111:2222:3333:4444:5555:6666:7777:8800/120")); |
| 129 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 130 | ipPrefix = Ip6Prefix.valueOf(value, 128); |
| 131 | assertThat(ipPrefix.toString(), |
| 132 | is("1111:2222:3333:4444:5555:6666:7777:8888/128")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 133 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 134 | value = new byte[] {0x00, 0x00, 0x00, 0x00, |
| 135 | 0x00, 0x00, 0x00, 0x00, |
| 136 | 0x00, 0x00, 0x00, 0x00, |
| 137 | 0x00, 0x00, 0x00, 0x00}; |
| 138 | ipPrefix = Ip6Prefix.valueOf(value, 0); |
| 139 | assertThat(ipPrefix.toString(), is("::/0")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 140 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 141 | ipPrefix = Ip6Prefix.valueOf(value, 128); |
| 142 | assertThat(ipPrefix.toString(), is("::/128")); |
| 143 | |
| 144 | value = new byte[] {(byte) 0xff, (byte) 0xff, |
| 145 | (byte) 0xff, (byte) 0xff, |
| 146 | (byte) 0xff, (byte) 0xff, |
| 147 | (byte) 0xff, (byte) 0xff, |
| 148 | (byte) 0xff, (byte) 0xff, |
| 149 | (byte) 0xff, (byte) 0xff, |
| 150 | (byte) 0xff, (byte) 0xff, |
| 151 | (byte) 0xff, (byte) 0xff}; |
| 152 | ipPrefix = Ip6Prefix.valueOf(value, 0); |
| 153 | assertThat(ipPrefix.toString(), is("::/0")); |
| 154 | |
| 155 | ipPrefix = Ip6Prefix.valueOf(value, 64); |
| 156 | assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64")); |
| 157 | |
| 158 | ipPrefix = Ip6Prefix.valueOf(value, 128); |
| 159 | assertThat(ipPrefix.toString(), |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 160 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 164 | * Tests invalid valueOf() converter for a null array for IPv6. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 165 | */ |
| 166 | @Test(expected = NullPointerException.class) |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 167 | public void testInvalidValueOfNullArrayIPv6() { |
| 168 | Ip6Prefix ipPrefix; |
| 169 | byte[] value; |
| 170 | |
| 171 | value = null; |
| 172 | ipPrefix = Ip6Prefix.valueOf(value, 120); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 176 | * Tests invalid valueOf() converter for a short array for IPv6. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 177 | */ |
| 178 | @Test(expected = IllegalArgumentException.class) |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 179 | public void testInvalidValueOfShortArrayIPv6() { |
| 180 | Ip6Prefix ipPrefix; |
| 181 | byte[] value; |
| 182 | |
| 183 | value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 184 | ipPrefix = Ip6Prefix.valueOf(value, 120); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 188 | * Tests invalid valueOf() converter for IPv6 byte array and |
| 189 | * negative prefix length. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 190 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 191 | @Test(expected = IllegalArgumentException.class) |
| 192 | public void testInvalidValueOfByteArrayNegativePrefixLengthIPv6() { |
| 193 | Ip6Prefix ipPrefix; |
| 194 | byte[] value; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 195 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 196 | value = new byte[] {0x11, 0x11, 0x22, 0x22, |
| 197 | 0x33, 0x33, 0x44, 0x44, |
| 198 | 0x55, 0x55, 0x66, 0x66, |
| 199 | 0x77, 0x77, (byte) 0x88, (byte) 0x88}; |
| 200 | ipPrefix = Ip6Prefix.valueOf(value, -1); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 204 | * Tests invalid valueOf() converter for IPv6 byte array and |
| 205 | * too long prefix length. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 206 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 207 | @Test(expected = IllegalArgumentException.class) |
| 208 | public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv6() { |
| 209 | Ip6Prefix ipPrefix; |
| 210 | byte[] value; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 211 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 212 | value = new byte[] {0x11, 0x11, 0x22, 0x22, |
| 213 | 0x33, 0x33, 0x44, 0x44, |
| 214 | 0x55, 0x55, 0x66, 0x66, |
| 215 | 0x77, 0x77, (byte) 0x88, (byte) 0x88}; |
| 216 | ipPrefix = Ip6Prefix.valueOf(value, 129); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 220 | * Tests valueOf() converter for IPv6 address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 221 | */ |
| 222 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 223 | public void testValueOfAddressIPv6() { |
| 224 | Ip6Address ipAddress; |
| 225 | Ip6Prefix ipPrefix; |
| 226 | |
| 227 | ipAddress = |
| 228 | Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 229 | ipPrefix = Ip6Prefix.valueOf(ipAddress, 120); |
| 230 | assertThat(ipPrefix.toString(), |
| 231 | is("1111:2222:3333:4444:5555:6666:7777:8800/120")); |
| 232 | |
| 233 | ipPrefix = Ip6Prefix.valueOf(ipAddress, 128); |
| 234 | assertThat(ipPrefix.toString(), |
| 235 | is("1111:2222:3333:4444:5555:6666:7777:8888/128")); |
| 236 | |
| 237 | ipAddress = Ip6Address.valueOf("::"); |
| 238 | ipPrefix = Ip6Prefix.valueOf(ipAddress, 0); |
| 239 | assertThat(ipPrefix.toString(), is("::/0")); |
| 240 | |
| 241 | ipPrefix = Ip6Prefix.valueOf(ipAddress, 128); |
| 242 | assertThat(ipPrefix.toString(), is("::/128")); |
| 243 | |
| 244 | ipAddress = |
| 245 | Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 246 | ipPrefix = Ip6Prefix.valueOf(ipAddress, 0); |
| 247 | assertThat(ipPrefix.toString(), is("::/0")); |
| 248 | |
| 249 | ipPrefix = Ip6Prefix.valueOf(ipAddress, 64); |
| 250 | assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64")); |
| 251 | |
| 252 | ipPrefix = Ip6Prefix.valueOf(ipAddress, 128); |
| 253 | assertThat(ipPrefix.toString(), |
| 254 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 258 | * Tests invalid valueOf() converter for a null IP address. |
| 259 | */ |
| 260 | @Test(expected = NullPointerException.class) |
| 261 | public void testInvalidValueOfNullAddress() { |
| 262 | Ip6Address ipAddress; |
| 263 | Ip6Prefix ipPrefix; |
| 264 | |
| 265 | ipAddress = null; |
| 266 | ipPrefix = Ip6Prefix.valueOf(ipAddress, 24); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Tests invalid valueOf() converter for IPv6 address and |
| 271 | * negative prefix length. |
| 272 | */ |
| 273 | @Test(expected = IllegalArgumentException.class) |
| 274 | public void testInvalidValueOfAddressNegativePrefixLengthIPv6() { |
| 275 | Ip6Address ipAddress; |
| 276 | Ip6Prefix ipPrefix; |
| 277 | |
| 278 | ipAddress = |
| 279 | Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 280 | ipPrefix = Ip6Prefix.valueOf(ipAddress, -1); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Tests invalid valueOf() converter for IPv6 address and |
| 285 | * too long prefix length. |
| 286 | */ |
| 287 | @Test(expected = IllegalArgumentException.class) |
| 288 | public void testInvalidValueOfAddressTooLongPrefixLengthIPv6() { |
| 289 | Ip6Address ipAddress; |
| 290 | Ip6Prefix ipPrefix; |
| 291 | |
| 292 | ipAddress = |
| 293 | Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 294 | ipPrefix = Ip6Prefix.valueOf(ipAddress, 129); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Tests valueOf() converter for IPv6 string. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 299 | */ |
| 300 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 301 | public void testValueOfStringIPv6() { |
| 302 | Ip6Prefix ipPrefix; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 303 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 304 | ipPrefix = |
| 305 | Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/120"); |
| 306 | assertThat(ipPrefix.toString(), |
| 307 | is("1111:2222:3333:4444:5555:6666:7777:8800/120")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 308 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 309 | ipPrefix = |
| 310 | Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/128"); |
| 311 | assertThat(ipPrefix.toString(), |
| 312 | is("1111:2222:3333:4444:5555:6666:7777:8888/128")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 313 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 314 | ipPrefix = Ip6Prefix.valueOf("::/0"); |
| 315 | assertThat(ipPrefix.toString(), is("::/0")); |
| 316 | |
| 317 | ipPrefix = Ip6Prefix.valueOf("::/128"); |
| 318 | assertThat(ipPrefix.toString(), is("::/128")); |
| 319 | |
| 320 | ipPrefix = |
| 321 | Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0"); |
| 322 | assertThat(ipPrefix.toString(), is("::/0")); |
| 323 | |
| 324 | ipPrefix = |
| 325 | Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64"); |
| 326 | assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64")); |
| 327 | |
| 328 | ipPrefix = |
| 329 | Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 330 | assertThat(ipPrefix.toString(), |
| 331 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Tests invalid valueOf() converter for a null string. |
| 336 | */ |
| 337 | @Test(expected = NullPointerException.class) |
| 338 | public void testInvalidValueOfNullString() { |
| 339 | Ip6Prefix ipPrefix; |
| 340 | String fromString; |
| 341 | |
| 342 | fromString = null; |
| 343 | ipPrefix = Ip6Prefix.valueOf(fromString); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Tests invalid valueOf() converter for an empty string. |
| 348 | */ |
| 349 | @Test(expected = IllegalArgumentException.class) |
| 350 | public void testInvalidValueOfEmptyString() { |
| 351 | Ip6Prefix ipPrefix; |
| 352 | String fromString; |
| 353 | |
| 354 | fromString = ""; |
| 355 | ipPrefix = Ip6Prefix.valueOf(fromString); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Tests invalid valueOf() converter for an incorrect string. |
| 360 | */ |
| 361 | @Test(expected = IllegalArgumentException.class) |
| 362 | public void testInvalidValueOfIncorrectString() { |
| 363 | Ip6Prefix ipPrefix; |
| 364 | String fromString; |
| 365 | |
| 366 | fromString = "NoSuchIpPrefix"; |
| 367 | ipPrefix = Ip6Prefix.valueOf(fromString); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Tests invalid valueOf() converter for IPv6 string and |
| 372 | * negative prefix length. |
| 373 | */ |
| 374 | @Test(expected = IllegalArgumentException.class) |
| 375 | public void testInvalidValueOfStringNegativePrefixLengthIPv6() { |
| 376 | Ip6Prefix ipPrefix; |
| 377 | |
| 378 | ipPrefix = |
| 379 | Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/-1"); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Tests invalid valueOf() converter for IPv6 string and |
| 384 | * too long prefix length. |
| 385 | */ |
| 386 | @Test(expected = IllegalArgumentException.class) |
| 387 | public void testInvalidValueOfStringTooLongPrefixLengthIPv6() { |
| 388 | Ip6Prefix ipPrefix; |
| 389 | |
| 390 | ipPrefix = |
| 391 | Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/129"); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Tests IP prefix contains another IP prefix for IPv6. |
| 396 | */ |
| 397 | @Test |
| 398 | public void testContainsIpPrefixIPv6() { |
| 399 | Ip6Prefix ipPrefix; |
| 400 | |
| 401 | ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/120"); |
| 402 | assertTrue(ipPrefix.contains( |
| 403 | Ip6Prefix.valueOf("1111:2222:3333:4444::/120"))); |
| 404 | assertTrue(ipPrefix.contains( |
| 405 | Ip6Prefix.valueOf("1111:2222:3333:4444::/128"))); |
| 406 | assertTrue(ipPrefix.contains( |
| 407 | Ip6Prefix.valueOf("1111:2222:3333:4444::1/128"))); |
| 408 | assertFalse(ipPrefix.contains( |
| 409 | Ip6Prefix.valueOf("1111:2222:3333:4444::/64"))); |
| 410 | assertFalse(ipPrefix.contains( |
| 411 | Ip6Prefix.valueOf("1111:2222:3333:4445::/120"))); |
| 412 | assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/64"))); |
| 413 | assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/0"))); |
| 414 | assertFalse(ipPrefix.contains( |
| 415 | Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); |
| 416 | |
| 417 | ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/128"); |
| 418 | assertFalse(ipPrefix.contains( |
| 419 | Ip6Prefix.valueOf("1111:2222:3333:4444::/120"))); |
| 420 | assertTrue(ipPrefix.contains( |
| 421 | Ip6Prefix.valueOf("1111:2222:3333:4444::/128"))); |
| 422 | assertFalse(ipPrefix.contains( |
| 423 | Ip6Prefix.valueOf("1111:2222:3333:4444::1/128"))); |
| 424 | assertFalse(ipPrefix.contains( |
| 425 | Ip6Prefix.valueOf("1111:2222:3333:4444::/64"))); |
| 426 | assertFalse(ipPrefix.contains( |
| 427 | Ip6Prefix.valueOf("1111:2222:3333:4445::/120"))); |
| 428 | assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/64"))); |
| 429 | assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/0"))); |
| 430 | assertFalse(ipPrefix.contains( |
| 431 | Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); |
| 432 | |
| 433 | ipPrefix = Ip6Prefix.valueOf("::/0"); |
| 434 | assertTrue(ipPrefix.contains( |
| 435 | Ip6Prefix.valueOf("1111:2222:3333:4444::/120"))); |
| 436 | assertTrue(ipPrefix.contains( |
| 437 | Ip6Prefix.valueOf("1111:2222:3333:4444::/128"))); |
| 438 | assertTrue(ipPrefix.contains( |
| 439 | Ip6Prefix.valueOf("1111:2222:3333:4444::1/128"))); |
| 440 | assertTrue(ipPrefix.contains( |
| 441 | Ip6Prefix.valueOf("1111:2222:3333:4444::/64"))); |
| 442 | assertTrue(ipPrefix.contains( |
| 443 | Ip6Prefix.valueOf("1111:2222:3333:4445::/120"))); |
| 444 | assertTrue(ipPrefix.contains(Ip6Prefix.valueOf("::/64"))); |
| 445 | assertTrue(ipPrefix.contains(Ip6Prefix.valueOf("::/0"))); |
| 446 | assertTrue(ipPrefix.contains( |
| 447 | Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); |
| 448 | |
| 449 | ipPrefix = |
| 450 | Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 451 | assertFalse(ipPrefix.contains( |
| 452 | Ip6Prefix.valueOf("1111:2222:3333:4444::/120"))); |
| 453 | assertFalse(ipPrefix.contains( |
| 454 | Ip6Prefix.valueOf("1111:2222:3333:4444::/128"))); |
| 455 | assertFalse(ipPrefix.contains( |
| 456 | Ip6Prefix.valueOf("1111:2222:3333:4444::1/128"))); |
| 457 | assertFalse(ipPrefix.contains( |
| 458 | Ip6Prefix.valueOf("1111:2222:3333:4444::/64"))); |
| 459 | assertFalse(ipPrefix.contains( |
| 460 | Ip6Prefix.valueOf("1111:2222:3333:4445::/120"))); |
| 461 | assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/64"))); |
| 462 | assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/0"))); |
| 463 | assertTrue(ipPrefix.contains( |
| 464 | Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Tests IP prefix contains IP address for IPv6. |
| 469 | */ |
| 470 | @Test |
| 471 | public void testContainsIpAddressIPv6() { |
| 472 | Ip6Prefix ipPrefix; |
| 473 | |
| 474 | ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/120"); |
| 475 | assertTrue(ipPrefix.contains( |
| 476 | Ip6Address.valueOf("1111:2222:3333:4444::"))); |
| 477 | assertTrue(ipPrefix.contains( |
| 478 | Ip6Address.valueOf("1111:2222:3333:4444::1"))); |
| 479 | assertFalse(ipPrefix.contains( |
| 480 | Ip6Address.valueOf("1111:2222:3333:4445::"))); |
| 481 | assertFalse(ipPrefix.contains(Ip6Address.valueOf("::"))); |
| 482 | assertFalse(ipPrefix.contains( |
| 483 | Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); |
| 484 | |
| 485 | ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/128"); |
| 486 | assertTrue(ipPrefix.contains( |
| 487 | Ip6Address.valueOf("1111:2222:3333:4444::"))); |
| 488 | assertFalse(ipPrefix.contains( |
| 489 | Ip6Address.valueOf("1111:2222:3333:4444::1"))); |
| 490 | assertFalse(ipPrefix.contains( |
| 491 | Ip6Address.valueOf("1111:2222:3333:4445::"))); |
| 492 | assertFalse(ipPrefix.contains(Ip6Address.valueOf("::"))); |
| 493 | assertFalse(ipPrefix.contains( |
| 494 | Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); |
| 495 | |
| 496 | ipPrefix = Ip6Prefix.valueOf("::/0"); |
| 497 | assertTrue(ipPrefix.contains( |
| 498 | Ip6Address.valueOf("1111:2222:3333:4444::"))); |
| 499 | assertTrue(ipPrefix.contains( |
| 500 | Ip6Address.valueOf("1111:2222:3333:4444::1"))); |
| 501 | assertTrue(ipPrefix.contains( |
| 502 | Ip6Address.valueOf("1111:2222:3333:4445::"))); |
| 503 | assertTrue(ipPrefix.contains(Ip6Address.valueOf("::"))); |
| 504 | assertTrue(ipPrefix.contains( |
| 505 | Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); |
| 506 | |
| 507 | ipPrefix = |
| 508 | Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 509 | assertFalse(ipPrefix.contains( |
| 510 | Ip6Address.valueOf("1111:2222:3333:4444::"))); |
| 511 | assertFalse(ipPrefix.contains( |
| 512 | Ip6Address.valueOf("1111:2222:3333:4444::1"))); |
| 513 | assertFalse(ipPrefix.contains( |
| 514 | Ip6Address.valueOf("1111:2222:3333:4445::"))); |
| 515 | assertFalse(ipPrefix.contains(Ip6Address.valueOf("::"))); |
| 516 | assertTrue(ipPrefix.contains( |
| 517 | Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Tests equality of {@link Ip6Prefix} for IPv6. |
| 522 | */ |
| 523 | @Test |
| 524 | public void testEqualityIPv6() { |
| 525 | new EqualsTester() |
| 526 | .addEqualityGroup( |
| 527 | Ip6Prefix.valueOf("1111:2222:3333:4444::/120"), |
| 528 | Ip6Prefix.valueOf("1111:2222:3333:4444::1/120"), |
| 529 | Ip6Prefix.valueOf("1111:2222:3333:4444::/120")) |
| 530 | .addEqualityGroup( |
| 531 | Ip6Prefix.valueOf("1111:2222:3333:4444::/64"), |
| 532 | Ip6Prefix.valueOf("1111:2222:3333:4444::/64")) |
| 533 | .addEqualityGroup( |
| 534 | Ip6Prefix.valueOf("1111:2222:3333:4444::/128"), |
| 535 | Ip6Prefix.valueOf("1111:2222:3333:4444::/128")) |
| 536 | .addEqualityGroup( |
| 537 | Ip6Prefix.valueOf("1111:2222:3333:4445::/64"), |
| 538 | Ip6Prefix.valueOf("1111:2222:3333:4445::/64")) |
| 539 | .addEqualityGroup( |
| 540 | Ip6Prefix.valueOf("::/0"), |
| 541 | Ip6Prefix.valueOf("::/0")) |
| 542 | .addEqualityGroup( |
| 543 | Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"), |
| 544 | Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")) |
| 545 | .testEquals(); |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * Tests object string representation for IPv6. |
| 550 | */ |
| 551 | @Test |
| 552 | public void testToStringIPv6() { |
| 553 | Ip6Prefix ipPrefix; |
| 554 | |
| 555 | ipPrefix = Ip6Prefix.valueOf("1100::/8"); |
| 556 | assertThat(ipPrefix.toString(), is("1100::/8")); |
| 557 | |
| 558 | ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8"); |
| 559 | assertThat(ipPrefix.toString(), is("1100::/8")); |
| 560 | |
| 561 | ipPrefix = Ip6Prefix.valueOf("::/0"); |
| 562 | assertThat(ipPrefix.toString(), is("::/0")); |
| 563 | |
| 564 | ipPrefix = Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 565 | assertThat(ipPrefix.toString(), |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 566 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); |
| 567 | } |
| 568 | } |