blob: f0e6ae7ae9fc8b3cb23c8cb2f78fb4bf3b8ba0f8 [file] [log] [blame]
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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 Radoslavov34c81642014-11-04 16:21:38 -080081 * Tests getting the Ip4Address and Ip6Address view of the IP address.
82 */
83 @Test
84 public void testGetIp4AndIp6AddressView() {
85 IpAddress ipAddress;
86 Ip4Address ip4Address;
87 Ip6Address ip6Address;
88
89 // Pure IPv4 IpAddress
90 ipAddress = IpAddress.valueOf("1.2.3.4");
91 ip4Address = ipAddress.getIp4Address();
92 ip6Address = ipAddress.getIp6Address();
93 assertThat(ip4Address.toString(), is("1.2.3.4"));
94 assertNull(ip6Address);
95
96 // IPv4 IpAddress that is Ip4Address
97 ipAddress = Ip4Address.valueOf("1.2.3.4");
98 ip4Address = ipAddress.getIp4Address();
99 ip6Address = ipAddress.getIp6Address();
100 assertThat(ip4Address.toString(), is("1.2.3.4"));
101 assertNull(ip6Address);
102
103 // Pure IPv6 IpAddress
104 ipAddress = IpAddress.valueOf("1111:2222::");
105 ip4Address = ipAddress.getIp4Address();
106 ip6Address = ipAddress.getIp6Address();
107 assertNull(ip4Address);
108 assertThat(ip6Address.toString(), is("1111:2222::"));
109
110 // IPv6 IpAddress that is Ip6Address
111 ipAddress = Ip6Address.valueOf("1111:2222::");
112 ip4Address = ipAddress.getIp4Address();
113 ip6Address = ipAddress.getIp6Address();
114 assertNull(ip4Address);
115 assertThat(ip6Address.toString(), is("1111:2222::"));
116 }
117
118 /**
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700119 * Tests returning an IPv4 address as a byte array.
120 */
121 @Test
122 public void testAddressToOctetsIPv4() {
123 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800124 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700125
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800126 value = new byte[] {1, 2, 3, 4};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700127 ipAddress = IpAddress.valueOf("1.2.3.4");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800128 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700129
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800130 value = new byte[] {0, 0, 0, 0};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700131 ipAddress = IpAddress.valueOf("0.0.0.0");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800132 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700133
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800134 value = new byte[] {(byte) 0xff, (byte) 0xff,
135 (byte) 0xff, (byte) 0xff};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700136 ipAddress = IpAddress.valueOf("255.255.255.255");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800137 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700138 }
139
140 /**
141 * Tests returning an IPv6 address as a byte array.
142 */
143 @Test
144 public void testAddressToOctetsIPv6() {
145 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800146 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700147
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800148 value = new byte[] {0x11, 0x11, 0x22, 0x22,
149 0x33, 0x33, 0x44, 0x44,
150 0x55, 0x55, 0x66, 0x66,
151 0x77, 0x77,
152 (byte) 0x88, (byte) 0x88};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700153 ipAddress =
154 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800155 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700156
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800157 value = new byte[] {0x00, 0x00, 0x00, 0x00,
158 0x00, 0x00, 0x00, 0x00,
159 0x00, 0x00, 0x00, 0x00,
160 0x00, 0x00, 0x00, 0x00};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700161 ipAddress = IpAddress.valueOf("::");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800162 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700163
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800164 value = new byte[] {(byte) 0xff, (byte) 0xff,
165 (byte) 0xff, (byte) 0xff,
166 (byte) 0xff, (byte) 0xff,
167 (byte) 0xff, (byte) 0xff,
168 (byte) 0xff, (byte) 0xff,
169 (byte) 0xff, (byte) 0xff,
170 (byte) 0xff, (byte) 0xff,
171 (byte) 0xff, (byte) 0xff};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700172 ipAddress =
173 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800174 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700175 }
176
177 /**
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800178 * Tests valueOf() converter for IPv4 integer value.
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700179 */
180 @Test
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800181 public void testValueOfForIntegerIPv4() {
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700182 IpAddress ipAddress;
183
184 ipAddress = IpAddress.valueOf(0x01020304);
185 assertThat(ipAddress.toString(), is("1.2.3.4"));
186
187 ipAddress = IpAddress.valueOf(0);
188 assertThat(ipAddress.toString(), is("0.0.0.0"));
189
190 ipAddress = IpAddress.valueOf(0xffffffff);
191 assertThat(ipAddress.toString(), is("255.255.255.255"));
192 }
193
194 /**
195 * Tests valueOf() converter for IPv4 byte array.
196 */
197 @Test
198 public void testValueOfByteArrayIPv4() {
199 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800200 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700201
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800202 value = new byte[] {1, 2, 3, 4};
203 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700204 assertThat(ipAddress.toString(), is("1.2.3.4"));
205
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800206 value = new byte[] {0, 0, 0, 0};
207 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700208 assertThat(ipAddress.toString(), is("0.0.0.0"));
209
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800210 value = new byte[] {(byte) 0xff, (byte) 0xff,
211 (byte) 0xff, (byte) 0xff};
212 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700213 assertThat(ipAddress.toString(), is("255.255.255.255"));
214 }
215
216 /**
217 * Tests valueOf() converter for IPv6 byte array.
218 */
219 @Test
220 public void testValueOfByteArrayIPv6() {
221 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800222 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700223
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800224 value = new byte[] {0x11, 0x11, 0x22, 0x22,
225 0x33, 0x33, 0x44, 0x44,
226 0x55, 0x55, 0x66, 0x66,
227 0x77, 0x77,
228 (byte) 0x88, (byte) 0x88};
229 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700230 assertThat(ipAddress.toString(),
231 is("1111:2222:3333:4444:5555:6666:7777:8888"));
232
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800233 value = new byte[] {0x00, 0x00, 0x00, 0x00,
234 0x00, 0x00, 0x00, 0x00,
235 0x00, 0x00, 0x00, 0x00,
236 0x00, 0x00, 0x00, 0x00};
237 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700238 assertThat(ipAddress.toString(), is("::"));
239
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800240 value = new byte[] {(byte) 0xff, (byte) 0xff,
241 (byte) 0xff, (byte) 0xff,
242 (byte) 0xff, (byte) 0xff,
243 (byte) 0xff, (byte) 0xff,
244 (byte) 0xff, (byte) 0xff,
245 (byte) 0xff, (byte) 0xff,
246 (byte) 0xff, (byte) 0xff,
247 (byte) 0xff, (byte) 0xff};
248 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700249 assertThat(ipAddress.toString(),
250 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
251 }
252
253 /**
254 * Tests invalid valueOf() converter for a null array for IPv4.
255 */
256 @Test(expected = NullPointerException.class)
257 public void testInvalidValueOfNullArrayIPv4() {
258 IpAddress ipAddress;
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800259 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700260
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800261 value = null;
262 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700263 }
264
265 /**
266 * Tests invalid valueOf() converter for a null array for IPv6.
267 */
268 @Test(expected = NullPointerException.class)
269 public void testInvalidValueOfNullArrayIPv6() {
270 IpAddress ipAddress;
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800271 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700272
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800273 value = null;
274 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700275 }
276
277 /**
278 * Tests invalid valueOf() converger for an array that is too short for
279 * IPv4.
280 */
281 @Test(expected = IllegalArgumentException.class)
282 public void testInvalidValueOfShortArrayIPv4() {
283 IpAddress ipAddress;
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800284 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700285
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800286 value = new byte[] {1, 2, 3};
287 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700288 }
289
290 /**
291 * Tests invalid valueOf() converger for an array that is too short for
292 * IPv6.
293 */
294 @Test(expected = IllegalArgumentException.class)
295 public void testInvalidValueOfShortArrayIPv6() {
296 IpAddress ipAddress;
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800297 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700298
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800299 value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
300 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700301 }
302
303 /**
304 * Tests valueOf() converter for IPv4 byte array and an offset.
305 */
306 @Test
307 public void testValueOfByteArrayOffsetIPv4() {
308 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800309 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700310
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800311 value = new byte[] {11, 22, 33, // Preamble
312 1, 2, 3, 4,
313 44, 55}; // Extra bytes
314 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 3);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700315 assertThat(ipAddress.toString(), is("1.2.3.4"));
316
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800317 value = new byte[] {11, 22, // Preamble
318 0, 0, 0, 0,
319 33}; // Extra bytes
320 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700321 assertThat(ipAddress.toString(), is("0.0.0.0"));
322
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800323 value = new byte[] {11, 22, // Preamble
324 (byte) 0xff, (byte) 0xff,
325 (byte) 0xff, (byte) 0xff,
326 33}; // Extra bytes
327 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700328 assertThat(ipAddress.toString(), is("255.255.255.255"));
329 }
330
331 /**
332 * Tests valueOf() converter for IPv6 byte array and an offset.
333 */
334 @Test
335 public void testValueOfByteArrayOffsetIPv6() {
336 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800337 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700338
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800339 value = new byte[] {11, 22, 33, // Preamble
340 0x11, 0x11, 0x22, 0x22,
341 0x33, 0x33, 0x44, 0x44,
342 0x55, 0x55, 0x66, 0x66,
343 0x77, 0x77,
344 (byte) 0x88, (byte) 0x88,
345 44, 55}; // Extra bytes
346 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 3);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700347 assertThat(ipAddress.toString(),
348 is("1111:2222:3333:4444:5555:6666:7777:8888"));
349
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800350 value = new byte[] {11, 22, // Preamble
351 0x00, 0x00, 0x00, 0x00,
352 0x00, 0x00, 0x00, 0x00,
353 0x00, 0x00, 0x00, 0x00,
354 0x00, 0x00, 0x00, 0x00,
355 33}; // Extra bytes
356 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700357 assertThat(ipAddress.toString(), is("::"));
358
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800359 value = new byte[] {11, 22, // Preamble
360 (byte) 0xff, (byte) 0xff,
361 (byte) 0xff, (byte) 0xff,
362 (byte) 0xff, (byte) 0xff,
363 (byte) 0xff, (byte) 0xff,
364 (byte) 0xff, (byte) 0xff,
365 (byte) 0xff, (byte) 0xff,
366 (byte) 0xff, (byte) 0xff,
367 (byte) 0xff, (byte) 0xff,
368 33}; // Extra bytes
369 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700370 assertThat(ipAddress.toString(),
371 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
372 }
373
374 /**
375 * Tests invalid valueOf() converger for an array and an invalid offset
376 * for IPv4.
377 */
378 @Test(expected = IllegalArgumentException.class)
379 public void testInvalidValueOfArrayInvalidOffsetIPv4() {
380 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800381 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700382
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800383 value = new byte[] {11, 22, 33, // Preamble
384 1, 2, 3, 4,
385 44, 55}; // Extra bytes
386 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 6);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700387 }
388
389 /**
390 * Tests invalid valueOf() converger for an array and an invalid offset
391 * for IPv6.
392 */
393 @Test(expected = IllegalArgumentException.class)
394 public void testInvalidValueOfArrayInvalidOffsetIPv6() {
395 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800396 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700397
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800398 value = new byte[] {11, 22, 33, // Preamble
399 0x11, 0x11, 0x22, 0x22,
400 0x33, 0x33, 0x44, 0x44,
401 0x55, 0x55, 0x66, 0x66,
402 0x77, 0x77,
403 (byte) 0x88, (byte) 0x88,
404 44, 55}; // Extra bytes
405 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 6);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700406 }
407
408 /**
Pavlin Radoslavovaf5ff792014-10-31 20:51:47 -0700409 * Tests valueOf() converter for IPv4 InetAddress.
410 */
411 @Test
412 public void testValueOfInetAddressIPv4() {
413 IpAddress ipAddress;
414 InetAddress inetAddress;
415
416 inetAddress = InetAddresses.forString("1.2.3.4");
417 ipAddress = IpAddress.valueOf(inetAddress);
418 assertThat(ipAddress.toString(), is("1.2.3.4"));
419
420 inetAddress = InetAddresses.forString("0.0.0.0");
421 ipAddress = IpAddress.valueOf(inetAddress);
422 assertThat(ipAddress.toString(), is("0.0.0.0"));
423
424 inetAddress = InetAddresses.forString("255.255.255.255");
425 ipAddress = IpAddress.valueOf(inetAddress);
426 assertThat(ipAddress.toString(), is("255.255.255.255"));
427 }
428
429 /**
430 * Tests valueOf() converter for IPv6 InetAddress.
431 */
432 @Test
433 public void testValueOfInetAddressIPv6() {
434 IpAddress ipAddress;
435 InetAddress inetAddress;
436
437 inetAddress =
438 InetAddresses.forString("1111:2222:3333:4444:5555:6666:7777:8888");
439 ipAddress = IpAddress.valueOf(inetAddress);
440 assertThat(ipAddress.toString(),
441 is("1111:2222:3333:4444:5555:6666:7777:8888"));
442
443 inetAddress = InetAddresses.forString("::");
444 ipAddress = IpAddress.valueOf(inetAddress);
445 assertThat(ipAddress.toString(), is("::"));
446
447 inetAddress =
448 InetAddresses.forString("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
449 ipAddress = IpAddress.valueOf(inetAddress);
450 assertThat(ipAddress.toString(),
451 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
452 }
453
454 /**
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700455 * Tests valueOf() converter for IPv4 string.
456 */
457 @Test
458 public void testValueOfStringIPv4() {
459 IpAddress ipAddress;
460
461 ipAddress = IpAddress.valueOf("1.2.3.4");
462 assertThat(ipAddress.toString(), is("1.2.3.4"));
463
464 ipAddress = IpAddress.valueOf("0.0.0.0");
465 assertThat(ipAddress.toString(), is("0.0.0.0"));
466
467 ipAddress = IpAddress.valueOf("255.255.255.255");
468 assertThat(ipAddress.toString(), is("255.255.255.255"));
469 }
470
471 /**
472 * Tests valueOf() converter for IPv6 string.
473 */
474 @Test
475 public void testValueOfStringIPv6() {
476 IpAddress ipAddress;
477
478 ipAddress =
479 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
480 assertThat(ipAddress.toString(),
481 is("1111:2222:3333:4444:5555:6666:7777:8888"));
482
483 ipAddress = IpAddress.valueOf("::");
484 assertThat(ipAddress.toString(), is("::"));
485
486 ipAddress =
487 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
488 assertThat(ipAddress.toString(),
489 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
490 }
491
492 /**
493 * Tests invalid valueOf() converter for a null string.
494 */
495 @Test(expected = NullPointerException.class)
496 public void testInvalidValueOfNullString() {
497 IpAddress ipAddress;
498
499 String fromString = null;
500 ipAddress = IpAddress.valueOf(fromString);
501 }
502
503 /**
504 * Tests invalid valueOf() converter for an empty string.
505 */
506 @Test(expected = IllegalArgumentException.class)
507 public void testInvalidValueOfEmptyString() {
508 IpAddress ipAddress;
509
510 String fromString = "";
511 ipAddress = IpAddress.valueOf(fromString);
512 }
513
514 /**
515 * Tests invalid valueOf() converter for an incorrect string.
516 */
517 @Test(expected = IllegalArgumentException.class)
518 public void testInvalidValueOfIncorrectString() {
519 IpAddress ipAddress;
520
521 String fromString = "NoSuchIpAddress";
522 ipAddress = IpAddress.valueOf(fromString);
523 }
524
525 /**
526 * Tests making a mask prefix for a given prefix length for IPv4.
527 */
528 @Test
529 public void testMakeMaskPrefixIPv4() {
530 IpAddress ipAddress;
531
532 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 25);
533 assertThat(ipAddress.toString(), is("255.255.255.128"));
534
535 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 0);
536 assertThat(ipAddress.toString(), is("0.0.0.0"));
537
538 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 32);
539 assertThat(ipAddress.toString(), is("255.255.255.255"));
540 }
541
542 /**
543 * Tests making a mask prefix for a given prefix length for IPv6.
544 */
545 @Test
546 public void testMakeMaskPrefixIPv6() {
547 IpAddress ipAddress;
548
549 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 8);
550 assertThat(ipAddress.toString(), is("ff00::"));
551
552 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 120);
553 assertThat(ipAddress.toString(),
554 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00"));
555
556 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 0);
557 assertThat(ipAddress.toString(), is("::"));
558
559 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 128);
560 assertThat(ipAddress.toString(),
561 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
562
563 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 64);
564 assertThat(ipAddress.toString(), is("ffff:ffff:ffff:ffff::"));
565 }
566
567 /**
568 * Tests making a mask prefix for an invalid prefix length for IPv4:
569 * negative prefix length.
570 */
571 @Test(expected = IllegalArgumentException.class)
572 public void testInvalidMakeNegativeMaskPrefixIPv4() {
573 IpAddress ipAddress;
574
575 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, -1);
576 }
577
578 /**
579 * Tests making a mask prefix for an invalid prefix length for IPv6:
580 * negative prefix length.
581 */
582 @Test(expected = IllegalArgumentException.class)
583 public void testInvalidMakeNegativeMaskPrefixIPv6() {
584 IpAddress ipAddress;
585
586 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, -1);
587 }
588
589 /**
590 * Tests making a mask prefix for an invalid prefix length for IPv4:
591 * too long prefix length.
592 */
593 @Test(expected = IllegalArgumentException.class)
594 public void testInvalidMakeTooLongMaskPrefixIPv4() {
595 IpAddress ipAddress;
596
597 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 33);
598 }
599
600 /**
601 * Tests making a mask prefix for an invalid prefix length for IPv6:
602 * too long prefix length.
603 */
604 @Test(expected = IllegalArgumentException.class)
605 public void testInvalidMakeTooLongMaskPrefixIPv6() {
606 IpAddress ipAddress;
607
608 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 129);
609 }
610
611 /**
612 * Tests making of a masked address for IPv4.
613 */
614 @Test
615 public void testMakeMaskedAddressIPv4() {
616 IpAddress ipAddress = IpAddress.valueOf("1.2.3.5");
617 IpAddress ipAddressMasked;
618
619 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 24);
620 assertThat(ipAddressMasked.toString(), is("1.2.3.0"));
621
622 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0);
623 assertThat(ipAddressMasked.toString(), is("0.0.0.0"));
624
625 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 32);
626 assertThat(ipAddressMasked.toString(), is("1.2.3.5"));
627 }
628
629 /**
630 * Tests making of a masked address for IPv6.
631 */
632 @Test
633 public void testMakeMaskedAddressIPv6() {
634 IpAddress ipAddress =
635 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
636 IpAddress ipAddressMasked;
637
638 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 8);
639 assertThat(ipAddressMasked.toString(), is("1100::"));
640
641 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 120);
642 assertThat(ipAddressMasked.toString(),
643 is("1111:2222:3333:4444:5555:6666:7777:8800"));
644
645 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0);
646 assertThat(ipAddressMasked.toString(), is("::"));
647
648 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 128);
649 assertThat(ipAddressMasked.toString(),
650 is("1111:2222:3333:4444:5555:6666:7777:8885"));
651
652 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 64);
653 assertThat(ipAddressMasked.toString(), is("1111:2222:3333:4444::"));
654 }
655
656 /**
657 * Tests making of a masked address for invalid prefix length for IPv4:
658 * negative prefix length.
659 */
660 @Test(expected = IllegalArgumentException.class)
661 public void testInvalidMakeNegativeMaskedAddressIPv4() {
662 IpAddress ipAddress = IpAddress.valueOf("1.2.3.5");
663 IpAddress ipAddressMasked;
664
665 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1);
666 }
667
668 /**
669 * Tests making of a masked address for invalid prefix length for IPv6:
670 * negative prefix length.
671 */
672 @Test(expected = IllegalArgumentException.class)
673 public void testInvalidMakeNegativeMaskedAddressIPv6() {
674 IpAddress ipAddress =
675 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
676 IpAddress ipAddressMasked;
677
678 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1);
679 }
680
681 /**
682 * Tests making of a masked address for an invalid prefix length for IPv4:
683 * too long prefix length.
684 */
685 @Test(expected = IllegalArgumentException.class)
686 public void testInvalidMakeTooLongMaskedAddressIPv4() {
687 IpAddress ipAddress = IpAddress.valueOf("1.2.3.5");
688 IpAddress ipAddressMasked;
689
690 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 33);
691 }
692
693 /**
694 * Tests making of a masked address for an invalid prefix length for IPv6:
695 * too long prefix length.
696 */
697 @Test(expected = IllegalArgumentException.class)
698 public void testInvalidMakeTooLongMaskedAddressIPv6() {
699 IpAddress ipAddress =
700 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
701 IpAddress ipAddressMasked;
702
703 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 129);
704 }
705
706 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800707 * Tests if address is zero for IPv4.
708 */
709 @Test
710 public void testIsZeroIPv4() {
711 IpAddress normalIP = IpAddress.valueOf("10.0.0.1");
712 IpAddress zeroIP = IpAddress.valueOf("0.0.0.0");
713 assertFalse(normalIP.isZero());
714 assertTrue(zeroIP.isZero());
715 }
716
717 /**
718 * Tests if address is zero for IPv6.
719 */
720 @Test
721 public void testIsZeroIPv6() {
722 IpAddress normalIP = IpAddress.valueOf("fe80::1");
723 IpAddress zeroIP = IpAddress.valueOf("::");
724 assertFalse(normalIP.isZero());
725 assertTrue(zeroIP.isZero());
726 }
727
728 /**
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700729 * Tests comparison of {@link IpAddress} for IPv4.
730 */
731 @Test
732 public void testComparisonIPv4() {
733 IpAddress addr1, addr2, addr3, addr4;
734
735 addr1 = IpAddress.valueOf("1.2.3.4");
736 addr2 = IpAddress.valueOf("1.2.3.4");
737 addr3 = IpAddress.valueOf("1.2.3.3");
738 addr4 = IpAddress.valueOf("1.2.3.5");
739 assertTrue(addr1.compareTo(addr2) == 0);
740 assertTrue(addr1.compareTo(addr3) > 0);
741 assertTrue(addr1.compareTo(addr4) < 0);
742
743 addr1 = IpAddress.valueOf("255.2.3.4");
744 addr2 = IpAddress.valueOf("255.2.3.4");
745 addr3 = IpAddress.valueOf("255.2.3.3");
746 addr4 = IpAddress.valueOf("255.2.3.5");
747 assertTrue(addr1.compareTo(addr2) == 0);
748 assertTrue(addr1.compareTo(addr3) > 0);
749 assertTrue(addr1.compareTo(addr4) < 0);
750 }
751
752 /**
753 * Tests comparison of {@link IpAddress} for IPv6.
754 */
755 @Test
756 public void testComparisonIPv6() {
757 IpAddress addr1, addr2, addr3, addr4;
758
759 addr1 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
760 addr2 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
761 addr3 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8887");
762 addr4 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8889");
763 assertTrue(addr1.compareTo(addr2) == 0);
764 assertTrue(addr1.compareTo(addr3) > 0);
765 assertTrue(addr1.compareTo(addr4) < 0);
766
767 addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
768 addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
769 addr3 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8887");
770 addr4 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8889");
771 assertTrue(addr1.compareTo(addr2) == 0);
772 assertTrue(addr1.compareTo(addr3) > 0);
773 assertTrue(addr1.compareTo(addr4) < 0);
774
775 addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
776 addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
777 addr3 = IpAddress.valueOf("ffff:2222:3333:4443:5555:6666:7777:8888");
778 addr4 = IpAddress.valueOf("ffff:2222:3333:4445:5555:6666:7777:8888");
779 assertTrue(addr1.compareTo(addr2) == 0);
780 assertTrue(addr1.compareTo(addr3) > 0);
781 assertTrue(addr1.compareTo(addr4) < 0);
782 }
783
784 /**
785 * Tests equality of {@link IpAddress} for IPv4.
786 */
787 @Test
788 public void testEqualityIPv4() {
Pavlin Radoslavov854ecab2014-11-03 09:43:25 -0800789 new EqualsTester()
790 .addEqualityGroup(IpAddress.valueOf("1.2.3.4"),
791 IpAddress.valueOf("1.2.3.4"))
792 .addEqualityGroup(IpAddress.valueOf("1.2.3.5"),
793 IpAddress.valueOf("1.2.3.5"))
794 .addEqualityGroup(IpAddress.valueOf("0.0.0.0"),
795 IpAddress.valueOf("0.0.0.0"))
796 .addEqualityGroup(IpAddress.valueOf("255.255.255.255"),
797 IpAddress.valueOf("255.255.255.255"))
798 .testEquals();
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700799 }
800
801 /**
802 * Tests equality of {@link IpAddress} for IPv6.
803 */
804 @Test
805 public void testEqualityIPv6() {
Pavlin Radoslavov854ecab2014-11-03 09:43:25 -0800806 new EqualsTester()
807 .addEqualityGroup(
808 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"),
809 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"))
810 .addEqualityGroup(
811 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"),
812 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"))
813 .addEqualityGroup(
814 IpAddress.valueOf("::"),
815 IpAddress.valueOf("::"))
816 .addEqualityGroup(
817 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
818 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))
819 .testEquals();
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700820 }
821
822 /**
823 * Tests object string representation for IPv4.
824 */
825 @Test
826 public void testToStringIPv4() {
827 IpAddress ipAddress;
828
829 ipAddress = IpAddress.valueOf("1.2.3.4");
830 assertThat(ipAddress.toString(), is("1.2.3.4"));
831
832 ipAddress = IpAddress.valueOf("0.0.0.0");
833 assertThat(ipAddress.toString(), is("0.0.0.0"));
834
835 ipAddress = IpAddress.valueOf("255.255.255.255");
836 assertThat(ipAddress.toString(), is("255.255.255.255"));
837 }
838
839 /**
840 * Tests object string representation for IPv6.
841 */
842 @Test
843 public void testToStringIPv6() {
844 IpAddress ipAddress;
845
846 ipAddress =
847 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
848 assertThat(ipAddress.toString(),
849 is("1111:2222:3333:4444:5555:6666:7777:8888"));
850
851 ipAddress = IpAddress.valueOf("1111::8888");
852 assertThat(ipAddress.toString(), is("1111::8888"));
853
854 ipAddress = IpAddress.valueOf("1111::");
855 assertThat(ipAddress.toString(), is("1111::"));
856
857 ipAddress = IpAddress.valueOf("::8888");
858 assertThat(ipAddress.toString(), is("::8888"));
859
860 ipAddress = IpAddress.valueOf("::");
861 assertThat(ipAddress.toString(), is("::"));
862
863 ipAddress =
864 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
865 assertThat(ipAddress.toString(),
866 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
867 }
868}