Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/MulticastSocket/SetGetNetworkInterfaceTest.java
38811 views
/*1* Copyright (c) 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*/222324/*25* @test26* @bug 645802727* @summary Disabling IPv6 on a specific network interface causes problems.28*29*/3031import java.io.IOException;32import java.net.InetAddress;33import java.net.MulticastSocket;34import java.net.NetworkInterface;35import java.net.SocketException;36import java.util.Arrays;37import java.util.Enumeration;383940public class SetGetNetworkInterfaceTest {4142public static void main(String[] args) throws Exception {4344boolean passed = true;45try {46MulticastSocket ms = new MulticastSocket();47Enumeration<NetworkInterface> networkInterfaces = NetworkInterface48.getNetworkInterfaces();49while (networkInterfaces.hasMoreElements()) {50NetworkInterface netIf = networkInterfaces.nextElement();51if (isNetworkInterfaceTestable(netIf)) {52printNetIfDetails(netIf);53ms.setNetworkInterface(netIf);54NetworkInterface msNetIf = ms.getNetworkInterface();55if (netIf.equals(msNetIf)) {56System.out.println(" OK");57} else {58System.out.println("FAILED!!!");59printNetIfDetails(msNetIf);60passed = false;61}62System.out.println("------------------");63}64}65} catch (IOException e) {66e.printStackTrace();67passed = false;68}69if (!passed) {70throw new RuntimeException("Test Fail");71}72System.out.println("Test passed ");73}7475private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {76System.out.println("checking netif == " + netIf.getName());77return (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));78}7980private static boolean isIpAddrAvailable (NetworkInterface netIf) {81boolean ipAddrAvailable = false;82byte[] nullIpAddr = {'0', '0', '0', '0'};83byte[] testIpAddr = null;8485Enumeration<InetAddress> ipAddresses = netIf.getInetAddresses();86while (ipAddresses.hasMoreElements()) {87InetAddress testAddr = ipAddresses.nextElement();88testIpAddr = testAddr.getAddress();89if ((testIpAddr != null) && (!Arrays.equals(testIpAddr, nullIpAddr))) {90ipAddrAvailable = true;91break;92} else {93System.out.println("ignore netif " + netIf.getName());94}95}96return ipAddrAvailable;97}9899private static void printNetIfDetails(NetworkInterface ni)100throws SocketException {101System.out.println("Name " + ni.getName() + " index " + ni.getIndex());102Enumeration<InetAddress> en = ni.getInetAddresses();103while (en.hasMoreElements()) {104System.out.println(" InetAdress: " + en.nextElement());105}106System.out.println("HardwareAddress: " + createMacAddrString(ni));107System.out.println("loopback: " + ni.isLoopback() + "; pointToPoint: "108+ ni.isPointToPoint() + "; virtual: " + ni.isVirtual()109+ "; MTU: " + ni.getMTU());110}111112private static String createMacAddrString(NetworkInterface netIf)113throws SocketException {114byte[] macAddr = netIf.getHardwareAddress();115StringBuilder sb = new StringBuilder();116if (macAddr != null) {117for (int i = 0; i < macAddr.length; i++) {118sb.append(String.format("%02X%s", macAddr[i],119(i < macAddr.length - 1) ? "-" : ""));120}121}122return sb.toString();123}124}125126127