Search in sources :

Example 76 with AccessControlContext

use of java.security.AccessControlContext 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 77 with AccessControlContext

use of java.security.AccessControlContext 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 78 with AccessControlContext

use of java.security.AccessControlContext 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 79 with AccessControlContext

use of java.security.AccessControlContext 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 80 with AccessControlContext

use of java.security.AccessControlContext in project jdk8u_jdk by JetBrains.

the class SocketPermissionTest method getAccessControlContext.

private static AccessControlContext getAccessControlContext(Permission... ps) {
    Permissions perms = new Permissions();
    for (Permission p : ps) {
        perms.add(p);
    }
    /*
         *Create an AccessControlContext that consist a single protection domain
         * with only the permissions calculated above
         */
    ProtectionDomain pd = new ProtectionDomain(null, perms);
    return new AccessControlContext(new ProtectionDomain[] { pd });
}
Also used : ProtectionDomain(java.security.ProtectionDomain) AccessControlContext(java.security.AccessControlContext) Permissions(java.security.Permissions) Permission(java.security.Permission) SocketPermission(java.net.SocketPermission)

Aggregations

AccessControlContext (java.security.AccessControlContext)100 ProtectionDomain (java.security.ProtectionDomain)24 Subject (javax.security.auth.Subject)24 PrivilegedAction (java.security.PrivilegedAction)18 Permissions (java.security.Permissions)14 PrivilegedActionException (java.security.PrivilegedActionException)13 IOException (java.io.IOException)11 SocketPermission (java.net.SocketPermission)10 Test (org.testng.annotations.Test)8 Principal (java.security.Principal)7 CodeSource (java.security.CodeSource)6 Permission (java.security.Permission)6 DatagramSocket (java.net.DatagramSocket)5 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)5 MulticastSocket (java.net.MulticastSocket)4 Set (java.util.Set)4 ExecutorService (java.util.concurrent.ExecutorService)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 ReflectionException (javax.management.ReflectionException)4 Test (org.junit.Test)4