3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.sal.utils;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.Arrays;
16 import org.junit.Assert;
17 import org.junit.Test;
19 public class NetUtilsTest {
22 public void testByteArrayMethods() {
24 Assert.assertTrue(NetUtils
25 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
28 Assert.assertTrue(NetUtils
29 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
32 Assert.assertTrue(NetUtils
33 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
36 Assert.assertTrue(NetUtils
37 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
40 Assert.assertTrue(NetUtils
41 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
44 Assert.assertTrue(NetUtils
45 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
47 byte ba[] = { (byte) 0xf, (byte) 0xf, (byte) 0xf, (byte) 0xff };
48 Assert.assertTrue(Arrays.equals(ba, NetUtils.intToByteArray4(NetUtils
49 .byteArray4ToInt(ba))));
51 byte ba1[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255 };
52 Assert.assertTrue(Arrays.equals(ba1, NetUtils.intToByteArray4(NetUtils
53 .byteArray4ToInt(ba1))));
55 byte ba2[] = { (byte) 255, (byte) 0, (byte) 0, (byte) 0 };
56 Assert.assertTrue(Arrays.equals(ba2, NetUtils.intToByteArray4(NetUtils
57 .byteArray4ToInt(ba2))));
59 byte ba3[] = { (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
60 Assert.assertTrue(Arrays.equals(ba3, NetUtils.intToByteArray4(NetUtils
61 .byteArray4ToInt(ba3))));
63 byte ba4[] = { (byte) 255, (byte) 128, (byte) 0, (byte) 0 };
64 Assert.assertTrue(Arrays.equals(ba4, NetUtils.intToByteArray4(NetUtils
65 .byteArray4ToInt(ba4))));
69 public void testInetMethods() throws UnknownHostException {
71 InetAddress inet = InetAddress.getByName("255.255.255.240");
72 Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
75 inet = InetAddress.getByName("0.0.0.0");
76 Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
79 inet = InetAddress.getByName("9.255.255.9");
80 Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
84 public void testMasksV4() throws UnknownHostException {
86 InetAddress mask = InetAddress.getByName("128.0.0.0");
87 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(1, false)));
89 mask = InetAddress.getByName("192.0.0.0");
90 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(2, false)));
92 mask = InetAddress.getByName("224.0.0.0");
93 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(3, false)));
95 mask = InetAddress.getByName("240.0.0.0");
96 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(4, false)));
98 mask = InetAddress.getByName("248.0.0.0");
99 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(5, false)));
101 mask = InetAddress.getByName("252.0.0.0");
102 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(6, false)));
104 mask = InetAddress.getByName("254.0.0.0");
105 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(7, false)));
107 mask = InetAddress.getByName("255.0.0.0");
108 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(8, false)));
110 mask = InetAddress.getByName("255.128.0.0");
111 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(9, false)));
113 mask = InetAddress.getByName("255.192.0.0");
114 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(10, false)));
116 mask = InetAddress.getByName("255.224.0.0");
117 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(11, false)));
119 mask = InetAddress.getByName("255.240.0.0");
120 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(12, false)));
122 mask = InetAddress.getByName("255.248.0.0");
123 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(13, false)));
125 mask = InetAddress.getByName("255.252.0.0");
126 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(14, false)));
128 mask = InetAddress.getByName("255.254.0.0");
129 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(15, false)));
131 mask = InetAddress.getByName("255.255.0.0");
132 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(16, false)));
134 mask = InetAddress.getByName("255.255.128.0");
135 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(17, false)));
137 mask = InetAddress.getByName("255.255.192.0");
138 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(18, false)));
140 mask = InetAddress.getByName("255.255.224.0");
141 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(19, false)));
143 mask = InetAddress.getByName("255.255.240.0");
144 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(20, false)));
146 mask = InetAddress.getByName("255.255.248.0");
147 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(21, false)));
149 mask = InetAddress.getByName("255.255.252.0");
150 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(22, false)));
152 mask = InetAddress.getByName("255.255.254.0");
153 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(23, false)));
155 mask = InetAddress.getByName("255.255.255.0");
156 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(24, false)));
158 mask = InetAddress.getByName("255.255.255.128");
159 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(25, false)));
161 mask = InetAddress.getByName("255.255.255.192");
162 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(26, false)));
164 mask = InetAddress.getByName("255.255.255.224");
165 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(27, false)));
167 mask = InetAddress.getByName("255.255.255.240");
168 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(28, false)));
170 mask = InetAddress.getByName("255.255.255.248");
171 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(29, false)));
173 mask = InetAddress.getByName("255.255.255.252");
174 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(30, false)));
176 mask = InetAddress.getByName("255.255.255.254");
177 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(31, false)));
179 mask = InetAddress.getByName("255.255.255.255");
180 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(32, false)));
184 public void testMasksV6() throws UnknownHostException {
186 InetAddress mask = InetAddress.getByName("ff00::0");
187 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(8, true)));
189 mask = InetAddress.getByName("8000::0");
190 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(1, true)));
192 mask = InetAddress.getByName("f800::0");
193 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(5, true)));
195 mask = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe");
196 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(127, true)));
200 public void testGetSubnetLen() {
202 byte address[] = { (byte) 128, (byte) 0, (byte) 0, 0 };
203 Assert.assertTrue(NetUtils.getSubnetMaskLength(address) == 1);
205 byte address1[] = { (byte) 255, 0, 0, 0 };
206 Assert.assertTrue(NetUtils.getSubnetMaskLength(address1) == 8);
208 byte address2[] = { (byte) 255, (byte) 255, (byte) 248, 0 };
209 Assert.assertTrue(NetUtils.getSubnetMaskLength(address2) == 21);
211 byte address4[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 254 };
212 Assert.assertTrue(NetUtils.getSubnetMaskLength(address4) == 31);
214 byte address5[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
215 (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
217 Assert.assertTrue(NetUtils.getSubnetMaskLength(address5) == 128);
219 byte address6[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
220 (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
222 Assert.assertTrue(NetUtils.getSubnetMaskLength(address6) == 127);
224 byte address7[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
225 (byte) 255, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
226 Assert.assertTrue(NetUtils.getSubnetMaskLength(address7) == 64);
228 byte address8[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
229 (byte) 254, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
230 Assert.assertTrue(NetUtils.getSubnetMaskLength(address8) == 63);
232 byte address9[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 128,
233 (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
234 Assert.assertTrue(NetUtils.getSubnetMaskLength(address9) == 49);
236 byte address10[] = { (byte) 128, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
237 (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
238 Assert.assertTrue(NetUtils.getSubnetMaskLength(address10) == 1);
240 byte address11[] = { (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
241 (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
242 Assert.assertTrue(NetUtils.getSubnetMaskLength(address11) == 0);
246 public void testGetSubnetPrefix() throws UnknownHostException {
247 InetAddress ip = InetAddress.getByName("192.128.64.252");
249 Assert.assertTrue(NetUtils.getSubnetPrefix(ip, maskLen).equals(
250 InetAddress.getByName("192.128.64.128")));
254 public void testIsIPv6Valid() throws UnknownHostException {
255 Assert.assertTrue(NetUtils
256 .isIPv6AddressValid("fe80:0000:0000:0000:0204:61ff:fe9d:f156")); //normal ipv6
257 Assert.assertTrue(NetUtils
258 .isIPv6AddressValid("fe80:0:0:0:204:61ff:fe9d:f156")); //no leading zeroes
259 Assert.assertTrue(NetUtils
260 .isIPv6AddressValid("fe80::204:61ff:fe9d:f156")); //zeroes to ::
263 .isIPv6AddressValid("fe80:0000:0000:0000:0204:61ff:254.157.241.86")); // ipv4 ending
264 Assert.assertTrue(NetUtils
265 .isIPv6AddressValid("fe80:0:0:0:0204:61ff:254.157.241.86")); // no leading zeroes, ipv4 end
266 Assert.assertTrue(NetUtils
267 .isIPv6AddressValid("fe80::204:61ff:254.157.241.86")); // zeroes ::, no leading zeroes
269 Assert.assertTrue(NetUtils.isIPv6AddressValid("2001::")); //link-local prefix
270 Assert.assertTrue(NetUtils.isIPv6AddressValid("::1")); //localhost
271 Assert.assertTrue(NetUtils.isIPv6AddressValid("fe80::")); //global-unicast
272 Assert.assertFalse(NetUtils.isIPv6AddressValid("abcd")); //not valid
273 Assert.assertFalse(NetUtils.isIPv6AddressValid("1")); //not valid
274 Assert.assertFalse(NetUtils
275 .isIPv6AddressValid("fe80:0:0:0:204:61ff:fe9d")); //not valid, too short
276 Assert.assertFalse(NetUtils
277 .isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d")); //not valid
278 Assert.assertFalse(NetUtils.isIPv6AddressValid("192.168.1.1")); //not valid,ipv4
280 .assertFalse(NetUtils
281 .isIPv6AddressValid("2001:0000:1234:0000:10001:C1C0:ABCD:0876")); //not valid, extra number
283 .assertFalse(NetUtils
284 .isIPv6AddressValid("20010:0000:1234:0000:10001:C1C0:ABCD:0876")); //not valid, extra number
288 .isIPv6AddressValid("2001:0DB8:0000:CD30:0000:0000:0000:0000/60")); //full with mask
289 Assert.assertTrue(NetUtils.isIPv6AddressValid("2001:0DB8:0:CD30::/64")); //shortened with mask
290 Assert.assertTrue(NetUtils.isIPv6AddressValid("2001:0DB8:0:CD30::/0")); //0 subnet with mask
291 Assert.assertTrue(NetUtils.isIPv6AddressValid("::1/128")); //localhost 128 mask
293 Assert.assertFalse(NetUtils.isIPv6AddressValid("124.15.6.89/60")); //invalid, ip with mask
295 .assertFalse(NetUtils
296 .isIPv6AddressValid("2001:0DB8:0000:CD30:0000:0000:0000:0000/130")); //invalid, mask >128
298 .assertFalse(NetUtils
299 .isIPv6AddressValid("2001:0DB8:0:CD30::/-5")); //invalid, mask < 0
300 Assert.assertFalse(NetUtils
301 .isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d/64")); //not valid ip, valid netmask
302 Assert.assertFalse(NetUtils
303 .isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d/-1")); //not valid both
308 public void testInetAddressConflict() throws UnknownHostException {
310 // test a ipv4 testAddress in the same subnet as the filter
311 // the method should return false as there is no conflict
312 Assert.assertFalse(NetUtils.inetAddressConflict(
313 InetAddress.getByName("9.9.1.1"),
314 InetAddress.getByName("9.9.1.0"), null,
315 InetAddress.getByName("255.255.255.0")));
317 // test a ipv4 testAddress not in the same subnet as the filter
318 // the method should return true as there is a conflict
319 Assert.assertTrue(NetUtils.inetAddressConflict(
320 InetAddress.getByName("9.9.2.1"),
321 InetAddress.getByName("9.9.1.0"), null,
322 InetAddress.getByName("255.255.255.0")));
324 // test a ipv4 testAddress more generic than the filter
325 // the method should return true as there is a conflict
326 Assert.assertTrue(NetUtils.inetAddressConflict(
327 InetAddress.getByName("9.9.1.1"),
328 InetAddress.getByName("9.9.1.0"),
329 InetAddress.getByName("255.255.0.0"),
330 InetAddress.getByName("255.255.255.0")));
332 // test a ipv4 testAddress less generic than the filter and in the same
333 // subnet as the filter
334 // the method should return false as there is no conflict
335 Assert.assertFalse(NetUtils.inetAddressConflict(
336 InetAddress.getByName("9.9.1.0"),
337 InetAddress.getByName("9.9.0.0"),
338 InetAddress.getByName("255.255.255.0"),
339 InetAddress.getByName("255.255.0.0")));
341 // test a ipv4 testAddress less generic than the filter and not in the
342 // same subnet as the filter
343 // the method should return true as there is a conflict
344 Assert.assertTrue(NetUtils.inetAddressConflict(
345 InetAddress.getByName("9.8.1.0"),
346 InetAddress.getByName("9.9.0.0"),
347 InetAddress.getByName("255.255.255.0"),
348 InetAddress.getByName("255.255.0.0")));
353 public void testIPAddressValidity() {
354 Assert.assertFalse(NetUtils.isIPAddressValid(null));
355 Assert.assertFalse(NetUtils.isIPAddressValid("abc"));
356 Assert.assertFalse(NetUtils.isIPAddressValid("1.1.1"));
357 Assert.assertFalse(NetUtils.isIPAddressValid("1.1.1.1/49"));
359 Assert.assertTrue(NetUtils.isIPAddressValid("1.1.1.1"));
360 Assert.assertTrue(NetUtils.isIPAddressValid("1.1.1.1/32"));
361 Assert.assertTrue(NetUtils
362 .isIPAddressValid("2001:420:281:1004:407a:57f4:4d15:c355"));