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/MulticastSocket/SetGetNetworkInterfaceTest.java
38811 views
1
/*
2
* Copyright (c) 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
25
/*
26
* @test
27
* @bug 6458027
28
* @summary Disabling IPv6 on a specific network interface causes problems.
29
*
30
*/
31
32
import java.io.IOException;
33
import java.net.InetAddress;
34
import java.net.MulticastSocket;
35
import java.net.NetworkInterface;
36
import java.net.SocketException;
37
import java.util.Arrays;
38
import java.util.Enumeration;
39
40
41
public class SetGetNetworkInterfaceTest {
42
43
public static void main(String[] args) throws Exception {
44
45
boolean passed = true;
46
try {
47
MulticastSocket ms = new MulticastSocket();
48
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
49
.getNetworkInterfaces();
50
while (networkInterfaces.hasMoreElements()) {
51
NetworkInterface netIf = networkInterfaces.nextElement();
52
if (isNetworkInterfaceTestable(netIf)) {
53
printNetIfDetails(netIf);
54
ms.setNetworkInterface(netIf);
55
NetworkInterface msNetIf = ms.getNetworkInterface();
56
if (netIf.equals(msNetIf)) {
57
System.out.println(" OK");
58
} else {
59
System.out.println("FAILED!!!");
60
printNetIfDetails(msNetIf);
61
passed = false;
62
}
63
System.out.println("------------------");
64
}
65
}
66
} catch (IOException e) {
67
e.printStackTrace();
68
passed = false;
69
}
70
if (!passed) {
71
throw new RuntimeException("Test Fail");
72
}
73
System.out.println("Test passed ");
74
}
75
76
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
77
System.out.println("checking netif == " + netIf.getName());
78
return (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
79
}
80
81
private static boolean isIpAddrAvailable (NetworkInterface netIf) {
82
boolean ipAddrAvailable = false;
83
byte[] nullIpAddr = {'0', '0', '0', '0'};
84
byte[] testIpAddr = null;
85
86
Enumeration<InetAddress> ipAddresses = netIf.getInetAddresses();
87
while (ipAddresses.hasMoreElements()) {
88
InetAddress testAddr = ipAddresses.nextElement();
89
testIpAddr = testAddr.getAddress();
90
if ((testIpAddr != null) && (!Arrays.equals(testIpAddr, nullIpAddr))) {
91
ipAddrAvailable = true;
92
break;
93
} else {
94
System.out.println("ignore netif " + netIf.getName());
95
}
96
}
97
return ipAddrAvailable;
98
}
99
100
private static void printNetIfDetails(NetworkInterface ni)
101
throws SocketException {
102
System.out.println("Name " + ni.getName() + " index " + ni.getIndex());
103
Enumeration<InetAddress> en = ni.getInetAddresses();
104
while (en.hasMoreElements()) {
105
System.out.println(" InetAdress: " + en.nextElement());
106
}
107
System.out.println("HardwareAddress: " + createMacAddrString(ni));
108
System.out.println("loopback: " + ni.isLoopback() + "; pointToPoint: "
109
+ ni.isPointToPoint() + "; virtual: " + ni.isVirtual()
110
+ "; MTU: " + ni.getMTU());
111
}
112
113
private static String createMacAddrString(NetworkInterface netIf)
114
throws SocketException {
115
byte[] macAddr = netIf.getHardwareAddress();
116
StringBuilder sb = new StringBuilder();
117
if (macAddr != null) {
118
for (int i = 0; i < macAddr.length; i++) {
119
sb.append(String.format("%02X%s", macAddr[i],
120
(i < macAddr.length - 1) ? "-" : ""));
121
}
122
}
123
return sb.toString();
124
}
125
}
126
127