Search in sources :

Example 41 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 42 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 43 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)

Example 44 with SocketPermission

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

the class SocketPermissionTest method connectDatagramSocketTest.

@Test
public void connectDatagramSocketTest() throws Exception {
    byte[] msg = "Hello".getBytes(UTF_8);
    InetAddress lh = InetAddress.getLocalHost();
    try (DatagramSocket ds = new DatagramSocket(0)) {
        int port = ds.getLocalPort();
        String addr = lh.getHostAddress() + ":" + port;
        AccessControlContext acc = getAccessControlContext(new SocketPermission(addr, "connect,resolve"));
        // Positive
        AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
            DatagramPacket dp = new DatagramPacket(msg, msg.length, lh, port);
            ds.send(dp);
            return null;
        }, acc);
        // Negative
        try {
            AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
                DatagramPacket dp = new DatagramPacket(msg, msg.length, lh, port);
                ds.send(dp);
                fail("Expected SecurityException");
                return null;
            }, RESTRICTED_ACC);
        } catch (SecurityException expected) {
        }
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) DatagramSocket(java.net.DatagramSocket) SocketPermission(java.net.SocketPermission) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress) Test(org.testng.annotations.Test)

Example 45 with SocketPermission

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

the class Wildcard method main.

public static void main(String[] args) throws Exception {
    SocketPermission star_All = new SocketPermission("*.blabla.bla", "listen,accept,connect");
    SocketPermission www_All = new SocketPermission("bla.blabla.bla", "listen,accept,connect");
    if (!star_All.implies(www_All)) {
        throw new RuntimeException("Failed: " + star_All + " does not imply " + www_All);
    }
}
Also used : SocketPermission(java.net.SocketPermission)

Aggregations

SocketPermission (java.net.SocketPermission)45 Deployment (org.jboss.arquillian.container.test.api.Deployment)13 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)10 AccessControlContext (java.security.AccessControlContext)9 PropertyPermission (java.util.PropertyPermission)9 FilePermission (java.io.FilePermission)8 Test (org.testng.annotations.Test)8 StringAsset (org.jboss.shrinkwrap.api.asset.StringAsset)7 URL (java.net.URL)6 IOException (java.io.IOException)5 DatagramSocket (java.net.DatagramSocket)5 InetAddress (java.net.InetAddress)4 InetSocketAddress (java.net.InetSocketAddress)4 MulticastSocket (java.net.MulticastSocket)4 ServerSocket (java.net.ServerSocket)3 Permissions (java.security.Permissions)3 PrivilegedActionException (java.security.PrivilegedActionException)3 Properties (java.util.Properties)3 JavaArchive (org.jboss.shrinkwrap.api.spec.JavaArchive)3 File (java.io.File)2