test/jdk/java/nio/channels/unixdomain/Bind.java
branchunixdomainchannels
changeset 59078 4e648a2d8480
child 59082 5e250ee9259e
equal deleted inserted replaced
59077:85df93a18fe0 59078:4e648a2d8480
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 8231358
       
    27  * @run main Bind
       
    28  * @summary Bind test
       
    29  */
       
    30 
       
    31 import java.io.Closeable;
       
    32 import java.io.IOException;
       
    33 import java.net.*;
       
    34 import java.nio.ByteBuffer;
       
    35 import java.nio.channels.*;
       
    36 import java.nio.file.Files;
       
    37 import java.nio.file.Path;
       
    38 import java.util.Set;
       
    39 
       
    40 /**
       
    41  * Check that all bind variations work
       
    42  */
       
    43 public class Bind {
       
    44 
       
    45     static Path spath, cpath;
       
    46 
       
    47     static UnixDomainSocketAddress sAddr, cAddr, nullAddr;
       
    48     static ServerSocketChannel server;
       
    49     static SocketChannel client, accept1;
       
    50 
       
    51     public static void main(String args[]) throws Exception {
       
    52         if (!supported()) {
       
    53             System.out.println("Unix domain channels not supported");
       
    54             return;
       
    55         }
       
    56 	spath = Path.of("server.sock");
       
    57 	cpath = Path.of("client.sock");
       
    58 	sAddr = new UnixDomainSocketAddress(spath);
       
    59 	cAddr = new UnixDomainSocketAddress(cpath);
       
    60 	nullAddr = new UnixDomainSocketAddress("");
       
    61         runTests();
       
    62     }
       
    63 
       
    64     static boolean supported() {
       
    65 	try {
       
    66 	    SocketChannel.open(StandardProtocolFamily.UNIX);
       
    67 	} catch (UnsupportedAddressTypeException e) {
       
    68 	    return false;
       
    69 	} catch (Exception e) {
       
    70 	    return true; // continue test to see what problem is
       
    71 	}
       
    72 	return true;
       
    73     }
       
    74 
       
    75     static interface ThrowingRunnable {
       
    76 	public void run() throws Exception;
       
    77     }
       
    78 
       
    79     static void init() throws IOException {
       
    80 	Files.deleteIfExists(cpath);
       
    81 	Files.deleteIfExists(spath);
       
    82 	client = null; server = null; accept1 = null;
       
    83     }
       
    84 	
       
    85     static void checkNormal(ThrowingRunnable r) {
       
    86 	try {
       
    87 	    init();
       
    88 	    r.run();
       
    89 	    System.out.println("PASS:");
       
    90 	} catch (Exception e) {
       
    91 	    throw new RuntimeException(e);
       
    92 	} finally {
       
    93 	    cleanup();
       
    94 	}
       
    95     }
       
    96 
       
    97     static void checkException(Class<? extends Exception> expected, ThrowingRunnable r) {
       
    98 	try {
       
    99 	    init();
       
   100 	    r.run();
       
   101 	    throw new RuntimeException("Exception expected");
       
   102 	} catch (Exception e) {
       
   103 	    if (!expected.isAssignableFrom(e.getClass())) {
       
   104 		String msg = "Expected: " + expected + " Got: " + e.getClass();
       
   105 	    	throw new RuntimeException(msg);
       
   106 	    }
       
   107 	    System.out.println("PASS: Got " + e);
       
   108 	} finally {
       
   109 	    cleanup();
       
   110 	}
       
   111     }
       
   112 
       
   113     static void cleanup() {
       
   114 	try {
       
   115 	    if (server != null)
       
   116 		server.close();
       
   117 	    if (client != null)
       
   118 		client.close();
       
   119 	    if (accept1 != null)
       
   120 		accept1.close();
       
   121 	} catch (IOException e) {}
       
   122     }
       
   123 
       
   124     static void assertClientAddress(SocketAddress a) {
       
   125 	assertAddress(a, cAddr, "client");
       
   126     }
       
   127 
       
   128     static void assertServerAddress(SocketAddress a) {
       
   129 	assertAddress(a, sAddr, "server");
       
   130     }
       
   131 
       
   132     static void assertAddress(SocketAddress a, UnixDomainSocketAddress a1, String s) {
       
   133 	if (!(a instanceof UnixDomainSocketAddress))
       
   134 	    throw new RuntimeException("wrong address type");
       
   135 	UnixDomainSocketAddress ua = (UnixDomainSocketAddress)a;
       
   136 	if (!a.equals(a1))
       
   137 	    throw new RuntimeException("this is not the " + s + " address");
       
   138     }
       
   139 
       
   140     public static void runTests() throws IOException {
       
   141 	checkNormal(() -> {
       
   142 	    client = SocketChannel.open(StandardProtocolFamily.UNIX);
       
   143 	    client.bind(cAddr);
       
   144 	});
       
   145 	checkNormal(() -> {
       
   146 	    server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
       
   147 	    server.bind(sAddr);
       
   148 	});
       
   149 	// Repeat first two to make sure they are repeatable
       
   150         checkNormal(() -> {
       
   151             client = SocketChannel.open(StandardProtocolFamily.UNIX);
       
   152             client.bind(cAddr);
       
   153         });
       
   154         checkNormal(() -> {
       
   155             server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
       
   156             server.bind(sAddr);
       
   157         });
       
   158 	// client bind to null: allowed
       
   159 	checkNormal(() -> {
       
   160 	    client = SocketChannel.open(StandardProtocolFamily.UNIX);
       
   161 	    client.bind(null);
       
   162 	    assertAddress(client.getLocalAddress(), nullAddr, "null address");
       
   163 	});
       
   164 	// server bind to null: not allowed
       
   165 	checkException(
       
   166 	    BindException.class, () -> {
       
   167 	        server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
       
   168 	        server.bind(null);
       
   169 	    }
       
   170 	);
       
   171 	// server no bind : not allowed
       
   172 	checkException(
       
   173 	    NotYetBoundException.class, () -> {
       
   174 	        server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
       
   175 		server.accept();
       
   176 	    }
       
   177 	);
       
   178 	// client implicit bind and connect
       
   179 	checkNormal(() -> {
       
   180             server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
       
   181             client = SocketChannel.open(StandardProtocolFamily.UNIX);
       
   182             server.bind(sAddr);
       
   183 	    client.connect(sAddr);
       
   184 	    assertAddress(client.getLocalAddress(), nullAddr, "null address");
       
   185 	    assertServerAddress(server.getLocalAddress());
       
   186 	});
       
   187 	// client null bind and connect (check all addresses)
       
   188 	checkNormal(() -> {
       
   189             server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
       
   190             client = SocketChannel.open(StandardProtocolFamily.UNIX);
       
   191             server.bind(sAddr);
       
   192 	    client.bind(null);
       
   193 	    client.connect(sAddr);
       
   194 	    assertAddress(client.getLocalAddress(), nullAddr, "null address");
       
   195 	    assertServerAddress(server.getLocalAddress());
       
   196 	});
       
   197         // client explicit bind and connect (check all addresses)
       
   198         checkNormal(() -> {
       
   199             server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
       
   200             client = SocketChannel.open(StandardProtocolFamily.UNIX);
       
   201             server.bind(sAddr);
       
   202             client.bind(cAddr);
       
   203             client.connect(sAddr);
       
   204 	    accept1 = server.accept();
       
   205             assertClientAddress(client.getLocalAddress());
       
   206             assertServerAddress(server.getLocalAddress());
       
   207 	    assertAddress(client.getRemoteAddress(), sAddr, "client's remote server address");
       
   208 	    assertAddress(accept1.getLocalAddress(), sAddr, "accepted local address (server)");
       
   209 	    assertAddress(accept1.getRemoteAddress(), cAddr, "accepted remote address (client)");
       
   210         });
       
   211         // server multiple bind : not allowed
       
   212         checkException(
       
   213             AlreadyBoundException.class, () -> {
       
   214                 server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
       
   215                 server.bind(sAddr);
       
   216                 server.bind(sAddr);
       
   217             }
       
   218         );
       
   219         // client multiple bind : not allowed
       
   220         checkException(
       
   221             AlreadyBoundException.class, () -> {
       
   222                 client = SocketChannel.open(StandardProtocolFamily.UNIX);
       
   223                 client.bind(cAddr);
       
   224                 client.bind(cAddr);
       
   225             }
       
   226         );
       
   227     }
       
   228 }