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) {
}
}
}
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) {
}
}
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) {
}
}
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) {
}
}
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 });
}
Aggregations