Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/NetworkInterface/GetMacAddress.java
38811 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.Collections;32import java.util.List;33import java.util.concurrent.Callable;34import java.util.concurrent.ExecutorService;35import java.util.concurrent.Executors;36import java.util.concurrent.Future;37import java.util.concurrent.Phaser;38import java.util.function.Predicate;39import java.util.stream.Collectors;40import java.util.stream.Stream;4142public class GetMacAddress implements Callable<Exception> {43static final int NUM_THREADS = 5;44static final int NUM_ITERS = 100;45static volatile boolean failed; // false4647final String threadName;48final NetworkInterface ni;49final Phaser startingGate;5051public GetMacAddress(NetworkInterface ni, String name, Phaser phaser) {52this.ni = ni;53this.threadName = name;54this.startingGate = phaser;55}5657@Override58public Exception call() {59int count = 0;60startingGate.arriveAndAwaitAdvance();61try {62for (int i = 0; i < NUM_ITERS; i++) {63ni.getMTU();64byte[] addr = ni.getHardwareAddress();65if (addr == null) {66System.out.println(threadName + ". mac id is null");67failed = true;68}69count = count + 1;70if (count % 100 == 0) {71System.out.println(threadName + ". count is " + count);72}73}74} catch (Exception ex) {75System.out.println(threadName + ". Not expecting exception:" + ex.getMessage());76failed = true;77return ex;78}79return null;80}8182static final Predicate<NetworkInterface> hasHardwareAddress = ni -> {83try {84if (ni.getHardwareAddress() == null) {85System.out.println("Not testing null addr: " + ni.getName());86return false;87}88} catch (Exception ex) {89System.out.println("Not testing: " + ni.getName() +90" " + ex.getMessage());91return false;92}93return true;94};9596public static Stream<NetworkInterface> getNetworkInterfacesAsStream() throws Exception {97// JDK 9 and later98//return NetworkInterface.networkInterfaces();99// pre JDK 9100return Collections.list(NetworkInterface.getNetworkInterfaces()).stream();101}102103public static void main(String[] args) throws Exception {104List<NetworkInterface> toTest = getNetworkInterfacesAsStream()105.filter(hasHardwareAddress)106.collect(Collectors.toList());107108ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);109110for (NetworkInterface ni : toTest) {111Phaser startingGate = new Phaser(NUM_THREADS);112System.out.println("Testing: " + ni.getName());113List<Callable<Exception>> list = new ArrayList<>();114for (int i = 0; i < NUM_THREADS; i++)115list.add(new GetMacAddress(ni, ni.getName() + "-Thread-" + i, startingGate));116List<Future<Exception>> futures = executor.invokeAll(list);117for (Future<Exception> f : futures) {118if (f.get() != null)119f.get().printStackTrace(System.out);120}121if (failed)122break;123}124executor.shutdownNow();125if (!failed) {126System.out.println("PASSED - Finished all threads");127} else {128throw new RuntimeException("Failed");129}130}131}132133134