test/jdk/java/nio/channels/DatagramChannel/Promiscuous.java
author aeubanks
Tue, 16 Apr 2019 13:06:23 -0700
changeset 54770 62b6e7587b1f
parent 47216 71c04702a3d5
child 54811 9db7c0f561a6
permissions -rw-r--r--
8220673: Add test library support for determining platform IP support Reviewed-by: dfuchs, chegar Contributed-by: aeubanks@google.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18535
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
     1
/*
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
     4
 *
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
     8
 *
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    13
 * accompanied this code).
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    14
 *
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    18
 *
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    21
 * questions.
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    22
 *
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    23
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    24
/* @test
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    25
 * @bug 8014377
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    26
 * @summary Test for interference when two sockets are bound to the same
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    27
 *   port but joined to different multicast groups
45186
387a39577f09 8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents: 30435
diff changeset
    28
 * @library /test/lib
45467
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45186
diff changeset
    29
 * @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
    30
 *        jdk.test.lib.Platform
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45186
diff changeset
    31
 *        Promiscuous
18535
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    32
 * @run main Promiscuous
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    33
 * @run main/othervm -Djava.net.preferIPv4Stack=true Promiscuous
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 18535
diff changeset
    34
 * @key randomness
18535
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    35
 */
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    36
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    37
import java.nio.ByteBuffer;
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    38
import java.nio.channels.*;
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    39
import java.net.*;
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    40
import static java.net.StandardProtocolFamily.*;
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    41
import java.util.*;
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    42
import java.io.IOException;
45186
387a39577f09 8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents: 30435
diff changeset
    43
import java.util.stream.Collectors;
387a39577f09 8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents: 30435
diff changeset
    44
387a39577f09 8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents: 30435
diff changeset
    45
