2 * Copyright (c) 2013-2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.sal.utils;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.Arrays;
15 import org.junit.Assert;
16 import org.junit.Test;
18 public class NetUtilsTest {
21 public void testByteArrayMethods() {
23 Assert.assertTrue(NetUtils
24 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
27 Assert.assertTrue(NetUtils
28 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
31 Assert.assertTrue(NetUtils
32 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
35 Assert.assertTrue(NetUtils
36 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
39 Assert.assertTrue(NetUtils
40 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
43 Assert.assertTrue(NetUtils
44 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
46 byte ba[] = { (byte) 0xf, (byte) 0xf, (byte) 0xf, (byte) 0xff };
47 Assert.assertTrue(Arrays.equals(ba, NetUtils.intToByteArray4(NetUtils
48 .byteArray4ToInt(ba))));
50 byte ba1[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255 };
51 Assert.assertTrue(Arrays.equals(ba1, NetUtils.intToByteArray4(NetUtils
52 .byteArray4ToInt(ba1))));
54 byte ba2[] = { (byte) 255, (byte) 0, (byte) 0, (byte) 0 };
55 Assert.assertTrue(Arrays.equals(ba2, NetUtils.intToByteArray4(NetUtils
56 .byteArray4ToInt(ba2))));
58 byte ba3[] = { (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
59 Assert.assertTrue(Arrays.equals(ba3, NetUtils.intToByteArray4(NetUtils
60 .byteArray4ToInt(ba3))));
62 byte ba4[] = { (byte) 255, (byte) 128, (byte) 0, (byte) 0 };
63 Assert.assertTrue(Arrays.equals(ba4, NetUtils.intToByteArray4(NetUtils
64 .byteArray4ToInt(ba4))));
68 public void testByteArrayMethodsForLong() {
69 // Test of longToByteArray6 method.
71 (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44,
72 (byte) 0x55, (byte) 0x66
74 long mac = 0x112233445566L;
75 Assert.assertTrue(Arrays.equals(ba, NetUtils.longToByteArray6(mac)));
78 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
79 (byte) 0xff, (byte) 0xff
81 long mac1 = 0xffffffffffffL;
82 Assert.assertTrue(Arrays.equals(ba1, NetUtils.longToByteArray6(mac1)));
85 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
86 (byte) 0x00, (byte) 0x00
88 long mac2 = 0x000000000000L;
89 Assert.assertTrue(Arrays.equals(ba2, NetUtils.longToByteArray6(mac2)));
92 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00,
93 (byte) 0x00, (byte) 0x00
95 long mac3 = 0xffffff000000L;
96 Assert.assertTrue(Arrays.equals(ba3, NetUtils.longToByteArray6(mac3)));
99 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xff,
100 (byte) 0xff, (byte) 0xff
102 long mac4 = 0x000000ffffffL;
103 Assert.assertTrue(Arrays.equals(ba4, NetUtils.longToByteArray6(mac4)));
105 // Convert a long number to a byte array,
106 // and revert it to the long number again.
107 Assert.assertTrue(NetUtils
108 .byteArray6ToLong(NetUtils.longToByteArray6(mac)) == mac);
110 Assert.assertTrue(NetUtils
111 .byteArray6ToLong(NetUtils.longToByteArray6(mac1)) == mac1);
113 Assert.assertTrue(NetUtils
114 .byteArray6ToLong(NetUtils.longToByteArray6(mac2)) == mac2);
116 Assert.assertTrue(NetUtils
117 .byteArray6ToLong(NetUtils.longToByteArray6(mac3)) == mac3);
119 Assert.assertTrue(NetUtils
120 .byteArray6ToLong(NetUtils.longToByteArray6(mac4)) == mac4);
122 // Convert a byte array to a long nubmer,
123 // and revert it to the byte array again.
124 Assert.assertTrue(Arrays.equals(ba,
125 NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba))));
127 Assert.assertTrue(Arrays.equals(ba1,
128 NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba1))));
130 Assert.assertTrue(Arrays.equals(ba2,
131 NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba2))));
133 Assert.assertTrue(Arrays.equals(ba3,
134 NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba3))));
136 Assert.assertTrue(Arrays.equals(ba4,
137 NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba4))));
139 // Test of paramter validation of byteArray6ToLong method.
141 (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44
143 Assert.assertEquals(0, NetUtils.byteArray6ToLong(array5));
146 (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44,
147 (byte) 0x55, (byte) 0x66, (byte) 0x77
149 Assert.assertEquals(0, NetUtils.byteArray6ToLong(array7));
151 byte arrayNull[] = null;
152 Assert.assertEquals(0, NetUtils.byteArray6ToLong(arrayNull));
156 public void testInetMethods() throws UnknownHostException {
158 InetAddress inet = InetAddress.getByName("255.255.255.240");
159 Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
162 inet = InetAddress.getByName("0.0.0.0");
163 Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
166 inet = InetAddress.getByName("9.255.255.9");
167 Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
171 public void testMasksV4() throws UnknownHostException {
173 InetAddress mask = InetAddress.getByName("128.0.0.0");
174 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(1, false)));
176 mask = InetAddress.getByName("192.0.0.0");
177 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(2, false)));
179 mask = InetAddress.getByName("224.0.0.0");
180 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(3, false)));
182 mask = InetAddress.getByName("240.0.0.0");
183 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(4, false)));
185 mask = InetAddress.getByName("248.0.0.0");
186 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(5, false)));
188 mask = InetAddress.getByName("252.0.0.0");
189 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(6, false)));
191 mask = InetAddress.getByName("254.0.0.0");
192 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(7, false)));
194 mask = InetAddress.getByName("255.0.0.0");
195 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(8, false)));
197 mask = InetAddress.getByName("255.128.0.0");
198 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(9, false)));
200 mask = InetAddress.getByName("255.192.0.0");
201 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(10, false)));
203 mask = InetAddress.getByName("255.224.0.0");
204 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(11, false)));
206 mask = InetAddress.getByName("255.240.0.0");
207 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(12, false)));
209 mask = InetAddress.getByName("255.248.0.0");
210 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(13, false)));
212 mask = InetAddress.getByName("255.252.0.0");
213 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(14, false)));
215 mask = InetAddress.getByName("255.254.0.0");
216 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(15, false)));
218 mask = InetAddress.getByName("255.255.0.0");
219 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(16, false)));
221 mask = InetAddress.getByName("255.255.128.0");
222 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(17, false)));
224 mask = InetAddress.getByName("255.255.192.0");
225 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(18, false)));
227 mask = InetAddress.getByName("255.255.224.0");
228 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(19, false)));
230 mask = InetAddress.getByName("255.255.240.0");
231 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(20, false)));
233 mask = InetAddress.getByName("255.255.248.0");
234 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(21, false)));
236 mask = InetAddress.getByName("255.255.252.0");
237 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(22, false)));
239 mask = InetAddress.getByName("255.255.254.0");
240 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(23, false)));
242 mask = InetAddress.getByName("255.255.255.0");
243 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(24, false)));
245 mask = InetAddress.getByName("255.255.255.128");
246 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(25, false)));
248 mask = InetAddress.getByName("255.255.255.192");
249 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(26, false)));
251 mask = InetAddress.getByName("255.255.255.224");
252 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(27, false)));
254 mask = InetAddress.getByName("255.255.255.240");
255 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(28, false)));
257 mask = InetAddress.getByName("255.255.255.248");
258 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(29, false)));
260 mask = InetAddress.getByName("255.255.255.252");
261 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(30, false)));
263 mask = InetAddress.getByName("255.255.255.254");
264 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(31, false)));
266 mask = InetAddress.getByName("255.255.255.255");
267 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(32, false)));
271 public void testMasksV6() throws UnknownHostException {
273 InetAddress mask = InetAddress.getByName("ff00::0");
274 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(8, true)));
276 mask = InetAddress.getByName("8000::0");
277 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(1, true)));
279 mask = InetAddress.getByName("f800::0");
280 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(5, true)));
282 mask = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe");
283 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(127, true)));
287 public void testGetSubnetLen() {
289 byte address[] = { (byte) 128, (byte) 0, (byte) 0, 0 };
290 Assert.assertTrue(NetUtils.getSubnetMaskLength(address) == 1);
292 byte address1[] = { (byte) 255, 0, 0, 0 };
293 Assert.assertTrue(NetUtils.getSubnetMaskLength(address1) == 8);
295 byte address2[] = { (byte) 255, (byte) 255, (byte) 248, 0 };
296 Assert.assertTrue(NetUtils.getSubnetMaskLength(address2) == 21);
298 byte address4[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 254 };
299 Assert.assertTrue(NetUtils.getSubnetMaskLength(address4) == 31);
301 byte address5[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
302 (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
304 Assert.assertTrue(NetUtils.getSubnetMaskLength(address5) == 128);
306 byte address6[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
307 (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
309 Assert.assertTrue(NetUtils.getSubnetMaskLength(address6) == 127);
311 byte address7[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
312 (byte) 255, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
313 Assert.assertTrue(NetUtils.getSubnetMaskLength(address7) == 64);
315 byte address8[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
316 (byte) 254, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
317 Assert.assertTrue(NetUtils.getSubnetMaskLength(address8) == 63);
319 byte address9[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 128,
320 (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
321 Assert.assertTrue(NetUtils.getSubnetMaskLength(address9) == 49);
323 byte address10[] = { (byte) 128, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
324 (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
325 Assert.assertTrue(NetUtils.getSubnetMaskLength(address10) == 1);
327 byte address11[] = { (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
328 (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
329 Assert.assertTrue(NetUtils.getSubnetMaskLength(address11) == 0);
333 public void testGetSubnetPrefix() throws UnknownHostException {
334 InetAddress ip = InetAddress.getByName("192.128.64.252");
336 Assert.assertTrue(NetUtils.getSubnetPrefix(ip, maskLen).equals(
337 InetAddress.getByName("192.128.64.128")));
341 public void testIsIPv6Valid() throws UnknownHostException {
342 Assert.assertTrue(NetUtils
343 .isIPv6AddressValid("fe80:0000:0000:0000:0204:61ff:fe9d:f156")); //normal ipv6
344 Assert.assertTrue(NetUtils
345 .isIPv6AddressValid("fe80:0:0:0:204:61ff:fe9d:f156")); //no leading zeroes
346 Assert.assertTrue(NetUtils
347 .isIPv6AddressValid("fe80::204:61ff:fe9d:f156")); //zeroes to ::
350 .isIPv6AddressValid("fe80:0000:0000:0000:0204:61ff:254.157.241.86")); // ipv4 ending
351 Assert.assertTrue(NetUtils
352 .isIPv6AddressValid("fe80:0:0:0:0204:61ff:254.157.241.86")); // no leading zeroes, ipv4 end
353 Assert.assertTrue(NetUtils
354 .isIPv6AddressValid("fe80::204:61ff:254.157.241.86")); // zeroes ::, no leading zeroes
356 Assert.assertTrue(NetUtils.isIPv6AddressValid("2001::")); //link-local prefix
357 Assert.assertTrue(NetUtils.isIPv6AddressValid("::1")); //localhost
358 Assert.assertTrue(NetUtils.isIPv6AddressValid("fe80::")); //global-unicast
359 Assert.assertFalse(NetUtils.isIPv6AddressValid("abcd")); //not valid
360 Assert.assertFalse(NetUtils.isIPv6AddressValid("1")); //not valid
361 Assert.assertFalse(NetUtils
362 .isIPv6AddressValid("fe80:0:0:0:204:61ff:fe9d")); //not valid, too short
363 Assert.assertFalse(NetUtils
364 .isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d")); //not valid
365 Assert.assertFalse(NetUtils.isIPv6AddressValid("192.168.1.1")); //not valid,ipv4
367 .assertFalse(NetUtils
368 .isIPv6AddressValid("2001:0000:1234:0000:10001:C1C0:ABCD:0876")); //not valid, extra number
370 .assertFalse(NetUtils
371 .isIPv6AddressValid("20010:0000:1234:0000:10001:C1C0:ABCD:0876")); //not valid, extra number
375 .isIPv6AddressValid("2001:0DB8:0000:CD30:0000:0000:0000:0000/60")); //full with mask
376 Assert.assertTrue(NetUtils.isIPv6AddressValid("2001:0DB8:0:CD30::/64")); //shortened with mask
377 Assert.assertTrue(NetUtils.isIPv6AddressValid("2001:0DB8:0:CD30::/0")); //0 subnet with mask
378 Assert.assertTrue(NetUtils.isIPv6AddressValid("::1/128")); //localhost 128 mask
380 Assert.assertFalse(NetUtils.isIPv6AddressValid("124.15.6.89/60")); //invalid, ip with mask
382 .assertFalse(NetUtils
383 .isIPv6AddressValid("2001:0DB8:0000:CD30:0000:0000:0000:0000/130")); //invalid, mask >128
385 .assertFalse(NetUtils
386 .isIPv6AddressValid("2001:0DB8:0:CD30::/-5")); //invalid, mask < 0
387 Assert.assertFalse(NetUtils
388 .isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d/64")); //not valid ip, valid netmask
389 Assert.assertFalse(NetUtils
390 .isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d/-1")); //not valid both
395 public void testInetAddressConflict() throws UnknownHostException {
397 // test a ipv4 testAddress in the same subnet as the filter
398 // the method should return false as there is no conflict
399 Assert.assertFalse(NetUtils.inetAddressConflict(
400 InetAddress.getByName("9.9.1.1"),
401 InetAddress.getByName("9.9.1.0"), null,
402 InetAddress.getByName("255.255.255.0")));
404 // test a ipv4 testAddress not in the same subnet as the filter
405 // the method should return true as there is a conflict
406 Assert.assertTrue(NetUtils.inetAddressConflict(
407 InetAddress.getByName("9.9.2.1"),
408 InetAddress.getByName("9.9.1.0"), null,
409 InetAddress.getByName("255.255.255.0")));
411 // test a ipv4 testAddress more generic than the filter
412 // the method should return true as there is a conflict
413 Assert.assertTrue(NetUtils.inetAddressConflict(
414 InetAddress.getByName("9.9.1.1"),
415 InetAddress.getByName("9.9.1.0"),
416 InetAddress.getByName("255.255.0.0"),
417 InetAddress.getByName("255.255.255.0")));
419 // test a ipv4 testAddress less generic than the filter and in the same
420 // subnet as the filter
421 // the method should return false as there is no conflict
422 Assert.assertFalse(NetUtils.inetAddressConflict(
423 InetAddress.getByName("9.9.1.0"),
424 InetAddress.getByName("9.9.0.0"),
425 InetAddress.getByName("255.255.255.0"),
426 InetAddress.getByName("255.255.0.0")));
428 // test a ipv4 testAddress less generic than the filter and not in the
429 // same subnet as the filter
430 // the method should return true as there is a conflict
431 Assert.assertTrue(NetUtils.inetAddressConflict(
432 InetAddress.getByName("9.8.1.0"),
433 InetAddress.getByName("9.9.0.0"),
434 InetAddress.getByName("255.255.255.0"),
435 InetAddress.getByName("255.255.0.0")));
440 public void testIPAddressValidity() {
441 Assert.assertFalse(NetUtils.isIPAddressValid(null));
442 Assert.assertFalse(NetUtils.isIPAddressValid("abc"));
443 Assert.assertFalse(NetUtils.isIPAddressValid("1.1.1"));
444 Assert.assertFalse(NetUtils.isIPAddressValid("1.1.1.1/49"));
446 Assert.assertTrue(NetUtils.isIPAddressValid("1.1.1.1"));
447 Assert.assertTrue(NetUtils.isIPAddressValid("1.1.1.1/32"));
448 Assert.assertTrue(NetUtils
449 .isIPAddressValid("2001:420:281:1004:407a:57f4:4d15:c355"));
453 public void testGetUnsignedByte() {
454 Assert.assertEquals(0, NetUtils.getUnsignedByte((byte) 0x00));
455 Assert.assertEquals(1, NetUtils.getUnsignedByte((byte) 0x01));
456 Assert.assertEquals(127, NetUtils.getUnsignedByte((byte) 0x7f));
458 Assert.assertEquals(128, NetUtils.getUnsignedByte((byte) 0x80));
459 Assert.assertEquals(255, NetUtils.getUnsignedByte((byte) 0xff));
463 public void testGetUnsignedShort() {
464 Assert.assertEquals(0, NetUtils.getUnsignedShort((short) 0x0000));
465 Assert.assertEquals(1, NetUtils.getUnsignedShort((short) 0x0001));
466 Assert.assertEquals(32767, NetUtils.getUnsignedShort((short) 0x7fff));
468 Assert.assertEquals(32768, NetUtils.getUnsignedShort((short) 0x8000));
469 Assert.assertEquals(65535, NetUtils.getUnsignedShort((short) 0xffff));