Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/NetworkInterface/NetworkInterfaceStreamTest.java
41149 views
1
/*
2
* Copyright (c) 2015, 2017, 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 8081678 8131155
26
* @summary Tests for stream returning methods
27
* @library /lib/testlibrary/bootlib /test/lib
28
* @build java.base/java.util.stream.OpTestCase
29
* @run testng/othervm NetworkInterfaceStreamTest
30
* @run testng/othervm -Djava.net.preferIPv4Stack=true NetworkInterfaceStreamTest
31
*/
32
33
import org.testng.annotations.BeforeTest;
34
import org.testng.annotations.Test;
35
36
import java.net.InetAddress;
37
import java.net.NetworkInterface;
38
import java.net.SocketException;
39
import java.util.ArrayList;
40
import java.util.Collection;
41
import java.util.Collections;
42
import java.util.function.Supplier;
43
import java.util.stream.OpTestCase;
44
import java.util.stream.Stream;
45
import java.util.stream.TestData;
46
47
import jdk.test.lib.net.IPSupport;
48
49
public class NetworkInterfaceStreamTest extends OpTestCase {
50
51
private final static boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");
52
53
@BeforeTest
54
void setup() {
55
IPSupport.throwSkippedExceptionIfNonOperational();
56
}
57
58
@Test
59
public void testNetworkInterfaces() throws SocketException {
60
Supplier<Stream<NetworkInterface>> ss = () -> {
61
try {
62
return NetworkInterface.networkInterfaces()
63
.filter(ni -> isIncluded(ni));
64
}
65
catch (SocketException e) {
66
throw new RuntimeException(e);
67
}
68
};
69
70
Collection<NetworkInterface> enums = Collections.list(NetworkInterface.getNetworkInterfaces());
71
Collection<NetworkInterface> expected = new ArrayList<>();
72
enums.forEach(ni -> {
73
if (isIncluded(ni)) {
74
expected.add(ni);
75
}
76
});
77
withData(TestData.Factory.ofSupplier("Top-level network interfaces", ss))
78
.stream(s -> s)
79
.expectedResult(expected)
80
.exercise();
81
}
82
83
private Collection<NetworkInterface> getAllNetworkInterfaces() throws SocketException {
84
Collection<NetworkInterface> anis = new ArrayList<>();
85
for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces())) {
86
getAllSubNetworkInterfaces(ni, anis);
87
}
88
return anis;
89
}
90
91
private void getAllSubNetworkInterfaces(NetworkInterface ni, Collection<NetworkInterface> result) {
92
if (isIncluded(ni)) {
93
result.add(ni);
94
}
95
96
for (NetworkInterface sni : Collections.list(ni.getSubInterfaces())) {
97
getAllSubNetworkInterfaces(sni, result);
98
}
99
}
100
101
private Stream<NetworkInterface> allNetworkInterfaces() throws SocketException {
102
return NetworkInterface.networkInterfaces()
103
.filter(ni -> isIncluded(ni))
104
.flatMap(this::allSubNetworkInterfaces);
105
}
106
107
private Stream<NetworkInterface> allSubNetworkInterfaces(NetworkInterface ni) {
108
return Stream.concat(
109
Stream.of(ni),
110
ni.subInterfaces().filter(sni -> isIncluded(sni)).flatMap(this::allSubNetworkInterfaces));
111
}
112
113
@Test
114
public void testSubNetworkInterfaces() throws SocketException {
115
Supplier<Stream<NetworkInterface>> ss = () -> {
116
try {
117
return allNetworkInterfaces();
118
}
119
catch (SocketException e) {
120
throw new RuntimeException(e);
121
}
122
};
123
124
Collection<NetworkInterface> expected = getAllNetworkInterfaces();
125
withData(TestData.Factory.ofSupplier("All network interfaces", ss))
126
.stream(s -> s)
127
.expectedResult(expected)
128
.exercise();
129
}
130
131
132
@Test
133
public void testInetAddresses() throws SocketException {
134
Supplier<Stream<InetAddress>> ss = () -> {
135
try {
136
return NetworkInterface.networkInterfaces()
137
.filter(ni -> isIncluded(ni))
138
.flatMap(NetworkInterface::inetAddresses);
139
}
140
catch (SocketException e) {
141
throw new RuntimeException(e);
142
}
143
};
144
145
Collection<NetworkInterface> nis = Collections.list(NetworkInterface.getNetworkInterfaces());
146
Collection<InetAddress> expected = new ArrayList<>();
147
for (NetworkInterface ni : nis) {
148
if (isIncluded(ni)) {
149
expected.addAll(Collections.list(ni.getInetAddresses()));
150
}
151
}
152
withData(TestData.Factory.ofSupplier("All inet addresses", ss))
153
.stream(s -> s)
154
.expectedResult(expected)
155
.exercise();
156
}
157
158
/**
159
* Check if the input network interface should be included in the test. It is necessary to exclude
160
* "Teredo Tunneling Pseudo-Interface" whose configuration can be variable during a test run.
161
*
162
* @param ni a network interace
163
* @return false if it is a "Teredo Tunneling Pseudo-Interface", otherwise true.
164
*/
165
private boolean isIncluded(NetworkInterface ni) {
166
if (!IS_WINDOWS) {
167
return true;
168
}
169
170
String dName = ni.getDisplayName();
171
return dName == null || !dName.contains("Teredo");
172
}
173
174
}
175
176
177