blob: c731ed6621df2a0064d0b358ba5c747ff75cf85f [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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 Ip4Prefix}.
30 */
31public class Ip4PrefixTest {
32 /**
33 * Tests the immutability of {@link Ip4Prefix}.
34 */
35 @Test
36 public void testImmutable() {
37 assertThatClassIsImmutable(Ip4Prefix.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(Ip4Prefix.VERSION, is(IpAddress.Version.INET));
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(Ip4Prefix.MAX_MASK_LENGTH, is(32));
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 */
59 @Test
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080060 public void testVersion() {
61 Ip4Prefix ipPrefix;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070062
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080063 // IPv4
64 ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
65 assertThat(ipPrefix.version(), is(IpAddress.Version.INET));
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 IPv4 prefix.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070071 */
72 @Test
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080073 public void testAddressAndPrefixLengthIPv4() {
74 Ip4Prefix ipPrefix;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070075
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080076 ipPrefix = Ip4Prefix.valueOf("1.2.3.0/24");
77 assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.0")));
78 assertThat(ipPrefix.prefixLength(), is(24));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070079
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080080 ipPrefix = Ip4Prefix.valueOf("1.2.3.4/24");
81 assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.0")));
82 assertThat(ipPrefix.prefixLength(), is(24));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070083
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080084 ipPrefix = Ip4Prefix.valueOf("1.2.3.4/32");
85 assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.4")));
86 assertThat(ipPrefix.prefixLength(), is(32));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070087
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080088 ipPrefix = Ip4Prefix.valueOf("1.2.3.5/32");
89 assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.5")));
90 assertThat(ipPrefix.prefixLength(), is(32));
91
92 ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
93 assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("0.0.0.0")));
94 assertThat(ipPrefix.prefixLength(), is(0));
95
96 ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32");
97 assertThat(ipPrefix.address(),
98 equalTo(Ip4Address.valueOf("255.255.255.255")));
99 assertThat(ipPrefix.prefixLength(), is(32));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700100 }
101
102 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800103 * Tests valueOf() converter for IPv4 integer value.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700104 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800105 @Test
106 public void testValueOfForIntegerIPv4() {
107 Ip4Prefix ipPrefix;
108
109 ipPrefix = Ip4Prefix.valueOf(0x01020304, 24);
110 assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
111
112 ipPrefix = Ip4Prefix.valueOf(0x01020304, 32);
113 assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
114
115 ipPrefix = Ip4Prefix.valueOf(0x01020305, 32);
116 assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
117
118 ipPrefix = Ip4Prefix.valueOf(0, 0);
119 assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
120
121 ipPrefix = Ip4Prefix.valueOf(0, 32);
122 assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
123
124 ipPrefix = Ip4Prefix.valueOf(0xffffffff, 0);
125 assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
126
127 ipPrefix = Ip4Prefix.valueOf(0xffffffff, 16);
128 assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
129
130 ipPrefix = Ip4Prefix.valueOf(0xffffffff, 32);
131 assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700132 }
133
134 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800135 * Tests invalid valueOf() converter for IPv4 integer value and
136 * negative prefix length.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700137 */
138 @Test(expected = IllegalArgumentException.class)
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800139 public void testInvalidValueOfIntegerNegativePrefixLengthIPv4() {
140 Ip4Prefix ipPrefix;
141
142 ipPrefix = Ip4Prefix.valueOf(0x01020304, -1);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700143 }
144
145 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800146 * Tests invalid valueOf() converter for IPv4 integer value and
147 * too long prefix length.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700148 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800149 @Test(expected = IllegalArgumentException.class)
150 public void testInvalidValueOfIntegerTooLongPrefixLengthIPv4() {
151 Ip4Prefix ipPrefix;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700152
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800153 ipPrefix = Ip4Prefix.valueOf(0x01020304, 33);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700154 }
155
156 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800157 * Tests valueOf() converter for IPv4 byte array.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700158 */
159 @Test
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800160 public void testValueOfByteArrayIPv4() {
161 Ip4Prefix ipPrefix;
162 byte[] value;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700163
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800164 value = new byte[] {1, 2, 3, 4};
165 ipPrefix = Ip4Prefix.valueOf(value, 24);
166 assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700167
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800168 ipPrefix = Ip4Prefix.valueOf(value, 32);
169 assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700170
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800171 value = new byte[] {1, 2, 3, 5};
172 ipPrefix = Ip4Prefix.valueOf(value, 32);
173 assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
174
175 value = new byte[] {0, 0, 0, 0};
176 ipPrefix = Ip4Prefix.valueOf(value, 0);
177 assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
178
179 ipPrefix = Ip4Prefix.valueOf(value, 32);
180 assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
181
182 value = new byte[] {(byte) 0xff, (byte) 0xff,
183 (byte) 0xff, (byte) 0xff};
184 ipPrefix = Ip4Prefix.valueOf(value, 0);
185 assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
186
187 ipPrefix = Ip4Prefix.valueOf(value, 16);
188 assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
189
190 ipPrefix = Ip4Prefix.valueOf(value, 32);
191 assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700192 }
193
194 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800195 * Tests invalid valueOf() converter for a null array for IPv4.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700196 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800197 @Test(expected = NullPointerException.class)
198 public void testInvalidValueOfNullArrayIPv4() {
199 Ip4Prefix ipPrefix;
200 byte[] value;
201
202 value = null;
203 ipPrefix = Ip4Prefix.valueOf(value, 24);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700204 }
205
206 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800207 * Tests invalid valueOf() converter for a short array for IPv4.
208 */
209 @Test(expected = IllegalArgumentException.class)
210 public void testInvalidValueOfShortArrayIPv4() {
211 Ip4Prefix ipPrefix;
212 byte[] value;
213
214 value = new byte[] {1, 2, 3};
215 ipPrefix = Ip4Prefix.valueOf(value, 24);
216 }
217
218 /**
219 * Tests invalid valueOf() converter for IPv4 byte array and
220 * negative prefix length.
221 */
222 @Test(expected = IllegalArgumentException.class)
223 public void testInvalidValueOfByteArrayNegativePrefixLengthIPv4() {
224 Ip4Prefix ipPrefix;
225 byte[] value;
226
227 value = new byte[] {1, 2, 3, 4};
228 ipPrefix = Ip4Prefix.valueOf(value, -1);
229 }
230
231 /**
232 * Tests invalid valueOf() converter for IPv4 byte array and
233 * too long prefix length.
234 */
235 @Test(expected = IllegalArgumentException.class)
236 public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv4() {
237 Ip4Prefix ipPrefix;
238 byte[] value;
239
240 value = new byte[] {1, 2, 3, 4};
241 ipPrefix = Ip4Prefix.valueOf(value, 33);
242 }
243
244 /**
245 * Tests valueOf() converter for IPv4 address.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700246 */
247 @Test
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800248 public void testValueOfAddressIPv4() {
249 Ip4Address ipAddress;
250 Ip4Prefix ipPrefix;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700251
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800252 ipAddress = Ip4Address.valueOf("1.2.3.4");
253 ipPrefix = Ip4Prefix.valueOf(ipAddress, 24);
254 assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700255
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800256 ipPrefix = Ip4Prefix.valueOf(ipAddress, 32);
257 assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700258
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800259 ipAddress = Ip4Address.valueOf("1.2.3.5");
260 ipPrefix = Ip4Prefix.valueOf(ipAddress, 32);
261 assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
262
263 ipAddress = Ip4Address.valueOf("0.0.0.0");
264 ipPrefix = Ip4Prefix.valueOf(ipAddress, 0);
265 assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
266
267 ipPrefix = Ip4Prefix.valueOf(ipAddress, 32);
268 assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
269
270 ipAddress = Ip4Address.valueOf("255.255.255.255");
271 ipPrefix = Ip4Prefix.valueOf(ipAddress, 0);
272 assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
273
274 ipPrefix = Ip4Prefix.valueOf(ipAddress, 16);
275 assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
276
277 ipPrefix = Ip4Prefix.valueOf(ipAddress, 32);
278 assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
279 }
280
281 /**
282 * Tests invalid valueOf() converter for a null IP address.
283 */
284 @Test(expected = NullPointerException.class)
285 public void testInvalidValueOfNullAddress() {
286 Ip4Address ipAddress;
287 Ip4Prefix ipPrefix;
288
289 ipAddress = null;
290 ipPrefix = Ip4Prefix.valueOf(ipAddress, 24);
291 }
292
293 /**
294 * Tests invalid valueOf() converter for IPv4 address and
295 * negative prefix length.
296 */
297 @Test(expected = IllegalArgumentException.class)
298 public void testInvalidValueOfAddressNegativePrefixLengthIPv4() {
299 Ip4Address ipAddress;
300 Ip4Prefix ipPrefix;
301
302 ipAddress = Ip4Address.valueOf("1.2.3.4");
303 ipPrefix = Ip4Prefix.valueOf(ipAddress, -1);
304 }
305
306 /**
307 * Tests invalid valueOf() converter for IPv4 address and
308 * too long prefix length.
309 */
310 @Test(expected = IllegalArgumentException.class)
311 public void testInvalidValueOfAddressTooLongPrefixLengthIPv4() {
312 Ip4Address ipAddress;
313 Ip4Prefix ipPrefix;
314
315 ipAddress = Ip4Address.valueOf("1.2.3.4");
316 ipPrefix = Ip4Prefix.valueOf(ipAddress, 33);
317 }
318
319 /**
320 * Tests valueOf() converter for IPv4 string.
321 */
322 @Test
323 public void testValueOfStringIPv4() {
324 Ip4Prefix ipPrefix;
325
326 ipPrefix = Ip4Prefix.valueOf("1.2.3.4/24");
327 assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
328
329 ipPrefix = Ip4Prefix.valueOf("1.2.3.4/32");
330 assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
331
332 ipPrefix = Ip4Prefix.valueOf("1.2.3.5/32");
333 assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
334
335 ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
336 assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
337
338 ipPrefix = Ip4Prefix.valueOf("0.0.0.0/32");
339 assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
340
341 ipPrefix = Ip4Prefix.valueOf("255.255.255.255/0");
342 assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
343
344 ipPrefix = Ip4Prefix.valueOf("255.255.255.255/16");
345 assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
346
347 ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32");
348 assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
349 }
350
351 /**
352 * Tests invalid valueOf() converter for a null string.
353 */
354 @Test(expected = NullPointerException.class)
355 public void testInvalidValueOfNullString() {
356 Ip4Prefix ipPrefix;
357 String fromString;
358
359 fromString = null;
360 ipPrefix = Ip4Prefix.valueOf(fromString);
361 }
362
363 /**
364 * Tests invalid valueOf() converter for an empty string.
365 */
366 @Test(expected = IllegalArgumentException.class)
367 public void testInvalidValueOfEmptyString() {
368 Ip4Prefix ipPrefix;
369 String fromString;
370
371 fromString = "";
372 ipPrefix = Ip4Prefix.valueOf(fromString);
373 }
374
375 /**
376 * Tests invalid valueOf() converter for an incorrect string.
377 */
378 @Test(expected = IllegalArgumentException.class)
379 public void testInvalidValueOfIncorrectString() {
380 Ip4Prefix ipPrefix;
381 String fromString;
382
383 fromString = "NoSuchIpPrefix";
384 ipPrefix = Ip4Prefix.valueOf(fromString);
385 }
386
387 /**
388 * Tests invalid valueOf() converter for IPv4 string and
389 * negative prefix length.
390 */
391 @Test(expected = IllegalArgumentException.class)
392 public void testInvalidValueOfStringNegativePrefixLengthIPv4() {
393 Ip4Prefix ipPrefix;
394
395 ipPrefix = Ip4Prefix.valueOf("1.2.3.4/-1");
396 }
397
398 /**
399 * Tests invalid valueOf() converter for IPv4 string and
400 * too long prefix length.
401 */
402 @Test(expected = IllegalArgumentException.class)
403 public void testInvalidValueOfStringTooLongPrefixLengthIPv4() {
404 Ip4Prefix ipPrefix;
405
406 ipPrefix = Ip4Prefix.valueOf("1.2.3.4/33");
407 }
408
409 /**
410 * Tests IP prefix contains another IP prefix for IPv4.
411 */
412 @Test
413 public void testContainsIpPrefixIPv4() {
414 Ip4Prefix ipPrefix;
415
416 ipPrefix = Ip4Prefix.valueOf("1.2.0.0/24");
417 assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24")));
418 assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32")));
419 assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32")));
420 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16")));
421 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24")));
422 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16")));
423 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0")));
424 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32")));
425
426 ipPrefix = Ip4Prefix.valueOf("1.2.0.0/32");
427 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24")));
428 assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32")));
429 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32")));
430 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16")));
431 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24")));
432 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16")));
433 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0")));
434 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32")));
435
436 ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
437 assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24")));
438 assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32")));
439 assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32")));
440 assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16")));
441 assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24")));
442 assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16")));
443 assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0")));
444 assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32")));
445
446 ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32");
447 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24")));
448 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32")));
449 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32")));
450 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16")));
451 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24")));
452 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16")));
453 assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0")));
454 assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32")));
455 }
456
457 /**
458 * Tests IP prefix contains IP address for IPv4.
459 */
460 @Test
461 public void testContainsIpAddressIPv4() {
462 Ip4Prefix ipPrefix;
463
464 ipPrefix = Ip4Prefix.valueOf("1.2.0.0/24");
465 assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0")));
466 assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4")));
467 assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0")));
468 assertFalse(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0")));
469 assertFalse(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255")));
470
471 ipPrefix = Ip4Prefix.valueOf("1.2.0.0/32");
472 assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0")));
473 assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4")));
474 assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0")));
475 assertFalse(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0")));
476 assertFalse(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255")));
477
478 ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
479 assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0")));
480 assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4")));
481 assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0")));
482 assertTrue(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0")));
483 assertTrue(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255")));
484
485 ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32");
486 assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0")));
487 assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4")));
488 assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0")));
489 assertFalse(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0")));
490 assertTrue(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255")));
491 }
492
493 /**
494 * Tests equality of {@link Ip4Prefix} for IPv4.
495 */
496 @Test
497 public void testEqualityIPv4() {
498 new EqualsTester()
499 .addEqualityGroup(Ip4Prefix.valueOf("1.2.0.0/24"),
500 Ip4Prefix.valueOf("1.2.0.0/24"),
501 Ip4Prefix.valueOf("1.2.0.4/24"))
502 .addEqualityGroup(Ip4Prefix.valueOf("1.2.0.0/16"),
503 Ip4Prefix.valueOf("1.2.0.0/16"))
504 .addEqualityGroup(Ip4Prefix.valueOf("1.2.0.0/32"),
505 Ip4Prefix.valueOf("1.2.0.0/32"))
506 .addEqualityGroup(Ip4Prefix.valueOf("1.3.0.0/24"),
507 Ip4Prefix.valueOf("1.3.0.0/24"))
508 .addEqualityGroup(Ip4Prefix.valueOf("0.0.0.0/0"),
509 Ip4Prefix.valueOf("0.0.0.0/0"))
510 .addEqualityGroup(Ip4Prefix.valueOf("255.255.255.255/32"),
511 Ip4Prefix.valueOf("255.255.255.255/32"))
512 .testEquals();
513 }
514
515 /**
516 * Tests object string representation for IPv4.
517 */
518 @Test
519 public void testToStringIPv4() {
520 Ip4Prefix ipPrefix;
521
522 ipPrefix = Ip4Prefix.valueOf("1.2.3.0/24");
523 assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
524
525 ipPrefix = Ip4Prefix.valueOf("1.2.3.4/24");
526 assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
527
528 ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
529 assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
530
531 ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32");
532 assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700533 }
534}