author | coleenp |
Wed, 17 Jun 2015 21:44:48 +0000 | |
changeset 31362 | 8957ccbb5821 |
parent 23045 | 949bd9e35147 |
permissions | -rw-r--r-- |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
21835
diff
changeset
|
2 |
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
4 |
* |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
8 |
* |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
13 |
* accompanied this code). |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
14 |
* |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
18 |
* |
5506 | 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 |
|
21 |
* questions. |
|
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
22 |
*/ |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
23 |
|
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
24 |
/* @test |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
25 |
* @bug 6717876 |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
26 |
* @summary Make java.net.NetworkInterface.getIndex() public |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
27 |
*/ |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
28 |
|
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
29 |
import java.net.*; |
19203
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
30 |
import java.util.Arrays; |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
31 |
import java.util.Collections; |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
32 |
import java.util.Enumeration; |
19203
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
33 |
import static java.lang.System.out; |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
34 |
|
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
35 |
public class IndexTest { |
21835 | 36 |
static final boolean isWindows = System.getProperty("os.name").startsWith("Windows"); |
37 |
||
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
38 |
public static void main(String[] args) throws Exception { |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
39 |
Enumeration<NetworkInterface> netifs = NetworkInterface.getNetworkInterfaces(); |
21835 | 40 |
NetworkInterface nif; |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
41 |
while (netifs.hasMoreElements()) { |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
42 |
nif = netifs.nextElement(); |
23045
949bd9e35147
8035633: TEST_BUG: java/net/NetworkInterface/Equals.java and some tests failed on windows intermittently
ewang
parents:
23010
diff
changeset
|
43 |
// JDK-8022212, Skip (Windows) Teredo Tunneling Pseudo-Interface |
949bd9e35147
8035633: TEST_BUG: java/net/NetworkInterface/Equals.java and some tests failed on windows intermittently
ewang
parents:
23010
diff
changeset
|
44 |
String dName = nif.getDisplayName(); |
949bd9e35147
8035633: TEST_BUG: java/net/NetworkInterface/Equals.java and some tests failed on windows intermittently
ewang
parents:
23010
diff
changeset
|
45 |
if (isWindows && dName != null && dName.contains("Teredo")) |
21835 | 46 |
continue; |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
47 |
int index = nif.getIndex(); |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
48 |
if (index >= 0) { |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
49 |
NetworkInterface nif2 = NetworkInterface.getByIndex(index); |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
50 |
if (! nif.equals(nif2)) { |
19203
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
51 |
out.printf("%nExpected interfaces to be the same, but got:%n"); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
52 |
displayInterfaceInformation(nif); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
53 |
displayInterfaceInformation(nif2); |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
54 |
throw new RuntimeException("both interfaces should be equal"); |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
55 |
} |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
56 |
} |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
57 |
} |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
58 |
try { |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
59 |
nif = NetworkInterface.getByIndex(-1); |
19203
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
60 |
out.printf("%ngetByIndex(-1) should have thrown, but instead returned:%n"); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
61 |
displayInterfaceInformation(nif); |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
62 |
throw new RuntimeException("Should have thrown IllegalArgumentException"); |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
63 |
} catch (IllegalArgumentException e) { |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
64 |
// OK |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
65 |
} |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
66 |
// In all likelyhood, this interface should not exist. |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
67 |
nif = NetworkInterface.getByIndex(Integer.MAX_VALUE - 1); |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
68 |
if (nif != null) { |
19203
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
69 |
out.printf("%ngetByIndex(MAX_VALUE - 1), expected null, got:%n"); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
70 |
displayInterfaceInformation(nif); |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
71 |
throw new RuntimeException("getByIndex() should have returned null"); |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
72 |
} |
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
73 |
} |
19203
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
74 |
|
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
75 |
static void displayInterfaceInformation(NetworkInterface netint) throws SocketException { |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
76 |
out.printf("Display name: %s%n", netint.getDisplayName()); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
77 |
out.printf("Name: %s%n", netint.getName()); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
78 |
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
79 |
|
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
80 |
for (InetAddress inetAddress : Collections.list(inetAddresses)) |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
81 |
out.printf("InetAddress: %s%n", inetAddress); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
82 |
|
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
83 |
out.printf("Up? %s%n", netint.isUp()); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
84 |
out.printf("Loopback? %s%n", netint.isLoopback()); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
85 |
out.printf("PointToPoint? %s%n", netint.isPointToPoint()); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
86 |
out.printf("Supports multicast? %s%n", netint.supportsMulticast()); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
87 |
out.printf("Virtual? %s%n", netint.isVirtual()); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
88 |
out.printf("Hardware address: %s%n", |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
89 |
Arrays.toString(netint.getHardwareAddress())); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
90 |
out.printf("MTU: %s%n", netint.getMTU()); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
91 |
out.printf("Index: %s%n", netint.getIndex()); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
92 |
out.printf("%n"); |
ff02aa0199c3
8022344: Additional debug info for test/java/net/NetworkInterface/IndexTest.java
chegar
parents:
5506
diff
changeset
|
93 |
} |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
diff
changeset
|
94 |
} |