Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/MulticastSocket/SetLoopbackMode.java
41149 views
1
/*
2
* Copyright (c) 2001, 2020, 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
* @test
26
* @bug 4686717
27
* @summary Test MulticastSocket.setLoopbackMode
28
* @library /test/lib
29
* @modules java.base/java.net:+open
30
* @build jdk.test.lib.NetworkConfiguration
31
* jdk.test.lib.Platform
32
* @run main/othervm SetLoopbackMode
33
* @run main/othervm -Djdk.net.usePlainDatagramSocketImpl SetLoopbackMode
34
*/
35
36
import java.lang.reflect.Method;
37
import java.lang.reflect.UndeclaredThrowableException;
38
import java.net.*;
39
import java.io.IOException;
40
import jdk.test.lib.NetworkConfiguration;
41
42
public class SetLoopbackMode {
43
44
static final boolean FAILED = true;
45
static final boolean PASSED = false;
46
47
static boolean test(MulticastSocket mc, InetAddress grp) throws IOException {
48
49
boolean disabled = mc.getLoopbackMode();
50
51
if (disabled) {
52
System.out.println("Loopback mode is disabled.");
53
} else {
54
System.out.println("Loopback mode is enabled.");
55
}
56
57
System.out.println(mc.getLocalSocketAddress());
58
59
byte b[] = "hello".getBytes();
60
DatagramPacket p = new DatagramPacket(b, b.length, grp,
61
mc.getLocalPort());
62
mc.send(p);
63
64
boolean gotPacket = false;
65
66
mc.setSoTimeout(1000);
67
try {
68
b = new byte[16];
69
p = new DatagramPacket(b, b.length);
70
mc.receive(p);
71
gotPacket = true;
72
73
/* purge any additional copies of the packet */
74
for (;;) {
75
mc.receive(p);
76
}
77
78
} catch (SocketTimeoutException x) {
79
}
80
81
if (gotPacket && disabled) {
82
System.out.println("Packet received (unexpected as loopback is disabled)");
83
return FAILED;
84
}
85
if (!gotPacket && !disabled) {
86
System.out.println
87
("Packet not received (packet excepted as loopback is enabled)");
88
return FAILED;
89
}
90
91
if (gotPacket && !disabled) {
92
System.out.println("Packet received - correct.");
93
} else {
94
System.out.println("Packet not received - correct.");
95
}
96
97
return PASSED;
98
}
99
100
private static boolean canUseIPv6(NetworkConfiguration nc) {
101
return nc.ip6MulticastInterfaces().toArray().length > 0;
102
}
103
104
public static void main (String args[]) throws Exception {
105
int failures = 0;
106
NetworkConfiguration nc = NetworkConfiguration.probe();
107
108
try (MulticastSocket mc = new MulticastSocket()) {
109
InetAddress grp = InetAddress.getByName("224.80.80.80");
110
111
112
/*
113
* If IPv6 is available then use IPv6 multicast group - needed
114
* to workaround Linux IPv6 bug whereby !IPV6_MULTICAST_LOOP
115
* doesn't prevent loopback of IPv4 multicast packets.
116
*/
117
118
if (canUseIPv6(nc)) {
119
System.out.println("IPv6 can be used");
120
grp = InetAddress.getByName("ff01::1");
121
} else {
122
System.out.println("IPv6 cannot be used: using IPv4");
123
}
124
System.out.println("Default network interface: " + DefaultInterface.getDefaultName());
125
System.out.println("\nTest will use multicast group: " + grp);
126
try {
127
System.out.println("NetworkInterface.getByInetAddress(grp): "
128
+ getName(NetworkInterface.getByInetAddress(grp)));
129
} catch (Exception x) {
130
// OK
131
}
132
mc.joinGroup(grp);
133
134
System.out.println("\n******************\n");
135
136
mc.setLoopbackMode(true);
137
if (test(mc, grp) == FAILED) failures++;
138
139
System.out.println("\n******************\n");
140
141
mc.setLoopbackMode(false);
142
if (test(mc, grp) == FAILED) failures++;
143
144
System.out.println("\n******************\n");
145
146
if (failures > 0) {
147
throw new RuntimeException("Test failed");
148
}
149
}
150
}
151
152
static String getName(NetworkInterface nif) {
153
return nif == null ? null : nif.getName();
154
}
155
156
static class DefaultInterface {
157
static final Method GET_DEFAULT;
158
static {
159
try {
160
GET_DEFAULT = Class.forName("java.net.DefaultInterface")
161
.getDeclaredMethod("getDefault");
162
GET_DEFAULT.setAccessible(true);
163
} catch (Exception x) {
164
throw new ExceptionInInitializerError(x);
165
}
166
}
167
static NetworkInterface getDefault() {
168
try {
169
return (NetworkInterface) GET_DEFAULT
170
.invoke(null);
171
} catch (Exception x) {
172
throw new UndeclaredThrowableException(x);
173
}
174
}
175
static String getDefaultName() {
176
return getName(getDefault());
177
}
178
}
179
}
180
181