19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 * or visit www.oracle.com if you need additional information or have any |
20 * or visit www.oracle.com if you need additional information or have any |
21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
|
24 import java.io.IOException; |
|
25 import java.io.UncheckedIOException; |
|
26 import java.net.MulticastSocket; |
|
27 import java.net.NetworkInterface; |
24 |
28 |
25 /* |
29 import jdk.test.lib.NetworkConfiguration; |
|
30 |
|
31 /** |
26 * @test |
32 * @test |
27 * @bug 6458027 |
33 * @bug 6458027 |
28 * @summary Disabling IPv6 on a specific network interface causes problems. |
34 * @summary Disabling IPv6 on a specific network interface causes problems. |
29 * |
35 * @library /test/lib |
30 */ |
36 * @build jdk.test.lib.NetworkConfiguration |
31 |
37 * jdk.test.lib.Platform |
32 import java.io.IOException; |
38 * @run main SetGetNetworkInterfaceTest |
33 import java.net.InetAddress; |
39 * @run main/othervm -Djava.net.preferIPv4Stack=true SetGetNetworkInterfaceTest |
34 import java.net.MulticastSocket; |
40 */ |
35 import java.net.NetworkInterface; |
41 public class SetGetNetworkInterfaceTest { |
36 import java.net.SocketException; |
|
37 import java.util.Arrays; |
|
38 import java.util.Enumeration; |
|
39 |
|
40 |
|
41 public class SetGetNetworkInterfaceTest { |
|
42 |
42 |
43 public static void main(String[] args) throws Exception { |
43 public static void main(String[] args) throws Exception { |
44 |
44 NetworkConfiguration nc = NetworkConfiguration.probe(); |
45 boolean passed = true; |
45 try (MulticastSocket ms = new MulticastSocket()) { |
46 try { |
46 nc.multicastInterfaces(true).forEach(nif -> setGetNetworkInterface(ms, nif)); |
47 MulticastSocket ms = new MulticastSocket(); |
|
48 Enumeration<NetworkInterface> networkInterfaces = NetworkInterface |
|
49 .getNetworkInterfaces(); |
|
50 while (networkInterfaces.hasMoreElements()) { |
|
51 NetworkInterface netIf = networkInterfaces.nextElement(); |
|
52 if (isNetworkInterfaceTestable(netIf)) { |
|
53 printNetIfDetails(netIf); |
|
54 ms.setNetworkInterface(netIf); |
|
55 NetworkInterface msNetIf = ms.getNetworkInterface(); |
|
56 if (netIf.equals(msNetIf)) { |
|
57 System.out.println(" OK"); |
|
58 } else { |
|
59 System.out.println("FAILED!!!"); |
|
60 printNetIfDetails(msNetIf); |
|
61 passed = false; |
|
62 } |
|
63 System.out.println("------------------"); |
|
64 } |
|
65 } |
|
66 } catch (IOException e) { |
47 } catch (IOException e) { |
67 e.printStackTrace(); |
48 e.printStackTrace(); |
68 passed = false; |
|
69 } |
49 } |
70 if (!passed) { |
50 System.out.println("Test passed."); |
71 throw new RuntimeException("Test Fail"); |
|
72 } |
|
73 System.out.println("Test passed "); |
|
74 } |
51 } |
75 |
52 |
76 private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception { |
53 static void setGetNetworkInterface(MulticastSocket ms, NetworkInterface nif) { |
77 System.out.println("checking netif == " + netIf.getName()); |
54 try { |
78 return (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf)); |
55 System.out.println(NetworkConfiguration.interfaceInformation(nif)); |
79 } |
56 ms.setNetworkInterface(nif); |
80 |
57 NetworkInterface msNetIf = ms.getNetworkInterface(); |
81 private static boolean isIpAddrAvailable (NetworkInterface netIf) { |
58 if (nif.equals(msNetIf)) { |
82 boolean ipAddrAvailable = false; |
59 System.out.println(" OK"); |
83 byte[] nullIpAddr = {'0', '0', '0', '0'}; |
|
84 byte[] testIpAddr = null; |
|
85 |
|
86 Enumeration<InetAddress> ipAddresses = netIf.getInetAddresses(); |
|
87 while (ipAddresses.hasMoreElements()) { |
|
88 InetAddress testAddr = ipAddresses.nextElement(); |
|
89 testIpAddr = testAddr.getAddress(); |
|
90 if ((testIpAddr != null) && (!Arrays.equals(testIpAddr, nullIpAddr))) { |
|
91 ipAddrAvailable = true; |
|
92 break; |
|
93 } else { |
60 } else { |
94 System.out.println("ignore netif " + netIf.getName()); |
61 System.out.println("FAILED!!!"); |
|
62 System.out.println(NetworkConfiguration.interfaceInformation(msNetIf)); |
|
63 throw new RuntimeException("Test Fail"); |
95 } |
64 } |
|
65 System.out.println("------------------"); |
|
66 } catch (IOException e) { |
|
67 throw new UncheckedIOException(e); |
96 } |
68 } |
97 return ipAddrAvailable; |
|
98 } |
|
99 |
|
100 private static void printNetIfDetails(NetworkInterface ni) |
|
101 throws SocketException { |
|
102 System.out.println("Name " + ni.getName() + " index " + ni.getIndex()); |
|
103 Enumeration<InetAddress> en = ni.getInetAddresses(); |
|
104 while (en.hasMoreElements()) { |
|
105 System.out.println(" InetAdress: " + en.nextElement()); |
|
106 } |
|
107 System.out.println("HardwareAddress: " + createMacAddrString(ni)); |
|
108 System.out.println("loopback: " + ni.isLoopback() + "; pointToPoint: " |
|
109 + ni.isPointToPoint() + "; virtual: " + ni.isVirtual() |
|
110 + "; MTU: " + ni.getMTU()); |
|
111 } |
|
112 |
|
113 private static String createMacAddrString(NetworkInterface netIf) |
|
114 throws SocketException { |
|
115 byte[] macAddr = netIf.getHardwareAddress(); |
|
116 StringBuilder sb = new StringBuilder(); |
|
117 if (macAddr != null) { |
|
118 for (int i = 0; i < macAddr.length; i++) { |
|
119 sb.append(String.format("%02X%s", macAddr[i], |
|
120 (i < macAddr.length - 1) ? "-" : "")); |
|
121 } |
|
122 } |
|
123 return sb.toString(); |
|
124 } |
69 } |
125 } |
70 } |