jdk/test/java/net/MulticastSocket/Test.java
author jjg
Wed, 04 Jan 2017 18:33:20 -0800
changeset 43029 1cd1c816581e
parent 5506 202f599c92aa
child 44927 d554736d963e
permissions -rw-r--r--
8172260: remove tests from ProblemList Reviewed-by: rfield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, 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: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 4488458
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary IPv4 and IPv6 multicasting broken on Linux
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    static int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    void doTest(String address) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        InetAddress ia = InetAddress.getByName(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        System.out.println("**********************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        System.out.println("Test " + count + ": " + ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        MulticastSocket mc = new MulticastSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        int port = mc.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        DatagramSocket s1 = new DatagramSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        byte msg[] = "Hello".getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        DatagramPacket p = new DatagramPacket(msg, msg.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        mc.setSoTimeout(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            for (int i=0; i<2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                System.out.println("Join: " + ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                mc.joinGroup(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                /* packets should be received */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                for (int j=0; j<2; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    p.setAddress(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    p.setPort(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    System.out.println("Send packet to: " + ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    s1.send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                        mc.receive(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        System.out.println("Got packet! - Good.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    } catch (SocketTimeoutException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                        failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                        System.out.println("Failed: No packet received within timeout!!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                System.out.println("Leave: " + ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                mc.leaveGroup(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                 * If there are multiple interface we might be a couple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                 * copies still in our queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        mc.receive(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                } catch (SocketTimeoutException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                /* packets should not be received */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                p.setAddress(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                p.setPort(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                s1.send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    mc.receive(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    System.out.println("Failed: Got packet after leaving group!!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                } catch (SocketTimeoutException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    System.out.println("No packet received within timeout! - Good.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            System.out.println("Failed: Unexpected exception thrown: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        mc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        s1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.out.println("Test failed!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    void allTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         * Assume machine has IPv4 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        doTest("224.80.80.80");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         * Check if IPv6 is enabled and the scope of the addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        boolean has_ipv6 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        boolean has_siteaddress = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        boolean has_linklocaladdress = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        boolean has_globaladdress = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Enumeration nifs = NetworkInterface.getNetworkInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        while (nifs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            NetworkInterface ni = (NetworkInterface)nifs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            Enumeration addrs = ni.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            while (addrs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                InetAddress ia = (InetAddress)addrs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                if (ia instanceof Inet6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    has_ipv6 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    if (ia.isLinkLocalAddress()) has_linklocaladdress = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    if (ia.isSiteLocalAddress()) has_siteaddress = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    if (!ia.isLinkLocalAddress() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        !ia.isSiteLocalAddress() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        !ia.isLoopbackAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        has_globaladdress = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         * If IPv6 is enabled perform multicast tests with various scopes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (has_ipv6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            doTest("ff01::a");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (has_linklocaladdress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            doTest("ff02::a");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (has_siteaddress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            doTest("ff05::a");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (has_globaladdress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            doTest("ff0e::a");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        Test t = new Test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (args.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            t.allTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            for (int i=0; i<args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                t.doTest(args[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        System.out.println("**********************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        System.out.println(count + " test(s) executed. " + failures +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                           " test(s) failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (failures > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            throw new Exception("Test failed - see log file for details");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
}