test/jdk/java/net/MulticastSocket/NoLoopbackPackets.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 40534 jdk/test/java/net/MulticastSocket/NoLoopbackPackets.java@bc3e5bbcb65e
child 54770 62b6e7587b1f
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
40534
bc3e5bbcb65e 8164592: java/net/MulticastSocket/NoLoopbackPackets.java tests may leave a daemon thread
asmotrak
parents: 7668
diff changeset
     2
 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5143
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5143
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5143
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4742177
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class NoLoopbackPackets {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    private static String osname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    static boolean isWindows() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        if (osname == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
            osname = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        return osname.contains("Windows");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static boolean hasIPv6() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        List<NetworkInterface> nics = Collections.list(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                                        NetworkInterface.getNetworkInterfaces());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        for (NetworkInterface nic : nics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            if (!nic.isLoopback()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                for (InetAddress addr : addrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                    if (addr instanceof Inet6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (isWindows()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            System.out.println("The test only run on non-Windows OS. Bye.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (!hasIPv6()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            System.out.println("No IPv6 available. Bye.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
5143
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    69
        MulticastSocket msock = null;
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    70
        List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();
40534
bc3e5bbcb65e 8164592: java/net/MulticastSocket/NoLoopbackPackets.java tests may leave a daemon thread
asmotrak
parents: 7668
diff changeset
    71
        Sender sender = null;
5143
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    72
        try {
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    73
            msock = new MulticastSocket();
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    74
            int port = msock.getLocalPort();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
5143
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    76
            // we will send packets to three multicast groups :-
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    77
            // 224.1.1.1, ::ffff:224.1.1.2, and ff02::1:1
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    78
            //
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    79
            List<SocketAddress> groups = new ArrayList<SocketAddress>();
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    80
            groups.add(new InetSocketAddress(InetAddress.getByName("224.1.1.1"), port));
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    81
            groups.add(new InetSocketAddress(InetAddress.getByName("::ffff:224.1.1.2"), port));
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    82
            groups.add(new InetSocketAddress(InetAddress.getByName("ff02::1:1"), port));
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    83
40534
bc3e5bbcb65e 8164592: java/net/MulticastSocket/NoLoopbackPackets.java tests may leave a daemon thread
asmotrak
parents: 7668
diff changeset
    84
            sender = new Sender(groups);
bc3e5bbcb65e 8164592: java/net/MulticastSocket/NoLoopbackPackets.java tests may leave a daemon thread
asmotrak
parents: 7668
diff changeset
    85
            new Thread(sender).start();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
5143
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    87
            // Now try to receive multicast packets. we should not see any of them
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    88
            // since we disable loopback mode.
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    89
            //
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    90
            msock.setSoTimeout(5000);       // 5 seconds
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    91
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    92
            byte[] buf = new byte[1024];
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    93
            DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    94
            for (SocketAddress group : groups) {
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    95
                msock.joinGroup(group, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
5143
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    97
                try {
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
    98
                    msock.receive(packet);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
5143
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   100
                    // it is an error if we receive something
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   101
                    failedGroups.add(group);
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   102
                } catch (SocketTimeoutException e) {
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   103
                    // we expect this
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   104
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
5143
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   106
                msock.leaveGroup(group, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
5143
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   108
        } finally {
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   109
            if (msock != null) try { msock.close(); } catch (Exception e) {}
40534
bc3e5bbcb65e 8164592: java/net/MulticastSocket/NoLoopbackPackets.java tests may leave a daemon thread
asmotrak
parents: 7668
diff changeset
   110
            if (sender != null) {
bc3e5bbcb65e 8164592: java/net/MulticastSocket/NoLoopbackPackets.java tests may leave a daemon thread
asmotrak
parents: 7668
diff changeset
   111
                sender.stop();
bc3e5bbcb65e 8164592: java/net/MulticastSocket/NoLoopbackPackets.java tests may leave a daemon thread
asmotrak
parents: 7668
diff changeset
   112
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (failedGroups.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.out.println("We should not receive anything from following groups, but we did:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            for (SocketAddress group : failedGroups)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                System.out.println(group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            throw new RuntimeException("test failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
5143
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   122
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   123
    static class Sender implements Runnable {
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   124
        private List<SocketAddress> sendToGroups;
40534
bc3e5bbcb65e 8164592: java/net/MulticastSocket/NoLoopbackPackets.java tests may leave a daemon thread
asmotrak
parents: 7668
diff changeset
   125
        private volatile boolean stop;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
5143
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   127
        public Sender(List<SocketAddress> groups) {
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   128
            sendToGroups = groups;
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   129
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
5143
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   131
        public void run() {
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   132
            byte[] buf = "hello world".getBytes();
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   133
            List<DatagramPacket> packets = new ArrayList<DatagramPacket>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
5143
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   135
            try {
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   136
                for (SocketAddress group : sendToGroups) {
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   137
                    DatagramPacket packet = new DatagramPacket(buf, buf.length, group);
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   138
                    packets.add(packet);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
5143
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   141
                MulticastSocket msock = new MulticastSocket();
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   142
                msock.setLoopbackMode(true);    // disable loopback mode
40534
bc3e5bbcb65e 8164592: java/net/MulticastSocket/NoLoopbackPackets.java tests may leave a daemon thread
asmotrak
parents: 7668
diff changeset
   143
                while (!stop) {
5143
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   144
                    for (DatagramPacket packet : packets) {
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   145
                        msock.send(packet);
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   146
                    }
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   147
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   148
                    Thread.sleep(1000);     // 1 second
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   149
                }
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   150
            } catch (Exception e) {
a1682a41e70f 6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents: 2
diff changeset
   151
                throw new RuntimeException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
40534
bc3e5bbcb65e 8164592: java/net/MulticastSocket/NoLoopbackPackets.java tests may leave a daemon thread
asmotrak
parents: 7668
diff changeset
   154
bc3e5bbcb65e 8164592: java/net/MulticastSocket/NoLoopbackPackets.java tests may leave a daemon thread
asmotrak
parents: 7668
diff changeset
   155
        void stop() {
bc3e5bbcb65e 8164592: java/net/MulticastSocket/NoLoopbackPackets.java tests may leave a daemon thread
asmotrak
parents: 7668
diff changeset
   156
            stop = true;
bc3e5bbcb65e 8164592: java/net/MulticastSocket/NoLoopbackPackets.java tests may leave a daemon thread
asmotrak
parents: 7668
diff changeset
   157
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}