Path: blob/master/test/jdk/java/nio/channels/unixdomain/Security.java
41153 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 824519426* @run main/othervm/java.security.policy=policy1 Security policy127* @run main/othervm/java.security.policy=policy2 Security policy228* @run main/othervm -Djava.security.manager=allow Security policy329* @summary Security test for Unix Domain socket and server socket channels30*/3132import java.io.File;33import java.io.IOException;34import java.net.SocketAddress;35import java.net.UnixDomainSocketAddress;36import java.nio.channels.*;37import java.nio.file.Files;38import java.nio.file.Path;39import java.util.Comparator;4041import static java.net.StandardProtocolFamily.UNIX;4243/**44* Tests required all with security manager45*/4647public class Security {4849static interface Command {50public void run() throws Exception;51}5253static <T extends Exception> void call(Command r, Class<? extends Exception> expectedException) {54boolean threw = false;55try {56r.run();57} catch (Throwable t) {58if (expectedException == null) {59t.printStackTrace();60throw new RuntimeException("an exception was thrown but was not expected");61}62threw = true;63if (!(expectedException.isAssignableFrom(t.getClass()))) {64throw new RuntimeException("wrong exception type thrown " + t.toString());65}66}67if (expectedException != null && !threw) {68// should have thrown69throw new RuntimeException("% was expected".formatted(expectedException.getName()));70}71}727374public static void main(String[] args) throws Exception {75try {76SocketChannel.open(UNIX);77} catch (UnsupportedOperationException e) {78System.out.println("Unix domain not supported");79return;80}8182String policy = args[0];83switch (policy) {84case "policy1":85testPolicy1();86break;87case "policy2":88testPolicy2();89break;90case "policy3":91testPolicy3();92break;93}94}9596static void setSecurityManager(String policy) {97String testSrc = System.getProperty("test.src");98// Three /// required for Windows below99String policyURL = "file:///" + testSrc + File.separator + policy;100System.out.println("POLICY: " + policyURL);101System.setProperty("java.security.policy", policyURL);102System.setSecurityManager(new SecurityManager());103}104105static void close(NetworkChannel... channels) {106107for (NetworkChannel chan : channels) {108try {109chan.close();110} catch (Exception e) {111}112}113}114115private static final Class<SecurityException> SE = SecurityException.class;116private static final Class<IOException> IOE = IOException.class;117118// No permission119120public static void testPolicy1() throws Exception {121Path servername = Path.of("sock");122Files.deleteIfExists(servername);123// Permission exists to bind a ServerSocketChannel124final UnixDomainSocketAddress saddr = UnixDomainSocketAddress.of(servername);125try (final ServerSocketChannel server = ServerSocketChannel.open(UNIX)) {126try (final SocketChannel client = SocketChannel.open(UNIX)) {127call(() -> {128server.bind(saddr);129}, SE);130call(() -> {131client.connect(saddr);132}, SE);133}134} finally {135Files.deleteIfExists(servername);136}137}138139// All permissions140141public static void testPolicy2() throws Exception {142Path servername = Path.of("sock");143Files.deleteIfExists(servername);144final UnixDomainSocketAddress saddr = UnixDomainSocketAddress.of(servername);145try (final ServerSocketChannel server = ServerSocketChannel.open(UNIX)) {146try (final SocketChannel client = SocketChannel.open(UNIX)) {147call(() -> {148server.bind(saddr);149}, null);150call(() -> {151client.connect(saddr);152}, null);153try (final SocketChannel peer = server.accept()) {154// Should succeed155}156}157} finally {158Files.deleteIfExists(servername);159}160}161162public static void testPolicy3() throws Exception {163Path sock1 = Path.of("sock3");164Path sock2 = null;165Files.deleteIfExists(sock1);166final UnixDomainSocketAddress saddr = UnixDomainSocketAddress.of(sock1);167try (var s1 = ServerSocketChannel.open(UNIX)) {168s1.bind(saddr);169try (var s2 = ServerSocketChannel.open(UNIX)) {170s2.bind(null);171var add2 = (UnixDomainSocketAddress)s2.getLocalAddress();172sock2 = add2.getPath();173174// Now set security manager and check if we can see addresses175176setSecurityManager("policy3");177178if (((UnixDomainSocketAddress)s1179.getLocalAddress())180.getPath()181.toString()182.length() != 0)183{184throw new RuntimeException("address should have been empty");185}186187if (((UnixDomainSocketAddress)s2188.getLocalAddress())189.getPath()190.toString()191.length() != 0)192{193throw new RuntimeException("address should have been empty");194}195}196} finally {197System.setSecurityManager(null);198Files.deleteIfExists(sock1);199Files.deleteIfExists(sock2);200}201}202}203204205