test/jdk/java/nio/channels/unixdomain/Bind.java
author michaelm
Thu, 14 Nov 2019 15:01:49 +0000
branchunixdomainchannels
changeset 59082 5e250ee9259e
parent 59078 4e648a2d8480
permissions -rw-r--r--
unixdomainchannels: fix some sockaddr_un address handling
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
        }
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    56
        spath = Path.of("server.sock");
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    57
        cpath = Path.of("client.sock");
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    58
        sAddr = new UnixDomainSocketAddress(spath);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    59
        cAddr = new UnixDomainSocketAddress(cpath);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    60
        nullAddr = new UnixDomainSocketAddress("");
59078
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() {
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    65
        try {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    66
            SocketChannel.open(StandardProtocolFamily.UNIX);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    67
        } catch (UnsupportedAddressTypeException e) {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    68
            return false;
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    69
        } catch (Exception e) {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    70
            return true; // continue test to see what problem is
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    71
        }
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    72
        return true;
59078
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 {
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    76
        public void run() throws Exception;
59078
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 {
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    80
        Files.deleteIfExists(cpath);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    81
        Files.deleteIfExists(spath);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    82
        client = null; server = null; accept1 = null;
59078
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
    83
    }
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    84
59078
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
    85
    static void checkNormal(ThrowingRunnable r) {
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    86
        try {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    87
            init();
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    88
            r.run();
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    89
            System.out.println("PASS:");
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    90
        } catch (Exception e) {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    91
            throw new RuntimeException(e);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    92
        } finally {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    93
            cleanup();
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    94
        }
59078
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) {
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    98
        try {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
    99
            init();
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   100
            r.run();
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   101
            throw new RuntimeException("Exception expected");
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   102
        } catch (Exception e) {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   103
            if (!expected.isAssignableFrom(e.getClass())) {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   104
                String msg = "Expected: " + expected + " Got: " + e.getClass();
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   105
                throw new RuntimeException(msg);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   106
            }
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   107
            System.out.println("PASS: Got " + e);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   108
        } finally {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   109
            cleanup();
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   110
        }
59078
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() {
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   114
        try {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   115
            if (server != null)
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   116
                server.close();
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   117
            if (client != null)
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   118
                client.close();
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   119
            if (accept1 != null)
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   120
                accept1.close();
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   121
        } catch (IOException e) {}
59078
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) {
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   125
        assertAddress(a, cAddr, "client");
59078
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) {
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   129
        assertAddress(a, sAddr, "server");
59078
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) {
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   133
        if (!(a instanceof UnixDomainSocketAddress)) {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   134
            System.err.println("adddr = " + a);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   135
            throw new RuntimeException("wrong address type");
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   136
        }
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   137
        UnixDomainSocketAddress ua = (UnixDomainSocketAddress)a;
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   138
        if (!a.equals(a1))
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   139
            throw new RuntimeException("this is not the " + s + " address");
59078
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   140
    }
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   141
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   142
    public static void runTests() throws IOException {
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   143
        checkNormal(() -> {
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   144
            client = SocketChannel.open(StandardProtocolFamily.UNIX);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   145
            client.bind(cAddr);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   146
        });
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   147
        checkNormal(() -> {
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   148
            server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   149
            server.bind(sAddr);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   150
        });
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   151
        // Repeat first two to make sure they are repeatable
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   152
        checkNormal(() -> {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   153
            client = SocketChannel.open(StandardProtocolFamily.UNIX);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   154
            client.bind(cAddr);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   155
        });
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   156
        checkNormal(() -> {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   157
            server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   158
            server.bind(sAddr);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   159
        });
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   160
        // client bind to null: allowed
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   161
        checkNormal(() -> {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   162
            client = SocketChannel.open(StandardProtocolFamily.UNIX);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   163
            client.bind(null);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   164
            SocketAddress a = client.getLocalAddress();
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   165
            assertAddress(client.getLocalAddress(), nullAddr, "null address");
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   166
        });
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   167
        // server bind to null: not allowed
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   168
        checkException(
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   169
            BindException.class, () -> {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   170
                server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   171
                server.bind(null);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   172
            }
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   173
        );
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   174
        // server no bind : not allowed
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   175
        checkException(
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   176
            NotYetBoundException.class, () -> {
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   177
                server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   178
                server.accept();
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   179
            }
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   180
        );
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   181
        // client implicit bind and connect
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   182
        checkNormal(() -> {
59078
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   183
            server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   184
            client = SocketChannel.open(StandardProtocolFamily.UNIX);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   185
            server.bind(sAddr);
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   186
            client.connect(sAddr);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   187
            assertAddress(client.getLocalAddress(), nullAddr, "null address");
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   188
            assertServerAddress(server.getLocalAddress());
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   189
        });
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   190
        // client null bind and connect (check all addresses)
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   191
        checkNormal(() -> {
59078
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   192
            server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   193
            client = SocketChannel.open(StandardProtocolFamily.UNIX);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   194
            server.bind(sAddr);
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   195
            client.bind(null);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   196
            client.connect(sAddr);
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   197
            assertAddress(client.getLocalAddress(), nullAddr, "null address");
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   198
            assertServerAddress(server.getLocalAddress());
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   199
        });
59078
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   200
        // client explicit bind and connect (check all addresses)
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   201
        checkNormal(() -> {
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   202
            server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   203
            client = SocketChannel.open(StandardProtocolFamily.UNIX);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   204
            server.bind(sAddr);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   205
            client.bind(cAddr);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   206
            client.connect(sAddr);
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   207
            accept1 = server.accept();
59078
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   208
            assertClientAddress(client.getLocalAddress());
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   209
            assertServerAddress(server.getLocalAddress());
59082
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   210
            assertAddress(client.getRemoteAddress(), sAddr, "client's remote server address");
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   211
            assertAddress(accept1.getLocalAddress(), sAddr, "accepted local address (server)");
5e250ee9259e unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents: 59078
diff changeset
   212
            assertAddress(accept1.getRemoteAddress(), cAddr, "accepted remote address (client)");
59078
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   213
        });
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   214
        // server multiple bind : not allowed
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   215
        checkException(
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   216
            AlreadyBoundException.class, () -> {
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   217
                server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   218
                server.bind(sAddr);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   219
                server.bind(sAddr);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   220
            }
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   221
        );
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   222
        // client multiple bind : not allowed
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   223
        checkException(
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   224
            AlreadyBoundException.class, () -> {
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   225
                client = SocketChannel.open(StandardProtocolFamily.UNIX);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   226
                client.bind(cAddr);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   227
                client.bind(cAddr);
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   228
            }
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   229
        );
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   230
    }
4e648a2d8480 unixdomainchannels: new test plus apidoc update
michaelm
parents:
diff changeset
   231
}