test/jdk/java/nio/channels/DatagramChannel/MulticastSendReceiveTests.java
author alanb
Sat, 09 Nov 2019 11:48:37 +0000
changeset 59000 612c58965775
parent 54811 9db7c0f561a6
permissions -rw-r--r--
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win) Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     1
/*
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
     2
 * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     4
 *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     8
 *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    13
 * accompanied this code).
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    14
 *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3424
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3424
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3424
diff changeset
    21
 * questions.
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    22
 */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    23
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    24
/* @test
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
    25
 * @bug 4527345 7026376 6633549 8233435
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    26
 * @summary Unit test for DatagramChannel's multicast support
45186
387a39577f09 8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents: 30046
diff changeset
    27
 * @library /test/lib
45467
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45186
diff changeset
    28
 * @build jdk.test.lib.NetworkConfiguration
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45186
diff changeset
    29
 *        jdk.test.lib.Platform
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
    30
 *        jdk.test.lib.net.IPSupport
45467
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45186
diff changeset
    31
 *        MulticastSendReceiveTests
3424
8f6c0145dc8f 6867244: Tests missing @run tag
alanb
parents: 1152
diff changeset
    32
 * @run main MulticastSendReceiveTests
13363
065e0e3545a3 6633549: (dc) Include-mode filtering of IPv6 sources does not block datagrams on Linux
alanb
parents: 9679
diff changeset
    33
 * @run main/othervm -Djava.net.preferIPv4Stack=true MulticastSendReceiveTests
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 29746
diff changeset
    34
 * @key randomness
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    35
 */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    36
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    37
import java.nio.ByteBuffer;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    38
import java.nio.channels.*;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    39
import java.net.*;
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
    40
import static java.net.StandardProtocolFamily.*;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    41
import java.util.*;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    42
import java.io.IOException;
45186
387a39577f09 8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents: 30046
diff changeset
    43
import java.util.stream.Collectors;
387a39577f09 8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents: 30046
diff changeset
    44
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
    45
import jdk.test.lib.Platform;
45186
387a39577f09 8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents: 30046
diff changeset
    46
