/* * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @bug 8182672 * @summary Java 8u121 on Linux intermittently returns null for MAC address * */ import java.net.NetworkInterface; import java.util.ArrayList; import java.util.Enumeration; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class GetMacAddress implements Runnable { String threadName; NetworkInterface ni; int count; static volatile boolean failed = false; static final int NUM_THREADS = 5; static final int NUM_ITERS = 100; public GetMacAddress(NetworkInterface ni, String name) { this.ni = ni; this.threadName = name; } public void run() { try { for (int i = 0; i < NUM_ITERS; i++) { byte[] addr = ni.getHardwareAddress(); if (addr == null) { System.out.println("mac id is null: " + threadName); failed = true; } count = count + 1; if (count % 100 == 0) { System.out.println("Count - " + count + " - " + threadName); } } } catch (Exception ex) { System.out.println("Not expecting exception:"); ex.printStackTrace(); failed = true; } } public static void main(String[] args) throws Exception { ArrayList toTest = new ArrayList<>(); ExecutorService executor; Enumeration e = NetworkInterface.getNetworkInterfaces(); // first, build a list of interfaces which we can test with while (e.hasMoreElements()) { NetworkInterface ni = e.nextElement(); try { byte[] addr = ni.getHardwareAddress(); if (addr == null ) { System.out.println("Not testing null addr: " + ni.getName()); } else { toTest.add(ni); } } catch (Exception ex) { System.out.println("Not testing: " + ni.getName() + " " + ex.getMessage()); } } for (NetworkInterface ni : toTest) { executor = Executors.newFixedThreadPool(NUM_THREADS); System.out.println("Testing: " + ni.getName()); for (int i = 0; i < NUM_THREADS; i++) { Runnable testworker = new GetMacAddress(ni, ni.getName() +"-Thread-" + i); executor.execute(testworker); } executor.shutdown(); while (!executor.isTerminated()) { } } if (!failed) { System.out.println("Finished all threads"); } else { throw new RuntimeException("Failed"); } } }