Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/MulticastSocket/Test.java
41149 views
1
/*
2
* Copyright (c) 2001, 2019, 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
import java.io.IOException;
25
import java.net.DatagramPacket;
26
import java.net.DatagramSocket;
27
import java.net.InetAddress;
28
import java.net.MulticastSocket;
29
import java.net.SocketTimeoutException;
30
31
import jdk.test.lib.NetworkConfiguration;
32
import jdk.test.lib.net.IPSupport;
33
34
/**
35
* @test
36
* @bug 4488458
37
* @summary IPv4 and IPv6 multicasting broken on Linux
38
* @library /test/lib
39
* @build jdk.test.lib.NetworkConfiguration
40
* jdk.test.lib.Platform
41
* @run main Test
42
* @run main/othervm -Djava.net.preferIPv4Stack=true Test
43
*/
44
public class Test {
45
46
static int count = 0;
47
static int failures = 0;
48
49
void doTest(String address) throws IOException {
50
boolean failed = false;
51
52
InetAddress ia = InetAddress.getByName(address);
53
54
count++;
55
System.out.println("**********************");
56
System.out.println("Test " + count + ": " + ia);
57
58
MulticastSocket mc = new MulticastSocket();
59
int port = mc.getLocalPort();
60
DatagramSocket s1 = new DatagramSocket();
61
62
byte msg[] = "Hello".getBytes();
63
DatagramPacket p = new DatagramPacket(msg, msg.length);
64
65
mc.setSoTimeout(2000);
66
67
try {
68
for (int i=0; i<2; i++) {
69
70
System.out.println("Join: " + ia);
71
mc.joinGroup(ia);
72
73
/* packets should be received */
74
75
for (int j = 0; j < 2; j++) {
76
p.setAddress(ia);
77
p.setPort(port);
78
79
System.out.println("Send packet to: " + ia);
80
s1.send(p);
81
82
try {
83
mc.receive(p);
84
System.out.println("Got packet! - Good.");
85
} catch (SocketTimeoutException e) {
86
failed = true;
87
System.out.println("Failed: No packet received within timeout!!!");
88
}
89
}
90
91
System.out.println("Leave: " + ia);
92
mc.leaveGroup(ia);
93
94
/*
95
* If there are multiple interface we might be a couple of
96
* copies still in our queue
97
*/
98
try {
99
while (true) {
100
mc.receive(p);
101
}
102
} catch (SocketTimeoutException e) { }
103
104
/* packets should not be received */
105
106
p.setAddress(ia);
107
p.setPort(port);
108
109
s1.send(p);
110
111
try {
112
mc.receive(p);
113
System.out.println("Failed: Got packet after leaving group!!!");
114
failed = true;
115
} catch (SocketTimeoutException e) {
116
System.out.println("No packet received within timeout! - Good.");
117
}
118
}
119
120
} catch (IOException ioe) {
121
System.out.println("Failed: Unexpected exception thrown: ");
122
ioe.printStackTrace();
123
failed = true;
124
}
125
126
mc.close();
127
s1.close();
128
129
if (failed) {
130
failures++;
131
System.out.println("Test failed!!");
132
} else {
133
System.out.println("Test passed.");
134
}
135
}
136
137
void allTests() throws IOException {
138
NetworkConfiguration nc = NetworkConfiguration.probe();
139
140
// unconditionally test IPv4 address
141
doTest("224.80.80.80");
142
143
// If IPv6 is enabled perform multicast tests with various scopes
144
if (nc.hasTestableIPv6Address()) {
145
doTest("ff01::a");
146
}
147
148
if (nc.hasLinkLocalAddress()) {
149
doTest("ff02::a");
150
}
151
152
if (nc.hasSiteLocalAddress()) {
153
doTest("ff05::a");
154
}
155
156
if (nc.has_globaladdress()) {
157
doTest("ff0e::a");
158
}
159
}
160
161
public static void main(String args[]) throws Exception {
162
IPSupport.throwSkippedExceptionIfNonOperational();
163
164
Test t = new Test();
165
166
if (args.length == 0) {
167
t.allTests();
168
} else {
169
for (int i = 0; i < args.length; i++) {
170
t.doTest(args[i]);
171
}
172
}
173
174
System.out.println("**********************");
175
System.out.println(count + " test(s) executed. " + failures +
176
" test(s) failed.");
177
178
if (failures > 0) {
179
throw new Exception("Test failed - see log file for details");
180
}
181
}
182
}
183
184