import jdk.test.lib.NetworkConfiguration;
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    47
import jdk.test.lib.net.IPSupport;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    48
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    49
public class MulticastSendReceiveTests {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    50
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
    51
    static final Random rand = new Random();
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
    52
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
    53
    static final ProtocolFamily UNSPEC = new ProtocolFamily() {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
    54
        public String name() {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
    55
            return "UNSPEC";
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
    56
        }
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
    57
    };
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    58
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    59
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    60
     * Send datagram from given local address to given multicast
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    61
     * group.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    62
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    63
    static int sendDatagram(InetAddress local,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    64
                            NetworkInterface nif,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    65
                            InetAddress group,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    66
                            int port)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    67
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    68
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    69
        ProtocolFamily family = (group instanceof Inet6Address) ?
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    70
            StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    71
        DatagramChannel dc = DatagramChannel.open(family)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    72
            .bind(new InetSocketAddress(local, 0))
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9035
diff changeset
    73
            .setOption(StandardSocketOptions.IP_MULTICAST_IF, nif);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    74
        int id = rand.nextInt();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    75
        byte[] msg = Integer.toString(id).getBytes("UTF-8");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    76
        ByteBuffer buf = ByteBuffer.wrap(msg);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    77
        System.out.format("Send message from %s -> group %s (id=0x%x)\n",
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    78
            local.getHostAddress(), group.getHostAddress(), id);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    79
        dc.send(buf, new InetSocketAddress(group, port));
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    80
        dc.close();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    81
        return id;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    82
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    83
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    84
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    85
     * Wait (with timeout) for datagram.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    86
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    87
     * @param   expectedSender - expected sender address, or
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    88
     *                           null if no datagram expected
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    89
     * @param   id - expected id of datagram
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    90
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    91
    static void receiveDatagram(DatagramChannel dc,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    92
                                InetAddress expectedSender,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    93
                                int id)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    94
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    95
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    96
        Selector sel = Selector.open();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    97
        dc.configureBlocking(false);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    98
        dc.register(sel, SelectionKey.OP_READ);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    99
        ByteBuffer buf = ByteBuffer.allocateDirect(100);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   100
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   101
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   102
            for (;;) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   103
                System.out.println("Waiting to receive message");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   104
                sel.select(5*1000);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   105
                SocketAddress sa = dc.receive(buf);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   106
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   107
                // no datagram received
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   108
                if (sa == null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   109
                    if (expectedSender != null) {
29746
f0730dd3de62 8076644: MulticastSendReceiveTests.java fails with NumberFormatException due to network interference
chegar
parents: 14342
diff changeset
   110
                        throw new RuntimeException("Expected message not received");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   111
                    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   112
                    System.out.println("No message received (correct)");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   113
                    return;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   114
                }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   115
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   116
                // datagram received
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   117
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   118
                InetAddress sender = ((InetSocketAddress)sa).getAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   119
                buf.flip();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   120
                byte[] bytes = new byte[buf.remaining()];
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   121
                buf.get(bytes);
29746
f0730dd3de62 8076644: MulticastSendReceiveTests.java fails with NumberFormatException due to network interference
chegar
parents: 14342
diff changeset
   122
                String s = new String(bytes, "UTF-8");
f0730dd3de62 8076644: MulticastSendReceiveTests.java fails with NumberFormatException due to network interference
chegar
parents: 14342
diff changeset
   123
                int receivedId = -1;
f0730dd3de62 8076644: MulticastSendReceiveTests.java fails with NumberFormatException due to network interference
chegar
parents: 14342
diff changeset
   124
                try {
f0730dd3de62 8076644: MulticastSendReceiveTests.java fails with NumberFormatException due to network interference
chegar
parents: 14342
diff changeset
   125
                    receivedId = Integer.parseInt(s);
f0730dd3de62 8076644: MulticastSendReceiveTests.java fails with NumberFormatException due to network interference
chegar
parents: 14342
diff changeset
   126
                    System.out.format("Received message from %s (id=0x%x)\n",
f0730dd3de62 8076644: MulticastSendReceiveTests.java fails with NumberFormatException due to network interference
chegar
parents: 14342
diff changeset
   127
                            sender, receivedId);
f0730dd3de62 8076644: MulticastSendReceiveTests.java fails with NumberFormatException due to network interference
chegar
parents: 14342
diff changeset
   128
                } catch (NumberFormatException x) {
f0730dd3de62 8076644: MulticastSendReceiveTests.java fails with NumberFormatException due to network interference
chegar
parents: 14342
diff changeset
   129
                    System.out.format("Received message from %s (msg=%s)\n", sender, s);
f0730dd3de62 8076644: MulticastSendReceiveTests.java fails with NumberFormatException due to network interference
chegar
parents: 14342
diff changeset
   130
                }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   131
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   132
                if (expectedSender == null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   133
                    if (receivedId == id)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   134
                        throw new RuntimeException("Message not expected");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   135
                    System.out.println("Message ignored (has wrong id)");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   136
                } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   137
                    if (sender.equals(expectedSender)) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   138
                        System.out.println("Message expected");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   139
                        return;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   140
                    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   141
                    System.out.println("Message ignored (wrong sender)");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   142
                }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   143
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   144
                sel.selectedKeys().clear();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   145
                buf.rewind();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   146
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   147
        } finally {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   148
            sel.close();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   149
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   150
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   151
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   153
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   154
     * Exercise multicast send/receive on given group/interface
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   155
     */
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   156
    static void test(ProtocolFamily family,
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   157
                     NetworkInterface nif,
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   158
                     InetAddress group,
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   159
                     InetAddress source)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   160
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   161
    {
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   162
        System.out.format("\nTest DatagramChannel to %s socket\n", family.name());
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   163
        try (DatagramChannel dc = (family == UNSPEC) ?
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   164
                DatagramChannel.open() : DatagramChannel.open(family)) {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9035
diff changeset
   165
            dc.setOption(StandardSocketOptions.SO_REUSEADDR, true)
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   166
              .bind(new InetSocketAddress(0));
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   167
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   168
            // join group
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   169
            System.out.format("join %s @ %s\n", group.getHostAddress(),
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   170
                nif.getName());
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   171
            MembershipKey key;
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   172
            try {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   173
                key = dc.join(group, nif);
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   174
            } catch (IllegalArgumentException iae) {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   175
                if (family == UNSPEC) {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   176
                    System.out.println("Not supported");
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   177
                    return;
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   178
                }
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   179
                throw iae;
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   180
            }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   181
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   182
            // send message to group
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   183
            int port = ((InetSocketAddress)dc.getLocalAddress()).getPort();
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   184
            int id = sendDatagram(source, nif, group, port);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   185
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   186
            // receive message and check id matches
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   187
            receiveDatagram(dc, source, id);
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   188
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   189
            // exclude-mode filtering
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   190
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   191
            try {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   192
                System.out.format("block %s\n", source.getHostAddress());
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   193
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   194
                // may throw UOE
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   195
                key.block(source);
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   196
                id = sendDatagram(source, nif, group, port);
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   197
                receiveDatagram(dc, null, id);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   198
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   199
                // unblock source, send message, message should be received
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   200
                System.out.format("unblock %s\n", source.getHostAddress());
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   201
                key.unblock(source);
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   202
                id = sendDatagram(source, nif, group, port);
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   203
                receiveDatagram(dc, source, id);
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   204
            } catch (UnsupportedOperationException x) {
13363
065e0e3545a3 6633549: (dc) Include-mode filtering of IPv6 sources does not block datagrams on Linux
alanb
parents: 9679
diff changeset
   205
                String os = System.getProperty("os.name");
065e0e3545a3 6633549: (dc) Include-mode filtering of IPv6 sources does not block datagrams on Linux
alanb
parents: 9679
diff changeset
   206
                // Exclude-mode filtering supported on these platforms so UOE should never be thrown
065e0e3545a3 6633549: (dc) Include-mode filtering of IPv6 sources does not block datagrams on Linux
alanb
parents: 9679
diff changeset
   207
                if (os.equals("SunOS") || os.equals("Linux"))
065e0e3545a3 6633549: (dc) Include-mode filtering of IPv6 sources does not block datagrams on Linux
alanb
parents: 9679
diff changeset
   208
                    throw x;
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   209
                System.out.println("Exclude-mode filtering not supported!");
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   210
            }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   211
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   212
            key.drop();
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   213
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   214
            // include-mode filtering
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   215
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   216
            InetAddress bogus = (group instanceof Inet6Address) ?
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   217
                InetAddress.getByName("fe80::1234") :
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   218
                InetAddress.getByName("1.2.3.4");
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   219
            System.out.format("join %s @ %s only-source %s\n", group.getHostAddress(),
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   220
                nif.getName(), bogus.getHostAddress());
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   221
            try {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   222
                // may throw UOE
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   223
                key = dc.join(group, nif, bogus);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   224
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   225
                id = sendDatagram(source, nif, group, port);
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   226
                receiveDatagram(dc, null, id);
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   227
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   228
                System.out.format("join %s @ %s only-source %s\n", group.getHostAddress(),
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   229
                    nif.getName(), source.getHostAddress());
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   230
                key = dc.join(group, nif, source);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   231
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   232
                id = sendDatagram(source, nif, group, port);
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   233
                receiveDatagram(dc, source, id);
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   234
            } catch (UnsupportedOperationException x) {
13363
065e0e3545a3 6633549: (dc) Include-mode filtering of IPv6 sources does not block datagrams on Linux
alanb
parents: 9679
diff changeset
   235
                String os = System.getProperty("os.name");
065e0e3545a3 6633549: (dc) Include-mode filtering of IPv6 sources does not block datagrams on Linux
alanb
parents: 9679
diff changeset
   236
                // Include-mode filtering supported on these platforms so UOE should never be thrown
065e0e3545a3 6633549: (dc) Include-mode filtering of IPv6 sources does not block datagrams on Linux
alanb
parents: 9679
diff changeset
   237
                if (os.equals("SunOS") || os.equals("Linux"))
065e0e3545a3 6633549: (dc) Include-mode filtering of IPv6 sources does not block datagrams on Linux
alanb
parents: 9679
diff changeset
   238
                    throw x;
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   239
                System.out.println("Include-mode filtering not supported!");
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   240
            }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   241
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   242
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   243
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   244
    public static void main(String[] args) throws IOException {
54811
9db7c0f561a6 8223652: Rename IPSupport.skipIfCurrentConfigurationIsInvalid() to IPSupport.throwSkippedExceptionIfNonOperational()
aeubanks
parents: 54770
diff changeset
   245
        IPSupport.throwSkippedExceptionIfNonOperational();
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
   246
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   247
        // IPv4 and IPv6 interfaces that support multicasting
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   248
        NetworkConfiguration config = NetworkConfiguration.probe();
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   249
        List<NetworkInterface> ip4MulticastInterfaces = config.ip4MulticastInterfaces()
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   250
                .collect(Collectors.toList());
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   251
        List<NetworkInterface> ip6MulticastInterfaces = config.ip6MulticastInterfaces()
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   252
                .collect(Collectors.toList());
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   253
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   254
        // multicast groups used for the test
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   255
        InetAddress ip4Group = InetAddress.getByName("225.4.5.6");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   256
        InetAddress ip6Group = InetAddress.getByName("ff02::a");
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   257
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   258
        // Platforms that allow dual sockets join IPv4 multicast groups
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   259
        boolean canIPv6JoinIPv4Group =
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   260
                Platform.isLinux() ||
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   261
                Platform.isOSX() ||
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   262
                Platform.isSolaris() ||
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   263
                Platform.isWindows();
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   264
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   265
        for (NetworkInterface nif : ip4MulticastInterfaces) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   266
            InetAddress source = config.ip4Addresses(nif).iterator().next();
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   267
            test(UNSPEC, nif, ip4Group, source);
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   268
            test(INET,   nif, ip4Group, source);
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   269
            if (IPSupport.hasIPv6() && canIPv6JoinIPv4Group) {
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   270
                test(INET6,  nif, ip4Group, source);
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   271
            }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   272
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   273
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   274
        for (NetworkInterface nif : ip6MulticastInterfaces) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   275
            InetAddress source = config.ip6Addresses(nif).iterator().next();
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 54811
diff changeset
   276
            test(UNSPEC, nif, ip6Group, source);
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 5506
diff changeset
   277
            test(INET6,  nif, ip6Group, source);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   278
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   279
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   280
}