Search in sources :

Example 1 with SocketPermission

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

the class SocketPermissionTest method acceptServerSocketTest.

@Test
public void acceptServerSocketTest() 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"), new SocketPermission("localhost:1024-", "accept"));
        // Positive
        AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
            InetAddress me = InetAddress.getLocalHost();
            try (Socket client = new Socket(me, port)) {
                ss.accept();
            }
            return null;
        }, acc);
        // Negative
        try {
            AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
                InetAddress me = InetAddress.getLocalHost();
                try (Socket client = new Socket(me, port)) {
                    ss.accept();
                }
                fail("Expected SecurityException");
                return null;
            }, RESTRICTED_ACC);
        } catch (SecurityException expected) {
        }
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) SocketPermission(java.net.SocketPermission) ServerSocket(java.net.ServerSocket) InetAddress(java.net.InetAddress) Socket(java.net.Socket) DatagramSocket(java.net.DatagramSocket) ServerSocket(java.net.ServerSocket) MulticastSocket(java.net.MulticastSocket) Test(org.testng.annotations.Test)

Example 2 with SocketPermission

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

the class SocketPermissionTest method joinGroupMulticastTest.

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

Example 3 with SocketPermission

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

the class SocketPermissionTest method sendDatagramPacketTest.

@Test
public void sendDatagramPacketTest() throws Exception {
    byte[] msg = "Hello".getBytes(UTF_8);
    InetAddress group = InetAddress.getByName("229.227.226.221");
    try (DatagramSocket ds = new DatagramSocket(0)) {
        int port = ds.getLocalPort();
        String addr = "localhost:" + port;
        //test for SocketPermission "229.227.226.221", "connect,accept"
        AccessControlContext acc = getAccessControlContext(new SocketPermission(addr, "listen,resolve"), new SocketPermission("229.227.226.221", "connect,accept"));
        // Positive
        AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
            DatagramPacket hi = new DatagramPacket(msg, msg.length, group, port);
            ds.send(hi);
            return null;
        }, acc);
        // Negative
        try {
            AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
                DatagramPacket hi = new DatagramPacket(msg, msg.length, group, port);
                ds.send(hi);
                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 4 with SocketPermission

use of java.net.SocketPermission in project wildfly by wildfly.

the class SPNEGOLoginModuleTestCase method deployment.

/**
     * Creates {@link WebArchive}.
     *
     * @return
     */
@Deployment(name = "WEB", testable = false)
public static WebArchive deployment() {
    LOGGER.debug("Web deployment");
    final WebArchive war = createWebApp(WEBAPP_NAME, "web-spnego-authn.xml", "SPNEGO");
    war.addAsManifestResource(PermissionUtils.createPermissionsXmlAsset(// Permissions for PropagateIdentityServlet to get delegation credentials DelegationCredentialContext.getDelegCredential()
    new RuntimePermission("org.jboss.security.negotiation.getDelegCredential"), // Permissions for PropagateIdentityServlet to read properties
    new PropertyPermission(GSSTestConstants.PROPERTY_PORT, "read"), new PropertyPermission(GSSTestConstants.PROPERTY_PRINCIPAL, "read"), new PropertyPermission(GSSTestConstants.PROPERTY_PASSWORD, "read"), // Permissions for GSSTestClient to connect to GSSTestServer
    new SocketPermission(TestSuiteEnvironment.getServerAddress(), "resolve,connect"), // Permissions for GSSTestClient to initiate gss context
    new ServicePermission(GSSTestConstants.PRINCIPAL, "initiate"), new ServicePermission("krbtgt/JBOSS.ORG@JBOSS.ORG", "initiate")), "permissions.xml");
    return war;
}
Also used : PropertyPermission(java.util.PropertyPermission) ServicePermission(javax.security.auth.kerberos.ServicePermission) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) SocketPermission(java.net.SocketPermission) OperateOnDeployment(org.jboss.arquillian.container.test.api.OperateOnDeployment) Deployment(org.jboss.arquillian.container.test.api.Deployment)

Example 5 with SocketPermission

use of java.net.SocketPermission in project robovm by robovm.

the class HttpURLConnectionImpl method getPermission.

@Override
public final Permission getPermission() throws IOException {
    String hostName = getURL().getHost();
    int hostPort = Util.getEffectivePort(getURL());
    if (usingProxy()) {
        InetSocketAddress proxyAddress = (InetSocketAddress) client.getProxy().address();
        hostName = proxyAddress.getHostName();
        hostPort = proxyAddress.getPort();
    }
    return new SocketPermission(hostName + ":" + hostPort, "connect, resolve");
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketPermission(java.net.SocketPermission)

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