blob: c063f7afcfaf4f874aca579adb1891c8cbca3c8b [file] [log] [blame]
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onlab.packet;
17
Ray Milkey37f6a382014-11-25 14:54:42 -080018import java.net.InetAddress;
19
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070020import org.junit.Test;
21
Ray Milkey37f6a382014-11-25 14:54:42 -080022import com.google.common.net.InetAddresses;
23import com.google.common.testing.EqualsTester;
Thomas Vachuska9d8f72f2014-11-03 10:14:05 -080024
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070025import static org.hamcrest.Matchers.is;
Pavlin Radoslavov34c81642014-11-04 16:21:38 -080026import static org.junit.Assert.assertNull;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070027import static org.junit.Assert.assertThat;
28import static org.junit.Assert.assertTrue;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080029import static org.junit.Assert.assertFalse;
Ray Milkey37f6a382014-11-25 14:54:42 -080030import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070031
32/**
33 * Tests for class {@link IpAddress}.
34 */
35public class IpAddressTest {
36 /**
37 * Tests the immutability of {@link IpAddress}.
38 */
39 @Test
40 public void testImmutable() {
Ray Milkey37f6a382014-11-25 14:54:42 -080041 assertThatClassIsImmutableBaseClass(IpAddress.class);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070042 }
43
44 /**
45 * Tests the length of the address in bytes (octets).
46 */
47 @Test
48 public void testAddrByteLength() {
49 assertThat(IpAddress.INET_BYTE_LENGTH, is(4));
50 assertThat(IpAddress.INET6_BYTE_LENGTH, is(16));
51 assertThat(IpAddress.byteLength(IpAddress.Version.INET), is(4));
52 assertThat(IpAddress.byteLength(IpAddress.Version.INET6), is(16));
53 }
54
55 /**
56 * Tests the length of the address in bits.
57 */
58 @Test
59 public void testAddrBitLength() {
60 assertThat(IpAddress.INET_BIT_LENGTH, is(32));
61 assertThat(IpAddress.INET6_BIT_LENGTH, is(128));
62 }
63
64 /**
65 * Tests returning the IP address version.
66 */
67 @Test
68 public void testVersion() {
69 IpAddress ipAddress;
70
71 // IPv4
72 ipAddress = IpAddress.valueOf("0.0.0.0");
73 assertThat(ipAddress.version(), is(IpAddress.Version.INET));
74
75 // IPv6
76 ipAddress = IpAddress.valueOf("::");
77 assertThat(ipAddress.version(), is(IpAddress.Version.INET6));
78 }
79
80 /**
Pavlin Radoslavov34ffe722015-03-10 12:48:55 -070081 * Tests whether the IP version of an address is IPv4.
82 */
83 @Test
84 public void testIsIp4() {
85 IpAddress ipAddress;
86
87 // IPv4
88 ipAddress = IpAddress.valueOf("0.0.0.0");
89 assertTrue(ipAddress.isIp4());
90
91 // IPv6
92 ipAddress = IpAddress.valueOf("::");
93 assertFalse(ipAddress.isIp4());
94 }
95
96 /**
97 * Tests whether the IP version of an address is IPv6.
98 */
99 @Test
100 public void testIsIp6() {
101 IpAddress ipAddress;
102
103 // IPv4
104 ipAddress = IpAddress.valueOf("0.0.0.0");
105 assertFalse(ipAddress.isIp6());
106
107 // IPv6
108 ipAddress = IpAddress.valueOf("::");
109 assertTrue(ipAddress.isIp6());
110 }
111
112 /**
Pavlin Radoslavov34c81642014-11-04 16:21:38 -0800113 * Tests getting the Ip4Address and Ip6Address view of the IP address.
114 */
115 @Test
116 public void testGetIp4AndIp6AddressView() {
117 IpAddress ipAddress;
118 Ip4Address ip4Address;
119 Ip6Address ip6Address;
120
121 // Pure IPv4 IpAddress
122 ipAddress = IpAddress.valueOf("1.2.3.4");
123 ip4Address = ipAddress.getIp4Address();
124 ip6Address = ipAddress.getIp6Address();
125 assertThat(ip4Address.toString(), is("1.2.3.4"));
126 assertNull(ip6Address);
127
128 // IPv4 IpAddress that is Ip4Address
129 ipAddress = Ip4Address.valueOf("1.2.3.4");
130 ip4Address = ipAddress.getIp4Address();
131 ip6Address = ipAddress.getIp6Address();
132 assertThat(ip4Address.toString(), is("1.2.3.4"));
133 assertNull(ip6Address);
134
135 // Pure IPv6 IpAddress
136 ipAddress = IpAddress.valueOf("1111:2222::");
137 ip4Address = ipAddress.getIp4Address();
138 ip6Address = ipAddress.getIp6Address();
139 assertNull(ip4Address);
140 assertThat(ip6Address.toString(), is("1111:2222::"));
141
142 // IPv6 IpAddress that is Ip6Address
143 ipAddress = Ip6Address.valueOf("1111:2222::");
144 ip4Address = ipAddress.getIp4Address();
145 ip6Address = ipAddress.getIp6Address();
146 assertNull(ip4Address);
147 assertThat(ip6Address.toString(), is("1111:2222::"));
148 }
149
150 /**
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700151 * Tests returning an IPv4 address as a byte array.
152 */
153 @Test
154 public void testAddressToOctetsIPv4() {
155 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800156 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700157
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800158 value = new byte[] {1, 2, 3, 4};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700159 ipAddress = IpAddress.valueOf("1.2.3.4");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800160 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700161
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800162 value = new byte[] {0, 0, 0, 0};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700163 ipAddress = IpAddress.valueOf("0.0.0.0");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800164 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700165
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800166 value = new byte[] {(byte) 0xff, (byte) 0xff,
167 (byte) 0xff, (byte) 0xff};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700168 ipAddress = IpAddress.valueOf("255.255.255.255");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800169 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700170 }
171
172 /**
173 * Tests returning an IPv6 address as a byte array.
174 */
175 @Test
176 public void testAddressToOctetsIPv6() {
177 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800178 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700179
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800180 value = new byte[] {0x11, 0x11, 0x22, 0x22,
181 0x33, 0x33, 0x44, 0x44,
182 0x55, 0x55, 0x66, 0x66,
183 0x77, 0x77,
184 (byte) 0x88, (byte) 0x88};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700185 ipAddress =
186 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800187 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700188
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800189 value = new byte[] {0x00, 0x00, 0x00, 0x00,
190 0x00, 0x00, 0x00, 0x00,
191 0x00, 0x00, 0x00, 0x00,
192 0x00, 0x00, 0x00, 0x00};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700193 ipAddress = IpAddress.valueOf("::");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800194 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700195
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800196 value = new byte[] {(byte) 0xff, (byte) 0xff,
197 (byte) 0xff, (byte) 0xff,
198 (byte) 0xff, (byte) 0xff,
199 (byte) 0xff, (byte) 0xff,
200 (byte) 0xff, (byte) 0xff,
201 (byte) 0xff, (byte) 0xff,
202 (byte) 0xff, (byte) 0xff,
203 (byte) 0xff, (byte) 0xff};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700204 ipAddress =
205 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800206 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700207 }
208
209 /**
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800210 * Tests valueOf() converter for IPv4 integer value.
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700211 */
212 @Test
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800213 public void testValueOfForIntegerIPv4() {
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700214 IpAddress ipAddress;
215
216 ipAddress = IpAddress.valueOf(0x01020304);
217 assertThat(ipAddress.toString(), is("1.2.3.4"));
218
219 ipAddress = IpAddress.valueOf(0);
220 assertThat(ipAddress.toString(), is("0.0.0.0"));
221
222 ipAddress = IpAddress.valueOf(0xffffffff);
223 assertThat(ipAddress.toString(), is("255.255.255.255"));
224 }
225
226 /**
227 * Tests valueOf() converter for IPv4 byte array.
228 */
229 @Test
230 public void testValueOfByteArrayIPv4() {
231 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800232 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700233
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800234 value = new byte[] {1, 2, 3, 4};
235 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700236 assertThat(ipAddress.toString(), is("1.2.3.4"));
237
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800238 value = new byte[] {0, 0, 0, 0};
239 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700240 assertThat(ipAddress.toString(), is("0.0.0.0"));
241
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800242 value = new byte[] {(byte) 0xff, (byte) 0xff,
243 (byte) 0xff, (byte) 0xff};
244 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700245 assertThat(ipAddress.toString(), is("255.255.255.255"));
246 }
247
248 /**
249 * Tests valueOf() converter for IPv6 byte array.
250 */
251 @Test
252 public void testValueOfByteArrayIPv6() {
253 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800254 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700255
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800256 value = new byte[] {0x11, 0x11, 0x22, 0x22,
257 0x33, 0x33, 0x44, 0x44,
258 0x55, 0x55, 0x66, 0x66,
259 0x77, 0x77,
260 (byte) 0x88, (byte) 0x88};
261 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700262 assertThat(ipAddress.toString(),
263 is("1111:2222:3333:4444:5555:6666:7777:8888"));
264
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800265 value = new byte[] {0x00, 0x00, 0x00, 0x00,
266 0x00, 0x00, 0x00, 0x00,
267 0x00, 0x00, 0x00, 0x00,
268 0x00, 0x00, 0x00, 0x00};
269 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700270 assertThat(ipAddress.toString(), is("::"));
271
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800272 value = new byte[] {(byte) 0xff, (byte) 0xff,
273 (byte) 0xff, (byte) 0xff,
274 (byte) 0xff, (byte) 0xff,
275 (byte) 0xff, (byte) 0xff,
276 (byte) 0xff, (byte) 0xff,
277 (byte) 0xff, (byte) 0xff,
278 (byte) 0xff, (byte) 0xff,
279 (byte) 0xff, (byte) 0xff};
280 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700281 assertThat(ipAddress.toString(),
282 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
283 }
284
285 /**
286 * Tests invalid valueOf() converter for a null array for IPv4.
287 */
288 @Test(expected = NullPointerException.class)
289 public void testInvalidValueOfNullArrayIPv4() {
290 IpAddress ipAddress;
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800291 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700292
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800293 value = null;
294 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700295 }
296
297 /**
298 * Tests invalid valueOf() converter for a null array for IPv6.
299 */
300 @Test(expected = NullPointerException.class)
301 public void testInvalidValueOfNullArrayIPv6() {
302 IpAddress ipAddress;
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800303 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700304
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800305 value = null;
306 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700307 }
308
309 /**
310 * Tests invalid valueOf() converger for an array that is too short for
311 * IPv4.
312 */
313 @Test(expected = IllegalArgumentException.class)
314 public void testInvalidValueOfShortArrayIPv4() {
315 IpAddress ipAddress;
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800316 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700317
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800318 value = new byte[] {1, 2, 3};
319 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700320 }
321
322 /**
323 * Tests invalid valueOf() converger for an array that is too short for
324 * IPv6.
325 */
326 @Test(expected = IllegalArgumentException.class)
327 public void testInvalidValueOfShortArrayIPv6() {
328 IpAddress ipAddress;
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800329 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700330
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800331 value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
332 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700333 }
334
335 /**
336 * Tests valueOf() converter for IPv4 byte array and an offset.
337 */
338 @Test
339 public void testValueOfByteArrayOffsetIPv4() {
340 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800341 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700342
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800343 value = new byte[] {11, 22, 33, // Preamble
344 1, 2, 3, 4,
345 44, 55}; // Extra bytes
346 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 3);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700347 assertThat(ipAddress.toString(), is("1.2.3.4"));
348
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800349 value = new byte[] {11, 22, // Preamble
350 0, 0, 0, 0,
351 33}; // Extra bytes
352 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700353 assertThat(ipAddress.toString(), is("0.0.0.0"));
354
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800355 value = new byte[] {11, 22, // Preamble
356 (byte) 0xff, (byte) 0xff,
357 (byte) 0xff, (byte) 0xff,
358 33}; // Extra bytes
359 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700360 assertThat(ipAddress.toString(), is("255.255.255.255"));
361 }
362
363 /**
364 * Tests valueOf() converter for IPv6 byte array and an offset.
365 */
366 @Test
367 public void testValueOfByteArrayOffsetIPv6() {
368 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800369 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700370
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800371 value = new byte[] {11, 22, 33, // Preamble
372 0x11, 0x11, 0x22, 0x22,
373 0x33, 0x33, 0x44, 0x44,
374 0x55, 0x55, 0x66, 0x66,
375 0x77, 0x77,
376 (byte) 0x88, (byte) 0x88,
377 44, 55}; // Extra bytes
378 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 3);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700379 assertThat(ipAddress.toString(),
380 is("1111:2222:3333:4444:5555:6666:7777:8888"));
381
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800382 value = new byte[] {11, 22, // Preamble
383 0x00, 0x00, 0x00, 0x00,
384 0x00, 0x00, 0x00, 0x00,
385 0x00, 0x00, 0x00, 0x00,
386 0x00, 0x00, 0x00, 0x00,
387 33}; // Extra bytes
388 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700389 assertThat(ipAddress.toString(), is("::"));
390
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800391 value = new byte[] {11, 22, // Preamble
392 (byte) 0xff, (byte) 0xff,
393 (byte) 0xff, (byte) 0xff,
394 (byte) 0xff, (byte) 0xff,
395 (byte) 0xff, (byte) 0xff,
396 (byte) 0xff, (byte) 0xff,
397 (byte) 0xff, (byte) 0xff,
398 (byte) 0xff, (byte) 0xff,
399 (byte) 0xff, (byte) 0xff,
400 33}; // Extra bytes
401 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700402 assertThat(ipAddress.toString(),
403 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
404 }
405
406 /**
407 * Tests invalid valueOf() converger for an array and an invalid offset
408 * for IPv4.
409 */
410 @Test(expected = IllegalArgumentException.class)
411 public void testInvalidValueOfArrayInvalidOffsetIPv4() {
412 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800413 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700414
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800415 value = new byte[] {11, 22, 33, // Preamble
416 1, 2, 3, 4,
417 44, 55}; // Extra bytes
418 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 6);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700419 }
420
421 /**
422 * Tests invalid valueOf() converger for an array and an invalid offset
423 * for IPv6.
424 */
425 @Test(expected = IllegalArgumentException.class)
426 public void testInvalidValueOfArrayInvalidOffsetIPv6() {
427 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800428 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700429
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800430 value = new byte[] {11, 22, 33, // Preamble
431 0x11, 0x11, 0x22, 0x22,
432 0x33, 0x33, 0x44, 0x44,
433 0x55, 0x55, 0x66, 0x66,
434 0x77, 0x77,
435 (byte) 0x88, (byte) 0x88,
436 44, 55}; // Extra bytes
437 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 6);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700438 }
439
440 /**
Pavlin Radoslavovaf5ff792014-10-31 20:51:47 -0700441 * Tests valueOf() converter for IPv4 InetAddress.
442 */
443 @Test
444 public void testValueOfInetAddressIPv4() {
445 IpAddress ipAddress;
446 InetAddress inetAddress;
447
448 inetAddress = InetAddresses.forString("1.2.3.4");
449 ipAddress = IpAddress.valueOf(inetAddress);
450 assertThat(ipAddress.toString(), is("1.2.3.4"));
451
452 inetAddress = InetAddresses.forString("0.0.0.0");
453 ipAddress = IpAddress.valueOf(inetAddress);
454 assertThat(ipAddress.toString(), is("0.0.0.0"));
455
456 inetAddress = InetAddresses.forString("255.255.255.255");
457 ipAddress = IpAddress.valueOf(inetAddress);
458 assertThat(ipAddress.toString(), is("255.255.255.255"));
459 }
460
461 /**
462 * Tests valueOf() converter for IPv6 InetAddress.
463 */
464 @Test
465 public void testValueOfInetAddressIPv6() {
466 IpAddress ipAddress;
467 InetAddress inetAddress;
468
469 inetAddress =
470 InetAddresses.forString("1111:2222:3333:4444:5555:6666:7777:8888");
471 ipAddress = IpAddress.valueOf(inetAddress);
472 assertThat(ipAddress.toString(),
473 is("1111:2222:3333:4444:5555:6666:7777:8888"));
474
475 inetAddress = InetAddresses.forString("::");
476 ipAddress = IpAddress.valueOf(inetAddress);
477 assertThat(ipAddress.toString(), is("::"));
478
479 inetAddress =
480 InetAddresses.forString("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
481 ipAddress = IpAddress.valueOf(inetAddress);
482 assertThat(ipAddress.toString(),
483 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
484 }
485
486 /**
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700487 * Tests valueOf() converter for IPv4 string.
488 */
489 @Test
490 public void testValueOfStringIPv4() {
491 IpAddress ipAddress;
492
493 ipAddress = IpAddress.valueOf("1.2.3.4");
494 assertThat(ipAddress.toString(), is("1.2.3.4"));
495
496 ipAddress = IpAddress.valueOf("0.0.0.0");
497 assertThat(ipAddress.toString(), is("0.0.0.0"));
498
499 ipAddress = IpAddress.valueOf("255.255.255.255");
500 assertThat(ipAddress.toString(), is("255.255.255.255"));
501 }
502
503 /**
504 * Tests valueOf() converter for IPv6 string.
505 */
506 @Test
507 public void testValueOfStringIPv6() {
508 IpAddress ipAddress;
509
510 ipAddress =
511 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
512 assertThat(ipAddress.toString(),
513 is("1111:2222:3333:4444:5555:6666:7777:8888"));
514
515 ipAddress = IpAddress.valueOf("::");
516 assertThat(ipAddress.toString(), is("::"));
517
518 ipAddress =
519 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
520 assertThat(ipAddress.toString(),
521 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
522 }
523
524 /**
525 * Tests invalid valueOf() converter for a null string.
526 */
527 @Test(expected = NullPointerException.class)
528 public void testInvalidValueOfNullString() {
529 IpAddress ipAddress;
530
531 String fromString = null;
532 ipAddress = IpAddress.valueOf(fromString);
533 }
534
535 /**
536 * Tests invalid valueOf() converter for an empty string.
537 */
538 @Test(expected = IllegalArgumentException.class)
539 public void testInvalidValueOfEmptyString() {
540 IpAddress ipAddress;
541
542 String fromString = "";
543 ipAddress = IpAddress.valueOf(fromString);
544 }
545
546 /**
547 * Tests invalid valueOf() converter for an incorrect string.
548 */
549 @Test(expected = IllegalArgumentException.class)
550 public void testInvalidValueOfIncorrectString() {
551 IpAddress ipAddress;
552
553 String fromString = "NoSuchIpAddress";
554 ipAddress = IpAddress.valueOf(fromString);
555 }
556
557 /**
558 * Tests making a mask prefix for a given prefix length for IPv4.
559 */
560 @Test
561 public void testMakeMaskPrefixIPv4() {
562 IpAddress ipAddress;
563
564 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 25);
565 assertThat(ipAddress.toString(), is("255.255.255.128"));
566
567 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 0);
568 assertThat(ipAddress.toString(), is("0.0.0.0"));
569
570 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 32);
571 assertThat(ipAddress.toString(), is("255.255.255.255"));
572 }
573
574 /**
575 * Tests making a mask prefix for a given prefix length for IPv6.
576 */
577 @Test
578 public void testMakeMaskPrefixIPv6() {
579 IpAddress ipAddress;
580
581 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 8);
582 assertThat(ipAddress.toString(), is("ff00::"));
583
584 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 120);
585 assertThat(ipAddress.toString(),
586 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00"));
587
588 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 0);
589 assertThat(ipAddress.toString(), is("::"));
590
591 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 128);
592 assertThat(ipAddress.toString(),
593 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
594
595 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 64);
596 assertThat(ipAddress.toString(), is("ffff:ffff:ffff:ffff::"));
597 }
598
599 /**
600 * Tests making a mask prefix for an invalid prefix length for IPv4:
601 * negative prefix length.
602 */
603 @Test(expected = IllegalArgumentException.class)
604 public void testInvalidMakeNegativeMaskPrefixIPv4() {
605 IpAddress ipAddress;
606
607 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, -1);
608 }
609
610 /**
611 * Tests making a mask prefix for an invalid prefix length for IPv6:
612 * negative prefix length.
613 */
614 @Test(expected = IllegalArgumentException.class)
615 public void testInvalidMakeNegativeMaskPrefixIPv6() {
616 IpAddress ipAddress;
617
618 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, -1);
619 }
620
621 /**
622 * Tests making a mask prefix for an invalid prefix length for IPv4:
623 * too long prefix length.
624 */
625 @Test(expected = IllegalArgumentException.class)
626 public void testInvalidMakeTooLongMaskPrefixIPv4() {
627 IpAddress ipAddress;
628
629 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 33);
630 }
631
632 /**
633 * Tests making a mask prefix for an invalid prefix length for IPv6:
634 * too long prefix length.
635 */
636 @Test(expected = IllegalArgumentException.class)
637 public void testInvalidMakeTooLongMaskPrefixIPv6() {
638 IpAddress ipAddress;
639
640 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 129);
641 }
642
643 /**
644 * Tests making of a masked address for IPv4.
645 */
646 @Test
647 public void testMakeMaskedAddressIPv4() {
648 IpAddress ipAddress = IpAddress.valueOf("1.2.3.5");
649 IpAddress ipAddressMasked;
650
651 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 24);
652 assertThat(ipAddressMasked.toString(), is("1.2.3.0"));
653
654 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0);
655 assertThat(ipAddressMasked.toString(), is("0.0.0.0"));
656
657 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 32);
658 assertThat(ipAddressMasked.toString(), is("1.2.3.5"));
659 }
660
661 /**
662 * Tests making of a masked address for IPv6.
663 */
664 @Test
665 public void testMakeMaskedAddressIPv6() {
666 IpAddress ipAddress =
667 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
668 IpAddress ipAddressMasked;
669
670 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 8);
671 assertThat(ipAddressMasked.toString(), is("1100::"));
672
673 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 120);
674 assertThat(ipAddressMasked.toString(),
675 is("1111:2222:3333:4444:5555:6666:7777:8800"));
676
677 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0);
678 assertThat(ipAddressMasked.toString(), is("::"));
679
680 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 128);
681 assertThat(ipAddressMasked.toString(),
682 is("1111:2222:3333:4444:5555:6666:7777:8885"));
683
684 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 64);
685 assertThat(ipAddressMasked.toString(), is("1111:2222:3333:4444::"));
686 }
687
688 /**
689 * Tests making of a masked address for invalid prefix length for IPv4:
690 * negative prefix length.
691 */
692 @Test(expected = IllegalArgumentException.class)
693 public void testInvalidMakeNegativeMaskedAddressIPv4() {
694 IpAddress ipAddress = IpAddress.valueOf("1.2.3.5");
695 IpAddress ipAddressMasked;
696
697 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1);
698 }
699
700 /**
701 * Tests making of a masked address for invalid prefix length for IPv6:
702 * negative prefix length.
703 */
704 @Test(expected = IllegalArgumentException.class)
705 public void testInvalidMakeNegativeMaskedAddressIPv6() {
706 IpAddress ipAddress =
707 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
708 IpAddress ipAddressMasked;
709
710 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1);
711 }
712
713 /**
714 * Tests making of a masked address for an invalid prefix length for IPv4:
715 * too long prefix length.
716 */
717 @Test(expected = IllegalArgumentException.class)
718 public void testInvalidMakeTooLongMaskedAddressIPv4() {
719 IpAddress ipAddress = IpAddress.valueOf("1.2.3.5");
720 IpAddress ipAddressMasked;
721
722 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 33);
723 }
724
725 /**
726 * Tests making of a masked address for an invalid prefix length for IPv6:
727 * too long prefix length.
728 */
729 @Test(expected = IllegalArgumentException.class)
730 public void testInvalidMakeTooLongMaskedAddressIPv6() {
731 IpAddress ipAddress =
732 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
733 IpAddress ipAddressMasked;
734
735 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 129);
736 }
737
738 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800739 * Tests if address is zero for IPv4.
740 */
741 @Test
742 public void testIsZeroIPv4() {
743 IpAddress normalIP = IpAddress.valueOf("10.0.0.1");
744 IpAddress zeroIP = IpAddress.valueOf("0.0.0.0");
745 assertFalse(normalIP.isZero());
746 assertTrue(zeroIP.isZero());
747 }
748
749 /**
750 * Tests if address is zero for IPv6.
751 */
752 @Test
753 public void testIsZeroIPv6() {
754 IpAddress normalIP = IpAddress.valueOf("fe80::1");
755 IpAddress zeroIP = IpAddress.valueOf("::");
756 assertFalse(normalIP.isZero());
757 assertTrue(zeroIP.isZero());
758 }
759
760 /**
Thomas Vachuskae7966102015-09-09 17:33:33 -0700761 * Tests if address is self-assigned for IPv4.
762 */
763 @Test
764 public void testIsSelfAssignedIpv4() {
765 IpAddress normalIP = IpAddress.valueOf("10.0.0.1");
Eduardo Ferreiracf8ee3c2018-01-24 18:59:43 -0200766 IpAddress selfAssignedIP = IpAddress.valueOf("169.254.2.3");
Thomas Vachuskae7966102015-09-09 17:33:33 -0700767 assertFalse(normalIP.isSelfAssigned());
768 assertTrue(selfAssignedIP.isSelfAssigned());
769 }
770
771 /**
Charles Chan4ca8e602016-02-25 18:05:59 -0800772 * Tests if the address is a multicast address.
773 */
774 @Test
775 public void testIsMulticast() {
776 IpAddress v4Unicast = IpAddress.valueOf("10.0.0.1");
777 IpAddress v4Multicast = IpAddress.valueOf("224.0.0.1");
778 IpAddress v6Unicast = IpAddress.valueOf("1000::1");
779 IpAddress v6Multicast = IpAddress.valueOf("ff02::1");
780 assertFalse(v4Unicast.isMulticast());
781 assertTrue(v4Multicast.isMulticast());
782 assertFalse(v6Unicast.isMulticast());
783 assertTrue(v6Multicast.isMulticast());
784 }
785
786 /**
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700787 * Tests comparison of {@link IpAddress} for IPv4.
788 */
789 @Test
790 public void testComparisonIPv4() {
791 IpAddress addr1, addr2, addr3, addr4;
792
793 addr1 = IpAddress.valueOf("1.2.3.4");
794 addr2 = IpAddress.valueOf("1.2.3.4");
795 addr3 = IpAddress.valueOf("1.2.3.3");
796 addr4 = IpAddress.valueOf("1.2.3.5");
797 assertTrue(addr1.compareTo(addr2) == 0);
798 assertTrue(addr1.compareTo(addr3) > 0);
799 assertTrue(addr1.compareTo(addr4) < 0);
800
801 addr1 = IpAddress.valueOf("255.2.3.4");
802 addr2 = IpAddress.valueOf("255.2.3.4");
803 addr3 = IpAddress.valueOf("255.2.3.3");
804 addr4 = IpAddress.valueOf("255.2.3.5");
805 assertTrue(addr1.compareTo(addr2) == 0);
806 assertTrue(addr1.compareTo(addr3) > 0);
807 assertTrue(addr1.compareTo(addr4) < 0);
808 }
809
810 /**
811 * Tests comparison of {@link IpAddress} for IPv6.
812 */
813 @Test
814 public void testComparisonIPv6() {
815 IpAddress addr1, addr2, addr3, addr4;
816
817 addr1 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
818 addr2 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
819 addr3 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8887");
820 addr4 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8889");
821 assertTrue(addr1.compareTo(addr2) == 0);
822 assertTrue(addr1.compareTo(addr3) > 0);
823 assertTrue(addr1.compareTo(addr4) < 0);
824
825 addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
826 addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
827 addr3 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8887");
828 addr4 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8889");
829 assertTrue(addr1.compareTo(addr2) == 0);
830 assertTrue(addr1.compareTo(addr3) > 0);
831 assertTrue(addr1.compareTo(addr4) < 0);
832
833 addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
834 addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
835 addr3 = IpAddress.valueOf("ffff:2222:3333:4443:5555:6666:7777:8888");
836 addr4 = IpAddress.valueOf("ffff:2222:3333:4445:5555:6666:7777:8888");
837 assertTrue(addr1.compareTo(addr2) == 0);
838 assertTrue(addr1.compareTo(addr3) > 0);
839 assertTrue(addr1.compareTo(addr4) < 0);
840 }
841
842 /**
843 * Tests equality of {@link IpAddress} for IPv4.
844 */
845 @Test
846 public void testEqualityIPv4() {
Pavlin Radoslavov854ecab2014-11-03 09:43:25 -0800847 new EqualsTester()
848 .addEqualityGroup(IpAddress.valueOf("1.2.3.4"),
849 IpAddress.valueOf("1.2.3.4"))
850 .addEqualityGroup(IpAddress.valueOf("1.2.3.5"),
851 IpAddress.valueOf("1.2.3.5"))
852 .addEqualityGroup(IpAddress.valueOf("0.0.0.0"),
853 IpAddress.valueOf("0.0.0.0"))
854 .addEqualityGroup(IpAddress.valueOf("255.255.255.255"),
855 IpAddress.valueOf("255.255.255.255"))
856 .testEquals();
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700857 }
858
859 /**
860 * Tests equality of {@link IpAddress} for IPv6.
861 */
862 @Test
863 public void testEqualityIPv6() {
Pavlin Radoslavov854ecab2014-11-03 09:43:25 -0800864 new EqualsTester()
865 .addEqualityGroup(
866 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"),
867 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"))
868 .addEqualityGroup(
869 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"),
870 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"))
871 .addEqualityGroup(
872 IpAddress.valueOf("::"),
873 IpAddress.valueOf("::"))
874 .addEqualityGroup(
875 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
876 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))
877 .testEquals();
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700878 }
879
880 /**
881 * Tests object string representation for IPv4.
882 */
883 @Test
884 public void testToStringIPv4() {
885 IpAddress ipAddress;
886
887 ipAddress = IpAddress.valueOf("1.2.3.4");
888 assertThat(ipAddress.toString(), is("1.2.3.4"));
889
890 ipAddress = IpAddress.valueOf("0.0.0.0");
891 assertThat(ipAddress.toString(), is("0.0.0.0"));
892
893 ipAddress = IpAddress.valueOf("255.255.255.255");
894 assertThat(ipAddress.toString(), is("255.255.255.255"));
895 }
896
897 /**
898 * Tests object string representation for IPv6.
899 */
900 @Test
901 public void testToStringIPv6() {
902 IpAddress ipAddress;
903
904 ipAddress =
905 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
906 assertThat(ipAddress.toString(),
907 is("1111:2222:3333:4444:5555:6666:7777:8888"));
908
909 ipAddress = IpAddress.valueOf("1111::8888");
910 assertThat(ipAddress.toString(), is("1111::8888"));
911
912 ipAddress = IpAddress.valueOf("1111::");
913 assertThat(ipAddress.toString(), is("1111::"));
914
915 ipAddress = IpAddress.valueOf("::8888");
916 assertThat(ipAddress.toString(), is("::8888"));
917
918 ipAddress = IpAddress.valueOf("::");
919 assertThat(ipAddress.toString(), is("::"));
920
921 ipAddress =
922 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
923 assertThat(ipAddress.toString(),
924 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
Aaron Kruglikovafdf4de2015-06-24 09:28:22 -0700925
926 ipAddress =
927 IpAddress.valueOf("::1111:2222");
928 assertThat(ipAddress.toString(),
929 is("::1111:2222"));
930
931 ipAddress =
932 IpAddress.valueOf("1:0:0:1:0:0:2:3");
933 assertThat(ipAddress.toString(),
934 is("1::1:0:0:2:3"));
935
936 ipAddress =
937 IpAddress.valueOf("::0123:0004");
938 assertThat(ipAddress.toString(),
939 is("::123:4"));
940
941 ipAddress =
942 IpAddress.valueOf("0:0:1:1:0:0:1:1");
943 assertThat(ipAddress.toString(),
944 is("::1:1:0:0:1:1"));
945
946 ipAddress =
947 IpAddress.valueOf("1:1a2b::");
948 assertThat(ipAddress.toString(),
949 is("1:1a2b::"));
950
951 ipAddress =
952 IpAddress.valueOf("0:0:00:00:0000:00:00:000");
953 assertThat(ipAddress.toString(),
954 is("::"));
955
956 ipAddress =
957 IpAddress.valueOf("0:0:0:1:0:0:0:0");
958 assertThat(ipAddress.toString(),
959 is("0:0:0:1::"));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700960 }
961}