Path: blob/master/test/jdk/java/net/ServerSocket/TestLocalAddress.java
41149 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.net.InetAddress;24import java.net.InetSocketAddress;25import java.net.ServerSocket;26import java.net.SocketAddress;27import java.net.SocketPermission;28import java.nio.channels.ServerSocketChannel;29import java.security.AccessControlContext;30import java.security.AllPermission;31import java.security.Permission;32import java.security.Permissions;33import java.security.Policy;34import java.security.PrivilegedAction;35import java.security.PrivilegedExceptionAction;36import java.security.ProtectionDomain;37import org.testng.annotations.BeforeTest;38import org.testng.annotations.Test;39import static java.lang.System.out;40import static java.security.AccessController.*;41import static org.testng.Assert.*;4243/*44* @test45* @bug 822473046* @summary Check local address access with a security manager47* @run testng/othervm -Djava.security.manager=allow TestLocalAddress48*/4950public class TestLocalAddress {5152InetAddress localHost;53ExposedSecurityManager exposedSecurityManager;5455@BeforeTest56public void setup() throws Exception {57localHost = InetAddress.getLocalHost();58out.println("localHost: " + localHost);5960Policy.setPolicy(new AllPermissionsPolicy());61exposedSecurityManager = new ExposedSecurityManager();62System.setSecurityManager(exposedSecurityManager);63out.println("Security manager set");64}6566@Test67public void serverSocketNoSecurityManager() throws Exception {68out.println("\n\n--- serverSocketNoSecurityManager ---");69try (ServerSocket ss = new ServerSocket()) {70testWithNoSecurityManager(ss);71}72}7374@Test75public void serverSocketAdapterNoSecurityManager() throws Exception {76out.println("\n\n--- serverSocketAdapterNoSecurityManager ---");77try (ServerSocket ss = ServerSocketChannel.open().socket()) {78testWithNoSecurityManager(ss);79}80}8182void testWithNoSecurityManager(ServerSocket ss) throws Exception {83final SecurityManager sm = System.getSecurityManager();84System.setSecurityManager(null);85try {86ss.bind(new InetSocketAddress(localHost, 0));8788var localSocketAddr = ((InetSocketAddress)ss.getLocalSocketAddress());89var localInetAddress = ss.getInetAddress();90assertEquals(localInetAddress, localSocketAddr.getAddress());91if (!(localHost.equals(InetAddress.getLoopbackAddress())))92assertNotEquals(localInetAddress, InetAddress.getLoopbackAddress());9394// toString95String s = ss.toString();96out.println("toString returned:" + s);97assertTrue(s.contains(localInetAddress.toString()),98"Expected [" + localInetAddress + "] in " + s);99100} finally {101System.setSecurityManager(sm);102}103}104105@Test106public void serverSocketNoPermissions() throws Exception {107out.println("\n\n--- serverSocketNoPermissions ---");108try (ServerSocket ss = new ServerSocket()) {109testWithNoPermissions(ss);110}111}112113@Test114public void serverSocketAdapterNoPermissions() throws Exception {115out.println("\n\n--- serverSocketAdapterNoPermissions ---");116try (ServerSocket ss = ServerSocketChannel.open().socket()) {117testWithNoPermissions(ss);118}119}120121void testWithNoPermissions(ServerSocket ss) throws Exception {122ss.bind(new InetSocketAddress(localHost, 0));123124PrivilegedExceptionAction<SocketAddress> pa = ss::getLocalSocketAddress;125var localSocketAddr = (InetSocketAddress) doPrivileged(pa, noPermissions());126assertSecurityManagerCalled();127PrivilegedExceptionAction<InetAddress> pa1 = ss::getInetAddress;128var localInetAddress = doPrivileged(pa1, noPermissions());129assertSecurityManagerCalled();130131assertEquals(localInetAddress, localSocketAddr.getAddress());132assertEquals(localInetAddress, InetAddress.getLoopbackAddress());133134// toString135PrivilegedExceptionAction<String> pa2 = ss::toString;136String s = doPrivileged(pa2, noPermissions());137assertSecurityManagerCalled();138out.println("toString returned:" + s);139assertTrue(s.contains(localInetAddress.toString()),140"Expected [" + localInetAddress + "] in " + s);141}142143144@Test145public void serverSocketFineGrainPermissions() throws Exception {146out.println("\n\n--- serverSocketFineGrainPermissions ---");147try (ServerSocket ss = new ServerSocket()) {148testWithFineGrainPermissions(ss);149}150}151152@Test153public void serverSocketAdapterFineGrainPermissions() throws Exception {154out.println("\n\n--- serverSocketAdapterFineGrainPermissions ---");155try (ServerSocket ss = ServerSocketChannel.open().socket()) {156testWithFineGrainPermissions(ss);157}158}159160void testWithFineGrainPermissions(ServerSocket ss) throws Exception {161AccessControlContext connectPermission = withPermissions(162new SocketPermission(localHost.getHostName(), "connect")163);164ss.bind(new InetSocketAddress(localHost, 0));165166PrivilegedExceptionAction<SocketAddress> pa = ss::getLocalSocketAddress;167var localSocketAddr = (InetSocketAddress) doPrivileged(pa, connectPermission);168assertSecurityManagerCalled();169PrivilegedExceptionAction<InetAddress> pa1 = ss::getInetAddress;170var localInetAddress = doPrivileged(pa1, connectPermission);171assertSecurityManagerCalled();172173assertEquals(localInetAddress, localSocketAddr.getAddress());174assertEquals(localInetAddress, localHost);175176// toString177PrivilegedExceptionAction<String> pa2 = ss::toString;178String s = doPrivileged(pa2, connectPermission);179assertSecurityManagerCalled();180out.println("toString returned:" + s);181assertTrue(s.contains(localInetAddress.toString()),182"Expected [" + localInetAddress + "] in " + s);183}184185186@Test187public void serverSocketUnbound() throws Exception {188out.println("\n\n--- serverSocketUnbound ---");189try (ServerSocket ss = new ServerSocket()) {190testUnbound(ss);191}192}193194@Test195public void serverSocketAdapterUnbound() throws Exception {196out.println("\n\n--- serverSocketAdapterUnbound ---");197try (ServerSocket ss = ServerSocketChannel.open().socket()) {198testUnbound(ss);199}200}201202void testUnbound(ServerSocket ss) {203assert !ss.isBound();204exposedSecurityManager.reset();205assertEquals(ss.getLocalSocketAddress(), null);206assertEquals(exposedSecurityManager.port, -999);207assertEquals(ss.getInetAddress(), null);208assertEquals(exposedSecurityManager.port, -999);209String s = ss.toString();210assertEquals(exposedSecurityManager.port, -999);211out.println("toString returned:" + s);212assertTrue(s.contains("unbound"), "Expected [unbound] in " + s);213}214215// A security manager that allows inspection of checkConnect's host/port.216static class ExposedSecurityManager extends SecurityManager {217volatile String host;218volatile int port;219ExposedSecurityManager() {220reset();221}222@Override223public void checkConnect(String host, int port) {224this.host = host;225this.port = port;226super.checkConnect(host, port);227}228void reset() {229host = "reset";230port = -999;231}232}233234void assertSecurityManagerCalled() {235assertEquals(exposedSecurityManager.port, -1);236assertEquals(exposedSecurityManager.host, localHost.getHostAddress());237exposedSecurityManager.reset();238}239240@Test241// Ensures that the test machinery is operating as expected.242public void sanity() {243PrivilegedAction<?> connectAction = () -> {244System.getSecurityManager().checkConnect("example.com", 80);245return null;246};247248try {249doPrivileged(connectAction, allPermissions());250} catch (SecurityException unexpected) {251throw unexpected;252}253try {254doPrivileged(connectAction, noPermissions());255fail("Expected exception not thrown");256} catch (SecurityException expected) { }257try {258doPrivileged(connectAction,259withPermissions(new SocketPermission("example.com:80", "connect")));260} catch (SecurityException unexpected) {261throw unexpected;262}263}264265static AccessControlContext withPermissions(Permission... perms) {266Permissions p = new Permissions();267for (Permission perm : perms) {268p.add(perm);269}270ProtectionDomain pd = new ProtectionDomain(null, p);271return new AccessControlContext(new ProtectionDomain[]{ pd });272}273274static AccessControlContext allPermissions() {275return withPermissions(new AllPermission());276}277278static AccessControlContext noPermissions() {279return withPermissions(/*empty*/);280}281282// A Policy that implies all permissions.283static class AllPermissionsPolicy extends Policy {284public boolean implies(ProtectionDomain domain, Permission permission) {285return true;286}287}288}289290291