Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/InterfaceAddress/NetworkPrefixLength.java
41149 views
1
/*
2
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/* @test
25
* @bug 6707289 7107883
26
* @summary InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
27
*/
28
29
import java.net.InetAddress;
30
import java.net.Inet4Address;
31
import java.net.InterfaceAddress;
32
import java.net.NetworkInterface;
33
import java.util.Enumeration;
34
import static java.lang.System.out;
35
36
public class NetworkPrefixLength {
37
static boolean passed = true;
38
39
public static void main(String[] args) throws Exception {
40
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
41
42
while (nics.hasMoreElements()) {
43
NetworkInterface nic = nics.nextElement();
44
for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
45
boolean valid = checkPrefix(iaddr);
46
if (!valid) {
47
passed = false;
48
debug(nic.getName(), iaddr);
49
}
50
InetAddress ia = iaddr.getAddress();
51
if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
52
// assumption: prefix length will always be 8
53
if (iaddr.getNetworkPrefixLength() != 8) {
54
out.println("Expected prefix of 8, got " + iaddr);
55
passed = false;
56
}
57
}
58
}
59
}
60
61
if (!passed)
62
throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
63
}
64
65
static boolean checkPrefix(InterfaceAddress iaddr) {
66
InetAddress addr = iaddr.getAddress();
67
68
if (addr instanceof Inet4Address)
69
return checkIPv4PrefixLength(iaddr.getNetworkPrefixLength());
70
else
71
return checkIPv6PrefixLength(iaddr.getNetworkPrefixLength());
72
}
73
74
static boolean checkIPv4PrefixLength(int prefix) {
75
if (prefix >=0 && prefix <= 32)
76
return true;
77
78
return false;
79
}
80
81
static boolean checkIPv6PrefixLength(int prefix) {
82
if (prefix >=0 && prefix <= 128)
83
return true;
84
85
return false;
86
}
87
88
static void debug(String nicName, InterfaceAddress iaddr) {
89
out.println("NIC " + nicName + " has an address with an invalid prefix length:\n" + iaddr);
90
}
91
}
92
93
94