Path: blob/master/test/jdk/java/net/NetworkInterface/GetMacAddress.java
41149 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 818267226* @summary Java 8u121 on Linux intermittently returns null for MAC address27*/2829import java.net.NetworkInterface;30import java.util.ArrayList;31import java.util.List;32import java.util.concurrent.Callable;33import java.util.concurrent.ExecutorService;34import java.util.concurrent.Executors;35import java.util.concurrent.Future;36import java.util.concurrent.Phaser;37import java.util.function.Predicate;38import java.util.stream.Collectors;39import java.util.stream.Stream;4041public class GetMacAddress implements Callable<Exception> {42static final int NUM_THREADS = 5;43static final int NUM_ITERS = 100;44static volatile boolean failed; // false4546final String threadName;47final NetworkInterface ni;48final Phaser startingGate;4950public GetMacAddress(NetworkInterface ni, String name, Phaser phaser) {51this.ni = ni;52this.threadName = name;53this.startingGate = phaser;54}5556@Override57public Exception call() {58int count = 0;59startingGate.arriveAndAwaitAdvance();60try {61for (int i = 0; i < NUM_ITERS; i++) {62ni.getMTU();63byte[] addr = ni.getHardwareAddress();64if (addr == null) {65System.out.println(threadName + ". mac id is null");66failed = true;67}68count = count + 1;69if (count % 100 == 0) {70System.out.println(threadName + ". count is " + count);71}72}73} catch (Exception ex) {74System.out.println(threadName + ". Not expecting exception:" + ex.getMessage());75failed = true;76return ex;77}78return null;79}8081static final Predicate<NetworkInterface> hasHardwareAddress = ni -> {82try {83if (ni.getHardwareAddress() == null) {84System.out.println("Not testing null addr: " + ni.getName());85return false;86}87} catch (Exception ex) {88System.out.println("Not testing: " + ni.getName() +89" " + ex.getMessage());90return false;91}92return true;93};9495public static Stream<NetworkInterface> getNetworkInterfacesAsStream() throws Exception {96// JDK 9 and later97return NetworkInterface.networkInterfaces();98// pre JDK 999//return Collections.list(NetworkInterface.getNetworkInterfaces()).stream();100}101102public static void main(String[] args) throws Exception {103List<NetworkInterface> toTest = getNetworkInterfacesAsStream()104.filter(hasHardwareAddress)105.collect(Collectors.toList());106107ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);108109for (NetworkInterface ni : toTest) {110Phaser startingGate = new Phaser(NUM_THREADS);111System.out.println("Testing: " + ni.getName());112List<Callable<Exception>> list = new ArrayList<>();113for (int i = 0; i < NUM_THREADS; i++)114list.add(new GetMacAddress(ni, ni.getName() + "-Thread-" + i, startingGate));115List<Future<Exception>> futures = executor.invokeAll(list);116for (Future<Exception> f : futures) {117if (f.get() != null)118f.get().printStackTrace(System.out);119}120if (failed)121break;122}123executor.shutdownNow();124if (!failed) {125System.out.println("PASSED - Finished all threads");126} else {127throw new RuntimeException("Failed");128}129}130}131132133