Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/NetworkInterface/GetMacAddress.java
41149 views
1
/*
2
* Copyright (c) 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
/*
25
* @test
26
* @bug 8182672
27
* @summary Java 8u121 on Linux intermittently returns null for MAC address
28
*/
29
30
import java.net.NetworkInterface;
31
import java.util.ArrayList;
32
import java.util.List;
33
import java.util.concurrent.Callable;
34
import java.util.concurrent.ExecutorService;
35
import java.util.concurrent.Executors;
36
import java.util.concurrent.Future;
37
import java.util.concurrent.Phaser;
38
import java.util.function.Predicate;
39
import java.util.stream.Collectors;
40
import java.util.stream.Stream;
41
42
public class GetMacAddress implements Callable<Exception> {
43
static final int NUM_THREADS = 5;
44
static final int NUM_ITERS = 100;
45
static volatile boolean failed; // false
46
47
final String threadName;
48
final NetworkInterface ni;
49
final Phaser startingGate;
50
51
public GetMacAddress(NetworkInterface ni, String name, Phaser phaser) {
52
this.ni = ni;
53
this.threadName = name;
54
this.startingGate = phaser;
55
}
56
57
@Override
58
public Exception call() {
59
int count = 0;
60
startingGate.arriveAndAwaitAdvance();
61
try {
62
for (int i = 0; i < NUM_ITERS; i++) {
63
ni.getMTU();
64
byte[] addr = ni.getHardwareAddress();
65
if (addr == null) {
66
System.out.println(threadName + ". mac id is null");
67
failed = true;
68
}
69
count = count + 1;
70
if (count % 100 == 0) {
71
System.out.println(threadName + ". count is " + count);
72
}
73
}
74
} catch (Exception ex) {
75
System.out.println(threadName + ". Not expecting exception:" + ex.getMessage());
76
failed = true;
77
return ex;
78
}
79
return null;
80
}
81
82
static final Predicate<NetworkInterface> hasHardwareAddress = ni -> {
83
try {
84
if (ni.getHardwareAddress() == null) {
85
System.out.println("Not testing null addr: " + ni.getName());
86
return false;
87
}
88
} catch (Exception ex) {
89
System.out.println("Not testing: " + ni.getName() +
90
" " + ex.getMessage());
91
return false;
92
}
93
return true;
94
};
95
96
public static Stream<NetworkInterface> getNetworkInterfacesAsStream() throws Exception {
97
// JDK 9 and later
98
return NetworkInterface.networkInterfaces();
99
// pre JDK 9
100
//return Collections.list(NetworkInterface.getNetworkInterfaces()).stream();
101
}
102
103
public static void main(String[] args) throws Exception {
104
List<NetworkInterface> toTest = getNetworkInterfacesAsStream()
105
.filter(hasHardwareAddress)
106
.collect(Collectors.toList());
107
108
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
109
110
for (NetworkInterface ni : toTest) {
111
Phaser startingGate = new Phaser(NUM_THREADS);
112
System.out.println("Testing: " + ni.getName());
113
List<Callable<Exception>> list = new ArrayList<>();
114
for (int i = 0; i < NUM_THREADS; i++)
115
list.add(new GetMacAddress(ni, ni.getName() + "-Thread-" + i, startingGate));
116
List<Future<Exception>> futures = executor.invokeAll(list);
117
for (Future<Exception> f : futures) {
118
if (f.get() != null)
119
f.get().printStackTrace(System.out);
120
}
121
if (failed)
122
break;
123
}
124
executor.shutdownNow();
125
if (!failed) {
126
System.out.println("PASSED - Finished all threads");
127
} else {
128
throw new RuntimeException("Failed");
129
}
130
}
131
}
132
133