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