Path: blob/master/test/jdk/java/nio/channels/unixdomain/SocketOptions.java
41153 views
/*1* Copyright (c) 2020, 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 SocketOptions27*/2829import java.io.IOException;30import java.net.*;31import java.nio.ByteBuffer;32import java.nio.channels.*;33import java.nio.file.Files;34import java.nio.file.Path;35import java.util.Set;36import jdk.net.UnixDomainPrincipal;37import static jdk.net.ExtendedSocketOptions.SO_PEERCRED;3839/**40* Check that all supported options can actually be set and got41*/42public class SocketOptions {4344public static void main(String args[]) throws Exception {45if (!supported()) {46System.out.println("Unix domain channels not supported");47return;48}49test(ServerSocketChannel.open(StandardProtocolFamily.UNIX));50test(SocketChannel.open(StandardProtocolFamily.UNIX));51testPeerCred();52}5354static void testPeerCred() throws Exception {55UnixDomainSocketAddress addr = null;56UnixDomainPrincipal p;57try (ServerSocketChannel s = ServerSocketChannel.open(StandardProtocolFamily.UNIX)) {58s.bind(null);59addr = (UnixDomainSocketAddress)s.getLocalAddress();60try (SocketChannel c = SocketChannel.open(addr)) {61if (!c.supportedOptions().contains(SO_PEERCRED)) {62return;63}64Files.deleteIfExists(addr.getPath());65p = c.getOption(SO_PEERCRED);66String s1 = p.user().getName();67System.out.println(s1);68System.out.println(p.group().getName());69String s2 = System.getProperty("user.name");7071// Check returned user name7273if (!s1.equals(s2)) {74throw new RuntimeException("wrong username");75}7677// Try setting the option: Read only7879try {80c.setOption(SO_PEERCRED, p);81throw new RuntimeException("should have thrown SocketException");82} catch (SocketException e) {}83}84} finally {85if (addr != null)86Files.deleteIfExists(addr.getPath());87}8889// Try getting from unconnected socket9091try (var c = SocketChannel.open(StandardProtocolFamily.UNIX)) {92try {93p = c.getOption(SO_PEERCRED);94System.out.println(p.user());95throw new RuntimeException("should have thrown SocketException");96} catch (SocketException e) {}97}9899// Try getting from ServerSocketChannel100101try (var server = ServerSocketChannel.open(StandardProtocolFamily.UNIX)) {102try {103p = server.getOption(SO_PEERCRED);104System.out.println(p.user());105throw new RuntimeException("should have thrown USE");106} catch (UnsupportedOperationException e) {}107}108}109110static boolean supported() {111try {112SocketChannel.open(StandardProtocolFamily.UNIX).close();113} catch (UnsupportedOperationException e) {114return false;115} catch (Exception e) {116return true; // continue test to see what problem is117}118return true;119}120121@SuppressWarnings("unchecked")122public static void test(NetworkChannel chan) throws IOException {123System.out.println("Checking: " + chan.getClass());124Set<SocketOption<?>> supported = chan.supportedOptions();125for (SocketOption<?> option : supported) {126String name = option.name();127System.out.println("Checking option " + name);128if (option.type() == Boolean.class) {129chan.setOption((SocketOption<Boolean>)option, true);130chan.setOption((SocketOption<Boolean>)option, false);131chan.getOption(option);132} else if (option.type() == Integer.class) {133chan.setOption((SocketOption<Integer>)option, 10);134chan.getOption(option);135}136}137}138}139140141