Path: blob/master/test/jdk/java/nio/channels/unixdomain/Shutdown.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 Shutdown27*/2829import java.io.Closeable;30import java.io.IOException;31import java.net.*;32import java.nio.ByteBuffer;33import java.nio.channels.*;34import java.nio.charset.StandardCharsets;35import java.nio.file.Files;36import java.util.Arrays;3738/**39* Check that half close works40*/41public class Shutdown {4243public static void main(String args[]) throws Exception {44if (!supported()) {45System.out.println("Unix domain channels not supported");46return;47}48runTest();49}5051static boolean supported() {52try {53SocketChannel.open(StandardProtocolFamily.UNIX).close();54} catch (UnsupportedOperationException e) {55return false;56} catch (Exception e) {57return true; // continue test to see what problem is58}59return true;60}6162static void assertTrue(boolean condition, String error) {63if (!condition)64throw new RuntimeException(error);65}6667public static void runTest() throws IOException {68ServerSocketChannel server = null;69SocketChannel client = null;70SocketChannel acceptee = null;71UnixDomainSocketAddress usa = null;7273try {74server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);75server.bind(null);76usa = (UnixDomainSocketAddress)server.getLocalAddress();77System.out.println("Local address " + usa);78client = SocketChannel.open(usa);79acceptee = server.accept();80ByteBuffer buf = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.ISO_8859_1));81ByteBuffer rx = ByteBuffer.allocate(buf.capacity());82client.write(buf);83buf.rewind();84while (rx.hasRemaining())85acceptee.read(rx);8687assertTrue(Arrays.equals(buf.array(), rx.array()), "array contents not equal");8889client.shutdownOutput();90try {91client.write(buf);92throw new RuntimeException("shutdown error");93} catch (ClosedChannelException e) {94}9596rx.clear();97int c = acceptee.read(rx);98assertTrue(c == -1, "read after remote shutdown");99100client.configureBlocking(false);101c = client.read(rx);102assertTrue(c == 0, "expected c == 0");103client.shutdownInput();104c = client.read(rx);105assertTrue(c == -1, "expected c == -1");106} finally {107close(server);108close(client);109close(acceptee);110if (usa != null)111Files.delete(usa.getPath());112}113System.out.println("OK");114}115116static void close(Closeable c) {117try {118if (c != null)119c.close();120} catch (IOException e) {}121}122}123124125