Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/MulticastSocket/TestInterfaces.java
38811 views
/*1* Copyright (c) 2001, 2013, 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*/29import java.net.*;30import java.util.Enumeration;31import java.io.IOException;3233public class TestInterfaces {3435static final boolean isWindows = System.getProperty("os.name").startsWith("Windows");3637public static void main(String args[]) throws Exception {38int failures = 0;3940MulticastSocket soc = new MulticastSocket();4142Enumeration nifs = NetworkInterface.getNetworkInterfaces();43while (nifs.hasMoreElements()) {44NetworkInterface ni = (NetworkInterface)nifs.nextElement();4546/*47* Test MulticastSocket.getInterface48*/49Enumeration addrs = ni.getInetAddresses();50while (addrs.hasMoreElements()) {51InetAddress ia = (InetAddress)addrs.nextElement();5253System.out.println("********************************");54System.out.println("MulticastSocket.setInterface(" + ia + ")");5556try {57soc.setInterface(ia);58} catch (IOException ioe) {59System.err.println("Can't set interface to: " + ia60+ " " + ioe.getMessage());61continue;62}6364InetAddress curr = soc.getInterface();65if (!curr.equals(ia)) {66System.err.println("MulticastSocket.getInterface returned: " + curr);67System.err.println("Failed! Expected: " + ia);68failures++;69} else {70System.out.println("Passed.");71}72}7374/*75* Test MulticastSocket.getNetworkInterface76*/77System.out.println("********************************");78System.out.println("MulticastSocket.setNetworkInterface(" +79ni.getName() + ")");8081try {82soc.setNetworkInterface(ni);83} catch (IOException ioe) {84System.err.println("Can't set interface to: " + ni.getName()85+ " " + ioe.getMessage());86continue;87}8889// JDK-8022963, Skip (Windows) Teredo Tunneling Pseudo-Interface90String dName = ni.getDisplayName();91if (isWindows && dName != null && dName.contains("Teredo"))92continue;9394NetworkInterface curr = soc.getNetworkInterface();95if (!curr.equals(ni)) {96System.err.println("MulticastSocket.getNetworkInterface returned: " + curr);97System.err.println("Failed! Expected: " + ni);98failures++;99} else {100System.out.println("Passed.");101}102103}104105if (failures > 0) {106System.out.println("********************************");107throw new Exception(failures + " test(s) failed!!!");108}109110}111112}113114115