--- old/src/java.base/share/classes/java/net/ServerSocket.java 2019-05-29 12:09:24.171748057 +0100 +++ new/src/java.base/share/classes/java/net/ServerSocket.java 2019-05-29 12:09:23.883748067 +0100 @@ -817,7 +817,8 @@ * Returns the implementation address and implementation port of * this socket as a {@code String}. *
- * If there is a security manager set, its {@code checkConnect} method is
+ * If there is a security manager set, and this socket is
+ * {@linkplain #isBound bound}, its {@code checkConnect} method is
* called with the local address and {@code -1} as its arguments to see
* if the operation is allowed. If the operation is not allowed,
* an {@code InetAddress} representing the
@@ -831,7 +832,7 @@
return "ServerSocket[unbound]";
InetAddress in;
if (System.getSecurityManager() != null)
- in = InetAddress.getLoopbackAddress();
+ in = getInetAddress();
else
in = impl.getInetAddress();
return "ServerSocket[addr=" + in +
--- /dev/null 2019-04-23 10:14:30.487678946 +0100
+++ new/test/jdk/java/net/ServerSocket/TestLocalAddress.java 2019-05-29 12:09:24.483748046 +0100
@@ -0,0 +1,289 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.net.SocketAddress;
+import java.net.SocketPermission;
+import java.nio.channels.ServerSocketChannel;
+import java.security.AccessControlContext;
+import java.security.AllPermission;
+import java.security.Permission;
+import java.security.Permissions;
+import java.security.Policy;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedExceptionAction;
+import java.security.ProtectionDomain;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+import static java.lang.System.out;
+import static java.security.AccessController.*;
+import static org.testng.Assert.*;
+
+/*
+ * @test
+ * @bug 8224730
+ * @summary Check local address access with a security manager
+ * @run testng/othervm TestLocalAddress
+ */
+
+public class TestLocalAddress {
+
+ InetAddress localHost;
+ ExposedSecurityManager exposedSecurityManager;
+
+ @BeforeTest
+ public void setup() throws Exception {
+ localHost = InetAddress.getLocalHost();
+ out.println("localHost: " + localHost);
+
+ Policy.setPolicy(new AllPermissionsPolicy());
+ exposedSecurityManager = new ExposedSecurityManager();
+ System.setSecurityManager(exposedSecurityManager);
+ out.println("Security manager set");
+ }
+
+ @Test
+ public void serverSocketNoSecurityManager() throws Exception {
+ out.println("\n\n--- serverSocketNoSecurityManager ---");
+ try (ServerSocket ss = new ServerSocket()) {
+ testWithNoSecurityManager(ss);
+ }
+ }
+
+ @Test
+ public void socketAdapterNoSecurityManager() throws Exception {
+ out.println("\n\n--- socketAdapterNoSecurityManager ---");
+ try (ServerSocket ss = ServerSocketChannel.open().socket()) {
+ testWithNoSecurityManager(ss);
+ }
+ }
+
+ void testWithNoSecurityManager(ServerSocket ss) throws Exception {
+ final SecurityManager sm = System.getSecurityManager();
+ System.setSecurityManager(null);
+ try {
+ ss.bind(new InetSocketAddress(localHost, 0));
+
+ var localSocketAddr = ((InetSocketAddress)ss.getLocalSocketAddress());
+ var localInetAddress = ss.getInetAddress();
+ assertEquals(localInetAddress, localSocketAddr.getAddress());
+ if (!(localHost.equals(InetAddress.getLoopbackAddress())))
+ assertNotEquals(localInetAddress, InetAddress.getLoopbackAddress());
+
+ // toString
+ String s = ss.toString();
+ out.println("toString returned:" + s);
+ assertTrue(s.contains(localInetAddress.toString()),
+ "Expected [" + localInetAddress + "] in " + s);
+
+ } finally {
+ System.setSecurityManager(sm);
+ }
+ }
+
+ @Test
+ public void serverSocketNoPermissions() throws Exception {
+ out.println("\n\n--- serverSocketNoPermissions ---");
+ try (ServerSocket ss = new ServerSocket()) {
+ testWithNoPermissions(ss);
+ }
+ }
+
+ @Test
+ public void socketAdapterNoPermissions() throws Exception {
+ out.println("\n\n--- socketAdapterNoPermissions ---");
+ try (ServerSocket ss = ServerSocketChannel.open().socket()) {
+ testWithNoPermissions(ss);
+ }
+ }
+
+ void testWithNoPermissions(ServerSocket ss) throws Exception {
+ ss.bind(new InetSocketAddress(localHost, 0));
+
+ PrivilegedExceptionAction