blob: 09496c814011af160a92cffb501d0221a9a1a9be [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070016package org.onlab.packet;
17
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080018import com.google.common.testing.EqualsTester;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070019import org.junit.Test;
20
21import static org.hamcrest.Matchers.equalTo;
22import static org.hamcrest.Matchers.is;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070023import static org.junit.Assert.assertThat;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080024import static org.junit.Assert.assertFalse;
25import static org.junit.Assert.assertTrue;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070026import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
27
28/**
29 * Tests for class {@link Ip6Prefix}.
30 */
31public 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 Radoslavovf182f012014-11-04 15:03:18 -080041 * Tests the IPv4 prefix address version constant.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070042 */
43 @Test
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080044 public void testAddressVersion() {
45 assertThat(Ip6Prefix.VERSION, is(IpAddress.Version.INET6));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070046 }
47
48 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080049 * Tests the maximum mask length.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070050 */
51 @Test
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080052 public void testMaxMaskLength() {
53 assertThat(Ip6Prefix.MAX_MASK_LENGTH, is(128));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070054 }
55
56 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080057 * Tests returning the IP version of the prefix.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070058 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080059 @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 Radoslavov9de27722014-10-23 20:31:15 -070066 }
67
68 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080069 * Tests returning the IP address value and IP address prefix length of
70 * an IPv6 prefix.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070071 */
72 @Test
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080073 public void testAddressAndPrefixLengthIPv6() {
74 Ip6Prefix ipPrefix;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070075
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080076 ipPrefix = Ip6Prefix.valueOf("1100::/8");
77 assertThat(ipPrefix.address(), equalTo(Ip6Address.valueOf("1100::")));
78 assertThat(ipPrefix.prefixLength(), is(8));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070079
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080080 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 Radoslavov9de27722014-10-23 20:31:15 -0700128 is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
129
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800130 ipPrefix = Ip6Prefix.valueOf(value, 128);
131 assertThat(ipPrefix.toString(),
132 is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700133
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800134 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 Radoslavov9de27722014-10-23 20:31:15 -0700140
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800141 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 Radoslavov9de27722014-10-23 20:31:15 -0700160 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700161 }
162
163 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800164 * Tests invalid valueOf() converter for a null array for IPv6.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700165 */
166 @Test(expected = NullPointerException.class)
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800167 public void testInvalidValueOfNullArrayIPv6() {
168 Ip6Prefix ipPrefix;
169 byte[] value;
170
171 value = null;
172 ipPrefix = Ip6Prefix.valueOf(value, 120);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700173 }
174
175 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800176 * Tests invalid valueOf() converter for a short array for IPv6.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700177 */
178 @Test(expected = IllegalArgumentException.class)
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800179 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 Radoslavov9de27722014-10-23 20:31:15 -0700185 }
186
187 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800188 * Tests invalid valueOf() converter for IPv6 byte array and
189 * negative prefix length.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700190 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800191 @Test(expected = IllegalArgumentException.class)
192 public void testInvalidValueOfByteArrayNegativePrefixLengthIPv6() {
193 Ip6Prefix ipPrefix;
194 byte[] value;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700195
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800196 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 Radoslavov9de27722014-10-23 20:31:15 -0700201 }
202
203 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800204 * Tests invalid valueOf() converter for IPv6 byte array and
205 * too long prefix length.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700206 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800207 @Test(expected = IllegalArgumentException.class)
208 public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv6() {
209 Ip6Prefix ipPrefix;
210 byte[] value;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700211
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800212 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 Radoslavov9de27722014-10-23 20:31:15 -0700217 }
218
219 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800220 * Tests valueOf() converter for IPv6 address.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700221 */
222 @Test
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800223 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 Radoslavov9de27722014-10-23 20:31:15 -0700255 }
256
257 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800258 * 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 Radoslavov9de27722014-10-23 20:31:15 -0700299 */
300 @Test
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800301 public void testValueOfStringIPv6() {
302 Ip6Prefix ipPrefix;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700303
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800304 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 Radoslavov9de27722014-10-23 20:31:15 -0700308
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800309 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 Radoslavov9de27722014-10-23 20:31:15 -0700313
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800314 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 Radoslavov9de27722014-10-23 20:31:15 -0700566 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
567 }
568}