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