Path: blob/master/test/jdk/java/net/MulticastSocket/IPMulticastIF.java
41149 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*/2223import java.net.InetAddress;24import java.net.InetSocketAddress;25import java.net.MulticastSocket;26import java.net.NetworkInterface;27import java.util.ArrayList;28import java.util.List;29import jdk.test.lib.NetworkConfiguration;30import jdk.test.lib.net.IPSupport;31import org.testng.annotations.BeforeTest;32import org.testng.annotations.DataProvider;33import org.testng.annotations.Test;34import static java.lang.String.format;35import static java.lang.System.out;36import static java.net.StandardSocketOptions.IP_MULTICAST_IF;37import static java.util.stream.Collectors.toList;38import static org.testng.Assert.assertEquals;39import static org.testng.Assert.assertTrue;4041/**42* @test43* @bug 823644144* @summary Bound MulticastSocket fails when setting outbound interface on Windows45* @library /test/lib46* @run testng IPMulticastIF47* @run testng/othervm -Djava.net.preferIPv4Stack=true IPMulticastIF48* @run testng/othervm -Djava.net.preferIPv6Addresses=true IPMulticastIF49* @run testng/othervm -Djava.net.preferIPv6Addresses=true -Djava.net.preferIPv4Stack=true IPMulticastIF50*/51public class IPMulticastIF {5253@BeforeTest54public void sanity() {55IPSupport.throwSkippedExceptionIfNonOperational();56NetworkConfiguration.printSystemConfiguration(out);57}5859@DataProvider(name = "scenarios")60public Object[][] positive() throws Exception {61List<InetAddress> addrs = List.of(InetAddress.getLocalHost(),62InetAddress.getLoopbackAddress());63List<Object[]> list = new ArrayList<>();64NetworkConfiguration nc = NetworkConfiguration.probe();65addrs.stream().forEach(a -> nc.multicastInterfaces(true)66.map(nif -> new Object[] { new InetSocketAddress(a, 0), nif })67.forEach(list::add) );6869return list.stream().toArray(Object[][]::new);70}7172@Test(dataProvider = "scenarios")73public void testSetGetInterfaceBound(InetSocketAddress bindAddr, NetworkInterface nif)74throws Exception75{76out.println(format("\n\n--- testSetGetInterfaceBound bindAddr=[%s], nif=[%s]", bindAddr, nif));77try (MulticastSocket ms = new MulticastSocket(bindAddr)) {78ms.setNetworkInterface(nif);79NetworkInterface msNetIf = ms.getNetworkInterface();80assertEquals(msNetIf, nif);81}82}8384@Test(dataProvider = "scenarios")85public void testSetGetInterfaceUnbound(InetSocketAddress ignore, NetworkInterface nif)86throws Exception87{88out.println(format("\n\n--- testSetGetInterfaceUnbound nif=[%s]", nif));89try (MulticastSocket ms = new MulticastSocket()) {90ms.setNetworkInterface(nif);91NetworkInterface msNetIf = ms.getNetworkInterface();92assertEquals(msNetIf, nif);93}94}9596@Test(dataProvider = "scenarios")97public void testSetGetOptionBound(InetSocketAddress bindAddr, NetworkInterface nif)98throws Exception99{100out.println(format("\n\n--- testSetGetOptionBound bindAddr=[%s], nif=[%s]", bindAddr, nif));101try (MulticastSocket ms = new MulticastSocket(bindAddr)) {102ms.setOption(IP_MULTICAST_IF, nif);103NetworkInterface msNetIf = ms.getOption(IP_MULTICAST_IF);104assertEquals(msNetIf, nif);105}106}107108@Test(dataProvider = "scenarios")109public void testSetGetOptionUnbound(InetSocketAddress ignore, NetworkInterface nif)110throws Exception111{112out.println(format("\n\n--- testSetGetOptionUnbound nif=[%s]", nif));113try (MulticastSocket ms = new MulticastSocket()) {114ms.setOption(IP_MULTICAST_IF, nif);115NetworkInterface msNetIf = ms.getOption(IP_MULTICAST_IF);116assertEquals(msNetIf, nif);117}118}119120// -- get without set121122@DataProvider(name = "bindAddresses")123public Object[][] bindAddresses() throws Exception {124return new Object[][] {125{ new InetSocketAddress(InetAddress.getLocalHost(), 0) },126{ new InetSocketAddress(InetAddress.getLoopbackAddress(), 0) },127};128}129130@Test(dataProvider = "bindAddresses")131public void testGetInterfaceBound(InetSocketAddress bindAddr)132throws Exception133{134out.println(format("\n\n--- testGetInterfaceBound bindAddr=[%s]", bindAddr));135try (MulticastSocket ms = new MulticastSocket(bindAddr)) {136assertPlaceHolder(ms.getNetworkInterface());137}138}139140@Test141public void testGettInterfaceUnbound() throws Exception {142out.println("\n\n--- testGettInterfaceUnbound ");143try (MulticastSocket ms = new MulticastSocket()) {144assertPlaceHolder(ms.getNetworkInterface());145}146}147148@Test(dataProvider = "bindAddresses")149public void testGetOptionBound(InetSocketAddress bindAddr)150throws Exception151{152out.println(format("\n\n--- testGetOptionBound bindAddr=[%s]", bindAddr));153try (MulticastSocket ms = new MulticastSocket(bindAddr)) {154assertEquals(ms.getOption(IP_MULTICAST_IF), null);155}156}157158@Test159public void testGetOptionUnbound() throws Exception {160out.println("\n\n--- testGetOptionUnbound ");161try (MulticastSocket ms = new MulticastSocket()) {162assertEquals(ms.getOption(IP_MULTICAST_IF), null);163}164}165166// Asserts that the placeholder NetworkInterface has a single InetAddress167// that represent any local address.168static void assertPlaceHolder(NetworkInterface nif) {169List<InetAddress> addrs = nif.inetAddresses().collect(toList());170assertEquals(addrs.size(), 1);171assertTrue(addrs.get(0).isAnyLocalAddress());172}173}174175176