Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/NetworkInterface/Test.java
38811 views
1
/*
2
* Copyright (c) 2001, 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 4405354 6594296 8058216
26
* @run main Test
27
* @run main/othervm -Djava.net.preferIPv4Stack=true Test
28
* @summary Basic tests for NetworkInterface
29
*/
30
import java.net.NetworkInterface;
31
import java.net.InetAddress;
32
import java.util.Enumeration;
33
34
public class Test {
35
36
public static void main(String args[]) throws Exception {
37
38
Enumeration nifs = NetworkInterface.getNetworkInterfaces();
39
40
while (nifs.hasMoreElements()) {
41
NetworkInterface ni = (NetworkInterface)nifs.nextElement();
42
43
String name = ni.getName();
44
System.out.println("\n" + name);
45
46
/*
47
* Enumeration the IP addresses on this interface
48
*/
49
Enumeration addrs = ni.getInetAddresses();
50
while (addrs.hasMoreElements()) {
51
InetAddress addr = (InetAddress)addrs.nextElement();
52
System.out.println(addr);
53
if (NetworkInterface.getByInetAddress(addr) == null) {
54
throw new Exception("getByInetAddress returned null");
55
}
56
}
57
System.out.println("getInetAddresses() test passed.");
58
59
/*
60
* Check equals and hashCode contract
61
*/
62
NetworkInterface ni2 = NetworkInterface.getByName(name);
63
if (!ni2.equals(ni)) {
64
throw new Exception("getByName returned: " + ni2);
65
}
66
if (!ni.equals(ni2)) {
67
throw new Exception("equals specification broken");
68
}
69
System.out.println("equals() tests passed.");
70
if (ni2.hashCode() != ni.hashCode()) {
71
throw new Exception("hashCode contract broken");
72
}
73
System.out.println("hashCode() test passed.");
74
75
byte[] ba = ni.getHardwareAddress();
76
if (ba != null && ba.length == 0) {
77
throw new Exception("getHardwareAddress returned 0 length byte array");
78
}
79
System.out.println("getHardwareAddress() test passed.");
80
}
81
82
// misc tests :-
83
// getByXXX(null) should throw NPE
84
// getByXXX("garbage") should return null
85
86
System.out.println("\nMiscellenous tests: ");
87
88
try {
89
NetworkInterface.getByName(null);
90
} catch (NullPointerException npe) {
91
}
92
System.out.println("getByName(null) test passed.");
93
94
try {
95
NetworkInterface.getByInetAddress(null);
96
} catch (NullPointerException npe) {
97
}
98
System.out.println("getByInetAddress(null) test passed.");
99
100
if (NetworkInterface.getByName("not-a-valid-name") != null) {
101
throw new Exception
102
("getByName returned unexpected interface: null expected");
103
}
104
System.out.println("getByName(<unknown>) test passed.");
105
106
InetAddress ia = InetAddress.getByName("255.255.255.255");
107
if (NetworkInterface.getByInetAddress(ia) != null) {
108
throw new Exception
109
("getByInetAddress returned unexpected interface: null expected");
110
}
111
System.out.println("getByName(getByInetAddress(<unknown>) test passed.");
112
113
}
114
}
115
116