Merge "Get/remove inactive hosts"
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / utils / NetUtilsTest.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 package org.opendaylight.controller.sal.utils;
11
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.Arrays;
15
16 import org.junit.Assert;
17 import org.junit.Test;
18
19 public class NetUtilsTest {
20
21     @Test
22     public void testByteArrayMethods() {
23         int ip = 8888;
24         Assert.assertTrue(NetUtils
25                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
26
27         ip = 0xffffffff;
28         Assert.assertTrue(NetUtils
29                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
30
31         ip = 0;
32         Assert.assertTrue(NetUtils
33                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
34
35         ip = 0x1fffffff;
36         Assert.assertTrue(NetUtils
37                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
38
39         ip = 0xfffffff;
40         Assert.assertTrue(NetUtils
41                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
42
43         ip = 0xf000ffff;
44         Assert.assertTrue(NetUtils
45                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
46
47         byte ba[] = { (byte) 0xf, (byte) 0xf, (byte) 0xf, (byte) 0xff };
48         Assert.assertTrue(Arrays.equals(ba, NetUtils.intToByteArray4(NetUtils
49                 .byteArray4ToInt(ba))));
50
51         byte ba1[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255 };
52         Assert.assertTrue(Arrays.equals(ba1, NetUtils.intToByteArray4(NetUtils
53                 .byteArray4ToInt(ba1))));
54
55         byte ba2[] = { (byte) 255, (byte) 0, (byte) 0, (byte) 0 };
56         Assert.assertTrue(Arrays.equals(ba2, NetUtils.intToByteArray4(NetUtils
57                 .byteArray4ToInt(ba2))));
58
59         byte ba3[] = { (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
60         Assert.assertTrue(Arrays.equals(ba3, NetUtils.intToByteArray4(NetUtils
61                 .byteArray4ToInt(ba3))));
62
63         byte ba4[] = { (byte) 255, (byte) 128, (byte) 0, (byte) 0 };
64         Assert.assertTrue(Arrays.equals(ba4, NetUtils.intToByteArray4(NetUtils
65                 .byteArray4ToInt(ba4))));
66     }
67
68     @Test
69     public void testInetMethods() throws UnknownHostException {
70         int ip = 0xfffffff0;
71         InetAddress inet = InetAddress.getByName("255.255.255.240");
72         Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
73
74         ip = 0;
75         inet = InetAddress.getByName("0.0.0.0");
76         Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
77
78         ip = 0x9ffff09;
79         inet = InetAddress.getByName("9.255.255.9");
80         Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
81     }
82
83     @Test
84     public void testMasksV4() throws UnknownHostException {
85
86         InetAddress mask = InetAddress.getByName("128.0.0.0");
87         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(1, false)));
88
89         mask = InetAddress.getByName("192.0.0.0");
90         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(2, false)));
91
92         mask = InetAddress.getByName("224.0.0.0");
93         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(3, false)));
94
95         mask = InetAddress.getByName("240.0.0.0");
96         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(4, false)));
97
98         mask = InetAddress.getByName("248.0.0.0");
99         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(5, false)));
100
101         mask = InetAddress.getByName("252.0.0.0");
102         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(6, false)));
103
104         mask = InetAddress.getByName("254.0.0.0");
105         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(7, false)));
106
107         mask = InetAddress.getByName("255.0.0.0");
108         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(8, false)));
109
110         mask = InetAddress.getByName("255.128.0.0");
111         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(9, false)));
112
113         mask = InetAddress.getByName("255.192.0.0");
114         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(10, false)));
115
116         mask = InetAddress.getByName("255.224.0.0");
117         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(11, false)));
118
119         mask = InetAddress.getByName("255.240.0.0");
120         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(12, false)));
121
122         mask = InetAddress.getByName("255.248.0.0");
123         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(13, false)));
124
125         mask = InetAddress.getByName("255.252.0.0");
126         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(14, false)));
127
128         mask = InetAddress.getByName("255.254.0.0");
129         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(15, false)));
130
131         mask = InetAddress.getByName("255.255.0.0");
132         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(16, false)));
133
134         mask = InetAddress.getByName("255.255.128.0");
135         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(17, false)));
136
137         mask = InetAddress.getByName("255.255.192.0");
138         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(18, false)));
139
140         mask = InetAddress.getByName("255.255.224.0");
141         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(19, false)));
142
143         mask = InetAddress.getByName("255.255.240.0");
144         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(20, false)));
145
146         mask = InetAddress.getByName("255.255.248.0");
147         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(21, false)));
148
149         mask = InetAddress.getByName("255.255.252.0");
150         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(22, false)));
151
152         mask = InetAddress.getByName("255.255.254.0");
153         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(23, false)));
154
155         mask = InetAddress.getByName("255.255.255.0");
156         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(24, false)));
157
158         mask = InetAddress.getByName("255.255.255.128");
159         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(25, false)));
160
161         mask = InetAddress.getByName("255.255.255.192");
162         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(26, false)));
163
164         mask = InetAddress.getByName("255.255.255.224");
165         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(27, false)));
166
167         mask = InetAddress.getByName("255.255.255.240");
168         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(28, false)));
169
170         mask = InetAddress.getByName("255.255.255.248");
171         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(29, false)));
172
173         mask = InetAddress.getByName("255.255.255.252");
174         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(30, false)));
175
176         mask = InetAddress.getByName("255.255.255.254");
177         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(31, false)));
178
179         mask = InetAddress.getByName("255.255.255.255");
180         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(32, false)));
181     }
182
183     @Test
184     public void testMasksV6() throws UnknownHostException {
185
186         InetAddress mask = InetAddress.getByName("ff00::0");
187         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(8, true)));
188
189         mask = InetAddress.getByName("8000::0");
190         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(1, true)));
191
192         mask = InetAddress.getByName("f800::0");
193         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(5, true)));
194
195         mask = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe");
196         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(127, true)));
197     }
198
199     @Test
200     public void testGetSubnetLen() {
201
202         byte address[] = { (byte) 128, (byte) 0, (byte) 0, 0 };
203         Assert.assertTrue(NetUtils.getSubnetMaskLength(address) == 1);
204
205         byte address1[] = { (byte) 255, 0, 0, 0 };
206         Assert.assertTrue(NetUtils.getSubnetMaskLength(address1) == 8);
207
208         byte address2[] = { (byte) 255, (byte) 255, (byte) 248, 0 };
209         Assert.assertTrue(NetUtils.getSubnetMaskLength(address2) == 21);
210
211         byte address4[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 254 };
212         Assert.assertTrue(NetUtils.getSubnetMaskLength(address4) == 31);
213
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,
216                 (byte) 255 };
217         Assert.assertTrue(NetUtils.getSubnetMaskLength(address5) == 128);
218
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,
221                 (byte) 254 };
222         Assert.assertTrue(NetUtils.getSubnetMaskLength(address6) == 127);
223
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);
227
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);
231
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);
235
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);
239
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);
243     }
244
245     @Test
246     public void testGetSubnetPrefix() throws UnknownHostException {
247         InetAddress ip = InetAddress.getByName("192.128.64.252");
248         int maskLen = 25;
249         Assert.assertTrue(NetUtils.getSubnetPrefix(ip, maskLen).equals(
250                 InetAddress.getByName("192.128.64.128")));
251     }
252
253     @Test
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 ::
261         Assert
262                 .assertTrue(NetUtils
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
268
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
279         Assert
280                 .assertFalse(NetUtils
281                         .isIPv6AddressValid("2001:0000:1234:0000:10001:C1C0:ABCD:0876")); //not valid, extra number
282         Assert
283                 .assertFalse(NetUtils
284                         .isIPv6AddressValid("20010:0000:1234:0000:10001:C1C0:ABCD:0876")); //not valid, extra number
285
286         Assert
287                 .assertTrue(NetUtils
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
292
293         Assert.assertFalse(NetUtils.isIPv6AddressValid("124.15.6.89/60")); //invalid, ip with mask
294         Assert
295                 .assertFalse(NetUtils
296                         .isIPv6AddressValid("2001:0DB8:0000:CD30:0000:0000:0000:0000/130")); //invalid, mask >128
297         Assert
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
304
305     }
306
307     @Test
308     public void testInetAddressConflict() throws UnknownHostException {
309
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")));
316
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")));
323
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")));
331
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")));
340
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")));
349
350     }
351
352     @Test
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"));
358
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"));
363     }
364 }