blob: fd0eac4bb341d5c30df991df58b20048942476fa [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 Radoslavovd0e32d72014-10-31 18:11:43 -070026import static org.junit.Assert.assertThat;
27import static org.junit.Assert.assertTrue;
28import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
29
30/**
31 * Tests for class {@link IpAddress}.
32 */
33public class IpAddressTest {
34 /**
35 * Tests the immutability of {@link IpAddress}.
36 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080037 @Ignore("The class is not pure immutable, because it is not 'final'")
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070038 @Test
39 public void testImmutable() {
40 assertThatClassIsImmutable(IpAddress.class);
41 }
42
43 /**
44 * Tests the length of the address in bytes (octets).
45 */
46 @Test
47 public void testAddrByteLength() {
48 assertThat(IpAddress.INET_BYTE_LENGTH, is(4));
49 assertThat(IpAddress.INET6_BYTE_LENGTH, is(16));
50 assertThat(IpAddress.byteLength(IpAddress.Version.INET), is(4));
51 assertThat(IpAddress.byteLength(IpAddress.Version.INET6), is(16));
52 }
53
54 /**
55 * Tests the length of the address in bits.
56 */
57 @Test
58 public void testAddrBitLength() {
59 assertThat(IpAddress.INET_BIT_LENGTH, is(32));
60 assertThat(IpAddress.INET6_BIT_LENGTH, is(128));
61 }
62
63 /**
64 * Tests returning the IP address version.
65 */
66 @Test
67 public void testVersion() {
68 IpAddress ipAddress;
69
70 // IPv4
71 ipAddress = IpAddress.valueOf("0.0.0.0");
72 assertThat(ipAddress.version(), is(IpAddress.Version.INET));
73
74 // IPv6
75 ipAddress = IpAddress.valueOf("::");
76 assertThat(ipAddress.version(), is(IpAddress.Version.INET6));
77 }
78
79 /**
80 * Tests returning an IPv4 address as a byte array.
81 */
82 @Test
83 public void testAddressToOctetsIPv4() {
84 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080085 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070086
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080087 value = new byte[] {1, 2, 3, 4};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070088 ipAddress = IpAddress.valueOf("1.2.3.4");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080089 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070090
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080091 value = new byte[] {0, 0, 0, 0};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070092 ipAddress = IpAddress.valueOf("0.0.0.0");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080093 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070094
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080095 value = new byte[] {(byte) 0xff, (byte) 0xff,
96 (byte) 0xff, (byte) 0xff};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070097 ipAddress = IpAddress.valueOf("255.255.255.255");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080098 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070099 }
100
101 /**
102 * Tests returning an IPv6 address as a byte array.
103 */
104 @Test
105 public void testAddressToOctetsIPv6() {
106 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800107 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700108
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800109 value = new byte[] {0x11, 0x11, 0x22, 0x22,
110 0x33, 0x33, 0x44, 0x44,
111 0x55, 0x55, 0x66, 0x66,
112 0x77, 0x77,
113 (byte) 0x88, (byte) 0x88};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700114 ipAddress =
115 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800116 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700117
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800118 value = new byte[] {0x00, 0x00, 0x00, 0x00,
119 0x00, 0x00, 0x00, 0x00,
120 0x00, 0x00, 0x00, 0x00,
121 0x00, 0x00, 0x00, 0x00};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700122 ipAddress = IpAddress.valueOf("::");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800123 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700124
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800125 value = new byte[] {(byte) 0xff, (byte) 0xff,
126 (byte) 0xff, (byte) 0xff,
127 (byte) 0xff, (byte) 0xff,
128 (byte) 0xff, (byte) 0xff,
129 (byte) 0xff, (byte) 0xff,
130 (byte) 0xff, (byte) 0xff,
131 (byte) 0xff, (byte) 0xff,
132 (byte) 0xff, (byte) 0xff};
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700133 ipAddress =
134 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800135 assertThat(ipAddress.toOctets(), is(value));
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700136 }
137
138 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800139 * Tests returning an IPv4 address as an integer.
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700140 */
141 @Test
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800142 public void testToInt() {
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700143 IpAddress ipAddress;
144
145 ipAddress = IpAddress.valueOf("1.2.3.4");
146 assertThat(ipAddress.toInt(), is(0x01020304));
147
148 ipAddress = IpAddress.valueOf("0.0.0.0");
149 assertThat(ipAddress.toInt(), is(0));
150
151 ipAddress = IpAddress.valueOf("255.255.255.255");
152 assertThat(ipAddress.toInt(), is(-1));
153 }
154
155 /**
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800156 * Tests valueOf() converter for IPv4 integer value.
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700157 */
158 @Test
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800159 public void testValueOfForIntegerIPv4() {
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700160 IpAddress ipAddress;
161
162 ipAddress = IpAddress.valueOf(0x01020304);
163 assertThat(ipAddress.toString(), is("1.2.3.4"));
164
165 ipAddress = IpAddress.valueOf(0);
166 assertThat(ipAddress.toString(), is("0.0.0.0"));
167
168 ipAddress = IpAddress.valueOf(0xffffffff);
169 assertThat(ipAddress.toString(), is("255.255.255.255"));
170 }
171
172 /**
173 * Tests valueOf() converter for IPv4 byte array.
174 */
175 @Test
176 public void testValueOfByteArrayIPv4() {
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[] {1, 2, 3, 4};
181 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700182 assertThat(ipAddress.toString(), is("1.2.3.4"));
183
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800184 value = new byte[] {0, 0, 0, 0};
185 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700186 assertThat(ipAddress.toString(), is("0.0.0.0"));
187
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800188 value = new byte[] {(byte) 0xff, (byte) 0xff,
189 (byte) 0xff, (byte) 0xff};
190 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700191 assertThat(ipAddress.toString(), is("255.255.255.255"));
192 }
193
194 /**
195 * Tests valueOf() converter for IPv6 byte array.
196 */
197 @Test
198 public void testValueOfByteArrayIPv6() {
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[] {0x11, 0x11, 0x22, 0x22,
203 0x33, 0x33, 0x44, 0x44,
204 0x55, 0x55, 0x66, 0x66,
205 0x77, 0x77,
206 (byte) 0x88, (byte) 0x88};
207 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700208 assertThat(ipAddress.toString(),
209 is("1111:2222:3333:4444:5555:6666:7777:8888"));
210
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800211 value = new byte[] {0x00, 0x00, 0x00, 0x00,
212 0x00, 0x00, 0x00, 0x00,
213 0x00, 0x00, 0x00, 0x00,
214 0x00, 0x00, 0x00, 0x00};
215 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700216 assertThat(ipAddress.toString(), is("::"));
217
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800218 value = new byte[] {(byte) 0xff, (byte) 0xff,
219 (byte) 0xff, (byte) 0xff,
220 (byte) 0xff, (byte) 0xff,
221 (byte) 0xff, (byte) 0xff,
222 (byte) 0xff, (byte) 0xff,
223 (byte) 0xff, (byte) 0xff,
224 (byte) 0xff, (byte) 0xff,
225 (byte) 0xff, (byte) 0xff};
226 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700227 assertThat(ipAddress.toString(),
228 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
229 }
230
231 /**
232 * Tests invalid valueOf() converter for a null array for IPv4.
233 */
234 @Test(expected = NullPointerException.class)
235 public void testInvalidValueOfNullArrayIPv4() {
236 IpAddress ipAddress;
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800237 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700238
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800239 value = null;
240 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700241 }
242
243 /**
244 * Tests invalid valueOf() converter for a null array for IPv6.
245 */
246 @Test(expected = NullPointerException.class)
247 public void testInvalidValueOfNullArrayIPv6() {
248 IpAddress ipAddress;
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800249 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700250
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800251 value = null;
252 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700253 }
254
255 /**
256 * Tests invalid valueOf() converger for an array that is too short for
257 * IPv4.
258 */
259 @Test(expected = IllegalArgumentException.class)
260 public void testInvalidValueOfShortArrayIPv4() {
261 IpAddress ipAddress;
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800262 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700263
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800264 value = new byte[] {1, 2, 3};
265 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700266 }
267
268 /**
269 * Tests invalid valueOf() converger for an array that is too short for
270 * IPv6.
271 */
272 @Test(expected = IllegalArgumentException.class)
273 public void testInvalidValueOfShortArrayIPv6() {
274 IpAddress ipAddress;
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800275 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700276
Pavlin Radoslavov315d6c82014-11-04 15:36:04 -0800277 value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
278 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700279 }
280
281 /**
282 * Tests valueOf() converter for IPv4 byte array and an offset.
283 */
284 @Test
285 public void testValueOfByteArrayOffsetIPv4() {
286 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800287 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700288
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800289 value = new byte[] {11, 22, 33, // Preamble
290 1, 2, 3, 4,
291 44, 55}; // Extra bytes
292 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 3);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700293 assertThat(ipAddress.toString(), is("1.2.3.4"));
294
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800295 value = new byte[] {11, 22, // Preamble
296 0, 0, 0, 0,
297 33}; // Extra bytes
298 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700299 assertThat(ipAddress.toString(), is("0.0.0.0"));
300
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800301 value = new byte[] {11, 22, // Preamble
302 (byte) 0xff, (byte) 0xff,
303 (byte) 0xff, (byte) 0xff,
304 33}; // Extra bytes
305 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700306 assertThat(ipAddress.toString(), is("255.255.255.255"));
307 }
308
309 /**
310 * Tests valueOf() converter for IPv6 byte array and an offset.
311 */
312 @Test
313 public void testValueOfByteArrayOffsetIPv6() {
314 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800315 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700316
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800317 value = new byte[] {11, 22, 33, // Preamble
318 0x11, 0x11, 0x22, 0x22,
319 0x33, 0x33, 0x44, 0x44,
320 0x55, 0x55, 0x66, 0x66,
321 0x77, 0x77,
322 (byte) 0x88, (byte) 0x88,
323 44, 55}; // Extra bytes
324 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 3);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700325 assertThat(ipAddress.toString(),
326 is("1111:2222:3333:4444:5555:6666:7777:8888"));
327
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800328 value = new byte[] {11, 22, // Preamble
329 0x00, 0x00, 0x00, 0x00,
330 0x00, 0x00, 0x00, 0x00,
331 0x00, 0x00, 0x00, 0x00,
332 0x00, 0x00, 0x00, 0x00,
333 33}; // Extra bytes
334 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700335 assertThat(ipAddress.toString(), is("::"));
336
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800337 value = new byte[] {11, 22, // Preamble
338 (byte) 0xff, (byte) 0xff,
339 (byte) 0xff, (byte) 0xff,
340 (byte) 0xff, (byte) 0xff,
341 (byte) 0xff, (byte) 0xff,
342 (byte) 0xff, (byte) 0xff,
343 (byte) 0xff, (byte) 0xff,
344 (byte) 0xff, (byte) 0xff,
345 (byte) 0xff, (byte) 0xff,
346 33}; // Extra bytes
347 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700348 assertThat(ipAddress.toString(),
349 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
350 }
351
352 /**
353 * Tests invalid valueOf() converger for an array and an invalid offset
354 * for IPv4.
355 */
356 @Test(expected = IllegalArgumentException.class)
357 public void testInvalidValueOfArrayInvalidOffsetIPv4() {
358 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800359 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700360
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800361 value = new byte[] {11, 22, 33, // Preamble
362 1, 2, 3, 4,
363 44, 55}; // Extra bytes
364 ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 6);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700365 }
366
367 /**
368 * Tests invalid valueOf() converger for an array and an invalid offset
369 * for IPv6.
370 */
371 @Test(expected = IllegalArgumentException.class)
372 public void testInvalidValueOfArrayInvalidOffsetIPv6() {
373 IpAddress ipAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800374 byte[] value;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700375
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800376 value = new byte[] {11, 22, 33, // Preamble
377 0x11, 0x11, 0x22, 0x22,
378 0x33, 0x33, 0x44, 0x44,
379 0x55, 0x55, 0x66, 0x66,
380 0x77, 0x77,
381 (byte) 0x88, (byte) 0x88,
382 44, 55}; // Extra bytes
383 ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 6);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700384 }
385
386 /**
Pavlin Radoslavovaf5ff792014-10-31 20:51:47 -0700387 * Tests valueOf() converter for IPv4 InetAddress.
388 */
389 @Test
390 public void testValueOfInetAddressIPv4() {
391 IpAddress ipAddress;
392 InetAddress inetAddress;
393
394 inetAddress = InetAddresses.forString("1.2.3.4");
395 ipAddress = IpAddress.valueOf(inetAddress);
396 assertThat(ipAddress.toString(), is("1.2.3.4"));
397
398 inetAddress = InetAddresses.forString("0.0.0.0");
399 ipAddress = IpAddress.valueOf(inetAddress);
400 assertThat(ipAddress.toString(), is("0.0.0.0"));
401
402 inetAddress = InetAddresses.forString("255.255.255.255");
403 ipAddress = IpAddress.valueOf(inetAddress);
404 assertThat(ipAddress.toString(), is("255.255.255.255"));
405 }
406
407 /**
408 * Tests valueOf() converter for IPv6 InetAddress.
409 */
410 @Test
411 public void testValueOfInetAddressIPv6() {
412 IpAddress ipAddress;
413 InetAddress inetAddress;
414
415 inetAddress =
416 InetAddresses.forString("1111:2222:3333:4444:5555:6666:7777:8888");
417 ipAddress = IpAddress.valueOf(inetAddress);
418 assertThat(ipAddress.toString(),
419 is("1111:2222:3333:4444:5555:6666:7777:8888"));
420
421 inetAddress = InetAddresses.forString("::");
422 ipAddress = IpAddress.valueOf(inetAddress);
423 assertThat(ipAddress.toString(), is("::"));
424
425 inetAddress =
426 InetAddresses.forString("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
427 ipAddress = IpAddress.valueOf(inetAddress);
428 assertThat(ipAddress.toString(),
429 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
430 }
431
432 /**
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700433 * Tests valueOf() converter for IPv4 string.
434 */
435 @Test
436 public void testValueOfStringIPv4() {
437 IpAddress ipAddress;
438
439 ipAddress = IpAddress.valueOf("1.2.3.4");
440 assertThat(ipAddress.toString(), is("1.2.3.4"));
441
442 ipAddress = IpAddress.valueOf("0.0.0.0");
443 assertThat(ipAddress.toString(), is("0.0.0.0"));
444
445 ipAddress = IpAddress.valueOf("255.255.255.255");
446 assertThat(ipAddress.toString(), is("255.255.255.255"));
447 }
448
449 /**
450 * Tests valueOf() converter for IPv6 string.
451 */
452 @Test
453 public void testValueOfStringIPv6() {
454 IpAddress ipAddress;
455
456 ipAddress =
457 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
458 assertThat(ipAddress.toString(),
459 is("1111:2222:3333:4444:5555:6666:7777:8888"));
460
461 ipAddress = IpAddress.valueOf("::");
462 assertThat(ipAddress.toString(), is("::"));
463
464 ipAddress =
465 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
466 assertThat(ipAddress.toString(),
467 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
468 }
469
470 /**
471 * Tests invalid valueOf() converter for a null string.
472 */
473 @Test(expected = NullPointerException.class)
474 public void testInvalidValueOfNullString() {
475 IpAddress ipAddress;
476
477 String fromString = null;
478 ipAddress = IpAddress.valueOf(fromString);
479 }
480
481 /**
482 * Tests invalid valueOf() converter for an empty string.
483 */
484 @Test(expected = IllegalArgumentException.class)
485 public void testInvalidValueOfEmptyString() {
486 IpAddress ipAddress;
487
488 String fromString = "";
489 ipAddress = IpAddress.valueOf(fromString);
490 }
491
492 /**
493 * Tests invalid valueOf() converter for an incorrect string.
494 */
495 @Test(expected = IllegalArgumentException.class)
496 public void testInvalidValueOfIncorrectString() {
497 IpAddress ipAddress;
498
499 String fromString = "NoSuchIpAddress";
500 ipAddress = IpAddress.valueOf(fromString);
501 }
502
503 /**
504 * Tests making a mask prefix for a given prefix length for IPv4.
505 */
506 @Test
507 public void testMakeMaskPrefixIPv4() {
508 IpAddress ipAddress;
509
510 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 25);
511 assertThat(ipAddress.toString(), is("255.255.255.128"));
512
513 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 0);
514 assertThat(ipAddress.toString(), is("0.0.0.0"));
515
516 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 32);
517 assertThat(ipAddress.toString(), is("255.255.255.255"));
518 }
519
520 /**
521 * Tests making a mask prefix for a given prefix length for IPv6.
522 */
523 @Test
524 public void testMakeMaskPrefixIPv6() {
525 IpAddress ipAddress;
526
527 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 8);
528 assertThat(ipAddress.toString(), is("ff00::"));
529
530 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 120);
531 assertThat(ipAddress.toString(),
532 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00"));
533
534 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 0);
535 assertThat(ipAddress.toString(), is("::"));
536
537 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 128);
538 assertThat(ipAddress.toString(),
539 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
540
541 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 64);
542 assertThat(ipAddress.toString(), is("ffff:ffff:ffff:ffff::"));
543 }
544
545 /**
546 * Tests making a mask prefix for an invalid prefix length for IPv4:
547 * negative prefix length.
548 */
549 @Test(expected = IllegalArgumentException.class)
550 public void testInvalidMakeNegativeMaskPrefixIPv4() {
551 IpAddress ipAddress;
552
553 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, -1);
554 }
555
556 /**
557 * Tests making a mask prefix for an invalid prefix length for IPv6:
558 * negative prefix length.
559 */
560 @Test(expected = IllegalArgumentException.class)
561 public void testInvalidMakeNegativeMaskPrefixIPv6() {
562 IpAddress ipAddress;
563
564 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, -1);
565 }
566
567 /**
568 * Tests making a mask prefix for an invalid prefix length for IPv4:
569 * too long prefix length.
570 */
571 @Test(expected = IllegalArgumentException.class)
572 public void testInvalidMakeTooLongMaskPrefixIPv4() {
573 IpAddress ipAddress;
574
575 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 33);
576 }
577
578 /**
579 * Tests making a mask prefix for an invalid prefix length for IPv6:
580 * too long prefix length.
581 */
582 @Test(expected = IllegalArgumentException.class)
583 public void testInvalidMakeTooLongMaskPrefixIPv6() {
584 IpAddress ipAddress;
585
586 ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 129);
587 }
588
589 /**
590 * Tests making of a masked address for IPv4.
591 */
592 @Test
593 public void testMakeMaskedAddressIPv4() {
594 IpAddress ipAddress = IpAddress.valueOf("1.2.3.5");
595 IpAddress ipAddressMasked;
596
597 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 24);
598 assertThat(ipAddressMasked.toString(), is("1.2.3.0"));
599
600 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0);
601 assertThat(ipAddressMasked.toString(), is("0.0.0.0"));
602
603 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 32);
604 assertThat(ipAddressMasked.toString(), is("1.2.3.5"));
605 }
606
607 /**
608 * Tests making of a masked address for IPv6.
609 */
610 @Test
611 public void testMakeMaskedAddressIPv6() {
612 IpAddress ipAddress =
613 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
614 IpAddress ipAddressMasked;
615
616 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 8);
617 assertThat(ipAddressMasked.toString(), is("1100::"));
618
619 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 120);
620 assertThat(ipAddressMasked.toString(),
621 is("1111:2222:3333:4444:5555:6666:7777:8800"));
622
623 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0);
624 assertThat(ipAddressMasked.toString(), is("::"));
625
626 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 128);
627 assertThat(ipAddressMasked.toString(),
628 is("1111:2222:3333:4444:5555:6666:7777:8885"));
629
630 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 64);
631 assertThat(ipAddressMasked.toString(), is("1111:2222:3333:4444::"));
632 }
633
634 /**
635 * Tests making of a masked address for invalid prefix length for IPv4:
636 * negative prefix length.
637 */
638 @Test(expected = IllegalArgumentException.class)
639 public void testInvalidMakeNegativeMaskedAddressIPv4() {
640 IpAddress ipAddress = IpAddress.valueOf("1.2.3.5");
641 IpAddress ipAddressMasked;
642
643 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1);
644 }
645
646 /**
647 * Tests making of a masked address for invalid prefix length for IPv6:
648 * negative prefix length.
649 */
650 @Test(expected = IllegalArgumentException.class)
651 public void testInvalidMakeNegativeMaskedAddressIPv6() {
652 IpAddress ipAddress =
653 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
654 IpAddress ipAddressMasked;
655
656 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1);
657 }
658
659 /**
660 * Tests making of a masked address for an invalid prefix length for IPv4:
661 * too long prefix length.
662 */
663 @Test(expected = IllegalArgumentException.class)
664 public void testInvalidMakeTooLongMaskedAddressIPv4() {
665 IpAddress ipAddress = IpAddress.valueOf("1.2.3.5");
666 IpAddress ipAddressMasked;
667
668 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 33);
669 }
670
671 /**
672 * Tests making of a masked address for an invalid prefix length for IPv6:
673 * too long prefix length.
674 */
675 @Test(expected = IllegalArgumentException.class)
676 public void testInvalidMakeTooLongMaskedAddressIPv6() {
677 IpAddress ipAddress =
678 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
679 IpAddress ipAddressMasked;
680
681 ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 129);
682 }
683
684 /**
685 * Tests comparison of {@link IpAddress} for IPv4.
686 */
687 @Test
688 public void testComparisonIPv4() {
689 IpAddress addr1, addr2, addr3, addr4;
690
691 addr1 = IpAddress.valueOf("1.2.3.4");
692 addr2 = IpAddress.valueOf("1.2.3.4");
693 addr3 = IpAddress.valueOf("1.2.3.3");
694 addr4 = IpAddress.valueOf("1.2.3.5");
695 assertTrue(addr1.compareTo(addr2) == 0);
696 assertTrue(addr1.compareTo(addr3) > 0);
697 assertTrue(addr1.compareTo(addr4) < 0);
698
699 addr1 = IpAddress.valueOf("255.2.3.4");
700 addr2 = IpAddress.valueOf("255.2.3.4");
701 addr3 = IpAddress.valueOf("255.2.3.3");
702 addr4 = IpAddress.valueOf("255.2.3.5");
703 assertTrue(addr1.compareTo(addr2) == 0);
704 assertTrue(addr1.compareTo(addr3) > 0);
705 assertTrue(addr1.compareTo(addr4) < 0);
706 }
707
708 /**
709 * Tests comparison of {@link IpAddress} for IPv6.
710 */
711 @Test
712 public void testComparisonIPv6() {
713 IpAddress addr1, addr2, addr3, addr4;
714
715 addr1 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
716 addr2 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
717 addr3 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8887");
718 addr4 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8889");
719 assertTrue(addr1.compareTo(addr2) == 0);
720 assertTrue(addr1.compareTo(addr3) > 0);
721 assertTrue(addr1.compareTo(addr4) < 0);
722
723 addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
724 addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
725 addr3 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8887");
726 addr4 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8889");
727 assertTrue(addr1.compareTo(addr2) == 0);
728 assertTrue(addr1.compareTo(addr3) > 0);
729 assertTrue(addr1.compareTo(addr4) < 0);
730
731 addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
732 addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
733 addr3 = IpAddress.valueOf("ffff:2222:3333:4443:5555:6666:7777:8888");
734 addr4 = IpAddress.valueOf("ffff:2222:3333:4445:5555:6666:7777:8888");
735 assertTrue(addr1.compareTo(addr2) == 0);
736 assertTrue(addr1.compareTo(addr3) > 0);
737 assertTrue(addr1.compareTo(addr4) < 0);
738 }
739
740 /**
741 * Tests equality of {@link IpAddress} for IPv4.
742 */
743 @Test
744 public void testEqualityIPv4() {
Pavlin Radoslavov854ecab2014-11-03 09:43:25 -0800745 new EqualsTester()
746 .addEqualityGroup(IpAddress.valueOf("1.2.3.4"),
747 IpAddress.valueOf("1.2.3.4"))
748 .addEqualityGroup(IpAddress.valueOf("1.2.3.5"),
749 IpAddress.valueOf("1.2.3.5"))
750 .addEqualityGroup(IpAddress.valueOf("0.0.0.0"),
751 IpAddress.valueOf("0.0.0.0"))
752 .addEqualityGroup(IpAddress.valueOf("255.255.255.255"),
753 IpAddress.valueOf("255.255.255.255"))
754 .testEquals();
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700755 }
756
757 /**
758 * Tests equality of {@link IpAddress} for IPv6.
759 */
760 @Test
761 public void testEqualityIPv6() {
Pavlin Radoslavov854ecab2014-11-03 09:43:25 -0800762 new EqualsTester()
763 .addEqualityGroup(
764 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"),
765 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"))
766 .addEqualityGroup(
767 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"),
768 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"))
769 .addEqualityGroup(
770 IpAddress.valueOf("::"),
771 IpAddress.valueOf("::"))
772 .addEqualityGroup(
773 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
774 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))
775 .testEquals();
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700776 }
777
778 /**
779 * Tests object string representation for IPv4.
780 */
781 @Test
782 public void testToStringIPv4() {
783 IpAddress ipAddress;
784
785 ipAddress = IpAddress.valueOf("1.2.3.4");
786 assertThat(ipAddress.toString(), is("1.2.3.4"));
787
788 ipAddress = IpAddress.valueOf("0.0.0.0");
789 assertThat(ipAddress.toString(), is("0.0.0.0"));
790
791 ipAddress = IpAddress.valueOf("255.255.255.255");
792 assertThat(ipAddress.toString(), is("255.255.255.255"));
793 }
794
795 /**
796 * Tests object string representation for IPv6.
797 */
798 @Test
799 public void testToStringIPv6() {
800 IpAddress ipAddress;
801
802 ipAddress =
803 IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
804 assertThat(ipAddress.toString(),
805 is("1111:2222:3333:4444:5555:6666:7777:8888"));
806
807 ipAddress = IpAddress.valueOf("1111::8888");
808 assertThat(ipAddress.toString(), is("1111::8888"));
809
810 ipAddress = IpAddress.valueOf("1111::");
811 assertThat(ipAddress.toString(), is("1111::"));
812
813 ipAddress = IpAddress.valueOf("::8888");
814 assertThat(ipAddress.toString(), is("::8888"));
815
816 ipAddress = IpAddress.valueOf("::");
817 assertThat(ipAddress.toString(), is("::"));
818
819 ipAddress =
820 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
821 assertThat(ipAddress.toString(),
822 is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
823 }
824}