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 | */ |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 18 | import org.junit.Test; |
| 19 | |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 20 | import com.google.common.testing.EqualsTester; |
| 21 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 22 | import static org.hamcrest.Matchers.equalTo; |
| 23 | import static org.hamcrest.Matchers.is; |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 24 | import static org.junit.Assert.assertFalse; |
Pavlin Radoslavov | 34c8164 | 2014-11-04 16:21:38 -0800 | [diff] [blame] | 25 | import static org.junit.Assert.assertNull; |
| 26 | import static org.junit.Assert.assertThat; |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 27 | import static org.junit.Assert.assertTrue; |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 28 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 29 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 30 | /** |
| 31 | * Tests for class {@link IpPrefix}. |
| 32 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 33 | public class IpPrefixTest { |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 34 | /** |
| 35 | * Tests the immutability of {@link IpPrefix}. |
| 36 | */ |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 37 | @Test |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 38 | public void testImmutable() { |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 39 | assertThatClassIsImmutableBaseClass(IpPrefix.class); |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 42 | /** |
| 43 | * Tests the maximum mask length. |
| 44 | */ |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 45 | @Test |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 46 | public void testMaxMaskLength() { |
| 47 | assertThat(IpPrefix.MAX_INET_MASK_LENGTH, is(32)); |
| 48 | assertThat(IpPrefix.MAX_INET6_MASK_LENGTH, is(128)); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Tests returning the IP version of the prefix. |
| 53 | */ |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 54 | @Test |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 55 | public void testVersion() { |
| 56 | IpPrefix ipPrefix; |
| 57 | |
| 58 | // IPv4 |
| 59 | ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); |
| 60 | assertThat(ipPrefix.version(), is(IpAddress.Version.INET)); |
| 61 | |
| 62 | // IPv6 |
| 63 | ipPrefix = IpPrefix.valueOf("::/0"); |
| 64 | assertThat(ipPrefix.version(), is(IpAddress.Version.INET6)); |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 65 | } |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 66 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Tests returning the IP address value and IP address prefix length of |
| 69 | * an IPv4 prefix. |
| 70 | */ |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 71 | @Test |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 72 | public void testAddressAndPrefixLengthIPv4() { |
| 73 | IpPrefix ipPrefix; |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 74 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 75 | ipPrefix = IpPrefix.valueOf("1.2.3.0/24"); |
| 76 | assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.0"))); |
| 77 | assertThat(ipPrefix.prefixLength(), is(24)); |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 78 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 79 | ipPrefix = IpPrefix.valueOf("1.2.3.4/24"); |
| 80 | assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.0"))); |
| 81 | assertThat(ipPrefix.prefixLength(), is(24)); |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 82 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 83 | ipPrefix = IpPrefix.valueOf("1.2.3.4/32"); |
| 84 | assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.4"))); |
| 85 | assertThat(ipPrefix.prefixLength(), is(32)); |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 86 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 87 | ipPrefix = IpPrefix.valueOf("1.2.3.5/32"); |
| 88 | assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.5"))); |
| 89 | assertThat(ipPrefix.prefixLength(), is(32)); |
| 90 | |
| 91 | ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); |
| 92 | assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("0.0.0.0"))); |
| 93 | assertThat(ipPrefix.prefixLength(), is(0)); |
| 94 | |
| 95 | ipPrefix = IpPrefix.valueOf("255.255.255.255/32"); |
| 96 | assertThat(ipPrefix.address(), |
| 97 | equalTo(IpAddress.valueOf("255.255.255.255"))); |
| 98 | assertThat(ipPrefix.prefixLength(), is(32)); |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 99 | } |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 100 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 101 | /** |
| 102 | * Tests returning the IP address value and IP address prefix length of |
| 103 | * an IPv6 prefix. |
| 104 | */ |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 105 | @Test |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 106 | public void testAddressAndPrefixLengthIPv6() { |
| 107 | IpPrefix ipPrefix; |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 108 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 109 | ipPrefix = IpPrefix.valueOf("1100::/8"); |
| 110 | assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1100::"))); |
| 111 | assertThat(ipPrefix.prefixLength(), is(8)); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 112 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 113 | ipPrefix = |
| 114 | IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8"); |
| 115 | assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1100::"))); |
| 116 | assertThat(ipPrefix.prefixLength(), is(8)); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 117 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 118 | ipPrefix = |
| 119 | IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8800/120"); |
| 120 | assertThat(ipPrefix.address(), |
| 121 | equalTo(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8800"))); |
| 122 | assertThat(ipPrefix.prefixLength(), is(120)); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 123 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 124 | ipPrefix = |
| 125 | IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/128"); |
| 126 | assertThat(ipPrefix.address(), |
| 127 | equalTo(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"))); |
| 128 | assertThat(ipPrefix.prefixLength(), is(128)); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 129 | |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 130 | ipPrefix = IpPrefix.valueOf("::/0"); |
| 131 | assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("::"))); |
| 132 | assertThat(ipPrefix.prefixLength(), is(0)); |
| 133 | |
| 134 | ipPrefix = |
| 135 | IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 136 | assertThat(ipPrefix.address(), |
| 137 | equalTo(IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); |
| 138 | assertThat(ipPrefix.prefixLength(), is(128)); |
| 139 | |
| 140 | ipPrefix = |
| 141 | IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/64"); |
| 142 | assertThat(ipPrefix.address(), |
| 143 | equalTo(IpAddress.valueOf("1111:2222:3333:4444::"))); |
| 144 | assertThat(ipPrefix.prefixLength(), is(64)); |
| 145 | } |
| 146 | |
| 147 | /** |
Pavlin Radoslavov | 34c8164 | 2014-11-04 16:21:38 -0800 | [diff] [blame] | 148 | * Tests getting the Ip4Prefix and Ip6Prefix view of the IP prefix. |
| 149 | */ |
| 150 | @Test |
| 151 | public void testGetIp4AndIp6PrefixView() { |
| 152 | IpPrefix ipPrefix; |
| 153 | Ip4Prefix ip4Prefix; |
| 154 | Ip6Prefix ip6Prefix; |
| 155 | |
| 156 | // Pure IPv4 IpPrefix |
| 157 | ipPrefix = IpPrefix.valueOf("1.2.3.0/24"); |
| 158 | ip4Prefix = ipPrefix.getIp4Prefix(); |
| 159 | ip6Prefix = ipPrefix.getIp6Prefix(); |
| 160 | assertThat(ip4Prefix.toString(), is("1.2.3.0/24")); |
| 161 | assertNull(ip6Prefix); |
| 162 | |
| 163 | // IPv4 IpPrefix that is Ip4Prefix |
| 164 | ipPrefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 165 | ip4Prefix = ipPrefix.getIp4Prefix(); |
| 166 | ip6Prefix = ipPrefix.getIp6Prefix(); |
| 167 | assertThat(ip4Prefix.toString(), is("1.2.3.0/24")); |
| 168 | assertNull(ip6Prefix); |
| 169 | |
| 170 | // Pure IPv6 IpPrefix |
| 171 | ipPrefix = IpPrefix.valueOf("1111:2222::/64"); |
| 172 | ip4Prefix = ipPrefix.getIp4Prefix(); |
| 173 | ip6Prefix = ipPrefix.getIp6Prefix(); |
| 174 | assertNull(ip4Prefix); |
| 175 | assertThat(ip6Prefix.toString(), is("1111:2222::/64")); |
| 176 | |
| 177 | // IPv6 IpPrefix that is Ip6Prefix |
| 178 | ipPrefix = Ip6Prefix.valueOf("1111:2222::/64"); |
| 179 | ip4Prefix = ipPrefix.getIp4Prefix(); |
| 180 | ip6Prefix = ipPrefix.getIp6Prefix(); |
| 181 | assertNull(ip4Prefix); |
| 182 | assertThat(ip6Prefix.toString(), is("1111:2222::/64")); |
| 183 | } |
| 184 | |
| 185 | /** |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 186 | * Tests valueOf() converter for IPv4 integer value. |
| 187 | */ |
| 188 | @Test |
| 189 | public void testValueOfForIntegerIPv4() { |
| 190 | IpPrefix ipPrefix; |
| 191 | |
| 192 | ipPrefix = IpPrefix.valueOf(0x01020304, 24); |
| 193 | assertThat(ipPrefix.toString(), is("1.2.3.0/24")); |
| 194 | |
| 195 | ipPrefix = IpPrefix.valueOf(0x01020304, 32); |
| 196 | assertThat(ipPrefix.toString(), is("1.2.3.4/32")); |
| 197 | |
| 198 | ipPrefix = IpPrefix.valueOf(0x01020305, 32); |
| 199 | assertThat(ipPrefix.toString(), is("1.2.3.5/32")); |
| 200 | |
| 201 | ipPrefix = IpPrefix.valueOf(0, 0); |
| 202 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 203 | |
| 204 | ipPrefix = IpPrefix.valueOf(0, 32); |
| 205 | assertThat(ipPrefix.toString(), is("0.0.0.0/32")); |
| 206 | |
| 207 | ipPrefix = IpPrefix.valueOf(0xffffffff, 0); |
| 208 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 209 | |
| 210 | ipPrefix = IpPrefix.valueOf(0xffffffff, 16); |
| 211 | assertThat(ipPrefix.toString(), is("255.255.0.0/16")); |
| 212 | |
| 213 | ipPrefix = IpPrefix.valueOf(0xffffffff, 32); |
| 214 | assertThat(ipPrefix.toString(), is("255.255.255.255/32")); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Tests invalid valueOf() converter for IPv4 integer value and |
| 219 | * negative prefix length. |
| 220 | */ |
| 221 | @Test(expected = IllegalArgumentException.class) |
| 222 | public void testInvalidValueOfIntegerNegativePrefixLengthIPv4() { |
| 223 | IpPrefix ipPrefix; |
| 224 | |
| 225 | ipPrefix = IpPrefix.valueOf(0x01020304, -1); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Tests invalid valueOf() converter for IPv4 integer value and |
| 230 | * too long prefix length. |
| 231 | */ |
| 232 | @Test(expected = IllegalArgumentException.class) |
| 233 | public void testInvalidValueOfIntegerTooLongPrefixLengthIPv4() { |
| 234 | IpPrefix ipPrefix; |
| 235 | |
| 236 | ipPrefix = IpPrefix.valueOf(0x01020304, 33); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Tests valueOf() converter for IPv4 byte array. |
| 241 | */ |
| 242 | @Test |
| 243 | public void testValueOfByteArrayIPv4() { |
| 244 | IpPrefix ipPrefix; |
| 245 | byte[] value; |
| 246 | |
| 247 | value = new byte[] {1, 2, 3, 4}; |
| 248 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 24); |
| 249 | assertThat(ipPrefix.toString(), is("1.2.3.0/24")); |
| 250 | |
| 251 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32); |
| 252 | assertThat(ipPrefix.toString(), is("1.2.3.4/32")); |
| 253 | |
| 254 | value = new byte[] {1, 2, 3, 5}; |
| 255 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32); |
| 256 | assertThat(ipPrefix.toString(), is("1.2.3.5/32")); |
| 257 | |
| 258 | value = new byte[] {0, 0, 0, 0}; |
| 259 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 0); |
| 260 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 261 | |
| 262 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32); |
| 263 | assertThat(ipPrefix.toString(), is("0.0.0.0/32")); |
| 264 | |
| 265 | value = new byte[] {(byte) 0xff, (byte) 0xff, |
| 266 | (byte) 0xff, (byte) 0xff}; |
| 267 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 0); |
| 268 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 269 | |
| 270 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 16); |
| 271 | assertThat(ipPrefix.toString(), is("255.255.0.0/16")); |
| 272 | |
| 273 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32); |
| 274 | assertThat(ipPrefix.toString(), is("255.255.255.255/32")); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Tests valueOf() converter for IPv6 byte array. |
| 279 | */ |
| 280 | @Test |
| 281 | public void testValueOfByteArrayIPv6() { |
| 282 | IpPrefix ipPrefix; |
| 283 | byte[] value; |
| 284 | |
| 285 | value = new byte[] {0x11, 0x11, 0x22, 0x22, |
| 286 | 0x33, 0x33, 0x44, 0x44, |
| 287 | 0x55, 0x55, 0x66, 0x66, |
| 288 | 0x77, 0x77, (byte) 0x88, (byte) 0x88}; |
| 289 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 120); |
| 290 | assertThat(ipPrefix.toString(), |
| 291 | is("1111:2222:3333:4444:5555:6666:7777:8800/120")); |
| 292 | |
| 293 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 128); |
| 294 | assertThat(ipPrefix.toString(), |
| 295 | is("1111:2222:3333:4444:5555:6666:7777:8888/128")); |
| 296 | |
| 297 | value = new byte[] {0x00, 0x00, 0x00, 0x00, |
| 298 | 0x00, 0x00, 0x00, 0x00, |
| 299 | 0x00, 0x00, 0x00, 0x00, |
| 300 | 0x00, 0x00, 0x00, 0x00}; |
| 301 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 0); |
| 302 | assertThat(ipPrefix.toString(), is("::/0")); |
| 303 | |
| 304 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 128); |
| 305 | assertThat(ipPrefix.toString(), is("::/128")); |
| 306 | |
| 307 | value = new byte[] {(byte) 0xff, (byte) 0xff, |
| 308 | (byte) 0xff, (byte) 0xff, |
| 309 | (byte) 0xff, (byte) 0xff, |
| 310 | (byte) 0xff, (byte) 0xff, |
| 311 | (byte) 0xff, (byte) 0xff, |
| 312 | (byte) 0xff, (byte) 0xff, |
| 313 | (byte) 0xff, (byte) 0xff, |
| 314 | (byte) 0xff, (byte) 0xff}; |
| 315 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 0); |
| 316 | assertThat(ipPrefix.toString(), is("::/0")); |
| 317 | |
| 318 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 64); |
| 319 | assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64")); |
| 320 | |
| 321 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 128); |
| 322 | assertThat(ipPrefix.toString(), |
| 323 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Tests invalid valueOf() converter for a null array for IPv4. |
| 328 | */ |
| 329 | @Test(expected = NullPointerException.class) |
| 330 | public void testInvalidValueOfNullArrayIPv4() { |
| 331 | IpPrefix ipPrefix; |
| 332 | byte[] value; |
| 333 | |
| 334 | value = null; |
| 335 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 24); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Tests invalid valueOf() converter for a null array for IPv6. |
| 340 | */ |
| 341 | @Test(expected = NullPointerException.class) |
| 342 | public void testInvalidValueOfNullArrayIPv6() { |
| 343 | IpPrefix ipPrefix; |
| 344 | byte[] value; |
| 345 | |
| 346 | value = null; |
| 347 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 120); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Tests invalid valueOf() converter for a short array for IPv4. |
| 352 | */ |
| 353 | @Test(expected = IllegalArgumentException.class) |
| 354 | public void testInvalidValueOfShortArrayIPv4() { |
| 355 | IpPrefix ipPrefix; |
| 356 | byte[] value; |
| 357 | |
| 358 | value = new byte[] {1, 2, 3}; |
| 359 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 24); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Tests invalid valueOf() converter for a short array for IPv6. |
| 364 | */ |
| 365 | @Test(expected = IllegalArgumentException.class) |
| 366 | public void testInvalidValueOfShortArrayIPv6() { |
| 367 | IpPrefix ipPrefix; |
| 368 | byte[] value; |
| 369 | |
| 370 | value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 371 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 120); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Tests invalid valueOf() converter for IPv4 byte array and |
| 376 | * negative prefix length. |
| 377 | */ |
| 378 | @Test(expected = IllegalArgumentException.class) |
| 379 | public void testInvalidValueOfByteArrayNegativePrefixLengthIPv4() { |
| 380 | IpPrefix ipPrefix; |
| 381 | byte[] value; |
| 382 | |
| 383 | value = new byte[] {1, 2, 3, 4}; |
| 384 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, -1); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Tests invalid valueOf() converter for IPv6 byte array and |
| 389 | * negative prefix length. |
| 390 | */ |
| 391 | @Test(expected = IllegalArgumentException.class) |
| 392 | public void testInvalidValueOfByteArrayNegativePrefixLengthIPv6() { |
| 393 | IpPrefix ipPrefix; |
| 394 | byte[] value; |
| 395 | |
| 396 | value = new byte[] {0x11, 0x11, 0x22, 0x22, |
| 397 | 0x33, 0x33, 0x44, 0x44, |
| 398 | 0x55, 0x55, 0x66, 0x66, |
| 399 | 0x77, 0x77, (byte) 0x88, (byte) 0x88}; |
| 400 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, -1); |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Tests invalid valueOf() converter for IPv4 byte array and |
| 405 | * too long prefix length. |
| 406 | */ |
| 407 | @Test(expected = IllegalArgumentException.class) |
| 408 | public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv4() { |
| 409 | IpPrefix ipPrefix; |
| 410 | byte[] value; |
| 411 | |
| 412 | value = new byte[] {1, 2, 3, 4}; |
| 413 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 33); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Tests invalid valueOf() converter for IPv6 byte array and |
| 418 | * too long prefix length. |
| 419 | */ |
| 420 | @Test(expected = IllegalArgumentException.class) |
| 421 | public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv6() { |
| 422 | IpPrefix ipPrefix; |
| 423 | byte[] value; |
| 424 | |
| 425 | value = new byte[] {0x11, 0x11, 0x22, 0x22, |
| 426 | 0x33, 0x33, 0x44, 0x44, |
| 427 | 0x55, 0x55, 0x66, 0x66, |
| 428 | 0x77, 0x77, (byte) 0x88, (byte) 0x88}; |
| 429 | ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 129); |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Tests valueOf() converter for IPv4 address. |
| 434 | */ |
| 435 | @Test |
| 436 | public void testValueOfAddressIPv4() { |
| 437 | IpAddress ipAddress; |
| 438 | IpPrefix ipPrefix; |
| 439 | |
| 440 | ipAddress = IpAddress.valueOf("1.2.3.4"); |
| 441 | ipPrefix = IpPrefix.valueOf(ipAddress, 24); |
| 442 | assertThat(ipPrefix.toString(), is("1.2.3.0/24")); |
| 443 | |
| 444 | ipPrefix = IpPrefix.valueOf(ipAddress, 32); |
| 445 | assertThat(ipPrefix.toString(), is("1.2.3.4/32")); |
| 446 | |
| 447 | ipAddress = IpAddress.valueOf("1.2.3.5"); |
| 448 | ipPrefix = IpPrefix.valueOf(ipAddress, 32); |
| 449 | assertThat(ipPrefix.toString(), is("1.2.3.5/32")); |
| 450 | |
| 451 | ipAddress = IpAddress.valueOf("0.0.0.0"); |
| 452 | ipPrefix = IpPrefix.valueOf(ipAddress, 0); |
| 453 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 454 | |
| 455 | ipPrefix = IpPrefix.valueOf(ipAddress, 32); |
| 456 | assertThat(ipPrefix.toString(), is("0.0.0.0/32")); |
| 457 | |
| 458 | ipAddress = IpAddress.valueOf("255.255.255.255"); |
| 459 | ipPrefix = IpPrefix.valueOf(ipAddress, 0); |
| 460 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 461 | |
| 462 | ipPrefix = IpPrefix.valueOf(ipAddress, 16); |
| 463 | assertThat(ipPrefix.toString(), is("255.255.0.0/16")); |
| 464 | |
| 465 | ipPrefix = IpPrefix.valueOf(ipAddress, 32); |
| 466 | assertThat(ipPrefix.toString(), is("255.255.255.255/32")); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Tests valueOf() converter for IPv6 address. |
| 471 | */ |
| 472 | @Test |
| 473 | public void testValueOfAddressIPv6() { |
| 474 | IpAddress ipAddress; |
| 475 | IpPrefix ipPrefix; |
| 476 | |
| 477 | ipAddress = |
| 478 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 479 | ipPrefix = IpPrefix.valueOf(ipAddress, 120); |
| 480 | assertThat(ipPrefix.toString(), |
| 481 | is("1111:2222:3333:4444:5555:6666:7777:8800/120")); |
| 482 | |
| 483 | ipPrefix = IpPrefix.valueOf(ipAddress, 128); |
| 484 | assertThat(ipPrefix.toString(), |
| 485 | is("1111:2222:3333:4444:5555:6666:7777:8888/128")); |
| 486 | |
| 487 | ipAddress = IpAddress.valueOf("::"); |
| 488 | ipPrefix = IpPrefix.valueOf(ipAddress, 0); |
| 489 | assertThat(ipPrefix.toString(), is("::/0")); |
| 490 | |
| 491 | ipPrefix = IpPrefix.valueOf(ipAddress, 128); |
| 492 | assertThat(ipPrefix.toString(), is("::/128")); |
| 493 | |
| 494 | ipAddress = |
| 495 | IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 496 | ipPrefix = IpPrefix.valueOf(ipAddress, 0); |
| 497 | assertThat(ipPrefix.toString(), is("::/0")); |
| 498 | |
| 499 | ipPrefix = IpPrefix.valueOf(ipAddress, 64); |
| 500 | assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64")); |
| 501 | |
| 502 | ipPrefix = IpPrefix.valueOf(ipAddress, 128); |
| 503 | assertThat(ipPrefix.toString(), |
| 504 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Tests invalid valueOf() converter for a null IP address. |
| 509 | */ |
| 510 | @Test(expected = NullPointerException.class) |
| 511 | public void testInvalidValueOfNullAddress() { |
| 512 | IpAddress ipAddress; |
| 513 | IpPrefix ipPrefix; |
| 514 | |
| 515 | ipAddress = null; |
| 516 | ipPrefix = IpPrefix.valueOf(ipAddress, 24); |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Tests invalid valueOf() converter for IPv4 address and |
| 521 | * negative prefix length. |
| 522 | */ |
| 523 | @Test(expected = IllegalArgumentException.class) |
| 524 | public void testInvalidValueOfAddressNegativePrefixLengthIPv4() { |
| 525 | IpAddress ipAddress; |
| 526 | IpPrefix ipPrefix; |
| 527 | |
| 528 | ipAddress = IpAddress.valueOf("1.2.3.4"); |
| 529 | ipPrefix = IpPrefix.valueOf(ipAddress, -1); |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Tests invalid valueOf() converter for IPv6 address and |
| 534 | * negative prefix length. |
| 535 | */ |
| 536 | @Test(expected = IllegalArgumentException.class) |
| 537 | public void testInvalidValueOfAddressNegativePrefixLengthIPv6() { |
| 538 | IpAddress ipAddress; |
| 539 | IpPrefix ipPrefix; |
| 540 | |
| 541 | ipAddress = |
| 542 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 543 | ipPrefix = IpPrefix.valueOf(ipAddress, -1); |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * Tests invalid valueOf() converter for IPv4 address and |
| 548 | * too long prefix length. |
| 549 | */ |
| 550 | @Test(expected = IllegalArgumentException.class) |
| 551 | public void testInvalidValueOfAddressTooLongPrefixLengthIPv4() { |
| 552 | IpAddress ipAddress; |
| 553 | IpPrefix ipPrefix; |
| 554 | |
| 555 | ipAddress = IpAddress.valueOf("1.2.3.4"); |
| 556 | ipPrefix = IpPrefix.valueOf(ipAddress, 33); |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Tests invalid valueOf() converter for IPv6 address and |
| 561 | * too long prefix length. |
| 562 | */ |
| 563 | @Test(expected = IllegalArgumentException.class) |
| 564 | public void testInvalidValueOfAddressTooLongPrefixLengthIPv6() { |
| 565 | IpAddress ipAddress; |
| 566 | IpPrefix ipPrefix; |
| 567 | |
| 568 | ipAddress = |
| 569 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 570 | ipPrefix = IpPrefix.valueOf(ipAddress, 129); |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Tests valueOf() converter for IPv4 string. |
| 575 | */ |
| 576 | @Test |
| 577 | public void testValueOfStringIPv4() { |
| 578 | IpPrefix ipPrefix; |
| 579 | |
| 580 | ipPrefix = IpPrefix.valueOf("1.2.3.4/24"); |
| 581 | assertThat(ipPrefix.toString(), is("1.2.3.0/24")); |
| 582 | |
| 583 | ipPrefix = IpPrefix.valueOf("1.2.3.4/32"); |
| 584 | assertThat(ipPrefix.toString(), is("1.2.3.4/32")); |
| 585 | |
| 586 | ipPrefix = IpPrefix.valueOf("1.2.3.5/32"); |
| 587 | assertThat(ipPrefix.toString(), is("1.2.3.5/32")); |
| 588 | |
| 589 | ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); |
| 590 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 591 | |
| 592 | ipPrefix = IpPrefix.valueOf("0.0.0.0/32"); |
| 593 | assertThat(ipPrefix.toString(), is("0.0.0.0/32")); |
| 594 | |
| 595 | ipPrefix = IpPrefix.valueOf("255.255.255.255/0"); |
| 596 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 597 | |
| 598 | ipPrefix = IpPrefix.valueOf("255.255.255.255/16"); |
| 599 | assertThat(ipPrefix.toString(), is("255.255.0.0/16")); |
| 600 | |
| 601 | ipPrefix = IpPrefix.valueOf("255.255.255.255/32"); |
| 602 | assertThat(ipPrefix.toString(), is("255.255.255.255/32")); |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Tests valueOf() converter for IPv6 string. |
| 607 | */ |
| 608 | @Test |
| 609 | public void testValueOfStringIPv6() { |
| 610 | IpPrefix ipPrefix; |
| 611 | |
| 612 | ipPrefix = |
| 613 | IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/120"); |
| 614 | assertThat(ipPrefix.toString(), |
| 615 | is("1111:2222:3333:4444:5555:6666:7777:8800/120")); |
| 616 | |
| 617 | ipPrefix = |
| 618 | IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/128"); |
| 619 | assertThat(ipPrefix.toString(), |
| 620 | is("1111:2222:3333:4444:5555:6666:7777:8888/128")); |
| 621 | |
| 622 | ipPrefix = IpPrefix.valueOf("::/0"); |
| 623 | assertThat(ipPrefix.toString(), is("::/0")); |
| 624 | |
| 625 | ipPrefix = IpPrefix.valueOf("::/128"); |
| 626 | assertThat(ipPrefix.toString(), is("::/128")); |
| 627 | |
| 628 | ipPrefix = |
| 629 | IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0"); |
| 630 | assertThat(ipPrefix.toString(), is("::/0")); |
| 631 | |
| 632 | ipPrefix = |
| 633 | IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64"); |
| 634 | assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64")); |
| 635 | |
| 636 | ipPrefix = |
| 637 | IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 638 | assertThat(ipPrefix.toString(), |
| 639 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Tests invalid valueOf() converter for a null string. |
| 644 | */ |
| 645 | @Test(expected = NullPointerException.class) |
| 646 | public void testInvalidValueOfNullString() { |
| 647 | IpPrefix ipPrefix; |
| 648 | String fromString; |
| 649 | |
| 650 | fromString = null; |
| 651 | ipPrefix = IpPrefix.valueOf(fromString); |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Tests invalid valueOf() converter for an empty string. |
| 656 | */ |
| 657 | @Test(expected = IllegalArgumentException.class) |
| 658 | public void testInvalidValueOfEmptyString() { |
| 659 | IpPrefix ipPrefix; |
| 660 | String fromString; |
| 661 | |
| 662 | fromString = ""; |
| 663 | ipPrefix = IpPrefix.valueOf(fromString); |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * Tests invalid valueOf() converter for an incorrect string. |
| 668 | */ |
| 669 | @Test(expected = IllegalArgumentException.class) |
| 670 | public void testInvalidValueOfIncorrectString() { |
| 671 | IpPrefix ipPrefix; |
| 672 | String fromString; |
| 673 | |
| 674 | fromString = "NoSuchIpPrefix"; |
| 675 | ipPrefix = IpPrefix.valueOf(fromString); |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * Tests invalid valueOf() converter for IPv4 string and |
| 680 | * negative prefix length. |
| 681 | */ |
| 682 | @Test(expected = IllegalArgumentException.class) |
| 683 | public void testInvalidValueOfStringNegativePrefixLengthIPv4() { |
| 684 | IpPrefix ipPrefix; |
| 685 | |
| 686 | ipPrefix = IpPrefix.valueOf("1.2.3.4/-1"); |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Tests invalid valueOf() converter for IPv6 string and |
| 691 | * negative prefix length. |
| 692 | */ |
| 693 | @Test(expected = IllegalArgumentException.class) |
| 694 | public void testInvalidValueOfStringNegativePrefixLengthIPv6() { |
| 695 | IpPrefix ipPrefix; |
| 696 | |
| 697 | ipPrefix = |
| 698 | IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/-1"); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Tests invalid valueOf() converter for IPv4 string and |
| 703 | * too long prefix length. |
| 704 | */ |
| 705 | @Test(expected = IllegalArgumentException.class) |
| 706 | public void testInvalidValueOfStringTooLongPrefixLengthIPv4() { |
| 707 | IpPrefix ipPrefix; |
| 708 | |
| 709 | ipPrefix = IpPrefix.valueOf("1.2.3.4/33"); |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * Tests invalid valueOf() converter for IPv6 string and |
| 714 | * too long prefix length. |
| 715 | */ |
| 716 | @Test(expected = IllegalArgumentException.class) |
| 717 | public void testInvalidValueOfStringTooLongPrefixLengthIPv6() { |
| 718 | IpPrefix ipPrefix; |
| 719 | |
| 720 | ipPrefix = |
| 721 | IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/129"); |
| 722 | } |
| 723 | |
| 724 | /** |
| 725 | * Tests IP prefix contains another IP prefix for IPv4. |
| 726 | */ |
| 727 | @Test |
| 728 | public void testContainsIpPrefixIPv4() { |
| 729 | IpPrefix ipPrefix; |
| 730 | |
| 731 | ipPrefix = IpPrefix.valueOf("1.2.0.0/24"); |
| 732 | assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24"))); |
| 733 | assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32"))); |
| 734 | assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32"))); |
| 735 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16"))); |
| 736 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24"))); |
| 737 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16"))); |
| 738 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0"))); |
| 739 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32"))); |
| 740 | |
| 741 | ipPrefix = IpPrefix.valueOf("1.2.0.0/32"); |
| 742 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24"))); |
| 743 | assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32"))); |
| 744 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32"))); |
| 745 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16"))); |
| 746 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24"))); |
| 747 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16"))); |
| 748 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0"))); |
| 749 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32"))); |
| 750 | |
| 751 | ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); |
| 752 | assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24"))); |
| 753 | assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32"))); |
| 754 | assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32"))); |
| 755 | assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16"))); |
| 756 | assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24"))); |
| 757 | assertTrue(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16"))); |
| 758 | assertTrue(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0"))); |
| 759 | assertTrue(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32"))); |
| 760 | |
| 761 | ipPrefix = IpPrefix.valueOf("255.255.255.255/32"); |
| 762 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24"))); |
| 763 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32"))); |
| 764 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32"))); |
| 765 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16"))); |
| 766 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24"))); |
| 767 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16"))); |
| 768 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0"))); |
| 769 | assertTrue(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32"))); |
Pavlin Radoslavov | dbeab4c | 2015-02-23 09:37:49 -0800 | [diff] [blame] | 770 | |
| 771 | // Test when there is a mistmatch in the compared IP address families |
| 772 | ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); |
| 773 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("1111:2222:3333:4444::/120"))); |
| 774 | ipPrefix = IpPrefix.valueOf("255.255.255.255/32"); |
| 775 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /** |
| 779 | * Tests IP prefix contains another IP prefix for IPv6. |
| 780 | */ |
| 781 | @Test |
| 782 | public void testContainsIpPrefixIPv6() { |
| 783 | IpPrefix ipPrefix; |
| 784 | |
| 785 | ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/120"); |
| 786 | assertTrue(ipPrefix.contains( |
| 787 | IpPrefix.valueOf("1111:2222:3333:4444::/120"))); |
| 788 | assertTrue(ipPrefix.contains( |
| 789 | IpPrefix.valueOf("1111:2222:3333:4444::/128"))); |
| 790 | assertTrue(ipPrefix.contains( |
| 791 | IpPrefix.valueOf("1111:2222:3333:4444::1/128"))); |
| 792 | assertFalse(ipPrefix.contains( |
| 793 | IpPrefix.valueOf("1111:2222:3333:4444::/64"))); |
| 794 | assertFalse(ipPrefix.contains( |
| 795 | IpPrefix.valueOf("1111:2222:3333:4445::/120"))); |
| 796 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/64"))); |
| 797 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0"))); |
| 798 | assertFalse(ipPrefix.contains( |
| 799 | IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); |
| 800 | |
| 801 | ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/128"); |
| 802 | assertFalse(ipPrefix.contains( |
| 803 | IpPrefix.valueOf("1111:2222:3333:4444::/120"))); |
| 804 | assertTrue(ipPrefix.contains( |
| 805 | IpPrefix.valueOf("1111:2222:3333:4444::/128"))); |
| 806 | assertFalse(ipPrefix.contains( |
| 807 | IpPrefix.valueOf("1111:2222:3333:4444::1/128"))); |
| 808 | assertFalse(ipPrefix.contains( |
| 809 | IpPrefix.valueOf("1111:2222:3333:4444::/64"))); |
| 810 | assertFalse(ipPrefix.contains( |
| 811 | IpPrefix.valueOf("1111:2222:3333:4445::/120"))); |
| 812 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/64"))); |
| 813 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0"))); |
| 814 | assertFalse(ipPrefix.contains( |
| 815 | IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); |
| 816 | |
| 817 | ipPrefix = IpPrefix.valueOf("::/0"); |
| 818 | assertTrue(ipPrefix.contains( |
| 819 | IpPrefix.valueOf("1111:2222:3333:4444::/120"))); |
| 820 | assertTrue(ipPrefix.contains( |
| 821 | IpPrefix.valueOf("1111:2222:3333:4444::/128"))); |
| 822 | assertTrue(ipPrefix.contains( |
| 823 | IpPrefix.valueOf("1111:2222:3333:4444::1/128"))); |
| 824 | assertTrue(ipPrefix.contains( |
| 825 | IpPrefix.valueOf("1111:2222:3333:4444::/64"))); |
| 826 | assertTrue(ipPrefix.contains( |
| 827 | IpPrefix.valueOf("1111:2222:3333:4445::/120"))); |
| 828 | assertTrue(ipPrefix.contains(IpPrefix.valueOf("::/64"))); |
| 829 | assertTrue(ipPrefix.contains(IpPrefix.valueOf("::/0"))); |
| 830 | assertTrue(ipPrefix.contains( |
| 831 | IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); |
| 832 | |
| 833 | ipPrefix = |
| 834 | IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 835 | assertFalse(ipPrefix.contains( |
| 836 | IpPrefix.valueOf("1111:2222:3333:4444::/120"))); |
| 837 | assertFalse(ipPrefix.contains( |
| 838 | IpPrefix.valueOf("1111:2222:3333:4444::/128"))); |
| 839 | assertFalse(ipPrefix.contains( |
| 840 | IpPrefix.valueOf("1111:2222:3333:4444::1/128"))); |
| 841 | assertFalse(ipPrefix.contains( |
| 842 | IpPrefix.valueOf("1111:2222:3333:4444::/64"))); |
| 843 | assertFalse(ipPrefix.contains( |
| 844 | IpPrefix.valueOf("1111:2222:3333:4445::/120"))); |
| 845 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/64"))); |
| 846 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0"))); |
| 847 | assertTrue(ipPrefix.contains( |
| 848 | IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); |
Pavlin Radoslavov | dbeab4c | 2015-02-23 09:37:49 -0800 | [diff] [blame] | 849 | |
| 850 | // Test when there is a mistmatch in the compared IP address families |
| 851 | ipPrefix = IpPrefix.valueOf("::/0"); |
| 852 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24"))); |
| 853 | ipPrefix = IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 854 | assertFalse(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32"))); |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | /** |
| 858 | * Tests IP prefix contains IP address for IPv4. |
| 859 | */ |
| 860 | @Test |
| 861 | public void testContainsIpAddressIPv4() { |
| 862 | IpPrefix ipPrefix; |
| 863 | |
| 864 | ipPrefix = IpPrefix.valueOf("1.2.0.0/24"); |
| 865 | assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.0"))); |
| 866 | assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.4"))); |
| 867 | assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0"))); |
| 868 | assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0"))); |
| 869 | assertFalse(ipPrefix.contains(IpAddress.valueOf("255.255.255.255"))); |
| 870 | |
| 871 | ipPrefix = IpPrefix.valueOf("1.2.0.0/32"); |
| 872 | assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.0"))); |
| 873 | assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.4"))); |
| 874 | assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0"))); |
| 875 | assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0"))); |
| 876 | assertFalse(ipPrefix.contains(IpAddress.valueOf("255.255.255.255"))); |
| 877 | |
| 878 | ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); |
| 879 | assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.0"))); |
| 880 | assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.4"))); |
| 881 | assertTrue(ipPrefix.contains(IpAddress.valueOf("1.3.0.0"))); |
| 882 | assertTrue(ipPrefix.contains(IpAddress.valueOf("0.0.0.0"))); |
| 883 | assertTrue(ipPrefix.contains(IpAddress.valueOf("255.255.255.255"))); |
| 884 | |
| 885 | ipPrefix = IpPrefix.valueOf("255.255.255.255/32"); |
| 886 | assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.0"))); |
| 887 | assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.4"))); |
| 888 | assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0"))); |
| 889 | assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0"))); |
| 890 | assertTrue(ipPrefix.contains(IpAddress.valueOf("255.255.255.255"))); |
Pavlin Radoslavov | dbeab4c | 2015-02-23 09:37:49 -0800 | [diff] [blame] | 891 | |
| 892 | // Test when there is a mistmatch in the compared IP address families |
| 893 | ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); |
| 894 | assertFalse(ipPrefix.contains(IpAddress.valueOf("1111:2222:3333:4444::"))); |
| 895 | ipPrefix = IpPrefix.valueOf("255.255.255.255/32"); |
| 896 | assertFalse(ipPrefix.contains(IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | /** |
| 900 | * Tests IP prefix contains IP address for IPv6. |
| 901 | */ |
| 902 | @Test |
| 903 | public void testContainsIpAddressIPv6() { |
| 904 | IpPrefix ipPrefix; |
| 905 | |
| 906 | ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/120"); |
| 907 | assertTrue(ipPrefix.contains( |
| 908 | IpAddress.valueOf("1111:2222:3333:4444::"))); |
| 909 | assertTrue(ipPrefix.contains( |
| 910 | IpAddress.valueOf("1111:2222:3333:4444::1"))); |
| 911 | assertFalse(ipPrefix.contains( |
| 912 | IpAddress.valueOf("1111:2222:3333:4445::"))); |
| 913 | assertFalse(ipPrefix.contains(IpAddress.valueOf("::"))); |
| 914 | assertFalse(ipPrefix.contains( |
| 915 | IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); |
| 916 | |
| 917 | ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/128"); |
| 918 | assertTrue(ipPrefix.contains( |
| 919 | IpAddress.valueOf("1111:2222:3333:4444::"))); |
| 920 | assertFalse(ipPrefix.contains( |
| 921 | IpAddress.valueOf("1111:2222:3333:4444::1"))); |
| 922 | assertFalse(ipPrefix.contains( |
| 923 | IpAddress.valueOf("1111:2222:3333:4445::"))); |
| 924 | assertFalse(ipPrefix.contains(IpAddress.valueOf("::"))); |
| 925 | assertFalse(ipPrefix.contains( |
| 926 | IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); |
| 927 | |
| 928 | ipPrefix = IpPrefix.valueOf("::/0"); |
| 929 | assertTrue(ipPrefix.contains( |
| 930 | IpAddress.valueOf("1111:2222:3333:4444::"))); |
| 931 | assertTrue(ipPrefix.contains( |
| 932 | IpAddress.valueOf("1111:2222:3333:4444::1"))); |
| 933 | assertTrue(ipPrefix.contains( |
| 934 | IpAddress.valueOf("1111:2222:3333:4445::"))); |
| 935 | assertTrue(ipPrefix.contains(IpAddress.valueOf("::"))); |
| 936 | assertTrue(ipPrefix.contains( |
| 937 | IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); |
| 938 | |
| 939 | ipPrefix = |
| 940 | IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 941 | assertFalse(ipPrefix.contains( |
| 942 | IpAddress.valueOf("1111:2222:3333:4444::"))); |
| 943 | assertFalse(ipPrefix.contains( |
| 944 | IpAddress.valueOf("1111:2222:3333:4444::1"))); |
| 945 | assertFalse(ipPrefix.contains( |
| 946 | IpAddress.valueOf("1111:2222:3333:4445::"))); |
| 947 | assertFalse(ipPrefix.contains(IpAddress.valueOf("::"))); |
| 948 | assertTrue(ipPrefix.contains( |
| 949 | IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); |
Pavlin Radoslavov | dbeab4c | 2015-02-23 09:37:49 -0800 | [diff] [blame] | 950 | |
| 951 | // Test when there is a mistmatch in the compared IP address families |
| 952 | ipPrefix = IpPrefix.valueOf("::/0"); |
| 953 | assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.0"))); |
| 954 | ipPrefix = IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 955 | assertFalse(ipPrefix.contains(IpAddress.valueOf("255.255.255.255"))); |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | /** |
| 959 | * Tests equality of {@link IpPrefix} for IPv4. |
| 960 | */ |
| 961 | @Test |
| 962 | public void testEqualityIPv4() { |
| 963 | new EqualsTester() |
| 964 | .addEqualityGroup(IpPrefix.valueOf("1.2.0.0/24"), |
| 965 | IpPrefix.valueOf("1.2.0.0/24"), |
| 966 | IpPrefix.valueOf("1.2.0.4/24")) |
| 967 | .addEqualityGroup(IpPrefix.valueOf("1.2.0.0/16"), |
| 968 | IpPrefix.valueOf("1.2.0.0/16")) |
| 969 | .addEqualityGroup(IpPrefix.valueOf("1.2.0.0/32"), |
| 970 | IpPrefix.valueOf("1.2.0.0/32")) |
| 971 | .addEqualityGroup(IpPrefix.valueOf("1.3.0.0/24"), |
| 972 | IpPrefix.valueOf("1.3.0.0/24")) |
| 973 | .addEqualityGroup(IpPrefix.valueOf("0.0.0.0/0"), |
| 974 | IpPrefix.valueOf("0.0.0.0/0")) |
| 975 | .addEqualityGroup(IpPrefix.valueOf("255.255.255.255/32"), |
| 976 | IpPrefix.valueOf("255.255.255.255/32")) |
| 977 | .testEquals(); |
| 978 | } |
| 979 | |
| 980 | /** |
| 981 | * Tests equality of {@link IpPrefix} for IPv6. |
| 982 | */ |
| 983 | @Test |
| 984 | public void testEqualityIPv6() { |
| 985 | new EqualsTester() |
| 986 | .addEqualityGroup( |
| 987 | IpPrefix.valueOf("1111:2222:3333:4444::/120"), |
| 988 | IpPrefix.valueOf("1111:2222:3333:4444::1/120"), |
| 989 | IpPrefix.valueOf("1111:2222:3333:4444::/120")) |
| 990 | .addEqualityGroup( |
| 991 | IpPrefix.valueOf("1111:2222:3333:4444::/64"), |
| 992 | IpPrefix.valueOf("1111:2222:3333:4444::/64")) |
| 993 | .addEqualityGroup( |
| 994 | IpPrefix.valueOf("1111:2222:3333:4444::/128"), |
| 995 | IpPrefix.valueOf("1111:2222:3333:4444::/128")) |
| 996 | .addEqualityGroup( |
| 997 | IpPrefix.valueOf("1111:2222:3333:4445::/64"), |
| 998 | IpPrefix.valueOf("1111:2222:3333:4445::/64")) |
| 999 | .addEqualityGroup( |
| 1000 | IpPrefix.valueOf("::/0"), |
| 1001 | IpPrefix.valueOf("::/0")) |
| 1002 | .addEqualityGroup( |
| 1003 | IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"), |
| 1004 | IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")) |
| 1005 | .testEquals(); |
| 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * Tests object string representation for IPv4. |
| 1010 | */ |
| 1011 | @Test |
| 1012 | public void testToStringIPv4() { |
| 1013 | IpPrefix ipPrefix; |
| 1014 | |
| 1015 | ipPrefix = IpPrefix.valueOf("1.2.3.0/24"); |
| 1016 | assertThat(ipPrefix.toString(), is("1.2.3.0/24")); |
| 1017 | |
| 1018 | ipPrefix = IpPrefix.valueOf("1.2.3.4/24"); |
| 1019 | assertThat(ipPrefix.toString(), is("1.2.3.0/24")); |
| 1020 | |
| 1021 | ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); |
| 1022 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 1023 | |
| 1024 | ipPrefix = IpPrefix.valueOf("255.255.255.255/32"); |
| 1025 | assertThat(ipPrefix.toString(), is("255.255.255.255/32")); |
| 1026 | } |
| 1027 | |
| 1028 | /** |
| 1029 | * Tests object string representation for IPv6. |
| 1030 | */ |
| 1031 | @Test |
| 1032 | public void testToStringIPv6() { |
| 1033 | IpPrefix ipPrefix; |
| 1034 | |
| 1035 | ipPrefix = IpPrefix.valueOf("1100::/8"); |
| 1036 | assertThat(ipPrefix.toString(), is("1100::/8")); |
| 1037 | |
| 1038 | ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8"); |
| 1039 | assertThat(ipPrefix.toString(), is("1100::/8")); |
| 1040 | |
| 1041 | ipPrefix = IpPrefix.valueOf("::/0"); |
| 1042 | assertThat(ipPrefix.toString(), is("::/0")); |
| 1043 | |
| 1044 | ipPrefix = IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 1045 | assertThat(ipPrefix.toString(), |
| 1046 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 1047 | } |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 1048 | } |