Path: blob/master/test/jdk/java/net/MulticastSocket/NoSetNetworkInterface.java
41152 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 823330726* @library /test/lib27* @run main/othervm NoSetNetworkInterface28* @run main/othervm -Djava.net.preferIPv4Stack=true NoSetNetworkInterface29* @run main/othervm -Djava.net.preferIPv6Addresses=true NoSetNetworkInterface30* @run main/othervm -Djdk.net.usePlainDatagramSocketImpl NoSetNetworkInterface31* @summary Check that methods that are used to set and get the NetworkInterface32* for a MulticastSocket work as expected. This test also checks that getOption33* returns null correctly when a NetworkInterface has not been set34*/3536import jdk.test.lib.NetworkConfiguration;3738import java.io.IOException;39import java.io.UncheckedIOException;40import java.net.InetAddress;41import java.net.MulticastSocket;42import java.net.NetworkInterface;43import java.net.StandardSocketOptions;44import java.util.Optional;45import java.util.function.Predicate;4647public class NoSetNetworkInterface {48public static void main(String[] args) throws Exception {4950NetworkConfiguration nc = NetworkConfiguration.probe();5152// check set and get methods work as expected53nc.multicastInterfaces(true).forEach(ni -> {54checkSetInterface(ni);55checkSetNetworkInterface(ni);56checkSetOption(ni);57});5859// Check that dummy NetworkInterface is returned when not set60checkDummyNetworkInterface();61}6263public static void checkSetInterface(NetworkInterface ni) {64try (MulticastSocket ms = new MulticastSocket()) {65Optional<InetAddress> iAddr = ni.inetAddresses()66.filter(Predicate.not(InetAddress::isAnyLocalAddress))67.findFirst();68if (iAddr.isPresent()) {69ms.setInterface(iAddr.get());70checkForCorrectNetworkInterface("setInterface", ms, ni);71}72} catch (IOException e) {73throw new UncheckedIOException(e);74}75}7677public static void checkSetNetworkInterface(NetworkInterface ni) {78try (MulticastSocket ms = new MulticastSocket()) {79ms.setNetworkInterface(ni);80checkForCorrectNetworkInterface("setNetworkInterface", ms, ni);81} catch (IOException e) {82throw new UncheckedIOException(e);83}84}8586public static void checkSetOption(NetworkInterface ni) {87try (MulticastSocket ms = new MulticastSocket()) {88ms.setOption(StandardSocketOptions.IP_MULTICAST_IF, ni);89checkForCorrectNetworkInterface("setOption", ms, ni);90} catch (IOException e) {91throw new UncheckedIOException(e);92}93}9495public static void checkForCorrectNetworkInterface(String setterMethod,96MulticastSocket ms,97NetworkInterface ni) throws IOException {9899// getInterface100InetAddress testAddr = ms.getInterface();101if (!ni.inetAddresses().anyMatch(i -> i.equals(testAddr))) {102throw new RuntimeException(setterMethod + " != getInterface");103}104105// getNetworkInterface106if (!ni.equals(ms.getNetworkInterface())) {107throw new RuntimeException(setterMethod + " != getNetworkInterface");108}109110// getOption111if (!ni.equals(ms.getOption(StandardSocketOptions.IP_MULTICAST_IF))) {112throw new RuntimeException(setterMethod + " != getOption");113}114}115116public static void checkDummyNetworkInterface() throws IOException {117118try(MulticastSocket ms = new MulticastSocket()) {119120// getOption with no Network Interface set121NetworkInterface n0 = ms.getOption(StandardSocketOptions.IP_MULTICAST_IF);122if (n0 != null) {123throw new RuntimeException("NetworkInterface should be null");124}125126// getNetworkInterface with no Network Interface set127NetworkInterface n1 = ms.getNetworkInterface();128if (n1 == null) {129throw 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))) {133134throw new RuntimeException("Dummy NetworkInterface not returned as expected");135}136137// getInterface with no Network Interface set138InetAddress iaddr = ms.getInterface();139if (iaddr == null) {140throw new RuntimeException("getInterface() should not return null");141} else if (!iaddr.isAnyLocalAddress()) {142throw new RuntimeException("getInterface() should return anyLocalAddress");143}144}145}146}147148149150