test/jdk/java/net/NetworkInterface/NoSetNetworkInterface.java
changeset 59313 6f12009ea9d7
equal deleted inserted replaced
59312:43eee1237934 59313:6f12009ea9d7
       
     1 /*
       
     2  * Copyright (c) 2019, 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 /*
       
    25  * @test
       
    26  * @bug 8233307
       
    27  * @library /test/lib
       
    28  * @run main/othervm NoSetNetworkInterface
       
    29  * @run main/othervm -Djava.net.preferIPv4Stack=true NoSetNetworkInterface
       
    30  * @run main/othervm -Djava.net.preferIPv6Addresses=true NoSetNetworkInterface
       
    31  * @summary Check that methods that are used to set and get the NetworkInterface
       
    32  *  for a MulticastSocket work as expected. This test also checks that getOption
       
    33  *  returns null correctly when a NetworkInterface has not been set
       
    34  */
       
    35 
       
    36 import jdk.test.lib.NetworkConfiguration;
       
    37 
       
    38 import java.io.IOException;
       
    39 import java.io.UncheckedIOException;
       
    40 import java.net.InetAddress;
       
    41 import java.net.MulticastSocket;
       
    42 import java.net.NetworkInterface;
       
    43 import java.net.StandardSocketOptions;
       
    44 import java.util.Optional;
       
    45 import java.util.function.Predicate;
       
    46 
       
    47 public class NoSetNetworkInterface {
       
    48     public static void main(String[] args) throws Exception {
       
    49 
       
    50         NetworkConfiguration nc = NetworkConfiguration.probe();
       
    51 
       
    52         // check set and get methods work as expected
       
    53         nc.multicastInterfaces(true).forEach(ni -> {
       
    54             checkSetInterface(ni);
       
    55             checkSetNetworkInterface(ni);
       
    56             checkSetOption(ni);
       
    57         });
       
    58 
       
    59         // Check that dummy NetworkInterface is returned when not set
       
    60         checkDummyNetworkInterface();
       
    61     }
       
    62 
       
    63     public static void checkSetInterface(NetworkInterface ni) {
       
    64         try (MulticastSocket ms = new MulticastSocket()) {
       
    65             Optional<InetAddress> iAddr = ni.inetAddresses()
       
    66                     .filter(Predicate.not(InetAddress::isAnyLocalAddress))
       
    67                     .findFirst();
       
    68             if (iAddr.isPresent()) {
       
    69                 ms.setInterface(iAddr.get());
       
    70                 checkForCorrectNetworkInterface("setInterface", ms, ni);
       
    71             }
       
    72         } catch (IOException e) {
       
    73             throw new UncheckedIOException(e);
       
    74         }
       
    75     }
       
    76 
       
    77     public static void checkSetNetworkInterface(NetworkInterface ni) {
       
    78         try (MulticastSocket ms = new MulticastSocket()) {
       
    79             ms.setNetworkInterface(ni);
       
    80             checkForCorrectNetworkInterface("setNetworkInterface", ms, ni);
       
    81         } catch (IOException e) {
       
    82             throw new UncheckedIOException(e);
       
    83         }
       
    84     }
       
    85 
       
    86     public static void checkSetOption(NetworkInterface ni) {
       
    87         try (MulticastSocket ms = new MulticastSocket()) {
       
    88             ms.setOption(StandardSocketOptions.IP_MULTICAST_IF, ni);
       
    89             checkForCorrectNetworkInterface("setOption", ms, ni);
       
    90         } catch (IOException e) {
       
    91             throw new UncheckedIOException(e);
       
    92         }
       
    93     }
       
    94 
       
    95     public static void checkForCorrectNetworkInterface(String setterMethod,
       
    96                                                        MulticastSocket ms,
       
    97                                                        NetworkInterface ni) throws IOException {
       
    98 
       
    99         // getInterface
       
   100         InetAddress testAddr = ms.getInterface();
       
   101         if (!ni.inetAddresses().anyMatch(i -> i.equals(testAddr))) {
       
   102             throw new RuntimeException(setterMethod + " != getInterface");
       
   103         }
       
   104 
       
   105         // getNetworkInterface
       
   106         if (!ni.equals(ms.getNetworkInterface())) {
       
   107             throw new RuntimeException(setterMethod + " != getNetworkInterface");
       
   108         }
       
   109 
       
   110         // getOption
       
   111         if (!ni.equals(ms.getOption(StandardSocketOptions.IP_MULTICAST_IF))) {
       
   112             throw new RuntimeException(setterMethod + " != getOption");
       
   113         }
       
   114     }
       
   115 
       
   116     public static void checkDummyNetworkInterface() throws IOException {
       
   117 
       
   118         try(MulticastSocket ms = new MulticastSocket()) {
       
   119 
       
   120             // getOption with no Network Interface set
       
   121             NetworkInterface n0 = ms.getOption(StandardSocketOptions.IP_MULTICAST_IF);
       
   122             if (n0 != null) {
       
   123                 throw new RuntimeException("NetworkInterface should be null");
       
   124             }
       
   125 
       
   126             // getNetworkInterface with no Network Interface set
       
   127             NetworkInterface n1 = ms.getNetworkInterface();
       
   128             if (n1 == null) {
       
   129                 throw new RuntimeException("getNetworkInterface() should not return null");
       
   130             } else if (!((n1.getName().equals("0.0.0.0") || n1.getName().equals("::"))
       
   131                     && (n1.getIndex() == 0)
       
   132                     && (n1.inetAddresses().count() == 1))) {
       
   133 
       
   134                 throw new RuntimeException("Dummy NetworkInterface not returned as expected");
       
   135             }
       
   136 
       
   137             // getInterface with no Network Interface set
       
   138             InetAddress iaddr = ms.getInterface();
       
   139             if (iaddr == null) {
       
   140                 throw new RuntimeException("getInterface() should not return null");
       
   141             } else if (!iaddr.isAnyLocalAddress()) {
       
   142                 throw new RuntimeException("getInterface() should return anyLocalAddress");
       
   143             }
       
   144         }
       
   145     }
       
   146 }
       
   147