blob: 7d6a5de43a1356b5916898fff45de5c394c0ff9e [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
Pavlin Radoslavovaf5ff792014-10-31 20:51:47 -070018import com.google.common.net.InetAddresses;
Pavlin Radoslavov854ecab2014-11-03 09:43:25 -080019import com.google.common.testing.EqualsTester;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080020import org.junit.Ignore;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070021import org.junit.Test;
22
Thomas Vachuska9d8f72f2014-11-03 10:14:05 -080023import java.net.InetAddress;
24
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;
29import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
30
31/**
32 * Tests for class {@link IpAddress}.
33 */
34public class IpAddressTest {
35 /**
36 * Tests the immutability of {@link IpAddress}.
37 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080038 @Ignore("The class is not pure immutable, because it is not 'final'")
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070039 @Test
40 public void testImmutable() {
41 assertThatClassIsImmutable(IpAddress.class);
42 }
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 /**
707 * Tests comparison of {@link IpAddress} for IPv4.
708 */
709 @Test
710 public void testComparisonIPv4() {
711 IpAddress addr1, addr2, addr3, addr4;
712
713 addr1 = IpAddress.valueOf("1.2.3.4");
714 addr2 = IpAddress.valueOf("1.2.3.4");
715 addr3 = IpAddress.valueOf("1.2.3.3");
716 addr4 = IpAddress.valueOf("1.2.3.5");
717 assertTrue(addr1.compareTo(addr2) == 0);
718 assertTrue(addr1.compareTo(addr3) > 0);
719 assertTrue(addr1.compareTo(addr4) < 0);
720
721 addr1 = IpAddress.valueOf("255.2.3.4");
722 addr2 = IpAddress.valueOf("255.2.3.4");
723 addr3 = IpAddress.valueOf("255.2.3.3");
724 addr4 = IpAddress.valueOf("255.2.3.5");
725 assertTrue(addr1.compareTo(addr2) == 0);
726 assertTrue(addr1.compareTo(addr3) > 0);
727 assertTrue(addr1.compareTo(addr4) < 0);
728 }
729
730 /**
731 * Tests comparison of {@link IpAddress} for IPv6.
732 */
733 @Test
734 public void testComparisonIPv6() {
735 IpAddress addr1, addr2, addr3, addr4;
736
737 addr1 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
738 addr2 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
739 addr3 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8887");
740 addr4 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8889");
741 assertTrue(addr1.compareTo(addr2) == 0);
742 assertTrue(addr1.compareTo(addr3) > 0);
743 assertTrue(addr1.compareTo(addr4) < 0);
744
745 addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
746 addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
747 addr3 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8887");
748 addr4 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8889");
749 assertTrue(addr1.compareTo(addr2) == 0);
750 assertTrue(addr1.compareTo(addr3) > 0);
751 assertTrue(addr1.compareTo(addr4) < 0);
752
753 addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
754 addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
755 addr3 = IpAddress.valueOf("ffff:2222:3333:4443:5555:6666:7777:8888");
756 addr4 = IpAddress.valueOf("ffff:2222:3333:4445:5555:6666:7777:8888");
757 assertTrue(addr1.compareTo(addr2) == 0);
758 assertTrue(addr1.compareTo(addr3) > 0);
759 assertTrue(addr1.compareTo(addr4) < 0);
760 }
761
762 /**
763 * Tests equality of {@link IpAddress} for IPv4.
764 */
765 @Test
766 public void testEqualityIPv4() {
Pavlin Radoslavov854ecab2014-11-03 09:43:25 -0800767 new EqualsTester()
768 .addEqualityGroup(IpAddress.valueOf("1.2.3.4"),
769 IpAddress.valueOf("1.2.3.4"))
770 .addEqualityGroup(IpAddress.valueOf("1.2.3.5"),
771 IpAddress.valueOf("1.2.3.5"))
772 .addEqualityGroup(IpAddress.valueOf("0.0.0.0"),
773 IpAddress.valueOf("0.0.0.0"))
774 .addEqualityGroup(IpAddress.valueOf("255.255.255.255"),
775 IpAddress.valueOf("255.255.255.255"))
776 .testEquals();
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700777 }
778
779 /**
780 * Tests equality of {@link IpAddress} for IPv6.
781 */
782 @Test
783 public void testEqualityIPv6() {
Pavlin Radoslavov854ecab2014-11-03 09:43:25 -0800784 new EqualsTester()
785 .addEqualityGroup(
786 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"),
787 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"))
788 .addEqualityGroup(
789 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"),
790 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"))
791 .addEqualityGroup(
792 IpAddress.valueOf("::"),
793 IpAddress.valueOf("::"))
794 .addEqualityGroup(
795 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
796 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))
797 .testEquals();
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700798 }
799
800 /**
801 * Tests object string representation for IPv4.
802 */
803 @Test
804 public void testToStringIPv4() {
805 IpAddress ipAddress;
806
807 ipAddress = IpAddress.valueOf("1.2.3.4");
808 assertThat(ipAddress.toString(), is("1.2.3.4"));
809
810 ipAddress = IpAddress.valueOf("0.0.0.0");
811 assertThat(ipAddress.toString(), is("0.0.0.0"));
812
813 ipAddress = IpAddress.valueOf("255.255.255.255");
814 assertThat(ipAddress.toString(), is("255.255.255.255"));
815 }
816
817 /**
818 * Tests object string representation for IPv6.
819 */
820 @Test
821 public void testToStringIPv6() {
822 IpAddress ipAddress;
823
824 ipAddress =
825 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
826 assertThat(ipAddress.toString(),
827 is("1111:2222:3333:4444:5555:6666:7777:8888"));
828
829 ipAddress = IpAddress.valueOf("1111::8888");
830 assertThat(ipAddress.toString(), is("1111::8888"));
831
832 ipAddress = IpAddress.valueOf("1111::");
833 assertThat(ipAddress.toString(), is("1111::"));
834
835 ipAddress = IpAddress.valueOf("::8888");
836 assertThat(ipAddress.toString(), is("::8888"));
837
838 ipAddress = IpAddress.valueOf("::");
839 assertThat(ipAddress.toString(), is("::"));
840
841 ipAddress =
842 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
843 assertThat(ipAddress.toString(),
844 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
845 }
846}