Path: blob/master/test/jdk/java/net/MulticastSocket/TestInterfaces.java
41149 views
/*1* Copyright (c) 2001, 2017, 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 442212226* @summary Test that MulticastSocket.getInterface returns the27* same InetAddress set by MulticastSocket.setInterface28* @library /test/lib29* @build jdk.test.lib.NetworkConfiguration30* jdk.test.lib.Platform31* @run main TestInterfaces32*/33import jdk.test.lib.NetworkConfiguration;3435import java.net.*;36import java.util.Arrays;37import java.util.Collections;38import java.util.Enumeration;39import java.io.IOException;4041public class TestInterfaces {4243static final boolean isWindows = System.getProperty("os.name").startsWith("Windows");4445public static void main(String args[]) throws Exception {46int failures = 0;4748MulticastSocket soc = new MulticastSocket();4950Enumeration nifs = NetworkInterface.getNetworkInterfaces();51while (nifs.hasMoreElements()) {52NetworkInterface ni = (NetworkInterface)nifs.nextElement();5354// JDK-8022963, Skip (Windows) Teredo Tunneling Pseudo-Interface55String dName = ni.getDisplayName();56if (isWindows && dName != null && dName.contains("Teredo"))57continue;5859// Skip those interfaces not up or not support multicast60if (!ni.isUp() || !ni.supportsMulticast())61continue;6263/*64* Test MulticastSocket.getInterface65*/66System.out.println("Testing network interface " + ni);67Enumeration addrs = ni.getInetAddresses();68while (addrs.hasMoreElements()) {69InetAddress ia = (InetAddress)addrs.nextElement();7071System.out.println("********************************");72System.out.println("MulticastSocket.setInterface(" + ia + ")");7374try {75soc.setInterface(ia);76} catch (IOException ioe) {77System.err.println("Can't set interface to: " + ia78+ " " + ioe.getMessage());79continue;80}8182InetAddress curr = soc.getInterface();83if (!curr.equals(ia)) {84System.err.println("NetworkInterface under test " + ni);85displayInterfaceInformation(ni);86System.err.println("MulticastSocket.getInterface returned: " + curr);87System.err.println("Failed! Expected: " + ia);88failures++;89} else {90System.out.println("Passed.");91}92}9394/*95* Test MulticastSocket.getNetworkInterface96*/97System.out.println("********************************");98System.out.println("MulticastSocket.setNetworkInterface(" +99ni.getName() + ")");100101try {102soc.setNetworkInterface(ni);103} catch (IOException ioe) {104System.err.println("Can't set interface to: " + ni.getName()105+ " " + ioe.getMessage());106continue;107}108109110NetworkInterface curr = soc.getNetworkInterface();111if (!curr.equals(ni)) {112System.err.println("MulticastSocket.getNetworkInterface returned: " + curr);113System.err.println("Failed! Expected: " + ni);114System.err.println("NetworkInterface details for curr variable ");115displayInterfaceInformation(curr);116System.err.println("NetworkInterface details for ni variable ");117displayInterfaceInformation(ni) ;118failures++;119} else {120System.out.println("Passed.");121}122123}124125if (failures > 0) {126System.err.println("********************************");127NetworkConfiguration.printSystemConfiguration(System.err);128System.out.println("********************************");129throw new Exception(failures + " test(s) failed!!!");130}131132}133134static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {135System.err.println("Display name: " + netint.getDisplayName());136System.err.println("Name: " + netint.getName());137Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();138139for (InetAddress inetAddress : Collections.list(inetAddresses))140System.err.println("InetAddress: " + inetAddress);141142System.err.println("Up? " + netint.isUp());143System.err.println("Loopback? " + netint.isLoopback());144System.err.println("PointToPoint? " + netint.isPointToPoint());145System.err.println("Supports multicast? " + netint.supportsMulticast());146System.err.println("Virtual? " + netint.isVirtual());147System.err.println("Hardware address: " +148Arrays.toString(netint.getHardwareAddress()));149System.err.println("MTU: " + netint.getMTU());150System.err.println("Index: " + netint.getIndex());151System.err.println();152}153}154155156