WithSecurityManager.java revision 893
893N/A/*
893N/A * Copyright 2008-2009 Sun Microsystems, Inc. All Rights Reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
893N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
893N/A * CA 95054 USA or visit www.sun.com if you need additional information or
893N/A * have any questions.
893N/A */
893N/A
893N/A/* @test
893N/A * @bug 4607272
893N/A * @summary Unit test for AsynchronousServerServerSocketChannel
893N/A * @build WithSecurityManager
893N/A * @run main/othervm WithSecurityManager allow
893N/A * @run main/othervm WithSecurityManager deny
893N/A */
893N/A
893N/Aimport java.nio.file.Paths;
893N/Aimport java.nio.channels.*;
893N/Aimport java.net.*;
893N/Aimport java.util.concurrent.*;
893N/A
893N/Apublic class WithSecurityManager {
893N/A public static void main(String[] args) throws Exception {
893N/A boolean allow = false;
893N/A String policy = (args[0].equals("allow")) ? "java.policy.allow" :
893N/A "java.policy.deny";
893N/A
893N/A String testSrc = System.getProperty("test.src");
893N/A if (testSrc == null)
893N/A testSrc = ".";
893N/A
893N/A System.setProperty("java.security.policy",
893N/A Paths.get(testSrc).resolve(policy).toString());
893N/A System.setSecurityManager(new SecurityManager());
893N/A
893N/A AsynchronousServerSocketChannel listener =
893N/A AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(0));
893N/A
893N/A InetAddress lh = InetAddress.getLocalHost();
893N/A int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();
893N/A
893N/A // establish and accept connection
893N/A SocketChannel sc = SocketChannel.open(new InetSocketAddress(lh, port));
893N/A Future<AsynchronousSocketChannel> result = listener.accept();
893N/A
893N/A if (allow) {
893N/A // no security exception
893N/A result.get().close();
893N/A } else {
893N/A try {
893N/A result.get();
893N/A } catch (ExecutionException x) {
893N/A if (!(x.getCause() instanceof SecurityException))
893N/A throw new RuntimeException("SecurityException expected");
893N/A }
893N/A }
893N/A
893N/A sc.close();
893N/A listener.close();
893N/A }
893N/A}