import jdk.test.lib.NetworkConfiguration;
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    46
import jdk.test.lib.net.IPSupport;
18535
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    47
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    48
public class Promiscuous {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    49
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    50
    static final Random rand = new Random();
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    51
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    52
    static final ProtocolFamily UNSPEC = new ProtocolFamily() {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    53
        public String name() {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    54
            return "UNSPEC";
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    55
        }
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    56
    };
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    57
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    58
    /**
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    59
     * Sends a datagram to the given multicast group
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    60
     */
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    61
    static int sendDatagram(NetworkInterface nif,
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    62
                            InetAddress group,
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    63
                            int port)
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    64
        throws IOException
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    65
    {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    66
        ProtocolFamily family = (group instanceof Inet6Address) ?
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    67
            StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    68
        DatagramChannel dc = DatagramChannel.open(family)
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    69
            .setOption(StandardSocketOptions.IP_MULTICAST_IF, nif);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    70
        int id = rand.nextInt();
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    71
        byte[] msg = Integer.toString(id).getBytes("UTF-8");
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    72
        ByteBuffer buf = ByteBuffer.wrap(msg);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    73
        System.out.format("Send message -> group %s (id=0x%x)\n",
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    74
            group.getHostAddress(), id);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    75
        dc.send(buf, new InetSocketAddress(group, port));
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    76
        dc.close();
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    77
        return id;
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    78
    }
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    79
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    80
    /**
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    81
     * Wait (with timeout) for datagram. The {@code datagramExepcted}
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    82
     * parameter indicates whether a datagram is expected, and if
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    83
     * {@true} then {@code id} is the identifier in the payload.
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    84
     */
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    85
    static void receiveDatagram(DatagramChannel dc,
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    86
                                String name,
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    87
                                boolean datagramExepcted,
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    88
                                int id)
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    89
        throws IOException
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    90
    {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    91
        System.out.println("Checking if received by " + name);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    92
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    93
        Selector sel = Selector.open();
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    94
        dc.configureBlocking(false);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    95
        dc.register(sel, SelectionKey.OP_READ);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    96
        ByteBuffer buf = ByteBuffer.allocateDirect(100);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    97
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    98
        try {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
    99
            for (;;) {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   100
                System.out.println("Waiting to receive message");
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   101
                sel.select(5*1000);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   102
                SocketAddress sa = dc.receive(buf);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   103
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   104
                // no datagram received
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   105
                if (sa == null) {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   106
                    if (datagramExepcted) {
30435
f9d75cf4dab8 8079651: (dc) Promiscuous.java fails with NumberFormatException due to network interference
alanb
parents: 30046
diff changeset
   107
                        throw new RuntimeException("Expected message not received");
18535
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   108
                    }
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   109
                    System.out.println("No message received (correct)");
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   110
                    return;
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   111
                }
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   112
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   113
                // datagram received
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   114
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   115
                InetAddress sender = ((InetSocketAddress)sa).getAddress();
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   116
                buf.flip();
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   117
                byte[] bytes = new byte[buf.remaining()];
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   118
                buf.get(bytes);
30435
f9d75cf4dab8 8079651: (dc) Promiscuous.java fails with NumberFormatException due to network interference
alanb
parents: 30046
diff changeset
   119
                String s = new String(bytes, "UTF-8");
f9d75cf4dab8 8079651: (dc) Promiscuous.java fails with NumberFormatException due to network interference
alanb
parents: 30046
diff changeset
   120
                int receivedId = -1;
f9d75cf4dab8 8079651: (dc) Promiscuous.java fails with NumberFormatException due to network interference
alanb
parents: 30046
diff changeset
   121
                try {
f9d75cf4dab8 8079651: (dc) Promiscuous.java fails with NumberFormatException due to network interference
alanb
parents: 30046
diff changeset
   122
                    receivedId = Integer.parseInt(s);
f9d75cf4dab8 8079651: (dc) Promiscuous.java fails with NumberFormatException due to network interference
alanb
parents: 30046
diff changeset
   123
                    System.out.format("Received message from %s (id=0x%x)\n",
f9d75cf4dab8 8079651: (dc) Promiscuous.java fails with NumberFormatException due to network interference
alanb
parents: 30046
diff changeset
   124
                            sender, receivedId);
f9d75cf4dab8 8079651: (dc) Promiscuous.java fails with NumberFormatException due to network interference
alanb
parents: 30046
diff changeset
   125
                } catch (NumberFormatException x) {
f9d75cf4dab8 8079651: (dc) Promiscuous.java fails with NumberFormatException due to network interference
alanb
parents: 30046
diff changeset
   126
                    System.out.format("Received message from %s (msg=%s)\n", sender, s);
f9d75cf4dab8 8079651: (dc) Promiscuous.java fails with NumberFormatException due to network interference
alanb
parents: 30046
diff changeset
   127
                }
18535
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   128
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   129
                if (!datagramExepcted) {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   130
                    if (receivedId == id)
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   131
                        throw new RuntimeException("Message not expected");
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   132
                    System.out.println("Message ignored (has wrong id)");
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   133
                } else {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   134
                    if (receivedId == id) {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   135
                        System.out.println("Message expected");
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   136
                        return;
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   137
                    }
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   138
                    System.out.println("Message ignored (wrong sender)");
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   139
                }
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   140
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   141
                sel.selectedKeys().clear();
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   142
                buf.rewind();
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   143
            }
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   144
        } finally {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   145
            sel.close();
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   146
        }
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   147
    }
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   148
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   149
    static void test(ProtocolFamily family,
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   150
                     NetworkInterface nif,
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   151
                     InetAddress group1,
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   152
                     InetAddress group2)
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   153
        throws IOException
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   154
    {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   155
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   156
        System.out.format("%nTest family=%s%n", family.name());
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   157
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   158
        DatagramChannel dc1 = (family == UNSPEC) ?
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   159
            DatagramChannel.open() : DatagramChannel.open(family);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   160
        DatagramChannel dc2 = (family == UNSPEC) ?
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   161
            DatagramChannel.open() : DatagramChannel.open(family);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   162
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   163
        try {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   164
            dc1.setOption(StandardSocketOptions.SO_REUSEADDR, true);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   165
            dc2.setOption(StandardSocketOptions.SO_REUSEADDR, true);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   166
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   167
            dc1.bind(new InetSocketAddress(0));
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   168
            int port = dc1.socket().getLocalPort();
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   169
            dc2.bind(new InetSocketAddress(port));
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   170
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   171
            System.out.format("dc1 joining [%s]:%d @ %s\n",
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   172
                group1.getHostAddress(), port, nif.getName());
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   173
            System.out.format("dc2 joining [%s]:%d @ %s\n",
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   174
                group2.getHostAddress(), port, nif.getName());
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   175
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   176
            dc1.join(group1, nif);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   177
            dc2.join(group2, nif);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   178
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   179
            int id = sendDatagram(nif, group1, port);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   180
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   181
            receiveDatagram(dc1, "dc1", true, id);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   182
            receiveDatagram(dc2, "dc2", false, id);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   183
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   184
            id = sendDatagram(nif, group2, port);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   185
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   186
            receiveDatagram(dc1, "dc1", false, id);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   187
            receiveDatagram(dc2, "dc2", true, id);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   188
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   189
        } finally {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   190
            dc1.close();
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   191
            dc2.close();
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   192
        }
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   193
    }
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   194
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   195
    public static void main(String[] args) throws IOException {
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
   196
        IPSupport.skipIfCurrentConfigurationIsInvalid();
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
   197
18535
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   198
        String os = System.getProperty("os.name");
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   199
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   200
        // Requires IP_MULTICAST_ALL on Linux (new since 2.6.31) so skip
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   201
        // on older kernels. Note that we skip on <= version 3 to keep the
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   202
        // parsing simple
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   203
        if (os.equals("Linux")) {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   204
            String osversion = System.getProperty("os.version");
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   205
            String[] vers = osversion.split("\\.", 0);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   206
            int major = Integer.parseInt(vers[0]);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   207
            if (major < 3) {
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   208
                System.out.format("Kernel version is %s, test skipped%n", osversion);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   209
                return;
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   210
            }
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   211
        }
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   212
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   213
        // get local network configuration to use
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   214
        NetworkConfiguration config = NetworkConfiguration.probe();
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   215
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   216
        // multicast groups used for the test
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   217
        InetAddress ip4Group1 = InetAddress.getByName("225.4.5.6");
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   218
        InetAddress ip4Group2 = InetAddress.getByName("225.4.6.6");
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   219
45186
387a39577f09 8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents: 30435
diff changeset
   220
        for (NetworkInterface nif: config.ip4MulticastInterfaces()
387a39577f09 8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents: 30435
diff changeset
   221
                                         .collect(Collectors.toList())) {
18535
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   222
            InetAddress source = config.ip4Addresses(nif).iterator().next();
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   223
            test(INET, nif, ip4Group1, ip4Group2);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   224
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   225
            // Solaris and Linux allow IPv6 sockets join IPv4 multicast groups
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   226
            if (os.equals("SunOS") || os.equals("Linux"))
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   227
                test(UNSPEC, nif, ip4Group1, ip4Group2);
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   228
        }
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   229
    }
71b74ae0ab8a 8014377: (dc) DatagramChannel should set IP_MULTICAST_ALL=0 (lnx)
alanb
parents:
diff changeset
   230
}