Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/B6210227.java
41152 views
1
/*
2
* Copyright (c) 2012, 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
/**
25
* @test
26
* @bug 6210227
27
* @library /test/lib
28
* @summary REGRESSION: Socket.getLocalAddress() returns address of 0.0.0.0 on outbound TCP.
29
* This test requires binding to the wildcard address.
30
* @run main B6210227
31
* @run main/othervm -Djava.net.preferIPv4Stack=true B6210227
32
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6210227
33
*/
34
35
import java.util.*;
36
import java.net.*;
37
import java.util.stream.IntStream;
38
import java.util.stream.Collectors;
39
import jdk.test.lib.net.IPSupport;
40
41
public class B6210227 {
42
public static void main(String[] args) throws Exception
43
{
44
IPSupport.throwSkippedExceptionIfNonOperational();
45
46
ServerSocket ss = new ServerSocket(0);
47
int port = ss.getLocalPort();
48
49
try {
50
InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);
51
Socket s = new Socket();
52
s.connect( isa, 1000 );
53
InetAddress iaLocal = s.getLocalAddress(); // if this comes back as 0.0.0.0 this would demonstrate issue
54
String sLocalHostname = iaLocal.getHostName();
55
byte[] address = iaLocal.getAddress();
56
if (isWildcard(address)) {
57
if (iaLocal instanceof Inet6Address) {
58
String msg = IntStream.range(0, address.length)
59
.mapToObj(i -> "0").collect(Collectors.joining(":"));
60
throw new RuntimeException(msg + " returned");
61
} else {
62
throw new RuntimeException ("0.0.0.0 returned");
63
}
64
}
65
System.out.println("local hostname is "+sLocalHostname );
66
} catch(Exception e) {
67
System.out.println("Exception happened");
68
throw e;
69
} finally {
70
ss.close();
71
}
72
}
73
74
private static boolean isWildcard(byte[] bytes) {
75
for (int i = 0; i < bytes.length ; i++) {
76
if (bytes[i] != 0) return false;
77
}
78
return true;
79
}
80
}
81
82