Search in sources :

Example 51 with SocketPermission

use of java.net.SocketPermission in project jdk8u_jdk by JetBrains.

the class EmptyInputStream method URLtoSocketPermission.

/**
     *  if the caller has a URLPermission for connecting to the
     *  given URL, then return a SocketPermission which permits
     *  access to that destination. Return null otherwise. The permission
     *  is cached in a field (which can only be changed by redirects)
     */
SocketPermission URLtoSocketPermission(URL url) throws IOException {
    if (socketPermission != null) {
        return socketPermission;
    }
    SecurityManager sm = System.getSecurityManager();
    if (sm == null) {
        return null;
    }
    // the permission, which we might grant
    SocketPermission newPerm = new SocketPermission(getHostAndPort(url), "connect");
    String actions = getRequestMethod() + ":" + getUserSetHeaders().getHeaderNamesInList();
    String urlstring = url.getProtocol() + "://" + url.getAuthority() + url.getPath();
    URLPermission p = new URLPermission(urlstring, actions);
    try {
        sm.checkPermission(p);
        socketPermission = newPerm;
        return socketPermission;
    } catch (SecurityException e) {
    // fall thru
    }
    return null;
}
Also used : SocketPermission(java.net.SocketPermission) URLPermission(java.net.URLPermission)

Example 52 with SocketPermission

use of java.net.SocketPermission in project jdk8u_jdk by JetBrains.

the class SocketPermissionTest method connectSocketTest.

@Test
public void connectSocketTest() throws Exception {
    try (ServerSocket ss = new ServerSocket(0)) {
        int port = ss.getLocalPort();
        String addr = "localhost:" + port;
        AccessControlContext acc = getAccessControlContext(new SocketPermission(addr, "listen,connect,resolve"));
        // Positive
        AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
            try (Socket client = new Socket(InetAddress.getLocalHost(), port)) {
            }
            return null;
        }, acc);
        //Negative
        try {
            AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
                Socket client = new Socket(InetAddress.getLocalHost(), port);
                fail("Expected SecurityException");
                return null;
            }, RESTRICTED_ACC);
        } catch (SecurityException expected) {
        }
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) SocketPermission(java.net.SocketPermission) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) DatagramSocket(java.net.DatagramSocket) ServerSocket(java.net.ServerSocket) MulticastSocket(java.net.MulticastSocket) Test(org.testng.annotations.Test)

Example 53 with SocketPermission

use of java.net.SocketPermission in project jdk8u_jdk by JetBrains.

the class SocketPermissionTest method listenServerSocketTest.

@Test
public void listenServerSocketTest() throws Exception {
    // the hardcoded port number doesn't really matter since we expect the
    // security permission to be checked before the underlying operation.
    int port = 8899;
    String addr = "localhost:" + port;
    AccessControlContext acc = getAccessControlContext(new SocketPermission(addr, "listen"));
    // Positive
    AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
        try (ServerSocket ss = new ServerSocket(port)) {
        } catch (IOException intermittentlyExpected) {
        }
        return null;
    }, acc);
    // Negative
    try {
        AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
            try (ServerSocket ss = new ServerSocket(port)) {
            } catch (IOException intermittentlyExpected) {
            }
            fail("Expected SecurityException");
            return null;
        }, RESTRICTED_ACC);
    } catch (SecurityException expected) {
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) SocketPermission(java.net.SocketPermission) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 54 with SocketPermission

use of java.net.SocketPermission in project jdk8u_jdk by JetBrains.

the class SocketPermissionTest method listenMulticastSocketTest.

@Test
public void listenMulticastSocketTest() throws Exception {
    // the hardcoded port number doesn't really matter since we expect the
    // security permission to be checked before the underlying operation.
    int port = 8899;
    String addr = "localhost:" + port;
    AccessControlContext acc = getAccessControlContext(new SocketPermission(addr, "listen"));
    // Positive
    AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
        try (MulticastSocket ms = new MulticastSocket(port)) {
        } catch (IOException intermittentlyExpected) {
        }
        return null;
    }, acc);
    // Negative
    try {
        AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
            try (MulticastSocket ms = new MulticastSocket(port)) {
            } catch (IOException intermittentlyExpected) {
            }
            fail("Expected SecurityException");
            return null;
        }, RESTRICTED_ACC);
    } catch (SecurityException expected) {
    }
}
Also used : MulticastSocket(java.net.MulticastSocket) AccessControlContext(java.security.AccessControlContext) SocketPermission(java.net.SocketPermission) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 55 with SocketPermission

use of java.net.SocketPermission in project jdk8u_jdk by JetBrains.

the class SocketPermissionTest method listenDatagramSocketTest.

@Test
public void listenDatagramSocketTest() throws Exception {
    // the hardcoded port number doesn't really matter since we expect the
    // security permission to be checked before the underlying operation.
    int port = 8899;
    String addr = "localhost:" + port;
    AccessControlContext acc = getAccessControlContext(new SocketPermission(addr, "listen"));
    // Positive
    AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
        try (DatagramSocket ds = new DatagramSocket(port)) {
        } catch (IOException intermittentlyExpected) {
        }
        return null;
    }, acc);
    // Negative
    try {
        AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
            try (DatagramSocket ds = new DatagramSocket(port)) {
            } catch (IOException intermittentlyExpected) {
            }
            fail("Expected SecurityException");
            return null;
        }, RESTRICTED_ACC);
    } catch (SecurityException expected) {
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) DatagramSocket(java.net.DatagramSocket) SocketPermission(java.net.SocketPermission) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Aggregations

SocketPermission (java.net.SocketPermission)83 Deployment (org.jboss.arquillian.container.test.api.Deployment)27 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)27 FilePermission (java.io.FilePermission)17 PropertyPermission (java.util.PropertyPermission)13 StringAsset (org.jboss.shrinkwrap.api.asset.StringAsset)13 AccessControlContext (java.security.AccessControlContext)9 JavaArchive (org.jboss.shrinkwrap.api.spec.JavaArchive)9 Test (org.testng.annotations.Test)8 URL (java.net.URL)7 PrivilegedActionException (java.security.PrivilegedActionException)6 OperateOnDeployment (org.jboss.arquillian.container.test.api.OperateOnDeployment)6 HttpRequest (org.jboss.as.test.integration.common.HttpRequest)6 MockTracer (io.opentracing.mock.MockTracer)5 IOException (java.io.IOException)5 DatagramSocket (java.net.DatagramSocket)5 SecurityPermission (java.security.SecurityPermission)5 InetAddress (java.net.InetAddress)4 InetSocketAddress (java.net.InetSocketAddress)4 MulticastSocket (java.net.MulticastSocket)4