1 /*
   2  * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4527345
  26  * @summary Unit test for DatagramChannel's multicast support
  27  * @library /test/lib
  28  * @build BasicMulticastTests
  29  * @run main BasicMulticastTests
  30  */
  31 
  32 import java.nio.ByteBuffer;
  33 import java.nio.channels.*;
  34 import java.net.*;
  35 import java.util.*;
  36 import java.io.IOException;
  37 
  38 import jdk.test.lib.NetworkConfiguration;
  39 
  40 public class BasicMulticastTests {
  41 
  42     /**
  43      * Tests that existing membership key is returned by join methods and that
  44      * membership key methods return the expected results
  45      */
  46     static void membershipKeyTests(NetworkInterface nif,
  47                                    InetAddress group,
  48                                    InetAddress source)
  49         throws IOException
  50     {
  51         System.out.format("MembershipKey test using %s @ %s\n",
  52             group.getHostAddress(), nif.getName());
  53 
  54         ProtocolFamily family = (group instanceof Inet4Address) ?
  55             StandardProtocolFamily.INET : StandardProtocolFamily.INET6;
  56 
  57         DatagramChannel dc = DatagramChannel.open(family)
  58             .setOption(StandardSocketOptions.SO_REUSEADDR, true)
  59             .bind(new InetSocketAddress(source, 0));
  60 
  61         // check existing key is returned
  62         MembershipKey key = dc.join(group, nif);
  63         MembershipKey other = dc.join(group, nif);
  64         if (other != key) {
  65             throw new RuntimeException("existing key not returned");
  66         }
  67 
  68         // check key
  69         if (!key.isValid())
  70             throw new RuntimeException("key is not valid");
  71         if (!key.group().equals(group))
  72             throw new RuntimeException("group is incorrect");
  73         if (!key.networkInterface().equals(nif))
  74             throw new RuntimeException("network interface is incorrect");
  75         if (key.sourceAddress() != null)
  76             throw new RuntimeException("key is source specific");
  77 
  78         // drop membership
  79         key.drop();
  80         if (key.isValid()) {
  81             throw new RuntimeException("key is still valid");
  82         }
  83 
  84         // source-specific
  85         try {
  86             key = dc.join(group, nif, source);
  87             other = dc.join(group, nif, source);
  88             if (other != key) {
  89                 throw new RuntimeException("existing key not returned");
  90             }
  91             if (!key.isValid())
  92                 throw new RuntimeException("key is not valid");
  93             if (!key.group().equals(group))
  94                 throw new RuntimeException("group is incorrect");
  95             if (!key.networkInterface().equals(nif))
  96                 throw new RuntimeException("network interface is incorrect");
  97             if (!key.sourceAddress().equals(source))
  98                 throw new RuntimeException("key's source address incorrect");
  99 
 100             // drop membership
 101             key.drop();
 102             if (key.isValid()) {
 103                 throw new RuntimeException("key is still valid");
 104             }
 105         } catch (UnsupportedOperationException x) {
 106         }
 107 
 108         // done
 109         dc.close();
 110     }
 111 
 112     /**
 113      * Tests exceptions for invalid arguments or scenarios
 114      */
 115     static void exceptionTests(NetworkInterface nif)
 116         throws IOException
 117     {
 118         System.out.println("Exception Tests");
 119 
 120         DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET)
 121             .setOption(StandardSocketOptions.SO_REUSEADDR, true)
 122             .bind(new InetSocketAddress(0));
 123 
 124         InetAddress group = InetAddress.getByName("225.4.5.6");
 125         InetAddress notGroup = InetAddress.getByName("1.2.3.4");
 126         InetAddress thisHost = InetAddress.getLocalHost();
 127 
 128         // IllegalStateException
 129         MembershipKey key;
 130         key = dc.join(group, nif);
 131         try {
 132             dc.join(group, nif, thisHost);
 133             throw new RuntimeException("IllegalStateException not thrown");
 134         } catch (IllegalStateException x) {
 135         } catch (UnsupportedOperationException x) {
 136         }
 137         key.drop();
 138         try {
 139             key = dc.join(group, nif, thisHost);
 140             try {
 141                 dc.join(group, nif);
 142                 throw new RuntimeException("IllegalStateException not thrown");
 143             } catch (IllegalStateException x) {
 144             }
 145             key.drop();
 146         } catch (UnsupportedOperationException x) {
 147         }
 148 
 149         // IllegalArgumentException
 150         try {
 151             dc.join(notGroup, nif);
 152             throw new RuntimeException("IllegalArgumentException not thrown");
 153         } catch (IllegalArgumentException x) {
 154         }
 155         try {
 156             dc.join(notGroup, nif, thisHost);
 157             throw new RuntimeException("IllegalArgumentException not thrown");
 158         } catch (IllegalArgumentException x) {
 159         } catch (UnsupportedOperationException x) {
 160         }
 161 
 162         // NullPointerException
 163         try {
 164             dc.join(null, nif);
 165             throw new RuntimeException("NullPointerException not thrown");
 166         } catch (NullPointerException x) {
 167         }
 168         try {
 169             dc.join(group, null);
 170             throw new RuntimeException("NullPointerException not thrown");
 171         } catch (NullPointerException x) {
 172         }
 173         try {
 174             dc.join(group, nif, null);
 175             throw new RuntimeException("NullPointerException not thrown");
 176         } catch (NullPointerException x) {
 177         } catch (UnsupportedOperationException x) {
 178         }
 179 
 180         dc.close();
 181 
 182         // ClosedChannelException
 183         try {
 184             dc.join(group, nif);
 185             throw new RuntimeException("ClosedChannelException not thrown");
 186         } catch (ClosedChannelException x) {
 187         }
 188         try {
 189             dc.join(group, nif, thisHost);
 190             throw new RuntimeException("ClosedChannelException not thrown");
 191         } catch (ClosedChannelException x) {
 192         } catch (UnsupportedOperationException x) {
 193         }
 194     }
 195 
 196 
 197     /**
 198      * Probe interfaces to get interfaces that support IPv4 or IPv6 multicasting
 199      * and invoke tests.
 200      */
 201     public static void main(String[] args) throws IOException {
 202 
 203         // multicast groups used for the test
 204         InetAddress ip4Group = InetAddress.getByName("225.4.5.6");
 205         InetAddress ip6Group = InetAddress.getByName("ff02::a");
 206 
 207 
 208         NetworkConfiguration config = NetworkConfiguration.probe();
 209 
 210         NetworkInterface nif = config.ip4MulticastInterfaces().iterator().next();
 211         InetAddress anySource = config.ip4Addresses(nif).iterator().next();
 212         membershipKeyTests(nif, ip4Group, anySource);
 213         exceptionTests(nif);
 214 
 215         // re-run the membership key tests with IPv6 if available
 216 
 217         Iterator<NetworkInterface> iter = config.ip6MulticastInterfaces().iterator();
 218         if (iter.hasNext()) {
 219             nif = iter.next();
 220             anySource = config.ip6Addresses(nif).iterator().next();
 221             membershipKeyTests(nif, ip6Group, anySource);
 222         }
 223     }
 224 }