Path: blob/master/test/jdk/java/nio/channels/unixdomain/Bind.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* @library /test/lib27* @run main/othervm Bind28*/2930import java.io.IOException;31import java.net.*;32import java.nio.channels.*;33import java.nio.file.Files;34import java.nio.file.Path;35import java.util.Arrays;36import jtreg.SkippedException;3738/**39* Check that all bind variations work40*/41public class Bind {4243static Path spath, cpath;4445static UnixDomainSocketAddress sAddr, cAddr, UNNAMED, nullAddr;46static ServerSocketChannel server;47static SocketChannel client, accept1;4849public static void main(String args[]) throws Exception {50checkSupported();51spath = Path.of("server.sock");52cpath = Path.of("client.sock");53sAddr = UnixDomainSocketAddress.of(spath);54cAddr = UnixDomainSocketAddress.of(cpath);55nullAddr = UnixDomainSocketAddress.of("");56UNNAMED = nullAddr;57runTests();58}5960static void checkSupported() {61try {62SocketChannel.open(StandardProtocolFamily.UNIX).close();63} catch (UnsupportedOperationException e) {64throw new SkippedException("Unix domain channels not supported");65} catch (Exception e) {66// continue test to see what problem is67}68}6970static interface ThrowingRunnable {71public void run() throws Exception;72}7374static void init() throws IOException {75Files.deleteIfExists(cpath);76Files.deleteIfExists(spath);77client = null; server = null; accept1 = null;78}7980static void checkNormal(ThrowingRunnable r) {81try {82init();83r.run();84System.out.println("PASS:");85} catch (Exception e) {86throw new RuntimeException(e);87} finally {88cleanup();89}90}9192static void checkException(Class<? extends Exception> expected, ThrowingRunnable r) {93try {94init();95r.run();96throw new RuntimeException("Exception expected");97} catch (Exception e) {98if (!expected.isAssignableFrom(e.getClass())) {99String msg = "Expected: " + expected + " Got: " + e.getClass();100throw new RuntimeException(msg);101}102System.out.println("PASS: Got " + e);103} finally {104cleanup();105}106}107108static void cleanup() {109try {110if (server != null)111server.close();112if (client != null)113client.close();114if (accept1 != null)115accept1.close();116} catch (IOException e) {}117}118119static void assertClientAddress(SocketAddress a) {120assertAddress(a, cAddr, "client");121}122123static void assertServerAddress(SocketAddress a) {124assertAddress(a, sAddr, "server");125}126127static void assertAddress(SocketAddress a, UnixDomainSocketAddress a1, String s) {128if (!(a instanceof UnixDomainSocketAddress)) {129throw new RuntimeException("wrong address type");130}131UnixDomainSocketAddress ua = (UnixDomainSocketAddress)a;132if (!a.equals(a1))133throw new RuntimeException("this is not the " + s + " address");134}135136static void assertEquals(Object a, Object b) {137if (!a.equals(b))138throw new RuntimeException("identity check failed");139}140141public static void runTests() throws IOException {142checkNormal(() -> {143client = SocketChannel.open(StandardProtocolFamily.UNIX);144client.bind(cAddr);145});146checkNormal(() -> {147server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);148server.bind(sAddr);149});150// Repeat first two to make sure they are repeatable151checkNormal(() -> {152client = SocketChannel.open(StandardProtocolFamily.UNIX);153client.bind(cAddr);154});155checkNormal(() -> {156server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);157server.bind(sAddr);158});159// address with space should work160checkNormal(() -> {161server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);162UnixDomainSocketAddress usa = UnixDomainSocketAddress.of("with space"); // relative to CWD163Files.deleteIfExists(usa.getPath());164server.bind(usa);165client = SocketChannel.open(usa);166Files.delete(usa.getPath());167assertAddress(client.getRemoteAddress(), usa, "address");168});169// client bind to null: allowed170checkNormal(() -> {171client = SocketChannel.open(StandardProtocolFamily.UNIX);172client.bind(null);173SocketAddress a = client.getLocalAddress();174assertAddress(a, nullAddr, "null address");175assertEquals(a, UNNAMED);176});177// client bind to UNNAMED: allowed178checkNormal(() -> {179client = SocketChannel.open(StandardProtocolFamily.UNIX);180client.bind(UNNAMED);181SocketAddress a = client.getLocalAddress();182assertAddress(a, nullAddr, "null address");183assertEquals(a, UNNAMED);184});185// server bind to null: should bind to a local address186checkNormal(() -> {187server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);188server.bind(null);189UnixDomainSocketAddress usa = (UnixDomainSocketAddress)server.getLocalAddress();190if (usa.getPath().toString().isEmpty())191throw new RuntimeException("expected non zero address length");192System.out.println("Null server address: " + server.getLocalAddress());193});194// server no bind : not allowed195checkException(196NotYetBoundException.class, () -> {197server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);198server.accept();199}200);201202// client implicit bind and connect203checkNormal(() -> {204server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);205client = SocketChannel.open(StandardProtocolFamily.UNIX);206server.bind(sAddr);207client.connect(sAddr);208SocketAddress cAddr = client.getLocalAddress();209assertAddress(cAddr, nullAddr, "null address");210assertEquals(cAddr, UNNAMED);211assertServerAddress(server.getLocalAddress());212});213// client null bind and connect (check all addresses)214checkNormal(() -> {215server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);216client = SocketChannel.open(StandardProtocolFamily.UNIX);217server.bind(sAddr);218client.bind(null);219client.connect(sAddr);220assertAddress(client.getLocalAddress(), UNNAMED, "unnamed address");221assertServerAddress(server.getLocalAddress());222});223// client explicit bind and connect (check all addresses)224checkNormal(() -> {225server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);226client = SocketChannel.open(StandardProtocolFamily.UNIX);227server.bind(sAddr);228client.bind(cAddr);229client.connect(sAddr);230accept1 = server.accept();231assertClientAddress(client.getLocalAddress());232assertServerAddress(server.getLocalAddress());233assertAddress(client.getRemoteAddress(), sAddr, "client's remote server address");234assertAddress(accept1.getLocalAddress(), sAddr, "accepted local address (server)");235assertAddress(accept1.getRemoteAddress(), cAddr, "accepted remote address (client)");236});237// server multiple bind : not allowed238checkException(239AlreadyBoundException.class, () -> {240server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);241server.bind(sAddr);242server.bind(sAddr);243}244);245// client multiple bind : not allowed246checkException(247AlreadyBoundException.class, () -> {248client = SocketChannel.open(StandardProtocolFamily.UNIX);249client.bind(cAddr);250client.bind(cAddr);251}252);253// client multiple bind to different addresses: not allowed254checkException(255AlreadyBoundException.class, () -> {256client = SocketChannel.open(StandardProtocolFamily.UNIX);257client.bind(cAddr);258client.bind(sAddr);259}260);261// client multiple bind to differnt addresses, incl null: not allowed262checkException(263AlreadyBoundException.class, () -> {264client = SocketChannel.open(StandardProtocolFamily.UNIX);265client.bind(null);266client.bind(cAddr);267}268);269270// server bind to existing name: not allowed271272checkException(273BindException.class, () -> {274var path = Files.createFile(Path.of("moo.sock"));275var addr = UnixDomainSocketAddress.of(path);276server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);277try {278server.bind(addr);279} finally {280Files.deleteIfExists(path);281}282}283);284285286// client bind to existing name: not allowed287checkException(288BindException.class, () -> {289var path = Path.of("temp.sock");290Files.deleteIfExists(path);291Files.createFile(path);292var addr = UnixDomainSocketAddress.of(path);293client = SocketChannel.open(StandardProtocolFamily.UNIX);294try {295client.bind(addr);296} finally {297Files.deleteIfExists(path);298}299}300);301302// bind and connect to name of close to max size303checkNormal(() -> {304int len = 100;305char[] chars = new char[len];306Arrays.fill(chars, 'x');307String name = new String(chars);308UnixDomainSocketAddress address = UnixDomainSocketAddress.of(name);309ServerSocketChannel server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);310server.bind(address);311SocketChannel client = SocketChannel.open(address);312assertAddress(server.getLocalAddress(), address, "server");313assertAddress(client.getRemoteAddress(), address, "client");314Files.delete(address.getPath());315});316317// implicit server bind318checkNormal(() -> {319server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);320server.bind(null);321UnixDomainSocketAddress usa = (UnixDomainSocketAddress)server.getLocalAddress();322client = SocketChannel.open(usa);323accept1 = server.accept();324assertAddress(client.getRemoteAddress(), usa, "server");325Files.delete(usa.getPath());326});327}328}329330331